Giter Club home page Giter Club logo

Comments (11)

hxorasani avatar hxorasani commented on July 22, 2024

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?

from duktape.

mslijepc avatar mslijepc commented on July 22, 2024

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

from duktape.

hxorasani avatar hxorasani commented on July 22, 2024

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.

from duktape.

mslijepc avatar mslijepc commented on July 22, 2024

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

from duktape.

hxorasani avatar hxorasani commented on July 22, 2024

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

from duktape.

mslijepc avatar mslijepc commented on July 22, 2024

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

from duktape.

hxorasani avatar hxorasani commented on July 22, 2024

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

from duktape.

mslijepc avatar mslijepc commented on July 22, 2024

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

from duktape.

hxorasani avatar hxorasani commented on July 22, 2024

@mslijepc yes! :)

from duktape.

mslijepc avatar mslijepc commented on July 22, 2024

and for what do you use specific_callback ?

from duktape.

hxorasani avatar hxorasani commented on July 22, 2024

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

from duktape.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.