fix: 类构造器寄存器放宽 u8 守卫并消除 this 槽 254 特判碰撞

This commit is contained in:
2026-10-16 17:01:17 +00:00
parent a791a312ec
commit 77782f01a5
6 changed files with 63 additions and 29 deletions
+2 -4
View File
@@ -14,10 +14,8 @@ impl Emitter {
&self, elements: &[ClassElement], ctor_reg: u32, key_slots: &[Option<u8>], ctx: &mut CompileCtx,
) -> Result<(), String> {
let saved_static_this = ctx.static_block_this_reg;
// static_block_this_reg 保留 u8(this 槽语义,254 特判),ctor_reg 超 u8 界时报错而非静默截断。
ctx.static_block_this_reg = Some(
u8::try_from(ctor_reg).map_err(|_| format!("class constructor register {ctor_reg} exceeds u8 limit"))?,
);
// ctor_reg 是 vreg 号(RegAlloc 期压缩到物理域),直接存 u32,不截断。
ctx.static_block_this_reg = Some(ctor_reg);
for (element, &key_slot) in elements.iter().zip(key_slots) {
match element {
ClassElement::PropertyDefinition(prop) => {
+3 -1
View File
@@ -66,7 +66,9 @@ pub struct CompileCtx {
/// 源码是否为 `source_escape` 产物(动态编译入口):正则字面量源文本切片
/// 的池键形态据此选择(见 [`Emitter::source_encoded`])。
pub(crate) source_encoded: bool,
pub(crate) static_block_this_reg: Option<u8>,
/// 静态块内 `this` 的构造器寄存器(vreg 号,无 u8 上限,RegAlloc 期压缩到物理域)。
/// None 表示非静态块上下文,`this` 走常规 this 槽。
pub(crate) static_block_this_reg: Option<u32>,
pub(crate) field_buffer: Option<FieldBuffer>,
/// 类构造器模块中 `@@field_keys` upvalue 下标(实例字段 computed key 数组)。
/// 类定义期求值一次存入 cell,构造器经此 upvalue 读取。
+6 -2
View File
@@ -8,8 +8,12 @@ use oxide_ir::operand::Operand;
impl Emitter {
pub(crate) fn emit_this_expression(&self, ctx: &mut CompileCtx) -> Result<u32, String> {
let r = ctx.alloc_reg();
let src = ctx.static_block_this_reg.unwrap_or(254);
let src_op = if src == 254 { Operand::This } else { Operand::Reg(src as u32) };
// None 是常规 this 槽;Some(reg) 是类构造器 vreg(vreg 号可越过 254,
// 按寄存器处理,不与 this 槽特判混淆)。
let src_op = match ctx.static_block_this_reg {
None => Operand::This,
Some(reg) => Operand::Reg(reg),
};
ctx.inst(Inst::new(OpCode::LOAD_VAR, Operand::Reg(r), src_op, Operand::None));
Ok(r)
}
+30
View File
@@ -576,3 +576,33 @@ fn private_field_read_captures_object() {
let names: HashSet<&str> = inner.upvalue_captures.iter().map(|c| c.name.as_str()).collect();
assert!(names.contains("self"), "私有字段读路径目标对象须入捕获集");
}
/// 面 21:顶层 vreg 号越过 255 后,类静态块 `this` 仍正确读类构造器 vreg——
/// 构造器寄存器不截断、不与 this 槽(254)特判混淆。
#[test]
fn static_block_this_reads_ctor_vreg_beyond_u8() {
// 大量顶层 var 把 vreg 号推过 255,再声明含静态块的类。
let mut src = String::new();
for i in 0..300 {
src.push_str(&format!("var v{i} = {i};\n"));
}
src.push_str("class C { static { this.x = 1; } }");
// 修复前此源码 emit 报「class constructor register … exceeds u8 limit」,
// `emit_ir` 会 panic。
let ir = emit_ir(&src);
// 静态块 `this` 应发 LOAD_VAR 读构造器 vreg(Operand::Reg),非 this 槽。
let this_load = ir
.insts
.iter()
.filter(|i| i.op == OpCode::LOAD_VAR)
.find(|i| matches!(i.a, Operand::Reg(_)))
.expect("静态块 this 应发 LOAD_VAR Reg");
let Operand::Reg(r) = this_load.a else {
unreachable!()
};
assert!(r > 255, "静态块 this 的构造器 vreg 号应越过 255:{r}");
assert!(
!ir.insts.iter().any(|i| i.op == OpCode::LOAD_VAR && matches!(i.a, Operand::This)),
"静态块 this 不应走 this 槽"
);
}