Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2010 the V8 project authors. All rights reserved. | 1 // Copyright 2010 the V8 project authors. All rights reserved. |
| 2 // Redistribution and use in source and binary forms, with or without | 2 // Redistribution and use in source and binary forms, with or without |
| 3 // modification, are permitted provided that the following conditions are | 3 // modification, are permitted provided that the following conditions are |
| 4 // met: | 4 // met: |
| 5 // | 5 // |
| 6 // * Redistributions of source code must retain the above copyright | 6 // * Redistributions of source code must retain the above copyright |
| 7 // notice, this list of conditions and the following disclaimer. | 7 // notice, this list of conditions and the following disclaimer. |
| 8 // * Redistributions in binary form must reproduce the above | 8 // * Redistributions in binary form must reproduce the above |
| 9 // copyright notice, this list of conditions and the following | 9 // copyright notice, this list of conditions and the following |
| 10 // disclaimer in the documentation and/or other materials provided | 10 // disclaimer in the documentation and/or other materials provided |
| (...skipping 282 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 293 // When we start splice, array is [0 .. 2*i - 1], so we get | 293 // When we start splice, array is [0 .. 2*i - 1], so we get |
| 294 // as a result [2*i], this element is removed from the array, | 294 // as a result [2*i], this element is removed from the array, |
| 295 // but [2 * i, 2 * i + 1] are added. | 295 // but [2 * i, 2 * i + 1] are added. |
| 296 assertEquals([2 * i - 1], spliced); | 296 assertEquals([2 * i - 1], spliced); |
| 297 assertEquals(2 * i, array[i]); | 297 assertEquals(2 * i, array[i]); |
| 298 assertEquals(2 * i + 1, array[i + 1]); | 298 assertEquals(2 * i + 1, array[i + 1]); |
| 299 } | 299 } |
| 300 } | 300 } |
| 301 })(); | 301 })(); |
| 302 | 302 |
| 303 // Check the behaviour when approaching maximal values for length. | |
| 304 (function() { | |
| 305 for (var i = 0; i < 7; i++) { | |
| 306 try { | |
| 307 new Array(Math.pow(2, 32) - 3).splice(-1, 0, 1, 2, 3, 4, 5); | |
| 308 throw 'Should have thrown RangeError'; | |
| 309 } catch (e) { | |
| 310 assertTrue(e instanceof RangeError); | |
| 311 } | |
|
neis
2016/04/08 15:21:43
Can't you just say
assertThrows(() => new Array(..
| |
| 312 | |
| 313 // Check smi boundary | |
| 314 var bigNum = (1 << 30) - 3; | |
| 315 var array = new Array(bigNum); | |
| 316 array.splice(-1, 0, 1, 2, 3, 4, 5, 6, 7); | |
| 317 assertEquals(bigNum + 7, array.length); | |
| 318 } | |
| 319 })(); | |
| 320 | |
| 321 (function() { | |
| 322 for (var i = 0; i < 7; i++) { | |
| 323 var a = [7, 8, 9]; | |
| 324 a.splice(0, 0, 1, 2, 3, 4, 5, 6); | |
| 325 assertEquals([1, 2, 3, 4, 5, 6, 7, 8, 9], a); | |
| 326 assertFalse(a.hasOwnProperty(10), "a.hasOwnProperty(10)"); | |
| 327 assertEquals(undefined, a[10]); | |
| 328 } | |
| 329 })(); | |
| 330 | |
| 331 (function testSpliceDeleteDouble() { | |
| 332 var a = [1.1, 1.2, 1.3, 1.4]; | |
| 333 a.splice(2, 1) | |
| 334 assertEquals([1.1, 1.2, 1.4], a); | |
| 335 })(); | |
| 336 | |
| 337 // Past this point the ArrayProtector is invalidated since we modify the | |
| 338 // Array.prototype. | |
| 339 | |
| 340 // Check the case of JS builtin .splice() | |
| 341 (function() { | |
| 342 for (var i = 0; i < 7; i++) { | |
| 343 var array = [1, 2, 3, 4]; | |
| 344 Array.prototype[3] = 'foo'; // To force JS builtin. | |
| 345 | |
| 346 var spliced = array.splice(); | |
| 347 | |
| 348 assertEquals([], spliced); | |
| 349 assertEquals([1, 2, 3, 4], array); | |
| 350 } | |
| 351 })(); | |
| 303 | 352 |
| 304 // Now check the case with array of holes and some elements on prototype. | 353 // Now check the case with array of holes and some elements on prototype. |
| 305 (function() { | 354 (function() { |
| 306 var len = 9; | 355 var len = 9; |
| 307 | 356 |
| 308 var at3 = "@3"; | 357 var at3 = "@3"; |
| 309 var at7 = "@7"; | 358 var at7 = "@7"; |
| 310 | 359 |
| 311 for (var i = 0; i < 7; i++) { | 360 for (var i = 0; i < 7; i++) { |
| 312 var array = new Array(len); | 361 var array = new Array(len); |
| (...skipping 30 matching lines...) Expand all Loading... | |
| 343 // and now check couple of indices above length. | 392 // and now check couple of indices above length. |
| 344 assertFalse(array.hasOwnProperty(10), "array.hasOwnProperty(10)"); | 393 assertFalse(array.hasOwnProperty(10), "array.hasOwnProperty(10)"); |
| 345 assertFalse(array.hasOwnProperty(15), "array.hasOwnProperty(15)"); | 394 assertFalse(array.hasOwnProperty(15), "array.hasOwnProperty(15)"); |
| 346 assertFalse(array.hasOwnProperty(31), "array.hasOwnProperty(31)"); | 395 assertFalse(array.hasOwnProperty(31), "array.hasOwnProperty(31)"); |
| 347 assertFalse(array.hasOwnProperty(63), "array.hasOwnProperty(63)"); | 396 assertFalse(array.hasOwnProperty(63), "array.hasOwnProperty(63)"); |
| 348 assertFalse(array.hasOwnProperty(Math.pow(2, 32) - 2), | 397 assertFalse(array.hasOwnProperty(Math.pow(2, 32) - 2), |
| 349 "array.hasOwnProperty(Math.pow(2, 32) - 2)"); | 398 "array.hasOwnProperty(Math.pow(2, 32) - 2)"); |
| 350 } | 399 } |
| 351 })(); | 400 })(); |
| 352 | 401 |
| 353 | |
| 354 // Now check the case with array of holes and some elements on prototype. | 402 // Now check the case with array of holes and some elements on prototype. |
| 355 (function() { | 403 (function() { |
| 356 var len = 9; | 404 var len = 9; |
| 357 | 405 |
| 358 var at3 = "@3"; | 406 var at3 = "@3"; |
| 359 var at7 = "@7"; | 407 var at7 = "@7"; |
| 360 | 408 |
| 361 for (var i = 0; i < 7; i++) { | 409 for (var i = 0; i < 7; i++) { |
| 362 var array = new Array(len); | 410 var array = new Array(len); |
| 363 Array.prototype[3] = at3; | 411 Array.prototype[3] = at3; |
| (...skipping 26 matching lines...) Expand all Loading... | |
| 390 | 438 |
| 391 // and now check couple of indices above length. | 439 // and now check couple of indices above length. |
| 392 assertFalse(array.hasOwnProperty(10), "array.hasOwnProperty(10)"); | 440 assertFalse(array.hasOwnProperty(10), "array.hasOwnProperty(10)"); |
| 393 assertFalse(array.hasOwnProperty(15), "array.hasOwnProperty(15)"); | 441 assertFalse(array.hasOwnProperty(15), "array.hasOwnProperty(15)"); |
| 394 assertFalse(array.hasOwnProperty(31), "array.hasOwnProperty(31)"); | 442 assertFalse(array.hasOwnProperty(31), "array.hasOwnProperty(31)"); |
| 395 assertFalse(array.hasOwnProperty(63), "array.hasOwnProperty(63)"); | 443 assertFalse(array.hasOwnProperty(63), "array.hasOwnProperty(63)"); |
| 396 assertFalse(array.hasOwnProperty(Math.pow(2, 32) - 2), | 444 assertFalse(array.hasOwnProperty(Math.pow(2, 32) - 2), |
| 397 "array.hasOwnProperty(Math.pow(2, 32) - 2)"); | 445 "array.hasOwnProperty(Math.pow(2, 32) - 2)"); |
| 398 } | 446 } |
| 399 })(); | 447 })(); |
| 400 | |
| 401 | |
| 402 // Check the case of JS builtin .splice() | |
| 403 (function() { | |
| 404 for (var i = 0; i < 7; i++) { | |
| 405 var array = [1, 2, 3, 4]; | |
| 406 Array.prototype[3] = 'foo'; // To force JS builtin. | |
| 407 | |
| 408 var spliced = array.splice(); | |
| 409 | |
| 410 assertEquals([], spliced); | |
| 411 assertEquals([1, 2, 3, 4], array); | |
| 412 } | |
| 413 })(); | |
| 414 | |
| 415 | |
| 416 // Check the behaviour when approaching maximal values for length. | |
| 417 (function() { | |
| 418 for (var i = 0; i < 7; i++) { | |
| 419 try { | |
| 420 new Array(Math.pow(2, 32) - 3).splice(-1, 0, 1, 2, 3, 4, 5); | |
| 421 throw 'Should have thrown RangeError'; | |
| 422 } catch (e) { | |
| 423 assertTrue(e instanceof RangeError); | |
| 424 } | |
| 425 | |
|
neis
2016/04/08 15:21:43
Ok now I see that you only moved this around :o
| |
| 426 // Check smi boundary | |
| 427 var bigNum = (1 << 30) - 3; | |
| 428 var array = new Array(bigNum); | |
| 429 array.splice(-1, 0, 1, 2, 3, 4, 5, 6, 7); | |
| 430 assertEquals(bigNum + 7, array.length); | |
| 431 } | |
| 432 })(); | |
| 433 | |
| 434 (function() { | |
| 435 for (var i = 0; i < 7; i++) { | |
| 436 var a = [7, 8, 9]; | |
| 437 a.splice(0, 0, 1, 2, 3, 4, 5, 6); | |
| 438 assertEquals([1, 2, 3, 4, 5, 6, 7, 8, 9], a); | |
| 439 assertFalse(a.hasOwnProperty(10), "a.hasOwnProperty(10)"); | |
| 440 assertEquals(undefined, a[10]); | |
| 441 } | |
| 442 })(); | |
| OLD | NEW |