Tarides introduces Wax, a Rust-like syntax for WebAssembly

Tarides introduces Wax, a Rust-like syntax for WebAssembly

A Principal Software Engineer at Tarides has published Wax, a Rust-like syntax for WebAssembly that compiles to exactly the same instructions as the equivalent hand-written WebAssembly. It ships with a toolchain written in OCaml that converts between Wax and WebAssembly's text format (WAT) and binary format in every direction. Wax grew out of wasm_of_ocaml, the OCaml-to-WebAssembly compiler developed alongside js_of_ocaml, but the author says it is meant for anyone who writes WebAssembly by hand.

The motivation is the wasm_of_ocaml runtime: nearly 27,000 lines of WebAssembly written by hand, spread over about fifty .wat files. It is the library of primitives that compiled OCaml code calls for strings, polymorphic comparison, hashing, marshalling and effect handlers. The author argues the text format was designed so a tool could print a binary and a human could check it, not so someone would write twenty-seven thousand lines of it. The maintainers, the author included, work in S-expressions where every variable read is a local.get and every loop is a loop with a branch back to its start. The WebAssembly GC proposal, now part of the standard, adds structs, arrays, nullable references, subtyping and downcasts, and makes the text format wordier still. Reading a struct field is (struct.get $list $value (local.get $l)) in WAT and l.value in Wax; both are the same two instructions.

The post walks through the same function in three forms. An add function has a seven-byte body in the binary; the WAT version is a folded S-expression; the Wax version is a short function with an #[export="add"] attribute and the body x + y. Compiling the Wax gives the seven bytes byte for byte. A second example sums a linked list: a struct type with a value and a nullable next reference, a while loop that tests l is &list, and a += that becomes an i32.add followed by a local.set. The while is a loop with a br back to its start. Notation is small: &t is a non-null reference, &?t may be null, e is &t is a type test, e as &t is a cast, and the types are those of Wasm (i32, i64, f32, f64 and references).

The tool is a single binary. wax input.wax -o output.wasm compiles to binary, wax input.wax -f wat compiles to text, wax program.wasm decompiles a binary to Wax, wax input.wat converts WAT to Wax, wax format -i reformats in place and wax check only type-checks. Converting between the two text formats keeps comments.

The most interesting engineering, the author says, is the reverse direction. As a demonstration the post decompiles caml_string_equal, the runtime's implementation of OCaml's String.equal. The decompiler moves each local to a let at its first assignment, recognises a loop ending in br $loop as a while and moves the increment into the loop header as a continue-expression (: (i += 1)), and drops redundant type annotations because the casts already say what the values are. Details of the notation come along: &eq is the type of any OCaml value in wasm_of_ocaml, unallocated values such as integers and booleans are &i31, so 1 as &i31 is OCaml's true, and a string is a &bytes. Reading a byte as s1[i] as i32_u gives an unsigned value between 0 and 255 through zero extension (array.get_u), while as i32_s would read it as signed through sign extension (array.get_s). Across the whole file, about 480 lines of WAT shrink to about 320 lines of Wax, and the result type-checks and compiles back.

Because the reverse arrow is how a project with an existing .wat codebase can try Wax at all, the author stresses trustworthiness. A round trip, decompiling then recompiling, gives back the same instruction stream: for string.wat, all instructions in the same order. Only names and numbering change, for example when a local's declaration moves or an anonymous function gets a name. By default the decompiler also tidies up a few idioms: an i32.eq followed by i32.eqz becomes a single !=, and a cast that cannot fail is dropped. The --faithful flag turns this off so the result recompiles instruction for instruction, and a nightly fuzzing run holds it to that on machine-generated modules, unreachable code included. The default mode is checked on behaviour: modules from the official WebAssembly test suite go to Wax and back, and the suite's assertions are re-run against the result.

Wax has its own type checker, so a mistake is reported in the Wax source at the exact spot instead of as a validation failure when an engine loads the module. The post shows an error for adding an i32 to an f64, with file, line and column. The author says he put a lot of effort into the messages: each of the hundreds of places where the parser can fail has its own message that says what was expected and, when an unclosed bracket is the likely cause, points back at it. They are generated from the grammar with a few written by hand, and automated checks make sure each one talks about what the user wrote and that what it says was expected is correct. Beyond type checking, Wax has a formatter, configurable lints, conditional compilation for build flags, and source maps for stepping through the generated Wasm in browser DevTools. Editor support includes a VS Code extension that runs the whole toolchain inside the editor, compiled to WebAssembly, so it works in desktop VS Code and on vscode.dev, plus tree-sitter syntax highlighting for Neovim and Emacs and wax lsp, a Language Server Protocol server offering diagnostics as you type and types on hover. The visible text breaks off in this section.

Key facts

  • Wax is a Rust-like syntax for WebAssembly that compiles to exactly the same instructions; the add example compiles to the same seven bytes as the WAT version.
  • The OCaml-written toolchain converts between Wax, WAT and binary in every direction, and also formats, type-checks and decompiles.
  • It came from wasm_of_ocaml, whose runtime is nearly 27,000 lines of hand-written WebAssembly in about fifty .wat files.
  • On caml_string_equal, about 480 lines of WAT become about 320 lines of Wax, and a decompile-recompile round trip returns the same instruction stream.
  • The --faithful flag guarantees instruction-for-instruction recompilation, checked by nightly fuzzing; the default mode, which tidies idioms, is checked on behaviour against the official WebAssembly test suite.

Why it matters

WebAssembly's text format was meant for inspecting compiler output, not for writing code, yet the wasm_of_ocaml runtime is nearly 27,000 lines of it. The GC proposal, now part of the standard, makes WAT wordier still. Wax offers a more readable way to write the same instructions without changing what runs: the one-to-one mapping means the author can say what you write is what runs. The two-way conversion is the notable part, since it lets a project with an existing .wat codebase move over and back.

Who it affects

The author says Wax is meant for anyone who writes WebAssembly by hand, not only OCaml developers. The first audience is the maintainers of the wasm_of_ocaml runtime, the author among them. Others who could care are people working with the WebAssembly GC features such as structs, arrays and nullable references, and anyone who has to read WAT or binary output and wants a more compact view of it.

How to use it

Everything is in one binary called wax. Compile with wax input.wax -o output.wasm for binary or wax input.wax -f wat for text. Run wax program.wasm to decompile a binary to Wax, wax input.wat to convert WAT, wax format -i input.wax to reformat in place and wax check input.wax to type-check only. Add --faithful when decompiling if you need the result to recompile instruction for instruction. For editors, the VS Code extension needs nothing else installed; Neovim and Emacs get highlighting from a tree-sitter grammar and the rest from wax lsp. The visible text gives no licence, repository link or installation instructions.

How solid is it

This is a first-party post by the tool's own author, and it backs its claims with concrete checks: the add function compiles to the same seven bytes, a round trip of string.wat returns all instructions in the same order, a nightly fuzzing run covers faithful mode on machine-generated modules, and the default mode is tested by sending official WebAssembly test suite modules to Wax and back and re-running the suite's assertions. The 480-to-320 line figure is for one file (string.wat), not a general claim about all code. The visible text gives no performance numbers and no release date or version number.

Risks and caveats

The source text is truncated in the editor-support section, so the reasons the runtime is written by hand, which the author promises to explain at the end, are not visible. The visible text does not say whether the wasm_of_ocaml runtime has been or will be rewritten in Wax. The strongest guarantee, instruction-for-instruction recompilation, applies to faithful mode; the default mode tidies a few idioms, such as turning an i32.eq followed by i32.eqz into a single !=, and is checked on behaviour rather than instruction identity. Names and numbering may change in a round trip. All claims here come from the author.

“a Rust-like syntax for WebAssembly that compiles to exactly the same instructions”

— From the Tarides post introducing Wax, by a Principal Software Engineer