fix: 脚本顶层 var/function 声明落 globalThis(DEFINE_GLOBAL_PROP 非可配置数据属性)

This commit is contained in:
2026-08-10 03:52:17 +08:00
parent a5a910a24e
commit e03ec0f635
11 changed files with 187 additions and 13 deletions
@@ -60,6 +60,10 @@ impl Emitter {
} else {
ctx.inst(Inst::new(OpCode::STORE_VAR, Operand::Reg(var_reg), Operand::Reg(var_reg), Operand::None));
}
// 脚本顶层函数声明:同步写全局对象,使 globalThis 可反射函数名。
if ctx.is_global_scope {
self.emit_global_prop_write(&name, var_reg, ctx);
}
Ok(None)
}
}
@@ -61,6 +61,10 @@ impl Emitter {
ctx.inst(Inst::new(OpCode::STORE_VAR, Operand::Reg(target_reg), Operand::Reg(tmp), Operand::None));
}
ctx.init_var(bi.name.as_str());
// 脚本顶层 var 无初始化:仍须写全局对象属性(值为 undefined)。
if ctx.is_global_scope && matches!(decl.kind, VariableDeclarationKind::Var) {
self.emit_global_prop_write(bi.name.as_str(), tmp, ctx);
}
r = Some(var_reg);
}
}