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

Unified Diff: Source/bindings/v8/Dictionary.h

Issue 85263002: Improve handling of dictionary conversions. (Closed) Base URL: https://chromium.googlesource.com/chromium/blink.git@master
Patch Set: Created 7 years, 1 month 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: Source/bindings/v8/Dictionary.h
diff --git a/Source/bindings/v8/Dictionary.h b/Source/bindings/v8/Dictionary.h
index 2d50a037606f3f40115eaa924e05a331849f3a67..ecf0abf6575a94107b9c4d035ee4f2b63e0742d0 100644
--- a/Source/bindings/v8/Dictionary.h
+++ b/Source/bindings/v8/Dictionary.h
@@ -26,7 +26,11 @@
#ifndef Dictionary_h
#define Dictionary_h
+#include "bindings/v8/ExceptionMessages.h"
+#include "bindings/v8/ExceptionState.h"
#include "bindings/v8/ScriptValue.h"
+#include "bindings/v8/V8Binding.h"
+#include "bindings/v8/V8BindingMacros.h"
#include "core/events/EventListener.h"
#include "core/dom/MessagePort.h"
#include <v8.h>
@@ -95,11 +99,28 @@ public:
bool get(const String&, RefPtr<VoidCallback>&) const;
bool get(const String&, v8::Local<v8::Value>&) const;
+ bool convert(const String&, bool&, ExceptionState&) const;
sof 2013/11/25 08:21:33 An alternate design that I played with was to have
+ bool convert(const String&, double&, ExceptionState&) const;
+ bool convert(const String&, String&, ExceptionState&) const;
+ bool convert(const String&, ScriptValue&, ExceptionState&) const;
+
+ template<typename T>
+ bool convert(const String&, enum IntegerConversionConfiguration, T&, ExceptionState&) const;
+ bool convert(const String&, const String&, bool, MessagePortArray&, ExceptionState&) const;
+ bool convert(const String&, const String&, bool, HashSet<AtomicString>&, ExceptionState&) const;
+ bool convert(const String&, const String&, bool, Dictionary&, ExceptionState&) const;
+ bool convert(const String&, const String&, bool, Vector<String>&, ExceptionState&) const;
+ bool convert(const String&, const String&, bool, ArrayValue&, ExceptionState&) const;
+ template<typename T>
+ bool convert(const String&, const String&, bool, RefPtr<T>&, ExceptionState&) const;
+
bool getOwnPropertiesAsStringHashMap(HashMap<String, String>&) const;
bool getOwnPropertyNames(Vector<String>&) const;
bool getWithUndefinedOrNullCheck(const String&, String&) const;
+ bool hasProperty(const String&) const;
+
// Only allow inline allocation.
void* operator new(size_t, NotNullTag, void* location) { return location; }
@@ -113,6 +134,145 @@ private:
v8::Isolate* m_isolate;
};
+template<>
+struct NativeValueTraits<Dictionary> {
+ static inline Dictionary nativeValue(const v8::Handle<v8::Value>& value, v8::Isolate* isolate)
+ {
+ return Dictionary(value, isolate);
+ }
+};
+
+template <typename T>
+struct IntegralTypeTraits {
+};
+
+template <>
+struct IntegralTypeTraits<uint8_t> {
+ static inline uint8_t toIntegral(v8::Handle<v8::Value> value, IntegerConversionConfiguration configuration, bool& ok)
+ {
+ return toUInt8(value, configuration, ok);
+ }
+ static const String typeName() { return "UInt8"; }
+};
+
+template <>
+struct IntegralTypeTraits<int8_t> {
+ static inline int8_t toIntegral(v8::Handle<v8::Value> value, IntegerConversionConfiguration configuration, bool& ok)
+ {
+ return toInt8(value, configuration, ok);
+ }
+ static const String typeName() { return "Int8"; }
+};
+
+template <>
+struct IntegralTypeTraits<unsigned short> {
+ static inline uint16_t toIntegral(v8::Handle<v8::Value> value, IntegerConversionConfiguration configuration, bool& ok)
+ {
+ return toUInt16(value, configuration, ok);
+ }
+ static const String typeName() { return "UInt16"; }
+};
+
+template <>
+struct IntegralTypeTraits<short> {
+ static inline int16_t toIntegral(v8::Handle<v8::Value> value, IntegerConversionConfiguration configuration, bool& ok)
+ {
+ return toInt16(value, configuration, ok);
+ }
+ static const String typeName() { return "Int16"; }
+};
+
+template <>
+struct IntegralTypeTraits<unsigned> {
+ static inline uint32_t toIntegral(v8::Handle<v8::Value> value, IntegerConversionConfiguration configuration, bool& ok)
+ {
+ return toUInt32(value, configuration, ok);
+ }
+ static const String typeName() { return "UInt32"; }
+};
+
+template <>
+struct IntegralTypeTraits<unsigned long> {
+ static inline uint32_t toIntegral(v8::Handle<v8::Value> value, IntegerConversionConfiguration configuration, bool& ok)
+ {
+ return toUInt32(value, configuration, ok);
+ }
+ static const String typeName() { return "UInt32"; }
+};
+
+template <>
+struct IntegralTypeTraits<int> {
+ static inline int32_t toIntegral(v8::Handle<v8::Value> value, IntegerConversionConfiguration configuration, bool& ok)
+ {
+ return toInt32(value, configuration, ok);
+ }
+ static const String typeName() { return "Int32"; }
+};
+
+template <>
+struct IntegralTypeTraits<long> {
+ static inline int32_t toIntegral(v8::Handle<v8::Value> value, IntegerConversionConfiguration configuration, bool& ok)
+ {
+ return toInt32(value, configuration, ok);
+ }
+ static const String typeName() { return "Int32"; }
+};
+
+template <>
+struct IntegralTypeTraits<unsigned long long> {
+ static inline unsigned long long toIntegral(v8::Handle<v8::Value> value, IntegerConversionConfiguration configuration, bool& ok)
+ {
+ return toUInt64(value, configuration, ok);
+ }
+ static const String typeName() { return "UInt64"; }
+};
+
+template <>
+struct IntegralTypeTraits<long long> {
+ static inline long long toIntegral(v8::Handle<v8::Value> value, IntegerConversionConfiguration configuration, bool& ok)
+ {
+ return toInt64(value, configuration, ok);
+ }
+ static const String typeName() { return "Int64"; }
+};
+
+template<typename T> bool Dictionary::convert(const String& key, IntegerConversionConfiguration configuration, T& value, ExceptionState& exceptionState) const
+{
+ v8::Local<v8::Value> v8Value;
+ if (!getKey(key, v8Value))
+ return true;
+
+ bool ok = false;
+ value = IntegralTypeTraits<T>::toIntegral(v8Value, configuration, ok);
+ if (!ok) {
+ V8TRYCATCH_RETURN(v8::Local<v8::Number>, v8Number, v8Value->ToNumber(), false);
+ if (v8Number.IsEmpty()) {
+ exceptionState.throwTypeError(ExceptionMessages::illTypedProperty(key, "does not have type " + IntegralTypeTraits<T>::typeName() + "."));
+ } else {
+ ASSERT(configuration == EnforceRange);
+ exceptionState.throwTypeError(ExceptionMessages::illTypedProperty(key, "is not a finite number."));
+ }
+ return false;
+ }
+ return true;
+}
+
+template<typename T> bool Dictionary::convert(const String& key, const String& typeName, bool isNullable, RefPtr<T>& value, ExceptionState& exceptionState) const
+{
+ if (!get(key, value))
+ return true;
+
+ if (!value) {
+ v8::Local<v8::Value> v8Value;
+ getKey(key, v8Value);
+ if (!(isNullable && WebCore::isUndefinedOrNull(v8Value))) {
+ exceptionState.throwTypeError(ExceptionMessages::illTypedProperty(key, "does not have " + (typeName.isEmpty() ? String("the expected type.") : ("a " + typeName + " type."))));
+ return false;
+ }
+ }
+ return true;
+}
+
}
#endif // Dictionary_h

Powered by Google App Engine
This is Rietveld 408576698