From 919272e0856e6871b5a8e2f667f87b5232d6b4fd Mon Sep 17 00:00:00 2001 From: pd Date: Tue, 22 Mar 2022 19:28:14 +0000 Subject: [PATCH] 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. --- boa_engine/src/bytecompiler.rs | 3 +-- boa_engine/src/tests.rs | 15 +++++++++++++++ 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/boa_engine/src/bytecompiler.rs b/boa_engine/src/bytecompiler.rs index 3708c1bd810..88ee542b219 100644 --- a/boa_engine/src/bytecompiler.rs +++ b/boa_engine/src/bytecompiler.rs @@ -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(); diff --git a/boa_engine/src/tests.rs b/boa_engine/src/tests.rs index 5500d3d34e6..e5e8d9dcbec 100644 --- a/boa_engine/src/tests.rs +++ b/boa_engine/src/tests.rs @@ -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#"