未捕获(承诺)LinkError:WebAssembly实例化:导入#1 module =“ env” function =“ __ memory_base”错误:全局导入必须为数字

时间:2019-04-27 19:15:42

标签: webassembly

我正在遵循Google CodeLab An Introduction to Web Assembly来学习WebAssembly。

在执行Serve over HTTP的步骤时,出现以下错误:

Uncaught (in promise) LinkError: WebAssembly Instantiation: Import #1 module="env" function="__memory_base" error: global import must be a number or WebAssembly.Global object
    at createWebAssembly (http://127.0.0.1:8000/:11:57)
    at async init (http://127.0.0.1:8000/:46:18)

这是网页上运行的JavaScript:

  async function createWebAssembly(path, importObject) {
    const result = await window.fetch(path);
    const bytes = await result.arrayBuffer();
    return WebAssembly.instantiate(bytes, importObject);
  }

  const memory = new WebAssembly.Memory({initial: 256, maximum: 256});
  let exports = null;

  async function init() {
    const env = {
      'abortStackOverflow': _ => { throw new Error('overflow'); },
      'table': new WebAssembly.Table({initial: 0, maximum: 0, element: 'anyfunc'}),
      'tableBase': 0,
      'memory': memory,
      'memoryBase': 1024,
      'STACKTOP': 0,
      'STACK_MAX': memory.buffer.byteLength,
    };
    const importObject = {env};

    const wasm = await createWebAssembly('output.wasm', importObject);
    exports = wasm.instance.exports;
    console.info('got exports', exports);
    exports._board_init();  // setup lyff board

    // TODO: interact with lyff board
  }
  init();

错误来自WebAssembly.instantiate(bytes, importObject);但是,我无法进入该功能。有人可以让我知道我在想什么吗?预先谢谢你!

1 个答案:

答案 0 :(得分:1)

我遇到了同样的问题,并在GitHub上找到了解决方案:

https://github.com/googlecodelabs/web-assembly-introduction/issues/11

  

在更高版本的emcc中,将memoryBase替换为__memory_base,将tableBase替换为__table_base