Skip to content
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

Open
mslijepc opened this issue Mar 27, 2023 · 11 comments
Open

Q: Example of using JS callbacks #2533

mslijepc opened this issue Mar 27, 2023 · 11 comments

Comments

@mslijepc
Copy link

mslijepc commented Mar 27, 2023

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 and write functions are defined)

function f() {
  write(addr, data , function (err) {
    if (!err) {
      read(addr, function (err, data) {
        // do something
      }
    }
  }
}

Thanks

@hxorasani
Copy link

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?

@mslijepc
Copy link
Author

Exactly, and still haven't found any solution. You?

@hxorasani
Copy link

I think I prolly just did, #2400
I'm in the process of trying it out, I'm still trying to figure out how to iterate over the global duk stash,
without keeping track of the pushed functions in C code manually. I can do it using a C based hash map but if i can
avoid it, that'd be better.

lemme know if you get it working before me.

@mslijepc
Copy link
Author

Have you checked this? https://wiki.duktape.org/howtonativepersistentreferences

@hxorasani
Copy link

I just now skimmed through it, it's almost the same method as the one in #2400
I've been trying to integrate libuv in duktape (got it to work finally using dukluv)
but before that i tried the callback method using this and it worked, im gonna leave it here, maybe
someone else might find it useful as a starting point.

#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;
}

@mslijepc
Copy link
Author

mslijepc commented Apr 14, 2023

And how do you call it from main? Could you share an example.. How does your .js look like?

@hxorasani
Copy link

hxorasani commented Apr 15, 2023

i just pushed them as global functions in duk and use them like

add_callback(some_function);
...
on_tick() {
    process_callbacks();
}

in "main"

push_c_function("add_callback", add_callback);
...

on_tick comes from SDL or GLFW or ...

@mslijepc
Copy link
Author

If I understood well, some_function will be the callback for on_tick ?

@hxorasani
Copy link

@mslijepc yes! :)

@mslijepc
Copy link
Author

mslijepc commented Apr 20, 2023

and for what do you use specific_callback ?

@hxorasani
Copy link

to target specific callbacks and delete them without affecting the other callbacks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants