Debian Codegraph @

js/src/builtin/temporal/PlainTime.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/PlainTime.h"
8
9 #include "mozilla/Assertions.h"
10
11 #include <algorithm>
12 #include <cmath>
13 #include <cstdlib>
14
15 #include "jsnum.h"
16 #include "jspubtd.h"
17 #include "NamespaceImports.h"
18
19 #include "builtin/intl/DateTimeFormat.h"
20 #include "builtin/temporal/Duration.h"
21 #include "builtin/temporal/PlainDateTime.h"
22 #include "builtin/temporal/Temporal.h"
23 #include "builtin/temporal/TemporalParser.h"
24 #include "builtin/temporal/TemporalRoundingMode.h"
25 #include "builtin/temporal/TemporalTypes.h"
26 #include "builtin/temporal/TemporalUnit.h"
27 #include "builtin/temporal/TimeZone.h"
28 #include "builtin/temporal/ToString.h"
29 #include "builtin/temporal/ZonedDateTime.h"
30 #include "gc/AllocKind.h"
31 #include "gc/Barrier.h"
32 #include "js/CallArgs.h"
33 #include "js/CallNonGenericMethod.h"
34 #include "js/Class.h"
35 #include "js/ErrorReport.h"
36 #include "js/friend/ErrorMessages.h"
37 #include "js/PropertyDescriptor.h"
38 #include "js/PropertySpec.h"
39 #include "js/RootingAPI.h"
40 #include "js/Value.h"
41 #include "vm/BytecodeUtil.h"
42 #include "vm/GlobalObject.h"
43 #include "vm/JSAtomState.h"
44 #include "vm/JSContext.h"
45 #include "vm/JSObject.h"
46 #include "vm/PlainObject.h"
47
48 #include "vm/JSObject-inl.h"
49 #include "vm/NativeObject-inl.h"
50 #include "vm/ObjectOperations-inl.h"
51
52 using namespace js;
53 using namespace js::temporal;
54
55 static inline bool IsPlainTime(Handle<Value> v) {
56 return v.isObject() && v.toObject().is<PlainTimeObject>();
57 }
58
59 #ifdef DEBUG
60 /**
61 * IsValidTime ( hour, minute, second, millisecond, microsecond, nanosecond )
62 */
63 template <typename T>
64 static bool IsValidTime(T hour, T minute, T second, T millisecond,
65 T microsecond, T nanosecond) {
66 static_assert(std::is_same_v<T, int32_t> || std::is_same_v<T, double>);
67
68 MOZ_ASSERT(IsInteger(hour));
69 MOZ_ASSERT(IsInteger(minute));
70 MOZ_ASSERT(IsInteger(second));
71 MOZ_ASSERT(IsInteger(millisecond));
72 MOZ_ASSERT(IsInteger(microsecond));
73 MOZ_ASSERT(IsInteger(nanosecond));
74
75 // Step 1.
76 if (hour < 0 || hour > 23) {
77 return false;
78 }
79
80 // Step 2.
81 if (minute < 0 || minute > 59) {
82 return false;
83 }
84
85 // Step 3.
86 if (second < 0 || second > 59) {
87 return false;
88 }
89
90 // Step 4.
91 if (millisecond < 0 || millisecond > 999) {
92 return false;
93 }
94
95 // Step 5.
96 if (microsecond < 0 || microsecond > 999) {
97 return false;
98 }
99
100 // Step 6.
101 if (nanosecond < 0 || nanosecond > 999) {
102 return false;
103 }
104
105 // Step 7.
106 return true;
107 }
108
109 /**
110 * IsValidTime ( hour, minute, second, millisecond, microsecond, nanosecond )
111 */
112 bool js::temporal::IsValidTime(const Time& time) {
113 const auto& [hour, minute, second, millisecond, microsecond, nanosecond] =
114 time;
115 return ::IsValidTime(hour, minute, second, millisecond, microsecond,
116 nanosecond);
117 }
118
119 /**
120 * IsValidTime ( hour, minute, second, millisecond, microsecond, nanosecond )
121 */
122 bool js::temporal::IsValidTime(double hour, double minute, double second,
123 double millisecond, double microsecond,
124 double nanosecond) {
125 return ::IsValidTime(hour, minute, second, millisecond, microsecond,
126 nanosecond);
127 }
128 #endif
129
130 static void ReportInvalidTimeValue(JSContext* cx, const char* name, int32_t min,
131 int32_t max, double num) {
132 Int32ToCStringBuf minCbuf;
133 const char* minStr = Int32ToCString(&minCbuf, min);
134
135 Int32ToCStringBuf maxCbuf;
136 const char* maxStr = Int32ToCString(&maxCbuf, max);
137
138 ToCStringBuf numCbuf;
139 const char* numStr = NumberToCString(&numCbuf, num);
140
141 JS_ReportErrorNumberASCII(cx, GetErrorMessage, nullptr,
142 JSMSG_TEMPORAL_PLAIN_TIME_INVALID_VALUE, name,
143 minStr, maxStr, numStr);
144 }
145
146 static inline bool ThrowIfInvalidTimeValue(JSContext* cx, const char* name,
147 int32_t min, int32_t max,
148 double num) {
149 if (min <= num && num <= max) {
150 return true;
151 }
152 ReportInvalidTimeValue(cx, name, min, max, num);
153 return false;
154 }
155
156 /**
157 * IsValidTime ( hour, minute, second, millisecond, microsecond, nanosecond )
158 */
159 bool js::temporal::ThrowIfInvalidTime(JSContext* cx, double hour, double minute,
160 double second, double millisecond,
161 double microsecond, double nanosecond) {
162 // Step 1.
163 MOZ_ASSERT(IsInteger(hour));
164 MOZ_ASSERT(IsInteger(minute));
165 MOZ_ASSERT(IsInteger(second));
166 MOZ_ASSERT(IsInteger(millisecond));
167 MOZ_ASSERT(IsInteger(microsecond));
168 MOZ_ASSERT(IsInteger(nanosecond));
169
170 // Step 2.
171 if (!ThrowIfInvalidTimeValue(cx, "hour", 0, 23, hour)) {
172 return false;
173 }
174
175 // Step 3.
176 if (!ThrowIfInvalidTimeValue(cx, "minute", 0, 59, minute)) {
177 return false;
178 }
179
180 // Step 4.
181 if (!ThrowIfInvalidTimeValue(cx, "second", 0, 59, second)) {
182 return false;
183 }
184
185 // Step 5.
186 if (!ThrowIfInvalidTimeValue(cx, "millisecond", 0, 999, millisecond)) {
187 return false;
188 }
189
190 // Step 6.
191 if (!ThrowIfInvalidTimeValue(cx, "microsecond", 0, 999, microsecond)) {
192 return false;
193 }
194
195 // Step 7.
196 if (!ThrowIfInvalidTimeValue(cx, "nanosecond", 0, 999, nanosecond)) {
197 return false;
198 }
199
200 // Step 8.
201 return true;
202 }
203
204 /**
205 * RegulateTime ( hour, minute, second, millisecond, microsecond, nanosecond,
206 * overflow )
207 */
208 bool js::temporal::RegulateTime(JSContext* cx, const TemporalTimeLike& time,
209 TemporalOverflow overflow, Time* result) {
210 auto [hour, minute, second, millisecond, microsecond, nanosecond] = time;
211 MOZ_ASSERT(IsInteger(hour));
212 MOZ_ASSERT(IsInteger(minute));
213 MOZ_ASSERT(IsInteger(second));
214 MOZ_ASSERT(IsInteger(millisecond));
215 MOZ_ASSERT(IsInteger(microsecond));
216 MOZ_ASSERT(IsInteger(nanosecond));
217
218 // Steps 1-2.
219 if (overflow == TemporalOverflow::Constrain) {
220 // Step 1.a.
221 hour = std::clamp(hour, 0.0, 23.0);
222
223 // Step 1.b.
224 minute = std::clamp(minute, 0.0, 59.0);
225
226 // Step 1.c.
227 second = std::clamp(second, 0.0, 59.0);
228
229 // Step 1.d.
230 millisecond = std::clamp(millisecond, 0.0, 999.0);
231
232 // Step 1.e.
233 microsecond = std::clamp(microsecond, 0.0, 999.0);
234
235 // Step 1.f.
236 nanosecond = std::clamp(nanosecond, 0.0, 999.0);
237 } else {
238 // Step 2.a.
239 MOZ_ASSERT(overflow == TemporalOverflow::Reject);
240
241 // Step 2.b.
242 if (!ThrowIfInvalidTime(cx, hour, minute, second, millisecond, microsecond,
243 nanosecond)) {
244 return false;
245 }
246 }
247
248 // Step 3.
249 *result = {
250 int32_t(hour), int32_t(minute), int32_t(second),
251 int32_t(millisecond), int32_t(microsecond), int32_t(nanosecond),
252 };
253 return true;
254 }
255
256 /**
257 * CreateTemporalTime ( time [ , newTarget ] )
258 */
259 static PlainTimeObject* CreateTemporalTime(JSContext* cx, const CallArgs& args,
260 const Time& time) {
261 MOZ_ASSERT(IsValidTime(time));
262
263 // Steps 1-2.
264 Rooted<JSObject*> proto(cx);
265 if (!GetPrototypeFromBuiltinConstructor(cx, args, JSProto_PlainTime,
266 &proto)) {
267 return nullptr;
268 }
269
270 auto* object = NewObjectWithClassProto<PlainTimeObject>(cx, proto);
271 if (!object) {
272 return nullptr;
273 }
274
275 // Step 3.
276 auto packedTime = PackedTime::pack(time);
277 object->setFixedSlot(
278 PlainTimeObject::PACKED_TIME_SLOT,
279 DoubleValue(mozilla::BitwiseCast<double>(packedTime.value)));
280
281 // Step 4.
282 return object;
283 }
284
285 /**
286 * CreateTemporalTime ( time [ , newTarget ] )
287 */
288 PlainTimeObject* js::temporal::CreateTemporalTime(JSContext* cx,
289 const Time& time) {
290 MOZ_ASSERT(IsValidTime(time));
291
292 // Steps 1-2.
293 auto* object = NewBuiltinClassInstance<PlainTimeObject>(cx);
294 if (!object) {
295 return nullptr;
296 }
297
298 // Step 3.
299 auto packedTime = PackedTime::pack(time);
300 object->setFixedSlot(
301 PlainTimeObject::PACKED_TIME_SLOT,
302 DoubleValue(mozilla::BitwiseCast<double>(packedTime.value)));
303
304 // Step 4.
305 return object;
306 }
307
308 /**
309 * BalanceTime ( hour, minute, second, millisecond, microsecond, nanosecond )
310 */
311 template <typename IntT>
312 static TimeRecord BalanceTime(IntT hour, IntT minute, IntT second,
313 IntT millisecond, IntT microsecond,
314 IntT nanosecond) {
315 // Combined floor'ed division and modulo operation.
316 auto divmod = [](IntT dividend, int32_t divisor, int32_t* remainder) {
317 MOZ_ASSERT(divisor > 0);
318
319 IntT quotient = dividend / divisor;
320 *remainder = dividend % divisor;
321
322 // The remainder is negative, add the divisor and simulate a floor instead
323 // of trunc division.
324 if (*remainder < 0) {
325 *remainder += divisor;
326 quotient -= 1;
327 }
328
329 return quotient;
330 };
331
332 Time time = {};
333
334 // Steps 1-2.
335 microsecond += divmod(nanosecond, 1000, &time.nanosecond);
336
337 // Steps 3-4.
338 millisecond += divmod(microsecond, 1000, &time.microsecond);
339
340 // Steps 5-6.
341 second += divmod(millisecond, 1000, &time.millisecond);
342
343 // Steps 7-8.
344 minute += divmod(second, 60, &time.second);
345
346 // Steps 9-10.
347 hour += divmod(minute, 60, &time.minute);
348
349 // Steps 11-12.
350 int64_t days = divmod(hour, 24, &time.hour);
351
352 // Step 13.
353 MOZ_ASSERT(IsValidTime(time));
354 return {days, time};
355 }
356
357 /**
358 * BalanceTime ( hour, minute, second, millisecond, microsecond, nanosecond )
359 */
360 static TimeRecord BalanceTime(int32_t hour, int32_t minute, int32_t second,
361 int32_t millisecond, int32_t microsecond,
362 int32_t nanosecond) {
363 MOZ_ASSERT(-24 < hour && hour < 2 * 24);
364 MOZ_ASSERT(-60 < minute && minute < 2 * 60);
365 MOZ_ASSERT(-60 < second && second < 2 * 60);
366 MOZ_ASSERT(-1000 < millisecond && millisecond < 2 * 1000);
367 MOZ_ASSERT(-1000 < microsecond && microsecond < 2 * 1000);
368 MOZ_ASSERT(-1000 < nanosecond && nanosecond < 2 * 1000);
369
370 return BalanceTime<int32_t>(hour, minute, second, millisecond, microsecond,
371 nanosecond);
372 }
373
374 /**
375 * BalanceTime ( hour, minute, second, millisecond, microsecond, nanosecond )
376 */
377 TimeRecord js::temporal::BalanceTime(const Time& time, int64_t nanoseconds) {
378 MOZ_ASSERT(IsValidTime(time));
379 MOZ_ASSERT(std::abs(nanoseconds) <= ToNanoseconds(TemporalUnit::Day));
380
381 return ::BalanceTime<int64_t>(time.hour, time.minute, time.second,
382 time.millisecond, time.microsecond,
383 time.nanosecond + nanoseconds);
384 }
385
386 /**
387 * TimeDurationFromComponents ( hours, minutes, seconds, milliseconds,
388 * microseconds, nanoseconds )
389 */
390 static TimeDuration TimeDurationFromComponents(int32_t hours, int32_t minutes,
391 int32_t seconds,
392 int32_t milliseconds,
393 int32_t microseconds,
394 int32_t nanoseconds) {
395 MOZ_ASSERT(std::abs(hours) <= 23);
396 MOZ_ASSERT(std::abs(minutes) <= 59);
397 MOZ_ASSERT(std::abs(seconds) <= 59);
398 MOZ_ASSERT(std::abs(milliseconds) <= 999);
399 MOZ_ASSERT(std::abs(microseconds) <= 999);
400 MOZ_ASSERT(std::abs(nanoseconds) <= 999);
401
402 // Steps 1-5.
403 int64_t nanos = int64_t(hours);
404 nanos *= 60;
405 nanos += int64_t(minutes);
406 nanos *= 60;
407 nanos += int64_t(seconds);
408 nanos *= 1000;
409 nanos += int64_t(milliseconds);
410 nanos *= 1000;
411 nanos += int64_t(microseconds);
412 nanos *= 1000;
413 nanos += int64_t(nanoseconds);
414 MOZ_ASSERT(std::abs(nanos) < ToNanoseconds(TemporalUnit::Day));
415
416 auto timeDuration = TimeDuration::fromNanoseconds(nanos);
417
418 // Step 6.
419 MOZ_ASSERT(IsValidTimeDuration(timeDuration));
420
421 // Step 7.
422 return timeDuration;
423 }
424
425 /**
426 * DifferenceTime ( time1, time2 )
427 */
428 TimeDuration js::temporal::DifferenceTime(const Time& time1,
429 const Time& time2) {
430 MOZ_ASSERT(IsValidTime(time1));
431 MOZ_ASSERT(IsValidTime(time2));
432
433 // Step 1.
434 int32_t hours = time2.hour - time1.hour;
435
436 // Step 2.
437 int32_t minutes = time2.minute - time1.minute;
438
439 // Step 3.
440 int32_t seconds = time2.second - time1.second;
441
442 // Step 4.
443 int32_t milliseconds = time2.millisecond - time1.millisecond;
444
445 // Step 5.
446 int32_t microseconds = time2.microsecond - time1.microsecond;
447
448 // Step 6.
449 int32_t nanoseconds = time2.nanosecond - time1.nanosecond;
450
451 // Step 7.
452 auto result = ::TimeDurationFromComponents(
453 hours, minutes, seconds, milliseconds, microseconds, nanoseconds);
454
455 // Step 8.
456 MOZ_ASSERT(result.abs() < TimeDuration::fromDays(1));
457
458 // Step 9.
459 return result;
460 }
461
462 /**
463 * ToTemporalTimeRecord ( temporalTimeLike [ , completeness ] )
464 */
465 static bool ToTemporalTimeRecord(JSContext* cx,
466 Handle<JSObject*> temporalTimeLike,
467 TemporalTimeLike* result) {
468 // Steps 1-3. (Not applicable in our implementation.)
469
470 // Step 4.
471 bool any = false;
472
473 Rooted<Value> value(cx);
474 auto getTimeProperty = [&](Handle<PropertyName*> property, const char* name,
475 double* num) {
476 if (!GetProperty(cx, temporalTimeLike, temporalTimeLike, property,
477 &value)) {
478 return false;
479 }
480
481 if (!value.isUndefined()) {
482 any = true;
483
484 if (!ToIntegerWithTruncation(cx, value, name, num)) {
485 return false;
486 }
487 }
488 return true;
489 };
490
491 // Steps 5-6.
492 if (!getTimeProperty(cx->names().hour, "hour", &result->hour)) {
493 return false;
494 }
495
496 // Steps 7-8.
497 if (!getTimeProperty(cx->names().microsecond, "microsecond",
498 &result->microsecond)) {
499 return false;
500 }
501
502 // Steps 9-10.
503 if (!getTimeProperty(cx->names().millisecond, "millisecond",
504 &result->millisecond)) {
505 return false;
506 }
507
508 // Steps 11-12.
509 if (!getTimeProperty(cx->names().minute, "minute", &result->minute)) {
510 return false;
511 }
512
513 // Steps 13-14.
514 if (!getTimeProperty(cx->names().nanosecond, "nanosecond",
515 &result->nanosecond)) {
516 return false;
517 }
518
519 // Steps 15-16.
520 if (!getTimeProperty(cx->names().second, "second", &result->second)) {
521 return false;
522 }
523
524 // Step 17.
525 if (!any) {
526 JS_ReportErrorNumberASCII(cx, GetErrorMessage, nullptr,
527 JSMSG_TEMPORAL_PLAIN_TIME_MISSING_UNIT);
528 return false;
529 }
530
531 // Step 18.
532 return true;
533 }
534
535 struct TimeOptions {
536 TemporalOverflow overflow = TemporalOverflow::Constrain;
537 };
538
539 /**
540 * ToTemporalTime ( item [ , options ] )
541 */
542 static bool ToTemporalTimeOptions(JSContext* cx, Handle<Value> options,
543 TimeOptions* result) {
544 if (options.isUndefined()) {
545 *result = {};
546 return true;
547 }
548
549 // NOTE: |options| are only passed from `Temporal.PlainTime.from`.
550
551 Rooted<JSObject*> resolvedOptions(
552 cx, RequireObjectArg(cx, "options", "from", options));
553 if (!resolvedOptions) {
554 return false;
555 }
556
557 auto overflow = TemporalOverflow::Constrain;
558 if (!GetTemporalOverflowOption(cx, resolvedOptions, &overflow)) {
559 return false;
560 }
561
562 *result = {overflow};
563 return true;
564 }
565
566 /**
567 * ToTemporalTime ( item [ , options ] )
568 */
569 static bool ToTemporalTime(JSContext* cx, Handle<JSObject*> item,
570 Handle<Value> options, Time* result) {
571 // Step 2.a.
572 if (auto* plainTime = item->maybeUnwrapIf<PlainTimeObject>()) {
573 auto time = plainTime->time();
574
575 // Steps 2.a.i-ii.
576 TimeOptions ignoredOptions;
577 if (!ToTemporalTimeOptions(cx, options, &ignoredOptions)) {
578 return false;
579 }
580
581 // Step 2.a.iii.
582 *result = time;
583 return true;
584 }
585
586 // Step 2.b.
587 if (auto* dateTime = item->maybeUnwrapIf<PlainDateTimeObject>()) {
588 auto time = dateTime->time();
589
590 // Steps 2.b.i-ii.
591 TimeOptions ignoredOptions;
592 if (!ToTemporalTimeOptions(cx, options, &ignoredOptions)) {
593 return false;
594 }
595
596 // Step 2.b.iii.
597 *result = time;
598 return true;
599 }
600
601 // Step 2.c.
602 if (auto* zonedDateTime = item->maybeUnwrapIf<ZonedDateTimeObject>()) {
603 auto epochNs = zonedDateTime->epochNanoseconds();
604 Rooted<TimeZoneValue> timeZone(cx, zonedDateTime->timeZone());
605
606 if (!timeZone.wrap(cx)) {
607 return false;
608 }
609
610 // Steps 2.c.i.
611 ISODateTime dateTime;
612 if (!GetISODateTimeFor(cx, timeZone, epochNs, &dateTime)) {
613 return false;
614 }
615
616 // Steps 2.c.ii-iii.
617 TimeOptions ignoredOptions;
618 if (!ToTemporalTimeOptions(cx, options, &ignoredOptions)) {
619 return false;
620 }
621
622 // Step 2.c.iv.
623 *result = dateTime.time;
624 return true;
625 }
626
627 // Step 2.d.
628 TemporalTimeLike timeResult{};
629 if (!ToTemporalTimeRecord(cx, item, &timeResult)) {
630 return false;
631 }
632
633 // Steps 2.e-f.
634 TimeOptions resolvedOptions;
635 if (!ToTemporalTimeOptions(cx, options, &resolvedOptions)) {
636 return false;
637 }
638 auto [overflow] = resolvedOptions;
639
640 // Step 2.g and 4.
641 return RegulateTime(cx, timeResult, overflow, result);
642 }
643
644 /**
645 * ToTemporalTime ( item [ , options ] )
646 */
647 static bool ToTemporalTime(JSContext* cx, Handle<Value> item,
648 Handle<Value> options, Time* result) {
649 // Step 1. (Not applicable in our implementation.)
650
651 // Step 2.
652 if (item.isObject()) {
653 Rooted<JSObject*> itemObj(cx, &item.toObject());
654 return ToTemporalTime(cx, itemObj, options, result);
655 }
656
657 // Step 3.a.
658 if (!item.isString()) {
659 ReportValueError(cx, JSMSG_UNEXPECTED_TYPE, JSDVG_IGNORE_STACK, item,
660 nullptr, "not a string");
661 return false;
662 }
663 Rooted<JSString*> string(cx, item.toString());
664
665 // Steps 3.b-e.
666 if (!ParseTemporalTimeString(cx, string, result)) {
667 return false;
668 }
669 MOZ_ASSERT(IsValidTime(*result));
670
671 // Steps 3.f-g.
672 TimeOptions ignoredOptions;
673 if (!ToTemporalTimeOptions(cx, options, &ignoredOptions)) {
674 return false;
675 }
676
677 // Step 4.
678 return true;
679 }
680
681 /**
682 * ToTemporalTime ( item [ , options ] )
683 */
684 bool js::temporal::ToTemporalTime(JSContext* cx, Handle<Value> item,
685 Time* result) {
686 return ToTemporalTime(cx, item, UndefinedHandleValue, result);
687 }
688
689 /**
690 * CompareTimeRecord ( time1, time2 )
691 */
692 int32_t js::temporal::CompareTimeRecord(const Time& one, const Time& two) {
693 // Steps 1-2.
694 if (int32_t diff = one.hour - two.hour) {
695 return diff < 0 ? -1 : 1;
696 }
697
698 // Steps 3-4.
699 if (int32_t diff = one.minute - two.minute) {
700 return diff < 0 ? -1 : 1;
701 }
702
703 // Steps 5-6.
704 if (int32_t diff = one.second - two.second) {
705 return diff < 0 ? -1 : 1;
706 }
707
708 // Steps 7-8.
709 if (int32_t diff = one.millisecond - two.millisecond) {
710 return diff < 0 ? -1 : 1;
711 }
712
713 // Steps 9-10.
714 if (int32_t diff = one.microsecond - two.microsecond) {
715 return diff < 0 ? -1 : 1;
716 }
717
718 // Steps 11-12.
719 if (int32_t diff = one.nanosecond - two.nanosecond) {
720 return diff < 0 ? -1 : 1;
721 }
722
723 // Step 13.
724 return 0;
725 }
726
727 static int64_t TimeToNanos(const Time& time) {
728 // No overflow possible because the input is a valid time.
729 MOZ_ASSERT(IsValidTime(time));
730
731 int64_t hour = time.hour;
732 int64_t minute = time.minute;
733 int64_t second = time.second;
734 int64_t millisecond = time.millisecond;
735 int64_t microsecond = time.microsecond;
736 int64_t nanosecond = time.nanosecond;
737
738 int64_t millis = ((hour * 60 + minute) * 60 + second) * 1000 + millisecond;
739 return (millis * 1000 + microsecond) * 1000 + nanosecond;
740 }
741
742 /**
743 * RoundTime ( time, increment, unit, roundingMode )
744 */
745 TimeRecord js::temporal::RoundTime(const Time& time, Increment increment,
746 TemporalUnit unit,
747 TemporalRoundingMode roundingMode) {
748 MOZ_ASSERT(IsValidTime(time));
749 MOZ_ASSERT(unit >= TemporalUnit::Day);
750 MOZ_ASSERT_IF(unit > TemporalUnit::Day,
751 increment <= MaximumTemporalDurationRoundingIncrement(unit));
752 MOZ_ASSERT_IF(unit == TemporalUnit::Day, increment == Increment{1});
753
754 int32_t days = 0;
755 auto [hour, minute, second, millisecond, microsecond, nanosecond] = time;
756
757 // Steps 1-6.
758 Time quantity;
759 int32_t* result;
760 switch (unit) {
761 case TemporalUnit::Day:
762 quantity = time;
763 result = &days;
764 break;
765 case TemporalUnit::Hour:
766 quantity = time;
767 result = &hour;
768 minute = 0;
769 second = 0;
770 millisecond = 0;
771 microsecond = 0;
772 nanosecond = 0;
773 break;
774 case TemporalUnit::Minute:
775 quantity = {0, minute, second, millisecond, microsecond, nanosecond};
776 result = &minute;
777 second = 0;
778 millisecond = 0;
779 microsecond = 0;
780 nanosecond = 0;
781 break;
782 case TemporalUnit::Second:
783 quantity = {0, 0, second, millisecond, microsecond, nanosecond};
784 result = &second;
785 millisecond = 0;
786 microsecond = 0;
787 nanosecond = 0;
788 break;
789 case TemporalUnit::Millisecond:
790 quantity = {0, 0, 0, millisecond, microsecond, nanosecond};
791 result = &millisecond;
792 microsecond = 0;
793 nanosecond = 0;
794 break;
795 case TemporalUnit::Microsecond:
796 quantity = {0, 0, 0, 0, microsecond, nanosecond};
797 result = &microsecond;
798 nanosecond = 0;
799 break;
800 case TemporalUnit::Nanosecond:
801 quantity = {0, 0, 0, 0, 0, nanosecond};
802 result = &nanosecond;
803 break;
804
805 case TemporalUnit::Auto:
806 case TemporalUnit::Year:
807 case TemporalUnit::Month:
808 case TemporalUnit::Week:
809 MOZ_CRASH("unexpected temporal unit");
810 }
811
812 int64_t quantityNs = TimeToNanos(quantity);
813 MOZ_ASSERT(0 <= quantityNs && quantityNs < ToNanoseconds(TemporalUnit::Day));
814
815 // Step 7.
816 int64_t unitLength = ToNanoseconds(unit);
817 int64_t incrementNs = increment.value() * unitLength;
818 MOZ_ASSERT(incrementNs <= ToNanoseconds(TemporalUnit::Day),
819 "incrementNs doesn't overflow time resolution");
820
821 // Step 8.
822 int64_t r = RoundNumberToIncrement(quantityNs, incrementNs, roundingMode) /
823 unitLength;
824 MOZ_ASSERT(r == int64_t(int32_t(r)),
825 "can't overflow when inputs are all in range");
826
827 *result = int32_t(r);
828
829 // Step 9.
830 if (unit == TemporalUnit::Day) {
831 return {int64_t(days), {0, 0, 0, 0, 0, 0}};
832 }
833
834 // Steps 10-16.
835 return ::BalanceTime(hour, minute, second, millisecond, microsecond,
836 nanosecond);
837 }
838
839 /**
840 * AddTime ( time, timeDuration )
841 */
842 TimeRecord js::temporal::AddTime(const Time& time,
843 const TimeDuration& duration) {
844 MOZ_ASSERT(IsValidTime(time));
845 MOZ_ASSERT(IsValidTimeDuration(duration));
846
847 auto [seconds, nanoseconds] = duration.denormalize();
848 MOZ_ASSERT(std::abs(nanoseconds) <= 999'999'999);
849
850 // Steps 1-2.
851 return ::BalanceTime<int64_t>(time.hour, time.minute, time.second + seconds,
852 time.millisecond, time.microsecond,
853 time.nanosecond + nanoseconds);
854 }
855
856 /**
857 * DifferenceTemporalPlainTime ( operation, temporalTime, other, options )
858 */
859 static bool DifferenceTemporalPlainTime(JSContext* cx,
860 TemporalDifference operation,
861 const CallArgs& args) {
862 auto temporalTime = args.thisv().toObject().as<PlainTimeObject>().time();
863
864 // Step 1.
865 Time other;
866 if (!ToTemporalTime(cx, args.get(0), &other)) {
867 return false;
868 }
869
870 // Steps 2-3.
871 DifferenceSettings settings;
872 if (args.hasDefined(1)) {
873 // Step 2.
874 Rooted<JSObject*> options(
875 cx, RequireObjectArg(cx, "options", ToName(operation), args[1]));
876 if (!options) {
877 return false;
878 }
879
880 // Step 3.
881 if (!GetDifferenceSettings(cx, operation, options, TemporalUnitGroup::Time,
882 TemporalUnit::Nanosecond, TemporalUnit::Hour,
883 &settings)) {
884 return false;
885 }
886 } else {
887 // Steps 2-3.
888 settings = {
889 TemporalUnit::Nanosecond,
890 TemporalUnit::Hour,
891 TemporalRoundingMode::Trunc,
892 Increment{1},
893 };
894 }
895
896 // Step 4.
897 auto timeDuration = DifferenceTime(temporalTime, other);
898
899 // Step 5.
900 timeDuration =
901 RoundTimeDuration(timeDuration, settings.roundingIncrement,
902 settings.smallestUnit, settings.roundingMode);
903
904 // Steps 6-7.
905 Duration duration;
906 if (!TemporalDurationFromInternal(cx, timeDuration, settings.largestUnit,
907 &duration)) {
908 return false;
909 }
910
911 // Step 8.
912 if (operation == TemporalDifference::Since) {
913 duration = duration.negate();
914 }
915
916 // Step 9.
917 auto* result = CreateTemporalDuration(cx, duration);
918 if (!result) {
919 return false;
920 }
921
922 args.rval().setObject(*result);
923 return true;
924 }
925
926 /**
927 * AddDurationToTime ( operation, temporalTime, temporalDurationLike )
928 */
929 static bool AddDurationToTime(JSContext* cx, TemporalAddDuration operation,
930 const CallArgs& args) {
931 auto* temporalTime = &args.thisv().toObject().as<PlainTimeObject>();
932 auto time = temporalTime->time();
933
934 // Step 1.
935 Duration duration;
936 if (!ToTemporalDuration(cx, args.get(0), &duration)) {
937 return false;
938 }
939
940 // Step 2.
941 if (operation == TemporalAddDuration::Subtract) {
942 duration = duration.negate();
943 }
944
945 // Step 3. (Inlined ToInternalDurationRecord)
946 auto timeDuration = TimeDurationFromComponents(duration);
947
948 // Step 4.
949 auto result = AddTime(time, timeDuration);
950 MOZ_ASSERT(IsValidTime(result.time));
951
952 // Step 5.
953 auto* obj = CreateTemporalTime(cx, result.time);
954 if (!obj) {
955 return false;
956 }
957
958 args.rval().setObject(*obj);
959 return true;
960 }
961
962 /**
963 * Temporal.PlainTime ( [ hour [ , minute [ , second [ , millisecond [ ,
964 * microsecond [ , nanosecond ] ] ] ] ] ] )
965 */
966 static bool PlainTimeConstructor(JSContext* cx, unsigned argc, Value* vp) {
967 CallArgs args = CallArgsFromVp(argc, vp);
968
969 // Step 1.
970 if (!ThrowIfNotConstructing(cx, args, "Temporal.PlainTime")) {
971 return false;
972 }
973
974 // Step 2.
975 double hour = 0;
976 if (args.hasDefined(0)) {
977 if (!ToIntegerWithTruncation(cx, args[0], "hour", &hour)) {
978 return false;
979 }
980 }
981
982 // Step 3.
983 double minute = 0;
984 if (args.hasDefined(1)) {
985 if (!ToIntegerWithTruncation(cx, args[1], "minute", &minute)) {
986 return false;
987 }
988 }
989
990 // Step 4.
991 double second = 0;
992 if (args.hasDefined(2)) {
993 if (!ToIntegerWithTruncation(cx, args[2], "second", &second)) {
994 return false;
995 }
996 }
997
998 // Step 5.
999 double millisecond = 0;
1000 if (args.hasDefined(3)) {
1001 if (!ToIntegerWithTruncation(cx, args[3], "millisecond", &millisecond)) {
1002 return false;
1003 }
1004 }
1005
1006 // Step 6.
1007 double microsecond = 0;
1008 if (args.hasDefined(4)) {
1009 if (!ToIntegerWithTruncation(cx, args[4], "microsecond", &microsecond)) {
1010 return false;
1011 }
1012 }
1013
1014 // Step 7.
1015 double nanosecond = 0;
1016 if (args.hasDefined(5)) {
1017 if (!ToIntegerWithTruncation(cx, args[5], "nanosecond", &nanosecond)) {
1018 return false;
1019 }
1020 }
1021
1022 // Steps 8-9.
1023 Time time;
1024 if (!RegulateTime(cx,
1025 {
1026 hour,
1027 minute,
1028 second,
1029 millisecond,
1030 microsecond,
1031 nanosecond,
1032 },
1033 TemporalOverflow::Reject, &time)) {
1034 return false;
1035 }
1036
1037 // Step 10.
1038 auto* temporalTime = CreateTemporalTime(cx, args, time);
1039 if (!temporalTime) {
1040 return false;
1041 }
1042
1043 args.rval().setObject(*temporalTime);
1044 return true;
1045 }
1046
1047 /**
1048 * Temporal.PlainTime.from ( item [ , options ] )
1049 */
1050 static bool PlainTime_from(JSContext* cx, unsigned argc, Value* vp) {
1051 CallArgs args = CallArgsFromVp(argc, vp);
1052
1053 // Step 1.
1054 Time result;
1055 if (!ToTemporalTime(cx, args.get(0), args.get(1), &result)) {
1056 return false;
1057 }
1058 MOZ_ASSERT(IsValidTime(result));
1059
1060 auto* obj = temporal::CreateTemporalTime(cx, result);
1061 if (!obj) {
1062 return false;
1063 }
1064
1065 args.rval().setObject(*obj);
1066 return true;
1067 }
1068
1069 /**
1070 * Temporal.PlainTime.compare ( one, two )
1071 */
1072 static bool PlainTime_compare(JSContext* cx, unsigned argc, Value* vp) {
1073 CallArgs args = CallArgsFromVp(argc, vp);
1074
1075 // Step 1.
1076 Time one;
1077 if (!ToTemporalTime(cx, args.get(0), &one)) {
1078 return false;
1079 }
1080
1081 // Step 2.
1082 Time two;
1083 if (!ToTemporalTime(cx, args.get(1), &two)) {
1084 return false;
1085 }
1086
1087 // Step 3.
1088 args.rval().setInt32(CompareTimeRecord(one, two));
1089 return true;
1090 }
1091
1092 /**
1093 * get Temporal.PlainTime.prototype.hour
1094 */
1095 static bool PlainTime_hour(JSContext* cx, const CallArgs& args) {
1096 // Step 3.
1097 auto* temporalTime = &args.thisv().toObject().as<PlainTimeObject>();
1098 args.rval().setInt32(temporalTime->time().hour);
1099 return true;
1100 }
1101
1102 /**
1103 * get Temporal.PlainTime.prototype.hour
1104 */
1105 static bool PlainTime_hour(JSContext* cx, unsigned argc, Value* vp) {
1106 // Steps 1-2.
1107 CallArgs args = CallArgsFromVp(argc, vp);
1108 return CallNonGenericMethod<IsPlainTime, PlainTime_hour>(cx, args);
1109 }
1110
1111 /**
1112 * get Temporal.PlainTime.prototype.minute
1113 */
1114 static bool PlainTime_minute(JSContext* cx, const CallArgs& args) {
1115 // Step 3.
1116 auto* temporalTime = &args.thisv().toObject().as<PlainTimeObject>();
1117 args.rval().setInt32(temporalTime->time().minute);
1118 return true;
1119 }
1120
1121 /**
1122 * get Temporal.PlainTime.prototype.minute
1123 */
1124 static bool PlainTime_minute(JSContext* cx, unsigned argc, Value* vp) {
1125 // Steps 1-2.
1126 CallArgs args = CallArgsFromVp(argc, vp);
1127 return CallNonGenericMethod<IsPlainTime, PlainTime_minute>(cx, args);
1128 }
1129
1130 /**
1131 * get Temporal.PlainTime.prototype.second
1132 */
1133 static bool PlainTime_second(JSContext* cx, const CallArgs& args) {
1134 // Step 3.
1135 auto* temporalTime = &args.thisv().toObject().as<PlainTimeObject>();
1136 args.rval().setInt32(temporalTime->time().second);
1137 return true;
1138 }
1139
1140 /**
1141 * get Temporal.PlainTime.prototype.second
1142 */
1143 static bool PlainTime_second(JSContext* cx, unsigned argc, Value* vp) {
1144 // Steps 1-2.
1145 CallArgs args = CallArgsFromVp(argc, vp);
1146 return CallNonGenericMethod<IsPlainTime, PlainTime_second>(cx, args);
1147 }
1148
1149 /**
1150 * get Temporal.PlainTime.prototype.millisecond
1151 */
1152 static bool PlainTime_millisecond(JSContext* cx, const CallArgs& args) {
1153 // Step 3.
1154 auto* temporalTime = &args.thisv().toObject().as<PlainTimeObject>();
1155 args.rval().setInt32(temporalTime->time().millisecond);
1156 return true;
1157 }
1158
1159 /**
1160 * get Temporal.PlainTime.prototype.millisecond
1161 */
1162 static bool PlainTime_millisecond(JSContext* cx, unsigned argc, Value* vp) {
1163 // Steps 1-2.
1164 CallArgs args = CallArgsFromVp(argc, vp);
1165 return CallNonGenericMethod<IsPlainTime, PlainTime_millisecond>(cx, args);
1166 }
1167
1168 /**
1169 * get Temporal.PlainTime.prototype.microsecond
1170 */
1171 static bool PlainTime_microsecond(JSContext* cx, const CallArgs& args) {
1172 // Step 3.
1173 auto* temporalTime = &args.thisv().toObject().as<PlainTimeObject>();
1174 args.rval().setInt32(temporalTime->time().microsecond);
1175 return true;
1176 }
1177
1178 /**
1179 * get Temporal.PlainTime.prototype.microsecond
1180 */
1181 static bool PlainTime_microsecond(JSContext* cx, unsigned argc, Value* vp) {
1182 // Steps 1-2.
1183 CallArgs args = CallArgsFromVp(argc, vp);
1184 return CallNonGenericMethod<IsPlainTime, PlainTime_microsecond>(cx, args);
1185 }
1186
1187 /**
1188 * get Temporal.PlainTime.prototype.nanosecond
1189 */
1190 static bool PlainTime_nanosecond(JSContext* cx, const CallArgs& args) {
1191 // Step 3.
1192 auto* temporalTime = &args.thisv().toObject().as<PlainTimeObject>();
1193 args.rval().setInt32(temporalTime->time().nanosecond);
1194 return true;
1195 }
1196
1197 /**
1198 * get Temporal.PlainTime.prototype.nanosecond
1199 */
1200 static bool PlainTime_nanosecond(JSContext* cx, unsigned argc, Value* vp) {
1201 // Steps 1-2.
1202 CallArgs args = CallArgsFromVp(argc, vp);
1203 return CallNonGenericMethod<IsPlainTime, PlainTime_nanosecond>(cx, args);
1204 }
1205
1206 /**
1207 * Temporal.PlainTime.prototype.add ( temporalDurationLike )
1208 */
1209 static bool PlainTime_add(JSContext* cx, const CallArgs& args) {
1210 // Step 3.
1211 return AddDurationToTime(cx, TemporalAddDuration::Add, args);
1212 }
1213
1214 /**
1215 * Temporal.PlainTime.prototype.add ( temporalDurationLike )
1216 */
1217 static bool PlainTime_add(JSContext* cx, unsigned argc, Value* vp) {
1218 // Steps 1-2.
1219 CallArgs args = CallArgsFromVp(argc, vp);
1220 return CallNonGenericMethod<IsPlainTime, PlainTime_add>(cx, args);
1221 }
1222
1223 /**
1224 * Temporal.PlainTime.prototype.subtract ( temporalDurationLike )
1225 */
1226 static bool PlainTime_subtract(JSContext* cx, const CallArgs& args) {
1227 // Step 3.
1228 return AddDurationToTime(cx, TemporalAddDuration::Subtract, args);
1229 }
1230
1231 /**
1232 * Temporal.PlainTime.prototype.subtract ( temporalDurationLike )
1233 */
1234 static bool PlainTime_subtract(JSContext* cx, unsigned argc, Value* vp) {
1235 // Steps 1-2.
1236 CallArgs args = CallArgsFromVp(argc, vp);
1237 return CallNonGenericMethod<IsPlainTime, PlainTime_subtract>(cx, args);
1238 }
1239
1240 /**
1241 * Temporal.PlainTime.prototype.with ( temporalTimeLike [ , options ] )
1242 */
1243 static bool PlainTime_with(JSContext* cx, const CallArgs& args) {
1244 auto* temporalTime = &args.thisv().toObject().as<PlainTimeObject>();
1245 auto time = temporalTime->time();
1246
1247 // Step 3.
1248 Rooted<JSObject*> temporalTimeLike(
1249 cx, RequireObjectArg(cx, "temporalTimeLike", "with", args.get(0)));
1250 if (!temporalTimeLike) {
1251 return false;
1252 }
1253 if (!ThrowIfTemporalLikeObject(cx, temporalTimeLike)) {
1254 return false;
1255 }
1256
1257 // Steps 4-16.
1258 TemporalTimeLike partialTime = {
1259 double(time.hour), double(time.minute),
1260 double(time.second), double(time.millisecond),
1261 double(time.microsecond), double(time.nanosecond),
1262 };
1263 if (!::ToTemporalTimeRecord(cx, temporalTimeLike, &partialTime)) {
1264 return false;
1265 }
1266
1267 // Steps 17-18
1268 auto overflow = TemporalOverflow::Constrain;
1269 if (args.hasDefined(1)) {
1270 // Step 17.
1271 Rooted<JSObject*> options(cx,
1272 RequireObjectArg(cx, "options", "with", args[1]));
1273 if (!options) {
1274 return false;
1275 }
1276
1277 // Step 18.
1278 if (!GetTemporalOverflowOption(cx, options, &overflow)) {
1279 return false;
1280 }
1281 }
1282
1283 // Step 19.
1284 Time result;
1285 if (!RegulateTime(cx, partialTime, overflow, &result)) {
1286 return false;
1287 }
1288
1289 // Step 20.
1290 auto* obj = CreateTemporalTime(cx, result);
1291 if (!obj) {
1292 return false;
1293 }
1294
1295 args.rval().setObject(*obj);
1296 return true;
1297 }
1298
1299 /**
1300 * Temporal.PlainTime.prototype.with ( temporalTimeLike [ , options ] )
1301 */
1302 static bool PlainTime_with(JSContext* cx, unsigned argc, Value* vp) {
1303 // Steps 1-2.
1304 CallArgs args = CallArgsFromVp(argc, vp);
1305 return CallNonGenericMethod<IsPlainTime, PlainTime_with>(cx, args);
1306 }
1307
1308 /**
1309 * Temporal.PlainTime.prototype.until ( other [ , options ] )
1310 */
1311 static bool PlainTime_until(JSContext* cx, const CallArgs& args) {
1312 // Step 3.
1313 return DifferenceTemporalPlainTime(cx, TemporalDifference::Until, args);
1314 }
1315
1316 /**
1317 * Temporal.PlainTime.prototype.until ( other [ , options ] )
1318 */
1319 static bool PlainTime_until(JSContext* cx, unsigned argc, Value* vp) {
1320 // Steps 1-2.
1321 CallArgs args = CallArgsFromVp(argc, vp);
1322 return CallNonGenericMethod<IsPlainTime, PlainTime_until>(cx, args);
1323 }
1324
1325 /**
1326 * Temporal.PlainTime.prototype.since ( other [ , options ] )
1327 */
1328 static bool PlainTime_since(JSContext* cx, const CallArgs& args) {
1329 // Step 3.
1330 return DifferenceTemporalPlainTime(cx, TemporalDifference::Since, args);
1331 }
1332
1333 /**
1334 * Temporal.PlainTime.prototype.since ( other [ , options ] )
1335 */
1336 static bool PlainTime_since(JSContext* cx, unsigned argc, Value* vp) {
1337 // Steps 1-2.
1338 CallArgs args = CallArgsFromVp(argc, vp);
1339 return CallNonGenericMethod<IsPlainTime, PlainTime_since>(cx, args);
1340 }
1341
1342 /**
1343 * Temporal.PlainTime.prototype.round ( roundTo )
1344 */
1345 static bool PlainTime_round(JSContext* cx, const CallArgs& args) {
1346 auto* temporalTime = &args.thisv().toObject().as<PlainTimeObject>();
1347 auto time = temporalTime->time();
1348
1349 // Steps 3-12.
1350 auto smallestUnit = TemporalUnit::Auto;
1351 auto roundingMode = TemporalRoundingMode::HalfExpand;
1352 auto roundingIncrement = Increment{1};
1353 if (args.get(0).isString()) {
1354 // Step 4. (Not applicable in our implementation.)
1355
1356 // Step 9.
1357 Rooted<JSString*> paramString(cx, args[0].toString());
1358 if (!GetTemporalUnitValuedOption(cx, paramString,
1359 TemporalUnitKey::SmallestUnit,
1360 TemporalUnitGroup::Time, &smallestUnit)) {
1361 return false;
1362 }
1363
1364 // Steps 6-8 and 10-12. (Implicit)
1365 } else {
1366 // Steps 3 and 5.
1367 Rooted<JSObject*> options(
1368 cx, RequireObjectArg(cx, "roundTo", "round", args.get(0)));
1369 if (!options) {
1370 return false;
1371 }
1372
1373 // Steps 6-7.
1374 if (!GetRoundingIncrementOption(cx, options, &roundingIncrement)) {
1375 return false;
1376 }
1377
1378 // Step 8.
1379 if (!GetRoundingModeOption(cx, options, &roundingMode)) {
1380 return false;
1381 }
1382
1383 // Step 9.
1384 if (!GetTemporalUnitValuedOption(cx, options, TemporalUnitKey::SmallestUnit,
1385 TemporalUnitGroup::Time, &smallestUnit)) {
1386 return false;
1387 }
1388
1389 if (smallestUnit == TemporalUnit::Auto) {
1390 JS_ReportErrorNumberASCII(cx, GetErrorMessage, nullptr,
1391 JSMSG_TEMPORAL_MISSING_OPTION, "smallestUnit");
1392 return false;
1393 }
1394
1395 // Steps 10-11.
1396 auto maximum = MaximumTemporalDurationRoundingIncrement(smallestUnit);
1397
1398 // Step 12.
1399 if (!ValidateTemporalRoundingIncrement(cx, roundingIncrement, maximum,
1400 false)) {
1401 return false;
1402 }
1403 }
1404
1405 // Step 13.
1406 auto result = RoundTime(time, roundingIncrement, smallestUnit, roundingMode);
1407
1408 // Step 14.
1409 auto* obj = CreateTemporalTime(cx, result.time);
1410 if (!obj) {
1411 return false;
1412 }
1413
1414 args.rval().setObject(*obj);
1415 return true;
1416 }
1417
1418 /**
1419 * Temporal.PlainTime.prototype.round ( roundTo )
1420 */
1421 static bool PlainTime_round(JSContext* cx, unsigned argc, Value* vp) {
1422 // Steps 1-2.
1423 CallArgs args = CallArgsFromVp(argc, vp);
1424 return CallNonGenericMethod<IsPlainTime, PlainTime_round>(cx, args);
1425 }
1426
1427 /**
1428 * Temporal.PlainTime.prototype.equals ( other )
1429 */
1430 static bool PlainTime_equals(JSContext* cx, const CallArgs& args) {
1431 auto temporalTime = args.thisv().toObject().as<PlainTimeObject>().time();
1432
1433 // Step 3.
1434 Time other;
1435 if (!ToTemporalTime(cx, args.get(0), &other)) {
1436 return false;
1437 }
1438
1439 // Steps 4-5.
1440 args.rval().setBoolean(temporalTime == other);
1441 return true;
1442 }
1443
1444 /**
1445 * Temporal.PlainTime.prototype.equals ( other )
1446 */
1447 static bool PlainTime_equals(JSContext* cx, unsigned argc, Value* vp) {
1448 // Steps 1-2.
1449 CallArgs args = CallArgsFromVp(argc, vp);
1450 return CallNonGenericMethod<IsPlainTime, PlainTime_equals>(cx, args);
1451 }
1452
1453 /**
1454 * Temporal.PlainTime.prototype.toString ( [ options ] )
1455 */
1456 static bool PlainTime_toString(JSContext* cx, const CallArgs& args) {
1457 auto* temporalTime = &args.thisv().toObject().as<PlainTimeObject>();
1458 auto time = temporalTime->time();
1459
1460 SecondsStringPrecision precision = {Precision::Auto(),
1461 TemporalUnit::Nanosecond, Increment{1}};
1462 auto roundingMode = TemporalRoundingMode::Trunc;
1463 if (args.hasDefined(0)) {
1464 // Step 3.
1465 Rooted<JSObject*> options(
1466 cx, RequireObjectArg(cx, "options", "toString", args[0]));
1467 if (!options) {
1468 return false;
1469 }
1470
1471 // Steps 4-5.
1472 auto digits = Precision::Auto();
1473 if (!GetTemporalFractionalSecondDigitsOption(cx, options, &digits)) {
1474 return false;
1475 }
1476
1477 // Step 6.
1478 if (!GetRoundingModeOption(cx, options, &roundingMode)) {
1479 return false;
1480 }
1481
1482 // Step 7.
1483 auto smallestUnit = TemporalUnit::Auto;
1484 if (!GetTemporalUnitValuedOption(cx, options, TemporalUnitKey::SmallestUnit,
1485 TemporalUnitGroup::Time, &smallestUnit)) {
1486 return false;
1487 }
1488
1489 // Step 8.
1490 if (smallestUnit == TemporalUnit::Hour) {
1491 JS_ReportErrorNumberASCII(cx, GetErrorMessage, nullptr,
1492 JSMSG_TEMPORAL_INVALID_UNIT_OPTION, "hour",
1493 "smallestUnit");
1494 return false;
1495 }
1496
1497 // Step 9.
1498 precision = ToSecondsStringPrecision(smallestUnit, digits);
1499 }
1500
1501 // Step 10.
1502 auto roundedTime =
1503 RoundTime(time, precision.increment, precision.unit, roundingMode);
1504
1505 // Step 11.
1506 JSString* str = TimeRecordToString(cx, roundedTime.time, precision.precision);
1507 if (!str) {
1508 return false;
1509 }
1510
1511 args.rval().setString(str);
1512 return true;
1513 }
1514
1515 /**
1516 * Temporal.PlainTime.prototype.toString ( [ options ] )
1517 */
1518 static bool PlainTime_toString(JSContext* cx, unsigned argc, Value* vp) {
1519 // Steps 1-2.
1520 CallArgs args = CallArgsFromVp(argc, vp);
1521 return CallNonGenericMethod<IsPlainTime, PlainTime_toString>(cx, args);
1522 }
1523
1524 /**
1525 * Temporal.PlainTime.prototype.toLocaleString ( [ locales [ , options ] ] )
1526 */
1527 static bool PlainTime_toLocaleString(JSContext* cx, const CallArgs& args) {
1528 // Steps 3-4.
1529 Handle<PropertyName*> required = cx->names().time;
1530 Handle<PropertyName*> defaults = cx->names().time;
1531 return TemporalObjectToLocaleString(cx, args, required, defaults);
1532 }
1533
1534 /**
1535 * Temporal.PlainTime.prototype.toLocaleString ( [ locales [ , options ] ] )
1536 */
1537 static bool PlainTime_toLocaleString(JSContext* cx, unsigned argc, Value* vp) {
1538 // Steps 1-2.
1539 CallArgs args = CallArgsFromVp(argc, vp);
1540 return CallNonGenericMethod<IsPlainTime, PlainTime_toLocaleString>(cx, args);
1541 }
1542
1543 /**
1544 * Temporal.PlainTime.prototype.toJSON ( )
1545 */
1546 static bool PlainTime_toJSON(JSContext* cx, const CallArgs& args) {
1547 auto* temporalTime = &args.thisv().toObject().as<PlainTimeObject>();
1548 auto time = temporalTime->time();
1549
1550 // Step 3.
1551 JSString* str = TimeRecordToString(cx, time, Precision::Auto());
1552 if (!str) {
1553 return false;
1554 }
1555
1556 args.rval().setString(str);
1557 return true;
1558 }
1559
1560 /**
1561 * Temporal.PlainTime.prototype.toJSON ( )
1562 */
1563 static bool PlainTime_toJSON(JSContext* cx, unsigned argc, Value* vp) {
1564 // Steps 1-2.
1565 CallArgs args = CallArgsFromVp(argc, vp);
1566 return CallNonGenericMethod<IsPlainTime, PlainTime_toJSON>(cx, args);
1567 }
1568
1569 /**
1570 * Temporal.PlainTime.prototype.valueOf ( )
1571 */
1572 static bool PlainTime_valueOf(JSContext* cx, unsigned argc, Value* vp) {
1573 JS_ReportErrorNumberASCII(cx, GetErrorMessage, nullptr, JSMSG_CANT_CONVERT_TO,
1574 "PlainTime", "primitive type");
1575 return false;
1576 }
1577
1578 const JSClass PlainTimeObject::class_ = {
1579 "Temporal.PlainTime",
1580 JSCLASS_HAS_RESERVED_SLOTS(PlainTimeObject::SLOT_COUNT) |
1581 JSCLASS_HAS_CACHED_PROTO(JSProto_PlainTime),
1582 JS_NULL_CLASS_OPS,
1583 &PlainTimeObject::classSpec_,
1584 };
1585
1586 const JSClass& PlainTimeObject::protoClass_ = PlainObject::class_;
1587
1588 static const JSFunctionSpec PlainTime_methods[] = {
1589 JS_FN("from", PlainTime_from, 1, 0),
1590 JS_FN("compare", PlainTime_compare, 2, 0),
1591 JS_FS_END,
1592 };
1593
1594 static const JSFunctionSpec PlainTime_prototype_methods[] = {
1595 JS_FN("add", PlainTime_add, 1, 0),
1596 JS_FN("subtract", PlainTime_subtract, 1, 0),
1597 JS_FN("with", PlainTime_with, 1, 0),
1598 JS_FN("until", PlainTime_until, 1, 0),
1599 JS_FN("since", PlainTime_since, 1, 0),
1600 JS_FN("round", PlainTime_round, 1, 0),
1601 JS_FN("equals", PlainTime_equals, 1, 0),
1602 JS_FN("toString", PlainTime_toString, 0, 0),
1603 JS_FN("toLocaleString", PlainTime_toLocaleString, 0, 0),
1604 JS_FN("toJSON", PlainTime_toJSON, 0, 0),
1605 JS_FN("valueOf", PlainTime_valueOf, 0, 0),
1606 JS_FS_END,
1607 };
1608
1609 static const JSPropertySpec PlainTime_prototype_properties[] = {
1610 JS_PSG("hour", PlainTime_hour, 0),
1611 JS_PSG("minute", PlainTime_minute, 0),
1612 JS_PSG("second", PlainTime_second, 0),
1613 JS_PSG("millisecond", PlainTime_millisecond, 0),
1614 JS_PSG("microsecond", PlainTime_microsecond, 0),
1615 JS_PSG("nanosecond", PlainTime_nanosecond, 0),
1616 JS_STRING_SYM_PS(toStringTag, "Temporal.PlainTime", JSPROP_READONLY),
1617 JS_PS_END,
1618 };
1619
1620 const ClassSpec PlainTimeObject::classSpec_ = {
1621 GenericCreateConstructor<PlainTimeConstructor, 0, gc::AllocKind::FUNCTION>,
1622 GenericCreatePrototype<PlainTimeObject>,
1623 PlainTime_methods,
1624 nullptr,
1625 PlainTime_prototype_methods,
1626 PlainTime_prototype_properties,
1627 nullptr,
1628 ClassSpec::DontDefineConstructor,
1629 };