Chromium Code Reviews| 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})"); | |
|
neis
2015/11/17 21:37:40
Here and below, check that it throws a TypeError.
Jakob Kummerow
2015/11/18 14:35:50
Done.
| |
| 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)"); | |
| 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)"); | |
| 55 | |
| 56 // Step 15b: Trap returns true for adding a non-configurable property. | |
| 57 target = {}; | |
| 58 desc = {value: 1, writable: true, configurable: false, enumerable: true}; | |
| 59 assertThrows("Object.defineProperty(proxy, 'foo', desc)"); | |
|
neis
2015/11/17 21:37:40
Maybe also test the special case of this special c
Jakob Kummerow
2015/11/18 14:35:50
Done.
| |
| 60 | |
| 61 // Step 16a: Trap returns true for non-compatible property descriptor. | |
| 62 Object.defineProperty(target, "foo", | |
| 63 {value: 1, writable: false, configurable: false}); | |
| 64 assertThrows("Object.defineProperty(proxy, 'foo', {value: 2})"); | |
| 65 | |
| 66 // Step 16b: Trap returns true for overwriting a configurable property | |
| 67 // with a non-configurable descriptor. | |
| 68 target.bar = "baz"; | |
| 69 assertThrows("Object.defineProperty(proxy, 'bar', {configurable: false})"); | |
| OLD | NEW |