Minimal requirements to load a wasm

To load web assembly file, you need to write some code to fetch the binary and execute it. As today, there's no other method than use Javascript.

The code is pretty simple must and be set inside your html page.

Wasmbrowsertest will inserted you automatically. You won't need it when you use it.

<html>  
    <head>
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <meta charset="utf-8"/>
        <script src="wasm_exec.js"></script>
        <script>


const wasmBrowserInstantiate = async (wasmModuleUrl, importObject) => {
  let response = undefined;

  // Check if the browser supports streaming instantiation
  if (WebAssembly.instantiateStreaming) {
    // Fetch the module, and instantiate it as it is downloading
    response = await WebAssembly.instantiateStreaming(
      fetch(wasmModuleUrl),
      importObject
    );
 
  } else {
    // Fallback to using fetch to download the entire module
    // And then instantiate the module
    const fetchAndInstantiateTask = async () => {
      const wasmArrayBuffer = await fetch(wasmModuleUrl).then(response =>
        response.arrayBuffer()
      );
      return WebAssembly.instantiate(wasmArrayBuffer, importObject);
    };
    response = await fetchAndInstantiateTask();
  }

  return response;
};

const go = new Go();
const runWasmAdd = async () => {
  // Get the importObject from the go instance.
  const importObject = go.importObject;

  // Instantiate our wasm module
  const wasmModule = await wasmBrowserInstantiate("app.wasm", importObject);

  // Allow the wasm_exec go instance, bootstrap and execute our wasm module
  go.run(wasmModule.instance);

};
runWasmAdd();

        </script>
    </head>
    <body></body>
</html>  

You app.wasm must be served with a mime-type set to 'application/wasm'

If your app can be launched and you have the "Failed to execute 'compile' on 'WebAssembly': Incorrect response MIME type. Expected 'application/wasm' ", Check your webserver settings to accept wasm mime type

The wasm_exec.js is provided by your compiler and must be the exact version proposed by your compiler when compiling your webassembly application

Location of wasm_exec.js

For a standard GO compiler:

$(go env GOROOT)/misc/wasm/wasm_exec.js

For TinyGo compiler:

/usr/local/tinygo/targets/wasm_exec.js

Last updated