2 * Any copyright is dedicated to the Public Domain.
3 * http://creativecommons.org/licenses/publicdomain/
4 * Contributor: Andreas Gal
7 //-----------------------------------------------------------------------------
8 var BUGNUMBER = 546590;
9 var summary = 'basic scripted proxies tests';
13 //-----------------------------------------------------------------------------
15 //-----------------------------------------------------------------------------
19 printBugNumber(BUGNUMBER);
22 testObj({ foo: 1, bar: 2 });
23 testObj({ 1: 2, 3: 4 });
27 testObj(new RegExp());
32 /* Test function proxies. */
33 var proxy = Proxy.createFunction({
34 get: function(obj,name) { return Function.prototype[name]; },
38 }, function() { return "call"; });
40 assertEq(proxy(), "call");
41 assertEq(Function.prototype.bind.call(proxy)(), "call");
42 assertEq(typeof proxy, "function");
43 if ("isTrapping" in Proxy) {
44 assertEq(Proxy.isTrapping(proxy), true);
45 assertEq(Proxy.fix(proxy), true);
46 assertEq(Proxy.isTrapping(proxy), false);
47 assertEq(typeof proxy, "function");
48 assertEq(proxy(), "call");
51 /* Test function proxies as constructors. */
52 var proxy = Proxy.createFunction({
53 get: function(obj, name) { return Function.prototype[name]; },
54 fix: function() { return ({}); }
56 function() { var x = {}; x.origin = "call"; return x; },
57 function() { var x = {}; x.origin = "new"; return x; })
59 assertEq(proxy().origin, "call");
60 assertEq(Function.prototype.bind.call(proxy)().origin, "call");
61 assertEq((new proxy()).origin, "new");
62 assertEq(new (Function.prototype.bind.call(proxy))().origin, "new");
64 assertEq(Proxy.fix(proxy), true);
65 assertEq(proxy().origin, "call");
66 assertEq((new proxy()).origin, "new");
69 /* Test fallback on call if no construct trap was given. */
70 var proxy = Proxy.createFunction({
71 get: function(obj, name) { return Function.prototype[name]; },
72 fix: function() { return ({}); }
74 function() { this.origin = "new"; return "new-ret"; });
76 assertEq((new proxy()).origin, "new");
78 assertEq(Proxy.fix(proxy), true);
79 assertEq((new proxy()).origin, "new");
83 var proxy = Proxy.create({ get: function(obj,name) { return function(a,b,c) { return name + uneval([a,b,c]); } }});
84 assertEq(proxy.foo(1,2,3), "foo[1, 2, 3]");
86 reportCompare(0, 0, "done.");
91 /* Test object proxies. */
92 function noopHandlerMaker(obj) {
94 getOwnPropertyDescriptor: function(name) {
95 var desc = Object.getOwnPropertyDescriptor(obj);
96 // a trapping proxy's properties must always be configurable
97 desc.configurable = true;
100 getPropertyDescriptor: function(name) {
101 var desc = Object.getPropertyDescriptor(obj); // assumed
102 // a trapping proxy's properties must always be configurable
103 desc.configurable = true;
106 getOwnPropertyNames: function() {
107 return Object.getOwnPropertyNames(obj);
109 defineProperty: function(name, desc) {
110 return Object.defineProperty(obj, name, desc);
112 delete: function(name) { return delete obj[name]; },
114 // As long as obj is not frozen, the proxy won't allow itself to be fixed
115 // if (!Object.isFrozen(obj)) [not implemented in SpiderMonkey]
117 // return Object.getOwnProperties(obj); // assumed [not implemented in SpiderMonkey]
120 props[x] = Object.getOwnPropertyDescriptor(obj, x);
123 has: function(name) { return name in obj; },
124 hasOwn: function(name) { return ({}).hasOwnProperty.call(obj, name); },
125 get: function(receiver, name) { return obj[name]; },
126 set: function(receiver, name, val) { obj[name] = val; return true; }, // bad behavior when set fails in non-strict mode
127 enumerate: function() {
129 for (name in obj) { result.push(name); };
132 keys: function() { return Object.keys(obj); }
136 function testNoopHandler(obj, proxy) {
137 /* Check that both objects see the same properties. */
139 assertEq(obj[x], proxy[x]);
141 assertEq(obj[x], proxy[x]);
142 /* Check that the iteration order is the same. */
148 assertEq(uneval(a), uneval(b));
151 function testObj(obj) {
152 var proxy = Proxy.create(noopHandlerMaker(obj));
153 testNoopHandler(obj, proxy);
154 assertEq(typeof proxy, "object");
155 if ("isTrapping" in Proxy) {
156 assertEq(Proxy.isTrapping(proxy), true);
157 assertEq(Proxy.fix(proxy), true);
158 assertEq(Proxy.isTrapping(proxy), false);
159 assertEq(typeof proxy, "object");
160 testNoopHandler(obj, proxy);