WebAssembly

Baseline 2023

Newly available

Since March 2023, this feature works across the latest devices and browser versions. This feature might not work in older devices or browsers.

WebAssembly 是一種新的低階程式語言,可在今日的網頁瀏覽器中被執行 —— 它是低階的類組合語言,具有嚴謹的二進位格式,能以接近原生應用程式的效能執行,並提供如 C/C++ 等語言一個構建目標,使它們能在 Web 上被執行。他也被設計為可與 JavaScript 共存,允許兩者一同工作。

簡而言之

WebAssembly 對於網路平台而言具有極大的意義 —— 它提供了一種方式,使得那些以各種語言編寫的程式都能以接近原生的速度於 Web 環境中執行,在這條件下,先前無法以此種方式運作的用戶端軟體皆能在 Web 中被執行。

WebAssembly 被設計來與 JavaScript 協同工作 —— 藉由 WebAssembly 的 JavaScript API,你可以把 WebAssembly 模組載入至一個 JavaScript 應用程式,並在兩者間共享功能。這允許你在同一應用程式內彈性地發揮 WebAssembly 的高效能威力同時兼顧 JavaScript 的表達力與高靈活性,即便你可能不知道如何編寫 WebAssembly 程式碼。

而且更棒的是,這是由 W3C WebAssembly Working GroupCommunity Group 開發的一項網路標準,並也得到來自各大主要瀏覽器廠商的積極參與。

指南

WebAssembly 概念

藉由閱讀 WebAssembly 後的高階概念開始 —— 也就是說,它為何是如此有用,是如何相容網路平台(及其他),和如何使用它。

將 C/C++ 模組編譯為 WebAssembly (en-US)

當你使用 C/C++ 編寫程式後,你可以使用諸如 Emscripten 等工具將其編譯為 .wasm 文件。讓我們看看它是如何實作的。

從現有的 C 模組編譯為 WebAssembly (en-US)

WebAssembly 核心的使用案例是期望能讓開發者在 Web 環境中使用現存 C 的生態環境。

將 Rust 程式碼編譯為 WebAssembly (en-US)

你可以將 Rust 編譯為 WebAssembly!本教程將帶你了解將 Rust 項目編譯為 wasm 並在現有的 Web 應用程序中使用它所需的所有知識。

載入並運行 WebAssembly 程式碼 (en-US)

本文介紹如何透過把 FetchXHR API 與 WebAssembly JavaScript (en-US) API 結合起來、編譯及實例化 .wasm 文件。

使用 WebAssembly 的 JavaScript API (en-US)

當你載入一個 .wasm 模組後,你就用到它。在本文中將向你展示如何透過 WebAssembly 的 JavaScript API 來使用 WebAssembly。

導出 WebAssembly 函數 (en-US)

被導出的 WebAssembly 函數是用 JavaScript 來表示 WebAssembly 函數的封裝,並允許從 JavaScript 調用 WebAssembly 原始碼。本文將描述他們。

了解 WebAssembly 的文件格式 (en-US)

本文介紹了 .wasm 的文件格式。這是在進行偵錯時由瀏覽器開發工具中展現出 .wasm 模組的一種低階文件格式。

把 WebAssembly 文件格式轉為 wasm (en-US)

本文提供一個如何把以 WebAssembly 編寫的模組轉換為二進位的 .wasm 文件格式的指南。

API 參考

WebAssembly control flow (en-US)

Reference documentation for the set of WebAssembly control-flow operators.

WebAssembly (en-US)

This object acts as the namespace for all WebAssembly related functionality.

WebAssembly.Global() (en-US)

A WebAssembly.Global object represents a global variable instance, accessible from both JavaScript and importable/exportable across one or more WebAssembly.Module (en-US) instances. This allows dynamic linking of multiple modules.

WebAssembly.Module() (en-US)

A WebAssembly.Module object contains stateless WebAssembly code that has already been compiled by the browser and can be efficiently shared with Workers (en-US), and instantiated multiple times.

WebAssembly.Instance() (en-US)

A WebAssembly.Instance object is a stateful, executable instance of a Module. Instance objects contain all the Exported WebAssembly functions (en-US) that allow calling into WebAssembly code from JavaScript.

WebAssembly.instantiateStreaming() (en-US)

The WebAssembly.instantiateStreaming() function is the primary API for compiling and instantiating WebAssembly code, returning both a Module and its first Instance.

WebAssembly.Memory() (en-US)

A WebAssembly.Memory object is a resizable ArrayBuffer that holds the raw bytes of memory accessed by an Instance.

WebAssembly.Table() (en-US)

A WebAssembly.Table object is a resizable typed array of opaque values, like function references, that are accessed by an Instance.

WebAssembly.CompileError() (en-US)

Creates a new WebAssembly CompileError object.

WebAssembly.LinkError() (en-US)

Creates a new WebAssembly LinkError object.

WebAssembly.RuntimeError() (en-US)

Creates a new WebAssembly RuntimeError object.

範本

規範

Specification
WebAssembly JavaScript Interface
# webassembly-namespace
Unknown specification
Unknown specification
Unknown specification
Unknown specification
WebAssembly Core: Garbage Collection
# garbage-collection①
Unknown specification
Unknown specification
Unknown specification
Unknown specification
Unknown specification
Unknown specification
Unknown specification

瀏覽器相容性

webassembly.api

BCD tables only load in the browser

webassembly.BigInt-to-i64-integration

BCD tables only load in the browser

webassembly.bulk-memory-operations

BCD tables only load in the browser

webassembly.exception-handling

BCD tables only load in the browser

webassembly.extended-constant-expressions

BCD tables only load in the browser

webassembly.fixed-width-SIMD

BCD tables only load in the browser

webassembly.garbage-collection

BCD tables only load in the browser

webassembly.multiMemory

BCD tables only load in the browser

webassembly.multi-value

BCD tables only load in the browser

webassembly.mutable-globals

BCD tables only load in the browser

webassembly.non-trapping-float-to-int-conversions

BCD tables only load in the browser

webassembly.reference-types

BCD tables only load in the browser

webassembly.sign-extension-operations

BCD tables only load in the browser

webassembly.tail-calls

BCD tables only load in the browser

webassembly.threads-and-atomics

BCD tables only load in the browser

參見