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