| 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 613 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 624 end_i += len; | 624 end_i += len; |
| 625 if (end_i < 0) end_i = 0; | 625 if (end_i < 0) end_i = 0; |
| 626 } else { | 626 } else { |
| 627 if (end_i > len) end_i = len; | 627 if (end_i > len) end_i = len; |
| 628 } | 628 } |
| 629 | 629 |
| 630 var result = []; | 630 var result = []; |
| 631 | 631 |
| 632 if (end_i < start_i) return result; | 632 if (end_i < start_i) return result; |
| 633 | 633 |
| 634 if (IS_ARRAY(this)) { | 634 if (IS_ARRAY(this) && |
| 635 (end_i > 1000) && |
| 636 (%EstimateNumberOfElements(this) < end_i)) { |
| 635 SmartSlice(this, start_i, end_i - start_i, len, result); | 637 SmartSlice(this, start_i, end_i - start_i, len, result); |
| 636 } else { | 638 } else { |
| 637 SimpleSlice(this, start_i, end_i - start_i, len, result); | 639 SimpleSlice(this, start_i, end_i - start_i, len, result); |
| 638 } | 640 } |
| 639 | 641 |
| 640 result.length = end_i - start_i; | 642 result.length = end_i - start_i; |
| 641 | 643 |
| 642 return result; | 644 return result; |
| 643 } | 645 } |
| 644 | 646 |
| (...skipping 88 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 733 if (x === y) return 0; | 735 if (x === y) return 0; |
| 734 if (%_IsSmi(x) && %_IsSmi(y)) { | 736 if (%_IsSmi(x) && %_IsSmi(y)) { |
| 735 return %SmiLexicographicCompare(x, y); | 737 return %SmiLexicographicCompare(x, y); |
| 736 } | 738 } |
| 737 x = ToString(x); | 739 x = ToString(x); |
| 738 y = ToString(y); | 740 y = ToString(y); |
| 739 if (x == y) return 0; | 741 if (x == y) return 0; |
| 740 else return x < y ? -1 : 1; | 742 else return x < y ? -1 : 1; |
| 741 }; | 743 }; |
| 742 } | 744 } |
| 743 var global_receiver = %GetGlobalReceiver(); | 745 var receiver = |
| 746 %_IsNativeOrStrictMode(comparefn) ? void 0 : %GetGlobalReceiver(); |
| 744 | 747 |
| 745 function InsertionSort(a, from, to) { | 748 function InsertionSort(a, from, to) { |
| 746 for (var i = from + 1; i < to; i++) { | 749 for (var i = from + 1; i < to; i++) { |
| 747 var element = a[i]; | 750 var element = a[i]; |
| 748 for (var j = i - 1; j >= from; j--) { | 751 for (var j = i - 1; j >= from; j--) { |
| 749 var tmp = a[j]; | 752 var tmp = a[j]; |
| 750 var order = %_CallFunction(global_receiver, tmp, element, comparefn); | 753 var order = %_CallFunction(receiver, tmp, element, comparefn); |
| 751 if (order > 0) { | 754 if (order > 0) { |
| 752 a[j + 1] = tmp; | 755 a[j + 1] = tmp; |
| 753 } else { | 756 } else { |
| 754 break; | 757 break; |
| 755 } | 758 } |
| 756 } | 759 } |
| 757 a[j + 1] = element; | 760 a[j + 1] = element; |
| 758 } | 761 } |
| 759 } | 762 } |
| 760 | 763 |
| 761 function QuickSort(a, from, to) { | 764 function QuickSort(a, from, to) { |
| 762 // Insertion sort is faster for short arrays. | 765 // Insertion sort is faster for short arrays. |
| 763 if (to - from <= 10) { | 766 if (to - from <= 10) { |
| 764 InsertionSort(a, from, to); | 767 InsertionSort(a, from, to); |
| 765 return; | 768 return; |
| 766 } | 769 } |
| 767 // Find a pivot as the median of first, last and middle element. | 770 // Find a pivot as the median of first, last and middle element. |
| 768 var v0 = a[from]; | 771 var v0 = a[from]; |
| 769 var v1 = a[to - 1]; | 772 var v1 = a[to - 1]; |
| 770 var middle_index = from + ((to - from) >> 1); | 773 var middle_index = from + ((to - from) >> 1); |
| 771 var v2 = a[middle_index]; | 774 var v2 = a[middle_index]; |
| 772 var c01 = %_CallFunction(global_receiver, v0, v1, comparefn); | 775 var c01 = %_CallFunction(receiver, v0, v1, comparefn); |
| 773 if (c01 > 0) { | 776 if (c01 > 0) { |
| 774 // v1 < v0, so swap them. | 777 // v1 < v0, so swap them. |
| 775 var tmp = v0; | 778 var tmp = v0; |
| 776 v0 = v1; | 779 v0 = v1; |
| 777 v1 = tmp; | 780 v1 = tmp; |
| 778 } // v0 <= v1. | 781 } // v0 <= v1. |
| 779 var c02 = %_CallFunction(global_receiver, v0, v2, comparefn); | 782 var c02 = %_CallFunction(receiver, v0, v2, comparefn); |
| 780 if (c02 >= 0) { | 783 if (c02 >= 0) { |
| 781 // v2 <= v0 <= v1. | 784 // v2 <= v0 <= v1. |
| 782 var tmp = v0; | 785 var tmp = v0; |
| 783 v0 = v2; | 786 v0 = v2; |
| 784 v2 = v1; | 787 v2 = v1; |
| 785 v1 = tmp; | 788 v1 = tmp; |
| 786 } else { | 789 } else { |
| 787 // v0 <= v1 && v0 < v2 | 790 // v0 <= v1 && v0 < v2 |
| 788 var c12 = %_CallFunction(global_receiver, v1, v2, comparefn); | 791 var c12 = %_CallFunction(receiver, v1, v2, comparefn); |
| 789 if (c12 > 0) { | 792 if (c12 > 0) { |
| 790 // v0 <= v2 < v1 | 793 // v0 <= v2 < v1 |
| 791 var tmp = v1; | 794 var tmp = v1; |
| 792 v1 = v2; | 795 v1 = v2; |
| 793 v2 = tmp; | 796 v2 = tmp; |
| 794 } | 797 } |
| 795 } | 798 } |
| 796 // v0 <= v1 <= v2 | 799 // v0 <= v1 <= v2 |
| 797 a[from] = v0; | 800 a[from] = v0; |
| 798 a[to - 1] = v2; | 801 a[to - 1] = v2; |
| 799 var pivot = v1; | 802 var pivot = v1; |
| 800 var low_end = from + 1; // Upper bound of elements lower than pivot. | 803 var low_end = from + 1; // Upper bound of elements lower than pivot. |
| 801 var high_start = to - 1; // Lower bound of elements greater than pivot. | 804 var high_start = to - 1; // Lower bound of elements greater than pivot. |
| 802 a[middle_index] = a[low_end]; | 805 a[middle_index] = a[low_end]; |
| 803 a[low_end] = pivot; | 806 a[low_end] = pivot; |
| 804 | 807 |
| 805 // From low_end to i are elements equal to pivot. | 808 // From low_end to i are elements equal to pivot. |
| 806 // From i to high_start are elements that haven't been compared yet. | 809 // From i to high_start are elements that haven't been compared yet. |
| 807 partition: for (var i = low_end + 1; i < high_start; i++) { | 810 partition: for (var i = low_end + 1; i < high_start; i++) { |
| 808 var element = a[i]; | 811 var element = a[i]; |
| 809 var order = %_CallFunction(global_receiver, element, pivot, comparefn); | 812 var order = %_CallFunction(receiver, element, pivot, comparefn); |
| 810 if (order < 0) { | 813 if (order < 0) { |
| 811 %_SwapElements(a, i, low_end); | 814 %_SwapElements(a, i, low_end); |
| 812 low_end++; | 815 low_end++; |
| 813 } else if (order > 0) { | 816 } else if (order > 0) { |
| 814 do { | 817 do { |
| 815 high_start--; | 818 high_start--; |
| 816 if (high_start == i) break partition; | 819 if (high_start == i) break partition; |
| 817 var top_elem = a[high_start]; | 820 var top_elem = a[high_start]; |
| 818 order = %_CallFunction(global_receiver, top_elem, pivot, comparefn); | 821 order = %_CallFunction(receiver, top_elem, pivot, comparefn); |
| 819 } while (order > 0); | 822 } while (order > 0); |
| 820 %_SwapElements(a, i, high_start); | 823 %_SwapElements(a, i, high_start); |
| 821 if (order < 0) { | 824 if (order < 0) { |
| 822 %_SwapElements(a, i, low_end); | 825 %_SwapElements(a, i, low_end); |
| 823 low_end++; | 826 low_end++; |
| 824 } | 827 } |
| 825 } | 828 } |
| 826 } | 829 } |
| 827 QuickSort(a, from, low_end); | 830 QuickSort(a, from, low_end); |
| 828 QuickSort(a, high_start, to); | 831 QuickSort(a, high_start, to); |
| (...skipping 414 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1243 i++; | 1246 i++; |
| 1244 break find_initial; | 1247 break find_initial; |
| 1245 } | 1248 } |
| 1246 } | 1249 } |
| 1247 throw MakeTypeError('reduce_no_initial', []); | 1250 throw MakeTypeError('reduce_no_initial', []); |
| 1248 } | 1251 } |
| 1249 | 1252 |
| 1250 for (; i < length; i++) { | 1253 for (; i < length; i++) { |
| 1251 var element = this[i]; | 1254 var element = this[i]; |
| 1252 if (!IS_UNDEFINED(element) || i in this) { | 1255 if (!IS_UNDEFINED(element) || i in this) { |
| 1253 current = callback.call(null, current, element, i, this); | 1256 current = callback.call(void 0, current, element, i, this); |
| 1254 } | 1257 } |
| 1255 } | 1258 } |
| 1256 return current; | 1259 return current; |
| 1257 } | 1260 } |
| 1258 | 1261 |
| 1259 function ArrayReduceRight(callback, current) { | 1262 function ArrayReduceRight(callback, current) { |
| 1260 if (IS_NULL_OR_UNDEFINED(this) && !IS_UNDETECTABLE(this)) { | 1263 if (IS_NULL_OR_UNDEFINED(this) && !IS_UNDETECTABLE(this)) { |
| 1261 throw MakeTypeError("called_on_null_or_undefined", | 1264 throw MakeTypeError("called_on_null_or_undefined", |
| 1262 ["Array.prototype.reduceRight"]); | 1265 ["Array.prototype.reduceRight"]); |
| 1263 } | 1266 } |
| (...skipping 10 matching lines...) Expand all Loading... |
| 1274 i--; | 1277 i--; |
| 1275 break find_initial; | 1278 break find_initial; |
| 1276 } | 1279 } |
| 1277 } | 1280 } |
| 1278 throw MakeTypeError('reduce_no_initial', []); | 1281 throw MakeTypeError('reduce_no_initial', []); |
| 1279 } | 1282 } |
| 1280 | 1283 |
| 1281 for (; i >= 0; i--) { | 1284 for (; i >= 0; i--) { |
| 1282 var element = this[i]; | 1285 var element = this[i]; |
| 1283 if (!IS_UNDEFINED(element) || i in this) { | 1286 if (!IS_UNDEFINED(element) || i in this) { |
| 1284 current = callback.call(null, current, element, i, this); | 1287 current = callback.call(void 0, current, element, i, this); |
| 1285 } | 1288 } |
| 1286 } | 1289 } |
| 1287 return current; | 1290 return current; |
| 1288 } | 1291 } |
| 1289 | 1292 |
| 1290 // ES5, 15.4.3.2 | 1293 // ES5, 15.4.3.2 |
| 1291 function ArrayIsArray(obj) { | 1294 function ArrayIsArray(obj) { |
| 1292 return IS_ARRAY(obj); | 1295 return IS_ARRAY(obj); |
| 1293 } | 1296 } |
| 1294 | 1297 |
| (...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1357 InternalArray.prototype.join = getFunction("join", ArrayJoin); | 1360 InternalArray.prototype.join = getFunction("join", ArrayJoin); |
| 1358 InternalArray.prototype.pop = getFunction("pop", ArrayPop); | 1361 InternalArray.prototype.pop = getFunction("pop", ArrayPop); |
| 1359 InternalArray.prototype.push = getFunction("push", ArrayPush); | 1362 InternalArray.prototype.push = getFunction("push", ArrayPush); |
| 1360 InternalArray.prototype.toString = function() { | 1363 InternalArray.prototype.toString = function() { |
| 1361 return "Internal Array, length " + this.length; | 1364 return "Internal Array, length " + this.length; |
| 1362 }; | 1365 }; |
| 1363 } | 1366 } |
| 1364 | 1367 |
| 1365 | 1368 |
| 1366 SetupArray(); | 1369 SetupArray(); |
| OLD | NEW |