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_one": 1 |
| 7 | }; |
| 8 | target.__proto__ = { |
| 9 | "target_two": 2 |
| 10 | }; |
| 11 | var handler = { |
| 12 | has: function(target, name) { |
| 13 | return name == "present"; |
| 14 | } |
| 15 | } |
| 16 | |
| 17 | var proxy = new Proxy(target, handler); |
| 18 | |
| 19 | // Test simple cases. |
| 20 | assertTrue("present" in proxy); |
| 21 | assertFalse("nonpresent" in proxy); |
| 22 | |
| 23 | // Test interesting algorithm steps: |
| 24 | |
| 25 | // Step 7: Fall through to target if trap is undefined. |
| 26 | handler.has = undefined; |
| 27 | assertTrue("target_one" in proxy); |
| 28 | assertTrue("target_two" in proxy); |
| 29 | assertFalse("in_your_dreams" in proxy); |
| 30 | |
| 31 | // Step 8: Result is converted to boolean. |
| 32 | var result = 1; |
| 33 | handler.has = function(t, n) { return result; } |
| 34 | assertTrue("foo" in proxy); |
| 35 | result = {}; |
| 36 | assertTrue("foo" in proxy); |
| 37 | result = undefined; |
| 38 | assertFalse("foo" in proxy); |
| 39 | result = "string"; |
| 40 | assertTrue("foo" in proxy); |
| 41 | |
| 42 | // Step 9b i. Trap result must confirm presence of non-configurable properties |
| 43 | // of the target. |
| 44 | Object.defineProperty(target, "nonconf", {value: 1, configurable: false}); |
| 45 | result = false; |
| 46 | assertThrows("'nonconf' in proxy", TypeError); |
| 47 | |
| 48 | // Step 9b iii. Trap result must confirm presence of all own properties of |
| 49 | // non-extensible targets. |
| 50 | Object.preventExtensions(target); |
| 51 | assertThrows("'nonconf' in proxy", TypeError); |
| 52 | assertThrows("'target_one' in proxy", TypeError); |
| 53 | assertFalse("target_two" in proxy); |
| 54 | assertFalse("in_your_dreams" in proxy); |
| 55 | |
| 56 | // Regression test for crbug.com/570120 (stray JSObject::cast). |
| 57 | (function TestHasPropertyFastPath() { |
| 58 | var proxy = new Proxy({}, {}); |
| 59 | var object = Object.create(proxy); |
| 60 | object.hasOwnProperty(0); |
| 61 | })(); |