How to deal with: "You need to wait for the runtime to be ready"

2,269 views
Skip to first unread message

Stefano Sabatini

unread,
Feb 24, 2015, 5:21:10 AM2/24/15
to emscripten Mailing List
Hi.

Even after reading this:
http://kripken.github.io/emscripten-site/docs/getting_started/FAQ.html#how-can-i-tell-when-the-page-is-fully-loaded-and-it-is-safe-to-call-compiled-functions

I'm not sure how to deal with the error:

"run() called, but dependencies remain, so not running" avc-codec.js:4
"Assertion failed: you need to wait for the runtime to be ready
(e.g. wait for main() to be called)"


Consider this simple example:
---------------8<------------------------- hello.c
#include <stdio.h>

void foo(void)
{
printf("foo\n");
}

void bar(void)
{
printf("bar\n");
}

---------------8<---------------------------------

Compile with:
/path/to/emcc hello.c -s NO_EXIT_RUNTIME=1 -s ASSERTIONS=1 -s EXPORTED_FUNCTIONS="['_foo', '_bar']" -O2 -o hello.js

---------------8<------------------------- hello.html
<!doctype html>
<html lang="en-us">
<head>
<meta charset="utf-8">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Emscripten-Generated Code</title>
<script type="text/javascript" src="hello.js"></script>
<script type="text/javascript">
_foo();
_bar();
</script>
</body>
</html>
-------------8<---------------------------------------

Now if I run the file from Firefox I get:
TypeError: asm.js type error: Disabled by debugger hello.js
uncaught exception: abort() at jsStackTrace@file:///home/stefano/src/Sandbox/JavaScript/emscripten/hello.js:1:18829
stackTrace@file:///home/stefano/src/Sandbox/JavaScript/emscripten/hello.js:1:19012
abort@file:///home/stefano/src/Sandbox/JavaScript/emscripten/hello.js:12:5432
assert@file:///home/stefano/src/Sandbox/JavaScript/emscripten/hello.js:1:8677
asm._foo@file:///home/stefano/src/Sandbox/JavaScript/emscripten/hello.js:12:727
@file:///home/stefano/src/Sandbox/JavaScript/emscripten/hello.html:9:7

"run() called, but dependencies remain, so not running" hello.js:1
"Assertion failed: you need to wait for the runtime to be ready (e.g. wait for main() to be called)" hello.js:1

[Note also that with -O1 this won't happen.]

It's not really clear to me how to apply the solutions suggested in
the FAQ.

|The easiest way to find out when loading is complete is to add a
|main() function, and within it call a JavaScript function to notify
|your code that loading is complete.

Consider this one:
---------------8<------------------------- hello2.c
#include <stdio.h>
#include <emscripten.h>

void foo(void)
{
printf("foo\n");
}

void bar(void)
{
printf("bar\n");
}

int main() {
EM_ASM( allReady() );
return 0;
}
---------------8<-------------------------------------

---------------8<------------------------- hello2.html
<!doctype html>
<html lang="en-us">
<head>
<meta charset="utf-8">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Emscripten-Generated Code</title>
<script type="text/javascript">

function allReady() {
_foo();
_bar();
}
</script>

<script type="text/javascript" src="hello2.js"></script>
</script>
</body>
</html>
---------------8<-----------------------------------

Compiling it with:
/path/to/emcc hello2.c -s NO_EXIT_RUNTIME=0 -s ASSERTIONS=1 -s EXPORTED_FUNCTIONS="['_foo', '_bar']" -O2 -o hello2.js

I still get:
"run() called, but dependencies remain, so not running" hello.js:1
"pre-main prep time: 26 ms" hello.js:1

and no main() function nor allReady() function is called.

[Same effect if I switch to -O1.]

What am I missing?

TIA

Alon Zakai

unread,
Feb 24, 2015, 4:29:23 PM2/24/15
to emscripte...@googlegroups.com
You probably want something like this:

---------------8<------------------------- hello.html
    <script type="text/javascript">
        var Module = { onRuntimeInitialized: function() {
          _foo();
          _bar();
        } };
    </script>
    <script type="text/javascript" src="hello.js"></script>
-------------8<---------------------------------------

Note how Module.onRuntimeInitialized is defined before the script is loaded. You could also put that in a --pre-js.

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

Stefano Sabatini

unread,
Feb 25, 2015, 5:47:06 AM2/25/15
to emscripte...@googlegroups.com
On date Tuesday 2015-02-24 13:29:20 -0800, Alon Zakai wrote:
> You probably want something like this:
>
> ---------------8<------------------------- hello.html
> <script type="text/javascript">
> var Module = { onRuntimeInitialized: function() {
> _foo();
> _bar();
> } };
> </script>
> <script type="text/javascript" src="hello.js"></script>
> -------------8<---------------------------------------
>
> Note how Module.onRuntimeInitialized is defined before the script is
> loaded. You could also put that in a --pre-js.

This seems to work fine. But I noticed an issue when compiling with
--closure 1.

hello.c:---------------------8<----------------------------------
#include <stdio.h>
#include <emscripten.h>

void foo(void)
{
printf("foo\n");
}

int main() {
return 0;
}
hello.c:---------------------8<----------------------------------

hello.html:------------------8<----------------------------------
<!doctype html>
<html lang="en-us">
<head>
<meta charset="utf-8">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Emscripten Test</title>
<script type="text/javascript">
var Module = {
onRuntimeInitialized: function() {
_foo();
}
}
</script>
<script type="text/javascript" src="hello.js"></script>
</body>
</html>
hello.html:------------------8<----------------------------------

compiled with:
EMCC hello.c -s NO_EXIT_RUNTIME=0 -s ASSERTIONS=1 -s EXPORTED_FUNCTIONS="['_foo']" --closure 1 -O2 -o hello.js

"run() called, but dependencies remain, so not running" hello.js:4
"pre-main prep time: 23 ms" hello.js:4
ReferenceError: _foo is not defined

This works otherwise with --closure 0 -O2 or --closure 1 -O1.

Alon Zakai

unread,
Feb 25, 2015, 2:28:46 PM2/25/15
to emscripte...@googlegroups.com
Calling _foo directly happened to work before because the locals in the emscripten output are all globals, if you don't use MODULARIZE or manually put things in a function scope etc. But on closure, it minifies locals. The proper way to use things is on Module, where we explicitly export symbols.

- Alon


Stefano Sabatini

unread,
Feb 26, 2015, 6:18:28 AM2/26/15
to emscripte...@googlegroups.com
On date Wednesday 2015-02-25 11:28:44 -0800, Alon Zakai wrote:
> You need `Module._foo` there. I added that now to the example on
> http://kripken.github.io/emscripten-site/docs/getting_started/FAQ.html#how-can-i-tell-when-the-page-is-fully-loaded-and-it-is-safe-to-call-compiled-functions
>
> Calling _foo directly happened to work before because the locals in the
> emscripten output are all globals, if you don't use MODULARIZE or manually
> put things in a function scope etc. But on closure, it minifies locals. The
> proper way to use things is on Module, where we explicitly export symbols.

Thanks Alon, it works fine with:

hello.html -----------------------8<--------------------------------
<!doctype html>
<html lang="en-us">
<head>
<meta charset="utf-8">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Emscripten Test</title>
<script type="text/javascript">
var Module = {
onRuntimeInitialized: function() {
Module._foo();
}
}
</script>
<script type="text/javascript" src="hello.js"></script>

</body>
</html>
hello.html -----------------------8<--------------------------------

I have still another problem related to --closure 1 and worker code,
I'll create a different thread for that.

Thank you again!
Reply all
Reply to author
Forward
0 new messages