Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
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
920
Other Decks in Programming
See All in Programming
Flutterで分数(Fraction)を表示する方法
koukimiura
0
140
kiroとCodexで最高のSpec駆動開発を!!数時間で web3ネイティブなミニゲームを作ってみたよ!
mashharuki
0
890
フロントエンド開発のためのブラウザ組み込みAI入門
masashi
7
3.4k
NIKKEI Tech Talk#38
cipepser
0
190
3年ぶりにコードを書いた元CTOが Claude Codeと30分でMVPを作った話
maikokojima
0
630
SODA - FACT BOOK(JP)
sodainc
1
8.7k
Writing Better Go: Lessons from 10 Code Reviews
konradreiche
3
6.4k
What Spring Developers Should Know About Jakarta EE
ivargrimstad
0
450
はじめてのDSPy - 言語モデルを『プロンプト』ではなく『プログラミング』するための仕組み
masahiro_nishimi
4
14k
The Past, Present, and Future of Enterprise Java
ivargrimstad
0
440
他言語経験者が Golangci-lint を最初のコーディングメンターにした話 / How Golangci-lint Became My First Coding Mentor: A Story from a Polyglot Programmer
uma31
0
340
「ちょっと古いから」って避けてた技術書、今だからこそ読もう
mottyzzz
12
7.1k
Featured
See All Featured
Gamification - CAS2011
davidbonilla
81
5.5k
実際に使うSQLの書き方 徹底解説 / pgcon21j-tutorial
soudai
PRO
190
55k
How to Think Like a Performance Engineer
csswizardry
27
2.1k
Java REST API Framework Comparison - PWX 2021
mraible
34
8.9k
The Power of CSS Pseudo Elements
geoffreycrofte
80
6k
Scaling GitHub
holman
463
140k
Principles of Awesome APIs and How to Build Them.
keavy
127
17k
Writing Fast Ruby
sferik
630
62k
Rails Girls Zürich Keynote
gr2m
95
14k
"I'm Feeling Lucky" - Building Great Search Experiences for Today's Users (#IAC19)
danielanewman
230
22k
Let's Do A Bunch of Simple Stuff to Make Websites Faster
chriscoyier
508
140k
What's in a price? How to price your products and services
michaelherold
246
12k
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