OLD | NEW |
| (Empty) |
1 // Copyright 2011 the V8 project authors. All rights reserved. | |
2 // Redistribution and use in source and binary forms, with or without | |
3 // modification, are permitted provided that the following conditions are | |
4 // met: | |
5 // | |
6 // * Redistributions of source code must retain the above copyright | |
7 // notice, this list of conditions and the following disclaimer. | |
8 // * Redistributions in binary form must reproduce the above | |
9 // copyright notice, this list of conditions and the following | |
10 // disclaimer in the documentation and/or other materials provided | |
11 // with the distribution. | |
12 // * Neither the name of Google Inc. nor the names of its | |
13 // contributors may be used to endorse or promote products derived | |
14 // from this software without specific prior written permission. | |
15 // | |
16 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | |
17 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | |
18 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR | |
19 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT | |
20 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | |
21 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | |
22 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | |
23 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | |
24 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | |
25 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | |
26 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | |
27 | |
28 // Flags: --harmony-scoping | |
29 | |
30 "use strict"; | |
31 | |
32 function props(x) { | |
33 var array = []; | |
34 for (let p in x) array.push(p); | |
35 return array.sort(); | |
36 } | |
37 | |
38 assertEquals(0, props({}).length); | |
39 assertEquals(1, props({x:1}).length); | |
40 assertEquals(2, props({x:1, y:2}).length); | |
41 | |
42 assertArrayEquals(["x"], props({x:1})); | |
43 assertArrayEquals(["x", "y"], props({x:1, y:2})); | |
44 assertArrayEquals(["x", "y", "zoom"], props({x:1, y:2, zoom:3})); | |
45 | |
46 assertEquals(0, props([]).length); | |
47 assertEquals(1, props([1]).length); | |
48 assertEquals(2, props([1,2]).length); | |
49 | |
50 assertArrayEquals(["0"], props([1])); | |
51 assertArrayEquals(["0", "1"], props([1,2])); | |
52 assertArrayEquals(["0", "1", "2"], props([1,2,3])); | |
53 | |
54 var o = {}; | |
55 var a = []; | |
56 let i = "outer_i"; | |
57 let s = "outer_s"; | |
58 for (let i = 0x0020; i < 0x01ff; i+=2) { | |
59 let s = 'char:' + String.fromCharCode(i); | |
60 a.push(s); | |
61 o[s] = i; | |
62 } | |
63 assertArrayEquals(a, props(o)); | |
64 assertEquals(i, "outer_i"); | |
65 assertEquals(s, "outer_s"); | |
66 | |
67 var a = []; | |
68 assertEquals(0, props(a).length); | |
69 a[Math.pow(2,30)-1] = 0; | |
70 assertEquals(1, props(a).length); | |
71 a[Math.pow(2,31)-1] = 0; | |
72 assertEquals(2, props(a).length); | |
73 a[1] = 0; | |
74 assertEquals(3, props(a).length); | |
75 | |
76 var result = ''; | |
77 for (let p in {a : [0], b : 1}) { result += p; } | |
78 assertEquals('ab', result); | |
79 | |
80 var result = ''; | |
81 for (let p in {a : {v:1}, b : 1}) { result += p; } | |
82 assertEquals('ab', result); | |
83 | |
84 var result = ''; | |
85 for (let p in { get a() {}, b : 1}) { result += p; } | |
86 assertEquals('ab', result); | |
87 | |
88 var result = ''; | |
89 for (let p in { get a() {}, set a(x) {}, b : 1}) { result += p; } | |
90 assertEquals('ab', result); | |
91 | |
92 | |
93 // Check that there is exactly one variable without initializer | |
94 // in a for-in statement with let variables. | |
95 assertThrows("function foo() { 'use strict'; for (let in {}) { } }", SyntaxError
); | |
96 assertThrows("function foo() { 'use strict'; for (let x = 3 in {}) { } }", Synta
xError); | |
97 assertThrows("function foo() { 'use strict'; for (let x, y in {}) { } }", Syntax
Error); | |
98 assertThrows("function foo() { 'use strict'; for (let x = 3, y in {}) { } }", Sy
ntaxError); | |
99 assertThrows("function foo() { 'use strict'; for (let x, y = 4 in {}) { } }", Sy
ntaxError); | |
100 assertThrows("function foo() { 'use strict'; for (let x = 3, y = 4 in {}) { } }"
, SyntaxError); | |
101 | |
102 | |
103 // In a normal for statement the iteration variable is | |
104 // freshly allocated for each iteration. | |
105 function closures1() { | |
106 let a = []; | |
107 for (let i = 0; i < 5; ++i) { | |
108 a.push(function () { return i; }); | |
109 } | |
110 for (let j = 0; j < 5; ++j) { | |
111 assertEquals(j, a[j]()); | |
112 } | |
113 } | |
114 closures1(); | |
115 | |
116 | |
117 function closures2() { | |
118 let a = [], b = []; | |
119 for (let i = 0, j = 10; i < 5; ++i, ++j) { | |
120 a.push(function () { return i; }); | |
121 b.push(function () { return j; }); | |
122 } | |
123 for (let k = 0; k < 5; ++k) { | |
124 assertEquals(k, a[k]()); | |
125 assertEquals(k + 10, b[k]()); | |
126 } | |
127 } | |
128 closures2(); | |
129 | |
130 | |
131 function closure_in_for_init() { | |
132 let a = []; | |
133 for (let i = 0, f = function() { return i }; i < 5; ++i) { | |
134 a.push(f); | |
135 } | |
136 for (let k = 0; k < 5; ++k) { | |
137 assertEquals(0, a[k]()); | |
138 } | |
139 } | |
140 closure_in_for_init(); | |
141 | |
142 | |
143 function closure_in_for_cond() { | |
144 let a = []; | |
145 for (let i = 0; a.push(function () { return i; }), i < 5; ++i) { } | |
146 for (let k = 0; k < 5; ++k) { | |
147 assertEquals(k, a[k]()); | |
148 } | |
149 } | |
150 closure_in_for_cond(); | |
151 | |
152 | |
153 function closure_in_for_next() { | |
154 let a = []; | |
155 for (let i = 0; i < 5; a.push(function () { return i; }), ++i) { } | |
156 for (let k = 0; k < 5; ++k) { | |
157 assertEquals(k + 1, a[k]()); | |
158 } | |
159 } | |
160 closure_in_for_next(); | |
161 | |
162 | |
163 // In a for-in statement the iteration variable is fresh | |
164 // for earch iteration. | |
165 function closures3(x) { | |
166 let a = []; | |
167 for (let p in x) { | |
168 a.push(function () { return p; }); | |
169 } | |
170 let k = 0; | |
171 for (let q in x) { | |
172 assertEquals(q, a[k]()); | |
173 ++k; | |
174 } | |
175 } | |
176 closures3({a : [0], b : 1, c : {v : 1}, get d() {}, set e(x) {}}); | |
OLD | NEW |