| 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 } |
| 160 } |
| 161 var result = %_FastAsciiArrayJoin(elements, separator); |
| 162 if (!IS_UNDEFINED(result)) return result; |
| 163 |
| 164 var length2 = (length << 1) - 1; |
| 165 var j = length2; |
| 166 var i = length; |
| 167 elements[--j] = elements[--i]; |
| 168 while (i > 0) { |
| 169 elements[--j] = separator; |
| 170 elements[--j] = elements[--i]; |
| 150 } | 171 } |
| 151 elements.length = elements_length; | 172 return %StringBuilderConcat(elements, length2, ''); |
| 152 var result = %_FastAsciiArrayJoin(elements, ""); | |
| 153 if (!IS_UNDEFINED(result)) return result; | |
| 154 return %StringBuilderConcat(elements, elements_length, ''); | |
| 155 } finally { | 173 } finally { |
| 156 // Make sure to pop the visited array no matter what happens. | 174 // Make sure to pop the visited array no matter what happens. |
| 157 if (is_array) visited_arrays.pop(); | 175 if (is_array) visited_arrays.pop(); |
| 158 } | 176 } |
| 159 } | 177 } |
| 160 | 178 |
| 161 | 179 |
| 162 function ConvertToString(x) { | 180 function ConvertToString(x) { |
| 163 if (IS_STRING(x)) return x; | 181 // Assumes x is a non-string. |
| 164 if (IS_NUMBER(x)) return %_NumberToString(x); | 182 if (IS_NUMBER(x)) return %_NumberToString(x); |
| 165 if (IS_BOOLEAN(x)) return x ? 'true' : 'false'; | 183 if (IS_BOOLEAN(x)) return x ? 'true' : 'false'; |
| 166 return (IS_NULL_OR_UNDEFINED(x)) ? '' : %ToString(%DefaultString(x)); | 184 return (IS_NULL_OR_UNDEFINED(x)) ? '' : %ToString(%DefaultString(x)); |
| 167 } | 185 } |
| 168 | 186 |
| 169 | 187 |
| 170 function ConvertToLocaleString(e) { | 188 function ConvertToLocaleString(e) { |
| 171 if (e == null) { | 189 if (e == null) { |
| 172 return ''; | 190 return ''; |
| 173 } else { | 191 } else { |
| (...skipping 1035 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1209 "lastIndexOf", getFunction("lastIndexOf", ArrayLastIndexOf, 1), | 1227 "lastIndexOf", getFunction("lastIndexOf", ArrayLastIndexOf, 1), |
| 1210 "reduce", getFunction("reduce", ArrayReduce, 1), | 1228 "reduce", getFunction("reduce", ArrayReduce, 1), |
| 1211 "reduceRight", getFunction("reduceRight", ArrayReduceRight, 1) | 1229 "reduceRight", getFunction("reduceRight", ArrayReduceRight, 1) |
| 1212 )); | 1230 )); |
| 1213 | 1231 |
| 1214 %FinishArrayPrototypeSetup($Array.prototype); | 1232 %FinishArrayPrototypeSetup($Array.prototype); |
| 1215 } | 1233 } |
| 1216 | 1234 |
| 1217 | 1235 |
| 1218 SetupArray(); | 1236 SetupArray(); |
| OLD | NEW |