Make toLocaleString on arrays always call toLocaleString on its elements.

As required by the spec.

BUG=v8:5113

Review-Url: https://codereview.chromium.org/2141603002
Cr-Commit-Position: refs/heads/master@{#37689}
diff --git a/src/js/array.js b/src/js/array.js
index c29b8f7..2fde9fb 100644
--- a/src/js/array.js
+++ b/src/js/array.js
@@ -151,8 +151,7 @@
 
   // Fast case for one-element arrays.
   if (length === 1) {
-    var e = array[0];
-    return IS_STRING(e) ? e : convert(e);
+    return convert(array[0]);
   }
 
   // Construct an array for the elements.
@@ -161,31 +160,14 @@
   // 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);
+      elements[i] = convert(array[i]);
     }
     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);
-    }
+  elements[0] = convert(array[0]);
+  for (var i = 1; i < length; i++) {
+    elements[i] = convert(array[i]);
   }
   return %StringBuilderJoin(elements, length, separator);
 }
diff --git a/test/mjsunit/array-tostring.js b/test/mjsunit/array-tostring.js
index 5be3d50..382bf8d 100644
--- a/test/mjsunit/array-tostring.js
+++ b/test/mjsunit/array-tostring.js
@@ -125,7 +125,9 @@
 assertEquals("1,2,3,4", la1.toLocaleString());
 
 // Used on a string (which looks like an array of characters).
-String.prototype.toLocaleString = Array.prototype.toLocaleString;
+String.prototype.toLocaleString = function() {
+  return (this.length == 1) ? this : Array.prototype.toLocaleString.call(this);
+}
 assertEquals("1,2,3,4", "1234".toLocaleString());
 
 // If toLocaleString of element is not callable, throw a TypeError.
@@ -157,3 +159,23 @@
 }
 Number.prototype.arrayToLocaleString = Array.prototype.toLocaleString;
 assertEquals("42,42,42", (42).arrayToLocaleString());
+
+
+(function TestToLocaleStringCalls() {
+  let log = [];
+  let pushArgs = (label) => (...args) => log.push(label, args);
+
+  let NumberToLocaleString = Number.prototype.toLocaleString;
+  let StringToLocaleString = String.prototype.toLocaleString;
+  let ObjectToLocaleString = Object.prototype.toLocaleString;
+  Number.prototype.toLocaleString = pushArgs("Number");
+  String.prototype.toLocaleString = pushArgs("String");
+  Object.prototype.toLocaleString = pushArgs("Object");
+
+  [42, "foo", {}].toLocaleString();
+  assertEquals(["Number", [], "String", [], "Object", []], log);
+
+  Number.prototype.toLocaleString = NumberToLocaleString;
+  String.prototype.toLocaleString = StringToLocaleString;
+  Object.prototype.toLocaleString = ObjectToLocaleString;
+})();
diff --git a/test/mjsunit/es6/typedarray-tostring.js b/test/mjsunit/es6/typedarray-tostring.js
index e6adda0..9d49cb1 100644
--- a/test/mjsunit/es6/typedarray-tostring.js
+++ b/test/mjsunit/es6/typedarray-tostring.js
@@ -83,4 +83,17 @@
   assertEquals("1,2", Array.prototype.join.call(a5));
   assertEquals("1,2,3", Array.prototype.toString.call(a5));
   assertEquals("1,2", Array.prototype.toLocaleString.call(a5));
+
+  (function TestToLocaleStringCalls() {
+    let log = [];
+    let pushArgs = (label) => (...args) => log.push(label, args);
+
+    let NumberToLocaleString = Number.prototype.toLocaleString;
+    Number.prototype.toLocaleString = pushArgs("Number");
+
+    (new constructor([1, 2])).toLocaleString();
+    assertEquals(["Number", [], "Number", []], log);
+
+    Number.prototype.toLocaleString = NumberToLocaleString;
+  })();
 }
diff --git a/test/test262/test262.status b/test/test262/test262.status
index 3a3d770..a19dbd5 100644
--- a/test/test262/test262.status
+++ b/test/test262/test262.status
@@ -340,17 +340,6 @@
   # https://bugs.chromium.org/p/v8/issues/detail?id=5012
   'intl402/Intl/getCanonicalLocales/*': [FAIL],
 
-  # https://bugs.chromium.org/p/v8/issues/detail?id=5113
-  'built-ins/TypedArray/prototype/toLocaleString/calls-tolocalestring-from-each-value': [FAIL],
-  'built-ins/TypedArray/prototype/toLocaleString/calls-tostring-from-each-value': [FAIL],
-  'built-ins/TypedArray/prototype/toLocaleString/calls-valueof-from-each-value': [FAIL],
-  'built-ins/TypedArray/prototype/toLocaleString/return-abrupt-from-firstelement-tolocalestring': [FAIL],
-  'built-ins/TypedArray/prototype/toLocaleString/return-abrupt-from-firstelement-tostring': [FAIL],
-  'built-ins/TypedArray/prototype/toLocaleString/return-abrupt-from-firstelement-valueof': [FAIL],
-  'built-ins/TypedArray/prototype/toLocaleString/return-abrupt-from-nextelement-tolocalestring': [FAIL],
-  'built-ins/TypedArray/prototype/toLocaleString/return-abrupt-from-nextelement-tostring': [FAIL],
-  'built-ins/TypedArray/prototype/toLocaleString/return-abrupt-from-nextelement-valueof': [FAIL],
-
   # https://bugs.chromium.org/p/v8/issues/detail?id=5115
   'language/statements/class/subclass/class-definition-null-proto-missing-return-override': [FAIL],
   'language/statements/class/subclass/class-definition-null-proto-this': [FAIL],