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 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 / 森建
Security issues being discussed on Web Platforms
petamoriken
0
1.4k
2年かけて Deno に DOMMatrix を実装した話 / How I implemented DOMMatrix in Deno over two years
petamoriken
0
250
WebAssembly を読み込むベストプラクティス 2026年春版 / Best Practices for Loading WebAssembly (Spring 2026)
petamoriken
5
1.3k
コントリビューターによるDenoのすゝめ / Deno Recommendations by a Contributor
petamoriken
0
290
Module Harmony
petamoriken
3
1.2k
Denoでフロントエンド開発 2025年春版 / Frontend Development with Deno (Spring 2025)
petamoriken
1
1.7k
非ブラウザランタイムとWeb標準 / Non-Browser Runtimes and Web Standards
petamoriken
0
690
ふかぼれ!CSSセレクターモジュール / Fukabore! CSS Selectors Module
petamoriken
0
360
フロントエンドの標準仕様をどう追っているか / How I follow the frontend standards specs
petamoriken
4
2.9k
Other Decks in Programming
See All in Programming
IBM Bob Dojo #1 仕様駆動開発入門
oniak3ibm
PRO
0
350
GemmaをJevのように使ってみる / Use Gemma like Jev
kishida
5
700
動作中のプログラムの中身をリアルタイムに覗く / Realtime Debugger for CSharp with Roslyn
prota
1
1.8k
手動確認はもう限界 〜XCUITestでCustom URL Schemeの遷移を起動種別ごとに自動テストする〜 / Testing Custom URL Schemes with XCUITest
otouto
0
390
APNsからLive Activityを開始する話
yumnumm
0
150
JAWS-UG 東京支部が始める、JAWS-UG支部コラボ / JAWS-UG lunchtime LT Collaboration
y0hgi
0
190
Java 27新機能 / Java 27 new features
kishida
2
200
Everything will be SERVERLESS — 信じて運用した10年の経験値 / Everything Will be Serverless — Lessons Learned from 10 Years of Operational Experience
seike460
PRO
1
750
20260914 AIエージェント時代のPlatform Engineering LLM基盤とプロダクトの責務境界線
kanfab1
7
2.4k
SREの越境 / SRE Collaboration
y0hgi
2
300
Verilogで学ぶCPU自作入門.pdf
uyuki234
8
4k
Herb in Rails 8.2: Your ERB views, now HTML-aware @ Rails World 2026, Austin, Texas
marcoroth
0
190
Featured
See All Featured
Rails Girls Zürich Keynote
gr2m
96
14k
JAMstack: Web Apps at Ludicrous Speed - All Things Open 2022
reverentgeek
1
620
Marketing Yourself as an Engineer | Alaka | Gurzu
gurzu
0
320
Cheating the UX When There Is Nothing More to Optimize - PixelPioneers
stephaniewalter
287
14k
Lightning talk: Run Django tests with GitHub Actions
sabderemane
0
290
Visualization
eitanlees
153
17k
A Guide to Academic Writing Using Generative AI - A Workshop
ks91
PRO
1
510
Performance Is Good for Brains [We Love Speed 2024]
tammyeverts
12
1.9k
The Pragmatic Product Professional
lauravandoore
37
7.5k
Marketing to machines
jonoalderson
1
5.9k
VelocityConf: Rendering Performance Case Studies
addyosmani
331
25k
How STYLIGHT went responsive
nonsquared
100
6.3k
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 まとめ