Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 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 |
| 3 | // found in the LICENSE file. |
| 4 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 5 | var target = { |
| 6 | target: 1 |
| 7 | }; |
| 8 | target.__proto__ = { |
| 9 | target_proto: 2 |
| 10 | }; |
| 11 | |
| 12 | var handler = { |
| 13 | ownKeys: function(target) { |
| 14 | return ["foo", "bar", Symbol("baz"), "non-enum", "not-found"]; |
| 15 | }, |
| 16 | getOwnPropertyDescriptor: function(target, name) { |
| 17 | if (name == "non-enum") return {configurable: true}; |
| 18 | if (name == "not-found") return undefined; |
| 19 | return {enumerable: true, configurable: true}; |
| 20 | } |
| 21 | } |
| 22 | |
| 23 | var proxy = new Proxy(target, handler); |
| 24 | |
| 25 | // Object.keys() ignores symbols and non-enumerable keys. |
| 26 | assertEquals(["foo", "bar"], Object.keys(proxy)); |
| 27 | |
| 28 | // Edge case: no properties left after filtering. |
| 29 | handler.getOwnPropertyDescriptor = undefined; |
| 30 | assertEquals([], Object.keys(proxy)); |
| 31 | |
| 32 | // Throwing shouldn't crash. |
| 33 | handler.getOwnPropertyDescriptor = function() { throw new Number(1); }; |
| 34 | assertThrows("Object.keys(proxy)", Number); |
| 35 | |
| 36 | // Fall through to target if there is no trap. |
| 37 | handler.ownKeys = undefined; |
| 38 | assertEquals(["target"], Object.keys(proxy)); |
| 39 | assertEquals(["target"], Object.keys(target)); |
Ben Murdoch | c561043 | 2016-08-08 18:44:38 +0100 | [diff] [blame] | 40 | |
| 41 | var proxy2 = new Proxy(proxy, {}); |
| 42 | assertEquals(["target"], Object.keys(proxy2)); |
| 43 | |
| 44 | |
| 45 | (function testForSymbols() { |
| 46 | var symbol = Symbol(); |
| 47 | var p = new Proxy({}, {ownKeys() { return ["1", symbol, "2"] }}); |
| 48 | assertEquals(["1","2"], Object.getOwnPropertyNames(p)); |
| 49 | assertEquals([symbol], Object.getOwnPropertySymbols(p)); |
| 50 | })(); |
Ben Murdoch | 61f157c | 2016-09-16 13:49:30 +0100 | [diff] [blame] | 51 | |
| 52 | (function testNoProxyTraps() { |
| 53 | var test_sym = Symbol("sym1"); |
| 54 | var test_sym2 = Symbol("sym2"); |
| 55 | var target = { |
| 56 | one: 1, |
| 57 | two: 2, |
| 58 | [test_sym]: 4, |
| 59 | 0: 0, |
| 60 | }; |
| 61 | Object.defineProperty( |
| 62 | target, "non-enum", |
| 63 | { enumerable: false, value: "nope", configurable: true, writable: true }); |
| 64 | target.__proto__ = { |
| 65 | target_proto: 3, |
| 66 | 1: 1, |
| 67 | [test_sym2]: 5 |
| 68 | }; |
| 69 | Object.defineProperty( |
| 70 | target.__proto__, "non-enum2", |
| 71 | { enumerable: false, value: "nope", configurable: true, writable: true }); |
| 72 | var proxy = new Proxy(target, {}); |
| 73 | |
| 74 | assertEquals(["0", "one", "two"], Object.keys(proxy)); |
| 75 | assertEquals(["0", "one", "two", "non-enum"], |
| 76 | Object.getOwnPropertyNames(proxy)); |
| 77 | assertEquals([test_sym], Object.getOwnPropertySymbols(proxy)); |
| 78 | })(); |