diff --git a/boa_engine/src/bytecompiler.rs b/boa_engine/src/bytecompiler.rs index 6e04cfc39f..f950c47826 100644 --- a/boa_engine/src/bytecompiler.rs +++ b/boa_engine/src/bytecompiler.rs @@ -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; @@ -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); @@ -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); diff --git a/boa_engine/src/tests.rs b/boa_engine/src/tests.rs index 494f1ca388..a8ee0730b3 100644 --- a/boa_engine/src/tests.rs +++ b/boa_engine/src/tests.rs @@ -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#"