Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Merged by Bors] - Labelled ByteCompiler Fix #2534

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 7 additions & 9 deletions boa_engine/src/bytecompiler/jump_control.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ bitflags! {
const LOOP = 0b0000_0001;
const SWITCH = 0b0000_0010;
const TRY_BLOCK = 0b0000_0100;
const LABELLED_BLOCK = 0b0000_1000;
const LABELLED = 0b0000_1000;
const IN_CATCH = 0b0001_0000;
const HAS_FINALLY = 0b0010_0000;
const FOR_OF_IN_LOOP = 0b0100_0000;
Expand Down Expand Up @@ -91,7 +91,7 @@ impl JumpControlInfo {
}

pub(crate) fn with_labelled_block_flag(mut self, value: bool) -> Self {
self.flags.set(JumpControlInfoFlags::LABELLED_BLOCK, value);
self.flags.set(JumpControlInfoFlags::LABELLED, value);
self
}

Expand Down Expand Up @@ -129,8 +129,8 @@ impl JumpControlInfo {
self.flags.contains(JumpControlInfoFlags::TRY_BLOCK)
}

pub(crate) const fn is_labelled_block(&self) -> bool {
self.flags.contains(JumpControlInfoFlags::LABELLED_BLOCK)
pub(crate) const fn is_labelled(&self) -> bool {
self.flags.contains(JumpControlInfoFlags::LABELLED)
}

pub(crate) const fn in_catch(&self) -> bool {
Expand Down Expand Up @@ -363,20 +363,18 @@ impl ByteCompiler<'_, '_> {
}
}

pub(crate) fn push_labelled_block_control_info(&mut self, label: Sym, start_address: u32) {
pub(crate) fn push_labelled_control_info(&mut self, label: Sym, start_address: u32) {
let new_info = JumpControlInfo::default()
.with_labelled_block_flag(true)
.with_label(Some(label))
.with_start_address(start_address);
self.jump_info.push(new_info);
}

pub(crate) fn pop_labelled_block_control_info(&mut self) {
pub(crate) fn pop_labelled_control_info(&mut self) {
let info = self.jump_info.pop().expect("no jump information found");

assert!(info.is_labelled_block());

self.emit_opcode(Opcode::PopEnvironment);
assert!(info.is_labelled());

for label in info.breaks {
self.patch_jump(label);
Expand Down
13 changes: 1 addition & 12 deletions boa_engine/src/bytecompiler/statement/block.rs
Original file line number Diff line number Diff line change
@@ -1,22 +1,15 @@
use crate::{bytecompiler::ByteCompiler, JsResult};

use boa_ast::statement::Block;
use boa_interner::Sym;

impl ByteCompiler<'_, '_> {
/// Compile a [`Block`] `boa_ast` node
pub(crate) fn compile_block(
&mut self,
block: &Block,
label: Option<Sym>,
use_expr: bool,
configurable_globals: bool,
) -> JsResult<()> {
if let Some(label) = label {
let next = self.next_opcode_location();
self.push_labelled_block_control_info(label, next);
}

self.context.push_compile_time_environment(false);
let push_env = self.emit_and_track_decl_env();

Expand All @@ -28,11 +21,7 @@ impl ByteCompiler<'_, '_> {
self.patch_jump_with_target(push_env.0, num_bindings as u32);
self.patch_jump_with_target(push_env.1, index_compile_environment as u32);

if label.is_some() {
self.pop_labelled_block_control_info();
} else {
self.emit_and_track_pop_env();
}
self.emit_and_track_pop_env();

Ok(())
}
Expand Down
13 changes: 5 additions & 8 deletions boa_engine/src/bytecompiler/statement/labelled.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ impl ByteCompiler<'_, '_> {
use_expr: bool,
configurable_globals: bool,
) -> JsResult<()> {
let labelled_loc = self.next_opcode_location();
self.push_labelled_control_info(labelled.label(), labelled_loc);

match labelled.item() {
LabelledItem::Statement(stmt) => match stmt {
Statement::ForLoop(for_loop) => {
Expand Down Expand Up @@ -49,21 +52,15 @@ impl ByteCompiler<'_, '_> {
configurable_globals,
)?;
}
Statement::Block(block) => {
self.compile_block(
block,
Some(labelled.label()),
use_expr,
configurable_globals,
)?;
}
stmt => self.compile_stmt(stmt, use_expr, configurable_globals)?,
},
LabelledItem::Function(f) => {
self.function(f.into(), NodeKind::Declaration, false)?;
}
}

self.pop_labelled_control_info();

Ok(())
}
}
2 changes: 1 addition & 1 deletion boa_engine/src/bytecompiler/statement/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ impl ByteCompiler<'_, '_> {
self.compile_do_while_loop(do_while_loop, None, configurable_globals)?;
}
Statement::Block(block) => {
self.compile_block(block, None, use_expr, configurable_globals)?;
self.compile_block(block, use_expr, configurable_globals)?;
}
Statement::Labelled(labelled) => {
self.compile_labelled(labelled, use_expr, configurable_globals)?;
Expand Down
32 changes: 32 additions & 0 deletions boa_engine/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2198,6 +2198,38 @@ fn break_environment_gauntlet() {
assert_eq!(&exec(scenario), "\"5601try_block\"");
}

#[test]
fn break_labelled_if_statement() {
let scenario = r#"
let result = "";
bar: if(true) {
result = "foo";
break bar;
result = 'this will not be executed';
}
result
"#;

assert_eq!(&exec(scenario), "\"foo\"");
}

#[test]
fn break_labelled_try_statement() {
let scenario = r#"
let result = ""
one: try {
result = "foo";
break one;
result = "did not break"
} catch (err) {
console.log(err)
}
result
"#;

assert_eq!(&exec(scenario), "\"foo\"");
}

#[test]
fn while_loop_late_break() {
// Ordering with statement before the break.
Expand Down