mirror of
https://github.com/pjh456/OxideJS.git
synced 2026-10-27 04:01:11 +08:00
refactor: 拆分编译流水线过程以避免逻辑漂移
This commit is contained in:
@@ -0,0 +1,67 @@
|
||||
use super::super::*;
|
||||
|
||||
impl Compiler {
|
||||
fn count_throw_statement(&self, stmt: &oxide_parser::ThrowStatement<'_>, ctx: &mut CompileCtx) {
|
||||
self.count_expression(&stmt.argument, ctx);
|
||||
ctx.projected_pc += 1;
|
||||
}
|
||||
|
||||
fn count_try_statement(&self, stmt: &oxide_parser::TryStatement<'_>, ctx: &mut CompileCtx) {
|
||||
let id = ctx.next_label_id();
|
||||
let catch_label = Label::CatchBody(id);
|
||||
let try_end_label = Label::TryEnd(id);
|
||||
let has_catch = stmt.handler.is_some();
|
||||
let has_finally = stmt.finalizer.is_some();
|
||||
ctx.alloc_reg();
|
||||
if has_finally {
|
||||
ctx.projected_pc += 1;
|
||||
}
|
||||
if has_catch {
|
||||
ctx.projected_pc += 1;
|
||||
}
|
||||
for s in &stmt.block.body {
|
||||
self.count_statement(s, ctx);
|
||||
}
|
||||
ctx.projected_pc += 1;
|
||||
if has_catch {
|
||||
ctx.projected_pc += 1;
|
||||
}
|
||||
let jmp_needed = has_catch || has_finally;
|
||||
if jmp_needed {
|
||||
ctx.projected_pc += 1;
|
||||
}
|
||||
ctx.labels.label_map.insert(catch_label, ctx.projected_pc);
|
||||
if let Some(catch) = &stmt.handler {
|
||||
ctx.push_scope();
|
||||
if let Some(param) = &catch.param {
|
||||
ctx.alloc_reg();
|
||||
if let oxide_parser::BindingPattern::BindingIdentifier(_) = ¶m.pattern {
|
||||
ctx.projected_pc += 1;
|
||||
}
|
||||
}
|
||||
for cs in &catch.body.body {
|
||||
self.count_statement(cs, ctx);
|
||||
}
|
||||
ctx.projected_pc += 1;
|
||||
ctx.pop_scope();
|
||||
}
|
||||
if let Some(finally) = &stmt.finalizer {
|
||||
let finally_label = Label::FinallyBody(id);
|
||||
ctx.labels.label_map.insert(finally_label, ctx.projected_pc);
|
||||
for fs in &finally.body {
|
||||
self.count_statement(fs, ctx);
|
||||
}
|
||||
ctx.projected_pc += 1;
|
||||
ctx.projected_pc += 1;
|
||||
}
|
||||
ctx.labels.label_map.insert(try_end_label, ctx.projected_pc);
|
||||
}
|
||||
|
||||
pub(crate) fn count_exception_domain(&self, stmt: &Statement, ctx: &mut CompileCtx) {
|
||||
match stmt {
|
||||
Statement::ThrowStatement(ts) => self.count_throw_statement(ts, ctx),
|
||||
Statement::TryStatement(ts) => self.count_try_statement(ts, ctx),
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user