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](/docs/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:

- Loading WebAssembly code, using the [`WebAssembly.instantiate()`](/docs/WebAssembly/Reference/JavaScript_interface/instantiate_static) function.
- Creating new memory and table instances via the [`WebAssembly.Memory()`](/docs/WebAssembly/Reference/JavaScript_interface/Memory)/[`WebAssembly.Table()`](/docs/WebAssembly/Reference/JavaScript_interface/Table) constructors.
- Providing facilities to handle errors that occur in WebAssembly via the [`WebAssembly.CompileError()`](/docs/WebAssembly/Reference/JavaScript_interface/CompileError)/[`WebAssembly.LinkError()`](/docs/WebAssembly/Reference/JavaScript_interface/LinkError)/[`WebAssembly.RuntimeError()`](/docs/WebAssembly/Reference/JavaScript_interface/RuntimeError) constructors.

## Interfaces

- [`WebAssembly.CompileError`](/docs/WebAssembly/Reference/JavaScript_interface/CompileError)
- : Indicates an error during WebAssembly decoding or validation.
- [`WebAssembly.Global`](/docs/WebAssembly/Reference/JavaScript_interface/Global)
- : Represents a global variable instance, accessible from both JavaScript and importable/exportable across one or more [`WebAssembly.Module`](/docs/WebAssembly/Reference/JavaScript_interface/Module) instances. This allows dynamic linking of multiple modules.
- [`WebAssembly.Instance`](/docs/WebAssembly/Reference/JavaScript_interface/Instance)
- : Is a stateful, executable instance of a [`WebAssembly.Module`](/docs/WebAssembly/Reference/JavaScript_interface/Module)
- [`WebAssembly.LinkError`](/docs/WebAssembly/Reference/JavaScript_interface/LinkError)
- : Indicates an error during module instantiation (besides [traps](https://webassembly.github.io/simd/core/intro/overview.html#trap) from the start function).
- [`WebAssembly.Memory`](/docs/WebAssembly/Reference/JavaScript_interface/Memory)
- : An object whose [`buffer`](/docs/WebAssembly/Reference/JavaScript_interface/Memory/buffer) property is a resizable ArrayBuffer that holds the raw bytes of memory accessed by a WebAssembly `Instance`.
- [`WebAssembly.Module`](/docs/WebAssembly/Reference/JavaScript_interface/Module)
- : Contains stateless WebAssembly code that has already been compiled by the browser and can be efficiently [shared with Workers](/docs/Web/API/Worker/postMessage), and instantiated multiple times.
- [`WebAssembly.RuntimeError`](/docs/WebAssembly/Reference/JavaScript_interface/RuntimeError)
- : Error type that is thrown whenever WebAssembly specifies a [trap](https://webassembly.github.io/simd/core/intro/overview.html#trap).
- [`WebAssembly.Suspending`](/docs/WebAssembly/Reference/JavaScript_interface/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`](/docs/WebAssembly/Reference/JavaScript_interface/Table)
- : An array-like structure representing a WebAssembly Table, which stores [references](https://webassembly.github.io/spec/core/syntax/types.html#syntax-reftype), such as function references.
- [`WebAssembly.Tag`](/docs/WebAssembly/Reference/JavaScript_interface/Tag)
- : An object that represents a type of WebAssembly exception.
- [`WebAssembly.Exception`](/docs/WebAssembly/Reference/JavaScript_interface/Exception)
- : A WebAssembly exception object that can be thrown, caught, and rethrown both within and across WebAssembly/JavaScript boundaries.

## Static properties

- [`WebAssembly.JSTag`](/docs/WebAssembly/Reference/JavaScript_interface/JSTag_static)
- : A built-in [`WebAssembly.Tag`](/docs/WebAssembly/Reference/JavaScript_interface/Tag) representing exceptions thrown in the JavaScript host — it allows exceptions thrown in JavaScript to be handled from inside a Wasm module.

## Static methods

- [`WebAssembly.compile()`](/docs/WebAssembly/Reference/JavaScript_interface/compile_static)
- : Compiles a [`WebAssembly.Module`](/docs/WebAssembly/Reference/JavaScript_interface/Module) from WebAssembly binary code, leaving instantiation as a separate step.
- [`WebAssembly.compileStreaming()`](/docs/WebAssembly/Reference/JavaScript_interface/compileStreaming_static)
- : Compiles a [`WebAssembly.Module`](/docs/WebAssembly/Reference/JavaScript_interface/Module) directly from a streamed underlying source, leaving instantiation as a separate step.
- [`WebAssembly.instantiate()`](/docs/WebAssembly/Reference/JavaScript_interface/instantiate_static)
- : The primary API for compiling and instantiating WebAssembly code, returning both a `Module` and its first `Instance`.
- [`WebAssembly.instantiateStreaming()`](/docs/WebAssembly/Reference/JavaScript_interface/instantiateStreaming_static)
- : Compiles and instantiates a WebAssembly module directly from a streamed underlying source, returning both a `Module` and its first `Instance`.
- [`WebAssembly.promising()`](/docs/WebAssembly/Reference/JavaScript_interface/promising_static)
- : Wraps an exported Wasm function that relies on an asynchronous operation (that is, an imported suspending function created via the [`WebAssembly.Suspending()`](/docs/WebAssembly/Reference/JavaScript_interface/Suspending/Suspending) constructor), turning it into a Promise.
- [`WebAssembly.validate()`](/docs/WebAssembly/Reference/JavaScript_interface/validate_static)
- : Validates a given typed array of WebAssembly binary code, returning whether the bytes are valid WebAssembly code (`true`) or not (`false`).

## Examples

### Stream a Wasm module then compile and instantiate it

The following example (see our [instantiate-streaming.html](https://github.com/mdn/webassembly-examples/blob/main/js-api-examples/instantiate-streaming.html) demo on GitHub, and [view it live](https://mdn.github.io/webassembly-examples/js-api-examples/instantiate-streaming.html) 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`](/docs/Web/API/Response) object, you can directly pass it a [`fetch()`](/docs/Web/API/Window/fetch) call, and it will pass the response into the function when it fulfills.

```js
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

- [WebAssembly](/docs/WebAssembly) overview
- [WebAssembly concepts](/docs/WebAssembly/Guides/Concepts)
- [Using the WebAssembly JavaScript API](/docs/WebAssembly/Guides/Using_the_JavaScript_API)

source: MDN Web Docs mirror · page

Cite this

"WebAssembly". MDN Web Docs. CC BY-SA 2.5 (MDN). https://developer.mozilla.org/en-US/docs/WebAssembly/Reference/JavaScript_interface

JSON record · API collection · citation policy