Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"Assertion failed: you need to wait for the runtime to be ready" Error when calling a C function in JavaScript

I am trying out a simple example to call a C function compiled to .wasm with JavaScript.

This is the counter.c file:

#include <emscripten.h>

int counter = 100;

EMSCRIPTEN_KEEPALIVE
int count() {  
    counter += 1;
    return counter;
}

I compiled it using emcc counter.c -s WASM=1 -o counter.js.

My main.js JavaScript file:

const count = Module.cwrap('count ', 'number');
console.log(count());

My index.html file only loads both .js files in the body, nothing else:

<script type="text/javascript" src="counter.js"></script>
<script type="text/javascript" src="main.js"></script>

The error I am getting is:

Uncaught abort("Assertion failed: you need to wait for the runtime to be ready (e.g. wait for main() to be called)") at Error

when I try to call count() in main.js. How can I wait for the runtime to be ready?

like image 709
Kilian Batzner Avatar asked Sep 20 '17 21:09

Kilian Batzner


People also ask

How to handle error in async await in JavaScript?

Async Await Error Handling in JavaScript 1 try/catch. When you're first getting started with async/await, it is tempting to use try/catch around every async operation. 2 Golang in JS. Another common pattern is using .then () to convert a promise that rejects into a promise that fulfills with an error. 3 Using catch () on the Function Call. ...

How to handle unexpected errors in async functions?

In async functions, try/catch can help you recover gracefully from expected errors. But unexpected errors do happen, we all occasionally end up with a surprise "TypeError: Cannot read property 'foo' of null" sometimes. You should handle unexpected errors in your async functions in the calling function.

How to fix ReferenceError in JavaScript?

You may find the ReferenceError fixed itself as you fix JavaScript errors from your scripts. Make sure the script is loaded before the call. Finally, the function is not defined error can also be caused by calling the function before the script is loaded to the browser. Suppose you have a JavaScript file separated from your HTML file as follows:

What causes the function is not defined error in JavaScript?

Finally, the function is not defined error can also be caused by calling the function before the script is loaded to the browser. Suppose you have a JavaScript file separated from your HTML file as follows: Then you load the script into your HTML file, but you call the fnAction function before you load the script as follows:


2 Answers

I found a quick solution. I needed to modify main.js to:

Module['onRuntimeInitialized'] = onRuntimeInitialized;
const count = Module.cwrap('count ', 'number');

function onRuntimeInitialized() {
    console.log(count());
}

This alters the Module object that is defined in the counter.js script generated by emscripten.

like image 131
Kilian Batzner Avatar answered Oct 12 '22 11:10

Kilian Batzner


The other answer works, as specified here under the "How can I tell when the page is fully loaded and it is safe to call compiled functions?" header, where the article also mentions another way to wait to call code where you include a main function in your C/C++ code that calls a javascript function through the C/C++ to Javascript API like so:

#include <emscripten.h>
int main() {
    EM_ASM(const count = Module.cwrap('count ', 'number'); console.log(count()););
    return 0;
}

This works because the main function always executes when the runtime is initialized.

like image 2
Asher Mancinelli Avatar answered Oct 12 '22 11:10

Asher Mancinelli