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 instantiate() method of the WebAssembly object compiles a .wasm file loaded into the JavaScript context as a typed array (e.g. using Fetch/arrayBuffer into a Module object instance and then instantiates that module, all in one step — it returns an Instance object instance.
WebAssembly.instantiate(bufferSource, importObject).then(function(object) {
// do something with compiled module
}); A promise that resolves to an object representing the compiled and instantiated module. the object contains two members:
module: A Module object instance representing the compiled WebAssembly module.instance: An Instance object instance representing an instance of the compiled WebAssembly module.TypeError is thrown.WebAssembly.CompileError, WebAssembly.LinkError, or WebAssembly.RuntimeError, depending on the cause of the failure.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 'instantiate()' 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/instantiate