blob: 1aa13adea6cb8d354c7a0b8863d7e99db22e473c [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
28// Flags: --harmony-proxies
29
30
31// Helper.
32
33function TestWithProxies(test, x, y, z) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000034 test(function(h) { return new Proxy({}, h) }, x, y, z)
35 test(function(h) {
36 return new Proxy(function() {}, h)
37 }, x, y, z)
Ben Murdochb8a8cc12014-11-26 15:28:44 +000038}
39
40
41
42// Getting.
43
44function TestWithGet(handler) {
45 TestWithProxies(TestWithGet2, handler)
46}
47
48var c = "global"
49var key = ""
50
51function TestWithGet2(create, handler) {
52 var b = "local"
53
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000054 var p = create(handler);
55 assertEquals("onproxy", p.a);
56 assertEquals(undefined, p.b);
57 assertEquals(undefined, p.c);
58
Ben Murdochb8a8cc12014-11-26 15:28:44 +000059 with (p) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000060 assertEquals("onproxy", a);
61 assertEquals("local", b);
62 assertEquals("global", c);
Ben Murdochb8a8cc12014-11-26 15:28:44 +000063 }
64
65 var o = Object.create(p, {d: {value: "own"}})
66 with (o) {
67 assertEquals("onproxy", a)
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000068 assertEquals("local", b);
Ben Murdochb8a8cc12014-11-26 15:28:44 +000069 assertEquals("global", c)
70 assertEquals("own", d)
71 }
72}
73
74TestWithGet({
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000075 get(target, k) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +000076 key = k;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000077 return k === "a" ? "onproxy" : undefined
78 },
79 has(target, k) { return k === 'a' }
Ben Murdochb8a8cc12014-11-26 15:28:44 +000080})
81
82TestWithGet({
83 get: function(r, k) { return this.get2(r, k) },
84 get2: function(r, k) { key = k; return k === "a" ? "onproxy" : undefined },
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000085 has(target, k) { return k === 'a' }
Ben Murdochb8a8cc12014-11-26 15:28:44 +000086})
87
Ben Murdochb8a8cc12014-11-26 15:28:44 +000088
89
90
91// Invoking.
92
93function TestWithGetCall(handler) {
94 TestWithProxies(TestWithGetCall2, handler)
95}
96
97var receiver = null
98var c = function() { return "global" }
99
100function TestWithGetCall2(create, handler) {
101 var b = function() { return "local" }
102
103 var p = create(handler)
104 with (p) {
105 receiver = null
106 assertEquals("onproxy", a())
107 assertSame(p, receiver)
108 assertEquals("local", b())
109 assertEquals("global", c())
110 }
111
112 var o = Object.create(p, {d: {value: function() { return "own" }}})
113 with (o) {
114 receiver = null
115 assertEquals("onproxy", a())
116 assertSame(o, receiver)
117 assertEquals("local", b())
118 assertEquals("global", c())
119 assertEquals("own", d())
120 }
121}
122
123function onproxy() { receiver = this; return "onproxy" }
124
125TestWithGetCall({
126 get: function(r, k) { key = k; return k === "a" ? onproxy : undefined },
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000127 has: function(t, k) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000128 key = k;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000129 return k === "a";
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000130 }
131})
132
133TestWithGetCall({
134 get: function(r, k) { return this.get2(r, k) },
135 get2: function(r, k) { key = k; return k === "a" ? onproxy : undefined },
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000136 has: function(t, k) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000137 key = k;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000138 return k === "a";
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000139 }
140})
141
142TestWithGetCall({
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000143 get: function(r, k) { key = k; return k === "a" ? onproxy : undefined },
144 has: function(t, k) {
145 return this.has2(k)
146 },
147 has2: function(k) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000148 key = k;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000149 return k === "a";
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000150 }
151})
152
153TestWithGetCall({
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000154 get: function(r, k) { key = k; return k === "a" ? onproxy : undefined },
155 has: function(t, k) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000156 key = k;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000157 return k === "a";
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000158 }
159})
160
161
162function TestWithGetCallThrow(handler) {
163 TestWithProxies(TestWithGetCallThrow2, handler)
164}
165
166function TestWithGetCallThrow2(create, handler) {
167 var b = function() { return "local" }
168
169 var p = create(handler)
170 with (p) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000171 assertThrowsEquals(function(){ a() }, "myexn")
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000172 assertEquals("local", b())
173 assertEquals("global", c())
174 }
175
176 var o = Object.create(p, {d: {value: function() { return "own" }}})
177 with (o) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000178 assertThrowsEquals(function(){ a() }, "myexn")
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000179 assertEquals("local", b())
180 assertEquals("global", c())
181 assertEquals("own", d())
182 }
183}
184
185function onproxythrow() { throw "myexn" }
186
187TestWithGetCallThrow({
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000188 has: function(r, k) { return k === "a"; },
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000189 get: function(r, k) { key = k; return k === "a" ? onproxythrow : undefined },
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000190})
191
192TestWithGetCallThrow({
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000193 has: function(r, k) { return k === "a"; },
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000194 get: function(r, k) { return this.get2(r, k) },
195 get2: function(r, k) { key = k; return k === "a" ? onproxythrow : undefined },
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000196})
197
198
199
200// Setting.
201
202var key
203var val
204
205function TestWithSet(handler, hasSetter) {
206 TestWithProxies(TestWithSet2, handler, hasSetter)
207}
208
209var c = "global"
210
211function TestWithSet2(create, handler, hasSetter) {
212 var b = "local"
213
214 var p = create(handler)
215 key = val = undefined
216 with (p) {
217 a = "set"
218 assertEquals("a", key)
219 assertEquals("set", val)
220 assertEquals("local", b)
221 assertEquals("global", c)
222 b = "local"
223 c = "global"
224 assertEquals("a", key)
225 assertEquals("set", val)
226 }
227
228 if (!hasSetter) return
229
230 var o = Object.create(p, {d: {value: "own"}})
231 key = val = undefined
232 with (o) {
233 a = "set"
234 assertEquals("a", key)
235 assertEquals("set", val)
236 assertEquals("local", b)
237 assertEquals("global", c)
238 assertEquals("own", d)
239 b = "local"
240 c = "global"
241 d = "own"
242 assertEquals("a", key)
243 assertEquals("set", val)
244 }
245}
246
247TestWithSet({
248 set: function(r, k, v) { key = k; val = v; return true },
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000249 has: function(t, k) {
250 return k === "a"
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000251 }
252})
253
254TestWithSet({
255 set: function(r, k, v) { return this.set2(r, k, v) },
256 set2: function(r, k, v) { key = k; val = v; return true },
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000257 has: function(t, k) {
258 return k === "a"
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000259 }
260})
261
262TestWithSet({
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000263 has: function(t, k) {
264 return k === "a"
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000265 },
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000266 defineProperty: function(t, k, desc) { key = k; val = desc.value }
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000267})
268
269TestWithSet({
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000270 has: function(t, k) {
271 return this.has2(k)
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000272 },
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000273 has2: function(k) {
274 return k === "a"
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000275 },
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000276 defineProperty: function(t, k, desc) { this.defineProperty2(k, desc) },
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000277 defineProperty2: function(k, desc) { key = k; val = desc.value }
278})
279
280TestWithSet({
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000281 has: function(t, k) {
282 return k === "a"
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000283 },
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000284 defineProperty: function(t, k, desc) { key = k; val = desc.value }
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000285})
286
287TestWithSet({
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000288 has: function(t, k) {
289 return this.has2(k) },
290 has2: function(k) {
291 return k === "a"
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000292 },
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000293 set: function(t, k, v) { key = k; val = v; return true }
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000294}, true)
295
296TestWithSet({
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000297 has: function(t, k) {
298 return k === "a"
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000299 },
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000300 defineProperty: function(t, k, desc) { key = k; val = desc.value }
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000301})
302
303
304function TestWithSetThrow(handler, hasSetter) {
305 TestWithProxies(TestWithSetThrow2, handler, hasSetter)
306}
307
308function TestWithSetThrow2(create, handler, hasSetter) {
309 var p = create(handler)
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000310 assertThrowsEquals(function(){
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000311 with (p) {
312 a = 1
313 }
314 }, "myexn")
315
316 if (!hasSetter) return
317
318 var o = Object.create(p, {})
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000319 assertThrowsEquals(function(){
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000320 with (o) {
321 a = 1
322 }
323 }, "myexn")
324}
325
326TestWithSetThrow({
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000327 set: function() { throw "myexn" },
328 has: function(t, k) {
329 return k === "a"
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000330 }
331})
332
333TestWithSetThrow({
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000334 has: function() { throw "myexn" },
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000335})
336
337TestWithSetThrow({
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000338 has: function() { throw "myexn" },
339})
340
341TestWithSetThrow({
342 has: function(t, k) {
343 return k === "a"
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000344 },
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000345 defineProperty: function() { throw "myexn" }
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000346})
347
348TestWithSetThrow({
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000349 has: function(t, k) {
350 return k === "a"
351 },
352 set: function() { throw "myexn" }
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000353}, true)