A WebAssembly.Module() constructor creates a new Module object containing stateless WebAssembly code that has already been compiled by the browser and can be efficiently shared with Workers , and instantiated multiple times.

WebAssembly.Module() constructor function can be called to synchronously compile given WebAssembly binary code. However, the primary way to get a Module is through an asynchronous compilation function like WebAssembly.compile() .

句法

Important : Since compilation for large modules can be expensive, developers should only use the Module() constructor when synchronous compilation is absolutely required; the asynchronous WebAssembly.compileStreaming() method should be used at all other times.

new WebAssembly.Module(bufferSource);
					

参数

bufferSource
A typed array or ArrayBuffer containing the binary code of the .wasm module you want to compile.

范例

Synchronously compiling a WebAssembly module

var importObject = {
  imports: {
    imported_func: function(arg) {
      console.log(arg);
    }
  }
};
function createWasmModule(bytes) {
  return new WebAssembly.Module(bytes);
}
fetch('simple.wasm').then(response =>
  response.arrayBuffer()
).then(bytes => {
  let mod = createWasmModule(bytes);
  WebAssembly.instantiate(mod, importObject)
  .then(result =>
     result.exports.exported_func()
  );
})
					

规范

规范
WebAssembly JavaScript 接口
The definition of 'WebAssembly.Module()' in that specification.

浏览器兼容性

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. 更新 GitHub 上的兼容性数据
Desktop Mobile Server
Chrome Edge Firefox Internet Explorer Opera Safari Android webview Chrome for Android Firefox for Android Opera for Android Safari on iOS Samsung Internet Node.js
Module() 构造函数 Chrome 57 Edge 16 Firefox 52
52
Disabled in the Firefox 52 Extended Support Release (ESR).
IE No Opera 44 Safari 11 WebView Android 57 Chrome Android 57 Firefox Android 52
52
Disabled in the Firefox 52 Extended Support Release (ESR).
Opera Android 43 Safari iOS 11 Samsung Internet Android 7.0 nodejs 8.0.0

图例

完整支持

完整支持

不支持

不支持

见实现注意事项。

另请参阅

元数据

  • 最后修改: