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 |
6 | 7 |
7 function TestMeta() { | 8 function TestMeta() { |
8 assertEquals(1, Object.values.length); | 9 assertEquals(1, Object.values.length); |
9 assertEquals(Function.prototype, Object.getPrototypeOf(Object.values)); | 10 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); |
10 } | 19 } |
11 TestMeta(); | 20 TestMeta(); |
12 | 21 |
13 | 22 |
14 function TestBasic() { | 23 function TestBasic() { |
15 var x = 16; | 24 var x = 16; |
16 var O = { | 25 var O = { |
17 d: 1, | 26 d: 1, |
18 c: 3, | 27 c: 3, |
19 [Symbol.iterator]: void 0, | 28 [Symbol.iterator]: void 0, |
20 0: 123, | 29 0: 123, |
21 1000: 456, | 30 1000: 456, |
22 [x * x]: "ducks", | 31 [x * x]: "ducks", |
23 [`0x${(x * x).toString(16)}`]: "quack" | 32 [`0x${(x * x).toString(16)}`]: "quack" |
24 }; | 33 }; |
25 O.a = 2; | 34 O.a = 2; |
26 O.b = 4; | 35 O.b = 4; |
27 Object.defineProperty(O, "HIDDEN", { enumerable: false, value: NaN }); | 36 Object.defineProperty(O, "HIDDEN", { enumerable: false, value: NaN }); |
28 assertEquals([123, "ducks", 456, 1, 3, "quack", 2, 4], Object.values(O)); | 37 assertEquals([123, "ducks", 456, 1, 3, "quack", 2, 4], Object.values(O)); |
29 assertEquals(Object.values(O), Object.keys(O).map(key => O[key])); | 38 assertEquals(Object.values(O), Object.keys(O).map(key => O[key])); |
| 39 |
| 40 assertTrue(Array.isArray(Object.values({}))); |
| 41 assertEquals(0, Object.values({}).length); |
30 } | 42 } |
31 TestBasic(); | 43 TestBasic(); |
32 | 44 |
33 | 45 |
| 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 |
34 function TestOrder() { | 54 function TestOrder() { |
35 var O = { | 55 var O = { |
36 a: 1, | 56 a: 1, |
37 [Symbol.iterator]: null | 57 [Symbol.iterator]: null |
38 }; | 58 }; |
39 O[456] = 123; | 59 O[456] = 123; |
40 Object.defineProperty(O, "HIDDEN", { enumerable: false, value: NaN }); | 60 Object.defineProperty(O, "HIDDEN", { enumerable: false, value: NaN }); |
| 61 var priv = %CreatePrivateSymbol("Secret"); |
| 62 O[priv] = 56; |
41 | 63 |
42 var log = []; | 64 var log = []; |
43 var P = new Proxy(O, { | 65 var P = new Proxy(O, { |
44 ownKeys(target) { | 66 ownKeys(target) { |
45 log.push("[[OwnPropertyKeys]]"); | 67 log.push("[[OwnPropertyKeys]]"); |
46 return Reflect.ownKeys(target); | 68 return Reflect.ownKeys(target); |
47 }, | 69 }, |
48 get(target, name) { | 70 get(target, name) { |
49 log.push(`[[Get]](${JSON.stringify(name)})`); | 71 log.push(`[[Get]](${JSON.stringify(name)})`); |
50 return Reflect.get(target, name); | 72 return Reflect.get(target, name); |
51 }, | 73 }, |
| 74 getOwnPropertyDescriptor(target, name) { |
| 75 log.push(`[[GetOwnProperty]](${JSON.stringify(name)})`); |
| 76 return Reflect.getOwnPropertyDescriptor(target, name); |
| 77 }, |
52 set(target, name, value) { | 78 set(target, name, value) { |
53 assertUnreachable(); | 79 assertUnreachable(); |
54 } | 80 } |
55 }); | 81 }); |
56 | 82 |
57 assertEquals([123, 1], Object.values(P)); | 83 assertEquals([123, 1], Object.values(P)); |
58 assertEquals([ | 84 assertEquals([ |
59 "[[OwnPropertyKeys]]", | 85 "[[OwnPropertyKeys]]", |
| 86 "[[GetOwnProperty]](\"456\")", |
60 "[[Get]](\"456\")", | 87 "[[Get]](\"456\")", |
61 "[[Get]](\"a\")" | 88 "[[GetOwnProperty]](\"a\")", |
| 89 "[[Get]](\"a\")", |
| 90 "[[GetOwnProperty]](\"HIDDEN\")" |
62 ], log); | 91 ], log); |
63 } | 92 } |
64 TestOrder(); | 93 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 |