Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Sign up for free
Menu
Search
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Pricing
Search
Sign in
Sign up for free
WebAssemblyインタプリタを書く ~Component Modelを添えて~
Search
Sponsored
·
Ship Features Fearlessly
Turn features on and off without deploys. Used by thousands of Ruby developers.
→
ruccho
August 09, 2025
Programming
1.3k
1
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
WebAssemblyインタプリタを書く ~Component Modelを添えて~
https://kernelvm.connpass.com/event/355100/
ruccho
August 09, 2025
More Decks by ruccho
See All by ruccho
Roslyn でフロー解析してみる
ruccho
0
730
URP の 2D Renderer と たわむれる
ruccho
0
23
Colonies
ruccho
0
690
タイルマップ拡張のススメ / Recommendation of Unity Tilemap Gotanda.unity #14
ruccho
0
3.7k
Other Decks in Programming
See All in Programming
Atomic Design, Enforced: Scaling Mobile Design Systems (next.app devCon / droidCon Berlin 2026)
steliosf
PRO
0
160
Augmenting AI with the Power of Jakarta EE
ivargrimstad
0
520
モデルのリファクタリングが難しいと思ったら、そもそも複雑だったのはビジネス仕様だった ? / is-the-business-domain-the-real-complexity
hatsu38
0
450
HTML-in-Canvasで作る未来のWeb UI表現
clockmaker
0
140
UnityでSystem.Net.WebSocketsなWebSocketサーバが動かないのでUnity Monoのコードを覗いてみた / about implementing websocket server with unity mono
drumath2237
1
570
Herb in Rails 8.2: Your ERB views, now HTML-aware @ Rails World 2026, Austin, Texas
marcoroth
0
200
Agentic Software Factoryに、すごく賢いIF文を。 / super smart IF statement into the Agentic Software Factory.
rkaga
8
2.1k
Verilogで学ぶCPU自作入門
uyuki234
8
4.1k
phperだってパンチカードでコーディングしたい
o0h
PRO
0
260
Apple Intelligence を用いた個人情報誤送信防止、及びユーザーリクエスト体験の改善について
yukiny
0
320
Unity6 URPによる海底をテーマにしたVJシステムの解説 | Unity Shader 完全に理解した 勉強会
gam0022
0
350
Are APIs Still Relevant in the AI Era?
soyuka
0
450
Featured
See All Featured
A Modern Web Designer's Workflow
chriscoyier
699
190k
Organizational Design Perspectives: An Ontology of Organizational Design Elements
kimpetersen
PRO
1
840
Templates, Plugins, & Blocks: Oh My! Creating the theme that thinks of everything
marktimemedia
31
2.9k
Designing for Performance
lara
612
70k
Git: the NoSQL Database
bkeepers
PRO
433
67k
Exploring the Power of Turbo Streams & Action Cable | RailsConf2023
kevinliebholz
37
6.6k
Paper Plane
katiecoart
PRO
4
54k
We Are The Robots
honzajavorek
0
390
Claude Code どこまでも/ Claude Code Everywhere
nwiizo
68
58k
The Art of Delivering Value - GDevCon NA Keynote
reverentgeek
16
2.2k
Designing Powerful Visuals for Engaging Learning
tmiket
1
600
Ten Tips & Tricks for a 🌱 transition
stuffmc
1
260
Transcript
WebAssemblyインタプリタを書く ~Component Modelを添えて~ るっちょ @ruccho_vector
作ったもの • Unity (.NET) 上で動く、C#製WebAssembly?インタプリタ • Component Model?に対応 • ゲームにおける会話イベントやカットシーンなどをWasmで動かす
ゲーム開発における「スクリプト」 • ゲーム開発はエンジンの上に軽量なスクリプト環境を作りがち • アプリケーション寄りのロジックを書く • 動的なロードや実行、ホットリロードなどをサポート • 非エンジニアが書けるようにする目的も エンジン
(C++) スクリプト (C#, Lua, …)
Unityの場合 • エンジンはC++、ユーザーコードとしてC#がサポートされている • しかし、C#はスクリプトとして難点あり ◦ 一部プラットフォームではAOT制約がある (JITができない) ◦ ホットリロード機能はあるが、
よく壊れるので実質機能していない ◦ コンパイル時間が長い • 新たなスクリプトレイヤーが欲しい! C++ C# スクリプト (WebAssembly)
WebAssembly (Wasm) • 特定のOSやCPUに依存しない仮想の命令セットとバイナリ形式 • Rust, C/C++, Java, Go, C#など様々な言語をWasmにコンパイルできる
👉言語非依存なスクリプト環境をUnity上に作れるのではないか
余談:ほかの選択肢 • Lua ◦ 組み込みが容易で、昔からゲーム向けスクリプト環境として人気 ◦ 動的型付け、エディタ補完の弱さから敬遠 • ビジュアルスクリプティング ◦
これを作る前に3回ビジュアルスクリプティング環境を自作した 結論、なんだかんだでテキストは強い
WebAssemblyインタプリタを書く
① モジュールのデコード • ヘッダ+セクションのシンプルな構成 • LEB128 (整数の可変長バイナリ表現) が多用される • 仕様見ながら地道に書く
ヘッダ Type Section Import Section Function Section Table Section Memory Section Global Section Export Section Start Section Code Section Data Section
② 命令のデコード・挙動の実装 • 数が多いのでデコーダは自動生成で実装 • とりあえずWasm 1.0まで実装 ◦ 2.0にはSIMDなど含まれる https://pengowray.github.io/wasm-ops/
③ VM • ハーバードアーキテクチャ ◦ 関数にはインデックスベースで飛ぶ • スタックマシン • ブロック単位の遷移
• 型はi32, i64, f32, f64の4種類 ◦ ※Wasm 1.0の場合 • 独自機能としてプリエンプティブなグリーンスレッドをサポート ◦ C#側の非同期関数をWasmから同期的に呼べる ◦ 会話イベントなど作るうえで便利
④テスト • 公式テストスイートがある
動いた!しかし…… • 素のWebAssemblyはFFIがたいへんやりにくい! • FFIでの引数・戻り値の型はi32, i64, f32, f64の4種類のみ ◦ それ以外のデータは線形メモリを介して渡す必要がある
• 線形メモリを介した任意長データの渡し方が十分に定義されていない ◦ 線形メモリ上のデータレイアウト、確保・解放の方法やタイミングなど ◦ つまり標準的なABIがない • 実用的には、FFI用のグルーコード生成が必要になる
その悩み、 WebAssembly Component Modelが 解決します
WebAssembly Component Model • Wasmのハイレベルなinteroperabilityを実現するための仕様 • 文字列、配列、構造体など複雑な値を扱うためのCanonical ABIがある ◦ 引数や戻り値はCanonical
ABIに従ってやりとりされる • WASI preview 2以降はComponent Modelに則って定義される • 将来的にはWasmコンポーネントをレジストリに公開して依存解決する、 みたいなのも目指している ◦ wa.devというパッケージレジストリが既にあったりする
利用者から見たComponent Model • WITという専用のDSLを使う ◦ Wasmコンポーネントが公開する関数のシグネチャや、そこで使われる データ型を定義するもの
利用者から見たComponent Model • WITから各言語向けのバインディングを自動生成する • コンパイルしたWasmモジュールにメタデータを付与してコンポーネントに 変換する WIT ゲストソース (Rust,
Go, …) バインディング 素のWasm Wasm Component
ランタイムから見たComponent Model • Wasmコンポーネントは独自のバイナリ仕様を定義している ◦ 素のWasmバイナリに様々なメタデータを付与する ◦ つまり、新たにデコーダが必要 • FFIでCanonical
ABIを実装する必要がある ◦ 配列や構造体、複雑な値をWasmの線形メモリにシリアライズしたり、 逆にデシリアライズしたり
Component Modelに対応できた! • 文字列とかカジュアルに渡せるようになった ◦ ゲームの会話イベントで必須なメッセージ本文の受け渡しなど活用範囲は広い • Component Model 対応の詳しい話は記事に書いています
◦ Component Model な Wasm ランタイムを作った https://zenn.dev/ruccho/articles/7aad0b660377ae
👈 Unity (C++) の上で動く C# の上で 動く Rust (Wasm)
Component Modelの普及状況 • ゲスト言語向けのツーリングは充実しつつある ◦ Rust, Go, C/C++, C#あたりは動く ◦
moonbit (Wasmをメインターゲットとした新興言語) も注目 • 実行環境のサポートはまだまだ ◦ wasmtime ◦ ブラウザはまだ非対応
WebAssemblyインタプリタを書く ~Component Modelを添えて~ るっちょ @ruccho_vector