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

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

Issue 373423002: Split Dictionary's get and convert into DictionaryHelper. (Closed) Base URL: svn://svn.chromium.org/blink/trunk
Patch Set: Created 6 years, 5 months 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 | Annotate | Revision Log
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
(...skipping 25 matching lines...) Expand all
36 #include "core/events/EventListener.h" 36 #include "core/events/EventListener.h"
37 #include "wtf/HashMap.h" 37 #include "wtf/HashMap.h"
38 #include "wtf/HashSet.h" 38 #include "wtf/HashSet.h"
39 #include "wtf/Vector.h" 39 #include "wtf/Vector.h"
40 #include "wtf/text/AtomicString.h" 40 #include "wtf/text/AtomicString.h"
41 #include "wtf/text/WTFString.h" 41 #include "wtf/text/WTFString.h"
42 #include <v8.h> 42 #include <v8.h>
43 43
44 namespace WebCore { 44 namespace WebCore {
45 45
46 class ArrayValue;
47 class DOMError;
48 class Element; 46 class Element;
49 class LocalDOMWindow;
50 class Gamepad;
51 class MediaStream;
52 class Headers;
53 class IDBKeyRange;
54 class MIDIPort;
55 class MediaKeyError;
56 class Notification;
57 class Path2D; 47 class Path2D;
58 class SpeechRecognitionResult;
59 class SpeechRecognitionResultList;
60 class Storage;
61 class TrackBase;
62 class VoidCallback;
63 48
64 class Dictionary { 49 class Dictionary {
65 ALLOW_ONLY_INLINE_ALLOCATION(); 50 ALLOW_ONLY_INLINE_ALLOCATION();
66 public: 51 public:
67 Dictionary(); 52 Dictionary();
68 Dictionary(const v8::Handle<v8::Value>& options, v8::Isolate*); 53 Dictionary(const v8::Handle<v8::Value>& options, v8::Isolate*);
69 ~Dictionary(); 54 ~Dictionary();
70 55
71 Dictionary& operator=(const Dictionary&); 56 Dictionary& operator=(const Dictionary&);
72 57
73 // This is different from the default constructor: 58 // This is different from the default constructor:
74 // * isObject() is true when using createEmpty(). 59 // * isObject() is true when using createEmpty().
75 // * isUndefinedOrNull() is true when using default constructor. 60 // * isUndefinedOrNull() is true when using default constructor.
76 static Dictionary createEmpty(v8::Isolate*); 61 static Dictionary createEmpty(v8::Isolate*);
77 62
78 bool isObject() const; 63 bool isObject() const;
79 bool isUndefinedOrNull() const; 64 bool isUndefinedOrNull() const;
80 65
81 bool get(const String&, bool&) const;
82 bool get(const String&, int32_t&) const;
83 bool get(const String&, double&, bool& hasValue) const;
84 bool get(const String&, double&) const;
85 bool get(const String&, String&) const;
86 bool get(const String&, AtomicString&) const;
87 bool get(const String&, ScriptValue&) const;
88 bool get(const String&, short&) const;
89 bool get(const String&, unsigned short&) const;
90 bool get(const String&, unsigned&) const;
91 bool get(const String&, unsigned long&) const;
92 bool get(const String&, unsigned long long&) const;
93 bool get(const String&, RefPtrWillBeMember<LocalDOMWindow>&) const;
94 bool get(const String&, RefPtrWillBeMember<Storage>&) const;
95 bool get(const String&, MessagePortArray&) const;
96 bool get(const String&, RefPtr<Uint8Array>&) const;
97 bool get(const String&, RefPtr<ArrayBufferView>&) const;
98 bool get(const String&, Member<MIDIPort>&) const;
99 bool get(const String&, RefPtrWillBeMember<MediaKeyError>&) const;
100 bool get(const String&, RefPtrWillBeMember<TrackBase>&) const;
101 bool get(const String&, Member<SpeechRecognitionResult>&) const;
102 bool get(const String&, Member<SpeechRecognitionResultList>&) const;
103 bool get(const String&, Member<Gamepad>&) const;
104 bool get(const String&, Member<MediaStream>&) const;
105 bool get(const String&, RefPtrWillBeMember<EventTarget>&) const;
106 bool get(const String&, HashSet<AtomicString>&) const;
107 bool get(const String&, Dictionary&) const; 66 bool get(const String&, Dictionary&) const;
108 bool get(const String&, Vector<String>&) const;
109 bool get(const String&, ArrayValue&) const;
110 bool get(const String&, RefPtrWillBeMember<DOMError>&) const;
111 bool get(const String&, v8::Local<v8::Value>&) const; 67 bool get(const String&, v8::Local<v8::Value>&) const;
112 bool get(const String&, RefPtr<Headers>&) const;
113 68
114 // Sets properties using default attributes. 69 // Sets properties using default attributes.
115 bool set(const String&, const v8::Handle<v8::Value>&); 70 bool set(const String&, const v8::Handle<v8::Value>&);
116 bool set(const String&, const String&); 71 bool set(const String&, const String&);
117 bool set(const String&, unsigned); 72 bool set(const String&, unsigned);
118 bool set(const String&, const Dictionary&); 73 bool set(const String&, const Dictionary&);
119 74
120 v8::Handle<v8::Value> v8Value() const { return m_options; } 75 v8::Handle<v8::Value> v8Value() const { return m_options; }
121 76
122 class ConversionContext { 77 class ConversionContext {
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after
159 ConversionContextScope(ConversionContext& context) 114 ConversionContextScope(ConversionContext& context)
160 : m_context(context) { } 115 : m_context(context) { }
161 ~ConversionContextScope() 116 ~ConversionContextScope()
162 { 117 {
163 m_context.resetPerPropertyContext(); 118 m_context.resetPerPropertyContext();
164 } 119 }
165 private: 120 private:
166 ConversionContext& m_context; 121 ConversionContext& m_context;
167 }; 122 };
168 123
169 bool convert(ConversionContext&, const String&, bool&) const;
170 bool convert(ConversionContext&, const String&, double&) const;
171 bool convert(ConversionContext&, const String&, String&) const;
172 bool convert(ConversionContext&, const String&, ScriptValue&) const;
173
174 template<typename IntegralType>
175 bool convert(ConversionContext&, const String&, IntegralType&) const;
176 template<typename IntegralType>
177 bool convert(ConversionContext&, const String&, Nullable<IntegralType>&) con st;
178
179 bool convert(ConversionContext&, const String&, MessagePortArray&) const;
180 bool convert(ConversionContext&, const String&, HashSet<AtomicString>&) cons t;
181 bool convert(ConversionContext&, const String&, Dictionary&) const; 124 bool convert(ConversionContext&, const String&, Dictionary&) const;
182 bool convert(ConversionContext&, const String&, Vector<String>&) const;
183 bool convert(ConversionContext&, const String&, ArrayValue&) const;
184 template<template <typename> class PointerType, typename T>
185 bool convert(ConversionContext&, const String&, PointerType<T>&) const;
186
187 template<typename StringType>
188 bool getStringType(const String&, StringType&) const;
189 125
190 bool getOwnPropertiesAsStringHashMap(HashMap<String, String>&) const; 126 bool getOwnPropertiesAsStringHashMap(HashMap<String, String>&) const;
191 bool getOwnPropertyNames(Vector<String>&) const; 127 bool getOwnPropertyNames(Vector<String>&) const;
192 128
193 bool getWithUndefinedOrNullCheck(const String&, String&) const; 129 bool getWithUndefinedOrNullCheck(const String&, String&) const;
194 bool getWithUndefinedOrNullCheck(const String&, RefPtrWillBeMember<Element>& ) const; 130 bool getWithUndefinedOrNullCheck(const String&, RefPtrWillBeMember<Element>& ) const;
195 bool getWithUndefinedOrNullCheck(const String&, RefPtr<Path2D>&) const; 131 bool getWithUndefinedOrNullCheck(const String&, RefPtr<Path2D>&) const;
196 132
197 bool hasProperty(const String&) const; 133 bool hasProperty(const String&) const;
198 134
135 v8::Isolate* isolate() const { return m_isolate; }
136
199 private: 137 private:
200 bool getKey(const String& key, v8::Local<v8::Value>&) const; 138 bool getKey(const String& key, v8::Local<v8::Value>&) const;
201 139
202 v8::Handle<v8::Value> m_options; 140 v8::Handle<v8::Value> m_options;
203 v8::Isolate* m_isolate; 141 v8::Isolate* m_isolate;
204 }; 142 };
205 143
206 template<> 144 template<>
207 struct NativeValueTraits<Dictionary> { 145 struct NativeValueTraits<Dictionary> {
208 static inline Dictionary nativeValue(const v8::Handle<v8::Value>& value, v8: :Isolate* isolate) 146 static inline Dictionary nativeValue(const v8::Handle<v8::Value>& value, v8: :Isolate* isolate)
209 { 147 {
210 return Dictionary(value, isolate); 148 return Dictionary(value, isolate);
211 } 149 }
212 }; 150 };
213 151
214 template <typename T> 152 // DictionaryHelper is a collection of static methods for getting or
215 struct IntegralTypeTraits { 153 // converting a value from Dictionary.
154 struct DictionaryHelper {
155 template <typename T>
156 static bool get(const Dictionary&, const String& key, T& value);
157 template <typename T>
158 static bool get(const Dictionary&, const String& key, T& value, bool& hasVal ue);
159 template <template <typename> class PointerType, typename T>
160 static bool get(const Dictionary&, const String& key, PointerType<T>& value) ;
161 template <typename T>
162 static bool convert(const Dictionary&, Dictionary::ConversionContext&, const String& key, T& value);
163 template <typename T>
164 static bool convert(const Dictionary&, Dictionary::ConversionContext&, const String& key, Nullable<T>& value);
165 template <template <typename> class PointerType, typename T>
166 static bool convert(const Dictionary&, Dictionary::ConversionContext&, const String& key, PointerType<T>& value);
216 }; 167 };
217 168
218 template <>
219 struct IntegralTypeTraits<uint8_t> {
220 static inline uint8_t toIntegral(v8::Handle<v8::Value> value, IntegerConvers ionConfiguration configuration, ExceptionState& exceptionState)
221 {
222 return toUInt8(value, configuration, exceptionState);
223 }
224 static const String typeName() { return "UInt8"; }
225 };
226
227 template <>
228 struct IntegralTypeTraits<int8_t> {
229 static inline int8_t toIntegral(v8::Handle<v8::Value> value, IntegerConversi onConfiguration configuration, ExceptionState& exceptionState)
230 {
231 return toInt8(value, configuration, exceptionState);
232 }
233 static const String typeName() { return "Int8"; }
234 };
235
236 template <>
237 struct IntegralTypeTraits<unsigned short> {
238 static inline uint16_t toIntegral(v8::Handle<v8::Value> value, IntegerConver sionConfiguration configuration, ExceptionState& exceptionState)
239 {
240 return toUInt16(value, configuration, exceptionState);
241 }
242 static const String typeName() { return "UInt16"; }
243 };
244
245 template <>
246 struct IntegralTypeTraits<short> {
247 static inline int16_t toIntegral(v8::Handle<v8::Value> value, IntegerConvers ionConfiguration configuration, ExceptionState& exceptionState)
248 {
249 return toInt16(value, configuration, exceptionState);
250 }
251 static const String typeName() { return "Int16"; }
252 };
253
254 template <>
255 struct IntegralTypeTraits<unsigned> {
256 static inline uint32_t toIntegral(v8::Handle<v8::Value> value, IntegerConver sionConfiguration configuration, ExceptionState& exceptionState)
257 {
258 return toUInt32(value, configuration, exceptionState);
259 }
260 static const String typeName() { return "UInt32"; }
261 };
262
263 template <>
264 struct IntegralTypeTraits<unsigned long> {
265 static inline uint32_t toIntegral(v8::Handle<v8::Value> value, IntegerConver sionConfiguration configuration, ExceptionState& exceptionState)
266 {
267 return toUInt32(value, configuration, exceptionState);
268 }
269 static const String typeName() { return "UInt32"; }
270 };
271
272 template <>
273 struct IntegralTypeTraits<int> {
274 static inline int32_t toIntegral(v8::Handle<v8::Value> value, IntegerConvers ionConfiguration configuration, ExceptionState& exceptionState)
275 {
276 return toInt32(value, configuration, exceptionState);
277 }
278 static const String typeName() { return "Int32"; }
279 };
280
281 template <>
282 struct IntegralTypeTraits<long> {
283 static inline int32_t toIntegral(v8::Handle<v8::Value> value, IntegerConvers ionConfiguration configuration, ExceptionState& exceptionState)
284 {
285 return toInt32(value, configuration, exceptionState);
286 }
287 static const String typeName() { return "Int32"; }
288 };
289
290 template <>
291 struct IntegralTypeTraits<unsigned long long> {
292 static inline unsigned long long toIntegral(v8::Handle<v8::Value> value, Int egerConversionConfiguration configuration, ExceptionState& exceptionState)
293 {
294 return toUInt64(value, configuration, exceptionState);
295 }
296 static const String typeName() { return "UInt64"; }
297 };
298
299 template <>
300 struct IntegralTypeTraits<long long> {
301 static inline long long toIntegral(v8::Handle<v8::Value> value, IntegerConve rsionConfiguration configuration, ExceptionState& exceptionState)
302 {
303 return toInt64(value, configuration, exceptionState);
304 }
305 static const String typeName() { return "Int64"; }
306 };
307
308 template<typename T> bool Dictionary::convert(ConversionContext& context, const String& key, T& value) const
309 {
310 ConversionContextScope scope(context);
311
312 v8::Local<v8::Value> v8Value;
313 if (!getKey(key, v8Value))
314 return true;
315
316 value = IntegralTypeTraits<T>::toIntegral(v8Value, NormalConversion, context .exceptionState());
317 if (context.exceptionState().throwIfNeeded())
318 return false;
319
320 return true;
321 }
322
323 template<typename T> bool Dictionary::convert(ConversionContext& context, const String& key, Nullable<T>& value) const
324 {
325 ConversionContextScope scope(context);
326
327 v8::Local<v8::Value> v8Value;
328 if (!getKey(key, v8Value))
329 return true;
330
331 if (context.isNullable() && WebCore::isUndefinedOrNull(v8Value)) {
332 value = Nullable<T>();
333 return true;
334 }
335
336 T converted = IntegralTypeTraits<T>::toIntegral(v8Value, NormalConversion, c ontext.exceptionState());
337
338 if (context.exceptionState().throwIfNeeded())
339 return false;
340
341 value = Nullable<T>(converted);
342 return true;
343 }
344
345 template<template <typename> class PointerType, typename T> bool Dictionary::con vert(ConversionContext& context, const String& key, PointerType<T>& value) const
346 {
347 ConversionContextScope scope(context);
348
349 if (!get(key, value))
350 return true;
351
352 if (value)
353 return true;
354
355 v8::Local<v8::Value> v8Value;
356 getKey(key, v8Value);
357 if (context.isNullable() && WebCore::isUndefinedOrNull(v8Value))
358 return true;
359
360 context.throwTypeError(ExceptionMessages::incorrectPropertyType(key, "does n ot have a " + context.typeName() + " type."));
361 return false;
362 }
363
364 } 169 }
365 170
366 #endif // Dictionary_h 171 #endif // Dictionary_h
OLDNEW
« no previous file with comments | « Source/bindings/core/v8/CustomElementConstructorBuilder.cpp ('k') | Source/bindings/core/v8/Dictionary.cpp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698