whesse@chromium.org | ba5a61b | 2010-07-26 11:44:40 +0000 | [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 | |
| 28 | // Tests the Function.prototype.bind (ES 15.3.4.5) method. |
| 29 | |
| 30 | // Simple tests. |
| 31 | function foo(x, y, z) { |
| 32 | return x + y + z; |
| 33 | } |
| 34 | |
| 35 | var f = foo.bind(foo); |
| 36 | assertEquals(3, f(1, 1, 1)); |
| 37 | assertEquals(3, f.length); |
| 38 | |
| 39 | f = foo.bind(foo, 2); |
| 40 | assertEquals(4, f(1, 1)); |
| 41 | assertEquals(2, f.length); |
| 42 | |
| 43 | f = foo.bind(foo, 2, 2); |
| 44 | assertEquals(5, f(1)); |
| 45 | assertEquals(1, f.length); |
| 46 | |
| 47 | f = foo.bind(foo, 2, 2, 2); |
| 48 | assertEquals(6, f()); |
| 49 | assertEquals(0, f.length); |
| 50 | |
| 51 | // Test that length works correctly even if more than the actual number |
| 52 | // of arguments are given when binding. |
| 53 | f = foo.bind(foo, 1, 2, 3, 4, 5, 6, 7, 8, 9); |
| 54 | assertEquals(6, f()); |
| 55 | assertEquals(0, f.length); |
| 56 | |
| 57 | // Use a different bound object. |
| 58 | var obj = {x: 42, y: 43}; |
| 59 | // Values that would normally be in "this" when calling f_bound_this. |
| 60 | var x = 42; |
| 61 | var y = 44; |
| 62 | |
| 63 | function f_bound_this(z) { |
| 64 | return z + this.y - this.x; |
| 65 | } |
| 66 | |
| 67 | assertEquals(3, f_bound_this(1)) |
| 68 | f = f_bound_this.bind(obj); |
| 69 | assertEquals(2, f(1)); |
| 70 | assertEquals(1, f.length); |
| 71 | |
| 72 | f = f_bound_this.bind(obj, 2); |
| 73 | assertEquals(3, f()); |
| 74 | assertEquals(0, f.length); |
| 75 | |
| 76 | // Test chained binds. |
| 77 | |
| 78 | // When only giving the thisArg, any number of binds should have |
| 79 | // the same effect. |
| 80 | f = foo.bind(foo); |
| 81 | assertEquals(3, f(1, 1, 1)); |
| 82 | f = foo.bind(foo).bind(foo).bind(foo).bind(foo); |
| 83 | assertEquals(3, f(1, 1, 1)); |
| 84 | assertEquals(3, f.length); |
| 85 | |
| 86 | // Giving bound parameters should work at any place in the chain. |
| 87 | f = foo.bind(foo, 1).bind(foo).bind(foo).bind(foo); |
| 88 | assertEquals(3, f(1, 1)); |
| 89 | assertEquals(2, f.length); |
| 90 | |
| 91 | f = foo.bind(foo).bind(foo, 1).bind(foo).bind(foo); |
| 92 | assertEquals(3, f(1, 1)); |
| 93 | assertEquals(2, f.length); |
| 94 | |
| 95 | f = foo.bind(foo).bind(foo).bind(foo,1 ).bind(foo); |
| 96 | assertEquals(3, f(1, 1)); |
| 97 | assertEquals(2, f.length); |
| 98 | |
| 99 | f = foo.bind(foo).bind(foo).bind(foo).bind(foo, 1); |
| 100 | assertEquals(3, f(1, 1)); |
| 101 | assertEquals(2, f.length); |
| 102 | |
| 103 | // Several parameters can be given, and given in different bind invokations. |
| 104 | f = foo.bind(foo, 1, 1).bind(foo).bind(foo).bind(foo); |
| 105 | assertEquals(3, f(1)); |
| 106 | assertEquals(1, f.length); |
| 107 | |
| 108 | f = foo.bind(foo).bind(foo, 1, 1).bind(foo).bind(foo); |
| 109 | assertEquals(3, f(1)); |
| 110 | assertEquals(1, f.length); |
| 111 | |
| 112 | f = foo.bind(foo).bind(foo, 1, 1).bind(foo).bind(foo); |
| 113 | assertEquals(3, f(1)); |
| 114 | assertEquals(1, f.length); |
| 115 | |
| 116 | f = foo.bind(foo).bind(foo).bind(foo, 1, 1).bind(foo); |
| 117 | assertEquals(3, f(1)); |
| 118 | assertEquals(1, f.length); |
| 119 | |
| 120 | f = foo.bind(foo).bind(foo).bind(foo).bind(foo, 1, 1); |
| 121 | assertEquals(3, f(1)); |
| 122 | assertEquals(1, f.length); |
| 123 | |
| 124 | f = foo.bind(foo, 1).bind(foo, 1).bind(foo).bind(foo); |
| 125 | assertEquals(3, f(1)); |
| 126 | assertEquals(1, f.length); |
| 127 | |
| 128 | f = foo.bind(foo, 1).bind(foo).bind(foo, 1).bind(foo); |
| 129 | assertEquals(3, f(1)); |
| 130 | assertEquals(1, f.length); |
| 131 | |
| 132 | f = foo.bind(foo, 1).bind(foo).bind(foo).bind(foo, 1); |
| 133 | assertEquals(3, f(1)); |
| 134 | assertEquals(1, f.length); |
| 135 | |
| 136 | f = foo.bind(foo).bind(foo, 1).bind(foo).bind(foo, 1); |
| 137 | assertEquals(3, f(1)); |
| 138 | assertEquals(1, f.length); |
| 139 | |
| 140 | // Test constructor calls. |
| 141 | |
| 142 | function bar(x, y, z) { |
| 143 | this.x = x; |
| 144 | this.y = y; |
| 145 | this.z = z; |
| 146 | } |
| 147 | |
| 148 | f = bar.bind(bar); |
| 149 | var obj2 = new f(1,2,3); |
| 150 | assertEquals(1, obj2.x); |
| 151 | assertEquals(2, obj2.y); |
| 152 | assertEquals(3, obj2.z); |
| 153 | |
| 154 | f = bar.bind(bar, 1); |
| 155 | obj2 = new f(2,3); |
| 156 | assertEquals(1, obj2.x); |
| 157 | assertEquals(2, obj2.y); |
| 158 | assertEquals(3, obj2.z); |
| 159 | |
| 160 | f = bar.bind(bar, 1, 2); |
| 161 | obj2 = new f(3); |
| 162 | assertEquals(1, obj2.x); |
| 163 | assertEquals(2, obj2.y); |
| 164 | assertEquals(3, obj2.z); |
| 165 | |
| 166 | f = bar.bind(bar, 1, 2, 3); |
| 167 | obj2 = new f(); |
| 168 | assertEquals(1, obj2.x); |
| 169 | assertEquals(2, obj2.y); |
| 170 | assertEquals(3, obj2.z); |
| 171 | |
| 172 | |
| 173 | // Test bind chains when used as a constructor. |
| 174 | |
| 175 | f = bar.bind(bar, 1).bind(bar, 2).bind(bar, 3); |
| 176 | obj2 = new f(); |
| 177 | assertEquals(1, obj2.x); |
| 178 | assertEquals(2, obj2.y); |
| 179 | assertEquals(3, obj2.z); |
| 180 | |
| 181 | // Test instanceof obj2 is bar, not f. |
| 182 | assertTrue(obj2 instanceof bar); |
| 183 | assertFalse(obj2 instanceof f); |
| 184 | |