The WebAssembly.Instance()
constructor creates a new Instance
object which is a stateful, executable instance of a WebAssembly.Module
.
Syntax
Important: Since instantiation for large modules can be expensive, developers should only use the Instance()
constructor when synchronous instantiation is absolutely required; the asynchronous WebAssembly.instantiateStreaming()
method should be used at all other times.
new WebAssembly.Instance(module, importObject);
Parameters
- module
- The
WebAssembly.Module
object to be instantiated. - importObject Optional
- An object containing the values to be imported into the newly-created
Instance
, such as functions orWebAssembly.Memory
objects. There must be one matching property for each declared import ofmodule
or else aWebAssembly.LinkError
is thrown.
Examples
Synchronously instantiating a WebAssembly module
The WebAssembly.Instance()
constructor function can be called to synchronously instantiate a given WebAssembly.Module
object, for example:
const importObject = { imports: { imported_func: function(arg) { console.log(arg); } } }; fetch('simple.wasm').then(response => response.arrayBuffer() ).then(bytes => { let mod = new WebAssembly.Module(bytes); let instance = new WebAssembly.Instance(mod, importObject); instance.exports.exported_func(); })
However, the preferred way to get an Instance
is through the asynchronous WebAssembly.instantiateStreaming()
function, for example like this:
const importObject = { imports: { imported_func: function(arg) { console.log(arg); } } }; WebAssembly.instantiateStreaming(fetch('simple.wasm'), importObject) .then(obj => obj.instance.exports.exported_func());
Specifications
Specification |
---|
WebAssembly JavaScript Interface The definition of 'Instance' in that specification. |
Browser compatibility
The compatibility table on this page is generated from structured data. If you'd like to contribute to the data, please check out https://github.com/mdn/browser-compat-data and send us a pull request.
Desktop | Mobile | Server | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|
Instance() constructor | 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 |
Legend
- Full support
- Full support
- No support
- No support
- See implementation notes.
- See implementation notes.