Skip to content

Commit 00ec3c1

Browse files
committed
Skip frames that can't catch exceptions without mutating them
1 parent c42c2f4 commit 00ec3c1

File tree

12 files changed

+54
-7
lines changed

12 files changed

+54
-7
lines changed

storage/src/storage_ptr.rs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,7 @@ pub struct StoragePtr<T> {
1414

1515
impl<T> Clone for StoragePtr<T> {
1616
fn clone(&self) -> Self {
17-
Self {
18-
_marker: std::marker::PhantomData,
19-
data: self.data,
20-
}
17+
*self
2118
}
2219
}
2320

valuescript_vm/src/array_higher_functions/array_mapping_frame.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,10 @@ impl StackFrameTrait for ArrayMappingFrame {
146146
panic!("Not appropriate for MapFrame")
147147
}
148148

149+
fn can_catch_exception(&self, _exception: &Val) -> bool {
150+
false
151+
}
152+
149153
fn catch_exception(&mut self, _exception: &mut Val) {}
150154

151155
fn clone_to_stack_frame(&self) -> StackFrame {

valuescript_vm/src/array_higher_functions/array_reduce.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,10 @@ impl StackFrameTrait for ReduceFrame {
111111
panic!("Not appropriate for ReduceFrame")
112112
}
113113

114+
fn can_catch_exception(&self, _exception: &Val) -> bool {
115+
false
116+
}
117+
114118
fn catch_exception(&mut self, _exception: &mut Val) {}
115119

116120
fn clone_to_stack_frame(&self) -> StackFrame {

valuescript_vm/src/array_higher_functions/array_reduce_right.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,10 @@ impl StackFrameTrait for ReduceRightFrame {
124124
panic!("Not appropriate for ReduceRightFrame")
125125
}
126126

127+
fn can_catch_exception(&self, _exception: &Val) -> bool {
128+
false
129+
}
130+
127131
fn catch_exception(&mut self, _exception: &mut Val) {}
128132

129133
fn clone_to_stack_frame(&self) -> StackFrame {

valuescript_vm/src/array_higher_functions/array_sort.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -305,6 +305,10 @@ impl StackFrameTrait for SortFrame {
305305
panic!("Not appropriate for SortFrame")
306306
}
307307

308+
fn can_catch_exception(&self, _exception: &Val) -> bool {
309+
false
310+
}
311+
308312
fn catch_exception(&mut self, _exception: &mut Val) {}
309313

310314
fn clone_to_stack_frame(&self) -> StackFrame {

valuescript_vm/src/bytecode_stack_frame.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -709,6 +709,10 @@ impl StackFrameTrait for BytecodeStackFrame {
709709
panic!("Not appropriate for BytecodeStackFrame")
710710
}
711711

712+
fn can_catch_exception(&self, _exception: &Val) -> bool {
713+
self.catch_setting.is_some()
714+
}
715+
712716
fn catch_exception(&mut self, exception: &mut Val) {
713717
if let Some(catch_setting) = &self.catch_setting {
714718
let exception = take(exception);

valuescript_vm/src/cat_stack_frame.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,10 @@ impl StackFrameTrait for CatStackFrame {
145145
panic!("Not appropriate for CatStackFrame");
146146
}
147147

148+
fn can_catch_exception(&self, _exception: &Val) -> bool {
149+
false
150+
}
151+
148152
fn catch_exception(&mut self, _exception: &mut Val) {}
149153

150154
fn clone_to_stack_frame(&self) -> StackFrame {

valuescript_vm/src/first_stack_frame.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,12 @@ impl FirstStackFrame {
1919
}
2020
}
2121

22+
impl Default for FirstStackFrame {
23+
fn default() -> Self {
24+
Self::new()
25+
}
26+
}
27+
2228
impl StackFrameTrait for FirstStackFrame {
2329
fn write_this(&mut self, _const: bool, _this: Val) -> Result<(), Val> {
2430
panic!("Not appropriate for FirstStackFrame");
@@ -41,6 +47,10 @@ impl StackFrameTrait for FirstStackFrame {
4147
self.call_result.clone()
4248
}
4349

50+
fn can_catch_exception(&self, _exception: &Val) -> bool {
51+
panic!("Not appropriate for FirstStackFrame");
52+
}
53+
4454
fn catch_exception(&mut self, _exception: &mut Val) {
4555
panic!("Not appropriate for FirstStackFrame");
4656
}

valuescript_vm/src/generator.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,10 @@ impl StackFrameTrait for GeneratorFrame {
230230
panic!("Not appropriate for GeneratorFrame")
231231
}
232232

233+
fn can_catch_exception(&self, exception: &Val) -> bool {
234+
self.generator.frame.can_catch_exception(exception)
235+
}
236+
233237
fn catch_exception(&mut self, exception: &mut Val) {
234238
self.generator.frame.catch_exception(exception)
235239
}
@@ -343,6 +347,10 @@ impl StackFrameTrait for YieldStarFrame {
343347
panic!("Not appropriate for YieldStarFrame")
344348
}
345349

350+
fn can_catch_exception(&self, _exception: &Val) -> bool {
351+
false
352+
}
353+
346354
fn catch_exception(&mut self, _exception: &mut Val) {}
347355

348356
fn clone_to_stack_frame(&self) -> StackFrame {

valuescript_vm/src/make_generator_frame.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,10 @@ impl StackFrameTrait for MakeGeneratorFrame {
5050
panic!("Not appropriate for MakeGeneratorFrame")
5151
}
5252

53+
fn can_catch_exception(&self, _exception: &Val) -> bool {
54+
panic!("Not appropriate for MakeGeneratorFrame");
55+
}
56+
5357
fn catch_exception(&mut self, _exception: &mut Val) {
5458
panic!("Not appropriate for MakeGeneratorFrame");
5559
}

valuescript_vm/src/stack_frame.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ pub trait StackFrameTrait {
2424
fn step(&mut self) -> FrameStepResult;
2525
fn apply_call_result(&mut self, call_result: CallResult);
2626
fn get_call_result(&mut self) -> CallResult;
27+
fn can_catch_exception(&self, exception: &Val) -> bool;
2728
fn catch_exception(&mut self, exception: &mut Val);
2829
fn clone_to_stack_frame(&self) -> StackFrame;
2930
}
@@ -63,6 +64,10 @@ impl StackFrameTrait for VoidStackFrame {
6364
}
6465
}
6566

67+
fn can_catch_exception(&self, _exception: &Val) -> bool {
68+
false
69+
}
70+
6671
fn catch_exception(&mut self, _exception: &mut Val) {}
6772

6873
fn clone_to_stack_frame(&self) -> StackFrame {

valuescript_vm/src/virtual_machine.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -119,9 +119,8 @@ impl VirtualMachine {
119119

120120
pub fn handle_exception(&mut self, mut exception: Val) -> Result<(), Val> {
121121
while !self.stack.is_empty() {
122-
self.frame.catch_exception(&mut exception);
123-
124-
if let Val::Void = exception {
122+
if self.frame.can_catch_exception(&exception) {
123+
self.frame.catch_exception(&mut exception);
125124
return Ok(());
126125
}
127126

0 commit comments

Comments
 (0)