Movatterモバイル変換


[0]ホーム

URL:


MDN Web Docs

WebAssembly.Module

BaselineWidely available *

AWebAssembly.Module object contains stateless WebAssembly code that has already been compiled by the browser — this can be efficientlyshared with Workers, and instantiated multiple times.

Note:TheWebAssembly.Module object is unrelated to theModule object used in Emscripten.

Constructor

WebAssembly.Module()

Creates a newModule object.

Static methods

WebAssembly.Module.customSections()

Given aModule and string, returns a copy of the contents of all custom sections in the module with the given string name.

WebAssembly.Module.exports()

Given aModule, returns an array containing descriptions of all the declared exports.

WebAssembly.Module.imports()

Given aModule, returns an array containing descriptions of all the declared imports.

Examples

Sending a compiled module to a worker

The following example compiles the loadedsimple.wasm byte code using theWebAssembly.compileStreaming() method and sends the resultingModule instance to aworker usingpostMessage().

See theindex-compile.htmlsource code orview it live.

js
const worker = new Worker("wasm_worker.js");WebAssembly.compileStreaming(fetch("simple.wasm")).then((mod) =>  worker.postMessage(mod),);

The worker functionwasm_worker.js defines an import object for the module to use. The function then sets up an event handler to receive the module from the main thread. When the module is received, we create an instance from it using theWebAssembly.instantiate() method and invoke an exported function from inside it.

js
const importObject = {  my_namespace: {    imported_func(arg) {      console.log(arg);    },  },};onmessage = (e) => {  console.log("module received from main thread");  const mod = e.data;  WebAssembly.instantiate(mod, importObject).then((instance) => {    instance.exports.exported_func();  });};

Specifications

Specification
WebAssembly JavaScript Interface
# modules

Browser compatibility

See also

Help improve MDN

Learn how to contribute.

This page was last modified on byMDN contributors.


[8]ページ先頭

©2009-2025 Movatter.jp