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 |
234 // This is part of the old simple-minded splice. We are using it either | 278 // This is part of the old simple-minded splice. We are using it either |
235 // because the receiver is not an array (so we have no choice) or because we | 279 // because the receiver is not an array (so we have no choice) or because we |
236 // know we are not deleting or moving a lot of elements. | 280 // know we are not deleting or moving a lot of elements. |
237 function SimpleSlice(array, start_i, del_count, len, deleted_elements) { | 281 function SimpleSlice(array, start_i, del_count, len, deleted_elements) { |
238 for (var i = 0; i < del_count; i++) { | 282 for (var i = 0; i < del_count; i++) { |
239 var index = start_i + i; | 283 var index = start_i + i; |
240 // The spec could also be interpreted such that %HasOwnProperty | 284 // The spec could also be interpreted such that %HasOwnProperty |
241 // would be the appropriate test. We follow KJS in consulting the | 285 // would be the appropriate test. We follow KJS in consulting the |
242 // prototype. | 286 // prototype. |
243 var current = array[index]; | 287 var current = array[index]; |
244 if (!IS_UNDEFINED(current) || index in array) { | 288 if (!IS_UNDEFINED(current) || index in array) { |
245 deleted_elements[i] = current; | 289 deleted_elements[i] = current; |
246 } | 290 } |
247 } | 291 } |
248 } | 292 } |
249 | 293 |
250 | 294 |
251 function SimpleMove(array, start_i, del_count, len, num_additional_args, | 295 function SimpleMove(array, start_i, del_count, len, num_additional_args) { |
252 force) { | 296 if (num_additional_args !== del_count) { |
253 if (num_additional_args !== del_count || force) { | |
254 // Move the existing elements after the elements to be deleted | 297 // Move the existing elements after the elements to be deleted |
255 // to the right position in the resulting array. | 298 // to the right position in the resulting array. |
256 if (num_additional_args > del_count) { | 299 if (num_additional_args > del_count) { |
257 for (var i = len - del_count; i > start_i; i--) { | 300 for (var i = len - del_count; i > start_i; i--) { |
258 var from_index = i + del_count - 1; | 301 var from_index = i + del_count - 1; |
259 var to_index = i + num_additional_args - 1; | 302 var to_index = i + num_additional_args - 1; |
260 // The spec could also be interpreted such that | 303 // The spec could also be interpreted such that |
261 // %HasOwnProperty would be the appropriate test. We follow | 304 // %HasOwnProperty would be the appropriate test. We follow |
262 // KJS in consulting the prototype. | 305 // KJS in consulting the prototype. |
263 var current = array[from_index]; | 306 var current = array[from_index]; |
(...skipping 290 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
554 if (ObjectIsSealed(array)) { | 597 if (ObjectIsSealed(array)) { |
555 throw MakeTypeError("array_functions_change_sealed", | 598 throw MakeTypeError("array_functions_change_sealed", |
556 ["Array.prototype.shift"]); | 599 ["Array.prototype.shift"]); |
557 } | 600 } |
558 | 601 |
559 if (%IsObserved(array)) | 602 if (%IsObserved(array)) |
560 return ObservedArrayShift.call(array, len); | 603 return ObservedArrayShift.call(array, len); |
561 | 604 |
562 var first = array[0]; | 605 var first = array[0]; |
563 | 606 |
564 SimpleMove(array, 0, 1, len, 0); | 607 if (IS_ARRAY(array)) { |
| 608 SmartMove(array, 0, 1, len, 0); |
| 609 } else { |
| 610 SimpleMove(array, 0, 1, len, 0); |
| 611 } |
565 | 612 |
566 array.length = len - 1; | 613 array.length = len - 1; |
567 | 614 |
568 return first; | 615 return first; |
569 } | 616 } |
570 | 617 |
571 function ObservedArrayUnshift() { | 618 function ObservedArrayUnshift() { |
572 var len = TO_UINT32(this.length); | 619 var len = TO_UINT32(this.length); |
573 var num_arguments = %_ArgumentsLength(); | 620 var num_arguments = %_ArgumentsLength(); |
574 | 621 |
(...skipping 15 matching lines...) Expand all Loading... |
590 | 637 |
591 function ArrayUnshift(arg1) { // length == 1 | 638 function ArrayUnshift(arg1) { // length == 1 |
592 CHECK_OBJECT_COERCIBLE(this, "Array.prototype.unshift"); | 639 CHECK_OBJECT_COERCIBLE(this, "Array.prototype.unshift"); |
593 | 640 |
594 if (%IsObserved(this)) | 641 if (%IsObserved(this)) |
595 return ObservedArrayUnshift.apply(this, arguments); | 642 return ObservedArrayUnshift.apply(this, arguments); |
596 | 643 |
597 var array = TO_OBJECT_INLINE(this); | 644 var array = TO_OBJECT_INLINE(this); |
598 var len = TO_UINT32(array.length); | 645 var len = TO_UINT32(array.length); |
599 var num_arguments = %_ArgumentsLength(); | 646 var num_arguments = %_ArgumentsLength(); |
600 SimpleMove(array, 0, 0, len, num_arguments, true); | 647 var is_sealed = ObjectIsSealed(array); |
| 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 |
601 for (var i = 0; i < num_arguments; i++) { | 655 for (var i = 0; i < num_arguments; i++) { |
602 array[i] = %_Arguments(i); | 656 array[i] = %_Arguments(i); |
603 } | 657 } |
604 | 658 |
605 var new_length = len + num_arguments; | 659 var new_length = len + num_arguments; |
606 array.length = new_length; | 660 array.length = new_length; |
607 return new_length; | 661 return new_length; |
608 } | 662 } |
609 | 663 |
610 | 664 |
(...skipping 134 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
745 throw MakeTypeError("array_functions_on_frozen", | 799 throw MakeTypeError("array_functions_on_frozen", |
746 ["Array.prototype.splice"]); | 800 ["Array.prototype.splice"]); |
747 } | 801 } |
748 | 802 |
749 var changed_elements = del_count; | 803 var changed_elements = del_count; |
750 if (num_elements_to_add != del_count) { | 804 if (num_elements_to_add != del_count) { |
751 // If the slice needs to do a actually move elements after the insertion | 805 // If the slice needs to do a actually move elements after the insertion |
752 // point, then include those in the estimate of changed elements. | 806 // point, then include those in the estimate of changed elements. |
753 changed_elements += len - start_i - del_count; | 807 changed_elements += len - start_i - del_count; |
754 } | 808 } |
755 SimpleSlice(array, start_i, del_count, len, deleted_elements); | 809 if (UseSparseVariant(array, len, IS_ARRAY(array), changed_elements)) { |
756 SimpleMove(array, start_i, del_count, len, num_elements_to_add); | 810 %NormalizeElements(array); |
| 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 } |
757 | 818 |
758 // Insert the arguments into the resulting array in | 819 // Insert the arguments into the resulting array in |
759 // place of the deleted elements. | 820 // place of the deleted elements. |
760 var i = start_i; | 821 var i = start_i; |
761 var arguments_index = 2; | 822 var arguments_index = 2; |
762 var arguments_length = %_ArgumentsLength(); | 823 var arguments_length = %_ArgumentsLength(); |
763 while (arguments_index < arguments_length) { | 824 while (arguments_index < arguments_length) { |
764 array[i++] = %_Arguments(arguments_index++); | 825 array[i++] = %_Arguments(arguments_index++); |
765 } | 826 } |
766 array.length = len - del_count + num_elements_to_add; | 827 array.length = len - del_count + num_elements_to_add; |
(...skipping 737 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1504 )); | 1565 )); |
1505 | 1566 |
1506 SetUpLockedPrototype(InternalPackedArray, $Array(), $Array( | 1567 SetUpLockedPrototype(InternalPackedArray, $Array(), $Array( |
1507 "join", getFunction("join", ArrayJoin), | 1568 "join", getFunction("join", ArrayJoin), |
1508 "pop", getFunction("pop", ArrayPop), | 1569 "pop", getFunction("pop", ArrayPop), |
1509 "push", getFunction("push", ArrayPush) | 1570 "push", getFunction("push", ArrayPush) |
1510 )); | 1571 )); |
1511 } | 1572 } |
1512 | 1573 |
1513 SetUpArray(); | 1574 SetUpArray(); |
OLD | NEW |