WebAssembly code is typically written and read in two formats: wasm which is a binary format and the WebAssembly Text (WAT) format. WAT, similar to assembly language for conventional machines, offers a human-readable version of WebAssembly binary code.
For example, if we have the following C++ code:
1#include <iostream>
2using namespace std;
3int main() {
4 cout << "Welcome to WebAssembly!";
5 return 0;
6}
This could be compiled down into a WAT representation that looks something like this:
1(module
2 (import "env" "putchar" (func $putchar (param i32)))
3 (func $main (export "main") (param) (result i32)
4 i32.const 0
5 )
6
7 (func $writeString (param i32)
8 get_local 0
9 call $putchar
10 drop
11 )
12)
The key takeaway here is that WAT is a representation of the binary code that can be written and read by humans. However, it's not as convenient to write as higher-level languages. WAT is more like a tool that aids in understanding, debugging, and optimizing WebAssembly applications.
Usually, we use WAT when we want a clear view of what our higher-level language code (like C++ or Rust) compiles down into, it helps to understand the instructions and structure of the code.
xxxxxxxxxx
using namespace std;
int main() {
cout << "Welcome to WebAssembly!";
return 0;
}