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 = { target: 1 }; |
| 6 | target.__proto__ = {}; |
| 7 | var handler = { handler: 1 }; |
| 8 | var proxy = new Proxy(target, handler); |
| 9 | |
| 10 | assertSame(Object.getPrototypeOf(proxy), target.__proto__ ); |
| 11 | |
| 12 | target.__proto__ = []; |
| 13 | assertSame(Object.getPrototypeOf(proxy), target.__proto__); |
| 14 | |
| 15 | handler.getPrototypeOf = function() { |
| 16 | return 1; |
| 17 | } |
| 18 | assertThrows(function() { Object.getPrototypeOf(proxy) }, TypeError); |
| 19 | |
| 20 | var target_prototype = {a:1, b:2}; |
| 21 | handler.getPrototypeOf = function() { |
| 22 | return target_prototype ; |
| 23 | } |
| 24 | assertSame(Object.getPrototypeOf(proxy), target_prototype); |
| 25 | |
| 26 | // Test with proxy target: |
| 27 | var proxy2 = new Proxy(proxy, {'handler':1}); |
| 28 | assertSame(Object.getPrototypeOf(proxy2), target_prototype); |
| 29 | |
| 30 | // Test with Proxy handler: |
| 31 | var proxy3_prototype = {'proto3':true}; |
| 32 | var handler_proxy = new Proxy({ |
| 33 | getPrototypeOf: function() { return proxy3_prototype } |
| 34 | }, {}); |
| 35 | var proxy3 = new Proxy(target, handler_proxy); |
| 36 | assertSame(Object.getPrototypeOf(proxy3), proxy3_prototype); |
| 37 | |
| 38 | |
| 39 | // Some tests with Object.prototype.isPrototypeOf |
| 40 | |
| 41 | (function () { |
| 42 | var object = {}; |
| 43 | var handler = {}; |
| 44 | var proto = new Proxy({}, handler); |
| 45 | object.__proto__ = proto; |
| 46 | |
| 47 | assertTrue(proto.isPrototypeOf(object)); |
| 48 | assertTrue(Object.prototype.isPrototypeOf.call(proto, object)); |
| 49 | |
| 50 | handler.getPrototypeOf = function () { return Object.prototype }; |
| 51 | assertTrue(proto.isPrototypeOf(object)); |
| 52 | assertTrue(Object.prototype.isPrototypeOf.call(proto, object)); |
| 53 | assertTrue(Object.prototype.isPrototypeOf(object)); |
| 54 | assertFalse(Object.prototype.isPrototypeOf.call(Array.prototype, object)); |
| 55 | assertFalse(Array.prototype.isPrototypeOf(object)); |
| 56 | |
| 57 | handler.getPrototypeOf = function () { return object }; |
| 58 | assertTrue(Object.prototype.isPrototypeOf.call(proto, object)); |
| 59 | assertTrue(proto.isPrototypeOf(object)); |
| 60 | assertTrue(Object.prototype.isPrototypeOf.call(object, object)); |
| 61 | assertTrue(object.isPrototypeOf(object)); |
| 62 | |
| 63 | handler.getPrototypeOf = function () { throw "foo" }; |
| 64 | assertTrue(proto.isPrototypeOf(object)); |
| 65 | assertTrue(Object.prototype.isPrototypeOf.call(proto, object)); |
| 66 | assertThrows(()=> Object.prototype.isPrototypeOf(object)); |
| 67 | assertThrows(()=> Object.prototype.isPrototypeOf.call(Array.prototype, object)); |
| 68 | assertThrows(()=> Array.prototype.isPrototypeOf(object)); |
| 69 | })(); |
| 70 | |
| 71 | (function () { |
| 72 | var handler = {}; |
| 73 | var object = new Proxy({}, handler); |
| 74 | var proto = {}; |
| 75 | |
| 76 | assertFalse(Object.prototype.isPrototypeOf.call(object, object)); |
| 77 | assertFalse(Object.prototype.isPrototypeOf.call(proto, object)); |
| 78 | assertTrue(Object.prototype.isPrototypeOf.call(Object.prototype, object)); |
| 79 | |
| 80 | handler.getPrototypeOf = function () { return proto }; |
| 81 | assertTrue(Object.prototype.isPrototypeOf.call(proto, object)); |
| 82 | assertFalse(Object.prototype.isPrototypeOf.call({}, object)); |
| 83 | assertTrue(Object.prototype.isPrototypeOf.call(Object.prototype, object)); |
| 84 | |
| 85 | handler.getPrototypeOf = function () { return object }; |
| 86 | assertTrue(Object.prototype.isPrototypeOf.call(object, object)); |
| 87 | |
| 88 | handler.getPrototypeOf = function () { throw "foo" }; |
| 89 | assertThrows(()=> Object.prototype.isPrototypeOf.call(object, object)); |
| 90 | assertThrows(()=> Object.prototype.isPrototypeOf(object)); |
| 91 | })(); |