OLD | NEW |
1 // Copyright 2016 the V8 project authors. All rights reserved. | 1 // Copyright 2016 the V8 project authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 // Flags: --harmony-object-values-entries --harmony-proxies --harmony-reflect | 5 // Flags: --harmony-object-values-entries --harmony-proxies --harmony-reflect |
6 // Flags: --allow-natives-syntax | |
7 | 6 |
8 function TestMeta() { | 7 function TestMeta() { |
9 assertEquals(1, Object.values.length); | 8 assertEquals(1, Object.values.length); |
10 assertEquals(Function.prototype, Object.getPrototypeOf(Object.values)); | 9 assertEquals(Function.prototype, Object.getPrototypeOf(Object.values)); |
11 assertEquals("values", Object.values.name); | |
12 | |
13 var descriptor = Object.getOwnPropertyDescriptor(Object, "values"); | |
14 assertTrue(descriptor.writable); | |
15 assertFalse(descriptor.enumerable); | |
16 assertTrue(descriptor.configurable); | |
17 | |
18 assertThrows(() => new Object.values({}), TypeError); | |
19 } | 10 } |
20 TestMeta(); | 11 TestMeta(); |
21 | 12 |
22 | 13 |
23 function TestBasic() { | 14 function TestBasic() { |
24 var x = 16; | 15 var x = 16; |
25 var O = { | 16 var O = { |
26 d: 1, | 17 d: 1, |
27 c: 3, | 18 c: 3, |
28 [Symbol.iterator]: void 0, | 19 [Symbol.iterator]: void 0, |
29 0: 123, | 20 0: 123, |
30 1000: 456, | 21 1000: 456, |
31 [x * x]: "ducks", | 22 [x * x]: "ducks", |
32 [`0x${(x * x).toString(16)}`]: "quack" | 23 [`0x${(x * x).toString(16)}`]: "quack" |
33 }; | 24 }; |
34 O.a = 2; | 25 O.a = 2; |
35 O.b = 4; | 26 O.b = 4; |
36 Object.defineProperty(O, "HIDDEN", { enumerable: false, value: NaN }); | 27 Object.defineProperty(O, "HIDDEN", { enumerable: false, value: NaN }); |
37 assertEquals([123, "ducks", 456, 1, 3, "quack", 2, 4], Object.values(O)); | 28 assertEquals([123, "ducks", 456, 1, 3, "quack", 2, 4], Object.values(O)); |
38 assertEquals(Object.values(O), Object.keys(O).map(key => O[key])); | 29 assertEquals(Object.values(O), Object.keys(O).map(key => O[key])); |
39 | |
40 assertTrue(Array.isArray(Object.values({}))); | |
41 assertEquals(0, Object.values({}).length); | |
42 } | 30 } |
43 TestBasic(); | 31 TestBasic(); |
44 | 32 |
45 | 33 |
46 function TestToObject() { | |
47 assertThrows(function() { Object.values(); }, TypeError); | |
48 assertThrows(function() { Object.values(null); }, TypeError); | |
49 assertThrows(function() { Object.values(void 0); }, TypeError); | |
50 } | |
51 TestToObject(); | |
52 | |
53 | |
54 function TestOrder() { | 34 function TestOrder() { |
55 var O = { | 35 var O = { |
56 a: 1, | 36 a: 1, |
57 [Symbol.iterator]: null | 37 [Symbol.iterator]: null |
58 }; | 38 }; |
59 O[456] = 123; | 39 O[456] = 123; |
60 Object.defineProperty(O, "HIDDEN", { enumerable: false, value: NaN }); | 40 Object.defineProperty(O, "HIDDEN", { enumerable: false, value: NaN }); |
61 var priv = %CreatePrivateSymbol("Secret"); | |
62 O[priv] = 56; | |
63 | 41 |
64 var log = []; | 42 var log = []; |
65 var P = new Proxy(O, { | 43 var P = new Proxy(O, { |
66 ownKeys(target) { | 44 ownKeys(target) { |
67 log.push("[[OwnPropertyKeys]]"); | 45 log.push("[[OwnPropertyKeys]]"); |
68 return Reflect.ownKeys(target); | 46 return Reflect.ownKeys(target); |
69 }, | 47 }, |
70 get(target, name) { | 48 get(target, name) { |
71 log.push(`[[Get]](${JSON.stringify(name)})`); | 49 log.push(`[[Get]](${JSON.stringify(name)})`); |
72 return Reflect.get(target, name); | 50 return Reflect.get(target, name); |
73 }, | 51 }, |
74 getOwnPropertyDescriptor(target, name) { | |
75 log.push(`[[GetOwnProperty]](${JSON.stringify(name)})`); | |
76 return Reflect.getOwnPropertyDescriptor(target, name); | |
77 }, | |
78 set(target, name, value) { | 52 set(target, name, value) { |
79 assertUnreachable(); | 53 assertUnreachable(); |
80 } | 54 } |
81 }); | 55 }); |
82 | 56 |
83 assertEquals([123, 1], Object.values(P)); | 57 assertEquals([123, 1], Object.values(P)); |
84 assertEquals([ | 58 assertEquals([ |
85 "[[OwnPropertyKeys]]", | 59 "[[OwnPropertyKeys]]", |
86 "[[GetOwnProperty]](\"456\")", | |
87 "[[Get]](\"456\")", | 60 "[[Get]](\"456\")", |
88 "[[GetOwnProperty]](\"a\")", | 61 "[[Get]](\"a\")" |
89 "[[Get]](\"a\")", | |
90 "[[GetOwnProperty]](\"HIDDEN\")" | |
91 ], log); | 62 ], log); |
92 } | 63 } |
93 TestOrder(); | 64 TestOrder(); |
94 | |
95 | |
96 function TestOrderWithDuplicates() { | |
97 var O = { | |
98 a: 1, | |
99 [Symbol.iterator]: null | |
100 }; | |
101 O[456] = 123; | |
102 Object.defineProperty(O, "HIDDEN", { enumerable: false, value: NaN }); | |
103 O[priv] = 56; | |
104 var priv = %CreatePrivateSymbol("private"); | |
105 | |
106 var log = []; | |
107 var P = new Proxy(O, { | |
108 ownKeys(target) { | |
109 log.push("[[OwnPropertyKeys]]"); | |
110 return [ "a", Symbol.iterator, "a", "456", "HIDDEN", "HIDDEN", "456" ]; | |
111 }, | |
112 get(target, name) { | |
113 log.push(`[[Get]](${JSON.stringify(name)})`); | |
114 return Reflect.get(target, name); | |
115 }, | |
116 getOwnPropertyDescriptor(target, name) { | |
117 log.push(`[[GetOwnProperty]](${JSON.stringify(name)})`); | |
118 return Reflect.getOwnPropertyDescriptor(target, name); | |
119 }, | |
120 set(target, name, value) { | |
121 assertUnreachable(); | |
122 } | |
123 }); | |
124 | |
125 assertEquals([1, 1, 123, 123], Object.values(P)); | |
126 assertEquals([ | |
127 "[[OwnPropertyKeys]]", | |
128 "[[GetOwnProperty]](\"a\")", | |
129 "[[Get]](\"a\")", | |
130 "[[GetOwnProperty]](\"a\")", | |
131 "[[Get]](\"a\")", | |
132 "[[GetOwnProperty]](\"456\")", | |
133 "[[Get]](\"456\")", | |
134 "[[GetOwnProperty]](\"HIDDEN\")", | |
135 "[[GetOwnProperty]](\"HIDDEN\")", | |
136 "[[GetOwnProperty]](\"456\")", | |
137 "[[Get]](\"456\")", | |
138 ], log); | |
139 } | |
140 TestOrderWithDuplicates(); | |
141 | |
142 | |
143 function TestPropertyFilter() { | |
144 var object = { prop3: 30 }; | |
145 object[2] = 40; | |
146 object["prop4"] = 50; | |
147 Object.defineProperty(object, "prop5", { value: 60, enumerable: true }); | |
148 Object.defineProperty(object, "prop6", { value: 70, enumerable: false }); | |
149 Object.defineProperty(object, "prop7", { | |
150 enumerable: true, get() { return 80; }}); | |
151 var sym = Symbol("prop8"); | |
152 object[sym] = 90; | |
153 | |
154 values = Object.values(object); | |
155 assertEquals(5, values.length); | |
156 assertEquals([40,30,50,60,80], values); | |
157 } | |
158 TestPropertyFilter(); | |
159 | |
160 | |
161 function TestWithProxy() { | |
162 var obj1 = {prop1:10}; | |
163 var proxy1 = new Proxy(obj1, { }); | |
164 assertEquals([10], Object.values(proxy1)); | |
165 | |
166 var obj2 = {}; | |
167 Object.defineProperty(obj2, "prop2", { value: 20, enumerable: true }); | |
168 Object.defineProperty(obj2, "prop3", { | |
169 get() { return 30; }, enumerable: true }); | |
170 var proxy2 = new Proxy(obj2, { | |
171 getOwnPropertyDescriptor(target, name) { | |
172 return Reflect.getOwnPropertyDescriptor(target, name); | |
173 } | |
174 }); | |
175 assertEquals([20, 30], Object.values(proxy2)); | |
176 | |
177 var obj3 = {}; | |
178 var count = 0; | |
179 var proxy3 = new Proxy(obj3, { | |
180 get(target, property, receiver) { | |
181 return count++ * 5; | |
182 }, | |
183 getOwnPropertyDescriptor(target, property) { | |
184 return { configurable: true, enumerable: true }; | |
185 }, | |
186 ownKeys(target) { | |
187 return [ "prop0", "prop1", Symbol("prop2"), Symbol("prop5") ]; | |
188 } | |
189 }); | |
190 assertEquals([0, 5], Object.values(proxy3)); | |
191 } | |
192 TestWithProxy(); | |
193 | |
194 | |
195 function TestMutateDuringEnumeration() { | |
196 var aDeletesB = { | |
197 get a() { | |
198 delete this.b; | |
199 return 1; | |
200 }, | |
201 b: 2 | |
202 }; | |
203 assertEquals([1], Object.values(aDeletesB)); | |
204 | |
205 var aRemovesB = { | |
206 get a() { | |
207 Object.defineProperty(this, "b", { enumerable: false }); | |
208 return 1; | |
209 }, | |
210 b: 2 | |
211 }; | |
212 assertEquals([1], Object.values(aRemovesB)); | |
213 | |
214 var aAddsB = { get a() { this.b = 2; return 1; } }; | |
215 assertEquals([1], Object.values(aAddsB)); | |
216 | |
217 var aMakesBEnumerable = {}; | |
218 Object.defineProperty(aMakesBEnumerable, "a", { | |
219 get() { | |
220 Object.defineProperty(this, "b", { enumerable: true }); | |
221 return 1; | |
222 }, | |
223 enumerable: true | |
224 }); | |
225 Object.defineProperty(aMakesBEnumerable, "b", { | |
226 value: 2, configurable:true, enumerable: false }); | |
227 assertEquals([1, 2], Object.values(aMakesBEnumerable)); | |
228 } | |
229 TestMutateDuringEnumeration(); | |
OLD | NEW |