Browse Source

Add assertion to check that a break label is identified at compile-time (#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.
pull/1855/head
Addison Crump 3 years ago
parent
commit
1d2851465f
  1. 23
      boa/src/bytecompiler.rs
  2. 12
      boa/src/tests.rs

23
boa/src/bytecompiler.rs

@ -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) => {

12
boa/src/tests.rs

@ -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#"

Loading…
Cancel
Save