feat: 搭建闭包捕获基础设施

This commit is contained in:
2026-06-30 19:58:58 +08:00
parent 930b14b3f0
commit c58aae4862
17 changed files with 660 additions and 12 deletions
+15
View File
@@ -12,6 +12,13 @@ pub enum Constant {
Undefined,
}
#[derive(Debug, Clone)]
pub struct UpvalueCapture {
pub name: String,
pub enclosing_reg: u8,
pub cell_idx: u8,
}
pub struct CompiledModule {
pub bytecode: Vec<opcode::Instr>,
pub constants: Vec<Constant>,
@@ -37,6 +44,8 @@ pub struct CompiledModule {
pub is_derived_constructor: bool,
/// True for prototype methods whose function object needs a runtime home_object.
pub needs_home_object: bool,
pub upvalue_captures: Vec<UpvalueCapture>,
pub cells_needed: u8,
}
impl CompiledModule {
@@ -55,6 +64,8 @@ impl CompiledModule {
is_class_constructor: false,
is_derived_constructor: false,
needs_home_object: false,
upvalue_captures: Vec::new(),
cells_needed: 0,
}
}
}
@@ -81,6 +92,8 @@ impl Clone for CompiledModule {
is_class_constructor: self.is_class_constructor,
is_derived_constructor: self.is_derived_constructor,
needs_home_object: self.needs_home_object,
upvalue_captures: self.upvalue_captures.clone(),
cells_needed: self.cells_needed,
}
}
}
@@ -93,6 +106,8 @@ impl fmt::Display for CompiledModule {
writeln!(f, "; [{i}] = {c:?}")?;
}
writeln!(f)?;
writeln!(f, "; upvalue_captures: {:?}", self.upvalue_captures)?;
writeln!(f)?;
for (offset, &instr) in self.bytecode.iter().enumerate() {
let op = opcode::opcode(instr);
let rd = opcode::rd(instr);
+5
View File
@@ -109,7 +109,10 @@ define_opcodes! {
DELETE_PROP_STATIC = 0x39 => "DELETE_PROP_STATIC",
DELETE_PROP_DYNAMIC = 0x3A => "DELETE_PROP_DYNAMIC",
INSTANCEOF = 0x3B => "INSTANCEOF",
MAKE_CELL = 0x3C => "MAKE_CELL",
CELL_GET = 0x3D => "CELL_GET",
REST_OBJECT = 0x3E => "REST_OBJECT",
CELL_SET = 0x3F => "CELL_SET",
// -- Variable (0x30-0x32) --
LOAD_VAR = 0x30 => "LOAD_VAR",
@@ -126,8 +129,10 @@ define_opcodes! {
SUPER_STATIC_GET_PROP = 0x46 => "SUPER_STATIC_GET_PROP",
SET_HOME_OBJECT = 0x47 => "SET_HOME_OBJECT",
DEFINE_ACCESSOR = 0x48 => "DEFINE_ACCESSOR",
LOAD_UPVALUE = 0x49 => "LOAD_UPVALUE",
CREATE_CLOSURE = 0x4A => "CREATE_CLOSURE",
CREATE_REGEXP = 0x4B => "CREATE_REGEXP",
STORE_UPVALUE = 0x4C => "STORE_UPVALUE",
// ── Object Property (0x50-0x5F) ──
IC_GET_PROP = 0x50 => "IC_GET_PROP",