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) { 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) 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 54 var p = create(handler); 55 assertEquals("onproxy", p.a); 56 assertEquals(undefined, p.b); 57 assertEquals(undefined, p.c); 58 59 with (p) { 60 assertEquals("onproxy", a); 61 assertEquals("local", b); 62 assertEquals("global", c); 63 } 64 65 var o = Object.create(p, {d: {value: "own"}}) 66 with (o) { 67 assertEquals("onproxy", a) 68 assertEquals("local", b); 69 assertEquals("global", c) 70 assertEquals("own", d) 71 } 72 } 73 74 TestWithGet({ 75 get(target, k) { 76 key = k; 77 return k === "a" ? "onproxy" : undefined 78 }, 79 has(target, k) { return k === 'a' } 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 }, 85 has(target, k) { return k === 'a' } 86 }) 87 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 }, 127 has: function(t, k) { 128 key = k; 129 return k === "a"; 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 }, 136 has: function(t, k) { 137 key = k; 138 return k === "a"; 139 } 140 }) 141 142 TestWithGetCall({ 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) { 148 key = k; 149 return k === "a"; 150 } 151 }) 152 153 TestWithGetCall({ 154 get: function(r, k) { key = k; return k === "a" ? onproxy : undefined }, 155 has: function(t, k) { 156 key = k; 157 return k === "a"; 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) { 171 assertThrowsEquals(function(){ a() }, "myexn") 172 assertEquals("local", b()) 173 assertEquals("global", c()) 174 } 175 176 var o = Object.create(p, {d: {value: function() { return "own" }}}) 177 with (o) { 178 assertThrowsEquals(function(){ a() }, "myexn") 179 assertEquals("local", b()) 180 assertEquals("global", c()) 181 assertEquals("own", d()) 182 } 183 } 184 185 function onproxythrow() { throw "myexn" } 186 187 TestWithGetCallThrow({ 188 has: function(r, k) { return k === "a"; }, 189 get: function(r, k) { key = k; return k === "a" ? onproxythrow : undefined }, 190 }) 191 192 TestWithGetCallThrow({ 193 has: function(r, k) { return k === "a"; }, 194 get: function(r, k) { return this.get2(r, k) }, 195 get2: function(r, k) { key = k; return k === "a" ? onproxythrow : undefined }, 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 }, 249 has: function(t, k) { 250 return k === "a" 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 }, 257 has: function(t, k) { 258 return k === "a" 259 } 260 }) 261 262 TestWithSet({ 263 has: function(t, k) { 264 return k === "a" 265 }, 266 defineProperty: function(t, k, desc) { key = k; val = desc.value } 267 }) 268 269 TestWithSet({ 270 has: function(t, k) { 271 return this.has2(k) 272 }, 273 has2: function(k) { 274 return k === "a" 275 }, 276 defineProperty: function(t, k, desc) { this.defineProperty2(k, desc) }, 277 defineProperty2: function(k, desc) { key = k; val = desc.value } 278 }) 279 280 TestWithSet({ 281 has: function(t, k) { 282 return k === "a" 283 }, 284 defineProperty: function(t, k, desc) { key = k; val = desc.value } 285 }) 286 287 TestWithSet({ 288 has: function(t, k) { 289 return this.has2(k) }, 290 has2: function(k) { 291 return k === "a" 292 }, 293 set: function(t, k, v) { key = k; val = v; return true } 294 }, true) 295 296 TestWithSet({ 297 has: function(t, k) { 298 return k === "a" 299 }, 300 defineProperty: function(t, k, desc) { key = k; val = desc.value } 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) 310 assertThrowsEquals(function(){ 311 with (p) { 312 a = 1 313 } 314 }, "myexn") 315 316 if (!hasSetter) return 317 318 var o = Object.create(p, {}) 319 assertThrowsEquals(function(){ 320 with (o) { 321 a = 1 322 } 323 }, "myexn") 324 } 325 326 TestWithSetThrow({ 327 set: function() { throw "myexn" }, 328 has: function(t, k) { 329 return k === "a" 330 } 331 }) 332 333 TestWithSetThrow({ 334 has: function() { throw "myexn" }, 335 }) 336 337 TestWithSetThrow({ 338 has: function() { throw "myexn" }, 339 }) 340 341 TestWithSetThrow({ 342 has: function(t, k) { 343 return k === "a" 344 }, 345 defineProperty: function() { throw "myexn" } 346 }) 347 348 TestWithSetThrow({ 349 has: function(t, k) { 350 return k === "a" 351 }, 352 set: function() { throw "myexn" } 353 }, true) 354