Debian Codegraph @

js/src/builtin/intl/DateTimeFormat.h

raw
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_DateTimeFormat_h
8 #define builtin_intl_DateTimeFormat_h
9
10 #include "builtin/SelfHostingDefines.h"
11 #include "builtin/temporal/Calendar.h"
12 #include "builtin/temporal/TimeZone.h"
13 #include "js/Class.h"
14 #include "vm/NativeObject.h"
15
16 namespace mozilla::intl {
17 class DateTimeFormat;
18 class DateIntervalFormat;
19 } // namespace mozilla::intl
20
21 namespace js {
22
23 enum class DateTimeValueKind {
24 Number,
25 TemporalDate,
26 TemporalTime,
27 TemporalDateTime,
28 TemporalYearMonth,
29 TemporalMonthDay,
30 TemporalZonedDateTime,
31 TemporalInstant,
32 };
33
34 class DateTimeFormatObject : public NativeObject {
35 public:
36 static const JSClass class_;
37 static const JSClass& protoClass_;
38
39 static constexpr uint32_t INTERNALS_SLOT = 0;
40 static constexpr uint32_t DATE_FORMAT_SLOT = 1;
41 static constexpr uint32_t DATE_INTERVAL_FORMAT_SLOT = 2;
42 static constexpr uint32_t DATE_TIME_VALUE_KIND_SLOT = 3;
43 static constexpr uint32_t CALENDAR_SLOT = 4;
44 static constexpr uint32_t TIMEZONE_SLOT = 5;
45 static constexpr uint32_t SLOT_COUNT = 6;
46
47 static_assert(INTERNALS_SLOT == INTL_INTERNALS_OBJECT_SLOT,
48 "INTERNALS_SLOT must match self-hosting define for internals "
49 "object slot");
50
51 // Estimated memory use for UDateFormat (see IcuMemoryUsage).
52 static constexpr size_t UDateFormatEstimatedMemoryUse = 72440;
53
54 // Estimated memory use for UDateIntervalFormat (see IcuMemoryUsage).
55 static constexpr size_t UDateIntervalFormatEstimatedMemoryUse = 175646;
56
57 mozilla::intl::DateTimeFormat* getDateFormat() const {
58 const auto& slot = getFixedSlot(DATE_FORMAT_SLOT);
59 if (slot.isUndefined()) {
60 return nullptr;
61 }
62 return static_cast<mozilla::intl::DateTimeFormat*>(slot.toPrivate());
63 }
64
65 void setDateFormat(mozilla::intl::DateTimeFormat* dateFormat) {
66 setFixedSlot(DATE_FORMAT_SLOT, PrivateValue(dateFormat));
67 }
68
69 mozilla::intl::DateIntervalFormat* getDateIntervalFormat() const {
70 const auto& slot = getFixedSlot(DATE_INTERVAL_FORMAT_SLOT);
71 if (slot.isUndefined()) {
72 return nullptr;
73 }
74 return static_cast<mozilla::intl::DateIntervalFormat*>(slot.toPrivate());
75 }
76
77 void setDateIntervalFormat(
78 mozilla::intl::DateIntervalFormat* dateIntervalFormat) {
79 setFixedSlot(DATE_INTERVAL_FORMAT_SLOT, PrivateValue(dateIntervalFormat));
80 }
81
82 DateTimeValueKind getDateTimeValueKind() const {
83 const auto& slot = getFixedSlot(DATE_TIME_VALUE_KIND_SLOT);
84 if (slot.isUndefined()) {
85 return DateTimeValueKind::Number;
86 }
87 return static_cast<DateTimeValueKind>(slot.toInt32());
88 }
89
90 void setDateTimeValueKind(DateTimeValueKind kind) {
91 setFixedSlot(DATE_TIME_VALUE_KIND_SLOT,
92 Int32Value(static_cast<int32_t>(kind)));
93 }
94
95 temporal::CalendarValue getCalendar() const {
96 const auto& slot = getFixedSlot(CALENDAR_SLOT);
97 if (slot.isUndefined()) {
98 return temporal::CalendarValue();
99 }
100 return temporal::CalendarValue(slot);
101 }
102
103 void setCalendar(const temporal::CalendarValue& calendar) {
104 setFixedSlot(CALENDAR_SLOT, calendar.toSlotValue());
105 }
106
107 temporal::TimeZoneValue getTimeZone() const {
108 const auto& slot = getFixedSlot(TIMEZONE_SLOT);
109 if (slot.isUndefined()) {
110 return temporal::TimeZoneValue();
111 }
112 return temporal::TimeZoneValue(slot);
113 }
114
115 void setTimeZone(const temporal::TimeZoneValue& timeZone) {
116 setFixedSlot(TIMEZONE_SLOT, timeZone.toSlotValue());
117 }
118
119 void maybeClearCache(DateTimeValueKind kind);
120
121 private:
122 static const JSClassOps classOps_;
123 static const ClassSpec classSpec_;
124
125 static void finalize(JS::GCContext* gcx, JSObject* obj);
126 };
127
128 /**
129 * Returns a new instance of the standard built-in DateTimeFormat constructor.
130 *
131 * Usage: dateTimeFormat = intl_CreateDateTimeFormat(locales, options, required,
132 * defaults)
133 */
134 [[nodiscard]] extern bool intl_CreateDateTimeFormat(JSContext* cx,
135 unsigned argc,
136 JS::Value* vp);
137
138 /**
139 * Returns an array with the calendar type identifiers per Unicode
140 * Technical Standard 35, Unicode Locale Data Markup Language, for the
141 * supported calendars for the given locale. The default calendar is
142 * element 0.
143 *
144 * Usage: calendars = intl_availableCalendars(locale)
145 */
146 [[nodiscard]] extern bool intl_availableCalendars(JSContext* cx, unsigned argc,
147 JS::Value* vp);
148
149 /**
150 * Returns the calendar type identifier per Unicode Technical Standard 35,
151 * Unicode Locale Data Markup Language, for the default calendar for the given
152 * locale.
153 *
154 * Usage: calendar = intl_defaultCalendar(locale)
155 */
156 [[nodiscard]] extern bool intl_defaultCalendar(JSContext* cx, unsigned argc,
157 JS::Value* vp);
158
159 /**
160 * 6.4.1 IsValidTimeZoneName ( timeZone )
161 *
162 * Verifies that the given string is a valid time zone name. If it is a valid
163 * time zone name, its IANA time zone name is returned. Otherwise returns null.
164 *
165 * ES2017 Intl draft rev 4a23f407336d382ed5e3471200c690c9b020b5f3
166 *
167 * Usage: ianaTimeZone = intl_IsValidTimeZoneName(timeZone)
168 */
169 [[nodiscard]] extern bool intl_IsValidTimeZoneName(JSContext* cx, unsigned argc,
170 JS::Value* vp);
171
172 /**
173 * Return the canonicalized time zone name. Canonicalization resolves link
174 * names to their target time zones.
175 *
176 * Usage: ianaTimeZone = intl_canonicalizeTimeZone(timeZone)
177 */
178 [[nodiscard]] extern bool intl_canonicalizeTimeZone(JSContext* cx,
179 unsigned argc,
180 JS::Value* vp);
181
182 /**
183 * Return the default time zone name. The time zone name is not canonicalized.
184 *
185 * Usage: icuDefaultTimeZone = intl_defaultTimeZone()
186 */
187 [[nodiscard]] extern bool intl_defaultTimeZone(JSContext* cx, unsigned argc,
188 JS::Value* vp);
189
190 /**
191 * Return the raw offset from GMT in milliseconds for the default time zone.
192 *
193 * Usage: defaultTimeZoneOffset = intl_defaultTimeZoneOffset()
194 */
195 [[nodiscard]] extern bool intl_defaultTimeZoneOffset(JSContext* cx,
196 unsigned argc,
197 JS::Value* vp);
198
199 /**
200 * Return true if the given string is the default time zone as returned by
201 * intl_defaultTimeZone(). Otherwise return false.
202 *
203 * Usage: isIcuDefaultTimeZone = intl_isDefaultTimeZone(icuDefaultTimeZone)
204 */
205 [[nodiscard]] extern bool intl_isDefaultTimeZone(JSContext* cx, unsigned argc,
206 JS::Value* vp);
207
208 /**
209 * Returns a String value representing x (which must be a Number value)
210 * according to the effective locale and the formatting options of the
211 * given DateTimeFormat.
212 *
213 * Spec: ECMAScript Internationalization API Specification, 12.3.2.
214 *
215 * Usage: formatted = intl_FormatDateTime(dateTimeFormat, x, formatToParts)
216 */
217 [[nodiscard]] extern bool intl_FormatDateTime(JSContext* cx, unsigned argc,
218 JS::Value* vp);
219
220 /**
221 * Returns a String value representing the range between x and y (which both
222 * must be Number values) according to the effective locale and the formatting
223 * options of the given DateTimeFormat.
224 *
225 * Spec: Intl.DateTimeFormat.prototype.formatRange proposal
226 *
227 * Usage: formatted = intl_FormatDateTimeRange(dateTimeFmt, x, y, formatToParts)
228 */
229 [[nodiscard]] extern bool intl_FormatDateTimeRange(JSContext* cx, unsigned argc,
230 JS::Value* vp);
231
232 /**
233 * Extracts the resolved components from a DateTimeFormat and applies them to
234 * the object for resolved components.
235 *
236 * Usage: intl_resolveDateTimeFormatComponents(dateTimeFormat, resolved)
237 */
238 [[nodiscard]] extern bool intl_resolveDateTimeFormatComponents(JSContext* cx,
239 unsigned argc,
240 JS::Value* vp);
241
242 /**
243 * Shared `toLocaleString` implementation for Temporal objects.
244 */
245 [[nodiscard]] extern bool TemporalObjectToLocaleString(
246 JSContext* cx, const JS::CallArgs& args, JS::Handle<JSString*> required,
247 JS::Handle<JSString*> defaults,
248 JS::Handle<JS::Value> toLocaleStringTimeZone = JS::UndefinedHandleValue);
249
250 } // namespace js
251
252 #endif /* builtin_intl_DateTimeFormat_h */