files/en-us/webassembly/reference/javascript_interface/instantiate_static/index.md
The WebAssembly.instantiate() static method allows you to
compile and instantiate WebAssembly code. This function has two overloads:
Promise resolves to both a compiled
WebAssembly.Module and its first WebAssembly.Instance.WebAssembly.Module
and returns a Promise that resolves to an Instance of that
Module. This overload is useful if the Module has already
been compiled.[!WARNING] This method is not the most efficient way of fetching and instantiating Wasm modules. If at all possible, you should use the newer
WebAssembly.instantiateStreaming()method instead, which fetches, compiles, and instantiates a module all in one step, directly from the raw bytecode, so doesn't require conversion to an {{jsxref("ArrayBuffer")}}.
// Taking Wasm binary code
WebAssembly.instantiate(bufferSource)
WebAssembly.instantiate(bufferSource, importObject)
WebAssembly.instantiate(bufferSource, importObject, compileOptions)
// Taking a module object instance
WebAssembly.instantiate(module)
WebAssembly.instantiate(module, importObject)
WebAssembly.instantiate(module, importObject, compileOptions)
bufferSource
module
WebAssembly.Module object to be instantiated.importObject {{optional_inline}}
Instance, such as functions or WebAssembly.Memory objects.
There must be one matching property for each declared import of the compiled module or
else a WebAssembly.LinkError is thrown.compileOptions {{optional_inline}}
builtins {{optional_inline}}
"js-string", which enables JavaScript string builtins.importedStringConstants {{optional_inline}}
If a bufferSource is passed, returns a Promise that resolves to a ResultObject which contains two
fields:
module: A WebAssembly.Module object representing the compiled WebAssembly module. This Module can be instantiated again, shared via {{domxref("Worker.postMessage", "postMessage()")}}, or cached.instance: A WebAssembly.Instance object that contains all the Exported WebAssembly functions.If a module is passed, returns a Promise that resolves to a WebAssembly.Instance object.
WebAssembly.CompileError, WebAssembly.LinkError, or
WebAssembly.RuntimeError, depending on the cause of the failure.[!NOTE] You'll probably want to use
WebAssembly.instantiateStreaming()in most cases, as it is more efficient thaninstantiate().
After fetching some WebAssembly bytecode using fetch, we compile and instantiate the
module using the WebAssembly.instantiate() function, importing a
JavaScript function into the WebAssembly Module in the process. We then call an Exported WebAssembly function
that is exported by the Instance.
const importObject = {
my_namespace: {
imported_func(arg) {
console.log(arg);
},
},
};
fetch("simple.wasm")
.then((response) => response.arrayBuffer())
.then((bytes) => WebAssembly.instantiate(bytes, importObject))
.then((result) => result.instance.exports.exported_func());
[!NOTE] You can also find this example at index.html on GitHub (view it live also).
The following example (see our index-compile.html
demo on GitHub, and view it live also)
compiles the loaded simple.wasm byte code using the
WebAssembly.compileStreaming() method and then sends it to a worker using
{{domxref("Worker.postMessage", "postMessage()")}}.
const worker = new Worker("wasm_worker.js");
WebAssembly.compileStreaming(fetch("simple.wasm")).then((mod) =>
worker.postMessage(mod),
);
In the worker (see
wasm_worker.js)
we define an import object for the module to use, then set up an event handler to
receive the module from the main thread. When the module is received, we create an
instance from it using the WebAssembly.instantiate() method and invoke an
exported function from inside it.
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();
});
};
This example enables JavaScript string builtins and imported global string constants when compiling and instantiating the Wasm module with instantiate(), before running the exported main() function (which logs "hello world!" to the console). See it running live.
const importObject = {
// Regular import
m: {
log: console.log,
},
};
const compileOptions = {
builtins: ["js-string"], // Enable JavaScript string builtins
importedStringConstants: "string_constants", // Enable imported global string constants
};
fetch("log-concat.wasm")
.then((response) => response.arrayBuffer())
.then((bytes) => WebAssembly.instantiate(bytes, importObject, compileOptions))
.then((result) => result.instance.exports.main());
{{Specifications}}
{{Compat}}