# live .wat-to-.wasm compiler & equivalence gate

- Status: `experiment`
- Track: [local compute, .wat compilation & on-device inference](https://aifoc.us/labs/local-compute/)
- Layer: 5. Local Compute, Wasm & Zero-Framework UI
- Updated: 1 December 2026
- Live demo: https://zero-js-wat-web.kinlan-test.workers.dev
- Source: https://github.com/PaulKinlan/zero-js-wat-web

## Inspired by

- [wat the heck: why ship javascript when nobody reads the code?](https://aifoc.us/wat-the-heck/)
- [what falling ai model latency means for the web](https://aifoc.us/latency/)

## Hypothesis

- **Claim:** Compiling compact WebAssembly Text (`.wat`) directly to `.wasm` at runtime gated by an automated `Wasm(x) === JS(x)` equivalence oracle makes unreviewed stack instructions safer to execute while eliminating external compiler toolchains.
- **Confirms it:** Editable and synthesized `.wat` modules compile live to valid binary `.wasm`, fail closed whenever the equivalence gate detects a mismatch against JS test vectors, and clearly expose where DOM host-import marshaling erases pure CPU compute wins.
- **Falsifies or bounds it:** Cold `.wat`-to-`.wasm` parsing + equivalence vector generation costs more wall-clock time or tokens than running direct JavaScript on DOM-heavy workloads.

> **Why this exists:** Generating compact WebAssembly Text (`.wat`) directly from prompts raises practical questions: token cost, trusting unreviewed stack instructions via automated checks, compile latency versus the JS JIT, and strict host-import DOM boundaries. This lab compiles `.wat` to `.wasm` live in the browser and enforces an automated `Wasm(x) === JS(x)` equivalence gate.


## What this lab tests

If an AI model emits compact [WebAssembly Text (`.wat`)](https://webassembly.github.io/spec/core/text/index.html) instead of JavaScript, can an automated equivalence gate verify outputs before anyone has to read stack instructions, and when does compiling `.wat` on the fly beat the browser's JS JIT?

[Live .wat-to-.wasm Compiler & Equivalence Gate](https://zero-js-wat-web.kinlan-test.workers.dev) ([GitHub](https://github.com/PaulKinlan/zero-js-wat-web)) compiles raw `.wat` S-expressions into binary `.wasm` on the fly in both the browser and a Cloudflare Worker (`POST /api/compile-wat`), synthesizes new modules directly from parameter specs (`sum-squares`, `xor-checksum`, `threshold-count`, `collatz-steps`), checks `Wasm(x) === JS(x)` across curated test vectors ([`/wasm/preset/primes.wat`](https://zero-js-wat-web.kinlan-test.workers.dev/wasm/preset/primes.wat), [`/wasm/preset/primes.wasm`](https://zero-js-wat-web.kinlan-test.workers.dev/wasm/preset/primes.wasm)), races Cold compile+run vs. Warm Wasm vs. Warm JS loops, and demonstrates why DOM updates require explicit host capability imports (`dom.paint_bar`).


## Open questions to iterate on

- Which numerical/parsing tasks cross the Cold compile+run breakeven cleanly inside a Cloudflare Worker and browser tab?
- Can automated equivalence testing synthesize property-based counterexamples rather than fixed static test vectors?

## Iteration log

- **2026-12-01:** Built zero-dependency `.wat` S-expression compiler (client + worker), Spec-to-WAT synthesizer, JS oracle gate, Cold/Warm race, and DOM host-import stage.
- **2026-12-01:** Reordered workbench above setup docs, enforced a fail-closed equivalence gate with one-click bug injection, added CSP-safe WAT/JS/JSON DOM syntax highlighting, hardened 700px embed responsiveness, and routed static assets through the Worker with `Cache-Control: no-cache`.

## Primitives tested

- `WebAssembly Text (.wat)`
- `Live S-Expression Compiler`
- `Equivalence Test Gate`
- `Host-Import DOM Bridge`
- `Declarative Shadow DOM`
- `Invoker Commands`

## Connected labs in other tracks

- [browser ephemeral vm](https://aifoc.us/labs/browser-containment/browser-ephemeral-vm/) — Adds fuel metering and OPFS memory rollback around Wasm execution.
- [local-first model latency & capability bench](https://aifoc.us/labs/local-compute/local-first-model-bench/) — Benchmarks Wasm compute against WebGPU and edge inference round-trips.

---
Canonical: https://aifoc.us/labs/local-compute/zero-js-wat-web/
