fix: 裸 return 物化 undefined,防调用残留值冒充完成值

This commit is contained in:
2026-09-20 18:07:54 +00:00
parent 06cd63c3b0
commit c9e1ee2e09
3 changed files with 76 additions and 2 deletions
@@ -1,6 +1,7 @@
//! return 语句 emit:`emit_return_statement` 求值返回值并生成返回指令。
use crate::{CompileCtx, Emitter};
use oxide_bytecode::module::Constant;
use oxide_bytecode::opcode::OpCode;
use oxide_ir::inst::Inst;
use oxide_ir::operand::Operand;
@@ -32,7 +33,12 @@ impl Emitter {
if for_of_count == 0 && for_in_count == 0 {
self.emit_return_try_cleanup(ctx);
}
ctx.inst(Inst::ret(Operand::None, for_of_count, for_in_count));
// 裸 return 须显式物化 undefined 为完成值:RETURN 从 rd 槽读值,
// 不物化则交付槽内残留值(如最近一次调用的结果)冒充完成值。
let undef_idx = ctx.add_constant(Constant::Undefined);
let undef_reg = ctx.alloc_reg();
ctx.inst(Inst::load_const(Operand::Reg(undef_reg), undef_idx));
ctx.inst(Inst::ret(Operand::Reg(undef_reg), for_of_count, for_in_count));
}
}
Ok(None)