WebAssembly是一种新的编码方式,可以在现代的网络浏览器中运行 - 它是一种低级的类汇编语言,具有紧凑的二进制格式,可以接近原生的性能运行,并为诸如C / C ++等语言提供一个编译目标,以便它们可以在Web上运行。它也被设计为可以与JavaScript共存,允许两者一起工作。
简而言之
对于网络平台而言,WebAssembly具有巨大的意义——它提供了一条途径,以使得以各种语言编写的代码都可以以接近原生的速度在Web中运行。在这种情况下,以前无法以此方式运行的客户端软件都将可以运行在Web中。
WebAssembly被设计为可以和JavaScript一起协同工作——通过使用WebAssembly的JavaScript API,你可以把WebAssembly模块加载到一个JavaScript应用中并且在两者之间共享功能。这允许你在同一个应用中利用WebAssembly的性能和威力以及JavaScript的表达力和灵活性,即使你可能并不知道如何编写WebAssembly代码。
而且,更棒的是,这是通过W3C WebAssembly Community Group开发的一项网络标准,并得到了来自各大主要浏览器厂商的积极参与。
指南
- WebAssembly概念
- 通过阅读WebAssembly后面的高层概念开始——也就是说,它为什么如此有用,它是如何适应网络平台的(以及其他)和如何使用它。
- 从C/C++编译为WebAssembly
- 当你使用C/C++编写了代码后,你可以使用诸如Emscripten的工具把它编译为.wasm文件。让我们看看这是如何工作的。
- 加载运行WebAssembly代码
- 本文介绍如何通过把Fetch或者XHR API与WebAssembly JavaScript API结合在一起来获取、编译和实例化.wasm文件。
- 缓存编译后的WebAssembly模块
- 就提升应用启动速度而言,在客户端缓存大的WebAssembly模块是有用的。本文解释了如何使用IndexedDB来实现这一点。
- 使用WebAssembly的JavaScript API
- 当加载了一个.wasm模块之后,你就想要使用它。在本文中,我们向你展示了如何通过WebAssembly的JavaScript API来使用WebAssembly。
- 导出的WebAssembly函数
- 导出的WebAssembly函数是WebAssembly函数的JavaScript表现形式,并且允许从JavaScript中调用WebAssembly代码。本文描述它们。
- 理解WebAssembly的文本格式
- 本文介绍了wasm的文本格式。这是在进行调试的时候浏览器开发者工具中展现出来的.wasm模块的一种低级文本表示。
- 把WebAssembly文本格式转换为wasm
- 本文提供了一个如何把编写的文本格式的WebAssembly模块转换为 wasm二进制的指南。
API参考
WebAssembly
- 本对象是所有与WebAssembly相关功能的命名空间。
WebAssembly.Module
- 一个
WebAssembly.Module
对象包括了无状态的WebAssembly代码。该代码已经被浏览器编译并且能够通过Workers高效地共享,缓存到IndexedDB中以及多次实例化。 WebAssembly.Instance
- 一个对象
WebAssembly.Instance
对象是一个有状态的、可执行的模块的实例。实例对象包含所有的能够从JavaScript调用到WebAssembly代码的导出的WebAssembly函数。 WebAssembly.instantiate()
WebAssembly.instantiate()
函数是编译和实例化WebAssembly代码的主要的API,它返回一个Module
及其第一个实例。WebAssembly.Memory()
- 一个
WebAssembly.Memory
对象是一个可变长的ArrayBuffer
。它拥有能够被实例存取的原始字节内存。 WebAssembly.Table()
WebAssembly.Table
对象是一个可变长类型数组。它存储诸如函数引用之类的不透明值并且能够被实例存取。WebAssembly.CompileError()
- 创建一个新的WebAssembly
CompileError
对象。 WebAssembly.LinkError()
- 创建一个新的WebAssembly
LinkError
对象。 WebAssembly.RuntimeError()
- 创建一个新的WebAssembly
RuntimeError
对象。
示例
- WASMSobel
- 参考我们的WebAssembly示例库来获取一些其他的例子。
规范
Specification | Status | Comment |
---|---|---|
WebAssembly JavaScript Interface | Working Draft | Initial draft definition of the JavaScript API. |
浏览器兼容性
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.
Feature | Chrome | Edge | Firefox | Internet Explorer | Opera | Safari |
---|---|---|---|---|---|---|
Basic support | 57 | 16 | 522 | No | 44 | 11 |
CompileError | 57 | 16 | 522 | No | 44 | 11 |
Instance | 57 | 16 | 522 | No | 44 | 11 |
LinkError | 57 | 16 | 522 | No | 44 | 11 |
Memory | 57 | 16 | 522 | No | 44 | 11 |
Module | 57 | 16 | 522 | No | 44 | 11 |
RuntimeError | 57 | 16 | 522 | No | 44 | 11 |
Table | 57 | 16 | 522 | No | 44 | 11 |
compile | 57 | 16 | 522 | No | 44 | 11 |
compileStreaming | 61 | No | 58 | No | 47 | ? |
instantiate | 57 | 16 | 522 | No | 44 | 11 |
instantiateStreaming | 61 | No | 58 | No | 47 | ? |
validate | 57 | 16 | 522 | No | 44 | 11 |
Feature | Android webview | Chrome for Android | Edge mobile | Firefox for Android | Opera Android | iOS Safari | Samsung Internet |
---|---|---|---|---|---|---|---|
Basic support | 57 | 57 | Yes1 | 522 | ? | 11 | ? |
CompileError | 57 | 57 | Yes1 | 522 | ? | 11 | ? |
Instance | 57 | 57 | Yes1 | 522 | ? | 11 | ? |
LinkError | 57 | 57 | Yes1 | 522 | ? | 11 | ? |
Memory | 57 | 57 | Yes1 | 522 | ? | 11 | ? |
Module | 57 | 57 | Yes1 | 522 | ? | 11 | ? |
RuntimeError | 57 | 57 | Yes1 | 522 | ? | 11 | ? |
Table | 57 | 57 | Yes1 | 522 | ? | 11 | ? |
compile | 57 | 57 | Yes1 | 522 | ? | 11 | ? |
compileStreaming | 61 | 61 | No | 58 | ? | ? | ? |
instantiate | 57 | 57 | Yes1 | 522 | ? | 11 | ? |
instantiateStreaming | 61 | 61 | No | 58 | ? | ? | ? |
validate | 57 | 57 | Yes1 | 522 | ? | 11 | ? |
1. This feature is behind the Experimental JavaScript Features
preference.
2. Disabled in the Firefox 52 Extended Support Release (ESR).
Desktop | Mobile | Server | ||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
Chrome | Edge | Firefox | Internet Explorer | Opera | Safari | Android webview | Chrome for Android | Edge Mobile | Firefox for Android | Opera for Android | iOS Safari | Samsung Internet | Node.js | |
Basic support | Full support 57 | Full support 16 |
Full support
52 Notes
| No support No | Full support 44 | Full support 11 | Full support 57 | Full support 57 |
Full support
Yes Disabled
|
Full support
52 Notes
| ? | Full support 11 | ? | ? |
CompileError | Full support 57 | Full support 16 |
Full support
52 Notes
| No support No | Full support 44 | Full support 11 | Full support 57 | Full support 57 |
Full support
Yes Disabled
|
Full support
52 Notes
| ? | Full support 11 | ? | ? |
Instance | Full support 57 | Full support 16 |
Full support
52 Notes
| No support No | Full support 44 | Full support 11 | Full support 57 | Full support 57 |
Full support
Yes Disabled
|
Full support
52 Notes
| ? | Full support 11 | ? | ? |
LinkError | Full support 57 | Full support 16 |
Full support
52 Notes
| No support No | Full support 44 | Full support 11 | Full support 57 | Full support 57 |
Full support
Yes Disabled
|
Full support
52 Notes
| ? | Full support 11 | ? | ? |
Memory | Full support 57 | Full support 16 |
Full support
52 Notes
| No support No | Full support 44 | Full support 11 | Full support 57 | Full support 57 |
Full support
Yes Disabled
|
Full support
52 Notes
| ? | Full support 11 | ? | ? |
Module | Full support 57 | Full support 16 |
Full support
52 Notes
| No support No | Full support 44 | Full support 11 | Full support 57 | Full support 57 |
Full support
Yes Disabled
|
Full support
52 Notes
| ? | Full support 11 | ? | ? |
RuntimeError | Full support 57 | Full support 16 |
Full support
52 Notes
| No support No | Full support 44 | Full support 11 | Full support 57 | Full support 57 |
Full support
Yes Disabled
|
Full support
52 Notes
| ? | Full support 11 | ? | ? |
Table | Full support 57 | Full support 16 |
Full support
52 Notes
| No support No | Full support 44 | Full support 11 | Full support 57 | Full support 57 |
Full support
Yes Disabled
|
Full support
52 Notes
| ? | Full support 11 | ? | ? |
compile | Full support 57 | Full support 16 |
Full support
52 Notes
| No support No | Full support 44 | Full support 11 | Full support 57 | Full support 57 |
Full support
Yes Disabled
|
Full support
52 Notes
| ? | Full support 11 | ? | ? |
compileStreaming | Full support 61 | No support No | Full support 58 | No support No | Full support 47 | ? | Full support 61 | Full support 61 | No support No | Full support 58 | ? | ? | ? | ? |
instantiate | Full support 57 | Full support 16 |
Full support
52 Notes
| No support No | Full support 44 | Full support 11 | Full support 57 | Full support 57 |
Full support
Yes Disabled
|
Full support
52 Notes
| ? | Full support 11 | ? | ? |
instantiateStreaming | Full support 61 | No support No | Full support 58 | No support No | Full support 47 | ? | Full support 61 | Full support 61 | No support No | Full support 58 | ? | ? | ? | ? |
validate | Full support 57 | Full support 16 |
Full support
52 Notes
| No support No | Full support 44 | Full support 11 | Full support 57 | Full support 57 |
Full support
Yes Disabled
|
Full support
52 Notes
| ? | Full support 11 | ? | ? |
Legend
- Full support
- Full support
- No support
- No support
- Compatibility unknown
- Compatibility unknown
- See implementation notes.
- See implementation notes.
- User must explicitly enable this feature.
- User must explicitly enable this feature.