/
IDLTypes.h
323 lines (247 loc) · 12.7 KB
/
IDLTypes.h
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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
/*
* Copyright (C) 2016 Apple Inc. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
* THE POSSIBILITY OF SUCH DAMAGE.
*/
#pragma once
#include "StringAdaptors.h"
#include <heap/HandleTypes.h>
#include <wtf/Brigand.h>
#include <wtf/HashMap.h>
#include <wtf/StdLibExtras.h>
#include <wtf/text/WTFString.h>
#if ENABLE(WEBGL)
#include "WebGLAny.h"
#endif
namespace JSC {
class ArrayBuffer;
class ArrayBufferView;
class DataView;
class JSValue;
class JSObject;
template<typename> class Strong;
}
namespace WebCore {
class IDBKey;
class IDBKeyData;
class IDBValue;
class DOMPromise;
class ScheduledAction;
#if ENABLE(WEBGL)
class WebGLExtension;
#endif
template<typename T>
struct IDLType {
using ImplementationType = T;
using StorageType = T;
using ParameterType = T;
using NullableParameterType = std::optional<ImplementationType>;
using InnerParameterType = T;
using NullableInnerParameterType = std::optional<ImplementationType>;
using NullableType = std::optional<ImplementationType>;
static NullableType nullValue() { return std::nullopt; }
static bool isNullValue(const NullableType& value) { return !value; }
static ImplementationType extractValueFromNullable(const NullableType& value) { return value.value(); }
};
// IDLUnsupportedType is a special type that serves as a base class for currently unsupported types.
struct IDLUnsupportedType : IDLType<void> { };
// IDLNull is a special type for use as a subtype in an IDLUnion that is nullable.
struct IDLNull : IDLType<std::nullptr_t> { };
struct IDLAny : IDLType<JSC::Strong<JSC::Unknown>> {
using ParameterType = JSC::JSValue;
using NullableParameterType = JSC::JSValue;
using NullableType = JSC::Strong<JSC::Unknown>;
static inline std::nullptr_t nullValue() { return nullptr; }
template<typename U> static inline bool isNullValue(U&& value) { return !value; }
template<typename U> static inline U&& extractValueFromNullable(U&& value) { return std::forward<U>(value); }
};
struct IDLVoid : IDLType<void> { };
struct IDLBoolean : IDLType<bool> { };
template<typename NumericType> struct IDLNumber : IDLType<NumericType> { };
template<typename IntegerType> struct IDLInteger : IDLNumber<IntegerType> { };
struct IDLByte : IDLInteger<int8_t> { };
struct IDLOctet : IDLInteger<uint8_t> { };
struct IDLShort : IDLInteger<int16_t> { };
struct IDLUnsignedShort : IDLInteger<uint16_t> { };
struct IDLLong : IDLInteger<int32_t> { };
struct IDLUnsignedLong : IDLInteger<uint32_t> { };
struct IDLLongLong : IDLInteger<int64_t> { };
struct IDLUnsignedLongLong : IDLInteger<uint64_t> { };
template<typename T> struct IDLClampAdaptor : IDLInteger<typename T::ImplementationType> {
using InnerType = T;
};
template<typename T> struct IDLEnforceRangeAdaptor : IDLInteger<typename T::ImplementationType> {
using InnerType = T;
};
template<typename FloatingPointType> struct IDLFloatingPoint : IDLNumber<FloatingPointType> { };
struct IDLFloat : IDLFloatingPoint<float> { };
struct IDLUnrestrictedFloat : IDLFloatingPoint<float> { };
struct IDLDouble : IDLFloatingPoint<double> { };
struct IDLUnrestrictedDouble : IDLFloatingPoint<double> { };
template<typename StringType> struct IDLString : IDLType<StringType> {
using ParameterType = const StringType&;
using NullableParameterType = const StringType&;
using NullableType = StringType;
static StringType nullValue() { return StringType(); }
static bool isNullValue(const StringType& value) { return value.isNull(); }
static bool isNullValue(const UncachedString& value) { return value.string.isNull(); }
static bool isNullValue(const OwnedString& value) { return value.string.isNull(); }
template <typename U> static U&& extractValueFromNullable(U&& value) { return std::forward<U>(value); }
};
struct IDLDOMString : IDLString<String> { };
struct IDLByteString : IDLString<String> { };
struct IDLUSVString : IDLString<String> { };
template<typename T> struct IDLTreatNullAsEmptyAdaptor : IDLString<String> {
using InnerType = T;
};
template<typename T> struct IDLAtomicStringAdaptor : IDLString<AtomicString> {
using InnerType = T;
};
template<typename T> struct IDLRequiresExistingAtomicStringAdaptor : IDLString<AtomicString> {
using InnerType = T;
};
struct IDLObject : IDLType<JSC::Strong<JSC::JSObject>> {
using NullableType = JSC::Strong<JSC::JSObject>;
static inline std::nullptr_t nullValue() { return nullptr; }
template<typename U> static inline bool isNullValue(U&& value) { return !value; }
template<typename U> static inline U&& extractValueFromNullable(U&& value) { return std::forward<U>(value); }
};
template<typename T> struct IDLWrapper : IDLType<RefPtr<T>> {
using RawType = T;
using StorageType = Ref<T>;
using ParameterType = T&;
using NullableParameterType = T*;
using InnerParameterType = Ref<T>;
using NullableInnerParameterType = RefPtr<T>;
using NullableType = RefPtr<T>;
static inline std::nullptr_t nullValue() { return nullptr; }
template<typename U> static inline bool isNullValue(U&& value) { return !value; }
template<typename U> static inline U&& extractValueFromNullable(U&& value) { return std::forward<U>(value); }
};
template<typename T> struct IDLInterface : IDLWrapper<T> { };
template<typename T> struct IDLCallbackInterface : IDLWrapper<T> { };
template<typename T> struct IDLCallbackFunction : IDLWrapper<T> { };
template<typename T> struct IDLDictionary : IDLType<T> {
using ParameterType = const T&;
using NullableParameterType = const T&;
};
template<typename T> struct IDLEnumeration : IDLType<T> { };
template<typename T> struct IDLNullable : IDLType<typename T::NullableType> {
using InnerType = T;
using ParameterType = typename T::NullableParameterType;
using NullableParameterType = typename T::NullableParameterType;
using InnerParameterType = typename T::NullableInnerParameterType;
using NullableInnerParameterType = typename T::NullableInnerParameterType;
using NullableType = typename T::NullableType;
static inline auto nullValue() -> decltype(T::nullValue()) { return T::nullValue(); }
template<typename U> static inline bool isNullValue(U&& value) { return T::isNullValue(std::forward<U>(value)); }
template<typename U> static inline auto extractValueFromNullable(U&& value) -> decltype(T::extractValueFromNullable(std::forward<U>(value))) { return T::extractValueFromNullable(std::forward<U>(value)); }
};
template<typename T> struct IDLSequence : IDLType<Vector<typename T::ImplementationType>> {
using InnerType = T;
using ParameterType = const Vector<typename T::InnerParameterType>&;
using NullableParameterType = const std::optional<Vector<typename T::InnerParameterType>>&;
};
template<typename T> struct IDLFrozenArray : IDLType<Vector<typename T::ImplementationType>> {
using InnerType = T;
using ParameterType = const Vector<typename T::ImplementationType>&;
using NullableParameterType = const std::optional<Vector<typename T::ImplementationType>>&;
};
template<typename K, typename V> struct IDLRecord : IDLType<Vector<WTF::KeyValuePair<typename K::ImplementationType, typename V::ImplementationType>>> {
using KeyType = K;
using ValueType = V;
using ParameterType = const Vector<WTF::KeyValuePair<typename K::ImplementationType, typename V::ImplementationType>>&;
using NullableParameterType = const std::optional<Vector<WTF::KeyValuePair<typename K::ImplementationType, typename V::ImplementationType>>>&;
};
template<typename T> struct IDLPromise : IDLType<DOMPromise> {
using InnerType = T;
};
struct IDLError : IDLUnsupportedType { };
struct IDLDOMException : IDLUnsupportedType { };
template<typename... Ts>
struct IDLUnion : IDLType<Variant<typename Ts::ImplementationType...>> {
using TypeList = brigand::list<Ts...>;
using ParameterType = const Variant<typename Ts::ImplementationType...>&;
using NullableParameterType = const std::optional<Variant<typename Ts::ImplementationType...>>&;
};
template<typename T> struct IDLBufferSource : IDLWrapper<T> { };
struct IDLArrayBuffer : IDLBufferSource<JSC::ArrayBuffer> { };
// NOTE: WebIDL defines ArrayBufferView as an IDL union of all the TypedArray types.
// and DataView. For convience in our implementation, we give it a distinct
// type that maps to the shared based class of all those classes.
struct IDLArrayBufferView : IDLBufferSource<JSC::ArrayBufferView> { };
struct IDLDataView : IDLBufferSource<JSC::DataView> { };
template<typename T> struct IDLTypedArray : IDLBufferSource<T> { };
// NOTE: The specific typed array types are IDLTypedArray specialized on the typed array
// implementation type, e.g. IDLFloat64Array is IDLTypedArray<JSC::Float64Array>
// Non-WebIDL extensions
struct IDLDate : IDLType<double> {
using NullableType = double;
static double nullValue() { return std::numeric_limits<double>::quiet_NaN(); }
static bool isNullValue(double value) { return std::isnan(value); }
static double extractValueFromNullable(double value) { return value; }
};
struct IDLJSON : IDLType<String> {
using ParameterType = const String&;
using NullableParameterType = const String&;
using NullableType = String;
static String nullValue() { return String(); }
static bool isNullValue(const String& value) { return value.isNull(); }
template <typename U> static U&& extractValueFromNullable(U&& value) { return std::forward<U>(value); }
};
struct IDLScheduledAction : IDLType<std::unique_ptr<ScheduledAction>> { };
template<typename T> struct IDLSerializedScriptValue : IDLWrapper<T> { };
template<typename T> struct IDLEventListener : IDLWrapper<T> { };
template<typename T> struct IDLXPathNSResolver : IDLWrapper<T> { };
struct IDLIDBKey : IDLWrapper<IDBKey> { };
struct IDLIDBKeyData : IDLWrapper<IDBKeyData> { };
struct IDLIDBValue : IDLWrapper<IDBValue> { };
#if ENABLE(WEBGL)
struct IDLWebGLAny : IDLType<WebGLAny> { };
struct IDLWebGLExtension : IDLWrapper<WebGLExtension> { };
#endif
// Helper predicates
template<typename T>
struct IsIDLInterface : public std::integral_constant<bool, WTF::IsTemplate<T, IDLInterface>::value> { };
template<typename T>
struct IsIDLDictionary : public std::integral_constant<bool, WTF::IsTemplate<T, IDLDictionary>::value> { };
template<typename T>
struct IsIDLEnumeration : public std::integral_constant<bool, WTF::IsTemplate<T, IDLEnumeration>::value> { };
template<typename T>
struct IsIDLSequence : public std::integral_constant<bool, WTF::IsTemplate<T, IDLSequence>::value> { };
template<typename T>
struct IsIDLFrozenArray : public std::integral_constant<bool, WTF::IsTemplate<T, IDLFrozenArray>::value> { };
template<typename T>
struct IsIDLRecord : public std::integral_constant<bool, WTF::IsTemplate<T, IDLRecord>::value> { };
template<typename T>
struct IsIDLString : public std::integral_constant<bool, WTF::IsBaseOfTemplate<IDLString, T>::value> { };
template<typename T>
struct IsIDLStringOrEnumeration : public std::integral_constant<bool, WTF::IsBaseOfTemplate<IDLString, T>::value || WTF::IsTemplate<T, IDLEnumeration>::value> { };
template<typename T>
struct IsIDLNumber : public std::integral_constant<bool, WTF::IsBaseOfTemplate<IDLNumber, T>::value> { };
template<typename T>
struct IsIDLInteger : public std::integral_constant<bool, WTF::IsBaseOfTemplate<IDLInteger, T>::value> { };
template<typename T>
struct IsIDLFloatingPoint : public std::integral_constant<bool, WTF::IsBaseOfTemplate<IDLFloatingPoint, T>::value> { };
template<typename T>
struct IsIDLTypedArray : public std::integral_constant<bool, WTF::IsBaseOfTemplate<IDLTypedArray, T>::value> { };
} // namespace WebCore