OLD | NEW |
1 // Copyright 2014 the V8 project authors. All rights reserved. | 1 // Copyright 2014 the V8 project authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 'use strict'; | 5 'use strict'; |
6 | 6 |
7 // Top-level code | 7 // Top-level code |
8 let s = 0; | 8 let s = 0; |
9 let f = [undefined, undefined, undefined] | 9 let f = [undefined, undefined, undefined] |
10 for (const x of [1,2,3]) { | 10 for (const x of [1,2,3]) { |
11 s += x; | 11 s += x; |
12 f[x-1] = function() { return x; } | 12 f[x-1] = function() { return x; } |
13 } | 13 } |
14 assertEquals(6, s); | 14 assertEquals(6, s); |
15 assertEquals(1, f[0]()); | 15 assertEquals(1, f[0]()); |
16 assertEquals(2, f[1]()); | 16 assertEquals(2, f[1]()); |
17 assertEquals(3, f[2]()); | 17 assertEquals(3, f[2]()); |
18 | 18 |
19 let x = 1; | 19 let x = 1; |
20 s = 0; | 20 s = 0; |
21 for (const x of [x, x+1, x+2]) { | 21 for (const z of [x, x+1, x+2]) { |
22 s += x; | 22 s += z; |
23 } | 23 } |
24 assertEquals(6, s); | 24 assertEquals(6, s); |
25 | 25 |
26 s = 0; | 26 s = 0; |
27 var q = 1; | 27 var q = 1; |
28 for (const q of [q, q+1, q+2]) { | 28 for (const x of [q, q+1, q+2]) { |
29 s += q; | 29 s += x; |
30 } | 30 } |
31 assertEquals(6, s); | 31 assertEquals(6, s); |
32 | 32 |
33 let z = 1; | 33 let z = 1; |
34 s = 0; | 34 s = 0; |
35 for (const x = 1; z < 2; z++) { | 35 for (const x = 1; z < 2; z++) { |
36 s += x + z; | 36 s += x + z; |
37 } | 37 } |
38 assertEquals(2, s); | 38 assertEquals(2, s); |
39 | 39 |
40 | 40 |
41 s = ""; | 41 s = ""; |
42 for (const x in [1,2,3]) { | 42 for (const x in [1,2,3]) { |
43 s += x; | 43 s += x; |
44 } | 44 } |
45 assertEquals("012", s); | 45 assertEquals("012", s); |
46 | 46 |
47 assertThrows("'use strict'; for (const x in [1,2,3]) { x++ }", TypeError); | 47 assertThrows("'use strict'; for (const x in [1,2,3]) { x++ }", TypeError); |
48 | 48 |
49 // Function scope | 49 // Function scope |
50 (function() { | 50 (function() { |
51 let s = 0; | 51 let s = 0; |
52 for (const x of [1,2,3]) { | 52 for (const x of [1,2,3]) { |
53 s += x; | 53 s += x; |
54 } | 54 } |
55 assertEquals(6, s); | 55 assertEquals(6, s); |
56 | 56 |
57 let x = 1; | 57 let x = 1; |
58 s = 0; | 58 s = 0; |
59 for (const x of [x, x+1, x+2]) { | 59 for (const q of [x, x+1, x+2]) { |
60 s += x; | 60 s += q; |
61 } | 61 } |
62 assertEquals(6, s); | 62 assertEquals(6, s); |
63 | 63 |
64 s = 0; | 64 s = 0; |
65 var q = 1; | 65 var q = 1; |
66 for (const q of [q, q+1, q+2]) { | 66 for (const x of [q, q+1, q+2]) { |
67 s += q; | 67 s += x; |
68 } | 68 } |
69 assertEquals(6, s); | 69 assertEquals(6, s); |
70 | 70 |
71 s = ""; | 71 s = ""; |
72 for (const x in [1,2,3]) { | 72 for (const x in [1,2,3]) { |
73 s += x; | 73 s += x; |
74 } | 74 } |
75 assertEquals("012", s); | 75 assertEquals("012", s); |
76 }()); | 76 }()); |
OLD | NEW |