fix: 未声明标识符读取抛 ReferenceError(typeof 特判返回 undefined)

This commit is contained in:
2026-08-16 00:10:08 +08:00
parent 69152ad608
commit 75153a909d
9 changed files with 287 additions and 6 deletions
+8 -1
View File
@@ -45,7 +45,14 @@ impl Emitter {
Err(err) => return Err(err),
};
let r = ctx.alloc_reg();
ctx.inst(Inst::new(OpCode::LOAD_VAR, Operand::Reg(r), Operand::Reg(var_reg), Operand::None));
if ctx.implicit_global_reads.contains(&var_reg) {
// 未声明标识符读:运行期查 global object 属性存在性,缺失抛
// ReferenceError(sloppy 写先登记全局槽的读不在此集合,仍走 LOAD_VAR)。
let key_idx = ctx.add_constant(Constant::String(name.to_string()));
ctx.inst(Inst::new(OpCode::LOAD_GLOBAL, Operand::Reg(r), Operand::Const(key_idx), Operand::None));
} else {
ctx.inst(Inst::new(OpCode::LOAD_VAR, Operand::Reg(r), Operand::Reg(var_reg), Operand::None));
}
Ok(r)
}