"Assertion failed: you need to wait until it is ready to run." Error while calling C function in JavaScript

I tried a simple example to call a C function compiled into .wasm with JavaScript.

This is the file counter.c:

#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.jsJavaScript file:

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

My index.htmlfile only loads .js files into the body, nothing more:

<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 until the runtime is ready?

+5
source share
2 answers

I found a quick solution. I needed to change main.jsto:

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

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

Module, counter.js script, emscripten.

+5

, ". , , ?", , C/++, javascript API C/++ Javascript :

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

, , .

+1

All Articles