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

Unified 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 side-by-side diff with in-line comments
Download patch
Index: third_party/WebKit/Source/bindings/core/v8/NativeValueTraitsImpl.h
diff --git a/third_party/WebKit/Source/bindings/core/v8/NativeValueTraitsImpl.h b/third_party/WebKit/Source/bindings/core/v8/NativeValueTraitsImpl.h
new file mode 100644
index 0000000000000000000000000000000000000000..48c107e61769f960fe626c0ba94a80f8a3a464b1
--- /dev/null
+++ b/third_party/WebKit/Source/bindings/core/v8/NativeValueTraitsImpl.h
@@ -0,0 +1,343 @@
+// Copyright (c) 2017 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#ifndef NativeValueTraitsImpl_h
+#define NativeValueTraitsImpl_h
+
+#include "bindings/core/v8/IDLTypes.h"
+#include "bindings/core/v8/NativeValueTraits.h"
+#include "bindings/core/v8/SerializedScriptValue.h"
+#include "bindings/core/v8/V8Binding.h"
+#include "core/CoreExport.h"
+#include "wtf/text/WTFString.h"
+
+namespace blink {
+
+// Boolean
+template <>
+struct NativeValueTraits<IDLBoolean>
+ : public NativeValueTraitsBase<IDLBoolean> {
+ CORE_EXPORT static inline bool nativeValue(v8::Isolate* isolate,
+ v8::Local<v8::Value> value,
+ ExceptionState& exceptionState) {
+ return toBoolean(isolate, value, exceptionState);
+ }
+};
+
+// Integers
+//
+// All integer specializations offer a second nativeValue() besides the default
+// one: it takes an IntegerConversionConfiguration argument to let callers
+// specify how the integers should be converted. The default nativeValue()
+// overload will always use NormalConversion.
+template <>
+struct NativeValueTraits<IDLByte> : public NativeValueTraitsBase<IDLByte> {
+ CORE_EXPORT static inline int8_t nativeValue(v8::Isolate* isolate,
+ v8::Local<v8::Value> value,
+ ExceptionState& exceptionState) {
+ return nativeValue(isolate, value, exceptionState, NormalConversion);
+ }
+
+ CORE_EXPORT static inline int8_t nativeValue(
+ v8::Isolate* isolate,
+ v8::Local<v8::Value> value,
+ ExceptionState& exceptionState,
+ IntegerConversionConfiguration conversionMode) {
+ return toInt8(isolate, value, conversionMode, exceptionState);
+ }
+};
+
+template <>
+struct NativeValueTraits<IDLOctet> : public NativeValueTraitsBase<IDLOctet> {
+ CORE_EXPORT static inline uint8_t nativeValue(
+ v8::Isolate* isolate,
+ v8::Local<v8::Value> value,
+ ExceptionState& exceptionState) {
+ return nativeValue(isolate, value, exceptionState, NormalConversion);
+ }
+
+ CORE_EXPORT static inline uint8_t nativeValue(
+ v8::Isolate* isolate,
+ v8::Local<v8::Value> value,
+ ExceptionState& exceptionState,
+ IntegerConversionConfiguration conversionMode) {
+ return toUInt8(isolate, value, conversionMode, exceptionState);
+ }
+};
+
+template <>
+struct NativeValueTraits<IDLShort> : public NativeValueTraitsBase<IDLShort> {
+ CORE_EXPORT static inline int16_t nativeValue(
+ v8::Isolate* isolate,
+ v8::Local<v8::Value> value,
+ ExceptionState& exceptionState) {
+ return nativeValue(isolate, value, exceptionState, NormalConversion);
+ }
+
+ CORE_EXPORT static inline int16_t nativeValue(
+ v8::Isolate* isolate,
+ v8::Local<v8::Value> value,
+ ExceptionState& exceptionState,
+ IntegerConversionConfiguration conversionMode) {
+ return toInt16(isolate, value, conversionMode, exceptionState);
+ }
+};
+
+template <>
+struct NativeValueTraits<IDLUnsignedShort>
+ : public NativeValueTraitsBase<IDLUnsignedShort> {
+ CORE_EXPORT static inline uint16_t nativeValue(
+ v8::Isolate* isolate,
+ v8::Local<v8::Value> value,
+ ExceptionState& exceptionState) {
+ return nativeValue(isolate, value, exceptionState, NormalConversion);
+ }
+
+ CORE_EXPORT static inline uint16_t nativeValue(
+ v8::Isolate* isolate,
+ v8::Local<v8::Value> value,
+ ExceptionState& exceptionState,
+ IntegerConversionConfiguration conversionMode) {
+ return toUInt16(isolate, value, conversionMode, exceptionState);
+ }
+};
+
+template <>
+struct NativeValueTraits<IDLLong> : public NativeValueTraitsBase<IDLLong> {
+ CORE_EXPORT static inline int32_t nativeValue(
+ v8::Isolate* isolate,
+ v8::Local<v8::Value> value,
+ ExceptionState& exceptionState) {
+ return nativeValue(isolate, value, exceptionState, NormalConversion);
+ }
+
+ CORE_EXPORT static inline int32_t nativeValue(
+ v8::Isolate* isolate,
+ v8::Local<v8::Value> value,
+ ExceptionState& exceptionState,
+ IntegerConversionConfiguration conversionMode) {
+ return toInt32(isolate, value, conversionMode, exceptionState);
+ }
+};
+
+template <>
+struct NativeValueTraits<IDLUnsignedLong>
+ : public NativeValueTraitsBase<IDLUnsignedLong> {
+ CORE_EXPORT static inline uint32_t nativeValue(
+ v8::Isolate* isolate,
+ v8::Local<v8::Value> value,
+ ExceptionState& exceptionState) {
+ return nativeValue(isolate, value, exceptionState, NormalConversion);
+ }
+
+ CORE_EXPORT static inline uint32_t nativeValue(
+ v8::Isolate* isolate,
+ v8::Local<v8::Value> value,
+ ExceptionState& exceptionState,
+ IntegerConversionConfiguration conversionMode) {
+ return toUInt32(isolate, value, conversionMode, exceptionState);
+ }
+};
+
+template <>
+struct NativeValueTraits<IDLLongLong>
+ : public NativeValueTraitsBase<IDLLongLong> {
+ CORE_EXPORT static inline int64_t nativeValue(
+ v8::Isolate* isolate,
+ v8::Local<v8::Value> value,
+ ExceptionState& exceptionState) {
+ return nativeValue(isolate, value, exceptionState, NormalConversion);
+ }
+
+ CORE_EXPORT static inline int64_t nativeValue(
+ v8::Isolate* isolate,
+ v8::Local<v8::Value> value,
+ ExceptionState& exceptionState,
+ IntegerConversionConfiguration conversionMode) {
+ return toInt64(isolate, value, conversionMode, exceptionState);
+ }
+};
+
+template <>
+struct NativeValueTraits<IDLUnsignedLongLong>
+ : public NativeValueTraitsBase<IDLUnsignedLongLong> {
+ CORE_EXPORT static inline uint64_t nativeValue(
+ v8::Isolate* isolate,
+ v8::Local<v8::Value> value,
+ ExceptionState& exceptionState) {
+ return nativeValue(isolate, value, exceptionState, NormalConversion);
+ }
+
+ CORE_EXPORT static inline uint64_t nativeValue(
+ v8::Isolate* isolate,
+ v8::Local<v8::Value> value,
+ ExceptionState& exceptionState,
+ IntegerConversionConfiguration conversionMode) {
+ return toUInt64(isolate, value, conversionMode, exceptionState);
+ }
+};
+
+// Strings
+template <>
+struct NativeValueTraits<IDLByteString>
+ : public NativeValueTraitsBase<IDLByteString> {
+ CORE_EXPORT static inline String nativeValue(v8::Isolate* isolate,
+ v8::Local<v8::Value> value,
+ ExceptionState& exceptionState) {
+ return toByteString(isolate, value, exceptionState);
+ }
+};
+
+template <>
+struct NativeValueTraits<IDLString> : public NativeValueTraitsBase<IDLString> {
+ CORE_EXPORT static inline String nativeValue(v8::Isolate* isolate,
+ v8::Local<v8::Value> value,
+ ExceptionState& exceptionState) {
+ 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
+ exceptionState);
+ }
+
+ template <V8StringResourceMode Mode = DefaultMode>
+ CORE_EXPORT static inline String nativeValue(v8::Isolate* isolate,
+ v8::Local<v8::Value> value,
+ ExceptionState& exceptionState) {
+ 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.
+ if (!s.prepare(isolate, exceptionState))
+ return String();
+ return s;
+ }
+};
+
+template <>
+struct NativeValueTraits<IDLUSVString>
+ : public NativeValueTraitsBase<IDLUSVString> {
+ CORE_EXPORT static inline String nativeValue(v8::Isolate* isolate,
+ v8::Local<v8::Value> value,
+ ExceptionState& exceptionState) {
+ return toUSVString(isolate, value, exceptionState);
+ }
+};
+
+// Floats and doubles
+template <>
+struct NativeValueTraits<IDLDouble> : public NativeValueTraitsBase<IDLDouble> {
+ CORE_EXPORT static inline double nativeValue(v8::Isolate* isolate,
+ v8::Local<v8::Value> value,
+ ExceptionState& exceptionState) {
+ return toRestrictedDouble(isolate, value, exceptionState);
+ }
+};
+
+template <>
+struct NativeValueTraits<IDLUnrestrictedDouble>
+ : public NativeValueTraitsBase<IDLUnrestrictedDouble> {
+ CORE_EXPORT static inline double nativeValue(v8::Isolate* isolate,
+ v8::Local<v8::Value> value,
+ ExceptionState& exceptionState) {
+ return toDouble(isolate, value, exceptionState);
+ }
+};
+
+template <>
+struct NativeValueTraits<IDLFloat> : public NativeValueTraitsBase<IDLFloat> {
+ CORE_EXPORT static inline float nativeValue(v8::Isolate* isolate,
+ v8::Local<v8::Value> value,
+ ExceptionState& exceptionState) {
+ return toRestrictedFloat(isolate, value, exceptionState);
+ }
+};
+
+template <>
+struct NativeValueTraits<IDLUnrestrictedFloat>
+ : public NativeValueTraitsBase<IDLUnrestrictedFloat> {
+ CORE_EXPORT static inline float nativeValue(v8::Isolate* isolate,
+ v8::Local<v8::Value> value,
+ ExceptionState& exceptionState) {
+ return toFloat(isolate, value, exceptionState);
+ }
+};
+
+// Promises
+template <>
+struct NativeValueTraits<IDLPromise>
+ : public NativeValueTraitsBase<IDLPromise> {
+ CORE_EXPORT static inline ScriptPromise nativeValue(
+ v8::Isolate* isolate,
+ v8::Local<v8::Value> value,
+ ExceptionState& exceptionState) {
+ 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
+ }
+
+ CORE_EXPORT static inline ScriptPromise nativeValue(
+ v8::Isolate* isolate,
+ v8::Local<v8::Value> value) {
+ return ScriptPromise::cast(ScriptState::current(isolate), value);
+ }
+};
+
+// Type-specific overloads
+template <>
+struct NativeValueTraits<IDLDate> : public NativeValueTraitsBase<IDLDate> {
+ CORE_EXPORT static inline double nativeValue(v8::Isolate* isolate,
+ v8::Local<v8::Value> value,
+ ExceptionState& exceptionState) {
+ return toCoreDate(isolate, value, exceptionState);
+ }
+};
+
+template <>
+struct NativeValueTraits<SerializedScriptValue>
+ : public NativeValueTraitsBase<SerializedScriptValue> {
+ CORE_EXPORT static inline PassRefPtr<SerializedScriptValue> nativeValue(
+ v8::Isolate* isolate,
+ v8::Local<v8::Value> value,
+ ExceptionState& exceptionState) {
+ return SerializedScriptValue::serialize(isolate, value, nullptr, nullptr,
+ exceptionState);
+ }
+};
+
+// Generated IDL interfaces.
+template <typename T>
+struct NativeValueTraits<T,
+ typename std::enable_if<std::is_class<
+ typename V8TypeOf<T>::Type>::value>::type>
haraken 2017/03/06 12:56:33 Add a comment about when enble_if ... returns true
+ : public NativeValueTraitsBase<T> {
+ CORE_EXPORT static inline T* nativeValue(v8::Isolate* isolate,
+ v8::Local<v8::Value> value,
+ ExceptionState& exceptionState) {
+ return nativeValue(isolate, value);
+ }
+
+ CORE_EXPORT static inline T* nativeValue(v8::Isolate* isolate,
+ v8::Local<v8::Value> value) {
+ 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
+ }
+};
+
+// Sequences
+template <typename T>
+struct NativeValueTraits<IDLSequence<T>>
+ : public NativeValueTraitsBase<IDLSequence<T>> {
+ // Nondependent types need to be explicitly qualified to be accessible.
+ using typename NativeValueTraitsBase<IDLSequence<T>>::ImplType;
+
+ CORE_EXPORT static inline ImplType nativeValue(
+ v8::Isolate* isolate,
+ v8::Local<v8::Value> value,
+ ExceptionState& exceptionState) {
+ return nativeValue(isolate, value, exceptionState, 0);
+ }
+
+ CORE_EXPORT static inline ImplType nativeValue(v8::Isolate* isolate,
+ v8::Local<v8::Value> value,
+ ExceptionState& exceptionState,
+ int index) {
+ return toImplArray<ImplType, T>(value, index, isolate, exceptionState);
+ }
+};
+
+} // namespace blink
+
+#endif // NativeValueTraitsImpl_h

Powered by Google App Engine
This is Rietveld 408576698