-
Notifications
You must be signed in to change notification settings - Fork 54
/
setPrototypeOf.js
258 lines (186 loc) · 7.84 KB
/
setPrototypeOf.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
/*
* Any copyright is dedicated to the Public Domain.
* http://creativecommons.org/licenses/publicdomain/
*/
var gTestfile = "setPrototypeOf.js";
var BUGNUMBER = 888969;
var summary = "Scripted proxies' [[SetPrototypeOf]] behavior";
print(BUGNUMBER + ": " + summary);
/**************
* BEGIN TEST *
**************/
const log = [];
function observe(obj)
{
var observingHandler = new Proxy({}, {
get(target, p, receiver) {
log.push(p);
return Reflect.get(target, p, receiver);
}
});
return new Proxy(obj, observingHandler);
}
var p, h;
// 1. Assert: Either Type(V) is Object or Type(V) is Null.
// 2. Let handler be the value of the [[ProxyHandler]] internal slot of O.
// 3. If handler is null, throw a TypeError exception.
// 4. Assert: Type(handler) is Object.
// 5. Let target be the value of the [[ProxyTarget]] internal slot of O.
var rev = Proxy.revocable({}, {});
p = rev.proxy;
var originalProto = Reflect.getPrototypeOf(p);
assertEq(originalProto, Object.prototype);
rev.revoke();
assertThrowsInstanceOf(() => Reflect.setPrototypeOf(p, originalProto),
TypeError);
assertThrowsInstanceOf(() => Reflect.setPrototypeOf(p, null),
TypeError);
// 6. Let trap be ? GetMethod(handler, "setPrototypeOf").
// handler has uncallable (and not null/undefined) property
p = new Proxy({}, { setPrototypeOf: 9 });
assertThrowsInstanceOf(() => Reflect.setPrototypeOf(p, null),
TypeError);
p = new Proxy({}, { setPrototypeOf: -3.7 });
assertThrowsInstanceOf(() => Reflect.setPrototypeOf(p, null),
TypeError);
p = new Proxy({}, { setPrototypeOf: NaN });
assertThrowsInstanceOf(() => Reflect.setPrototypeOf(p, null),
TypeError);
p = new Proxy({}, { setPrototypeOf: Infinity });
assertThrowsInstanceOf(() => Reflect.setPrototypeOf(p, null),
TypeError);
p = new Proxy({}, { setPrototypeOf: true });
assertThrowsInstanceOf(() => Reflect.setPrototypeOf(p, null),
TypeError);
p = new Proxy({}, { setPrototypeOf: /x/ });
assertThrowsInstanceOf(() => Reflect.setPrototypeOf(p, null),
TypeError);
p = new Proxy({}, { setPrototypeOf: Symbol(42) });
assertThrowsInstanceOf(() => Reflect.setPrototypeOf(p, null),
TypeError);
p = new Proxy({}, { setPrototypeOf: class X {} });
assertThrowsInstanceOf(() => Reflect.setPrototypeOf(p, null),
TypeError);
p = new Proxy({}, observe({}));
assertEq(Reflect.setPrototypeOf(p, Object.prototype), true);
assertEq(log.length, 1);
assertEq(log[0], "get");
h = observe({ setPrototypeOf() { throw 3.14; } });
p = new Proxy(Object.create(Object.prototype), h);
// "setting" without change
log.length = 0;
assertThrowsValue(() => Reflect.setPrototypeOf(p, Object.prototype),
3.14);
assertEq(log.length, 1);
assertEq(log[0], "get");
// "setting" with change
log.length = 0;
assertThrowsValue(() => Reflect.setPrototypeOf(p, /foo/),
3.14);
assertEq(log.length, 1);
assertEq(log[0], "get");
// 7. If trap is undefined, then
// a. Return ? target.[[SetPrototypeOf]](V).
var settingProtoThrows =
new Proxy({}, { setPrototypeOf() { throw "agnizes"; } });
p = new Proxy(settingProtoThrows, { setPrototypeOf: undefined });
assertThrowsValue(() => Reflect.setPrototypeOf(p, null),
"agnizes");
p = new Proxy(settingProtoThrows, { setPrototypeOf: null });
assertThrowsValue(() => Reflect.setPrototypeOf(p, null),
"agnizes");
var anotherProto =
new Proxy({},
{ setPrototypeOf(t, p) {
log.push("reached");
return Reflect.setPrototypeOf(t, p);
} });
p = new Proxy(anotherProto, { setPrototypeOf: undefined });
log.length = 0;
assertEq(Reflect.setPrototypeOf(p, null), true);
assertEq(log.length, 1);
assertEq(log[0], "reached");
p = new Proxy(anotherProto, { setPrototypeOf: null });
log.length = 0;
assertEq(Reflect.setPrototypeOf(p, null), true);
assertEq(log.length, 1);
assertEq(log[0], "reached");
// 8. Let booleanTrapResult be ToBoolean(? Call(trap, handler, « target, V »)).
// The trap callable might throw.
p = new Proxy({}, { setPrototypeOf() { throw "ohai"; } });
assertThrowsValue(() => Reflect.setPrototypeOf(p, /x/),
"ohai");
var throwingTrap =
new Proxy(function() { throw "not called"; },
{ apply() { throw 37; } });
p = new Proxy({}, { setPrototypeOf: throwingTrap });
assertThrowsValue(() => Reflect.setPrototypeOf(p, Object.prototype),
37);
// The trap callable must *only* be called.
p = new Proxy({},
{
setPrototypeOf: observe(function() { throw "boo-urns"; })
});
log.length = 0;
assertThrowsValue(() => Reflect.setPrototypeOf(p, Object.prototype),
"boo-urns");
assertEq(log.length, 1);
assertEq(log[0], "apply");
// 9. If booleanTrapResult is false, return false.
p = new Proxy({}, { setPrototypeOf() { return false; } });
assertEq(Reflect.setPrototypeOf(p, Object.prototype), false);
p = new Proxy({}, { setPrototypeOf() { return +0; } });
assertEq(Reflect.setPrototypeOf(p, Object.prototype), false);
p = new Proxy({}, { setPrototypeOf() { return -0; } });
assertEq(Reflect.setPrototypeOf(p, Object.prototype), false);
p = new Proxy({}, { setPrototypeOf() { return NaN; } });
assertEq(Reflect.setPrototypeOf(p, Object.prototype), false);
p = new Proxy({}, { setPrototypeOf() { return ""; } });
assertEq(Reflect.setPrototypeOf(p, Object.prototype), false);
p = new Proxy({}, { setPrototypeOf() { return undefined; } });
assertEq(Reflect.setPrototypeOf(p, Object.prototype), false);
// 10. Let extensibleTarget be ? IsExtensible(target).
var targetThrowIsExtensible =
new Proxy({}, { isExtensible() { throw "psych!"; } });
p = new Proxy(targetThrowIsExtensible, { setPrototypeOf() { return true; } });
assertThrowsValue(() => Reflect.setPrototypeOf(p, Object.prototype),
"psych!");
// 11. If extensibleTarget is true, return true.
var targ = {};
p = new Proxy(targ, { setPrototypeOf() { return true; } });
assertEq(Reflect.setPrototypeOf(p, /x/), true);
assertEq(Reflect.getPrototypeOf(targ), Object.prototype);
assertEq(Reflect.getPrototypeOf(p), Object.prototype);
p = new Proxy(targ, { setPrototypeOf() { return /x/; } });
assertEq(Reflect.setPrototypeOf(p, /x/), true);
assertEq(Reflect.getPrototypeOf(targ), Object.prototype);
assertEq(Reflect.getPrototypeOf(p), Object.prototype);
p = new Proxy(targ, { setPrototypeOf() { return Infinity; } });
assertEq(Reflect.setPrototypeOf(p, /x/), true);
assertEq(Reflect.getPrototypeOf(targ), Object.prototype);
assertEq(Reflect.getPrototypeOf(p), Object.prototype);
p = new Proxy(targ, { setPrototypeOf() { return Symbol(true); } });
assertEq(Reflect.setPrototypeOf(p, /x/), true);
assertEq(Reflect.getPrototypeOf(targ), Object.prototype);
assertEq(Reflect.getPrototypeOf(p), Object.prototype);
// 12. Let targetProto be ? target.[[GetPrototypeOf]]().
var targetNotExtensibleGetProtoThrows =
new Proxy(Object.preventExtensions({}),
{ getPrototypeOf() { throw NaN; } });
p = new Proxy(targetNotExtensibleGetProtoThrows,
{ setPrototypeOf() { log.push("goober"); return true; } });
log.length = 0;
assertThrowsValue(() => Reflect.setPrototypeOf(p, /abcd/),
NaN);
// 13. If SameValue(V, targetProto) is false, throw a TypeError exception.
var newProto;
p = new Proxy(Object.preventExtensions(Object.create(Math)),
{ setPrototypeOf(t, p) { return true; } });
assertThrowsInstanceOf(() => Reflect.setPrototypeOf(p, null),
TypeError);
// 14. Return true.
assertEq(Reflect.setPrototypeOf(p, Math), true);
/******************************************************************************/
if (typeof reportCompare === "function")
reportCompare(true, true);
print("Tests complete");