Skip to content

Commit

Permalink
Skip frames that can't catch exceptions without mutating them
Browse files Browse the repository at this point in the history
  • Loading branch information
voltrevo committed Jul 18, 2024
1 parent c42c2f4 commit 00ec3c1
Show file tree
Hide file tree
Showing 12 changed files with 54 additions and 7 deletions.
5 changes: 1 addition & 4 deletions storage/src/storage_ptr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,7 @@ pub struct StoragePtr<T> {

impl<T> Clone for StoragePtr<T> {
fn clone(&self) -> Self {
Self {
_marker: std::marker::PhantomData,
data: self.data,
}
*self
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,10 @@ impl StackFrameTrait for ArrayMappingFrame {
panic!("Not appropriate for MapFrame")
}

fn can_catch_exception(&self, _exception: &Val) -> bool {
false
}

fn catch_exception(&mut self, _exception: &mut Val) {}

fn clone_to_stack_frame(&self) -> StackFrame {
Expand Down
4 changes: 4 additions & 0 deletions valuescript_vm/src/array_higher_functions/array_reduce.rs
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,10 @@ impl StackFrameTrait for ReduceFrame {
panic!("Not appropriate for ReduceFrame")
}

fn can_catch_exception(&self, _exception: &Val) -> bool {
false
}

fn catch_exception(&mut self, _exception: &mut Val) {}

fn clone_to_stack_frame(&self) -> StackFrame {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,10 @@ impl StackFrameTrait for ReduceRightFrame {
panic!("Not appropriate for ReduceRightFrame")
}

fn can_catch_exception(&self, _exception: &Val) -> bool {
false
}

fn catch_exception(&mut self, _exception: &mut Val) {}

fn clone_to_stack_frame(&self) -> StackFrame {
Expand Down
4 changes: 4 additions & 0 deletions valuescript_vm/src/array_higher_functions/array_sort.rs
Original file line number Diff line number Diff line change
Expand Up @@ -305,6 +305,10 @@ impl StackFrameTrait for SortFrame {
panic!("Not appropriate for SortFrame")
}

fn can_catch_exception(&self, _exception: &Val) -> bool {
false
}

fn catch_exception(&mut self, _exception: &mut Val) {}

fn clone_to_stack_frame(&self) -> StackFrame {
Expand Down
4 changes: 4 additions & 0 deletions valuescript_vm/src/bytecode_stack_frame.rs
Original file line number Diff line number Diff line change
Expand Up @@ -709,6 +709,10 @@ impl StackFrameTrait for BytecodeStackFrame {
panic!("Not appropriate for BytecodeStackFrame")
}

fn can_catch_exception(&self, _exception: &Val) -> bool {
self.catch_setting.is_some()
}

fn catch_exception(&mut self, exception: &mut Val) {
if let Some(catch_setting) = &self.catch_setting {
let exception = take(exception);
Expand Down
4 changes: 4 additions & 0 deletions valuescript_vm/src/cat_stack_frame.rs
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,10 @@ impl StackFrameTrait for CatStackFrame {
panic!("Not appropriate for CatStackFrame");
}

fn can_catch_exception(&self, _exception: &Val) -> bool {
false
}

fn catch_exception(&mut self, _exception: &mut Val) {}

fn clone_to_stack_frame(&self) -> StackFrame {
Expand Down
10 changes: 10 additions & 0 deletions valuescript_vm/src/first_stack_frame.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,12 @@ impl FirstStackFrame {
}
}

impl Default for FirstStackFrame {
fn default() -> Self {
Self::new()
}
}

impl StackFrameTrait for FirstStackFrame {
fn write_this(&mut self, _const: bool, _this: Val) -> Result<(), Val> {
panic!("Not appropriate for FirstStackFrame");
Expand All @@ -41,6 +47,10 @@ impl StackFrameTrait for FirstStackFrame {
self.call_result.clone()
}

fn can_catch_exception(&self, _exception: &Val) -> bool {
panic!("Not appropriate for FirstStackFrame");
}

fn catch_exception(&mut self, _exception: &mut Val) {
panic!("Not appropriate for FirstStackFrame");
}
Expand Down
8 changes: 8 additions & 0 deletions valuescript_vm/src/generator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,10 @@ impl StackFrameTrait for GeneratorFrame {
panic!("Not appropriate for GeneratorFrame")
}

fn can_catch_exception(&self, exception: &Val) -> bool {
self.generator.frame.can_catch_exception(exception)
}

fn catch_exception(&mut self, exception: &mut Val) {
self.generator.frame.catch_exception(exception)
}
Expand Down Expand Up @@ -343,6 +347,10 @@ impl StackFrameTrait for YieldStarFrame {
panic!("Not appropriate for YieldStarFrame")
}

fn can_catch_exception(&self, _exception: &Val) -> bool {
false
}

fn catch_exception(&mut self, _exception: &mut Val) {}

fn clone_to_stack_frame(&self) -> StackFrame {
Expand Down
4 changes: 4 additions & 0 deletions valuescript_vm/src/make_generator_frame.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,10 @@ impl StackFrameTrait for MakeGeneratorFrame {
panic!("Not appropriate for MakeGeneratorFrame")
}

fn can_catch_exception(&self, _exception: &Val) -> bool {
panic!("Not appropriate for MakeGeneratorFrame");
}

fn catch_exception(&mut self, _exception: &mut Val) {
panic!("Not appropriate for MakeGeneratorFrame");
}
Expand Down
5 changes: 5 additions & 0 deletions valuescript_vm/src/stack_frame.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ pub trait StackFrameTrait {
fn step(&mut self) -> FrameStepResult;
fn apply_call_result(&mut self, call_result: CallResult);
fn get_call_result(&mut self) -> CallResult;
fn can_catch_exception(&self, exception: &Val) -> bool;
fn catch_exception(&mut self, exception: &mut Val);
fn clone_to_stack_frame(&self) -> StackFrame;
}
Expand Down Expand Up @@ -63,6 +64,10 @@ impl StackFrameTrait for VoidStackFrame {
}
}

fn can_catch_exception(&self, _exception: &Val) -> bool {
false
}

fn catch_exception(&mut self, _exception: &mut Val) {}

fn clone_to_stack_frame(&self) -> StackFrame {
Expand Down
5 changes: 2 additions & 3 deletions valuescript_vm/src/virtual_machine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -119,9 +119,8 @@ impl VirtualMachine {

pub fn handle_exception(&mut self, mut exception: Val) -> Result<(), Val> {
while !self.stack.is_empty() {
self.frame.catch_exception(&mut exception);

if let Val::Void = exception {
if self.frame.can_catch_exception(&exception) {
self.frame.catch_exception(&mut exception);
return Ok(());
}

Expand Down

0 comments on commit 00ec3c1

Please sign in to comment.