Created
August 14, 2019 14:01
-
-
Save bakpakin/c20ddfc8b4d58c3cef4969d4153e86e4 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
static JanetFunction *global_cb = NULL; | |
Janet register_callback(int32_t argc, Janet *argv) { | |
janet_fixarity(argc, 1); | |
JanetFunction *cb = janet_getfunciton(argv, 0); | |
global_cb = cb; | |
// Make sure the callback is not garbage collected while waiting | |
janet_gcroot(argv[0]); | |
return janet_wrap_nil(); | |
} | |
void some_cb_handler(void) { | |
int32_t argc = 1; | |
Janet argv[1]; | |
argv[0] = janet_ckeywordv("some-arg"); | |
Janet return_value; | |
JanetFiber *fiber_used = NULL; | |
JanetSignal signal = janet_pcall(global_cb, argc, argv, &return_value, &fiber_used); | |
if (signal != JANET_SIGNAL_OK) { | |
janet_stacktrace(fiber_used, return_value); | |
} else { | |
// Everything went well, use return value as you want. The fiber fiber_used should be dead, though. | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment