Debian Codegraph @

js/src/builtin/temporal/Temporal.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_Temporal_h
8 #define builtin_temporal_Temporal_h
9
10 #include "mozilla/Assertions.h"
11
12 #include <stdint.h>
13
14 #include "jstypes.h"
15
16 #include "builtin/temporal/Int128.h"
17 #include "builtin/temporal/TemporalRoundingMode.h"
18 #include "builtin/temporal/TemporalUnit.h"
19 #include "js/RootingAPI.h"
20 #include "js/TypeDecls.h"
21 #include "vm/NativeObject.h"
22
23 namespace js {
24 struct ClassSpec;
25 } // namespace js
26
27 namespace js::temporal {
28
29 class TemporalObject : public NativeObject {
30 public:
31 static const JSClass class_;
32
33 private:
34 static const ClassSpec classSpec_;
35 };
36
37 /**
38 * Rounding increment, which is an integer in the range [1, 1'000'000'000].
39 *
40 * Temporal units are rounded to a multiple of the specified increment value.
41 */
42 class Increment final {
43 uint32_t value_;
44
45 public:
46 constexpr explicit Increment(uint32_t value) : value_(value) {
47 MOZ_ASSERT(1 <= value && value <= 1'000'000'000);
48 }
49
50 /**
51 * Minimum allowed rounding increment.
52 */
53 static constexpr auto min() { return Increment{1}; }
54
55 /**
56 * Maximum allowed rounding increment.
57 */
58 static constexpr auto max() { return Increment{1'000'000'000}; }
59
60 /**
61 * The rounding increment's value.
62 */
63 uint32_t value() const { return value_; }
64
65 bool operator==(const Increment& other) const {
66 return value_ == other.value_;
67 }
68
69 bool operator<(const Increment& other) const { return value_ < other.value_; }
70
71 // Other operators are implemented in terms of operator== and operator<.
72 bool operator!=(const Increment& other) const { return !(*this == other); }
73 bool operator>(const Increment& other) const { return other < *this; }
74 bool operator<=(const Increment& other) const { return !(other < *this); }
75 bool operator>=(const Increment& other) const { return !(*this < other); }
76 };
77
78 /**
79 * GetRoundingIncrementOption ( normalizedOptions, dividend, inclusive )
80 */
81 bool GetRoundingIncrementOption(JSContext* cx, JS::Handle<JSObject*> options,
82 Increment* increment);
83
84 /**
85 * ValidateTemporalRoundingIncrement ( increment, dividend, inclusive )
86 */
87 bool ValidateTemporalRoundingIncrement(JSContext* cx, Increment increment,
88 int64_t dividend, bool inclusive);
89
90 /**
91 * ValidateTemporalRoundingIncrement ( increment, dividend, inclusive )
92 */
93 inline bool ValidateTemporalRoundingIncrement(JSContext* cx,
94 Increment increment,
95 Increment dividend,
96 bool inclusive) {
97 return ValidateTemporalRoundingIncrement(cx, increment, dividend.value(),
98 inclusive);
99 }
100
101 /**
102 * MaximumTemporalDurationRoundingIncrement ( unit )
103 */
104 constexpr Increment MaximumTemporalDurationRoundingIncrement(
105 TemporalUnit unit) {
106 // Step 1. (Not applicable in our implementation.)
107 MOZ_ASSERT(unit > TemporalUnit::Day);
108
109 // Step 2.
110 if (unit == TemporalUnit::Hour) {
111 return Increment{24};
112 }
113
114 // Step 3.
115 if (unit <= TemporalUnit::Second) {
116 return Increment{60};
117 }
118
119 // Steps 4-5.
120 return Increment{1000};
121 }
122
123 enum class TemporalUnitGroup {
124 // Allow date units: "year", "month", "week", "day".
125 Date,
126
127 // Allow time units: "hour", "minute", "second", "milli-/micro-/nanoseconds".
128 Time,
129
130 // Allow date and time units.
131 DateTime,
132
133 // Allow "day" and time units.
134 DayTime,
135 };
136
137 enum class TemporalUnitKey {
138 SmallestUnit,
139 LargestUnit,
140 Unit,
141 };
142
143 /**
144 * GetTemporalUnitValuedOption ( normalizedOptions, key, unitGroup, default [ ,
145 * extraValues ] )
146 */
147 bool GetTemporalUnitValuedOption(JSContext* cx, JS::Handle<JSObject*> options,
148 TemporalUnitKey key,
149 TemporalUnitGroup unitGroup,
150 TemporalUnit* unit);
151
152 /**
153 * GetTemporalUnitValuedOption ( normalizedOptions, key, unitGroup, default [ ,
154 * extraValues ] )
155 */
156 bool GetTemporalUnitValuedOption(JSContext* cx, JS::Handle<JSString*> value,
157 TemporalUnitKey key,
158 TemporalUnitGroup unitGroup,
159 TemporalUnit* unit);
160
161 /**
162 * GetRoundingModeOption ( normalizedOptions, fallback )
163 */
164 bool GetRoundingModeOption(JSContext* cx, JS::Handle<JSObject*> options,
165 TemporalRoundingMode* mode);
166
167 /**
168 * RoundNumberToIncrement ( x, increment, roundingMode )
169 */
170 Int128 RoundNumberToIncrement(const Int128& numerator, int64_t denominator,
171 Increment increment,
172 TemporalRoundingMode roundingMode);
173
174 /**
175 * RoundNumberToIncrement ( x, increment, roundingMode )
176 */
177 int64_t RoundNumberToIncrement(int64_t x, int64_t increment,
178 TemporalRoundingMode roundingMode);
179
180 /**
181 * RoundNumberToIncrement ( x, increment, roundingMode )
182 */
183 inline int64_t RoundNumberToIncrement(int64_t x, Increment increment,
184 TemporalRoundingMode roundingMode) {
185 return RoundNumberToIncrement(x, int64_t(increment.value()), roundingMode);
186 }
187
188 /**
189 * RoundNumberToIncrement ( x, increment, roundingMode )
190 */
191 Int128 RoundNumberToIncrement(const Int128& x, const Int128& increment,
192 TemporalRoundingMode roundingMode);
193
194 /**
195 * Return the double value of the fractional number `numerator / denominator`.
196 */
197 double FractionToDouble(int64_t numerator, int64_t denominator);
198
199 /**
200 * Return the double value of the fractional number `numerator / denominator`.
201 */
202 double FractionToDouble(const Int128& numerator, const Int128& denominator);
203
204 enum class ShowCalendar { Auto, Always, Never, Critical };
205
206 /**
207 * GetTemporalShowCalendarNameOption ( normalizedOptions )
208 */
209 bool GetTemporalShowCalendarNameOption(JSContext* cx,
210 JS::Handle<JSObject*> options,
211 ShowCalendar* result);
212
213 /**
214 * Precision when displaying fractional seconds.
215 */
216 class Precision final {
217 int8_t value_;
218
219 enum class Tag {};
220 constexpr Precision(int8_t value, Tag) : value_(value) {}
221
222 public:
223 constexpr explicit Precision(uint8_t value) : value_(int8_t(value)) {
224 MOZ_ASSERT(value < 10);
225 }
226
227 bool operator==(const Precision& other) const {
228 return value_ == other.value_;
229 }
230
231 bool operator!=(const Precision& other) const { return !(*this == other); }
232
233 /**
234 * Return the number of fractional second digits.
235 */
236 uint8_t value() const {
237 MOZ_ASSERT(value_ >= 0, "auto and minute precision don't have a value");
238 return uint8_t(value_);
239 }
240
241 /**
242 * Limit the precision to trim off any trailing zeros.
243 */
244 static constexpr Precision Auto() { return {-1, Tag{}}; }
245
246 /**
247 * Limit the precision to minutes, i.e. don't display seconds and sub-seconds.
248 */
249 static constexpr Precision Minute() { return {-2, Tag{}}; }
250 };
251
252 /**
253 * GetTemporalFractionalSecondDigitsOption ( normalizedOptions )
254 */
255 bool GetTemporalFractionalSecondDigitsOption(JSContext* cx,
256 JS::Handle<JSObject*> options,
257 Precision* precision);
258
259 struct SecondsStringPrecision final {
260 Precision precision = Precision{0};
261 TemporalUnit unit = TemporalUnit::Auto;
262 Increment increment = Increment{1};
263 };
264
265 /**
266 * ToSecondsStringPrecisionRecord ( smallestUnit, fractionalDigitCount )
267 */
268 SecondsStringPrecision ToSecondsStringPrecision(TemporalUnit smallestUnit,
269 Precision fractionalDigitCount);
270
271 enum class TemporalOverflow { Constrain, Reject };
272
273 /**
274 * GetTemporalOverflowOption ( normalizedOptions )
275 */
276 bool GetTemporalOverflowOption(JSContext* cx, JS::Handle<JSObject*> options,
277 TemporalOverflow* result);
278
279 enum class TemporalDisambiguation { Compatible, Earlier, Later, Reject };
280
281 /**
282 * GetTemporalDisambiguationOption ( options )
283 */
284 bool GetTemporalDisambiguationOption(JSContext* cx,
285 JS::Handle<JSObject*> options,
286 TemporalDisambiguation* disambiguation);
287
288 enum class TemporalOffset { Prefer, Use, Ignore, Reject };
289
290 /**
291 * GetTemporalOffsetOption ( options, fallback )
292 */
293 bool GetTemporalOffsetOption(JSContext* cx, JS::Handle<JSObject*> options,
294 TemporalOffset* offset);
295
296 enum class ShowTimeZoneName { Auto, Never, Critical };
297
298 bool GetTemporalShowTimeZoneNameOption(JSContext* cx,
299 JS::Handle<JSObject*> options,
300 ShowTimeZoneName* result);
301
302 enum class ShowOffset { Auto, Never };
303
304 /**
305 * GetTemporalShowOffsetOption ( normalizedOptions )
306 */
307 bool GetTemporalShowOffsetOption(JSContext* cx, JS::Handle<JSObject*> options,
308 ShowOffset* result);
309
310 enum class Direction { Next, Previous };
311
312 /**
313 * GetDirectionOption ( options )
314 */
315 bool GetDirectionOption(JSContext* cx, JS::Handle<JSObject*> options,
316 Direction* result);
317
318 /**
319 * GetDirectionOption ( options )
320 */
321 bool GetDirectionOption(JSContext* cx, JS::Handle<JSString*> direction,
322 Direction* result);
323
324 /**
325 * IsPartialTemporalObject ( object )
326 *
327 * Our implementation performs error reporting in this function instead of in
328 * the caller to provide better error messages.
329 */
330 bool ThrowIfTemporalLikeObject(JSContext* cx, JS::Handle<JSObject*> object);
331
332 /**
333 * ToPositiveIntegerWithTruncation ( argument )
334 */
335 bool ToPositiveIntegerWithTruncation(JSContext* cx, JS::Handle<JS::Value> value,
336 const char* name, double* result);
337
338 /**
339 * ToIntegerWithTruncation ( argument )
340 */
341 bool ToIntegerWithTruncation(JSContext* cx, JS::Handle<JS::Value> value,
342 const char* name, double* result);
343
344 enum class TemporalDifference { Since, Until };
345
346 inline const char* ToName(TemporalDifference difference) {
347 return difference == TemporalDifference::Since ? "since" : "until";
348 }
349
350 enum class TemporalAddDuration { Add, Subtract };
351
352 inline const char* ToName(TemporalAddDuration addDuration) {
353 return addDuration == TemporalAddDuration::Add ? "add" : "subtract";
354 }
355
356 struct DifferenceSettings final {
357 TemporalUnit smallestUnit = TemporalUnit::Auto;
358 TemporalUnit largestUnit = TemporalUnit::Auto;
359 TemporalRoundingMode roundingMode = TemporalRoundingMode::Trunc;
360 Increment roundingIncrement = Increment{1};
361 };
362
363 /**
364 * GetDifferenceSettings ( operation, options, unitGroup, disallowedUnits,
365 * fallbackSmallestUnit, smallestLargestDefaultUnit )
366 */
367 bool GetDifferenceSettings(JSContext* cx, TemporalDifference operation,
368 JS::Handle<JSObject*> options,
369 TemporalUnitGroup unitGroup,
370 TemporalUnit smallestAllowedUnit,
371 TemporalUnit fallbackSmallestUnit,
372 TemporalUnit smallestLargestDefaultUnit,
373 DifferenceSettings* result);
374
375 /**
376 * GetDifferenceSettings ( operation, options, unitGroup, disallowedUnits,
377 * fallbackSmallestUnit, smallestLargestDefaultUnit )
378 */
379 inline bool GetDifferenceSettings(JSContext* cx, TemporalDifference operation,
380 JS::Handle<JSObject*> options,
381 TemporalUnitGroup unitGroup,
382 TemporalUnit fallbackSmallestUnit,
383 TemporalUnit smallestLargestDefaultUnit,
384 DifferenceSettings* result) {
385 return GetDifferenceSettings(cx, operation, options, unitGroup,
386 TemporalUnit::Nanosecond, fallbackSmallestUnit,
387 smallestLargestDefaultUnit, result);
388 }
389
390 } /* namespace js::temporal */
391
392 #endif /* builtin_temporal_Temporal_h */