Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2010 the V8 project authors. All rights reserved. | 1 // Copyright 2010 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 |
| 11 // with the distribution. | 11 // with the distribution. |
| 12 // * Neither the name of Google Inc. nor the names of its | 12 // * Neither the name of Google Inc. nor the names of its |
| 13 // contributors may be used to endorse or promote products derived | 13 // contributors may be used to endorse or promote products derived |
| 14 // from this software without specific prior written permission. | 14 // from this software without specific prior written permission. |
| 15 // | 15 // |
| 16 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | 16 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 17 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | 17 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 18 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR | 18 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 19 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT | 19 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 20 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | 20 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 21 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | 21 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 22 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | 22 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 23 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | 23 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 24 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | 24 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 25 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | 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. | 26 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 27 | 27 |
| 28 // Flags: --allow-natives-syntax | 28 // Flags: --allow-natives-syntax --harmony-proxies --harmony-reflect |
| 29 | 29 |
| 30 // Tests the Function.prototype.bind (ES 15.3.4.5) method. | 30 // Tests the Function.prototype.bind method. |
| 31 | |
| 31 | 32 |
| 32 // Simple tests. | 33 // Simple tests. |
| 33 function foo(x, y, z) { | 34 function foo(x, y, z) { |
| 34 return [this, arguments.length, x]; | 35 return [this, arguments.length, x]; |
| 35 } | 36 } |
| 36 | 37 |
| 37 assertEquals(3, foo.length); | 38 assertEquals(3, foo.length); |
| 38 | 39 |
| 39 var f = foo.bind(foo); | 40 var f = foo.bind(foo); |
| 40 assertEquals([foo, 3, 1], f(1, 2, 3)); | 41 assertEquals([foo, 3, 1], f(1, 2, 3)); |
| (...skipping 269 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 310 var bound = fun.bind({}); | 311 var bound = fun.bind({}); |
| 311 assertEquals(proto, Object.getPrototypeOf(bound)); | 312 assertEquals(proto, Object.getPrototypeOf(bound)); |
| 312 | 313 |
| 313 var bound2 = fun.bind({}); | 314 var bound2 = fun.bind({}); |
| 314 assertTrue(%HaveSameMap(new bound, new bound2)); | 315 assertTrue(%HaveSameMap(new bound, new bound2)); |
| 315 | 316 |
| 316 Object.setPrototypeOf(fun, null); | 317 Object.setPrototypeOf(fun, null); |
| 317 bound = Function.prototype.bind.call(fun, {}); | 318 bound = Function.prototype.bind.call(fun, {}); |
| 318 assertEquals(null, Object.getPrototypeOf(bound)); | 319 assertEquals(null, Object.getPrototypeOf(bound)); |
| 319 })(); | 320 })(); |
| 321 | |
| 322 | |
| 323 | |
| 324 /////////////////////////////////////////////////////////////////////////////// | |
| 325 // Proxies | |
| 326 | |
| 327 | |
| 328 var log = []; | |
| 329 var logger = {}; | |
| 330 var handler = new Proxy({}, logger); | |
| 331 | |
| 332 logger.get = function(t, trap, r) { | |
| 333 return function() { | |
| 334 log.push([trap, ...arguments]); | |
| 335 return Reflect[trap](...arguments); | |
| 336 } | |
| 337 }; | |
| 338 | |
| 339 | |
| 340 // Simple case | |
| 341 | |
| 342 var target = function(a, b, c) { "use strict"; return this }; | |
| 343 var proxy = new Proxy(target, handler); | |
| 344 var this_value = Symbol(); | |
| 345 | |
| 346 log.length = 0; | |
| 347 result = Function.prototype.bind.call(proxy, this_value, "foo"); | |
| 348 assertEquals(2, result.length); | |
| 349 assertEquals(this_value, result()); | |
| 350 assertEquals(4, log.length); | |
| 351 for (var i in log) assertSame(target, log[i][1]); | |
| 352 assertEquals(["getOwnPropertyDescriptor", target, "length"], log[0]); | |
| 353 assertEquals(["get", target, "length", proxy], log[1]); | |
| 354 assertEquals(["get", target, "name", proxy], log[2]); | |
| 355 assertEquals(["apply", target, this_value, ["foo"]], log[3]); | |
|
Camillo Bruni
2015/12/16 13:19:06
Could you add another test where you check the log
neis
2015/12/16 14:00:24
As discussed, the getPrototypeOf should already ha
| |
| 356 | |
| 357 | |
| 358 // Custom length | |
| 359 | |
| 360 handler = { | |
| 361 get() {return 42}, | |
| 362 getOwnPropertyDescriptor() {return {configurable: true}} | |
| 363 }; | |
| 364 proxy = new Proxy(target, handler); | |
| 365 | |
| 366 result = Function.prototype.bind.call(proxy, this_value, "foo"); | |
| 367 assertEquals(41, result.length); | |
| 368 assertEquals(this_value, result()); | |
| 369 | |
| 370 | |
| 371 // Long length | |
| 372 | |
| 373 handler = { | |
| 374 get() {return Math.pow(2, 100)}, | |
| 375 getOwnPropertyDescriptor() {return {configurable: true}} | |
| 376 }; | |
| 377 proxy = new Proxy(target, handler); | |
| 378 | |
| 379 result = Function.prototype.bind.call(proxy, this_value, "foo"); | |
| 380 assertEquals(Math.pow(2, 100) - 1, result.length); | |
| 381 assertEquals(this_value, result()); | |
| 382 | |
| 383 | |
| 384 // Very long length | |
| 385 | |
| 386 handler = { | |
| 387 get() {return 1/0}, | |
| 388 getOwnPropertyDescriptor() {return {configurable: true}} | |
| 389 }; | |
| 390 proxy = new Proxy(target, handler); | |
| 391 | |
| 392 result = Function.prototype.bind.call(proxy, this_value, "foo"); | |
| 393 assertEquals(1/0, result.length); | |
| 394 assertEquals(this_value, result()); | |
| 395 | |
| 396 | |
| 397 // Non-integer length | |
| 398 | |
| 399 handler = { | |
| 400 get() {return 4.2}, | |
| 401 getOwnPropertyDescriptor() {return {configurable: true}} | |
| 402 }; | |
| 403 proxy = new Proxy(target, handler); | |
| 404 | |
| 405 result = Function.prototype.bind.call(proxy, this_value, "foo"); | |
| 406 assertEquals(3, result.length); | |
| 407 assertEquals(this_value, result()); | |
| 408 | |
| 409 | |
| 410 // Undefined length | |
| 411 | |
| 412 handler = { | |
| 413 get() {}, | |
| 414 getOwnPropertyDescriptor() {return {configurable: true}} | |
| 415 }; | |
| 416 proxy = new Proxy(target, handler); | |
| 417 | |
| 418 result = Function.prototype.bind.call(proxy, this_value, "foo"); | |
| 419 assertEquals(0, result.length); | |
| 420 assertEquals(this_value, result()); | |
| 421 | |
| 422 | |
| 423 // Non-callable | |
| 424 | |
| 425 assertThrows(() => Function.prototype.bind.call(new Proxy({}, {})), TypeError); | |
| 426 assertThrows(() => Function.prototype.bind.call(new Proxy([], {})), TypeError); | |
| OLD | NEW |