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:
- 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.
Methods
WebAssembly.instantiate()
- The primary API for compiling and instantiating WebAssembly code, returning both a
Module
and its firstInstance
. WebAssembly.instantiateStreaming()
- Compiles and instantiates a WebAssembly module directly from a streamed underlying source, returning both a
Module
and its firstInstance
. WebAssembly.compile()
- Compiles a
WebAssembly.Module
from WebAssembly binary code, leaving instantiation as a separate step. WebAssembly.compileStreaming()
- compiles a
WebAssembly.Module
directly from a streamed underlying source, leaving instantiation as a separate step. WebAssembly.validate()
- Validates a given typed array of WebAssembly binary code, returning whether the bytes are valid WebAssembly code (
true
) or not (false
).
Constructors
WebAssembly.Global()
- Creates a new WebAssembly
Global
object. WebAssembly.Module()
- Creates a new WebAssembly
Module
object. WebAssembly.Instance()
- Creates a new WebAssembly
Instance
object. WebAssembly.Memory()
- Creates a new WebAssembly
Memory
object. WebAssembly.Table()
- Creates a new WebAssembly
Table
object. WebAssembly.CompileError()
- Creates a new WebAssembly
CompileError
object. WebAssembly.LinkError()
- Creates a new WebAssembly
LinkError
object. WebAssembly.RuntimeError()
- Creates a new WebAssembly
RuntimeError
object.
Examples
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 WindowOrWorkerGlobalScope.fetch()
call, and it will pass the response into the function when it fulfills.
var importObject = { imports: { imported_func: arg => console.log(arg) } }; WebAssembly.instantiateStreaming(fetch('simple.wasm'), importObject) .then(obj => obj.instance.exports.exported_func());
The ResultObject
's instance member is then accessed, and the contained exported function invoked.
Specifications
Specification | Status | Comment |
---|---|---|
WebAssembly JavaScript Interface The definition of 'WebAssembly' in that specification. |
Working Draft | Initial draft definition. |
Browser compatibility
Desktop | Mobile | Server | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|
WebAssembly | Chrome Full support 57 | Edge Full support 16 | Firefox
Full support
52
| IE No support No | Opera Full support 44 | Safari Full support 11 | WebView Android Full support 57 | Chrome Android Full support 57 | Firefox Android
Full support
52
| Opera Android Full support Yes | Safari iOS Full support 11 | Samsung Internet Android Full support 7.0 | nodejs Full support 8.0.0 |
CompileError | Chrome Full support 57 | Edge Full support 16 | Firefox
Full support
52
| IE No support No | Opera Full support 44 | Safari Full support 11 | WebView Android Full support 57 | Chrome Android Full support 57 | Firefox Android
Full support
52
| Opera Android ? | Safari iOS Full support 11 | Samsung Internet Android Full support 7.0 | nodejs Full support 8.0.0 |
Global | Chrome Full support 69 | Edge No support No | Firefox Full support 62 | IE No support No | Opera No support No | Safari No support No | WebView Android Full support 69 | Chrome Android Full support 69 | Firefox Android Full support 62 | Opera Android No support No | Safari iOS No support No | Samsung Internet Android Full support 10.0 | nodejs No support No |
Instance | Chrome Full support 57 | Edge Full support 16 | Firefox
Full support
52
| IE No support No | Opera Full support 44 | Safari Full support 11 | WebView Android Full support 57 | Chrome Android Full support 57 | Firefox Android
Full support
52
| Opera Android ? | Safari iOS Full support 11 | Samsung Internet Android Full support 7.0 | nodejs Full support 8.0.0 |
LinkError | Chrome Full support 57 | Edge Full support 16 | Firefox
Full support
52
| IE No support No | Opera Full support 44 | Safari Full support 11 | WebView Android Full support 57 | Chrome Android Full support 57 | Firefox Android
Full support
52
| Opera Android ? | Safari iOS Full support 11 | Samsung Internet Android Full support 7.0 | nodejs Full support 8.0.0 |
Memory | Chrome Full support 57 | Edge Full support 16 | Firefox
Full support
52
| IE No support No | Opera Full support 44 | Safari Full support 11 | WebView Android Full support 57 | Chrome Android Full support 57 | Firefox Android
Full support
52
| Opera Android ? | Safari iOS Full support 11 | Samsung Internet Android Full support 7.0 | nodejs Full support 8.0.0 |
Module | Chrome Full support 57 | Edge Full support 16 | Firefox
Full support
52
| IE No support No | Opera Full support 44 | Safari Full support 11 | WebView Android Full support 57 | Chrome Android Full support 57 | Firefox Android
Full support
52
| Opera Android ? | Safari iOS Full support 11 | Samsung Internet Android Full support 7.0 | nodejs Full support 8.0.0 |
RuntimeError | Chrome Full support 57 | Edge Full support 16 | Firefox
Full support
52
| IE No support No | Opera Full support 44 | Safari Full support 11 | WebView Android Full support 57 | Chrome Android Full support 57 | Firefox Android
Full support
52
| Opera Android ? | Safari iOS Full support 11 | Samsung Internet Android Full support 7.0 | nodejs Full support 8.0.0 |
Table | Chrome Full support 57 | Edge Full support 16 | Firefox
Full support
52
| IE No support No | Opera Full support 44 | Safari Full support 11 | WebView Android Full support 57 | Chrome Android Full support 57 | Firefox Android
Full support
52
| Opera Android ? | Safari iOS Full support 11 | Samsung Internet Android Full support 7.0 | nodejs Full support 8.0.0 |
compile | Chrome Full support 57 | Edge Full support 16 | Firefox
Full support
52
| IE No support No | Opera Full support 44 | Safari Full support 11 | WebView Android Full support 57 | Chrome Android Full support 57 | Firefox Android
Full support
52
| Opera Android Full support 43 | Safari iOS Full support 11 | Samsung Internet Android Full support 7.0 | nodejs Full support 8.0.0 |
compileStreaming | Chrome Full support 61 | Edge Full support 16 | Firefox Full support 58 | IE No support No | Opera Full support 47 | Safari No support No | WebView Android Full support 61 | Chrome Android Full support 61 | Firefox Android Full support 58 | Opera Android ? | Safari iOS No support No | Samsung Internet Android Full support 8.0 | nodejs No support No |
instantiate | Chrome Full support 57 | Edge Full support 16 | Firefox
Full support
52
| IE No support No | Opera Full support 44 | Safari Full support 11 | WebView Android Full support 57 | Chrome Android Full support 57 | Firefox Android
Full support
52
| Opera Android ? | Safari iOS Full support 11 | Samsung Internet Android Full support 7.0 | nodejs Full support 8.0.0 |
instantiateStreaming | Chrome Full support 61 | Edge Full support 16 | Firefox Full support 58 | IE No support No | Opera Full support 47 | Safari No support No | WebView Android Full support 61 | Chrome Android Full support 61 | Firefox Android Full support 58 | Opera Android ? | Safari iOS No support No | Samsung Internet Android Full support 8.0 | nodejs No support No |
validate | Chrome Full support 57 | Edge Full support 16 | Firefox
Full support
52
| IE No support No | Opera Full support 44 | Safari Full support 11 | WebView Android Full support 57 | Chrome Android Full support 57 | Firefox Android
Full support
52
| Opera Android ? | Safari iOS Full support 11 | Samsung Internet Android Full support 7.0 | nodejs Full support 8.0.0 |
Legend
- Full support
- Full support
- No support
- No support
- Compatibility unknown
- Compatibility unknown
- See implementation notes.
- See implementation notes.