Skip to main content

Module compiler

Module compiler 

Source
Expand description

The bytecode compiler.

Compilation is a single walk over the AST that emits symbolic instructions (jump targets are label ids, variable references are names), followed by a resolution pass that turns them into final bytecode. Splitting it this way keeps the emitter readable: nothing has to know slot indices or byte offsets while it is still discovering them.

Variable resolution follows the reference implementation’s model:

  • every function has a flat table of arguments and locals;
  • block scopes form a chain through VarDef::scope_next, which is what makes let/const bindings visible only inside their block and lets the runtime put them back into the temporal dead zone on entry;
  • a name that is not in the current function is looked up in the enclosing ones; if found, the variable is marked captured and reached through a closure reference;
  • a name found nowhere is a global, reached through a closure entry as well.

Emission order and instruction choice deliberately mirror quickjs.c: the differential test compares disassembly with the reference opcode by opcode, so an unnecessary dup or a differently chosen short form shows up immediately.

Structs§

Compiler
The compiler’s entry point.
Label
A label id, local to the function being compiled.

Functions§

compile_eval_with_private
Compile a direct eval body that inherits private names from its caller.
compile_module
Compile a module.
compile_module_with_source
compile_script_with_source for a module.
compile_script
Compile a script.
compile_script_suspensible
Compile a script whose top level is the body of an async function. The body is an ordinary script in every other way — it still reports a completion value — but the value is wrapped in {value} and the function’s kind is Async, so running it answers a promise instead of a value. The wrapper is what keeps that promise from being adopted: await 1 completes with 1, and the reference resolves to {value: 1} rather than to 1 — the same shape std.evalScript(…, {async: true}) documents.
compile_script_with_source
Compile a script, keeping its text so Function.prototype.toString can answer with the original source.
disassemble
Disassemble a compiled function.