W3cubDocs

/JavaScript

WebAssembly.compile

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 compile() 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.

Syntax

WebAssembly.compile(bufferSource).then(function(module) {
  // do something with compiled module
});

Parameters

bufferSource
A typed array containing the byte code of the .wasm module you want to compile.

Return value

A promise that resolves to a Module instance representing the compiled module.

Exceptions

Examples

The following example (see our index-compile.html test file on GitHub, and view it live also) compiles the loaded simple.wasm byte code using the compile() method, and then creates an instance using the WebAssembly.Instance() constructor.

It then imports a JavaScript function into the WebAssembly Module in the process, and then exports a function from the module via Instance.exports.

var importObject = {
  imports: {
    imported_func: function(arg) {
      console.log(arg);
    }
  }
};

fetch('simple.wasm').then(function(response) {
  response.arrayBuffer().then(function(bytes) {
    WebAssembly.compile(bytes).then(function(mod) {
      var instance = new WebAssembly.Instance(mod, importObject);
      instance.exports.exported_func();
    })
  })
});

Specifications

Specification Status Comment
Unknown
The definition of 'compile()' in that specification.
Unknown 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/compile