OLD | NEW |
---|---|
1 // Copyright 2015 the V8 project authors. All rights reserved. | 1 // Copyright 2015 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 var target = { | 5 (function testObjectKeys() { |
6 target: 1 | 6 var target = { |
7 }; | 7 target: 1 |
8 target.__proto__ = { | 8 }; |
9 target_proto: 2 | 9 target.__proto__ = { |
10 }; | 10 target_proto: 2 |
11 }; | |
11 | 12 |
12 var handler = { | 13 var handler = { |
13 ownKeys: function(target) { | 14 ownKeys: function(target) { |
14 return ["foo", "bar", Symbol("baz"), "non-enum", "not-found"]; | 15 return ["foo", "bar", Symbol("baz"), "non-enum", "not-found"]; |
15 }, | 16 }, |
16 getOwnPropertyDescriptor: function(target, name) { | 17 getOwnPropertyDescriptor: function(target, name) { |
17 if (name == "non-enum") return {configurable: true}; | 18 if (name == "non-enum") return {configurable: true}; |
18 if (name == "not-found") return undefined; | 19 if (name == "not-found") return undefined; |
19 return {enumerable: true, configurable: true}; | 20 return {enumerable: true, configurable: true}; |
21 } | |
20 } | 22 } |
21 } | |
22 | 23 |
23 var proxy = new Proxy(target, handler); | 24 var proxy = new Proxy(target, handler); |
24 | 25 |
25 // Object.keys() ignores symbols and non-enumerable keys. | 26 // Object.keys() ignores symbols and non-enumerable keys. |
26 assertEquals(["foo", "bar"], Object.keys(proxy)); | 27 assertEquals(["foo", "bar"], Object.keys(proxy)); |
27 | 28 |
28 // Edge case: no properties left after filtering. | 29 // Edge case: no properties left after filtering. |
29 handler.getOwnPropertyDescriptor = undefined; | 30 handler.getOwnPropertyDescriptor = undefined; |
30 assertEquals([], Object.keys(proxy)); | 31 assertEquals([], Object.keys(proxy)); |
31 | 32 |
32 // Throwing shouldn't crash. | 33 // Throwing shouldn't crash. |
33 handler.getOwnPropertyDescriptor = function() { throw new Number(1); }; | 34 handler.getOwnPropertyDescriptor = function() { throw new Number(1); }; |
34 assertThrows("Object.keys(proxy)", Number); | 35 assertThrows("Object.keys(proxy)", Number); |
35 | 36 |
36 // Fall through to target if there is no trap. | 37 // Fall through to target if there is no trap. |
adamk
2016/07/29 21:09:30
This comment should go below, and this comment sho
| |
37 handler.ownKeys = undefined; | 38 handler.ownKeys = undefined; |
38 assertEquals(["target"], Object.keys(proxy)); | 39 assertThrows("Object.keys(proxy)", Number); |
39 assertEquals(["target"], Object.keys(target)); | |
40 | 40 |
41 var proxy2 = new Proxy(proxy, {}); | 41 handler.getOwnPropertyDescriptor = undefined; |
42 assertEquals(["target"], Object.keys(proxy2)); | 42 assertEquals(["target"], Object.keys(proxy)); |
43 assertEquals(["target"], Object.keys(target)); | |
43 | 44 |
45 var proxy2 = new Proxy(proxy, {}); | |
46 assertEquals(["target"], Object.keys(proxy2)); | |
47 })(); | |
44 | 48 |
45 (function testForSymbols() { | 49 (function testForSymbols() { |
46 var symbol = Symbol(); | 50 var symbol = Symbol(); |
47 var p = new Proxy({}, {ownKeys() { return ["1", symbol, "2"] }}); | 51 var p = new Proxy({}, {ownKeys() { return ["1", symbol, "2"] }}); |
48 assertEquals(["1","2"], Object.getOwnPropertyNames(p)); | 52 assertEquals(["1","2"], Object.getOwnPropertyNames(p)); |
49 assertEquals([symbol], Object.getOwnPropertySymbols(p)); | 53 assertEquals([symbol], Object.getOwnPropertySymbols(p)); |
50 })(); | 54 })(); |
51 | 55 |
52 (function testNoProxyTraps() { | 56 (function testNoProxyTraps() { |
53 var test_sym = Symbol("sym1"); | 57 var test_sym = Symbol("sym1"); |
(...skipping 15 matching lines...) Expand all Loading... | |
69 Object.defineProperty( | 73 Object.defineProperty( |
70 target.__proto__, "non-enum2", | 74 target.__proto__, "non-enum2", |
71 { enumerable: false, value: "nope", configurable: true, writable: true }); | 75 { enumerable: false, value: "nope", configurable: true, writable: true }); |
72 var proxy = new Proxy(target, {}); | 76 var proxy = new Proxy(target, {}); |
73 | 77 |
74 assertEquals(["0", "one", "two"], Object.keys(proxy)); | 78 assertEquals(["0", "one", "two"], Object.keys(proxy)); |
75 assertEquals(["0", "one", "two", "non-enum"], | 79 assertEquals(["0", "one", "two", "non-enum"], |
76 Object.getOwnPropertyNames(proxy)); | 80 Object.getOwnPropertyNames(proxy)); |
77 assertEquals([test_sym], Object.getOwnPropertySymbols(proxy)); | 81 assertEquals([test_sym], Object.getOwnPropertySymbols(proxy)); |
78 })(); | 82 })(); |
OLD | NEW |