Reputation: 2119
So I am trying to create a WebAssembly Module from an ArrayBuffer.
C code:
#include <stdio.h>
int main() {
printf("hello, world!\n");
return 0;
}
I compile it like so:
$ emcc -O2 hello.c -s WASM=1 -o hello.html
I start a local http server. And I try to load it in my browser like so:
fetch('hello.wasm')
.then(res => res.arrayBuffer())
.then(buff => WebAssembly.Module(buff));
And I get the following error:
Uncaught (in promise) RangeError: WebAssembly.Module(): Wasm compilation exceeds internal limits in this context for the provided arguments at fetch.then.then.buff (:1:77) at
I don't what to make of this error and I can't find anything via web searches.
Any help is kindly appreciated
Thanks!
Upvotes: 4
Views: 602
Reputation: 6853
WebAssembly.Module
is synchronous and some browsers don't allow large modules on the main thread to avoid having compilation block the main thread.
Try this instead:
fetch('hello.wasm').then(response =>
response.arrayBuffer()
).then(buffer =>
WebAssembly.instantiate(buffer, importObj)
).then(({module, instance}) =>
instance.exports.f()
);
It's better to use WebAssembly.instantiate
because it does compilation and instantiation together and allows the engine to keep at the importObject
to make sure things look OK (especially, the WebAssembly.Memory
).
Here I assume you want more than main
, and instead want to invoke your module's exported function f
.
Upvotes: 2