使用 WebAssembly
¥Node.js with WebAssembly
WebAssembly 是一种高性能的汇编类语言,可以从各种语言编译,包括 C/C++、Rust 和 AssemblyScript。目前,Chrome、Firefox、Safari、Edge 和 Node.js 均支持它!
¥WebAssembly is a high-performance assembly-like language that can be compiled from various languages, including C/C++, Rust, and AssemblyScript. Currently, it is supported by Chrome, Firefox, Safari, Edge, and Node.js!
WebAssembly 规范详细说明了两种文件格式,一种二进制格式称为 WebAssembly 模块,带有 .wasm
扩展名,相应的文本表示称为 WebAssembly 文本格式,带有 .wat
扩展名。
¥The WebAssembly specification details two file formats, a binary format called a WebAssembly Module with a .wasm
extension and corresponding text representation called WebAssembly Text format with a .wat
extension.
关键概念
¥Key Concepts
-
模块 - 编译后的 WebAssembly 二进制文件,即
.wasm
文件。¥Module - A compiled WebAssembly binary, ie a
.wasm
file. -
内存 - 一个可调整大小的 ArrayBuffer。
¥Memory - A resizable ArrayBuffer.
-
表 - 可调整大小的类型化引用数组未存储在内存中。
¥Table - A resizable typed array of references not stored in Memory.
-
实例 - 模块及其内存、表和变量的实例化。
¥Instance - An instantiation of a Module with its Memory, Table, and variables.
为了使用 WebAssembly,你需要一个 .wasm
二进制文件和一组 API 来与 WebAssembly 通信。Node.js 通过全局 WebAssembly
对象提供必要的 API。
¥In order to use WebAssembly, you need a .wasm
binary file and a set of APIs to communicate with WebAssembly. Node.js provides the necessary APIs via the global WebAssembly
object.
console.log(WebAssembly);
/*
Object [WebAssembly] {
compile: [Function: compile],
validate: [Function: validate],
instantiate: [Function: instantiate]
}
*/
生成 WebAssembly 模块
¥Generating WebAssembly Modules
有多种方法可用于生成 WebAssembly 二进制文件,包括:
¥There are multiple methods available to generate WebAssembly binary files including:
-
手动编写 WebAssembly (
.wat
) 并使用 wabt 等工具转换为二进制格式¥Writing WebAssembly (
.wat
) by hand and converting to binary format using tools such as wabt -
将 emscripten 与 C/C++ 应用一起使用
¥Using emscripten with a C/C++ application
-
将 wasm-pack 与 Rust 应用一起使用
¥Using wasm-pack with a Rust application
-
如果你更喜欢类似 TypeScript 的体验,请使用 AssemblyScript
¥Using AssemblyScript if you prefer a TypeScript-like experience
其中一些工具不仅生成二进制文件,还生成 JavaScript "glue" 代码和相应的 HTML 文件以在浏览器中运行。
¥Some of these tools generate not only the binary file, but the JavaScript "glue" code and corresponding HTML files to run in the browser.
如何使用它
¥How to use it
一旦你拥有 WebAssembly 模块,你就可以使用 Node.js WebAssembly
对象来实例化它。
¥Once you have a WebAssembly module, you can use the Node.js WebAssembly
object to instantiate it.
// Assume add.wasm file exists that contains a single function adding 2 provided arguments
const fs = require('node:fs');
// Use the readFileSync function to read the contents of the "add.wasm" file
const wasmBuffer = fs.readFileSync('/path/to/add.wasm');
// Use the WebAssembly.instantiate method to instantiate the WebAssembly module
WebAssembly.instantiate(wasmBuffer).then(wasmModule => {
// Exported function lives under instance.exports object
const { add } = wasmModule.instance.exports;
const sum = add(5, 6);
console.log(sum); // Outputs: 11
});
与操作系统交互
¥Interacting with the OS
WebAssembly 模块本身无法直接访问操作系统功能。可以使用第三方工具 Wasmtime 来访问此功能。Wasmtime
使用 WASI API 来访问操作系统功能。
¥WebAssembly modules cannot directly access OS functionality on its own. A third-party tool Wasmtime can be used to access this functionality. Wasmtime
utilizes the WASI API to access the OS functionality.
资源
¥Resources