Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright 2014 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 PropertyBag_h | |
| 6 #define PropertyBag_h | |
| 7 | |
| 8 #include "bindings/core/v8/V8Binding.h" | |
| 9 #include "wtf/Noncopyable.h" | |
| 10 #include <v8.h> | |
| 11 | |
| 12 namespace blink { | |
| 13 | |
| 14 template <typename T> struct PropertyBagTraits; | |
| 15 | |
| 16 template<> | |
| 17 struct PropertyBagTraits<String> { | |
| 18 static inline bool get(v8::Handle<v8::Value>& v8Value, String& value, v8::Is olate* isolate) | |
| 19 { | |
| 20 TOSTRING_DEFAULT(V8StringResource<>, stringValue, v8Value, false); | |
|
haraken
2014/08/27 16:07:43
This can throw an exception, but is it OK? The sam
bashi
2014/08/29 05:21:17
Dictionary(Helper) doesn't check exceptions so I t
haraken
2014/08/29 05:44:04
I think it's OK to throw an exception here. It's a
| |
| 21 value = stringValue; | |
| 22 return true; | |
| 23 } | |
| 24 }; | |
| 25 | |
| 26 template<> | |
| 27 struct PropertyBagTraits<int> { | |
|
bashi
2014/08/27 12:01:34
We would need to add specialized traits for all pr
| |
| 28 static inline bool get(v8::Handle<v8::Value>& v8Value, int& value, v8::Isola te* isolate) | |
| 29 { | |
| 30 v8::Local<v8::Int32> v8Int32 = v8Value->ToInt32(); | |
| 31 if (v8Int32.IsEmpty()) | |
| 32 return false; | |
| 33 value = toInt32(v8Int32); | |
| 34 return true; | |
| 35 } | |
| 36 }; | |
| 37 | |
| 38 template<> | |
| 39 struct PropertyBagTraits<bool> { | |
| 40 static inline bool get(v8::Handle<v8::Value>& v8Value, bool& value, v8::Isol ate* isolate) | |
| 41 { | |
| 42 v8::Local<v8::Boolean> v8Boolean = v8Value->ToBoolean(); | |
| 43 if (v8Boolean.IsEmpty()) | |
| 44 return false; | |
| 45 value = v8Boolean->Value(); | |
| 46 return true; | |
| 47 } | |
| 48 }; | |
| 49 | |
| 50 template<> | |
| 51 struct PropertyBagTraits<double> { | |
| 52 static inline bool get(v8::Handle<v8::Value>& v8Value, double& value, v8::Is olate* isolate) | |
| 53 { | |
| 54 v8::Local<v8::Number> v8Number = v8Value->ToNumber(); | |
| 55 if (v8Number.IsEmpty()) | |
| 56 return false; | |
| 57 value = v8Number->Value(); | |
| 58 return true; | |
| 59 } | |
| 60 }; | |
| 61 | |
| 62 template<typename T> | |
| 63 struct PropertyBagTraits<Vector<T> > { | |
| 64 static inline bool get(v8::Handle<v8::Value>& v8Value, Vector<T>& value, v8: :Isolate* isolate) | |
| 65 { | |
| 66 if (!v8Value->IsArray()) | |
| 67 return false; | |
| 68 // FIXME: Check types of each value | |
| 69 value = toNativeArray<T>(v8Value, 0, isolate); | |
| 70 return true; | |
| 71 } | |
| 72 }; | |
| 73 | |
| 74 class PropertyBag { | |
|
haraken
2014/08/27 16:07:43
Add a comment about the relationship between Dicti
bashi
2014/08/29 05:21:17
Done.
| |
| 75 WTF_MAKE_NONCOPYABLE(PropertyBag); | |
| 76 public: | |
| 77 PropertyBag(const v8::Handle<v8::Object>& object, v8::Isolate* isolate) | |
|
haraken
2014/08/27 16:07:43
Put the Isolate parameter first. (Putting the Isol
bashi
2014/08/29 05:21:17
Done.
| |
| 78 : m_object(object) | |
| 79 , m_isolate(isolate) | |
| 80 { | |
| 81 } | |
| 82 | |
| 83 template <typename T> | |
| 84 bool get(const String& key, T& value) | |
| 85 { | |
|
haraken
2014/08/27 16:07:43
Add an m_object.IsEmpty() check or ASSERT(!m_objec
bashi
2014/08/29 05:21:16
Done.
| |
| 86 v8::Handle<v8::String> v8Key = v8String(m_isolate, key); | |
| 87 if (!m_object->Has(v8Key)) { | |
| 88 return false; | |
| 89 } | |
| 90 v8::Local<v8::Value> v8Value = m_object->Get(v8Key); | |
|
haraken
2014/08/27 16:07:43
Do we need to issue both Has and Get? Can't we kno
bashi
2014/08/29 05:21:17
Done.
| |
| 91 if (isUndefinedOrNull(v8Value)) { | |
| 92 return false; | |
| 93 } | |
| 94 return PropertyBagTraits<T>::get(v8Value, value, m_isolate); | |
| 95 } | |
| 96 | |
| 97 private: | |
| 98 v8::Handle<v8::Object> m_object; | |
| 99 v8::Isolate* m_isolate; | |
| 100 }; | |
| 101 | |
| 102 } // namespace blink | |
| 103 | |
| 104 #endif // PropertyBag_h | |
| OLD | NEW |