| 1 |
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- |
| 2 |
* vim: set ts=8 sts=2 et sw=2 tw=80: |
| 3 |
* This Source Code Form is subject to the terms of the Mozilla Public |
| 4 |
* License, v. 2.0. If a copy of the MPL was not distributed with this |
| 5 |
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
| 6 |
|
| 7 |
#ifndef builtin_intl_SharedIntlData_h |
| 8 |
#define builtin_intl_SharedIntlData_h |
| 9 |
|
| 10 |
#include "mozilla/MemoryReporting.h" |
| 11 |
#include "mozilla/UniquePtr.h" |
| 12 |
|
| 13 |
#include <stddef.h> |
| 14 |
|
| 15 |
#include "js/AllocPolicy.h" |
| 16 |
#include "js/GCAPI.h" |
| 17 |
#include "js/GCHashTable.h" |
| 18 |
#include "js/Result.h" |
| 19 |
#include "js/RootingAPI.h" |
| 20 |
#include "js/Utility.h" |
| 21 |
#include "vm/StringType.h" |
| 22 |
|
| 23 |
namespace mozilla::intl { |
| 24 |
class DateTimePatternGenerator; |
| 25 |
} // namespace mozilla::intl |
| 26 |
|
| 27 |
namespace js { |
| 28 |
|
| 29 |
class ArrayObject; |
| 30 |
|
| 31 |
namespace intl { |
| 32 |
|
| 33 |
/** |
| 34 |
* This deleter class exists so that mozilla::intl::DateTimePatternGenerator |
| 35 |
* can be a forward declaration, but still be used inside of a UniquePtr. |
| 36 |
*/ |
| 37 |
class DateTimePatternGeneratorDeleter { |
| 38 |
public: |
| 39 |
void operator()(mozilla::intl::DateTimePatternGenerator* ptr); |
| 40 |
}; |
| 41 |
|
| 42 |
/** |
| 43 |
* Stores Intl data which can be shared across compartments (but not contexts). |
| 44 |
* |
| 45 |
* Used for data which is expensive when computed repeatedly or is not |
| 46 |
* available through ICU. |
| 47 |
*/ |
| 48 |
class SharedIntlData { |
| 49 |
struct LinearStringLookup { |
| 50 |
union { |
| 51 |
const JS::Latin1Char* latin1Chars; |
| 52 |
const char16_t* twoByteChars; |
| 53 |
}; |
| 54 |
bool isLatin1; |
| 55 |
size_t length; |
| 56 |
JS::AutoCheckCannotGC nogc; |
| 57 |
HashNumber hash = 0; |
| 58 |
|
| 59 |
explicit LinearStringLookup(const JSLinearString* string) |
| 60 |
: isLatin1(string->hasLatin1Chars()), length(string->length()) { |
| 61 |
if (isLatin1) { |
| 62 |
latin1Chars = string->latin1Chars(nogc); |
| 63 |
} else { |
| 64 |
twoByteChars = string->twoByteChars(nogc); |
| 65 |
} |
| 66 |
} |
| 67 |
|
| 68 |
LinearStringLookup(const char* chars, size_t length) |
| 69 |
: isLatin1(true), length(length) { |
| 70 |
latin1Chars = reinterpret_cast<const JS::Latin1Char*>(chars); |
| 71 |
} |
| 72 |
}; |
| 73 |
|
| 74 |
public: |
| 75 |
/** |
| 76 |
* Information tracking the set of the supported time zone names, derived |
| 77 |
* from the IANA time zone database <https://www.iana.org/time-zones>. |
| 78 |
* |
| 79 |
* There are two kinds of IANA time zone names: Zone and Link (denoted as |
| 80 |
* such in database source files). Zone names are the canonical, preferred |
| 81 |
* name for a time zone, e.g. Asia/Kolkata. Link names simply refer to |
| 82 |
* target Zone names for their meaning, e.g. Asia/Calcutta targets |
| 83 |
* Asia/Kolkata. That a name is a Link doesn't *necessarily* reflect a |
| 84 |
* sense of deprecation: some Link names also exist partly for convenience, |
| 85 |
* e.g. UTC and GMT as Link names targeting the Zone name Etc/UTC. |
| 86 |
* |
| 87 |
* Two data sources determine the time zone names we support: those ICU |
| 88 |
* supports and IANA's zone information. |
| 89 |
* |
| 90 |
* Unfortunately the names ICU and IANA support, and their Link |
| 91 |
* relationships from name to target, aren't identical, so we can't simply |
| 92 |
* implicitly trust ICU's name handling. We must perform various |
| 93 |
* preprocessing of user-provided zone names and post-processing of |
| 94 |
* ICU-provided zone names to implement ECMA-402's IANA-consistent behavior. |
| 95 |
* |
| 96 |
* Also see <https://ssl.icu-project.org/trac/ticket/12044> and |
| 97 |
* <http://unicode.org/cldr/trac/ticket/9892>. |
| 98 |
*/ |
| 99 |
|
| 100 |
using TimeZoneName = JSAtom*; |
| 101 |
|
| 102 |
struct TimeZoneHasher { |
| 103 |
struct Lookup : LinearStringLookup { |
| 104 |
explicit Lookup(const JSLinearString* timeZone); |
| 105 |
}; |
| 106 |
|
| 107 |
static js::HashNumber hash(const Lookup& lookup) { return lookup.hash; } |
| 108 |
static bool match(TimeZoneName key, const Lookup& lookup); |
| 109 |
}; |
| 110 |
|
| 111 |
using TimeZoneSet = |
| 112 |
GCHashSet<TimeZoneName, TimeZoneHasher, SystemAllocPolicy>; |
| 113 |
using TimeZoneMap = |
| 114 |
GCHashMap<TimeZoneName, TimeZoneName, TimeZoneHasher, SystemAllocPolicy>; |
| 115 |
|
| 116 |
private: |
| 117 |
/** |
| 118 |
* As a threshold matter, available time zones are those time zones ICU |
| 119 |
* supports, via ucal_openTimeZones. But ICU supports additional non-IANA |
| 120 |
* time zones described in intl/icu/source/tools/tzcode/icuzones (listed in |
| 121 |
* IntlTimeZoneData.cpp's |legacyICUTimeZones|) for its own backwards |
| 122 |
* compatibility purposes. This set consists of ICU's supported time zones, |
| 123 |
* minus all backwards-compatibility time zones. |
| 124 |
*/ |
| 125 |
TimeZoneSet availableTimeZones; |
| 126 |
|
| 127 |
/** |
| 128 |
* IANA treats some time zone names as Zones, that ICU instead treats as |
| 129 |
* Links. For example, IANA considers "America/Indiana/Indianapolis" to be |
| 130 |
* a Zone and "America/Fort_Wayne" a Link that targets it, but ICU |
| 131 |
* considers the former a Link that targets "America/Indianapolis" (which |
| 132 |
* IANA treats as a Link). |
| 133 |
* |
| 134 |
* ECMA-402 requires that we respect IANA data, so if we're asked to |
| 135 |
* canonicalize a time zone name in this set, we must *not* return ICU's |
| 136 |
* canonicalization. |
| 137 |
*/ |
| 138 |
TimeZoneSet ianaZonesTreatedAsLinksByICU; |
| 139 |
|
| 140 |
/** |
| 141 |
* IANA treats some time zone names as Links to one target, that ICU |
| 142 |
* instead treats as either Zones, or Links to different targets. An |
| 143 |
* example of the former is "Asia/Calcutta, which IANA assigns the target |
| 144 |
* "Asia/Kolkata" but ICU considers its own Zone. An example of the latter |
| 145 |
* is "America/Virgin", which IANA assigns the target |
| 146 |
* "America/Port_of_Spain" but ICU assigns the target "America/St_Thomas". |
| 147 |
* |
| 148 |
* ECMA-402 requires that we respect IANA data, so if we're asked to |
| 149 |
* canonicalize a time zone name that's a key in this map, we *must* return |
| 150 |
* the corresponding value and *must not* return ICU's canonicalization. |
| 151 |
*/ |
| 152 |
TimeZoneMap ianaLinksCanonicalizedDifferentlyByICU; |
| 153 |
|
| 154 |
bool timeZoneDataInitialized = false; |
| 155 |
|
| 156 |
/** |
| 157 |
* Precomputes the available time zone names, because it's too expensive to |
| 158 |
* call ucal_openTimeZones() repeatedly. |
| 159 |
*/ |
| 160 |
bool ensureTimeZones(JSContext* cx); |
| 161 |
|
| 162 |
public: |
| 163 |
/** |
| 164 |
* Returns the validated time zone name in |result|. If the input time zone |
| 165 |
* isn't a valid IANA time zone name, |result| remains unchanged. |
| 166 |
*/ |
| 167 |
bool validateTimeZoneName(JSContext* cx, JS::Handle<JSString*> timeZone, |
| 168 |
JS::MutableHandle<JSAtom*> result); |
| 169 |
|
| 170 |
/** |
| 171 |
* Returns the canonical time zone name in |result|. If no canonical name |
| 172 |
* was found, |result| remains unchanged. |
| 173 |
* |
| 174 |
* This method only handles time zones which are canonicalized differently |
| 175 |
* by ICU when compared to IANA. |
| 176 |
*/ |
| 177 |
bool tryCanonicalizeTimeZoneConsistentWithIANA( |
| 178 |
JSContext* cx, JS::Handle<JSString*> timeZone, |
| 179 |
JS::MutableHandle<JSAtom*> result); |
| 180 |
|
| 181 |
/** |
| 182 |
* Returns an iterator over all available time zones supported by ICU. The |
| 183 |
* returned time zone names aren't canonicalized. |
| 184 |
*/ |
| 185 |
JS::Result<TimeZoneSet::Iterator> availableTimeZonesIteration(JSContext* cx); |
| 186 |
|
| 187 |
private: |
| 188 |
using Locale = JSAtom*; |
| 189 |
|
| 190 |
struct LocaleHasher { |
| 191 |
struct Lookup : LinearStringLookup { |
| 192 |
explicit Lookup(const JSLinearString* locale); |
| 193 |
Lookup(const char* chars, size_t length); |
| 194 |
}; |
| 195 |
|
| 196 |
static js::HashNumber hash(const Lookup& lookup) { return lookup.hash; } |
| 197 |
static bool match(Locale key, const Lookup& lookup); |
| 198 |
}; |
| 199 |
|
| 200 |
using LocaleSet = GCHashSet<Locale, LocaleHasher, SystemAllocPolicy>; |
| 201 |
|
| 202 |
// Set of supported locales for all Intl service constructors except Collator, |
| 203 |
// which uses its own set. |
| 204 |
// |
| 205 |
// UDateFormat: |
| 206 |
// udat_[count,get]Available() return the same results as their |
| 207 |
// uloc_[count,get]Available() counterparts. |
| 208 |
// |
| 209 |
// UNumberFormatter: |
| 210 |
// unum_[count,get]Available() return the same results as their |
| 211 |
// uloc_[count,get]Available() counterparts. |
| 212 |
// |
| 213 |
// UListFormatter, UPluralRules, and URelativeDateTimeFormatter: |
| 214 |
// We're going to use ULocale availableLocales as per ICU recommendation: |
| 215 |
// https://unicode-org.atlassian.net/browse/ICU-12756 |
| 216 |
LocaleSet supportedLocales; |
| 217 |
|
| 218 |
// ucol_[count,get]Available() return different results compared to |
| 219 |
// uloc_[count,get]Available(), we can't use |supportedLocales| here. |
| 220 |
LocaleSet collatorSupportedLocales; |
| 221 |
|
| 222 |
bool supportedLocalesInitialized = false; |
| 223 |
|
| 224 |
// CountAvailable and GetAvailable describe the signatures used for ICU API |
| 225 |
// to determine available locales for various functionality. |
| 226 |
using CountAvailable = int32_t (*)(); |
| 227 |
using GetAvailable = const char* (*)(int32_t localeIndex); |
| 228 |
|
| 229 |
template <class AvailableLocales> |
| 230 |
static bool getAvailableLocales(JSContext* cx, LocaleSet& locales, |
| 231 |
const AvailableLocales& availableLocales); |
| 232 |
|
| 233 |
/** |
| 234 |
* Precomputes the available locales sets. |
| 235 |
*/ |
| 236 |
bool ensureSupportedLocales(JSContext* cx); |
| 237 |
|
| 238 |
public: |
| 239 |
enum class SupportedLocaleKind { |
| 240 |
Collator, |
| 241 |
DateTimeFormat, |
| 242 |
DisplayNames, |
| 243 |
DurationFormat, |
| 244 |
ListFormat, |
| 245 |
NumberFormat, |
| 246 |
PluralRules, |
| 247 |
RelativeTimeFormat, |
| 248 |
Segmenter, |
| 249 |
}; |
| 250 |
|
| 251 |
/** |
| 252 |
* Sets |supported| to true if |locale| is supported by the requested Intl |
| 253 |
* service constructor. Otherwise sets |supported| to false. |
| 254 |
*/ |
| 255 |
[[nodiscard]] bool isSupportedLocale(JSContext* cx, SupportedLocaleKind kind, |
| 256 |
JS::Handle<JSString*> locale, |
| 257 |
bool* supported); |
| 258 |
|
| 259 |
/** |
| 260 |
* Returns all available locales for |kind|. |
| 261 |
*/ |
| 262 |
ArrayObject* availableLocalesOf(JSContext* cx, SupportedLocaleKind kind); |
| 263 |
|
| 264 |
private: |
| 265 |
/** |
| 266 |
* The case first parameter (BCP47 key "kf") allows to switch the order of |
| 267 |
* upper- and lower-case characters. ICU doesn't directly provide an API |
| 268 |
* to query the default case first value of a given locale, but instead |
| 269 |
* requires to instantiate a collator object and then query the case first |
| 270 |
* attribute (UCOL_CASE_FIRST). |
| 271 |
* To avoid instantiating an additional collator object whenever we need |
| 272 |
* to retrieve the default case first value of a specific locale, we |
| 273 |
* compute the default case first value for every supported locale only |
| 274 |
* once and then keep a list of all locales which don't use the default |
| 275 |
* case first setting. |
| 276 |
* There is almost no difference between lower-case first and when case |
| 277 |
* first is disabled (UCOL_LOWER_FIRST resp. UCOL_OFF), so we only need to |
| 278 |
* track locales which use upper-case first as their default setting. |
| 279 |
* |
| 280 |
* Instantiating collator objects for each available locale is slow |
| 281 |
* (bug 1527879), therefore we're hardcoding the two locales using upper-case |
| 282 |
* first ("da" (Danish) and "mt" (Maltese)) and only assert in debug-mode |
| 283 |
* these two locales match the upper-case first locales returned by ICU. A |
| 284 |
* system-ICU may support a different set of locales, therefore we're always |
| 285 |
* calling into ICU to find the upper-case first locales in that case. |
| 286 |
*/ |
| 287 |
|
| 288 |
#if DEBUG || MOZ_SYSTEM_ICU |
| 289 |
LocaleSet upperCaseFirstLocales; |
| 290 |
|
| 291 |
bool upperCaseFirstInitialized = false; |
| 292 |
|
| 293 |
/** |
| 294 |
* Precomputes the available locales which use upper-case first sorting. |
| 295 |
*/ |
| 296 |
bool ensureUpperCaseFirstLocales(JSContext* cx); |
| 297 |
#endif |
| 298 |
|
| 299 |
public: |
| 300 |
/** |
| 301 |
* Sets |isUpperFirst| to true if |locale| sorts upper-case characters |
| 302 |
* before lower-case characters. |
| 303 |
*/ |
| 304 |
bool isUpperCaseFirst(JSContext* cx, JS::Handle<JSString*> locale, |
| 305 |
bool* isUpperFirst); |
| 306 |
|
| 307 |
private: |
| 308 |
#if DEBUG || MOZ_SYSTEM_ICU |
| 309 |
LocaleSet ignorePunctuationLocales; |
| 310 |
|
| 311 |
bool ignorePunctuationInitialized = false; |
| 312 |
|
| 313 |
/** |
| 314 |
* Precomputes the available locales which ignore punctuation. |
| 315 |
*/ |
| 316 |
bool ensureIgnorePunctuationLocales(JSContext* cx); |
| 317 |
#endif |
| 318 |
|
| 319 |
public: |
| 320 |
/** |
| 321 |
* Sets |ignorePunctuation| to true if |locale| ignores punctuation. |
| 322 |
*/ |
| 323 |
bool isIgnorePunctuation(JSContext* cx, JS::Handle<JSString*> locale, |
| 324 |
bool* ignorePunctuation); |
| 325 |
|
| 326 |
private: |
| 327 |
using UniqueDateTimePatternGenerator = |
| 328 |
mozilla::UniquePtr<mozilla::intl::DateTimePatternGenerator, |
| 329 |
DateTimePatternGeneratorDeleter>; |
| 330 |
|
| 331 |
UniqueDateTimePatternGenerator dateTimePatternGenerator; |
| 332 |
JS::UniqueChars dateTimePatternGeneratorLocale; |
| 333 |
|
| 334 |
public: |
| 335 |
/** |
| 336 |
* Get a non-owned cached instance of the DateTimePatternGenerator, which is |
| 337 |
* expensive to instantiate. |
| 338 |
* |
| 339 |
* See: https://bugzilla.mozilla.org/show_bug.cgi?id=1549578 |
| 340 |
*/ |
| 341 |
mozilla::intl::DateTimePatternGenerator* getDateTimePatternGenerator( |
| 342 |
JSContext* cx, const char* locale); |
| 343 |
|
| 344 |
public: |
| 345 |
void destroyInstance(); |
| 346 |
|
| 347 |
void trace(JSTracer* trc); |
| 348 |
|
| 349 |
size_t sizeOfExcludingThis(mozilla::MallocSizeOf mallocSizeOf) const; |
| 350 |
}; |
| 351 |
|
| 352 |
} // namespace intl |
| 353 |
|
| 354 |
} // namespace js |
| 355 |
|
| 356 |
#endif /* builtin_intl_SharedIntlData_h */ |