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.Global()Global object.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.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.
| Specification | Status | Comment |
|---|---|---|
| WebAssembly JavaScript Interface The definition of 'WebAssembly' in that specification. | Working Draft | Initial draft definition. |
| Desktop | ||||||
|---|---|---|---|---|---|---|
| Chrome | Edge | Firefox | Internet Explorer | Opera | Safari | |
| Basic support | 57 | 16 | 52
|
No | 44 | 11 |
CompileError |
57 | 16 | 52
|
No | 44 | 11 |
Global |
69 | No | 62 | No | No | No |
Instance |
57 | 16 | 52
|
No | 44 | 11 |
LinkError |
57 | 16 | 52
|
No | 44 | 11 |
Memory |
57 | 16 | 52
|
No | 44 | 11 |
Module |
57 | 16 | 52
|
No | 44 | 11 |
RuntimeError |
57 | 16 | 52
|
No | 44 | 11 |
Table |
57 | 16 | 52
|
No | 44 | 11 |
compile |
57 | 16 | 52
|
No | 44 | 11 |
compileStreaming |
61 | 16 | 58 | No | 47 | No |
instantiate |
57 | 16 | 52
|
No | 44 | 11 |
instantiateStreaming |
61 | 16 | 58 | No | 47 | No |
validate |
57 | 16 | 52
|
No | 44 | 11 |
| Mobile | |||||||
|---|---|---|---|---|---|---|---|
| Android webview | Chrome for Android | Edge Mobile | Firefox for Android | Opera for Android | iOS Safari | Samsung Internet | |
| Basic support | 57 | 57 | Yes
|
52
|
? | 11 | 7.0 |
CompileError |
57 | 57 | Yes
|
52
|
? | 11 | 7.0 |
Global |
69 | 69 | No | 62 | No | No | No |
Instance |
57 | 57 | Yes
|
52
|
? | 11 | 7.0 |
LinkError |
57 | 57 | Yes
|
52
|
? | 11 | 7.0 |
Memory |
57 | 57 | Yes
|
52
|
? | 11 | 7.0 |
Module |
57 | 57 | Yes
|
52
|
? | 11 | 7.0 |
RuntimeError |
57 | 57 | Yes
|
52
|
? | 11 | 7.0 |
Table |
57 | 57 | Yes
|
52
|
? | 11 | 7.0 |
compile |
57 | 57 | Yes
|
52
|
44 | 11 | 7.0 |
compileStreaming |
61 | 61 | No | 58 | ? | No | No |
instantiate |
57 | 57 | Yes
|
52
|
? | 11 | 7.0 |
instantiateStreaming |
61 | 61 | No | 58 | ? | No | No |
validate |
57 | 57 | Yes
|
52
|
? | 11 | 7.0 |
| Server | |
|---|---|
| Node.js | |
| Basic support | 8.0.0 |
CompileError |
8.0.0 |
Global |
No |
Instance |
8.0.0 |
LinkError |
8.0.0 |
Memory |
8.0.0 |
Module |
8.0.0 |
RuntimeError |
8.0.0 |
Table |
8.0.0 |
compile |
8.0.0 |
compileStreaming |
No |
instantiate |
8.0.0 |
instantiateStreaming |
No |
validate |
8.0.0 |
© 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