svaarala / duktape

Duktape - embeddable Javascript engine with a focus on portability and compact footprint

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Q: Example of using JS callbacks

mslijepc opened this issue · comments

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

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
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.

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

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

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 ...

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

and for what do you use specific_callback ?

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