[Intl] Disprefer using ICU enums directly as instance variables
authorross.kirsling@sony.com <ross.kirsling@sony.com@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Thu, 25 Jun 2020 03:58:07 +0000 (03:58 +0000)
committerross.kirsling@sony.com <ross.kirsling@sony.com@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Thu, 25 Jun 2020 03:58:07 +0000 (03:58 +0000)
https://bugs.webkit.org/show_bug.cgi?id=213587

Reviewed by Yusuke Suzuki.

* runtime/IntlPluralRules.cpp:
(JSC::IntlPluralRules::initializePluralRules):
(JSC::IntlPluralRules::resolvedOptions const):
* runtime/IntlPluralRules.h:
* runtime/IntlRelativeTimeFormat.cpp:
(JSC::IntlRelativeTimeFormat::initializeRelativeTimeFormat):
(JSC::IntlRelativeTimeFormat::styleString): Renamed from JSC::styleString.
(JSC::IntlRelativeTimeFormat::resolvedOptions const):
(JSC::numericString): Deleted.
* runtime/IntlRelativeTimeFormat.h:

git-svn-id: http://svn.webkit.org/repository/webkit/trunk@263497 268f45cc-cd09-0410-ab3c-d52691b4dbfc

Source/JavaScriptCore/ChangeLog
Source/JavaScriptCore/runtime/IntlPluralRules.cpp
Source/JavaScriptCore/runtime/IntlPluralRules.h
Source/JavaScriptCore/runtime/IntlRelativeTimeFormat.cpp
Source/JavaScriptCore/runtime/IntlRelativeTimeFormat.h

index 41bdeab..facfa04 100644 (file)
@@ -1,3 +1,21 @@
+2020-06-24  Ross Kirsling  <ross.kirsling@sony.com>
+
+        [Intl] Disprefer using ICU enums directly as instance variables
+        https://bugs.webkit.org/show_bug.cgi?id=213587
+
+        Reviewed by Yusuke Suzuki.
+
+        * runtime/IntlPluralRules.cpp:
+        (JSC::IntlPluralRules::initializePluralRules):
+        (JSC::IntlPluralRules::resolvedOptions const):
+        * runtime/IntlPluralRules.h:
+        * runtime/IntlRelativeTimeFormat.cpp:
+        (JSC::IntlRelativeTimeFormat::initializeRelativeTimeFormat):
+        (JSC::IntlRelativeTimeFormat::styleString): Renamed from JSC::styleString.
+        (JSC::IntlRelativeTimeFormat::resolvedOptions const):
+        (JSC::numericString): Deleted.
+        * runtime/IntlRelativeTimeFormat.h:
+
 2020-06-24  Caitlin Potter  <caitp@igalia.com>
 
         [JSC] handle Put/DefinePrivateField in resetPutByID
index 616e52e..c499ada 100644 (file)
@@ -123,7 +123,7 @@ void IntlPluralRules::initializePluralRules(JSGlobalObject* globalObject, JSValu
 
     String typeString = intlStringOption(globalObject, options, vm.propertyNames->type, { "cardinal", "ordinal" }, "type must be \"cardinal\" or \"ordinal\"", "cardinal");
     RETURN_IF_EXCEPTION(scope, void());
-    m_type = typeString == "ordinal" ? UPLURAL_TYPE_ORDINAL : UPLURAL_TYPE_CARDINAL;
+    m_type = typeString == "ordinal" ? Type::Ordinal : Type::Cardinal;
 
     unsigned minimumIntegerDigits = intlNumberOption(globalObject, options, Identifier::fromString(vm, "minimumIntegerDigits"), 1, 21, 1);
     RETURN_IF_EXCEPTION(scope, void());
@@ -172,7 +172,7 @@ void IntlPluralRules::initializePluralRules(JSGlobalObject* globalObject, JSValu
     }
 
     status = U_ZERO_ERROR;
-    m_pluralRules = std::unique_ptr<UPluralRules, UPluralRulesDeleter>(uplrules_openForType(m_locale.utf8().data(), m_type, &status));
+    m_pluralRules = std::unique_ptr<UPluralRules, UPluralRulesDeleter>(uplrules_openForType(m_locale.utf8().data(), m_type == Type::Ordinal ? UPLURAL_TYPE_ORDINAL : UPLURAL_TYPE_CARDINAL, &status));
     if (U_FAILURE(status)) {
         throwTypeError(globalObject, scope, "failed to initialize PluralRules"_s);
         return;
@@ -189,7 +189,7 @@ JSObject* IntlPluralRules::resolvedOptions(JSGlobalObject* globalObject) const
 
     JSObject* options = constructEmptyObject(globalObject);
     options->putDirect(vm, vm.propertyNames->locale, jsNontrivialString(vm, m_locale));
-    options->putDirect(vm, vm.propertyNames->type, jsNontrivialString(vm, m_type == UPLURAL_TYPE_ORDINAL ? "ordinal"_s : "cardinal"_s));
+    options->putDirect(vm, vm.propertyNames->type, jsNontrivialString(vm, m_type == Type::Ordinal ? "ordinal"_s : "cardinal"_s));
     options->putDirect(vm, Identifier::fromString(vm, "minimumIntegerDigits"), jsNumber(m_minimumIntegerDigits));
     options->putDirect(vm, Identifier::fromString(vm, "minimumFractionDigits"), jsNumber(m_minimumFractionDigits));
     options->putDirect(vm, Identifier::fromString(vm, "maximumFractionDigits"), jsNumber(m_maximumFractionDigits));
index d371273..b015fe8 100644 (file)
@@ -64,6 +64,8 @@ private:
 
     static Vector<String> localeData(const String&, size_t);
 
+    enum class Type : bool { Cardinal, Ordinal };
+
     struct UPluralRulesDeleter {
         void operator()(UPluralRules*) const;
     };
@@ -75,12 +77,12 @@ private:
     std::unique_ptr<UNumberFormat, UNumberFormatDeleter> m_numberFormat;
 
     String m_locale;
-    UPluralType m_type { UPLURAL_TYPE_CARDINAL };
     unsigned m_minimumIntegerDigits { 1 };
     unsigned m_minimumFractionDigits { 0 };
     unsigned m_maximumFractionDigits { 3 };
     Optional<unsigned> m_minimumSignificantDigits;
     Optional<unsigned> m_maximumSignificantDigits;
+    Type m_type { Type::Cardinal };
 };
 
 } // namespace JSC
index 3093809..91b1bfa 100644 (file)
@@ -135,14 +135,18 @@ void IntlRelativeTimeFormat::initializeRelativeTimeFormat(JSGlobalObject* global
 
     String style = intlStringOption(globalObject, options, vm.propertyNames->style, { "long", "short", "narrow" }, "style must be either \"long\", \"short\", or \"narrow\"", "long");
     RETURN_IF_EXCEPTION(scope, void());
-    if (style == "long")
-        m_style = UDAT_STYLE_LONG;
-    else if (style == "short")
-        m_style = UDAT_STYLE_SHORT;
-    else if (style == "narrow")
-        m_style = UDAT_STYLE_NARROW;
-    else
-        ASSERT_NOT_REACHED();
+    UDateRelativeDateTimeFormatterStyle icuStyle;
+    if (style == "long") {
+        icuStyle = UDAT_STYLE_LONG;
+        m_style = Style::Long;
+    } else if (style == "short") {
+        icuStyle = UDAT_STYLE_SHORT;
+        m_style = Style::Short;
+    } else {
+        ASSERT(style == "narrow");
+        icuStyle = UDAT_STYLE_NARROW;
+        m_style = Style::Narrow;
+    }
 
     String numeric = intlStringOption(globalObject, options, vm.propertyNames->numeric, { "always", "auto" }, "numeric must be either \"always\" or \"auto\"", "always");
     RETURN_IF_EXCEPTION(scope, void());
@@ -155,32 +159,27 @@ void IntlRelativeTimeFormat::initializeRelativeTimeFormat(JSGlobalObject* global
         return;
     }
 
-    m_relativeDateTimeFormatter = std::unique_ptr<URelativeDateTimeFormatter, URelativeDateTimeFormatterDeleter>(ureldatefmt_open(dataLocaleWithExtensions.data(), nullptr, m_style, UDISPCTX_CAPITALIZATION_FOR_STANDALONE, &status));
+    m_relativeDateTimeFormatter = std::unique_ptr<URelativeDateTimeFormatter, URelativeDateTimeFormatterDeleter>(ureldatefmt_open(dataLocaleWithExtensions.data(), nullptr, icuStyle, UDISPCTX_CAPITALIZATION_FOR_STANDALONE, &status));
     if (UNLIKELY(U_FAILURE(status))) {
         throwTypeError(globalObject, scope, "failed to initialize RelativeTimeFormat"_s);
         return;
     }
 }
 
-static ASCIILiteral styleString(UDateRelativeDateTimeFormatterStyle style)
+ASCIILiteral IntlRelativeTimeFormat::styleString(Style style)
 {
     switch (style) {
-    case UDAT_STYLE_LONG:
+    case Style::Long:
         return "long"_s;
-    case UDAT_STYLE_SHORT:
+    case Style::Short:
         return "short"_s;
-    case UDAT_STYLE_NARROW:
+    case Style::Narrow:
         return "narrow"_s;
     }
     ASSERT_NOT_REACHED();
     return ASCIILiteral::null();
 }
 
-static ASCIILiteral numericString(bool numeric)
-{
-    return numeric ? "always"_s : "auto"_s;
-}
-
 // https://tc39.es/ecma402/#sec-intl.relativetimeformat.prototype.resolvedoptions
 JSObject* IntlRelativeTimeFormat::resolvedOptions(JSGlobalObject* globalObject) const
 {
@@ -188,7 +187,7 @@ JSObject* IntlRelativeTimeFormat::resolvedOptions(JSGlobalObject* globalObject)
     JSObject* options = constructEmptyObject(globalObject);
     options->putDirect(vm, vm.propertyNames->locale, jsNontrivialString(vm, m_locale));
     options->putDirect(vm, vm.propertyNames->style, jsNontrivialString(vm, styleString(m_style)));
-    options->putDirect(vm, vm.propertyNames->numeric, jsNontrivialString(vm, numericString(m_numeric)));
+    options->putDirect(vm, vm.propertyNames->numeric, jsNontrivialString(vm, m_numeric ? "always"_s : "auto"_s));
     options->putDirect(vm, vm.propertyNames->numberingSystem, jsNontrivialString(vm, m_numberingSystem));
     return options;
 }
index 0847b5e..85b81fa 100644 (file)
@@ -66,6 +66,8 @@ private:
 
     String formatInternal(JSGlobalObject*, double, StringView unit) const;
 
+    enum class Style : uint8_t { Long, Short, Narrow };
+
     struct URelativeDateTimeFormatterDeleter {
         void operator()(URelativeDateTimeFormatter*) const;
     };
@@ -73,12 +75,14 @@ private:
         void operator()(UNumberFormat*) const;
     };
 
+    static ASCIILiteral styleString(Style);
+
     std::unique_ptr<URelativeDateTimeFormatter, URelativeDateTimeFormatterDeleter> m_relativeDateTimeFormatter;
     std::unique_ptr<UNumberFormat, UNumberFormatDeleter> m_numberFormat;
 
     String m_locale;
     String m_numberingSystem;
-    UDateRelativeDateTimeFormatterStyle m_style { UDAT_STYLE_LONG };
+    Style m_style { Style::Long };
     bool m_numeric { true };
 };