Share
Embed
Perl on Ruby, Ruby on Python, Python on Perl, ...
YAPC::Tokyo 2026での発表資料です。
プロポーザル:
https://fortee.jp/yapc-tokyo-2026/proposal/6e17c376-6460-4fa7-bb63-1cf656e506ed
GitHub (dewasm/dewasm):
https://github.com/dewasm/dewasm
TSUYUSATO Kitsune
November 28, 2026
Annual Pro sale · Save $30
Have presentations of your own?
Share with private links, password protection, and more control over your work.
Transcript
86 slides
-
Perl on Ruby → Ruby on Python → Python on
Perl → …… Hiroya Fujinami (a.k.a. makenowjust) YAPC::Tokyo 2026 / 2026-11-29 (Sun) -
# => • Hiroy Fujin mi 藤浪 大弥 • Ph.D.
student t NII 国立情報学研究所の博士課程の学生 @m ke_now_just / • Ruby committer @m kenowjust Rubyというプログラミング言語のコミッタ • interests: progr mming/form l l ngu ges, regex a a a a a g a a a 興味のあること: プログラミング/形式言語、正規表現 a t @makenowjust.inspect 2 -
But not about running it in the browser. でも、ブラウザで W
sm を動かすという話ではありません。 a 4 -
Perl is a glue language. Perl は糊だと言われる。 a a a
a a a a T tsuhiko Miy g w - The T le of Pl ck 2/2 https://www.youtube.com/w tch?v=PhfpzbrOW0Q 7 -
How do you call Python code from Perl? じゃあ、どうやってPythonコードをPerlから呼び出す? +
(glue) Ex mple: Use Pygments from Perl. 例: PerlからPygmentsを使う ※ Pygments: synt x highlighting libr ry written in Python a a a a ※ Pygments: Pythonで書かれたシンタックスハイライトライブラリ 8 -
How do you call Python code from Perl? ── (1)
open '-|' どうやってPythonコードをPerlから呼び出す?── (1) open '-|' open my $py, '-|', 'python', '-c', '...Python code to call Pygments...'; my $html = do { local $/; <$py> }; 9 -
But it depends on the python command. python コマンドに依存してて微妙だ。 open
my $py, '-|', 'python', '-c', '...Python code to call Pygments...'; my $html = do { local $/; <$py> }; 11 -
How do you call Python code from Perl? ── (2)
XS どうやってPythonコードをPerlから呼び出す?── (2) XS SV *highlight(char *lang, char *code) CODE: PyObject *html = PyObject_CallFunction( highlight_fn, "ss", lang, code); RETVAL = newSVpv(PyUnicode_AsUTF8(html), 0); OUTPUT: RETVAL 13 -
But it still depends on the Python runtime. 今度は Python
ランタイムに依存してて微妙だよね。 M ke ile.PL WriteMakefile( NAME => 'PygmentsXS', LIBS => ['-lpython'], ); f a 15 -
Call Python from Perl without the Python runtime. Python ランタイムなしで
Perl から Python を呼びたい。 Possible? そんなん可能かいな。 16 -
How do you call Python code from Perl? ── (3)
open '-|' + Wasm どうやってPythonコードをPerlから呼び出す?── (3) open '-|' + w sm open my $py, '-|', 'wasmtime', 'cpython.wasm', '-c', '...Python code to call Pygments...'; my $html = do { local $/; <$py> }; a It works, but... 18 -
The dependency has merely moved from python to wasmtime. これは依存が
python から w smtime に移っただけでは? a 19 -
BTW, yesterday's talk... ところで、昨日のトークに…… go-perl は Perl 5.44.0 を W
sm にコンパイルし、 さらに Go へ変換することで、 Perl Interpreter を Pure Go の世界へ持ち込むプロジェクトです。 go-perl is project th t brings the Perl interpreter into the pure Go world by compiling Perl 5.44.0 to W sm nd then converting it into Go. a a a a a 23 -
In short, つまり、 Is th t lso possible...? ということは、こんなこともできるのでは……? cpython.wasm
perl-python a a 25 -
In short, つまり、 Is th t lso possible...? ということは、こんなこともできるのでは……? cpython.wasm
perl-python How does it work, nd wh t's fun bout it? a a a a a どうやってやるの? そして何が面白いん? 26 -
Chapter 2. dewasm: from WebAssembly to Source Code dew sm:
WebAssembly からソースコードへ a 27 -
dewasm is a translator dew sm は WebAssembly バイナリから 色んなプログラミング言語のソースコードへの変換器です。
from WebAssembly to source code. etc. a 28 -
dewasm is a translator dew sm は WebAssembly バイナリから 色んなプログラミング言語のソースコードへの変換器です。
from WebAssembly Wh t is it? to source code. etc. そもそも WebAssembly って何? a a 29 -
What is WebAssembly? WebAssembly って何? host W sm is isol
ted from the host. W smはホストの環境から隔離されてる。 a a a 30 -
どうやってW smはホストとやり取りする? export import memory Functions in W sm re
exposed to the host. Host functions re p ssed to W sm. import The host re ds nd writes it s byte rr y (line r memory). host export memory a a a a a a a a a a [0, 0, 0, ..., 72, 101, 108, ...] a a a How does Wasm communicate with the host? 31 -
dew smとは? • A WebAssembly to source code tr nsl
tor. dew sm/dew sm WebAssemblyからソースコードへの変換器。 • Written in Rust. Since 2026/07. Rustで書かれていて、2026年7月頃から開発している。 • Supported fe tures: W sm 1.0, WASI p1, EH, t il c lls. W sm 1.0, WASI preview 1, 例外処理、末尾呼び出しなどの機能をサポート。 • Supported b ckends: Ruby, Python, Perl, Go, J v , B sh Ruby, Python, Perl, Go, J v , B sh に変換可能。 a a a a a a a a a a a a a a a a 33 a g What is dewasm? -
Translation ow 変換の流れ 1 2 (module (func (export "calc") (param
$a i32) (param $b i32) (result i32) local.get $a local.get $b i32.add i32.const 2 i32.mul)) 3 p rse s IR IRとしてパース convert to source code ソースコードに変換 Return └─ Bin I32Mul ├─ Bin I32Add │ ├─ LocalGet 0 │ └─ LocalGet 1 └─ I32Const 2 return (l0 + l1) * 2 \ & 0xffffffff fl a a 34 -
(A little) optimization 様々な最適化 (そんなに大したことをやっていない) • One line per instruction
m kes the code much l rger, which slows down both p rsing nd execution. 1行1命令で変換するとコードが大きくなりすぎてパース時間とか実行が遅くなってヤバい。 → Fold expressions while building the IR. IRの構築時に式を畳み込んで多少縮むようにする。 • Overly l rge methods don't pl y well with Ruby JIT. Rubyの場合、デカすぎるメソッドがJITで上手く高速化されない。 → Extr ct the loop body into its own method. ループの内側の部分を、自動で別メソッドに切り出すことで対処。 a a a a a a a 35 -
どうやって作ったの? • Built with AI. Th nks to Cl ude
for Open Source. 大体AIにやらせた。Cl ude for Open Sourceに感謝。 • I h ven't re d much of th t code. そんなにコードを読んでいない。 • 77,000+ lines (impl: 45,000 lines, test: 32,000 lines) 行数は77000行くらい (実装が45000行、テストが32000行) • Across ll b ckends, we use the o ici l W sm nd WASI test suites, s well s our own E2E test c ses. すべてのバックエンドで、公式のW smとWASIのテストスイートや、独自のE2Eテストを使い、互換性を担保。 a a a ff a a a a a a a a a a 36 a a How did I build it? -
Three b ckends in one d y 1日で3言語のバックエンドを作れました 07-23 07-24
07-26 07-28 08-01 First commit! a a 37 -
Case study: doom.bash 100% pure b sh DOOM implement tion
converted from j cobenget/doom.w sm. 100% pure B shのDOOM実装 (doom.w sm から変換) Runs t 0.03 FPS (34s per fr me). 0.03FPSしか出ない (1フレーム34秒)。 a a a a a a a a 38 -
Case study: doom.bash doom.w sm (export "initGame" (func ...)) (export
"tickGame" (func ...)) (export "memory" ...) … (import "ui" "drawFrame" (func (param i32))) a a render_frame() { for (( y = 0; y < H; y++ )); do for (( x = 0; x < W; x++ )); do o=$(( FRAME_BUF_OFF + (y * W + x) * 4 )) b=${doom_memory[$o]} g=${doom_memory[$((o + 1))]} r=${doom_memory[$((o + 2))]} buf+="\e[48;2;${r};${g};${b}m " done buf+="\e[0m\n" done printf '%b' "$buf" } converted by dew sm imp_draw_frame() { FRAME_BUF_OFF=$1 R0= } declare -A IMPORTS=( ['ui.drawFrame']=imp_draw_frame ... ) … doom.b sh a doom_initGame() { ... } doom_tickGame() { ... } declare -gA doom_memory=() doom_initGame while :; do doom_tickGame render_frame done 39 -
Case study: doom.go (GUI) doom.w sm d.Exports["initGame"] = func() {
... } d.Exports["tickGame"] = func() { ... } d.memory.data = make([]byte, 72*65536) (export "initGame" (func ...)) (export "tickGame" (func ...)) (export "memory" ...) … (import "ui" "drawFrame" (func (param i32))) a a func hostDrawFrame(ptr uint32) { mem := d.memory.data for i := 0; i < W*H; i++ { o := int(ptr) + i*4 frameBuf[i*4+0] = mem[o+2] // R frameBuf[i*4+1] = mem[o+1] // G frameBuf[i*4+2] = mem[o+0] // B frameBuf[i*4+3] = 0xff // A } } imports := Imports{ "ui": {"drawFrame": hostDrawFrame}, ... } … doom.go converted by dew sm func (g *Game) Update() error { g.tickGame() return nil } func (g *Game) Draw(screen *ebiten.Image) { 40 screen.WritePixels(frameBuf) } -
Let's try it! やってみよう! cpython.wasm perl-python $ dewasm cpython.wasm --target
perl \ --mode library --module-name CPythonWasm \ -o CPythonWasm.pm 41 -
Let's try it! やってみよう! use CPythonWasm; my $py = CPythonWasm->new({},
args => ['python', '-c', '...Python code to call Pygments...']); $py->invoke('_start'); 42 -
Chapter 3. Rails on dewasm or pure Ruby on Rails
R ils on dew sm, あるいはpure Ruby on R ils a a a 46 -
Ruby is on Rails, but Rails is not. RubyはR ilsの上にあるが、R
ilsはそうとは限らない。 a a $ bundle install ... Fetching activemodel 8.1.4.1 Fetching timeout 0.6.1 Fetching activerecord 8.1.4.1 Fetching marcel 1.2.1 Fetching activestorage 8.1.4.1 Fetching mini_mime 1.1.5 Fetching date 3.6.0 Fetching net-protocol 0.4.0 Installing date 3.6.0 with native extensions Fetching net-imap 0.6.8 Fetching net-smtp 0.5.2 Fetching mail 2.9.1 Fetching actionmailbox 8.1.4.1 Fetching actionmailer 8.1.4.1 Fetching actiontext 8.1.4.1 Fetching bcrypt 3.1.22 Fetching cruise 0.3.0 (arm64-darwin) Installing bcrypt 3.1.22 with native extensions Fetching herb 0.11.2 (arm64-darwin) ... C extensions 47 -
C extensions (current) C拡張 (現状) downlo d pre-built bin ry
require rubygems.org compile a a a C extension source code C extension (.so) Your pp 48 -
C extensions + dewasm C拡張 + dew sm compile C
extension source code require dew sm C extension (.w sm) C extension (.rb) Your pp a a a a 49 -
C extensions + dewasm C拡張 + dew sm ruby.h for
dew sm dew sm_cext.rb def rb_class_new(sup) = to_v(Class.new(from_v(sup))) __attribute__((import_module("ruby"), import_name("rb_class_new"))) VALUE rb_class_new(VALUE); + import compile a a a a a C extension source code dew sm C extension (.w sm) C extension (.rb) Ruby object -> int def to_v(obj) case obj when nil then 4 when -0x4000_0000..0x3fff_ffff (obj << 1) | 1 else @objs << obj (@objs.size - 1) << 3 end end int -> Ruby object def from_v(v) return v >> 1 if v.odd? 50 v == 4 ? nil : @objs[v >> 3] end -
Rails on dewasm dew smの上にR ilsを載せる Gem ile require_relative "dewasm_cext/gemfile"
... group :dewasm do dewasm_gem "sqlite3", "2.10.2" dewasm_gem "bcrypt", "3.1.22" dewasm_gem "herb", "0.11.2" dewasm_gem "nokogiri", "1.19.7" dewasm_gem "pygments.rb", "5.0.0" end a f a ... $ bundle install ... dewasm-cext: building sqlite3 2.10.2 dewasm-cext: fetching sqlite3-2.10.2.gem dewasm-cext: built sqlite3 2.10.2 -> vendor/dewasm/gems/sqlite3-2.10.2 dewasm-cext: building bcrypt 3.1.22 dewasm-cext: fetching bcrypt-3.1.22.gem dewasm-cext: built bcrypt 3.1.22 -> vendor/dewasm/gems/bcrypt-3.1.22 dewasm-cext: building herb 0.11.2 dewasm-cext: fetching herb-0.11.2.gem dewasm-cext: built herb 0.11.2 -> vendor/dewasm/gems/herb-0.11.2 dewasm-cext: building nokogiri 1.19.7 dewasm-cext: fetching nokogiri-1.19.7.gem dewasm-cext: building libxml2 2.13.9 for wasm32-wasip1 dewasm-cext: building libxslt 1.1.43 for wasm32-wasip1 dewasm-cext: built nokogiri 1.19.7 -> vendor/dewasm/gems/nokogiri-1.19.7 dewasm-cext: building pygments.rb 5.0.0 ... 51 -
demo: /session/new デモ: /session/new Retrieve user info from sqlite3 nd
verify the p ssword using bcrypt. ユーザー情報をsqlite3から取得、 bcryptでパスワードを検証。 C extension (.rb) a a sqlite3 gem bcrypt gem ... 52 -
demo: /posts/new デモ: /posts/new S nitize <script> t g using
Nokogiri. <script> タグをNokogiriでサニタイズ。 S ve a a a a nokogiri gem sqlite3 gem ... C extension (.rb) post with sqlite3. 投稿をsqlite3に保存。 53 -
demo: /posts/3 デモ: /posts/3 S nitize <script> t g using
Nokogiri. <script> タグをNokogiriでサニタイズ。 a a nokogiri gem sqlite3 gem ... C extension (.rb) 54 -
demo: /posts/3 (HTML highlight) デモ: /posts/3 (HTMLハイライト) Highlight HTML with
pygments.rb. pygments.rbでHTMLをハイライト。 pygments.rb gem ... C extension (.rb) 55 -
pygments.rb + dewasm dew sm require cpython.w sm pygments.rb python.rb
(dew sm) a a a 60 -
pygments.rb + dewasm: problem from pygments import highlight from pygments.lexers
import get_lexer_by_name from pygments.formatters import HtmlFormatter highlight(code, get_lexer_by_name("html"), HtmlFormatter()) The irst highlight t kes 40 seconds. (initi liz tion + import) dew sm python.rb (dew sm) cpython.w sm 初回のハイライトに40秒かかる! a a a a a a f (インタープリタの初期化 + importの時間) 61 -
W smのメモリについて思い出してみると、 from pygments import highlight from pygments.lexers import get_lexer_by_name
from pygments.formatters import HtmlFormatter highlight(code, get_lexer_by_name("html"), HtmlFormatter()) fter... By lo ding the s ved memory, py.memory.buffer.values(:U8) initi liz tion nd imports # => [0, 0, 0, ..., 72, 101, 108, ...] c n be skipped! memory = py.memory.buffer.get_string File.binwrite("memory.bin", memory) 保存したメモリを読み込むことで、 初期化とimportをスキップできる! a a a a a a py.memory.buffer.set_string(File.binread("memory.bin")) a a Recall Wasm memory, 62 -
Wizer: The WebAssembly Pre-Initializer! Wizer: WebAssemblyを初期化するやつ! • bytecode lli nce/wizer
• Execute the pre-initi lize function nd b ke the resulting st te (memory + glob ls, etc.) into the W sm ile. 初期化関数を実行して、結果の状態 (メモリとグローバル変数など) をw smファイルに焼き付ける。 a a a a a f a a a a 64 -
pygments.rb + Wizer + dewasm bundle inst ll (dewasm_gem "pygments.rb")
$ wizer cpython.wasm -o cpython-pre.wasm \ --init-func pyg_init \ --allow-wasi --mapdir /gem::./gem $ dewasm cpython-pre.wasm --target ruby \ --mode library --module-name PythonPreWasm \ -o python_pre.rb 40s 1s a 初回のハイライトが1秒になった! 65 -
Make perl.py & python.rb perl.py と python.rb を作る $ dewasm
perl.wasm --target python \ --mode library --module-name perl -o perl.py $ dewasm cpython.wasm --target ruby \ --mode standalone -o python.rb 68 -
Run Perl on Python on Ruby Perl onPython on Rubyの実行
$ cat hello.py import perl perl.eval('printf("hello from perl %d\n", 6 * 7);') $ time ruby python.rb hello.py hello from perl 42 ruby python.rb hello.py 53.26s total It's too slow. さすがに遅い。 69 -
Make perl-python.rb with Wizer perl-python.rb をwizerで作る。 $ wizer cpython.wasm -o
perl-python.wasm \ --init-func perl_init \ --allow-wasi --mapdir /py::./py $ dewasm perl-python.wasm --target ruby \ --mode library --module-name PerlPython \ -o perl-python.rb Wizer + dew sm a 71 -
Run perl-python.rb perl-python.rb の実行 $ cat hello.rb require_relative "perl-python" PerlPython.eval
\ 'printf("hello from perl %d\n", 6 * 7);' $ time ruby hello.rb hello from perl 42 ruby hello.rb 4.71s total 10x f ster! a 10倍以上早くなった! 72 -
Other cases (with Wizer) その他の場合 (Wizerを添えて) Perl on Ruby on
Python Python on Ruby on Perl → 3.7 seconds → 4,617 seconds (~77 minutes) 73 -
What I wanted to talk about in this talk. このトークで何を話したかったのか。
• A typic l t lk bout W sm: (some l ngu ge or fr mework) c n work in the browser! W smについてのよくある話: (ある言語とかフレームワークが) ブラウザで動くぞ! • But W sm h s more interesting fe tures, such s port bility nd s ndboxing. でも、W smって他にも面白い機能が色々あるよね、ポータビリティとかサンドボックスとか。 • Focus on them! W sm is fun! それらに注目して欲しい! W smはいいぞ。 a a a a a a a a a a a a a a a a a a a 78 -
Play and Hack with Wasm! W smで遊ぼう、そしてハックしよう! a a a
a a https://github.com/dew sm/dew sm L st slide. Th nk you! ご清聴ありがとうございました。 86