5

I am playing around with WASM using wasmer and its package manager wapm. I can easily install the figlet WASM package (as described here) and run it locally:

ubuntu@ip-172-31-1-78:~$ wapm execute figlet it works
 _ _                        _        
(_) |_  __      _____  _ __| | _____ 
| | __| \ \ /\ / / _ \| '__| |/ / __|
| | |_   \ V  V / (_) | |  |   <\__ \
|_|\__|   \_/\_/ \___/|_|  |_|\_\___/
                                     
ubuntu@ip-172-31-1-78:~$ 

As a way to experiment with WASM, I would like to take the figlet.wasm file that was installed on my system and run it in the browser.

I have started a local nginx service and I have put the figlet.wasm into the root folder along with

  1. index.html
<!doctype html>

<html lang="en">
<head>
  <meta charset="utf-8">

  <title>Test</title>
</head>

<body>
  <script src="figlet.js"></script>
</body>
</html>
  1. figlet.js (taken from here)
const importObject = {
  imports: {
    imported_func(arg) {
      console.log(arg);
    }
  }
};

fetch('figlet.wasm').then((response) =>
  response.arrayBuffer()
).then((bytes) => {
  const mod = new WebAssembly.Module(bytes);
  const instance = new WebAssembly.Instance(mod, importObject);
  instance.exports.exported_func();
})

But I am getting a import object field 'wasi_snapshot_preview1' is not an Object.

Is there an easy way to fix the js file to be able to run the wasm module and see the output? The subsequent question would be how to pass the parameter figlet requires to render.

[UPDATE] Very slowly making progresses. It turns our that the specific error message import object field 'wasi_snapshot_preview1' is not an Object is due to the fact that the artifact has been compiled against a wasi interface that the browser doesn't understand (see here for background). I was able to reproduce the problem by compiling the hello-world rust simple app with a target wasm32-wasi. If I compile the simple app with target wasm32-unknown-unknown (the closer from the list of wasm targets in rust) I get a different error when loading in the browser: Uncaught (in promise) TypeError: result.instance.exports.exported_func is not a function.

Also interesting to note (and possibly expected) is that if I test the wasm32-wasi release with wasmer it works fine but if I test it with the wasm32-unknown-unknown release I get an error:

wasmer hello-rust-wasm.wasm
error: failed to run `hello-rust-wasm.wasm`
╰─▶ 1: No export `_start` found in the module.
       Similar functions found: `main`.
       Try with: wasmer hello-rust-wasm.wasm -i main

I am wondering if it is ever possible to run the SAME release/artifact (wasi or wasm that is) on both a browser AND a wasm/wasi runtime outside of the browser (or I just need to compile the program for two separate targets)

I will update the thread as/if I find out more.

mreferre
  • 5,464
  • 3
  • 22
  • 29
  • So, that `importObject` appears to be copied directly from [MDN's example](https://developer.mozilla.org/en-US/docs/WebAssembly/JavaScript_interface/Instance) (and should have had appropriate attribution). It was created for [the example WASM](https://github.com/mdn/webassembly-examples/blob/master/js-api-examples/simple.wasm), that has those functions defined. See [MDN's article on Exported functions](https://developer.mozilla.org/en-US/docs/WebAssembly/Exported_functions). – Heretic Monkey Sep 30 '22 at 12:43
  • Does this answer your question? [How to import wasm file inside javascript ES6 file](https://stackoverflow.com/questions/65541336/how-to-import-wasm-file-inside-javascript-es6-file) – Heretic Monkey Sep 30 '22 at 12:48
  • Sorry I forgot to attribute the site where I grabbed the js example (I fixed that in the question). I am exploring the MDN repo as well as the other SO thread you linked (which I did not find with my search). Thanks for now. – mreferre Sep 30 '22 at 12:57
  • 2
    @HereticMonkey All MDN code samples newer than 2010 are [licensed public domain CC0](https://developer.mozilla.org/en-US/docs/MDN/Writing_guidelines/Attrib_copyright_license#code_samples), no attribution is necessary. This is pointless nitpicking. – Etheryte Oct 01 '22 at 09:31
  • Did you fix this? All these wasm questions on stack overflow are unanswered! – Wilson Silva Aug 27 '23 at 05:30

0 Answers0