Bug 1431957 - Move a bunch of functions in builtin/intl/CommonFunctions.js into more-specific files, where those functions are only used in a single more-specific file. r=anba
authorJeff Walden <jwalden@mit.edu>
Sat, 20 Jan 2018 12:41:06 -0800
changeset 401047 c50cbf6e81761639707a404ea74ca3c4a66fff3a
parent 401046 767fb4816abcdd14a973ba71fad66f086eeb9e5c
child 401048 2d789409cb08485456488c06c5fc3c3ae8de9135
push id99283
push userjwalden@mit.edu
push dateSat, 27 Jan 2018 07:54:29 +0000
treeherdermozilla-inbound@c50cbf6e8176 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersanba
bugs1431957
milestone60.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 1431957 - Move a bunch of functions in builtin/intl/CommonFunctions.js into more-specific files, where those functions are only used in a single more-specific file. r=anba
js/src/builtin/intl/CommonFunctions.js
js/src/builtin/intl/DateTimeFormat.js
js/src/builtin/intl/NumberFormat.js
--- a/js/src/builtin/intl/CommonFunctions.js
+++ b/js/src/builtin/intl/CommonFunctions.js
@@ -1,37 +1,14 @@
 /* This Source Code Form is subject to the terms of the Mozilla Public
  * License, v. 2.0. If a copy of the MPL was not distributed with this
  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
 
 /* Portions Copyright Norbert Lindenberg 2011-2012. */
 
-/********** Locales, Time Zones, and Currencies **********/
-
-/**
- * Convert s to upper case, but limited to characters a-z.
- *
- * Spec: ECMAScript Internationalization API Specification, 6.1.
- */
-function toASCIIUpperCase(s) {
-    assert(typeof s === "string", "toASCIIUpperCase");
-
-    // String.prototype.toUpperCase may map non-ASCII characters into ASCII,
-    // so go character by character (actually code unit by code unit, but
-    // since we only care about ASCII characters here, that's OK).
-    var result = "";
-    for (var i = 0; i < s.length; i++) {
-        var c = callFunction(std_String_charCodeAt, s, i);
-        result += (0x61 <= c && c <= 0x7A)
-                  ? callFunction(std_String_fromCharCode, null, c & ~0x20)
-                  : s[i];
-    }
-    return result;
-}
-
 /**
  * Holder object for encapsulating regexp instances.
  *
  * Regular expression instances should be created after the initialization of
  * self-hosted global.
  */
 var internalIntlRegExps = std_Object_create(null);
 internalIntlRegExps.unicodeLocaleExtensionSequenceRE = null;
@@ -304,16 +281,35 @@ function IsStructurallyValidLanguageTag(
     // Check for duplicate variant or singleton subtags.
     var duplicateVariantRE = getDuplicateVariantRE();
     var duplicateSingletonRE = getDuplicateSingletonRE();
     return !regexp_test_no_statics(duplicateVariantRE, locale) &&
            !regexp_test_no_statics(duplicateSingletonRE, locale);
 }
 
 /**
+ * Joins the array elements in the given range with the supplied separator.
+ */
+function ArrayJoinRange(array, separator, from, to = array.length) {
+    assert(typeof separator === "string", "|separator| is a string value");
+    assert(typeof from === "number", "|from| is a number value");
+    assert(typeof to === "number", "|to| is a number value");
+    assert(0 <= from && from <= to && to <= array.length, "|from| and |to| form a valid range");
+
+    if (from === to)
+        return "";
+
+    var result = array[from];
+    for (var i = from + 1; i < to; i++) {
+        result += separator + array[i];
+    }
+    return result;
+}
+
+/**
  * Canonicalizes the given structurally valid BCP 47 language tag, including
  * regularized case of subtags. For example, the language tag
  * Zh-NAN-haNS-bu-variant2-Variant1-u-ca-chinese-t-Zh-laTN-x-PRIVATE, where
  *
  *     Zh             ; 2*3ALPHA
  *     -NAN           ; ["-" extlang]
  *     -haNS          ; ["-" script]
  *     -bu            ; ["-" region]
@@ -440,35 +436,16 @@ function CanonicalizeLanguageTag(locale)
         else
             canonical = privateUse;
     }
 
     return canonical;
 }
 
 /**
- * Joins the array elements in the given range with the supplied separator.
- */
-function ArrayJoinRange(array, separator, from, to = array.length) {
-    assert(typeof separator === "string", "|separator| is a string value");
-    assert(typeof from === "number", "|from| is a number value");
-    assert(typeof to === "number", "|to| is a number value");
-    assert(0 <= from && from <= to && to <= array.length, "|from| and |to| form a valid range");
-
-    if (from === to)
-        return "";
-
-    var result = array[from];
-    for (var i = from + 1; i < to; i++) {
-        result += separator + array[i];
-    }
-    return result;
-}
-
-/**
  * Returns true if the input contains only ASCII alphabetical characters.
  */
 function IsASCIIAlphaString(s) {
     assert(typeof s === "string", "IsASCIIAlphaString");
 
     for (var i = 0; i < s.length; i++) {
         var c = callFunction(std_String_charCodeAt, s, i);
         if (!((0x41 <= c && c <= 0x5A) || (0x61 <= c && c <= 0x7A)))
@@ -645,119 +622,16 @@ function DefaultLocale() {
 
     localeCache.defaultLocale = locale;
     localeCache.runtimeDefaultLocale = runtimeDefaultLocale;
 
     return locale;
 }
 
 /**
- * Verifies that the given string is a well-formed ISO 4217 currency code.
- *
- * Spec: ECMAScript Internationalization API Specification, 6.3.1.
- */
-function getIsWellFormedCurrencyCodeRE() {
-    return internalIntlRegExps.isWellFormedCurrencyCodeRE ||
-           (internalIntlRegExps.isWellFormedCurrencyCodeRE = RegExpCreate("[^A-Z]"));
-}
-function IsWellFormedCurrencyCode(currency) {
-    var c = ToString(currency);
-    var normalized = toASCIIUpperCase(c);
-    if (normalized.length !== 3)
-        return false;
-    return !regexp_test_no_statics(getIsWellFormedCurrencyCodeRE(), normalized);
-}
-
-var timeZoneCache = {
-    icuDefaultTimeZone: undefined,
-    defaultTimeZone: undefined,
-};
-
-/**
- * 6.4.2 CanonicalizeTimeZoneName ( timeZone )
- *
- * Canonicalizes the given IANA time zone name.
- *
- * ES2017 Intl draft rev 4a23f407336d382ed5e3471200c690c9b020b5f3
- */
-function CanonicalizeTimeZoneName(timeZone) {
-    assert(typeof timeZone === "string", "CanonicalizeTimeZoneName");
-
-    // Step 1. (Not applicable, the input is already a valid IANA time zone.)
-    assert(timeZone !== "Etc/Unknown", "Invalid time zone");
-    assert(timeZone === intl_IsValidTimeZoneName(timeZone), "Time zone name not normalized");
-
-    // Step 2.
-    var ianaTimeZone = intl_canonicalizeTimeZone(timeZone);
-    assert(ianaTimeZone !== "Etc/Unknown", "Invalid canonical time zone");
-    assert(ianaTimeZone === intl_IsValidTimeZoneName(ianaTimeZone), "Unsupported canonical time zone");
-
-    // Step 3.
-    if (ianaTimeZone === "Etc/UTC" || ianaTimeZone === "Etc/GMT") {
-        // ICU/CLDR canonicalizes Etc/UCT to Etc/GMT, but following IANA and
-        // ECMA-402 to the letter means Etc/UCT is a separate time zone.
-        if (timeZone === "Etc/UCT" || timeZone === "UCT")
-            ianaTimeZone = "Etc/UCT";
-        else
-            ianaTimeZone = "UTC";
-    }
-
-    // Step 4.
-    return ianaTimeZone;
-}
-
-/**
- * 6.4.3 DefaultTimeZone ()
- *
- * Returns the IANA time zone name for the host environment's current time zone.
- *
- * ES2017 Intl draft rev 4a23f407336d382ed5e3471200c690c9b020b5f3
- */
-function DefaultTimeZone() {
-    if (intl_isDefaultTimeZone(timeZoneCache.icuDefaultTimeZone))
-        return timeZoneCache.defaultTimeZone;
-
-    // Verify that the current ICU time zone is a valid ECMA-402 time zone.
-    var icuDefaultTimeZone = intl_defaultTimeZone();
-    var timeZone = intl_IsValidTimeZoneName(icuDefaultTimeZone);
-    if (timeZone === null) {
-        // Before defaulting to "UTC", try to represent the default time zone
-        // using the Etc/GMT + offset format. This format only accepts full
-        // hour offsets.
-        const msPerHour = 60 * 60 * 1000;
-        var offset = intl_defaultTimeZoneOffset();
-        assert(offset === (offset | 0),
-               "milliseconds offset shouldn't be able to exceed int32_t range");
-        var offsetHours = offset / msPerHour, offsetHoursFraction = offset % msPerHour;
-        if (offsetHoursFraction === 0) {
-            // Etc/GMT + offset uses POSIX-style signs, i.e. a positive offset
-            // means a location west of GMT.
-            timeZone = "Etc/GMT" + (offsetHours < 0 ? "+" : "-") + std_Math_abs(offsetHours);
-
-            // Check if the fallback is valid.
-            timeZone = intl_IsValidTimeZoneName(timeZone);
-        }
-
-        // Fallback to "UTC" if everything else fails.
-        if (timeZone === null)
-            timeZone = "UTC";
-    }
-
-    // Canonicalize the ICU time zone, e.g. change Etc/UTC to UTC.
-    var defaultTimeZone = CanonicalizeTimeZoneName(timeZone);
-
-    timeZoneCache.defaultTimeZone = defaultTimeZone;
-    timeZoneCache.icuDefaultTimeZone = icuDefaultTimeZone;
-
-    return defaultTimeZone;
-}
-
-/********** Locale and Parameter Negotiation **********/
-
-/**
  * Add old-style language tags without script code for locales that in current
  * usage would include a script subtag.  Also add an entry for the last-ditch
  * locale, in case ICU doesn't directly support it (but does support it through
  * fallback, e.g. supporting "en-GB" indirectly using "en" support).
  */
 function addSpecialMissingLanguageTags(availableLocales) {
     // Certain old-style language tags lack a script code, but in current usage
     // they *would* include a script code.  Map these over to modern forms.
@@ -1297,18 +1171,16 @@ function DefaultNumberOption(value, mini
  *
  * Spec: ECMAScript Internationalization API Specification, 9.2.12.
  */
 function GetNumberOption(options, property, minimum, maximum, fallback) {
     // Steps 1-3.
     return DefaultNumberOption(options[property], minimum, maximum, fallback);
 }
 
-/********** Property access for Intl objects **********/
-
 // Symbols in the self-hosting compartment can't be cloned, use a separate
 // object to hold the actual symbol value.
 // TODO: Can we add support to clone symbols?
 var intlFallbackSymbolHolder = { value: undefined };
 
 /**
  * The [[FallbackSymbol]] symbol of the %Intl% intrinsic object.
  *
--- a/js/src/builtin/intl/DateTimeFormat.js
+++ b/js/src/builtin/intl/DateTimeFormat.js
@@ -201,16 +201,100 @@ function UnwrapDateTimeFormat(dtf, metho
                        "DateTimeFormat");
     }
 
     // Step 3.
     return dtf;
 }
 
 /**
+ * 6.4.2 CanonicalizeTimeZoneName ( timeZone )
+ *
+ * Canonicalizes the given IANA time zone name.
+ *
+ * ES2017 Intl draft rev 4a23f407336d382ed5e3471200c690c9b020b5f3
+ */
+function CanonicalizeTimeZoneName(timeZone) {
+    assert(typeof timeZone === "string", "CanonicalizeTimeZoneName");
+
+    // Step 1. (Not applicable, the input is already a valid IANA time zone.)
+    assert(timeZone !== "Etc/Unknown", "Invalid time zone");
+    assert(timeZone === intl_IsValidTimeZoneName(timeZone), "Time zone name not normalized");
+
+    // Step 2.
+    var ianaTimeZone = intl_canonicalizeTimeZone(timeZone);
+    assert(ianaTimeZone !== "Etc/Unknown", "Invalid canonical time zone");
+    assert(ianaTimeZone === intl_IsValidTimeZoneName(ianaTimeZone), "Unsupported canonical time zone");
+
+    // Step 3.
+    if (ianaTimeZone === "Etc/UTC" || ianaTimeZone === "Etc/GMT") {
+        // ICU/CLDR canonicalizes Etc/UCT to Etc/GMT, but following IANA and
+        // ECMA-402 to the letter means Etc/UCT is a separate time zone.
+        if (timeZone === "Etc/UCT" || timeZone === "UCT")
+            ianaTimeZone = "Etc/UCT";
+        else
+            ianaTimeZone = "UTC";
+    }
+
+    // Step 4.
+    return ianaTimeZone;
+}
+
+var timeZoneCache = {
+    icuDefaultTimeZone: undefined,
+    defaultTimeZone: undefined,
+};
+
+/**
+ * 6.4.3 DefaultTimeZone ()
+ *
+ * Returns the IANA time zone name for the host environment's current time zone.
+ *
+ * ES2017 Intl draft rev 4a23f407336d382ed5e3471200c690c9b020b5f3
+ */
+function DefaultTimeZone() {
+    if (intl_isDefaultTimeZone(timeZoneCache.icuDefaultTimeZone))
+        return timeZoneCache.defaultTimeZone;
+
+    // Verify that the current ICU time zone is a valid ECMA-402 time zone.
+    var icuDefaultTimeZone = intl_defaultTimeZone();
+    var timeZone = intl_IsValidTimeZoneName(icuDefaultTimeZone);
+    if (timeZone === null) {
+        // Before defaulting to "UTC", try to represent the default time zone
+        // using the Etc/GMT + offset format. This format only accepts full
+        // hour offsets.
+        const msPerHour = 60 * 60 * 1000;
+        var offset = intl_defaultTimeZoneOffset();
+        assert(offset === (offset | 0),
+               "milliseconds offset shouldn't be able to exceed int32_t range");
+        var offsetHours = offset / msPerHour, offsetHoursFraction = offset % msPerHour;
+        if (offsetHoursFraction === 0) {
+            // Etc/GMT + offset uses POSIX-style signs, i.e. a positive offset
+            // means a location west of GMT.
+            timeZone = "Etc/GMT" + (offsetHours < 0 ? "+" : "-") + std_Math_abs(offsetHours);
+
+            // Check if the fallback is valid.
+            timeZone = intl_IsValidTimeZoneName(timeZone);
+        }
+
+        // Fallback to "UTC" if everything else fails.
+        if (timeZone === null)
+            timeZone = "UTC";
+    }
+
+    // Canonicalize the ICU time zone, e.g. change Etc/UTC to UTC.
+    var defaultTimeZone = CanonicalizeTimeZoneName(timeZone);
+
+    timeZoneCache.defaultTimeZone = defaultTimeZone;
+    timeZoneCache.icuDefaultTimeZone = icuDefaultTimeZone;
+
+    return defaultTimeZone;
+}
+
+/**
  * Initializes an object as a DateTimeFormat.
  *
  * This method is complicated a moderate bit by its implementing initialization
  * as a *lazy* concept.  Everything that must happen now, does -- but we defer
  * all the work we can until the object is actually used as a DateTimeFormat.
  * This later work occurs in |resolveDateTimeFormatInternals|; steps not noted
  * here occur there.
  *
--- a/js/src/builtin/intl/NumberFormat.js
+++ b/js/src/builtin/intl/NumberFormat.js
@@ -161,16 +161,55 @@ function SetNumberFormatDigitOptions(laz
         mnsd = DefaultNumberOption(mnsd, 1, 21, 1);
         mxsd = DefaultNumberOption(mxsd, mnsd, 21, 21);
         lazyData.minimumSignificantDigits = mnsd;
         lazyData.maximumSignificantDigits = mxsd;
     }
 }
 
 /**
+ * Convert s to upper case, but limited to characters a-z.
+ *
+ * Spec: ECMAScript Internationalization API Specification, 6.1.
+ */
+function toASCIIUpperCase(s) {
+    assert(typeof s === "string", "toASCIIUpperCase");
+
+    // String.prototype.toUpperCase may map non-ASCII characters into ASCII,
+    // so go character by character (actually code unit by code unit, but
+    // since we only care about ASCII characters here, that's OK).
+    var result = "";
+    for (var i = 0; i < s.length; i++) {
+        var c = callFunction(std_String_charCodeAt, s, i);
+        result += (0x61 <= c && c <= 0x7A)
+                  ? callFunction(std_String_fromCharCode, null, c & ~0x20)
+                  : s[i];
+    }
+    return result;
+}
+
+/**
+ * Verifies that the given string is a well-formed ISO 4217 currency code.
+ *
+ * Spec: ECMAScript Internationalization API Specification, 6.3.1.
+ */
+function getIsWellFormedCurrencyCodeRE() {
+    return internalIntlRegExps.isWellFormedCurrencyCodeRE ||
+           (internalIntlRegExps.isWellFormedCurrencyCodeRE = RegExpCreate("[^A-Z]"));
+}
+
+function IsWellFormedCurrencyCode(currency) {
+    var c = ToString(currency);
+    var normalized = toASCIIUpperCase(c);
+    if (normalized.length !== 3)
+        return false;
+    return !regexp_test_no_statics(getIsWellFormedCurrencyCodeRE(), normalized);
+}
+
+/**
  * Initializes an object as a NumberFormat.
  *
  * This method is complicated a moderate bit by its implementing initialization
  * as a *lazy* concept.  Everything that must happen now, does -- but we defer
  * all the work we can until the object is actually used as a NumberFormat.
  * This later work occurs in |resolveNumberFormatInternals|; steps not noted
  * here occur there.
  *