首页
学习
活动
专区
工具
TVP
发布
精选内容/技术社群/优惠产品,尽在小程序
立即前往

WebAssembly.compile

这是一个实验中的功能

在使用此产品之前,请仔细检查浏览器兼容性表。

WebAssembly.compile()?方法编译WebAssembly二进制代码到一个WebAssembly.Module对象。如果在实例化之前有必要去编译一个模块,那么这个方法是有用的(否则,将会使用WebAssembly.instantiate()方法)

语法

代码语言:javascript
复制
Promise<WebAssembly.Module> WebAssembly.compile(bufferSource);

参数

bufferSource一个包含你想编译的wasm模块二进制代码的typed array(类型数组) orArrayBuffer(数组缓冲区)

返回值

一个解析为WebAssembly.ModulePromise?对象。

异常

示例

下面的例子 (查看GitHub上的?index-compile.html例子, 并且也能?查看运行效果) 使用compile()方法编译加载进来的 simple.wasm 二进制代码并且使用?postMessage()?发送给一个?worker

代码语言:javascript
复制
var worker = new Worker("wasm_worker.js");

fetch('simple.wasm').then(response =>
? response.arrayBuffer()
).then(bytes =>
? WebAssembly.compile(bytes)
).then(mod =>
? worker.postMessage(mod)
);

在线程中 (查看?wasm_worker.js) 我们定义了一个导入对象共模块使用,然后设置了一个事件处理函数来接收主线程发送过来的模块。当模块被接收之后,我们使用WebAssembly.Instantiate()方法创建了一个实例,调用从它里面导出的一个方法,接下来展示了我们可以用WebAssembly.Module.exports属性来调用模块上返回的可用信息。

代码语言:javascript
复制
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();
  });

  var exports = WebAssembly.Module.exports(mod);
  console.log(exports[0]);
};

规范

Specification

Status

Comment

Web Assembly JavaScript APIThe definition of 'compile()' in that specification.

Draft

Initial draft definition.

浏览器兼容性

Feature

Chrome

Edge

Firefox

Internet Explorer

Opera

Safari

Basic Support

57

16

522

No

44

11

Feature

Android

Chrome for Android

Edge mobile

Firefox for Android

IE mobile

Opera Android

iOS Safari

Basic Support

57

57

(Yes)1

522

No

?

11

扫码关注腾讯云开发者

领取腾讯云代金券

http://www.vxiaotou.com