WebAssembly
Baseline Widely available *
This feature is well established and works across many devices and browser versions. It’s been available across browsers since October 2017.
* Some parts of this feature may have varying levels of support.
TheWebAssembly JavaScript object acts as the namespace for allWebAssembly-related functionality.
Unlike most other global objects,WebAssembly is not a constructor (it is not a function object). You can compare it toMath, which is also a namespace object for mathematical constants and functions, or toIntl which is the namespace object for internationalization constructors and other language-sensitive functions.
In this article
Description
The primary uses for theWebAssembly object are:
- Loading WebAssembly code, using the
WebAssembly.instantiate()function. - Creating new memory and table instances via the
WebAssembly.Memory()/WebAssembly.Table()constructors. - Providing facilities to handle errors that occur in WebAssembly via the
WebAssembly.CompileError()/WebAssembly.LinkError()/WebAssembly.RuntimeError()constructors.
Interfaces
WebAssembly.CompileErrorIndicates an error during WebAssembly decoding or validation.
WebAssembly.GlobalRepresents a global variable instance, accessible from both JavaScript and importable/exportable across one or more
WebAssembly.Moduleinstances. This allows dynamic linking of multiple modules.WebAssembly.InstanceIs a stateful, executable instance of a
WebAssembly.ModuleWebAssembly.LinkErrorIndicates an error during module instantiation (besidestraps from the start function).
WebAssembly.MemoryAn object whose
bufferproperty is a resizableArrayBufferthat holds the raw bytes of memory accessed by a WebAssemblyInstance.WebAssembly.ModuleContains stateless WebAssembly code that has already been compiled by the browser and can be efficientlyshared with Workers, and instantiated multiple times.
WebAssembly.RuntimeErrorError type that is thrown whenever WebAssembly specifies atrap.
WebAssembly.TableAn array-like structure representing a WebAssembly Table, which storesreferences, such as function references.
WebAssembly.TagAn object that represents a type of WebAssembly exception.
WebAssembly.ExceptionA WebAssembly exception object that can be thrown, caught, and rethrown both within and across WebAssembly/JavaScript boundaries.
Static methods
WebAssembly.compile()Compiles a
WebAssembly.Modulefrom WebAssembly binary code, leaving instantiation as a separate step.WebAssembly.compileStreaming()Compiles a
WebAssembly.Moduledirectly from a streamed underlying source, leaving instantiation as a separate step.WebAssembly.instantiate()The primary API for compiling and instantiating WebAssembly code, returning both a
Moduleand its firstInstance.WebAssembly.instantiateStreaming()Compiles and instantiates a WebAssembly module directly from a streamed underlying source, returning both a
Moduleand its firstInstance.WebAssembly.validate()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 ourinstantiate-streaming.html demo on GitHub, andview it live also) directly streams a Wasm module from an underlying source then compiles and instantiates it, the promise fulfilling with aResultObject. Because theinstantiateStreaming() function accepts a promise for aResponse object, you can directly pass it afetch() 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(),);TheResultObject's.instance property is then accessed, and the contained exported function invoked.
Specifications
| Specification |
|---|
| WebAssembly JavaScript Interface> # webassembly-namespace> |