mirror of
https://github.com/pjh456/OxideJS.git
synced 2026-10-23 18:21:13 +08:00
fix: 形参默认值扫描包深度增减,恢复父级绑定捕获判定
This commit is contained in:
@@ -568,7 +568,10 @@ pub(crate) fn collect_capture_names_simple_target(
|
||||
}
|
||||
|
||||
/// 收集函数参数默认值表达式里的引用(子层 upvalue 判定;参数名遮蔽)。
|
||||
/// 默认值在定义处外层作用域求值,其中对外层 own 绑定的引用须纳入捕获判定:
|
||||
/// 扫描前临时深度加一过门槛,扫完即减回;upvalue 判定无深度门槛,不受影响。
|
||||
pub(crate) fn collect_fn_default_names(params: &oxide_parser::FormalParameters, eng: &mut CaptureEngine) {
|
||||
eng.depth += 1;
|
||||
for p in ¶ms.items {
|
||||
if let Some(init) = &p.initializer {
|
||||
collect_capture_names_expr(init, eng);
|
||||
@@ -578,6 +581,7 @@ pub(crate) fn collect_fn_default_names(params: &oxide_parser::FormalParameters,
|
||||
collect_capture_names_binding_pattern(&p.pattern, eng);
|
||||
eng.shadow = saved;
|
||||
}
|
||||
eng.depth -= 1;
|
||||
}
|
||||
|
||||
/// 按表达式类型递归扫描引用;进入嵌套函数体时以形参 + 体内声明扩充遮蔽集。与
|
||||
|
||||
@@ -227,6 +227,20 @@ fn closure_captures_cell_and_upvalue() {
|
||||
);
|
||||
}
|
||||
|
||||
/// 面 13:直接子函数(深度 0)形参默认值对父级绑定的直接引用须纳入父捕获集
|
||||
/// (父建 cell、子建 upvalue),否则逃逸闭包缺参调用经 LOAD_VAR 读父帧死寄存器取陈旧值。
|
||||
#[test]
|
||||
fn nested_fn_default_ref_captures_parent_binding() {
|
||||
let src = "function f() { var x = 1; var g = function(a = x) { return a; }; return g; }";
|
||||
let ir = emit_ir(src);
|
||||
let f = &ir.nested[0];
|
||||
assert!(f.cells_needed >= 1, "父函数应为默认值引用的 x 建 cell");
|
||||
assert!(count_op(&f.insts, OpCode::MAKE_CELL) >= 1, "父函数入口应发 MAKE_CELL");
|
||||
let g = &f.nested[0];
|
||||
let g_names: HashSet<&str> = g.upvalue_captures.iter().map(|c| c.name.as_str()).collect();
|
||||
assert!(g_names.contains("x"), "子函数形参默认值引用的 x 应成为子 upvalue");
|
||||
}
|
||||
|
||||
/// 面 5:对象 rest 展开与数组 holes 的现状形态:REST_OBJECT 的 ext 键是排除键
|
||||
/// (显式绑定的键名);解构赋值经 for-of 迭代面(FOR_OF_INIT)逐元素绑定,
|
||||
/// 空洞元素被消费但不绑定;常量池含 Undefined。
|
||||
|
||||
Reference in New Issue
Block a user