Skip to content

Commit

Permalink
Continue panic fixes (#1896)
Browse files Browse the repository at this point in the history
This PR changes the following:

- Fixes the panics induced by incorrect continues.
- Adds tests which demonstrate the various panics induced.
- Actually rustfmts correctly?
  • Loading branch information
addisoncrump committed Mar 6, 2022
1 parent 7fa37b5 commit cc755db
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 4 deletions.
21 changes: 17 additions & 4 deletions boa_engine/src/bytecompiler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1489,9 +1489,7 @@ impl<'b> ByteCompiler<'b> {
.iter()
.rev()
.filter(|info| info.kind == JumpControlInfoKind::Loop);
let address = if node.label().is_none() {
items.next().expect("continue target").start_address
} else {
let address = if let Some(label_name) = node.label() {
let mut num_loops = 0;
let mut emit_for_of_in_exit = 0;
let mut address_info = None;
Expand All @@ -1505,7 +1503,14 @@ impl<'b> ByteCompiler<'b> {
emit_for_of_in_exit += 1;
}
}
let address = address_info.expect("continue target").start_address;
let address = address_info
.ok_or_else(|| {
self.context.construct_syntax_error(format!(
"Cannot use the undeclared label '{}'",
self.context.interner().resolve_expect(label_name)
))
})?
.start_address;
for _ in 0..emit_for_of_in_exit {
self.emit_opcode(Opcode::Pop);
self.emit_opcode(Opcode::Pop);
Expand All @@ -1514,6 +1519,14 @@ impl<'b> ByteCompiler<'b> {
self.emit_opcode(Opcode::LoopEnd);
}
address
} else {
items
.next()
.ok_or_else(|| {
self.context
.construct_syntax_error("continue must be inside loop")
})?
.start_address
};
self.emit_opcode(Opcode::LoopEnd);
self.emit_opcode(Opcode::LoopStart);
Expand Down
25 changes: 25 additions & 0 deletions boa_engine/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -442,6 +442,31 @@ fn test_invalid_break_target() {
assert!(Context::default().eval(src).is_err());
}

#[test]
fn test_invalid_continue_target() {
let mut context = Context::default();
let src = r#"
while (false) {
continue nonexistent;
}
"#;
let string = forward(&mut context, src);
assert_eq!(
string,
"Uncaught \"SyntaxError\": \"Cannot use the undeclared label 'nonexistent'\""
);
}

#[test]
fn test_invalid_continue() {
let mut context = Context::default();
let string = forward(&mut context, r"continue;");
assert_eq!(
string,
"Uncaught \"SyntaxError\": \"continue must be inside loop\""
);
}

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

0 comments on commit cc755db

Please sign in to comment.