Chromium Code Reviews| Index: third_party/WebKit/Source/bindings/core/v8/NativeValueTraits.h |
| diff --git a/third_party/WebKit/Source/bindings/core/v8/NativeValueTraits.h b/third_party/WebKit/Source/bindings/core/v8/NativeValueTraits.h |
| index 3bc77e548afbe3d997b21bdb675e06fdf1f05fac..5edacfc776dcd2bf41c024bd53ab79196347d1f1 100644 |
| --- a/third_party/WebKit/Source/bindings/core/v8/NativeValueTraits.h |
| +++ b/third_party/WebKit/Source/bindings/core/v8/NativeValueTraits.h |
| @@ -5,6 +5,8 @@ |
| #ifndef NativeValueTraits_h |
| #define NativeValueTraits_h |
| +#include <type_traits> |
| +#include "bindings/core/v8/IDLTypesBase.h" |
| #include "v8/include/v8.h" |
| #include "wtf/Allocator.h" |
| @@ -12,13 +14,75 @@ namespace blink { |
| class ExceptionState; |
| -template <typename T, typename... Arguments> |
| -struct NativeValueTraits { |
| - STATIC_ONLY(NativeValueTraits); |
| - static T nativeValue(v8::Isolate*, |
| - v8::Local<v8::Value>, |
| - ExceptionState&, |
| - Arguments... args); |
| +// NativeValueTraitsBase is supposed to be inherited by NativeValueTraits |
| +// classes. They serve as a way to hold the ImplType typedef without requiring |
| +// all NativeValueTraits specializations to declare it. |
| +// |
| +// The primary template below is used by NativeValueTraits specializations with |
| +// types that do not inherit from IDLBase, in which case it is assumed the type |
| +// of the specialization is also |ImplType|. The NativeValueTraitsBase |
| +// specialization is used for IDLBase-based types, which are supposed to have |
| +// their own |ImplType| typedefs. |
| +template <typename T, typename SFINAEHelper = void> |
| +struct NativeValueTraitsBase { |
| + using ImplType = T; |
| + STATIC_ONLY(NativeValueTraitsBase); |
| +}; |
| + |
| +template <typename T> |
| +struct NativeValueTraitsBase< |
| + T, |
| + typename std::enable_if<std::is_base_of<IDLBase, T>::value>::type> { |
| + using ImplType = typename T::ImplType; |
| + STATIC_ONLY(NativeValueTraitsBase); |
| +}; |
| + |
| +// Primary template for NativeValueTraits. It is not supposed to be used |
| +// directly: there needs to be a specialization for each type which represents |
| +// a JavaScript type that will be converted to a C++ representation. |
| +// Its main goal is to provide a standard interface for converting JS types |
| +// into C++ ones. |
| +// |
| +// Example: |
| +// template <> |
| +// struct NativeValueTraits<IDLLong> : public NativeValueTraitsBase<IDLLong> { |
| +// static inline int32_t nativeValue(v8::Isolate* isolate, |
| +// v8::Local<v8::Value> value, |
| +// ExceptionState& exceptionState) { |
| +// return toInt32(isolate, value, exceptionState, NormalConversion); |
| +// } |
| +// } |
| +// |
| +// The SFINAEHelper template parameter can be used by template specializations |
| +// on T that need to perform type introspection on it. |
| +// |
| +// Example: |
| +// struct S { |
| +// static const int i = 42; |
| +// }; |
| +// /* This specialization is chosen when T has an integral member called i */ |
| +// template <typename T> |
| +// struct NativeValueTraits<T, |
| +// typename std::enable_if<std::is_integral<decltype(T::i)>::value>::type> |
| +// : public NativeValueTraitsBase<T> { |
| +// static inline bool nativeValue(v8::Isolate* isolate, |
|
haraken
2017/03/06 12:56:33
Why are we returning bool while the type is an int
|
| +// v8::Local<v8::Value> value, |
| +// ExceptionState& exceptionState) { |
| +// return true; |
| +// } |
| +// }; |
| +// |
| +// Note that there exist some specializations (particularly in V8Binding.h) for |
| +// which T actually represents the final C++ type that a JavaScript value |
| +// should be converted to. Introducing new specializations of this kind is |
| +// discouraged. |
| +template <typename T, typename SFINAEHelper = void> |
| +struct NativeValueTraits : public NativeValueTraitsBase<T> { |
| + // This declaration serves only as a blueprint for specializations: the |
| + // return type can change, but all specializations are expected to provide a |
| + // nativeValue() method that takes the 3 arguments below. |
|
haraken
2017/03/06 12:56:33
Just to confirm: If someone forgets to specialize
Raphael Kubo da Costa (rakuco)
2017/03/06 14:29:17
Correct. If there's no specialization for a given
|
| + static inline typename NativeValueTraitsBase<T>::ImplType |
|
Yuki
2017/03/06 11:39:00
nit: inline keyword here doesn't make much sense.
|
| + nativeValue(v8::Isolate*, v8::Local<v8::Value>, ExceptionState&); |
| }; |
| } // namespace blink |