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 function props(x) { | |
31 var array = []; | |
32 for (let p in x) array.push(p); | |
33 return array.sort(); | |
34 } | |
35 | |
36 assertEquals(0, props({}).length, "olen0"); | |
Lasse Reichstein
2011/10/17 11:49:40
You can safely choose to omit the third parameter
Steven
2011/10/17 12:18:29
Done.
| |
37 assertEquals(1, props({x:1}).length, "olen1"); | |
38 assertEquals(2, props({x:1, y:2}).length, "olen2"); | |
39 | |
40 assertArrayEquals(["x"], props({x:1}), "x"); | |
41 assertArrayEquals(["x", "y"], props({x:1, y:2}), "xy"); | |
42 assertArrayEquals(["x", "y", "zoom"], props({x:1, y:2, zoom:3}), "xyzoom"); | |
43 | |
44 assertEquals(0, props([]).length, "alen0"); | |
45 assertEquals(1, props([1]).length, "alen1"); | |
46 assertEquals(2, props([1,2]).length, "alen2"); | |
47 | |
48 assertArrayEquals(["0"], props([1]), "0"); | |
49 assertArrayEquals(["0", "1"], props([1,2]), "01"); | |
50 assertArrayEquals(["0", "1", "2"], props([1,2,3]), "012"); | |
51 | |
52 var o = {}; | |
53 var a = []; | |
54 for (let i = 0x0020; i < 0x01ff; i+=2) { | |
Lasse Reichstein
2011/10/17 11:49:40
Try declaring i and s prior to this loop, and see
Steven
2011/10/17 12:18:29
Done.
| |
55 let s = 'char:' + String.fromCharCode(i); | |
56 a.push(s); | |
57 o[s] = i; | |
58 } | |
59 assertArrayEquals(a, props(o), "charcodes"); | |
60 | |
61 var a = []; | |
62 assertEquals(0, props(a).length, "proplen0"); | |
63 a[Math.pow(2,30)-1] = 0; | |
64 assertEquals(1, props(a).length, "proplen1"); | |
65 a[Math.pow(2,31)-1] = 0; | |
66 assertEquals(2, props(a).length, "proplen2"); | |
67 a[1] = 0; | |
68 assertEquals(3, props(a).length, "proplen3"); | |
69 | |
70 var result = ''; | |
71 for (let p in {a : [0], b : 1}) { result += p; } | |
72 assertEquals('ab', result, "ab"); | |
73 | |
74 var result = ''; | |
75 for (let p in {a : {v:1}, b : 1}) { result += p; } | |
76 assertEquals('ab', result, "ab-nodeep"); | |
77 | |
78 var result = ''; | |
79 for (let p in { get a() {}, b : 1}) { result += p; } | |
80 assertEquals('ab', result, "abget"); | |
81 | |
82 var result = ''; | |
83 for (let p in { get a() {}, set a(x) {}, b : 1}) { result += p; } | |
84 assertEquals('ab', result, "abgetset"); | |
85 | |
86 | |
87 // Check that there is exactly one variable without initializer | |
88 // in a for-in statement with let variables. | |
89 assertThrows("function foo() { for (let in {}) { } }", SyntaxError); | |
90 assertThrows("function foo() { for (let x = 3 in {}) { } }", SyntaxError); | |
91 assertThrows("function foo() { for (let x, y in {}) { } }", SyntaxError); | |
92 assertThrows("function foo() { for (let x = 3, y in {}) { } }", SyntaxError); | |
93 assertThrows("function foo() { for (let x, y = 4 in {}) { } }", SyntaxError); | |
94 assertThrows("function foo() { for (let x = 3, y = 4 in {}) { } }", SyntaxError) ; | |
95 | |
96 | |
97 // In a normal for statement the iteration variable is not | |
98 // freshly allocated for each iteration. | |
99 function closures1() { | |
100 let a = []; | |
101 for (let i = 0; i < 5; ++i) { | |
102 a.push(function () { return i; }); | |
103 } | |
104 for (let j = 0; j < 5; ++j) { | |
105 assertEquals(5, a[j]()); | |
106 } | |
107 } | |
108 closures1(); | |
109 | |
110 | |
111 function closures2() { | |
112 let a = [], b = []; | |
113 for (let i = 0, j = 10; i < 5; ++i, ++j) { | |
114 a.push(function () { return i; }); | |
115 b.push(function () { return j; }); | |
116 } | |
117 for (let k = 0; k < 5; ++k) { | |
118 assertEquals(5, a[k]()); | |
119 assertEquals(15, b[k]()); | |
120 } | |
121 } | |
122 closures2(); | |
123 | |
124 | |
125 // In a for-in statement the iteration variable is fresh | |
126 // for earch iteration. | |
127 function closures3(x) { | |
128 let a = []; | |
129 for (let p in x) { | |
130 a.push(function () { return p; }); | |
131 } | |
132 let k = 0; | |
133 for (let q in x) { | |
134 assertEquals(q, a[k]()); | |
135 ++k; | |
136 } | |
137 } | |
138 closures3({a : [0], b : 1, c : {v : 1}, get d() {}, set e(x) {}}); | |
OLD | NEW |