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 | |
| 5 | // Flags: --harmony-proxies --harmony-reflect |
| 6 | |
| 7 | var target = { |
| 8 | "target_one": 1 |
| 9 | }; |
| 10 | target.__proto__ = { |
| 11 | "target_proto_two": 2 |
| 12 | }; |
| 13 | var handler = { |
| 14 | ownKeys: function(target) { |
| 15 | return ["foo", "bar"]; |
| 16 | } |
| 17 | } |
| 18 | |
| 19 | var proxy = new Proxy(target, handler); |
| 20 | |
| 21 | // Simple case. |
| 22 | assertEquals(["foo", "bar"], Reflect.ownKeys(proxy)); |
| 23 | |
| 24 | // Test interesting steps of the algorithm: |
| 25 | |
| 26 | // Step 6: Fall through to target.[[OwnPropertyKeys]] if the trap is undefined. |
| 27 | handler.ownKeys = undefined; |
| 28 | assertEquals(["target_one"], Reflect.ownKeys(proxy)); |
| 29 | |
| 30 | // Step 7: Throwing traps don't crash. |
| 31 | handler.ownKeys = function(target) { throw 1; }; |
| 32 | assertThrows("Reflect.ownKeys(proxy)"); |
| 33 | |
| 34 | // Step 8: CreateListFromArrayLike error cases: |
| 35 | // Returning a non-Object throws. |
| 36 | var keys = 1; |
| 37 | handler.ownKeys = function(target) { return keys; }; |
| 38 | assertThrows("Reflect.ownKeys(proxy)", TypeError); |
| 39 | keys = "string"; |
| 40 | assertThrows("Reflect.ownKeys(proxy)", TypeError); |
| 41 | keys = Symbol("foo"); |
| 42 | assertThrows("Reflect.ownKeys(proxy)", TypeError); |
| 43 | keys = null; |
| 44 | assertThrows("Reflect.ownKeys(proxy)", TypeError); |
| 45 | |
| 46 | // "length" property is honored. |
| 47 | keys = { 0: "a", 1: "b", 2: "c" }; |
| 48 | keys.length = 0; |
| 49 | assertEquals([], Reflect.ownKeys(proxy)); |
| 50 | keys.length = 1; |
| 51 | assertEquals(["a"], Reflect.ownKeys(proxy)); |
| 52 | keys.length = 3; |
| 53 | assertEquals(["a", "b", "c"], Reflect.ownKeys(proxy)); |
| 54 | // The spec wants to allow lengths up to 2^53, but we can't allocate arrays |
| 55 | // of that size, so we throw even for smaller values. |
| 56 | keys.length = Math.pow(2, 33); |
| 57 | assertThrows("Reflect.ownKeys(proxy)", RangeError); |
| 58 | |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame^] | 59 | // Check that we allow duplicated keys. |
| 60 | keys = ['a', 'a', 'a'] |
| 61 | assertEquals(keys, Reflect.ownKeys(proxy)); |
| 62 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 63 | // Non-Name results throw. |
| 64 | keys = [1]; |
| 65 | assertThrows("Reflect.ownKeys(proxy)", TypeError); |
| 66 | keys = [{}]; |
| 67 | assertThrows("Reflect.ownKeys(proxy)", TypeError); |
| 68 | keys = [{toString: function() { return "foo"; }}]; |
| 69 | assertThrows("Reflect.ownKeys(proxy)", TypeError); |
| 70 | keys = [null]; |
| 71 | assertThrows("Reflect.ownKeys(proxy)", TypeError); |
| 72 | |
| 73 | // Step 17a: The trap result must include all non-configurable keys. |
| 74 | Object.defineProperty(target, "nonconf", {value: 1, configurable: false}); |
| 75 | keys = ["foo"]; |
| 76 | assertThrows("Reflect.ownKeys(proxy)", TypeError); |
| 77 | keys = ["nonconf"]; |
| 78 | assertEquals(keys, Reflect.ownKeys(proxy)); |
| 79 | |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame^] | 80 | // Check that we allow duplicated keys. |
| 81 | keys = ['nonconf', 'nonconf', 'nonconf'] |
| 82 | assertEquals(keys, Reflect.ownKeys(proxy)); |
| 83 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 84 | // Step 19a: The trap result must all keys of a non-extensible target. |
| 85 | Object.preventExtensions(target); |
| 86 | assertThrows("Reflect.ownKeys(proxy)", TypeError); |
| 87 | keys = ["nonconf", "target_one"]; |
| 88 | assertEquals(keys, Reflect.ownKeys(proxy)); |
| 89 | |
| 90 | // Step 20: The trap result must not add keys to a non-extensible target. |
| 91 | keys = ["nonconf", "target_one", "fantasy"]; |
| 92 | assertThrows("Reflect.ownKeys(proxy)", TypeError); |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame^] | 93 | |
| 94 | // Check that we allow duplicated keys. |
| 95 | keys = ['nonconf', 'target_one', 'nonconf', 'nonconf', 'target_one',] |
| 96 | assertEquals(keys, Reflect.ownKeys(proxy)); |