Skip to content

Instantly share code, notes, and snippets.

@bakpakin
Created August 14, 2019 14:01
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bakpakin/c20ddfc8b4d58c3cef4969d4153e86e4 to your computer and use it in GitHub Desktop.
Save bakpakin/c20ddfc8b4d58c3cef4969d4153e86e4 to your computer and use it in GitHub Desktop.
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