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 28 matching lines...) Expand all Loading... |
39 | 39 |
40 | 40 |
41 // Calling (call, Function.prototype.call, Function.prototype.apply, | 41 // Calling (call, Function.prototype.call, Function.prototype.apply, |
42 // Function.prototype.bind). | 42 // Function.prototype.bind). |
43 | 43 |
44 var global_object = this | 44 var global_object = this |
45 var receiver | 45 var receiver |
46 | 46 |
47 function TestCall(isStrict, callTrap) { | 47 function TestCall(isStrict, callTrap) { |
48 assertEquals(42, callTrap(5, 37)) | 48 assertEquals(42, callTrap(5, 37)) |
49 // TODO(rossberg): unrelated bug: this does not succeed for optimized code: | 49 assertEquals(isStrict ? undefined : global_object, receiver) |
50 // assertEquals(isStrict ? undefined : global_object, receiver) | |
51 | 50 |
52 var f = Proxy.createFunction({}, callTrap) | 51 var f = Proxy.createFunction({}, callTrap) |
53 receiver = 333 | 52 receiver = 333 |
54 assertEquals(42, f(11, 31)) | 53 assertEquals(42, f(11, 31)) |
55 assertEquals(isStrict ? undefined : global_object, receiver) | 54 assertEquals(isStrict ? undefined : global_object, receiver) |
56 var o = {} | 55 var o = {f: f} |
| 56 receiver = 333 |
| 57 assertEquals(42, o.f(10, 32)) |
| 58 assertSame(o, receiver) |
| 59 receiver = 333 |
| 60 assertEquals(42, o["f"](9, 33)) |
| 61 assertSame(o, receiver) |
| 62 receiver = 333 |
| 63 assertEquals(42, (1, o).f(8, 34)) |
| 64 assertSame(o, receiver) |
| 65 receiver = 333 |
| 66 assertEquals(42, (1, o)["f"](7, 35)) |
| 67 assertSame(o, receiver) |
| 68 receiver = 333 |
57 assertEquals(42, Function.prototype.call.call(f, o, 20, 22)) | 69 assertEquals(42, Function.prototype.call.call(f, o, 20, 22)) |
58 assertEquals(o, receiver) | 70 assertSame(o, receiver) |
| 71 receiver = 333 |
59 assertEquals(43, Function.prototype.call.call(f, null, 20, 23)) | 72 assertEquals(43, Function.prototype.call.call(f, null, 20, 23)) |
60 assertEquals(isStrict ? null : global_object, receiver) | 73 assertEquals(isStrict ? null : global_object, receiver) |
61 assertEquals(44, Function.prototype.call.call(f, 2, 21, 23)) | 74 assertEquals(44, Function.prototype.call.call(f, 2, 21, 23)) |
62 assertEquals(2, receiver.valueOf()) | 75 assertEquals(2, receiver.valueOf()) |
63 receiver = 333 | 76 receiver = 333 |
64 assertEquals(32, Function.prototype.apply.call(f, o, [17, 15])) | 77 assertEquals(32, Function.prototype.apply.call(f, o, [17, 15])) |
65 assertEquals(o, receiver) | 78 assertSame(o, receiver) |
66 var ff = Function.prototype.bind.call(f, o, 12) | 79 var ff = Function.prototype.bind.call(f, o, 12) |
67 receiver = 333 | 80 receiver = 333 |
68 assertEquals(42, ff(30)) | 81 assertEquals(42, ff(30)) |
69 assertEquals(o, receiver) | 82 assertSame(o, receiver) |
70 receiver = 333 | 83 receiver = 333 |
71 assertEquals(32, Function.prototype.apply.call(ff, {}, [20])) | 84 assertEquals(32, Function.prototype.apply.call(ff, {}, [20])) |
72 assertEquals(o, receiver) | 85 assertSame(o, receiver) |
73 | 86 |
74 var f = CreateFrozen({}, callTrap) | 87 var f = CreateFrozen({}, callTrap) |
75 receiver = 333 | 88 receiver = 333 |
76 assertEquals(42, f(11, 31)) | 89 assertEquals(42, f(11, 31)) |
77 // TODO(rossberg): unrelated bug: this does not succeed for optimized code. | 90 assertEquals(isStrict ? undefined : global_object, receiver) |
78 // assertEquals(isStrict ? undefined : global, receiver) | 91 var o = {f: f} |
| 92 receiver = 333 |
| 93 assertEquals(42, o.f(10, 32)) |
| 94 assertSame(o, receiver) |
| 95 receiver = 333 |
| 96 assertEquals(42, o["f"](9, 33)) |
| 97 assertSame(o, receiver) |
| 98 receiver = 333 |
| 99 assertEquals(42, (1, o).f(8, 34)) |
| 100 assertSame(o, receiver) |
| 101 receiver = 333 |
| 102 assertEquals(42, (1, o)["f"](7, 35)) |
| 103 assertSame(o, receiver) |
79 receiver = 333 | 104 receiver = 333 |
80 assertEquals(42, Function.prototype.call.call(f, o, 20, 22)) | 105 assertEquals(42, Function.prototype.call.call(f, o, 20, 22)) |
81 assertEquals(o, receiver) | 106 assertSame(o, receiver) |
82 receiver = 333 | 107 receiver = 333 |
83 assertEquals(32, Function.prototype.apply.call(f, o, [17, 15])) | 108 assertEquals(32, Function.prototype.apply.call(f, o, [17, 15])) |
84 assertEquals(o, receiver) | 109 assertSame(o, receiver) |
85 receiver = 333 | 110 receiver = 333 |
86 assertEquals(42, ff(30)) | 111 assertEquals(42, ff(30)) |
87 assertEquals(o, receiver) | 112 assertSame(o, receiver) |
88 receiver = 333 | 113 receiver = 333 |
89 assertEquals(32, Function.prototype.apply.call(ff, {}, [20])) | 114 assertEquals(32, Function.prototype.apply.call(ff, {}, [20])) |
90 assertEquals(o, receiver) | 115 assertSame(o, receiver) |
91 } | 116 } |
92 | 117 |
93 TestCall(false, function(x, y) { | 118 TestCall(false, function(x, y) { |
94 receiver = this; return x + y | 119 receiver = this; return x + y |
95 }) | 120 }) |
96 | 121 |
97 TestCall(true, function(x, y) { | 122 TestCall(true, function(x, y) { |
98 "use strict"; | 123 "use strict"; |
99 receiver = this; return x + y | 124 receiver = this; return x + y |
100 }) | 125 }) |
101 | 126 |
102 TestCall(false, Proxy.createFunction({}, function(x, y) { | 127 TestCall(false, Proxy.createFunction({}, function(x, y) { |
103 receiver = this; return x + y | 128 receiver = this; return x + y |
104 })) | 129 })) |
105 | 130 |
106 TestCall(true, Proxy.createFunction({}, function(x, y) { | 131 TestCall(true, Proxy.createFunction({}, function(x, y) { |
107 "use strict"; | 132 "use strict"; |
108 receiver = this; return x + y | 133 receiver = this; return x + y |
109 })) | 134 })) |
110 | 135 |
111 TestCall(false, CreateFrozen({}, function(x, y) { | 136 TestCall(false, CreateFrozen({}, function(x, y) { |
112 receiver = this; return x + y | 137 receiver = this; return x + y |
113 })) | 138 })) |
114 | 139 |
115 | 140 |
| 141 function TestCallIntrinsic(type, callTrap) { |
| 142 var f = Proxy.createFunction({}, callTrap) |
| 143 var x = f() |
| 144 assertTrue(typeof x == type) |
| 145 } |
| 146 |
| 147 TestCallIntrinsic("boolean", Boolean) |
| 148 TestCallIntrinsic("number", Number) |
| 149 TestCallIntrinsic("string", String) |
| 150 TestCallIntrinsic("object", Object) |
| 151 TestCallIntrinsic("function", Function) |
| 152 |
| 153 |
116 function TestCallThrow(callTrap) { | 154 function TestCallThrow(callTrap) { |
117 var f = Proxy.createFunction({}, callTrap) | 155 var f = Proxy.createFunction({}, callTrap) |
118 assertThrows(function(){ f(11) }, "myexn") | 156 assertThrows(function(){ f(11) }, "myexn") |
| 157 assertThrows(function(){ ({x: f}).x(11) }, "myexn") |
| 158 assertThrows(function(){ ({x: f})["x"](11) }, "myexn") |
119 assertThrows(function(){ Function.prototype.call.call(f, {}, 2) }, "myexn") | 159 assertThrows(function(){ Function.prototype.call.call(f, {}, 2) }, "myexn") |
120 assertThrows(function(){ Function.prototype.apply.call(f, {}, [1]) }, "myexn") | 160 assertThrows(function(){ Function.prototype.apply.call(f, {}, [1]) }, "myexn") |
121 | 161 |
122 var f = CreateFrozen({}, callTrap) | 162 var f = CreateFrozen({}, callTrap) |
123 assertThrows(function(){ f(11) }, "myexn") | 163 assertThrows(function(){ f(11) }, "myexn") |
| 164 assertThrows(function(){ ({x: f}).x(11) }, "myexn") |
| 165 assertThrows(function(){ ({x: f})["x"](11) }, "myexn") |
124 assertThrows(function(){ Function.prototype.call.call(f, {}, 2) }, "myexn") | 166 assertThrows(function(){ Function.prototype.call.call(f, {}, 2) }, "myexn") |
125 assertThrows(function(){ Function.prototype.apply.call(f, {}, [1]) }, "myexn") | 167 assertThrows(function(){ Function.prototype.apply.call(f, {}, [1]) }, "myexn") |
126 } | 168 } |
127 | 169 |
128 TestCallThrow(function() { throw "myexn" }) | 170 TestCallThrow(function() { throw "myexn" }) |
129 TestCallThrow(Proxy.createFunction({}, function() { throw "myexn" })) | 171 TestCallThrow(Proxy.createFunction({}, function() { throw "myexn" })) |
130 TestCallThrow(CreateFrozen({}, function() { throw "myexn" })) | 172 TestCallThrow(CreateFrozen({}, function() { throw "myexn" })) |
131 | 173 |
132 | 174 |
133 | 175 |
(...skipping 24 matching lines...) Expand all Loading... |
158 } | 200 } |
159 | 201 |
160 function TestConstruct(proto, constructTrap) { | 202 function TestConstruct(proto, constructTrap) { |
161 TestConstruct2(proto, constructTrap, handlerWithPrototype) | 203 TestConstruct2(proto, constructTrap, handlerWithPrototype) |
162 TestConstruct2(proto, constructTrap, handlerSansPrototype) | 204 TestConstruct2(proto, constructTrap, handlerSansPrototype) |
163 } | 205 } |
164 | 206 |
165 function TestConstruct2(proto, constructTrap, handler) { | 207 function TestConstruct2(proto, constructTrap, handler) { |
166 var f = Proxy.createFunction(handler, function() {}, constructTrap) | 208 var f = Proxy.createFunction(handler, function() {}, constructTrap) |
167 var o = new f(11, 31) | 209 var o = new f(11, 31) |
168 // TODO(rossberg): doesn't hold, due to unrelated bug. | 210 assertEquals(undefined, receiver) |
169 // assertEquals(undefined, receiver) | |
170 assertEquals(42, o.sum) | 211 assertEquals(42, o.sum) |
171 assertSame(proto, Object.getPrototypeOf(o)) | 212 assertSame(proto, Object.getPrototypeOf(o)) |
172 | 213 |
173 var f = CreateFrozen(handler, function() {}, constructTrap) | 214 var f = CreateFrozen(handler, function() {}, constructTrap) |
174 var o = new f(11, 32) | 215 var o = new f(11, 32) |
175 // TODO(rossberg): doesn't hold, due to unrelated bug. | 216 assertEquals(undefined, receiver) |
176 // assertEquals(undefined, receiver) | |
177 assertEquals(43, o.sum) | 217 assertEquals(43, o.sum) |
178 assertSame(proto, Object.getPrototypeOf(o)) | 218 assertSame(proto, Object.getPrototypeOf(o)) |
179 } | 219 } |
180 | 220 |
181 TestConstruct(Object.prototype, ReturnNew) | 221 TestConstruct(Object.prototype, ReturnNew) |
182 TestConstruct(prototype, ReturnNewWithProto) | 222 TestConstruct(prototype, ReturnNewWithProto) |
183 | 223 |
184 TestConstruct(Object.prototype, Proxy.createFunction({}, ReturnNew)) | 224 TestConstruct(Object.prototype, Proxy.createFunction({}, ReturnNew)) |
185 TestConstruct(prototype, Proxy.createFunction({}, ReturnNewWithProto)) | 225 TestConstruct(prototype, Proxy.createFunction({}, ReturnNewWithProto)) |
186 | 226 |
(...skipping 186 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
373 | 413 |
374 TestAccessorCall( | 414 TestAccessorCall( |
375 Proxy.createFunction({}, function() { receiver = this; return 42 }), | 415 Proxy.createFunction({}, function() { receiver = this; return 42 }), |
376 Proxy.createFunction({}, function(x) { receiver = this; value = x }) | 416 Proxy.createFunction({}, function(x) { receiver = this; value = x }) |
377 ) | 417 ) |
378 | 418 |
379 TestAccessorCall( | 419 TestAccessorCall( |
380 CreateFrozen({}, function() { receiver = this; return 42 }), | 420 CreateFrozen({}, function() { receiver = this; return 42 }), |
381 CreateFrozen({}, function(x) { receiver = this; value = x }) | 421 CreateFrozen({}, function(x) { receiver = this; value = x }) |
382 ) | 422 ) |
OLD | NEW |