WebAssembly

Baseline Widely available *

This feature is well established and works across many devices and browser versions. It’s been available across browsers since October 2017.

* Some parts of this feature may have varying levels of support.

WebAssembly JavaScript 对象是所有 WebAssembly 相关功能的命名空间。

和大多数全局对象不一样,WebAssembly不是一个构造函数(它不是一个函数对象)。它类似于 Math 对象或者 Intl 对象,Math 对象也是一个命名空间对象,用于保存数学常量和函数;Intl 则是用于国际化和其他语言相关函数的命名空间对象。

描述

WebAssembly 对象主要用于:

方法

WebAssembly.instantiate()

用于编译和实例化 WebAssembly 代码的主 API,返回一个 Module 和它的第一个Instance实例。

WebAssembly.instantiateStreaming()

直接从流式底层源编译和实例化 WebAssembly 模块,同时返回Module及其第一个Instance实例。

WebAssembly.compile()

把 WebAssembly 二进制代码编译为一个 WebAssembly.Module ,不进行实例化。

WebAssembly.compileStreaming()

直接从流式底层源代码编译WebAssembly.Module ,将实例化作为一个单独的步骤。

WebAssembly.validate()

校验 WebAssembly 二进制代码的类型数组是否合法,合法则返回 true,否则返回 false。

构造器

WebAssembly.Global()

创建一个新的 WebAssembly Global 全局对象。

WebAssembly.Module()

创建一个新的 WebAssembly Module模块对象。

WebAssembly.Instance()

创建一个新的 WebAssembly Instance实例对象。

WebAssembly.Memory()

创建一个新的 WebAssembly Memory内存对象。

WebAssembly.Table()

创建一个新的 WebAssembly Table表格对象。

WebAssembly.CompileError()

创建一个新的 WebAssembly CompileError编译错误对象。

WebAssembly.LinkError()

创建一个新的 WebAssembly LinkError链接错误对象。

WebAssembly.RuntimeError()

创建一个新的 WebAssembly RuntimeError运行时错误对象。

示例

下面的示例(请参见 GitHub 上的 Instantiate-streaming.html 演示,并查看在线演示)直接从流式底层源传输 .wasm 模块,然后对其进行编译和实例化,并通过 ResultObject 实现 promise。由于 instantiateStreaming() 函数接受对 Response 对象的 promise,因此你可以直接向其传递 fetch() 调用,然后它将把返回的 response 传递给随后的函数。

js
var importObject = { imports: { imported_func: (arg) => console.log(arg) } };

WebAssembly.instantiateStreaming(fetch("simple.wasm"), importObject).then(
  (obj) => obj.instance.exports.exported_func(),
);

返回的ResultObject实例的成员可以被随后访问到,可以调用实例中被导出的方法。

规范

Specification
WebAssembly JavaScript Interface
# webassembly-namespace

浏览器兼容性

Report problems with this compatibility data on GitHub
desktopmobileserver
Chrome
Edge
Firefox
Opera
Safari
Chrome Android
Firefox for Android
Opera Android
Safari on iOS
Samsung Internet
WebView Android
WebView on iOS
Deno
Node.js
api
CompileError
CompileError() constructor
Exception
Exception() constructor
options.traceStack parameter
Non-standard
Exception.getArg
Exception.is
Stack trace
Non-standard
Global
Global() constructor
Global.value
Global.valueOf
Instance
Instance() constructor
Instance.exports
LinkError
LinkError() constructor
Memory
Memory() constructor
shared flag
Memory.buffer
Memory.grow
Module
Module() constructor
compileOptions parameter
customSections() static method
exports() static method
imports() static method
RuntimeError
RuntimeError() constructor
Table
Table() constructor
Table.get
Table.grow
Table.length
Table.set
Tag
Tag() constructor
Tag.type
compileStreaming() static method
compileOptions parameter
compile() static method
compileOptions parameter
instantiateStreaming() static method
compileOptions parameter
instantiate() static method
compileOptions parameter
validate() static method
compileOptions parameter

Legend

Tip: you can click/tap on a cell for more information.

Full support
Full support
No support
No support
Non-standard. Check cross-browser support before using.
See implementation notes.
User must explicitly enable this feature.

参见