feat: 解构模式语义补齐(REST 枚举/getter/computed 排除、IteratorClose、RegAlloc def 干涉、for-in 捕获遮蔽)

This commit is contained in:
爱特每羊队
2026-08-09 15:01:31 +08:00
parent 0e620ff963
commit 8f8c9a39ff
12 changed files with 294 additions and 61 deletions
+45 -10
View File
@@ -22,6 +22,17 @@ impl Emitter {
names
}
/// 收集 for-in/for-of 头部声明(`var x` / 解构 pattern)绑定的名字。
fn collect_for_left_decl_names(&self, left: &oxide_parser::ForStatementLeft, out: &mut HashSet<String>) {
if let oxide_parser::ForStatementLeft::VariableDeclaration(vd) = left {
for d in &vd.declarations {
if let oxide_parser::BindingPattern::BindingIdentifier(bi) = &d.id {
out.insert(bi.name.to_string());
}
}
}
}
/// 收集当前函数作用域声明的绑定名(参数 + 变量/函数声明,含嵌套 block,不含嵌套函数体)。
pub(crate) fn collect_own_binding_names(&self, param_names: &[&str], stmts: &[Statement]) -> HashSet<String> {
let mut names = HashSet::new();
@@ -66,6 +77,28 @@ impl Emitter {
}
self.collect_decl_names_stmt(std::slice::from_ref(&f.body), out);
}
// for-in / for-of 头部的 var 声明是本函数局部绑定(遮蔽外层同名),
// 须计入 own_bindings,否则闭包捕获分析会误判为捕获外层变量。
Statement::ForInStatement(fi) => {
if let oxide_parser::ForStatementLeft::VariableDeclaration(vd) = &fi.left {
for d in &vd.declarations {
if let oxide_parser::BindingPattern::BindingIdentifier(bi) = &d.id {
out.insert(bi.name.to_string());
}
}
}
self.collect_decl_names_stmt(std::slice::from_ref(&fi.body), out);
}
Statement::ForOfStatement(fo) => {
if let oxide_parser::ForStatementLeft::VariableDeclaration(vd) = &fo.left {
for d in &vd.declarations {
if let oxide_parser::BindingPattern::BindingIdentifier(bi) = &d.id {
out.insert(bi.name.to_string());
}
}
}
self.collect_decl_names_stmt(std::slice::from_ref(&fo.body), out);
}
Statement::SwitchStatement(sw) => {
for case in &sw.cases {
self.collect_decl_names_stmt(&case.consequent, out);
@@ -87,13 +120,6 @@ impl Emitter {
}
}
/// 扫描 stmts 内(含任意深度嵌套函数)对 `ref_set` 的引用,写入 out。
/// 递归进入嵌套函数时累加其局部绑定为遮蔽集,避免把内层局部误判为捕获。
fn collect_capture_names(&self, stmts: &[Statement], ref_set: &HashSet<String>, out: &mut HashSet<String>) {
let shadow = HashSet::new();
self.collect_capture_names_shadowed(stmts, ref_set, &shadow, out);
}
fn collect_capture_names_shadowed(
&self, stmts: &[Statement], ref_set: &HashSet<String>, shadow: &HashSet<String>, out: &mut HashSet<String>,
) {
@@ -174,11 +200,16 @@ impl Emitter {
}
Statement::ForInStatement(fi) => {
self.collect_capture_names_expr(&fi.right, ref_set, shadow, out);
self.collect_capture_names_stmt(&fi.body, ref_set, shadow, out);
// left 的 var 声明遮蔽外层同名绑定,body 内引用不视为捕获外层。
let mut for_shadow = shadow.clone();
self.collect_for_left_decl_names(&fi.left, &mut for_shadow);
self.collect_capture_names_shadowed(std::slice::from_ref(&fi.body), ref_set, &for_shadow, out);
}
Statement::ForOfStatement(fo) => {
self.collect_capture_names_expr(&fo.right, ref_set, shadow, out);
self.collect_capture_names_stmt(&fo.body, ref_set, shadow, out);
let mut for_shadow = shadow.clone();
self.collect_for_left_decl_names(&fo.left, &mut for_shadow);
self.collect_capture_names_shadowed(std::slice::from_ref(&fo.body), ref_set, &for_shadow, out);
}
Statement::BlockStatement(b) => self.collect_capture_names_shadowed(&b.body, ref_set, shadow, out),
Statement::TryStatement(ts) => {
@@ -501,7 +532,11 @@ impl Emitter {
Statement::FunctionDeclaration(fd) => {
let body: &[Statement] = fd.body.as_ref().map(|b| &b.statements[..]).unwrap_or(&[]);
self.collect_fn_default_captured(&fd.params, own, out);
self.collect_capture_names(body, own, out);
// 函数参数与体内部声明遮蔽父级绑定:体内对这些名字的引用不算捕获父级。
let mut fn_shadow = HashSet::new();
fn_shadow.extend(self.collect_fn_param_names(&fd.params));
fn_shadow.extend(self.collect_own_binding_names(&[], body));
self.collect_capture_names_shadowed(body, own, &fn_shadow, out);
}
Statement::ClassDeclaration(cd) => {
// 类构造器/方法体与字段表达式引用的父级绑定须建 cell,供子模块 upvalue 捕获。
+86 -8
View File
@@ -190,23 +190,74 @@ impl Emitter {
fn emit_object_binding(
&self, op: &ObjectPattern, src_reg: u32, kind: VariableDeclarationKind, is_const: bool, ctx: &mut CompileCtx,
) -> Result<(), String> {
// 对象解构先 ToObject(rhs):null/undefined 抛 TypeError(含空 pattern),
// 原始值包装为对应对象,保证属性读取与 rest 都以对象为源。
// 写到临时寄存器,保留 src_reg 原值(声明初始化路径的返回值语义)。
let obj_reg = ctx.alloc_reg();
ctx.inst(Inst::new(OpCode::LOAD_VAR, Operand::Reg(obj_reg), Operand::Reg(src_reg), Operand::None));
ctx.inst(Inst::new(OpCode::TO_OBJECT, Operand::Reg(obj_reg), Operand::None, Operand::None));
// 有 rest 时构建运行时 excluded 数组:静态 key 直接 push 常量,computed key 求值结果 push 值。
let mut excluded = Vec::new();
let mut excl_arr_reg = None;
let mut excl_push = 0usize;
if op.rest.is_some() {
excl_arr_reg = Some(self.emit_new_excluded_array(ctx));
}
for prop in &op.properties {
let (prop_reg, static_key) = self.emit_object_property_read_key(src_reg, &prop.key, prop.computed, ctx)?;
let (prop_reg, static_key, key_reg) =
self.emit_object_property_read_key(obj_reg, &prop.key, prop.computed, ctx)?;
if let Some(key) = static_key {
excluded.push(key);
excluded.push(key.clone());
if let Some(arr) = excl_arr_reg {
self.emit_push_excluded_key(arr, None, &key, excl_push, ctx);
excl_push += 1;
}
} else if let (Some(arr), Some(key_reg)) = (excl_arr_reg, key_reg) {
self.emit_push_excluded_key(arr, Some(key_reg), "", excl_push, ctx);
excl_push += 1;
}
self.emit_binding_pattern(&prop.value, prop_reg, kind, is_const, ctx)?;
}
if let Some(rest) = &op.rest {
let rest_reg = ctx.alloc_reg();
let excluded_idx = ctx.add_constant(Constant::String(excluded.join("\0")));
ctx.inst(Inst::rest_object(Operand::Reg(rest_reg), Operand::Reg(src_reg), excluded_idx as u32));
ctx.inst(Inst::rest_object(
Operand::Reg(rest_reg),
Operand::Reg(obj_reg),
excluded_idx as u32,
excl_arr_reg.map(Operand::Reg),
));
self.emit_binding_pattern(&rest.argument, rest_reg, kind, is_const, ctx)?;
}
Ok(())
}
/// 新建运行时 excluded 数组(NEW_ARRAY),供 REST_OBJECT 排除 computed key。
fn emit_new_excluded_array(&self, ctx: &mut CompileCtx) -> u32 {
let arr = ctx.alloc_reg();
ctx.inst(Inst::new(OpCode::NEW_ARRAY, Operand::Reg(arr), Operand::None, Operand::None));
arr
}
/// 向 excluded 数组下标 `idx` 写入一个键:`key_val` 为运行时寄存器值,否则用 `key_str` 常量。
fn emit_push_excluded_key(
&self, arr: u32, key_val: Option<u32>, key_str: &str, idx: usize, ctx: &mut CompileCtx,
) {
let val_reg = match key_val {
Some(kv) => kv,
None => {
let ki = ctx.add_constant(Constant::String(key_str.to_string()));
let kr = ctx.alloc_reg();
ctx.inst(Inst::load_const(Operand::Reg(kr), ki));
kr
}
};
let elem_idx = ctx.alloc_reg();
let cidx = ctx.add_constant(Constant::Int(idx as i32));
ctx.inst(Inst::load_const(Operand::Reg(elem_idx), cidx));
ctx.inst(Inst::new(OpCode::SET_ELEM, Operand::Reg(arr), Operand::Reg(elem_idx), Operand::Reg(val_reg)));
}
fn emit_assignment_maybe_default(
&self, target: &AssignmentTargetMaybeDefault, src_reg: u32, ctx: &mut CompileCtx,
) -> Result<(), String> {
@@ -260,13 +311,28 @@ impl Emitter {
pub(crate) fn emit_object_assignment(
&self, op: &ObjectAssignmentTarget, src_reg: u32, ctx: &mut CompileCtx,
) -> Result<(), String> {
// 对象解构先 ToObject(rhs):null/undefined 抛 TypeError(含空 pattern),
// 原始值包装为对应对象,保证属性读取与 rest 都以对象为源。
// 写到临时寄存器,保留 src_reg 原值(解构赋值表达式返回值 = rhs)。
let obj_reg = ctx.alloc_reg();
ctx.inst(Inst::new(OpCode::LOAD_VAR, Operand::Reg(obj_reg), Operand::Reg(src_reg), Operand::None));
ctx.inst(Inst::new(OpCode::TO_OBJECT, Operand::Reg(obj_reg), Operand::None, Operand::None));
let mut excluded = Vec::new();
let mut excl_arr_reg = None;
let mut excl_push = 0usize;
if op.rest.is_some() {
excl_arr_reg = Some(self.emit_new_excluded_array(ctx));
}
for prop in &op.properties {
match prop {
AssignmentTargetProperty::AssignmentTargetPropertyIdentifier(id) => {
let key = id.binding.name.as_str().to_string();
excluded.push(key.clone());
let mut prop_reg = self.emit_object_property_read(src_reg, &key, ctx);
if let Some(arr) = excl_arr_reg {
self.emit_push_excluded_key(arr, None, &key, excl_push, ctx);
excl_push += 1;
}
let mut prop_reg = self.emit_object_property_read(obj_reg, &key, ctx);
if let Some(default_expr) = &id.init {
let name = id.binding.name.as_str();
prop_reg = self.emit_default_if_undefined(prop_reg, default_expr, Some(name), ctx)?;
@@ -281,10 +347,17 @@ impl Emitter {
));
}
AssignmentTargetProperty::AssignmentTargetPropertyProperty(prop) => {
let (prop_reg, static_key) =
self.emit_object_property_read_key(src_reg, &prop.name, prop.computed, ctx)?;
let (prop_reg, static_key, key_reg) =
self.emit_object_property_read_key(obj_reg, &prop.name, prop.computed, ctx)?;
if let Some(key) = static_key {
excluded.push(key);
excluded.push(key.clone());
if let Some(arr) = excl_arr_reg {
self.emit_push_excluded_key(arr, None, &key, excl_push, ctx);
excl_push += 1;
}
} else if let (Some(arr), Some(key_reg)) = (excl_arr_reg, key_reg) {
self.emit_push_excluded_key(arr, Some(key_reg), "", excl_push, ctx);
excl_push += 1;
}
self.emit_assignment_maybe_default(&prop.binding, prop_reg, ctx)?;
}
@@ -293,7 +366,12 @@ impl Emitter {
if let Some(rest) = &op.rest {
let rest_reg = ctx.alloc_reg();
let excluded_idx = ctx.add_constant(Constant::String(excluded.join("\0")));
ctx.inst(Inst::rest_object(Operand::Reg(rest_reg), Operand::Reg(src_reg), excluded_idx as u32));
ctx.inst(Inst::rest_object(
Operand::Reg(rest_reg),
Operand::Reg(obj_reg),
excluded_idx as u32,
excl_arr_reg.map(Operand::Reg),
));
self.emit_assign_target(&rest.target, rest_reg, ctx)?;
}
Ok(())
+3 -3
View File
@@ -281,11 +281,11 @@ impl Emitter {
pub(crate) fn emit_object_property_read_key(
&self, src_reg: u32, key: &PropertyKey, computed: bool, ctx: &mut CompileCtx,
) -> Result<(u32, Option<String>), String> {
) -> Result<(u32, Option<String>, Option<u32>), String> {
if !computed {
let key_name = self.static_property_name(key)?;
let prop_reg = self.emit_object_property_read(src_reg, &key_name, ctx);
return Ok((prop_reg, Some(key_name)));
return Ok((prop_reg, Some(key_name), None));
}
let prop_reg = ctx.alloc_reg();
ctx.inst(Inst::new(OpCode::LOAD_VAR, Operand::Reg(prop_reg), Operand::Reg(src_reg), Operand::None));
@@ -297,6 +297,6 @@ impl Emitter {
Operand::Reg(key_reg),
Operand::Reg(val_reg),
));
Ok((val_reg, None))
Ok((val_reg, None, Some(key_reg)))
}
}