W3cubDocs

/JavaScript

WebAssembly.module

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 Module() constructor of the WebAssembly global object creates a new Module object instance.

A Module object instance is a JavaScript wrapper around a wasm module. You can create one using the WebAssembly.Module() constructor, or the WebAssembly.compile() method.

Syntax

var myModule = new WebAssembly.Module(bufferSource);

Parameters

bufferSource
An ArrayBuffer containing the byte code for a WebAssembly module, e.g. loaded using XMLHttpRequest or the Fetch API.

Module Constructor Methods

WebAssembly.Module.customSections()
Returns an array containing references to the custom sections available in a given WebAssembly module.
WebAssembly.Module.exports()
Returns an array containing references to the exported functions available in a given WebAssembly module.
WebAssembly.Module.imports()
Returns an array containing references to the imported functions available in a given WebAssembly module.

Module instances

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

Instance properties

Module.prototype.constructor
Returns the function that created this object's instance. By default this is the Number object.

Instance methods

Module instances have no default methods of their own.

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 WebAssembly.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();
    })
  })
});

The module instance in this case is the value returned when the compile() promise resolves (mod). You could also compile and instantiate using the relevant constructors, e.g.:

var testMod = new WebAssembly.Module(bytes);
var testInst = new WebAssembly.Instance(testMod, importObject);

Specifications

Specification Status Comment
Web Assembly JavaScript API
The definition of 'WebAssembly.Module()' 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/Module