WebAssembly JS API
8 methods浏览器与 Node.js 中操作 WebAssembly 模块的 JavaScript 接口集合。
WebAssembly.Module(bytes)从二进制字节码同步编译创建一个 WebAssembly 模块。
Parameters
| Name | Type | Description |
|---|---|---|
| bytes | BufferSource | 包含 wasm 二进制的 ArrayBuffer 或 TypedArray。 |
Returns
返回一个 WebAssembly.Module 实例。
Example
webassembly
const bytes = new Uint8Array([0x00, 0x61, 0x73, 0x6d, 0x01, 0x00, 0x00, 0x00]);
const module = new WebAssembly.Module(bytes);
console.log(module instanceof WebAssembly.Module); // trueWebAssembly.Instance(module, imports)基于已编译模块同步创建实例,可传入导入对象。
Parameters
| Name | Type | Description |
|---|---|---|
| module | WebAssembly.Module | 已编译的模块对象。 |
| imports | Object | 导入对象,包含函数、内存、表等。 |
Returns
返回一个 WebAssembly.Instance 实例,其 exports 属性为导出对象。
Example
webassembly
const importObject = {
imports: { imported_func: arg => console.log(arg) }
};
const instance = new WebAssembly.Instance(module, importObject);
const exportedFunc = instance.exports.exported_func;
exportedFunc();WebAssembly.instantiate(bytes, imports)一步完成编译与实例化,推荐使用,返回模块与实例。
Parameters
| Name | Type | Description |
|---|---|---|
| bytes | BufferSource | wasm 二进制数据。 |
| imports | Object | 导入对象。 |
Returns
返回 Promise<{ module, instance }>,module 为编译后的模块,instance 为实例。
Example
webassembly
const importObject = { imports: { print: console.log } };
WebAssembly.instantiate(bytes, importObject).then(result => {
const { module, instance } = result;
instance.exports.add(2, 3);
});
// async/await
const { instance } = await WebAssembly.instantiate(bytes, importObject);WebAssembly.compile(bytes)异步编译 wasm 字节码为模块,不创建实例。
Parameters
| Name | Type | Description |
|---|---|---|
| bytes | BufferSource | wasm 二进制数据。 |
Returns
返回 Promise<WebAssembly.Module>。
Example
webassembly
WebAssembly.compile(bytes).then(module => {
const instance = new WebAssembly.Instance(module);
console.log(instance.exports);
});
const module = await WebAssembly.compile(bytes);WebAssembly.validate(bytes)校验给定字节码是否为有效的 wasm 模块。
Parameters
| Name | Type | Description |
|---|---|---|
| bytes | BufferSource | 待校验的二进制数据。 |
Returns
返回布尔值,true 表示有效,false 表示无效。
Example
webassembly
const valid = WebAssembly.validate(bytes);
console.log(valid); // true
const invalid = WebAssembly.validate(new Uint8Array([0, 0, 0, 0]));
console.log(invalid); // falseWebAssembly.Memory(descriptor)创建一段线性内存,供 wasm 模块读写。
Parameters
| Name | Type | Description |
|---|---|---|
| descriptor | Object | { initial: number, maximum?: number }。 |
Returns
返回一个 WebAssembly.Memory 实例,buffer 属性为 ArrayBuffer。
Example
webassembly
const memory = new WebAssembly.Memory({ initial: 1, maximum: 10 });
const buffer = memory.buffer;
const view = new Uint32Array(memory.buffer);
view[0] = 42;
console.log(view[0]); // 42
memory.grow(1);WebAssembly.Table(descriptor)创建函数引用表,用于在 wasm 与 JS 间传递函数引用。
Parameters
| Name | Type | Description |
|---|---|---|
| descriptor | Object | { element: 'anyfunc', initial: number, maximum?: number }。 |
Returns
返回一个 WebAssembly.Table 实例,length 为表长度。
Example
webassembly
const table = new WebAssembly.Table({ element: 'anyfunc', initial: 1 });
console.log(table.length); // 1
table.set(0, funcRef);
const fn = table.get(0);WebAssembly.instantiateStreaming(source, imports)从网络 Response 流式编译并实例化 wasm,效率高于 instantiate。
Parameters
| Name | Type | Description |
|---|---|---|
| source | Promise<Response> | fetch 返回的 Response,Content-Type 须为 application/wasm。 |
| imports | Object | 导入对象。 |
Returns
返回 Promise<{ module, instance }>。
Example
webassembly
WebAssembly.instantiateStreaming(fetch('module.wasm'), importObject)
.then(({ instance }) => instance.exports.add(2, 3));
// async/await
const { instance } = await WebAssembly.instantiateStreaming(
fetch('module.wasm'),
importObject
);