mirror of
https://github.com/pjh456/OxideJS.git
synced 2026-10-24 02:31:13 +08:00
refactor: 移除 lexical 预声明函数的死参数 global_lexical
This commit is contained in:
@@ -429,7 +429,7 @@ impl Emitter {
|
||||
// 预声明 body 级 `let`/`const`/`class`(未初始化 TDZ 占位),
|
||||
// 使声明点前读取可编译为运行时 ReferenceError。函数体 lexical 声明是
|
||||
// 局部绑定,不做受限全局名检查;重复声明错在 emit 期报。
|
||||
let _ = self.predeclare_lexical_declarations(body_stmts, &mut ctx, false);
|
||||
let _ = self.predeclare_lexical_declarations(body_stmts, &mut ctx);
|
||||
|
||||
// 块级函数名 web-compat 外层 var 绑定(sloppy):块内函数声明名在实例化
|
||||
// 期于函数作用域建外层 var 绑定——无同名绑定则新建 var 槽;名在抑制集
|
||||
|
||||
@@ -445,7 +445,7 @@ impl Emitter {
|
||||
self.pre_register_builtin_references(body, &lexical_excluded, ctx);
|
||||
self.predeclare_var_declarations(body, ctx);
|
||||
// 模块 lexical 声明入模块环境(非全局对象),不做受限全局名检查。
|
||||
let _ = self.predeclare_lexical_declarations(body, ctx, false);
|
||||
let _ = self.predeclare_lexical_declarations(body, ctx);
|
||||
|
||||
// 闭包捕获分析(import 绑定名纳入 own_bindings,供嵌套函数 cell 捕获)。
|
||||
ctx.own_bindings = collect_own_binding_names(&[], body);
|
||||
|
||||
@@ -292,11 +292,8 @@ impl Emitter {
|
||||
/// 属 Declaration、非 Statement 子产生式,parser 按语法错误直接拒绝,这些
|
||||
/// 形状不会进入 emit)。
|
||||
/// - 跳过 for 头声明(循环作用域由 for 分支内联 declare)。
|
||||
/// - `global_lexical` 仅脚本顶层调用点传 `!is_eval_script`,其余调用点传
|
||||
/// false;当前只透传不消费(死参数,待后续清理),顶层与局部的 lexical
|
||||
/// 声明均按合法遮蔽处理。
|
||||
pub(crate) fn predeclare_lexical_declarations(
|
||||
&self, statements: &[Statement], ctx: &mut CompileCtx, global_lexical: bool,
|
||||
&self, statements: &[Statement], ctx: &mut CompileCtx,
|
||||
) -> Result<(), String> {
|
||||
for statement in statements {
|
||||
match statement {
|
||||
@@ -306,7 +303,7 @@ impl Emitter {
|
||||
}
|
||||
let is_const = matches!(decl.kind, VariableDeclarationKind::Const);
|
||||
for d in &decl.declarations {
|
||||
self.predeclare_lexical_pattern(&d.id, is_const, ctx, global_lexical)?;
|
||||
self.predeclare_lexical_pattern(&d.id, is_const, ctx)?;
|
||||
}
|
||||
}
|
||||
Statement::ClassDeclaration(cd) => {
|
||||
@@ -324,7 +321,7 @@ impl Emitter {
|
||||
}
|
||||
let is_const = matches!(vd.kind, VariableDeclarationKind::Const);
|
||||
for d in &vd.declarations {
|
||||
self.predeclare_lexical_pattern(&d.id, is_const, ctx, global_lexical)?;
|
||||
self.predeclare_lexical_pattern(&d.id, is_const, ctx)?;
|
||||
}
|
||||
}
|
||||
Declaration::ClassDeclaration(cd) => {
|
||||
@@ -359,11 +356,8 @@ impl Emitter {
|
||||
}
|
||||
|
||||
/// 递归预声明绑定 pattern 内的全部标识符(含数组/对象/默认值解构)。
|
||||
/// `global_lexical` 当前只透传不消费(死参数,待后续清理),递归调用
|
||||
/// 必须原样下传,故仅递归使用。
|
||||
#[allow(clippy::only_used_in_recursion)]
|
||||
fn predeclare_lexical_pattern(
|
||||
&self, pattern: &BindingPattern, is_const: bool, ctx: &mut CompileCtx, global_lexical: bool,
|
||||
&self, pattern: &BindingPattern, is_const: bool, ctx: &mut CompileCtx,
|
||||
) -> Result<(), String> {
|
||||
match pattern {
|
||||
BindingPattern::BindingIdentifier(bi) => {
|
||||
@@ -381,22 +375,22 @@ impl Emitter {
|
||||
}
|
||||
BindingPattern::ArrayPattern(ap) => {
|
||||
for e in ap.elements.iter().flatten() {
|
||||
self.predeclare_lexical_pattern(e, is_const, ctx, global_lexical)?;
|
||||
self.predeclare_lexical_pattern(e, is_const, ctx)?;
|
||||
}
|
||||
if let Some(rest) = &ap.rest {
|
||||
self.predeclare_lexical_pattern(&rest.argument, is_const, ctx, global_lexical)?;
|
||||
self.predeclare_lexical_pattern(&rest.argument, is_const, ctx)?;
|
||||
}
|
||||
}
|
||||
BindingPattern::ObjectPattern(op) => {
|
||||
for prop in &op.properties {
|
||||
self.predeclare_lexical_pattern(&prop.value, is_const, ctx, global_lexical)?;
|
||||
self.predeclare_lexical_pattern(&prop.value, is_const, ctx)?;
|
||||
}
|
||||
if let Some(rest) = &op.rest {
|
||||
self.predeclare_lexical_pattern(&rest.argument, is_const, ctx, global_lexical)?;
|
||||
self.predeclare_lexical_pattern(&rest.argument, is_const, ctx)?;
|
||||
}
|
||||
}
|
||||
BindingPattern::AssignmentPattern(ap) => {
|
||||
self.predeclare_lexical_pattern(&ap.left, is_const, ctx, global_lexical)?;
|
||||
self.predeclare_lexical_pattern(&ap.left, is_const, ctx)?;
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
|
||||
@@ -309,12 +309,8 @@ impl Emitter {
|
||||
// 预声明顶层 `var` 名,使首个 sub-pass 中提升的函数声明能解析外层 var。
|
||||
self.predeclare_var_declarations(&program.body, &mut ctx);
|
||||
|
||||
// 预声明顶层 `let`/`const`/`class`(未初始化 TDZ 占位)。受限全局名检查
|
||||
// 仅对脚本代码启用:脚本声明实例化查全局对象受限自有属性名,eval 代码
|
||||
// 声明实例化不查——门控随 is_eval_script 而非作用域标志(嵌套块内
|
||||
// is_global_scope 仍为 true,不能作门控)。
|
||||
let global_lexical = !ctx.is_eval_script;
|
||||
self.predeclare_lexical_declarations(&program.body, &mut ctx, global_lexical)?;
|
||||
// 预声明顶层 `let`/`const`/`class`(未初始化 TDZ 占位)。
|
||||
self.predeclare_lexical_declarations(&program.body, &mut ctx)?;
|
||||
|
||||
// 顶层块级函数名按 sloppy 模式下与浏览器/web 实现惯例兼容的行为处理
|
||||
// (块级函数声明建外层 var 绑定并求值写回;sloppy eval 代码同走
|
||||
|
||||
@@ -15,7 +15,7 @@ impl Emitter {
|
||||
// 命中已存在的函数绑定自然报重复声明错,不被静默覆盖破坏 TDZ。
|
||||
self.predeclare_block_function_declarations(&block.body, ctx, false);
|
||||
// 块 lexical 声明是局部绑定:不做受限全局名检查(重复声明错在 emit 期报)。
|
||||
let _ = self.predeclare_lexical_declarations(&block.body, ctx, false);
|
||||
let _ = self.predeclare_lexical_declarations(&block.body, ctx);
|
||||
// 块级函数声明的块入口初始化:直接子(含标签直接子体)闭包物化入预
|
||||
// 声明块槽,声明语句前读命中函数对象(sloppy 与 strict 同形);声明
|
||||
// 点不再重编闭包,只处理外层 var 写回(见 emit_function_declaration)。
|
||||
|
||||
@@ -37,7 +37,7 @@ impl Emitter {
|
||||
// 函数预声明先于 lexical:同名 let 命中已有函数绑定报重复声明错。
|
||||
self.predeclare_block_function_declarations(&ts.block.body, ctx, false);
|
||||
// try 块 lexical 声明是局部绑定:不做受限全局名检查(重复声明错在 emit 期报)。
|
||||
let _ = self.predeclare_lexical_declarations(&ts.block.body, ctx, false);
|
||||
let _ = self.predeclare_lexical_declarations(&ts.block.body, ctx);
|
||||
// 块级函数声明的块入口初始化:声明点前读命中函数对象,声明点复用入口槽。
|
||||
ctx.block_fn_entry_mats.push(HashMap::new());
|
||||
for s in &ts.block.body {
|
||||
@@ -85,7 +85,7 @@ impl Emitter {
|
||||
// catch 块函数预声明先于 lexical,块入口物化与 try/普通块同口径。
|
||||
self.predeclare_block_function_declarations(&catch.body.body, ctx, false);
|
||||
// catch 块 lexical 声明同为局部绑定:不做受限全局名检查。
|
||||
let _ = self.predeclare_lexical_declarations(&catch.body.body, ctx, false);
|
||||
let _ = self.predeclare_lexical_declarations(&catch.body.body, ctx);
|
||||
ctx.block_fn_entry_mats.push(HashMap::new());
|
||||
for s in &catch.body.body {
|
||||
self.emit_block_fn_entry_init_stmt(s, ctx)?;
|
||||
@@ -123,7 +123,7 @@ impl Emitter {
|
||||
// finally 块函数预声明先于 lexical,块入口物化与 try/普通块同口径。
|
||||
self.predeclare_block_function_declarations(&ts.finalizer.as_ref().unwrap().body, ctx, false);
|
||||
// finally 块 lexical 声明同为局部绑定:不做受限全局名检查。
|
||||
let _ = self.predeclare_lexical_declarations(&ts.finalizer.as_ref().unwrap().body, ctx, false);
|
||||
let _ = self.predeclare_lexical_declarations(&ts.finalizer.as_ref().unwrap().body, ctx);
|
||||
ctx.block_fn_entry_mats.push(HashMap::new());
|
||||
for s in &ts.finalizer.as_ref().unwrap().body {
|
||||
self.emit_block_fn_entry_init_stmt(s, ctx)?;
|
||||
|
||||
@@ -34,7 +34,7 @@ impl Emitter {
|
||||
ctx.push_scope();
|
||||
for case in cases.iter() {
|
||||
self.predeclare_block_function_declarations(&case.consequent, ctx, false);
|
||||
self.predeclare_lexical_declarations(&case.consequent, ctx, false)?;
|
||||
self.predeclare_lexical_declarations(&case.consequent, ctx)?;
|
||||
}
|
||||
// case 内函数声明的块入口初始化:同名重复声明按源序物化,末次声明成为
|
||||
// 入口值;声明点复用块槽写回外层 var(见 emit_function_declaration)。
|
||||
|
||||
Reference in New Issue
Block a user