W3cubDocs

/JavaScript

WebAssembly.instantiate()

The WebAssembly.instantiate() function allows you to compile and instantiate WebAssembly code. This function has two overloads:

  • The primary overload takes the WebAssembly binary code, in the form of a typed array or ArrayBuffer, and performs both compilation and instantiation in one step. The returned Promise resolves to both a compiled WebAssembly.Module and its first WebAssembly.Instance.
  • The secondary overload takes an already-compiled WebAssembly.Module and returns a Promise that resolves to an Instance of that Module. This overload is useful if the Module has already been compiled.

Warning: This method is not the most efficient way of fetching and instantiating wasm modules. If at all possible, you should use the newer WebAssembly.instantiateStreaming() method instead, which fetches, compiles, and instantiates a module all in one step, directly from the raw bytecode, so doesn't require conversion to an ArrayBuffer.

Syntax

Primary overload — taking wasm binary code

WebAssembly.instantiate(bufferSource, importObject);

Parameters

bufferSource

A typed array or ArrayBuffer containing the binary code of the .wasm module you want to compile, or a WebAssembly.Module.

importObject Optional

An object containing the values to be imported into the newly-created Instance, such as functions or WebAssembly.Memory objects. There must be one matching property for each declared import of the compiled module or else a WebAssembly.LinkError is thrown.

Return value

A Promise that resolves to a ResultObject which contains two fields:

Exceptions

Secondary overload — taking a module object instance

WebAssembly.instantiate(module, importObject);

Parameters

module

The WebAssembly.Module object to be instantiated.

importObject Optional

An object containing the values to be imported into the newly-created Instance, such as functions or WebAssembly.Memory objects. There must be one matching property for each declared import of module or else a WebAssembly.LinkError is thrown.

Return value

A Promise that resolves to an WebAssembly.Instance object.

Exceptions

Examples

Note: You'll probably want to use WebAssembly.instantiateStreaming() in most cases, as it is more efficient than instantiate().

First overload example

After fetching some WebAssembly bytecode using fetch, we compile and instantiate the module using the WebAssembly.instantiate() function, importing a JavaScript function into the WebAssembly Module in the process. We then call an Exported WebAssembly function that is exported by the Instance.

const importObject = {
  imports: {
    imported_func(arg) {
      console.log(arg);
    },
  },
};

fetch('simple.wasm')
  .then((response) => response.arrayBuffer())
  .then((bytes) => WebAssembly.instantiate(bytes, importObject))
  .then((result) => result.instance.exports.exported_func());

Note: You can also find this example at index.html on GitHub (view it live also).

Second overload example

The following example (see our index-compile.html demo on GitHub, and view it live also) compiles the loaded simple.wasm byte code using the WebAssembly.compileStreaming() method and then sends it to a worker using postMessage().

const worker = new Worker("wasm_worker.js");

WebAssembly.compileStreaming(fetch('simple.wasm'))
  .then((mod) => worker.postMessage(mod));

In the worker (see wasm_worker.js) we define an import object for the module to use, then set up an event handler to receive the module from the main thread. When the module is received, we create an instance from it using the WebAssembly.instantiate() method and invoke an exported function from inside it.

const importObject = {
  imports: {
    imported_func(arg) {
      console.log(arg);
    },
  },
};

onmessage = (e) => {
  console.log('module received from main thread');
  const mod = e.data;

  WebAssembly.instantiate(mod, importObject).then((instance) => {
    instance.exports.exported_func();
  });
};

Specifications

Browser compatibility

Desktop Mobile Server
Chrome Edge Firefox Internet Explorer Opera Safari WebView Android Chrome Android Firefox for Android Opera Android Safari on IOS Samsung Internet Deno Node.js
instantiate
57
16
52
No
44
11
57
57
52
43
11
7.0
1.0
8.0.0

See also

© 2005–2022 MDN contributors.
Licensed under the Creative Commons Attribution-ShareAlike License v2.5 or later.
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/WebAssembly/instantiate