blob: 547c748dffddd36693a5057b35038e496f6f96ea [file] [log] [blame]
Steve Blocka7e24c12009-10-30 11:49:00 +00001// Copyright 2008 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
Ben Murdochc5610432016-08-08 18:44:38 +010028// Flags: --noharmony-for-in
29
Steve Blocka7e24c12009-10-30 11:49:00 +000030function props(x) {
31 var array = [];
32 for (var p in x) array.push(p);
33 return array.sort();
34}
35
Steve Block3ce2e202009-11-05 08:53:23 +000036assertEquals(0, props({}).length, "olen0");
37assertEquals(1, props({x:1}).length, "olen1");
38assertEquals(2, props({x:1, y:2}).length, "olen2");
Steve Blocka7e24c12009-10-30 11:49:00 +000039
Steve Block3ce2e202009-11-05 08:53:23 +000040assertArrayEquals(["x"], props({x:1}), "x");
41assertArrayEquals(["x", "y"], props({x:1, y:2}), "xy");
42assertArrayEquals(["x", "y", "zoom"], props({x:1, y:2, zoom:3}), "xyzoom");
Steve Blocka7e24c12009-10-30 11:49:00 +000043
Steve Block3ce2e202009-11-05 08:53:23 +000044assertEquals(0, props([]).length, "alen0");
45assertEquals(1, props([1]).length, "alen1");
46assertEquals(2, props([1,2]).length, "alen2");
Steve Blocka7e24c12009-10-30 11:49:00 +000047
Steve Block3ce2e202009-11-05 08:53:23 +000048assertArrayEquals(["0"], props([1]), "0");
49assertArrayEquals(["0", "1"], props([1,2]), "01");
50assertArrayEquals(["0", "1", "2"], props([1,2,3]), "012");
Steve Blocka7e24c12009-10-30 11:49:00 +000051
52var o = {};
53var a = [];
54for (var i = 0x0020; i < 0x01ff; i+=2) {
55 var s = 'char:' + String.fromCharCode(i);
56 a.push(s);
57 o[s] = i;
58}
Steve Block3ce2e202009-11-05 08:53:23 +000059assertArrayEquals(a, props(o), "charcodes");
Steve Blocka7e24c12009-10-30 11:49:00 +000060
61var a = [];
Steve Block3ce2e202009-11-05 08:53:23 +000062assertEquals(0, props(a).length, "proplen0");
Steve Blocka7e24c12009-10-30 11:49:00 +000063a[Math.pow(2,30)-1] = 0;
Steve Block3ce2e202009-11-05 08:53:23 +000064assertEquals(1, props(a).length, "proplen1");
Steve Blocka7e24c12009-10-30 11:49:00 +000065a[Math.pow(2,31)-1] = 0;
Steve Block3ce2e202009-11-05 08:53:23 +000066assertEquals(2, props(a).length, "proplen2");
Steve Blocka7e24c12009-10-30 11:49:00 +000067a[1] = 0;
Steve Block3ce2e202009-11-05 08:53:23 +000068assertEquals(3, props(a).length, "proplen3");
Steve Blocka7e24c12009-10-30 11:49:00 +000069
70for (var hest = 'hest' in {}) { }
Steve Block3ce2e202009-11-05 08:53:23 +000071assertEquals('hest', hest, "empty-no-override");
Steve Blocka7e24c12009-10-30 11:49:00 +000072
73var result = '';
74for (var p in {a : [0], b : 1}) { result += p; }
Steve Block3ce2e202009-11-05 08:53:23 +000075assertEquals('ab', result, "ab");
Steve Blocka7e24c12009-10-30 11:49:00 +000076
77var result = '';
78for (var p in {a : {v:1}, b : 1}) { result += p; }
Steve Block3ce2e202009-11-05 08:53:23 +000079assertEquals('ab', result, "ab-nodeep");
Steve Blocka7e24c12009-10-30 11:49:00 +000080
81var result = '';
82for (var p in { get a() {}, b : 1}) { result += p; }
Steve Block3ce2e202009-11-05 08:53:23 +000083assertEquals('ab', result, "abget");
Steve Blocka7e24c12009-10-30 11:49:00 +000084
85var result = '';
86for (var p in { get a() {}, set a(x) {}, b : 1}) { result += p; }
Steve Block3ce2e202009-11-05 08:53:23 +000087assertEquals('ab', result, "abgetset");
Steve Blocka7e24c12009-10-30 11:49:00 +000088
Steve Block8defd9f2010-07-08 12:39:36 +010089
90// Test that for-in in the global scope works with a keyed property as "each".
91// Test outside a loop and in a loop for multiple iterations.
92a = [1,2,3,4];
93x = {foo:5, bar:6, zip:7, glep:9, 10:11};
94delete x.bar;
95y = {}
96
97for (a[2] in x) {
98 y[a[2]] = x[a[2]];
99}
100
101assertEquals(5, y.foo, "y.foo");
102assertEquals("undefined", typeof y.bar, "y.bar");
103assertEquals(7, y.zip, "y.zip");
104assertEquals(9, y.glep, "y.glep");
105assertEquals(11, y[10], "y[10]");
106assertEquals("undefined", typeof y[2], "y[2]");
107assertEquals("undefined", typeof y[0], "y[0]");
108
109for (i=0 ; i < 3; ++i) {
110 y = {}
111
112 for (a[2] in x) {
113 y[a[2]] = x[a[2]];
114 }
115
116 assertEquals(5, y.foo, "y.foo");
117 assertEquals("undefined", typeof y.bar, "y.bar");
118 assertEquals(7, y.zip, "y.zip");
119 assertEquals(9, y.glep, "y.glep");
120 assertEquals(11, y[10], "y[10]");
121 assertEquals("undefined", typeof y[2], "y[2]");
122 assertEquals("undefined", typeof y[0], "y[0]");
123}
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000124
Ben Murdochda12d292016-06-02 14:46:10 +0100125(function testLargeElementKeys() {
126 // Key out of SMI range but well within safe double representaion.
127 var large_key = 2147483650;
128 var o = [];
129 // Trigger dictionary elements with HeapNumber keys.
130 o[large_key] = 0;
131 o[large_key+1] = 1;
132 o[large_key+2] = 2;
133 o[large_key+3] = 3;
134 var keys = [];
135 for (var k in o) {
136 keys.push(k);
137 }
138 assertEquals(["2147483650", "2147483651", "2147483652", "2147483653"], keys);
139})();
140
141(function testLargeElementKeysWithProto() {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000142 var large_key = 2147483650;
143 var o = {__proto__: {}};
144 o[large_key] = 1;
145 o.__proto__[large_key] = 1;
146 var keys = [];
147 for (var k in o) {
148 keys.push(k);
149 }
150 assertEquals(["2147483650"], keys);
151})();
Ben Murdochda12d292016-06-02 14:46:10 +0100152
153(function testNonEnumerableArgumentsIndex() {
154 Object.defineProperty(arguments, 0, {enumerable:false});
155 for (var k in arguments) {
156 assertUnreachable();
157 }
158})();
159
160(function testNonEnumerableSloppyArgumentsIndex(a) {
161 Object.defineProperty(arguments, 0, {enumerable:false});
162 for (var k in arguments) {
163 assertUnreachable();
164 }
165})(true);