# WebAssembly

**WebAssembly**（缩写 **Wasm**）是一种低级、类汇编的字节码格式，采用紧凑的二进制编码，被设计为 C、C++ 和 Rust 等源语言的编译目标，使这些语言的代码能在网页浏览器和非浏览器环境中以接近原生的速度运行<sup>[1](https://developer.mozilla.org/en-US/docs/WebAssembly/Guides/Concepts)</sup>。它是由 W3C WebAssembly 社区组制定的开放标准，其目的并非取代 [JavaScript](https://www.edgepedia.org/zh/javascript)，而是与 JavaScript 并行运行：二者可以同步互相调用，浏览器的虚拟机负责加载和执行两者<sup>[1](https://developer.mozilla.org/en-US/docs/WebAssembly/Guides/Concepts)</sup>。

| 关键事实 | 详情 |
|---|---|
| 是什么 | 一种可移植、体积小且加载高效的二进制格式，作为编译目标，可在移动端和物联网等平台上以原生速度执行<sup>[2](https://webassembly.org/docs/high-level-goals/)</sup> |
| 文本格式 | 一种人类可编辑的文本格式（WAT），可与二进制格式互相转换，两者一一对应<sup>[2](https://webassembly.org/docs/high-level-goals/)</sup><sup> • </sup><sup>[1](https://developer.mozilla.org/en-US/docs/WebAssembly/Guides/Concepts)</sup> |
| MVP 共识 | 2017 年 11 月，代表 Chrome、Edge、Firefox 和 WebKit 的社区组成员一致认为初始 MVP 的 API 与二进制格式设计已经完成<sup>[3](https://webassembly.org/features/?categories=browsers%2Cstandalones%2Ctools)</sup> |
| 标准化 | WebAssembly 核心规范于 2019 年 12 月 5 日成为 W3C 推荐标准（Wasm 1.0）；Wasm 2.0 为 2025 年 6 月 16 日的候选推荐草案，Wasm 3.0 规范也已发布<sup>[4](https://webassembly.org/specs/)</sup> |
| 安全模型 | 每个模块运行在沙箱中，通过故障隔离与宿主分离；在浏览器中适用同源策略和权限策略<sup>[5](https://github.com/WebAssembly/design/blob/main/Security.md)</sup><sup> • </sup><sup>[2](https://webassembly.org/docs/high-level-goals/)</sup> |
| 系统接口 | WASI 提供模块化系统接口，用于在 Web 之外运行 Wasm，涵盖文件、网络连接、时钟和随机数；0.2.12 版基于组件模型<sup>[4](https://webassembly.org/specs/)</sup><sup> • </sup><sup>[6](https://github.com/WebAssembly/WASI/blob/main/specifications/wasi-0.2.12/Overview.md)</sup> |
| 工具链 | Clang/LLVM 通过 `--target=wasm32` 编译到 Wasm；Emscripten 编译 C/C++ 并生成 JavaScript 胶水代码；AssemblyScript 编译严格版 TypeScript<sup>[1](https://developer.mozilla.org/en-US/docs/WebAssembly/Guides/Concepts)</sup> |

## 目标与设计

该项目公开的高层目标是：定义一种可移植、体积小且加载高效的二进制格式，能利用常见硬件能力以原生速度执行；通过独立的功能提案逐步规范和实现，同时保持向后兼容；与现有 Web 平台集成，包括与 JavaScript 之间的同步调用以及同源策略和权限策略的执行；支持非浏览器嵌入；并通过形式语义规范保持高度确定性<sup>[2](https://webassembly.org/docs/high-level-goals/)</sup>。

分层是设计中有意为之的一部分：核心规范涵盖纯沙箱计算，宿主交互则被分离到更高的规范层<sup>[2](https://webassembly.org/docs/high-level-goals/)</sup>。规范本身在 WebAssembly/spec 仓库中通过参考解释器和官方测试套件维护<sup>[7](https://github.com/webassembly/spec)</sup>。

## 浏览器中的执行模型

四个概念描述了 Wasm 在浏览器中的运行方式，每个概念在 JavaScript API 中都有一一对应的体现<sup>[1](https://developer.mozilla.org/en-US/docs/WebAssembly/Guides/Concepts)</sup>：

- **模块（Module）**：一个已编译、无状态的 WebAssembly 二进制，声明导入和导出，类似于 ES 模块，可通过 `postMessage()` 在窗口和工作线程之间共享。
- **内存（Memory）**：一个可增长的缓冲区，存放模块内存指令读写的线性字节数组。
- **表（Table）**：一个可增长的引用数组，例如函数引用，这些引用无法安全地以原始字节形式存储在内存中。
- **实例（Instance）**：一个模块及其运行时状态的组合，包括内存、表和导入值。

在底层内存模型中，内存是一段连续的无类型字节范围，称为线性内存，通过加载和存储指令访问；与原生 C/C++ 程序不同——后者的可寻址范围覆盖整个进程——实例的内存仅限于包含在 `WebAssembly.Memory` 对象中的某个特定范围，因此单个 Web 应用可以运行多个相互独立的库，各自拥有完全隔离的内存<sup>[8](https://developer.mozilla.org/en-US/docs/WebAssembly/Guides/Using_the_JavaScript_API)</sup>。表的存在是因为引用是引擎可信的值，其字节不得被内容直接读写；编译 C/C++ 函数指针需要函数引用<sup>[8](https://developer.mozilla.org/en-US/docs/WebAssembly/Guides/Using_the_JavaScript_API)</sup>。

执行以栈式虚拟机定义：每条指令压入或弹出指定数量的 `i32`、`i64`、`f32` 或 `f64` 值，验证规则要求栈与声明的结果类型完全匹配<sup>[9](https://developer.mozilla.org/en-US/docs/WebAssembly/Understanding_the_text_format)</sup>。

JavaScript API 提供编译（`WebAssembly.compile()` 及流式变体）、使用 JavaScript 导入进行实例化，以及访问导出的函数和内存<sup>[10](https://webassembly.org/docs/web/)</sup>。JavaScript 可以提供 `WebAssembly.Memory` 作为导入，或接收其作为导出，内存导入允许一个内存对象被多个模块实例共享，这是动态链接的构建基础<sup>[8](https://developer.mozilla.org/en-US/docs/WebAssembly/Guides/Using_the_JavaScript_API)</sup>。

## 安全

安全模型有两个目标：保护用户免受有缺陷或恶意模块的侵害，并在这一约束下为开发者提供构建安全应用的有用原语<sup>[5](https://github.com/WebAssembly/design/blob/main/Security.md)</sup>。每个模块在沙箱环境中执行，通过故障隔离与宿主运行时分离，因此应用除非通过适当的 API，否则无法逃出沙箱，且每个模块受其嵌入环境的安全策略约束，例如浏览器中的同源限制<sup>[5](https://github.com/WebAssembly/design/blob/main/Security.md)</sup>。

若干机制源于执行语义。模块必须在加载时声明所有可访问的函数及其类型，这通过结构化控制流实现控制流完整性；间接调用在运行时进行类型检查；受保护的调用栈守护函数返回；编译后的代码不可变，防止控制流劫持<sup>[5](https://github.com/WebAssembly/design/blob/main/Security.md)</sup>。缓冲区溢出无法影响存储在索引空间中的局部或全局变量，线性内存访问在区域级别进行边界检查，越界访问会触发陷阱<sup>[5](https://github.com/WebAssembly/design/blob/main/Security.md)</sup>。

沙箱限制了模块对宿主的行为，但并不保证结果正确：编程错误的模块仍可能破坏自身内存中的数据，故障可能通过 ArrayBuffer 对共享同一内存的其他模块可见<sup>[11](https://github.com/WebAssembly/content-security-policy/blob/main/proposals/CSP.md)</sup>。检查时与使用时不一致漏洞（TOCTOU）等竞态条件以及侧信道攻击（包括时序攻击）仍然可能存在<sup>[5](https://github.com/WebAssembly/design/blob/main/Security.md)</sup>。

## 可移植性

二进制格式旨在跨操作系统和指令集架构高效运行，无论是否在 Web 上<sup>[12](https://webassembly.org/docs/portability/)</sup>。高效执行假定环境提供可按字节粒度寻址的 8 位字节、32 位及可选 64 位的二进制补码有符号整数、[IEEE 754](https://www.edgepedia.org/zh/ieee-754)-2019 的 32 位和 64 位浮点数（有少数例外）、小端字节序、可用 32 位指针高效寻址的内存（wasm64 配置额外支持使用 64 位指针的超过 4 GiB 的线性内存）、模块间的安全隔离、所有线程的前进保证，以及 8 位、16 位和 32 位访问的无锁原子操作，至少包括原子比较交换<sup>[12](https://webassembly.org/docs/portability/)</sup>。

WebAssembly 本身不规定任何 API 或系统调用，只规定导入机制，可用导入由宿主环境定义。在 Web 环境中，功能通过 [Web API](https://www.edgepedia.org/zh/web-api) 提供；非 Web 环境可以实现标准 Web API、标准非 Web API（如 POSIX）或自有 API<sup>[12](https://webassembly.org/docs/portability/)</sup>。

## 浏览器之外：WASI 与组件模型

**WASI**（[WebAssembly 系统接口](https://www.edgepedia.org/zh/webassembly-system-interface)）定义了在 Web 之外运行 Wasm 的模块化系统接口，提供对文件、网络连接、时钟和随机数的访问<sup>[4](https://webassembly.org/specs/)</sup>。WASI 围绕[基于能力的安全](https://www.edgepedia.org/zh/capability-based-security)设计，利用组件模型的设施：所有对外部资源的访问都由能力提供——要么是组件模型类型系统中不可伪造的句柄，要么是少量使用的链接时能力——WASI 没有环境权限，意味着运行时没有全局命名空间，链接时没有全局函数<sup>[13](https://github.com/WebAssembly/WASI/blob/main/docs/DesignPrinciples.md)</sup>。

**组件模型（Component Model）** 是一个分层规范，位于 WebAssembly 核心规范之上、WASI 标准之下，定义模块如何组合以及接口如何虚拟化，并为工具链提供类似于传统操作系统 ABI 的二进制目标<sup>[14](https://www.w3.org/2021/10/TPAC/demos/web-assembly.html)</sup>。其接口描述语言是 **WIT**（WebAssembly 接口类型），一种开发者友好的格式，用于描述组件的导入和导出；WIT 的 `world` 是组件导入和导出的完整描述，构成绑定生成的基础<sup>[15](https://github.com/WebAssembly/component-model/blob/main/design/mvp/WIT.md)</sup>。

组件模型的规范 ABI 为任何组件级函数类型规定了对应的核心函数类型以及将值复制进线性内存和从中复制出来的规则，`lift` 将核心函数包装为组件函数，`lower` 执行相反操作<sup>[16](https://github.com/WebAssembly/component-model/blob/main/design/mvp/Explainer.md)</sup>。各版本依次为：WASI 0.2.0，首个基于组件模型的版本，具有无共享和共享一切的链接方式、带句柄的资源类型以及 WIT；0.3.0，首个原生支持并发的版本，增加了 `async` 函数、`stream` 和 `future`；0.3.1，增加了 `map<K,V>` 类型<sup>[17](https://github.com/webassembly/component-model)</sup>。WASI 子组投票批准了 WASI 0.3.0，将 WASI 的基础调整为组件模型的异步原语；在 0.3 中，网络访问通过 world 导入授予，而非通过贯穿每次调用的 `network` 能力资源<sup>[18](https://github.com/WebAssembly/WASI/releases)</sup>。

## 编译到 WebAssembly

大多数 Wasm 代码由编译器生成而非手写。Clang/LLVM 通过 `--target=wasm32` 支持 Wasm 输出，Rust WebAssembly 工作组维护着将 Rust 编译到 Wasm 的工具链；[AssemblyScript](https://www.edgepedia.org/zh/assemblyscript) 为希望留在该生态中的开发者编译严格版 [TypeScript](https://www.edgepedia.org/zh/typescript)<sup>[1](https://developer.mozilla.org/en-US/docs/WebAssembly/Guides/Concepts)</sup>。

对于 C 和 C++，Emscripten 是完整的工具链而非裸编译器：它将源代码送入 clang+LLVM，将结果转换为 Wasm 二进制，并生成在浏览器中运行应用所需的 JavaScript 胶水代码和 HTML，以 Web API 实现 SDL、OpenGL、OpenAL 和部分 POSIX 等库<sup>[1](https://developer.mozilla.org/en-US/docs/WebAssembly/Guides/Concepts)</sup>。Wasm 本身目前无法直接访问 DOM；它只能调用 JavaScript，再由 JavaScript 发起 Web API 调用<sup>[1](https://developer.mozilla.org/en-US/docs/WebAssembly/Guides/Concepts)</sup>。

## 标准化状态

在 2017 年 11 月四大浏览器引擎达成 MVP 共识之后，功能通过标准化流程作为独立提案逐步添加<sup>[3](https://webassembly.org/features/?categories=browsers%2Cstandalones%2Ctools)</sup>。规范家族现在包括 Wasm 3.0 核心规范、2025 年 6 月 16 日的 W3C 核心规范候选推荐草案（Wasm 2.0）、2019 年推荐标准（Wasm 1.0）、定义从绑定到源的 `Response` 类型进行流式编译的 Web API 嵌入规范、WASI API，以及关于链接方案、调试信息和语言 ABI 的非标准工具约定<sup>[4](https://webassembly.org/specs/)</sup>。

## References

1. [WebAssembly concepts, MDN Web Docs](https://developer.mozilla.org/en-US/docs/WebAssembly/Guides/Concepts)
2. [WebAssembly High-Level Goals, webassembly.org](https://webassembly.org/docs/high-level-goals/)
3. [Feature Status, webassembly.org](https://webassembly.org/features/?categories=browsers%2Cstandalones%2Ctools)
4. [Specifications, webassembly.org](https://webassembly.org/specs/)
5. [WebAssembly Security model, WebAssembly/design GitHub](https://github.com/WebAssembly/design/blob/main/Security.md)
6. [WASI Specification v0.2.12 Overview, WebAssembly/WASI GitHub](https://github.com/WebAssembly/WASI/blob/main/specifications/wasi-0.2.12/Overview.md)
7. [WebAssembly/spec: specification, reference interpreter, and test suite, GitHub](https://github.com/webassembly/spec)
8. [Using the WebAssembly JavaScript API, MDN Web Docs](https://developer.mozilla.org/en-US/docs/WebAssembly/Guides/Using_the_JavaScript_API)
9. [Understanding WebAssembly text format, MDN Web Docs](https://developer.mozilla.org/en-US/docs/WebAssembly/Understanding_the_text_format)
10. [Web Embedding, webassembly.org](https://webassembly.org/docs/web/)
11. [WebAssembly content-security-policy proposal, GitHub](https://github.com/WebAssembly/content-security-policy/blob/main/proposals/CSP.md)
12. [Portability, webassembly.org](https://webassembly.org/docs/portability/)
13. [WASI Design Principles, WebAssembly/WASI GitHub](https://github.com/WebAssembly/WASI/blob/main/docs/DesignPrinciples.md)
14. [TPAC 2021: WebAssembly Community and Working Groups updates, W3C](https://www.w3.org/2021/10/TPAC/demos/web-assembly.html)
15. [The WIT text format, WebAssembly/component-model GitHub](https://github.com/WebAssembly/component-model/blob/main/design/mvp/WIT.md)
16. [Component Model Explainer, WebAssembly/component-model GitHub](https://github.com/WebAssembly/component-model/blob/main/design/mvp/Explainer.md)
17. [WebAssembly/component-model repository, GitHub](https://github.com/webassembly/component-model)
18. [WASI Releases, WebAssembly/WASI GitHub](https://github.com/WebAssembly/WASI/releases)

---
*Initially written Sep 17, 2026 · Reviewed: Sep 17, 2026; Sep 30, 2026; Oct 15, 2026 · Edited: Oct 15, 2026 · Last review: Oct 15, 2026*

Topic: 技术与人造世界 › 计算技术与数字系统 › 软件与编程 › Compilers, interpreters, and toolchains

Copyright 2026 Edgepedia

License: Edgepedia Community License 1.0, https://www.edgepedia.org/edgepedia/license

Source: https://www.edgepedia.org/zh/webassembly

English original: https://www.edgepedia.org/webassembly
