-
Notifications
You must be signed in to change notification settings - Fork 514
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Q: Example of using JS callbacks #2533
Comments
i was looking for the exact same solution! are you trying to have it where you pass a "callback" function to C code which is called later like an async Promise? |
Exactly, and still haven't found any solution. You? |
I think I prolly just did, #2400 lemme know if you get it working before me. |
Have you checked this? https://wiki.duktape.org/howtonativepersistentreferences |
I just now skimmed through it, it's almost the same method as the one in #2400 #define bindjs(n) duk_ret_t n (duk_context *J)
static long callbacks = 0;
bindjs (add_callback) {
const char *str = duk_get_string(J , 0); // 0
duk_push_global_stash(J); // 1 push the global stash to store the function object.
duk_push_number(J, callbacks); // 2 key
duk_dup(J, 0); // 3 duplicate the function argument so we can use duk_put_prop*
duk_put_prop(J, -3); // store the function with given key.
duk_pop(J); // remove the stash object.
callbacks++;
return 0;
}
bindjs (specific_callback) {
int n = duk_get_top(J);
if (n == 1) {
duk_push_global_stash(J); // 1 obj
duk_dup(J, 0); // 2
if (duk_get_prop(J, -2)) {
if (duk_is_function(J, -1)) {
duk_call(J, 0); // adapt with number of arguments.
duk_pop(J); // remove return value
} else printf("%s not a function\n", duk_get_string(J, -1));
duk_dup(J, 0); // 2
duk_del_prop(J, -2); // auto pops key from stack as well
} else printf("%s not found\n", duk_to_string(J, 0));
duk_pop(J); // remember to pop, regardless of whether or not present
duk_pop(J); // remove the stash object.
}
return 0;
}
bindjs (process_callbacks) {
duk_push_global_stash(J); // 1 obj
duk_enum(J, -1, DUK_ENUM_INCLUDE_NONENUMERABLE); // 2 enum
while (duk_next(J, -1 /*enum_idx*/, 1 /*get_value*/)) { // 3 key 4 val
if (duk_is_function(J, -1)) {
duk_call(J, 0); // adapt with number of arguments.
duk_pop(J); // remove return value
}
duk_del_prop(J, -3); // auto pops key from stack as well
}
duk_pop(J); // pop enum object
duk_pop(J); // remove the stash object.
print_duk_stack();
return 0;
} |
And how do you call it from |
i just pushed them as global functions in duk and use them like
in "main"
on_tick comes from SDL or GLFW or ... |
If I understood well, |
@mslijepc yes! :) |
and for what do you use |
to target specific callbacks and delete them without affecting the other callbacks. |
Hi,
is there some example on how we can use JS callbacks/async call with Duktape?
E.g. I would like C-code to call this script (
read
andwrite
functions are defined)Thanks
The text was updated successfully, but these errors were encountered: