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

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: Move specialization for SerializedScriptValue to the appropriate header 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/V8Binding.h"
11 #include "core/CoreExport.h"
12 #include "wtf/text/WTFString.h"
13
14 namespace blink {
15
16 // Boolean
17 template <>
18 struct NativeValueTraits<IDLBoolean>
19 : public NativeValueTraitsBase<IDLBoolean> {
20 CORE_EXPORT static inline bool nativeValue(v8::Isolate* isolate,
21 v8::Local<v8::Value> value,
22 ExceptionState& exceptionState) {
23 return toBoolean(isolate, value, exceptionState);
24 }
25 };
26
27 // Integers
28 //
29 // All integer specializations offer a second nativeValue() besides the default
30 // one: it takes an IntegerConversionConfiguration argument to let callers
31 // specify how the integers should be converted. The default nativeValue()
32 // overload will always use NormalConversion.
33 template <>
34 struct NativeValueTraits<IDLByte> : public NativeValueTraitsBase<IDLByte> {
35 CORE_EXPORT static inline int8_t nativeValue(v8::Isolate* isolate,
36 v8::Local<v8::Value> value,
37 ExceptionState& exceptionState) {
38 return nativeValue(isolate, value, exceptionState, NormalConversion);
39 }
40
41 CORE_EXPORT static inline int8_t nativeValue(
42 v8::Isolate* isolate,
43 v8::Local<v8::Value> value,
44 ExceptionState& exceptionState,
45 IntegerConversionConfiguration conversionMode) {
46 return toInt8(isolate, value, conversionMode, exceptionState);
47 }
48 };
49
50 template <>
51 struct NativeValueTraits<IDLOctet> : public NativeValueTraitsBase<IDLOctet> {
52 CORE_EXPORT static inline uint8_t nativeValue(
53 v8::Isolate* isolate,
54 v8::Local<v8::Value> value,
55 ExceptionState& exceptionState) {
56 return nativeValue(isolate, value, exceptionState, NormalConversion);
57 }
58
59 CORE_EXPORT static inline uint8_t nativeValue(
60 v8::Isolate* isolate,
61 v8::Local<v8::Value> value,
62 ExceptionState& exceptionState,
63 IntegerConversionConfiguration conversionMode) {
64 return toUInt8(isolate, value, conversionMode, exceptionState);
65 }
66 };
67
68 template <>
69 struct NativeValueTraits<IDLShort> : public NativeValueTraitsBase<IDLShort> {
70 CORE_EXPORT static inline int16_t nativeValue(
71 v8::Isolate* isolate,
72 v8::Local<v8::Value> value,
73 ExceptionState& exceptionState) {
74 return nativeValue(isolate, value, exceptionState, NormalConversion);
75 }
76
77 CORE_EXPORT static inline int16_t nativeValue(
78 v8::Isolate* isolate,
79 v8::Local<v8::Value> value,
80 ExceptionState& exceptionState,
81 IntegerConversionConfiguration conversionMode) {
82 return toInt16(isolate, value, conversionMode, exceptionState);
83 }
84 };
85
86 template <>
87 struct NativeValueTraits<IDLUnsignedShort>
88 : public NativeValueTraitsBase<IDLUnsignedShort> {
89 CORE_EXPORT static inline uint16_t nativeValue(
90 v8::Isolate* isolate,
91 v8::Local<v8::Value> value,
92 ExceptionState& exceptionState) {
93 return nativeValue(isolate, value, exceptionState, NormalConversion);
94 }
95
96 CORE_EXPORT static inline uint16_t nativeValue(
97 v8::Isolate* isolate,
98 v8::Local<v8::Value> value,
99 ExceptionState& exceptionState,
100 IntegerConversionConfiguration conversionMode) {
101 return toUInt16(isolate, value, conversionMode, exceptionState);
102 }
103 };
104
105 template <>
106 struct NativeValueTraits<IDLLong> : public NativeValueTraitsBase<IDLLong> {
107 CORE_EXPORT static inline int32_t nativeValue(
108 v8::Isolate* isolate,
109 v8::Local<v8::Value> value,
110 ExceptionState& exceptionState) {
111 return nativeValue(isolate, value, exceptionState, NormalConversion);
112 }
113
114 CORE_EXPORT static inline int32_t nativeValue(
115 v8::Isolate* isolate,
116 v8::Local<v8::Value> value,
117 ExceptionState& exceptionState,
118 IntegerConversionConfiguration conversionMode) {
119 return toInt32(isolate, value, conversionMode, exceptionState);
120 }
121 };
122
123 template <>
124 struct NativeValueTraits<IDLUnsignedLong>
125 : public NativeValueTraitsBase<IDLUnsignedLong> {
126 CORE_EXPORT static inline uint32_t nativeValue(
127 v8::Isolate* isolate,
128 v8::Local<v8::Value> value,
129 ExceptionState& exceptionState) {
130 return nativeValue(isolate, value, exceptionState, NormalConversion);
131 }
132
133 CORE_EXPORT static inline uint32_t nativeValue(
134 v8::Isolate* isolate,
135 v8::Local<v8::Value> value,
136 ExceptionState& exceptionState,
137 IntegerConversionConfiguration conversionMode) {
138 return toUInt32(isolate, value, conversionMode, exceptionState);
139 }
140 };
141
142 template <>
143 struct NativeValueTraits<IDLLongLong>
144 : public NativeValueTraitsBase<IDLLongLong> {
145 CORE_EXPORT static inline int64_t nativeValue(
146 v8::Isolate* isolate,
147 v8::Local<v8::Value> value,
148 ExceptionState& exceptionState) {
149 return nativeValue(isolate, value, exceptionState, NormalConversion);
150 }
151
152 CORE_EXPORT static inline int64_t nativeValue(
153 v8::Isolate* isolate,
154 v8::Local<v8::Value> value,
155 ExceptionState& exceptionState,
156 IntegerConversionConfiguration conversionMode) {
157 return toInt64(isolate, value, conversionMode, exceptionState);
158 }
159 };
160
161 template <>
162 struct NativeValueTraits<IDLUnsignedLongLong>
163 : public NativeValueTraitsBase<IDLUnsignedLongLong> {
164 CORE_EXPORT static inline uint64_t nativeValue(
165 v8::Isolate* isolate,
166 v8::Local<v8::Value> value,
167 ExceptionState& exceptionState) {
168 return nativeValue(isolate, value, exceptionState, NormalConversion);
169 }
170
171 CORE_EXPORT static inline uint64_t nativeValue(
172 v8::Isolate* isolate,
173 v8::Local<v8::Value> value,
174 ExceptionState& exceptionState,
175 IntegerConversionConfiguration conversionMode) {
176 return toUInt64(isolate, value, conversionMode, exceptionState);
177 }
178 };
179
180 // Strings
181 template <>
182 struct NativeValueTraits<IDLByteString>
183 : public NativeValueTraitsBase<IDLByteString> {
184 CORE_EXPORT static inline String nativeValue(v8::Isolate* isolate,
185 v8::Local<v8::Value> value,
186 ExceptionState& exceptionState) {
187 return toByteString(isolate, value, exceptionState);
188 }
189 };
190
191 template <>
192 struct NativeValueTraits<IDLString> : public NativeValueTraitsBase<IDLString> {
193 CORE_EXPORT static inline String nativeValue(v8::Isolate* isolate,
194 v8::Local<v8::Value> value,
195 ExceptionState& exceptionState) {
196 return nativeValue<V8StringResourceMode::DefaultMode>(isolate, value,
197 exceptionState);
198 }
199
200 template <V8StringResourceMode Mode = DefaultMode>
201 CORE_EXPORT static inline String nativeValue(v8::Isolate* isolate,
202 v8::Local<v8::Value> value,
203 ExceptionState& exceptionState) {
204 V8StringResource<Mode> string(value);
205 if (!string.prepare(isolate, exceptionState))
206 return String();
207 return string;
208 }
209 };
210
211 template <>
212 struct NativeValueTraits<IDLUSVString>
213 : public NativeValueTraitsBase<IDLUSVString> {
214 CORE_EXPORT static inline String nativeValue(v8::Isolate* isolate,
215 v8::Local<v8::Value> value,
216 ExceptionState& exceptionState) {
217 return toUSVString(isolate, value, exceptionState);
218 }
219 };
220
221 // Floats and doubles
222 template <>
223 struct NativeValueTraits<IDLDouble> : public NativeValueTraitsBase<IDLDouble> {
224 CORE_EXPORT static inline double nativeValue(v8::Isolate* isolate,
225 v8::Local<v8::Value> value,
226 ExceptionState& exceptionState) {
227 return toRestrictedDouble(isolate, value, exceptionState);
228 }
229 };
230
231 template <>
232 struct NativeValueTraits<IDLUnrestrictedDouble>
233 : public NativeValueTraitsBase<IDLUnrestrictedDouble> {
234 CORE_EXPORT static inline double nativeValue(v8::Isolate* isolate,
235 v8::Local<v8::Value> value,
236 ExceptionState& exceptionState) {
237 return toDouble(isolate, value, exceptionState);
238 }
239 };
240
241 template <>
242 struct NativeValueTraits<IDLFloat> : public NativeValueTraitsBase<IDLFloat> {
243 CORE_EXPORT static inline float nativeValue(v8::Isolate* isolate,
244 v8::Local<v8::Value> value,
245 ExceptionState& exceptionState) {
246 return toRestrictedFloat(isolate, value, exceptionState);
247 }
248 };
249
250 template <>
251 struct NativeValueTraits<IDLUnrestrictedFloat>
252 : public NativeValueTraitsBase<IDLUnrestrictedFloat> {
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 template <>
262 struct NativeValueTraits<IDLPromise>
263 : public NativeValueTraitsBase<IDLPromise> {
264 CORE_EXPORT static inline ScriptPromise nativeValue(
265 v8::Isolate* isolate,
266 v8::Local<v8::Value> value,
267 ExceptionState& exceptionState) {
268 return nativeValue(isolate, value);
269 }
270
271 CORE_EXPORT static inline ScriptPromise nativeValue(
272 v8::Isolate* isolate,
273 v8::Local<v8::Value> value) {
274 return ScriptPromise::cast(ScriptState::current(isolate), value);
275 }
276 };
277
278 // Type-specific overloads
279 template <>
280 struct NativeValueTraits<IDLDate> : public NativeValueTraitsBase<IDLDate> {
281 CORE_EXPORT static inline double nativeValue(v8::Isolate* isolate,
282 v8::Local<v8::Value> value,
283 ExceptionState& exceptionState) {
284 return toCoreDate(isolate, value, exceptionState);
285 }
286 };
287
288 // Sequences
289 template <typename T>
290 struct NativeValueTraits<IDLSequence<T>>
291 : public NativeValueTraitsBase<IDLSequence<T>> {
292 // Nondependent types need to be explicitly qualified to be accessible.
293 using typename NativeValueTraitsBase<IDLSequence<T>>::ImplType;
294
295 CORE_EXPORT static inline ImplType nativeValue(
296 v8::Isolate* isolate,
297 v8::Local<v8::Value> value,
298 ExceptionState& exceptionState) {
299 return nativeValue(isolate, value, exceptionState, 0);
300 }
301
302 CORE_EXPORT static inline ImplType nativeValue(v8::Isolate* isolate,
303 v8::Local<v8::Value> value,
304 ExceptionState& exceptionState,
305 int index) {
306 return toImplArray<ImplType, T>(value, index, isolate, exceptionState);
307 }
308 };
309
310 } // namespace blink
311
312 #endif // NativeValueTraitsImpl_h
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698