• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1// Copyright JS Foundation and other contributors, http://js.foundation
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7//     http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15// Copyright 2015 the V8 project authors. All rights reserved.
16// Redistribution and use in source and binary forms, with or without
17// modification, are permitted provided that the following conditions are
18// met:
19//
20//     * Redistributions of source code must retain the above copyright
21//       notice, this list of conditions and the following disclaimer.
22//     * Redistributions in binary form must reproduce the above
23//       copyright notice, this list of conditions and the following
24//       disclaimer in the documentation and/or other materials provided
25//       with the distribution.
26//     * Neither the name of Google Inc. nor the names of its
27//       contributors may be used to endorse or promote products derived
28//       from this software without specific prior written permission.
29//
30// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
31// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
32// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
33// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
34// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
35// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
36// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
37// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
38// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
39// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
40// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
41
42var object1 = {
43  property1: 42,
44  property2: 13
45};
46
47var array1 = [];
48
49assert (Reflect.ownKeys (object1)[0] === "property1");
50assert (Reflect.ownKeys (object1)[1] === "property2");
51assert (Reflect.ownKeys (object1).length === 2);
52
53var obj = { a: 1, b: 2};
54var keys = Reflect.ownKeys (obj);
55assert (2 === keys.length);
56assert ("a" === keys[0]);
57assert ("b" === keys[1]);
58
59var obj = { a: function(){}, b: function(){} };
60var keys = Reflect.ownKeys (obj);
61assert (2 === keys.length);
62assert ("a" === keys[0]);
63assert ("b" === keys[1]);
64
65// Check slow case
66var obj = { a: 1, b: 2, c: 3 };
67delete obj.b;
68var keys = Reflect.ownKeys (obj)
69assert (2 === keys.length);
70assert ("a" === keys[0]);
71assert ("c" === keys[1]);
72
73// Check that non-enumerable properties are being returned.
74var keys = Reflect.ownKeys ([1, 2]);
75assert (3 === keys.length);
76assert ("0" === keys[0]);
77assert ("1" === keys[1]);
78assert ("string" === typeof keys[0]);
79assert ("string" === typeof keys[1]);
80assert ("length" === keys[2]);
81
82// Check that no proto properties are returned.
83var obj = { foo: "foo" };
84var obj2 = { bar: "bar" };
85Object.setPrototypeOf (obj, obj2)
86keys = Reflect.ownKeys (obj);
87assert (1 === keys.length);
88assert ("foo" === keys[0]);
89
90// Check that getter properties are returned.
91var obj = {};
92Object.defineProperty (obj, "getter", function() {})
93//obj.__defineGetter__("getter", function() {});
94keys = Reflect.ownKeys (obj);
95assert (1 === keys.length);
96assert ("getter" === keys[0]);
97
98// Check that implementation does not access Array.prototype.
99var savedConcat = Array.prototype.concat;
100Array.prototype.concat = function() { return []; }
101keys = Reflect.ownKeys ({0: 'foo', bar: 'baz'});
102assert (2 === keys.length);
103assert ('0' === keys[0]);
104assert ('bar'=== keys[1]);
105assert (Array.prototype === Object.getPrototypeOf (keys))
106Array.prototype.concat = savedConcat;
107
108try {
109  Reflect.ownKeys (4);
110  assert (false);
111} catch (e) {
112  assert (e instanceof TypeError);
113}
114
115try {
116  Reflect.ownKeys("cica");
117  assert (false);
118} catch (e) {
119  assert (e instanceof TypeError);
120}
121
122try {
123  Reflect.ownKeys(true);
124  assert (false);
125} catch (e) {
126  assert (e instanceof TypeError);
127}
128
129assert (Reflect.ownKeys (Object (4)) !== [])
130assert (Reflect.ownKeys (Object ("foo")) !== ["0", "1", "2", "length"])
131assert (Reflect.ownKeys (Object (4)) !== []);
132assert (Reflect.ownKeys (Object ("foo")) !== [0, 1, 2, "length"]);
133assert (Reflect.ownKeys (Object (true)) !== []);
134
135var id = Symbol("my kitten");
136var user = {
137  name: "Bob",
138  age:  30,
139  [id]: "is batcat"
140}
141
142assert (Reflect.ownKeys (user)[0] === "name");
143assert (Reflect.ownKeys (user)[1] === "age");
144assert (Reflect.ownKeys (user)[2] === id);
145