Debian Codegraph @

js/src/builtin/temporal/TemporalParser.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_TemporalParser_h
8 #define builtin_temporal_TemporalParser_h
9
10 #include "mozilla/Assertions.h"
11 #include "mozilla/Attributes.h"
12
13 #include <cstdlib>
14 #include <stdint.h>
15
16 #include "builtin/temporal/TemporalTypes.h"
17 #include "builtin/temporal/TemporalUnit.h"
18 #include "js/RootingAPI.h"
19 #include "js/TypeDecls.h"
20
21 class JSLinearString;
22 class JS_PUBLIC_API JSTracer;
23
24 namespace js::temporal {
25
26 struct MOZ_STACK_CLASS ParsedTimeZone final {
27 JSLinearString* name = nullptr;
28 int32_t offset = INT32_MIN;
29
30 void trace(JSTracer* trc);
31
32 static ParsedTimeZone fromName(JSLinearString* name) {
33 MOZ_ASSERT(name);
34 return {name, 0};
35 }
36
37 static ParsedTimeZone fromOffset(int32_t offset) {
38 MOZ_ASSERT(std::abs(offset) < UnitsPerDay(TemporalUnit::Minute));
39 return {nullptr, offset};
40 }
41
42 explicit operator bool() const {
43 return name != nullptr || offset != INT32_MIN;
44 }
45 };
46
47 struct MOZ_STACK_CLASS ParsedZonedDateTime final {
48 ISODateTime dateTime = {};
49 JSLinearString* calendar = nullptr;
50 ParsedTimeZone timeZoneAnnotation{};
51 int64_t timeZoneOffset = 0;
52 bool isUTC = false;
53 bool hasOffset = false;
54 bool isStartOfDay = false;
55
56 void trace(JSTracer* trc);
57 };
58
59 /**
60 * ParseTemporalInstantString ( isoString )
61 */
62 bool ParseTemporalInstantString(JSContext* cx, JS::Handle<JSString*> str,
63 ISODateTime* result, int64_t* offset);
64
65 /**
66 * ParseTemporalTimeZoneString ( timeZoneString )
67 */
68 bool ParseTemporalTimeZoneString(JSContext* cx, JS::Handle<JSString*> str,
69 JS::MutableHandle<ParsedTimeZone> result);
70
71 /**
72 * ParseTimeZoneIdentifier ( identifier )
73 */
74 bool ParseTimeZoneIdentifier(JSContext* cx, JS::Handle<JSString*> str,
75 JS::MutableHandle<ParsedTimeZone> result);
76
77 /**
78 * ParseDateTimeUTCOffset ( offsetString )
79 */
80 bool ParseDateTimeUTCOffset(JSContext* cx, JS::Handle<JSString*> str,
81 int64_t* result);
82
83 /**
84 * ParseTemporalDurationString ( isoString )
85 */
86 bool ParseTemporalDurationString(JSContext* cx, JS::Handle<JSString*> str,
87 Duration* result);
88
89 /**
90 * ParseTemporalCalendarString ( isoString )
91 */
92 JSLinearString* ParseTemporalCalendarString(JSContext* cx,
93 JS::Handle<JSString*> str);
94
95 /**
96 * ParseTemporalTimeString ( isoString )
97 */
98 bool ParseTemporalTimeString(JSContext* cx, JS::Handle<JSString*> str,
99 Time* result);
100
101 /**
102 * ParseTemporalMonthDayString ( isoString )
103 */
104 bool ParseTemporalMonthDayString(JSContext* cx, JS::Handle<JSString*> str,
105 ISODate* result, bool* hasYear,
106 JS::MutableHandle<JSString*> calendar);
107
108 /**
109 * ParseTemporalYearMonthString ( isoString )
110 */
111 bool ParseTemporalYearMonthString(JSContext* cx, JS::Handle<JSString*> str,
112 ISODate* result,
113 JS::MutableHandle<JSString*> calendar);
114
115 /**
116 * ParseTemporalDateTimeString ( isoString )
117 */
118 bool ParseTemporalDateTimeString(JSContext* cx, JS::Handle<JSString*> str,
119 ISODateTime* result,
120 JS::MutableHandle<JSString*> calendar);
121
122 /**
123 * ParseTemporalZonedDateTimeString ( isoString )
124 */
125 bool ParseTemporalZonedDateTimeString(
126 JSContext* cx, JS::Handle<JSString*> str,
127 JS::MutableHandle<ParsedZonedDateTime> result);
128
129 /**
130 * ParseTemporalRelativeToString ( isoString )
131 */
132 bool ParseTemporalRelativeToString(
133 JSContext* cx, JS::Handle<JSString*> str,
134 JS::MutableHandle<ParsedZonedDateTime> result);
135
136 } /* namespace js::temporal */
137
138 namespace js {
139
140 template <typename Wrapper>
141 class WrappedPtrOperations<temporal::ParsedTimeZone, Wrapper> {
142 const auto& object() const {
143 return static_cast<const Wrapper*>(this)->get();
144 }
145
146 public:
147 JS::Handle<JSLinearString*> name() const {
148 return JS::Handle<JSLinearString*>::fromMarkedLocation(&object().name);
149 }
150
151 int32_t offset() const { return object().offset; }
152
153 explicit operator bool() const { return bool(object()); }
154 };
155
156 template <typename Wrapper>
157 class WrappedPtrOperations<temporal::ParsedZonedDateTime, Wrapper> {
158 const auto& object() const {
159 return static_cast<const Wrapper*>(this)->get();
160 }
161
162 public:
163 const temporal::ISODateTime& dateTime() const { return object().dateTime; }
164
165 JS::Handle<JSLinearString*> calendar() const {
166 return JS::Handle<JSLinearString*>::fromMarkedLocation(&object().calendar);
167 }
168
169 JS::Handle<temporal::ParsedTimeZone> timeZoneAnnotation() const {
170 return JS::Handle<temporal::ParsedTimeZone>::fromMarkedLocation(
171 &object().timeZoneAnnotation);
172 }
173
174 int64_t timeZoneOffset() const { return object().timeZoneOffset; }
175
176 bool isUTC() const { return object().isUTC; }
177
178 bool hasOffset() const { return object().hasOffset; }
179
180 bool isStartOfDay() const { return object().isStartOfDay; }
181 };
182
183 } /* namespace js */
184
185 #endif /* builtin_temporal_TemporalParser_h */