Commit 02fe0c24 authored by Frank Tang's avatar Frank Tang Committed by Commit Bot

[Intl] Plumb through locale and options from Array#toLocaleString

Use the given locale and options when performing toLocaleString on each
individual element in a given array.

Bug: v8:7832
Cq-Include-Trybots: luci.v8.try:v8_linux_noi18n_rel_ng
Change-Id: I718a33c42e85819065599ee6bad59fb25afa7e15
Reviewed-on: https://chromium-review.googlesource.com/1132464
Commit-Queue: Frank Tang <ftang@chromium.org>
Reviewed-by: 's avatarSathya Gunasekaran <gsathya@chromium.org>
Cr-Commit-Position: refs/heads/master@{#54474}
parent 5dee5ade
......@@ -58,24 +58,26 @@ function GetSortedArrayKeys(array, indices) {
}
function SparseJoinWithSeparatorJS(array, keys, length, use_locale, separator) {
function SparseJoinWithSeparatorJS(
array, keys, length, use_locale, separator, locales, options) {
var keys_length = keys.length;
var elements = new InternalArray(keys_length * 2);
for (var i = 0; i < keys_length; i++) {
var key = keys[i];
elements[i * 2] = key;
elements[i * 2 + 1] = ConvertToString(use_locale, array[key]);
elements[i * 2 + 1] = ConvertToString(
use_locale, array[key], locales, options);
}
return %SparseJoinWithSeparator(elements, length, separator);
}
// Optimized for sparse arrays if separator is ''.
function SparseJoin(array, keys, use_locale) {
function SparseJoin(array, keys, use_locale, locales, options) {
var keys_length = keys.length;
var elements = new InternalArray(keys_length);
for (var i = 0; i < keys_length; i++) {
elements[i] = ConvertToString(use_locale, array[keys[i]]);
elements[i] = ConvertToString(use_locale, array[keys[i]], locales, options);
}
return %StringBuilderConcat(elements, keys_length, '');
}
......@@ -128,28 +130,29 @@ function StackHas(stack, v) {
// join invocations.
var visited_arrays = new Stack();
function DoJoin(array, length, is_array, separator, use_locale) {
function DoJoin(
array, length, is_array, separator, use_locale, locales, options) {
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, use_locale);
return SparseJoin(array, keys, use_locale, locales, options);
} else {
return SparseJoinWithSeparatorJS(
array, keys, length, use_locale, separator);
array, keys, length, use_locale, separator, locales, options);
}
}
// Fast case for one-element arrays.
if (length === 1) {
return ConvertToString(use_locale, array[0]);
return ConvertToString(use_locale, array[0], locales, options);
}
// 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]);
elements[i] = ConvertToString(use_locale, array[i], locales, options);
}
if (separator === '') {
......@@ -159,7 +162,7 @@ function DoJoin(array, length, is_array, separator, use_locale) {
}
}
function Join(array, length, separator, use_locale) {
function Join(array, length, separator, use_locale, locales, options) {
if (length === 0) return '';
var is_array = IS_ARRAY(array);
......@@ -173,7 +176,8 @@ function Join(array, length, separator, use_locale) {
// Attempt to convert the elements.
try {
return DoJoin(array, length, is_array, separator, use_locale);
return DoJoin(
array, length, is_array, separator, use_locale, locales, options);
} finally {
// Make sure to remove the last element of the visited array no
// matter what happens.
......@@ -182,9 +186,18 @@ function Join(array, length, separator, use_locale) {
}
function ConvertToString(use_locale, x) {
function ConvertToString(use_locale, x, locales, options) {
if (IS_NULL_OR_UNDEFINED(x)) return '';
return TO_STRING(use_locale ? x.toLocaleString() : x);
if (use_locale) {
if (IS_NULL_OR_UNDEFINED(locales)) {
return TO_STRING(x.toLocaleString());
} else if (IS_NULL_OR_UNDEFINED(options)) {
return TO_STRING(x.toLocaleString(locales));
}
return TO_STRING(x.toLocaleString(locales, options));
}
return TO_STRING(x);
}
......@@ -347,17 +360,21 @@ DEFINE_METHOD(
}
);
function InnerArrayToLocaleString(array, length) {
return Join(array, TO_LENGTH(length), ',', true);
// ecma402 #sup-array.prototype.tolocalestring
function InnerArrayToLocaleString(array, length, locales, options) {
return Join(array, TO_LENGTH(length), ',', true, locales, options);
}
DEFINE_METHOD(
GlobalArray.prototype,
// ecma402 #sup-array.prototype.tolocalestring
toLocaleString() {
var array = TO_OBJECT(this);
var arrayLen = array.length;
return InnerArrayToLocaleString(array, arrayLen);
var locales = arguments[0];
var options = arguments[1];
return InnerArrayToLocaleString(array, arrayLen, locales, options);
}
);
......
......@@ -59,14 +59,16 @@ function ValidateTypedArray(array, methodName) {
// ES6 section 22.2.3.27
// ecma402 #sup-array.prototype.tolocalestring
DEFINE_METHOD(
GlobalTypedArray.prototype,
toLocaleString() {
ValidateTypedArray(this, "%TypedArray%.prototype.toLocaleString");
var locales = arguments[0];
var options = arguments[1];
var length = %_TypedArrayGetLength(this);
return InnerArrayToLocaleString(this, length);
return InnerArrayToLocaleString(this, length, locales, options);
}
);
......
......@@ -512,10 +512,6 @@
'language/expressions/async-generator/generator-created-after-decl-inst': [FAIL],
'language/statements/async-generator/generator-created-after-decl-inst': [FAIL],
# https://bugs.chromium.org/p/v8/issues/detail?id=7832
'intl402/Array/prototype/toLocaleString/calls-toLocaleString-number-elements': [FAIL],
'intl402/TypedArray/prototype/toLocaleString/calls-toLocaleString-number-elements': [FAIL],
######################## NEEDS INVESTIGATION ###########################
# These test failures are specific to the intl402 suite and need investigation
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment