The WebAssembly.compile() function compiles WebAssembly binary code into a WebAssembly.Module object. This function is useful if it is necessary to a compile a module before it can be instantiated (otherwise, the WebAssembly.instantiate() function should be used).
Syntax
Promise<WebAssembly.Module> WebAssembly.compile(bufferSource);
Parameters
- bufferSource
- A typed array or ArrayBuffer containing the binary code of the .wasm module you want to compile.
Return value
A Promise that resolves to a WebAssembly.Module object representing the compiled module.
Exceptions
- If
bufferSourceis not a typed array, aTypeErroris thrown. - If compilation fails, the promise rejects with a
WebAssembly.CompileError.
Examples
Using compile
The following example compiles the loaded simple.wasm byte code using the compile() function and then sends it to a worker using postMessage().
var worker = new Worker("wasm_worker.js");
fetch('simple.wasm').then(response =>
response.arrayBuffer()
).then(bytes =>
WebAssembly.compile(bytes)
).then(mod =>
worker.postMessage(mod)
);
Note: You'll probably want to use WebAssembly.compileStreaming() in most cases, as it is more efficient than compile().
Specifications
| Specification |
|---|
| WebAssembly JavaScript Interface The definition of 'compile()' in that specification. |
Browser compatibility
The compatibility table on this page is generated from structured data. If you'd like to contribute to the data, please check out https://github.com/mdn/browser-compat-data and send us a pull request.
| Desktop | Mobile | Server | |||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
compile | Chrome Full support 57 | Edge Full support 16 | Firefox
Full support
52
| IE No support No | Opera Full support 44 | Safari Full support 11 | WebView Android Full support 57 | Chrome Android Full support 57 | Firefox Android
Full support
52
| Opera Android Full support 43 | Safari iOS Full support 11 | Samsung Internet Android Full support 7.0 | nodejs Full support 8.0.0 |
Legend
- Full support
- Full support
- No support
- No support
- See implementation notes.
- See implementation notes.
