1// Copyright 2008-2015 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// This is adapted from mjsunit/for-in.js.
29
30// Flags: --harmony-reflect
31
32
33function props(x) {
34  var array = [];
35  for (var p of Reflect.enumerate(x)) array.push(p);
36  return array.sort();
37}
38
39assertEquals(0, props({}).length, "olen0");
40assertEquals(1, props({x:1}).length, "olen1");
41assertEquals(2, props({x:1, y:2}).length, "olen2");
42
43assertArrayEquals(["x"], props({x:1}), "x");
44assertArrayEquals(["x", "y"], props({x:1, y:2}), "xy");
45assertArrayEquals(["x", "y", "zoom"], props({x:1, y:2, zoom:3}), "xyzoom");
46
47assertEquals(0, props([]).length, "alen0");
48assertEquals(1, props([1]).length, "alen1");
49assertEquals(2, props([1,2]).length, "alen2");
50
51assertArrayEquals(["0"], props([1]), "0");
52assertArrayEquals(["0", "1"], props([1,2]), "01");
53assertArrayEquals(["0", "1", "2"], props([1,2,3]), "012");
54
55var o = {};
56var a = [];
57for (var i = 0x0020; i < 0x01ff; i+=2) {
58  var s = 'char:' + String.fromCharCode(i);
59  a.push(s);
60  o[s] = i;
61}
62assertArrayEquals(a, props(o), "charcodes");
63
64var a = [];
65assertEquals(0, props(a).length, "proplen0");
66a[Math.pow(2,30)-1] = 0;
67assertEquals(1, props(a).length, "proplen1");
68a[Math.pow(2,31)-1] = 0;
69assertEquals(2, props(a).length, "proplen2");
70a[1] = 0;
71assertEquals(3, props(a).length, "proplen3");
72
73var result = '';
74for (var p of Reflect.enumerate({a : [0], b : 1})) { result += p; }
75assertEquals('ab', result, "ab");
76
77var result = '';
78for (var p of Reflect.enumerate({a : {v:1}, b : 1})) { result += p; }
79assertEquals('ab', result, "ab-nodeep");
80
81var result = '';
82for (var p of Reflect.enumerate({ get a() {}, b : 1})) { result += p; }
83assertEquals('ab', result, "abget");
84
85var result = '';
86for (var p of Reflect.enumerate({ get a() {}, set a(x) {}, b : 1})) {
87  result += p;
88}
89assertEquals('ab', result, "abgetset");
90
91(function() {
92  var large_key = 2147483650;
93  var o = {__proto__: {}};
94  o[large_key] = 1;
95  o.__proto__[large_key] = 1;
96  var keys = [];
97  for (var k of Reflect.enumerate(o)) {
98    keys.push(k);
99  }
100  assertEquals(["2147483650"], keys);
101})();
102