Debian Codegraph @

js/src/builtin/temporal/PlainDate.h

raw
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*-
2 * vim: set ts=8 sts=2 et sw=2 tw=80:
3 * This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6
7 #ifndef builtin_temporal_PlainDate_h
8 #define builtin_temporal_PlainDate_h
9
10 #include "mozilla/Assertions.h"
11 #include "mozilla/Attributes.h"
12
13 #include <stdint.h>
14
15 #include "builtin/temporal/Calendar.h"
16 #include "builtin/temporal/TemporalTypes.h"
17 #include "js/RootingAPI.h"
18 #include "js/TypeDecls.h"
19 #include "js/Value.h"
20 #include "vm/NativeObject.h"
21
22 class JS_PUBLIC_API JSTracer;
23
24 namespace js {
25 struct ClassSpec;
26 } // namespace js
27
28 namespace js::temporal {
29
30 class PlainDateObject : public NativeObject {
31 public:
32 static const JSClass class_;
33 static const JSClass& protoClass_;
34
35 static constexpr uint32_t PACKED_DATE_SLOT = 0;
36 static constexpr uint32_t CALENDAR_SLOT = 1;
37 static constexpr uint32_t SLOT_COUNT = 2;
38
39 /**
40 * Extract the date fields from this PlainDate object.
41 */
42 ISODate date() const {
43 auto packed = PackedDate{getFixedSlot(PACKED_DATE_SLOT).toPrivateUint32()};
44 return PackedDate::unpack(packed);
45 }
46
47 CalendarValue calendar() const {
48 return CalendarValue(getFixedSlot(CALENDAR_SLOT));
49 }
50
51 private:
52 static const ClassSpec classSpec_;
53 };
54
55 #ifdef DEBUG
56 /**
57 * IsValidISODate ( year, month, day )
58 */
59 bool IsValidISODate(const ISODate& date);
60 #endif
61
62 /**
63 * IsValidISODate ( year, month, day )
64 */
65 bool ThrowIfInvalidISODate(JSContext* cx, const ISODate& date);
66
67 /**
68 * IsValidISODate ( year, month, day )
69 */
70 bool ThrowIfInvalidISODate(JSContext* cx, double year, double month,
71 double day);
72
73 /**
74 * ISODateWithinLimits ( isoDate )
75 */
76 bool ISODateWithinLimits(const ISODate& isoDate);
77
78 class MOZ_STACK_CLASS PlainDate final {
79 ISODate date_;
80 CalendarValue calendar_;
81
82 public:
83 PlainDate() = default;
84
85 PlainDate(const ISODate& date, const CalendarValue& calendar)
86 : date_(date), calendar_(calendar) {
87 MOZ_ASSERT(ISODateWithinLimits(date));
88 }
89
90 explicit PlainDate(const PlainDateObject* date)
91 : PlainDate(date->date(), date->calendar()) {}
92
93 const auto& date() const { return date_; }
94 const auto& calendar() const { return calendar_; }
95
96 // Allow implicit conversion to an ISODate.
97 operator const ISODate&() const { return date(); }
98
99 explicit operator bool() const { return !!calendar_; }
100
101 void trace(JSTracer* trc) { calendar_.trace(trc); }
102
103 const auto* calendarDoNotUse() const { return &calendar_; }
104 };
105
106 /**
107 * CreateTemporalDate ( isoDate, calendar [ , newTarget ] )
108 */
109 PlainDateObject* CreateTemporalDate(JSContext* cx, const ISODate& isoDate,
110 JS::Handle<CalendarValue> calendar);
111
112 /**
113 * CreateTemporalDate ( isoDate, calendar [ , newTarget ] )
114 */
115 PlainDateObject* CreateTemporalDate(JSContext* cx, JS::Handle<PlainDate> date);
116
117 /**
118 * CreateTemporalDate ( isoDate, calendar [ , newTarget ] )
119 */
120 bool CreateTemporalDate(JSContext* cx, const ISODate& isoDate,
121 JS::Handle<CalendarValue> calendar,
122 JS::MutableHandle<PlainDate> result);
123
124 /**
125 * CompareISODate ( y1, m1, d1, y2, m2, d2 )
126 */
127 int32_t CompareISODate(const ISODate& one, const ISODate& two);
128
129 /**
130 * BalanceISODate ( year, month, day )
131 */
132 bool BalanceISODate(JSContext* cx, const ISODate& date, int64_t days,
133 ISODate* result);
134
135 /**
136 * BalanceISODate ( year, month, day )
137 */
138 ISODate BalanceISODate(const ISODate& date, int32_t days);
139
140 } /* namespace js::temporal */
141
142 namespace js {
143
144 template <typename Wrapper>
145 class WrappedPtrOperations<temporal::PlainDate, Wrapper> {
146 const auto& container() const {
147 return static_cast<const Wrapper*>(this)->get();
148 }
149
150 public:
151 explicit operator bool() const { return bool(container()); }
152
153 const auto& date() const { return container().date(); }
154
155 JS::Handle<temporal::CalendarValue> calendar() const {
156 return JS::Handle<temporal::CalendarValue>::fromMarkedLocation(
157 container().calendarDoNotUse());
158 }
159
160 // Allow implicit conversion to an ISODate.
161 operator const temporal::ISODate&() const { return date(); }
162 };
163
164 } // namespace js
165
166 #endif /* builtin_temporal_PlainDate_h */