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

Side by Side 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 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/v8/ExceptionMessages.h"
30 #include "bindings/v8/ExceptionState.h"
29 #include "bindings/v8/ScriptValue.h" 31 #include "bindings/v8/ScriptValue.h"
32 #include "bindings/v8/V8Binding.h"
33 #include "bindings/v8/V8BindingMacros.h"
30 #include "core/events/EventListener.h" 34 #include "core/events/EventListener.h"
31 #include "core/dom/MessagePort.h" 35 #include "core/dom/MessagePort.h"
32 #include <v8.h> 36 #include <v8.h>
33 #include "wtf/HashMap.h" 37 #include "wtf/HashMap.h"
34 #include "wtf/HashSet.h" 38 #include "wtf/HashSet.h"
35 #include "wtf/Vector.h" 39 #include "wtf/Vector.h"
36 #include "wtf/text/AtomicString.h" 40 #include "wtf/text/AtomicString.h"
37 #include "wtf/text/WTFString.h" 41 #include "wtf/text/WTFString.h"
38 42
39 namespace WebCore { 43 namespace WebCore {
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after
88 bool get(const String&, RefPtr<MediaStream>&) const; 92 bool get(const String&, RefPtr<MediaStream>&) const;
89 bool get(const String&, RefPtr<EventTarget>&) const; 93 bool get(const String&, RefPtr<EventTarget>&) const;
90 bool get(const String&, HashSet<AtomicString>&) const; 94 bool get(const String&, HashSet<AtomicString>&) const;
91 bool get(const String&, Dictionary&) const; 95 bool get(const String&, Dictionary&) const;
92 bool get(const String&, Vector<String>&) const; 96 bool get(const String&, Vector<String>&) const;
93 bool get(const String&, ArrayValue&) const; 97 bool get(const String&, ArrayValue&) const;
94 bool get(const String&, RefPtr<DOMError>&) const; 98 bool get(const String&, RefPtr<DOMError>&) const;
95 bool get(const String&, RefPtr<VoidCallback>&) const; 99 bool get(const String&, RefPtr<VoidCallback>&) const;
96 bool get(const String&, v8::Local<v8::Value>&) const; 100 bool get(const String&, v8::Local<v8::Value>&) const;
97 101
102 bool convert(const String&, bool&, ExceptionState&) const;
sof 2013/11/25 08:21:33 An alternate design that I played with was to have
103 bool convert(const String&, double&, ExceptionState&) const;
104 bool convert(const String&, String&, ExceptionState&) const;
105 bool convert(const String&, ScriptValue&, ExceptionState&) const;
106
107 template<typename T>
108 bool convert(const String&, enum IntegerConversionConfiguration, T&, Excepti onState&) const;
109 bool convert(const String&, const String&, bool, MessagePortArray&, Exceptio nState&) const;
110 bool convert(const String&, const String&, bool, HashSet<AtomicString>&, Exc eptionState&) const;
111 bool convert(const String&, const String&, bool, Dictionary&, ExceptionState &) const;
112 bool convert(const String&, const String&, bool, Vector<String>&, ExceptionS tate&) const;
113 bool convert(const String&, const String&, bool, ArrayValue&, ExceptionState &) const;
114 template<typename T>
115 bool convert(const String&, const String&, bool, RefPtr<T>&, ExceptionState& ) const;
116
98 bool getOwnPropertiesAsStringHashMap(HashMap<String, String>&) const; 117 bool getOwnPropertiesAsStringHashMap(HashMap<String, String>&) const;
99 bool getOwnPropertyNames(Vector<String>&) const; 118 bool getOwnPropertyNames(Vector<String>&) const;
100 119
101 bool getWithUndefinedOrNullCheck(const String&, String&) const; 120 bool getWithUndefinedOrNullCheck(const String&, String&) const;
102 121
122 bool hasProperty(const String&) const;
123
103 // Only allow inline allocation. 124 // Only allow inline allocation.
104 void* operator new(size_t, NotNullTag, void* location) { return location; } 125 void* operator new(size_t, NotNullTag, void* location) { return location; }
105 126
106 private: 127 private:
107 // Disallow new allocation. 128 // Disallow new allocation.
108 void* operator new(size_t); 129 void* operator new(size_t);
109 130
110 bool getKey(const String& key, v8::Local<v8::Value>&) const; 131 bool getKey(const String& key, v8::Local<v8::Value>&) const;
111 132
112 v8::Handle<v8::Value> m_options; 133 v8::Handle<v8::Value> m_options;
113 v8::Isolate* m_isolate; 134 v8::Isolate* m_isolate;
114 }; 135 };
115 136
137 template<>
138 struct NativeValueTraits<Dictionary> {
139 static inline Dictionary nativeValue(const v8::Handle<v8::Value>& value, v8: :Isolate* isolate)
140 {
141 return Dictionary(value, isolate);
142 }
143 };
144
145 template <typename T>
146 struct IntegralTypeTraits {
147 };
148
149 template <>
150 struct IntegralTypeTraits<uint8_t> {
151 static inline uint8_t toIntegral(v8::Handle<v8::Value> value, IntegerConvers ionConfiguration configuration, bool& ok)
152 {
153 return toUInt8(value, configuration, ok);
154 }
155 static const String typeName() { return "UInt8"; }
156 };
157
158 template <>
159 struct IntegralTypeTraits<int8_t> {
160 static inline int8_t toIntegral(v8::Handle<v8::Value> value, IntegerConversi onConfiguration configuration, bool& ok)
161 {
162 return toInt8(value, configuration, ok);
163 }
164 static const String typeName() { return "Int8"; }
165 };
166
167 template <>
168 struct IntegralTypeTraits<unsigned short> {
169 static inline uint16_t toIntegral(v8::Handle<v8::Value> value, IntegerConver sionConfiguration configuration, bool& ok)
170 {
171 return toUInt16(value, configuration, ok);
172 }
173 static const String typeName() { return "UInt16"; }
174 };
175
176 template <>
177 struct IntegralTypeTraits<short> {
178 static inline int16_t toIntegral(v8::Handle<v8::Value> value, IntegerConvers ionConfiguration configuration, bool& ok)
179 {
180 return toInt16(value, configuration, ok);
181 }
182 static const String typeName() { return "Int16"; }
183 };
184
185 template <>
186 struct IntegralTypeTraits<unsigned> {
187 static inline uint32_t toIntegral(v8::Handle<v8::Value> value, IntegerConver sionConfiguration configuration, bool& ok)
188 {
189 return toUInt32(value, configuration, ok);
190 }
191 static const String typeName() { return "UInt32"; }
192 };
193
194 template <>
195 struct IntegralTypeTraits<unsigned long> {
196 static inline uint32_t toIntegral(v8::Handle<v8::Value> value, IntegerConver sionConfiguration configuration, bool& ok)
197 {
198 return toUInt32(value, configuration, ok);
199 }
200 static const String typeName() { return "UInt32"; }
201 };
202
203 template <>
204 struct IntegralTypeTraits<int> {
205 static inline int32_t toIntegral(v8::Handle<v8::Value> value, IntegerConvers ionConfiguration configuration, bool& ok)
206 {
207 return toInt32(value, configuration, ok);
208 }
209 static const String typeName() { return "Int32"; }
210 };
211
212 template <>
213 struct IntegralTypeTraits<long> {
214 static inline int32_t toIntegral(v8::Handle<v8::Value> value, IntegerConvers ionConfiguration configuration, bool& ok)
215 {
216 return toInt32(value, configuration, ok);
217 }
218 static const String typeName() { return "Int32"; }
219 };
220
221 template <>
222 struct IntegralTypeTraits<unsigned long long> {
223 static inline unsigned long long toIntegral(v8::Handle<v8::Value> value, Int egerConversionConfiguration configuration, bool& ok)
224 {
225 return toUInt64(value, configuration, ok);
226 }
227 static const String typeName() { return "UInt64"; }
228 };
229
230 template <>
231 struct IntegralTypeTraits<long long> {
232 static inline long long toIntegral(v8::Handle<v8::Value> value, IntegerConve rsionConfiguration configuration, bool& ok)
233 {
234 return toInt64(value, configuration, ok);
235 }
236 static const String typeName() { return "Int64"; }
237 };
238
239 template<typename T> bool Dictionary::convert(const String& key, IntegerConversi onConfiguration configuration, T& value, ExceptionState& exceptionState) const
240 {
241 v8::Local<v8::Value> v8Value;
242 if (!getKey(key, v8Value))
243 return true;
244
245 bool ok = false;
246 value = IntegralTypeTraits<T>::toIntegral(v8Value, configuration, ok);
247 if (!ok) {
248 V8TRYCATCH_RETURN(v8::Local<v8::Number>, v8Number, v8Value->ToNumber(), false);
249 if (v8Number.IsEmpty()) {
250 exceptionState.throwTypeError(ExceptionMessages::illTypedProperty(ke y, "does not have type " + IntegralTypeTraits<T>::typeName() + "."));
251 } else {
252 ASSERT(configuration == EnforceRange);
253 exceptionState.throwTypeError(ExceptionMessages::illTypedProperty(ke y, "is not a finite number."));
254 }
255 return false;
256 }
257 return true;
258 }
259
260 template<typename T> bool Dictionary::convert(const String& key, const String& t ypeName, bool isNullable, RefPtr<T>& value, ExceptionState& exceptionState) cons t
261 {
262 if (!get(key, value))
263 return true;
264
265 if (!value) {
266 v8::Local<v8::Value> v8Value;
267 getKey(key, v8Value);
268 if (!(isNullable && WebCore::isUndefinedOrNull(v8Value))) {
269 exceptionState.throwTypeError(ExceptionMessages::illTypedProperty(ke y, "does not have " + (typeName.isEmpty() ? String("the expected type.") : ("a " + typeName + " type."))));
270 return false;
271 }
272 }
273 return true;
274 }
275
116 } 276 }
117 277
118 #endif // Dictionary_h 278 #endif // Dictionary_h
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698