Skip to content

Commit

Permalink
Add assertion to check that a break label is identified at compile-ti…
Browse files Browse the repository at this point in the history
…me (#1852)

This PR changes the following:

- Adds a check at compile time for the existence of a break label (this should be a syntax error in the future; refactor from panics to results in compile should be a separate PR)
- Adds a test for break label existence in boa/tests

262 misses some fairly important JS parity issues and not performing this check eagerly can lead to other more severe issues during VM execution.
  • Loading branch information
addisoncrump committed Feb 21, 2022
1 parent 9f6aa19 commit 1d28514
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 8 deletions.
23 changes: 15 additions & 8 deletions boa/src/bytecompiler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1471,19 +1471,26 @@ impl<'b> ByteCompiler<'b> {
self.emit(Opcode::FinallySetJump, &[u32::MAX]);
}
let label = self.jump();
if node.label().is_none() {
self.jump_info
.last_mut()
.expect("no jump information found")
.breaks
.push(label);
} else {
if let Some(label_name) = node.label() {
let mut found = false;
for info in self.jump_info.iter_mut().rev() {
if info.label == node.label() {
if info.label == Some(label_name) {
info.breaks.push(label);
found = true;
break;
}
}
assert!(
found,
"Undefined label '{}'",
self.interner().resolve_expect(label_name)
);
} else {
self.jump_info
.last_mut()
.expect("no jump information found")
.breaks
.push(label);
}
}
Node::Block(block) => {
Expand Down
12 changes: 12 additions & 0 deletions boa/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -430,6 +430,18 @@ fn for_loop_iteration_variable_does_not_leak() {
assert_eq!(&exec(inner_scope), "\"i is not defined\"");
}

#[test]
#[should_panic]
fn test_invalid_break_target() {
let src = r#"
while (false) {
break nonexistent;
}
"#;

let _ = &exec(src);
}

#[test]
fn unary_pre() {
let unary_inc = r#"
Expand Down

0 comments on commit 1d28514

Please sign in to comment.