Skip to content

Commit

Permalink
Some cleanup and remove thrown flag
Browse files Browse the repository at this point in the history
  • Loading branch information
nekevss committed Feb 3, 2023
1 parent 781cc67 commit 0833494
Show file tree
Hide file tree
Showing 7 changed files with 33 additions and 20 deletions.
2 changes: 1 addition & 1 deletion boa_engine/src/bytecompiler/jump_control.rs
Original file line number Diff line number Diff line change
Expand Up @@ -404,7 +404,7 @@ impl ByteCompiler<'_, '_> {
}
}

// Handle breaks in a finally block for label in info.breaks {
// Handle breaks in a finally block
for label in info.breaks {
self.patch_jump(label);
}
Expand Down
4 changes: 4 additions & 0 deletions boa_engine/src/vm/call_frame/abrupt_record.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,10 @@ impl AbruptCompletionRecord {
self.kind == AbruptKind::Throw
}

pub(crate) fn is_throw_with_target(self) -> bool {
self.is_throw() && self.target < u32::MAX
}

/// Returns the value of `AbruptCompletionRecord`'s `target` field.
pub(crate) const fn target(self) -> u32 {
self.target
Expand Down
4 changes: 0 additions & 4 deletions boa_engine/src/vm/call_frame/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,6 @@ pub struct CallFrame {
#[unsafe_ignore_trace]
pub(crate) generator_resume_kind: GeneratorResumeKind,

// Indicate that the last try block has thrown an exception.
pub(crate) thrown: bool,

// When an async generator is resumed, the generator object is needed
// to fulfill the steps 4.e-j in [AsyncGeneratorStart](https://tc39.es/ecma262/#sec-asyncgeneratorstart).
pub(crate) async_generator: Option<JsObject>,
Expand All @@ -56,7 +53,6 @@ impl CallFrame {
param_count: 0,
arg_count: 0,
generator_resume_kind: GeneratorResumeKind::Normal,
thrown: false,
async_generator: None,
}
}
Expand Down
14 changes: 7 additions & 7 deletions boa_engine/src/vm/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -336,18 +336,17 @@ impl Context<'_> {
self.vm.frame_mut().pc = catch_target as usize;
} else {
self.vm.frame_mut().pc = target_address as usize;
let record = AbruptCompletionRecord::default()
.with_throw_flag()
.with_initial_target(catch_target);
self.vm.frame_mut().abrupt_completion = Some(record);
};

for _ in 0..self.vm.frame().pop_on_return {
self.vm.pop();
}
self.vm.frame_mut().pop_on_return = 0;

self.vm.frame_mut().thrown = true;
self.vm.frame_mut().pop_on_return = 0;
let record = AbruptCompletionRecord::default()
.with_throw_flag()
.with_initial_target(catch_target);
self.vm.frame_mut().abrupt_completion = Some(record);
self.vm.frame_mut().finally_return = FinallyReturn::Err;
let err = e.to_opaque(self);
self.vm.push(err);
Expand Down Expand Up @@ -388,7 +387,8 @@ impl Context<'_> {
}
self.vm.frame_mut().pop_on_return = 0;

self.vm.frame_mut().thrown = true;
let record = AbruptCompletionRecord::default().with_throw_flag();
self.vm.frame_mut().abrupt_completion = Some(record);
self.vm.frame_mut().pc = address;
self.vm.frame_mut().finally_return = FinallyReturn::Err;
let err = e.to_opaque(self);
Expand Down
1 change: 0 additions & 1 deletion boa_engine/src/vm/opcode/control_flow/catch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ impl Operation for CatchStart {

context.vm.frame_mut().abrupt_completion = None;
context.vm.frame_mut().finally_return = FinallyReturn::None;
context.vm.frame_mut().thrown = false;
Ok(ShouldExit::False)
}
}
Expand Down
16 changes: 14 additions & 2 deletions boa_engine/src/vm/opcode/control_flow/finally.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@ impl Operation for FinallyEnd {
const INSTRUCTION: &'static str = "INST - FinallyEnd";

fn execute(context: &mut Context<'_>) -> JsResult<ShouldExit> {
// TODO handle a new way to get next_finally value
let finally_candidates = context.vm.frame().env_stack.iter().filter(|env| {
env.is_finally_env() && context.vm.frame().pc < (env.start_address() as usize)
});
Expand Down Expand Up @@ -148,7 +147,8 @@ impl Operation for FinallyEnd {
envs_to_pop += env_entry.env_num();
context.vm.frame_mut().env_stack.pop();
}
} else if record.is_throw() && context.vm.frame().pc < record.target() as usize
} else if record.is_throw_with_target()
&& context.vm.frame().pc < record.target() as usize
{
context.vm.frame_mut().pc = record.target() as usize;
for _ in 0..context.vm.frame().env_stack.len() {
Expand All @@ -165,6 +165,18 @@ impl Operation for FinallyEnd {
}
}
context.vm.frame_mut().abrupt_completion = None;
} else if !record.is_throw_with_target() {
let current_stack = context
.vm
.frame_mut()
.env_stack
.pop()
.expect("Popping current finally stack.");

for _ in 0..current_stack.env_num() {
context.realm.environments.pop();
}
return Err(JsError::from_opaque(context.vm.pop()));
}

for _ in 0..envs_to_pop {
Expand Down
12 changes: 7 additions & 5 deletions boa_engine/src/vm/opcode/pop/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,13 @@ impl Operation for PopIfThrown {
const INSTRUCTION: &'static str = "INST - PopIfThrown";

fn execute(context: &mut Context<'_>) -> JsResult<ShouldExit> {
let frame = context.vm.frame_mut();
if frame.thrown {
frame.thrown = false;
context.vm.pop();
}
let frame = context.vm.frame();
match frame.abrupt_completion {
Some(record) if record.is_throw() => {
context.vm.pop();
}
_ => {}
};
Ok(ShouldExit::False)
}
}
Expand Down

0 comments on commit 0833494

Please sign in to comment.