| OLD | NEW |
| 1 // Copyright 2012 the V8 project authors. All rights reserved. | 1 // Copyright 2012 the V8 project authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 "use strict"; | 5 "use strict"; |
| 6 | 6 |
| 7 // This file relies on the fact that the following declarations have been made | 7 // This file relies on the fact that the following declarations have been made |
| 8 // in runtime.js: | 8 // in runtime.js: |
| 9 // var $Array = global.Array; | 9 // var $Array = global.Array; |
| 10 | 10 |
| (...skipping 213 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 224 if (!IS_UNDEFINED(current) || key in array) { | 224 if (!IS_UNDEFINED(current) || key in array) { |
| 225 deleted_elements[key - start_i] = current; | 225 deleted_elements[key - start_i] = current; |
| 226 } | 226 } |
| 227 } | 227 } |
| 228 } | 228 } |
| 229 } | 229 } |
| 230 } | 230 } |
| 231 } | 231 } |
| 232 | 232 |
| 233 | 233 |
| 234 // This function implements the optimized splice implementation that can use | |
| 235 // special array operations to handle sparse arrays in a sensible fashion. | |
| 236 function SmartMove(array, start_i, del_count, len, num_additional_args) { | |
| 237 // Move data to new array. | |
| 238 var new_array = new InternalArray(len - del_count + num_additional_args); | |
| 239 var indices = %GetArrayKeys(array, len); | |
| 240 if (IS_NUMBER(indices)) { | |
| 241 var limit = indices; | |
| 242 for (var i = 0; i < start_i && i < limit; ++i) { | |
| 243 var current = array[i]; | |
| 244 if (!IS_UNDEFINED(current) || i in array) { | |
| 245 new_array[i] = current; | |
| 246 } | |
| 247 } | |
| 248 for (var i = start_i + del_count; i < limit; ++i) { | |
| 249 var current = array[i]; | |
| 250 if (!IS_UNDEFINED(current) || i in array) { | |
| 251 new_array[i - del_count + num_additional_args] = current; | |
| 252 } | |
| 253 } | |
| 254 } else { | |
| 255 var length = indices.length; | |
| 256 for (var k = 0; k < length; ++k) { | |
| 257 var key = indices[k]; | |
| 258 if (!IS_UNDEFINED(key)) { | |
| 259 if (key < start_i) { | |
| 260 var current = array[key]; | |
| 261 if (!IS_UNDEFINED(current) || key in array) { | |
| 262 new_array[key] = current; | |
| 263 } | |
| 264 } else if (key >= start_i + del_count) { | |
| 265 var current = array[key]; | |
| 266 if (!IS_UNDEFINED(current) || key in array) { | |
| 267 new_array[key - del_count + num_additional_args] = current; | |
| 268 } | |
| 269 } | |
| 270 } | |
| 271 } | |
| 272 } | |
| 273 // Move contents of new_array into this array | |
| 274 %MoveArrayContents(new_array, array); | |
| 275 } | |
| 276 | |
| 277 | |
| 278 // This is part of the old simple-minded splice. We are using it either | 234 // This is part of the old simple-minded splice. We are using it either |
| 279 // because the receiver is not an array (so we have no choice) or because we | 235 // because the receiver is not an array (so we have no choice) or because we |
| 280 // know we are not deleting or moving a lot of elements. | 236 // know we are not deleting or moving a lot of elements. |
| 281 function SimpleSlice(array, start_i, del_count, len, deleted_elements) { | 237 function SimpleSlice(array, start_i, del_count, len, deleted_elements) { |
| 282 for (var i = 0; i < del_count; i++) { | 238 for (var i = 0; i < del_count; i++) { |
| 283 var index = start_i + i; | 239 var index = start_i + i; |
| 284 // The spec could also be interpreted such that %HasOwnProperty | 240 // The spec could also be interpreted such that %HasOwnProperty |
| 285 // would be the appropriate test. We follow KJS in consulting the | 241 // would be the appropriate test. We follow KJS in consulting the |
| 286 // prototype. | 242 // prototype. |
| 287 var current = array[index]; | 243 var current = array[index]; |
| 288 if (!IS_UNDEFINED(current) || index in array) { | 244 if (!IS_UNDEFINED(current) || index in array) { |
| 289 deleted_elements[i] = current; | 245 deleted_elements[i] = current; |
| 290 } | 246 } |
| 291 } | 247 } |
| 292 } | 248 } |
| 293 | 249 |
| 294 | 250 |
| 295 function SimpleMove(array, start_i, del_count, len, num_additional_args) { | 251 function SimpleMove(array, start_i, del_count, len, num_additional_args, |
| 296 if (num_additional_args !== del_count) { | 252 force) { |
| 253 if (num_additional_args !== del_count || force) { |
| 297 // Move the existing elements after the elements to be deleted | 254 // Move the existing elements after the elements to be deleted |
| 298 // to the right position in the resulting array. | 255 // to the right position in the resulting array. |
| 299 if (num_additional_args > del_count) { | 256 if (num_additional_args > del_count) { |
| 300 for (var i = len - del_count; i > start_i; i--) { | 257 for (var i = len - del_count; i > start_i; i--) { |
| 301 var from_index = i + del_count - 1; | 258 var from_index = i + del_count - 1; |
| 302 var to_index = i + num_additional_args - 1; | 259 var to_index = i + num_additional_args - 1; |
| 303 // The spec could also be interpreted such that | 260 // The spec could also be interpreted such that |
| 304 // %HasOwnProperty would be the appropriate test. We follow | 261 // %HasOwnProperty would be the appropriate test. We follow |
| 305 // KJS in consulting the prototype. | 262 // KJS in consulting the prototype. |
| 306 var current = array[from_index]; | 263 var current = array[from_index]; |
| (...skipping 290 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 597 if (ObjectIsSealed(array)) { | 554 if (ObjectIsSealed(array)) { |
| 598 throw MakeTypeError("array_functions_change_sealed", | 555 throw MakeTypeError("array_functions_change_sealed", |
| 599 ["Array.prototype.shift"]); | 556 ["Array.prototype.shift"]); |
| 600 } | 557 } |
| 601 | 558 |
| 602 if (%IsObserved(array)) | 559 if (%IsObserved(array)) |
| 603 return ObservedArrayShift.call(array, len); | 560 return ObservedArrayShift.call(array, len); |
| 604 | 561 |
| 605 var first = array[0]; | 562 var first = array[0]; |
| 606 | 563 |
| 607 if (IS_ARRAY(array)) { | 564 SimpleMove(array, 0, 1, len, 0); |
| 608 SmartMove(array, 0, 1, len, 0); | |
| 609 } else { | |
| 610 SimpleMove(array, 0, 1, len, 0); | |
| 611 } | |
| 612 | 565 |
| 613 array.length = len - 1; | 566 array.length = len - 1; |
| 614 | 567 |
| 615 return first; | 568 return first; |
| 616 } | 569 } |
| 617 | 570 |
| 618 function ObservedArrayUnshift() { | 571 function ObservedArrayUnshift() { |
| 619 var len = TO_UINT32(this.length); | 572 var len = TO_UINT32(this.length); |
| 620 var num_arguments = %_ArgumentsLength(); | 573 var num_arguments = %_ArgumentsLength(); |
| 621 | 574 |
| (...skipping 15 matching lines...) Expand all Loading... |
| 637 | 590 |
| 638 function ArrayUnshift(arg1) { // length == 1 | 591 function ArrayUnshift(arg1) { // length == 1 |
| 639 CHECK_OBJECT_COERCIBLE(this, "Array.prototype.unshift"); | 592 CHECK_OBJECT_COERCIBLE(this, "Array.prototype.unshift"); |
| 640 | 593 |
| 641 if (%IsObserved(this)) | 594 if (%IsObserved(this)) |
| 642 return ObservedArrayUnshift.apply(this, arguments); | 595 return ObservedArrayUnshift.apply(this, arguments); |
| 643 | 596 |
| 644 var array = TO_OBJECT_INLINE(this); | 597 var array = TO_OBJECT_INLINE(this); |
| 645 var len = TO_UINT32(array.length); | 598 var len = TO_UINT32(array.length); |
| 646 var num_arguments = %_ArgumentsLength(); | 599 var num_arguments = %_ArgumentsLength(); |
| 647 var is_sealed = ObjectIsSealed(array); | 600 SimpleMove(array, 0, 0, len, num_arguments, true); |
| 648 | |
| 649 if (IS_ARRAY(array) && !is_sealed && len > 0) { | |
| 650 SmartMove(array, 0, 0, len, num_arguments); | |
| 651 } else { | |
| 652 SimpleMove(array, 0, 0, len, num_arguments); | |
| 653 } | |
| 654 | |
| 655 for (var i = 0; i < num_arguments; i++) { | 601 for (var i = 0; i < num_arguments; i++) { |
| 656 array[i] = %_Arguments(i); | 602 array[i] = %_Arguments(i); |
| 657 } | 603 } |
| 658 | 604 |
| 659 var new_length = len + num_arguments; | 605 var new_length = len + num_arguments; |
| 660 array.length = new_length; | 606 array.length = new_length; |
| 661 return new_length; | 607 return new_length; |
| 662 } | 608 } |
| 663 | 609 |
| 664 | 610 |
| (...skipping 134 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 799 throw MakeTypeError("array_functions_on_frozen", | 745 throw MakeTypeError("array_functions_on_frozen", |
| 800 ["Array.prototype.splice"]); | 746 ["Array.prototype.splice"]); |
| 801 } | 747 } |
| 802 | 748 |
| 803 var changed_elements = del_count; | 749 var changed_elements = del_count; |
| 804 if (num_elements_to_add != del_count) { | 750 if (num_elements_to_add != del_count) { |
| 805 // If the slice needs to do a actually move elements after the insertion | 751 // If the slice needs to do a actually move elements after the insertion |
| 806 // point, then include those in the estimate of changed elements. | 752 // point, then include those in the estimate of changed elements. |
| 807 changed_elements += len - start_i - del_count; | 753 changed_elements += len - start_i - del_count; |
| 808 } | 754 } |
| 809 if (UseSparseVariant(array, len, IS_ARRAY(array), changed_elements)) { | 755 SimpleSlice(array, start_i, del_count, len, deleted_elements); |
| 810 %NormalizeElements(array); | 756 SimpleMove(array, start_i, del_count, len, num_elements_to_add); |
| 811 %NormalizeElements(deleted_elements); | |
| 812 SmartSlice(array, start_i, del_count, len, deleted_elements); | |
| 813 SmartMove(array, start_i, del_count, len, num_elements_to_add); | |
| 814 } else { | |
| 815 SimpleSlice(array, start_i, del_count, len, deleted_elements); | |
| 816 SimpleMove(array, start_i, del_count, len, num_elements_to_add); | |
| 817 } | |
| 818 | 757 |
| 819 // Insert the arguments into the resulting array in | 758 // Insert the arguments into the resulting array in |
| 820 // place of the deleted elements. | 759 // place of the deleted elements. |
| 821 var i = start_i; | 760 var i = start_i; |
| 822 var arguments_index = 2; | 761 var arguments_index = 2; |
| 823 var arguments_length = %_ArgumentsLength(); | 762 var arguments_length = %_ArgumentsLength(); |
| 824 while (arguments_index < arguments_length) { | 763 while (arguments_index < arguments_length) { |
| 825 array[i++] = %_Arguments(arguments_index++); | 764 array[i++] = %_Arguments(arguments_index++); |
| 826 } | 765 } |
| 827 array.length = len - del_count + num_elements_to_add; | 766 array.length = len - del_count + num_elements_to_add; |
| (...skipping 737 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1565 )); | 1504 )); |
| 1566 | 1505 |
| 1567 SetUpLockedPrototype(InternalPackedArray, $Array(), $Array( | 1506 SetUpLockedPrototype(InternalPackedArray, $Array(), $Array( |
| 1568 "join", getFunction("join", ArrayJoin), | 1507 "join", getFunction("join", ArrayJoin), |
| 1569 "pop", getFunction("pop", ArrayPop), | 1508 "pop", getFunction("pop", ArrayPop), |
| 1570 "push", getFunction("push", ArrayPush) | 1509 "push", getFunction("push", ArrayPush) |
| 1571 )); | 1510 )); |
| 1572 } | 1511 } |
| 1573 | 1512 |
| 1574 SetUpArray(); | 1513 SetUpArray(); |
| OLD | NEW |