1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

wasmtimeのドキュメントを読む

Posted at

WASMの理解を深めるために wasmtime のドキュメントを読む。英語の技術文章を読む練習にもなるので一石二鳥。

wasmtime.dev

  • wasmtime と rust をインストール (コマンド一発で簡単なのでWSL2に入れた)
  • hello.rs を rustc hello.rs --target wasm32-wasi で wasm にビルド
  • wasmtime で実行 (wasmer でも同じく実行できた!)

wasmtime-rb

https://github.com/bytecodealliance/wasmtime-rb
yard: https://bytecodealliance.github.io/wasmtime-rb/latest/

Rubyスクリプトから WASM を実行できるgem。内部実装的には各処理系毎のwasmtimeを呼んでいるだけっぽいがインストールはgemに隠ぺいされているので便利。

require "wasmtime"

engine = Wasmtime::Engine.new
mod = Wasmtime::Module.from_file(engine, "hello.wasm")

linker = Wasmtime::Linker.new(engine, wasi: true)

wasi_ctx = Wasmtime::WasiCtxBuilder.new
  .set_stdin_string("hi!")
  .inherit_stdout
  .inherit_stderr
  .set_argv(ARGV)
  .set_env(ENV)
store = Wasmtime::Store.new(engine, wasi_ctx: wasi_ctx)

instance = linker.instantiate(store, mod)
instance.invoke("_start")

ドキュメントが若干不足しているがrustのcrateが充実しているのでそっちのドキュメント読むのがよさそう。yard内に同じ概念の型がリンクを貼っている。

まとめ

サンプルでも紹介されているが markdown パーサーみたいなやつは一回実装書けばどこの言語でも安定して使えるようになって便利では。RubyやPythonだとC拡張に相当するものをWASMで書けばOS毎に別の実装を使わずに済む。

例えば https://github.com/silvia-odwyer/photon を内部に抱いた画像操作用のgemとか作れるだろうか。

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?