summaryrefslogtreecommitdiffstats
path: root/js/src/builtin/temporal/TemporalFields.h
blob: 4cdbade772168295456029814b5f07f216d3d720 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*-
 * vim: set ts=8 sts=2 et sw=2 tw=80:
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

#ifndef builtin_temporal_TemporalFields_h
#define builtin_temporal_TemporalFields_h

#include "mozilla/Attributes.h"
#include "mozilla/FloatingPoint.h"

#include <initializer_list>

#include "jstypes.h"

#include "js/RootingAPI.h"
#include "js/TypeDecls.h"
#include "js/Value.h"

class JS_PUBLIC_API JSTracer;

namespace js {
class PlainObject;
}

namespace js::temporal {
enum class TemporalField {
  Year,
  Month,
  MonthCode,
  Day,
  Hour,
  Minute,
  Second,
  Millisecond,
  Microsecond,
  Nanosecond,
  Offset,
  Era,
  EraYear,
  TimeZone,
};

// Default values are specified in Table 15 [1]. `undefined` is replaced with
// an appropriate value based on the type, for example `double` fields use
// NaN whereas pointer fields use nullptr.
//
// [1] <https://tc39.es/proposal-temporal/#table-temporal-field-requirements>
struct MOZ_STACK_CLASS TemporalFields final {
  double year = mozilla::UnspecifiedNaN<double>();
  double month = mozilla::UnspecifiedNaN<double>();
  JSString* monthCode = nullptr;
  double day = mozilla::UnspecifiedNaN<double>();
  double hour = 0;
  double minute = 0;
  double second = 0;
  double millisecond = 0;
  double microsecond = 0;
  double nanosecond = 0;
  JSString* offset = nullptr;
  JSString* era = nullptr;
  double eraYear = mozilla::UnspecifiedNaN<double>();
  JS::Value timeZone = JS::UndefinedValue();

  TemporalFields() = default;

  void trace(JSTracer* trc);
};
}  // namespace js::temporal

namespace js {

template <typename Wrapper>
class WrappedPtrOperations<temporal::TemporalFields, Wrapper> {
  const temporal::TemporalFields& fields() const {
    return static_cast<const Wrapper*>(this)->get();
  }

 public:
  double year() const { return fields().year; }
  double month() const { return fields().month; }
  double day() const { return fields().day; }
  double hour() const { return fields().hour; }
  double minute() const { return fields().minute; }
  double second() const { return fields().second; }
  double millisecond() const { return fields().millisecond; }
  double microsecond() const { return fields().microsecond; }
  double nanosecond() const { return fields().nanosecond; }
  double eraYear() const { return fields().eraYear; }

  JS::Handle<JSString*> monthCode() const {
    return JS::Handle<JSString*>::fromMarkedLocation(&fields().monthCode);
  }
  JS::Handle<JSString*> offset() const {
    return JS::Handle<JSString*>::fromMarkedLocation(&fields().offset);
  }
  JS::Handle<JSString*> era() const {
    return JS::Handle<JSString*>::fromMarkedLocation(&fields().era);
  }
  JS::Handle<JS::Value> timeZone() const {
    return JS::Handle<JS::Value>::fromMarkedLocation(&fields().timeZone);
  }
};

template <typename Wrapper>
class MutableWrappedPtrOperations<temporal::TemporalFields, Wrapper>
    : public WrappedPtrOperations<temporal::TemporalFields, Wrapper> {
  temporal::TemporalFields& fields() {
    return static_cast<Wrapper*>(this)->get();
  }

 public:
  double& year() { return fields().year; }
  double& month() { return fields().month; }
  double& day() { return fields().day; }
  double& hour() { return fields().hour; }
  double& minute() { return fields().minute; }
  double& second() { return fields().second; }
  double& millisecond() { return fields().millisecond; }
  double& microsecond() { return fields().microsecond; }
  double& nanosecond() { return fields().nanosecond; }
  double& eraYear() { return fields().eraYear; }

  JS::MutableHandle<JSString*> monthCode() {
    return JS::MutableHandle<JSString*>::fromMarkedLocation(
        &fields().monthCode);
  }
  JS::MutableHandle<JSString*> offset() {
    return JS::MutableHandle<JSString*>::fromMarkedLocation(&fields().offset);
  }
  JS::MutableHandle<JSString*> era() {
    return JS::MutableHandle<JSString*>::fromMarkedLocation(&fields().era);
  }
  JS::MutableHandle<JS::Value> timeZone() {
    return JS::MutableHandle<JS::Value>::fromMarkedLocation(&fields().timeZone);
  }
};

}  // namespace js

namespace js::temporal {

/**
 * PrepareTemporalFields ( fields, fieldNames, requiredFields [ ,
 * duplicateBehaviour ] )
 */
bool PrepareTemporalFields(JSContext* cx, JS::Handle<JSObject*> fields,
                           std::initializer_list<TemporalField> fieldNames,
                           std::initializer_list<TemporalField> requiredFields,
                           JS::MutableHandle<TemporalFields> result);

using TemporalFieldNames = JS::StackGCVector<JS::PropertyKey>;

/**
 * PrepareTemporalFields ( fields, fieldNames, requiredFields [ ,
 * duplicateBehaviour ] )
 */
PlainObject* PrepareTemporalFields(JSContext* cx, JS::Handle<JSObject*> fields,
                                   JS::Handle<TemporalFieldNames> fieldNames);

/**
 * PrepareTemporalFields ( fields, fieldNames, requiredFields [ ,
 * duplicateBehaviour ] )
 */
PlainObject* PrepareTemporalFields(
    JSContext* cx, JS::Handle<JSObject*> fields,
    JS::Handle<TemporalFieldNames> fieldNames,
    std::initializer_list<TemporalField> requiredFields);

/**
 * PrepareTemporalFields ( fields, fieldNames, requiredFields [ ,
 * duplicateBehaviour ] )
 */
PlainObject* PreparePartialTemporalFields(
    JSContext* cx, JS::Handle<JSObject*> fields,
    JS::Handle<TemporalFieldNames> fieldNames);

[[nodiscard]] bool ConcatTemporalFieldNames(
    const TemporalFieldNames& receiverFieldNames,
    const TemporalFieldNames& inputFieldNames,
    TemporalFieldNames& concatenatedFieldNames);

[[nodiscard]] bool AppendSorted(
    JSContext* cx, TemporalFieldNames& fieldNames,
    std::initializer_list<TemporalField> additionalNames);

[[nodiscard]] bool SortTemporalFieldNames(JSContext* cx,
                                          TemporalFieldNames& fieldNames);

} /* namespace js::temporal */

#endif /* builtin_temporal_TemporalFields_h */