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 |
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 Loading... | |
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 class ConversionContext { | |
103 public: | |
104 ConversionContext(const String& interfaceName, const String& methodName, ExceptionState& exceptionState) | |
105 : m_interfaceName(interfaceName) | |
sof
2013/11/27 23:46:43
I notice https://codereview.chromium.org/87963002,
| |
106 , m_methodName(methodName) | |
107 , m_exceptionState(exceptionState) | |
108 , m_dirty(true) | |
109 { | |
110 resetPerPropertyContext(); | |
111 } | |
112 | |
113 const String& interfaceName() const { return m_interfaceName; } | |
114 const String& methodName() const { return m_methodName; } | |
115 bool forConstructor() const { return m_methodName.isEmpty(); } | |
116 ExceptionState& exceptionState() const { return m_exceptionState; } | |
117 | |
118 bool isNullable() const { return m_isNullable; } | |
119 String typeName() const { return m_propertyTypeName; } | |
120 IntegerConversionConfiguration numberConversion() const { return m_numbe rConversion; } | |
121 | |
122 ConversionContext& withAttributes(bool, IntegerConversionConfiguration, const String&); | |
123 ConversionContext& withAttributes(bool, IntegerConversionConfiguration); | |
124 ConversionContext& withAttributes(bool, const String&); | |
125 ConversionContext& withAttributes(bool); | |
126 ConversionContext& withAttributes(const String&); | |
127 | |
128 void throwTypeError(const String& detail); | |
129 | |
130 void resetPerPropertyContext(); | |
131 | |
132 private: | |
133 const String m_interfaceName; | |
134 const String m_methodName; | |
135 ExceptionState& m_exceptionState; | |
136 bool m_dirty; | |
137 | |
138 bool m_isNullable; | |
139 String m_propertyTypeName; | |
140 IntegerConversionConfiguration m_numberConversion; | |
141 }; | |
142 | |
143 class ConversionContextScope { | |
144 public: | |
145 ConversionContextScope(ConversionContext& context) | |
146 : m_context(context) { } | |
147 ~ConversionContextScope() | |
148 { | |
149 m_context.resetPerPropertyContext(); | |
150 } | |
151 private: | |
152 ConversionContext& m_context; | |
153 }; | |
154 | |
155 bool convert(ConversionContext&, const String&, bool&) const; | |
156 bool convert(ConversionContext&, const String&, double&) const; | |
157 bool convert(ConversionContext&, const String&, String&) const; | |
158 bool convert(ConversionContext&, const String&, ScriptValue&) const; | |
159 | |
160 template<typename IntegralType> | |
161 bool convert(ConversionContext &, const String&, IntegralType&) const; | |
162 bool convert(ConversionContext &, const String&, MessagePortArray&) const; | |
163 bool convert(ConversionContext &, const String&, HashSet<AtomicString>&) con st; | |
164 bool convert(ConversionContext &, const String&, Dictionary&) const; | |
165 bool convert(ConversionContext &, const String&, Vector<String>&) const; | |
166 bool convert(ConversionContext &, const String&, ArrayValue&) const; | |
167 template<typename T> | |
168 bool convert(ConversionContext &, const String&, RefPtr<T>&) const; | |
169 | |
98 bool getOwnPropertiesAsStringHashMap(HashMap<String, String>&) const; | 170 bool getOwnPropertiesAsStringHashMap(HashMap<String, String>&) const; |
99 bool getOwnPropertyNames(Vector<String>&) const; | 171 bool getOwnPropertyNames(Vector<String>&) const; |
100 | 172 |
101 bool getWithUndefinedOrNullCheck(const String&, String&) const; | 173 bool getWithUndefinedOrNullCheck(const String&, String&) const; |
102 | 174 |
175 bool hasProperty(const String&) const; | |
176 | |
103 // Only allow inline allocation. | 177 // Only allow inline allocation. |
104 void* operator new(size_t, NotNullTag, void* location) { return location; } | 178 void* operator new(size_t, NotNullTag, void* location) { return location; } |
105 | 179 |
106 private: | 180 private: |
107 // Disallow new allocation. | 181 // Disallow new allocation. |
108 void* operator new(size_t); | 182 void* operator new(size_t); |
109 | 183 |
110 bool getKey(const String& key, v8::Local<v8::Value>&) const; | 184 bool getKey(const String& key, v8::Local<v8::Value>&) const; |
111 | 185 |
112 v8::Handle<v8::Value> m_options; | 186 v8::Handle<v8::Value> m_options; |
113 v8::Isolate* m_isolate; | 187 v8::Isolate* m_isolate; |
114 }; | 188 }; |
115 | 189 |
190 template<> | |
191 struct NativeValueTraits<Dictionary> { | |
192 static inline Dictionary nativeValue(const v8::Handle<v8::Value>& value, v8: :Isolate* isolate) | |
193 { | |
194 return Dictionary(value, isolate); | |
195 } | |
196 }; | |
197 | |
198 template <typename T> | |
199 struct IntegralTypeTraits { | |
200 }; | |
201 | |
202 template <> | |
203 struct IntegralTypeTraits<uint8_t> { | |
204 static inline uint8_t toIntegral(v8::Handle<v8::Value> value, IntegerConvers ionConfiguration configuration, bool& ok) | |
205 { | |
206 return toUInt8(value, configuration, ok); | |
207 } | |
208 static const String typeName() { return "UInt8"; } | |
209 }; | |
210 | |
211 template <> | |
212 struct IntegralTypeTraits<int8_t> { | |
213 static inline int8_t toIntegral(v8::Handle<v8::Value> value, IntegerConversi onConfiguration configuration, bool& ok) | |
214 { | |
215 return toInt8(value, configuration, ok); | |
216 } | |
217 static const String typeName() { return "Int8"; } | |
218 }; | |
219 | |
220 template <> | |
221 struct IntegralTypeTraits<unsigned short> { | |
222 static inline uint16_t toIntegral(v8::Handle<v8::Value> value, IntegerConver sionConfiguration configuration, bool& ok) | |
223 { | |
224 return toUInt16(value, configuration, ok); | |
225 } | |
226 static const String typeName() { return "UInt16"; } | |
227 }; | |
228 | |
229 template <> | |
230 struct IntegralTypeTraits<short> { | |
231 static inline int16_t toIntegral(v8::Handle<v8::Value> value, IntegerConvers ionConfiguration configuration, bool& ok) | |
232 { | |
233 return toInt16(value, configuration, ok); | |
234 } | |
235 static const String typeName() { return "Int16"; } | |
236 }; | |
237 | |
238 template <> | |
239 struct IntegralTypeTraits<unsigned> { | |
240 static inline uint32_t toIntegral(v8::Handle<v8::Value> value, IntegerConver sionConfiguration configuration, bool& ok) | |
241 { | |
242 return toUInt32(value, configuration, ok); | |
243 } | |
244 static const String typeName() { return "UInt32"; } | |
245 }; | |
246 | |
247 template <> | |
248 struct IntegralTypeTraits<unsigned long> { | |
249 static inline uint32_t toIntegral(v8::Handle<v8::Value> value, IntegerConver sionConfiguration configuration, bool& ok) | |
250 { | |
251 return toUInt32(value, configuration, ok); | |
252 } | |
253 static const String typeName() { return "UInt32"; } | |
254 }; | |
255 | |
256 template <> | |
257 struct IntegralTypeTraits<int> { | |
258 static inline int32_t toIntegral(v8::Handle<v8::Value> value, IntegerConvers ionConfiguration configuration, bool& ok) | |
259 { | |
260 return toInt32(value, configuration, ok); | |
261 } | |
262 static const String typeName() { return "Int32"; } | |
263 }; | |
264 | |
265 template <> | |
266 struct IntegralTypeTraits<long> { | |
267 static inline int32_t toIntegral(v8::Handle<v8::Value> value, IntegerConvers ionConfiguration configuration, bool& ok) | |
268 { | |
269 return toInt32(value, configuration, ok); | |
270 } | |
271 static const String typeName() { return "Int32"; } | |
272 }; | |
273 | |
274 template <> | |
275 struct IntegralTypeTraits<unsigned long long> { | |
276 static inline unsigned long long toIntegral(v8::Handle<v8::Value> value, Int egerConversionConfiguration configuration, bool& ok) | |
277 { | |
278 return toUInt64(value, configuration, ok); | |
279 } | |
280 static const String typeName() { return "UInt64"; } | |
281 }; | |
282 | |
283 template <> | |
284 struct IntegralTypeTraits<long long> { | |
285 static inline long long toIntegral(v8::Handle<v8::Value> value, IntegerConve rsionConfiguration configuration, bool& ok) | |
286 { | |
287 return toInt64(value, configuration, ok); | |
288 } | |
289 static const String typeName() { return "Int64"; } | |
290 }; | |
291 | |
292 template<typename T> bool Dictionary::convert(ConversionContext& context, const String& key, T& value) const | |
293 { | |
294 ConversionContextScope scope(context); | |
295 | |
296 v8::Local<v8::Value> v8Value; | |
297 if (!getKey(key, v8Value)) | |
298 return true; | |
299 | |
300 bool ok = false; | |
301 value = IntegralTypeTraits<T>::toIntegral(v8Value, context.numberConversion( ), ok); | |
302 if (!ok) { | |
303 V8TRYCATCH_RETURN(v8::Local<v8::Number>, v8Number, v8Value->ToNumber(), false); | |
304 if (v8Number.IsEmpty()) { | |
305 context.throwTypeError(ExceptionMessages::incorrectPropertyType(key, "does not have type " + IntegralTypeTraits<T>::typeName() + ".")); | |
306 } else { | |
307 ASSERT(context.numberConversion() == EnforceRange); | |
308 context.throwTypeError(ExceptionMessages::incorrectPropertyType(key, "is not a finite number.")); | |
309 } | |
310 return false; | |
311 } | |
312 return true; | |
313 } | |
314 | |
315 template<typename T> bool Dictionary::convert(ConversionContext& context, const String& key, RefPtr<T>& value) const | |
316 { | |
317 ConversionContextScope scope(context); | |
318 | |
319 if (!get(key, value)) | |
320 return true; | |
321 | |
322 if (!value) { | |
323 v8::Local<v8::Value> v8Value; | |
324 getKey(key, v8Value); | |
325 if (!(context.isNullable() && WebCore::isUndefinedOrNull(v8Value))) { | |
326 context.throwTypeError(ExceptionMessages::incorrectPropertyType(key, "does not have " + (context.typeName().isEmpty() ? String("the expected type.") : ("a " + context.typeName() + " type.")))); | |
327 return false; | |
328 } | |
329 } | |
330 return true; | |
331 } | |
332 | |
116 } | 333 } |
117 | 334 |
118 #endif // Dictionary_h | 335 #endif // Dictionary_h |
OLD | NEW |