W3cubDocs

/JavaScript

WebAssembly.instance

A WebAssembly.Instance object is a stateful, executable instance of a WebAssembly.Module. Instance objects contain all the Exported WebAssembly functions that allow calling into WebAssembly code from JavaScript.

Constructor

WebAssembly.Instance()
Creates a new Instance object.

Instance properties

Instance.prototype.exports
Returns an object containing as its members all the functions exported from the WebAssembly module instance, to allow them to be accessed and used by JavaScript. Read-only.

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();
})

The preferred way to get an Instance is asynchronously, for example using the WebAssembly.instantiateStreaming() function 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());

This also demonstrates how the exports property is used to access exported functions.

Specifications

Browser compatibilityUpdate compatibility data on GitHub

Desktop
Chrome Edge Firefox Internet Explorer Opera Safari
Instance 57 16 52
52
Disabled in the Firefox 52 Extended Support Release (ESR).
No 44 11
Instance() constructor 57 16 52
52
Disabled in the Firefox 52 Extended Support Release (ESR).
No 44 11
exports 57 16 52
52
Disabled in the Firefox 52 Extended Support Release (ESR).
No 44 11
Mobile
Android webview Chrome for Android Firefox for Android Opera for Android Safari on iOS Samsung Internet
Instance 57 57 52
52
Disabled in the Firefox 52 Extended Support Release (ESR).
43 11 7.0
Instance() constructor 57 57 52
52
Disabled in the Firefox 52 Extended Support Release (ESR).
43 11 7.0
exports 57 57 52
52
Disabled in the Firefox 52 Extended Support Release (ESR).
43 11 7.0
Server
Node.js
Instance 8.0.0
Instance() constructor 8.0.0
exports 8.0.0

See also

© 2005–2018 Mozilla Developer Network and individual contributors.
Licensed under the Creative Commons Attribution-ShareAlike License v2.5 or later.
https://wiki.developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/WebAssembly/Instance