|
1 /* |
|
2 * Any copyright is dedicated to the Public Domain. |
|
3 * http://creativecommons.org/licenses/publicdomain/ |
|
4 * Contributor: Andreas Gal |
|
5 */ |
|
6 |
|
7 //----------------------------------------------------------------------------- |
|
8 var BUGNUMBER = 546590; |
|
9 var summary = 'basic scripted proxies tests'; |
|
10 var actual = ''; |
|
11 var expect = ''; |
|
12 |
|
13 //----------------------------------------------------------------------------- |
|
14 test(); |
|
15 //----------------------------------------------------------------------------- |
|
16 |
|
17 function test() { |
|
18 enterFunc ('test'); |
|
19 printBugNumber(BUGNUMBER); |
|
20 printStatus(summary); |
|
21 |
|
22 testObj({ foo: 1, bar: 2 }); |
|
23 testObj({ 1: 2, 3: 4 }); |
|
24 testObj([ 1, 2, 3 ]); |
|
25 testObj(new Date()); |
|
26 testObj(new Array()); |
|
27 testObj(new RegExp()); |
|
28 testObj(Date); |
|
29 testObj(Array); |
|
30 testObj(RegExp); |
|
31 |
|
32 /* Test function proxies. */ |
|
33 var proxy = Proxy.createFunction({ |
|
34 get: function(obj,name) { return Function.prototype[name]; }, |
|
35 fix: function() { |
|
36 return ({}); |
|
37 } |
|
38 }, function() { return "call"; }); |
|
39 |
|
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"); |
|
49 } |
|
50 |
|
51 /* Test function proxies as constructors. */ |
|
52 var proxy = Proxy.createFunction({ |
|
53 get: function(obj, name) { return Function.prototype[name]; }, |
|
54 fix: function() { return ({}); } |
|
55 }, |
|
56 function() { var x = {}; x.origin = "call"; return x; }, |
|
57 function() { var x = {}; x.origin = "new"; return x; }) |
|
58 |
|
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"); |
|
63 if ("fix" in Proxy) { |
|
64 assertEq(Proxy.fix(proxy), true); |
|
65 assertEq(proxy().origin, "call"); |
|
66 assertEq((new proxy()).origin, "new"); |
|
67 } |
|
68 |
|
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 ({}); } |
|
73 }, |
|
74 function() { this.origin = "new"; return "new-ret"; }); |
|
75 |
|
76 assertEq((new proxy()).origin, "new"); |
|
77 if ("fix" in Proxy) { |
|
78 assertEq(Proxy.fix(proxy), true); |
|
79 assertEq((new proxy()).origin, "new"); |
|
80 } |
|
81 |
|
82 /* Test invoke. */ |
|
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]"); |
|
85 |
|
86 reportCompare(0, 0, "done."); |
|
87 |
|
88 exitFunc ('test'); |
|
89 } |
|
90 |
|
91 /* Test object proxies. */ |
|
92 function noopHandlerMaker(obj) { |
|
93 return { |
|
94 getOwnPropertyDescriptor: function(name) { |
|
95 var desc = Object.getOwnPropertyDescriptor(obj); |
|
96 // a trapping proxy's properties must always be configurable |
|
97 desc.configurable = true; |
|
98 return desc; |
|
99 }, |
|
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; |
|
104 return desc; |
|
105 }, |
|
106 getOwnPropertyNames: function() { |
|
107 return Object.getOwnPropertyNames(obj); |
|
108 }, |
|
109 defineProperty: function(name, desc) { |
|
110 return Object.defineProperty(obj, name, desc); |
|
111 }, |
|
112 delete: function(name) { return delete obj[name]; }, |
|
113 fix: function() { |
|
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] |
|
116 // return undefined; |
|
117 // return Object.getOwnProperties(obj); // assumed [not implemented in SpiderMonkey] |
|
118 var props = {}; |
|
119 for (x in obj) |
|
120 props[x] = Object.getOwnPropertyDescriptor(obj, x); |
|
121 return props; |
|
122 }, |
|
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() { |
|
128 var result = []; |
|
129 for (name in obj) { result.push(name); }; |
|
130 return result; |
|
131 }, |
|
132 keys: function() { return Object.keys(obj); } |
|
133 }; |
|
134 }; |
|
135 |
|
136 function testNoopHandler(obj, proxy) { |
|
137 /* Check that both objects see the same properties. */ |
|
138 for (x in obj) |
|
139 assertEq(obj[x], proxy[x]); |
|
140 for (x in proxy) |
|
141 assertEq(obj[x], proxy[x]); |
|
142 /* Check that the iteration order is the same. */ |
|
143 var a = [], b = []; |
|
144 for (x in obj) |
|
145 a.push(x); |
|
146 for (x in proxy) |
|
147 b.push(x); |
|
148 assertEq(uneval(a), uneval(b)); |
|
149 } |
|
150 |
|
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); |
|
161 } |
|
162 } |