MDN self-hosted library · e01.org

WebAssembly

The WebAssembly JavaScript object acts as the namespace for all WebAssembly-related functionality.

The WebAssembly JavaScript object acts as the namespace for all WebAssembly-related functionality.

Unlike most other global objects, WebAssembly is not a constructor (it is not a function object). You can compare it to Math, which is also a namespace object for mathematical constants and functions, or to Intl which is the namespace object for internationalization constructors and other language-sensitive functions.

Description

The primary uses for the WebAssembly object are:

Interfaces

  • WebAssembly.CompileError
  • : Indicates an error during WebAssembly decoding or validation.
  • WebAssembly.Global
  • : Represents a global variable instance, accessible from both JavaScript and importable/exportable across one or more WebAssembly.Module instances. This allows dynamic linking of multiple modules.
  • WebAssembly.Instance
  • : Is a stateful, executable instance of a WebAssembly.Module
  • WebAssembly.LinkError
  • : Indicates an error during module instantiation (besides traps from the start function).
  • WebAssembly.Memory
  • : An object whose buffer property is a resizable ArrayBuffer that holds the raw bytes of memory accessed by a WebAssembly Instance.
  • WebAssembly.Module
  • : Contains stateless WebAssembly code that has already been compiled by the browser and can be efficiently shared with Workers, and instantiated multiple times.
  • WebAssembly.RuntimeError
  • : Error type that is thrown whenever WebAssembly specifies a trap.
  • WebAssembly.Suspending
  • : Represents a suspending function — an asynchronous (Promise-based) JavaScript function that, when imported into a Wasm module and called from inside, will result in execution being suspended until the promise resolves.
  • WebAssembly.Table
  • : An array-like structure representing a WebAssembly Table, which stores references, such as function references.
  • WebAssembly.Tag
  • : An object that represents a type of WebAssembly exception.
  • WebAssembly.Exception
  • : A WebAssembly exception object that can be thrown, caught, and rethrown both within and across WebAssembly/JavaScript boundaries.

Static properties

  • WebAssembly.JSTag
  • : A built-in WebAssembly.Tag representing exceptions thrown in the JavaScript host — it allows exceptions thrown in JavaScript to be handled from inside a Wasm module.

Static methods

Examples

Stream a Wasm module then compile and instantiate it

The following example (see our instantiate-streaming.html demo on GitHub, and view it live also) directly streams a Wasm module from an underlying source then compiles and instantiates it, the promise fulfilling with a ResultObject. Because the instantiateStreaming() function accepts a promise for a Response object, you can directly pass it a fetch() call, and it will pass the response into the function when it fulfills.

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

WebAssembly.instantiateStreaming(fetch("simple.wasm"), importObject).then(
  (obj) => obj.instance.exports.exported_func(),
);

The ResultObject's .instance property is then accessed, and the contained exported function invoked.

Specifications

Browser compatibility

See also