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
WebAssembly by Rust
Search
petamoriken / 森建
February 19, 2019
Programming
1.6k
0
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
WebAssembly by Rust
Fukuoka.rs vol.2
https://fukuokars.connpass.com/event/119615/
petamoriken / 森建
February 19, 2019
More Decks by petamoriken / 森建
See All by petamoriken / 森建
2年かけて Deno に DOMMatrix を実装した話 / How I implemented DOMMatrix in Deno over two years
petamoriken
0
220
WebAssembly を読み込むベストプラクティス 2026年春版 / Best Practices for Loading WebAssembly (Spring 2026)
petamoriken
5
1.2k
コントリビューターによるDenoのすゝめ / Deno Recommendations by a Contributor
petamoriken
0
280
Module Harmony
petamoriken
3
1.1k
Denoでフロントエンド開発 2025年春版 / Frontend Development with Deno (Spring 2025)
petamoriken
1
1.6k
非ブラウザランタイムとWeb標準 / Non-Browser Runtimes and Web Standards
petamoriken
0
680
ふかぼれ!CSSセレクターモジュール / Fukabore! CSS Selectors Module
petamoriken
0
350
フロントエンドの標準仕様をどう追っているか / How I follow the frontend standards specs
petamoriken
4
2.8k
ECMAScript、Web標準の型はどう管理されているか / How ECMAScript and Web standards types are maintained
petamoriken
3
680
Other Decks in Programming
See All in Programming
仕様駆動開発の消費期限
watany
20
9k
ソフトウェアエンジニアにとっての生成AI - 特性を知って使い倒す / generative ai for software enginner
kishida
7
2.1k
AI時代に学ぶ 好きなルール 嫌いなルール Linter編
shorty5121
0
150
Dockerfile CMD for Node.js
grazie1999
0
120
Claude CodeとAgentCore Gatewayを繋ぐ際の認証認可 / Authentication and authorization when connecting Claude Code with AgentCore Gateway
har1101
2
380
自動化したのに回らない テスト運用の壁―AI時代の品質責任と生産性
mfunaki
0
510
書籍「プロフェッショナルAI駆動開発」紹介スライド
juntaromatsumoto
0
590
Pythonの実行はどこまで賢くなったのか? CPythonとPyPyから見る最適化のしくみ
curekoshimizu
3
2k
実装をデザインガイドラインに追従させるための取り組み / 260731-dip-mosh-design-system
dachi023
0
7.8k
AIと壁打ちしながら進めるコスト管理
fufuhu
2
1.7k
不幸な GC
chencmd
0
740
Discordを用いたラボオートメーション関連情報収集の自動化
noguhiro2002
0
440
Featured
See All Featured
16th Malabo Montpellier Forum Presentation
akademiya2063
PRO
0
360
Stop Working from a Prison Cell
hatefulcrawdad
274
21k
Why You Should Never Use an ORM
jnunemaker
PRO
61
10k
Refactoring Trust on Your Teams (GOTO; Chicago 2020)
rmw
35
3.8k
The State of eCommerce SEO: How to Win in Today's Products SERPs - #SEOweek
aleyda
2
11k
Everyday Curiosity
cassininazir
0
290
How to Think Like a Performance Engineer
csswizardry
28
2.7k
How to make the Groovebox
asonas
2
2.3k
DBのスキルで生き残る技術 - AI時代におけるテーブル設計の勘所
soudai
PRO
68
57k
Darren the Foodie - Storyboard
khoart
PRO
3
3.7k
The Art of Programming - Codeland 2020
erikaheidi
57
14k
Designing for Timeless Needs
cassininazir
1
450
Transcript
WebAssembly by Rust 明日からプロダクトに使える WebAssembly pixiv Inc. 森内 建太 2019.2.19
2 自己紹介 • 主にフロントエンドエンジニア • Ruby on Rails、iOS アプリ開発もやる •
ECMAScript とか DOM API とかの仕様まわりを調 べるのが好き • 今好きな HTTP レスポンスヘッダは Report-To petamoriken 福岡オフィスプロジェクト エンジニア
WebAssembly とは 3
4 • アセンブリではないバイナリ形式(32bit) • ブラウザでなくても実行できる ◦ モダンブラウザ Chrome / Firefox
/ Safari / Edge で扱える ◦ Node.js v8 以降で使える(AWS Lambda, BigQuery...) • JavaScript から WebAssembly を呼ぶインターフェースも定義されている ◦ 初期化時に函数などの受け渡しが可能(後述) WebAssembly とは
シンプルな例 5 #[no_mangle] pub extern "C" fn succ(x: i32) ->
i32 { x + 1 } const { instance } = await WebAssembly.instantiateStreaming(fetch("foo.wasm")); const exports = instance.exports; console.log(exports.succ(42)); // 43
函数の受け渡しの例 6 extern { fn console_log(x: i32); } #[no_mangle] pub
extern "C" fn succ_log(x: i32) { unsafe { console_log(x + 1); } } const imports = { env: { console_log: console.log } }; const { instance } = await WebAssembly.instantiateStreaming(fetch("foo.wasm"), imports); const exports = instance.exports; exports.succ_log(42); // console.log(42 + 1)
7 • 受け渡す函数は引数で数値型( i32, i64, f32, f64)のみ扱える • その他受け渡せるもの ◦
Memory ▪ WebAssembly で使われるヒープ。バッファを渡したいときにはこれに積んでか らその先頭ポインタ(i32 と同等)を函数に渡す ◦ Table (今のところ Function Table のみ) ◦ Global Variable (数値型のみ。Chrome, Firefox のみ対応) 初期化時の受け渡しについて
バッファの受け渡しの例 8 use std::{mem, alloc as std_alloc}; #[no_mangle] pub unsafe
extern "C" fn alloc(size: usize) -> *mut u8 { let layout = std_alloc::Layout::from_size_align(size, mem::align_of::<usize>()).unwrap(); std_alloc::alloc(layout) } #[no_mangle] pub unsafe extern "C" fn dealloc(ptr: *mut u8, size: usize) { if size == 0 { return } let layout = std_alloc::Layout::from_size_align_unchecked(size, mem::align_of::<usize>()); std_alloc::dealloc(ptr, layout) }
バッファの受け渡しの例 9 const { instance } = await WebAssembly.instantiateStreaming(fetch("foo.wasm")); const
exports = instance.exports; const ptr = exports.alloc(8); new Uint8Array(exports.memory.buffer).set([0, 1, 2, 3, 4, 5, 6, 7], ptr); exports.foo(ptr, 8); exports.dealloc(ptr, 8); use std::slice; #[no_mangle] pub unsafe extern "C" fn foo(ptr: *const u8, size: usize) { let buf = slice::from_raw_parts(ptr, size); // ... }
wasm-bindgen 10
11
12 • Rust (WebAssembly) と JavaScript 間のラッパー • JavaScript の函数の型を書くとインポートしてくれるし、
String 型の変換もする ◦ 多くの JavaScript の型が Rust 上では JsValue という型になる ▪ Optional unwrap 祭りになってそれはそれで辛い • 標準函数は自動でインポートすることが出来る(力技) ◦ js-sys ▪ ECMAScript の函数を全て Rust でラップするクレート ◦ web-sys ▪ DOM API の函数を全て(ry wasm-bindgen とは
13 • JavaScript をラップするだけではなく TypeScript の型ファイルも作ってくれる • wasm-pack ◦ package.json
を吐き出すので npm にそのまま publish 出来る ◦ wasm-pack-plugin を使うと webpack であたかも普通のモジュールかのように扱 える Rust における wasm-bindgen と wasm-pack と cargo-web と stdweb の違い https://qiita.com/legokichi/items/5d6344314ab6d6633554 wasm-bindgen とは
14 • Rust で書く WebAssembly はもうプロダクトに使えそう ◦ wasm-bindgen (web-sys) を使うと
DOM API にアクセスすることが出来るがあまり メリットはない ◦ 要所での重い処理(バッファの変換とか)に向いている ▪ WebP のデコードとか 詳しくは個人の Scrapbox にまとめてますので興味があれば https://scrapbox.io/petamoriken/WebAssembly_by_Rust まとめ