Skip to content

Commit

Permalink
Fix incorrect LoopContinue instruction in while-do loops (#2866)
Browse files Browse the repository at this point in the history
While working on #2857 I discovered that while generating the bytecode for `do-while` loops we were emitting an orphan `LoopContinue` that is never executed, which was preventing the error to be thrown when max loop iteration is reached.

This can more easily be identified with it's flowgraph : (`do { 1; } while(0) `)

Main:

<details>
<img src="https://user-images.githubusercontent.com/8566042/233908011-247313bc-6435-4622-8ecb-f469d9eaf850.png">
</details>

With this PR:

<details>
<img src="https://user-images.githubusercontent.com/8566042/233908030-3552636e-f09c-4c5e-8c7c-1ecfa0024dfe.png">
</details>
  • Loading branch information
HalidOdat committed Apr 24, 2023
1 parent 7d2be7e commit 41448e1
Showing 1 changed file with 3 additions and 3 deletions.
6 changes: 3 additions & 3 deletions boa_engine/src/bytecompiler/statement/loop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -392,13 +392,13 @@ impl ByteCompiler<'_, '_> {
let initial_label = self.jump();

let start_address = self.next_opcode_location();
let (continue_start, continue_exit) =
self.emit_opcode_with_two_operands(Opcode::LoopContinue);
self.patch_jump_with_target(continue_start, start_address);
self.patch_jump_with_target(loop_start, start_address);
self.push_loop_control_info(label, start_address);

let condition_label_address = self.next_opcode_location();
let (continue_start, continue_exit) =
self.emit_opcode_with_two_operands(Opcode::LoopContinue);
self.patch_jump_with_target(continue_start, start_address);
self.compile_expr(do_while_loop.cond(), true);
let exit = self.jump_if_false();

Expand Down

0 comments on commit 41448e1

Please sign in to comment.