Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(33)

Unified Diff: src/js/array.js

Issue 2164923002: Make toLocaleString on arrays always call toLocaleString on its elements. (Closed) Base URL: https://chromium.googlesource.com/v8/v8.git@master
Patch Set: Created 4 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « no previous file | test/mjsunit/array-tostring.js » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: src/js/array.js
diff --git a/src/js/array.js b/src/js/array.js
index c29b8f7256ad1d2c568f5a5103d4c666f4aaf485..c28f96e8d94eaf3b0553a3abbb2d513804c63808 100644
--- a/src/js/array.js
+++ b/src/js/array.js
@@ -50,9 +50,9 @@ function KeySortCompare(a, b) {
function GetSortedArrayKeys(array, indices) {
if (IS_NUMBER(indices)) {
- var keys = new InternalArray();
// It's an interval
var limit = indices;
+ var keys = new InternalArray();
for (var i = 0; i < limit; ++i) {
var e = array[i];
if (!IS_UNDEFINED(e) || i in array) {
@@ -65,26 +65,24 @@ function GetSortedArrayKeys(array, indices) {
}
-function SparseJoinWithSeparatorJS(array, keys, length, convert, separator) {
+function SparseJoinWithSeparatorJS(array, keys, length, use_locale, separator) {
var keys_length = keys.length;
var elements = new InternalArray(keys_length * 2);
for (var i = 0; i < keys_length; i++) {
var key = keys[i];
- var e = array[key];
elements[i * 2] = key;
- elements[i * 2 + 1] = IS_STRING(e) ? e : convert(e);
+ elements[i * 2 + 1] = ConvertToString(use_locale, array[key]);
}
return %SparseJoinWithSeparator(elements, length, separator);
}
// Optimized for sparse arrays if separator is ''.
-function SparseJoin(array, keys, convert) {
+function SparseJoin(array, keys, use_locale) {
var keys_length = keys.length;
var elements = new InternalArray(keys_length);
for (var i = 0; i < keys_length; i++) {
- var e = array[keys[i]];
- elements[i] = IS_STRING(e) ? e : convert(e);
+ elements[i] = ConvertToString(use_locale, array[keys[i]]);
}
return %StringBuilderConcat(elements, keys_length, '');
}
@@ -137,60 +135,38 @@ function StackHas(stack, v) {
// join invocations.
var visited_arrays = new Stack();
-function DoJoin(array, length, is_array, separator, convert) {
+function DoJoin(array, length, is_array, separator, use_locale) {
if (UseSparseVariant(array, length, is_array, length)) {
%NormalizeElements(array);
var keys = GetSortedArrayKeys(array, %GetArrayKeys(array, length));
if (separator === '') {
if (keys.length === 0) return '';
- return SparseJoin(array, keys, convert);
+ return SparseJoin(array, keys, use_locale);
} else {
- return SparseJoinWithSeparatorJS(array, keys, length, convert, separator);
+ return SparseJoinWithSeparatorJS(
+ array, keys, length, use_locale, separator);
}
}
// Fast case for one-element arrays.
if (length === 1) {
- var e = array[0];
- return IS_STRING(e) ? e : convert(e);
+ return ConvertToString(use_locale, array[0]);
}
// Construct an array for the elements.
var elements = new InternalArray(length);
+ for (var i = 0; i < length; i++) {
+ elements[i] = ConvertToString(use_locale, array[i]);
+ }
- // We pull the empty separator check outside the loop for speed!
if (separator === '') {
- for (var i = 0; i < length; i++) {
- var e = array[i];
- elements[i] = IS_STRING(e) ? e : convert(e);
- }
return %StringBuilderConcat(elements, length, '');
- }
- // Non-empty separator case.
- // If the first element is a number then use the heuristic that the
- // remaining elements are also likely to be numbers.
- var e = array[0];
- if (IS_NUMBER(e)) {
- elements[0] = %_NumberToString(e);
- for (var i = 1; i < length; i++) {
- e = array[i];
- if (IS_NUMBER(e)) {
- elements[i] = %_NumberToString(e);
- } else {
- elements[i] = IS_STRING(e) ? e : convert(e);
- }
- }
} else {
- elements[0] = IS_STRING(e) ? e : convert(e);
- for (var i = 1; i < length; i++) {
- e = array[i];
- elements[i] = IS_STRING(e) ? e : convert(e);
- }
+ return %StringBuilderJoin(elements, length, separator);
}
- return %StringBuilderJoin(elements, length, separator);
}
-function Join(array, length, separator, convert) {
+function Join(array, length, separator, use_locale) {
if (length === 0) return '';
var is_array = IS_ARRAY(array);
@@ -204,7 +180,7 @@ function Join(array, length, separator, convert) {
// Attempt to convert the elements.
try {
- return DoJoin(array, length, is_array, separator, convert);
+ return DoJoin(array, length, is_array, separator, use_locale);
} finally {
// Make sure to remove the last element of the visited array no
// matter what happens.
@@ -213,15 +189,9 @@ function Join(array, length, separator, convert) {
}
-function ConvertToString(x) {
+function ConvertToString(use_locale, x) {
if (IS_NULL_OR_UNDEFINED(x)) return '';
- return TO_STRING(x);
-}
-
-
-function ConvertToLocaleString(e) {
- if (IS_NULL_OR_UNDEFINED(e)) return '';
- return TO_STRING(e.toLocaleString());
+ return TO_STRING(use_locale ? x.toLocaleString() : x);
}
@@ -368,7 +338,7 @@ function ArrayToString() {
if (IS_ARRAY(this)) {
func = this.join;
if (func === ArrayJoin) {
- return Join(this, this.length, ',', ConvertToString);
+ return Join(this, this.length, ',', false);
}
array = this;
} else {
@@ -383,9 +353,7 @@ function ArrayToString() {
function InnerArrayToLocaleString(array, length) {
- var len = TO_LENGTH(length);
- if (len === 0) return "";
- return Join(array, len, ',', ConvertToLocaleString);
+ return Join(array, TO_LENGTH(length), ',', true);
}
@@ -410,7 +378,7 @@ function InnerArrayJoin(separator, array, length) {
return TO_STRING(e);
}
- return Join(array, length, separator, ConvertToString);
+ return Join(array, length, separator, false);
}
« no previous file with comments | « no previous file | test/mjsunit/array-tostring.js » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698