OLD | NEW |
(Empty) | |
| 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 // Flags: --type-profile --turbo |
| 29 |
| 30 // Copy of mjsunit/array-splice as integration test for type profiling. |
| 31 // It only tests that nothing crashes, no actual test for type profile output. |
| 32 |
| 33 (function() { |
| 34 for (var i = 0; i < 7; i++) { |
| 35 var array = new Array(10); |
| 36 var spliced = array.splice(1, 1, 'one', 'two'); |
| 37 assertEquals(1, spliced.length); |
| 38 assertFalse(0 in spliced, "0 in spliced"); |
| 39 |
| 40 assertEquals(11, array.length); |
| 41 assertFalse(0 in array, "0 in array"); |
| 42 assertTrue(1 in array); |
| 43 assertTrue(2 in array); |
| 44 assertFalse(3 in array, "3 in array"); |
| 45 } |
| 46 })(); |
| 47 |
| 48 |
| 49 // Check various variants of empty array's splicing. |
| 50 (function() { |
| 51 for (var i = 0; i < 7; i++) { |
| 52 assertEquals([], [].splice(0, 0)); |
| 53 assertEquals([], [].splice(1, 0)); |
| 54 assertEquals([], [].splice(0, 1)); |
| 55 assertEquals([], [].splice(-1, 0)); |
| 56 } |
| 57 })(); |
| 58 |
| 59 |
| 60 // Check that even if result array is empty, receiver gets sliced. |
| 61 (function() { |
| 62 for (var i = 0; i < 7; i++) { |
| 63 var a = [1, 2, 3]; |
| 64 assertEquals([], a.splice(1, 0, 'a', 'b', 'c')); |
| 65 assertEquals([1, 'a', 'b', 'c', 2, 3], a); |
| 66 } |
| 67 })(); |
| 68 |
| 69 |
| 70 // Check various forms of arguments omission. |
| 71 (function() { |
| 72 var array; |
| 73 for (var i = 0; i < 7; i++) { |
| 74 array = [1, 2, 3] |
| 75 assertEquals([], array.splice()); |
| 76 assertEquals([1, 2, 3], array); |
| 77 |
| 78 // SpiderMonkey, TraceMonkey and JSC treat the case where no delete count is |
| 79 // given differently from when an undefined delete count is given. |
| 80 // This does not follow ECMA-262, but we do the same for |
| 81 // compatibility. |
| 82 array = [1, 2, 3] |
| 83 assertEquals([1, 2, 3], array.splice(0)); |
| 84 assertEquals([], array); |
| 85 |
| 86 array = [1, 2, 3] |
| 87 assertEquals([1, 2, 3], array.splice(undefined)); |
| 88 assertEquals([], array); |
| 89 |
| 90 array = [1, 2, 3] |
| 91 assertEquals([1, 2, 3], array.splice("foobar")); |
| 92 assertEquals([], array); |
| 93 |
| 94 array = [1, 2, 3] |
| 95 assertEquals([], array.splice(undefined, undefined)); |
| 96 assertEquals([1, 2, 3], array); |
| 97 |
| 98 array = [1, 2, 3] |
| 99 assertEquals([], array.splice("foobar", undefined)); |
| 100 assertEquals([1, 2, 3], array); |
| 101 |
| 102 array = [1, 2, 3] |
| 103 assertEquals([], array.splice(undefined, "foobar")); |
| 104 assertEquals([1, 2, 3], array); |
| 105 |
| 106 array = [1, 2, 3] |
| 107 assertEquals([], array.splice("foobar", "foobar")); |
| 108 assertEquals([1, 2, 3], array); |
| 109 } |
| 110 })(); |
| 111 |
| 112 |
| 113 // Check variants of negatives and positive indices. |
| 114 (function() { |
| 115 var array, spliced; |
| 116 for (var i = 0; i < 7; i++) { |
| 117 array = [1, 2, 3, 4, 5, 6, 7]; |
| 118 spliced = array.splice(-100); |
| 119 assertEquals([], array); |
| 120 assertEquals([1, 2, 3, 4, 5, 6, 7], spliced); |
| 121 |
| 122 array = [1, 2, 3, 4, 5, 6, 7]; |
| 123 spliced = array.splice(-1e100); |
| 124 assertEquals([], array); |
| 125 assertEquals([1, 2, 3, 4, 5, 6, 7], spliced); |
| 126 |
| 127 array = [1, 2, 3, 4, 5, 6, 7]; |
| 128 spliced = array.splice(-3); |
| 129 assertEquals([1, 2, 3, 4], array); |
| 130 assertEquals([5, 6, 7], spliced); |
| 131 |
| 132 array = [1, 2, 3, 4, 5, 6, 7]; |
| 133 spliced = array.splice(-3.999999); |
| 134 assertEquals([1, 2, 3, 4], array); |
| 135 assertEquals([5, 6, 7], spliced); |
| 136 |
| 137 array = [1, 2, 3, 4, 5, 6, 7]; |
| 138 spliced = array.splice(-3.000001); |
| 139 assertEquals([1, 2, 3, 4], array); |
| 140 assertEquals([5, 6, 7], spliced); |
| 141 |
| 142 array = [1, 2, 3, 4, 5, 6, 7]; |
| 143 spliced = array.splice(4); |
| 144 assertEquals([1, 2, 3, 4], array); |
| 145 assertEquals([5, 6, 7], spliced); |
| 146 |
| 147 array = [1, 2, 3, 4, 5, 6, 7]; |
| 148 spliced = array.splice(4.999999); |
| 149 assertEquals([1, 2, 3, 4], array); |
| 150 assertEquals([5, 6, 7], spliced); |
| 151 |
| 152 array = [1, 2, 3, 4, 5, 6, 7]; |
| 153 spliced = array.splice(4.000001); |
| 154 assertEquals([1, 2, 3, 4], array); |
| 155 assertEquals([5, 6, 7], spliced); |
| 156 |
| 157 array = [1, 2, 3, 4, 5, 6, 7]; |
| 158 spliced = array.splice(6); |
| 159 assertEquals([1, 2, 3, 4, 5, 6], array); |
| 160 assertEquals([7], spliced); |
| 161 |
| 162 array = [1, 2, 3, 4, 5, 6, 7]; |
| 163 spliced = array.splice(7); |
| 164 assertEquals([1, 2, 3, 4, 5, 6, 7], array); |
| 165 assertEquals([], spliced); |
| 166 |
| 167 array = [1, 2, 3, 4, 5, 6, 7]; |
| 168 spliced = array.splice(8); |
| 169 assertEquals([1, 2, 3, 4, 5, 6, 7], array); |
| 170 assertEquals([], spliced); |
| 171 |
| 172 array = [1, 2, 3, 4, 5, 6, 7]; |
| 173 spliced = array.splice(100); |
| 174 assertEquals([1, 2, 3, 4, 5, 6, 7], array); |
| 175 assertEquals([], spliced); |
| 176 |
| 177 array = [1, 2, 3, 4, 5, 6, 7]; |
| 178 spliced = array.splice(1e100); |
| 179 assertEquals([1, 2, 3, 4, 5, 6, 7], array); |
| 180 assertEquals([], spliced); |
| 181 |
| 182 array = [1, 2, 3, 4, 5, 6, 7]; |
| 183 spliced = array.splice(0, -100); |
| 184 assertEquals([1, 2, 3, 4, 5, 6, 7], array); |
| 185 assertEquals([], spliced); |
| 186 |
| 187 array = [1, 2, 3, 4, 5, 6, 7]; |
| 188 spliced = array.splice(0, -1e100); |
| 189 assertEquals([1, 2, 3, 4, 5, 6, 7], array); |
| 190 assertEquals([], spliced); |
| 191 |
| 192 array = [1, 2, 3, 4, 5, 6, 7]; |
| 193 spliced = array.splice(0, -3); |
| 194 assertEquals([1, 2, 3, 4, 5, 6, 7], array); |
| 195 assertEquals([], spliced); |
| 196 |
| 197 array = [1, 2, 3, 4, 5, 6, 7]; |
| 198 spliced = array.splice(0, -3.999999); |
| 199 assertEquals([1, 2, 3, 4, 5, 6, 7], array); |
| 200 assertEquals([], spliced); |
| 201 |
| 202 array = [1, 2, 3, 4, 5, 6, 7]; |
| 203 spliced = array.splice(0, -3.000001); |
| 204 assertEquals([1, 2, 3, 4, 5, 6, 7], array); |
| 205 assertEquals([], spliced); |
| 206 |
| 207 array = [1, 2, 3, 4, 5, 6, 7]; |
| 208 spliced = array.splice(0, 4); |
| 209 assertEquals([5, 6, 7], array); |
| 210 assertEquals([1, 2, 3, 4], spliced); |
| 211 |
| 212 array = [1, 2, 3, 4, 5, 6, 7]; |
| 213 spliced = array.splice(0, 4.999999); |
| 214 assertEquals([5, 6, 7], array); |
| 215 assertEquals([1, 2, 3, 4], spliced); |
| 216 |
| 217 array = [1, 2, 3, 4, 5, 6, 7]; |
| 218 spliced = array.splice(0, 4.000001); |
| 219 assertEquals([5, 6, 7], array); |
| 220 assertEquals([1, 2, 3, 4], spliced); |
| 221 |
| 222 array = [1, 2, 3, 4, 5, 6, 7]; |
| 223 spliced = array.splice(0, 6); |
| 224 assertEquals([7], array); |
| 225 assertEquals([1, 2, 3, 4, 5, 6], spliced); |
| 226 |
| 227 array = [1, 2, 3, 4, 5, 6, 7]; |
| 228 spliced = array.splice(0, 7); |
| 229 assertEquals([], array); |
| 230 assertEquals([1, 2, 3, 4, 5, 6, 7], spliced); |
| 231 |
| 232 array = [1, 2, 3, 4, 5, 6, 7]; |
| 233 spliced = array.splice(0, 8); |
| 234 assertEquals([], array); |
| 235 assertEquals([1, 2, 3, 4, 5, 6, 7], spliced); |
| 236 |
| 237 array = [1, 2, 3, 4, 5, 6, 7]; |
| 238 spliced = array.splice(0, 100); |
| 239 assertEquals([], array); |
| 240 assertEquals([1, 2, 3, 4, 5, 6, 7], spliced); |
| 241 |
| 242 array = [1, 2, 3, 4, 5, 6, 7]; |
| 243 spliced = array.splice(0, 1e100); |
| 244 assertEquals([], array); |
| 245 assertEquals([1, 2, 3, 4, 5, 6, 7], spliced); |
| 246 |
| 247 // Some exotic cases. |
| 248 obj = { toString: function() { throw 'Exception'; } }; |
| 249 |
| 250 // Throwing an exception in conversion: |
| 251 try { |
| 252 [1, 2, 3].splice(obj, 3); |
| 253 throw 'Should have thrown'; |
| 254 } catch (e) { |
| 255 assertEquals('Exception', e); |
| 256 } |
| 257 |
| 258 try { |
| 259 [1, 2, 3].splice(0, obj, 3); |
| 260 throw 'Should have thrown'; |
| 261 } catch (e) { |
| 262 assertEquals('Exception', e); |
| 263 } |
| 264 |
| 265 array = [1, 2, 3]; |
| 266 array.splice(0, 3, obj); |
| 267 assertEquals(1, array.length); |
| 268 |
| 269 // Custom conversion: |
| 270 array = [1, 2, 3]; |
| 271 spliced = array.splice({valueOf: function() { return 1; }}, |
| 272 {toString: function() { return 2; }}, |
| 273 'one', 'two'); |
| 274 assertEquals([2, 3], spliced); |
| 275 assertEquals([1, 'one', 'two'], array); |
| 276 } |
| 277 })(); |
| 278 |
| 279 |
| 280 // Nasty: modify the array in ToInteger. |
| 281 (function() { |
| 282 var array = []; |
| 283 var spliced; |
| 284 |
| 285 for (var i = 0; i < 13; i++) { |
| 286 bad_start = { valueOf: function() { array.push(2*i); return -1; } }; |
| 287 bad_count = { valueOf: function() { array.push(2*i + 1); return 1; } }; |
| 288 spliced = array.splice(bad_start, bad_count); |
| 289 // According to the spec (15.4.4.12), length is calculated before |
| 290 // performing ToInteger on arguments. However, v8 ignores elements |
| 291 // we add while converting, so we need corrective pushes. |
| 292 array.push(2*i); array.push(2*i + 1); |
| 293 if (i == 0) { |
| 294 assertEquals([], spliced); // Length was 0, nothing to get. |
| 295 assertEquals([0, 1], array); |
| 296 } else { |
| 297 // When we start splice, array is [0 .. 2*i - 1], so we get |
| 298 // as a result [2*i], this element is removed from the array, |
| 299 // but [2 * i, 2 * i + 1] are added. |
| 300 assertEquals([2 * i - 1], spliced); |
| 301 assertEquals(2 * i, array[i]); |
| 302 assertEquals(2 * i + 1, array[i + 1]); |
| 303 } |
| 304 } |
| 305 })(); |
| 306 |
| 307 // Check the behaviour when approaching maximal values for length. |
| 308 (function() { |
| 309 for (var i = 0; i < 7; i++) { |
| 310 try { |
| 311 new Array(Math.pow(2, 32) - 3).splice(-1, 0, 1, 2, 3, 4, 5); |
| 312 throw 'Should have thrown RangeError'; |
| 313 } catch (e) { |
| 314 assertTrue(e instanceof RangeError); |
| 315 } |
| 316 |
| 317 // Check smi boundary |
| 318 var bigNum = (1 << 30) - 3; |
| 319 var array = new Array(bigNum); |
| 320 array.splice(-1, 0, 1, 2, 3, 4, 5, 6, 7); |
| 321 assertEquals(bigNum + 7, array.length); |
| 322 } |
| 323 })(); |
| 324 |
| 325 (function() { |
| 326 for (var i = 0; i < 7; i++) { |
| 327 var a = [7, 8, 9]; |
| 328 a.splice(0, 0, 1, 2, 3, 4, 5, 6); |
| 329 assertEquals([1, 2, 3, 4, 5, 6, 7, 8, 9], a); |
| 330 assertFalse(a.hasOwnProperty(10), "a.hasOwnProperty(10)"); |
| 331 assertEquals(undefined, a[10]); |
| 332 } |
| 333 })(); |
| 334 |
| 335 (function testSpliceDeleteDouble() { |
| 336 var a = [1.1, 1.2, 1.3, 1.4]; |
| 337 a.splice(2, 1) |
| 338 assertEquals([1.1, 1.2, 1.4], a); |
| 339 })(); |
| 340 |
| 341 // Past this point the ArrayProtector is invalidated since we modify the |
| 342 // Array.prototype. |
| 343 |
| 344 // Check the case of JS builtin .splice() |
| 345 (function() { |
| 346 for (var i = 0; i < 7; i++) { |
| 347 var array = [1, 2, 3, 4]; |
| 348 Array.prototype[3] = 'foo'; // To force JS builtin. |
| 349 |
| 350 var spliced = array.splice(); |
| 351 |
| 352 assertEquals([], spliced); |
| 353 assertEquals([1, 2, 3, 4], array); |
| 354 } |
| 355 })(); |
| 356 |
| 357 // Now check the case with array of holes and some elements on prototype. |
| 358 (function() { |
| 359 var len = 9; |
| 360 |
| 361 var at3 = "@3"; |
| 362 var at7 = "@7"; |
| 363 |
| 364 for (var i = 0; i < 7; i++) { |
| 365 var array = new Array(len); |
| 366 var array_proto = []; |
| 367 array_proto[3] = at3; |
| 368 array_proto[7] = at7; |
| 369 array.__proto__ = array_proto; |
| 370 |
| 371 var spliced = array.splice(2, 2, 'one', undefined, 'two'); |
| 372 |
| 373 // Second hole (at index 3) of array turns into |
| 374 // value of Array.prototype[3] while copying. |
| 375 assertEquals([, at3], spliced); |
| 376 assertEquals([, , 'one', undefined, 'two', , , at7, at7, ,], array); |
| 377 |
| 378 // ... but array[3] and array[7] is actually a hole: |
| 379 assertTrue(delete array_proto[3]); |
| 380 assertEquals(undefined, array[3]); |
| 381 assertTrue(delete array_proto[7]); |
| 382 assertEquals(undefined, array[7]); |
| 383 |
| 384 // and now check hasOwnProperty |
| 385 assertFalse(array.hasOwnProperty(0), "array.hasOwnProperty(0)"); |
| 386 assertFalse(array.hasOwnProperty(1), "array.hasOwnProperty(1)"); |
| 387 assertTrue(array.hasOwnProperty(2)); |
| 388 assertTrue(array.hasOwnProperty(3)); |
| 389 assertTrue(array.hasOwnProperty(4)); |
| 390 assertFalse(array.hasOwnProperty(5), "array.hasOwnProperty(5)"); |
| 391 assertFalse(array.hasOwnProperty(6), "array.hasOwnProperty(6)"); |
| 392 assertFalse(array.hasOwnProperty(7), "array.hasOwnProperty(7)"); |
| 393 assertTrue(array.hasOwnProperty(8)); |
| 394 assertFalse(array.hasOwnProperty(9), "array.hasOwnProperty(9)"); |
| 395 |
| 396 // and now check couple of indices above length. |
| 397 assertFalse(array.hasOwnProperty(10), "array.hasOwnProperty(10)"); |
| 398 assertFalse(array.hasOwnProperty(15), "array.hasOwnProperty(15)"); |
| 399 assertFalse(array.hasOwnProperty(31), "array.hasOwnProperty(31)"); |
| 400 assertFalse(array.hasOwnProperty(63), "array.hasOwnProperty(63)"); |
| 401 assertFalse(array.hasOwnProperty(Math.pow(2, 32) - 2), |
| 402 "array.hasOwnProperty(Math.pow(2, 32) - 2)"); |
| 403 } |
| 404 })(); |
| 405 |
| 406 // Now check the case with array of holes and some elements on prototype. |
| 407 (function() { |
| 408 var len = 9; |
| 409 |
| 410 var at3 = "@3"; |
| 411 var at7 = "@7"; |
| 412 |
| 413 for (var i = 0; i < 7; i++) { |
| 414 var array = new Array(len); |
| 415 Array.prototype[3] = at3; |
| 416 Array.prototype[7] = at7; |
| 417 |
| 418 var spliced = array.splice(2, 2, 'one', undefined, 'two'); |
| 419 |
| 420 // Second hole (at index 3) of array turns into |
| 421 // value of Array.prototype[3] while copying. |
| 422 assertEquals([, at3], spliced); |
| 423 assertEquals([, , 'one', undefined, 'two', , , at7, at7, ,], array); |
| 424 |
| 425 // ... but array[3] and array[7] is actually a hole: |
| 426 assertTrue(delete Array.prototype[3]); |
| 427 assertEquals(undefined, array[3]); |
| 428 assertTrue(delete Array.prototype[7]); |
| 429 assertEquals(undefined, array[7]); |
| 430 |
| 431 // and now check hasOwnProperty |
| 432 assertFalse(array.hasOwnProperty(0), "array.hasOwnProperty(0)"); |
| 433 assertFalse(array.hasOwnProperty(1), "array.hasOwnProperty(1)"); |
| 434 assertTrue(array.hasOwnProperty(2)); |
| 435 assertTrue(array.hasOwnProperty(3)); |
| 436 assertTrue(array.hasOwnProperty(4)); |
| 437 assertFalse(array.hasOwnProperty(5), "array.hasOwnProperty(5)"); |
| 438 assertFalse(array.hasOwnProperty(6), "array.hasOwnProperty(6)"); |
| 439 assertFalse(array.hasOwnProperty(7), "array.hasOwnProperty(7)"); |
| 440 assertTrue(array.hasOwnProperty(8)); |
| 441 assertFalse(array.hasOwnProperty(9), "array.hasOwnProperty(9)"); |
| 442 |
| 443 // and now check couple of indices above length. |
| 444 assertFalse(array.hasOwnProperty(10), "array.hasOwnProperty(10)"); |
| 445 assertFalse(array.hasOwnProperty(15), "array.hasOwnProperty(15)"); |
| 446 assertFalse(array.hasOwnProperty(31), "array.hasOwnProperty(31)"); |
| 447 assertFalse(array.hasOwnProperty(63), "array.hasOwnProperty(63)"); |
| 448 assertFalse(array.hasOwnProperty(Math.pow(2, 32) - 2), |
| 449 "array.hasOwnProperty(Math.pow(2, 32) - 2)"); |
| 450 } |
| 451 })(); |
OLD | NEW |