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] - Fix labelled block statement #2285

Closed
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
41 changes: 41 additions & 0 deletions boa_engine/src/bytecompiler/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ enum JumpControlInfoKind {
Loop,
Switch,
Try,
LabelledBlock,
}

#[derive(Debug, Clone, Copy)]
Expand Down Expand Up @@ -491,6 +492,36 @@ impl<'b> ByteCompiler<'b> {
}
}

#[inline]
fn push_labelled_block_control_info(&mut self, label: Sym, start_address: u32) {
self.jump_info.push(JumpControlInfo {
label: Some(label),
start_address,
kind: JumpControlInfoKind::LabelledBlock,
breaks: Vec::new(),
try_continues: Vec::new(),
in_catch: false,
has_finally: false,
finally_start: None,
for_of_in_loop: false,
});
}

#[inline]
fn pop_labelled_block_control_info(&mut self) {
let info = self.jump_info.pop().expect("no jump information found");

assert!(info.kind == JumpControlInfoKind::LabelledBlock);

for label in info.breaks {
self.patch_jump(label);
}

for label in info.try_continues {
self.patch_jump_with_target(label, info.start_address);
}
}

#[inline]
fn compile_access(node: &Node) -> Option<Access<'_>> {
match node {
Expand Down Expand Up @@ -1793,6 +1824,11 @@ impl<'b> ByteCompiler<'b> {
}
}
Node::Block(block) => {
if let Some(label) = block.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_opcode_with_two_operands(Opcode::PushDeclarativeEnvironment);
Expand All @@ -1803,6 +1839,11 @@ impl<'b> ByteCompiler<'b> {
let index_compile_environment = self.push_compile_environment(compile_environment);
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 block.label().is_some() {
self.pop_labelled_block_control_info();
}

self.emit_opcode(Opcode::PopEnvironment);
}
Node::Throw(throw) => {
Expand Down
11 changes: 10 additions & 1 deletion boa_engine/src/syntax/ast/node/block/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,11 @@ mod tests;
/// [spec]: https://tc39.es/ecma262/#prod-BlockStatement
/// [mdn]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/block
#[cfg_attr(feature = "deser", derive(Serialize, Deserialize))]
#[cfg_attr(feature = "deser", serde(transparent))]
#[derive(Clone, Debug, PartialEq)]
pub struct Block {
#[cfg_attr(feature = "deser", serde(flatten))]
statements: StatementList,
label: Option<Sym>,
}

impl Block {
Expand All @@ -52,6 +52,14 @@ impl Block {
" ".repeat(indentation)
)
}

pub fn label(&self) -> Option<Sym> {
self.label
}

pub fn set_label(&mut self, label: Sym) {
self.label = Some(label);
}
}

impl<T> From<T> for Block
Expand All @@ -61,6 +69,7 @@ where
fn from(list: T) -> Self {
Self {
statements: list.into(),
label: None,
}
}
}
Expand Down
1 change: 1 addition & 0 deletions boa_engine/src/syntax/parser/statement/labelled_stm/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ fn set_label_for_node(node: &mut Node, name: Sym) {
Node::ForInLoop(ref mut for_in_loop) => for_in_loop.set_label(name),
Node::DoWhileLoop(ref mut do_while_loop) => do_while_loop.set_label(name),
Node::WhileLoop(ref mut while_loop) => while_loop.set_label(name),
Node::Block(ref mut block) => block.set_label(name),
_ => (),
}
}
19 changes: 19 additions & 0 deletions boa_engine/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1632,3 +1632,22 @@ fn test_empty_statement() {
"#;
assert_eq!(&exec(src), "10");
}

#[test]
fn test_labelled_block() {
let src = r#"
let result = true;
{
let x = 2;
L: {
let x = 3;
result &&= (x === 3);
break L;
result &&= (false);
}
result &&= (x === 2);
}
result;
"#;
assert_eq!(&exec(src), "true");
}