js/src/tests/ecma_5/extensions/proxy-__proto__.js

changeset 0
6474c204b198
equal deleted inserted replaced
-1:000000000000 0:2c5a4d993ba7
1 /*
2 * Any copyright is dedicated to the Public Domain.
3 * http://creativecommons.org/licenses/publicdomain/
4 */
5
6 var gTestfile = 'proxy-__proto__.js';
7 var BUGNUMBER = 770344;
8 var summary = "Behavior of __proto__ on proxies";
9
10 print(BUGNUMBER + ": " + summary);
11
12 /**************
13 * BEGIN TEST *
14 **************/
15
16 var protoDesc = Object.getOwnPropertyDescriptor(Object.prototype, "__proto__");
17 var protoGetter = protoDesc.get;
18 var protoSetter = protoDesc.set;
19
20 function pp(arr)
21 {
22 return arr.map(function(v) { return "" + v; }).join(", ");
23 }
24
25 function testProxy(creator, args, proto)
26 {
27 print("Now testing behavior for " +
28 "Proxy." + creator + "(" + pp(args) + ")");
29
30 var pobj = Proxy[creator].apply(Proxy, args);
31
32 // Check [[Prototype]] before attempted mutation
33 assertEq(Object.getPrototypeOf(pobj), proto);
34 assertEq(protoGetter.call(pobj), proto);
35
36 // Attempt [[Prototype]] mutation
37 protoSetter.call(pobj, null);
38
39 // Check [[Prototype]] after attempted mutation
40 assertEq(Object.getPrototypeOf(pobj), null);
41 assertEq(protoGetter.call(pobj), null);
42 }
43
44 // Proxy object with non-null [[Prototype]]
45 var nonNullProto = { toString: function() { return "non-null prototype"; } };
46 var nonNullHandler = { toString: function() { return "non-null handler"; } };
47 testProxy("create", [nonNullHandler, nonNullProto], nonNullProto);
48
49 // Proxy object with null [[Prototype]]
50 var nullProto = null;
51 var nullHandler = { toString: function() { return "null handler"; } };
52 testProxy("create", [nullHandler, nullProto], nullProto);
53
54 // Proxy function with [[Call]]
55 var callForCallOnly = function () { };
56 callForCallOnly.toString = function() { return "callForCallOnly"; };
57 var callOnlyHandler = { toString: function() { return "call-only handler"; } };
58 testProxy("createFunction",
59 [callOnlyHandler, callForCallOnly], Function.prototype);
60
61 // Proxy function with [[Call]] and [[Construct]]
62 var callForCallConstruct = function() { };
63 callForCallConstruct.toString = function() { return "call/construct call"; };
64 var constructForCallConstruct = function() { };
65 constructForCallConstruct.toString =
66 function() { return "call/construct construct"; };
67 var handlerForCallConstruct =
68 { toString: function() { return "call/construct handler"; } };
69 testProxy("createFunction",
70 [handlerForCallConstruct,
71 callForCallConstruct,
72 constructForCallConstruct],
73 Function.prototype);
74
75
76 /******************************************************************************/
77
78 if (typeof reportCompare === "function")
79 reportCompare(true, true);
80
81 print("Tests complete");

mercurial