| 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 |
#include "builtin/temporal/CalendarFields.h" |
| 8 |
|
| 9 |
#include "mozilla/Assertions.h" |
| 10 |
#include "mozilla/EnumTypeTraits.h" |
| 11 |
#include "mozilla/Maybe.h" |
| 12 |
#include "mozilla/Range.h" |
| 13 |
#include "mozilla/TextUtils.h" |
| 14 |
|
| 15 |
#include <stdint.h> |
| 16 |
#include <string_view> |
| 17 |
|
| 18 |
#include "jspubtd.h" |
| 19 |
#include "NamespaceImports.h" |
| 20 |
|
| 21 |
#include "builtin/temporal/Calendar.h" |
| 22 |
#include "builtin/temporal/Crash.h" |
| 23 |
#include "builtin/temporal/Era.h" |
| 24 |
#include "builtin/temporal/Temporal.h" |
| 25 |
#include "builtin/temporal/TemporalParser.h" |
| 26 |
#include "builtin/temporal/TimeZone.h" |
| 27 |
#include "gc/Barrier.h" |
| 28 |
#include "gc/Tracer.h" |
| 29 |
#include "js/Conversions.h" |
| 30 |
#include "js/ErrorReport.h" |
| 31 |
#include "js/friend/ErrorMessages.h" |
| 32 |
#include "js/GCAPI.h" |
| 33 |
#include "js/Printer.h" |
| 34 |
#include "js/RootingAPI.h" |
| 35 |
#include "js/Value.h" |
| 36 |
#include "util/Text.h" |
| 37 |
#include "vm/BytecodeUtil.h" |
| 38 |
#include "vm/JSAtomState.h" |
| 39 |
#include "vm/JSContext.h" |
| 40 |
#include "vm/JSObject.h" |
| 41 |
#include "vm/StringType.h" |
| 42 |
|
| 43 |
#include "vm/JSObject-inl.h" |
| 44 |
#include "vm/ObjectOperations-inl.h" |
| 45 |
|
| 46 |
using namespace js; |
| 47 |
using namespace js::temporal; |
| 48 |
|
| 49 |
void CalendarFields::trace(JSTracer* trc) { |
| 50 |
TraceNullableRoot(trc, &era_, "CalendarFields::era"); |
| 51 |
timeZone_.trace(trc); |
| 52 |
} |
| 53 |
|
| 54 |
void CalendarFields::setFrom(CalendarField field, |
| 55 |
const CalendarFields& source) { |
| 56 |
MOZ_ASSERT(source.has(field)); |
| 57 |
|
| 58 |
switch (field) { |
| 59 |
case CalendarField::Era: |
| 60 |
setEra(source.era()); |
| 61 |
return; |
| 62 |
case CalendarField::EraYear: |
| 63 |
setEraYear(source.eraYear()); |
| 64 |
return; |
| 65 |
case CalendarField::Year: |
| 66 |
setYear(source.year()); |
| 67 |
return; |
| 68 |
case CalendarField::Month: |
| 69 |
setMonth(source.month()); |
| 70 |
return; |
| 71 |
case CalendarField::MonthCode: |
| 72 |
setMonthCode(source.monthCode()); |
| 73 |
return; |
| 74 |
case CalendarField::Day: |
| 75 |
setDay(source.day()); |
| 76 |
return; |
| 77 |
case CalendarField::Hour: |
| 78 |
setHour(source.hour()); |
| 79 |
return; |
| 80 |
case CalendarField::Minute: |
| 81 |
setMinute(source.minute()); |
| 82 |
return; |
| 83 |
case CalendarField::Second: |
| 84 |
setSecond(source.second()); |
| 85 |
return; |
| 86 |
case CalendarField::Millisecond: |
| 87 |
setMillisecond(source.millisecond()); |
| 88 |
return; |
| 89 |
case CalendarField::Microsecond: |
| 90 |
setMicrosecond(source.microsecond()); |
| 91 |
return; |
| 92 |
case CalendarField::Nanosecond: |
| 93 |
setNanosecond(source.nanosecond()); |
| 94 |
return; |
| 95 |
case CalendarField::Offset: |
| 96 |
setOffset(source.offset()); |
| 97 |
return; |
| 98 |
case CalendarField::TimeZone: |
| 99 |
setTimeZone(source.timeZone()); |
| 100 |
return; |
| 101 |
} |
| 102 |
MOZ_CRASH("invalid temporal field"); |
| 103 |
} |
| 104 |
|
| 105 |
static PropertyName* ToPropertyName(JSContext* cx, CalendarField field) { |
| 106 |
switch (field) { |
| 107 |
case CalendarField::Era: |
| 108 |
return cx->names().era; |
| 109 |
case CalendarField::EraYear: |
| 110 |
return cx->names().eraYear; |
| 111 |
case CalendarField::Year: |
| 112 |
return cx->names().year; |
| 113 |
case CalendarField::Month: |
| 114 |
return cx->names().month; |
| 115 |
case CalendarField::MonthCode: |
| 116 |
return cx->names().monthCode; |
| 117 |
case CalendarField::Day: |
| 118 |
return cx->names().day; |
| 119 |
case CalendarField::Hour: |
| 120 |
return cx->names().hour; |
| 121 |
case CalendarField::Minute: |
| 122 |
return cx->names().minute; |
| 123 |
case CalendarField::Second: |
| 124 |
return cx->names().second; |
| 125 |
case CalendarField::Millisecond: |
| 126 |
return cx->names().millisecond; |
| 127 |
case CalendarField::Microsecond: |
| 128 |
return cx->names().microsecond; |
| 129 |
case CalendarField::Nanosecond: |
| 130 |
return cx->names().nanosecond; |
| 131 |
case CalendarField::Offset: |
| 132 |
return cx->names().offset; |
| 133 |
case CalendarField::TimeZone: |
| 134 |
return cx->names().timeZone; |
| 135 |
} |
| 136 |
MOZ_CRASH("invalid temporal field name"); |
| 137 |
} |
| 138 |
|
| 139 |
static constexpr const char* ToCString(CalendarField field) { |
| 140 |
switch (field) { |
| 141 |
case CalendarField::Era: |
| 142 |
return "era"; |
| 143 |
case CalendarField::EraYear: |
| 144 |
return "eraYear"; |
| 145 |
case CalendarField::Year: |
| 146 |
return "year"; |
| 147 |
case CalendarField::Month: |
| 148 |
return "month"; |
| 149 |
case CalendarField::MonthCode: |
| 150 |
return "monthCode"; |
| 151 |
case CalendarField::Day: |
| 152 |
return "day"; |
| 153 |
case CalendarField::Hour: |
| 154 |
return "hour"; |
| 155 |
case CalendarField::Minute: |
| 156 |
return "minute"; |
| 157 |
case CalendarField::Second: |
| 158 |
return "second"; |
| 159 |
case CalendarField::Millisecond: |
| 160 |
return "millisecond"; |
| 161 |
case CalendarField::Microsecond: |
| 162 |
return "microsecond"; |
| 163 |
case CalendarField::Nanosecond: |
| 164 |
return "nanosecond"; |
| 165 |
case CalendarField::Offset: |
| 166 |
return "offset"; |
| 167 |
case CalendarField::TimeZone: |
| 168 |
return "timeZone"; |
| 169 |
} |
| 170 |
JS_CONSTEXPR_CRASH("invalid temporal field name"); |
| 171 |
} |
| 172 |
|
| 173 |
static constexpr bool CalendarFieldsAreSorted() { |
| 174 |
constexpr auto min = mozilla::ContiguousEnumValues<CalendarField>::min; |
| 175 |
constexpr auto max = mozilla::ContiguousEnumValues<CalendarField>::max; |
| 176 |
|
| 177 |
auto field = min; |
| 178 |
while (field != max) { |
| 179 |
auto next = static_cast<CalendarField>(mozilla::UnderlyingValue(field) + 1); |
| 180 |
|
| 181 |
auto a = std::string_view{ToCString(field)}; |
| 182 |
auto b = std::string_view{ToCString(next)}; |
| 183 |
if (a.compare(b) >= 0) { |
| 184 |
return false; |
| 185 |
} |
| 186 |
field = next; |
| 187 |
} |
| 188 |
return true; |
| 189 |
} |
| 190 |
|
| 191 |
/** |
| 192 |
* CalendarExtraFields ( calendar, fields ) |
| 193 |
*/ |
| 194 |
static mozilla::EnumSet<CalendarField> CalendarExtraFields( |
| 195 |
CalendarId calendar, mozilla::EnumSet<CalendarField> fields) { |
| 196 |
// Step 1. |
| 197 |
if (calendar == CalendarId::ISO8601) { |
| 198 |
return {}; |
| 199 |
} |
| 200 |
|
| 201 |
// Step 2. |
| 202 |
|
| 203 |
// "era" and "eraYear" are relevant for calendars with multiple eras when |
| 204 |
// "year" is present. |
| 205 |
if (fields.contains(CalendarField::Year) && CalendarEraRelevant(calendar)) { |
| 206 |
return {CalendarField::Era, CalendarField::EraYear}; |
| 207 |
} |
| 208 |
return {}; |
| 209 |
} |
| 210 |
|
| 211 |
/** |
| 212 |
* ToMonthCode ( argument ) |
| 213 |
*/ |
| 214 |
template <typename CharT> |
| 215 |
static mozilla::Maybe<MonthCodeField> ToMonthCode( |
| 216 |
mozilla::Range<const CharT> chars) { |
| 217 |
// Steps 1-2. (Not applicable) |
| 218 |
|
| 219 |
// Step 3. |
| 220 |
// |
| 221 |
// Caller is responsible to ensure the string has the correct length. |
| 222 |
MOZ_ASSERT(chars.length() >= 3 && chars.length() <= 4); |
| 223 |
|
| 224 |
// Steps 4 and 7. |
| 225 |
// |
| 226 |
// Starts with capital letter 'M'. Leap months end with capital letter 'L'. |
| 227 |
bool isLeapMonth = chars.length() == 4; |
| 228 |
if (chars[0] != 'M' || (isLeapMonth && chars[3] != 'L')) { |
| 229 |
return mozilla::Nothing(); |
| 230 |
} |
| 231 |
|
| 232 |
// Steps 5-6. |
| 233 |
// |
| 234 |
// Month numbers are ASCII digits. |
| 235 |
if (!mozilla::IsAsciiDigit(chars[1]) || !mozilla::IsAsciiDigit(chars[2])) { |
| 236 |
return mozilla::Nothing(); |
| 237 |
} |
| 238 |
|
| 239 |
// Steps 8-9. |
| 240 |
int32_t ordinal = |
| 241 |
AsciiDigitToNumber(chars[1]) * 10 + AsciiDigitToNumber(chars[2]); |
| 242 |
|
| 243 |
// Step 10. |
| 244 |
if (ordinal == 0 && !isLeapMonth) { |
| 245 |
return mozilla::Nothing(); |
| 246 |
} |
| 247 |
|
| 248 |
// Step 11. |
| 249 |
return mozilla::Some(MonthCodeField{ordinal, isLeapMonth}); |
| 250 |
} |
| 251 |
|
| 252 |
/** |
| 253 |
* ToMonthCode ( argument ) |
| 254 |
*/ |
| 255 |
static auto ToMonthCode(const JSLinearString* linear) { |
| 256 |
JS::AutoCheckCannotGC nogc; |
| 257 |
|
| 258 |
if (linear->hasLatin1Chars()) { |
| 259 |
return ToMonthCode(linear->latin1Range(nogc)); |
| 260 |
} |
| 261 |
return ToMonthCode(linear->twoByteRange(nogc)); |
| 262 |
} |
| 263 |
|
| 264 |
/** |
| 265 |
* ToMonthCode ( argument ) |
| 266 |
*/ |
| 267 |
static bool ToMonthCode(JSContext* cx, Handle<Value> value, |
| 268 |
MonthCodeField* result) { |
| 269 |
auto reportInvalidMonthCode = [&](JSLinearString* monthCode) { |
| 270 |
if (auto code = QuoteString(cx, monthCode)) { |
| 271 |
JS_ReportErrorNumberUTF8(cx, GetErrorMessage, nullptr, |
| 272 |
JSMSG_TEMPORAL_CALENDAR_INVALID_MONTHCODE, |
| 273 |
code.get()); |
| 274 |
} |
| 275 |
return false; |
| 276 |
}; |
| 277 |
|
| 278 |
// Step 1. |
| 279 |
Rooted<Value> monthCode(cx, value); |
| 280 |
if (!ToPrimitive(cx, JSTYPE_STRING, &monthCode)) { |
| 281 |
return false; |
| 282 |
} |
| 283 |
|
| 284 |
// Step 2. |
| 285 |
if (!monthCode.isString()) { |
| 286 |
ReportValueError(cx, JSMSG_UNEXPECTED_TYPE, JSDVG_IGNORE_STACK, monthCode, |
| 287 |
nullptr, "not a string"); |
| 288 |
return false; |
| 289 |
} |
| 290 |
|
| 291 |
JSLinearString* monthCodeStr = monthCode.toString()->ensureLinear(cx); |
| 292 |
if (!monthCodeStr) { |
| 293 |
return false; |
| 294 |
} |
| 295 |
|
| 296 |
// Step 3. |
| 297 |
if (monthCodeStr->length() < 3 || monthCodeStr->length() > 4) { |
| 298 |
return reportInvalidMonthCode(monthCodeStr); |
| 299 |
} |
| 300 |
|
| 301 |
// Steps 4-11. |
| 302 |
auto parsed = ToMonthCode(monthCodeStr); |
| 303 |
if (!parsed) { |
| 304 |
return reportInvalidMonthCode(monthCodeStr); |
| 305 |
} |
| 306 |
|
| 307 |
*result = *parsed; |
| 308 |
return true; |
| 309 |
} |
| 310 |
|
| 311 |
/** |
| 312 |
* ToOffsetString ( argument ) |
| 313 |
*/ |
| 314 |
static bool ToOffsetString(JSContext* cx, Handle<Value> value, |
| 315 |
int64_t* result) { |
| 316 |
// Step 1. |
| 317 |
Rooted<Value> offset(cx, value); |
| 318 |
if (!ToPrimitive(cx, JSTYPE_STRING, &offset)) { |
| 319 |
return false; |
| 320 |
} |
| 321 |
|
| 322 |
// Step 2. |
| 323 |
if (!offset.isString()) { |
| 324 |
ReportValueError(cx, JSMSG_UNEXPECTED_TYPE, JSDVG_IGNORE_STACK, offset, |
| 325 |
nullptr, "not a string"); |
| 326 |
return false; |
| 327 |
} |
| 328 |
Rooted<JSString*> offsetStr(cx, offset.toString()); |
| 329 |
|
| 330 |
// Steps 3-4. |
| 331 |
return ParseDateTimeUTCOffset(cx, offsetStr, result); |
| 332 |
} |
| 333 |
|
| 334 |
enum class Partial : bool { No, Yes }; |
| 335 |
|
| 336 |
/** |
| 337 |
* PrepareCalendarFields ( calendar, fields, calendarFieldNames, |
| 338 |
* nonCalendarFieldNames, requiredFieldNames ) |
| 339 |
*/ |
| 340 |
static bool PrepareCalendarFields( |
| 341 |
JSContext* cx, Handle<CalendarValue> calendar, Handle<JSObject*> fields, |
| 342 |
mozilla::EnumSet<CalendarField> fieldNames, |
| 343 |
mozilla::EnumSet<CalendarField> requiredFields, Partial partial, |
| 344 |
MutableHandle<CalendarFields> result) { |
| 345 |
MOZ_ASSERT_IF(partial == Partial::Yes, requiredFields.isEmpty()); |
| 346 |
|
| 347 |
// Steps 1-2. (Not applicable in our implementation.) |
| 348 |
|
| 349 |
// Step 3. |
| 350 |
auto extraFieldNames = CalendarExtraFields(calendar.identifier(), fieldNames); |
| 351 |
|
| 352 |
// Step 4. |
| 353 |
fieldNames += extraFieldNames; |
| 354 |
|
| 355 |
// Step 5. (Not applicable in our implementation.) |
| 356 |
|
| 357 |
// Step 6. |
| 358 |
// |
| 359 |
// Default initialize the result. |
| 360 |
result.set(CalendarFields{}); |
| 361 |
|
| 362 |
// Step 7. (Not applicable in our implementation.) |
| 363 |
|
| 364 |
// Step 8. |
| 365 |
static_assert(CalendarFieldsAreSorted(), |
| 366 |
"EnumSet<CalendarField> iteration is sorted"); |
| 367 |
|
| 368 |
// Step 9. |
| 369 |
Rooted<Value> value(cx); |
| 370 |
for (auto fieldName : fieldNames) { |
| 371 |
auto* propertyName = ToPropertyName(cx, fieldName); |
| 372 |
const auto* cstr = ToCString(fieldName); |
| 373 |
|
| 374 |
// Step 9.a. (Not applicable in our implementation.) |
| 375 |
|
| 376 |
// Step 9.b. |
| 377 |
if (!GetProperty(cx, fields, fields, propertyName, &value)) { |
| 378 |
return false; |
| 379 |
} |
| 380 |
|
| 381 |
// Steps 9.c-d. |
| 382 |
if (!value.isUndefined()) { |
| 383 |
// Steps 9.c.i-ii. (Not applicable in our implementation.) |
| 384 |
|
| 385 |
// Steps 9.c.iii-ix. |
| 386 |
switch (fieldName) { |
| 387 |
case CalendarField::Era: { |
| 388 |
JSString* era = ToString(cx, value); |
| 389 |
if (!era) { |
| 390 |
return false; |
| 391 |
} |
| 392 |
result.setEra(era); |
| 393 |
break; |
| 394 |
} |
| 395 |
case CalendarField::EraYear: { |
| 396 |
double eraYear; |
| 397 |
if (!ToIntegerWithTruncation(cx, value, cstr, &eraYear)) { |
| 398 |
return false; |
| 399 |
} |
| 400 |
result.setEraYear(eraYear); |
| 401 |
break; |
| 402 |
} |
| 403 |
case CalendarField::Year: { |
| 404 |
double year; |
| 405 |
if (!ToIntegerWithTruncation(cx, value, cstr, &year)) { |
| 406 |
return false; |
| 407 |
} |
| 408 |
result.setYear(year); |
| 409 |
break; |
| 410 |
} |
| 411 |
case CalendarField::Month: { |
| 412 |
double month; |
| 413 |
if (!ToPositiveIntegerWithTruncation(cx, value, cstr, &month)) { |
| 414 |
return false; |
| 415 |
} |
| 416 |
result.setMonth(month); |
| 417 |
break; |
| 418 |
} |
| 419 |
case CalendarField::MonthCode: { |
| 420 |
MonthCodeField monthCode; |
| 421 |
if (!ToMonthCode(cx, value, &monthCode)) { |
| 422 |
return false; |
| 423 |
} |
| 424 |
result.setMonthCode(monthCode); |
| 425 |
break; |
| 426 |
} |
| 427 |
case CalendarField::Day: { |
| 428 |
double day; |
| 429 |
if (!ToPositiveIntegerWithTruncation(cx, value, cstr, &day)) { |
| 430 |
return false; |
| 431 |
} |
| 432 |
result.setDay(day); |
| 433 |
break; |
| 434 |
} |
| 435 |
case CalendarField::Hour: { |
| 436 |
double hour; |
| 437 |
if (!ToIntegerWithTruncation(cx, value, cstr, &hour)) { |
| 438 |
return false; |
| 439 |
} |
| 440 |
result.setHour(hour); |
| 441 |
break; |
| 442 |
} |
| 443 |
case CalendarField::Minute: { |
| 444 |
double minute; |
| 445 |
if (!ToIntegerWithTruncation(cx, value, cstr, &minute)) { |
| 446 |
return false; |
| 447 |
} |
| 448 |
result.setMinute(minute); |
| 449 |
break; |
| 450 |
} |
| 451 |
case CalendarField::Second: { |
| 452 |
double second; |
| 453 |
if (!ToIntegerWithTruncation(cx, value, cstr, &second)) { |
| 454 |
return false; |
| 455 |
} |
| 456 |
result.setSecond(second); |
| 457 |
break; |
| 458 |
} |
| 459 |
case CalendarField::Millisecond: { |
| 460 |
double millisecond; |
| 461 |
if (!ToIntegerWithTruncation(cx, value, cstr, &millisecond)) { |
| 462 |
return false; |
| 463 |
} |
| 464 |
result.setMillisecond(millisecond); |
| 465 |
break; |
| 466 |
} |
| 467 |
case CalendarField::Microsecond: { |
| 468 |
double microsecond; |
| 469 |
if (!ToIntegerWithTruncation(cx, value, cstr, µsecond)) { |
| 470 |
return false; |
| 471 |
} |
| 472 |
result.setMicrosecond(microsecond); |
| 473 |
break; |
| 474 |
} |
| 475 |
case CalendarField::Nanosecond: { |
| 476 |
double nanosecond; |
| 477 |
if (!ToIntegerWithTruncation(cx, value, cstr, &nanosecond)) { |
| 478 |
return false; |
| 479 |
} |
| 480 |
result.setNanosecond(nanosecond); |
| 481 |
break; |
| 482 |
} |
| 483 |
case CalendarField::Offset: { |
| 484 |
int64_t offset; |
| 485 |
if (!ToOffsetString(cx, value, &offset)) { |
| 486 |
return false; |
| 487 |
} |
| 488 |
result.setOffset(OffsetField{offset}); |
| 489 |
break; |
| 490 |
} |
| 491 |
case CalendarField::TimeZone: |
| 492 |
Rooted<TimeZoneValue> timeZone(cx); |
| 493 |
if (!ToTemporalTimeZone(cx, value, &timeZone)) { |
| 494 |
return false; |
| 495 |
} |
| 496 |
result.setTimeZone(timeZone); |
| 497 |
break; |
| 498 |
} |
| 499 |
} else if (partial == Partial::No) { |
| 500 |
// Step 9.d.i. |
| 501 |
if (requiredFields.contains(fieldName)) { |
| 502 |
JS_ReportErrorNumberASCII(cx, GetErrorMessage, nullptr, |
| 503 |
JSMSG_TEMPORAL_MISSING_PROPERTY, cstr); |
| 504 |
return false; |
| 505 |
} |
| 506 |
|
| 507 |
// Step 9.d.ii. |
| 508 |
result.setDefault(fieldName); |
| 509 |
} |
| 510 |
} |
| 511 |
|
| 512 |
// Step 10. |
| 513 |
if (partial == Partial::Yes && result.keys().isEmpty()) { |
| 514 |
JS_ReportErrorNumberASCII(cx, GetErrorMessage, nullptr, |
| 515 |
JSMSG_TEMPORAL_MISSING_TEMPORAL_FIELDS); |
| 516 |
return false; |
| 517 |
} |
| 518 |
|
| 519 |
// Step 11. |
| 520 |
return true; |
| 521 |
} |
| 522 |
|
| 523 |
/** |
| 524 |
* PrepareCalendarFields ( calendar, fields, calendarFieldNames, |
| 525 |
* nonCalendarFieldNames, requiredFieldNames ) |
| 526 |
*/ |
| 527 |
bool js::temporal::PrepareCalendarFields( |
| 528 |
JSContext* cx, Handle<CalendarValue> calendar, Handle<JSObject*> fields, |
| 529 |
mozilla::EnumSet<CalendarField> fieldNames, |
| 530 |
mozilla::EnumSet<CalendarField> requiredFields, |
| 531 |
MutableHandle<CalendarFields> result) { |
| 532 |
return PrepareCalendarFields(cx, calendar, fields, fieldNames, requiredFields, |
| 533 |
Partial::No, result); |
| 534 |
} |
| 535 |
|
| 536 |
/** |
| 537 |
* PrepareCalendarFields ( calendar, fields, calendarFieldNames, |
| 538 |
* nonCalendarFieldNames, requiredFieldNames ) |
| 539 |
*/ |
| 540 |
bool js::temporal::PreparePartialCalendarFields( |
| 541 |
JSContext* cx, Handle<CalendarValue> calendar, Handle<JSObject*> fields, |
| 542 |
mozilla::EnumSet<CalendarField> fieldNames, |
| 543 |
JS::MutableHandle<CalendarFields> result) { |
| 544 |
return PrepareCalendarFields(cx, calendar, fields, fieldNames, {}, |
| 545 |
Partial::Yes, result); |
| 546 |
} |
| 547 |
|
| 548 |
/** |
| 549 |
* CalendarFieldKeysToIgnore ( calendar, keys ) |
| 550 |
*/ |
| 551 |
static auto CalendarFieldKeysToIgnore(CalendarId calendar, |
| 552 |
mozilla::EnumSet<CalendarField> keys) { |
| 553 |
// Step 1. |
| 554 |
if (calendar == CalendarId::ISO8601) { |
| 555 |
// Steps 1.a and 1.b.i. |
| 556 |
auto ignoredKeys = keys; |
| 557 |
|
| 558 |
// Step 1.b.ii. |
| 559 |
if (keys.contains(CalendarField::Month)) { |
| 560 |
ignoredKeys += CalendarField::MonthCode; |
| 561 |
} |
| 562 |
|
| 563 |
// Step 1.b.iii. |
| 564 |
else if (keys.contains(CalendarField::MonthCode)) { |
| 565 |
ignoredKeys += CalendarField::Month; |
| 566 |
} |
| 567 |
|
| 568 |
// Steps 1.c-d. |
| 569 |
return ignoredKeys; |
| 570 |
} |
| 571 |
|
| 572 |
// Step 2. |
| 573 |
|
| 574 |
static constexpr auto eraOrEraYear = mozilla::EnumSet{ |
| 575 |
CalendarField::Era, |
| 576 |
CalendarField::EraYear, |
| 577 |
}; |
| 578 |
|
| 579 |
static constexpr auto eraOrAnyYear = mozilla::EnumSet{ |
| 580 |
CalendarField::Era, |
| 581 |
CalendarField::EraYear, |
| 582 |
CalendarField::Year, |
| 583 |
}; |
| 584 |
|
| 585 |
static constexpr auto monthOrMonthCode = mozilla::EnumSet{ |
| 586 |
CalendarField::Month, |
| 587 |
CalendarField::MonthCode, |
| 588 |
}; |
| 589 |
|
| 590 |
static constexpr auto dayOrAnyMonth = mozilla::EnumSet{ |
| 591 |
CalendarField::Day, |
| 592 |
CalendarField::Month, |
| 593 |
CalendarField::MonthCode, |
| 594 |
}; |
| 595 |
|
| 596 |
// A field always invalidates at least itself, so start with ignoring all |
| 597 |
// input fields. |
| 598 |
auto result = keys; |
| 599 |
|
| 600 |
// "month" and "monthCode" are mutually exclusive. |
| 601 |
if (!(keys & monthOrMonthCode).isEmpty()) { |
| 602 |
result += monthOrMonthCode; |
| 603 |
} |
| 604 |
|
| 605 |
// "era", "eraYear", and "year" are mutually exclusive in non-single era |
| 606 |
// calendar systems. |
| 607 |
if (CalendarEraRelevant(calendar) && !(keys & eraOrAnyYear).isEmpty()) { |
| 608 |
result += eraOrAnyYear; |
| 609 |
} |
| 610 |
|
| 611 |
// If eras don't start at year boundaries, we have to ignore "era" and |
| 612 |
// "eraYear" if any of "day", "month", or "monthCode" is present. |
| 613 |
if (!CalendarEraStartsAtYearBoundary(calendar) && |
| 614 |
!(keys & dayOrAnyMonth).isEmpty()) { |
| 615 |
result += eraOrEraYear; |
| 616 |
} |
| 617 |
|
| 618 |
return result; |
| 619 |
} |
| 620 |
|
| 621 |
/** |
| 622 |
* CalendarMergeFields ( calendar, fields, additionalFields ) |
| 623 |
*/ |
| 624 |
CalendarFields js::temporal::CalendarMergeFields( |
| 625 |
const CalendarValue& calendar, const CalendarFields& fields, |
| 626 |
const CalendarFields& additionalFields) { |
| 627 |
auto calendarId = calendar.identifier(); |
| 628 |
|
| 629 |
// Steps 1. |
| 630 |
auto additionalKeys = additionalFields.keys(); |
| 631 |
|
| 632 |
// Step 2. |
| 633 |
auto overriddenKeys = CalendarFieldKeysToIgnore(calendarId, additionalKeys); |
| 634 |
MOZ_ASSERT(overriddenKeys.contains(additionalKeys)); |
| 635 |
|
| 636 |
// Step 3. |
| 637 |
auto merged = CalendarFields{}; |
| 638 |
|
| 639 |
// Step 4. |
| 640 |
auto fieldsKeys = fields.keys(); |
| 641 |
|
| 642 |
// Step 5.b. |
| 643 |
for (auto key : (fieldsKeys - overriddenKeys)) { |
| 644 |
merged.setFrom(key, fields); |
| 645 |
} |
| 646 |
|
| 647 |
// Step 5.c. |
| 648 |
for (auto key : additionalKeys) { |
| 649 |
merged.setFrom(key, additionalFields); |
| 650 |
} |
| 651 |
|
| 652 |
// Step 6. |
| 653 |
return merged; |
| 654 |
} |