Skip to content

Commit a188b49

Browse files
committed
Add MaybeException opcode and remove handler check
1 parent e2a0b6d commit a188b49

File tree

5 files changed

+39
-21
lines changed

5 files changed

+39
-21
lines changed

boa_engine/src/bytecompiler/declaration/declaration_pattern.rs

Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -180,26 +180,15 @@ impl ByteCompiler<'_, '_> {
180180
self.emit_opcode(Opcode::ValueNotNullOrUndefined);
181181
self.emit_opcode(Opcode::GetIterator);
182182

183-
// TODO: maybe, refactor this to be more condensed.
184183
let handler_index = self.push_handler();
185184
for element in pattern.bindings() {
186185
self.compile_array_pattern_element(element, def);
187186
}
188187

189-
self.emit_opcode(Opcode::PushFalse);
190-
191-
let exit = self.jump();
192188
self.patch_handler(handler_index);
193-
let generator_return_handle = self.in_generator.then(|| self.push_handler());
194-
self.emit_opcode(Opcode::Exception);
195-
self.emit_opcode(Opcode::PushTrue);
196-
if let Some(generator_return_handle) = generator_return_handle {
197-
let is_not_generator_exit = self.jump();
198-
self.patch_handler(generator_return_handle);
199-
self.emit_opcode(Opcode::PushFalse);
200-
self.patch_jump(is_not_generator_exit);
201-
}
202-
self.patch_jump(exit);
189+
self.emit_opcode(Opcode::MaybeException);
190+
191+
// stack: hasPending, exception?
203192

204193
self.current_stack_value_count += 2;
205194

boa_engine/src/vm/code_block.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -566,6 +566,7 @@ impl CodeBlock {
566566
| Opcode::Throw
567567
| Opcode::ReThrow
568568
| Opcode::Exception
569+
| Opcode::MaybeException
569570
| Opcode::This
570571
| Opcode::Super
571572
| Opcode::Return
@@ -677,8 +678,7 @@ impl CodeBlock {
677678
| Opcode::Reserved60
678679
| Opcode::Reserved61
679680
| Opcode::Reserved62
680-
| Opcode::Reserved63
681-
| Opcode::Reserved64 => unreachable!("Reserved opcodes are unrechable"),
681+
| Opcode::Reserved63 => unreachable!("Reserved opcodes are unrechable"),
682682
}
683683
}
684684
}

boa_engine/src/vm/flowgraph/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -537,6 +537,7 @@ impl CodeBlock {
537537
| Opcode::GetReturnValue
538538
| Opcode::SetReturnValue
539539
| Opcode::Exception
540+
| Opcode::MaybeException
540541
| Opcode::Nop => {
541542
graph.add_node(previous_pc, NodeShape::None, label.into(), Color::None);
542543
graph.add_edge(previous_pc, pc, None, Color::None, EdgeStyle::Line);
@@ -606,8 +607,7 @@ impl CodeBlock {
606607
| Opcode::Reserved60
607608
| Opcode::Reserved61
608609
| Opcode::Reserved62
609-
| Opcode::Reserved63
610-
| Opcode::Reserved64 => unreachable!("Reserved opcodes are unrechable"),
610+
| Opcode::Reserved63 => unreachable!("Reserved opcodes are unrechable"),
611611
}
612612
}
613613

boa_engine/src/vm/opcode/control_flow/throw.rs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,30 @@ impl Operation for Exception {
8686
}
8787
}
8888

89+
/// `MaybeException` implements the Opcode Operation for `Opcode::MaybeException`
90+
///
91+
/// Operation:
92+
/// - Get the thrown pending exception if it's set and push `true`, otherwise push only `false`.
93+
#[derive(Debug, Clone, Copy)]
94+
pub(crate) struct MaybeException;
95+
96+
impl Operation for MaybeException {
97+
const NAME: &'static str = "MaybeException";
98+
const INSTRUCTION: &'static str = "INST - MaybeException";
99+
100+
fn execute(context: &mut Context<'_>) -> JsResult<CompletionType> {
101+
if let Some(error) = context.vm.pending_exception.take() {
102+
let error = error.to_opaque(context);
103+
context.vm.push(error);
104+
context.vm.push(true);
105+
return Ok(CompletionType::Normal);
106+
}
107+
108+
context.vm.push(false);
109+
Ok(CompletionType::Normal)
110+
}
111+
}
112+
89113
/// `ThrowNewTypeError` implements the Opcode Operation for `Opcode::ThrowNewTypeError`
90114
///
91115
/// Operation:

boa_engine/src/vm/opcode/mod.rs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1147,7 +1147,7 @@ generate_impl! {
11471147
/// Stack: **=>**
11481148
ReThrow,
11491149

1150-
/// Get the thrown pending exception and push on the stack.
1150+
/// Get the thrown pending exception (if it's set) and push on the stack.
11511151
///
11521152
/// If there is no pending exception, which can happend if we are handling `return()` call on generator,
11531153
/// then we rethrow the empty exception. See [`Opcode::ReThrow`].
@@ -1157,6 +1157,13 @@ generate_impl! {
11571157
/// Stack: **=>** exception
11581158
Exception,
11591159

1160+
/// Get the thrown pending exception if it's set and push `true`, otherwise push only `false`.
1161+
///
1162+
/// Operands:
1163+
///
1164+
/// Stack: **=>** (`true`, exception) or `false`
1165+
MaybeException,
1166+
11601167
/// Throw a new `TypeError` exception
11611168
///
11621169
/// Operands: message: u32
@@ -1764,8 +1771,6 @@ generate_impl! {
17641771
Reserved62 => Reserved,
17651772
/// Reserved [`Opcode`].
17661773
Reserved63 => Reserved,
1767-
/// Reserved [`Opcode`].
1768-
Reserved64 => Reserved,
17691774
}
17701775
}
17711776

0 commit comments

Comments
 (0)