Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1 | // Copyright 2015 the V8 project authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 5 | // Tests the interaction of Function.prototype.bind with proxies. |
| 6 | |
| 7 | |
| 8 | // (Helper) |
| 9 | |
| 10 | var log = []; |
| 11 | var logger = {}; |
| 12 | var handler = new Proxy({}, logger); |
| 13 | |
| 14 | logger.get = function(t, trap, r) { |
| 15 | return function() { |
| 16 | log.push([trap, ...arguments]); |
| 17 | return Reflect[trap](...arguments); |
| 18 | } |
| 19 | }; |
| 20 | |
| 21 | |
| 22 | // Simple case |
| 23 | |
| 24 | var target = function(a, b, c) { "use strict"; return this }; |
| 25 | var proxy = new Proxy(target, handler); |
| 26 | var this_value = Symbol(); |
| 27 | |
| 28 | log.length = 0; |
| 29 | result = Function.prototype.bind.call(proxy, this_value, "foo"); |
| 30 | assertEquals(2, result.length); |
| 31 | assertEquals(target.__proto__, result.__proto__); |
| 32 | assertEquals(this_value, result()); |
| 33 | assertEquals(5, log.length); |
| 34 | for (var i in log) assertSame(target, log[i][1]); |
| 35 | assertEquals(["getPrototypeOf", target], log[0]); |
| 36 | assertEquals(["getOwnPropertyDescriptor", target, "length"], log[1]); |
| 37 | assertEquals(["get", target, "length", proxy], log[2]); |
| 38 | assertEquals(["get", target, "name", proxy], log[3]); |
| 39 | assertEquals(["apply", target, this_value, ["foo"]], log[4]); |
| 40 | assertEquals(new target(), new result()); |
| 41 | |
| 42 | |
| 43 | // Custom prototype |
| 44 | |
| 45 | log.length = 0; |
| 46 | target.__proto__ = {radio: "gaga"}; |
| 47 | result = Function.prototype.bind.call(proxy, this_value, "foo"); |
| 48 | assertEquals(2, result.length); |
| 49 | assertSame(target.__proto__, result.__proto__); |
| 50 | assertEquals(this_value, result()); |
| 51 | assertEquals(5, log.length); |
| 52 | for (var i in log) assertSame(target, log[i][1]); |
| 53 | assertEquals(["getPrototypeOf", target], log[0]); |
| 54 | assertEquals(["getOwnPropertyDescriptor", target, "length"], log[1]); |
| 55 | assertEquals(["get", target, "length", proxy], log[2]); |
| 56 | assertEquals(["get", target, "name", proxy], log[3]); |
| 57 | assertEquals(["apply", target, this_value, ["foo"]], log[4]); |
| 58 | |
| 59 | |
| 60 | // Custom length |
| 61 | |
| 62 | handler = { |
| 63 | get() {return 42}, |
| 64 | getOwnPropertyDescriptor() {return {configurable: true}} |
| 65 | }; |
| 66 | proxy = new Proxy(target, handler); |
| 67 | |
| 68 | result = Function.prototype.bind.call(proxy, this_value, "foo"); |
| 69 | assertEquals(41, result.length); |
| 70 | assertEquals(this_value, result()); |
| 71 | |
| 72 | |
| 73 | // Long length |
| 74 | |
| 75 | handler = { |
| 76 | get() {return Math.pow(2, 100)}, |
| 77 | getOwnPropertyDescriptor() {return {configurable: true}} |
| 78 | }; |
| 79 | proxy = new Proxy(target, handler); |
| 80 | |
| 81 | result = Function.prototype.bind.call(proxy, this_value, "foo"); |
| 82 | assertEquals(Math.pow(2, 100) - 1, result.length); |
| 83 | assertEquals(this_value, result()); |
| 84 | |
| 85 | |
| 86 | // Very long length |
| 87 | |
| 88 | handler = { |
| 89 | get() {return 1/0}, |
| 90 | getOwnPropertyDescriptor() {return {configurable: true}} |
| 91 | }; |
| 92 | proxy = new Proxy(target, handler); |
| 93 | |
| 94 | result = Function.prototype.bind.call(proxy, this_value, "foo"); |
| 95 | assertEquals(1/0, result.length); |
| 96 | assertEquals(this_value, result()); |
| 97 | |
| 98 | |
| 99 | // Non-integer length |
| 100 | |
| 101 | handler = { |
| 102 | get() {return 4.2}, |
| 103 | getOwnPropertyDescriptor() {return {configurable: true}} |
| 104 | }; |
| 105 | proxy = new Proxy(target, handler); |
| 106 | |
| 107 | result = Function.prototype.bind.call(proxy, this_value, "foo"); |
| 108 | assertEquals(3, result.length); |
| 109 | assertEquals(this_value, result()); |
| 110 | |
| 111 | |
| 112 | // Undefined length |
| 113 | |
| 114 | handler = { |
| 115 | get() {}, |
| 116 | getOwnPropertyDescriptor() {return {configurable: true}} |
| 117 | }; |
| 118 | proxy = new Proxy(target, handler); |
| 119 | |
| 120 | result = Function.prototype.bind.call(proxy, this_value, "foo"); |
| 121 | assertEquals(0, result.length); |
| 122 | assertEquals(this_value, result()); |
| 123 | |
| 124 | |
| 125 | // Non-callable |
| 126 | |
| 127 | assertThrows(() => Function.prototype.bind.call(new Proxy({}, {})), TypeError); |
| 128 | assertThrows(() => Function.prototype.bind.call(new Proxy([], {})), TypeError); |
| 129 | |
| 130 | |
| 131 | // Non-constructable |
| 132 | |
| 133 | result = Function.prototype.bind.call(() => 42, this_value, "foo"); |
| 134 | assertEquals(42, result()); |
| 135 | assertThrows(() => new result()); |