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

Side by Side Diff: third_party/WebKit/Source/bindings/core/v8/Dictionary.h

Issue 2509713002: binding: Makes Dictionary throw an exception (constructor). (Closed)
Patch Set: Updated text expectations. Created 4 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 unified diff | Download patch
OLDNEW
1 /* 1 /*
2 * Copyright (C) 2010 Google Inc. All rights reserved. 2 * Copyright (C) 2010 Google Inc. All rights reserved.
3 * 3 *
4 * Redistribution and use in source and binary forms, with or without 4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions 5 * modification, are permitted provided that the following conditions
6 * are met: 6 * are met:
7 * 7 *
8 * 1. Redistributions of source code must retain the above copyright 8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer. 9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright 10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the 11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution. 12 * documentation and/or other materials provided with the distribution.
13 * 13 *
14 * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY 14 * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY
15 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 15 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
16 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 16 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
17 * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY 17 * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY
18 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 18 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
19 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 19 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
20 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 20 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
21 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 21 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
23 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 23 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24 */ 24 */
25 25
26 #ifndef Dictionary_h 26 #ifndef Dictionary_h
27 #define Dictionary_h 27 #define Dictionary_h
28 28
29 #include "bindings/core/v8/DictionaryIterator.h" 29 #include "bindings/core/v8/DictionaryIterator.h"
30 #include "bindings/core/v8/ExceptionMessages.h"
31 #include "bindings/core/v8/Nullable.h" 30 #include "bindings/core/v8/Nullable.h"
32 #include "bindings/core/v8/V8Binding.h" 31 #include "bindings/core/v8/V8Binding.h"
33 #include "core/CoreExport.h" 32 #include "core/CoreExport.h"
34 #include "wtf/HashMap.h" 33 #include "wtf/HashMap.h"
35 #include "wtf/Vector.h" 34 #include "wtf/Vector.h"
36 #include "wtf/text/StringView.h" 35 #include "wtf/text/StringView.h"
37 #include <v8.h> 36 #include <v8.h>
38 37
39 namespace blink { 38 namespace blink {
40 39
41 class ExceptionState; 40 class ExceptionState;
42 class ExecutionContext; 41 class ExecutionContext;
43 42
44 // Dictionary class provides ways to retrieve property values as C++ objects 43 // Dictionary class provides ways to retrieve property values as C++ objects
45 // from a V8 object. Instances of this class must not outlive V8's handle scope 44 // from a V8 object. Instances of this class must not outlive V8's handle scope
46 // because they hold a V8 value without putting it on persistent handles. 45 // because they hold a V8 value without putting it on persistent handles.
47 class CORE_EXPORT Dictionary final { 46 class CORE_EXPORT Dictionary final {
48 DISALLOW_NEW_EXCEPT_PLACEMENT_NEW(); 47 DISALLOW_NEW_EXCEPT_PLACEMENT_NEW();
49 48
50 public: 49 public:
51 Dictionary() : m_isolate(nullptr) {} 50 Dictionary() : m_isolate(nullptr) {}
52 Dictionary(v8::Isolate* isolate, const v8::Local<v8::Value>& options) 51 Dictionary(v8::Isolate*,
53 : m_options(options), m_isolate(isolate) { 52 v8::Local<v8::Value> dictionaryObject,
54 DCHECK(m_isolate); 53 ExceptionState&);
54
55 Dictionary& operator=(const Dictionary&) = default;
bashi 2016/11/18 15:03:55 Just curious, why do we need this explicit operato
Yuki 2016/11/21 12:40:27 Technically no need. But I think having "= defaul
56
57 bool isObject() const { return !m_dictionaryObject.IsEmpty(); }
58 bool isUndefinedOrNull() const { return !isObject(); }
59
60 v8::Local<v8::Value> v8Value() const {
61 if (!m_isolate)
62 return v8::Local<v8::Value>();
63 switch (m_valueType) {
64 case ValueType::Undefined:
65 return v8::Undefined(m_isolate);
66 case ValueType::Null:
67 return v8::Null(m_isolate);
68 case ValueType::Object:
69 return m_dictionaryObject;
70 default:
71 NOTREACHED();
72 return v8::Local<v8::Value>();
73 }
55 } 74 }
56 Dictionary(const v8::Local<v8::Value>& options,
57 v8::Isolate* isolate,
58 ExceptionState&) // DEPRECATED
59 : Dictionary(isolate, options) {}
60 75
61 Dictionary& operator=(const Dictionary&); 76 bool get(const StringView& key, v8::Local<v8::Value>& value) const {
62 77 return m_isolate && getInternal(v8String(m_isolate, key), value);
63 bool isObject() const; 78 }
64 bool isUndefinedOrNull() const; 79 bool get(const StringView& key, Dictionary&) const;
65
66 bool get(const StringView&, Dictionary&) const;
67 bool get(const StringView&, v8::Local<v8::Value>&) const;
68
69 v8::Local<v8::Value> v8Value() const { return m_options; }
70 80
71 bool getOwnPropertiesAsStringHashMap(HashMap<String, String>&) const; 81 bool getOwnPropertiesAsStringHashMap(HashMap<String, String>&) const;
72 bool getPropertyNames(Vector<String>&) const; 82 bool getPropertyNames(Vector<String>&) const;
73 83
74 bool hasProperty(const StringView&) const; 84 bool hasProperty(const StringView&) const;
75 85
76 v8::Isolate* isolate() const { return m_isolate; } 86 v8::Isolate* isolate() const { return m_isolate; }
77 v8::Local<v8::Context> v8Context() const { 87 v8::Local<v8::Context> v8Context() const {
78 ASSERT(m_isolate); 88 ASSERT(m_isolate);
79 return m_isolate->GetCurrentContext(); 89 return m_isolate->GetCurrentContext();
80 } 90 }
81 91
82 DictionaryIterator getIterator(ExecutionContext*) const; 92 DictionaryIterator getIterator(ExecutionContext*) const;
83 93
84 private: 94 private:
85 bool getInternal(const v8::Local<v8::Value>& key, 95 bool getInternal(const v8::Local<v8::Value>& key,
86 v8::Local<v8::Value>& result) const; 96 v8::Local<v8::Value>& result) const;
87 bool toObject(v8::Local<v8::Object>&) const;
88 97
89 v8::Local<v8::Value> m_options;
90 v8::Isolate* m_isolate; 98 v8::Isolate* m_isolate;
99 // Undefined, Null, or Object is allowed as type of dictionary.
100 enum class ValueType {
101 Undefined,
102 Null,
103 Object
104 } m_valueType = ValueType::Undefined;
105 v8::Local<v8::Object> m_dictionaryObject; // an Object or empty
91 }; 106 };
92 107
93 template <> 108 template <>
94 struct NativeValueTraits<Dictionary> { 109 struct NativeValueTraits<Dictionary> {
95 static inline Dictionary nativeValue(v8::Isolate* isolate, 110 static Dictionary nativeValue(v8::Isolate* isolate,
96 v8::Local<v8::Value> value, 111 v8::Local<v8::Value> value,
97 ExceptionState&) { 112 ExceptionState& exceptionState) {
98 return Dictionary(isolate, value); 113 return Dictionary(isolate, value, exceptionState);
99 } 114 }
100 }; 115 };
101 116
102 // DictionaryHelper is a collection of static methods for getting or 117 // DictionaryHelper is a collection of static methods for getting or
103 // converting a value from Dictionary. 118 // converting a value from Dictionary.
104 struct DictionaryHelper { 119 struct DictionaryHelper {
105 STATIC_ONLY(DictionaryHelper); 120 STATIC_ONLY(DictionaryHelper);
106 template <typename T> 121 template <typename T>
107 static bool get(const Dictionary&, const StringView& key, T& value); 122 static bool get(const Dictionary&, const StringView& key, T& value);
108 template <typename T> 123 template <typename T>
(...skipping 19 matching lines...) Expand all
128 static bool get(const Dictionary&, 143 static bool get(const Dictionary&,
129 const StringView& key, 144 const StringView& key,
130 PointerType<T>& value); 145 PointerType<T>& value);
131 template <typename T> 146 template <typename T>
132 static bool get(const Dictionary&, const StringView& key, Nullable<T>& value); 147 static bool get(const Dictionary&, const StringView& key, Nullable<T>& value);
133 }; 148 };
134 149
135 } // namespace blink 150 } // namespace blink
136 151
137 #endif // Dictionary_h 152 #endif // Dictionary_h
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698