Javascript Wasm编译超出了所提供参数在此上下文中的内部限制
因此,我试图从ArrayBuffer创建一个WebAssembly模块 C代码:Javascript Wasm编译超出了所提供参数在此上下文中的内部限制,javascript,c,webassembly,Javascript,C,Webassembly,因此,我试图从ArrayBuffer创建一个WebAssembly模块 C代码: #include <stdio.h> int main() { printf("hello, world!\n"); return 0; } 我启动一个本地http服务器。 我尝试将其加载到浏览器中,如下所示: fetch('hello.wasm') .then(res => res.arrayBuffer()) .then(buff => WebAssemb
#include <stdio.h>
int main() {
printf("hello, world!\n");
return 0;
}
我启动一个本地http服务器。
我尝试将其加载到浏览器中,如下所示:
fetch('hello.wasm')
.then(res => res.arrayBuffer())
.then(buff => WebAssembly.Module(buff));
我得到以下错误:
未捕获(承诺中)RangeError:WebAssembly.Module():Wasm编译超出了所提供参数在此上下文中的内部限制
在fetch.then.then.buff(:1:77)
在
我不知道该如何处理这个错误,也无法通过网络搜索找到任何东西
任何帮助都将不胜感激
谢谢
WebAssembly.Module
是同步的,有些浏览器不允许主线程上的大模块避免编译阻塞主线程
请尝试以下方法:
fetch('hello.wasm').then(response =>
response.arrayBuffer()
).then(buffer =>
WebAssembly.instantiate(buffer, importObj)
).then(({module, instance}) =>
instance.exports.f()
);
最好使用它,因为它同时进行编译和实例化,并允许引擎保持在importObject
以确保一切正常(尤其是WebAssembly.Memory
)
在这里,我假设您想要的不仅仅是
main
,而是想调用模块的导出函数f
是的,但是在您对前面问题的回答中,您说我可以动态创建importObj,这意味着使用工厂函数WebAssembly.module()如何创建importObject
?WebAssembly的常规用法已经知道importObject
将包含哪些内容,因此WebAssembly.instance
是大多数应用程序的正确API。如果您确实不知道它将包含什么,那么可以使用获取WebAssembly.Module
,然后是WebAssembly.instantiate
。这仍然是基于承诺的,但分解了编译/实例化,并允许您在获取模块后创建importObject
。我已使用WebAssembly.compile()成功获取模块实例,但我偶然发现了另一个错误。我将为此发布另一个问题。谢谢你的帮助!
fetch('hello.wasm').then(response =>
response.arrayBuffer()
).then(buffer =>
WebAssembly.instantiate(buffer, importObj)
).then(({module, instance}) =>
instance.exports.f()
);