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