javascript function callback in C code using Emscripten -
the task call javascript function callback in order show progress of while-loop operation. e.g. js:
var my_js_fn = function(curstate, maxstate){//int variables console.log(curstate.tostring() + " of " + maxstate.tostring()); }
c pseudocode:
int smth_that_calls_my_fn(int i, int max) { /* the_magic call my_js_fn() */ } int main(){ //.... while (i < max){ smth_that_calls_my_fn(i,max); } //.... return 0; }
how can link smth_that_calls_my_fn
, my_js_fn
?
the magic you're looking pretty simple -- need use em_asm_args macro.
specifically, can like
int smth_that_calls_my_fn(int i, int max) { em_asm_args({ my_js_fn($0, $1); }, i, max); }
make sure #include <emscripten.h>
in c file macro exists.
the em_asm_args macro takes javascript code (in braces) first argument, , other parameters want pass in. in js code, $0 first argument follow, $1 next , on.
i wrote blog entry going detail on topic if want more information: http://devosoft.org/an-introduction-to-web-development-with-emscripten/
Comments
Post a Comment