pub struct Chunk {
pub code: Vec<Instruction>,
pub constants: Vec<JsValue>,
pub names: Vec<String>,
pub locals: Vec<u32>,
pub functions: Vec<FunctionTemplate>,
}Expand description
A compiled chunk of bytecode with its constant pool.
Fields§
§code: Vec<Instruction>The bytecode instructions.
constants: Vec<JsValue>Constant pool — holds literal values.
names: Vec<String>Deduplicated name table for variable/property names.
Indexed by operand in GetVar/SetVar/etc. opcodes.
Separate from constants to allow zero-copy &str access in the VM.
locals: Vec<u32>Local slots table. Each entry stores an index into names.
Slot index is used by GetLocal/SetLocal/InitLocal opcodes.
functions: Vec<FunctionTemplate>Function templates for MakeClosure.
Implementations§
Source§impl Chunk
impl Chunk
pub fn new() -> Self
Sourcepub fn add_function(&mut self, template: FunctionTemplate) -> u32
pub fn add_function(&mut self, template: FunctionTemplate) -> u32
Add a function template and return its index.
Sourcepub fn emit(&mut self, instr: Instruction) -> usize
pub fn emit(&mut self, instr: Instruction) -> usize
Emit an instruction and return its index.
Sourcepub fn emit_with(&mut self, op: OpCode, operand: u32) -> usize
pub fn emit_with(&mut self, op: OpCode, operand: u32) -> usize
Emit an instruction with one operand.
Sourcepub fn add_constant(&mut self, value: JsValue) -> u32
pub fn add_constant(&mut self, value: JsValue) -> u32
Add a constant to the pool and return its index.
Sourcepub fn add_name(&mut self, s: &str) -> u32
pub fn add_name(&mut self, s: &str) -> u32
Add a name to the deduplicated name table and return its index.
Used for variable names, property names — allows zero-copy &str access in the VM.
Sourcepub fn add_local(&mut self, name_idx: u32) -> u32
pub fn add_local(&mut self, name_idx: u32) -> u32
Add a local slot for a name index and return its slot index.
Sourcepub fn get_local_name(&self, slot: u32) -> &str
pub fn get_local_name(&self, slot: u32) -> &str
Get a local slot’s name (zero-copy reference).
Sourcepub fn patch_jump(&mut self, jump_idx: usize)
pub fn patch_jump(&mut self, jump_idx: usize)
Patch a jump instruction’s operand to point to the current code position.
Sourcepub fn current_pos(&self) -> usize
pub fn current_pos(&self) -> usize
Get the current code position (for jump targets).
Sourcepub fn disassemble(&self, name: &str) -> String
pub fn disassemble(&self, name: &str) -> String
Disassemble the chunk for debugging.