LinkError when trying Hello World in Rust with WebAssembly

Viewed 1243

I'm trying to run a hello world program made in Rust with WebAssembly, but I'm getting an error message when I try to load the program.

I was able to get it running when following some tutorials that I found, the problem is that they use Emscripten to create JavaScript and HTML for loading the code, but this JavaScript and HTML includes tons and tons of boilerplate and other stuff. I got kinda lost and instead wanted to try to get a really simple example that I'm loading myself.

I ran the following to compile hello.wasm

echo 'fn main() { println!("Hello, Emscripten!"); }' > hello.rs
rustc --target=wasm32-unknown-emscripten hello.rs

To load hello.wasm, I took the example from the Mozilla WebAssembly docs and tried to run that.

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

fetch('hello.wasm').then(response =>
  response.arrayBuffer()
).then(bytes =>
  WebAssembly.instantiate(bytes, importObject)
).then(results => {
  // Do something with the compiled results!
});

WebAssembly.instantiate crashes:

LinkError: Import #0 module="env" error: module is not an object or function

I found something about this error having to do with something missing, that the boilerplate code is supposed to load, but looking through the autogenerated HTML and JavaScript I couldn't figure out exactly what it could be.

1 Answers
Related