quickjs-compile 0.17.32

Bytecode compiler for the pure-Rust QuickJS port
Documentation

quickjs-compile

The AST-to-bytecode compiler for quickjs-pure.

It turns the tree quickjs-parse produces into the stack bytecode the VM runs, using the same instruction set and the same opcode selection as QuickJS. "Same" is meant literally: the disassembly is compared instruction for instruction against the reference's qjsc -d, so a construct that produces different code counts as a bug even when its observable behaviour matches.

[dependencies]
quickjs-compile = "0.17.32"

The package is quickjs-compile but the library is js_compile:

use js_compile::{compile_script, disassemble};
use js_parse::parse_script;

let program = parse_script("var x = 1; x + 2;")?;
let function = compile_script(&program)?;
print!("{}", disassemble(&function));
# Ok::<(), Box<dyn std::error::Error>>(())

This example lives in examples/compile.rs and is run by the test suite.

What is here

Item Role
compile_script / compile_module Program to a CompiledFunction
disassemble the readable listing the differential tests compare
bytecode::Function atoms, constant pool, variables, resolved code, stack size
opcode::Opcode the instruction set, with per-opcode size and stack effect
serialize a stable on-disk form, for the AOT milestone

How it gets the code right

Compiling in two passes is what makes the output match the reference:

  1. Symbolic pass. Instructions are emitted with labels and names, not slots. That is the only way to compile a reference to a binding declared later, or to decide that a let in a nested block shadows an outer one — the binding that wins is the one whose scope encloses the access.
  2. Resolve pass. Names become slots, block scope chains are built, and then a peephole runs: dup+put becomes set, a get right after its put folds away, and unreachable code disappears. Each pattern is annotated with the bytecode it removes, because the whole point is to be one instruction shorter than a literal translation.

Scope handling is the part that has to be exactly right, and its failure mode is silent: a let that leaks out of its block produces a program that keeps running with the wrong answer. Sentinel block keys are therefore values no source offset can produce, and a lookup for a function-scoped var is kept strictly separate from the scope-chain walk that finds lexical bindings.

Measured

measured value
opcodes in the instruction set counted from src/opcode.rs 251
test262 test/language (what the compiler emits) runnable 23228/23499 (98.8%)
test262 test/built-ins runnable 18351/18351 (100.0%)
bytecode vs the reference, instruction for instruction needs the oracle built cargo test -p quickjs-compile

Status

The compiler covers the language the VM executes — classes with private fields and static blocks, generators, async functions, destructuring, for await, modules — and still reports … is not supported by the compiler yet rather than emitting wrong code for anything it does not, so an unimplemented construct is a loud error and never a wrong answer.

Pre-1.0, and pub items in this crate have changed in 0.x minors: the bytecode type and the opcode table are not frozen, because a new syntax form sometimes needs a new instruction. The stability promise is in docs/compatibility.md.