Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1 | // 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 | |
| 33 | function TestWithProxies(test, x, y, z) { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 34 | 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 Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 38 | } |
| 39 | |
| 40 | |
| 41 | |
| 42 | // Getting. |
| 43 | |
| 44 | function TestWithGet(handler) { |
| 45 | TestWithProxies(TestWithGet2, handler) |
| 46 | } |
| 47 | |
| 48 | var c = "global" |
| 49 | var key = "" |
| 50 | |
| 51 | function TestWithGet2(create, handler) { |
| 52 | var b = "local" |
| 53 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 54 | var p = create(handler); |
| 55 | assertEquals("onproxy", p.a); |
| 56 | assertEquals(undefined, p.b); |
| 57 | assertEquals(undefined, p.c); |
| 58 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 59 | with (p) { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 60 | assertEquals("onproxy", a); |
| 61 | assertEquals("local", b); |
| 62 | assertEquals("global", c); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 63 | } |
| 64 | |
| 65 | var o = Object.create(p, {d: {value: "own"}}) |
| 66 | with (o) { |
| 67 | assertEquals("onproxy", a) |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 68 | assertEquals("local", b); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 69 | assertEquals("global", c) |
| 70 | assertEquals("own", d) |
| 71 | } |
| 72 | } |
| 73 | |
| 74 | TestWithGet({ |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 75 | get(target, k) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 76 | key = k; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 77 | return k === "a" ? "onproxy" : undefined |
| 78 | }, |
| 79 | has(target, k) { return k === 'a' } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 80 | }) |
| 81 | |
| 82 | TestWithGet({ |
| 83 | get: function(r, k) { return this.get2(r, k) }, |
| 84 | get2: function(r, k) { key = k; return k === "a" ? "onproxy" : undefined }, |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 85 | has(target, k) { return k === 'a' } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 86 | }) |
| 87 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 88 | |
| 89 | |
| 90 | |
| 91 | // Invoking. |
| 92 | |
| 93 | function TestWithGetCall(handler) { |
| 94 | TestWithProxies(TestWithGetCall2, handler) |
| 95 | } |
| 96 | |
| 97 | var receiver = null |
| 98 | var c = function() { return "global" } |
| 99 | |
| 100 | function 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 | |
| 123 | function onproxy() { receiver = this; return "onproxy" } |
| 124 | |
| 125 | TestWithGetCall({ |
| 126 | get: function(r, k) { key = k; return k === "a" ? onproxy : undefined }, |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 127 | has: function(t, k) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 128 | key = k; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 129 | return k === "a"; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 130 | } |
| 131 | }) |
| 132 | |
| 133 | TestWithGetCall({ |
| 134 | get: function(r, k) { return this.get2(r, k) }, |
| 135 | get2: function(r, k) { key = k; return k === "a" ? onproxy : undefined }, |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 136 | has: function(t, k) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 137 | key = k; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 138 | return k === "a"; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 139 | } |
| 140 | }) |
| 141 | |
| 142 | TestWithGetCall({ |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 143 | 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 Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 148 | key = k; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 149 | return k === "a"; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 150 | } |
| 151 | }) |
| 152 | |
| 153 | TestWithGetCall({ |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 154 | get: function(r, k) { key = k; return k === "a" ? onproxy : undefined }, |
| 155 | has: function(t, k) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 156 | key = k; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 157 | return k === "a"; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 158 | } |
| 159 | }) |
| 160 | |
| 161 | |
| 162 | function TestWithGetCallThrow(handler) { |
| 163 | TestWithProxies(TestWithGetCallThrow2, handler) |
| 164 | } |
| 165 | |
| 166 | function TestWithGetCallThrow2(create, handler) { |
| 167 | var b = function() { return "local" } |
| 168 | |
| 169 | var p = create(handler) |
| 170 | with (p) { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 171 | assertThrowsEquals(function(){ a() }, "myexn") |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 172 | assertEquals("local", b()) |
| 173 | assertEquals("global", c()) |
| 174 | } |
| 175 | |
| 176 | var o = Object.create(p, {d: {value: function() { return "own" }}}) |
| 177 | with (o) { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 178 | assertThrowsEquals(function(){ a() }, "myexn") |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 179 | assertEquals("local", b()) |
| 180 | assertEquals("global", c()) |
| 181 | assertEquals("own", d()) |
| 182 | } |
| 183 | } |
| 184 | |
| 185 | function onproxythrow() { throw "myexn" } |
| 186 | |
| 187 | TestWithGetCallThrow({ |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 188 | has: function(r, k) { return k === "a"; }, |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 189 | get: function(r, k) { key = k; return k === "a" ? onproxythrow : undefined }, |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 190 | }) |
| 191 | |
| 192 | TestWithGetCallThrow({ |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 193 | has: function(r, k) { return k === "a"; }, |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 194 | get: function(r, k) { return this.get2(r, k) }, |
| 195 | get2: function(r, k) { key = k; return k === "a" ? onproxythrow : undefined }, |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 196 | }) |
| 197 | |
| 198 | |
| 199 | |
| 200 | // Setting. |
| 201 | |
| 202 | var key |
| 203 | var val |
| 204 | |
| 205 | function TestWithSet(handler, hasSetter) { |
| 206 | TestWithProxies(TestWithSet2, handler, hasSetter) |
| 207 | } |
| 208 | |
| 209 | var c = "global" |
| 210 | |
| 211 | function 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 | |
| 247 | TestWithSet({ |
| 248 | set: function(r, k, v) { key = k; val = v; return true }, |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 249 | has: function(t, k) { |
| 250 | return k === "a" |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 251 | } |
| 252 | }) |
| 253 | |
| 254 | TestWithSet({ |
| 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 Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 257 | has: function(t, k) { |
| 258 | return k === "a" |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 259 | } |
| 260 | }) |
| 261 | |
| 262 | TestWithSet({ |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 263 | has: function(t, k) { |
| 264 | return k === "a" |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 265 | }, |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 266 | defineProperty: function(t, k, desc) { key = k; val = desc.value } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 267 | }) |
| 268 | |
| 269 | TestWithSet({ |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 270 | has: function(t, k) { |
| 271 | return this.has2(k) |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 272 | }, |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 273 | has2: function(k) { |
| 274 | return k === "a" |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 275 | }, |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 276 | defineProperty: function(t, k, desc) { this.defineProperty2(k, desc) }, |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 277 | defineProperty2: function(k, desc) { key = k; val = desc.value } |
| 278 | }) |
| 279 | |
| 280 | TestWithSet({ |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 281 | has: function(t, k) { |
| 282 | return k === "a" |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 283 | }, |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 284 | defineProperty: function(t, k, desc) { key = k; val = desc.value } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 285 | }) |
| 286 | |
| 287 | TestWithSet({ |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 288 | has: function(t, k) { |
| 289 | return this.has2(k) }, |
| 290 | has2: function(k) { |
| 291 | return k === "a" |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 292 | }, |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 293 | set: function(t, k, v) { key = k; val = v; return true } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 294 | }, true) |
| 295 | |
| 296 | TestWithSet({ |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 297 | has: function(t, k) { |
| 298 | return k === "a" |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 299 | }, |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 300 | defineProperty: function(t, k, desc) { key = k; val = desc.value } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 301 | }) |
| 302 | |
| 303 | |
| 304 | function TestWithSetThrow(handler, hasSetter) { |
| 305 | TestWithProxies(TestWithSetThrow2, handler, hasSetter) |
| 306 | } |
| 307 | |
| 308 | function TestWithSetThrow2(create, handler, hasSetter) { |
| 309 | var p = create(handler) |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 310 | assertThrowsEquals(function(){ |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 311 | with (p) { |
| 312 | a = 1 |
| 313 | } |
| 314 | }, "myexn") |
| 315 | |
| 316 | if (!hasSetter) return |
| 317 | |
| 318 | var o = Object.create(p, {}) |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 319 | assertThrowsEquals(function(){ |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 320 | with (o) { |
| 321 | a = 1 |
| 322 | } |
| 323 | }, "myexn") |
| 324 | } |
| 325 | |
| 326 | TestWithSetThrow({ |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 327 | set: function() { throw "myexn" }, |
| 328 | has: function(t, k) { |
| 329 | return k === "a" |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 330 | } |
| 331 | }) |
| 332 | |
| 333 | TestWithSetThrow({ |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 334 | has: function() { throw "myexn" }, |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 335 | }) |
| 336 | |
| 337 | TestWithSetThrow({ |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 338 | has: function() { throw "myexn" }, |
| 339 | }) |
| 340 | |
| 341 | TestWithSetThrow({ |
| 342 | has: function(t, k) { |
| 343 | return k === "a" |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 344 | }, |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 345 | defineProperty: function() { throw "myexn" } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 346 | }) |
| 347 | |
| 348 | TestWithSetThrow({ |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 349 | has: function(t, k) { |
| 350 | return k === "a" |
| 351 | }, |
| 352 | set: function() { throw "myexn" } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 353 | }, true) |