This is an experimental technology
Because this technology's specification has not stabilized, check the compatibility table for usage in various browsers. Also note that the syntax and behavior of an experimental technology is subject to change in future versions of browsers as the specification changes.
The Instance() constructor of the WebAssembly global object instantiates a Module instance to create a new Instance object instance.
An Instance instance can be thought of as a JavaScript wrapper around a WebAssembly module instance. You can create one using the WebAssembly.Instance() constructor, or the WebAssembly.instantiate() method.
var myInstance = new WebAssembly.Instance(module, importObject);
WebAssembly.compile() or the WebAssembly.Module() constructor.Instance constructor methodsNone.
Instance instancesAll Instance instances inherit from the Instance() constructor's prototype object — this can be modified to affect all Module instances.
Instance.prototype.constructorWebAssembly.Instance() constructor.Instance.prototype.exports Read only
None.
The following example compiles and instantiates the loaded simple.wasm byte code using the WebAssembly.instantiate() method, importing a JavaScript function into the WebAssembly Module in the process — this promise resolves to an object (obj) containing the compiled Module instance, and it's instantiated Instance instance. We then export a function from the module via Instance.exports.exported_func().
var importObject = {
imports: {
imported_func: function(arg) {
console.log(arg);
}
}
};
fetch('simple.wasm').then(function(response) {
response.arrayBuffer().then(function(bytes) {
WebAssembly.instantiate(bytes, importObject).then(function(obj) {
obj.instance.exports.exported_func();
})
})
}); Note: See index.html on GitHub (view it live also) for an example that makes use of Instance.
| Specification | Status | Comment |
|---|---|---|
| Web Assembly JavaScript API The definition of 'Instance' in that specification. | Draft | Initial draft definition. |
| Feature | Chrome | Edge | Firefox (Gecko) | Internet Explorer | Opera | Safari (WebKit) |
|---|---|---|---|---|---|---|
| Basic support | No support[1] | No support | No support[2] | No support | No support[1] | No support |
| Feature | Android | Android Webview | Edge | Firefox Mobile (Gecko) | IE Phone | Opera Mobile | Safari Mobile | Chrome for Android |
|---|---|---|---|---|---|---|---|---|
| Basic support | No support | No support[1] | No support | No support[2] | No support | No support | No support | No support[1] |
[1] Experimental support can be enabled in Chrome 51+ and Opera 38+ by going to chrome://flags and enabling the Experimental WebAssembly flag.
[2] Experimental support can be enabled in Firefox 47+ by enabling the javascript.options.wasm flag in about:config.
© 2005–2017 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/Instance