This is an experimental technology
Because this technology's specification has not stabilized, check the compatibility table for usage in various browsers. Also note that the syntax and behavior of an experimental technology is subject to change in future versions of browsers as the specification changes.
The compile() method of the WebAssembly object compiles a .wasm file loaded into the JavaScript context as a typed array (e.g. using Fetch/arrayBuffer into a Module object instance.
WebAssembly.compile(bufferSource).then(function(module) {
// do something with compiled module
}); A promise that resolves to a Module instance representing the compiled module.
TypeError is thrown.WebAssembly.CompileError.The following example (see our index-compile.html test file on GitHub, and view it live also) compiles the loaded simple.wasm byte code using the compile() method, and then creates an instance using the WebAssembly.Instance() constructor.
It then imports a JavaScript function into the WebAssembly Module in the process, and then exports a function from the module via Instance.exports.
var importObject = {
imports: {
imported_func: function(arg) {
console.log(arg);
}
}
};
fetch('simple.wasm').then(function(response) {
response.arrayBuffer().then(function(bytes) {
WebAssembly.compile(bytes).then(function(mod) {
var instance = new WebAssembly.Instance(mod, importObject);
instance.exports.exported_func();
})
})
}); | Specification | Status | Comment |
|---|---|---|
| Unknown The definition of 'compile()' in that specification. | Unknown | Initial draft definition. |
| Feature | Chrome | Edge | Firefox (Gecko) | Internet Explorer | Opera | Safari (WebKit) |
|---|---|---|---|---|---|---|
| Basic support | No support[1] | No support | No support[2] | No support | No support[1] | No support |
| Feature | Android | Android Webview | Edge | Firefox Mobile (Gecko) | IE Phone | Opera Mobile | Safari Mobile | Chrome for Android |
|---|---|---|---|---|---|---|---|---|
| Basic support | No support | No support[1] | No support | No support[2] | No support | No support | No support | No support[1] |
[1] Experimental support can be enabled in Chrome 51+ and Opera 38+ by going to chrome://flags and enabling the Experimental WebAssembly flag.
[2] Experimental support can be enabled in Firefox 47+ by enabling the javascript.options.wasm flag in about:config.
© 2005–2017 Mozilla Developer Network and individual contributors.
Licensed under the Creative Commons Attribution-ShareAlike License v2.5 or later.
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/WebAssembly/compile