Comments (11)
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.
Exactly, and still haven't found any solution. You?
from duktape.
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.
Have you checked this? https://wiki.duktape.org/howtonativepersistentreferences
from duktape.
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.
And how do you call it from main
? Could you share an example.. How does your .js
look like?
from duktape.
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.
If I understood well, some_function
will be the callback
for on_tick
?
from duktape.
@mslijepc yes! :)
from duktape.
and for what do you use specific_callback
?
from duktape.
to target specific callbacks and delete them without affecting the other callbacks.
from duktape.
Related Issues (20)
- Is there a way to build this on systems without `npm`? HOT 4
- Reading JS variable within C. HOT 3
- `duk_to_pointer` shows an use-after-free HOT 1
- Json Loop
- Embedding for rust. HOT 2
- Disable usage eval in Javascript code HOT 1
- Include test suite in release tarballs
- Does Buffer have plans to support more encodings?
- Crash Scenario (duk_dup_top and embedding JSON object data within itself) - Solved HOT 4
- webpacked "hello world" from Kotlin/JS erroring out with "empty expression not allowed" HOT 3
- [HELP] Build with emcc to execute wasm on nodejs
- Object.assign throws RangeError for a large number of arguments
- Embedding Duktape in Lazarus / Free Pascal
- A possible performance problem of calling sort function with long array
- Makefile.cmdline builds a broken binary that instantly segfaults
- dukluv forward compat HOT 3
- New release? 🤔 HOT 3
- loading handlebars helpers HOT 1
- Is nested duk_pcall_* from the same thread supported? HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from duktape.