The WebAssembly.Instance()
constructor creates a new Instance
object which is a stateful, executable instance of a WebAssembly.Module
.
WebAssembly.Instance() constructor
Syntax
Warning: 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
Browser compatibility
Desktop | Mobile | Server | ||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
Chrome | Edge | Firefox | Internet Explorer | Opera | Safari | WebView Android | Chrome Android | Firefox for Android | Opera Android | Safari on IOS | Samsung Internet | Deno | Node.js | |
Instance |
57
|
16
|
52
Disabled in the Firefox 52 Extended Support Release (ESR).
|
No
|
44
|
11
|
57
|
57
|
52
Disabled in the Firefox 52 Extended Support Release (ESR).
|
43
|
11
|
7.0
|
1.0
|
8.0.0
|
See also
© 2005–2022 MDN 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/Instance/Instance