Skip to content

Truly independent web browser

LibJS: Snapshot a statement's completion value read from a local

Shannon Booth39622415543e
diff --git a/Libraries/LibJS/Rust/src/bytecode/codegen.rs b/Libraries/LibJS/Rust/src/bytecode/codegen.rs
index 89b4f6dacad..7e2f4326719 100644
--- a/Libraries/LibJS/Rust/src/bytecode/codegen.rs
+++ b/Libraries/LibJS/Rust/src/bytecode/codegen.rs
@@ -2923,7 +2923,9 @@ fn generate_scope_children(
if generator.must_propagate_completion
&& let Some(ref val) = result
{
- last_result = result.clone();
+ // A later statement that completes empty can still reassign a local the value is read from.
+ let val = &generator.copy_if_needed_to_preserve_evaluation_order(val);
+ last_result = Some(val.clone());
if !generator.is_current_block_terminated()
&& let Some(ref completion_reg) = generator.current_completion_register.clone()
{
diff --git a/Tests/LibJS/Bytecode/expected/for-of-iteration-env-capacity.txt b/Tests/LibJS/Bytecode/expected/for-of-iteration-env-capacity.txt
index 473c5f51327..179a152e0ca 100644
--- a/Tests/LibJS/Bytecode/expected/for-of-iteration-env-capacity.txt
+++ b/Tests/LibJS/Bytecode/expected/for-of-iteration-env-capacity.txt
@@ -1,5 +1,5 @@
-$8bb295e5 for-of-iteration-env-capacity.js:1:14
- Registers: 13
+$0a509d5d for-of-iteration-env-capacity.js:1:14
+ Registers: 14
Blocks: 9
Locals: x~0
Constants:
@@ -27,22 +27,22 @@ block3:
[ a8] JumpStrictlyEqualsRhsInt32 arith_feedback:0, lhs:reg9, rhs:1, true_target:block5, false_target:block6
block4:
- [ c0] Mov2 c0_dst:x~0, c0_src:reg11, c1_dst:reg5, c1_src:x~0
- [ d8] JumpLoop target:block2
+ [ c0] Mov3 c0_dst:x~0, c0_src:reg11, c1_dst:reg13, c1_src:x~0, c2_dst:reg5, c2_src:reg13
+ [ e0] JumpLoop target:block2
block5:
- [ e0] IteratorClose iterator_object:reg6, iterator_next:reg7, iterator_done:reg8, completion_value:reg10
- [ 100] Throw src:reg10
+ [ e8] IteratorClose iterator_object:reg6, iterator_next:reg7, iterator_done:reg8, completion_value:reg10
+ [ 108] Throw src:reg10
block6:
- [ 108] IteratorClose iterator_object:reg6, iterator_next:reg7, iterator_done:reg8, completion_value:Undefined
- [ 128] JumpStrictlyEqualsRhsInt32 arith_feedback:1, lhs:reg9, rhs:2, true_target:block7, false_target:block8
+ [ 110] IteratorClose iterator_object:reg6, iterator_next:reg7, iterator_done:reg8, completion_value:Undefined
+ [ 130] JumpStrictlyEqualsRhsInt32 arith_feedback:1, lhs:reg9, rhs:2, true_target:block7, false_target:block8
block7:
- [ 140] Return value:reg10
+ [ 148] Return value:reg10
block8:
- [ 148] Throw src:reg10
+ [ 150] Throw src:reg10
Exception handlers:
- [ c0 .. e0] => handler block3
+ [ c0 .. e8] => handler block3
diff --git a/Tests/LibJS/Runtime/eval-basic.js b/Tests/LibJS/Runtime/eval-basic.js
index 0514f98a469..da81b90dcd2 100644
--- a/Tests/LibJS/Runtime/eval-basic.js
+++ b/Tests/LibJS/Runtime/eval-basic.js
@@ -48,6 +48,13 @@ test("a for-in iterator does not leak through a labelled break", () => {
expect(leaked).toBeUndefined();
});
+test("completion value read from a local that a later declaration reassigns", () => {
+ expect(eval("{ let a = 1; a; let b = (a = 9); }")).toBe(1);
+ expect(eval("{ let a = 1; if (true) { a; let b = (a = 9); } }")).toBe(1);
+ expect(eval("{ let a = 1; L: { a; let b = (a = 9); } }")).toBe(1);
+ expect(eval("{ let a = 1, i = 0; while (i++ < 1) { a; let b = (a = 9); } }")).toBe(1);
+});
+
test("completion value of a try statement whose catch clause runs", () => {
expect(eval("try { class C extends null.x {} } catch (e) {}")).toBeUndefined();
expect(eval("try { function f() {} null.x; } catch (e) { 3; } finally {}")).toBe(3);