Chromium Code Reviews| Index: Source/bindings/core/v8/PropertyBag.h |
| diff --git a/Source/bindings/core/v8/PropertyBag.h b/Source/bindings/core/v8/PropertyBag.h |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..fae96ccb347849e29a37b3c6c2a86bd7a00e0743 |
| --- /dev/null |
| +++ b/Source/bindings/core/v8/PropertyBag.h |
| @@ -0,0 +1,104 @@ |
| +// Copyright 2014 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 PropertyBag_h |
| +#define PropertyBag_h |
| + |
| +#include "bindings/core/v8/V8Binding.h" |
| +#include "wtf/Noncopyable.h" |
| +#include <v8.h> |
| + |
| +namespace blink { |
| + |
| +template <typename T> struct PropertyBagTraits; |
| + |
| +template<> |
| +struct PropertyBagTraits<String> { |
| + static inline bool get(v8::Handle<v8::Value>& v8Value, String& value, v8::Isolate* isolate) |
| + { |
| + 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
|
| + value = stringValue; |
| + return true; |
| + } |
| +}; |
| + |
| +template<> |
| +struct PropertyBagTraits<int> { |
|
bashi
2014/08/27 12:01:34
We would need to add specialized traits for all pr
|
| + static inline bool get(v8::Handle<v8::Value>& v8Value, int& value, v8::Isolate* isolate) |
| + { |
| + v8::Local<v8::Int32> v8Int32 = v8Value->ToInt32(); |
| + if (v8Int32.IsEmpty()) |
| + return false; |
| + value = toInt32(v8Int32); |
| + return true; |
| + } |
| +}; |
| + |
| +template<> |
| +struct PropertyBagTraits<bool> { |
| + static inline bool get(v8::Handle<v8::Value>& v8Value, bool& value, v8::Isolate* isolate) |
| + { |
| + v8::Local<v8::Boolean> v8Boolean = v8Value->ToBoolean(); |
| + if (v8Boolean.IsEmpty()) |
| + return false; |
| + value = v8Boolean->Value(); |
| + return true; |
| + } |
| +}; |
| + |
| +template<> |
| +struct PropertyBagTraits<double> { |
| + static inline bool get(v8::Handle<v8::Value>& v8Value, double& value, v8::Isolate* isolate) |
| + { |
| + v8::Local<v8::Number> v8Number = v8Value->ToNumber(); |
| + if (v8Number.IsEmpty()) |
| + return false; |
| + value = v8Number->Value(); |
| + return true; |
| + } |
| +}; |
| + |
| +template<typename T> |
| +struct PropertyBagTraits<Vector<T> > { |
| + static inline bool get(v8::Handle<v8::Value>& v8Value, Vector<T>& value, v8::Isolate* isolate) |
| + { |
| + if (!v8Value->IsArray()) |
| + return false; |
| + // FIXME: Check types of each value |
| + value = toNativeArray<T>(v8Value, 0, isolate); |
| + return true; |
| + } |
| +}; |
| + |
| +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.
|
| + WTF_MAKE_NONCOPYABLE(PropertyBag); |
| +public: |
| + 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.
|
| + : m_object(object) |
| + , m_isolate(isolate) |
| + { |
| + } |
| + |
| + template <typename T> |
| + bool get(const String& key, T& value) |
| + { |
|
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.
|
| + v8::Handle<v8::String> v8Key = v8String(m_isolate, key); |
| + if (!m_object->Has(v8Key)) { |
| + return false; |
| + } |
| + 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.
|
| + if (isUndefinedOrNull(v8Value)) { |
| + return false; |
| + } |
| + return PropertyBagTraits<T>::get(v8Value, value, m_isolate); |
| + } |
| + |
| +private: |
| + v8::Handle<v8::Object> m_object; |
| + v8::Isolate* m_isolate; |
| +}; |
| + |
| +} // namespace blink |
| + |
| +#endif // PropertyBag_h |