OLD | NEW |
1 // Copyright 2011 the V8 project authors. All rights reserved. | 1 // Copyright 2011 the V8 project authors. All rights reserved. |
2 // Redistribution and use in source and binary forms, with or without | 2 // Redistribution and use in source and binary forms, with or without |
3 // modification, are permitted provided that the following conditions are | 3 // modification, are permitted provided that the following conditions are |
4 // met: | 4 // met: |
5 // | 5 // |
6 // * Redistributions of source code must retain the above copyright | 6 // * Redistributions of source code must retain the above copyright |
7 // notice, this list of conditions and the following disclaimer. | 7 // notice, this list of conditions and the following disclaimer. |
8 // * Redistributions in binary form must reproduce the above | 8 // * Redistributions in binary form must reproduce the above |
9 // copyright notice, this list of conditions and the following | 9 // copyright notice, this list of conditions and the following |
10 // disclaimer in the documentation and/or other materials provided | 10 // disclaimer in the documentation and/or other materials provided |
(...skipping 10 matching lines...) Expand all Loading... |
21 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | 21 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
22 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | 22 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
23 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | 23 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
24 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | 24 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
25 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | 25 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
26 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | 26 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
27 | 27 |
28 // Flags: --harmony-proxies --allow-natives-syntax | 28 // Flags: --harmony-proxies --allow-natives-syntax |
29 | 29 |
30 | 30 |
31 // Helper. | 31 // TODO(neis): These tests are temporarily commented out because of ongoing |
32 | 32 // changes to the implementation of proxies. |
33 function CreateFrozen(handler, callTrap, constructTrap) { | |
34 if (handler.fix === undefined) handler.fix = function() { return {} } | |
35 var f = Proxy.createFunction(handler, callTrap, constructTrap) | |
36 Object.freeze(f) | |
37 return f | |
38 } | |
39 | 33 |
40 | 34 |
41 // Ensures that checking the "length" property of a function proxy doesn't | 35 //// Ensures that checking the "length" property of a function proxy doesn't |
42 // crash due to lack of a [[Get]] method. | 36 //// crash due to lack of a [[Get]] method. |
43 var handler = { | 37 //var handler = { |
44 get : function(r, n) { return n == "length" ? 2 : undefined } | 38 // get : function(r, n) { return n == "length" ? 2 : undefined } |
45 } | 39 //} |
46 | 40 // |
47 | 41 // |
48 // Calling (call, Function.prototype.call, Function.prototype.apply, | 42 //// Calling (call, Function.prototype.call, Function.prototype.apply, |
49 // Function.prototype.bind). | 43 //// Function.prototype.bind). |
50 | 44 // |
51 var global_object = this | 45 //var global_object = this |
52 var receiver | 46 //var receiver |
53 | 47 // |
54 function TestCall(isStrict, callTrap) { | 48 //function TestCall(isStrict, callTrap) { |
55 assertEquals(42, callTrap(5, 37)) | 49 // assertEquals(42, callTrap(5, 37)) |
56 assertSame(isStrict ? undefined : global_object, receiver) | 50 // assertSame(isStrict ? undefined : global_object, receiver) |
57 | 51 // |
58 var handler = { | 52 // var handler = { |
59 get: function(r, k) { | 53 // get: function(r, k) { |
60 return k == "length" ? 2 : Function.prototype[k] | 54 // return k == "length" ? 2 : Function.prototype[k] |
61 } | 55 // } |
62 } | 56 // } |
63 var f = Proxy.createFunction(handler, callTrap) | 57 // var f = Proxy.createFunction(handler, callTrap) |
64 var o = {f: f} | 58 // var o = {f: f} |
65 global_object.f = f | 59 // global_object.f = f |
66 | 60 // |
67 receiver = 333 | 61 // receiver = 333 |
68 assertEquals(42, f(11, 31)) | 62 // assertEquals(42, f(11, 31)) |
69 assertSame(isStrict ? undefined : global_object, receiver) | 63 // assertSame(isStrict ? undefined : global_object, receiver) |
70 receiver = 333 | 64 // receiver = 333 |
71 assertEquals(42, o.f(10, 32)) | 65 // assertEquals(42, o.f(10, 32)) |
72 assertSame(o, receiver) | 66 // assertSame(o, receiver) |
73 receiver = 333 | 67 // receiver = 333 |
74 assertEquals(42, o["f"](9, 33)) | 68 // assertEquals(42, o["f"](9, 33)) |
75 assertSame(o, receiver) | 69 // assertSame(o, receiver) |
76 receiver = 333 | 70 // receiver = 333 |
77 assertEquals(42, (1, o).f(8, 34)) | 71 // assertEquals(42, (1, o).f(8, 34)) |
78 assertSame(o, receiver) | 72 // assertSame(o, receiver) |
79 receiver = 333 | 73 // receiver = 333 |
80 assertEquals(42, (1, o)["f"](7, 35)) | 74 // assertEquals(42, (1, o)["f"](7, 35)) |
81 assertSame(o, receiver) | 75 // assertSame(o, receiver) |
82 receiver = 333 | 76 // receiver = 333 |
83 assertEquals(42, f.call(o, 32, 10)) | 77 // assertEquals(42, f.call(o, 32, 10)) |
84 assertSame(o, receiver) | 78 // assertSame(o, receiver) |
85 receiver = 333 | 79 // receiver = 333 |
86 assertEquals(42, f.call(undefined, 33, 9)) | 80 // assertEquals(42, f.call(undefined, 33, 9)) |
87 assertSame(isStrict ? undefined : global_object, receiver) | 81 // assertSame(isStrict ? undefined : global_object, receiver) |
88 receiver = 333 | 82 // receiver = 333 |
89 assertEquals(42, f.call(null, 33, 9)) | 83 // assertEquals(42, f.call(null, 33, 9)) |
90 assertSame(isStrict ? null : global_object, receiver) | 84 // assertSame(isStrict ? null : global_object, receiver) |
91 receiver = 333 | 85 // receiver = 333 |
92 assertEquals(44, f.call(2, 21, 23)) | 86 // assertEquals(44, f.call(2, 21, 23)) |
93 assertSame(2, receiver.valueOf()) | 87 // assertSame(2, receiver.valueOf()) |
94 receiver = 333 | 88 // receiver = 333 |
95 assertEquals(42, Function.prototype.call.call(f, o, 20, 22)) | 89 // assertEquals(42, Function.prototype.call.call(f, o, 20, 22)) |
96 assertSame(o, receiver) | 90 // assertSame(o, receiver) |
97 receiver = 333 | 91 // receiver = 333 |
98 assertEquals(43, Function.prototype.call.call(f, null, 20, 23)) | 92 // assertEquals(43, Function.prototype.call.call(f, null, 20, 23)) |
99 assertSame(isStrict ? null : global_object, receiver) | 93 // assertSame(isStrict ? null : global_object, receiver) |
100 assertEquals(44, Function.prototype.call.call(f, 2, 21, 23)) | 94 // assertEquals(44, Function.prototype.call.call(f, 2, 21, 23)) |
101 assertEquals(2, receiver.valueOf()) | 95 // assertEquals(2, receiver.valueOf()) |
102 receiver = 333 | 96 // receiver = 333 |
103 assertEquals(32, f.apply(o, [16, 16])) | 97 // assertEquals(32, f.apply(o, [16, 16])) |
104 assertSame(o, receiver) | 98 // assertSame(o, receiver) |
105 receiver = 333 | 99 // receiver = 333 |
106 assertEquals(32, Function.prototype.apply.call(f, o, [17, 15])) | 100 // assertEquals(32, Function.prototype.apply.call(f, o, [17, 15])) |
107 assertSame(o, receiver) | 101 // assertSame(o, receiver) |
108 receiver = 333 | 102 // receiver = 333 |
109 assertEquals(42, %Call(f, o, 11, 31)); | 103 // assertEquals(42, %Call(f, o, 11, 31)); |
110 assertSame(o, receiver) | 104 // assertSame(o, receiver) |
111 receiver = 333 | 105 // receiver = 333 |
112 assertEquals(42, %Call(f, null, 11, 31)); | 106 // assertEquals(42, %Call(f, null, 11, 31)); |
113 assertSame(isStrict ? null : global_object, receiver) | 107 // assertSame(isStrict ? null : global_object, receiver) |
114 receiver = 333 | 108 // receiver = 333 |
115 assertEquals(42, %Apply(f, o, [11, 31], 0, 2)) | 109 // assertEquals(42, %Apply(f, o, [11, 31], 0, 2)) |
116 assertSame(o, receiver) | 110 // assertSame(o, receiver) |
117 receiver = 333 | 111 // receiver = 333 |
118 assertEquals(42, %Apply(f, null, [11, 31], 0, 2)) | 112 // assertEquals(42, %Apply(f, null, [11, 31], 0, 2)) |
119 assertSame(isStrict ? null : global_object, receiver) | 113 // assertSame(isStrict ? null : global_object, receiver) |
120 receiver = 333 | 114 // receiver = 333 |
121 assertEquals(42, %_Call(f, o, 11, 31)) | 115 // assertEquals(42, %_Call(f, o, 11, 31)) |
122 assertSame(o, receiver) | 116 // assertSame(o, receiver) |
123 receiver = 333 | 117 // receiver = 333 |
124 assertEquals(42, %_Call(f, null, 11, 31)) | 118 // assertEquals(42, %_Call(f, null, 11, 31)) |
125 assertSame(isStrict ? null : global_object, receiver) | 119 // assertSame(isStrict ? null : global_object, receiver) |
126 | 120 // |
127 var ff = Function.prototype.bind.call(f, o, 12) | 121 // var ff = Function.prototype.bind.call(f, o, 12) |
128 assertTrue(ff.length <= 1) // TODO(rossberg): Not spec'ed yet, be lax. | 122 // assertTrue(ff.length <= 1) // TODO(rossberg): Not spec'ed yet, be lax. |
129 receiver = 333 | 123 // receiver = 333 |
130 assertEquals(42, ff(30)) | 124 // assertEquals(42, ff(30)) |
131 assertSame(o, receiver) | 125 // assertSame(o, receiver) |
132 receiver = 333 | 126 // receiver = 333 |
133 assertEquals(33, Function.prototype.call.call(ff, {}, 21)) | 127 // assertEquals(33, Function.prototype.call.call(ff, {}, 21)) |
134 assertSame(o, receiver) | 128 // assertSame(o, receiver) |
135 receiver = 333 | 129 // receiver = 333 |
136 assertEquals(32, Function.prototype.apply.call(ff, {}, [20])) | 130 // assertEquals(32, Function.prototype.apply.call(ff, {}, [20])) |
137 assertSame(o, receiver) | 131 // assertSame(o, receiver) |
138 receiver = 333 | 132 // receiver = 333 |
139 assertEquals(23, %Call(ff, {}, 11)); | 133 // assertEquals(23, %Call(ff, {}, 11)); |
140 assertSame(o, receiver) | 134 // assertSame(o, receiver) |
141 receiver = 333 | 135 // receiver = 333 |
142 assertEquals(23, %Call(ff, {}, 11, 3)); | 136 // assertEquals(23, %Call(ff, {}, 11, 3)); |
143 assertSame(o, receiver) | 137 // assertSame(o, receiver) |
144 receiver = 333 | 138 // receiver = 333 |
145 assertEquals(24, %Apply(ff, {}, [12, 13], 0, 1)) | 139 // assertEquals(24, %Apply(ff, {}, [12, 13], 0, 1)) |
146 assertSame(o, receiver) | 140 // assertSame(o, receiver) |
147 receiver = 333 | 141 // receiver = 333 |
148 assertEquals(24, %Apply(ff, {}, [12, 13], 0, 2)) | 142 // assertEquals(24, %Apply(ff, {}, [12, 13], 0, 2)) |
149 assertSame(o, receiver) | 143 // assertSame(o, receiver) |
150 receiver = 333 | 144 // receiver = 333 |
151 assertEquals(34, %_Call(ff, {}, 22)) | 145 // assertEquals(34, %_Call(ff, {}, 22)) |
152 assertSame(o, receiver) | 146 // assertSame(o, receiver) |
153 receiver = 333 | 147 // receiver = 333 |
154 assertEquals(34, %_Call(ff, {}, 22, 3)) | 148 // assertEquals(34, %_Call(ff, {}, 22, 3)) |
155 assertSame(o, receiver) | 149 // assertSame(o, receiver) |
156 | 150 // |
157 var fff = Function.prototype.bind.call(ff, o, 30) | 151 // var fff = Function.prototype.bind.call(ff, o, 30) |
158 assertEquals(0, fff.length) | 152 // assertEquals(0, fff.length) |
159 receiver = 333 | 153 // receiver = 333 |
160 assertEquals(42, fff()) | 154 // assertEquals(42, fff()) |
161 assertSame(o, receiver) | 155 // assertSame(o, receiver) |
162 receiver = 333 | 156 // receiver = 333 |
163 assertEquals(42, Function.prototype.call.call(fff, {})) | 157 // assertEquals(42, Function.prototype.call.call(fff, {})) |
164 assertSame(o, receiver) | 158 // assertSame(o, receiver) |
165 receiver = 333 | 159 // receiver = 333 |
166 assertEquals(42, Function.prototype.apply.call(fff, {})) | 160 // assertEquals(42, Function.prototype.apply.call(fff, {})) |
167 assertSame(o, receiver) | 161 // assertSame(o, receiver) |
168 receiver = 333 | 162 // receiver = 333 |
169 assertEquals(42, %Call(fff, {})); | 163 // assertEquals(42, %Call(fff, {})); |
170 assertSame(o, receiver) | 164 // assertSame(o, receiver) |
171 receiver = 333 | 165 // receiver = 333 |
172 assertEquals(42, %Call(fff, {}, 11, 3)) | 166 // assertEquals(42, %Call(fff, {}, 11, 3)) |
173 assertSame(o, receiver) | 167 // assertSame(o, receiver) |
174 receiver = 333 | 168 // receiver = 333 |
175 assertEquals(42, %Apply(fff, {}, [], 0, 0)) | 169 // assertEquals(42, %Apply(fff, {}, [], 0, 0)) |
176 assertSame(o, receiver) | 170 // assertSame(o, receiver) |
177 receiver = 333 | 171 // receiver = 333 |
178 assertEquals(42, %Apply(fff, {}, [12, 13], 0, 0)) | 172 // assertEquals(42, %Apply(fff, {}, [12, 13], 0, 0)) |
179 assertSame(o, receiver) | 173 // assertSame(o, receiver) |
180 receiver = 333 | 174 // receiver = 333 |
181 assertEquals(42, %Apply(fff, {}, [12, 13], 0, 2)) | 175 // assertEquals(42, %Apply(fff, {}, [12, 13], 0, 2)) |
182 assertSame(o, receiver) | 176 // assertSame(o, receiver) |
183 receiver = 333 | 177 // receiver = 333 |
184 assertEquals(42, %_Call(fff, {})) | 178 // assertEquals(42, %_Call(fff, {})) |
185 assertSame(o, receiver) | 179 // assertSame(o, receiver) |
186 receiver = 333 | 180 // receiver = 333 |
187 assertEquals(42, %_Call(fff, {}, 3, 4, 5)) | 181 // assertEquals(42, %_Call(fff, {}, 3, 4, 5)) |
188 assertSame(o, receiver) | 182 // assertSame(o, receiver) |
189 | 183 // |
190 var f = CreateFrozen({}, callTrap) | 184 // var f = CreateFrozen({}, callTrap) |
191 receiver = 333 | 185 // receiver = 333 |
192 assertEquals(42, f(11, 31)) | 186 // assertEquals(42, f(11, 31)) |
193 assertSame(isStrict ? undefined : global_object, receiver) | 187 // assertSame(isStrict ? undefined : global_object, receiver) |
194 var o = {f: f} | 188 // var o = {f: f} |
195 receiver = 333 | 189 // receiver = 333 |
196 assertEquals(42, o.f(10, 32)) | 190 // assertEquals(42, o.f(10, 32)) |
197 assertSame(o, receiver) | 191 // assertSame(o, receiver) |
198 receiver = 333 | 192 // receiver = 333 |
199 assertEquals(42, o["f"](9, 33)) | 193 // assertEquals(42, o["f"](9, 33)) |
200 assertSame(o, receiver) | 194 // assertSame(o, receiver) |
201 receiver = 333 | 195 // receiver = 333 |
202 assertEquals(42, (1, o).f(8, 34)) | 196 // assertEquals(42, (1, o).f(8, 34)) |
203 assertSame(o, receiver) | 197 // assertSame(o, receiver) |
204 receiver = 333 | 198 // receiver = 333 |
205 assertEquals(42, (1, o)["f"](7, 35)) | 199 // assertEquals(42, (1, o)["f"](7, 35)) |
206 assertSame(o, receiver) | 200 // assertSame(o, receiver) |
207 receiver = 333 | 201 // receiver = 333 |
208 assertEquals(42, Function.prototype.call.call(f, o, 20, 22)) | 202 // assertEquals(42, Function.prototype.call.call(f, o, 20, 22)) |
209 assertSame(o, receiver) | 203 // assertSame(o, receiver) |
210 receiver = 333 | 204 // receiver = 333 |
211 assertEquals(32, Function.prototype.apply.call(f, o, [17, 15])) | 205 // assertEquals(32, Function.prototype.apply.call(f, o, [17, 15])) |
212 assertSame(o, receiver) | 206 // assertSame(o, receiver) |
213 receiver = 333 | 207 // receiver = 333 |
214 assertEquals(23, %Call(f, o, 11, 12)) | 208 // assertEquals(23, %Call(f, o, 11, 12)) |
215 assertSame(o, receiver) | 209 // assertSame(o, receiver) |
216 receiver = 333 | 210 // receiver = 333 |
217 assertEquals(27, %Apply(f, o, [12, 13, 14], 1, 2)) | 211 // assertEquals(27, %Apply(f, o, [12, 13, 14], 1, 2)) |
218 assertSame(o, receiver) | 212 // assertSame(o, receiver) |
219 receiver = 333 | 213 // receiver = 333 |
220 assertEquals(42, %_Call(f, o, 18, 24)) | 214 // assertEquals(42, %_Call(f, o, 18, 24)) |
221 assertSame(o, receiver) | 215 // assertSame(o, receiver) |
222 } | 216 //} |
223 | 217 // |
224 TestCall(false, function(x, y) { | 218 //TestCall(false, function(x, y) { |
225 receiver = this | 219 // receiver = this |
226 return x + y | 220 // return x + y |
227 }) | 221 //}) |
228 | 222 // |
229 TestCall(true, function(x, y) { | 223 //TestCall(true, function(x, y) { |
230 "use strict" | 224 // "use strict" |
231 receiver = this | 225 // receiver = this |
232 return x + y | 226 // return x + y |
233 }) | 227 //}) |
234 | 228 // |
235 TestCall(false, function() { | 229 //TestCall(false, function() { |
236 receiver = this | 230 // receiver = this |
237 return arguments[0] + arguments[1] | 231 // return arguments[0] + arguments[1] |
238 }) | 232 //}) |
239 | 233 // |
240 TestCall(false, Proxy.createFunction(handler, function(x, y) { | 234 //TestCall(false, Proxy.createFunction(handler, function(x, y) { |
241 receiver = this | 235 // receiver = this |
242 return x + y | 236 // return x + y |
243 })) | 237 //})) |
244 | 238 // |
245 TestCall(true, Proxy.createFunction(handler, function(x, y) { | 239 //TestCall(true, Proxy.createFunction(handler, function(x, y) { |
246 "use strict" | 240 // "use strict" |
247 receiver = this | 241 // receiver = this |
248 return x + y | 242 // return x + y |
249 })) | 243 //})) |
250 | 244 // |
251 TestCall(false, CreateFrozen(handler, function(x, y) { | 245 //TestCall(false, CreateFrozen(handler, function(x, y) { |
252 receiver = this | 246 // receiver = this |
253 return x + y | 247 // return x + y |
254 })) | 248 //})) |
255 | 249 // |
256 | 250 // |
257 | 251 // |
258 // Using intrinsics as call traps. | 252 //// Using intrinsics as call traps. |
259 | 253 // |
260 function TestCallIntrinsic(type, callTrap) { | 254 //function TestCallIntrinsic(type, callTrap) { |
261 var f = Proxy.createFunction({}, callTrap) | 255 // var f = Proxy.createFunction({}, callTrap) |
262 var x = f() | 256 // var x = f() |
263 assertTrue(typeof x == type) | 257 // assertTrue(typeof x == type) |
264 } | 258 //} |
265 | 259 // |
266 TestCallIntrinsic("boolean", Boolean) | 260 //TestCallIntrinsic("boolean", Boolean) |
267 TestCallIntrinsic("number", Number) | 261 //TestCallIntrinsic("number", Number) |
268 TestCallIntrinsic("string", String) | 262 //TestCallIntrinsic("string", String) |
269 TestCallIntrinsic("object", Object) | 263 //TestCallIntrinsic("object", Object) |
270 TestCallIntrinsic("function", Function) | 264 //TestCallIntrinsic("function", Function) |
271 | 265 // |
272 | 266 // |
273 | 267 // |
274 // Throwing from call trap. | 268 //// Throwing from call trap. |
275 | 269 // |
276 function TestCallThrow(callTrap) { | 270 //function TestCallThrow(callTrap) { |
277 var f = Proxy.createFunction({}, callTrap) | 271 // var f = Proxy.createFunction({}, callTrap) |
278 assertThrows(function(){ f(11) }, "myexn") | 272 // assertThrows(function(){ f(11) }, "myexn") |
279 assertThrows(function(){ ({x: f}).x(11) }, "myexn") | 273 // assertThrows(function(){ ({x: f}).x(11) }, "myexn") |
280 assertThrows(function(){ ({x: f})["x"](11) }, "myexn") | 274 // assertThrows(function(){ ({x: f})["x"](11) }, "myexn") |
281 assertThrows(function(){ Function.prototype.call.call(f, {}, 2) }, "myexn") | 275 // assertThrows(function(){ Function.prototype.call.call(f, {}, 2) }, "myexn") |
282 assertThrows(function(){ Function.prototype.apply.call(f, {}, [1]) }, "myexn") | 276 // assertThrows(function(){ Function.prototype.apply.call(f, {}, [1]) }, "myexn
") |
283 assertThrows(function(){ %Call(f, {}) }, "myexn") | 277 // assertThrows(function(){ %Call(f, {}) }, "myexn") |
284 assertThrows(function(){ %Call(f, {}, 1, 2) }, "myexn") | 278 // assertThrows(function(){ %Call(f, {}, 1, 2) }, "myexn") |
285 assertThrows(function(){ %Apply({}, f, [], 3, 0) }, "myexn") | 279 // assertThrows(function(){ %Apply({}, f, [], 3, 0) }, "myexn") |
286 assertThrows(function(){ %Apply({}, f, [3, 4], 0, 1) }, "myexn") | 280 // assertThrows(function(){ %Apply({}, f, [3, 4], 0, 1) }, "myexn") |
287 assertThrows(function(){ %_Call(f, {}) }, "myexn") | 281 // assertThrows(function(){ %_Call(f, {}) }, "myexn") |
288 assertThrows(function(){ %_Call(f, {}, 1, 2) }, "myexn") | 282 // assertThrows(function(){ %_Call(f, {}, 1, 2) }, "myexn") |
289 | 283 // |
290 var f = CreateFrozen({}, callTrap) | 284 // var f = CreateFrozen({}, callTrap) |
291 assertThrows(function(){ f(11) }, "myexn") | 285 // assertThrows(function(){ f(11) }, "myexn") |
292 assertThrows(function(){ ({x: f}).x(11) }, "myexn") | 286 // assertThrows(function(){ ({x: f}).x(11) }, "myexn") |
293 assertThrows(function(){ ({x: f})["x"](11) }, "myexn") | 287 // assertThrows(function(){ ({x: f})["x"](11) }, "myexn") |
294 assertThrows(function(){ Function.prototype.call.call(f, {}, 2) }, "myexn") | 288 // assertThrows(function(){ Function.prototype.call.call(f, {}, 2) }, "myexn") |
295 assertThrows(function(){ Function.prototype.apply.call(f, {}, [1]) }, "myexn") | 289 // assertThrows(function(){ Function.prototype.apply.call(f, {}, [1]) }, "myexn
") |
296 assertThrows(function(){ %Call(f, {}) }, "myexn") | 290 // assertThrows(function(){ %Call(f, {}) }, "myexn") |
297 assertThrows(function(){ %Call(f, {}, 1, 2) }, "myexn") | 291 // assertThrows(function(){ %Call(f, {}, 1, 2) }, "myexn") |
298 assertThrows(function(){ %Apply({}, f, [], 3, 0) }, "myexn") | 292 // assertThrows(function(){ %Apply({}, f, [], 3, 0) }, "myexn") |
299 assertThrows(function(){ %Apply({}, f, [3, 4], 0, 1) }, "myexn") | 293 // assertThrows(function(){ %Apply({}, f, [3, 4], 0, 1) }, "myexn") |
300 assertThrows(function(){ %_Call(f, {}) }, "myexn") | 294 // assertThrows(function(){ %_Call(f, {}) }, "myexn") |
301 assertThrows(function(){ %_Call(f, {}, 1, 2) }, "myexn") | 295 // assertThrows(function(){ %_Call(f, {}, 1, 2) }, "myexn") |
302 } | 296 //} |
303 | 297 // |
304 TestCallThrow(function() { throw "myexn" }) | 298 //TestCallThrow(function() { throw "myexn" }) |
305 TestCallThrow(Proxy.createFunction({}, function() { throw "myexn" })) | 299 //TestCallThrow(Proxy.createFunction({}, function() { throw "myexn" })) |
306 TestCallThrow(CreateFrozen({}, function() { throw "myexn" })) | 300 //TestCallThrow(CreateFrozen({}, function() { throw "myexn" })) |
307 | 301 // |
308 | 302 // |
309 | 303 // |
310 // Construction (new). | 304 //// Construction (new). |
311 | 305 // |
312 var prototype = {myprop: 0} | 306 //var prototype = {myprop: 0} |
313 var receiver | 307 //var receiver |
314 | 308 // |
315 var handlerWithPrototype = { | 309 //var handlerWithPrototype = { |
316 fix: function() { return { prototype: { value: prototype } }; }, | 310 // fix: function() { return { prototype: { value: prototype } }; }, |
317 get: function(r, n) { | 311 // get: function(r, n) { |
318 if (n == "length") return 2; | 312 // if (n == "length") return 2; |
319 assertEquals("prototype", n); | 313 // assertEquals("prototype", n); |
320 return prototype; | 314 // return prototype; |
321 } | 315 // } |
322 } | 316 //} |
323 | 317 // |
324 var handlerSansPrototype = { | 318 //var handlerSansPrototype = { |
325 fix: function() { return { length: { value: 2 } } }, | 319 // fix: function() { return { length: { value: 2 } } }, |
326 get: function(r, n) { | 320 // get: function(r, n) { |
327 if (n == "length") return 2; | 321 // if (n == "length") return 2; |
328 assertEquals("prototype", n); | 322 // assertEquals("prototype", n); |
329 return undefined; | 323 // return undefined; |
330 } | 324 // } |
331 } | 325 //} |
332 | 326 // |
333 function ReturnUndef(x, y) { | 327 //function ReturnUndef(x, y) { |
334 "use strict"; | 328 // "use strict"; |
335 receiver = this; | 329 // receiver = this; |
336 this.sum = x + y; | 330 // this.sum = x + y; |
337 } | 331 //} |
338 | 332 // |
339 function ReturnThis(x, y) { | 333 //function ReturnThis(x, y) { |
340 "use strict"; | 334 // "use strict"; |
341 receiver = this; | 335 // receiver = this; |
342 this.sum = x + y; | 336 // this.sum = x + y; |
343 return this; | 337 // return this; |
344 } | 338 //} |
345 | 339 // |
346 function ReturnNew(x, y) { | 340 //function ReturnNew(x, y) { |
347 "use strict"; | 341 // "use strict"; |
348 receiver = this; | 342 // receiver = this; |
349 return {sum: x + y}; | 343 // return {sum: x + y}; |
350 } | 344 //} |
351 | 345 // |
352 function ReturnNewWithProto(x, y) { | 346 //function ReturnNewWithProto(x, y) { |
353 "use strict"; | 347 // "use strict"; |
354 receiver = this; | 348 // receiver = this; |
355 var result = Object.create(prototype); | 349 // var result = Object.create(prototype); |
356 result.sum = x + y; | 350 // result.sum = x + y; |
357 return result; | 351 // return result; |
358 } | 352 //} |
359 | 353 // |
360 function TestConstruct(proto, constructTrap) { | 354 //function TestConstruct(proto, constructTrap) { |
361 TestConstruct2(proto, constructTrap, handlerWithPrototype) | 355 // TestConstruct2(proto, constructTrap, handlerWithPrototype) |
362 TestConstruct2(proto, constructTrap, handlerSansPrototype) | 356 // TestConstruct2(proto, constructTrap, handlerSansPrototype) |
363 } | 357 //} |
364 | 358 // |
365 function TestConstruct2(proto, constructTrap, handler) { | 359 //function TestConstruct2(proto, constructTrap, handler) { |
366 var f = Proxy.createFunction(handler, function() {}, constructTrap) | 360 // var f = Proxy.createFunction(handler, function() {}, constructTrap) |
367 var o = new f(11, 31) | 361 // var o = new f(11, 31) |
368 assertEquals(undefined, receiver) | 362 // assertEquals(undefined, receiver) |
369 assertEquals(42, o.sum) | 363 // assertEquals(42, o.sum) |
370 assertSame(proto, Object.getPrototypeOf(o)) | 364 // assertSame(proto, Object.getPrototypeOf(o)) |
371 | 365 // |
372 var f = CreateFrozen(handler, function() {}, constructTrap) | 366 // var f = CreateFrozen(handler, function() {}, constructTrap) |
373 var o = new f(11, 32) | 367 // var o = new f(11, 32) |
374 assertEquals(undefined, receiver) | 368 // assertEquals(undefined, receiver) |
375 assertEquals(43, o.sum) | 369 // assertEquals(43, o.sum) |
376 assertSame(proto, Object.getPrototypeOf(o)) | 370 // assertSame(proto, Object.getPrototypeOf(o)) |
377 } | 371 //} |
378 | 372 // |
379 TestConstruct(Object.prototype, ReturnNew) | 373 //TestConstruct(Object.prototype, ReturnNew) |
380 TestConstruct(prototype, ReturnNewWithProto) | 374 //TestConstruct(prototype, ReturnNewWithProto) |
381 | 375 // |
382 TestConstruct(Object.prototype, Proxy.createFunction(handler, ReturnNew)) | 376 //TestConstruct(Object.prototype, Proxy.createFunction(handler, ReturnNew)) |
383 TestConstruct(prototype, Proxy.createFunction(handler, ReturnNewWithProto)) | 377 //TestConstruct(prototype, Proxy.createFunction(handler, ReturnNewWithProto)) |
384 | 378 // |
385 TestConstruct(Object.prototype, CreateFrozen(handler, ReturnNew)) | 379 //TestConstruct(Object.prototype, CreateFrozen(handler, ReturnNew)) |
386 TestConstruct(prototype, CreateFrozen(handler, ReturnNewWithProto)) | 380 //TestConstruct(prototype, CreateFrozen(handler, ReturnNewWithProto)) |
387 | 381 // |
388 | 382 // |
389 | 383 // |
390 // Construction with derived construct trap. | 384 //// Construction with derived construct trap. |
391 | 385 // |
392 function TestConstructFromCall(proto, returnsThis, callTrap) { | 386 //function TestConstructFromCall(proto, returnsThis, callTrap) { |
393 TestConstructFromCall2(prototype, returnsThis, callTrap, handlerWithPrototype) | 387 // TestConstructFromCall2(prototype, returnsThis, callTrap, handlerWithPrototyp
e) |
394 TestConstructFromCall2(proto, returnsThis, callTrap, handlerSansPrototype) | 388 // TestConstructFromCall2(proto, returnsThis, callTrap, handlerSansPrototype) |
395 } | 389 //} |
396 | 390 // |
397 function TestConstructFromCall2(proto, returnsThis, callTrap, handler) { | 391 //function TestConstructFromCall2(proto, returnsThis, callTrap, handler) { |
398 // TODO(rossberg): handling of prototype for derived construct trap will be | 392 // // TODO(rossberg): handling of prototype for derived construct trap will be |
399 // fixed in a separate change. Commenting out checks below for now. | 393 // // fixed in a separate change. Commenting out checks below for now. |
400 var f = Proxy.createFunction(handler, callTrap) | 394 // var f = Proxy.createFunction(handler, callTrap) |
401 var o = new f(11, 31) | 395 // var o = new f(11, 31) |
402 if (returnsThis) assertEquals(o, receiver) | 396 // if (returnsThis) assertEquals(o, receiver) |
403 assertEquals(42, o.sum) | 397 // assertEquals(42, o.sum) |
404 // assertSame(proto, Object.getPrototypeOf(o)) | 398 // // assertSame(proto, Object.getPrototypeOf(o)) |
405 | 399 // |
406 var g = CreateFrozen(handler, callTrap) | 400 // var g = CreateFrozen(handler, callTrap) |
407 // assertSame(f.prototype, g.prototype) | 401 // // assertSame(f.prototype, g.prototype) |
408 var o = new g(11, 32) | 402 // var o = new g(11, 32) |
409 if (returnsThis) assertEquals(o, receiver) | 403 // if (returnsThis) assertEquals(o, receiver) |
410 assertEquals(43, o.sum) | 404 // assertEquals(43, o.sum) |
411 // assertSame(proto, Object.getPrototypeOf(o)) | 405 // // assertSame(proto, Object.getPrototypeOf(o)) |
412 } | 406 //} |
413 | 407 // |
414 TestConstructFromCall(Object.prototype, true, ReturnUndef) | 408 //TestConstructFromCall(Object.prototype, true, ReturnUndef) |
415 TestConstructFromCall(Object.prototype, true, ReturnThis) | 409 //TestConstructFromCall(Object.prototype, true, ReturnThis) |
416 TestConstructFromCall(Object.prototype, false, ReturnNew) | 410 //TestConstructFromCall(Object.prototype, false, ReturnNew) |
417 TestConstructFromCall(prototype, false, ReturnNewWithProto) | 411 //TestConstructFromCall(prototype, false, ReturnNewWithProto) |
418 | 412 // |
419 TestConstructFromCall(Object.prototype, true, | 413 //TestConstructFromCall(Object.prototype, true, |
420 Proxy.createFunction(handler, ReturnUndef)) | 414 // Proxy.createFunction(handler, ReturnUndef)) |
421 TestConstructFromCall(Object.prototype, true, | 415 //TestConstructFromCall(Object.prototype, true, |
422 Proxy.createFunction(handler, ReturnThis)) | 416 // Proxy.createFunction(handler, ReturnThis)) |
423 TestConstructFromCall(Object.prototype, false, | 417 //TestConstructFromCall(Object.prototype, false, |
424 Proxy.createFunction(handler, ReturnNew)) | 418 // Proxy.createFunction(handler, ReturnNew)) |
425 TestConstructFromCall(prototype, false, | 419 //TestConstructFromCall(prototype, false, |
426 Proxy.createFunction(handler, ReturnNewWithProto)) | 420 // Proxy.createFunction(handler, ReturnNewWithProto)) |
427 | 421 // |
428 TestConstructFromCall(Object.prototype, true, CreateFrozen({}, ReturnUndef)) | 422 //TestConstructFromCall(Object.prototype, true, CreateFrozen({}, ReturnUndef)) |
429 TestConstructFromCall(Object.prototype, true, CreateFrozen({}, ReturnThis)) | 423 //TestConstructFromCall(Object.prototype, true, CreateFrozen({}, ReturnThis)) |
430 TestConstructFromCall(Object.prototype, false, CreateFrozen({}, ReturnNew)) | 424 //TestConstructFromCall(Object.prototype, false, CreateFrozen({}, ReturnNew)) |
431 TestConstructFromCall(prototype, false, CreateFrozen({}, ReturnNewWithProto)) | 425 //TestConstructFromCall(prototype, false, CreateFrozen({}, ReturnNewWithProto)) |
432 | 426 // |
433 ReturnUndef.prototype = prototype | 427 //ReturnUndef.prototype = prototype |
434 ReturnThis.prototype = prototype | 428 //ReturnThis.prototype = prototype |
435 ReturnNew.prototype = prototype | 429 //ReturnNew.prototype = prototype |
436 ReturnNewWithProto.prototype = prototype | 430 //ReturnNewWithProto.prototype = prototype |
437 | 431 // |
438 TestConstructFromCall(prototype, true, ReturnUndef) | 432 //TestConstructFromCall(prototype, true, ReturnUndef) |
439 TestConstructFromCall(prototype, true, ReturnThis) | 433 //TestConstructFromCall(prototype, true, ReturnThis) |
440 TestConstructFromCall(Object.prototype, false, ReturnNew) | 434 //TestConstructFromCall(Object.prototype, false, ReturnNew) |
441 TestConstructFromCall(prototype, false, ReturnNewWithProto) | 435 //TestConstructFromCall(prototype, false, ReturnNewWithProto) |
442 | 436 // |
443 TestConstructFromCall(Object.prototype, true, | 437 //TestConstructFromCall(Object.prototype, true, |
444 Proxy.createFunction(handler, ReturnUndef)) | 438 // Proxy.createFunction(handler, ReturnUndef)) |
445 TestConstructFromCall(Object.prototype, true, | 439 //TestConstructFromCall(Object.prototype, true, |
446 Proxy.createFunction(handler, ReturnThis)) | 440 // Proxy.createFunction(handler, ReturnThis)) |
447 TestConstructFromCall(Object.prototype, false, | 441 //TestConstructFromCall(Object.prototype, false, |
448 Proxy.createFunction(handler, ReturnNew)) | 442 // Proxy.createFunction(handler, ReturnNew)) |
449 TestConstructFromCall(prototype, false, | 443 //TestConstructFromCall(prototype, false, |
450 Proxy.createFunction(handler, ReturnNewWithProto)) | 444 // Proxy.createFunction(handler, ReturnNewWithProto)) |
451 | 445 // |
452 TestConstructFromCall(prototype, true, | 446 //TestConstructFromCall(prototype, true, |
453 Proxy.createFunction(handlerWithPrototype, ReturnUndef)) | 447 // Proxy.createFunction(handlerWithPrototype, ReturnUndef)) |
454 TestConstructFromCall(prototype, true, | 448 //TestConstructFromCall(prototype, true, |
455 Proxy.createFunction(handlerWithPrototype, ReturnThis)) | 449 // Proxy.createFunction(handlerWithPrototype, ReturnThis)) |
456 TestConstructFromCall(Object.prototype, false, | 450 //TestConstructFromCall(Object.prototype, false, |
457 Proxy.createFunction(handlerWithPrototype, ReturnNew)) | 451 // Proxy.createFunction(handlerWithPrototype, ReturnNew)) |
458 TestConstructFromCall(prototype, false, | 452 //TestConstructFromCall(prototype, false, |
459 Proxy.createFunction(handlerWithPrototype, | 453 // Proxy.createFunction(handlerWithPrototype, |
460 ReturnNewWithProto)) | 454 // ReturnNewWithProto)) |
461 | 455 // |
462 TestConstructFromCall(prototype, true, | 456 //TestConstructFromCall(prototype, true, |
463 CreateFrozen(handlerWithPrototype, ReturnUndef)) | 457 // CreateFrozen(handlerWithPrototype, ReturnUndef)) |
464 TestConstructFromCall(prototype, true, | 458 //TestConstructFromCall(prototype, true, |
465 CreateFrozen(handlerWithPrototype, ReturnThis)) | 459 // CreateFrozen(handlerWithPrototype, ReturnThis)) |
466 TestConstructFromCall(Object.prototype, false, | 460 //TestConstructFromCall(Object.prototype, false, |
467 CreateFrozen(handlerWithPrototype, ReturnNew)) | 461 // CreateFrozen(handlerWithPrototype, ReturnNew)) |
468 TestConstructFromCall(prototype, false, | 462 //TestConstructFromCall(prototype, false, |
469 CreateFrozen(handlerWithPrototype, ReturnNewWithProto)) | 463 // CreateFrozen(handlerWithPrototype, ReturnNewWithProto)) |
470 | 464 // |
471 | 465 // |
472 | 466 // |
473 // Throwing from the construct trap. | 467 //// Throwing from the construct trap. |
474 | 468 // |
475 function TestConstructThrow(trap) { | 469 //function TestConstructThrow(trap) { |
476 TestConstructThrow2(Proxy.createFunction({ fix: function() {return {};} }, | 470 // TestConstructThrow2(Proxy.createFunction({ fix: function() {return {};} }, |
477 trap)) | 471 // trap)) |
478 TestConstructThrow2(Proxy.createFunction({ fix: function() {return {};} }, | 472 // TestConstructThrow2(Proxy.createFunction({ fix: function() {return {};} }, |
479 function() {}, | 473 // function() {}, |
480 trap)) | 474 // trap)) |
481 } | 475 //} |
482 | 476 // |
483 function TestConstructThrow2(f) { | 477 //function TestConstructThrow2(f) { |
484 assertThrows(function(){ new f(11) }, "myexn") | 478 // assertThrows(function(){ new f(11) }, "myexn") |
485 Object.freeze(f) | 479 // Object.freeze(f) |
486 assertThrows(function(){ new f(11) }, "myexn") | 480 // assertThrows(function(){ new f(11) }, "myexn") |
487 } | 481 //} |
488 | 482 // |
489 TestConstructThrow(function() { throw "myexn" }) | 483 //TestConstructThrow(function() { throw "myexn" }) |
490 TestConstructThrow(Proxy.createFunction({}, function() { throw "myexn" })) | 484 //TestConstructThrow(Proxy.createFunction({}, function() { throw "myexn" })) |
491 TestConstructThrow(CreateFrozen({}, function() { throw "myexn" })) | 485 //TestConstructThrow(CreateFrozen({}, function() { throw "myexn" })) |
492 | 486 // |
493 | 487 // |
494 | 488 // |
495 // Using function proxies as getters and setters. | 489 //// Using function proxies as getters and setters. |
496 | 490 // |
497 var value | 491 //var value |
498 var receiver | 492 //var receiver |
499 | 493 // |
500 function TestAccessorCall(getterCallTrap, setterCallTrap) { | 494 //function TestAccessorCall(getterCallTrap, setterCallTrap) { |
501 var handler = { fix: function() { return {} } } | 495 // var handler = { fix: function() { return {} } } |
502 var pgetter = Proxy.createFunction(handler, getterCallTrap) | 496 // var pgetter = Proxy.createFunction(handler, getterCallTrap) |
503 var psetter = Proxy.createFunction(handler, setterCallTrap) | 497 // var psetter = Proxy.createFunction(handler, setterCallTrap) |
504 | 498 // |
505 var o = {} | 499 // var o = {} |
506 var oo = Object.create(o) | 500 // var oo = Object.create(o) |
507 Object.defineProperty(o, "a", {get: pgetter, set: psetter}) | 501 // Object.defineProperty(o, "a", {get: pgetter, set: psetter}) |
508 Object.defineProperty(o, "b", {get: pgetter}) | 502 // Object.defineProperty(o, "b", {get: pgetter}) |
509 Object.defineProperty(o, "c", {set: psetter}) | 503 // Object.defineProperty(o, "c", {set: psetter}) |
510 Object.defineProperty(o, "3", {get: pgetter, set: psetter}) | 504 // Object.defineProperty(o, "3", {get: pgetter, set: psetter}) |
511 Object.defineProperty(oo, "a", {value: 43}) | 505 // Object.defineProperty(oo, "a", {value: 43}) |
512 | 506 // |
513 receiver = "" | 507 // receiver = "" |
514 assertEquals(42, o.a) | 508 // assertEquals(42, o.a) |
515 assertSame(o, receiver) | 509 // assertSame(o, receiver) |
516 receiver = "" | 510 // receiver = "" |
517 assertEquals(42, o.b) | 511 // assertEquals(42, o.b) |
518 assertSame(o, receiver) | 512 // assertSame(o, receiver) |
519 receiver = "" | 513 // receiver = "" |
520 assertEquals(undefined, o.c) | 514 // assertEquals(undefined, o.c) |
521 assertEquals("", receiver) | 515 // assertEquals("", receiver) |
522 receiver = "" | 516 // receiver = "" |
523 assertEquals(42, o["a"]) | 517 // assertEquals(42, o["a"]) |
524 assertSame(o, receiver) | 518 // assertSame(o, receiver) |
525 receiver = "" | 519 // receiver = "" |
526 assertEquals(42, o[3]) | 520 // assertEquals(42, o[3]) |
527 assertSame(o, receiver) | 521 // assertSame(o, receiver) |
528 | 522 // |
529 receiver = "" | 523 // receiver = "" |
530 assertEquals(43, oo.a) | 524 // assertEquals(43, oo.a) |
531 assertEquals("", receiver) | 525 // assertEquals("", receiver) |
532 receiver = "" | 526 // receiver = "" |
533 assertEquals(42, oo.b) | 527 // assertEquals(42, oo.b) |
534 assertSame(oo, receiver) | 528 // assertSame(oo, receiver) |
535 receiver = "" | 529 // receiver = "" |
536 assertEquals(undefined, oo.c) | 530 // assertEquals(undefined, oo.c) |
537 assertEquals("", receiver) | 531 // assertEquals("", receiver) |
538 receiver = "" | 532 // receiver = "" |
539 assertEquals(43, oo["a"]) | 533 // assertEquals(43, oo["a"]) |
540 assertEquals("", receiver) | 534 // assertEquals("", receiver) |
541 receiver = "" | 535 // receiver = "" |
542 assertEquals(42, oo[3]) | 536 // assertEquals(42, oo[3]) |
543 assertSame(oo, receiver) | 537 // assertSame(oo, receiver) |
544 | 538 // |
545 receiver = "" | 539 // receiver = "" |
546 assertEquals(50, o.a = 50) | 540 // assertEquals(50, o.a = 50) |
547 assertSame(o, receiver) | 541 // assertSame(o, receiver) |
548 assertEquals(50, value) | 542 // assertEquals(50, value) |
549 receiver = "" | 543 // receiver = "" |
550 assertEquals(51, o.b = 51) | 544 // assertEquals(51, o.b = 51) |
551 assertEquals("", receiver) | 545 // assertEquals("", receiver) |
552 assertEquals(50, value) // no setter | 546 // assertEquals(50, value) // no setter |
553 assertThrows(function() { "use strict"; o.b = 51 }, TypeError) | 547 // assertThrows(function() { "use strict"; o.b = 51 }, TypeError) |
554 receiver = "" | 548 // receiver = "" |
555 assertEquals(52, o.c = 52) | 549 // assertEquals(52, o.c = 52) |
556 assertSame(o, receiver) | 550 // assertSame(o, receiver) |
557 assertEquals(52, value) | 551 // assertEquals(52, value) |
558 receiver = "" | 552 // receiver = "" |
559 assertEquals(53, o["a"] = 53) | 553 // assertEquals(53, o["a"] = 53) |
560 assertSame(o, receiver) | 554 // assertSame(o, receiver) |
561 assertEquals(53, value) | 555 // assertEquals(53, value) |
562 receiver = "" | 556 // receiver = "" |
563 assertEquals(54, o[3] = 54) | 557 // assertEquals(54, o[3] = 54) |
564 assertSame(o, receiver) | 558 // assertSame(o, receiver) |
565 assertEquals(54, value) | 559 // assertEquals(54, value) |
566 | 560 // |
567 value = 0 | 561 // value = 0 |
568 receiver = "" | 562 // receiver = "" |
569 assertEquals(60, oo.a = 60) | 563 // assertEquals(60, oo.a = 60) |
570 assertEquals("", receiver) | 564 // assertEquals("", receiver) |
571 assertEquals(0, value) // oo has own 'a' | 565 // assertEquals(0, value) // oo has own 'a' |
572 assertEquals(61, oo.b = 61) | 566 // assertEquals(61, oo.b = 61) |
573 assertSame("", receiver) | 567 // assertSame("", receiver) |
574 assertEquals(0, value) // no setter | 568 // assertEquals(0, value) // no setter |
575 assertThrows(function() { "use strict"; oo.b = 61 }, TypeError) | 569 // assertThrows(function() { "use strict"; oo.b = 61 }, TypeError) |
576 receiver = "" | 570 // receiver = "" |
577 assertEquals(62, oo.c = 62) | 571 // assertEquals(62, oo.c = 62) |
578 assertSame(oo, receiver) | 572 // assertSame(oo, receiver) |
579 assertEquals(62, value) | 573 // assertEquals(62, value) |
580 receiver = "" | 574 // receiver = "" |
581 assertEquals(63, oo["c"] = 63) | 575 // assertEquals(63, oo["c"] = 63) |
582 assertSame(oo, receiver) | 576 // assertSame(oo, receiver) |
583 assertEquals(63, value) | 577 // assertEquals(63, value) |
584 receiver = "" | 578 // receiver = "" |
585 assertEquals(64, oo[3] = 64) | 579 // assertEquals(64, oo[3] = 64) |
586 assertSame(oo, receiver) | 580 // assertSame(oo, receiver) |
587 assertEquals(64, value) | 581 // assertEquals(64, value) |
588 } | 582 //} |
589 | 583 // |
590 TestAccessorCall( | 584 //TestAccessorCall( |
591 function() { receiver = this; return 42 }, | 585 // function() { receiver = this; return 42 }, |
592 function(x) { receiver = this; value = x } | 586 // function(x) { receiver = this; value = x } |
593 ) | 587 //) |
594 | 588 // |
595 TestAccessorCall( | 589 //TestAccessorCall( |
596 function() { "use strict"; receiver = this; return 42 }, | 590 // function() { "use strict"; receiver = this; return 42 }, |
597 function(x) { "use strict"; receiver = this; value = x } | 591 // function(x) { "use strict"; receiver = this; value = x } |
598 ) | 592 //) |
599 | 593 // |
600 TestAccessorCall( | 594 //TestAccessorCall( |
601 Proxy.createFunction({}, function() { receiver = this; return 42 }), | 595 // Proxy.createFunction({}, function() { receiver = this; return 42 }), |
602 Proxy.createFunction({}, function(x) { receiver = this; value = x }) | 596 // Proxy.createFunction({}, function(x) { receiver = this; value = x }) |
603 ) | 597 //) |
604 | 598 // |
605 TestAccessorCall( | 599 //TestAccessorCall( |
606 CreateFrozen({}, function() { receiver = this; return 42 }), | 600 // CreateFrozen({}, function() { receiver = this; return 42 }), |
607 CreateFrozen({}, function(x) { receiver = this; value = x }) | 601 // CreateFrozen({}, function(x) { receiver = this; value = x }) |
608 ) | 602 //) |
609 | 603 // |
610 | 604 // |
611 | 605 // |
612 // Passing a proxy function to higher-order library functions. | 606 //// Passing a proxy function to higher-order library functions. |
613 | 607 // |
614 function TestHigherOrder(f) { | 608 //function TestHigherOrder(f) { |
615 assertEquals(6, [6, 2].map(f)[0]) | 609 // assertEquals(6, [6, 2].map(f)[0]) |
616 assertEquals(4, [5, 2].reduce(f, 4)) | 610 // assertEquals(4, [5, 2].reduce(f, 4)) |
617 assertTrue([1, 2].some(f)) | 611 // assertTrue([1, 2].some(f)) |
618 assertEquals("a.b.c", "a.b.c".replace(".", f)) | 612 // assertEquals("a.b.c", "a.b.c".replace(".", f)) |
619 } | 613 //} |
620 | 614 // |
621 TestHigherOrder(function(x) { return x }) | 615 //TestHigherOrder(function(x) { return x }) |
622 TestHigherOrder(function(x) { "use strict"; return x }) | 616 //TestHigherOrder(function(x) { "use strict"; return x }) |
623 TestHigherOrder(Proxy.createFunction({}, function(x) { return x })) | 617 //TestHigherOrder(Proxy.createFunction({}, function(x) { return x })) |
624 TestHigherOrder(CreateFrozen({}, function(x) { return x })) | 618 //TestHigherOrder(CreateFrozen({}, function(x) { return x })) |
625 | 619 // |
626 | 620 // |
627 | 621 // |
628 // TODO(rossberg): Ultimately, I want to have the following test function | 622 //// TODO(rossberg): Ultimately, I want to have the following test function |
629 // run through, but it currently fails on so many cases (some not even | 623 //// run through, but it currently fails on so many cases (some not even |
630 // involving proxies), that I leave that for later... | 624 //// involving proxies), that I leave that for later... |
631 /* | 625 ///* |
632 function TestCalls() { | 626 //function TestCalls() { |
633 var handler = { | 627 // var handler = { |
634 get: function(r, k) { | 628 // get: function(r, k) { |
635 return k == "length" ? 2 : Function.prototype[k] | 629 // return k == "length" ? 2 : Function.prototype[k] |
636 } | 630 // } |
637 } | 631 // } |
638 var bind = Function.prototype.bind | 632 // var bind = Function.prototype.bind |
639 var o = {} | 633 // var o = {} |
640 | 634 // |
641 var traps = [ | 635 // var traps = [ |
642 function(x, y) { | 636 // function(x, y) { |
643 return {receiver: this, result: x + y, strict: false} | 637 // return {receiver: this, result: x + y, strict: false} |
644 }, | 638 // }, |
645 function(x, y) { "use strict"; | 639 // function(x, y) { "use strict"; |
646 return {receiver: this, result: x + y, strict: true} | 640 // return {receiver: this, result: x + y, strict: true} |
647 }, | 641 // }, |
648 function() { | 642 // function() { |
649 var x = arguments[0], y = arguments[1] | 643 // var x = arguments[0], y = arguments[1] |
650 return {receiver: this, result: x + y, strict: false} | 644 // return {receiver: this, result: x + y, strict: false} |
651 }, | 645 // }, |
652 Proxy.createFunction(handler, function(x, y) { | 646 // Proxy.createFunction(handler, function(x, y) { |
653 return {receiver: this, result: x + y, strict: false} | 647 // return {receiver: this, result: x + y, strict: false} |
654 }), | 648 // }), |
655 Proxy.createFunction(handler, function() { | 649 // Proxy.createFunction(handler, function() { |
656 var x = arguments[0], y = arguments[1] | 650 // var x = arguments[0], y = arguments[1] |
657 return {receiver: this, result: x + y, strict: false} | 651 // return {receiver: this, result: x + y, strict: false} |
658 }), | 652 // }), |
659 Proxy.createFunction(handler, function(x, y) { "use strict" | 653 // Proxy.createFunction(handler, function(x, y) { "use strict" |
660 return {receiver: this, result: x + y, strict: true} | 654 // return {receiver: this, result: x + y, strict: true} |
661 }), | 655 // }), |
662 CreateFrozen(handler, function(x, y) { | 656 // CreateFrozen(handler, function(x, y) { |
663 return {receiver: this, result: x + y, strict: false} | 657 // return {receiver: this, result: x + y, strict: false} |
664 }), | 658 // }), |
665 CreateFrozen(handler, function(x, y) { "use strict" | 659 // CreateFrozen(handler, function(x, y) { "use strict" |
666 return {receiver: this, result: x + y, strict: true} | 660 // return {receiver: this, result: x + y, strict: true} |
667 }), | 661 // }), |
668 ] | 662 // ] |
669 var creates = [ | 663 // var creates = [ |
670 function(trap) { return trap }, | 664 // function(trap) { return trap }, |
671 function(trap) { return CreateFrozen({}, callTrap) }, | 665 // function(trap) { return CreateFrozen({}, callTrap) }, |
672 function(trap) { return Proxy.createFunction(handler, callTrap) }, | 666 // function(trap) { return Proxy.createFunction(handler, callTrap) }, |
673 function(trap) { | 667 // function(trap) { |
674 return Proxy.createFunction(handler, CreateFrozen({}, callTrap)) | 668 // return Proxy.createFunction(handler, CreateFrozen({}, callTrap)) |
675 }, | 669 // }, |
676 function(trap) { | 670 // function(trap) { |
677 return Proxy.createFunction(handler, Proxy.createFunction(handler, callTra
p)) | 671 // return Proxy.createFunction(handler, Proxy.createFunction(handler, callT
rap)) |
678 }, | 672 // }, |
679 ] | 673 // ] |
680 var binds = [ | 674 // var binds = [ |
681 function(f, o, x, y) { return f }, | 675 // function(f, o, x, y) { return f }, |
682 function(f, o, x, y) { return bind.call(f, o) }, | 676 // function(f, o, x, y) { return bind.call(f, o) }, |
683 function(f, o, x, y) { return bind.call(f, o, x) }, | 677 // function(f, o, x, y) { return bind.call(f, o, x) }, |
684 function(f, o, x, y) { return bind.call(f, o, x, y) }, | 678 // function(f, o, x, y) { return bind.call(f, o, x, y) }, |
685 function(f, o, x, y) { return bind.call(f, o, x, y, 5) }, | 679 // function(f, o, x, y) { return bind.call(f, o, x, y, 5) }, |
686 function(f, o, x, y) { return bind.call(bind.call(f, o), {}, x, y) }, | 680 // function(f, o, x, y) { return bind.call(bind.call(f, o), {}, x, y) }, |
687 function(f, o, x, y) { return bind.call(bind.call(f, o, x), {}, y) }, | 681 // function(f, o, x, y) { return bind.call(bind.call(f, o, x), {}, y) }, |
688 function(f, o, x, y) { return bind.call(bind.call(f, o, x, y), {}, 5) }, | 682 // function(f, o, x, y) { return bind.call(bind.call(f, o, x, y), {}, 5) }, |
689 ] | 683 // ] |
690 var calls = [ | 684 // var calls = [ |
691 function(f, x, y) { return f(x, y) }, | 685 // function(f, x, y) { return f(x, y) }, |
692 function(f, x, y) { var g = f; return g(x, y) }, | 686 // function(f, x, y) { var g = f; return g(x, y) }, |
693 function(f, x, y) { with ({}) return f(x, y) }, | 687 // function(f, x, y) { with ({}) return f(x, y) }, |
694 function(f, x, y) { var g = f; with ({}) return g(x, y) }, | 688 // function(f, x, y) { var g = f; with ({}) return g(x, y) }, |
695 function(f, x, y, o) { with (o) return f(x, y) }, | 689 // function(f, x, y, o) { with (o) return f(x, y) }, |
696 function(f, x, y, o) { return f.call(o, x, y) }, | 690 // function(f, x, y, o) { return f.call(o, x, y) }, |
697 function(f, x, y, o) { return f.apply(o, [x, y]) }, | 691 // function(f, x, y, o) { return f.apply(o, [x, y]) }, |
698 function(f, x, y, o) { return Function.prototype.call.call(f, o, x, y) }, | 692 // function(f, x, y, o) { return Function.prototype.call.call(f, o, x, y) }, |
699 function(f, x, y, o) { return Function.prototype.apply.call(f, o, [x, y]) }, | 693 // function(f, x, y, o) { return Function.prototype.apply.call(f, o, [x, y])
}, |
700 function(f, x, y, o) { return %_Call(f, o, x, y) }, | 694 // function(f, x, y, o) { return %_Call(f, o, x, y) }, |
701 function(f, x, y, o) { return %Call(f, o, x, y) }, | 695 // function(f, x, y, o) { return %Call(f, o, x, y) }, |
702 function(f, x, y, o) { return %Apply(f, o, [null, x, y, null], 1, 2) }, | 696 // function(f, x, y, o) { return %Apply(f, o, [null, x, y, null], 1, 2) }, |
703 function(f, x, y, o) { return %Apply(f, o, arguments, 2, 2) }, | 697 // function(f, x, y, o) { return %Apply(f, o, arguments, 2, 2) }, |
704 function(f, x, y, o) { if (typeof o == "object") return o.f(x, y) }, | 698 // function(f, x, y, o) { if (typeof o == "object") return o.f(x, y) }, |
705 function(f, x, y, o) { if (typeof o == "object") return o["f"](x, y) }, | 699 // function(f, x, y, o) { if (typeof o == "object") return o["f"](x, y) }, |
706 function(f, x, y, o) { if (typeof o == "object") return (1, o).f(x, y) }, | 700 // function(f, x, y, o) { if (typeof o == "object") return (1, o).f(x, y) }, |
707 function(f, x, y, o) { if (typeof o == "object") return (1, o)["f"](x, y) }, | 701 // function(f, x, y, o) { if (typeof o == "object") return (1, o)["f"](x, y)
}, |
708 ] | 702 // ] |
709 var receivers = [o, global_object, undefined, null, 2, "bla", true] | 703 // var receivers = [o, global_object, undefined, null, 2, "bla", true] |
710 var expectedSloppies = [o, global_object, global_object, global_object] | 704 // var expectedSloppies = [o, global_object, global_object, global_object] |
711 | 705 // |
712 for (var t = 0; t < traps.length; ++t) { | 706 // for (var t = 0; t < traps.length; ++t) { |
713 for (var i = 0; i < creates.length; ++i) { | 707 // for (var i = 0; i < creates.length; ++i) { |
714 for (var j = 0; j < binds.length; ++j) { | 708 // for (var j = 0; j < binds.length; ++j) { |
715 for (var k = 0; k < calls.length; ++k) { | 709 // for (var k = 0; k < calls.length; ++k) { |
716 for (var m = 0; m < receivers.length; ++m) { | 710 // for (var m = 0; m < receivers.length; ++m) { |
717 for (var n = 0; n < receivers.length; ++n) { | 711 // for (var n = 0; n < receivers.length; ++n) { |
718 var bound = receivers[m] | 712 // var bound = receivers[m] |
719 var receiver = receivers[n] | 713 // var receiver = receivers[n] |
720 var func = binds[j](creates[i](traps[t]), bound, 31, 11) | 714 // var func = binds[j](creates[i](traps[t]), bound, 31, 11) |
721 var expected = j > 0 ? bound : receiver | 715 // var expected = j > 0 ? bound : receiver |
722 var expectedSloppy = expectedSloppies[j > 0 ? m : n] | 716 // var expectedSloppy = expectedSloppies[j > 0 ? m : n] |
723 o.f = func | 717 // o.f = func |
724 global_object.f = func | 718 // global_object.f = func |
725 var x = calls[k](func, 11, 31, receiver) | 719 // var x = calls[k](func, 11, 31, receiver) |
726 if (x !== undefined) { | 720 // if (x !== undefined) { |
727 assertEquals(42, x.result) | 721 // assertEquals(42, x.result) |
728 if (calls[k].length < 4) | 722 // if (calls[k].length < 4) |
729 assertSame(x.strict ? undefined : global_object, x.receiver) | 723 // assertSame(x.strict ? undefined : global_object, x.receiver) |
730 else if (x.strict) | 724 // else if (x.strict) |
731 assertSame(expected, x.receiver) | 725 // assertSame(expected, x.receiver) |
732 else if (expectedSloppy === undefined) | 726 // else if (expectedSloppy === undefined) |
733 assertSame(expected, x.receiver.valueOf()) | 727 // assertSame(expected, x.receiver.valueOf()) |
734 else | 728 // else |
735 assertSame(expectedSloppy, x.receiver) | 729 // assertSame(expectedSloppy, x.receiver) |
736 } | 730 // } |
737 } | 731 // } |
738 } | 732 // } |
739 } | 733 // } |
740 } | 734 // } |
741 } | 735 // } |
742 } | 736 // } |
743 } | 737 //} |
744 | 738 // |
745 TestCalls() | 739 //TestCalls() |
746 */ | 740 //*/ |
747 | 741 // |
748 var realms = [Realm.create(), Realm.create()]; | 742 //var realms = [Realm.create(), Realm.create()]; |
749 Realm.shared = {}; | 743 //Realm.shared = {}; |
750 | 744 // |
751 Realm.eval(realms[0], "function f() { return this; };"); | 745 //Realm.eval(realms[0], "function f() { return this; };"); |
752 Realm.eval(realms[0], "Realm.shared.f = f;"); | 746 //Realm.eval(realms[0], "Realm.shared.f = f;"); |
753 Realm.eval(realms[0], "Realm.shared.fg = this;"); | 747 //Realm.eval(realms[0], "Realm.shared.fg = this;"); |
754 Realm.eval(realms[1], "function g() { return this; };"); | 748 //Realm.eval(realms[1], "function g() { return this; };"); |
755 Realm.eval(realms[1], "Realm.shared.g = g;"); | 749 //Realm.eval(realms[1], "Realm.shared.g = g;"); |
756 Realm.eval(realms[1], "Realm.shared.gg = this;"); | 750 //Realm.eval(realms[1], "Realm.shared.gg = this;"); |
757 | 751 // |
758 var fp = Proxy.createFunction({}, Realm.shared.f); | 752 //var fp = Proxy.createFunction({}, Realm.shared.f); |
759 var gp = Proxy.createFunction({}, Realm.shared.g); | 753 //var gp = Proxy.createFunction({}, Realm.shared.g); |
760 | 754 // |
761 for (var i = 0; i < 10; i++) { | 755 //for (var i = 0; i < 10; i++) { |
762 assertEquals(Realm.shared.fg, fp()); | 756 // assertEquals(Realm.shared.fg, fp()); |
763 assertEquals(Realm.shared.gg, gp()); | 757 // assertEquals(Realm.shared.gg, gp()); |
764 | 758 // |
765 with (this) { | 759 // with (this) { |
766 assertEquals(this, fp()); | 760 // assertEquals(this, fp()); |
767 assertEquals(this, gp()); | 761 // assertEquals(this, gp()); |
768 } | 762 // } |
769 | 763 // |
770 with ({}) { | 764 // with ({}) { |
771 assertEquals(Realm.shared.fg, fp()); | 765 // assertEquals(Realm.shared.fg, fp()); |
772 assertEquals(Realm.shared.gg, gp()); | 766 // assertEquals(Realm.shared.gg, gp()); |
773 } | 767 // } |
774 } | 768 //} |
OLD | NEW |