| 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 99 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 110 | 110 |
| 111 // Attempt to convert the elements. | 111 // Attempt to convert the elements. |
| 112 try { | 112 try { |
| 113 if (UseSparseVariant(array, length, is_array) && (separator.length == 0)) { | 113 if (UseSparseVariant(array, length, is_array) && (separator.length == 0)) { |
| 114 return SparseJoin(array, length, convert); | 114 return SparseJoin(array, length, convert); |
| 115 } | 115 } |
| 116 | 116 |
| 117 // Fast case for one-element arrays. | 117 // Fast case for one-element arrays. |
| 118 if (length == 1) { | 118 if (length == 1) { |
| 119 var e = array[0]; | 119 var e = array[0]; |
| 120 if (!IS_UNDEFINED(e) || (0 in array)) { | 120 if (IS_STRING(e)) return e; |
| 121 if (IS_STRING(e)) return e; | 121 return convert(e); |
| 122 return convert(e); | |
| 123 } | |
| 124 } | 122 } |
| 125 | 123 |
| 126 // Construct an array for the elements. | 124 // Construct an array for the elements. |
| 127 var elements; | 125 var elements = new $Array(length); |
| 128 var elements_length = 0; | |
| 129 | 126 |
| 130 // We pull the empty separator check outside the loop for speed! | 127 // We pull the empty separator check outside the loop for speed! |
| 131 if (separator.length == 0) { | 128 if (separator.length == 0) { |
| 132 elements = new $Array(length); | 129 var elements_length = 0; |
| 133 for (var i = 0; i < length; i++) { | 130 for (var i = 0; i < length; i++) { |
| 134 var e = array[i]; | 131 var e = array[i]; |
| 135 if (!IS_UNDEFINED(e) || (i in array)) { | 132 if (!IS_UNDEFINED(e)) { |
| 136 if (!IS_STRING(e)) e = convert(e); | 133 if (!IS_STRING(e)) e = convert(e); |
| 137 elements[elements_length++] = e; | 134 elements[elements_length++] = e; |
| 138 } | 135 } |
| 139 } | 136 } |
| 140 } else { | 137 elements.length = elements_length; |
| 141 elements = new $Array(length << 1); | 138 var result = %_FastAsciiArrayJoin(elements, ''); |
| 139 if (!IS_UNDEFINED(result)) return result; |
| 140 return %StringBuilderConcat(elements, elements_length, ''); |
| 141 } |
| 142 // Non-empty separator case. |
| 143 // If the first element is a number then use the heuristic that the |
| 144 // remaining elements are also likely to be numbers. |
| 145 if (!IS_NUMBER(array[0])) { |
| 142 for (var i = 0; i < length; i++) { | 146 for (var i = 0; i < length; i++) { |
| 143 var e = array[i]; | 147 var e = array[i]; |
| 144 if (i != 0) elements[elements_length++] = separator; | 148 if (!IS_STRING(e)) e = convert(e); |
| 145 if (!IS_UNDEFINED(e) || (i in array)) { | 149 elements[i] = e; |
| 150 } |
| 151 } else { |
| 152 for (var i = 0; i < length; i++) { |
| 153 var e = array[i]; |
| 154 if (IS_NUMBER(e)) elements[i] = %_NumberToString(e); |
| 155 else { |
| 146 if (!IS_STRING(e)) e = convert(e); | 156 if (!IS_STRING(e)) e = convert(e); |
| 147 elements[elements_length++] = e; | 157 elements[i] = e; |
| 148 } | 158 } |
| 149 } | 159 } |
| 150 } | 160 } |
| 151 elements.length = elements_length; | 161 var result = %_FastAsciiArrayJoin(elements, separator); |
| 152 var result = %_FastAsciiArrayJoin(elements, ""); | 162 if (!IS_UNDEFINED(result)) return result; |
| 153 if (!IS_UNDEFINED(result)) return result; | 163 |
| 154 return %StringBuilderConcat(elements, elements_length, ''); | 164 return %StringBuilderJoin(elements, length, separator); |
| 155 } finally { | 165 } finally { |
| 156 // Make sure to pop the visited array no matter what happens. | 166 // Make sure to remove the last element of the visited array no |
| 157 if (is_array) visited_arrays.pop(); | 167 // matter what happens. |
| 168 if (is_array) visited_arrays.length = visited_arrays.length - 1; |
| 158 } | 169 } |
| 159 } | 170 } |
| 160 | 171 |
| 161 | 172 |
| 162 function ConvertToString(x) { | 173 function ConvertToString(x) { |
| 163 if (IS_STRING(x)) return x; | 174 // Assumes x is a non-string. |
| 164 if (IS_NUMBER(x)) return %_NumberToString(x); | 175 if (IS_NUMBER(x)) return %_NumberToString(x); |
| 165 if (IS_BOOLEAN(x)) return x ? 'true' : 'false'; | 176 if (IS_BOOLEAN(x)) return x ? 'true' : 'false'; |
| 166 return (IS_NULL_OR_UNDEFINED(x)) ? '' : %ToString(%DefaultString(x)); | 177 return (IS_NULL_OR_UNDEFINED(x)) ? '' : %ToString(%DefaultString(x)); |
| 167 } | 178 } |
| 168 | 179 |
| 169 | 180 |
| 170 function ConvertToLocaleString(e) { | 181 function ConvertToLocaleString(e) { |
| 171 if (e == null) { | 182 if (e == null) { |
| 172 return ''; | 183 return ''; |
| 173 } else { | 184 } else { |
| (...skipping 404 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 578 var start_i = TO_INTEGER(start); | 589 var start_i = TO_INTEGER(start); |
| 579 | 590 |
| 580 if (start_i < 0) { | 591 if (start_i < 0) { |
| 581 start_i += len; | 592 start_i += len; |
| 582 if (start_i < 0) start_i = 0; | 593 if (start_i < 0) start_i = 0; |
| 583 } else { | 594 } else { |
| 584 if (start_i > len) start_i = len; | 595 if (start_i > len) start_i = len; |
| 585 } | 596 } |
| 586 | 597 |
| 587 // SpiderMonkey, TraceMonkey and JSC treat the case where no delete count is | 598 // SpiderMonkey, TraceMonkey and JSC treat the case where no delete count is |
| 588 // given differently from when an undefined delete count is given. | 599 // given as a request to delete all the elements from the start. |
| 600 // And it differs from the case of undefined delete count. |
| 589 // This does not follow ECMA-262, but we do the same for | 601 // This does not follow ECMA-262, but we do the same for |
| 590 // compatibility. | 602 // compatibility. |
| 591 var del_count = 0; | 603 var del_count = 0; |
| 592 if (num_arguments > 1) { | 604 if (num_arguments == 1) { |
| 605 del_count = len - start_i; |
| 606 } else { |
| 593 del_count = TO_INTEGER(delete_count); | 607 del_count = TO_INTEGER(delete_count); |
| 594 if (del_count < 0) del_count = 0; | 608 if (del_count < 0) del_count = 0; |
| 595 if (del_count > len - start_i) del_count = len - start_i; | 609 if (del_count > len - start_i) del_count = len - start_i; |
| 596 } else { | |
| 597 del_count = len - start_i; | |
| 598 } | 610 } |
| 599 | 611 |
| 600 var deleted_elements = []; | 612 var deleted_elements = []; |
| 601 deleted_elements.length = del_count; | 613 deleted_elements.length = del_count; |
| 602 | 614 |
| 603 // Number of elements to add. | 615 // Number of elements to add. |
| 604 var num_additional_args = 0; | 616 var num_additional_args = 0; |
| 605 if (num_arguments > 2) { | 617 if (num_arguments > 2) { |
| 606 num_additional_args = num_arguments - 2; | 618 num_additional_args = num_arguments - 2; |
| 607 } | 619 } |
| (...skipping 383 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 991 | 1003 |
| 992 | 1004 |
| 993 function ArrayIndexOf(element, index) { | 1005 function ArrayIndexOf(element, index) { |
| 994 var length = TO_UINT32(this.length); | 1006 var length = TO_UINT32(this.length); |
| 995 if (length == 0) return -1; | 1007 if (length == 0) return -1; |
| 996 if (IS_UNDEFINED(index)) { | 1008 if (IS_UNDEFINED(index)) { |
| 997 index = 0; | 1009 index = 0; |
| 998 } else { | 1010 } else { |
| 999 index = TO_INTEGER(index); | 1011 index = TO_INTEGER(index); |
| 1000 // If index is negative, index from the end of the array. | 1012 // If index is negative, index from the end of the array. |
| 1001 if (index < 0) index = length + index; | 1013 if (index < 0) { |
| 1002 // If index is still negative, search the entire array. | 1014 index = length + index; |
| 1003 if (index < 0) index = 0; | 1015 // If index is still negative, search the entire array. |
| 1016 if (index < 0) index = 0; |
| 1017 } |
| 1004 } | 1018 } |
| 1005 var min = index; | 1019 var min = index; |
| 1006 var max = length; | 1020 var max = length; |
| 1007 if (UseSparseVariant(this, length, true)) { | 1021 if (UseSparseVariant(this, length, true)) { |
| 1008 var intervals = %GetArrayKeys(this, length); | 1022 var intervals = %GetArrayKeys(this, length); |
| 1009 if (intervals.length == 2 && intervals[0] < 0) { | 1023 if (intervals.length == 2 && intervals[0] < 0) { |
| 1010 // A single interval. | 1024 // A single interval. |
| 1011 var intervalMin = -(intervals[0] + 1); | 1025 var intervalMin = -(intervals[0] + 1); |
| 1012 var intervalMax = intervalMin + intervals[1]; | 1026 var intervalMax = intervalMin + intervals[1]; |
| 1013 min = MAX(min, intervalMin); | 1027 min = MAX(min, intervalMin); |
| (...skipping 195 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1209 "lastIndexOf", getFunction("lastIndexOf", ArrayLastIndexOf, 1), | 1223 "lastIndexOf", getFunction("lastIndexOf", ArrayLastIndexOf, 1), |
| 1210 "reduce", getFunction("reduce", ArrayReduce, 1), | 1224 "reduce", getFunction("reduce", ArrayReduce, 1), |
| 1211 "reduceRight", getFunction("reduceRight", ArrayReduceRight, 1) | 1225 "reduceRight", getFunction("reduceRight", ArrayReduceRight, 1) |
| 1212 )); | 1226 )); |
| 1213 | 1227 |
| 1214 %FinishArrayPrototypeSetup($Array.prototype); | 1228 %FinishArrayPrototypeSetup($Array.prototype); |
| 1215 } | 1229 } |
| 1216 | 1230 |
| 1217 | 1231 |
| 1218 SetupArray(); | 1232 SetupArray(); |
| OLD | NEW |