Skip to content

Commit

Permalink
Fix panic in do while (#1968)
Browse files Browse the repository at this point in the history
 `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.
  • Loading branch information
pdogr authored and Razican committed Jun 8, 2022
1 parent 7611276 commit 919272e
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 2 deletions.
3 changes: 1 addition & 2 deletions boa_engine/src/bytecompiler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1472,11 +1472,10 @@ impl<'b> ByteCompiler<'b> {

self.compile_stmt(do_while.body(), false)?;
self.emit(Opcode::Jump, &[condition_label_address]);
self.patch_jump(exit);

self.pop_loop_control_info();
self.emit_opcode(Opcode::LoopEnd);

self.patch_jump(exit);
}
Node::Continue(node) => {
let next = self.next_opcode_location();
Expand Down
15 changes: 15 additions & 0 deletions boa_engine/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -381,6 +381,21 @@ fn do_while_post_inc() {
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]
fn for_loop() {
let simple = r#"
Expand Down

0 comments on commit 919272e

Please sign in to comment.