Skip to content

Commit

Permalink
Use start and end field instead of range for handle
Browse files Browse the repository at this point in the history
  • Loading branch information
HalidOdat committed Jul 20, 2023
1 parent 4c02568 commit 1684049
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ impl ByteCompiler<'_, '_> {
self.emit_opcode(Opcode::PushTrue);
self.patch_jump(exit);

self.handlers[handler_index as usize].range.end = handler_address;
self.handlers[handler_index as usize].end = handler_address;

let iterator_close_handler = self.push_handler();
self.iterator_close(false);
Expand All @@ -213,8 +213,7 @@ impl ByteCompiler<'_, '_> {
self.emit_opcode(Opcode::Throw);
self.patch_jump(jump);

self.handlers[iterator_close_handler as usize].range.end =
iterator_close_handler_address;
self.handlers[iterator_close_handler as usize].end = iterator_close_handler_address;
}
}
}
Expand Down
7 changes: 4 additions & 3 deletions boa_engine/src/bytecompiler/jump_control.rs
Original file line number Diff line number Diff line change
Expand Up @@ -299,7 +299,8 @@ impl ByteCompiler<'_, '_> {
// FIXME(HalidOdat): figure out value stack fp value.
let env_fp = self.current_open_environments_count;
self.handlers.push(Handler {
range: (start_address..u32::MAX),
start: start_address,
end: u32::MAX,
fp: 0,
env_fp,
});
Expand All @@ -318,9 +319,9 @@ impl ByteCompiler<'_, '_> {
.handler_index
.expect("try jump info must have handler index");

assert!(handler_address >= self.handlers[handler_index as usize].range.start);
assert!(handler_address >= self.handlers[handler_index as usize].start);

self.handlers[handler_index as usize].range.end = handler_address;
self.handlers[handler_index as usize].end = handler_address;
}

/// Does the jump control info have the `use_expr` flag set to true.
Expand Down
4 changes: 2 additions & 2 deletions boa_engine/src/bytecompiler/statement/loop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -378,10 +378,10 @@ impl ByteCompiler<'_, '_> {
let handler_index = self.push_handler();
self.iterator_close(for_of_loop.r#await());
let end_address = self.next_opcode_location();
self.handlers[handler_index as usize].range.end = end_address;
self.handlers[handler_index as usize].end = end_address;
}

self.handlers[handler_index as usize].range.end = handler_address;
self.handlers[handler_index as usize].end = handler_address;

self.emit_opcode(Opcode::Throw);
self.patch_jump(exit);
Expand Down
31 changes: 22 additions & 9 deletions boa_engine/src/vm/code_block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,8 @@ unsafe impl Trace for CodeBlockFlags {
#[allow(dead_code)]
#[derive(Debug, Clone)]
pub(crate) struct Handler {
pub(crate) range: std::ops::Range<u32>,
pub(crate) start: u32,
pub(crate) end: u32,

pub(crate) fp: u32,
pub(crate) env_fp: u32,
Expand All @@ -99,7 +100,12 @@ pub(crate) struct Handler {
impl Handler {
/// Get the handler address.
pub(crate) const fn handler(&self) -> u32 {
self.range.end
self.end
}

/// Check if the provided `pc` is contained in the handler range.
pub(crate) const fn contains(&self, pc: u32) -> bool {
pc < self.end && pc >= self.start
}
}

Expand Down Expand Up @@ -243,7 +249,7 @@ impl CodeBlock {
self.handlers
.iter()
.rev()
.find(|&handler| handler.range.contains(&pc))
.find(|handler| handler.contains(pc))
}
}

Expand Down Expand Up @@ -687,14 +693,17 @@ impl ToInternedString for CodeBlock {
let mut count = 0;
while pc < self.bytecode.len() {
for (i, handler) in self.handlers.iter().enumerate().rev() {
if pc == handler.range.start as usize {
f.push_str(&format!("\n#{i} Handler Start\n"));
if pc == handler.end as usize {
f.push_str(&format!("#{i} Handler End\n\n",));
}
}

if pc == handler.range.end as usize {
f.push_str(&format!("#{i} Handler End\n\n",));
for (i, handler) in self.handlers.iter().enumerate().rev() {
if pc == handler.start as usize {
f.push_str(&format!("\n#{i} Handler Start\n"));
}
}

let opcode: Opcode = self.bytecode[pc].into();
let opcode = opcode.as_str();
let previous_pc = pc;
Expand Down Expand Up @@ -750,8 +759,12 @@ impl ToInternedString for CodeBlock {
} else {
for (i, handler) in self.handlers.iter().enumerate() {
f.push_str(&format!(
" {i:04}: ({}..{}): handler: {}, env_fp: {}\n",
handler.range.start, handler.range.end, handler.range.end, handler.env_fp,
" {i:04}: Range: [{}, {}): Handler: {}, Stack FP {}, Env FP: {}\n",
handler.start,
handler.end,
handler.handler(),
handler.fp,
handler.env_fp,
));
}
}
Expand Down

0 comments on commit 1684049

Please sign in to comment.