javascript - WebAssembly 链接错误模块 ="env"

标签 javascript webassembly

我正在 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, {}))

但是我得到这个错误:

RangeError

所以我尝试使用 MDN docs 中的 WebAssembly.instantiate()使用此代码:

const instantiate = (bytes, imports = {}) =>
  WebAssembly.compile(bytes).then(m =>
    WebAssembly.instantiate(m, imports)
  )

我得到了一个不同的:

LinkError

知道如何解决吗?

最佳答案

您的问题并不清楚,但进一步的评论解释说您将导入对象保留为 {},导致实例化失败。 WebAssembly 使用双命名空间,其中 import object实现了 WebAssembly.Moduleimports .每次导入都是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/

相关文章:

javascript - 单个 div 的视差效果

c++14 - 如何在 WebAssembly 中使用向量(C++ STL)

javascript - ESLint JSDoc 回调错误

javascript - 网格到 ListView ,仅 CSS(带 Flexbox)

javascript - 有趣的JavasSript混淆方法——无法解码

node.js - 如何使用库导入来编译 C 文件到 WebAssembly 文件 (Emscripten)

javascript - Webpack 使用 Emscripten 抛出错误 : Can't resolve 'fs'

javascript - 将 WebAsembly 内存传递给 WebGL2

rust - 在 wasm-bindgen 中扩展 AudioWorkletProcessor?

javascript - 我可以将 javascript 存储在本地存储对象中并运行它吗?