Chromium Code Reviews| 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 107 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 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_UNDEFINED(e) || (0 in array)) { |
| 121 if (IS_STRING(e)) return e; | 121 if (IS_STRING(e)) return e; |
| 122 return convert(e); | 122 return convert(e); |
| 123 } | 123 } |
| 124 return ''; | 124 return ''; |
| 125 } | 125 } |
| 126 | 126 |
| 127 // Construct an array for the elements. | 127 // Construct an array for the elements. |
| 128 var elements; | 128 var elements = new $Array(length); |
| 129 var elements_length = 0; | 129 var elements_length = 0; |
| 130 | 130 |
| 131 // We pull the empty separator check outside the loop for speed! | 131 // We pull the empty separator check outside the loop for speed! |
| 132 if (separator.length == 0) { | 132 if (separator.length == 0) { |
| 133 elements = new $Array(length); | |
| 134 for (var i = 0; i < length; i++) { | 133 for (var i = 0; i < length; i++) { |
| 135 var e = array[i]; | 134 var e = array[i]; |
| 136 if (!IS_UNDEFINED(e) || (i in array)) { | 135 if (!IS_UNDEFINED(e) || (i in array)) { |
| 137 if (!IS_STRING(e)) e = convert(e); | 136 if (!IS_STRING(e)) e = convert(e); |
| 138 elements[elements_length++] = e; | 137 elements[elements_length++] = e; |
| 139 } | 138 } |
| 140 } | 139 } |
| 141 } else { | 140 elements.length = elements_length; |
| 142 elements = new $Array(length << 1); | 141 var result = %_FastAsciiArrayJoin(elements, ''); |
| 143 for (var i = 0; i < length; i++) { | 142 if (!IS_UNDEFINED(result)) return result; |
| 144 var e = array[i]; | 143 return %StringBuilderConcat(elements, elements_length, ''); |
| 145 if (i != 0) elements[elements_length++] = separator; | 144 } |
| 146 if (!IS_UNDEFINED(e) || (i in array)) { | 145 // Non-empty separator. |
| 147 if (!IS_STRING(e)) e = convert(e); | 146 for (var i = 0; i < length; i++) { |
| 148 elements[elements_length++] = e; | 147 var e = array[i]; |
| 149 } | 148 if (!IS_UNDEFINED(e) || (i in array)) { |
|
Lasse Reichstein
2011/01/07 14:58:58
If all actually used convert functions convert und
sandholm
2011/01/08 12:49:52
Done.
| |
| 149 if (!IS_STRING(e)) e = convert(e); | |
| 150 elements[i] = e; | |
| 151 } else { | |
| 152 elements[i] = ''; | |
|
Lasse Reichstein
2011/01/07 14:58:58
Would it be slower to have a separate counter for
sandholm
2011/01/08 12:49:52
You need to know where the separator goes, hence a
| |
| 150 } | 153 } |
| 151 } | 154 } |
| 152 elements.length = elements_length; | 155 var result = %_FastAsciiArrayJoin(elements, separator); |
| 153 var result = %_FastAsciiArrayJoin(elements, ""); | 156 if (!IS_UNDEFINED(result)) return result; |
| 154 if (!IS_UNDEFINED(result)) return result; | 157 |
| 155 return %StringBuilderConcat(elements, elements_length, ''); | 158 var length2 = (length << 1) - 1; |
| 159 var elements2 = new $Array(length2); | |
| 160 elements2[0] = elements[0]; | |
| 161 for (var i = 1, j = 1; i < length; i++) { | |
| 162 elements2[j++] = separator; | |
| 163 elements2[j++] = elements[i]; | |
| 164 } | |
|
Lasse Reichstein
2011/01/07 14:58:58
If you run backwards, you could expand the array i
sandholm
2011/01/08 12:49:52
Done.
| |
| 165 return %StringBuilderConcat(elements2, length2, ''); | |
| 156 } finally { | 166 } finally { |
| 157 // Make sure to pop the visited array no matter what happens. | 167 // Make sure to pop the visited array no matter what happens. |
| 158 if (is_array) visited_arrays.pop(); | 168 if (is_array) visited_arrays.pop(); |
| 159 } | 169 } |
| 160 } | 170 } |
| 161 | 171 |
| 162 | 172 |
| 163 function ConvertToString(x) { | 173 function ConvertToString(x) { |
| 164 // Assumes x is a non-string. | 174 // Assumes x is a non-string. |
| 165 if (IS_NUMBER(x)) return %_NumberToString(x); | 175 if (IS_NUMBER(x)) return %_NumberToString(x); |
| (...skipping 1044 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1210 "lastIndexOf", getFunction("lastIndexOf", ArrayLastIndexOf, 1), | 1220 "lastIndexOf", getFunction("lastIndexOf", ArrayLastIndexOf, 1), |
| 1211 "reduce", getFunction("reduce", ArrayReduce, 1), | 1221 "reduce", getFunction("reduce", ArrayReduce, 1), |
| 1212 "reduceRight", getFunction("reduceRight", ArrayReduceRight, 1) | 1222 "reduceRight", getFunction("reduceRight", ArrayReduceRight, 1) |
| 1213 )); | 1223 )); |
| 1214 | 1224 |
| 1215 %FinishArrayPrototypeSetup($Array.prototype); | 1225 %FinishArrayPrototypeSetup($Array.prototype); |
| 1216 } | 1226 } |
| 1217 | 1227 |
| 1218 | 1228 |
| 1219 SetupArray(); | 1229 SetupArray(); |
| OLD | NEW |