Wasm, WASI, Component Model をながめる
Topics
-
WebAssembly (Core) Specification 2.0
- Wasmのコア仕様はかなり低水準なインタフェースしか提供しない。各々のツールチェインがその言語側やJS側に独自のバインディング用コードを沢山生成することで機能する。それぞれの言語やツールが生成したwasmに相互運用性はない。
-
WebAssembly Component Model
- 標準の高水準インタフェースを定めて、Wasmの相互運用性を実現しようとするもの。
- WASIのためだけのものではないが、WASI 0.2の基盤でもある。
- WIT - WebAssembly Interface Type
-
WASI 0.1 (Preview 1) → 0.2 (Preview 2)
- Web Assemblyのための標準APIを定めようとするもの。WASI 0.2 はComponent Modelに基づいて実現される。Webからプラグインからクラウドから組込みまで共通のAPIに落し込めたらうれしい。
- その他
- Rust用 toolchains
- WasmGC (あまり関係ない)
Wasm全般
そもそもWasmの基礎がわかってない
ツール
- wabt (wasm/watの操作など): https://github.com/WebAssembly/wabt - The WebAssembly Binary Toolkit
- binaryen (コンパイル・最適化をはじめとして、その他のコードに踏み入るツール): https://github.com/WebAssembly/binaryen - Optimizer and compiler/toolchain library for WebAssembly
- wasm-tools (Rustによるwabtのようなもの(?)。Wasm Component Modelにも対応) https://github.com/bytecodealliance/wasm-tools - CLI and Rust libraries for low-level manipulation of WebAssembly modules
テキスト形式の構造
Core specではルート要素はモジュール。
モジュール: https://webassembly.github.io/spec/core/syntax/modules.html
- type - 関数の型を定義する
- func - 関数
- table - reftype (funcref, externref) のテーブル。関数の間接呼び出しに使われる。
- memory - メモリ
- global - グローバル変数 (const or mut)
- start - モジュール初期化後に実行される関数を指定できる
- elem - テーブルの初期値を設定する
- data - データセグメント。メモリの指定範囲に初期値を設定する
- export - エクスポート func, table, mem, global
- import - インポート func, table, mem, global
Wasm Components の場合は component がルート要素になる。まったく構造が違う。
Wasm - Feature Extensions
Wasm 2.0 の変更点(現時点での): https://webassembly.github.io/spec/core/appendix/changes.html
Wasmの拡張機能のサポート状況:
WebAssembly Component Model (Wasm Components)
Wasm 同士を組み合わせて使うことは難しかった。Wasm自体は非常に低水準なインタフェースしか持っておらず、高水準なインタフェースは標準化されていなかった。Wasmに相互運用性などなかった。
Wasmのうえに高水準な標準インターフェースを定めようとするのが Wasm Component Model。
- https://component-model.bytecodealliance.org/ (user-focussed explanation)
- https://github.com/WebAssembly/component-model
- https://github.com/WebAssembly/component-model/blob/main/design/mvp/Explainer.md
既存のWasmの枠組みのうえで、インタフェースを定める → WIT - The Wasm Interface Type
WASI 0.2もこの Component Model に基いて作られる。
WIT (IDL)
WIT = WebAssembly Interface Type
WebAssembly Component Model におけるインタフェース定義言語
WITをもとに各言語のBindingsを生成する。
Component Model の開発
ツール:
- wasm-tools (Rust版のwabtのようなものに加えてComponents周りの機能も持つ) https://github.com/bytecodealliance/wasm-tools - CLI and Rust libraries for low-level manipulation of WebAssembly modules
- 各言語用のWIT bindingsを生成: https://github.com/bytecodealliance/wit-bindgen
各言語;
- Rust: https://github.com/bytecodealliance/cargo-component
- Node.js: https://github.com/bytecodealliance/jco
Registory
DockerHubやnpm のようなものを作るためのコンポーネントレジストリの仕様 warg が開発されている。
- https://github.com/bytecodealliance/registry
- https://warg.io/
- https://wa.dev/ (wargに準拠したレジストリの実装の一例)
関連記事
WASI
WASI Subgroup in the W3C WebAssembly Community Group の管轄
WASI 0.1 (Preview 1) はWASIの初期開発段階。すでに広く使われていて、対応しているランタイムも多い。
WASI 0.2 (Preview 2) からは、WASIは WebAssembly Component Model に従う。つまり Rustで作ったコンポーネントとGoで作ったコンポーネントを対話させたり、くっつけたりできる。WASIの未来をめざすステージ。
wasi.dev
wasi.dev が語る夢:
- 「言語間をつなぐためにHTTPベースのマイクロサービスのような不確かなインタフェースは要らなくなるし」
- 「プラグイン機構のあるアプリケーションはみんなWASI使えばいいと信じてる」
- 「複数の言語にSDKを提供するプロジェクトにも理想的には適する (?)」
誰のためのもの?
- web app
- plugins
- serverless functions
- user-defined functions in database
- embedded controller
- sidecar networking filters
- etc.
WASI対応ランタイム:
Wasmtime, WAMR, WasmEdge, wazero, Wasmer, wasmi, wasm3
- WAMR -> IoT, Embedded, Edge, etc.
- Wasmtime -> server-side, non-web embeddings, etc.
WASI 0.2
WASI 0.2 向けに設計されたAPIは、Component Model の WIT (WebAssembly Interface Type) というインタフェース定義言語 (IDL) (.wit ファイル) で定められている。
Preview 2には以下が用意されている。
- I/O 0.2
- Clocks 0.2
- Random 0.2
- Filesystem 0.2
- Sockets 0.2
- CLI 0.2
- HTTP 0.2
WITから各言語のBindingsを生成すれば、これらのAPIを呼び出せる。WITについてはComponent Modelのドキュメントを参照。
API Proposals
提案されているAPIは、 WASI GitHub repository にある。
WASI 0.1 はどうだった
WITXというIDLを使っていた。より低級で、WebAssembly Text Format の派生型だった。WASIリポジトリのlegacyに残っている。
RustにおけるWASI
Python
Runtime
py2wasm
Runtime
-
Wasmtime: https://github.com/bytecodealliance/wasmtime
- ある種のリファレンス実装 by Bytecode Alliance
- A fast and secure runtime for WebAssembly
-
WAMR: https://github.com/bytecodealliance/wasm-micro-runtime
- WebAssembly Micro Runtime (WAMR) by Bytecode Alliance
- 組込みやエッジ向け
- WasmEdge: https://wasmedge.org/
-
Wasmer: https://github.com/wasmerio/wasmer
- 営利企業では最も有名
-
Wazero: https://wazero.io/
- Go用。依存ライブラリなし。
-
wasmi: https://github.com/wasmi-labs/wasmi
- インタプリタ
- etc.