所以我正在尝试从 ArrayBuffer 创建 WebAssembly 模块。
C 代码:
#include <stdio.h>
int main() {
printf("hello, world!\n");
return 0;
}
我是这样编译的:
$ emcc -O2 hello.c -s WASM=1 -o hello.html
我启动了一个本地 http 服务器。 我尝试像这样在我的浏览器中加载它:
fetch('hello.wasm')
.then(res => res.arrayBuffer())
.then(buff => WebAssembly.Module(buff));
我收到以下错误:
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
我不知道这个错误是怎么回事,我无法通过网络搜索找到任何东西。
感谢任何帮助
谢谢!
最佳答案
WebAssembly.Module
是同步的,一些浏览器不允许主线程上有大型模块以避免编译阻塞主线程。
试试这个:
fetch('hello.wasm').then(response =>
response.arrayBuffer()
).then(buffer =>
WebAssembly.instantiate(buffer, importObj)
).then(({module, instance}) =>
instance.exports.f()
);
最好用WebAssembly.instantiate
因为它同时进行编译和实例化,并允许引擎保留在 importObject
以确保一切正常(尤其是 WebAssembly.Memory
)。
这里我假设您想要的不仅仅是main
,而是想要调用模块的导出函数f
。
关于javascript - 对于提供的参数,Wasm 编译超出了此上下文中的内部限制,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44507633/