pub struct Chunk {
pub code: Vec<u8>,
pub constants: Vec<Constant>,
pub lines: Vec<u32>,
pub columns: Vec<u32>,
pub functions: Vec<CompiledFunction>,
/* private fields */
}Expand description
A compiled chunk of bytecode.
Fields§
§code: Vec<u8>The bytecode instructions.
constants: Vec<Constant>Constant pool.
lines: Vec<u32>Source line numbers for each instruction (for error reporting).
columns: Vec<u32>Source column numbers for each instruction (for error reporting).
Parallel to lines; 0 means no column info available.
functions: Vec<CompiledFunction>Compiled function bodies (for closures).
Implementations§
Source§impl Chunk
impl Chunk
pub fn new() -> Self
Sourcepub fn set_column(&mut self, col: u32)
pub fn set_column(&mut self, col: u32)
Set the current column for subsequent emit calls.
Sourcepub fn add_constant(&mut self, constant: Constant) -> u16
pub fn add_constant(&mut self, constant: Constant) -> u16
Add a constant and return its index.
Sourcepub fn emit_u16(&mut self, op: Op, arg: u16, line: u32)
pub fn emit_u16(&mut self, op: Op, arg: u16, line: u32)
Emit an instruction with a u16 argument.
Sourcepub fn emit_method_call(&mut self, name_idx: u16, arg_count: u8, line: u32)
pub fn emit_method_call(&mut self, name_idx: u16, arg_count: u8, line: u32)
Emit a method call: op + u16 (method name) + u8 (arg count).
Sourcepub fn emit_method_call_opt(&mut self, name_idx: u16, arg_count: u8, line: u32)
pub fn emit_method_call_opt(&mut self, name_idx: u16, arg_count: u8, line: u32)
Emit an optional method call (?.) — returns nil if receiver is nil.
Sourcepub fn current_offset(&self) -> usize
pub fn current_offset(&self) -> usize
Current code offset (for jump patching).
Sourcepub fn emit_jump(&mut self, op: Op, line: u32) -> usize
pub fn emit_jump(&mut self, op: Op, line: u32) -> usize
Emit a jump instruction with a placeholder offset. Returns the position to patch.
Sourcepub fn patch_jump(&mut self, patch_pos: usize)
pub fn patch_jump(&mut self, patch_pos: usize)
Patch a jump instruction at the given position to jump to the current offset.
Sourcepub fn patch_jump_to(&mut self, patch_pos: usize, target: usize)
pub fn patch_jump_to(&mut self, patch_pos: usize, target: usize)
Patch a jump to a specific target position.
Sourcepub fn disassemble(&self, name: &str) -> String
pub fn disassemble(&self, name: &str) -> String
Disassemble for debugging.