Truly independent web browser
LibJS: Copy per-iteration bindings when a for body continues
Shannon Booth52024f76099d
diff --git a/Libraries/LibJS/Rust/src/bytecode/codegen.rs b/Libraries/LibJS/Rust/src/bytecode/codegen.rsindex 2b22512c177..dc80d766409 100644--- a/Libraries/LibJS/Rust/src/bytecode/codegen.rs+++ b/Libraries/LibJS/Rust/src/bytecode/codegen.rs@@ -2752,10 +2752,16 @@ fn generate_for_statement_without_disposal(// CreatePerIterationEnvironment after init (first iteration setup).emit_per_iteration_bindings(generator, &per_iteration_binding_names);- // Block creation order: body → update (if exists) → test (if exists) → end.+ // Block creation order: body → per-iteration (if needed) → update (if exists) → test (if exists) → end.// If 'test' is missing, fuse 'test' and 'body' blocks.// If 'update' is missing, fuse 'body' and 'update' blocks.let body_block = generator.make_block();+ // `continue` targets this block too, so CreatePerIterationEnvironment runs before every update.+ let per_iteration_block = if per_iteration_binding_names.is_empty() {+ None+ } else {+ Some(generator.make_block())+ };let update_block = if update.is_some() {generator.make_block()} else {@@ -2809,8 +2815,9 @@ fn generate_for_statement_without_disposal(// Bodygenerator.switch_to_basic_block(body_block);let labels = std::mem::take(&mut generator.pending_labels);- let continue_target = if update.is_some() { update_block } else { test_block };- generator.begin_continuable_scope(continue_target, labels.clone(), completion.clone());+ let next_iteration_block = if update.is_some() { update_block } else { test_block };+ let continue_block = per_iteration_block.unwrap_or(next_iteration_block);+ generator.begin_continuable_scope(continue_block, labels.clone(), completion.clone());generator.begin_breakable_scope(end_block, labels, completion.clone());generate_with_completion(body, generator, completion.as_ref(), preferred_dst);@@ -2818,13 +2825,15 @@ fn generate_for_statement_without_disposal(generator.end_breakable_scope();generator.end_continuable_scope();if !generator.is_current_block_terminated() {- // CreatePerIterationEnvironment at end of each iteration.+ generator.emit(Instruction::Jump { target: continue_block });+ }++ if let Some(per_iteration_block) = per_iteration_block {+ generator.switch_to_basic_block(per_iteration_block);emit_per_iteration_bindings(generator, &per_iteration_binding_names);- if update.is_some() {- generator.emit(Instruction::Jump { target: update_block });- } else {- generator.emit(Instruction::Jump { target: test_block });- }+ generator.emit(Instruction::Jump {+ target: next_iteration_block,+ });}generator.switch_to_basic_block(end_block);diff --git a/Tests/LibJS/Runtime/loops/for-scopes.js b/Tests/LibJS/Runtime/loops/for-scopes.jsindex d3a7e645c84..b506a83dca9 100644--- a/Tests/LibJS/Runtime/loops/for-scopes.js+++ b/Tests/LibJS/Runtime/loops/for-scopes.js@@ -32,3 +32,28 @@ test("let variables captured by value", () => {expect(result).toEqual("012345678012345678");});++test("let variables captured by value when the body continues", () => {+ const functions = [];+ for (let i = 0; i < 3; i++) {+ functions.push(() => i);+ continue;+ }+ expect(functions.map(f => f())).toEqual([0, 1, 2]);++ functions.length = 0;+ for (let i = 0; i < 4; i++) {+ functions.push(() => i);+ if (i % 2 === 0) continue;+ }+ expect(functions.map(f => f())).toEqual([0, 1, 2, 3]);++ functions.length = 0;+ outer: for (let i = 0; i < 3; i++) {+ for (;;) {+ functions.push(() => i);+ continue outer;+ }+ }+ expect(functions.map(f => f())).toEqual([0, 1, 2]);+});