Back to Content

WebAssembly

files/en-us/webassembly/reference/javascript_interface/index.md

latest6.4 KB
Original Source

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 {{jsxref("Math")}}, which is also a namespace object for mathematical constants and functions, or to {{jsxref("Intl")}} which is the namespace object for internationalization constructors and other language-sensitive functions.

Description

The primary uses for the WebAssembly object are:

Interfaces

Static methods

Examples

Stream a Wasm module then compile and instantiate it

The following example (see our instantiate-streaming.html demo on GitHub, and view it live also) directly streams a Wasm module from an underlying source then compiles and instantiates it, the promise fulfilling with a ResultObject. Because the instantiateStreaming() function accepts a promise for a Response object, you can directly pass it a fetch() call, and it will pass the response into the function when it fulfills.

js
const importObject = {
  my_namespace: { imported_func: (arg) => console.log(arg) },
};

WebAssembly.instantiateStreaming(fetch("simple.wasm"), importObject).then(
  (obj) => obj.instance.exports.exported_func(),
);

The ResultObject's .instance property is then accessed, and the contained exported function invoked.

Specifications

{{Specifications}}

Browser compatibility

{{Compat}}

See also