What is WebAssembly Text (WAT)?
Any time you write a program in a high level language such as C, Python or Rust, then instruct the compiler to generate a WebAssembly1 file, the result will be a
.wasm file containing the binary op codes for your program. Since this is an executable file, it is intended only to be machine-readable, not human-readable. Most of the time, this is not a problem; however, as part of your development process, you may need to look inside this machine-readable file: and at this point you need a set of tools that can translate the WebAssembly op codes back into some sort of human-readable format.
Working With WebAssembly Files
When you need to directly manipulate a
.wasm file in some way, the WebAssembly Binary Toolkit (WABT) becomes a vital resource in your development tool bag. WABT provides you with a variety of tools for working directly with WebAssembly files, in both their binary and human-readable forms.
Using the WABT disassembler called
wasm2wat, you can transform a compiled WebAssembly module (a
.wasm file) back into a file containing the plain text op codes used by that program (a
.wat file). This plain text representation is known as WebAssembly Text (or WAT).
In addition to disassembling
.wasm files to plain text
.wat files, WABT also provides you with a tool called
wat2wasm that does the reverse. It takes a plain text WebAssembly Text file and assembles into an executable
The object of this tutorial is to give an introduction to writing programs directly in WebAssembly Text format.
But WebAssembly is Just a Compilation Target, So Why Bother?
WebAssembly certainly is a compilation target, and it is certainly true that the majority of WebAssembly programs will be generated by compilers, not humans. However, it is a weak line of reasoning to take this fact, and from it, conclude that there are therefore no cases in which it would be beneficial to write a WebAssembly Text program by hand.
Although this development process requires you to write the very low-level WebAssembly Text instructions by hand, it brings with it the benefits of being able to build an extremely small, highly efficient program ideally suited for performing CPU-bound tasks.
This introduction to WebAssembly Text serves as the starting point for a subsequent tutorial that describes how write a browser-based WebAssembly Text program that plots the Mandelbrot and Julia Sets.
Table of Contents
- Benefits of WebAssembly
- Creating a WebAssembly Module
- Calling WebAssembly from a Host Environment
- WAT Datatypes
- Local Variables
- Arrangement of WAT Instructions
- More About Functions
- WASM and Shared Memory
Please note: there is no space between the words “Web” and “Assembly” ↩