🎓 A1Learner
🔍
0 of 50 lessons completed

WebAssembly: Inspect a Module with WABT

WABT is a set of tools for looking inside WebAssembly modules. Its commands turn WAT into bytes and bytes back into WAT.

Build the binary

This command builds add.wasm from add.wat.

$ wat2wasm add.wat -o add.wasm

Turn it back into text

This command prints the same function back as WAT.

$ wasm2wat add.wasm

Check it is valid

This command checks add.wasm and prints the exit code.

$ wasm-validate add.wasm
$ echo $?

List the parts inside

This command lists the sections of add.wasm.

$ wasm-objdump -x add.wasm

Example

const B64 = "AGFzbQEAAAABBwFgAn9/AX8DAgEABwcBA2FkZAAACgkBBwAgACABags=";
(async () => {
  const bytes = Uint8Array.from(atob(B64), c => c.charCodeAt(0));
  const { instance } = await WebAssembly.instantiate(bytes, {});
  console.log('2 + 3 =', instance.exports.add(2, 3));
})();

Key points

  • WABT: a set of tools for working with WebAssembly modules.
  • wat2wasm: the tool that turns WAT text into a .wasm file.
  • wasm2wat: the tool that turns a .wasm file back into WAT text.
  • wasm-objdump: the tool that lists the parts inside a .wasm file.
  • wasm-validate: the tool that checks whether a .wasm file is valid.
  • Use wat2wasm to build a .wasm file from a .wat file.
  • Use wasm2wat to read a .wasm file you did not write.

In this example, the first line holds the module bytes as text. The next line turns that text into bytes. Then the code builds the module and waits for it. Then it calls add with 2 and 3. The line prints 5. Those bytes came from wat2wasm, run on the L8 WAT. The same bytes can be read back as text, listed, and checked.

Note

Silence from wasm-validate means the module passed.

Try it Yourself

Edit the code, then press Run.

Runs in your browser — the preview under the editor is the real result. Nothing is sent to a server. The code runner panel (right) does the same with more room.

Exercise

What does wasm2wat do?