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