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_module
Compile a module.
compile_script
Compile a script.
compile_script_suspensible
Compile a script whose top level is the body of an async function.
disassemble
Disassemble a compiled function.