Adarsh S · AI Engineer at Neeroma Technologies · Chennai, India

clox

Systems · C · 2026 · Solo Builder

A bytecode virtual machine for a programming language, written in C from scratch, that runs in a browser playground with a time-travel debugger via WebAssembly.

How it works

  1. Source code: Closures, classes and inheritance
  2. Bytecode: What the stack-based VM runs
  3. Stack-based VM: NaN boxing and other optimizations
  4. Mark-sweep GC: Stress-tested on every allocation
  5. WebAssembly: Browser playground with a time-travel debugger

Programs, end to end

Real programs, traced through clox as you scroll. The REPL session and the demo programs are the ones recorded in the repo, and the bytecode and the stack are what clox’s own trace prints.

The REPL: > var greeting = "Hello"; > print greeting + ", Lox!"; → Hello, Lox! > fun square(x) { return x * x; } > print square(12); → 144

Compiled (fun square(n) { return n * n; } print square(3) + 1;): <script>: OP_CLOSURE 1 <fn square>, OP_DEFINE_GLOBAL 0 'square', OP_GET_GLOBAL 0 'square', OP_CONSTANT 2 '3', OP_CALL 1, OP_CONSTANT 3 '1', OP_ADD, OP_PRINT, OP_NIL, OP_RETURN. square: OP_GET_LOCAL 1, OP_GET_LOCAL 1, OP_MULTIPLY, OP_RETURN, OP_NIL, OP_RETURN. n is local slot 1 (slot 0 holds the function being called), and OP_CALL 1 calls whatever sits one slot below the single argument.

Run, with the stack after each instruction: OP_CLOSURE 1 <fn square> [<script>, <fn square>]; OP_DEFINE_GLOBAL 0 'square' [<script>]; OP_GET_GLOBAL 0 'square' [<script>, <fn square>]; OP_CONSTANT 2 '3' [<script>, <fn square>, 3]; OP_CALL 1 [<script>, <fn square>, 3]; OP_GET_LOCAL 1 [<script>, <fn square>, 3, 3]; OP_GET_LOCAL 1 [<script>, <fn square>, 3, 3, 3]; OP_MULTIPLY [<script>, <fn square>, 3, 9]; OP_RETURN [<script>, 9]; OP_CONSTANT 3 '1' [<script>, 9, 1]; OP_ADD [<script>, 10]; OP_PRINT [<script>] prints 10; OP_NIL [<script>, nil]; OP_RETURN [].

demo/zoo.lox prints: Rex makes a sound: woof! / 1 / 2.

./build/release/clox demo/oops.lox: Operands must be numbers. [line 2] in area() [line 6] in describe() [line 9] in script Exit code 70.

Results

Five of the book’s benchmarks, run in clox and in line-for-line Python ports on the same Apple M3 Pro: clox built with Apple clang 21 at -O2, against Python 3.14.5 with profile-guided optimization and the JIT off. Best of 5 runs.

  • Faster than Python on method calls: 1.16x (method_call: 0.127 s vs 0.147 s)
  • Faster than Python creating objects: 1.14x (instantiation: 0.627 s vs 0.715 s)
  • Slower than Python on recursive calls: 1.17x (fib: 0.766 s vs 0.656 s)
  • Startup, against 18 ms for Python: 2 ms (On the programs’ own clocks, clox leads by 1.07x to 1.09x)

Python wins three of the five: fib, equality (1.05x) and string_equality (1.29x). On equality CPython drops statements that are only a constant; in the loop that really compares values, clox is faster, 1.40 s against 2.01 s. Python wins fib by specializing its bytecode as it runs, while clox hashes the name fib on every call.

Full table, method and how to rerun it

Highlights

  • A full language with closures, classes and inheritance, run by a stack-based VM.
  • Mark-sweep garbage collector, tested with a stress mode that runs it on every allocation.
  • NaN boxing and other optimizations make it about as fast as Python 3.14.
  • Compiled to WebAssembly with Emscripten for a browser playground with a time-travel debugger.
  • Passes 246/246 official tests, with ASan/UBSan and CI on Linux and macOS.

Stack

C, WebAssembly, Emscripten, GitHub Actions

All projects