W3cubDocs

/JavaScript

WebAssembly.module.imports

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 imports() method of the WebAssembly.Module() constructor returns an array containing references to the imported functions available in a given WebAssembly module.

Syntax

var custSec = WebAssembly.Module.imports(module);

Parameters

module
A WebAssembly.Module object instance.

Return value

An array containing objects representing the imported functions of the given module.

Exceptions

If module is not a WebAssembly.Module object instance, a TypeError is thrown.

Example

The following example compiles and instantiates the loaded simple.wasm byte code, importing a JavaScript function into the WebAssembly Module in the process. It then exports a function from the module via Instance.exports.

In addition, we create two variables, storing in them the exports and imports properties of the module instance mod.

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();

      var exports = WebAssembly.Module.exports(mod);
      var imp = WebAssembly.Module.imports(mod);
      console.log(exports[0]);
      console.log(imp[0]);
    })
  })
});

In this case, console.log(imports[0]) returns an object containing information about the imported function imported_func(). It looks like this:

{ module: "imports", name: "imported_func", kind: "function" }

Specifications

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