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 makeslet/constbindings 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§
Functions§
- compile_
eval_ with_ private - Compile a direct
evalbody that inherits private names from its caller. - compile_
module - Compile a module.
- compile_
module_ with_ source compile_script_with_sourcefor a module.- compile_
script - Compile a script.
- compile_
script_ suspensible - Compile a script whose top level is the body of an
asyncfunction. 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 isAsync, so running it answers a promise instead of a value. The wrapper is what keeps that promise from being adopted:await 1completes with1, and the reference resolves to{value: 1}rather than to1— the same shapestd.evalScript(…, {async: true})documents. - compile_
script_ with_ source - Compile a script, keeping its text so
Function.prototype.toStringcan answer with the original source. - disassemble
- Disassemble a compiled function.