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.
The primary uses for the WebAssembly object are:
WebAssembly.instantiate() function.WebAssembly.Memory()/WebAssembly.Table() constructors.WebAssembly.CompileError()/WebAssembly.LinkError()/WebAssembly.RuntimeError() constructors.WebAssembly.instantiate()Module and its first Instance.WebAssembly.instantiateStreaming()Module and its first Instance.WebAssembly.compile()WebAssembly.Module from WebAssembly binary code, leaving instantiation as a separate step.WebAssembly.compileStreaming()WebAssembly.Module directly from a streamed underlying source, leaving instantiation as a separate step.WebAssembly.validate()true) or not (false).WebAssembly.Module()Module object.WebAssembly.Instance()Instance object.WebAssembly.Memory()Memory object.WebAssembly.Table()Table object.WebAssembly.CompileError()CompileError object.WebAssembly.LinkError()LinkError object.WebAssembly.RuntimeError()RuntimeError object.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. This promise resolves to an object (result) containing the compiled Module and Instance objects. We then call an Exported WebAssembly function that is exported by the Instance.
var importObject = {
imports: {
imported_func: function(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: See index.html on GitHub (view it live also) for an example that makes use of our fetchAndInstantiate() library function.
| Specification | Status | Comment |
|---|---|---|
| WebAssembly JavaScript API The definition of 'WebAssembly' in that specification. | Draft | Initial draft definition. |
| Feature | Chrome | Edge | Firefox | Internet Explorer | Opera | Safari |
|---|---|---|---|---|---|---|
| Basic support | 57 | 16 | 522 | No | 44 | 11 |
CompileError |
57 | 16 | 522 | No | 44 | 11 |
Instance |
57 | 16 | 522 | No | 44 | 11 |
LinkError |
57 | 16 | 522 | No | 44 | 11 |
Memory |
57 | 16 | 522 | No | 44 | 11 |
Module |
57 | 16 | 522 | No | 44 | 11 |
RuntimeError |
57 | 16 | 522 | No | 44 | 11 |
Table |
57 | 16 | 522 | No | 44 | 11 |
compile |
57 | 16 | 522 | No | 44 | 11 |
compileStreaming |
61 | No | 58 | No | 47 | ? |
instantiate |
57 | 16 | 522 | No | 44 | 11 |
instantiateStreaming |
61 | No | 58 | No | 47 | ? |
validate |
57 | 16 | 522 | No | 44 | 11 |
| Feature | Android webview | Chrome for Android | Edge mobile | Firefox for Android | Opera Android | iOS Safari | Samsung Internet |
|---|---|---|---|---|---|---|---|
| Basic support | 57 | 57 | Yes1 | 522 | ? | 11 | ? |
CompileError |
57 | 57 | Yes1 | 522 | ? | 11 | ? |
Instance |
57 | 57 | Yes1 | 522 | ? | 11 | ? |
LinkError |
57 | 57 | Yes1 | 522 | ? | 11 | ? |
Memory |
57 | 57 | Yes1 | 522 | ? | 11 | ? |
Module |
57 | 57 | Yes1 | 522 | ? | 11 | ? |
RuntimeError |
57 | 57 | Yes1 | 522 | ? | 11 | ? |
Table |
57 | 57 | Yes1 | 522 | ? | 11 | ? |
compile |
57 | 57 | Yes1 | 522 | ? | 11 | ? |
compileStreaming |
61 | 61 | No | 58 | ? | ? | ? |
instantiate |
57 | 57 | Yes1 | 522 | ? | 11 | ? |
instantiateStreaming |
61 | 61 | No | 58 | ? | ? | ? |
validate |
57 | 57 | Yes1 | 522 | ? | 11 | ? |
1. This feature is behind the Experimental JavaScript Features preference.
2. Disabled in the Firefox 52 Extended Support Release (ESR).
© 2005–2018 Mozilla Developer Network and individual 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