对于提供的参数,在此上下文中,Wasm编译超出了内部限制

时间:2017-06-12 19:51:23

标签: javascript c webassembly

所以我试图从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(在promise中)RangeError:WebAssembly.Module():对于提供的参数,Wasm编译在此上下文中超出内部限制       at fetch.then.then.buff(:1:77)       在

我不知道如何处理此错误,我无法通过网络搜索找到任何内容。

非常感谢任何帮助

谢谢!

1 个答案:

答案 0 :(得分:2)

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)。 / p>

这里我假设您需要的不仅仅是main,而是想要调用模块的导出函数f

相关问题