Emscripten js library initialization

800 views
Skip to first unread message

Alexandre Perrot

unread,
May 22, 2015, 6:01:06 AM5/22/15
to emscripte...@googlegroups.com
Hello,

I have made my own js library using emscripten and I don't really know how to manage initializatoin to be user-friendly.

I use -s MODULARIZE=1 and  EXPORT_NAME=My_lib, so the generated code only gives a function named My_lib.
So, the user has to call the function himself and store the result :
var my_lib = My_lib();

I use embind, so after this function has exited, the library is not yet ready since global initializers need to be run to init bindings.

My question is how can I make sure everything is ready after calling an init function ?
The objective is to have a user friendly way of initalizing the library, such as :

var my_lib = My_lib();
my_lib.init()
do_something();

Wheras for now, I have to rely on 'postRun' to do that, which requires users to know about emscripten :

var my_lib = My_lib({'postRun' : do_something});

Brion Vibber

unread,
May 22, 2015, 7:21:03 AM5/22/15
to emscripten Mailing List
You can prepend or append additional JavaScript code to the generated module by specifying a file via the --pre-js or --post-js options on emcc (or by just taking the output JS and manually appending to it). This should make it fairly straightforward to set up initialization, such as by setting module options in a --pre-js, or creating a wrapper function that does the async setup for you.

Another thing that may help: if asynchronous setup of the module doesn't fit well with your code model, you can disable the memory file with "--memory-init-file 0" so all the code loading and initializers run immediately. This will however increase the total size of your code, as the data segments will be included as an array in the JS instead of a separate data file.

-- brion vibber (brion @ pobox.com / bvibber @ wikimedia.org)


--
You received this message because you are subscribed to the Google Groups "emscripten-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to emscripten-disc...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Alexandre Perrot

unread,
May 22, 2015, 7:51:54 AM5/22/15
to emscripte...@googlegroups.com
Hi Brion,


Le vendredi 22 mai 2015 13:21:03 UTC+2, Brion Vibber a écrit :
You can prepend or append additional JavaScript code to the generated module by specifying a file via the --pre-js or --post-js options on emcc (or by just taking the output JS and manually appending to it). This should make it fairly straightforward to set up initialization, such as by setting module options in a --pre-js, or creating a wrapper function that does the async setup for you.

I use this to add some utility functions to the Module. But this doesn't help, since the code is added inside the function generated by MODULARIZE
 
Another thing that may help: if asynchronous setup of the module doesn't fit well with your code model, you can disable the memory file with "--memory-init-file 0" so all the code loading and initializers run immediately. This will however increase the total size of your code, as the data segments will be included as an array in the JS instead of a separate data file.



This didn't directly work either. Even without a memory init file, global initializers (which setup embind bindings) are called asynchronously and i cannot directly call code using my_lib.
However, it works if you use the load event :
var my_lib = My_lib();
window.onload = do_something;

That's a start, thanks :)
How reliable do you think this is ?

Alexandre Perrot

unread,
May 22, 2015, 10:04:47 AM5/22/15
to emscripte...@googlegroups.com
Unfortunately, the onload event does not seem to work properly in Chrome.

In


var my_lib = My_lib();
window.onload = do_something;

onload is called before My_lib() is executed, resulting in an error.

matthias

unread,
Jun 8, 2015, 12:08:23 PM6/8/15
to emscripte...@googlegroups.com
Reply all
Reply to author
Forward
0 new messages