OLD | NEW |
(Empty) | |
| 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 |
| 6 |
| 7 // Check basic call to trap. |
| 8 |
| 9 var g_target, g_name, g_desc; |
| 10 var handler = { |
| 11 defineProperty: function(target, name, desc) { |
| 12 g_target = target; |
| 13 g_name = name; |
| 14 g_desc = desc; |
| 15 return true; |
| 16 } |
| 17 } |
| 18 var target = {} |
| 19 var proxy = new Proxy(target, handler); |
| 20 var desc = { value: 1, writable: true, configurable: true, enumerable: true }; |
| 21 Object.defineProperty(proxy, "foo", desc); |
| 22 assertSame(target, g_target); |
| 23 assertEquals("foo", g_name); |
| 24 assertEquals(desc, g_desc); |
| 25 |
| 26 // Check specific steps in the spec: |
| 27 |
| 28 // Step 6: Trap isn't callable. |
| 29 handler.defineProperty = 1; |
| 30 assertThrows("Object.defineProperty(proxy, 'foo', {value: 2})", TypeError); |
| 31 |
| 32 // Step 7: Trap is undefined. |
| 33 handler.defineProperty = undefined; |
| 34 Object.defineProperty(proxy, "prop1", desc); |
| 35 assertEquals(desc, Object.getOwnPropertyDescriptor(target, "prop1")); |
| 36 var target2 = {}; |
| 37 var proxy2 = new Proxy(target2, {}); |
| 38 Object.defineProperty(proxy2, "prop2", desc); |
| 39 assertEquals(desc, Object.getOwnPropertyDescriptor(target2, "prop2")); |
| 40 |
| 41 // Step 9: Property name is passed to the trap as a string. |
| 42 handler.defineProperty = function(t, name, d) { g_name = name; return true; }; |
| 43 Object.defineProperty(proxy, 0, desc); |
| 44 assertTrue(typeof g_name === "string"); |
| 45 assertEquals("0", g_name); |
| 46 |
| 47 // Step 10: Trap returns false. |
| 48 handler.defineProperty = function(t, n, d) { return false; } |
| 49 assertThrows("Object.defineProperty(proxy, 'foo', desc)", TypeError); |
| 50 |
| 51 // Step 15a: Trap returns true for adding a property to a non-extensible target. |
| 52 handler.defineProperty = function(t, n, d) { return true; } |
| 53 Object.freeze(target); |
| 54 assertThrows("Object.defineProperty(proxy, 'foo', desc)", TypeError); |
| 55 |
| 56 // Step 15b: Trap returns true for adding a non-configurable property. |
| 57 target = {}; |
| 58 proxy = new Proxy(target, handler); |
| 59 desc = {value: 1, writable: true, configurable: false, enumerable: true}; |
| 60 assertThrows("Object.defineProperty(proxy, 'foo', desc)", TypeError); |
| 61 // No exception is thrown if a non-configurable property exists on the target. |
| 62 Object.defineProperty(target, "nonconf", |
| 63 {value: 1, writable: true, configurable: false}); |
| 64 Object.defineProperty(proxy, "nonconf", {value: 2, configurable: false}); |
| 65 |
| 66 // Step 16a: Trap returns true for non-compatible property descriptor. |
| 67 Object.defineProperty(target, "foo", |
| 68 {value: 1, writable: false, configurable: false}); |
| 69 assertThrows("Object.defineProperty(proxy, 'foo', {value: 2})", TypeError); |
| 70 |
| 71 // Step 16b: Trap returns true for overwriting a configurable property |
| 72 // with a non-configurable descriptor. |
| 73 target.bar = "baz"; |
| 74 assertThrows("Object.defineProperty(proxy, 'bar', {configurable: false})", |
| 75 TypeError); |
OLD | NEW |