blob: 710e8b5e2dcef68f63f34faef1efeb4566c2d35b [file] [log] [blame]
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001// Copyright 2011 the V8 project authors. All rights reserved.
2// Redistribution and use in source and binary forms, with or without
3// modification, are permitted provided that the following conditions are
4// met:
5//
6// * Redistributions of source code must retain the above copyright
7// notice, this list of conditions and the following disclaimer.
8// * Redistributions in binary form must reproduce the above
9// copyright notice, this list of conditions and the following
10// disclaimer in the documentation and/or other materials provided
11// with the distribution.
12// * Neither the name of Google Inc. nor the names of its
13// contributors may be used to endorse or promote products derived
14// from this software without specific prior written permission.
15//
16// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
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.
27
Ben Murdochb8a8cc12014-11-26 15:28:44 +000028
29// Helper.
30
31function TestWithProxies(test, x, y, z) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000032 test(function(h) { return new Proxy({}, h) }, x, y, z)
33 test(function(h) {
34 return new Proxy(function() {}, h)
35 }, x, y, z)
Ben Murdochb8a8cc12014-11-26 15:28:44 +000036}
37
38
39
40// Getting.
41
42function TestWithGet(handler) {
43 TestWithProxies(TestWithGet2, handler)
44}
45
46var c = "global"
47var key = ""
48
49function TestWithGet2(create, handler) {
50 var b = "local"
51
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000052 var p = create(handler);
53 assertEquals("onproxy", p.a);
54 assertEquals(undefined, p.b);
55 assertEquals(undefined, p.c);
56
Ben Murdochb8a8cc12014-11-26 15:28:44 +000057 with (p) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000058 assertEquals("onproxy", a);
59 assertEquals("local", b);
60 assertEquals("global", c);
Ben Murdochb8a8cc12014-11-26 15:28:44 +000061 }
62
63 var o = Object.create(p, {d: {value: "own"}})
64 with (o) {
65 assertEquals("onproxy", a)
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000066 assertEquals("local", b);
Ben Murdochb8a8cc12014-11-26 15:28:44 +000067 assertEquals("global", c)
68 assertEquals("own", d)
69 }
70}
71
72TestWithGet({
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000073 get(target, k) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +000074 key = k;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000075 return k === "a" ? "onproxy" : undefined
76 },
77 has(target, k) { return k === 'a' }
Ben Murdochb8a8cc12014-11-26 15:28:44 +000078})
79
80TestWithGet({
81 get: function(r, k) { return this.get2(r, k) },
82 get2: function(r, k) { key = k; return k === "a" ? "onproxy" : undefined },
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000083 has(target, k) { return k === 'a' }
Ben Murdochb8a8cc12014-11-26 15:28:44 +000084})
85
Ben Murdochb8a8cc12014-11-26 15:28:44 +000086
87
88
89// Invoking.
90
91function TestWithGetCall(handler) {
92 TestWithProxies(TestWithGetCall2, handler)
93}
94
95var receiver = null
96var c = function() { return "global" }
97
98function TestWithGetCall2(create, handler) {
99 var b = function() { return "local" }
100
101 var p = create(handler)
102 with (p) {
103 receiver = null
104 assertEquals("onproxy", a())
105 assertSame(p, receiver)
106 assertEquals("local", b())
107 assertEquals("global", c())
108 }
109
110 var o = Object.create(p, {d: {value: function() { return "own" }}})
111 with (o) {
112 receiver = null
113 assertEquals("onproxy", a())
114 assertSame(o, receiver)
115 assertEquals("local", b())
116 assertEquals("global", c())
117 assertEquals("own", d())
118 }
119}
120
121function onproxy() { receiver = this; return "onproxy" }
122
123TestWithGetCall({
124 get: function(r, k) { key = k; return k === "a" ? onproxy : undefined },
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000125 has: function(t, k) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000126 key = k;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000127 return k === "a";
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000128 }
129})
130
131TestWithGetCall({
132 get: function(r, k) { return this.get2(r, k) },
133 get2: function(r, k) { key = k; return k === "a" ? onproxy : undefined },
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000134 has: function(t, k) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000135 key = k;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000136 return k === "a";
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000137 }
138})
139
140TestWithGetCall({
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000141 get: function(r, k) { key = k; return k === "a" ? onproxy : undefined },
142 has: function(t, k) {
143 return this.has2(k)
144 },
145 has2: function(k) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000146 key = k;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000147 return k === "a";
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000148 }
149})
150
151TestWithGetCall({
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000152 get: function(r, k) { key = k; return k === "a" ? onproxy : undefined },
153 has: function(t, k) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000154 key = k;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000155 return k === "a";
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000156 }
157})
158
159
160function TestWithGetCallThrow(handler) {
161 TestWithProxies(TestWithGetCallThrow2, handler)
162}
163
164function TestWithGetCallThrow2(create, handler) {
165 var b = function() { return "local" }
166
167 var p = create(handler)
168 with (p) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000169 assertThrowsEquals(function(){ a() }, "myexn")
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000170 assertEquals("local", b())
171 assertEquals("global", c())
172 }
173
174 var o = Object.create(p, {d: {value: function() { return "own" }}})
175 with (o) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000176 assertThrowsEquals(function(){ a() }, "myexn")
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000177 assertEquals("local", b())
178 assertEquals("global", c())
179 assertEquals("own", d())
180 }
181}
182
183function onproxythrow() { throw "myexn" }
184
185TestWithGetCallThrow({
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000186 has: function(r, k) { return k === "a"; },
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000187 get: function(r, k) { key = k; return k === "a" ? onproxythrow : undefined },
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000188})
189
190TestWithGetCallThrow({
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000191 has: function(r, k) { return k === "a"; },
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000192 get: function(r, k) { return this.get2(r, k) },
193 get2: function(r, k) { key = k; return k === "a" ? onproxythrow : undefined },
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000194})
195
196
197
198// Setting.
199
200var key
201var val
202
203function TestWithSet(handler, hasSetter) {
204 TestWithProxies(TestWithSet2, handler, hasSetter)
205}
206
207var c = "global"
208
209function TestWithSet2(create, handler, hasSetter) {
210 var b = "local"
211
212 var p = create(handler)
213 key = val = undefined
214 with (p) {
215 a = "set"
216 assertEquals("a", key)
217 assertEquals("set", val)
218 assertEquals("local", b)
219 assertEquals("global", c)
220 b = "local"
221 c = "global"
222 assertEquals("a", key)
223 assertEquals("set", val)
224 }
225
226 if (!hasSetter) return
227
228 var o = Object.create(p, {d: {value: "own"}})
229 key = val = undefined
230 with (o) {
231 a = "set"
232 assertEquals("a", key)
233 assertEquals("set", val)
234 assertEquals("local", b)
235 assertEquals("global", c)
236 assertEquals("own", d)
237 b = "local"
238 c = "global"
239 d = "own"
240 assertEquals("a", key)
241 assertEquals("set", val)
242 }
243}
244
245TestWithSet({
246 set: function(r, k, v) { key = k; val = v; return true },
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000247 has: function(t, k) {
248 return k === "a"
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000249 }
250})
251
252TestWithSet({
253 set: function(r, k, v) { return this.set2(r, k, v) },
254 set2: function(r, k, v) { key = k; val = v; return true },
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000255 has: function(t, k) {
256 return k === "a"
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000257 }
258})
259
260TestWithSet({
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000261 has: function(t, k) {
262 return k === "a"
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000263 },
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000264 defineProperty: function(t, k, desc) { key = k; val = desc.value }
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000265})
266
267TestWithSet({
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000268 has: function(t, k) {
269 return this.has2(k)
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000270 },
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000271 has2: function(k) {
272 return k === "a"
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000273 },
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000274 defineProperty: function(t, k, desc) { this.defineProperty2(k, desc) },
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000275 defineProperty2: function(k, desc) { key = k; val = desc.value }
276})
277
278TestWithSet({
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000279 has: function(t, k) {
280 return k === "a"
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000281 },
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000282 defineProperty: function(t, k, desc) { key = k; val = desc.value }
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000283})
284
285TestWithSet({
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000286 has: function(t, k) {
287 return this.has2(k) },
288 has2: function(k) {
289 return k === "a"
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000290 },
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000291 set: function(t, k, v) { key = k; val = v; return true }
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000292}, true)
293
294TestWithSet({
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000295 has: function(t, k) {
296 return k === "a"
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000297 },
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000298 defineProperty: function(t, k, desc) { key = k; val = desc.value }
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000299})
300
301
302function TestWithSetThrow(handler, hasSetter) {
303 TestWithProxies(TestWithSetThrow2, handler, hasSetter)
304}
305
306function TestWithSetThrow2(create, handler, hasSetter) {
307 var p = create(handler)
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000308 assertThrowsEquals(function(){
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000309 with (p) {
310 a = 1
311 }
312 }, "myexn")
313
314 if (!hasSetter) return
315
316 var o = Object.create(p, {})
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000317 assertThrowsEquals(function(){
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000318 with (o) {
319 a = 1
320 }
321 }, "myexn")
322}
323
324TestWithSetThrow({
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000325 set: function() { throw "myexn" },
326 has: function(t, k) {
327 return k === "a"
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000328 }
329})
330
331TestWithSetThrow({
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000332 has: function() { throw "myexn" },
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000333})
334
335TestWithSetThrow({
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000336 has: function() { throw "myexn" },
337})
338
339TestWithSetThrow({
340 has: function(t, k) {
341 return k === "a"
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000342 },
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000343 defineProperty: function() { throw "myexn" }
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000344})
345
346TestWithSetThrow({
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000347 has: function(t, k) {
348 return k === "a"
349 },
350 set: function() { throw "myexn" }
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000351}, true)