blob: a628f3fa15e92282930554976e4983da0fb2791a [file] [log] [blame]
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001// 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 Murdoch4a90d5f2016-03-22 12:00:34 +00005var target = { target: 1 };
6target.__proto__ = {};
7var handler = { handler: 1 };
8var proxy = new Proxy(target, handler);
9
10assertSame(Object.getPrototypeOf(proxy), target.__proto__ );
11
12target.__proto__ = [];
13assertSame(Object.getPrototypeOf(proxy), target.__proto__);
14
15handler.getPrototypeOf = function() {
16 return 1;
17}
18assertThrows(function() { Object.getPrototypeOf(proxy) }, TypeError);
19
20var target_prototype = {a:1, b:2};
21handler.getPrototypeOf = function() {
22 return target_prototype ;
23}
24assertSame(Object.getPrototypeOf(proxy), target_prototype);
25
26// Test with proxy target:
27var proxy2 = new Proxy(proxy, {'handler':1});
28assertSame(Object.getPrototypeOf(proxy2), target_prototype);
29
30// Test with Proxy handler:
31var proxy3_prototype = {'proto3':true};
32var handler_proxy = new Proxy({
33 getPrototypeOf: function() { return proxy3_prototype }
34}, {});
35var proxy3 = new Proxy(target, handler_proxy);
36assertSame(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})();