• 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 = { setPrototypeOf (target) {
21  throw 42;
22}};
23
24var proxy = new Proxy(target, handler);
25
26try {
27  // 19.1.2.18
28  Object.setPrototypeOf(proxy, {});
29  assert(false);
30} catch (e) {
31  assert(e === 42);
32}
33
34// test basic funcionality
35var targetProto = {};
36var target = {
37  foo: false
38};
39
40var handler = {
41  setPrototypeOf(target, targetrProto) {
42    target.foo = true;
43    return false;
44  }
45};
46
47var proxy = new Proxy(target, handler);
48
49assert(Reflect.setPrototypeOf(proxy, targetProto) === false);
50assert(target.foo === true);
51
52try {
53  Object.setPrototypeOf(proxy, targetProto);
54  assert(false);
55} catch (e) {
56  assert(e instanceof TypeError);
57}
58
59try {
60  Object.setPrototypeOf(proxy, undefined);
61  assert(false);
62} catch (e) {
63  assert(e instanceof TypeError);
64}
65
66try {
67  Object.setPrototypeOf(proxy, 1);
68  assert(false);
69} catch (e) {
70  assert(e instanceof TypeError);
71}
72
73var target = {};
74var handler = {};
75var prototype = [];
76
77var proxy = new Proxy(target, handler);
78
79Object.setPrototypeOf(proxy, prototype);
80assert(Object.getPrototypeOf(target) === prototype);
81
82handler.setPrototypeOf = function(target, proto) {
83  return false;
84};
85
86try {
87  Object.setPrototypeOf(proxy, {});
88  assert(false);
89} catch (e) {
90  assert(e instanceof TypeError);
91}
92
93handler.setPrototypeOf = function(target, proto) {
94  return undefined;
95};
96
97try {
98  Object.setPrototypeOf(proxy, {});
99  assert(false);
100} catch (e) {
101  assert(e instanceof TypeError);
102}
103
104handler.setPrototypeOf = function(proto) {};
105
106try {
107  Object.setPrototypeOf(proxy, {});
108  assert(false);
109} catch (e) {
110  assert(e instanceof TypeError);
111}
112
113var proto = {};
114Object.setPrototypeOf(proto, Function.prototype);
115
116var seen_prototype;
117var seen_target;
118
119handler.setPrototypeOf = function(target, proto) {
120  seen_target = target;
121  seen_prototype = proto;
122  return true;
123}
124
125Object.setPrototypeOf(proxy, proto);
126assert(target === seen_target);
127assert(proto === seen_prototype);
128
129// test when target is proxy
130var target = {};
131var handler = {};
132var handler2 = {};
133var target2 = new Proxy(target, handler2);
134var proxy2 = new Proxy(target2, handler);
135var prototype = [2,3];
136
137Object.setPrototypeOf(proxy2, prototype);
138
139assert(prototype === Object.getPrototypeOf(target));
140
141// test when invariants gets violated
142var target = {};
143var handler = {
144    setPrototypeOf(target, proto) {
145        Object.setPrototypeOf(target, Function.prototype);
146        Object.preventExtensions(target);
147        return true;
148    }
149}
150
151var proxy = new Proxy(target, handler);
152
153try {
154  Object.setPrototypeOf(proxy, Array.prototype);
155  assert(false);
156} catch (e) {
157  assert(e instanceof TypeError);
158}
159