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
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.
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.
28 // Flags: --harmony-scoping
34 for (let p in x) array.push(p);
38 assertEquals(0, props({}).length);
39 assertEquals(1, props({x:1}).length);
40 assertEquals(2, props({x:1, y:2}).length);
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}));
46 assertEquals(0, props([]).length);
47 assertEquals(1, props([1]).length);
48 assertEquals(2, props([1,2]).length);
50 assertArrayEquals(["0"], props([1]));
51 assertArrayEquals(["0", "1"], props([1,2]));
52 assertArrayEquals(["0", "1", "2"], props([1,2,3]));
58 for (let i = 0x0020; i < 0x01ff; i+=2) {
59 let s = 'char:' + String.fromCharCode(i);
63 assertArrayEquals(a, props(o));
64 assertEquals(i, "outer_i");
65 assertEquals(s, "outer_s");
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);
74 assertEquals(3, props(a).length);
77 for (let p in {a : [0], b : 1}) { result += p; }
78 assertEquals('ab', result);
81 for (let p in {a : {v:1}, b : 1}) { result += p; }
82 assertEquals('ab', result);
85 for (let p in { get a() {}, b : 1}) { result += p; }
86 assertEquals('ab', result);
89 for (let p in { get a() {}, set a(x) {}, b : 1}) { result += p; }
90 assertEquals('ab', result);
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 {}) { } }", SyntaxError);
97 assertThrows("function foo() { 'use strict'; for (let x, y in {}) { } }", SyntaxError);
98 assertThrows("function foo() { 'use strict'; for (let x = 3, y in {}) { } }", SyntaxError);
99 assertThrows("function foo() { 'use strict'; for (let x, y = 4 in {}) { } }", SyntaxError);
100 assertThrows("function foo() { 'use strict'; for (let x = 3, y = 4 in {}) { } }", SyntaxError);
103 // In a normal for statement the iteration variable is
104 // freshly allocated for each iteration.
105 function closures1() {
107 for (let i = 0; i < 5; ++i) {
108 a.push(function () { return i; });
110 for (let j = 0; j < 5; ++j) {
111 assertEquals(j, a[j]());
117 function closures2() {
119 for (let i = 0, j = 10; i < 5; ++i, ++j) {
120 a.push(function () { return i; });
121 b.push(function () { return j; });
123 for (let k = 0; k < 5; ++k) {
124 assertEquals(k, a[k]());
125 assertEquals(k + 10, b[k]());
131 function closure_in_for_init() {
133 for (let i = 0, f = function() { return i }; i < 5; ++i) {
136 for (let k = 0; k < 5; ++k) {
137 assertEquals(0, a[k]());
140 closure_in_for_init();
143 function closure_in_for_cond() {
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]());
150 closure_in_for_next();
153 function closure_in_for_next() {
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]());
160 closure_in_for_next();
163 // In a for-in statement the iteration variable is fresh
164 // for earch iteration.
165 function closures3(x) {
168 a.push(function () { return p; });
172 assertEquals(q, a[k]());
176 closures3({a : [0], b : 1, c : {v : 1}, get d() {}, set e(x) {}});