Lock in $30 Savings on PRO—Offer Ends Soon! ⏳
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
FFI: RustとWebAssemblyとJavaScriptと
Search
d0iasm
January 27, 2020
Programming
1
1.3k
FFI: RustとWebAssemblyとJavaScriptと
2020/1/17にRust LT #8 で発表した資料です。
https://rust.connpass.com/event/160225/
d0iasm
January 27, 2020
Tweet
Share
More Decks by d0iasm
See All by d0iasm
WebAssembly outside of the browser
d0iasm
12
3.9k
Hello World in coreboot
d0iasm
3
930
Other Decks in Programming
See All in Programming
Context is King? 〜Verifiability時代とコンテキスト設計 / Beyond "Context is King"
rkaga
10
1.3k
手が足りない!兼業データエンジニアに必要だったアーキテクチャと立ち回り
zinkosuke
0
770
AIコーディングエージェント(Manus)
kondai24
0
200
Github Copilotのチャット履歴ビューワーを作りました~WPF、dotnet10もあるよ~ #clrh111
katsuyuzu
0
120
バックエンドエンジニアによる Amebaブログ K8s 基盤への CronJobの導入・運用経験
sunabig
0
160
re:Invent 2025 のイケてるサービスを紹介する
maroon1st
0
140
DevFest Android in Korea 2025 - 개발자 커뮤니티를 통해 얻는 가치
wisemuji
0
160
著者と進める!『AIと個人開発したくなったらまずCursorで要件定義だ!』
yasunacoffee
0
150
JETLS.jl ─ A New Language Server for Julia
abap34
1
420
Pythonではじめるオープンデータ分析〜書籍の紹介と書籍で紹介しきれなかった事例の紹介〜
welliving
1
270
非同期処理の迷宮を抜ける: 初学者がつまづく構造的な原因
pd1xx
1
740
UIデザインに役立つ 2025年の最新CSS / The Latest CSS for UI Design 2025
clockmaker
18
7.6k
Featured
See All Featured
Amusing Abliteration
ianozsvald
0
63
The Invisible Side of Design
smashingmag
302
51k
Mozcon NYC 2025: Stop Losing SEO Traffic
samtorres
0
75
技術選定の審美眼(2025年版) / Understanding the Spiral of Technologies 2025 edition
twada
PRO
115
91k
Redefining SEO in the New Era of Traffic Generation
szymonslowik
1
150
Hiding What from Whom? A Critical Review of the History of Programming languages for Music
tomoyanonymous
0
290
Learning to Love Humans: Emotional Interface Design
aarron
274
41k
Navigating Team Friction
lara
191
16k
The Curious Case for Waylosing
cassininazir
0
190
Effective software design: The role of men in debugging patriarchy in IT @ Voxxed Days AMS
baasie
0
170
The Straight Up "How To Draw Better" Workshop
denniskardys
239
140k
Jess Joyce - The Pitfalls of Following Frameworks
techseoconnect
PRO
1
20
Transcript
FFI: RustとWebAssemblyと JavaScriptと @d0iasm
何をしてきた? • インターンシップ @ Google ◦ Chrome OS / Chrome
browser • Coreboot (オープンソースBIOS) 開発 @ GSoC 今は何してる? • インターンシップ @ Wasmer (Rust 使用) • RISC-Vエミュレータ開発 @ 趣味 (Rust 使用) • 修論 @ 大学 (Rust 不使用) Doi Asami d0iasm
rvemu: RISC-V Emulator Web上で動くRustで書かれたRISC-Vエミュレータ ブログ: https://d0iasm.github.io/blog/risc-v/2019/12/19/fibonacci-numbers-on-my-riscv-emulator.html • 現在RV64Gをサポート • まだ実用レベルでは無い
• ほとんどの部分がRustで書 かれている
rvemu: RISC-V Emulator 仕組み • バックエンドの仕組みは必要なく、ブラウザだけで動く • メインのコード(CPUエミュレーション)がRustで書かれている • ブラウザがサポートしている言語は
JavaScriptのみ 疑問 • Rustでフロントエンド開発するために、どうやって JavaScriptとやり取りする? Rust JavaScript Browser
RustをWebAssemblyにコンパイル Rust JavaScript Browser WebAssembly compile • スタック形式のVMで動くバイ ナリ •
LLVMが生成形式のターゲッ トとしてサポート
RustをWebAssemblyにコンパイル Rust JavaScript Browser WebAssembly compile i32 | i64 |
f32 | f64 の型しか サポートしていない OK: fn add(a: i32, b: i32) -> i32 NG: fn concat(a: &str, b: &str) -> String
wasm-bindgen (https://github.com/rustwasm/wasm-bindgen) • RustとJS間のグルーコードを生成してくれるツール • FFI (foreign function interface) の一種
◦ 異なるプログラミング言語間でやりとりをするための仕組み • JSから呼び出したいRustの関数に #[wasm_bindgen] をつけると、文字列等の データのやりとりが可能になる wasm_bindgenによって生成される ファイル • sample.js: グルーコード • sample_bg.wasm: 関数本体 sample.rs $ wasm-pack build --target web
wasm-bindgen (https://github.com/rustwasm/wasm-bindgen) sample.rs index.js $ wasm-pack build --target web sample.js
sample_bg.wasm
使い方はわかった。 で、 どういう仕組み? wasm-bindgenの実装と 生成されたコードを読んでみました
wasm-bindgenで文字列を扱う仕組み lib.rs hoge.js (プロジェクト名.js) 生成 生成されたJSコードから予想できること 1. Wasmが持つメモリ領域に対して mallocを し、文字列を保存する
2. 確保したメモリのポインタを ptr0とする 3. Rust関数の本体(wasm.bar)に対し、ポイン タ(ptr0)と変数の長さ(len0)を引数として渡 す。型はどちらもi32
wasm-bindgenで文字列を扱う仕組み src/lib.rs in rustwasm/wasm-bindgen JSコードからの予想通り、 Wasmが持つメモリ領域に対し てメモリ確保をしていることを確 認
wasm-bindgenで文字列を扱う仕組み Rust JavaScript WebAssembly fn bar(a: &str) fn bar(ptr: i32,
len: i32) 変形&生成 f o o bar(“foo”); 1. メモリ確保 2. 文字列保存 3. ポインタと文字列の長さを 使ってWasmの関数を呼ぶ Linear Memory
まとめ • RustとJS間をWebAssemblyを介することによってやりとり可能にする wasm-bindgenのツールについて紹介した • wasm-bindgenがどのように文字列をやりとりしているのかを説明した • 今回は触れなかったが、 web-sysという、DOMをRustから操作できるライブラリも 存在する
FFIの技術を使用することで Rustでフロントエンド開発ができる! ―― Rust is everywhere ありがとうございました @d0iasm 今回使用したコード : https://github.com/d0iasm/wasm-bindgen-sample
• Rust, WebAssembly, and Javascript Make Three: An FFI Story
◦ https://youtu.be/nvLw_XKlZaU • rustwasm/wasm-bindgen/examples/without-a-bundler ◦ https://github.com/rustwasm/wasm-bindgen/tree/master/exampl es/without-a-bundler • Design of wasm-bindgen ◦ https://rustwasm.github.io/docs/wasm-bindgen/contributing/desi gn/index.html References