tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

commit fb163b93205386cc920b2f77761dcdea6c4c94b7
parent b4f3df492a462e8f1a796080aa611232aa52b13d
Author: André Bargull <andre.bargull@gmail.com>
Date:   Fri, 12 Dec 2025 17:38:14 +0000

Bug 2005531 - Part 3: Rename SupportedLocaleKind to AvailableLocaleKind. r=spidermonkey-reviewers,dminor

Rename the `enum` to match the ECMA-402 names a bit more closely.

Also move to namespace level to allow forward declarations.

Differential Revision: https://phabricator.services.mozilla.com/D276020

Diffstat:
Mjs/src/builtin/TestingFunctions.cpp | 22+++++++++++-----------
Mjs/src/builtin/intl/IntlObject.cpp | 44++++++++++++++++++++++----------------------
Mjs/src/builtin/intl/SharedIntlData.cpp | 97+++++++++++++++++++++++++++++++++++++------------------------------------------
Mjs/src/builtin/intl/SharedIntlData.h | 48++++++++++++++++++++++++------------------------
4 files changed, 103 insertions(+), 108 deletions(-)

diff --git a/js/src/builtin/TestingFunctions.cpp b/js/src/builtin/TestingFunctions.cpp @@ -9795,9 +9795,9 @@ static bool GetAvailableLocalesOf(JSContext* cx, unsigned argc, Value* vp) { ArrayObject* result; #ifdef JS_HAS_INTL_API - using SupportedLocaleKind = js::intl::SharedIntlData::SupportedLocaleKind; + using AvailableLocaleKind = js::intl::AvailableLocaleKind; - SupportedLocaleKind kind; + AvailableLocaleKind kind; { JSLinearString* typeStr = arg.toString()->ensureLinear(cx); if (!typeStr) { @@ -9805,23 +9805,23 @@ static bool GetAvailableLocalesOf(JSContext* cx, unsigned argc, Value* vp) { } if (StringEqualsLiteral(typeStr, "Collator")) { - kind = SupportedLocaleKind::Collator; + kind = AvailableLocaleKind::Collator; } else if (StringEqualsLiteral(typeStr, "DateTimeFormat")) { - kind = SupportedLocaleKind::DateTimeFormat; + kind = AvailableLocaleKind::DateTimeFormat; } else if (StringEqualsLiteral(typeStr, "DisplayNames")) { - kind = SupportedLocaleKind::DisplayNames; + kind = AvailableLocaleKind::DisplayNames; } else if (StringEqualsLiteral(typeStr, "DurationFormat")) { - kind = SupportedLocaleKind::DurationFormat; + kind = AvailableLocaleKind::DurationFormat; } else if (StringEqualsLiteral(typeStr, "ListFormat")) { - kind = SupportedLocaleKind::ListFormat; + kind = AvailableLocaleKind::ListFormat; } else if (StringEqualsLiteral(typeStr, "NumberFormat")) { - kind = SupportedLocaleKind::NumberFormat; + kind = AvailableLocaleKind::NumberFormat; } else if (StringEqualsLiteral(typeStr, "PluralRules")) { - kind = SupportedLocaleKind::PluralRules; + kind = AvailableLocaleKind::PluralRules; } else if (StringEqualsLiteral(typeStr, "RelativeTimeFormat")) { - kind = SupportedLocaleKind::RelativeTimeFormat; + kind = AvailableLocaleKind::RelativeTimeFormat; } else if (StringEqualsLiteral(typeStr, "Segmenter")) { - kind = SupportedLocaleKind::Segmenter; + kind = AvailableLocaleKind::Segmenter; } else { ReportUsageErrorASCII(cx, callee, "Unsupported Intl constructor name"); return false; diff --git a/js/src/builtin/intl/IntlObject.cpp b/js/src/builtin/intl/IntlObject.cpp @@ -132,11 +132,11 @@ static bool SameOrParentLocale(const JSLinearString* locale, return false; } -using SupportedLocaleKind = js::intl::SharedIntlData::SupportedLocaleKind; +using AvailableLocaleKind = js::intl::AvailableLocaleKind; // 9.2.2 BestAvailableLocale ( availableLocales, locale ) static JS::Result<JSLinearString*> BestAvailableLocale( - JSContext* cx, SupportedLocaleKind kind, Handle<JSLinearString*> locale, + JSContext* cx, AvailableLocaleKind kind, Handle<JSLinearString*> locale, Handle<JSLinearString*> defaultLocale) { // In the spec, [[availableLocales]] is formally a list of all available // locales. But in our implementation, it's an *incomplete* list, not @@ -174,7 +174,7 @@ static JS::Result<JSLinearString*> BestAvailableLocale( while (true) { // Step 2.a. bool supported = false; - if (!sharedIntlData.isSupportedLocale(cx, kind, candidate, &supported)) { + if (!sharedIntlData.isAvailableLocale(cx, kind, candidate, &supported)) { return cx->alreadyReportedError(); } if (supported) { @@ -221,7 +221,7 @@ bool js::intl_BestAvailableLocale(JSContext* cx, unsigned argc, Value* vp) { CallArgs args = CallArgsFromVp(argc, vp); MOZ_ASSERT(args.length() == 3); - SupportedLocaleKind kind; + AvailableLocaleKind kind; { JSLinearString* typeStr = args[0].toString()->ensureLinear(cx); if (!typeStr) { @@ -229,24 +229,24 @@ bool js::intl_BestAvailableLocale(JSContext* cx, unsigned argc, Value* vp) { } if (StringEqualsLiteral(typeStr, "Collator")) { - kind = SupportedLocaleKind::Collator; + kind = AvailableLocaleKind::Collator; } else if (StringEqualsLiteral(typeStr, "DateTimeFormat")) { - kind = SupportedLocaleKind::DateTimeFormat; + kind = AvailableLocaleKind::DateTimeFormat; } else if (StringEqualsLiteral(typeStr, "DisplayNames")) { - kind = SupportedLocaleKind::DisplayNames; + kind = AvailableLocaleKind::DisplayNames; } else if (StringEqualsLiteral(typeStr, "DurationFormat")) { - kind = SupportedLocaleKind::DurationFormat; + kind = AvailableLocaleKind::DurationFormat; } else if (StringEqualsLiteral(typeStr, "ListFormat")) { - kind = SupportedLocaleKind::ListFormat; + kind = AvailableLocaleKind::ListFormat; } else if (StringEqualsLiteral(typeStr, "NumberFormat")) { - kind = SupportedLocaleKind::NumberFormat; + kind = AvailableLocaleKind::NumberFormat; } else if (StringEqualsLiteral(typeStr, "PluralRules")) { - kind = SupportedLocaleKind::PluralRules; + kind = AvailableLocaleKind::PluralRules; } else if (StringEqualsLiteral(typeStr, "RelativeTimeFormat")) { - kind = SupportedLocaleKind::RelativeTimeFormat; + kind = AvailableLocaleKind::RelativeTimeFormat; } else { MOZ_ASSERT(StringEqualsLiteral(typeStr, "Segmenter")); - kind = SupportedLocaleKind::Segmenter; + kind = AvailableLocaleKind::Segmenter; } } @@ -390,13 +390,13 @@ JSLinearString* js::intl::ComputeDefaultLocale(JSContext* cx) { Rooted<JSLinearString*> supportedCollator(cx); JS_TRY_VAR_OR_RETURN_NULL( cx, supportedCollator, - BestAvailableLocale(cx, SupportedLocaleKind::Collator, candidate, + BestAvailableLocale(cx, AvailableLocaleKind::Collator, candidate, nullptr)); Rooted<JSLinearString*> supportedDateTimeFormat(cx); JS_TRY_VAR_OR_RETURN_NULL( cx, supportedDateTimeFormat, - BestAvailableLocale(cx, SupportedLocaleKind::DateTimeFormat, candidate, + BestAvailableLocale(cx, AvailableLocaleKind::DateTimeFormat, candidate, nullptr)); #ifdef DEBUG @@ -404,13 +404,13 @@ JSLinearString* js::intl::ComputeDefaultLocale(JSContext* cx) { // constructors, because the set of supported locales is exactly equal to // the set of supported locales of Intl.DateTimeFormat. for (auto kind : { - SupportedLocaleKind::DisplayNames, - SupportedLocaleKind::DurationFormat, - SupportedLocaleKind::ListFormat, - SupportedLocaleKind::NumberFormat, - SupportedLocaleKind::PluralRules, - SupportedLocaleKind::RelativeTimeFormat, - SupportedLocaleKind::Segmenter, + AvailableLocaleKind::DisplayNames, + AvailableLocaleKind::DurationFormat, + AvailableLocaleKind::ListFormat, + AvailableLocaleKind::NumberFormat, + AvailableLocaleKind::PluralRules, + AvailableLocaleKind::RelativeTimeFormat, + AvailableLocaleKind::Segmenter, }) { JSLinearString* supported; JS_TRY_VAR_OR_RETURN_NULL( diff --git a/js/src/builtin/intl/SharedIntlData.cpp b/js/src/builtin/intl/SharedIntlData.cpp @@ -561,21 +561,21 @@ static bool IsSameAvailableLocales(const AvailableLocales1& availableLocales1, } #endif -bool js::intl::SharedIntlData::ensureSupportedLocales(JSContext* cx) { - if (supportedLocalesInitialized) { +bool js::intl::SharedIntlData::ensureAvailableLocales(JSContext* cx) { + if (availableLocalesInitialized) { return true; } - // If ensureSupportedLocales() was called previously, but didn't complete due + // If ensureAvailableLocales() was called previously, but didn't complete due // to OOM, clear all data and start from scratch. - supportedLocales.clearAndCompact(); - collatorSupportedLocales.clearAndCompact(); + availableLocales.clearAndCompact(); + collatorAvailableLocales.clearAndCompact(); - if (!getAvailableLocales(cx, supportedLocales, + if (!getAvailableLocales(cx, availableLocales, mozilla::intl::Locale::GetAvailableLocales())) { return false; } - if (!getAvailableLocales(cx, collatorSupportedLocales, + if (!getAvailableLocales(cx, collatorAvailableLocales, mozilla::intl::Collator::GetAvailableLocales())) { return false; } @@ -588,66 +588,61 @@ bool js::intl::SharedIntlData::ensureSupportedLocales(JSContext* cx) { mozilla::intl::Locale::GetAvailableLocales(), mozilla::intl::NumberFormat::GetAvailableLocales())); - MOZ_ASSERT(!supportedLocalesInitialized, - "ensureSupportedLocales is neither reentrant nor thread-safe"); - supportedLocalesInitialized = true; + MOZ_ASSERT(!availableLocalesInitialized, + "ensureAvailableLocales is neither reentrant nor thread-safe"); + availableLocalesInitialized = true; return true; } -bool js::intl::SharedIntlData::isSupportedLocale(JSContext* cx, - SupportedLocaleKind kind, - HandleString locale, - bool* supported) { - if (!ensureSupportedLocales(cx)) { +bool js::intl::SharedIntlData::isAvailableLocale(JSContext* cx, + AvailableLocaleKind kind, + Handle<JSLinearString*> locale, + bool* available) { + if (!ensureAvailableLocales(cx)) { return false; } - JSLinearString* localeLinear = locale->ensureLinear(cx); - if (!localeLinear) { - return false; - } - - LocaleHasher::Lookup lookup(localeLinear); + LocaleHasher::Lookup lookup(locale); switch (kind) { - case SupportedLocaleKind::Collator: - *supported = collatorSupportedLocales.has(lookup); + case AvailableLocaleKind::Collator: + *available = collatorAvailableLocales.has(lookup); return true; - case SupportedLocaleKind::DateTimeFormat: - case SupportedLocaleKind::DisplayNames: - case SupportedLocaleKind::DurationFormat: - case SupportedLocaleKind::ListFormat: - case SupportedLocaleKind::NumberFormat: - case SupportedLocaleKind::PluralRules: - case SupportedLocaleKind::RelativeTimeFormat: - case SupportedLocaleKind::Segmenter: - *supported = supportedLocales.has(lookup); + case AvailableLocaleKind::DateTimeFormat: + case AvailableLocaleKind::DisplayNames: + case AvailableLocaleKind::DurationFormat: + case AvailableLocaleKind::ListFormat: + case AvailableLocaleKind::NumberFormat: + case AvailableLocaleKind::PluralRules: + case AvailableLocaleKind::RelativeTimeFormat: + case AvailableLocaleKind::Segmenter: + *available = availableLocales.has(lookup); return true; } MOZ_CRASH("Invalid Intl constructor"); } js::ArrayObject* js::intl::SharedIntlData::availableLocalesOf( - JSContext* cx, SupportedLocaleKind kind) { - if (!ensureSupportedLocales(cx)) { + JSContext* cx, AvailableLocaleKind kind) { + if (!ensureAvailableLocales(cx)) { return nullptr; } LocaleSet* localeSet = nullptr; switch (kind) { - case SupportedLocaleKind::Collator: - localeSet = &collatorSupportedLocales; + case AvailableLocaleKind::Collator: + localeSet = &collatorAvailableLocales; break; - case SupportedLocaleKind::DateTimeFormat: - case SupportedLocaleKind::DisplayNames: - case SupportedLocaleKind::DurationFormat: - case SupportedLocaleKind::ListFormat: - case SupportedLocaleKind::NumberFormat: - case SupportedLocaleKind::PluralRules: - case SupportedLocaleKind::RelativeTimeFormat: - case SupportedLocaleKind::Segmenter: - localeSet = &supportedLocales; + case AvailableLocaleKind::DateTimeFormat: + case AvailableLocaleKind::DisplayNames: + case AvailableLocaleKind::DurationFormat: + case AvailableLocaleKind::ListFormat: + case AvailableLocaleKind::NumberFormat: + case AvailableLocaleKind::PluralRules: + case AvailableLocaleKind::RelativeTimeFormat: + case AvailableLocaleKind::Segmenter: + localeSet = &availableLocales; break; default: MOZ_CRASH("Invalid Intl constructor"); @@ -882,8 +877,8 @@ void js::intl::SharedIntlData::destroyInstance() { availableTimeZones.clearAndCompact(); ianaZonesTreatedAsLinksByICU.clearAndCompact(); ianaLinksCanonicalizedDifferentlyByICU.clearAndCompact(); - supportedLocales.clearAndCompact(); - collatorSupportedLocales.clearAndCompact(); + availableLocales.clearAndCompact(); + collatorAvailableLocales.clearAndCompact(); #if DEBUG || MOZ_SYSTEM_ICU upperCaseFirstLocales.clearAndCompact(); ignorePunctuationLocales.clearAndCompact(); @@ -896,8 +891,8 @@ void js::intl::SharedIntlData::trace(JSTracer* trc) { availableTimeZones.trace(trc); ianaZonesTreatedAsLinksByICU.trace(trc); ianaLinksCanonicalizedDifferentlyByICU.trace(trc); - supportedLocales.trace(trc); - collatorSupportedLocales.trace(trc); + availableLocales.trace(trc); + collatorAvailableLocales.trace(trc); #if DEBUG || MOZ_SYSTEM_ICU upperCaseFirstLocales.trace(trc); ignorePunctuationLocales.trace(trc); @@ -911,8 +906,8 @@ size_t js::intl::SharedIntlData::sizeOfExcludingThis( ianaZonesTreatedAsLinksByICU.shallowSizeOfExcludingThis(mallocSizeOf) + ianaLinksCanonicalizedDifferentlyByICU.shallowSizeOfExcludingThis( mallocSizeOf) + - supportedLocales.shallowSizeOfExcludingThis(mallocSizeOf) + - collatorSupportedLocales.shallowSizeOfExcludingThis(mallocSizeOf) + + availableLocales.shallowSizeOfExcludingThis(mallocSizeOf) + + collatorAvailableLocales.shallowSizeOfExcludingThis(mallocSizeOf) + #if DEBUG || MOZ_SYSTEM_ICU upperCaseFirstLocales.shallowSizeOfExcludingThis(mallocSizeOf) + ignorePunctuationLocales.shallowSizeOfExcludingThis(mallocSizeOf) + diff --git a/js/src/builtin/intl/SharedIntlData.h b/js/src/builtin/intl/SharedIntlData.h @@ -31,6 +31,18 @@ class ArrayObject; namespace intl { +enum class AvailableLocaleKind { + Collator, + DateTimeFormat, + DisplayNames, + DurationFormat, + ListFormat, + NumberFormat, + PluralRules, + RelativeTimeFormat, + Segmenter, +}; + /** * This deleter class exists so that mozilla::intl::DateTimePatternGenerator * can be a forward declaration, but still be used inside of a UniquePtr. @@ -259,7 +271,7 @@ class SharedIntlData { using LocaleSet = GCHashSet<Locale, LocaleHasher, SystemAllocPolicy>; - // Set of supported locales for all Intl service constructors except Collator, + // Set of available locales for all Intl service constructors except Collator, // which uses its own set. // // UDateFormat: @@ -273,13 +285,13 @@ class SharedIntlData { // UListFormatter, UPluralRules, and URelativeDateTimeFormatter: // We're going to use ULocale availableLocales as per ICU recommendation: // https://unicode-org.atlassian.net/browse/ICU-12756 - LocaleSet supportedLocales; + LocaleSet availableLocales; // ucol_[count,get]Available() return different results compared to - // uloc_[count,get]Available(), we can't use |supportedLocales| here. - LocaleSet collatorSupportedLocales; + // uloc_[count,get]Available(), we can't use |availableLocales| here. + LocaleSet collatorAvailableLocales; - bool supportedLocalesInitialized = false; + bool availableLocalesInitialized = false; // CountAvailable and GetAvailable describe the signatures used for ICU API // to determine available locales for various functionality. @@ -293,33 +305,21 @@ class SharedIntlData { /** * Precomputes the available locales sets. */ - bool ensureSupportedLocales(JSContext* cx); + bool ensureAvailableLocales(JSContext* cx); public: - enum class SupportedLocaleKind { - Collator, - DateTimeFormat, - DisplayNames, - DurationFormat, - ListFormat, - NumberFormat, - PluralRules, - RelativeTimeFormat, - Segmenter, - }; - /** - * Sets |supported| to true if |locale| is supported by the requested Intl - * service constructor. Otherwise sets |supported| to false. + * Sets |available| to true if |locale| is supported by the requested Intl + * service constructor. Otherwise sets |available| to false. */ - [[nodiscard]] bool isSupportedLocale(JSContext* cx, SupportedLocaleKind kind, - JS::Handle<JSString*> locale, - bool* supported); + [[nodiscard]] bool isAvailableLocale(JSContext* cx, AvailableLocaleKind kind, + JS::Handle<JSLinearString*> locale, + bool* available); /** * Returns all available locales for |kind|. */ - ArrayObject* availableLocalesOf(JSContext* cx, SupportedLocaleKind kind); + ArrayObject* availableLocalesOf(JSContext* cx, AvailableLocaleKind kind); private: /**