[ Web Proxy ]
URL:
Viewing: https://developer.mozilla.org/ko/docs/WebAssembly/Reference/JavaScript_interface/instantiate_static [Back]  [Original]

WebAssembly.instantiate() - | MDN

This page was translated from English by the community. Learn more and join the MDN Web Docs community.

View in English Always switch to English

WebAssembly.instantiate()

Baseline Widely available *

This feature is well established and works across many devices and browser versions. Its been available across browsers since 2017 10.

* Some parts of this feature may have varying levels of support.

WebAssembly.instantiate() WebAssembly . overloads .

In this article

Syntax

Primary overload taking wasm binary code

Promise<ResultObject> WebAssembly.instantiate(bufferSource, importObject);

Parameters

bufferSource

.wasm typed array ArrayBuffer.

importObject Optional

WebAssembly.Memory . . WebAssembly.LinkError .

Return value

ResultObject Promise :

Exceptions

Secondary overload taking a module object instance

Promise<WebAssembly.Instance> WebAssembly.instantiate(module, importObject);

Parameters

module

WebAssembly.Module .

importObject Optional

WebAssembly.Memory . module . WebAssembly.LinkError .

Return value

A Promise that resolves to an WebAssembly.Instance object.

Exceptions

Examples

Note: instantiate() WebAssembly.instantiateStreaming () .

First overload example

fetch WebAssembly WebAssembly.instantiate () JavaScript WebAssembly . Instance Exported WebAssembly function .

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

fetch("simple.wasm")
  .then((response) => response.arrayBuffer())
  .then((bytes) => WebAssembly.instantiate(bytes, importObject))
  .then((result) => result.instance.exports.exported_func());

: GitHub index.html ( ).

Second overload example

(GitHub index-compile.html ). WebAssembly.compileStreaming () simple.wasm postMessage() worker .

js
var worker = new Worker("wasm_worker.js");

WebAssembly.compileStreaming(fetch("simple.wasm")).then((mod) =>
  worker.postMessage(mod),
);

(wasm_worker.js ) . WebAssembly.instantiate () .

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

onmessage = function (e) {
  console.log("module received from main thread");
  var mod = e.data;

  WebAssembly.instantiate(mod, importObject).then(function (instance) {
    instance.exports.exported_func();
  });
};

Specification
WebAssembly JavaScript Interface
# dom-webassembly-instantiate

See also


Web Proxy Viewer  |  New URL  |  Original Page