Reland "[Intl] Move Number.toLocaleString from js to c++"

This reverts commit c83fae0663.

Reason for revert: the CL that required the revert of this CL was relanded.

https://chromium-review.googlesource.com/c/v8/v8/+/1154247

Original change's description:
> Revert "[Intl] Move Number.toLocaleString from js to c++"
>
> This reverts commit a895f01ab2.
>
> Reason for revert: Needed for other revert:
> https://chromium-review.googlesource.com/c/v8/v8/+/1152767
>
> Original change's description:
> > [Intl] Move Number.toLocaleString from js to c++
> >
> >
> > Bug: v8:7960
> > Cq-Include-Trybots: luci.v8.try:v8_linux_noi18n_rel_ng
> > Change-Id: I21a524b620e210cce625a1a00e68c0b687187087
> > Reviewed-on: https://chromium-review.googlesource.com/1144659
> > Commit-Queue: Frank Tang <ftang@chromium.org>
> > Reviewed-by: Jungshik Shin <jshin@chromium.org>
> > Reviewed-by: Sathya Gunasekaran <gsathya@chromium.org>
> > Cr-Commit-Position: refs/heads/master@{#54741}
>
> TBR=jshin@chromium.org,gsathya@chromium.org,bstell.chromium.org@gmail.com,ftang@chromium.org
>
> Change-Id: I060fa2834dde5e1b4cc71923cc066d97bce2a33b
> No-Presubmit: true
> No-Tree-Checks: true
> No-Try: true
> Bug: v8:7960
> Cq-Include-Trybots: luci.v8.try:v8_linux_noi18n_rel_ng
> Reviewed-on: https://chromium-review.googlesource.com/1152787
> Reviewed-by: Michael Achenbach <machenbach@chromium.org>
> Commit-Queue: Michael Achenbach <machenbach@chromium.org>
> Cr-Commit-Position: refs/heads/master@{#54746}

TBR=machenbach@chromium.org,jshin@chromium.org,gsathya@chromium.org,bstell.chromium.org@gmail.com,ftang@chromium.org

# Not skipping CQ checks because original CL landed > 1 day ago.

Bug: v8:7960
Change-Id: I7a7a67db77b9b5a181f1751a03186eb7e8b271a0
Cq-Include-Trybots: luci.v8.try:v8_linux_noi18n_rel_ng
Cq-Include-Trybots: luci.chromium.try:linux_chromium_rel_ng
Reviewed-on: https://chromium-review.googlesource.com/1154248
Commit-Queue: Jungshik Shin <jshin@chromium.org>
Reviewed-by: Jungshik Shin <jshin@chromium.org>
Cr-Commit-Position: refs/heads/master@{#54774}
This commit is contained in:
Jungshik Shin 2018-07-28 22:06:36 +00:00 committed by Commit Bot
parent ef3e20879e
commit 65d0b783e3
7 changed files with 45 additions and 53 deletions

View File

@ -1834,7 +1834,6 @@ void Genesis::InitializeGlobal(Handle<JSGlobalObject> global_object,
SimpleInstallFunction(isolate_, prototype, "valueOf",
Builtins::kNumberPrototypeValueOf, 0, true);
// Install Intl fallback functions.
SimpleInstallFunction(isolate_, prototype, "toLocaleString",
Builtins::kNumberPrototypeToLocaleString, 0, false);

View File

@ -8,6 +8,9 @@
#include "src/conversions.h"
#include "src/counters.h"
#include "src/objects-inl.h"
#ifdef V8_INTL_SUPPORT
#include "src/objects/intl-objects.h"
#endif
namespace v8 {
namespace internal {
@ -114,6 +117,7 @@ BUILTIN(NumberPrototypeToLocaleString) {
if (value->IsJSValue()) {
value = handle(Handle<JSValue>::cast(value)->value(), isolate);
}
// 1. Let x be ? thisNumberValue(this value)
if (!value->IsNumber()) {
THROW_NEW_ERROR_RETURN_FAILURE(
isolate, NewTypeError(MessageTemplate::kNotGeneric,
@ -122,8 +126,15 @@ BUILTIN(NumberPrototypeToLocaleString) {
isolate->factory()->Number_string()));
}
#ifdef V8_INTL_SUPPORT
RETURN_RESULT_OR_FAILURE(
isolate,
Intl::NumberToLocaleString(isolate, value, args.atOrUndefined(isolate, 1),
args.atOrUndefined(isolate, 2)));
#else
// Turn the {value} into a String.
return *isolate->factory()->NumberToString(value);
#endif // V8_INTL_SUPPORT
}
// ES6 section 20.1.3.5 Number.prototype.toPrecision ( precision )

View File

@ -1271,19 +1271,6 @@ DEFINE_METHOD(
}
);
/**
* Returns a String value representing the result of calling ToNumber(value)
* according to the effective locale and the formatting options of this
* NumberFormat.
*/
function formatNumber(formatter, value) {
// Spec treats -0 and +0 as 0.
var number = TO_NUMBER(value) + 0;
return %InternalNumberFormat(formatter, number);
}
/**
* Returns a string that matches LDML representation of the options object.
*/
@ -1914,25 +1901,6 @@ function cachedOrNewService(service, locales, options, defaults) {
"cached_or_new_service", cachedOrNewService
]);
/**
* Formats a Number object (this) using locale and options values.
* If locale or options are omitted, defaults are used.
*/
DEFINE_METHOD(
GlobalNumber.prototype,
toLocaleString() {
if (!(this instanceof GlobalNumber) && typeof(this) !== 'number') {
throw %make_type_error(kMethodInvokedOnWrongType, "Number");
}
var locales = arguments[0];
var options = arguments[1];
var numberFormat = cachedOrNewService('numberformat', locales, options);
return formatNumber(numberFormat, this);
}
);
/**
* Returns actual formatted date or fails if date parameter is invalid.
*/

View File

@ -1322,7 +1322,7 @@ MaybeHandle<JSObject> NumberFormat::Unwrap(Isolate* isolate,
Intl::Type::kNumberFormat, method_name_str, true);
}
MaybeHandle<Object> NumberFormat::FormatNumber(
MaybeHandle<String> NumberFormat::FormatNumber(
Isolate* isolate, Handle<JSObject> number_format_holder, double value) {
icu::DecimalFormat* number_format =
NumberFormat::UnpackNumberFormat(number_format_holder);
@ -1996,5 +1996,32 @@ Handle<Object> Intl::InternalCompare(Isolate* isolate,
return factory->NewNumberFromInt(result);
}
// ecma402/#sup-properties-of-the-number-prototype-object
MaybeHandle<String> Intl::NumberToLocaleString(Isolate* isolate,
Handle<Object> num,
Handle<Object> locales,
Handle<Object> options) {
Factory* factory = isolate->factory();
Handle<JSObject> number_format_holder;
// 2. Let numberFormat be ? Construct(%NumberFormat%, « locales, options »).
ASSIGN_RETURN_ON_EXCEPTION(
isolate, number_format_holder,
CachedOrNewService(isolate,
factory->NewStringFromStaticChars("numberformat"),
locales, options),
String);
DCHECK(
Intl::IsObjectOfType(isolate, number_format_holder, Intl::kNumberFormat));
Handle<Object> number_obj;
ASSIGN_RETURN_ON_EXCEPTION(isolate, number_obj,
Object::ToNumber(isolate, num), String);
// Spec treats -0 and +0 as 0.
double number = number_obj->Number() + 0;
// Return FormatNumber(numberFormat, x).
return NumberFormat::FormatNumber(isolate, number_format_holder, number);
}
} // namespace internal
} // namespace v8

View File

@ -81,7 +81,7 @@ class NumberFormat {
const char* method_name);
// ecm402/#sec-formatnumber
static MaybeHandle<Object> FormatNumber(Isolate* isolate,
static MaybeHandle<String> FormatNumber(Isolate* isolate,
Handle<JSObject> number_format_holder,
double value);
@ -330,6 +330,11 @@ class Intl {
V8_WARN_UNUSED_RESULT static Handle<Object> InternalCompare(
Isolate* isolate, Handle<JSObject> collator, Handle<String> s1,
Handle<String> s2);
// ecma402/#sup-properties-of-the-number-prototype-object
V8_WARN_UNUSED_RESULT static MaybeHandle<String> NumberToLocaleString(
Isolate* isolate, Handle<Object> num, Handle<Object> locales,
Handle<Object> options);
};
} // namespace internal

View File

@ -205,23 +205,6 @@ RUNTIME_FUNCTION(Runtime_CreateNumberFormat) {
isolate, Intl::CreateNumberFormat(isolate, locale, options, resolved));
}
RUNTIME_FUNCTION(Runtime_InternalNumberFormat) {
HandleScope scope(isolate);
DCHECK_EQ(2, args.length());
CONVERT_ARG_HANDLE_CHECKED(JSObject, number_format_holder, 0);
CONVERT_ARG_HANDLE_CHECKED(Object, value, 1);
Handle<Object> number_obj;
ASSIGN_RETURN_FAILURE_ON_EXCEPTION(isolate, number_obj,
Object::ToNumber(isolate, value));
double number = number_obj->Number();
RETURN_RESULT_OR_FAILURE(isolate, NumberFormat::FormatNumber(
isolate, number_format_holder, number));
}
RUNTIME_FUNCTION(Runtime_CurrencyDigits) {
HandleScope scope(isolate);
DCHECK_EQ(1, args.length());

View File

@ -220,7 +220,6 @@ namespace internal {
F(GetDefaultICULocale, 0, 1) \
F(InternalCompare, 3, 1) \
F(InternalDateFormat, 2, 1) \
F(InternalNumberFormat, 2, 1) \
F(IntlUnwrapReceiver, 5, 1) \
F(IsInitializedIntlObjectOfType, 2, 1) \
F(IsWellFormedCurrencyCode, 1, 1) \