Debian Codegraph @

js/src/builtin/temporal/PlainDate.cpp

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 #include "builtin/temporal/PlainDate.h"
8
9 #include "mozilla/Assertions.h"
10 #include "mozilla/CheckedInt.h"
11 #include "mozilla/EnumSet.h"
12
13 #include <cmath>
14 #include <cstdlib>
15 #include <stdint.h>
16 #include <utility>
17
18 #include "jsdate.h"
19 #include "jsnum.h"
20 #include "jspubtd.h"
21 #include "jstypes.h"
22 #include "NamespaceImports.h"
23
24 #include "builtin/intl/DateTimeFormat.h"
25 #include "builtin/temporal/Calendar.h"
26 #include "builtin/temporal/CalendarFields.h"
27 #include "builtin/temporal/Duration.h"
28 #include "builtin/temporal/Instant.h"
29 #include "builtin/temporal/PlainDateTime.h"
30 #include "builtin/temporal/PlainMonthDay.h"
31 #include "builtin/temporal/PlainTime.h"
32 #include "builtin/temporal/PlainYearMonth.h"
33 #include "builtin/temporal/Temporal.h"
34 #include "builtin/temporal/TemporalParser.h"
35 #include "builtin/temporal/TemporalRoundingMode.h"
36 #include "builtin/temporal/TemporalTypes.h"
37 #include "builtin/temporal/TemporalUnit.h"
38 #include "builtin/temporal/TimeZone.h"
39 #include "builtin/temporal/ToString.h"
40 #include "builtin/temporal/ZonedDateTime.h"
41 #include "gc/AllocKind.h"
42 #include "gc/Barrier.h"
43 #include "gc/GCEnum.h"
44 #include "js/CallArgs.h"
45 #include "js/CallNonGenericMethod.h"
46 #include "js/Class.h"
47 #include "js/ErrorReport.h"
48 #include "js/friend/ErrorMessages.h"
49 #include "js/PropertyDescriptor.h"
50 #include "js/PropertySpec.h"
51 #include "js/RootingAPI.h"
52 #include "js/TypeDecls.h"
53 #include "js/Value.h"
54 #include "vm/BytecodeUtil.h"
55 #include "vm/GlobalObject.h"
56 #include "vm/JSAtomState.h"
57 #include "vm/JSContext.h"
58 #include "vm/JSObject.h"
59 #include "vm/PlainObject.h"
60 #include "vm/StringType.h"
61
62 #include "vm/JSObject-inl.h"
63 #include "vm/NativeObject-inl.h"
64 #include "vm/ObjectOperations-inl.h"
65
66 using namespace js;
67 using namespace js::temporal;
68
69 static inline bool IsPlainDate(Handle<Value> v) {
70 return v.isObject() && v.toObject().is<PlainDateObject>();
71 }
72
73 #ifdef DEBUG
74 /**
75 * IsValidISODate ( year, month, day )
76 */
77 bool js::temporal::IsValidISODate(const ISODate& date) {
78 const auto& [year, month, day] = date;
79
80 // Step 1.
81 if (month < 1 || month > 12) {
82 return false;
83 }
84
85 // Step 2.
86 int32_t daysInMonth = js::temporal::ISODaysInMonth(year, month);
87
88 // Steps 3-4.
89 return 1 <= day && day <= daysInMonth;
90 }
91 #endif
92
93 /**
94 * ISODateWithinLimits ( isoDate )
95 */
96 bool js::temporal::ISODateWithinLimits(const ISODate& isoDate) {
97 MOZ_ASSERT(IsValidISODate(isoDate));
98
99 constexpr auto min = ISODate::min();
100 constexpr auto max = ISODate::max();
101
102 const auto& year = isoDate.year;
103
104 // Fast-path when the input is definitely in range.
105 if (min.year < year && year < max.year) {
106 return true;
107 }
108
109 // Check |isoDate| is within the valid limits.
110 if (year < 0) {
111 return isoDate >= min;
112 }
113 return isoDate <= max;
114 }
115
116 static void ReportInvalidDateValue(JSContext* cx, const char* name, int32_t min,
117 int32_t max, double num) {
118 Int32ToCStringBuf minCbuf;
119 const char* minStr = Int32ToCString(&minCbuf, min);
120
121 Int32ToCStringBuf maxCbuf;
122 const char* maxStr = Int32ToCString(&maxCbuf, max);
123
124 ToCStringBuf numCbuf;
125 const char* numStr = NumberToCString(&numCbuf, num);
126
127 JS_ReportErrorNumberASCII(cx, GetErrorMessage, nullptr,
128 JSMSG_TEMPORAL_PLAIN_DATE_INVALID_VALUE, name,
129 minStr, maxStr, numStr);
130 }
131
132 template <typename T>
133 static inline bool ThrowIfInvalidDateValue(JSContext* cx, const char* name,
134 int32_t min, int32_t max, T num) {
135 if (min <= num && num <= max) {
136 return true;
137 }
138 ReportInvalidDateValue(cx, name, min, max, num);
139 return false;
140 }
141
142 /**
143 * IsValidISODate ( year, month, day )
144 */
145 template <typename T>
146 static bool ThrowIfInvalidISODate(JSContext* cx, T year, T month, T day) {
147 static_assert(std::is_same_v<T, int32_t> || std::is_same_v<T, double>);
148
149 // Step 1.
150 MOZ_ASSERT(IsInteger(year));
151 MOZ_ASSERT(IsInteger(month));
152 MOZ_ASSERT(IsInteger(day));
153
154 if constexpr (std::is_same_v<T, double>) {
155 if (!ThrowIfInvalidDateValue(cx, "year", INT32_MIN, INT32_MAX, year)) {
156 return false;
157 }
158 }
159
160 // Step 2.
161 if (!ThrowIfInvalidDateValue(cx, "month", 1, 12, month)) {
162 return false;
163 }
164
165 // Step 3.
166 int32_t daysInMonth =
167 js::temporal::ISODaysInMonth(int32_t(year), int32_t(month));
168
169 // Steps 4-5.
170 return ThrowIfInvalidDateValue(cx, "day", 1, daysInMonth, day);
171 }
172
173 /**
174 * IsValidISODate ( year, month, day )
175 */
176 bool js::temporal::ThrowIfInvalidISODate(JSContext* cx, const ISODate& date) {
177 const auto& [year, month, day] = date;
178 return ::ThrowIfInvalidISODate(cx, year, month, day);
179 }
180
181 /**
182 * IsValidISODate ( year, month, day )
183 */
184 bool js::temporal::ThrowIfInvalidISODate(JSContext* cx, double year,
185 double month, double day) {
186 return ::ThrowIfInvalidISODate(cx, year, month, day);
187 }
188
189 /**
190 * CreateTemporalDate ( isoDate, calendar [ , newTarget ] )
191 */
192 static PlainDateObject* CreateTemporalDate(JSContext* cx, const CallArgs& args,
193 const ISODate& isoDate,
194 Handle<CalendarValue> calendar) {
195 MOZ_ASSERT(IsValidISODate(isoDate));
196
197 // Step 1.
198 if (!ISODateWithinLimits(isoDate)) {
199 JS_ReportErrorNumberASCII(cx, GetErrorMessage, nullptr,
200 JSMSG_TEMPORAL_PLAIN_DATE_INVALID);
201 return nullptr;
202 }
203
204 // Steps 2-3.
205 Rooted<JSObject*> proto(cx);
206 if (!GetPrototypeFromBuiltinConstructor(cx, args, JSProto_PlainDate,
207 &proto)) {
208 return nullptr;
209 }
210
211 auto* object = NewObjectWithClassProto<PlainDateObject>(cx, proto);
212 if (!object) {
213 return nullptr;
214 }
215
216 // Step 4.
217 auto packedDate = PackedDate::pack(isoDate);
218 object->setFixedSlot(PlainDateObject::PACKED_DATE_SLOT,
219 PrivateUint32Value(packedDate.value));
220
221 // Step 5.
222 object->setFixedSlot(PlainDateObject::CALENDAR_SLOT, calendar.toSlotValue());
223
224 // Step 6.
225 return object;
226 }
227
228 /**
229 * CreateTemporalDate ( isoDate, calendar [ , newTarget ] )
230 */
231 PlainDateObject* js::temporal::CreateTemporalDate(
232 JSContext* cx, const ISODate& isoDate, Handle<CalendarValue> calendar) {
233 MOZ_ASSERT(IsValidISODate(isoDate));
234
235 // Step 1.
236 if (!ISODateWithinLimits(isoDate)) {
237 JS_ReportErrorNumberASCII(cx, GetErrorMessage, nullptr,
238 JSMSG_TEMPORAL_PLAIN_DATE_INVALID);
239 return nullptr;
240 }
241
242 // Steps 2-3.
243 auto* object = NewBuiltinClassInstance<PlainDateObject>(cx);
244 if (!object) {
245 return nullptr;
246 }
247
248 // Step 4.
249 auto packedDate = PackedDate::pack(isoDate);
250 object->setFixedSlot(PlainDateObject::PACKED_DATE_SLOT,
251 PrivateUint32Value(packedDate.value));
252
253 // Step 5.
254 object->setFixedSlot(PlainDateObject::CALENDAR_SLOT, calendar.toSlotValue());
255
256 // Step 6.
257 return object;
258 }
259
260 /**
261 * CreateTemporalDate ( isoDate, calendar [ , newTarget ] )
262 */
263 PlainDateObject* js::temporal::CreateTemporalDate(JSContext* cx,
264 Handle<PlainDate> date) {
265 MOZ_ASSERT(ISODateWithinLimits(date));
266 return CreateTemporalDate(cx, date, date.calendar());
267 }
268
269 /**
270 * CreateTemporalDate ( isoDate, calendar [ , newTarget ] )
271 */
272 bool js::temporal::CreateTemporalDate(JSContext* cx, const ISODate& isoDate,
273 Handle<CalendarValue> calendar,
274 MutableHandle<PlainDate> result) {
275 MOZ_ASSERT(IsValidISODate(isoDate));
276
277 // Step 1.
278 if (!ISODateWithinLimits(isoDate)) {
279 JS_ReportErrorNumberASCII(cx, GetErrorMessage, nullptr,
280 JSMSG_TEMPORAL_PLAIN_DATE_INVALID);
281 return false;
282 }
283
284 // Steps 2-6.
285 result.set(PlainDate{isoDate, calendar});
286 return true;
287 }
288
289 struct DateOptions {
290 TemporalOverflow overflow = TemporalOverflow::Constrain;
291 };
292
293 /**
294 * ToTemporalDate ( item [ , options ] )
295 */
296 static bool ToTemporalDateOptions(JSContext* cx, Handle<Value> options,
297 DateOptions* result) {
298 if (options.isUndefined()) {
299 *result = {};
300 return true;
301 }
302
303 // NOTE: |options| are only passed from `Temporal.PlainDate.from`.
304
305 Rooted<JSObject*> resolvedOptions(
306 cx, RequireObjectArg(cx, "options", "from", options));
307 if (!resolvedOptions) {
308 return false;
309 }
310
311 auto overflow = TemporalOverflow::Constrain;
312 if (!GetTemporalOverflowOption(cx, resolvedOptions, &overflow)) {
313 return false;
314 }
315
316 *result = {overflow};
317 return true;
318 }
319
320 /**
321 * ToTemporalDate ( item [ , options ] )
322 */
323 static bool ToTemporalDate(JSContext* cx, Handle<JSObject*> item,
324 Handle<Value> options,
325 MutableHandle<PlainDate> result) {
326 // Step 1. (Not applicable in our implementation.)
327
328 // Step 2.a.
329 if (auto* plainDate = item->maybeUnwrapIf<PlainDateObject>()) {
330 auto date = plainDate->date();
331 Rooted<CalendarValue> calendar(cx, plainDate->calendar());
332 if (!calendar.wrap(cx)) {
333 return false;
334 }
335
336 // Steps 2.a.i-ii.
337 DateOptions ignoredOptions;
338 if (!ToTemporalDateOptions(cx, options, &ignoredOptions)) {
339 return false;
340 }
341
342 // Step 2.a.iii.
343 result.set(PlainDate{date, calendar});
344 return true;
345 }
346
347 // Step 2.b.
348 if (auto* zonedDateTime = item->maybeUnwrapIf<ZonedDateTimeObject>()) {
349 auto epochNs = zonedDateTime->epochNanoseconds();
350 Rooted<TimeZoneValue> timeZone(cx, zonedDateTime->timeZone());
351 Rooted<CalendarValue> calendar(cx, zonedDateTime->calendar());
352
353 if (!timeZone.wrap(cx)) {
354 return false;
355 }
356 if (!calendar.wrap(cx)) {
357 return false;
358 }
359
360 // Steps 2.b.ii.
361 ISODateTime isoDateTime;
362 if (!GetISODateTimeFor(cx, timeZone, epochNs, &isoDateTime)) {
363 return false;
364 }
365
366 // Steps 2.b.ii-iii.
367 DateOptions ignoredOptions;
368 if (!ToTemporalDateOptions(cx, options, &ignoredOptions)) {
369 return false;
370 }
371
372 // Step 2.b.iv.
373 result.set(PlainDate{isoDateTime.date, calendar});
374 return true;
375 }
376
377 // Step 2.c.
378 if (auto* dateTime = item->maybeUnwrapIf<PlainDateTimeObject>()) {
379 auto date = dateTime->date();
380 Rooted<CalendarValue> calendar(cx, dateTime->calendar());
381 if (!calendar.wrap(cx)) {
382 return false;
383 }
384
385 // Steps 2.c.i-ii.
386 DateOptions ignoredOptions;
387 if (!ToTemporalDateOptions(cx, options, &ignoredOptions)) {
388 return false;
389 }
390
391 // Step 2.c.iii.
392 result.set(PlainDate{date, calendar});
393 return true;
394 }
395
396 // Step 2.d.
397 Rooted<CalendarValue> calendar(cx);
398 if (!GetTemporalCalendarWithISODefault(cx, item, &calendar)) {
399 return false;
400 }
401
402 // Step 2.e.
403 Rooted<CalendarFields> fields(cx);
404 if (!PrepareCalendarFields(cx, calendar, item,
405 {
406 CalendarField::Year,
407 CalendarField::Month,
408 CalendarField::MonthCode,
409 CalendarField::Day,
410 },
411 &fields)) {
412 return false;
413 }
414
415 // Steps 2.f-g.
416 DateOptions resolvedOptions;
417 if (!ToTemporalDateOptions(cx, options, &resolvedOptions)) {
418 return false;
419 }
420 auto [overflow] = resolvedOptions;
421
422 // Steps 2.h-i.
423 return CalendarDateFromFields(cx, calendar, fields, overflow, result);
424 }
425
426 /**
427 * ToTemporalDate ( item [ , options ] )
428 */
429 static bool ToTemporalDate(JSContext* cx, Handle<Value> item,
430 Handle<Value> options,
431 MutableHandle<PlainDate> result) {
432 // Step 1. (Not applicable in our implementation.)
433
434 // Step 2.
435 if (item.isObject()) {
436 Rooted<JSObject*> itemObj(cx, &item.toObject());
437 return ToTemporalDate(cx, itemObj, options, result);
438 }
439
440 // Step 3.
441 if (!item.isString()) {
442 ReportValueError(cx, JSMSG_UNEXPECTED_TYPE, JSDVG_IGNORE_STACK, item,
443 nullptr, "not a string");
444 return false;
445 }
446 Rooted<JSString*> string(cx, item.toString());
447
448 // Step 4.
449 ISODateTime dateTime;
450 Rooted<JSString*> calendarString(cx);
451 if (!ParseTemporalDateTimeString(cx, string, &dateTime, &calendarString)) {
452 return false;
453 }
454 MOZ_ASSERT(IsValidISODate(dateTime.date));
455
456 // Steps 5-7.
457 Rooted<CalendarValue> calendar(cx, CalendarValue(CalendarId::ISO8601));
458 if (calendarString) {
459 if (!CanonicalizeCalendar(cx, calendarString, &calendar)) {
460 return false;
461 }
462 }
463
464 // Steps 8-9.
465 DateOptions ignoredOptions;
466 if (!ToTemporalDateOptions(cx, options, &ignoredOptions)) {
467 return false;
468 }
469
470 // Steps 10-11.
471 return CreateTemporalDate(cx, dateTime.date, calendar, result);
472 }
473
474 /**
475 * ToTemporalDate ( item [ , options ] )
476 */
477 static bool ToTemporalDate(JSContext* cx, Handle<Value> item,
478 MutableHandle<PlainDate> result) {
479 return ToTemporalDate(cx, item, UndefinedHandleValue, result);
480 }
481
482 static bool IsValidISODateEpochMilliseconds(int64_t epochMilliseconds) {
483 // Epoch nanoseconds limits, adjusted to the range supported by ISODate.
484 constexpr auto oneDay = EpochDuration::fromDays(1);
485 constexpr auto min = EpochNanoseconds::min() - oneDay;
486 constexpr auto max = EpochNanoseconds::max() + oneDay;
487
488 // NB: Minimum limit is inclusive, whereas maximim limit is exclusive.
489 auto epochNs = EpochNanoseconds::fromMilliseconds(epochMilliseconds);
490 return min <= epochNs && epochNs < max;
491 }
492
493 /**
494 * BalanceISODate ( year, month, day )
495 */
496 bool js::temporal::BalanceISODate(JSContext* cx, const ISODate& date,
497 int64_t days, ISODate* result) {
498 MOZ_ASSERT(IsValidISODate(date));
499 MOZ_ASSERT(ISODateWithinLimits(date));
500
501 // Step 1.
502 auto epochDays = MakeDay(date) + mozilla::CheckedInt64{days};
503
504 // Step 2.
505 auto epochMilliseconds = epochDays * ToMilliseconds(TemporalUnit::Day);
506 if (!epochMilliseconds.isValid() ||
507 !IsValidISODateEpochMilliseconds(epochMilliseconds.value())) {
508 JS_ReportErrorNumberASCII(cx, GetErrorMessage, nullptr,
509 JSMSG_TEMPORAL_PLAIN_DATE_INVALID);
510 return false;
511 }
512
513 // Step 3.
514 auto [year, month, day] = ToYearMonthDay(epochMilliseconds.value());
515
516 *result = ISODate{year, month + 1, day};
517 MOZ_ASSERT(IsValidISODate(*result));
518 MOZ_ASSERT(ISODateWithinLimits(*result));
519
520 return true;
521 }
522
523 /**
524 * BalanceISODate ( year, month, day )
525 */
526 ISODate js::temporal::BalanceISODate(const ISODate& date, int32_t days) {
527 MOZ_ASSERT(IsValidISODate(date));
528 MOZ_ASSERT(ISODateWithinLimits(date));
529 MOZ_ASSERT(std::abs(days) <= 400'000'000, "days limit for ToYearMonthDay");
530
531 // Step 1.
532 int32_t epochDays = MakeDay(date) + days;
533
534 // Step 2.
535 int64_t epochMilliseconds = epochDays * ToMilliseconds(TemporalUnit::Day);
536
537 // Step 3.
538 auto [year, month, day] = ToYearMonthDay(epochMilliseconds);
539
540 // NB: The returned date is possibly outside the valid limits!
541 auto result = ISODate{year, month + 1, day};
542 MOZ_ASSERT(IsValidISODate(result));
543
544 return result;
545 }
546
547 /**
548 * CompareISODate ( y1, m1, d1, y2, m2, d2 )
549 */
550 int32_t js::temporal::CompareISODate(const ISODate& one, const ISODate& two) {
551 // Steps 1-2.
552 if (one.year != two.year) {
553 return one.year < two.year ? -1 : 1;
554 }
555
556 // Steps 3-4.
557 if (one.month != two.month) {
558 return one.month < two.month ? -1 : 1;
559 }
560
561 // Steps 5-6.
562 if (one.day != two.day) {
563 return one.day < two.day ? -1 : 1;
564 }
565
566 // Step 7.
567 return 0;
568 }
569
570 /**
571 * DifferenceTemporalPlainDate ( operation, temporalDate, other, options )
572 */
573 static bool DifferenceTemporalPlainDate(JSContext* cx,
574 TemporalDifference operation,
575 const CallArgs& args) {
576 Rooted<PlainDate> temporalDate(
577 cx, &args.thisv().toObject().as<PlainDateObject>());
578
579 // Step 1.
580 Rooted<PlainDate> other(cx);
581 if (!ToTemporalDate(cx, args.get(0), &other)) {
582 return false;
583 }
584
585 // Step 2.
586 if (!CalendarEquals(temporalDate.calendar(), other.calendar())) {
587 JS_ReportErrorNumberASCII(
588 cx, GetErrorMessage, nullptr, JSMSG_TEMPORAL_CALENDAR_INCOMPATIBLE,
589 CalendarIdentifier(temporalDate.calendar()).data(),
590 CalendarIdentifier(other.calendar()).data());
591 return false;
592 }
593
594 // Steps 3-4.
595 DifferenceSettings settings;
596 if (args.hasDefined(1)) {
597 // Step 3.
598 Rooted<JSObject*> options(
599 cx, RequireObjectArg(cx, "options", ToName(operation), args[1]));
600 if (!options) {
601 return false;
602 }
603
604 // Step 4.
605 if (!GetDifferenceSettings(cx, operation, options, TemporalUnitGroup::Date,
606 TemporalUnit::Day, TemporalUnit::Day,
607 &settings)) {
608 return false;
609 }
610 } else {
611 // Steps 3-4.
612 settings = {
613 TemporalUnit::Day,
614 TemporalUnit::Day,
615 TemporalRoundingMode::Trunc,
616 Increment{1},
617 };
618 }
619
620 // Step 5.
621 if (temporalDate.date() == other.date()) {
622 auto* obj = CreateTemporalDuration(cx, {});
623 if (!obj) {
624 return false;
625 }
626
627 args.rval().setObject(*obj);
628 return true;
629 }
630
631 // Step 6.
632 DateDuration dateDifference;
633 if (!CalendarDateUntil(cx, temporalDate.calendar(), temporalDate.date(),
634 other.date(), settings.largestUnit, &dateDifference)) {
635 return false;
636 }
637
638 // Step 7.
639 auto duration = InternalDuration{dateDifference, {}};
640
641 // Step 8.
642 if (settings.smallestUnit != TemporalUnit::Day ||
643 settings.roundingIncrement != Increment{1}) {
644 // Step 8.a.
645 auto isoDateTime = ISODateTime{temporalDate.date(), {}};
646
647 // Step 8.b.
648 auto isoDateTimeOther = ISODateTime{other.date(), {}};
649
650 // Step 8.c.
651 auto destEpochNs = GetUTCEpochNanoseconds(isoDateTimeOther);
652
653 // Step 8.d.
654 Rooted<TimeZoneValue> timeZone(cx, TimeZoneValue{});
655 if (!RoundRelativeDuration(cx, duration, destEpochNs, isoDateTime, timeZone,
656 temporalDate.calendar(), settings.largestUnit,
657 settings.roundingIncrement,
658 settings.smallestUnit, settings.roundingMode,
659 &duration)) {
660 return false;
661 }
662 }
663 MOZ_ASSERT(IsValidDuration(duration));
664 MOZ_ASSERT(duration.time == TimeDuration{});
665
666 // Step 9. (Inlined TemporalDurationFromInternal)
667 auto result = duration.date.toDuration();
668
669 // Step 10.
670 if (operation == TemporalDifference::Since) {
671 result = result.negate();
672 }
673 MOZ_ASSERT(IsValidDuration(result));
674
675 // Step 11.
676 auto* obj = CreateTemporalDuration(cx, result);
677 if (!obj) {
678 return false;
679 }
680
681 args.rval().setObject(*obj);
682 return true;
683 }
684
685 /**
686 * AddDurationToDate ( operation, temporalDate, temporalDurationLike, options )
687 */
688 static bool AddDurationToDate(JSContext* cx, TemporalAddDuration operation,
689 const CallArgs& args) {
690 Rooted<PlainDate> temporalDate(
691 cx, &args.thisv().toObject().as<PlainDateObject>());
692
693 // Step 1.
694 auto calendar = temporalDate.calendar();
695
696 // Step 2.
697 Duration duration;
698 if (!ToTemporalDuration(cx, args.get(0), &duration)) {
699 return false;
700 }
701
702 // Step 3.
703 if (operation == TemporalAddDuration::Subtract) {
704 duration = duration.negate();
705 }
706
707 // Step 4.
708 auto dateDuration = ToDateDurationRecordWithoutTime(duration);
709
710 // Steps 6-7.
711 auto overflow = TemporalOverflow::Constrain;
712 if (args.hasDefined(1)) {
713 // Step 6.
714 Rooted<JSObject*> options(
715 cx, RequireObjectArg(cx, "options", ToName(operation), args[1]));
716 if (!options) {
717 return false;
718 }
719
720 // Step 7.
721 if (!GetTemporalOverflowOption(cx, options, &overflow)) {
722 return false;
723 }
724 }
725
726 // Step 8.
727 ISODate result;
728 if (!CalendarDateAdd(cx, calendar, temporalDate.date(), dateDuration,
729 overflow, &result)) {
730 return false;
731 }
732
733 // Step 9.
734 auto* obj = CreateTemporalDate(cx, result, calendar);
735 if (!obj) {
736 return false;
737 }
738
739 args.rval().setObject(*obj);
740 return true;
741 }
742
743 /**
744 * Temporal.PlainDate ( isoYear, isoMonth, isoDay [ , calendar ] )
745 */
746 static bool PlainDateConstructor(JSContext* cx, unsigned argc, Value* vp) {
747 CallArgs args = CallArgsFromVp(argc, vp);
748
749 // Step 1.
750 if (!ThrowIfNotConstructing(cx, args, "Temporal.PlainDate")) {
751 return false;
752 }
753
754 // Step 2.
755 double isoYear;
756 if (!ToIntegerWithTruncation(cx, args.get(0), "year", &isoYear)) {
757 return false;
758 }
759
760 // Step 3.
761 double isoMonth;
762 if (!ToIntegerWithTruncation(cx, args.get(1), "month", &isoMonth)) {
763 return false;
764 }
765
766 // Step 4.
767 double isoDay;
768 if (!ToIntegerWithTruncation(cx, args.get(2), "day", &isoDay)) {
769 return false;
770 }
771
772 // Steps 5-7.
773 Rooted<CalendarValue> calendar(cx, CalendarValue(CalendarId::ISO8601));
774 if (args.hasDefined(3)) {
775 // Step 6.
776 if (!args[3].isString()) {
777 ReportValueError(cx, JSMSG_UNEXPECTED_TYPE, JSDVG_IGNORE_STACK, args[3],
778 nullptr, "not a string");
779 return false;
780 }
781
782 // Step 7.
783 Rooted<JSString*> calendarString(cx, args[3].toString());
784 if (!CanonicalizeCalendar(cx, calendarString, &calendar)) {
785 return false;
786 }
787 }
788
789 // Step 8.
790 if (!ThrowIfInvalidISODate(cx, isoYear, isoMonth, isoDay)) {
791 return false;
792 }
793
794 // Step 9.
795 auto isoDate = ISODate{int32_t(isoYear), int32_t(isoMonth), int32_t(isoDay)};
796
797 // Step 10.
798 auto* temporalDate = CreateTemporalDate(cx, args, isoDate, calendar);
799 if (!temporalDate) {
800 return false;
801 }
802
803 args.rval().setObject(*temporalDate);
804 return true;
805 }
806
807 /**
808 * Temporal.PlainDate.from ( item [ , options ] )
809 */
810 static bool PlainDate_from(JSContext* cx, unsigned argc, Value* vp) {
811 CallArgs args = CallArgsFromVp(argc, vp);
812
813 // Step 1.
814 Rooted<PlainDate> date(cx);
815 if (!ToTemporalDate(cx, args.get(0), args.get(1), &date)) {
816 return false;
817 }
818
819 auto* result = CreateTemporalDate(cx, date);
820 if (!result) {
821 return false;
822 }
823
824 args.rval().setObject(*result);
825 return true;
826 }
827
828 /**
829 * Temporal.PlainDate.compare ( one, two )
830 */
831 static bool PlainDate_compare(JSContext* cx, unsigned argc, Value* vp) {
832 CallArgs args = CallArgsFromVp(argc, vp);
833
834 // Step 1.
835 Rooted<PlainDate> one(cx);
836 if (!ToTemporalDate(cx, args.get(0), &one)) {
837 return false;
838 }
839
840 // Step 2.
841 Rooted<PlainDate> two(cx);
842 if (!ToTemporalDate(cx, args.get(1), &two)) {
843 return false;
844 }
845
846 // Step 3.
847 args.rval().setInt32(CompareISODate(one, two));
848 return true;
849 }
850
851 /**
852 * get Temporal.PlainDate.prototype.calendarId
853 */
854 static bool PlainDate_calendarId(JSContext* cx, const CallArgs& args) {
855 auto* temporalDate = &args.thisv().toObject().as<PlainDateObject>();
856
857 // Step 3.
858 auto* str =
859 NewStringCopy<CanGC>(cx, CalendarIdentifier(temporalDate->calendar()));
860 if (!str) {
861 return false;
862 }
863
864 args.rval().setString(str);
865 return true;
866 }
867
868 /**
869 * get Temporal.PlainDate.prototype.calendarId
870 */
871 static bool PlainDate_calendarId(JSContext* cx, unsigned argc, Value* vp) {
872 // Steps 1-2.
873 CallArgs args = CallArgsFromVp(argc, vp);
874 return CallNonGenericMethod<IsPlainDate, PlainDate_calendarId>(cx, args);
875 }
876
877 /**
878 * get Temporal.PlainDate.prototype.era
879 */
880 static bool PlainDate_era(JSContext* cx, const CallArgs& args) {
881 auto* temporalDate = &args.thisv().toObject().as<PlainDateObject>();
882 Rooted<CalendarValue> calendar(cx, temporalDate->calendar());
883
884 // Step 3.
885 return CalendarEra(cx, calendar, temporalDate->date(), args.rval());
886 }
887
888 /**
889 * get Temporal.PlainDate.prototype.era
890 */
891 static bool PlainDate_era(JSContext* cx, unsigned argc, Value* vp) {
892 // Steps 1-2.
893 CallArgs args = CallArgsFromVp(argc, vp);
894 return CallNonGenericMethod<IsPlainDate, PlainDate_era>(cx, args);
895 }
896
897 /**
898 * get Temporal.PlainDate.prototype.eraYear
899 */
900 static bool PlainDate_eraYear(JSContext* cx, const CallArgs& args) {
901 auto* temporalDate = &args.thisv().toObject().as<PlainDateObject>();
902 Rooted<CalendarValue> calendar(cx, temporalDate->calendar());
903
904 // Steps 3-5.
905 return CalendarEraYear(cx, calendar, temporalDate->date(), args.rval());
906 }
907
908 /**
909 * get Temporal.PlainDate.prototype.eraYear
910 */
911 static bool PlainDate_eraYear(JSContext* cx, unsigned argc, Value* vp) {
912 // Steps 1-2.
913 CallArgs args = CallArgsFromVp(argc, vp);
914 return CallNonGenericMethod<IsPlainDate, PlainDate_eraYear>(cx, args);
915 }
916
917 /**
918 * get Temporal.PlainDate.prototype.year
919 */
920 static bool PlainDate_year(JSContext* cx, const CallArgs& args) {
921 auto* temporalDate = &args.thisv().toObject().as<PlainDateObject>();
922 Rooted<CalendarValue> calendar(cx, temporalDate->calendar());
923
924 // Step 3.
925 return CalendarYear(cx, calendar, temporalDate->date(), args.rval());
926 }
927
928 /**
929 * get Temporal.PlainDate.prototype.year
930 */
931 static bool PlainDate_year(JSContext* cx, unsigned argc, Value* vp) {
932 // Steps 1-2.
933 CallArgs args = CallArgsFromVp(argc, vp);
934 return CallNonGenericMethod<IsPlainDate, PlainDate_year>(cx, args);
935 }
936
937 /**
938 * get Temporal.PlainDate.prototype.month
939 */
940 static bool PlainDate_month(JSContext* cx, const CallArgs& args) {
941 auto* temporalDate = &args.thisv().toObject().as<PlainDateObject>();
942 Rooted<CalendarValue> calendar(cx, temporalDate->calendar());
943
944 // Step 3.
945 return CalendarMonth(cx, calendar, temporalDate->date(), args.rval());
946 }
947
948 /**
949 * get Temporal.PlainDate.prototype.month
950 */
951 static bool PlainDate_month(JSContext* cx, unsigned argc, Value* vp) {
952 // Steps 1-2.
953 CallArgs args = CallArgsFromVp(argc, vp);
954 return CallNonGenericMethod<IsPlainDate, PlainDate_month>(cx, args);
955 }
956
957 /**
958 * get Temporal.PlainDate.prototype.monthCode
959 */
960 static bool PlainDate_monthCode(JSContext* cx, const CallArgs& args) {
961 auto* temporalDate = &args.thisv().toObject().as<PlainDateObject>();
962 Rooted<CalendarValue> calendar(cx, temporalDate->calendar());
963
964 // Step 3.
965 return CalendarMonthCode(cx, calendar, temporalDate->date(), args.rval());
966 }
967
968 /**
969 * get Temporal.PlainDate.prototype.monthCode
970 */
971 static bool PlainDate_monthCode(JSContext* cx, unsigned argc, Value* vp) {
972 // Steps 1-2.
973 CallArgs args = CallArgsFromVp(argc, vp);
974 return CallNonGenericMethod<IsPlainDate, PlainDate_monthCode>(cx, args);
975 }
976
977 /**
978 * get Temporal.PlainDate.prototype.day
979 */
980 static bool PlainDate_day(JSContext* cx, const CallArgs& args) {
981 auto* temporalDate = &args.thisv().toObject().as<PlainDateObject>();
982 Rooted<CalendarValue> calendar(cx, temporalDate->calendar());
983
984 // Step 3.
985 return CalendarDay(cx, calendar, temporalDate->date(), args.rval());
986 }
987
988 /**
989 * get Temporal.PlainDate.prototype.day
990 */
991 static bool PlainDate_day(JSContext* cx, unsigned argc, Value* vp) {
992 // Steps 1-2.
993 CallArgs args = CallArgsFromVp(argc, vp);
994 return CallNonGenericMethod<IsPlainDate, PlainDate_day>(cx, args);
995 }
996
997 /**
998 * get Temporal.PlainDate.prototype.dayOfWeek
999 */
1000 static bool PlainDate_dayOfWeek(JSContext* cx, const CallArgs& args) {
1001 auto* temporalDate = &args.thisv().toObject().as<PlainDateObject>();
1002 Rooted<CalendarValue> calendar(cx, temporalDate->calendar());
1003
1004 // Step 3.
1005 return CalendarDayOfWeek(cx, calendar, temporalDate->date(), args.rval());
1006 }
1007
1008 /**
1009 * get Temporal.PlainDate.prototype.dayOfWeek
1010 */
1011 static bool PlainDate_dayOfWeek(JSContext* cx, unsigned argc, Value* vp) {
1012 // Steps 1-2.
1013 CallArgs args = CallArgsFromVp(argc, vp);
1014 return CallNonGenericMethod<IsPlainDate, PlainDate_dayOfWeek>(cx, args);
1015 }
1016
1017 /**
1018 * get Temporal.PlainDate.prototype.dayOfYear
1019 */
1020 static bool PlainDate_dayOfYear(JSContext* cx, const CallArgs& args) {
1021 auto* temporalDate = &args.thisv().toObject().as<PlainDateObject>();
1022 Rooted<CalendarValue> calendar(cx, temporalDate->calendar());
1023
1024 // Step 3.
1025 return CalendarDayOfYear(cx, calendar, temporalDate->date(), args.rval());
1026 }
1027
1028 /**
1029 * get Temporal.PlainDate.prototype.dayOfYear
1030 */
1031 static bool PlainDate_dayOfYear(JSContext* cx, unsigned argc, Value* vp) {
1032 // Steps 1-2.
1033 CallArgs args = CallArgsFromVp(argc, vp);
1034 return CallNonGenericMethod<IsPlainDate, PlainDate_dayOfYear>(cx, args);
1035 }
1036
1037 /**
1038 * get Temporal.PlainDate.prototype.weekOfYear
1039 */
1040 static bool PlainDate_weekOfYear(JSContext* cx, const CallArgs& args) {
1041 auto* temporalDate = &args.thisv().toObject().as<PlainDateObject>();
1042 Rooted<CalendarValue> calendar(cx, temporalDate->calendar());
1043
1044 // Steps 3-5.
1045 return CalendarWeekOfYear(cx, calendar, temporalDate->date(), args.rval());
1046 }
1047
1048 /**
1049 * get Temporal.PlainDate.prototype.weekOfYear
1050 */
1051 static bool PlainDate_weekOfYear(JSContext* cx, unsigned argc, Value* vp) {
1052 // Steps 1-2.
1053 CallArgs args = CallArgsFromVp(argc, vp);
1054 return CallNonGenericMethod<IsPlainDate, PlainDate_weekOfYear>(cx, args);
1055 }
1056
1057 /**
1058 * get Temporal.PlainDate.prototype.yearOfWeek
1059 */
1060 static bool PlainDate_yearOfWeek(JSContext* cx, const CallArgs& args) {
1061 auto* temporalDate = &args.thisv().toObject().as<PlainDateObject>();
1062 Rooted<CalendarValue> calendar(cx, temporalDate->calendar());
1063
1064 // Steps 3-5.
1065 return CalendarYearOfWeek(cx, calendar, temporalDate->date(), args.rval());
1066 }
1067
1068 /**
1069 * get Temporal.PlainDate.prototype.yearOfWeek
1070 */
1071 static bool PlainDate_yearOfWeek(JSContext* cx, unsigned argc, Value* vp) {
1072 // Steps 1-2.
1073 CallArgs args = CallArgsFromVp(argc, vp);
1074 return CallNonGenericMethod<IsPlainDate, PlainDate_yearOfWeek>(cx, args);
1075 }
1076
1077 /**
1078 * get Temporal.PlainDate.prototype.daysInWeek
1079 */
1080 static bool PlainDate_daysInWeek(JSContext* cx, const CallArgs& args) {
1081 auto* temporalDate = &args.thisv().toObject().as<PlainDateObject>();
1082 Rooted<CalendarValue> calendar(cx, temporalDate->calendar());
1083
1084 // Step 3.
1085 return CalendarDaysInWeek(cx, calendar, temporalDate->date(), args.rval());
1086 }
1087
1088 /**
1089 * get Temporal.PlainDate.prototype.daysInWeek
1090 */
1091 static bool PlainDate_daysInWeek(JSContext* cx, unsigned argc, Value* vp) {
1092 // Steps 1-2.
1093 CallArgs args = CallArgsFromVp(argc, vp);
1094 return CallNonGenericMethod<IsPlainDate, PlainDate_daysInWeek>(cx, args);
1095 }
1096
1097 /**
1098 * get Temporal.PlainDate.prototype.daysInMonth
1099 */
1100 static bool PlainDate_daysInMonth(JSContext* cx, const CallArgs& args) {
1101 auto* temporalDate = &args.thisv().toObject().as<PlainDateObject>();
1102 Rooted<CalendarValue> calendar(cx, temporalDate->calendar());
1103
1104 // Step 3.
1105 return CalendarDaysInMonth(cx, calendar, temporalDate->date(), args.rval());
1106 }
1107
1108 /**
1109 * get Temporal.PlainDate.prototype.daysInMonth
1110 */
1111 static bool PlainDate_daysInMonth(JSContext* cx, unsigned argc, Value* vp) {
1112 // Steps 1-2.
1113 CallArgs args = CallArgsFromVp(argc, vp);
1114 return CallNonGenericMethod<IsPlainDate, PlainDate_daysInMonth>(cx, args);
1115 }
1116
1117 /**
1118 * get Temporal.PlainDate.prototype.daysInYear
1119 */
1120 static bool PlainDate_daysInYear(JSContext* cx, const CallArgs& args) {
1121 auto* temporalDate = &args.thisv().toObject().as<PlainDateObject>();
1122 Rooted<CalendarValue> calendar(cx, temporalDate->calendar());
1123
1124 // Step 3.
1125 return CalendarDaysInYear(cx, calendar, temporalDate->date(), args.rval());
1126 }
1127
1128 /**
1129 * get Temporal.PlainDate.prototype.daysInYear
1130 */
1131 static bool PlainDate_daysInYear(JSContext* cx, unsigned argc, Value* vp) {
1132 // Steps 1-2.
1133 CallArgs args = CallArgsFromVp(argc, vp);
1134 return CallNonGenericMethod<IsPlainDate, PlainDate_daysInYear>(cx, args);
1135 }
1136
1137 /**
1138 * get Temporal.PlainDate.prototype.monthsInYear
1139 */
1140 static bool PlainDate_monthsInYear(JSContext* cx, const CallArgs& args) {
1141 auto* temporalDate = &args.thisv().toObject().as<PlainDateObject>();
1142 Rooted<CalendarValue> calendar(cx, temporalDate->calendar());
1143
1144 // Step 3.
1145 return CalendarMonthsInYear(cx, calendar, temporalDate->date(), args.rval());
1146 }
1147
1148 /**
1149 * get Temporal.PlainDate.prototype.monthsInYear
1150 */
1151 static bool PlainDate_monthsInYear(JSContext* cx, unsigned argc, Value* vp) {
1152 // Steps 1-2.
1153 CallArgs args = CallArgsFromVp(argc, vp);
1154 return CallNonGenericMethod<IsPlainDate, PlainDate_monthsInYear>(cx, args);
1155 }
1156
1157 /**
1158 * get Temporal.PlainDate.prototype.inLeapYear
1159 */
1160 static bool PlainDate_inLeapYear(JSContext* cx, const CallArgs& args) {
1161 auto* temporalDate = &args.thisv().toObject().as<PlainDateObject>();
1162 Rooted<CalendarValue> calendar(cx, temporalDate->calendar());
1163
1164 // Step 3.
1165 return CalendarInLeapYear(cx, calendar, temporalDate->date(), args.rval());
1166 }
1167
1168 /**
1169 * get Temporal.PlainDate.prototype.inLeapYear
1170 */
1171 static bool PlainDate_inLeapYear(JSContext* cx, unsigned argc, Value* vp) {
1172 // Steps 1-2.
1173 CallArgs args = CallArgsFromVp(argc, vp);
1174 return CallNonGenericMethod<IsPlainDate, PlainDate_inLeapYear>(cx, args);
1175 }
1176
1177 /**
1178 * Temporal.PlainDate.prototype.toPlainYearMonth ( )
1179 */
1180 static bool PlainDate_toPlainYearMonth(JSContext* cx, const CallArgs& args) {
1181 Rooted<PlainDate> temporalDate(
1182 cx, &args.thisv().toObject().as<PlainDateObject>());
1183
1184 // Step 3.
1185 auto calendar = temporalDate.calendar();
1186
1187 // Step 4.
1188 Rooted<CalendarFields> fields(cx);
1189 if (!ISODateToFields(cx, temporalDate, &fields)) {
1190 return false;
1191 }
1192
1193 // Step 5.
1194 Rooted<PlainYearMonth> result(cx);
1195 if (!CalendarYearMonthFromFields(cx, calendar, fields,
1196 TemporalOverflow::Constrain, &result)) {
1197 return false;
1198 }
1199
1200 // Steps 6-7.
1201 auto* obj = CreateTemporalYearMonth(cx, result);
1202 if (!obj) {
1203 return false;
1204 }
1205
1206 args.rval().setObject(*obj);
1207 return true;
1208 }
1209
1210 /**
1211 * Temporal.PlainDate.prototype.toPlainYearMonth ( )
1212 */
1213 static bool PlainDate_toPlainYearMonth(JSContext* cx, unsigned argc,
1214 Value* vp) {
1215 // Steps 1-2.
1216 CallArgs args = CallArgsFromVp(argc, vp);
1217 return CallNonGenericMethod<IsPlainDate, PlainDate_toPlainYearMonth>(cx,
1218 args);
1219 }
1220
1221 /**
1222 * Temporal.PlainDate.prototype.toPlainMonthDay ( )
1223 */
1224 static bool PlainDate_toPlainMonthDay(JSContext* cx, const CallArgs& args) {
1225 Rooted<PlainDate> temporalDate(
1226 cx, &args.thisv().toObject().as<PlainDateObject>());
1227
1228 // Step 3.
1229 auto calendar = temporalDate.calendar();
1230
1231 // Step 4.
1232 Rooted<CalendarFields> fields(cx);
1233 if (!ISODateToFields(cx, temporalDate, &fields)) {
1234 return false;
1235 }
1236
1237 // Step 5.
1238 Rooted<PlainMonthDay> result(cx);
1239 if (!CalendarMonthDayFromFields(cx, calendar, fields,
1240 TemporalOverflow::Constrain, &result)) {
1241 return false;
1242 }
1243
1244 // Steps 6-7.
1245 auto* obj = CreateTemporalMonthDay(cx, result);
1246 if (!obj) {
1247 return false;
1248 }
1249
1250 args.rval().setObject(*obj);
1251 return true;
1252 }
1253
1254 /**
1255 * Temporal.PlainDate.prototype.toPlainMonthDay ( )
1256 */
1257 static bool PlainDate_toPlainMonthDay(JSContext* cx, unsigned argc, Value* vp) {
1258 // Steps 1-2.
1259 CallArgs args = CallArgsFromVp(argc, vp);
1260 return CallNonGenericMethod<IsPlainDate, PlainDate_toPlainMonthDay>(cx, args);
1261 }
1262
1263 /**
1264 * Temporal.PlainDate.prototype.toPlainDateTime ( [ temporalTime ] )
1265 */
1266 static bool PlainDate_toPlainDateTime(JSContext* cx, const CallArgs& args) {
1267 auto* temporalDate = &args.thisv().toObject().as<PlainDateObject>();
1268 Rooted<CalendarValue> calendar(cx, temporalDate->calendar());
1269
1270 // Step 4. (Reordered)
1271 //
1272 // Default initialize the time component to all zero.
1273 auto isoDateTime = ISODateTime{temporalDate->date(), {}};
1274
1275 // Step 3. (Inlined ToTemporalTimeOrMidnight)
1276 if (args.hasDefined(0)) {
1277 if (!ToTemporalTime(cx, args[0], &isoDateTime.time)) {
1278 return false;
1279 }
1280 }
1281
1282 // Step 4. (Moved above)
1283
1284 // Step 5.
1285 auto* obj = CreateTemporalDateTime(cx, isoDateTime, calendar);
1286 if (!obj) {
1287 return false;
1288 }
1289
1290 args.rval().setObject(*obj);
1291 return true;
1292 }
1293
1294 /**
1295 * Temporal.PlainDate.prototype.toPlainDateTime ( [ temporalTime ] )
1296 */
1297 static bool PlainDate_toPlainDateTime(JSContext* cx, unsigned argc, Value* vp) {
1298 // Steps 1-2.
1299 CallArgs args = CallArgsFromVp(argc, vp);
1300 return CallNonGenericMethod<IsPlainDate, PlainDate_toPlainDateTime>(cx, args);
1301 }
1302
1303 /**
1304 * Temporal.PlainDate.prototype.add ( temporalDurationLike [ , options ] )
1305 */
1306 static bool PlainDate_add(JSContext* cx, const CallArgs& args) {
1307 // Step 3.
1308 return AddDurationToDate(cx, TemporalAddDuration::Add, args);
1309 }
1310
1311 /**
1312 * Temporal.PlainDate.prototype.add ( temporalDurationLike [ , options ] )
1313 */
1314 static bool PlainDate_add(JSContext* cx, unsigned argc, Value* vp) {
1315 // Steps 1-2.
1316 CallArgs args = CallArgsFromVp(argc, vp);
1317 return CallNonGenericMethod<IsPlainDate, PlainDate_add>(cx, args);
1318 }
1319
1320 /**
1321 * Temporal.PlainDate.prototype.subtract ( temporalDurationLike [ , options ] )
1322 */
1323 static bool PlainDate_subtract(JSContext* cx, const CallArgs& args) {
1324 // Step 3.
1325 return AddDurationToDate(cx, TemporalAddDuration::Subtract, args);
1326 }
1327
1328 /**
1329 * Temporal.PlainDate.prototype.subtract ( temporalDurationLike [ , options ] )
1330 */
1331 static bool PlainDate_subtract(JSContext* cx, unsigned argc, Value* vp) {
1332 // Steps 1-2.
1333 CallArgs args = CallArgsFromVp(argc, vp);
1334 return CallNonGenericMethod<IsPlainDate, PlainDate_subtract>(cx, args);
1335 }
1336
1337 /**
1338 * Temporal.PlainDate.prototype.with ( temporalDateLike [ , options ] )
1339 */
1340 static bool PlainDate_with(JSContext* cx, const CallArgs& args) {
1341 Rooted<PlainDate> temporalDate(
1342 cx, &args.thisv().toObject().as<PlainDateObject>());
1343
1344 // Step 3.
1345 Rooted<JSObject*> temporalDateLike(
1346 cx, RequireObjectArg(cx, "temporalDateLike", "with", args.get(0)));
1347 if (!temporalDateLike) {
1348 return false;
1349 }
1350 if (!ThrowIfTemporalLikeObject(cx, temporalDateLike)) {
1351 return false;
1352 }
1353
1354 // Step 4.
1355 auto calendar = temporalDate.calendar();
1356
1357 // Step 5.
1358 Rooted<CalendarFields> fields(cx);
1359 if (!ISODateToFields(cx, temporalDate, &fields)) {
1360 return false;
1361 }
1362
1363 // Step 6.
1364 Rooted<CalendarFields> partialDate(cx);
1365 if (!PreparePartialCalendarFields(cx, calendar, temporalDateLike,
1366 {
1367 CalendarField::Year,
1368 CalendarField::Month,
1369 CalendarField::MonthCode,
1370 CalendarField::Day,
1371 },
1372 &partialDate)) {
1373 return false;
1374 }
1375 MOZ_ASSERT(!partialDate.keys().isEmpty());
1376
1377 // Step 7.
1378 fields = CalendarMergeFields(calendar, fields, partialDate);
1379
1380 // Steps 8-9.
1381 auto overflow = TemporalOverflow::Constrain;
1382 if (args.hasDefined(1)) {
1383 // Step 8.
1384 Rooted<JSObject*> options(cx,
1385 RequireObjectArg(cx, "options", "with", args[1]));
1386 if (!options) {
1387 return false;
1388 }
1389
1390 // Step 9.
1391 if (!GetTemporalOverflowOption(cx, options, &overflow)) {
1392 return false;
1393 }
1394 }
1395
1396 // Step 10.
1397 Rooted<PlainDate> date(cx);
1398 if (!CalendarDateFromFields(cx, calendar, fields, overflow, &date)) {
1399 return false;
1400 }
1401
1402 // Step 11.
1403 auto* result = CreateTemporalDate(cx, date);
1404 if (!result) {
1405 return false;
1406 }
1407
1408 args.rval().setObject(*result);
1409 return true;
1410 }
1411
1412 /**
1413 * Temporal.PlainDate.prototype.with ( temporalDateLike [ , options ] )
1414 */
1415 static bool PlainDate_with(JSContext* cx, unsigned argc, Value* vp) {
1416 // Steps 1-2.
1417 CallArgs args = CallArgsFromVp(argc, vp);
1418 return CallNonGenericMethod<IsPlainDate, PlainDate_with>(cx, args);
1419 }
1420
1421 /**
1422 * Temporal.PlainDate.prototype.withCalendar ( calendar )
1423 */
1424 static bool PlainDate_withCalendar(JSContext* cx, const CallArgs& args) {
1425 auto* temporalDate = &args.thisv().toObject().as<PlainDateObject>();
1426 auto date = temporalDate->date();
1427
1428 // Step 3.
1429 Rooted<CalendarValue> calendar(cx);
1430 if (!ToTemporalCalendar(cx, args.get(0), &calendar)) {
1431 return false;
1432 }
1433
1434 // Step 4.
1435 auto* result = CreateTemporalDate(cx, date, calendar);
1436 if (!result) {
1437 return false;
1438 }
1439
1440 args.rval().setObject(*result);
1441 return true;
1442 }
1443
1444 /**
1445 * Temporal.PlainDate.prototype.withCalendar ( calendar )
1446 */
1447 static bool PlainDate_withCalendar(JSContext* cx, unsigned argc, Value* vp) {
1448 // Steps 1-2.
1449 CallArgs args = CallArgsFromVp(argc, vp);
1450 return CallNonGenericMethod<IsPlainDate, PlainDate_withCalendar>(cx, args);
1451 }
1452
1453 /**
1454 * Temporal.PlainDate.prototype.until ( other [ , options ] )
1455 */
1456 static bool PlainDate_until(JSContext* cx, const CallArgs& args) {
1457 // Step 3.
1458 return DifferenceTemporalPlainDate(cx, TemporalDifference::Until, args);
1459 }
1460
1461 /**
1462 * Temporal.PlainDate.prototype.until ( other [ , options ] )
1463 */
1464 static bool PlainDate_until(JSContext* cx, unsigned argc, Value* vp) {
1465 // Steps 1-2.
1466 CallArgs args = CallArgsFromVp(argc, vp);
1467 return CallNonGenericMethod<IsPlainDate, PlainDate_until>(cx, args);
1468 }
1469
1470 /**
1471 * Temporal.PlainDate.prototype.since ( other [ , options ] )
1472 */
1473 static bool PlainDate_since(JSContext* cx, const CallArgs& args) {
1474 // Step 3.
1475 return DifferenceTemporalPlainDate(cx, TemporalDifference::Since, args);
1476 }
1477
1478 /**
1479 * Temporal.PlainDate.prototype.since ( other [ , options ] )
1480 */
1481 static bool PlainDate_since(JSContext* cx, unsigned argc, Value* vp) {
1482 // Steps 1-2.
1483 CallArgs args = CallArgsFromVp(argc, vp);
1484 return CallNonGenericMethod<IsPlainDate, PlainDate_since>(cx, args);
1485 }
1486
1487 /**
1488 * Temporal.PlainDate.prototype.equals ( other )
1489 */
1490 static bool PlainDate_equals(JSContext* cx, const CallArgs& args) {
1491 auto* temporalDate = &args.thisv().toObject().as<PlainDateObject>();
1492 auto date = temporalDate->date();
1493 Rooted<CalendarValue> calendar(cx, temporalDate->calendar());
1494
1495 // Step 3.
1496 Rooted<PlainDate> other(cx);
1497 if (!ToTemporalDate(cx, args.get(0), &other)) {
1498 return false;
1499 }
1500
1501 // Steps 4-5.
1502 bool equals =
1503 date == other.date() && CalendarEquals(calendar, other.calendar());
1504
1505 args.rval().setBoolean(equals);
1506 return true;
1507 }
1508
1509 /**
1510 * Temporal.PlainDate.prototype.equals ( other )
1511 */
1512 static bool PlainDate_equals(JSContext* cx, unsigned argc, Value* vp) {
1513 // Steps 1-2.
1514 CallArgs args = CallArgsFromVp(argc, vp);
1515 return CallNonGenericMethod<IsPlainDate, PlainDate_equals>(cx, args);
1516 }
1517
1518 /**
1519 * Temporal.PlainDate.prototype.toZonedDateTime ( item )
1520 *
1521 * The |item| argument represents either a time zone or an options object. The
1522 * following cases are supported:
1523 * - |item| is an options object with `timeZone` and `plainTime` properties.
1524 * - |item| is a time zone identifier string.
1525 */
1526 static bool PlainDate_toZonedDateTime(JSContext* cx, const CallArgs& args) {
1527 auto* temporalDate = &args.thisv().toObject().as<PlainDateObject>();
1528 auto date = temporalDate->date();
1529 Rooted<CalendarValue> calendar(cx, temporalDate->calendar());
1530
1531 // Steps 3-4
1532 Rooted<TimeZoneValue> timeZone(cx);
1533 Rooted<Value> temporalTime(cx);
1534 if (args.get(0).isObject()) {
1535 Rooted<JSObject*> item(cx, &args[0].toObject());
1536
1537 // Step 3.a.
1538 Rooted<Value> timeZoneLike(cx);
1539 if (!GetProperty(cx, item, item, cx->names().timeZone, &timeZoneLike)) {
1540 return false;
1541 }
1542
1543 // Steps 3.b-c.
1544 if (timeZoneLike.isUndefined()) {
1545 // Step 3.b.i.
1546 if (!ToTemporalTimeZone(cx, args[0], &timeZone)) {
1547 return false;
1548 }
1549
1550 // Step 3.b.ii.
1551 MOZ_ASSERT(temporalTime.isUndefined());
1552 } else {
1553 // Step 3.c.i.
1554 if (!ToTemporalTimeZone(cx, timeZoneLike, &timeZone)) {
1555 return false;
1556 }
1557
1558 // Step 3.c.ii.
1559 if (!GetProperty(cx, item, item, cx->names().plainTime, &temporalTime)) {
1560 return false;
1561 }
1562 }
1563 } else {
1564 // Step 4.a.
1565 if (!ToTemporalTimeZone(cx, args.get(0), &timeZone)) {
1566 return false;
1567 }
1568
1569 // Step 4.b.
1570 MOZ_ASSERT(temporalTime.isUndefined());
1571 }
1572
1573 // Steps 5-6.
1574 EpochNanoseconds epochNs;
1575 if (temporalTime.isUndefined()) {
1576 // Step 5.a.
1577 if (!GetStartOfDay(cx, timeZone, date, &epochNs)) {
1578 return false;
1579 }
1580 } else {
1581 // Step 6.a.
1582 Time time;
1583 if (!ToTemporalTime(cx, temporalTime, &time)) {
1584 return false;
1585 }
1586
1587 // Step 6.b.
1588 auto isoDateTime = ISODateTime{date, time};
1589
1590 // Step 6.c.
1591 if (!ISODateTimeWithinLimits(isoDateTime)) {
1592 JS_ReportErrorNumberASCII(cx, GetErrorMessage, nullptr,
1593 JSMSG_TEMPORAL_PLAIN_DATE_TIME_INVALID);
1594 return false;
1595 }
1596
1597 // Step 6.d.
1598 if (!GetEpochNanosecondsFor(cx, timeZone, isoDateTime,
1599 TemporalDisambiguation::Compatible, &epochNs)) {
1600 return false;
1601 }
1602 }
1603
1604 // Step 7.
1605 auto* result = CreateTemporalZonedDateTime(cx, epochNs, timeZone, calendar);
1606 if (!result) {
1607 return false;
1608 }
1609
1610 args.rval().setObject(*result);
1611 return true;
1612 }
1613
1614 /**
1615 * Temporal.PlainDate.prototype.toZonedDateTime ( item )
1616 */
1617 static bool PlainDate_toZonedDateTime(JSContext* cx, unsigned argc, Value* vp) {
1618 // Steps 1-2.
1619 CallArgs args = CallArgsFromVp(argc, vp);
1620 return CallNonGenericMethod<IsPlainDate, PlainDate_toZonedDateTime>(cx, args);
1621 }
1622
1623 /**
1624 * Temporal.PlainDate.prototype.toString ( [ options ] )
1625 */
1626 static bool PlainDate_toString(JSContext* cx, const CallArgs& args) {
1627 Rooted<PlainDateObject*> temporalDate(
1628 cx, &args.thisv().toObject().as<PlainDateObject>());
1629
1630 auto showCalendar = ShowCalendar::Auto;
1631 if (args.hasDefined(0)) {
1632 // Step 3.
1633 Rooted<JSObject*> options(
1634 cx, RequireObjectArg(cx, "options", "toString", args[0]));
1635 if (!options) {
1636 return false;
1637 }
1638
1639 // Step 4.
1640 if (!GetTemporalShowCalendarNameOption(cx, options, &showCalendar)) {
1641 return false;
1642 }
1643 }
1644
1645 // Step 5.
1646 JSString* str = TemporalDateToString(cx, temporalDate, showCalendar);
1647 if (!str) {
1648 return false;
1649 }
1650
1651 args.rval().setString(str);
1652 return true;
1653 }
1654
1655 /**
1656 * Temporal.PlainDate.prototype.toString ( [ options ] )
1657 */
1658 static bool PlainDate_toString(JSContext* cx, unsigned argc, Value* vp) {
1659 // Steps 1-2.
1660 CallArgs args = CallArgsFromVp(argc, vp);
1661 return CallNonGenericMethod<IsPlainDate, PlainDate_toString>(cx, args);
1662 }
1663
1664 /**
1665 * Temporal.PlainDate.prototype.toLocaleString ( [ locales [ , options ] ] )
1666 */
1667 static bool PlainDate_toLocaleString(JSContext* cx, const CallArgs& args) {
1668 // Steps 3-4.
1669 Handle<PropertyName*> required = cx->names().date;
1670 Handle<PropertyName*> defaults = cx->names().date;
1671 return TemporalObjectToLocaleString(cx, args, required, defaults);
1672 }
1673
1674 /**
1675 * Temporal.PlainDate.prototype.toLocaleString ( [ locales [ , options ] ] )
1676 */
1677 static bool PlainDate_toLocaleString(JSContext* cx, unsigned argc, Value* vp) {
1678 // Steps 1-2.
1679 CallArgs args = CallArgsFromVp(argc, vp);
1680 return CallNonGenericMethod<IsPlainDate, PlainDate_toLocaleString>(cx, args);
1681 }
1682
1683 /**
1684 * Temporal.PlainDate.prototype.toJSON ( )
1685 */
1686 static bool PlainDate_toJSON(JSContext* cx, const CallArgs& args) {
1687 Rooted<PlainDateObject*> temporalDate(
1688 cx, &args.thisv().toObject().as<PlainDateObject>());
1689
1690 // Step 3.
1691 JSString* str = TemporalDateToString(cx, temporalDate, ShowCalendar::Auto);
1692 if (!str) {
1693 return false;
1694 }
1695
1696 args.rval().setString(str);
1697 return true;
1698 }
1699
1700 /**
1701 * Temporal.PlainDate.prototype.toJSON ( )
1702 */
1703 static bool PlainDate_toJSON(JSContext* cx, unsigned argc, Value* vp) {
1704 // Steps 1-2.
1705 CallArgs args = CallArgsFromVp(argc, vp);
1706 return CallNonGenericMethod<IsPlainDate, PlainDate_toJSON>(cx, args);
1707 }
1708
1709 /**
1710 * Temporal.PlainDate.prototype.valueOf ( )
1711 */
1712 static bool PlainDate_valueOf(JSContext* cx, unsigned argc, Value* vp) {
1713 JS_ReportErrorNumberASCII(cx, GetErrorMessage, nullptr, JSMSG_CANT_CONVERT_TO,
1714 "PlainDate", "primitive type");
1715 return false;
1716 }
1717
1718 const JSClass PlainDateObject::class_ = {
1719 "Temporal.PlainDate",
1720 JSCLASS_HAS_RESERVED_SLOTS(PlainDateObject::SLOT_COUNT) |
1721 JSCLASS_HAS_CACHED_PROTO(JSProto_PlainDate),
1722 JS_NULL_CLASS_OPS,
1723 &PlainDateObject::classSpec_,
1724 };
1725
1726 const JSClass& PlainDateObject::protoClass_ = PlainObject::class_;
1727
1728 static const JSFunctionSpec PlainDate_methods[] = {
1729 JS_FN("from", PlainDate_from, 1, 0),
1730 JS_FN("compare", PlainDate_compare, 2, 0),
1731 JS_FS_END,
1732 };
1733
1734 static const JSFunctionSpec PlainDate_prototype_methods[] = {
1735 JS_FN("toPlainMonthDay", PlainDate_toPlainMonthDay, 0, 0),
1736 JS_FN("toPlainYearMonth", PlainDate_toPlainYearMonth, 0, 0),
1737 JS_FN("toPlainDateTime", PlainDate_toPlainDateTime, 0, 0),
1738 JS_FN("add", PlainDate_add, 1, 0),
1739 JS_FN("subtract", PlainDate_subtract, 1, 0),
1740 JS_FN("with", PlainDate_with, 1, 0),
1741 JS_FN("withCalendar", PlainDate_withCalendar, 1, 0),
1742 JS_FN("until", PlainDate_until, 1, 0),
1743 JS_FN("since", PlainDate_since, 1, 0),
1744 JS_FN("equals", PlainDate_equals, 1, 0),
1745 JS_FN("toZonedDateTime", PlainDate_toZonedDateTime, 1, 0),
1746 JS_FN("toString", PlainDate_toString, 0, 0),
1747 JS_FN("toLocaleString", PlainDate_toLocaleString, 0, 0),
1748 JS_FN("toJSON", PlainDate_toJSON, 0, 0),
1749 JS_FN("valueOf", PlainDate_valueOf, 0, 0),
1750 JS_FS_END,
1751 };
1752
1753 static const JSPropertySpec PlainDate_prototype_properties[] = {
1754 JS_PSG("calendarId", PlainDate_calendarId, 0),
1755 JS_PSG("era", PlainDate_era, 0),
1756 JS_PSG("eraYear", PlainDate_eraYear, 0),
1757 JS_PSG("year", PlainDate_year, 0),
1758 JS_PSG("month", PlainDate_month, 0),
1759 JS_PSG("monthCode", PlainDate_monthCode, 0),
1760 JS_PSG("day", PlainDate_day, 0),
1761 JS_PSG("dayOfWeek", PlainDate_dayOfWeek, 0),
1762 JS_PSG("dayOfYear", PlainDate_dayOfYear, 0),
1763 JS_PSG("weekOfYear", PlainDate_weekOfYear, 0),
1764 JS_PSG("yearOfWeek", PlainDate_yearOfWeek, 0),
1765 JS_PSG("daysInWeek", PlainDate_daysInWeek, 0),
1766 JS_PSG("daysInMonth", PlainDate_daysInMonth, 0),
1767 JS_PSG("daysInYear", PlainDate_daysInYear, 0),
1768 JS_PSG("monthsInYear", PlainDate_monthsInYear, 0),
1769 JS_PSG("inLeapYear", PlainDate_inLeapYear, 0),
1770 JS_STRING_SYM_PS(toStringTag, "Temporal.PlainDate", JSPROP_READONLY),
1771 JS_PS_END,
1772 };
1773
1774 const ClassSpec PlainDateObject::classSpec_ = {
1775 GenericCreateConstructor<PlainDateConstructor, 3, gc::AllocKind::FUNCTION>,
1776 GenericCreatePrototype<PlainDateObject>,
1777 PlainDate_methods,
1778 nullptr,
1779 PlainDate_prototype_methods,
1780 PlainDate_prototype_properties,
1781 nullptr,
1782 ClassSpec::DontDefineConstructor,
1783 };