Kristian Monsen | 50ef84f | 2010-07-29 15:18:00 +0100 | [diff] [blame] | 1 | // Copyright 2010 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 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 28 | // Flags: --allow-natives-syntax |
| 29 | |
| 30 | // Tests the Function.prototype.bind method. |
| 31 | |
Kristian Monsen | 50ef84f | 2010-07-29 15:18:00 +0100 | [diff] [blame] | 32 | |
| 33 | // Simple tests. |
| 34 | function foo(x, y, z) { |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 35 | return [this, arguments.length, x]; |
Kristian Monsen | 50ef84f | 2010-07-29 15:18:00 +0100 | [diff] [blame] | 36 | } |
| 37 | |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 38 | assertEquals(3, foo.length); |
| 39 | |
Kristian Monsen | 50ef84f | 2010-07-29 15:18:00 +0100 | [diff] [blame] | 40 | var f = foo.bind(foo); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 41 | assertEquals([foo, 3, 1], f(1, 2, 3)); |
Kristian Monsen | 50ef84f | 2010-07-29 15:18:00 +0100 | [diff] [blame] | 42 | assertEquals(3, f.length); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 43 | assertEquals("function () { [native code] }", f.toString()); |
Kristian Monsen | 50ef84f | 2010-07-29 15:18:00 +0100 | [diff] [blame] | 44 | |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 45 | f = foo.bind(foo, 1); |
| 46 | assertEquals([foo, 3, 1], f(2, 3)); |
Kristian Monsen | 50ef84f | 2010-07-29 15:18:00 +0100 | [diff] [blame] | 47 | assertEquals(2, f.length); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 48 | assertEquals("function () { [native code] }", f.toString()); |
Kristian Monsen | 50ef84f | 2010-07-29 15:18:00 +0100 | [diff] [blame] | 49 | |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 50 | f = foo.bind(foo, 1, 2); |
| 51 | assertEquals([foo, 3, 1], f(3)); |
Kristian Monsen | 50ef84f | 2010-07-29 15:18:00 +0100 | [diff] [blame] | 52 | assertEquals(1, f.length); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 53 | assertEquals("function () { [native code] }", f.toString()); |
Kristian Monsen | 50ef84f | 2010-07-29 15:18:00 +0100 | [diff] [blame] | 54 | |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 55 | f = foo.bind(foo, 1, 2, 3); |
| 56 | assertEquals([foo, 3, 1], f()); |
Kristian Monsen | 50ef84f | 2010-07-29 15:18:00 +0100 | [diff] [blame] | 57 | assertEquals(0, f.length); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 58 | assertEquals("function () { [native code] }", f.toString()); |
Kristian Monsen | 50ef84f | 2010-07-29 15:18:00 +0100 | [diff] [blame] | 59 | |
| 60 | // Test that length works correctly even if more than the actual number |
| 61 | // of arguments are given when binding. |
| 62 | f = foo.bind(foo, 1, 2, 3, 4, 5, 6, 7, 8, 9); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 63 | assertEquals([foo, 9, 1], f()); |
Kristian Monsen | 50ef84f | 2010-07-29 15:18:00 +0100 | [diff] [blame] | 64 | assertEquals(0, f.length); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 65 | assertEquals("function () { [native code] }", f.toString()); |
Kristian Monsen | 50ef84f | 2010-07-29 15:18:00 +0100 | [diff] [blame] | 66 | |
| 67 | // Use a different bound object. |
| 68 | var obj = {x: 42, y: 43}; |
| 69 | // Values that would normally be in "this" when calling f_bound_this. |
| 70 | var x = 42; |
| 71 | var y = 44; |
| 72 | |
| 73 | function f_bound_this(z) { |
| 74 | return z + this.y - this.x; |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 75 | } |
Kristian Monsen | 50ef84f | 2010-07-29 15:18:00 +0100 | [diff] [blame] | 76 | |
| 77 | assertEquals(3, f_bound_this(1)) |
| 78 | f = f_bound_this.bind(obj); |
| 79 | assertEquals(2, f(1)); |
| 80 | assertEquals(1, f.length); |
| 81 | |
| 82 | f = f_bound_this.bind(obj, 2); |
| 83 | assertEquals(3, f()); |
| 84 | assertEquals(0, f.length); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 85 | assertEquals('[object Function]', Object.prototype.toString.call(f)); |
Kristian Monsen | 50ef84f | 2010-07-29 15:18:00 +0100 | [diff] [blame] | 86 | |
| 87 | // Test chained binds. |
| 88 | |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 89 | // When only giving the thisArg, any number of binds should have |
Kristian Monsen | 50ef84f | 2010-07-29 15:18:00 +0100 | [diff] [blame] | 90 | // the same effect. |
| 91 | f = foo.bind(foo); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 92 | assertEquals([foo, 3, 1], f(1, 2, 3)); |
| 93 | |
| 94 | var not_foo = {}; |
| 95 | f = foo.bind(foo).bind(not_foo).bind(not_foo).bind(not_foo); |
| 96 | assertEquals([foo, 3, 1], f(1, 2, 3)); |
Kristian Monsen | 50ef84f | 2010-07-29 15:18:00 +0100 | [diff] [blame] | 97 | assertEquals(3, f.length); |
| 98 | |
| 99 | // Giving bound parameters should work at any place in the chain. |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 100 | f = foo.bind(foo, 1).bind(not_foo).bind(not_foo).bind(not_foo); |
| 101 | assertEquals([foo, 3, 1], f(2, 3)); |
Kristian Monsen | 50ef84f | 2010-07-29 15:18:00 +0100 | [diff] [blame] | 102 | assertEquals(2, f.length); |
| 103 | |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 104 | f = foo.bind(foo).bind(not_foo, 1).bind(not_foo).bind(not_foo); |
| 105 | assertEquals([foo, 3, 1], f(2, 3)); |
Kristian Monsen | 50ef84f | 2010-07-29 15:18:00 +0100 | [diff] [blame] | 106 | assertEquals(2, f.length); |
| 107 | |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 108 | f = foo.bind(foo).bind(not_foo).bind(not_foo,1 ).bind(not_foo); |
| 109 | assertEquals([foo, 3, 1], f(2, 3)); |
Kristian Monsen | 50ef84f | 2010-07-29 15:18:00 +0100 | [diff] [blame] | 110 | assertEquals(2, f.length); |
| 111 | |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 112 | f = foo.bind(foo).bind(not_foo).bind(not_foo).bind(not_foo, 1); |
| 113 | assertEquals([foo, 3, 1], f(2, 3)); |
Kristian Monsen | 50ef84f | 2010-07-29 15:18:00 +0100 | [diff] [blame] | 114 | assertEquals(2, f.length); |
| 115 | |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 116 | // Several parameters can be given, and given in different bind invocations. |
| 117 | f = foo.bind(foo, 1, 2).bind(not_foo).bind(not_foo).bind(not_foo); |
| 118 | assertEquals([foo, 3, 1], f(3)); |
Kristian Monsen | 50ef84f | 2010-07-29 15:18:00 +0100 | [diff] [blame] | 119 | assertEquals(1, f.length); |
| 120 | |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 121 | f = foo.bind(foo).bind(not_foo, 1, 2).bind(not_foo).bind(not_foo); |
| 122 | assertEquals([foo, 3, 1], f(1)); |
Kristian Monsen | 50ef84f | 2010-07-29 15:18:00 +0100 | [diff] [blame] | 123 | assertEquals(1, f.length); |
| 124 | |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 125 | f = foo.bind(foo).bind(not_foo, 1, 2).bind(not_foo).bind(not_foo); |
| 126 | assertEquals([foo, 3, 1], f(3)); |
Kristian Monsen | 50ef84f | 2010-07-29 15:18:00 +0100 | [diff] [blame] | 127 | assertEquals(1, f.length); |
| 128 | |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 129 | f = foo.bind(foo).bind(not_foo).bind(not_foo, 1, 2).bind(not_foo); |
| 130 | assertEquals([foo, 3, 1], f(1)); |
Kristian Monsen | 50ef84f | 2010-07-29 15:18:00 +0100 | [diff] [blame] | 131 | assertEquals(1, f.length); |
| 132 | |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 133 | f = foo.bind(foo).bind(not_foo).bind(not_foo).bind(not_foo, 1, 2); |
| 134 | assertEquals([foo, 3, 1], f(3)); |
Kristian Monsen | 50ef84f | 2010-07-29 15:18:00 +0100 | [diff] [blame] | 135 | assertEquals(1, f.length); |
| 136 | |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 137 | f = foo.bind(foo, 1).bind(not_foo, 2).bind(not_foo).bind(not_foo); |
| 138 | assertEquals([foo, 3, 1], f(3)); |
Kristian Monsen | 50ef84f | 2010-07-29 15:18:00 +0100 | [diff] [blame] | 139 | assertEquals(1, f.length); |
| 140 | |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 141 | f = foo.bind(foo, 1).bind(not_foo).bind(not_foo, 2).bind(not_foo); |
| 142 | assertEquals([foo, 3, 1], f(3)); |
Kristian Monsen | 50ef84f | 2010-07-29 15:18:00 +0100 | [diff] [blame] | 143 | assertEquals(1, f.length); |
| 144 | |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 145 | f = foo.bind(foo, 1).bind(not_foo).bind(not_foo).bind(not_foo, 2); |
| 146 | assertEquals([foo, 3, 1], f(3)); |
Kristian Monsen | 50ef84f | 2010-07-29 15:18:00 +0100 | [diff] [blame] | 147 | assertEquals(1, f.length); |
| 148 | |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 149 | f = foo.bind(foo).bind(not_foo, 1).bind(not_foo).bind(not_foo, 2); |
| 150 | assertEquals([foo, 3, 1], f(3)); |
Kristian Monsen | 50ef84f | 2010-07-29 15:18:00 +0100 | [diff] [blame] | 151 | assertEquals(1, f.length); |
| 152 | |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 153 | // The wrong number of arguments can be given to bound functions too. |
| 154 | f = foo.bind(foo); |
| 155 | assertEquals(3, f.length); |
| 156 | assertEquals([foo, 0, undefined], f()); |
| 157 | assertEquals([foo, 1, 1], f(1)); |
| 158 | assertEquals([foo, 2, 1], f(1, 2)); |
| 159 | assertEquals([foo, 3, 1], f(1, 2, 3)); |
| 160 | assertEquals([foo, 4, 1], f(1, 2, 3, 4)); |
| 161 | |
| 162 | f = foo.bind(foo, 1); |
| 163 | assertEquals(2, f.length); |
| 164 | assertEquals([foo, 1, 1], f()); |
| 165 | assertEquals([foo, 2, 1], f(2)); |
| 166 | assertEquals([foo, 3, 1], f(2, 3)); |
| 167 | assertEquals([foo, 4, 1], f(2, 3, 4)); |
| 168 | |
| 169 | f = foo.bind(foo, 1, 2); |
| 170 | assertEquals(1, f.length); |
| 171 | assertEquals([foo, 2, 1], f()); |
| 172 | assertEquals([foo, 3, 1], f(3)); |
| 173 | assertEquals([foo, 4, 1], f(3, 4)); |
| 174 | |
| 175 | f = foo.bind(foo, 1, 2, 3); |
| 176 | assertEquals(0, f.length); |
| 177 | assertEquals([foo, 3, 1], f()); |
| 178 | assertEquals([foo, 4, 1], f(4)); |
| 179 | |
| 180 | f = foo.bind(foo, 1, 2, 3, 4); |
| 181 | assertEquals(0, f.length); |
| 182 | assertEquals([foo, 4, 1], f()); |
| 183 | |
Kristian Monsen | 50ef84f | 2010-07-29 15:18:00 +0100 | [diff] [blame] | 184 | // Test constructor calls. |
| 185 | |
| 186 | function bar(x, y, z) { |
| 187 | this.x = x; |
| 188 | this.y = y; |
| 189 | this.z = z; |
| 190 | } |
| 191 | |
| 192 | f = bar.bind(bar); |
| 193 | var obj2 = new f(1,2,3); |
| 194 | assertEquals(1, obj2.x); |
| 195 | assertEquals(2, obj2.y); |
| 196 | assertEquals(3, obj2.z); |
| 197 | |
| 198 | f = bar.bind(bar, 1); |
| 199 | obj2 = new f(2,3); |
| 200 | assertEquals(1, obj2.x); |
| 201 | assertEquals(2, obj2.y); |
| 202 | assertEquals(3, obj2.z); |
| 203 | |
| 204 | f = bar.bind(bar, 1, 2); |
| 205 | obj2 = new f(3); |
| 206 | assertEquals(1, obj2.x); |
| 207 | assertEquals(2, obj2.y); |
| 208 | assertEquals(3, obj2.z); |
| 209 | |
| 210 | f = bar.bind(bar, 1, 2, 3); |
| 211 | obj2 = new f(); |
| 212 | assertEquals(1, obj2.x); |
| 213 | assertEquals(2, obj2.y); |
| 214 | assertEquals(3, obj2.z); |
| 215 | |
| 216 | |
| 217 | // Test bind chains when used as a constructor. |
Kristian Monsen | 50ef84f | 2010-07-29 15:18:00 +0100 | [diff] [blame] | 218 | f = bar.bind(bar, 1).bind(bar, 2).bind(bar, 3); |
| 219 | obj2 = new f(); |
| 220 | assertEquals(1, obj2.x); |
| 221 | assertEquals(2, obj2.y); |
| 222 | assertEquals(3, obj2.z); |
| 223 | |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 224 | // Test obj2 is instanceof both bar and f. |
Kristian Monsen | 50ef84f | 2010-07-29 15:18:00 +0100 | [diff] [blame] | 225 | assertTrue(obj2 instanceof bar); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 226 | assertTrue(obj2 instanceof f); |
| 227 | |
| 228 | // This-args are not relevant to instanceof. |
| 229 | f = bar.bind(foo.prototype, 1). |
| 230 | bind(String.prototype, 2). |
| 231 | bind(Function.prototype, 3); |
| 232 | var obj3 = new f(); |
| 233 | assertTrue(obj3 instanceof bar); |
| 234 | assertTrue(obj3 instanceof f); |
| 235 | assertFalse(obj3 instanceof foo); |
| 236 | assertFalse(obj3 instanceof Function); |
| 237 | assertFalse(obj3 instanceof String); |
| 238 | |
| 239 | // thisArg is converted to object. |
| 240 | f = foo.bind(undefined); |
| 241 | assertEquals([this, 0, undefined], f()); |
| 242 | |
| 243 | f = foo.bind(null); |
| 244 | assertEquals([this, 0, undefined], f()); |
| 245 | |
| 246 | f = foo.bind(42); |
| 247 | assertEquals([Object(42), 0, undefined], f()); |
| 248 | |
| 249 | f = foo.bind("foo"); |
| 250 | assertEquals([Object("foo"), 0, undefined], f()); |
| 251 | |
| 252 | f = foo.bind(true); |
| 253 | assertEquals([Object(true), 0, undefined], f()); |
| 254 | |
| 255 | // Strict functions don't convert thisArg. |
| 256 | function soo(x, y, z) { |
| 257 | "use strict"; |
| 258 | return [this, arguments.length, x]; |
| 259 | } |
| 260 | |
| 261 | var s = soo.bind(undefined); |
| 262 | assertEquals([undefined, 0, undefined], s()); |
| 263 | |
| 264 | s = soo.bind(null); |
| 265 | assertEquals([null, 0, undefined], s()); |
| 266 | |
| 267 | s = soo.bind(42); |
| 268 | assertEquals([42, 0, undefined], s()); |
| 269 | |
| 270 | s = soo.bind("foo"); |
| 271 | assertEquals(["foo", 0, undefined], s()); |
| 272 | |
| 273 | s = soo.bind(true); |
| 274 | assertEquals([true, 0, undefined], s()); |
| 275 | |
| 276 | // Test that .arguments and .caller are poisoned according to the ES5 spec. |
| 277 | |
| 278 | // Check that property descriptors are correct (unconfigurable, unenumerable, |
| 279 | // and both get and set is the ThrowTypeError function). |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 280 | // |
| 281 | // Poisoned accessors are no longer own properties --- get them from the |
| 282 | // prototype |
| 283 | var f_proto = Object.getPrototypeOf(f); |
| 284 | var cdesc = Object.getOwnPropertyDescriptor(f_proto, "caller"); |
| 285 | var adesc = Object.getOwnPropertyDescriptor(f_proto, "arguments"); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 286 | |
| 287 | assertFalse(cdesc.enumerable); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 288 | assertTrue(cdesc.configurable); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 289 | |
| 290 | assertFalse(adesc.enumerable); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 291 | assertTrue(adesc.configurable); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 292 | |
| 293 | assertSame(cdesc.get, cdesc.set); |
| 294 | assertSame(cdesc.get, adesc.get); |
| 295 | assertSame(cdesc.get, adesc.set); |
| 296 | |
| 297 | assertTrue(cdesc.get instanceof Function); |
| 298 | assertEquals(0, cdesc.get.length); |
| 299 | assertThrows(cdesc.get, TypeError); |
| 300 | |
| 301 | assertThrows(function() { return f.caller; }, TypeError); |
| 302 | assertThrows(function() { f.caller = 42; }, TypeError); |
| 303 | assertThrows(function() { return f.arguments; }, TypeError); |
| 304 | assertThrows(function() { f.arguments = 42; }, TypeError); |
| 305 | |
| 306 | // Shouldn't throw. Accessing the functions caller must throw if |
| 307 | // the caller is strict and the callee isn't. A bound function is built-in, |
| 308 | // but not considered strict. |
| 309 | (function foo() { return foo.caller; }).bind()(); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 310 | |
| 311 | |
| 312 | (function TestProtoIsPreserved() { |
| 313 | function fun() {} |
| 314 | |
| 315 | function proto() {} |
| 316 | Object.setPrototypeOf(fun, proto); |
| 317 | var bound = fun.bind({}); |
| 318 | assertEquals(proto, Object.getPrototypeOf(bound)); |
| 319 | |
| 320 | var bound2 = fun.bind({}); |
| 321 | assertTrue(%HaveSameMap(new bound, new bound2)); |
| 322 | |
| 323 | Object.setPrototypeOf(fun, null); |
| 324 | bound = Function.prototype.bind.call(fun, {}); |
| 325 | assertEquals(null, Object.getPrototypeOf(bound)); |
| 326 | })(); |