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?
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. ...
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.
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:
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:
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With