Recommended way to detect emscripten script has loaded ?

546 views
Skip to first unread message

Rodrigo Benenson

unread,
Aug 13, 2014, 5:39:39 PM8/13/14
to emscripte...@googlegroups.com
This is certainly a beginner's question.

I ported a library from C++ to javascript via emscripten.
The script works fine, but sometimes when internet is slow the user might launch call to the emscripten functions before they are defined.

What is the recommended way to detect when emscripten has finished loading ?
In other words, how to get a programmatic notification of the usual "pre-main prep time: 1792 ms" message ?
(note that this is a library, so no main function defined)

Thanks for you answers. 
I could not find an answer in the documentation, I hope it was not hidden somewhere out there.
Regards,
rodrigob.

Alon Zakai

unread,
Aug 13, 2014, 5:56:15 PM8/13/14
to emscripte...@googlegroups.com
We should document this better, yeah. I'll add something to the FAQ.

Basically, the easiest way is to just add a main() function. It will be called when all startup is done. You can make main call your JS (using EM_ASM for example) to notify it that the library is now safe to call.

- Alon



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

Rodrigo Benenson

unread,
Aug 14, 2014, 9:19:01 AM8/14/14
to emscripte...@googlegroups.com
What about listening to DOMContentLoaded, would that be an option ?

regards,
rodrigob.
> You received this message because you are subscribed to a topic in the
> Google Groups "emscripten-discuss" group.
> To unsubscribe from this topic, visit
> https://groups.google.com/d/topic/emscripten-discuss/Tf8ox1NEPXA/unsubscribe.
> To unsubscribe from this group and all its topics, send an email to

Rodrigo Benenson

unread,
Aug 14, 2014, 5:43:44 PM8/14/14
to emscripte...@googlegroups.com
Inspecting emscripten code I found Module.calledRun 
I could not find any documentation about it, but from the code it seems to be doing what I need.
Would this variable be suitable to monitor ?

Regards,
rodrigob.

Alon Zakai

unread,
Aug 14, 2014, 5:53:16 PM8/14/14
to emscripte...@googlegroups.com
DOMContentLoaded doesn't know about XHRs and such, which is what emscripten uses to load data files.

- Alon

Alon Zakai

unread,
Aug 14, 2014, 5:54:25 PM8/14/14
to emscripte...@googlegroups.com
In theory, yes, you can look at those variables. *However*, they are not documented on purpose, as we change those things over time, they are internal implementation details. The solution I suggested before, of waiting for main() to be called, is a behavior we are guaranteed *not* to change.

- Alon



--

Rodrigo Benenson

unread,
Aug 14, 2014, 7:27:05 PM8/14/14
to emscripte...@googlegroups.com
I just tried the "main()" strategy, but got "run() called, but
dependencies remain, so not running" (and my printf inside the main
did not appear in the javascript console).

Any idea of what this means ?

Regards,
rodrigob.
> You received this message because you are subscribed to a topic in the
> Google Groups "emscripten-discuss" group.
> To unsubscribe from this topic, visit
> https://groups.google.com/d/topic/emscripten-discuss/Tf8ox1NEPXA/unsubscribe.
> To unsubscribe from this group and all its topics, send an email to

Rodrigo Benenson

unread,
Aug 14, 2014, 7:53:40 PM8/14/14
to emscripte...@googlegroups.com
Sorry about the spam.
Figured out that I was missing to add '_main' to my EXPORTED_FUNCTIONS list.

Now the entry point works fine, but for some strange reason the other exported functions miss-behave when including main in my javascript library.

I am using
em++ the_main.cpp library_one.a library_two.a -s EXPORTED_FUNCTIONS="['_main', '_function_a', '_function_b']" --preload-file data -s DEMANGLE_SUPPORT=1 -o my_javascript_lib.js

is there anything obviously wrong ?

Using
em++ library_one.a library_two.a -s EXPORTED_FUNCTIONS="[ '_function_a', '_function_b']" --preload-file data -s DEMANGLE_SUPPORT=1 -o my_javascript_lib.js

and then the "calledRun fetching hack" works fine instead (later calls to function_a and function_b work as expected).

Regards,
rodrigob.

Alon Zakai

unread,
Aug 18, 2014, 7:18:40 PM8/18/14
to emscripte...@googlegroups.com
That is just a warning that main() is not running immediately. It will run later, after the asynchronous stuff it depends on has all arrived. You can ignore it.

- Alon

Alon Zakai

unread,
Aug 18, 2014, 7:19:46 PM8/18/14
to emscripte...@googlegroups.com
Nothing looks obviously wrong there. How do they misbehave?

- Alon



--

Rodrigo Benenson

unread,
Aug 27, 2014, 7:35:25 PM8/27/14
to emscripte...@googlegroups.com
This happened while preparing

http://rodrigob.github.io/html5_demonstrations/boundaries_estimation/boundaries_estimation_dart.html

the symptom was that then the functions would return a black image
(instead of the proper boundaries).
Since the code was unchanged, this behaviour was as if the functions
calling and/or memory allocation/reading/writing was not working
properly.
Since my "calledRun fetching hack" worked (and was a bit more
comfortable to use in my codebase), I did not look further into this.

Regards,
rodrigob.
> You received this message because you are subscribed to a topic in the
> Google Groups "emscripten-discuss" group.
> To unsubscribe from this topic, visit
> https://groups.google.com/d/topic/emscripten-discuss/Tf8ox1NEPXA/unsubscribe.
> To unsubscribe from this group and all its topics, send an email to
Reply all
Reply to author
Forward
0 new messages