简体   繁体   中英

WebAssembly LinkError module=“env”

I'm running the tutorial on webassembly.org and now I want to run the hello.wasm from my own page. I'm compiling the code using Emscripten as per tutorial's instructions.

Following these instructions in my index.html I'm doing:

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

But I get this error:

引发RangeError

So I tried to use WebAssembly.instantiate() from the MDN docs with this code:

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

And I get a different one:

LinkError

Any idea how to fix it?

It wasn't clear from your question, but further comments explain that you leave the import object as {} , leading instantiation to fail. WebAssembly uses a double namespace, where the import object fulfills the WebAssembly.Module 's imports . Each import is specified as module+field+kind, which the JavaScript import object must fulfill.

Emscripten already generates HTML+JS which loads hello.wasm for you, including the WebAssembly import object. What Emscripten generates is pretty big because it emulates an OS. The import object supplies all the syscalls (to JavaScript). You'd have to pass these in for the example to work... or just use the ones Emscripten already generated.

The code you're using is expecting a module called env . Emscripten contains code such as:

let importObject = {
  env: { foo: () => 42, bar: () => 3.14 }
};

That's the double namespace I mentioned earlier: env is a module, and foo / bar are fields. Their type is function . WebAssembly supports other kinds of imports and exports: table, memory, and global.

Missing a single module, or a module's field, or mismatching the kind, leads to instantiation failure as you're getting.

Apparently, your example module wants to import something from a module named "env" . However, the imports object you provide is empty. To make instantiation of your module succeed you need to provide an imports object of the form {env: {...}} , where the dots are properties corresponding to every single import made from "env" .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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