W3cubDocs

/JavaScript

WebAssembly

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 WebAssembly JavaScript object represents the WebAssembly environment in JavaScript, and acts as the namespace for all WebAssembly-related functionality.

Syntax

WebAssembly

Description

The primary uses for the WebAssembly object are:

Constructors

WebAssembly.Module()
Creates a new WebAssembly Module object instance.
WebAssembly.Instance()
Creates a new WebAssembly Instance object instance.
WebAssembly.Memory()
Creates a new WebAssembly Memory object instance.
WebAssembly.Table()
Creates a new WebAssembly Table object instance.
WebAssembly.CompileError()
Creates a new WebAssembly CompileError object instance.
WebAssembly.LinkError()
Creates a new WebAssembly LinkError object instance.
WebAssembly.RuntimeError()
Creates a new WebAssembly RuntimeError object instance.

Methods

WebAssembly.compile()
Compiles a .wasm file loaded into the JavaScript context as a typed array (e.g. using Fetch/arrayBuffer into a Module object instance.
WebAssembly.instantiate()
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.validate()
Validates a given array buffer source, returning a Boolean that specifies whether the source is valid wasm code (true) or not (false).

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 'WebAssembly' 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