test: remove obsolete harmony flags
[platform/upstream/nodejs.git] / deps / v8 / test / mjsunit / es6 / block-for.js
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 "use strict";
28
29 function props(x) {
30   var array = [];
31   for (let p in x) array.push(p);
32   return array.sort();
33 }
34
35 assertEquals(0, props({}).length);
36 assertEquals(1, props({x:1}).length);
37 assertEquals(2, props({x:1, y:2}).length);
38
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}));
42
43 assertEquals(0, props([]).length);
44 assertEquals(1, props([1]).length);
45 assertEquals(2, props([1,2]).length);
46
47 assertArrayEquals(["0"], props([1]));
48 assertArrayEquals(["0", "1"], props([1,2]));
49 assertArrayEquals(["0", "1", "2"], props([1,2,3]));
50
51 var o = {};
52 var a = [];
53 let i = "outer_i";
54 let s = "outer_s";
55 for (let i = 0x0020; i < 0x01ff; i+=2) {
56   let s = 'char:' + String.fromCharCode(i);
57   a.push(s);
58   o[s] = i;
59 }
60 assertArrayEquals(a, props(o));
61 assertEquals(i, "outer_i");
62 assertEquals(s, "outer_s");
63
64 var a = [];
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);
70 a[1] = 0;
71 assertEquals(3, props(a).length);
72
73 var result = '';
74 for (let p in {a : [0], b : 1}) { result += p; }
75 assertEquals('ab', result);
76
77 var result = '';
78 for (let p in {a : {v:1}, b : 1}) { result += p; }
79 assertEquals('ab', result);
80
81 var result = '';
82 for (let p in { get a() {}, b : 1}) { result += p; }
83 assertEquals('ab', result);
84
85 var result = '';
86 for (let p in { get a() {}, set a(x) {}, b : 1}) { result += p; }
87 assertEquals('ab', result);
88
89
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);
98
99
100 // In a normal for statement the iteration variable is
101 // freshly allocated for each iteration.
102 function closures1() {
103   let a = [];
104   for (let i = 0; i < 5; ++i) {
105     a.push(function () { return i; });
106   }
107   for (let j = 0; j < 5; ++j) {
108     assertEquals(j, a[j]());
109   }
110 }
111 closures1();
112
113
114 function closures2() {
115   let a = [], b = [];
116   for (let i = 0, j = 10; i < 5; ++i, ++j) {
117     a.push(function () { return i; });
118     b.push(function () { return j; });
119   }
120   for (let k = 0; k < 5; ++k) {
121     assertEquals(k, a[k]());
122     assertEquals(k + 10, b[k]());
123   }
124 }
125 closures2();
126
127
128 function closure_in_for_init() {
129   let a = [];
130   for (let i = 0, f = function() { return i }; i < 5; ++i) {
131     a.push(f);
132   }
133   for (let k = 0; k < 5; ++k) {
134     assertEquals(0, a[k]());
135   }
136 }
137 closure_in_for_init();
138
139
140 function closure_in_for_cond() {
141   let a = [];
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]());
145   }
146 }
147 closure_in_for_cond();
148
149
150 function closure_in_for_next() {
151   let a = [];
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]());
155   }
156 }
157 closure_in_for_next();
158
159
160 // In a for-in statement the iteration variable is fresh
161 // for earch iteration.
162 function closures3(x) {
163   let a = [];
164   for (let p in x) {
165     a.push(function () { return p; });
166   }
167   let k = 0;
168   for (let q in x) {
169     assertEquals(q, a[k]());
170     ++k;
171   }
172 }
173 closures3({a : [0], b : 1, c : {v : 1}, get d() {}, set e(x) {}});