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}
This commit is contained in:
neis 2016-07-12 10:32:32 -07:00 committed by Commit bot
parent 9b9f885e99
commit 457c02573c
4 changed files with 41 additions and 35 deletions

View File

@ -151,8 +151,7 @@ function DoJoin(array, length, is_array, separator, convert) {
// 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 @@ function DoJoin(array, length, is_array, separator, convert) {
// 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);
}

View File

@ -125,7 +125,9 @@ var la1 = [1, [2, 3], 4];
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 @@ for (var i = 0; i < 3; i++) {
}
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;
})();

View File

@ -83,4 +83,17 @@ for (var constructor of typedArrayConstructors) {
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;
})();
}

View File

@ -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],