| 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_temporal_Calendar_h |
| 8 |
#define builtin_temporal_Calendar_h |
| 9 |
|
| 10 |
#include "mozilla/Assertions.h" |
| 11 |
#include "mozilla/Attributes.h" |
| 12 |
|
| 13 |
#include <stdint.h> |
| 14 |
#include <string_view> |
| 15 |
|
| 16 |
#include "js/RootingAPI.h" |
| 17 |
#include "js/TypeDecls.h" |
| 18 |
#include "js/Value.h" |
| 19 |
#include "vm/NativeObject.h" |
| 20 |
|
| 21 |
class JS_PUBLIC_API JSTracer; |
| 22 |
|
| 23 |
namespace js { |
| 24 |
struct ClassSpec; |
| 25 |
} // namespace js |
| 26 |
|
| 27 |
namespace js::temporal { |
| 28 |
|
| 29 |
enum class CalendarId : int32_t { |
| 30 |
ISO8601, |
| 31 |
|
| 32 |
// Thai Buddhist solar calendar. |
| 33 |
Buddhist, |
| 34 |
|
| 35 |
// Chinese lunisolar calendar. |
| 36 |
Chinese, |
| 37 |
|
| 38 |
// Coptic calendar. |
| 39 |
Coptic, |
| 40 |
|
| 41 |
// Korean lunisolar calendar. |
| 42 |
Dangi, |
| 43 |
|
| 44 |
// Ethiopian Amete Mihret calendar. |
| 45 |
Ethiopian, |
| 46 |
|
| 47 |
// Ethiopian Amete Alem calendar. |
| 48 |
EthiopianAmeteAlem, |
| 49 |
|
| 50 |
// Gregorian calendar. |
| 51 |
Gregorian, |
| 52 |
|
| 53 |
// Hebrew lunisolar calendar. |
| 54 |
Hebrew, |
| 55 |
|
| 56 |
// Indian national calendar. |
| 57 |
Indian, |
| 58 |
|
| 59 |
// Islamic lunar calendars. |
| 60 |
Islamic, |
| 61 |
IslamicCivil, |
| 62 |
IslamicRGSA, |
| 63 |
IslamicTabular, |
| 64 |
IslamicUmmAlQura, |
| 65 |
|
| 66 |
// Japanese calendar. |
| 67 |
Japanese, |
| 68 |
|
| 69 |
// Persian solar Hijri calendar. |
| 70 |
Persian, |
| 71 |
|
| 72 |
// Republic of China (ROC) calendar. |
| 73 |
ROC, |
| 74 |
}; |
| 75 |
|
| 76 |
inline constexpr auto availableCalendars = { |
| 77 |
CalendarId::ISO8601, |
| 78 |
CalendarId::Buddhist, |
| 79 |
CalendarId::Chinese, |
| 80 |
CalendarId::Coptic, |
| 81 |
CalendarId::Dangi, |
| 82 |
CalendarId::Ethiopian, |
| 83 |
CalendarId::EthiopianAmeteAlem, |
| 84 |
CalendarId::Gregorian, |
| 85 |
CalendarId::Hebrew, |
| 86 |
CalendarId::Indian, |
| 87 |
// See Bug 1950425, this calendar is only available on Nightly due to |
| 88 |
// inconsistencies between ICU4X and ICU4C. |
| 89 |
#ifdef NIGHTLY_BUILD |
| 90 |
CalendarId::Islamic, |
| 91 |
#endif |
| 92 |
CalendarId::IslamicCivil, |
| 93 |
// See Bug 1950425, this calendar is only available on Nightly due to |
| 94 |
// inconsistencies between ICU4X and ICU4C. |
| 95 |
#ifdef NIGHTLY_BUILD |
| 96 |
CalendarId::IslamicRGSA, |
| 97 |
#endif |
| 98 |
CalendarId::IslamicTabular, |
| 99 |
// See Bug 1950425, this calendar is only available on Nightly due to |
| 100 |
// inconsistencies between ICU4X and ICU4C. |
| 101 |
#ifdef NIGHTLY_BUILD |
| 102 |
CalendarId::IslamicUmmAlQura, |
| 103 |
#endif |
| 104 |
CalendarId::Japanese, |
| 105 |
CalendarId::Persian, |
| 106 |
CalendarId::ROC, |
| 107 |
}; |
| 108 |
|
| 109 |
/** |
| 110 |
* AvailableCalendars ( ) |
| 111 |
*/ |
| 112 |
constexpr auto& AvailableCalendars() { return availableCalendars; } |
| 113 |
|
| 114 |
class CalendarObject : public NativeObject { |
| 115 |
public: |
| 116 |
static const JSClass class_; |
| 117 |
static const JSClass& protoClass_; |
| 118 |
|
| 119 |
static constexpr uint32_t IDENTIFIER_SLOT = 0; |
| 120 |
static constexpr uint32_t SLOT_COUNT = 1; |
| 121 |
|
| 122 |
CalendarId identifier() const { |
| 123 |
return static_cast<CalendarId>(getFixedSlot(IDENTIFIER_SLOT).toInt32()); |
| 124 |
} |
| 125 |
|
| 126 |
private: |
| 127 |
static const ClassSpec classSpec_; |
| 128 |
}; |
| 129 |
|
| 130 |
/** |
| 131 |
* Calendar value, which is a string containing a canonical calendar identifier. |
| 132 |
*/ |
| 133 |
class MOZ_STACK_CLASS CalendarValue final { |
| 134 |
JS::Value value_{}; |
| 135 |
|
| 136 |
public: |
| 137 |
/** |
| 138 |
* Default initialize this CalendarValue. |
| 139 |
*/ |
| 140 |
CalendarValue() = default; |
| 141 |
|
| 142 |
/** |
| 143 |
* Default initialize this CalendarValue. |
| 144 |
*/ |
| 145 |
explicit CalendarValue(const JS::Value& value) : value_(value) { |
| 146 |
MOZ_ASSERT(value.isInt32()); |
| 147 |
} |
| 148 |
|
| 149 |
/** |
| 150 |
* Initialize this CalendarValue with a canonical calendar identifier. |
| 151 |
*/ |
| 152 |
explicit CalendarValue(CalendarId calendarId) |
| 153 |
: value_(JS::Int32Value(static_cast<int32_t>(calendarId))) {} |
| 154 |
|
| 155 |
/** |
| 156 |
* Return true iff this CalendarValue is initialized with either a canonical |
| 157 |
* calendar identifier or a calendar object. |
| 158 |
*/ |
| 159 |
explicit operator bool() const { return !value_.isUndefined(); } |
| 160 |
|
| 161 |
/** |
| 162 |
* Return the slot Value representation of this CalendarValue. |
| 163 |
*/ |
| 164 |
JS::Value toSlotValue() const { return value_; } |
| 165 |
|
| 166 |
/** |
| 167 |
* Return the calendar identifier. |
| 168 |
*/ |
| 169 |
CalendarId identifier() const { |
| 170 |
return static_cast<CalendarId>(value_.toInt32()); |
| 171 |
} |
| 172 |
|
| 173 |
void trace(JSTracer* trc); |
| 174 |
|
| 175 |
JS::Value* valueDoNotUse() { return &value_; } |
| 176 |
JS::Value const* valueDoNotUse() const { return &value_; } |
| 177 |
}; |
| 178 |
|
| 179 |
struct DateDuration; |
| 180 |
struct ISODate; |
| 181 |
struct ISODateTime; |
| 182 |
class PlainDate; |
| 183 |
class PlainMonthDayObject; |
| 184 |
class PlainMonthDay; |
| 185 |
class PlainYearMonthObject; |
| 186 |
class PlainYearMonth; |
| 187 |
class CalendarFields; |
| 188 |
enum class TemporalOverflow; |
| 189 |
enum class TemporalUnit; |
| 190 |
|
| 191 |
/** |
| 192 |
* ISODaysInMonth ( year, month ) |
| 193 |
*/ |
| 194 |
int32_t ISODaysInMonth(int32_t year, int32_t month); |
| 195 |
|
| 196 |
/** |
| 197 |
* 21.4.1.12 MakeDay ( year, month, date ) |
| 198 |
*/ |
| 199 |
int32_t MakeDay(const ISODate& date); |
| 200 |
|
| 201 |
/** |
| 202 |
* 21.4.1.13 MakeDate ( day, time ) |
| 203 |
*/ |
| 204 |
int64_t MakeDate(const ISODateTime& dateTime); |
| 205 |
|
| 206 |
/** |
| 207 |
* Return the BCP 47 identifier of the calendar. |
| 208 |
*/ |
| 209 |
std::string_view CalendarIdentifier(CalendarId calendarId); |
| 210 |
|
| 211 |
/** |
| 212 |
* Return the BCP 47 identifier of the calendar. |
| 213 |
*/ |
| 214 |
inline std::string_view CalendarIdentifier(const CalendarValue& calendar) { |
| 215 |
return CalendarIdentifier(calendar.identifier()); |
| 216 |
} |
| 217 |
|
| 218 |
/** |
| 219 |
* CanonicalizeCalendar ( id ) |
| 220 |
* |
| 221 |
* Return the case-normalized calendar identifier if |id| is a built-in calendar |
| 222 |
* identifier. Otherwise throws a RangeError. |
| 223 |
*/ |
| 224 |
bool CanonicalizeCalendar(JSContext* cx, JS::Handle<JSString*> id, |
| 225 |
JS::MutableHandle<CalendarValue> result); |
| 226 |
|
| 227 |
/** |
| 228 |
* ToTemporalCalendarSlotValue ( temporalCalendarLike ) |
| 229 |
*/ |
| 230 |
bool ToTemporalCalendar(JSContext* cx, |
| 231 |
JS::Handle<JS::Value> temporalCalendarLike, |
| 232 |
JS::MutableHandle<CalendarValue> result); |
| 233 |
|
| 234 |
/** |
| 235 |
* GetTemporalCalendarWithISODefault ( item ) |
| 236 |
*/ |
| 237 |
bool GetTemporalCalendarWithISODefault(JSContext* cx, |
| 238 |
JS::Handle<JSObject*> item, |
| 239 |
JS::MutableHandle<CalendarValue> result); |
| 240 |
|
| 241 |
/** |
| 242 |
* CalendarDateAdd ( calendar, isoDate, duration, overflow ) |
| 243 |
*/ |
| 244 |
bool CalendarDateAdd(JSContext* cx, JS::Handle<CalendarValue> calendar, |
| 245 |
const ISODate& isoDate, const DateDuration& duration, |
| 246 |
TemporalOverflow overflow, ISODate* result); |
| 247 |
|
| 248 |
/** |
| 249 |
* CalendarDateUntil ( calendar, one, two, largestUnit ) |
| 250 |
*/ |
| 251 |
bool CalendarDateUntil(JSContext* cx, JS::Handle<CalendarValue> calendar, |
| 252 |
const ISODate& one, const ISODate& two, |
| 253 |
TemporalUnit largestUnit, DateDuration* result); |
| 254 |
|
| 255 |
/** |
| 256 |
* CalendarISOToDate ( calendar, isoDate ) |
| 257 |
* |
| 258 |
* When accessing the [[Era]] of the returned Calendar Date Record. |
| 259 |
*/ |
| 260 |
bool CalendarEra(JSContext* cx, JS::Handle<CalendarValue> calendar, |
| 261 |
const ISODate& date, JS::MutableHandle<JS::Value> result); |
| 262 |
|
| 263 |
/** |
| 264 |
* CalendarISOToDate ( calendar, isoDate ) |
| 265 |
* |
| 266 |
* When accessing the [[EraYear]] of the returned Calendar Date Record. |
| 267 |
*/ |
| 268 |
bool CalendarEraYear(JSContext* cx, JS::Handle<CalendarValue> calendar, |
| 269 |
const ISODate& date, JS::MutableHandle<JS::Value> result); |
| 270 |
/** |
| 271 |
* CalendarISOToDate ( calendar, isoDate ) |
| 272 |
* |
| 273 |
* When accessing the [[Year]] of the returned Calendar Date Record. |
| 274 |
*/ |
| 275 |
bool CalendarYear(JSContext* cx, JS::Handle<CalendarValue> calendar, |
| 276 |
const ISODate& date, JS::MutableHandle<JS::Value> result); |
| 277 |
|
| 278 |
/** |
| 279 |
* CalendarISOToDate ( calendar, isoDate ) |
| 280 |
* |
| 281 |
* When accessing the [[Month]] of the returned Calendar Date Record. |
| 282 |
*/ |
| 283 |
bool CalendarMonth(JSContext* cx, JS::Handle<CalendarValue> calendar, |
| 284 |
const ISODate& date, JS::MutableHandle<JS::Value> result); |
| 285 |
|
| 286 |
/** |
| 287 |
* CalendarISOToDate ( calendar, isoDate ) |
| 288 |
* |
| 289 |
* When accessing the [[MonthCode]] of the returned Calendar Date Record. |
| 290 |
*/ |
| 291 |
bool CalendarMonthCode(JSContext* cx, JS::Handle<CalendarValue> calendar, |
| 292 |
const ISODate& date, |
| 293 |
JS::MutableHandle<JS::Value> result); |
| 294 |
|
| 295 |
/** |
| 296 |
* CalendarISOToDate ( calendar, isoDate ) |
| 297 |
* |
| 298 |
* When accessing the [[Day]] of the returned Calendar Date Record. |
| 299 |
*/ |
| 300 |
bool CalendarDay(JSContext* cx, JS::Handle<CalendarValue> calendar, |
| 301 |
const ISODate& date, JS::MutableHandle<JS::Value> result); |
| 302 |
|
| 303 |
/** |
| 304 |
* CalendarISOToDate ( calendar, isoDate ) |
| 305 |
* |
| 306 |
* When accessing the [[DayOfWeek]] of the returned Calendar Date Record. |
| 307 |
*/ |
| 308 |
bool CalendarDayOfWeek(JSContext* cx, JS::Handle<CalendarValue> calendar, |
| 309 |
const ISODate& date, |
| 310 |
JS::MutableHandle<JS::Value> result); |
| 311 |
|
| 312 |
/** |
| 313 |
* CalendarISOToDate ( calendar, isoDate ) |
| 314 |
* |
| 315 |
* When accessing the [[DayOfYear]] of the returned Calendar Date Record. |
| 316 |
*/ |
| 317 |
bool CalendarDayOfYear(JSContext* cx, JS::Handle<CalendarValue> calendar, |
| 318 |
const ISODate& date, |
| 319 |
JS::MutableHandle<JS::Value> result); |
| 320 |
|
| 321 |
/** |
| 322 |
* CalendarISOToDate ( calendar, isoDate ) |
| 323 |
* |
| 324 |
* When accessing the [[Week]] field of the [[WeekOfYear]] of the returned |
| 325 |
* Calendar Date Record. |
| 326 |
*/ |
| 327 |
bool CalendarWeekOfYear(JSContext* cx, JS::Handle<CalendarValue> calendar, |
| 328 |
const ISODate& date, |
| 329 |
JS::MutableHandle<JS::Value> result); |
| 330 |
|
| 331 |
/** |
| 332 |
* CalendarISOToDate ( calendar, isoDate ) |
| 333 |
* |
| 334 |
* When accessing the [[Year]] field of the [[WeekOfYear]] of the returned |
| 335 |
* Calendar Date Record. |
| 336 |
*/ |
| 337 |
bool CalendarYearOfWeek(JSContext* cx, JS::Handle<CalendarValue> calendar, |
| 338 |
const ISODate& date, |
| 339 |
JS::MutableHandle<JS::Value> result); |
| 340 |
|
| 341 |
/** |
| 342 |
* * CalendarISOToDate ( calendar, isoDate ) |
| 343 |
* |
| 344 |
* When accessing the [[DaysInWeek]] of the returned Calendar Date Record. |
| 345 |
*/ |
| 346 |
bool CalendarDaysInWeek(JSContext* cx, JS::Handle<CalendarValue> calendar, |
| 347 |
const ISODate& date, |
| 348 |
JS::MutableHandle<JS::Value> result); |
| 349 |
|
| 350 |
/** |
| 351 |
* * CalendarISOToDate ( calendar, isoDate ) |
| 352 |
* |
| 353 |
* When accessing the [[DaysInMonth]] of the returned Calendar Date Record. |
| 354 |
*/ |
| 355 |
bool CalendarDaysInMonth(JSContext* cx, JS::Handle<CalendarValue> calendar, |
| 356 |
const ISODate& date, |
| 357 |
JS::MutableHandle<JS::Value> result); |
| 358 |
|
| 359 |
/** |
| 360 |
* CalendarISOToDate ( calendar, isoDate ) |
| 361 |
* |
| 362 |
* When accessing the [[DaysInYear]] of the returned Calendar Date Record. |
| 363 |
*/ |
| 364 |
bool CalendarDaysInYear(JSContext* cx, JS::Handle<CalendarValue> calendar, |
| 365 |
const ISODate& date, |
| 366 |
JS::MutableHandle<JS::Value> result); |
| 367 |
|
| 368 |
/** |
| 369 |
* * CalendarISOToDate ( calendar, isoDate ) |
| 370 |
* |
| 371 |
* When accessing the [[MonthsInYear]] of the returned Calendar Date Record. |
| 372 |
*/ |
| 373 |
bool CalendarMonthsInYear(JSContext* cx, JS::Handle<CalendarValue> calendar, |
| 374 |
const ISODate& date, |
| 375 |
JS::MutableHandle<JS::Value> result); |
| 376 |
|
| 377 |
/** |
| 378 |
* CalendarISOToDate ( calendar, isoDate ) |
| 379 |
* |
| 380 |
* When accessing the [[InLeapYear]] of the returned Calendar Date Record. |
| 381 |
*/ |
| 382 |
bool CalendarInLeapYear(JSContext* cx, JS::Handle<CalendarValue> calendar, |
| 383 |
const ISODate& date, |
| 384 |
JS::MutableHandle<JS::Value> result); |
| 385 |
|
| 386 |
/** |
| 387 |
* CalendarDateFromFields ( calendar, fields, overflow ) |
| 388 |
*/ |
| 389 |
bool CalendarDateFromFields(JSContext* cx, JS::Handle<CalendarValue> calendar, |
| 390 |
JS::Handle<CalendarFields> fields, |
| 391 |
TemporalOverflow overflow, |
| 392 |
MutableHandle<PlainDate> result); |
| 393 |
|
| 394 |
/** |
| 395 |
* CalendarYearMonthFromFields ( calendar, fields, overflow ) |
| 396 |
*/ |
| 397 |
bool CalendarYearMonthFromFields(JSContext* cx, |
| 398 |
JS::Handle<CalendarValue> calendar, |
| 399 |
JS::Handle<CalendarFields> fields, |
| 400 |
TemporalOverflow overflow, |
| 401 |
JS::MutableHandle<PlainYearMonth> result); |
| 402 |
|
| 403 |
/** |
| 404 |
* CalendarMonthDayFromFields ( calendar, fields, overflow ) |
| 405 |
*/ |
| 406 |
bool CalendarMonthDayFromFields(JSContext* cx, |
| 407 |
JS::Handle<CalendarValue> calendar, |
| 408 |
JS::Handle<CalendarFields> fields, |
| 409 |
TemporalOverflow overflow, |
| 410 |
JS::MutableHandle<PlainMonthDay> result); |
| 411 |
|
| 412 |
/** |
| 413 |
* CalendarEquals ( one, two ) |
| 414 |
*/ |
| 415 |
inline bool CalendarEquals(const CalendarValue& one, const CalendarValue& two) { |
| 416 |
// Steps 1-2. |
| 417 |
return one.identifier() == two.identifier(); |
| 418 |
} |
| 419 |
|
| 420 |
// Helper for MutableWrappedPtrOperations. |
| 421 |
bool WrapCalendarValue(JSContext* cx, JS::MutableHandle<JS::Value> calendar); |
| 422 |
|
| 423 |
} /* namespace js::temporal */ |
| 424 |
|
| 425 |
namespace js { |
| 426 |
|
| 427 |
template <typename Wrapper> |
| 428 |
class WrappedPtrOperations<temporal::CalendarValue, Wrapper> { |
| 429 |
const auto& container() const { |
| 430 |
return static_cast<const Wrapper*>(this)->get(); |
| 431 |
} |
| 432 |
|
| 433 |
public: |
| 434 |
explicit operator bool() const { return bool(container()); } |
| 435 |
|
| 436 |
JS::Handle<JS::Value> toSlotValue() const { |
| 437 |
return JS::Handle<JS::Value>::fromMarkedLocation( |
| 438 |
container().valueDoNotUse()); |
| 439 |
} |
| 440 |
|
| 441 |
temporal::CalendarId identifier() const { return container().identifier(); } |
| 442 |
}; |
| 443 |
|
| 444 |
template <typename Wrapper> |
| 445 |
class MutableWrappedPtrOperations<temporal::CalendarValue, Wrapper> |
| 446 |
: public WrappedPtrOperations<temporal::CalendarValue, Wrapper> { |
| 447 |
auto& container() { return static_cast<Wrapper*>(this)->get(); } |
| 448 |
|
| 449 |
JS::MutableHandle<JS::Value> toMutableValue() { |
| 450 |
return JS::MutableHandle<JS::Value>::fromMarkedLocation( |
| 451 |
container().valueDoNotUse()); |
| 452 |
} |
| 453 |
|
| 454 |
public: |
| 455 |
bool wrap(JSContext* cx) { |
| 456 |
return temporal::WrapCalendarValue(cx, toMutableValue()); |
| 457 |
} |
| 458 |
}; |
| 459 |
|
| 460 |
} /* namespace js */ |
| 461 |
|
| 462 |
#endif /* builtin_temporal_Calendar_h */ |