The wasm_of_ocaml runtime, an OCaml-to-WebAssembly compiler, contains nearly 27,000 lines of hand-written WebAssembly text format (WAT). Developers often find 27,000 lines of WAT tedious to maintain. Wax is a new tool offering a Rust-like syntax that compiles to exactly the same instructions.
It includes an OCaml toolchain converting between Wax and WebAssembly's binary and text formats bidirectionally. While wasm_of_ocaml originated the format, Wax targets anyone writing WebAssembly by hand. The article explains WebAssembly's stack-based binary format and its text format, WAT, noting that the binary is not human-readable, necessitating the text format for inspection.
It details how a simple addition function appears in binary (seven bytes), WAT (parenthesized S-expressions), and Wax (compact Rust-like syntax). Crucially, Wax preserves the compiled output byte-for-byte. The text format's verbosity increases with the WebAssembly GC proposal, which adds structs and references for garbage-collected languages like OCaml.
Wax simplifies this, allowing code like linked list traversal with concise syntax. The toolchain ensures that what the developer writes in Wax is precisely what the Wasm engine executes, bridging the gap between developer ergonomics and machine efficiency.
Source: Hacker News · Summarized by HeadlinesBriefing