我正在 webassembly.org 上运行教程现在我想从我自己的页面运行 hello.wasm
。
我正在使用 Emscripten 编译代码按照教程的说明。
正在关注 these instructions在我的 index.html
中我正在做:
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)
)
我得到了一个不同的:
知道如何解决吗?
最佳答案
您的问题并不清楚,但进一步的评论解释说您将导入对象保留为 {}
,导致实例化失败。 WebAssembly 使用双命名空间,其中 import object实现了 WebAssembly.Module
的 imports .每次导入都是specified作为 module+field+kind,JavaScript 导入对象必须满足。
Emscripten 已经生成 HTML+JS 为您加载 hello.wasm
,包括 WebAssembly 导入对象。 Emscripten 生成的内容非常大,因为它模拟操作系统。导入对象提供所有系统调用(对 JavaScript)。您必须将这些传递给示例才能正常工作……或者只使用 Emscripten 已经生成的那些。
您正在使用的代码需要一个名为 env
的模块。 Emscripten 包含如下代码:
let importObject = {
env: { foo: () => 42, bar: () => 3.14 }
};
这就是我前面提到的双命名空间:env
是一个模块,foo
/bar
是字段。它们的类型是function
。 WebAssembly 支持其他类型的导入和导出:表、内存和全局。
缺少单个模块或模块的字段,或类型不匹配,会导致实例化失败。
关于javascript - WebAssembly 链接错误模块 ="env",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42668708/