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

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

Issue 2730183003: bindings: Add C++ versions of WebIDL types and generalize NativeValueTraits. (Closed)
Patch Set: Remove unnecessary include 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 NativeValueTraitsImpl_h
6 #define NativeValueTraitsImpl_h
7
8 #include "bindings/core/v8/IDLTypes.h"
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 "wtf/text/WTFString.h"
14
15 namespace blink {
16
17 // Boolean
18 template <>
19 struct NativeValueTraits<IDLBoolean>
20 : public NativeValueTraitsBase<IDLBoolean> {
21 CORE_EXPORT static inline bool nativeValue(v8::Isolate* isolate,
22 v8::Local<v8::Value> value,
23 ExceptionState& exceptionState) {
24 return toBoolean(isolate, value, exceptionState);
25 }
26 };
27
28 // Integers
29 //
30 // All integer specializations offer a second nativeValue() besides the default
31 // one: it takes an IntegerConversionConfiguration argument to let callers
32 // specify how the integers should be converted. The default nativeValue()
33 // overload will always use NormalConversion.
34 template <>
35 struct NativeValueTraits<IDLByte> : public NativeValueTraitsBase<IDLByte> {
36 CORE_EXPORT static inline int8_t nativeValue(v8::Isolate* isolate,
37 v8::Local<v8::Value> value,
38 ExceptionState& exceptionState) {
39 return nativeValue(isolate, value, exceptionState, NormalConversion);
40 }
41
42 CORE_EXPORT static inline int8_t nativeValue(
43 v8::Isolate* isolate,
44 v8::Local<v8::Value> value,
45 ExceptionState& exceptionState,
46 IntegerConversionConfiguration conversionMode) {
47 return toInt8(isolate, value, conversionMode, exceptionState);
48 }
49 };
50
51 template <>
52 struct NativeValueTraits<IDLOctet> : public NativeValueTraitsBase<IDLOctet> {
53 CORE_EXPORT static inline uint8_t nativeValue(
54 v8::Isolate* isolate,
55 v8::Local<v8::Value> value,
56 ExceptionState& exceptionState) {
57 return nativeValue(isolate, value, exceptionState, NormalConversion);
58 }
59
60 CORE_EXPORT static inline uint8_t nativeValue(
61 v8::Isolate* isolate,
62 v8::Local<v8::Value> value,
63 ExceptionState& exceptionState,
64 IntegerConversionConfiguration conversionMode) {
65 return toUInt8(isolate, value, conversionMode, exceptionState);
66 }
67 };
68
69 template <>
70 struct NativeValueTraits<IDLShort> : public NativeValueTraitsBase<IDLShort> {
71 CORE_EXPORT static inline int16_t nativeValue(
72 v8::Isolate* isolate,
73 v8::Local<v8::Value> value,
74 ExceptionState& exceptionState) {
75 return nativeValue(isolate, value, exceptionState, NormalConversion);
76 }
77
78 CORE_EXPORT static inline int16_t nativeValue(
79 v8::Isolate* isolate,
80 v8::Local<v8::Value> value,
81 ExceptionState& exceptionState,
82 IntegerConversionConfiguration conversionMode) {
83 return toInt16(isolate, value, conversionMode, exceptionState);
84 }
85 };
86
87 template <>
88 struct NativeValueTraits<IDLUnsignedShort>
89 : public NativeValueTraitsBase<IDLUnsignedShort> {
90 CORE_EXPORT static inline uint16_t nativeValue(
91 v8::Isolate* isolate,
92 v8::Local<v8::Value> value,
93 ExceptionState& exceptionState) {
94 return nativeValue(isolate, value, exceptionState, NormalConversion);
95 }
96
97 CORE_EXPORT static inline uint16_t nativeValue(
98 v8::Isolate* isolate,
99 v8::Local<v8::Value> value,
100 ExceptionState& exceptionState,
101 IntegerConversionConfiguration conversionMode) {
102 return toUInt16(isolate, value, conversionMode, exceptionState);
103 }
104 };
105
106 template <>
107 struct NativeValueTraits<IDLLong> : public NativeValueTraitsBase<IDLLong> {
108 CORE_EXPORT static inline int32_t nativeValue(
109 v8::Isolate* isolate,
110 v8::Local<v8::Value> value,
111 ExceptionState& exceptionState) {
112 return nativeValue(isolate, value, exceptionState, NormalConversion);
113 }
114
115 CORE_EXPORT static inline int32_t nativeValue(
116 v8::Isolate* isolate,
117 v8::Local<v8::Value> value,
118 ExceptionState& exceptionState,
119 IntegerConversionConfiguration conversionMode) {
120 return toInt32(isolate, value, conversionMode, exceptionState);
121 }
122 };
123
124 template <>
125 struct NativeValueTraits<IDLUnsignedLong>
126 : public NativeValueTraitsBase<IDLUnsignedLong> {
127 CORE_EXPORT static inline uint32_t nativeValue(
128 v8::Isolate* isolate,
129 v8::Local<v8::Value> value,
130 ExceptionState& exceptionState) {
131 return nativeValue(isolate, value, exceptionState, NormalConversion);
132 }
133
134 CORE_EXPORT static inline uint32_t nativeValue(
135 v8::Isolate* isolate,
136 v8::Local<v8::Value> value,
137 ExceptionState& exceptionState,
138 IntegerConversionConfiguration conversionMode) {
139 return toUInt32(isolate, value, conversionMode, exceptionState);
140 }
141 };
142
143 template <>
144 struct NativeValueTraits<IDLLongLong>
145 : public NativeValueTraitsBase<IDLLongLong> {
146 CORE_EXPORT static inline int64_t nativeValue(
147 v8::Isolate* isolate,
148 v8::Local<v8::Value> value,
149 ExceptionState& exceptionState) {
150 return nativeValue(isolate, value, exceptionState, NormalConversion);
151 }
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) {
158 return toInt64(isolate, value, conversionMode, exceptionState);
159 }
160 };
161
162 template <>
163 struct NativeValueTraits<IDLUnsignedLongLong>
164 : public NativeValueTraitsBase<IDLUnsignedLongLong> {
165 CORE_EXPORT static inline uint64_t nativeValue(
166 v8::Isolate* isolate,
167 v8::Local<v8::Value> value,
168 ExceptionState& exceptionState) {
169 return nativeValue(isolate, value, exceptionState, NormalConversion);
170 }
171
172 CORE_EXPORT static inline uint64_t nativeValue(
173 v8::Isolate* isolate,
174 v8::Local<v8::Value> value,
175 ExceptionState& exceptionState,
176 IntegerConversionConfiguration conversionMode) {
177 return toUInt64(isolate, value, conversionMode, exceptionState);
178 }
179 };
180
181 // Strings
182 template <>
183 struct NativeValueTraits<IDLByteString>
184 : public NativeValueTraitsBase<IDLByteString> {
185 CORE_EXPORT static inline String nativeValue(v8::Isolate* isolate,
186 v8::Local<v8::Value> value,
187 ExceptionState& exceptionState) {
188 return toByteString(isolate, value, exceptionState);
189 }
190 };
191
192 template <>
193 struct NativeValueTraits<IDLString> : public NativeValueTraitsBase<IDLString> {
194 CORE_EXPORT static inline String nativeValue(v8::Isolate* isolate,
195 v8::Local<v8::Value> value,
196 ExceptionState& exceptionState) {
197 return nativeValue<V8StringResourceMode::DefaultMode>(isolate, value,
haraken 2017/03/06 12:56:33 Just to confirm: Using the DefaultMode doesn't cha
Raphael Kubo da Costa (rakuco) 2017/03/06 14:29:17 Right, that's what the specialization for WTF::Str
198 exceptionState);
199 }
200
201 template <V8StringResourceMode Mode = DefaultMode>
202 CORE_EXPORT static inline String nativeValue(v8::Isolate* isolate,
203 v8::Local<v8::Value> value,
204 ExceptionState& exceptionState) {
205 V8StringResource<Mode> s(value);
haraken 2017/03/06 12:56:33 s => string
Raphael Kubo da Costa (rakuco) 2017/03/06 14:29:17 Done.
206 if (!s.prepare(isolate, exceptionState))
207 return String();
208 return s;
209 }
210 };
211
212 template <>
213 struct NativeValueTraits<IDLUSVString>
214 : public NativeValueTraitsBase<IDLUSVString> {
215 CORE_EXPORT static inline String nativeValue(v8::Isolate* isolate,
216 v8::Local<v8::Value> value,
217 ExceptionState& exceptionState) {
218 return toUSVString(isolate, value, exceptionState);
219 }
220 };
221
222 // Floats and doubles
223 template <>
224 struct NativeValueTraits<IDLDouble> : public NativeValueTraitsBase<IDLDouble> {
225 CORE_EXPORT static inline double nativeValue(v8::Isolate* isolate,
226 v8::Local<v8::Value> value,
227 ExceptionState& exceptionState) {
228 return toRestrictedDouble(isolate, value, exceptionState);
229 }
230 };
231
232 template <>
233 struct NativeValueTraits<IDLUnrestrictedDouble>
234 : public NativeValueTraitsBase<IDLUnrestrictedDouble> {
235 CORE_EXPORT static inline double nativeValue(v8::Isolate* isolate,
236 v8::Local<v8::Value> value,
237 ExceptionState& exceptionState) {
238 return toDouble(isolate, value, exceptionState);
239 }
240 };
241
242 template <>
243 struct NativeValueTraits<IDLFloat> : public NativeValueTraitsBase<IDLFloat> {
244 CORE_EXPORT static inline float nativeValue(v8::Isolate* isolate,
245 v8::Local<v8::Value> value,
246 ExceptionState& exceptionState) {
247 return toRestrictedFloat(isolate, value, exceptionState);
248 }
249 };
250
251 template <>
252 struct NativeValueTraits<IDLUnrestrictedFloat>
253 : public NativeValueTraitsBase<IDLUnrestrictedFloat> {
254 CORE_EXPORT static inline float nativeValue(v8::Isolate* isolate,
255 v8::Local<v8::Value> value,
256 ExceptionState& exceptionState) {
257 return toFloat(isolate, value, exceptionState);
258 }
259 };
260
261 // Promises
262 template <>
263 struct NativeValueTraits<IDLPromise>
264 : public NativeValueTraitsBase<IDLPromise> {
265 CORE_EXPORT static inline ScriptPromise nativeValue(
266 v8::Isolate* isolate,
267 v8::Local<v8::Value> value,
268 ExceptionState& exceptionState) {
269 return nativeValue(isolate, value);
haraken 2017/03/06 12:56:33 Is this used? If it's unused, we can add NOT_REACH
Raphael Kubo da Costa (rakuco) 2017/03/06 14:29:17 This one is supposed to be used by callers which h
270 }
271
272 CORE_EXPORT static inline ScriptPromise nativeValue(
273 v8::Isolate* isolate,
274 v8::Local<v8::Value> value) {
275 return ScriptPromise::cast(ScriptState::current(isolate), value);
276 }
277 };
278
279 // Type-specific overloads
280 template <>
281 struct NativeValueTraits<IDLDate> : public NativeValueTraitsBase<IDLDate> {
282 CORE_EXPORT static inline double nativeValue(v8::Isolate* isolate,
283 v8::Local<v8::Value> value,
284 ExceptionState& exceptionState) {
285 return toCoreDate(isolate, value, exceptionState);
286 }
287 };
288
289 template <>
290 struct NativeValueTraits<SerializedScriptValue>
291 : public NativeValueTraitsBase<SerializedScriptValue> {
292 CORE_EXPORT static inline PassRefPtr<SerializedScriptValue> nativeValue(
293 v8::Isolate* isolate,
294 v8::Local<v8::Value> value,
295 ExceptionState& exceptionState) {
296 return SerializedScriptValue::serialize(isolate, value, nullptr, nullptr,
297 exceptionState);
298 }
299 };
300
301 // Generated IDL interfaces.
302 template <typename T>
303 struct NativeValueTraits<T,
304 typename std::enable_if<std::is_class<
305 typename V8TypeOf<T>::Type>::value>::type>
haraken 2017/03/06 12:56:33 Add a comment about when enble_if ... returns true
306 : public NativeValueTraitsBase<T> {
307 CORE_EXPORT static inline T* nativeValue(v8::Isolate* isolate,
308 v8::Local<v8::Value> value,
309 ExceptionState& exceptionState) {
310 return nativeValue(isolate, value);
311 }
312
313 CORE_EXPORT static inline T* nativeValue(v8::Isolate* isolate,
314 v8::Local<v8::Value> value) {
315 return V8TypeOf<T>::Type::toImplWithTypeCheck(isolate, value);
haraken 2017/03/06 12:56:33 Just for the record, we should be careful not to u
316 }
317 };
318
319 // Sequences
320 template <typename T>
321 struct NativeValueTraits<IDLSequence<T>>
322 : public NativeValueTraitsBase<IDLSequence<T>> {
323 // Nondependent types need to be explicitly qualified to be accessible.
324 using typename NativeValueTraitsBase<IDLSequence<T>>::ImplType;
325
326 CORE_EXPORT static inline ImplType nativeValue(
327 v8::Isolate* isolate,
328 v8::Local<v8::Value> value,
329 ExceptionState& exceptionState) {
330 return nativeValue(isolate, value, exceptionState, 0);
331 }
332
333 CORE_EXPORT static inline ImplType nativeValue(v8::Isolate* isolate,
334 v8::Local<v8::Value> value,
335 ExceptionState& exceptionState,
336 int index) {
337 return toImplArray<ImplType, T>(value, index, isolate, exceptionState);
338 }
339 };
340
341 } // namespace blink
342
343 #endif // NativeValueTraitsImpl_h
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698