mirror of
https://github.com/pjh456/OxideJS.git
synced 2026-10-23 18:21:13 +08:00
fix: 全局三常量描述符翻可配置,var 重复声明以 builtin 镜像值保留现存值
This commit is contained in:
@@ -1627,11 +1627,21 @@ impl Emitter {
|
||||
// 全局声明实例化序言:脚本求值前为顶层 var 名创建全局对象属性(值 undefined),
|
||||
// 使声明语句执行前的读取(typeof、反射、自引用)可经全局对象见绑定;同名
|
||||
// 函数声明的属性由首个 sub-pass 以函数值覆盖,声明语句保持值更新语义。
|
||||
// builtin 名的全局属性运行期预存(session 绑定):序言值写是单 opcode 值
|
||||
// 混叠,直接写 undefined 会把现存值(NaN 等)抹掉,故以 builtin 镜像槽
|
||||
// (run 起点预载全局属性值)作为写入值——描述符按 CreateGlobalVarBinding
|
||||
// 翻 writable,值幂等保留;裸读走镜像、反射见预存属性。
|
||||
let gdi_var_names = self.collect_var_binding_names(&program.body);
|
||||
if !gdi_var_names.is_empty() {
|
||||
let undef_reg = self.emit_undefined(&mut ctx);
|
||||
for name in &gdi_var_names {
|
||||
self.emit_global_prop_write(name, undef_reg, &mut ctx);
|
||||
let value_reg = if CompileCtx::is_known_builtin(name) {
|
||||
// 镜像槽未预登记时(解构 pattern 名等)就地登记,run 起点预载。
|
||||
ctx.lookup_or_builtin(name).unwrap_or(undef_reg)
|
||||
} else {
|
||||
undef_reg
|
||||
};
|
||||
self.emit_global_prop_write(name, value_reg, &mut ctx);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -372,6 +372,19 @@ impl Emitter {
|
||||
}
|
||||
}
|
||||
|
||||
/// 预声明单个 `var` 名:仅顶层(全局作用域)的 builtin 名落 builtin 镜像槽
|
||||
/// (run 起点预载全局属性值),使 GDI 序言与声明点同步都用入口原值而非
|
||||
/// fresh undefined 槽,现存值(NaN 等)不被抹;函数体内 builtin 名仍是局部
|
||||
/// var 遮蔽(fresh 槽,不命中只读内置拦截);其余名 fresh var 槽。
|
||||
fn predeclare_var_name(&self, name: &str, ctx: &mut CompileCtx) {
|
||||
if ctx.is_global_scope && CompileCtx::is_known_builtin(name) {
|
||||
let _ = ctx.lookup_or_builtin(name);
|
||||
} else {
|
||||
let reg = ctx.alloc_reg();
|
||||
let _ = ctx.declare_initialized(name, reg, VariableDeclarationKind::Var, false);
|
||||
}
|
||||
}
|
||||
|
||||
/// 预声明语句列表中的全部 `var` 绑定,使先发的提升函数声明能解析它们。
|
||||
/// 顶层 `var` 名在编译闭包捕获它的函数体时必须可见。
|
||||
pub(crate) fn predeclare_var_declarations(&self, statements: &[Statement], ctx: &mut CompileCtx) {
|
||||
@@ -383,8 +396,7 @@ impl Emitter {
|
||||
}
|
||||
for d in &decl.declarations {
|
||||
if let oxide_parser::BindingPattern::BindingIdentifier(bi) = &d.id {
|
||||
let reg = ctx.alloc_reg();
|
||||
let _ = ctx.declare_initialized(bi.name.as_str(), reg, VariableDeclarationKind::Var, false);
|
||||
self.predeclare_var_name(bi.name.as_str(), ctx);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -406,13 +418,7 @@ impl Emitter {
|
||||
if matches!(decl.kind, VariableDeclarationKind::Var) {
|
||||
for d in &decl.declarations {
|
||||
if let oxide_parser::BindingPattern::BindingIdentifier(bi) = &d.id {
|
||||
let reg = ctx.alloc_reg();
|
||||
let _ = ctx.declare_initialized(
|
||||
bi.name.as_str(),
|
||||
reg,
|
||||
VariableDeclarationKind::Var,
|
||||
false,
|
||||
);
|
||||
self.predeclare_var_name(bi.name.as_str(), ctx);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -446,9 +452,7 @@ impl Emitter {
|
||||
}
|
||||
for d in &decl.declarations {
|
||||
if let oxide_parser::BindingPattern::BindingIdentifier(bi) = &d.id {
|
||||
let reg = ctx.alloc_reg();
|
||||
let _ =
|
||||
ctx.declare_initialized(bi.name.as_str(), reg, VariableDeclarationKind::Var, false);
|
||||
self.predeclare_var_name(bi.name.as_str(), ctx);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -84,8 +84,13 @@ impl Emitter {
|
||||
ctx.init_var(bi.name.as_str());
|
||||
// 脚本顶层 var:全局对象属性同步绑定当前值(首次声明即 undefined,
|
||||
// 先前已写入则保留写入值,声明不是覆盖性赋值)。被捕获绑定的值
|
||||
// 存在 cell 而非槽,从 cell 同步。
|
||||
if ctx.is_global_scope && matches!(decl.kind, VariableDeclarationKind::Var) {
|
||||
// 存在 cell 而非槽,从 cell 同步。builtin 名不同步:描述符翻
|
||||
// writable 已由 GDI 序言以镜像值完成,未初始化的槽/cell 是
|
||||
// undefined,再同步会把现存值(NaN 等)抹掉。
|
||||
if ctx.is_global_scope
|
||||
&& matches!(decl.kind, VariableDeclarationKind::Var)
|
||||
&& !CompileCtx::is_known_builtin(bi.name.as_str())
|
||||
{
|
||||
let src = if let Some(&cell_idx) = ctx.captured_bindings.get(bi.name.as_str()) {
|
||||
let r = ctx.alloc_reg();
|
||||
if let Some((binding, _)) = ctx.scopes.symbols.lookup_any_binding(bi.name.as_str()) {
|
||||
|
||||
Reference in New Issue
Block a user