From fd978b7ef8201ecfd88f32c5b69de6d388afe5c6 Mon Sep 17 00:00:00 2001
From: pjh456 <147148383@qq.com>
Date: Wed, 5 Aug 2026 01:17:22 +0800
Subject: [PATCH] =?UTF-8?q?refactor:=20=E7=A7=BB=E9=99=A4=E8=AF=AD?=
=?UTF-8?q?=E5=8F=A5=E8=B7=B3=E8=BD=AC=E8=AE=A1=E6=95=B0=E8=BE=85=E5=8A=A9?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
crates/oxide_compiler/src/compiler.rs | 18 +++--
.../src/stmt/basic/break_continue.rs | 8 ---
.../oxide_compiler/src/stmt/basic/labeled.rs | 18 -----
crates/oxide_compiler/src/stmt/basic/mod.rs | 9 ---
crates/oxide_compiler/src/stmt/control.rs | 27 --------
.../oxide_compiler/src/stmt/exception/mod.rs | 7 --
.../src/stmt/exception/try_catch.rs | 67 ++-----------------
crates/oxide_compiler/src/stmt/switch.rs | 37 ----------
.../tests/compile_stmt_tests.rs | 12 ++++
9 files changed, 28 insertions(+), 175 deletions(-)
diff --git a/crates/oxide_compiler/src/compiler.rs b/crates/oxide_compiler/src/compiler.rs
index 30c68114..86d5c5ab 100644
--- a/crates/oxide_compiler/src/compiler.rs
+++ b/crates/oxide_compiler/src/compiler.rs
@@ -331,6 +331,17 @@ impl CompileCtx {
});
}
+ pub(crate) fn emit_try_finally_begin_labeled(&mut self, label: Label) {
+ let pc = self.bytecode.len();
+ self.emit(opcode::encode_try_finally_begin(0));
+ self.fixups.push(JumpFixup {
+ pc,
+ label,
+ opcode: OpCode::TRY_FINALLY_BEGIN,
+ rd: 0,
+ });
+ }
+
pub(crate) fn count_word(&mut self) {
self.projected_pc += 1;
}
@@ -481,6 +492,7 @@ impl CompileCtx {
OpCode::JMP_IF_TRUE => opcode::encode_jmp_if_true(fixup.rd, offset),
OpCode::JMP_IF_NULLISH => opcode::encode_jmp_if_nullish(fixup.rd, offset),
OpCode::TRY_BEGIN => opcode::encode_try_begin(offset),
+ OpCode::TRY_FINALLY_BEGIN => opcode::encode_try_finally_begin(offset),
_ => return Err(format!("Unsupported fixup opcode {:?}", fixup.opcode)),
};
}
@@ -1275,17 +1287,11 @@ impl Compiler {
pub(crate) fn count_statement(&self, stmt: &Statement, ctx: &mut CompileCtx) {
match stmt {
- Statement::BreakStatement(_) | Statement::ContinueStatement(_) | Statement::LabeledStatement(_) => {
- self.count_basic(stmt, ctx)
- }
- Statement::IfStatement(_) => self.count_control_domain(stmt, ctx),
Statement::WhileStatement(_)
| Statement::DoWhileStatement(_)
| Statement::ForStatement(_)
| Statement::ForInStatement(_)
| Statement::ForOfStatement(_) => self.count_iteration_domain(stmt, ctx),
- Statement::SwitchStatement(_) => self.count_switch_domain(stmt, ctx),
- Statement::TryStatement(_) => self.count_exception_domain(stmt, ctx),
_ => {}
}
}
diff --git a/crates/oxide_compiler/src/stmt/basic/break_continue.rs b/crates/oxide_compiler/src/stmt/basic/break_continue.rs
index d9f4bd30..15361446 100644
--- a/crates/oxide_compiler/src/stmt/basic/break_continue.rs
+++ b/crates/oxide_compiler/src/stmt/basic/break_continue.rs
@@ -1,14 +1,6 @@
use crate::compiler::{CompileCtx, Compiler};
impl Compiler {
- pub(super) fn count_break_statement(&self, ctx: &mut CompileCtx) {
- ctx.projected_pc += 1;
- }
-
- pub(super) fn count_continue_statement(&self, ctx: &mut CompileCtx) {
- ctx.projected_pc += 1;
- }
-
pub(crate) fn emit_break_statement(
&self, stmt: &oxide_parser::BreakStatement, ctx: &mut CompileCtx,
) -> Result