【发布时间】:2017-03-08 10:24:24
【问题描述】:
我正在webassembly.org 上运行教程,现在我想从我自己的页面运行hello.wasm。
我正在按照教程的说明使用Emscripten 编译代码。
在我的index.html 中关注these instructions 我正在做:
const instantiate = (bytes, imports = {}) =>
WebAssembly.compile(bytes).then(m =>
new WebAssembly.Instance(m, imports)
)
fetch('hello.wasm')
.then(response => response.arrayBuffer())
.then(bytes => instantiate(bytes, {}))
但我收到此错误:
所以我尝试将MDN docs 中的WebAssembly.instantiate() 与此代码一起使用:
const instantiate = (bytes, imports = {}) =>
WebAssembly.compile(bytes).then(m =>
WebAssembly.instantiate(m, imports)
)
我得到一个不同的:
知道怎么解决吗?
【问题讨论】:
-
让我们尝试缩小问题范围:在第二个示例中您不需要
compile,只需要instantiate:它需要.wasm字节并编译+实例化它们。imports对象是什么?还是您将其保留为默认{}?您的模块声明的导入是什么(我假设来自 Emscripten)? -
即使我使用
instantiate的重载方法,它也会失败并出现同样的错误。我将一个空对象作为imports传递给它,但我不知道所需的依赖项。 -
Emscripten 已经生成了 HTML+JS,它会为您加载
hello.wasm,包括 WebAssembly 导入对象。 Emscripten 生成的内容非常大,因为它模拟了操作系统。导入对象基本上提供所有系统调用(对 JavaScript)。您必须将这些传递给示例才能工作......或者只使用 Emscripten 已经生成的那些。 -
我认为你的评论和@Andreas 的答案应该是正确的答案。
-
太好了,我回答了额外的信息!