WebAssemblyを使うことでコンパイルされたコードをWebブラウザ上で実行できるようになります。Safariでもサポートされたことで、実用的になってきたと言えます。そんな WebAssemblyはC/C++/Rustで書くのが基本です。 そんな WebAssemblyのコードを解析したり、CUIで実行できるのがjs-webassembly-interpreterになります。

js-webassembly-interpreterの使い方

js-webassembly-interpreterによるダンプです。

$ wasmdump add.wasm 
0x0:   0x0 0x61 0x73 0x6d     ; wasm magic: header
0x4:   0x1 0x0 0x0 0x0        ; wasm version
; section Type
0xa:   0x1                    ; section code
0xa:   0x0                    ; section size (ignore)
0xb:   0x1                    ; num types
; type 0
0xc:   0x60                   ; func
0xd:   0x2                    ; number
0xe:   0x7f                   ; i32
0xf:   0x7f                   ; i32
0x10:   0x1                   ; number
0x11:   0x7f                  ; i32
; section Function
0x13:   0x3                   ; section code
0x13:   0x0                   ; section size (ignore)
; section Export
0x17:   0x7                   ; section code
0x17:   0x0                   ; section size (ignore)
0x18:   0x1                   ; num exports
0x1c:                         ; export name (add)
0x1d:   0x0                   ; export kind
0x1e:   0x0                   ; export func index
; section Code

処理内容はさすがに分かりませんが、APIは分かります。他にも WebAssemblyのバイナリを実行する wasmrun があります。

$ wasmrun add.wasm add 1 3
Compiling...
Executing...
exited with code 31

他にASTのJSONに変換するコマンドもあります。WebAssemblyのバイナリを解析してそのAPIを調べたり、できあがったバイナリをターミナル上で実行するのに便利なコマンドが揃っています。WebAssemblyを使った開発時にぜひ役立ててください。

js-webassembly-interpreterはnode/JavaScript製のオープンソース・ソフトウェア(GPL)です。

xtuc/js-webassembly-interpreter: WebAssembly interpreter