Browse Source

Fix panic in do while (#1968)

`Node::DoWhileLoop` ast node had a buggy bytecode generation where `self.patch_jump(exit)` was called after emitting `LoopEnd` opcode. This would patch the loop exit to the instruction following the do while code, which would panic in cases where do while was enclosed in a block statement.

This Pull Request fixes #1929.

It changes the following:
- Patch jump before emitting `Opcode::LoopEnd`
- Add test which has do while statement inside a block statement to demonstrate that the change fixes the panic.
pull/1971/head
pd 3 years ago
parent
commit
e2630faf82
  1. 3
      boa_engine/src/bytecompiler.rs
  2. 15
      boa_engine/src/tests.rs

3
boa_engine/src/bytecompiler.rs

@ -1472,11 +1472,10 @@ impl<'b> ByteCompiler<'b> {
self.compile_stmt(do_while.body(), false)?; self.compile_stmt(do_while.body(), false)?;
self.emit(Opcode::Jump, &[condition_label_address]); self.emit(Opcode::Jump, &[condition_label_address]);
self.patch_jump(exit);
self.pop_loop_control_info(); self.pop_loop_control_info();
self.emit_opcode(Opcode::LoopEnd); self.emit_opcode(Opcode::LoopEnd);
self.patch_jump(exit);
} }
Node::Continue(node) => { Node::Continue(node) => {
let next = self.next_opcode_location(); let next = self.next_opcode_location();

15
boa_engine/src/tests.rs

@ -381,6 +381,21 @@ fn do_while_post_inc() {
assert_eq!(&exec(with_post_incrementors), "11"); assert_eq!(&exec(with_post_incrementors), "11");
} }
#[test]
fn do_while_in_block() {
let in_block = r#"
{
var i = 0;
do {
i += 1;
}
while(false);
i;
}
"#;
assert_eq!(&exec(in_block), "1");
}
#[test] #[test]
fn for_loop() { fn for_loop() {
let simple = r#" let simple = r#"

Loading…
Cancel
Save