• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1// Copyright JS Foundation and other contributors, http://js.foundation
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7//     http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15// Copyright 2015 the V8 project authors. All rights reserved.
16// Use of this source code is governed by a BSD-style license that can be
17// found in the LICENSE file.
18
19var target = {};
20var handler = { getPrototypeOf (target) {
21  throw 42;
22}};
23
24var proxy = new Proxy(target, handler);
25
26try {
27  // 19.1.2.9.2
28  Object.getPrototypeOf(proxy);
29  assert(false);
30} catch (e) {
31  assert(e === 42);
32}
33
34try {
35  // 19.1.3.3
36  Object.prototype.isPrototypeOf(proxy);
37  assert(false);
38} catch (e) {
39  assert(e === 42);
40}
41
42(function () {
43  class e extends Array {};
44  function f () {};
45  function g () {};
46
47  Object.setPrototypeOf(g, proxy);
48
49  // 7.3.19.7.b
50  try {
51    g instanceof f;
52    assert(false);
53  } catch (e) {
54    assert(e === 42);
55  }
56
57  // ecma_op_implicit_class_constructor_has_instance [[GetPrototypeOf]]
58  try {
59    g instanceof e;
60    assert(false);
61  } catch (e) {
62    assert(e === 42);
63  }
64})();
65
66try {
67  // 9.4.1.3.3
68  Function.prototype.bind.call(proxy);
69  assert(false);
70} catch (e) {
71  assert(e instanceof TypeError);
72}
73
74// test basic functionality
75var target = {};
76var handler = {
77  getPrototypeOf(target) {
78    return Array.prototype;
79  }
80}
81var proxy = new Proxy(target, handler);
82
83assert(Object.getPrototypeOf(proxy) === Array.prototype);
84assert(Reflect.getPrototypeOf(proxy) === Array.prototype);
85assert(Array.prototype.isPrototypeOf(proxy));
86assert(proxy instanceof Array);
87
88var obj = Object.preventExtensions({});
89assert(Object.getPrototypeOf(obj) === Object.prototype);
90
91var handler = {
92  getPrototypeOf(target) {
93    return Object.prototype;
94  }
95}
96var proxy = new Proxy(target, handler);
97assert(Object.getPrototypeOf(proxy) === Object.prototype);
98
99// test with no trap
100var target = {};
101var handler = {};
102var proxy = new Proxy(target, handler);
103
104assert(Object.getPrototypeOf(proxy) === Object.prototype);
105
106// test with "undefined" trap
107var target = {};
108var handler = { getPrototypeOf: null };
109var proxy = new Proxy(target, handler);
110
111assert(Object.getPrototypeOf(proxy) === Object.prototype);
112
113// test with invalid trap
114var target = {};
115var handler = { getPrototypeOf: 42 };
116var proxy = new Proxy(target, handler);
117
118try {
119  Object.getPrototypeOf(proxy)
120  assert(false);
121} catch (e) {
122  assert(e instanceof TypeError);
123}
124
125// test when target is proxy
126var target = {};
127var handler = {};
128var proxy = new Proxy(target, handler);
129
130var target_prototype = {};
131handler.getPrototypeOf = function() {
132  return target_prototype ;
133}
134
135var proxy2 = new Proxy(proxy, handler);
136assert(Object.getPrototypeOf(proxy2) === target_prototype);
137
138// test when invariants gets violated
139var target = {};
140var handler = {
141  getPrototypeOf(target) {
142    return 'foo';
143  }
144}
145var proxy = new Proxy(target, handler);
146
147try {
148  Object.getPrototypeOf(proxy);
149  assert(false);
150} catch (e) {
151  assert(e instanceof TypeError);
152}
153
154var target = Object.preventExtensions({});
155var handler = {
156  getPrototypeOf(target) {
157    return {};
158  }
159}
160
161var proxy = new Proxy(target, handler);
162
163try {
164  Object.getPrototypeOf(proxy);
165  assert(false);
166} catch (e) {
167  assert(e instanceof TypeError);
168}
169