-
Notifications
You must be signed in to change notification settings - Fork 649
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Summary: Emit a scope in while loops, so that lexically scoped declarations in the loop will have a different instance on each iteration. Reviewed By: avp Differential Revision: D56026167 fbshipit-source-id: 77cadc4e4d72b86f820bb183a5bd4ccbde498029
- Loading branch information
1 parent
c477182
commit 1bc4a01
Showing
3 changed files
with
96 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
/** | ||
* Copyright (c) Meta Platforms, Inc. and affiliates. | ||
* | ||
* This source code is licensed under the MIT license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
*/ | ||
|
||
// RUN: %hermes %s -Xes6-block-scoping | %FileCheck --match-full-lines %s | ||
// RUN: %hermes -Xes6-block-scoping -lazy %s | %FileCheck --match-full-lines %s | ||
// RUN: %shermes -Xes6-block-scoping -exec %s | %FileCheck --match-full-lines %s | ||
|
||
let arr = (function (){ | ||
let arr = []; | ||
try { | ||
while (true) { | ||
let x = 42; | ||
// Make sure there is a capture to force a scope. | ||
arr.push(()=>x); | ||
return arr; | ||
} | ||
} finally { | ||
// Test that emitting this finally block at the location of the return above | ||
// (which is a different scope in the IR) works correctly. | ||
while (arr.length < 10) { | ||
let x = arr.length; | ||
arr.push(()=>x); | ||
} | ||
} | ||
})(); | ||
|
||
for (v of arr){ | ||
print(v()); | ||
} | ||
|
||
// CHECK: 42 | ||
// CHECK-NEXT: 1 | ||
// CHECK-NEXT: 2 | ||
// CHECK-NEXT: 3 | ||
// CHECK-NEXT: 4 | ||
// CHECK-NEXT: 5 | ||
// CHECK-NEXT: 6 | ||
// CHECK-NEXT: 7 | ||
// CHECK-NEXT: 8 | ||
// CHECK-NEXT: 9 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
/** | ||
* Copyright (c) Meta Platforms, Inc. and affiliates. | ||
* | ||
* This source code is licensed under the MIT license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
*/ | ||
|
||
// RUN: %hermes -Xes6-block-scoping %s | %FileCheck --match-full-lines %s | ||
// RUN: %shermes -Xes6-block-scoping -exec %s | %FileCheck --match-full-lines %s | ||
|
||
(function (){ | ||
let arr = []; | ||
let i = 0; | ||
while (i < 10) { | ||
let j = i; | ||
arr.push(()=>j); | ||
i++; | ||
} | ||
|
||
for(f of arr) { | ||
print(f()); | ||
} | ||
})(); | ||
|
||
// CHECK: 0 | ||
// CHECK-NEXT: 1 | ||
// CHECK-NEXT: 2 | ||
// CHECK-NEXT: 3 | ||
// CHECK-NEXT: 4 | ||
// CHECK-NEXT: 5 | ||
// CHECK-NEXT: 6 | ||
// CHECK-NEXT: 7 | ||
// CHECK-NEXT: 8 | ||
// CHECK-NEXT: 9 |