OLD | NEW |
(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 { |
| 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 template <typename SequenceType> |
| 45 struct CORE_EXPORT Sequence final {}; |
| 46 |
| 47 } // namespace idl |
| 48 |
| 49 class DOMWindow; |
| 50 |
| 51 // Boolean |
| 52 // ------- |
| 53 template <> |
| 54 struct NativeValueTraits<idl::Boolean> { |
| 55 using ImplType = bool; |
| 56 |
| 57 CORE_EXPORT static inline bool nativeValue(v8::Isolate* isolate, |
| 58 v8::Local<v8::Value> value, |
| 59 ExceptionState& exceptionState) { |
| 60 return toBoolean(isolate, value, exceptionState); |
| 61 } |
| 62 }; |
| 63 |
| 64 // Integers |
| 65 // -------- |
| 66 template <> |
| 67 struct NativeValueTraits<idl::Byte> { |
| 68 using ImplType = int8_t; |
| 69 |
| 70 CORE_EXPORT static inline int8_t nativeValue(v8::Isolate* isolate, |
| 71 v8::Local<v8::Value> value, |
| 72 ExceptionState& exceptionState, |
| 73 IntegerConversionConfiguration co
nversionMode = NormalConversion) { |
| 74 return toInt8(isolate, value, conversionMode, exceptionState); |
| 75 } |
| 76 }; |
| 77 |
| 78 template <> |
| 79 struct NativeValueTraits<idl::Octet> { |
| 80 using ImplType = uint8_t; |
| 81 |
| 82 CORE_EXPORT static inline uint8_t nativeValue(v8::Isolate* isolate, |
| 83 v8::Local<v8::Value> value, |
| 84 ExceptionState& exceptionState, |
| 85 IntegerConversionConfiguration c
onversionMode = NormalConversion) { |
| 86 return toUInt8(isolate, value, conversionMode, exceptionState); |
| 87 } |
| 88 }; |
| 89 |
| 90 template <> |
| 91 struct NativeValueTraits<idl::Short> { |
| 92 using ImplType = int16_t; |
| 93 |
| 94 CORE_EXPORT static inline int16_t nativeValue(v8::Isolate* isolate, |
| 95 v8::Local<v8::Value> value, |
| 96 ExceptionState& exceptionState, |
| 97 IntegerConversionConfiguration c
onversionMode = NormalConversion) { |
| 98 return toInt16(isolate, value, conversionMode, exceptionState); |
| 99 } |
| 100 }; |
| 101 |
| 102 template <> |
| 103 struct NativeValueTraits<idl::UnsignedShort> { |
| 104 using ImplType = uint16_t; |
| 105 |
| 106 CORE_EXPORT static inline uint16_t nativeValue(v8::Isolate* isolate, |
| 107 v8::Local<v8::Value> value, |
| 108 ExceptionState& exceptionState, |
| 109 IntegerConversionConfiguration
conversionMode = NormalConversion) { |
| 110 return toUInt16(isolate, value, conversionMode, exceptionState); |
| 111 } |
| 112 }; |
| 113 |
| 114 template <> |
| 115 struct NativeValueTraits<idl::Long> { |
| 116 using ImplType = int32_t; |
| 117 |
| 118 CORE_EXPORT static inline int32_t nativeValue(v8::Isolate* isolate, |
| 119 v8::Local<v8::Value> value, |
| 120 ExceptionState& exceptionState, |
| 121 IntegerConversionConfiguration c
onversionMode = NormalConversion) { |
| 122 return toInt32(isolate, value, conversionMode, exceptionState); |
| 123 } |
| 124 }; |
| 125 |
| 126 template <> |
| 127 struct NativeValueTraits<idl::UnsignedLong> { |
| 128 using ImplType = uint32_t; |
| 129 |
| 130 CORE_EXPORT static inline uint32_t nativeValue(v8::Isolate* isolate, |
| 131 v8::Local<v8::Value> value, |
| 132 ExceptionState& exceptionState, |
| 133 IntegerConversionConfiguration
conversionMode = NormalConversion) { |
| 134 return toUInt32(isolate, value, conversionMode, exceptionState); |
| 135 } |
| 136 }; |
| 137 |
| 138 template <> |
| 139 struct NativeValueTraits<idl::LongLong> { |
| 140 using ImplType = int64_t; |
| 141 |
| 142 CORE_EXPORT static inline int64_t nativeValue(v8::Isolate* isolate, |
| 143 v8::Local<v8::Value> value, |
| 144 ExceptionState& exceptionState, |
| 145 IntegerConversionConfiguration c
onversionMode = NormalConversion) { |
| 146 return toInt64(isolate, value, conversionMode, exceptionState); |
| 147 } |
| 148 }; |
| 149 |
| 150 template <> |
| 151 struct NativeValueTraits<idl::UnsignedLongLong> { |
| 152 using ImplType = uint64_t; |
| 153 |
| 154 CORE_EXPORT static inline uint64_t nativeValue(v8::Isolate* isolate, |
| 155 v8::Local<v8::Value> value, |
| 156 ExceptionState& exceptionState, |
| 157 IntegerConversionConfiguration
conversionMode = NormalConversion) { |
| 158 return toUInt64(isolate, value, conversionMode, exceptionState); |
| 159 } |
| 160 }; |
| 161 |
| 162 // Strings |
| 163 // ------- |
| 164 template <> |
| 165 struct NativeValueTraits<idl::ByteString> { |
| 166 using ImplType = String; |
| 167 |
| 168 CORE_EXPORT static inline String nativeValue(v8::Isolate* isolate, |
| 169 v8::Local<v8::Value> value, |
| 170 ExceptionState& exceptionState) { |
| 171 return toByteString(isolate, value, exceptionState); |
| 172 } |
| 173 }; |
| 174 |
| 175 template <> |
| 176 struct NativeValueTraits<idl::String> { |
| 177 using ImplType = String; |
| 178 |
| 179 template <V8StringResourceMode Mode = DefaultMode> |
| 180 CORE_EXPORT static inline String nativeValue(v8::Isolate* isolate, |
| 181 v8::Local<v8::Value> value, |
| 182 ExceptionState& exceptionState) { |
| 183 V8StringResource<Mode> s(value); |
| 184 if (!s.prepare(isolate, exceptionState)) |
| 185 return String(); |
| 186 return s; |
| 187 } |
| 188 }; |
| 189 |
| 190 template <> |
| 191 struct NativeValueTraits<idl::USVString> { |
| 192 using ImplType = String; |
| 193 |
| 194 CORE_EXPORT static inline String nativeValue(v8::Isolate* isolate, |
| 195 v8::Local<v8::Value> value, |
| 196 ExceptionState& exceptionState) { |
| 197 return toUSVString(isolate, value, exceptionState); |
| 198 } |
| 199 }; |
| 200 |
| 201 // Floats and doubles |
| 202 // ------------------ |
| 203 template <> |
| 204 struct NativeValueTraits<idl::Double> { |
| 205 using ImplType = double; |
| 206 |
| 207 CORE_EXPORT static inline double nativeValue(v8::Isolate* isolate, |
| 208 v8::Local<v8::Value> value, |
| 209 ExceptionState& exceptionState) { |
| 210 return toRestrictedDouble(isolate, value, exceptionState); |
| 211 } |
| 212 }; |
| 213 |
| 214 template <> |
| 215 struct NativeValueTraits<idl::UnrestrictedDouble> { |
| 216 using ImplType = double; |
| 217 |
| 218 CORE_EXPORT static inline double nativeValue(v8::Isolate* isolate, |
| 219 v8::Local<v8::Value> value, |
| 220 ExceptionState& exceptionState) { |
| 221 return toDouble(isolate, value, exceptionState); |
| 222 } |
| 223 }; |
| 224 |
| 225 template <> |
| 226 struct NativeValueTraits<idl::Float> { |
| 227 using ImplType = float; |
| 228 |
| 229 CORE_EXPORT static inline float nativeValue(v8::Isolate* isolate, |
| 230 v8::Local<v8::Value> value, |
| 231 ExceptionState& exceptionState) { |
| 232 return toRestrictedFloat(isolate, value, exceptionState); |
| 233 } |
| 234 }; |
| 235 |
| 236 template <> |
| 237 struct NativeValueTraits<idl::UnrestrictedFloat> { |
| 238 using ImplType = float; |
| 239 |
| 240 CORE_EXPORT static inline float nativeValue(v8::Isolate* isolate, |
| 241 v8::Local<v8::Value> value, |
| 242 ExceptionState& exceptionState) { |
| 243 return toFloat(isolate, value, exceptionState); |
| 244 } |
| 245 }; |
| 246 |
| 247 // Type-specific overloads. |
| 248 // ------------------------ |
| 249 template <> |
| 250 struct NativeValueTraits<DOMWindow> { |
| 251 using ImplType = DOMWindow; |
| 252 |
| 253 CORE_EXPORT static inline DOMWindow* nativeValue( |
| 254 v8::Isolate* isolate, |
| 255 v8::Local<v8::Value> value, |
| 256 ExceptionState& exceptionState) { |
| 257 return toDOMWindow(isolate, value); |
| 258 } |
| 259 }; |
| 260 |
| 261 template <> |
| 262 struct NativeValueTraits<SerializedScriptValue> { |
| 263 using ImplType = SerializedScriptValue; |
| 264 |
| 265 CORE_EXPORT static inline PassRefPtr<SerializedScriptValue> nativeValue( |
| 266 v8::Isolate* isolate, |
| 267 v8::Local<v8::Value> value, |
| 268 ExceptionState& exceptionState) { |
| 269 return SerializedScriptValue::serialize(isolate, value, nullptr, nullptr, ex
ceptionState); |
| 270 } |
| 271 }; |
| 272 |
| 273 template <> |
| 274 struct NativeValueTraits<idl::Date> { |
| 275 using ImplType = double; |
| 276 |
| 277 CORE_EXPORT static inline double nativeValue(v8::Isolate* isolate, |
| 278 v8::Local<v8::Value> value, |
| 279 ExceptionState& exceptionState) { |
| 280 return toCoreDate(isolate, value, exceptionState); |
| 281 } |
| 282 }; |
| 283 |
| 284 template <typename T> |
| 285 struct NativeValueTraits<Member<T>> { |
| 286 using ImplType = Member<T>; |
| 287 |
| 288 CORE_EXPORT static inline T* nativeValue(v8::Isolate* isolate, |
| 289 v8::Local<v8::Value> value, |
| 290 ExceptionState& exceptionState) { |
| 291 return NativeValueTraits<T>::nativeValue(isolate, value, exceptionState); |
| 292 } |
| 293 }; |
| 294 |
| 295 template <typename T> |
| 296 struct NativeValueTraits<T> { |
| 297 using ImplType = T; |
| 298 |
| 299 CORE_EXPORT static inline T* nativeValue(v8::Isolate* isolate, |
| 300 v8::Local<v8::Value> value, |
| 301 ExceptionState& exceptionState) { |
| 302 return V8TypeOf<T>::Type::toImplWithTypeCheck(isolate, value); |
| 303 } |
| 304 }; |
| 305 |
| 306 // Sequences |
| 307 // --------- |
| 308 template <typename T> |
| 309 struct NativeValueTraits<idl::Sequence<T>> { |
| 310 private: |
| 311 using MemberImplType = typename NativeValueTraits<T>::ImplType; |
| 312 using FullMemberImplType = typename std::conditional<WTF::IsGarbageCollectedTy
pe<MemberImplType>::value, |
| 313 Member<MemberImplType>, |
| 314 MemberImplType>::type; |
| 315 |
| 316 public: |
| 317 // This template metaprogramming black magic intends to cover the following ca
ses: |
| 318 // * The vector's member type is an Oilpan type: use HeapVector<Member<T>> |
| 319 // * The vector's member type is an IDL union or dictionary type: use HeapVect
or<T> |
| 320 // * Everything else: use a simple Vector<T> |
| 321 using ImplType = |
| 322 typename std::conditional<WTF::IsGarbageCollectedType<MemberImplType>::val
ue || |
| 323 std::is_class<typename V8TypeOf<MemberImplType>:
:Type>::value, |
| 324 HeapVector<FullMemberImplType>, |
| 325 Vector<FullMemberImplType>>::type; |
| 326 |
| 327 CORE_EXPORT static inline ImplType |
| 328 nativeValue(v8::Isolate* isolate, v8::Local<v8::Value> value, ExceptionState&
exceptionState, int index = 0) { |
| 329 return toImplArray<ImplType>(value, index, isolate, exceptionState); |
| 330 } |
| 331 }; |
| 332 |
| 333 } // namespace blink |
| 334 |
| 335 #endif // IDLTypes_h |
OLD | NEW |