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 acts as the namespace for all WebAssembly-related functionality.

Unlike most other global objects, WebAssembly is not a constructor (it is not a function object). You can compare it to Math, which is also a namespace object for mathematical constants and functions, or to Intl which is the namespace object for internationalization constructors and other language sensitive functions.

Description

The primary uses for the WebAssembly object are:

Methods

WebAssembly.instantiate()
The primary API for compiling and instantiating WebAssembly code, returning both a Module and its first Instance.
WebAssembly.compile()
Compiles a WebAssembly.Module from WebAssembly binary code, leaving instantiation as a separate step.
WebAssembly.validate()
Validates a given typed array of WebAssembly binary code, returning whether the bytes are valid WebAssembly code (true) or not (false).

Constructors

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

Examples

After fetching some WebAssembly bytecode using fetch, we compile and instantiate the module using the WebAssembly.instantiate() function, importing a JavaScript function into the WebAssembly Module in the process. This promise resolves to an object (result) containing the compiled Module and Instance objects. We then call an Exported WebAssembly function that is exported by the Instance.

var importObject = {
  imports: {
    imported_func: function(arg) {
      console.log(arg);
    }
  }
};
fetch('simple.wasm').then(response =>
  response.arrayBuffer()
).then(bytes =>
  WebAssembly.instantiate(bytes, importObject)
).then(result =>
  result.instance.exports.exported_func()
);

Note: See index.html on GitHub (view it live also) for an example that makes use of our fetchAndInstantiate() library function.

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 57 15[2] 52 (52)[1] No support 44 11
Feature Chrome for Android Android Webview Edge Mobile Firefox Mobile (Gecko) IE Mobile Opera Mobile Safari Mobile
Basic support 57 57 No support 52.0 (52)[1] No support No support 11

[1] WebAssembly is enabled in Firefox 52+, although disabled in the Firefox 52 Extended Support Release (ESR.)

[2] Currently supported behind the “Experimental JavaScript Features” flag. See this blog post for more details.

See also

Document Tags and Contributors

 Contributors to this page: chrisdavidmills, emilbayes, apurv-pandey, lukewagner, fscholz
 Last updated by: chrisdavidmills,