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 (function(global, utils, extrasUtils) { | 5 (function(global, utils, extrasUtils) { |
6 | 6 |
7 "use strict"; | 7 "use strict"; |
8 | 8 |
9 %CheckIsBootstrapping(); | 9 %CheckIsBootstrapping(); |
10 | 10 |
(...skipping 133 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
144 if (separator === '') { | 144 if (separator === '') { |
145 if (keys.length === 0) return ''; | 145 if (keys.length === 0) return ''; |
146 return SparseJoin(array, keys, convert); | 146 return SparseJoin(array, keys, convert); |
147 } else { | 147 } else { |
148 return SparseJoinWithSeparatorJS(array, keys, length, convert, separator); | 148 return SparseJoinWithSeparatorJS(array, keys, length, convert, separator); |
149 } | 149 } |
150 } | 150 } |
151 | 151 |
152 // Fast case for one-element arrays. | 152 // Fast case for one-element arrays. |
153 if (length === 1) { | 153 if (length === 1) { |
154 var e = array[0]; | 154 return convert(array[0]); |
155 return IS_STRING(e) ? e : convert(e); | |
156 } | 155 } |
157 | 156 |
158 // Construct an array for the elements. | 157 // Construct an array for the elements. |
159 var elements = new InternalArray(length); | 158 var elements = new InternalArray(length); |
160 | 159 |
161 // We pull the empty separator check outside the loop for speed! | 160 // We pull the empty separator check outside the loop for speed! |
162 if (separator === '') { | 161 if (separator === '') { |
163 for (var i = 0; i < length; i++) { | 162 for (var i = 0; i < length; i++) { |
164 var e = array[i]; | 163 elements[i] = convert(array[i]); |
165 elements[i] = IS_STRING(e) ? e : convert(e); | |
166 } | 164 } |
167 return %StringBuilderConcat(elements, length, ''); | 165 return %StringBuilderConcat(elements, length, ''); |
168 } | 166 } |
169 // Non-empty separator case. | 167 // Non-empty separator case. |
170 // If the first element is a number then use the heuristic that the | 168 elements[0] = convert(array[0]); |
171 // remaining elements are also likely to be numbers. | 169 for (var i = 1; i < length; i++) { |
172 var e = array[0]; | 170 elements[i] = convert(array[i]); |
173 if (IS_NUMBER(e)) { | |
174 elements[0] = %_NumberToString(e); | |
175 for (var i = 1; i < length; i++) { | |
176 e = array[i]; | |
177 if (IS_NUMBER(e)) { | |
178 elements[i] = %_NumberToString(e); | |
179 } else { | |
180 elements[i] = IS_STRING(e) ? e : convert(e); | |
181 } | |
182 } | |
183 } else { | |
184 elements[0] = IS_STRING(e) ? e : convert(e); | |
185 for (var i = 1; i < length; i++) { | |
186 e = array[i]; | |
187 elements[i] = IS_STRING(e) ? e : convert(e); | |
188 } | |
189 } | 171 } |
190 return %StringBuilderJoin(elements, length, separator); | 172 return %StringBuilderJoin(elements, length, separator); |
191 } | 173 } |
192 | 174 |
193 function Join(array, length, separator, convert) { | 175 function Join(array, length, separator, convert) { |
194 if (length === 0) return ''; | 176 if (length === 0) return ''; |
195 | 177 |
196 var is_array = IS_ARRAY(array); | 178 var is_array = IS_ARRAY(array); |
197 | 179 |
198 if (is_array) { | 180 if (is_array) { |
(...skipping 1579 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1778 %InstallToContext([ | 1760 %InstallToContext([ |
1779 "array_pop", ArrayPop, | 1761 "array_pop", ArrayPop, |
1780 "array_push", ArrayPush, | 1762 "array_push", ArrayPush, |
1781 "array_shift", ArrayShift, | 1763 "array_shift", ArrayShift, |
1782 "array_splice", ArraySplice, | 1764 "array_splice", ArraySplice, |
1783 "array_slice", ArraySlice, | 1765 "array_slice", ArraySlice, |
1784 "array_unshift", ArrayUnshift, | 1766 "array_unshift", ArrayUnshift, |
1785 ]); | 1767 ]); |
1786 | 1768 |
1787 }); | 1769 }); |
OLD | NEW |