Commit a895f01a authored by Frank Tang's avatar Frank Tang Committed by Commit Bot

[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: 's avatarJungshik Shin <jshin@chromium.org>
Reviewed-by: 's avatarSathya Gunasekaran <gsathya@chromium.org>
Cr-Commit-Position: refs/heads/master@{#54741}
parent 51ad234f
......@@ -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);
......
......@@ -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 )
......
......@@ -1452,19 +1452,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.
*/
......@@ -2095,25 +2082,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.
*/
......
......@@ -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);
......@@ -1986,5 +1986,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
......@@ -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);
......@@ -327,6 +327,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
......
......@@ -229,23 +229,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());
......
......@@ -219,7 +219,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) \
......
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