WebAssembly LinkError 模块 =“env”

pie*_*909 4 javascript webassembly

我正在web assembly.org上运行该教程,现在我想hello.wasm从我自己的页面运行该教程。我正在按照教程的说明使用Emscripten编译代码。

按照我正在做的这些说明进行操作: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, {}))
Run Code Online (Sandbox Code Playgroud)

但我收到这个错误:

范围误差

所以我尝试使用MDN 文档WebAssembly.instantiate()中的以下代码:

const instantiate = (bytes, imports = {}) =>
  WebAssembly.compile(bytes).then(m =>
    WebAssembly.instantiate(m, imports)
  )
Run Code Online (Sandbox Code Playgroud)

我得到了一个不同的:

链接错误

知道如何修复它吗?

JF *_*ien 5

您的问题尚不清楚,但进一步的注释解释说您将导入对象保留为{},导致实例化失败。WebAssembly 使用双命名空间,其中导入对象WebAssembly.Module满足的导入。每个导入都被指定为 module+field+kind,JavaScript 导入对象必须满足这一点。

Emscripten 已经生成了为您加载的 HTML+JS hello.wasm,包括 WebAssembly 导入对象。Emscripten 生成的内容非常大,因为它模拟操作系统。导入对象提供所有系统调用(到 JavaScript)。您必须将这些传递给示例才能工作......或者只使用 Emscripten 已经生成的那些。

您正在使用的代码需要一个名为 的模块env。Emscripten 包含如下代码:

let importObject = {
  env: { foo: () => 42, bar: () => 3.14 }
};
Run Code Online (Sandbox Code Playgroud)

这就是我之前提到的双重命名空间:env是模块,foo/bar是字段。他们的类型是function. WebAssembly 支持其他类型的导入和导出:表、内存和全局。

缺少单个模块或模块的字段,或者类型不匹配,都会导致实例化失败。