Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(396)

Side by Side Diff: third_party/WebKit/Source/bindings/core/v8/IDLTypes.h

Issue 2709983004: WIP bindings: Add support for the record<K,V> WebIDL type. (Closed)
Patch Set: Rebased patch using NativeValueTraits for IDL types Created 3 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 // Copyright (c) 2017 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #ifndef IDLTypes_h
6 #define IDLTypes_h
7
8 #include <type_traits>
9 #include "bindings/core/v8/NativeValueTraits.h"
10 #include "bindings/core/v8/SerializedScriptValue.h"
11 #include "bindings/core/v8/V8Binding.h"
12 #include "core/CoreExport.h"
13 #include "platform/heap/Handle.h"
14 #include "wtf/TypeTraits.h"
15 #include "wtf/text/WTFString.h"
16
17 namespace blink {
18
19 namespace idl {
Yuki 2017/03/02 07:47:28 Do we have a consensus to introduce a new nested n
Raphael Kubo da Costa (rakuco) 2017/03/02 08:16:24 We don't, I introduced this namespace while writin
Yuki 2017/03/02 13:00:24 I'm fine with either way. "IDLFoo" seems okay to
20
21 struct CORE_EXPORT Boolean final {};
22
23 struct CORE_EXPORT Byte final {};
24 struct CORE_EXPORT Octet final {};
25 struct CORE_EXPORT Short final {};
26 struct CORE_EXPORT UnsignedShort final {};
27 struct CORE_EXPORT Long final {};
28 struct CORE_EXPORT UnsignedLong final {};
29 struct CORE_EXPORT LongLong final {};
30 struct CORE_EXPORT UnsignedLongLong final {};
31
32 struct CORE_EXPORT ByteString final {};
33 struct CORE_EXPORT String final {};
34 struct CORE_EXPORT USVString final {};
35
36 struct CORE_EXPORT Double final {};
37 struct CORE_EXPORT UnrestrictedDouble final {};
38
39 struct CORE_EXPORT Float final {};
40 struct CORE_EXPORT UnrestrictedFloat final {};
41
42 struct CORE_EXPORT Date final {};
43
44 struct CORE_EXPORT Promise final {};
45
46 template <typename SequenceType>
47 struct CORE_EXPORT Sequence final {};
48
49 template <typename KeyType, typename ValueType>
50 struct CORE_EXPORT Record final {};
51
52 } // namespace idl
53
54 class DOMWindow;
55
56 // Boolean
57 // -------
58 template <>
59 struct NativeValueTraits<idl::Boolean> {
60 using ImplType = bool;
61
62 CORE_EXPORT static inline bool nativeValue(v8::Isolate* isolate,
63 v8::Local<v8::Value> value,
64 ExceptionState& exceptionState) {
65 return toBoolean(isolate, value, exceptionState);
66 }
67 };
68
69 // Integers
70 // --------
71 template <>
72 struct NativeValueTraits<idl::Byte> {
73 using ImplType = int8_t;
74
75 CORE_EXPORT static inline int8_t nativeValue(
76 v8::Isolate* isolate,
77 v8::Local<v8::Value> value,
78 ExceptionState& exceptionState,
79 IntegerConversionConfiguration conversionMode = NormalConversion) {
80 return toInt8(isolate, value, conversionMode, exceptionState);
81 }
82 };
83
84 template <>
85 struct NativeValueTraits<idl::Octet> {
86 using ImplType = uint8_t;
87
88 CORE_EXPORT static inline uint8_t nativeValue(
89 v8::Isolate* isolate,
90 v8::Local<v8::Value> value,
91 ExceptionState& exceptionState,
92 IntegerConversionConfiguration conversionMode = NormalConversion) {
93 return toUInt8(isolate, value, conversionMode, exceptionState);
94 }
95 };
96
97 template <>
98 struct NativeValueTraits<idl::Short> {
99 using ImplType = int16_t;
100
101 CORE_EXPORT static inline int16_t nativeValue(
102 v8::Isolate* isolate,
103 v8::Local<v8::Value> value,
104 ExceptionState& exceptionState,
105 IntegerConversionConfiguration conversionMode = NormalConversion) {
106 return toInt16(isolate, value, conversionMode, exceptionState);
107 }
108 };
109
110 template <>
111 struct NativeValueTraits<idl::UnsignedShort> {
112 using ImplType = uint16_t;
113
114 CORE_EXPORT static inline uint16_t nativeValue(
115 v8::Isolate* isolate,
116 v8::Local<v8::Value> value,
117 ExceptionState& exceptionState,
118 IntegerConversionConfiguration conversionMode = NormalConversion) {
119 return toUInt16(isolate, value, conversionMode, exceptionState);
120 }
121 };
122
123 template <>
124 struct NativeValueTraits<idl::Long> {
125 using ImplType = int32_t;
126
127 CORE_EXPORT static inline int32_t nativeValue(
128 v8::Isolate* isolate,
129 v8::Local<v8::Value> value,
130 ExceptionState& exceptionState,
131 IntegerConversionConfiguration conversionMode = NormalConversion) {
132 return toInt32(isolate, value, conversionMode, exceptionState);
133 }
134 };
135
136 template <>
137 struct NativeValueTraits<idl::UnsignedLong> {
138 using ImplType = uint32_t;
139
140 CORE_EXPORT static inline uint32_t nativeValue(
141 v8::Isolate* isolate,
142 v8::Local<v8::Value> value,
143 ExceptionState& exceptionState,
144 IntegerConversionConfiguration conversionMode = NormalConversion) {
145 return toUInt32(isolate, value, conversionMode, exceptionState);
146 }
147 };
148
149 template <>
150 struct NativeValueTraits<idl::LongLong> {
151 using ImplType = int64_t;
152
153 CORE_EXPORT static inline int64_t nativeValue(
154 v8::Isolate* isolate,
155 v8::Local<v8::Value> value,
156 ExceptionState& exceptionState,
157 IntegerConversionConfiguration conversionMode = NormalConversion) {
158 return toInt64(isolate, value, conversionMode, exceptionState);
159 }
160 };
161
162 template <>
163 struct NativeValueTraits<idl::UnsignedLongLong> {
164 using ImplType = uint64_t;
165
166 CORE_EXPORT static inline uint64_t nativeValue(
167 v8::Isolate* isolate,
168 v8::Local<v8::Value> value,
169 ExceptionState& exceptionState,
170 IntegerConversionConfiguration conversionMode = NormalConversion) {
171 return toUInt64(isolate, value, conversionMode, exceptionState);
172 }
173 };
174
175 // Strings
176 // -------
177 template <>
178 struct NativeValueTraits<idl::ByteString> {
179 using ImplType = String;
180
181 CORE_EXPORT static inline String nativeValue(v8::Isolate* isolate,
182 v8::Local<v8::Value> value,
183 ExceptionState& exceptionState) {
184 return toByteString(isolate, value, exceptionState);
185 }
186 };
187
188 template <>
189 struct NativeValueTraits<idl::String> {
190 using ImplType = String;
191
192 template <V8StringResourceMode Mode = DefaultMode>
193 CORE_EXPORT static inline String nativeValue(v8::Isolate* isolate,
194 v8::Local<v8::Value> value,
195 ExceptionState& exceptionState) {
196 V8StringResource<Mode> s(value);
197 if (!s.prepare(isolate, exceptionState))
198 return String();
199 return s;
200 }
201 };
202
203 template <>
204 struct NativeValueTraits<idl::USVString> {
205 using ImplType = String;
206
207 CORE_EXPORT static inline String nativeValue(v8::Isolate* isolate,
208 v8::Local<v8::Value> value,
209 ExceptionState& exceptionState) {
210 return toUSVString(isolate, value, exceptionState);
211 }
212 };
213
214 // Floats and doubles
215 // ------------------
216 template <>
217 struct NativeValueTraits<idl::Double> {
218 using ImplType = double;
219
220 CORE_EXPORT static inline double nativeValue(v8::Isolate* isolate,
221 v8::Local<v8::Value> value,
222 ExceptionState& exceptionState) {
223 return toRestrictedDouble(isolate, value, exceptionState);
224 }
225 };
226
227 template <>
228 struct NativeValueTraits<idl::UnrestrictedDouble> {
229 using ImplType = double;
230
231 CORE_EXPORT static inline double nativeValue(v8::Isolate* isolate,
232 v8::Local<v8::Value> value,
233 ExceptionState& exceptionState) {
234 return toDouble(isolate, value, exceptionState);
235 }
236 };
237
238 template <>
239 struct NativeValueTraits<idl::Float> {
240 using ImplType = float;
241
242 CORE_EXPORT static inline float nativeValue(v8::Isolate* isolate,
243 v8::Local<v8::Value> value,
244 ExceptionState& exceptionState) {
245 return toRestrictedFloat(isolate, value, exceptionState);
246 }
247 };
248
249 template <>
250 struct NativeValueTraits<idl::UnrestrictedFloat> {
251 using ImplType = float;
252
253 CORE_EXPORT static inline float nativeValue(v8::Isolate* isolate,
254 v8::Local<v8::Value> value,
255 ExceptionState& exceptionState) {
256 return toFloat(isolate, value, exceptionState);
257 }
258 };
259
260 // Promises
261 // --------
262 template <>
263 struct NativeValueTraits<idl::Promise> {
264 using ImplType = ScriptPromise;
265 static inline ScriptPromise nativeValue(v8::Isolate* isolate,
266 v8::Local<v8::Value> value,
267 ExceptionState& exceptionState) {
268 return ScriptPromise::cast(ScriptState::current(isolate), value);
269 }
270 };
271
272 // Type-specific overloads.
273 // ------------------------
274 template <>
275 struct NativeValueTraits<DOMWindow> {
276 using ImplType = DOMWindow;
277
278 CORE_EXPORT static inline DOMWindow* nativeValue(
279 v8::Isolate* isolate,
280 v8::Local<v8::Value> value,
281 ExceptionState& exceptionState) {
282 return toDOMWindow(isolate, value);
283 }
284 };
285
286 template <>
287 struct NativeValueTraits<SerializedScriptValue> {
288 using ImplType = SerializedScriptValue;
289
290 CORE_EXPORT static inline PassRefPtr<SerializedScriptValue> nativeValue(
291 v8::Isolate* isolate,
292 v8::Local<v8::Value> value,
293 ExceptionState& exceptionState) {
294 return SerializedScriptValue::serialize(isolate, value, nullptr, nullptr,
295 exceptionState);
296 }
297 };
298
299 template <>
300 struct NativeValueTraits<idl::Date> {
301 using ImplType = double;
302
303 CORE_EXPORT static inline double nativeValue(v8::Isolate* isolate,
304 v8::Local<v8::Value> value,
305 ExceptionState& exceptionState) {
306 return toCoreDate(isolate, value, exceptionState);
307 }
308 };
309
310 template <typename T>
311 struct NativeValueTraits<Member<T>> {
312 using ImplType = Member<T>;
313
314 CORE_EXPORT static inline T* nativeValue(v8::Isolate* isolate,
315 v8::Local<v8::Value> value,
316 ExceptionState& exceptionState) {
317 return NativeValueTraits<T>::nativeValue(isolate, value, exceptionState);
318 }
319 };
320
321 template <typename T>
322 struct NativeValueTraits<T> {
323 using ImplType = T;
324
325 CORE_EXPORT static inline T* nativeValue(v8::Isolate* isolate,
326 v8::Local<v8::Value> value,
327 ExceptionState& exceptionState) {
328 return V8TypeOf<T>::Type::toImplWithTypeCheck(isolate, value);
329 }
330 };
331
332 // Type helpers
333 // ------------
334 template <typename T>
335 struct MaybeWrapped {
336 using ImplType = typename std::
337 conditional<WTF::IsGarbageCollectedType<T>::value, Member<T>, T>::type;
338 };
339
340 template <typename T>
341 struct NeedsHeapVector {
342 static const bool value = WTF::IsGarbageCollectedType<T>::value ||
343 std::is_class<typename V8TypeOf<T>::Type>::value;
344 };
345
346 // Sequences
347 // ---------
348 template <typename T>
349 struct NativeValueTraits<idl::Sequence<T>> {
350 private:
351 using CppType = typename NativeValueTraits<T>::ImplType;
352 using MaybeWrappedCppType = typename MaybeWrapped<CppType>::ImplType;
353
354 public:
355 using ImplType = typename std::conditional<NeedsHeapVector<CppType>::value,
356 HeapVector<MaybeWrappedCppType>,
357 Vector<MaybeWrappedCppType>>::type;
358
359 CORE_EXPORT static inline ImplType nativeValue(v8::Isolate* isolate,
360 v8::Local<v8::Value> value,
361 ExceptionState& exceptionState,
362 int index = 0) {
363 return toImplArray<ImplType, T>(value, index, isolate, exceptionState);
364 }
365 };
366
367 // Records
368 // -------
369 template <typename K, typename V>
370 struct NativeValueTraits<idl::Record<K, V>> {
371 private:
372 using ValueCppType = typename NativeValueTraits<V>::ImplType;
373 using MaybeWrappedValueCppType =
374 typename MaybeWrapped<ValueCppType>::ImplType;
375
376 public:
377 using ImplType = typename std::conditional<
378 NeedsHeapVector<ValueCppType>::value,
379 HeapVector<std::pair<String, MaybeWrappedValueCppType>>,
380 Vector<std::pair<String, MaybeWrappedValueCppType>>>::type;
381
382 CORE_EXPORT static inline ImplType nativeValue(
383 v8::Isolate* isolate,
384 v8::Local<v8::Value> value,
385 ExceptionState& exceptionState) {
386 return toImplRecord<K, V, ImplType>(isolate, value, exceptionState);
387 }
388 };
389
390 } // namespace blink
391
392 #endif // IDLTypes_h
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698