简体   繁体   English

实例化 WASM 模块时出错:“模块不是 object 或函数”

[英]Error instantiating WASM module: "module is not an object or function"

I'm trying to instantiate a.wasm file locally in node.js. The goal is to run the binary locally and reproduce the functionalities of the web page.我正在尝试在 node.js 本地实例化 a.wasm 文件。目标是在本地运行二进制文件并重现 web 页面的功能。 Here's my minimum reproducible example:这是我的最小可重现示例:

const fetch = require("node-fetch");

const importObject = {
  imports: {
    imported_func: function(arg) {
      console.log(arg);
    }
  }
};

fetch('https://www.supremenewyork.com/ticket.wasm').then(response =>
  response.arrayBuffer()
).then(bytes => {
  let mod = new WebAssembly.Module(bytes);
  let instance = new WebAssembly.Instance(mod, importObject);
  instance.exports.exported_func();
})

The error I get is:我得到的错误是:

TypeError: WebAssembly.Instance(): Import #0 module="wasi_unstable" error: module is not an object or function TypeError: WebAssembly.Instance(): Import #0 module="wasi_unstable" 错误:模块不是 object 或 function

I saw some questions with similar problems but no real solutions were provided.我看到一些类似问题的问题,但没有提供真正的解决方案。 This is my first time working with wasm so I'm pretty lost.这是我第一次使用 wasm,所以我很迷茫。

Your module seems to depend on the wasi_unstable API.您的模块似乎依赖于wasi_unstable API。 If you want to load it you will need an implementation of that API.如果你想加载它,你需要一个 API 的实现。

To see exactly what imports you module needs you can use wasm2wat or wasmdis tools from wabt and binaryen projects respectively.要准确查看您的模块需要哪些导入,您可以分别使用wasm2watwasmdis工具。

If you built your wasm module with emscripten then the recommended practice is to have the emscripten generate JS to that implenents these APIs and takes case of loading the module for you.如果你使用 emscripten 构建了你的 wasm 模块,那么推荐的做法是让 emscripten 生成 JS 来实现这些 API,并为你加载模块。

If you build your wasm module with the wasi-sdk then you need some kind of web polyfill for the WASI APIs.如果您使用 wasi-sdk 构建您的 wasm 模块,那么您需要某种用于 WASI API 的 web polyfill。

This will make the error go away:这将使错误 go 消失:

const importObject = {
  imports: {
    imported_func: function(arg) {
      console.log(arg);
    },
    wasi_unstable: () => {}
  }
};

Try this one试试这个

const importObject = {
    wasi_unstable: {
        imported_func: function(args) {
            console.log(args);
        }
    }
};

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM