mirror of
https://github.com/pjh456/OxideJS.git
synced 2026-10-27 04:01:11 +08:00
fix: 修正 if 支臂函数绑定门控的嵌套与内置名判据
This commit is contained in:
@@ -175,10 +175,11 @@ impl Emitter {
|
|||||||
/// - 只处理块内函数声明;switch 不推 scope(其 case 内函数声明随 switch
|
/// - 只处理块内函数声明;switch 不推 scope(其 case 内函数声明随 switch
|
||||||
/// 作用域修复一并处理);export 声明不可能出现在块内。
|
/// 作用域修复一并处理);export 声明不可能出现在块内。
|
||||||
/// - `if` 支臂的裸函数声明属 Annex B 隐式支臂块:名已有外层 var 绑定承载
|
/// - `if` 支臂的裸函数声明属 Annex B 隐式支臂块:名已有外层 var 绑定承载
|
||||||
/// (非 eval 且未被形参/词法同名抑制)时不建外层块绑定,声明点物化进该
|
/// (非 eval 脚本顶层、未被形参/词法同名抑制、且非脚本顶层只读三常量)时
|
||||||
/// var 槽(建外层块 Let 会遮蔽同名 var);名无外层 var 绑定(形参/词法抑制
|
/// 不建外层块绑定,声明点物化进该 var 槽(建外层块 Let 会遮蔽同名 var);
|
||||||
/// 或 eval 脚本顶层)时保留块级绑定承载支臂闭包,避免声明点覆写外层绑定。
|
/// 名无外层 var 绑定(形参/词法抑制、eval 脚本顶层或只读三常量)时保留
|
||||||
/// 支臂为块时由该块自身的块入口流程处理。
|
/// 块级绑定承载支臂闭包,避免声明点覆写外层绑定。支臂为块时由该块自身的
|
||||||
|
/// 块入口流程处理。
|
||||||
/// - 与 lexical 预声明的顺序:本函数在前,`{ let g; function g(){} }` 时
|
/// - 与 lexical 预声明的顺序:本函数在前,`{ let g; function g(){} }` 时
|
||||||
/// lexical 的 `declare_predeclared` 命中已存在的函数绑定自然报重复声明错,
|
/// lexical 的 `declare_predeclared` 命中已存在的函数绑定自然报重复声明错,
|
||||||
/// 避免函数预声明被 lexical 占位静默覆盖而破坏 let 的 TDZ。
|
/// 避免函数预声明被 lexical 占位静默覆盖而破坏 let 的 TDZ。
|
||||||
@@ -253,35 +254,55 @@ impl Emitter {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// 预声明 `if` 支臂裸函数声明的块级绑定(展开标签链后取函数声明):仅当该
|
/// 预声明 `if` 支臂裸函数声明的块级绑定(展开标签链并下钻嵌套 `if` 后取
|
||||||
/// 名没有外层 var 绑定承载时才在当前块建块 Let,承载支臂闭包,避免声明点
|
/// 函数声明):仅当该名没有外层 var 绑定承载时才在当前块建块 Let,承载支臂
|
||||||
/// 覆写外层形参/词法绑定;名已有外层 var 绑定(非 eval 且未被形参/词法同名
|
/// 闭包,避免声明点覆写外层形参/词法绑定;名已有外层 var 绑定(非 eval 脚本
|
||||||
/// 抑制)时不建块级绑定,声明点物化进该 var 槽。
|
/// 顶层、未被形参/词法同名抑制、且非全局只读内置三常量)时不建块级绑定,
|
||||||
|
/// 声明点物化进该 var 槽。
|
||||||
///
|
///
|
||||||
/// # 边界与前提
|
/// # 边界与前提
|
||||||
/// - 支臂为块时返回:该块自身的块入口流程预声明其函数声明。
|
/// - 支臂为块时返回:该块自身的块入口流程预声明其函数声明。
|
||||||
/// - 支臂为其它语句(含嵌套 `if`)时返回:嵌套 `if` 由 emit 到达时经本函数
|
/// - 支臂为嵌套 `if`(含 `else if` 链)时沿 consequent/alternate 下钻至叶子,
|
||||||
/// 的 `IfStatement` 臂处理。
|
/// 叶子按同一门控判定——嵌套支臂与单层支臂同属 Annex B 隐式支臂块。
|
||||||
|
/// - 支臂为其它语句时返回:其中的函数声明不属于 `if` 支臂隐式块。
|
||||||
/// - 须在 `block_fn_suppressed` 已构建之后调用(函数体/程序声明实例化阶段
|
/// - 须在 `block_fn_suppressed` 已构建之后调用(函数体/程序声明实例化阶段
|
||||||
/// 先于任何块 emit);`is_eval_script` 仅标记 eval 脚本顶层程序,其顶层
|
/// 先于任何块 emit);`is_eval_script` 仅标记 eval 脚本顶层程序,其顶层
|
||||||
/// 不建块级函数外层 var 绑定。
|
/// 不建块级函数外层 var 绑定;脚本顶层的只读三常量全局属性不可配置,
|
||||||
|
/// 声明实例化不建外层 var 绑定,须保留块级绑定承载支臂闭包。
|
||||||
fn predeclare_if_arm_block_function(&self, stmt: &Statement, ctx: &mut CompileCtx) {
|
fn predeclare_if_arm_block_function(&self, stmt: &Statement, ctx: &mut CompileCtx) {
|
||||||
let mut cur = stmt;
|
let mut cur = stmt;
|
||||||
while let Statement::LabeledStatement(ls) = cur {
|
while let Statement::LabeledStatement(ls) = cur {
|
||||||
cur = &ls.body;
|
cur = &ls.body;
|
||||||
}
|
}
|
||||||
let Statement::FunctionDeclaration(f) = cur else {
|
match cur {
|
||||||
return;
|
Statement::FunctionDeclaration(f) => {
|
||||||
};
|
|
||||||
let Some(identifier) = &f.id else {
|
let Some(identifier) = &f.id else {
|
||||||
return;
|
return;
|
||||||
};
|
};
|
||||||
if !ctx.is_eval_script && !ctx.block_fn_suppressed.contains(identifier.name.as_str()) {
|
if Self::if_arm_has_outer_var_carrier(ctx, identifier.name.as_str()) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
let reg = ctx.alloc_reg();
|
let reg = ctx.alloc_reg();
|
||||||
let _ = ctx.declare_initialized(identifier.name.as_str(), reg, VariableDeclarationKind::Let, false);
|
let _ = ctx.declare_initialized(identifier.name.as_str(), reg, VariableDeclarationKind::Let, false);
|
||||||
}
|
}
|
||||||
|
Statement::IfStatement(is) => {
|
||||||
|
self.predeclare_if_arm_block_function(&is.consequent, ctx);
|
||||||
|
if let Some(alt) = &is.alternate {
|
||||||
|
self.predeclare_if_arm_block_function(alt, ctx);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
_ => {}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// `if` 支臂裸函数声明的名是否已有外层 var/函数绑定承载:非 eval 脚本顶层的
|
||||||
|
/// 非抑制名由实例化阶段建外层 var 绑定,唯一例外是脚本顶层的只读三常量
|
||||||
|
/// (全局属性不可配置,声明实例化刻意不建外层绑定),其名须退回块级绑定。
|
||||||
|
fn if_arm_has_outer_var_carrier(ctx: &CompileCtx, name: &str) -> bool {
|
||||||
|
!ctx.is_eval_script
|
||||||
|
&& !ctx.block_fn_suppressed.contains(name)
|
||||||
|
&& !(ctx.is_global_scope && CompileCtx::is_non_writable_global_builtin(name))
|
||||||
|
}
|
||||||
|
|
||||||
/// 预声明当前作用域直接子语句中的 `let`/`const`/`class` 绑定(未初始化,
|
/// 预声明当前作用域直接子语句中的 `let`/`const`/`class` 绑定(未初始化,
|
||||||
/// 建立 TDZ 占位)。使声明点之前的读取在编译期可分辨为 TDZ 而非隐式全局,
|
/// 建立 TDZ 占位)。使声明点之前的读取在编译期可分辨为 TDZ 而非隐式全局,
|
||||||
|
|||||||
@@ -3,7 +3,8 @@
|
|||||||
//! 遮蔽该 `var`;块内前读须见 `var` 值、外层读须见支臂求值写回。
|
//! 遮蔽该 `var`;块内前读须见 `var` 值、外层读须见支臂求值写回。
|
||||||
//!
|
//!
|
||||||
//! 覆盖:块内前读/typeof 前读/后读、`if(false)` 未求值支、标签包裹支臂、支臂
|
//! 覆盖:块内前读/typeof 前读/后读、`if(false)` 未求值支、标签包裹支臂、支臂
|
||||||
//! 前 `var` 重赋值、块外后读、外层词法/形参同名不覆写、strict 早期错误。
|
//! 前 `var` 重赋值、块外后读、外层词法/形参同名不覆写、嵌套 `if`/`else if` 支臂
|
||||||
|
//! (eval 顶层与形参抑制面)、只读三常量名支臂、strict 早期错误。
|
||||||
|
|
||||||
use std::sync::Arc;
|
use std::sync::Arc;
|
||||||
|
|
||||||
@@ -96,6 +97,43 @@ fn if_arm_fn_keeps_outer_param_binding() {
|
|||||||
assert_eq!(eval(source).unwrap().as_int(), 42);
|
assert_eq!(eval(source).unwrap().as_int(), 42);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn nested_else_if_arm_fn_eval_top_level_keeps_block_binding() {
|
||||||
|
// 嵌套 `else if` 支臂在 eval 顶层无外层 var 面承载:须下钻到叶子并建块级
|
||||||
|
// 绑定,否则声明点无绑定可物化而报 `Identifier is not defined`。
|
||||||
|
let source = "eval(\"{ if(false) {} else if(true) function g(){}; typeof g }\")";
|
||||||
|
assert_eq!(eval_str(source).unwrap(), "function");
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn nested_if_if_arm_fn_eval_top_level_keeps_block_binding() {
|
||||||
|
// 嵌套 `if` 支臂(不带 `else`)同形:逐层下钻后叶子建块级绑定。
|
||||||
|
let source = "eval(\"{ if(true) if(true) function g(){}; typeof g }\")";
|
||||||
|
assert_eq!(eval_str(source).unwrap(), "function");
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn nested_else_if_arm_fn_keeps_outer_param_binding() {
|
||||||
|
// 形参抑制面嵌套 `else if`:支臂闭包写块槽,块外读仍见形参 42。
|
||||||
|
let source = "function f(g){ { if(false){} else if(true) function g(){} } return g } f(42)";
|
||||||
|
assert_eq!(eval(source).unwrap().as_int(), 42);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn if_arm_fn_named_undefined_at_script_top_keeps_block_binding() {
|
||||||
|
// 脚本顶层只读三常量无外层 var 绑定承载:块内支臂须建块级绑定,
|
||||||
|
// 否则声明点无绑定可物化而编译错。
|
||||||
|
let source = "{ if(true) function undefined(){} }";
|
||||||
|
assert!(eval(source).unwrap().is_undefined());
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn if_arm_fn_named_nan_at_script_top_keeps_block_binding() {
|
||||||
|
// 同只读三常量面:`NaN` 名为只读全局内置,块内支臂须建块级绑定。
|
||||||
|
let source = "{ if(true) function NaN(){} }";
|
||||||
|
assert!(eval(source).unwrap().is_undefined());
|
||||||
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn if_arm_fn_strict_is_early_error() {
|
fn if_arm_fn_strict_is_early_error() {
|
||||||
// strict 代码中 if 子句内的函数声明是早期错误(Annex B 仅 sloppy)。
|
// strict 代码中 if 子句内的函数声明是早期错误(Annex B 仅 sloppy)。
|
||||||
|
|||||||
Reference in New Issue
Block a user