浏览代码

LibJS/Bytecode: Fix multiple wrong jumps in ForStatement codegen

Andreas Kling 2 年之前
父节点
当前提交
6f39882f11
共有 1 个文件被更改,包括 13 次插入8 次删除
  1. 13 8
      Userland/Libraries/LibJS/Bytecode/ASTCodegen.cpp

+ 13 - 8
Userland/Libraries/LibJS/Bytecode/ASTCodegen.cpp

@@ -831,8 +831,16 @@ Bytecode::CodeGenerationErrorOr<void> ForStatement::generate_labelled_evaluation
             Bytecode::Label { end_block });
             Bytecode::Label { end_block });
     }
     }
 
 
+    if (m_update) {
+        generator.switch_to_basic_block(*update_block_ptr);
+        TRY(m_update->generate_bytecode(generator));
+        generator.emit<Bytecode::Op::Jump>().set_targets(
+            Bytecode::Label { *test_block_ptr },
+            {});
+    }
+
     generator.switch_to_basic_block(*body_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);
     generator.begin_breakable_scope(Bytecode::Label { end_block }, label_set);
     TRY(m_body->generate_bytecode(generator));
     TRY(m_body->generate_bytecode(generator));
     generator.end_breakable_scope();
     generator.end_breakable_scope();
@@ -843,14 +851,11 @@ Bytecode::CodeGenerationErrorOr<void> ForStatement::generate_labelled_evaluation
             generator.emit<Bytecode::Op::Jump>().set_targets(
             generator.emit<Bytecode::Op::Jump>().set_targets(
                 Bytecode::Label { *update_block_ptr },
                 Bytecode::Label { *update_block_ptr },
                 {});
                 {});
-
-            generator.switch_to_basic_block(*update_block_ptr);
-            TRY(m_update->generate_bytecode(generator));
+        } else {
+            generator.emit<Bytecode::Op::Jump>().set_targets(
+                Bytecode::Label { *test_block_ptr },
+                {});
         }
         }
-
-        generator.emit<Bytecode::Op::Jump>().set_targets(
-            Bytecode::Label { *test_block_ptr },
-            {});
     }
     }
 
 
     generator.switch_to_basic_block(end_block);
     generator.switch_to_basic_block(end_block);