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