GlobalIntlData.h (5750B)
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_GlobalIntlData_h 8 #define builtin_intl_GlobalIntlData_h 9 10 #include "gc/Barrier.h" 11 #include "js/TypeDecls.h" 12 #include "vm/JSObject.h" 13 #include "vm/StringType.h" 14 15 class JS_PUBLIC_API JSTracer; 16 17 namespace js { 18 class CollatorObject; 19 class DateTimeFormatObject; 20 class NumberFormatObject; 21 22 namespace temporal { 23 class TimeZoneObject; 24 } 25 } // namespace js 26 27 namespace js::intl { 28 29 enum class DateTimeFormatKind; 30 31 /** 32 * Cached per-global Intl data. In contrast to SharedIntlData, which is 33 * a per-runtime shared Intl cache, this cache is per-global. 34 */ 35 class GlobalIntlData { 36 /** 37 * The locale information provided by the embedding, guiding SpiderMonkey's 38 * selection of a default locale. See intl::ComputeDefaultLocale(), whose 39 * value controls the value returned by defaultLocale() that's what's 40 * *actually* used. 41 */ 42 GCPtr<JSLinearString*> realmLocale_; 43 44 /** 45 * The actual default locale. 46 */ 47 GCPtr<JSLinearString*> defaultLocale_; 48 49 /** 50 * Time zone information provided by ICU or the embedding. See 51 * temporal::ComputeSystemTimeZoneIdentifier(), whose value controls the value 52 * returned by defaultTimeZone() that's what's *actually* used. 53 */ 54 GCPtr<JSLinearString*> realmTimeZone_; 55 56 /** 57 * The actual default time zone. 58 */ 59 GCPtr<JSLinearString*> defaultTimeZone_; 60 61 /** 62 * Cached temporal::TimeZoneObject for the default time zone. 63 */ 64 GCPtr<JSObject*> defaultTimeZoneObject_; 65 66 /** 67 * Cached temporal::TimeZoneObject of the last request to create a named 68 * time zone. 69 */ 70 GCPtr<JSObject*> timeZoneObject_; 71 72 /** 73 * Locale string passed to the last call to localeCompare String method. Not 74 * necessarily the actual locale when the string can't be resolved to a 75 * supported Collator locale. 76 */ 77 GCPtr<JSLinearString*> collatorLocale_; 78 79 /** 80 * Cached Intl.Collator when String.prototype.localeCompare is called with 81 * |locales| either a |undefined| or a string, and |options| having the value 82 * |undefined|. 83 */ 84 GCPtr<JSObject*> collator_; 85 86 /** 87 * Locale string passed to the last call to toLocaleString Number method. Not 88 * necessarily the actual locale when the string can't be resolved to a 89 * supported NumberFormat locale. 90 */ 91 GCPtr<JSLinearString*> numberFormatLocale_; 92 93 /** 94 * Cached Intl.NumberFormat when Number.prototype.toLocaleString is called 95 * with |locales| either a |undefined| or a string, and |options| having the 96 * value |undefined|. 97 */ 98 GCPtr<JSObject*> numberFormat_; 99 100 /** 101 * Locale string passed to the last call to toLocale*String Date method. Not 102 * necessarily the actual locale when the string can't be resolved to a 103 * supported DateTimeFormat locale. 104 */ 105 GCPtr<JSLinearString*> dateTimeFormatLocale_; 106 107 /** 108 * Cached Intl.DateTimeFormat when Date.prototype.toLocaleString is called 109 * with |locales| either a |undefined| or a string, and |options| having the 110 * value |undefined|. 111 */ 112 GCPtr<JSObject*> dateTimeFormatToLocaleAll_; 113 114 /** 115 * Cached Intl.DateTimeFormat when Date.prototype.toLocaleDateString is called 116 * with |locales| either a |undefined| or a string, and |options| having the 117 * value |undefined|. 118 */ 119 GCPtr<JSObject*> dateTimeFormatToLocaleDate_; 120 121 /** 122 * Cached Intl.DateTimeFormat when Date.prototype.toLocaleTimeString is called 123 * with |locales| either a |undefined| or a string, and |options| having the 124 * value |undefined|. 125 */ 126 GCPtr<JSObject*> dateTimeFormatToLocaleTime_; 127 128 public: 129 /** 130 * Returns the BCP 47 language tag for the global's current locale. 131 */ 132 JSLinearString* defaultLocale(JSContext* cx); 133 134 /** 135 * Returns the IANA time zone name for the global's current time zone. 136 */ 137 JSLinearString* defaultTimeZone(JSContext* cx); 138 139 /** 140 * Get or create the time zone object for the global's current time zone. 141 */ 142 temporal::TimeZoneObject* getOrCreateDefaultTimeZone(JSContext* cx); 143 144 /** 145 * Get or create the time zone for the IANA time zone name |identifier|. 146 * |primaryIdentifier| must be the primary identifier for |identifier|, i.e. 147 * if |identifier| is a time zone link name, |primaryIdentifier| must be the 148 * link's target time zone. 149 */ 150 temporal::TimeZoneObject* getOrCreateTimeZone( 151 JSContext* cx, JS::Handle<JSLinearString*> identifier, 152 JS::Handle<JSLinearString*> primaryIdentifier); 153 154 /** 155 * Get or create the Intl.Collator instance for |locale|. The default locale 156 * is used when |locale| is null. 157 */ 158 CollatorObject* getOrCreateCollator(JSContext* cx, 159 JS::Handle<JSLinearString*> locale); 160 161 /** 162 * Get or create the Intl.NumberFormat instance for |locale|. The default 163 * locale is used when |locale| is null. 164 */ 165 NumberFormatObject* getOrCreateNumberFormat( 166 JSContext* cx, JS::Handle<JSLinearString*> locale); 167 168 /** 169 * Get or create the Intl.DateTimeFormat instance for |locale|. The default 170 * locale is used when |locale| is null. 171 */ 172 DateTimeFormatObject* getOrCreateDateTimeFormat( 173 JSContext* cx, DateTimeFormatKind kind, 174 JS::Handle<JSLinearString*> locale); 175 176 void trace(JSTracer* trc); 177 178 private: 179 bool ensureRealmLocale(JSContext* cx); 180 bool ensureRealmTimeZone(JSContext* cx); 181 182 void resetCollator(); 183 void resetNumberFormat(); 184 void resetDateTimeFormat(); 185 }; 186 187 } // namespace js::intl 188 189 #endif /* builtin_intl_GlobalIntlData_h */