From 6f39882f112bb2546472237bb4e9f80a0a621f1c Mon Sep 17 00:00:00 2001 From: Andreas Kling Date: Thu, 15 Jun 2023 19:13:00 +0200 Subject: [PATCH] LibJS/Bytecode: Fix multiple wrong jumps in ForStatement codegen --- .../Libraries/LibJS/Bytecode/ASTCodegen.cpp | 21 ++++++++++++------- 1 file changed, 13 insertions(+), 8 deletions(-) diff --git a/Userland/Libraries/LibJS/Bytecode/ASTCodegen.cpp b/Userland/Libraries/LibJS/Bytecode/ASTCodegen.cpp index 0e5769ab4a..2bcdf5125c 100644 --- a/Userland/Libraries/LibJS/Bytecode/ASTCodegen.cpp +++ b/Userland/Libraries/LibJS/Bytecode/ASTCodegen.cpp @@ -831,8 +831,16 @@ Bytecode::CodeGenerationErrorOr ForStatement::generate_labelled_evaluation Bytecode::Label { end_block }); } + if (m_update) { + generator.switch_to_basic_block(*update_block_ptr); + TRY(m_update->generate_bytecode(generator)); + generator.emit().set_targets( + Bytecode::Label { *test_block_ptr }, + {}); + } + generator.switch_to_basic_block(*body_block_ptr); - generator.begin_continuable_scope(Bytecode::Label { *update_block_ptr }, label_set); + generator.begin_continuable_scope(Bytecode::Label { m_update ? *update_block_ptr : *test_block_ptr }, label_set); generator.begin_breakable_scope(Bytecode::Label { end_block }, label_set); TRY(m_body->generate_bytecode(generator)); generator.end_breakable_scope(); @@ -843,14 +851,11 @@ Bytecode::CodeGenerationErrorOr ForStatement::generate_labelled_evaluation generator.emit().set_targets( Bytecode::Label { *update_block_ptr }, {}); - - generator.switch_to_basic_block(*update_block_ptr); - TRY(m_update->generate_bytecode(generator)); + } else { + generator.emit().set_targets( + Bytecode::Label { *test_block_ptr }, + {}); } - - generator.emit().set_targets( - Bytecode::Label { *test_block_ptr }, - {}); } generator.switch_to_basic_block(end_block);