首页 > 解决方案 > Uncaught (in promise) LinkError: WebAssembly Instantiation: Import #1 module="env" function="__memory_base" error: global import must be a number

问题描述

I am following the Google CodeLab An Introduction to Web Assembly to learn WebAssembly.

When I work on the step of Serve over HTTP, I hit the below error:

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)

Here is the JavaScript running on the webpage:

  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();

The error is from WebAssembly.instantiate(bytes, importObject); however, I cannot step into that function though. Can someone please let me know what I am missing? Thank you in advance!

标签: webassembly

解决方案


I had the same issue and found the solution on GitHub:

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

With later version of emcc replace memoryBase with __memory_base and tableBase with __table_base.


推荐阅读