| OLD | NEW |
| (Empty) |
| 1 description('Tests to ensure that Function.apply works correctly for Arrays, arg
uments and array-like objects.'); | |
| 2 | |
| 3 function argumentsApply1(a, b, c) | |
| 4 { | |
| 5 function t(a, b, c) | |
| 6 { | |
| 7 return a; | |
| 8 } | |
| 9 return t.apply(null, arguments); | |
| 10 } | |
| 11 | |
| 12 function argumentsApply2(a, b, c) | |
| 13 { | |
| 14 function t(a, b, c) | |
| 15 { | |
| 16 return b; | |
| 17 } | |
| 18 return t.apply(null, arguments); | |
| 19 } | |
| 20 | |
| 21 function argumentsApply3(a, b, c) | |
| 22 { | |
| 23 function t(a, b, c) | |
| 24 { | |
| 25 return c; | |
| 26 } | |
| 27 return t.apply(null, arguments); | |
| 28 } | |
| 29 | |
| 30 function argumentsApplyLength(a, b, c) | |
| 31 { | |
| 32 function t(a, b, c) | |
| 33 { | |
| 34 return arguments.length; | |
| 35 } | |
| 36 return t.apply(null, arguments); | |
| 37 } | |
| 38 var executedAdditionalArgument = false; | |
| 39 function argumentsApplyExcessArguments(a, b, c) | |
| 40 { | |
| 41 function t(a, b, c) | |
| 42 { | |
| 43 return arguments.length; | |
| 44 } | |
| 45 return t.apply(null, arguments, executedAdditionalArgument = true); | |
| 46 } | |
| 47 | |
| 48 shouldBe("argumentsApply1(1, 2, 3)", "1"); | |
| 49 shouldBe("argumentsApply2(1, 2, 3)", "2"); | |
| 50 shouldBe("argumentsApply3(1, 2, 3)", "3"); | |
| 51 shouldBe("argumentsApplyLength(1, 2, 3)", "3"); | |
| 52 shouldBe("argumentsApplyExcessArguments(1, 2, 3)", "3"); | |
| 53 shouldBeTrue("executedAdditionalArgument"); | |
| 54 | |
| 55 function arrayApply1(array) | |
| 56 { | |
| 57 function t(a, b, c) | |
| 58 { | |
| 59 return a; | |
| 60 } | |
| 61 return t.apply(null, array); | |
| 62 } | |
| 63 | |
| 64 function arrayApply2(array) | |
| 65 { | |
| 66 function t(a, b, c) | |
| 67 { | |
| 68 return b; | |
| 69 } | |
| 70 return t.apply(null, array); | |
| 71 } | |
| 72 | |
| 73 function arrayApply3(array) | |
| 74 { | |
| 75 function t(a, b, c) | |
| 76 { | |
| 77 return c; | |
| 78 } | |
| 79 return t.apply(null, array); | |
| 80 } | |
| 81 | |
| 82 function arrayApplyLength(array) | |
| 83 { | |
| 84 function t(a, b, c) | |
| 85 { | |
| 86 return arguments.length; | |
| 87 } | |
| 88 return t.apply(null, array); | |
| 89 } | |
| 90 | |
| 91 shouldBe("arrayApply1([1, 2, 3])", "1"); | |
| 92 shouldBe("arrayApply2([1, 2, 3])", "2"); | |
| 93 shouldBe("arrayApply3([1, 2, 3])", "3"); | |
| 94 shouldBe("arrayApplyLength([1, 2, 3])", "3"); | |
| 95 | |
| 96 | |
| 97 function argumentsApplyDelete1(a, b, c) | |
| 98 { | |
| 99 function t(a, b, c) | |
| 100 { | |
| 101 return a; | |
| 102 } | |
| 103 delete arguments[1]; | |
| 104 return t.apply(null, arguments); | |
| 105 } | |
| 106 | |
| 107 function argumentsApplyDelete2(a, b, c) | |
| 108 { | |
| 109 function t(a, b, c) | |
| 110 { | |
| 111 return b; | |
| 112 } | |
| 113 delete arguments[1]; | |
| 114 return t.apply(null, arguments); | |
| 115 } | |
| 116 | |
| 117 function argumentsApplyDelete3(a, b, c) | |
| 118 { | |
| 119 function t(a, b, c) | |
| 120 { | |
| 121 return c; | |
| 122 } | |
| 123 delete arguments[1]; | |
| 124 return t.apply(null, arguments); | |
| 125 } | |
| 126 | |
| 127 function argumentsApplyDeleteLength(a, b, c) | |
| 128 { | |
| 129 function t(a, b, c) | |
| 130 { | |
| 131 return arguments.length; | |
| 132 } | |
| 133 delete arguments[1]; | |
| 134 return t.apply(null, arguments); | |
| 135 } | |
| 136 | |
| 137 shouldBe("argumentsApplyDelete1(1, 2, 3)", "1"); | |
| 138 shouldBe("argumentsApplyDelete2(1, 2, 3)", "undefined"); | |
| 139 shouldBe("argumentsApplyDelete3(1, 2, 3)", "3"); | |
| 140 shouldBe("argumentsApplyDeleteLength(1, 2, 3)", "3"); | |
| 141 | |
| 142 | |
| 143 function arrayApplyDelete1(array) | |
| 144 { | |
| 145 function t(a, b, c) | |
| 146 { | |
| 147 return a; | |
| 148 } | |
| 149 delete array[1]; | |
| 150 return t.apply(null, array); | |
| 151 } | |
| 152 | |
| 153 function arrayApplyDelete2(array) | |
| 154 { | |
| 155 function t(a, b, c) | |
| 156 { | |
| 157 return b; | |
| 158 } | |
| 159 delete array[1]; | |
| 160 return t.apply(null, array); | |
| 161 } | |
| 162 | |
| 163 function arrayApplyDelete3(array) | |
| 164 { | |
| 165 function t(a, b, c) | |
| 166 { | |
| 167 return c; | |
| 168 } | |
| 169 delete array[1]; | |
| 170 return t.apply(null, array); | |
| 171 } | |
| 172 | |
| 173 function arrayApplyDeleteLength(array) | |
| 174 { | |
| 175 function t(a, b, c) | |
| 176 { | |
| 177 return arguments.length; | |
| 178 } | |
| 179 delete array[1]; | |
| 180 return t.apply(null, array); | |
| 181 } | |
| 182 | |
| 183 shouldBe("arrayApplyDelete1([1, 2, 3])", "1"); | |
| 184 shouldBe("arrayApplyDelete2([1, 2, 3])", "undefined"); | |
| 185 shouldBe("arrayApplyDelete3([1, 2, 3])", "3"); | |
| 186 shouldBe("arrayApplyDeleteLength([1, 2, 3])", "3"); | |
| 187 | |
| 188 | |
| 189 function argumentsApplyChangeLength1() | |
| 190 { | |
| 191 function f() { | |
| 192 return arguments.length; | |
| 193 }; | |
| 194 arguments.length = 2; | |
| 195 return f.apply(null, arguments); | |
| 196 } | |
| 197 | |
| 198 | |
| 199 function argumentsApplyChangeLength2() | |
| 200 { | |
| 201 function f(a) { | |
| 202 return arguments.length; | |
| 203 }; | |
| 204 arguments.length = 2; | |
| 205 return f.apply(null, arguments); | |
| 206 } | |
| 207 | |
| 208 | |
| 209 function argumentsApplyChangeLength3() | |
| 210 { | |
| 211 function f(a, b, c) { | |
| 212 return arguments.length; | |
| 213 }; | |
| 214 arguments.length = 2; | |
| 215 return f.apply(null, arguments); | |
| 216 }; | |
| 217 | |
| 218 function argumentsApplyChangeLength4() | |
| 219 { | |
| 220 function f() { | |
| 221 return arguments.length; | |
| 222 }; | |
| 223 arguments.length = 0; | |
| 224 return f.apply(null, arguments); | |
| 225 }; | |
| 226 | |
| 227 function argumentsApplyChangeLength5() | |
| 228 { | |
| 229 function f() { | |
| 230 return arguments.length; | |
| 231 }; | |
| 232 arguments.length = "Not A Number"; | |
| 233 return f.apply(null, arguments); | |
| 234 } | |
| 235 | |
| 236 shouldBe("argumentsApplyChangeLength1(1)", "2"); | |
| 237 shouldBe("argumentsApplyChangeLength2(1)", "2"); | |
| 238 shouldBe("argumentsApplyChangeLength3(1)", "2"); | |
| 239 shouldBe("argumentsApplyChangeLength4(1)", "0"); | |
| 240 shouldBe("argumentsApplyChangeLength5(1)", "0"); | |
| 241 | |
| 242 function arrayApplyChangeLength1() | |
| 243 { | |
| 244 function f() { | |
| 245 return arguments.length; | |
| 246 }; | |
| 247 var array = []; | |
| 248 array.length = 2; | |
| 249 return f.apply(null, array); | |
| 250 } | |
| 251 | |
| 252 function arrayApplyChangeLength2() | |
| 253 { | |
| 254 function f(a) { | |
| 255 return arguments.length; | |
| 256 }; | |
| 257 var array = []; | |
| 258 array.length = 2; | |
| 259 return f.apply(null, array); | |
| 260 } | |
| 261 | |
| 262 function arrayApplyChangeLength3() | |
| 263 { | |
| 264 function f(a, b, c) { | |
| 265 return arguments.length; | |
| 266 }; | |
| 267 var array = []; | |
| 268 array.length = 2; | |
| 269 return f.apply(null, array); | |
| 270 } | |
| 271 | |
| 272 function arrayApplyChangeLength4() | |
| 273 { | |
| 274 function f() { | |
| 275 return arguments.length; | |
| 276 }; | |
| 277 var array = [1]; | |
| 278 array.length = 0; | |
| 279 return f.apply(null, array); | |
| 280 }; | |
| 281 | |
| 282 shouldBe("arrayApplyChangeLength1()", "2"); | |
| 283 shouldBe("arrayApplyChangeLength2()", "2"); | |
| 284 shouldBe("arrayApplyChangeLength3()", "2"); | |
| 285 shouldBe("arrayApplyChangeLength4()", "0"); | |
| 286 | |
| 287 shouldBe("var a = []; a.length = 0xFFFE; [].constructor.apply('', a).length", "0
xFFFE"); | |
| 288 shouldBe("var a = []; a.length = 0xFFFF; [].constructor.apply('', a).length", "0
xFFFF"); | |
| 289 shouldBe("var a = []; a.length = 0x10000; [].constructor.apply('', a).length", "
0x10000"); | |
| 290 shouldThrow("var a = []; a.length = 0x10001; [].constructor.apply('', a).length"
); | |
| 291 shouldThrow("var a = []; a.length = 0xFFFFFFFE; [].constructor.apply('', a).leng
th"); | |
| 292 shouldThrow("var a = []; a.length = 0xFFFFFFFF; [].constructor.apply('', a).leng
th"); | |
| 293 | |
| 294 // ES5 permits apply with array-like objects. | |
| 295 shouldBe("(function(a,b,c,d){ return d ? -1 : (a+b+c); }).apply(undefined, {leng
th:3, 0:100, 1:20, 2:3})", '123'); | |
| OLD | NEW |