Browse Source

Continue panic fixes (#1896)

This PR changes the following:

- Fixes the panics induced by incorrect continues.
- Adds tests which demonstrate the various panics induced.
- Actually rustfmts correctly?
pull/1904/head
Addison Crump 3 years ago
parent
commit
cc755db485
  1. 21
      boa_engine/src/bytecompiler.rs
  2. 25
      boa_engine/src/tests.rs

21
boa_engine/src/bytecompiler.rs

@ -1489,9 +1489,7 @@ impl<'b> ByteCompiler<'b> {
.iter() .iter()
.rev() .rev()
.filter(|info| info.kind == JumpControlInfoKind::Loop); .filter(|info| info.kind == JumpControlInfoKind::Loop);
let address = if node.label().is_none() { let address = if let Some(label_name) = node.label() {
items.next().expect("continue target").start_address
} else {
let mut num_loops = 0; let mut num_loops = 0;
let mut emit_for_of_in_exit = 0; let mut emit_for_of_in_exit = 0;
let mut address_info = None; let mut address_info = None;
@ -1505,7 +1503,14 @@ impl<'b> ByteCompiler<'b> {
emit_for_of_in_exit += 1; 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 { for _ in 0..emit_for_of_in_exit {
self.emit_opcode(Opcode::Pop); self.emit_opcode(Opcode::Pop);
self.emit_opcode(Opcode::Pop); self.emit_opcode(Opcode::Pop);
@ -1514,6 +1519,14 @@ impl<'b> ByteCompiler<'b> {
self.emit_opcode(Opcode::LoopEnd); self.emit_opcode(Opcode::LoopEnd);
} }
address 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::LoopEnd);
self.emit_opcode(Opcode::LoopStart); self.emit_opcode(Opcode::LoopStart);

25
boa_engine/src/tests.rs

@ -442,6 +442,31 @@ fn test_invalid_break_target() {
assert!(Context::default().eval(src).is_err()); 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] #[test]
fn unary_pre() { fn unary_pre() {
let unary_inc = r#" let unary_inc = r#"

Loading…
Cancel
Save