From b5b2a5a1ad0d4515f2c04aa2c05a7d5f474ddf22 Mon Sep 17 00:00:00 2001 From: Haled Odat <8566042+HalidOdat@users.noreply.github.com> Date: Mon, 26 Jun 2023 09:13:34 +0200 Subject: [PATCH] Refactor environment, exception handling and jumping in VM --- boa_engine/src/bytecompiler/env.rs | 2 + boa_engine/src/bytecompiler/jump_control.rs | 292 +++++++++----- boa_engine/src/bytecompiler/mod.rs | 18 + .../src/bytecompiler/statement/break.rs | 89 ++--- .../src/bytecompiler/statement/continue.rs | 274 ++++++++----- .../src/bytecompiler/statement/labelled.rs | 1 - boa_engine/src/bytecompiler/statement/loop.rs | 26 +- .../src/bytecompiler/statement/switch.rs | 4 +- boa_engine/src/bytecompiler/statement/try.rs | 54 ++- boa_engine/src/vm/call_frame/abrupt_record.rs | 89 ----- boa_engine/src/vm/call_frame/env_stack.rs | 201 ---------- boa_engine/src/vm/call_frame/mod.rs | 37 +- boa_engine/src/vm/call_frame/try_stack.rs | 45 +++ boa_engine/src/vm/code_block.rs | 33 +- boa_engine/src/vm/flowgraph/mod.rs | 35 +- boa_engine/src/vm/mod.rs | 10 +- .../src/vm/opcode/control_flow/break.rs | 33 +- .../src/vm/opcode/control_flow/continue.rs | 43 +- .../src/vm/opcode/control_flow/finally.rs | 217 +++++----- .../src/vm/opcode/control_flow/labelled.rs | 24 +- .../src/vm/opcode/control_flow/return.rs | 56 +-- .../src/vm/opcode/control_flow/throw.rs | 374 ++++++++++-------- boa_engine/src/vm/opcode/control_flow/try.rs | 34 +- .../src/vm/opcode/iteration/loop_ops.rs | 62 +-- boa_engine/src/vm/opcode/jump/mod.rs | 37 +- boa_engine/src/vm/opcode/mod.rs | 48 ++- boa_engine/src/vm/opcode/pop/mod.rs | 1 - boa_engine/src/vm/opcode/push/environment.rs | 2 - test262 | 2 +- 29 files changed, 1005 insertions(+), 1138 deletions(-) delete mode 100644 boa_engine/src/vm/call_frame/abrupt_record.rs delete mode 100644 boa_engine/src/vm/call_frame/env_stack.rs create mode 100644 boa_engine/src/vm/call_frame/try_stack.rs diff --git a/boa_engine/src/bytecompiler/env.rs b/boa_engine/src/bytecompiler/env.rs index ffe025a94d3..7d189de9fe7 100644 --- a/boa_engine/src/bytecompiler/env.rs +++ b/boa_engine/src/bytecompiler/env.rs @@ -7,6 +7,7 @@ use boa_ast::expression::Identifier; impl ByteCompiler<'_, '_> { /// Push either a new declarative or function environment on the compile time environment stack. pub(crate) fn push_compile_environment(&mut self, function_scope: bool) { + self.current_open_environments_count += 1; self.current_environment = Rc::new(RefCell::new(CompileTimeEnvironment::new( self.current_environment.clone(), function_scope, @@ -16,6 +17,7 @@ impl ByteCompiler<'_, '_> { /// Pops the top compile time environment and returns its index in the compile time environments array. #[track_caller] pub(crate) fn pop_compile_environment(&mut self) -> u32 { + self.current_open_environments_count -= 1; let index = self.compile_environments.len() as u32; self.compile_environments .push(self.current_environment.clone()); diff --git a/boa_engine/src/bytecompiler/jump_control.rs b/boa_engine/src/bytecompiler/jump_control.rs index 0c86fbd0ba4..a58e29853b1 100644 --- a/boa_engine/src/bytecompiler/jump_control.rs +++ b/boa_engine/src/bytecompiler/jump_control.rs @@ -9,19 +9,59 @@ //! [try spec]: https://tc39.es/ecma262/#sec-try-statement //! [labelled spec]: https://tc39.es/ecma262/#sec-labelled-statements -use crate::bytecompiler::{ByteCompiler, Label}; +use crate::{ + bytecompiler::{ByteCompiler, Label}, + vm::Opcode, +}; use bitflags::bitflags; use boa_interner::Sym; +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +pub(crate) enum JumpRecordKind { + Break, + Continue, +} + +#[derive(Debug, Clone, Copy)] +pub(crate) struct JumpRecord { + kind: JumpRecordKind, + label: Option, + address: Label, + target_index: Option, +} + +impl JumpRecord { + pub(crate) const fn new( + kind: JumpRecordKind, + label: Option, + address: Label, + target_index: Option, + ) -> Self { + Self { + kind, + label, + address, + target_index, + } + } + + pub(crate) const fn label(&self) -> Option { + self.label + } + + pub(crate) const fn target_index(&self) -> Option { + self.target_index + } +} + /// Boa's `ByteCompiler` jump information tracking struct. #[derive(Debug, Clone)] pub(crate) struct JumpControlInfo { label: Option, start_address: u32, flags: JumpControlInfoFlags, - set_jumps: Vec