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.
31 for (let p in x) array.push(p);
35 assertEquals(0, props({}).length);
36 assertEquals(1, props({x:1}).length);
37 assertEquals(2, props({x:1, y:2}).length);
39 assertArrayEquals(["x"], props({x:1}));
40 assertArrayEquals(["x", "y"], props({x:1, y:2}));
41 assertArrayEquals(["x", "y", "zoom"], props({x:1, y:2, zoom:3}));
43 assertEquals(0, props([]).length);
44 assertEquals(1, props([1]).length);
45 assertEquals(2, props([1,2]).length);
47 assertArrayEquals(["0"], props([1]));
48 assertArrayEquals(["0", "1"], props([1,2]));
49 assertArrayEquals(["0", "1", "2"], props([1,2,3]));
55 for (let i = 0x0020; i < 0x01ff; i+=2) {
56 let s = 'char:' + String.fromCharCode(i);
60 assertArrayEquals(a, props(o));
61 assertEquals(i, "outer_i");
62 assertEquals(s, "outer_s");
65 assertEquals(0, props(a).length);
66 a[Math.pow(2,30)-1] = 0;
67 assertEquals(1, props(a).length);
68 a[Math.pow(2,31)-1] = 0;
69 assertEquals(2, props(a).length);
71 assertEquals(3, props(a).length);
74 for (let p in {a : [0], b : 1}) { result += p; }
75 assertEquals('ab', result);
78 for (let p in {a : {v:1}, b : 1}) { result += p; }
79 assertEquals('ab', result);
82 for (let p in { get a() {}, b : 1}) { result += p; }
83 assertEquals('ab', result);
86 for (let p in { get a() {}, set a(x) {}, b : 1}) { result += p; }
87 assertEquals('ab', result);
90 // Check that there is exactly one variable without initializer
91 // in a for-in statement with let variables.
92 assertThrows("function foo() { 'use strict'; for (let in {}) { } }", SyntaxError);
93 assertThrows("function foo() { 'use strict'; for (let x = 3 in {}) { } }", SyntaxError);
94 assertThrows("function foo() { 'use strict'; for (let x, y in {}) { } }", SyntaxError);
95 assertThrows("function foo() { 'use strict'; for (let x = 3, y in {}) { } }", SyntaxError);
96 assertThrows("function foo() { 'use strict'; for (let x, y = 4 in {}) { } }", SyntaxError);
97 assertThrows("function foo() { 'use strict'; for (let x = 3, y = 4 in {}) { } }", SyntaxError);
100 // In a normal for statement the iteration variable is
101 // freshly allocated for each iteration.
102 function closures1() {
104 for (let i = 0; i < 5; ++i) {
105 a.push(function () { return i; });
107 for (let j = 0; j < 5; ++j) {
108 assertEquals(j, a[j]());
114 function closures2() {
116 for (let i = 0, j = 10; i < 5; ++i, ++j) {
117 a.push(function () { return i; });
118 b.push(function () { return j; });
120 for (let k = 0; k < 5; ++k) {
121 assertEquals(k, a[k]());
122 assertEquals(k + 10, b[k]());
128 function closure_in_for_init() {
130 for (let i = 0, f = function() { return i }; i < 5; ++i) {
133 for (let k = 0; k < 5; ++k) {
134 assertEquals(0, a[k]());
137 closure_in_for_init();
140 function closure_in_for_cond() {
142 for (let i = 0; a.push(function () { return i; }), i < 5; ++i) { }
143 for (let k = 0; k < 5; ++k) {
144 assertEquals(k, a[k]());
147 closure_in_for_cond();
150 function closure_in_for_next() {
152 for (let i = 0; i < 5; a.push(function () { return i; }), ++i) { }
153 for (let k = 0; k < 5; ++k) {
154 assertEquals(k + 1, a[k]());
157 closure_in_for_next();
160 // In a for-in statement the iteration variable is fresh
161 // for earch iteration.
162 function closures3(x) {
165 a.push(function () { return p; });
169 assertEquals(q, a[k]());
173 closures3({a : [0], b : 1, c : {v : 1}, get d() {}, set e(x) {}});