fix: try/finally 支持 break/continue/return 穿越 finally(completion 机制)

This commit is contained in:
2026-08-08 12:38:25 +08:00
parent 6c29515624
commit cd214069cd
18 changed files with 529 additions and 39 deletions
+4
View File
@@ -36,6 +36,8 @@ impl Inst {
| OpCode::RETURN
| OpCode::THROW
| OpCode::JMP
| OpCode::BREAK
| OpCode::CONTINUE
| OpCode::JMP_IF_FALSE
| OpCode::JMP_IF_TRUE
| OpCode::JMP_IF_NULLISH
@@ -197,6 +199,8 @@ impl Inst {
}
// 无条件跳转 / try 标记:b 是 Label,无寄存器
OpCode::JMP
| OpCode::BREAK
| OpCode::CONTINUE
| OpCode::TRY_BEGIN
| OpCode::TRY_FINALLY_BEGIN
| OpCode::TRY_END
+12
View File
@@ -218,6 +218,18 @@ impl Inst {
Self::new(OpCode::JMP, Operand::None, Operand::None, Operand::Label(label))
}
/// break 完成:label 指向循环/switch 出口,`crossed` 为逃出的 finally 域数
/// (emit 词法计算,运行时据此逐个穿越 finally;0 表示未逃出,不产生完成语义)。
/// 编码:rd 槽放 crossed(≤255),label 放 b 槽。
pub fn brk(label: LabelId, crossed: u16) -> Self {
Self::new(OpCode::BREAK, Operand::Imm(crossed), Operand::None, Operand::Label(label))
}
/// continue 完成:同 break,label 指向循环继续目标。
pub fn cont(label: LabelId, crossed: u16) -> Self {
Self::new(OpCode::CONTINUE, Operand::Imm(crossed), Operand::None, Operand::Label(label))
}
/// 条件寄存器为 false 时跳转。
pub fn jmp_if_false(cond_reg: u32, label: LabelId) -> Self {
Self::new(OpCode::JMP_IF_FALSE, Operand::Reg(cond_reg), Operand::None, Operand::Label(label))
+3
View File
@@ -126,6 +126,7 @@ fn encode_inst(inst: &Inst, instrs: &mut Vec<u32>, jumps: &mut Vec<(usize, u32)>
let start = instrs.len();
let instr = match inst.op {
OpCode::JMP => opcode::encode_jmp(0),
OpCode::BREAK | OpCode::CONTINUE => opcode::encode(inst.op, cond, 0, 0),
OpCode::JMP_IF_FALSE => opcode::encode_jmp_if_false(cond, 0),
OpCode::JMP_IF_TRUE => opcode::encode_jmp_if_true(cond, 0),
OpCode::JMP_IF_NULLISH => opcode::encode_jmp_if_nullish(cond, 0),
@@ -149,6 +150,8 @@ fn is_jump_op(op: OpCode) -> bool {
matches!(
op,
OpCode::JMP
| OpCode::BREAK
| OpCode::CONTINUE
| OpCode::JMP_IF_FALSE
| OpCode::JMP_IF_TRUE
| OpCode::JMP_IF_NULLISH