W3cubDocs

/JavaScript

WebAssembly.instance

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.

Syntax

var myInstance = new WebAssembly.Instance(module, importObject);

Parameters

module
A WebAssembly Module instance e.g. created using WebAssembly.compile() or the WebAssembly.Module() constructor.
importObject Optional
An object containing definitions of functions that you want to import into the WebAssembly module instance, if any.

Instance constructor methods

None.

Instance instances

All Instance instances inherit from the Instance() constructor's prototype object — this can be modified to affect all Module instances.

Instance properties

Instance.prototype.constructor
Returns the function that created this object's instance. By default this is the WebAssembly.Instance() constructor.
Instance.prototype.exports Read only
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.

Instance methods

None.

Examples

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.

Specifications

Specification Status Comment
Web Assembly JavaScript API
The definition of 'Instance' in that specification.
Draft Initial draft definition.

Browser compatibility

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.

See also

  • WebAssembly landing page.
  • WebAssembly concepts
  • Using the WebAssembly JavaScript API

© 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