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..351fb39cc9295b1d83d89936001ca989100f59c3 |
| --- /dev/null |
| +++ b/Source/bindings/core/v8/PropertyBag.h |
| @@ -0,0 +1,72 @@ |
| +// 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; |
| + |
| +// PropertyBag is used by IDL dictionary implementations to retrieve native |
| +// values from a V8 object. It is similar to Dictionary(Helper), but its get() |
| +// method returns false when the V8 object doesn't have the given key. |
| +// FIXME: Eliminate duplication between Dictionary(Helper) and PropertyBag. |
| +// When we have enough IDL dictionary support, we should be able to remove |
| +// Dictionary(Helper). |
| +class PropertyBag { |
| + WTF_MAKE_NONCOPYABLE(PropertyBag); |
| +public: |
| + PropertyBag(v8::Isolate* isolate, const v8::Handle<v8::Object>& object) |
| + : m_isolate(isolate) |
| + , m_object(object) |
| + { |
| + ASSERT(!m_object.IsEmpty()); |
| + } |
| + |
| + template <typename T> |
| + bool get(const String& key, T& value) |
| + { |
| + v8::Handle<v8::String> v8Key = v8String(m_isolate, key); |
| + v8::Local<v8::Value> v8Value = m_object->Get(v8Key); |
| + if (v8Value.IsEmpty() || isUndefinedOrNull(v8Value)) { |
|
haraken
2014/08/29 05:44:05
Just to confirm: I understand you want to return f
|
| + return false; |
| + } |
| + return getInternal(v8Value, value); |
| + } |
| + |
| +private: |
| + bool getInternal(v8::Handle<v8::Value>& v8Value, String& value); |
|
haraken
2014/08/29 05:44:04
Drop |v8Value| and |value|.
|
| + bool getInternal(v8::Handle<v8::Value>& v8Value, int& value); |
| + bool getInternal(v8::Handle<v8::Value>& v8Value, bool& value); |
| + bool getInternal(v8::Handle<v8::Value>& v8Value, double& value); |
| + |
| + template <template <typename> class PointerType, typename T> |
| + bool getInternal(v8::Handle<v8::Value>& v8Value, PointerType<T>& value) |
| + { |
| + value = PropertyBagTraits<T>::type::toNativeWithTypeCheck(m_isolate, v8Value); |
| + return value; |
| + } |
| + |
| + template <typename T> |
| + bool getInternal(v8::Handle<v8::Value>& v8Value, Vector<T>& value) |
| + { |
| + if (!v8Value->IsArray()) |
| + return false; |
| + // FIXME: Check types of each value |
| + value = toNativeArray<T>(v8Value, 0, m_isolate); |
| + return true; |
| + } |
| + |
| + v8::Isolate* m_isolate; |
| + v8::Handle<v8::Object> m_object; |
| +}; |
| + |
| +} // namespace blink |
| + |
| +#endif // PropertyBag_h |