I should go back to nand2tetris soon.
According to this, “the textual format for WebAssembly modules is a rendering of their abstract syntax into S-expressions.”
You can write a C or C++ function in WasmExplorer and compile it to see an example.
This function:
int add(int a, int b) {
return a + b;
}
will compile to:
(module
(table 0 anyfunc)
(memory $0 1)
(export "memory" (memory $0))
(export "_Z3addii" (func $_Z3addii))
(func $_Z3addii (; 0 ;) (param $0 i32) (param $1 i32) (result i32)
(i32.add
(get_local $1)
(get_local $0)
)
)
)
and then clicking “download” will get you a binary .wasm
file that can run in the browser.
If you have the WebAssembly Binary Toolkit installed, you can convert back and forth between wat
and wasm
formats. You can write raw WebAssembly Text, but I think WebAssembly is meant to be written in Rust/C/C++ or other languages.
Edit: here’s a page that explains it:
https://developer.mozilla.org/en-US/docs/WebAssembly/Understanding_the_text_format