From c83fae0663f0360b474861d53094b4915ebf8d66 Mon Sep 17 00:00:00 2001 From: Michael Achenbach Date: Fri, 27 Jul 2018 08:52:54 +0000 Subject: [PATCH] Revert "[Intl] Move Number.toLocaleString from js to c++" This reverts commit a895f01ab2a5ddca5e22e4ab252647ea4b0bb8e5. 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 > Reviewed-by: Jungshik Shin > Reviewed-by: Sathya Gunasekaran > 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 Commit-Queue: Michael Achenbach Cr-Commit-Position: refs/heads/master@{#54746} --- src/bootstrapper.cc | 1 + src/builtins/builtins-number.cc | 11 ----------- src/js/intl.js | 32 ++++++++++++++++++++++++++++++++ src/objects/intl-objects.cc | 29 +---------------------------- src/objects/intl-objects.h | 7 +------ src/runtime/runtime-intl.cc | 17 +++++++++++++++++ src/runtime/runtime.h | 1 + 7 files changed, 53 insertions(+), 45 deletions(-) diff --git a/src/bootstrapper.cc b/src/bootstrapper.cc index 4f4d208d39..7fc293a796 100644 --- a/src/bootstrapper.cc +++ b/src/bootstrapper.cc @@ -1834,6 +1834,7 @@ void Genesis::InitializeGlobal(Handle global_object, SimpleInstallFunction(isolate_, prototype, "valueOf", Builtins::kNumberPrototypeValueOf, 0, true); + // Install Intl fallback functions. SimpleInstallFunction(isolate_, prototype, "toLocaleString", Builtins::kNumberPrototypeToLocaleString, 0, false); diff --git a/src/builtins/builtins-number.cc b/src/builtins/builtins-number.cc index 2a2eb9a478..7e701c1546 100644 --- a/src/builtins/builtins-number.cc +++ b/src/builtins/builtins-number.cc @@ -8,9 +8,6 @@ #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 { @@ -117,7 +114,6 @@ BUILTIN(NumberPrototypeToLocaleString) { if (value->IsJSValue()) { value = handle(Handle::cast(value)->value(), isolate); } - // 1. Let x be ? thisNumberValue(this value) if (!value->IsNumber()) { THROW_NEW_ERROR_RETURN_FAILURE( isolate, NewTypeError(MessageTemplate::kNotGeneric, @@ -126,15 +122,8 @@ 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 ) diff --git a/src/js/intl.js b/src/js/intl.js index 717d0ba823..7e97a8f047 100644 --- a/src/js/intl.js +++ b/src/js/intl.js @@ -1452,6 +1452,19 @@ 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. */ @@ -2082,6 +2095,25 @@ 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. */ diff --git a/src/objects/intl-objects.cc b/src/objects/intl-objects.cc index bb820901e0..67964b439d 100644 --- a/src/objects/intl-objects.cc +++ b/src/objects/intl-objects.cc @@ -1322,7 +1322,7 @@ MaybeHandle NumberFormat::Unwrap(Isolate* isolate, Intl::Type::kNumberFormat, method_name_str, true); } -MaybeHandle NumberFormat::FormatNumber( +MaybeHandle NumberFormat::FormatNumber( Isolate* isolate, Handle number_format_holder, double value) { icu::DecimalFormat* number_format = NumberFormat::UnpackNumberFormat(number_format_holder); @@ -1986,32 +1986,5 @@ Handle Intl::InternalCompare(Isolate* isolate, return factory->NewNumberFromInt(result); } - -// ecma402/#sup-properties-of-the-number-prototype-object -MaybeHandle Intl::NumberToLocaleString(Isolate* isolate, - Handle num, - Handle locales, - Handle options) { - Factory* factory = isolate->factory(); - Handle 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 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 diff --git a/src/objects/intl-objects.h b/src/objects/intl-objects.h index 4d733638b2..6a231d013b 100644 --- a/src/objects/intl-objects.h +++ b/src/objects/intl-objects.h @@ -81,7 +81,7 @@ class NumberFormat { const char* method_name); // ecm402/#sec-formatnumber - static MaybeHandle FormatNumber(Isolate* isolate, + static MaybeHandle FormatNumber(Isolate* isolate, Handle number_format_holder, double value); @@ -327,11 +327,6 @@ class Intl { V8_WARN_UNUSED_RESULT static Handle InternalCompare( Isolate* isolate, Handle collator, Handle s1, Handle s2); - - // ecma402/#sup-properties-of-the-number-prototype-object - V8_WARN_UNUSED_RESULT static MaybeHandle NumberToLocaleString( - Isolate* isolate, Handle num, Handle locales, - Handle options); }; } // namespace internal diff --git a/src/runtime/runtime-intl.cc b/src/runtime/runtime-intl.cc index b645327c59..4c62c4b7a3 100644 --- a/src/runtime/runtime-intl.cc +++ b/src/runtime/runtime-intl.cc @@ -229,6 +229,23 @@ 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 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()); diff --git a/src/runtime/runtime.h b/src/runtime/runtime.h index 045675e971..7644b6ec6a 100644 --- a/src/runtime/runtime.h +++ b/src/runtime/runtime.h @@ -219,6 +219,7 @@ 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) \