OLD | NEW |
| (Empty) |
1 /* | |
2 * Copyright (C) 2010 Google Inc. All rights reserved. | |
3 * | |
4 * Redistribution and use in source and binary forms, with or without | |
5 * modification, are permitted provided that the following conditions | |
6 * are met: | |
7 * | |
8 * 1. Redistributions of source code must retain the above copyright | |
9 * notice, this list of conditions and the following disclaimer. | |
10 * 2. Redistributions in binary form must reproduce the above copyright | |
11 * notice, this list of conditions and the following disclaimer in the | |
12 * documentation and/or other materials provided with the distribution. | |
13 * | |
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 | |
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 | |
18 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES | |
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 | |
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 | |
23 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | |
24 */ | |
25 | |
26 #include "config.h" | |
27 #include "bindings/v8/Dictionary.h" | |
28 | |
29 #include "bindings/core/v8/V8DOMError.h" | |
30 #include "bindings/core/v8/V8Element.h" | |
31 #include "bindings/core/v8/V8EventTarget.h" | |
32 #include "bindings/core/v8/V8MediaKeyError.h" | |
33 #include "bindings/core/v8/V8MessagePort.h" | |
34 #include "bindings/core/v8/V8Storage.h" | |
35 #include "bindings/core/v8/V8TextTrack.h" | |
36 #include "bindings/core/v8/V8VoidCallback.h" | |
37 #include "bindings/core/v8/V8Window.h" | |
38 #include "bindings/core/v8/custom/V8ArrayBufferViewCustom.h" | |
39 #include "bindings/core/v8/custom/V8Uint8ArrayCustom.h" | |
40 #include "bindings/modules/v8/V8Gamepad.h" | |
41 #include "bindings/modules/v8/V8HeaderMap.h" | |
42 #include "bindings/modules/v8/V8Headers.h" | |
43 #include "bindings/modules/v8/V8IDBKeyRange.h" | |
44 #include "bindings/modules/v8/V8MIDIPort.h" | |
45 #include "bindings/modules/v8/V8MediaStream.h" | |
46 #include "bindings/modules/v8/V8SpeechRecognitionResult.h" | |
47 #include "bindings/modules/v8/V8SpeechRecognitionResultList.h" | |
48 #include "bindings/v8/ArrayValue.h" | |
49 #include "bindings/v8/ExceptionMessages.h" | |
50 #include "bindings/v8/ExceptionState.h" | |
51 #include "bindings/v8/V8Binding.h" | |
52 #include "core/html/track/TrackBase.h" | |
53 #include "modules/gamepad/Gamepad.h" | |
54 #include "modules/indexeddb/IDBKeyRange.h" | |
55 #include "modules/mediastream/MediaStream.h" | |
56 #include "modules/speech/SpeechRecognitionResult.h" | |
57 #include "modules/speech/SpeechRecognitionResultList.h" | |
58 #include "wtf/MathExtras.h" | |
59 | |
60 namespace WebCore { | |
61 | |
62 Dictionary::Dictionary() | |
63 : m_isolate(0) | |
64 { | |
65 } | |
66 | |
67 Dictionary::Dictionary(const v8::Handle<v8::Value>& options, v8::Isolate* isolat
e) | |
68 : m_options(options) | |
69 , m_isolate(isolate) | |
70 { | |
71 ASSERT(m_isolate); | |
72 } | |
73 | |
74 Dictionary::~Dictionary() | |
75 { | |
76 } | |
77 | |
78 Dictionary& Dictionary::operator=(const Dictionary& optionsObject) | |
79 { | |
80 m_options = optionsObject.m_options; | |
81 m_isolate = optionsObject.m_isolate; | |
82 return *this; | |
83 } | |
84 | |
85 bool Dictionary::isObject() const | |
86 { | |
87 return !isUndefinedOrNull() && m_options->IsObject(); | |
88 } | |
89 | |
90 bool Dictionary::isUndefinedOrNull() const | |
91 { | |
92 if (m_options.IsEmpty()) | |
93 return true; | |
94 return WebCore::isUndefinedOrNull(m_options); | |
95 } | |
96 | |
97 bool Dictionary::hasProperty(const String& key) const | |
98 { | |
99 if (isUndefinedOrNull()) | |
100 return false; | |
101 v8::Local<v8::Object> options = m_options->ToObject(); | |
102 ASSERT(!options.IsEmpty()); | |
103 | |
104 ASSERT(m_isolate); | |
105 ASSERT(m_isolate == v8::Isolate::GetCurrent()); | |
106 v8::Handle<v8::String> v8Key = v8String(m_isolate, key); | |
107 if (!options->Has(v8Key)) | |
108 return false; | |
109 | |
110 return true; | |
111 } | |
112 | |
113 bool Dictionary::getKey(const String& key, v8::Local<v8::Value>& value) const | |
114 { | |
115 if (isUndefinedOrNull()) | |
116 return false; | |
117 v8::Local<v8::Object> options = m_options->ToObject(); | |
118 ASSERT(!options.IsEmpty()); | |
119 | |
120 ASSERT(m_isolate); | |
121 ASSERT(m_isolate == v8::Isolate::GetCurrent()); | |
122 v8::Handle<v8::String> v8Key = v8String(m_isolate, key); | |
123 if (!options->Has(v8Key)) | |
124 return false; | |
125 value = options->Get(v8Key); | |
126 if (value.IsEmpty()) | |
127 return false; | |
128 return true; | |
129 } | |
130 | |
131 bool Dictionary::get(const String& key, v8::Local<v8::Value>& value) const | |
132 { | |
133 return getKey(key, value); | |
134 } | |
135 | |
136 bool Dictionary::get(const String& key, bool& value) const | |
137 { | |
138 v8::Local<v8::Value> v8Value; | |
139 if (!getKey(key, v8Value)) | |
140 return false; | |
141 | |
142 v8::Local<v8::Boolean> v8Bool = v8Value->ToBoolean(); | |
143 if (v8Bool.IsEmpty()) | |
144 return false; | |
145 value = v8Bool->Value(); | |
146 return true; | |
147 } | |
148 | |
149 bool Dictionary::convert(ConversionContext& context, const String& key, bool& va
lue) const | |
150 { | |
151 ConversionContextScope scope(context); | |
152 get(key, value); | |
153 return true; | |
154 } | |
155 | |
156 bool Dictionary::get(const String& key, int32_t& value) const | |
157 { | |
158 v8::Local<v8::Value> v8Value; | |
159 if (!getKey(key, v8Value)) | |
160 return false; | |
161 | |
162 v8::Local<v8::Int32> v8Int32 = v8Value->ToInt32(); | |
163 if (v8Int32.IsEmpty()) | |
164 return false; | |
165 value = v8Int32->Value(); | |
166 return true; | |
167 } | |
168 | |
169 bool Dictionary::get(const String& key, double& value, bool& hasValue) const | |
170 { | |
171 v8::Local<v8::Value> v8Value; | |
172 if (!getKey(key, v8Value)) { | |
173 hasValue = false; | |
174 return false; | |
175 } | |
176 | |
177 hasValue = true; | |
178 TONATIVE_DEFAULT(v8::Local<v8::Number>, v8Number, v8Value->ToNumber(), false
); | |
179 if (v8Number.IsEmpty()) | |
180 return false; | |
181 value = v8Number->Value(); | |
182 return true; | |
183 } | |
184 | |
185 bool Dictionary::get(const String& key, double& value) const | |
186 { | |
187 bool unused; | |
188 return get(key, value, unused); | |
189 } | |
190 | |
191 bool Dictionary::convert(ConversionContext& context, const String& key, double&
value) const | |
192 { | |
193 ConversionContextScope scope(context); | |
194 | |
195 bool hasValue = false; | |
196 if (!get(key, value, hasValue) && hasValue) { | |
197 context.throwTypeError(ExceptionMessages::incorrectPropertyType(key, "is
not of type 'double'.")); | |
198 return false; | |
199 } | |
200 return true; | |
201 } | |
202 | |
203 template<typename StringType> | |
204 inline bool Dictionary::getStringType(const String& key, StringType& value) cons
t | |
205 { | |
206 v8::Local<v8::Value> v8Value; | |
207 if (!getKey(key, v8Value)) | |
208 return false; | |
209 | |
210 TOSTRING_DEFAULT(V8StringResource<>, stringValue, v8Value, false); | |
211 value = stringValue; | |
212 return true; | |
213 } | |
214 | |
215 bool Dictionary::get(const String& key, String& value) const | |
216 { | |
217 return getStringType(key, value); | |
218 } | |
219 | |
220 bool Dictionary::get(const String& key, AtomicString& value) const | |
221 { | |
222 return getStringType(key, value); | |
223 } | |
224 | |
225 bool Dictionary::convert(ConversionContext& context, const String& key, String&
value) const | |
226 { | |
227 ConversionContextScope scope(context); | |
228 | |
229 v8::Local<v8::Value> v8Value; | |
230 if (!getKey(key, v8Value)) | |
231 return true; | |
232 | |
233 TOSTRING_DEFAULT(V8StringResource<>, stringValue, v8Value, false); | |
234 value = stringValue; | |
235 return true; | |
236 } | |
237 | |
238 bool Dictionary::get(const String& key, ScriptValue& value) const | |
239 { | |
240 v8::Local<v8::Value> v8Value; | |
241 if (!getKey(key, v8Value)) | |
242 return false; | |
243 | |
244 value = ScriptValue(ScriptState::current(m_isolate), v8Value); | |
245 return true; | |
246 } | |
247 | |
248 bool Dictionary::convert(ConversionContext& context, const String& key, ScriptVa
lue& value) const | |
249 { | |
250 ConversionContextScope scope(context); | |
251 | |
252 get(key, value); | |
253 return true; | |
254 } | |
255 | |
256 bool Dictionary::get(const String& key, unsigned short& value) const | |
257 { | |
258 v8::Local<v8::Value> v8Value; | |
259 if (!getKey(key, v8Value)) | |
260 return false; | |
261 | |
262 v8::Local<v8::Int32> v8Int32 = v8Value->ToInt32(); | |
263 if (v8Int32.IsEmpty()) | |
264 return false; | |
265 value = static_cast<unsigned short>(v8Int32->Value()); | |
266 return true; | |
267 } | |
268 | |
269 bool Dictionary::get(const String& key, short& value) const | |
270 { | |
271 v8::Local<v8::Value> v8Value; | |
272 if (!getKey(key, v8Value)) | |
273 return false; | |
274 | |
275 v8::Local<v8::Int32> v8Int32 = v8Value->ToInt32(); | |
276 if (v8Int32.IsEmpty()) | |
277 return false; | |
278 value = static_cast<short>(v8Int32->Value()); | |
279 return true; | |
280 } | |
281 | |
282 bool Dictionary::get(const String& key, unsigned& value) const | |
283 { | |
284 v8::Local<v8::Value> v8Value; | |
285 if (!getKey(key, v8Value)) | |
286 return false; | |
287 | |
288 v8::Local<v8::Int32> v8Int32 = v8Value->ToInt32(); | |
289 if (v8Int32.IsEmpty()) | |
290 return false; | |
291 value = static_cast<unsigned>(v8Int32->Value()); | |
292 return true; | |
293 } | |
294 | |
295 bool Dictionary::get(const String& key, unsigned long& value) const | |
296 { | |
297 v8::Local<v8::Value> v8Value; | |
298 if (!getKey(key, v8Value)) | |
299 return false; | |
300 | |
301 v8::Local<v8::Integer> v8Integer = v8Value->ToInteger(); | |
302 if (v8Integer.IsEmpty()) | |
303 return false; | |
304 value = static_cast<unsigned long>(v8Integer->Value()); | |
305 return true; | |
306 } | |
307 | |
308 bool Dictionary::get(const String& key, unsigned long long& value) const | |
309 { | |
310 v8::Local<v8::Value> v8Value; | |
311 if (!getKey(key, v8Value)) | |
312 return false; | |
313 | |
314 TONATIVE_DEFAULT(v8::Local<v8::Number>, v8Number, v8Value->ToNumber(), false
); | |
315 if (v8Number.IsEmpty()) | |
316 return false; | |
317 double d = v8Number->Value(); | |
318 doubleToInteger(d, value); | |
319 return true; | |
320 } | |
321 | |
322 bool Dictionary::get(const String& key, RefPtrWillBeMember<LocalDOMWindow>& valu
e) const | |
323 { | |
324 v8::Local<v8::Value> v8Value; | |
325 if (!getKey(key, v8Value)) | |
326 return false; | |
327 | |
328 // We need to handle a DOMWindow specially, because a DOMWindow wrapper | |
329 // exists on a prototype chain of v8Value. | |
330 value = toDOMWindow(v8Value, m_isolate); | |
331 return true; | |
332 } | |
333 | |
334 bool Dictionary::get(const String& key, RefPtrWillBeMember<Storage>& value) cons
t | |
335 { | |
336 v8::Local<v8::Value> v8Value; | |
337 if (!getKey(key, v8Value)) | |
338 return false; | |
339 | |
340 value = V8Storage::toNativeWithTypeCheck(m_isolate, v8Value); | |
341 return true; | |
342 } | |
343 | |
344 bool Dictionary::get(const String& key, MessagePortArray& value) const | |
345 { | |
346 v8::Local<v8::Value> v8Value; | |
347 if (!getKey(key, v8Value)) | |
348 return false; | |
349 | |
350 ASSERT(m_isolate); | |
351 ASSERT(m_isolate == v8::Isolate::GetCurrent()); | |
352 if (WebCore::isUndefinedOrNull(v8Value)) | |
353 return true; | |
354 bool success = false; | |
355 value = toRefPtrWillBeMemberNativeArray<MessagePort, V8MessagePort>(v8Value,
key, m_isolate, &success); | |
356 return success; | |
357 } | |
358 | |
359 bool Dictionary::convert(ConversionContext& context, const String& key, MessageP
ortArray& value) const | |
360 { | |
361 ConversionContextScope scope(context); | |
362 | |
363 v8::Local<v8::Value> v8Value; | |
364 if (!getKey(key, v8Value)) | |
365 return true; | |
366 | |
367 return get(key, value); | |
368 } | |
369 | |
370 bool Dictionary::get(const String& key, HashSet<AtomicString>& value) const | |
371 { | |
372 v8::Local<v8::Value> v8Value; | |
373 if (!getKey(key, v8Value)) | |
374 return false; | |
375 | |
376 // FIXME: Support array-like objects | |
377 if (!v8Value->IsArray()) | |
378 return false; | |
379 | |
380 ASSERT(m_isolate); | |
381 ASSERT(m_isolate == v8::Isolate::GetCurrent()); | |
382 v8::Local<v8::Array> v8Array = v8::Local<v8::Array>::Cast(v8Value); | |
383 for (size_t i = 0; i < v8Array->Length(); ++i) { | |
384 v8::Local<v8::Value> indexedValue = v8Array->Get(v8::Integer::New(m_isol
ate, i)); | |
385 TOSTRING_DEFAULT(V8StringResource<>, stringValue, indexedValue, false); | |
386 value.add(stringValue); | |
387 } | |
388 | |
389 return true; | |
390 } | |
391 | |
392 bool Dictionary::convert(ConversionContext& context, const String& key, HashSet<
AtomicString>& value) const | |
393 { | |
394 ConversionContextScope scope(context); | |
395 | |
396 v8::Local<v8::Value> v8Value; | |
397 if (!getKey(key, v8Value)) | |
398 return true; | |
399 | |
400 if (context.isNullable() && WebCore::isUndefinedOrNull(v8Value)) | |
401 return true; | |
402 | |
403 if (!v8Value->IsArray()) { | |
404 context.throwTypeError(ExceptionMessages::notASequenceTypeProperty(key))
; | |
405 return false; | |
406 } | |
407 | |
408 return get(key, value); | |
409 } | |
410 | |
411 bool Dictionary::getWithUndefinedOrNullCheck(const String& key, String& value) c
onst | |
412 { | |
413 v8::Local<v8::Value> v8Value; | |
414 if (!getKey(key, v8Value) || WebCore::isUndefinedOrNull(v8Value)) | |
415 return false; | |
416 | |
417 TOSTRING_DEFAULT(V8StringResource<>, stringValue, v8Value, false); | |
418 value = stringValue; | |
419 return true; | |
420 } | |
421 | |
422 bool Dictionary::getWithUndefinedOrNullCheck(const String& key, RefPtrWillBeMemb
er<Element>& value) const | |
423 { | |
424 v8::Local<v8::Value> v8Value; | |
425 if (!getKey(key, v8Value) || WebCore::isUndefinedOrNull(v8Value)) | |
426 return false; | |
427 | |
428 value = V8Element::toNativeWithTypeCheck(m_isolate, v8Value); | |
429 return true; | |
430 } | |
431 | |
432 bool Dictionary::get(const String& key, RefPtr<Uint8Array>& value) const | |
433 { | |
434 v8::Local<v8::Value> v8Value; | |
435 if (!getKey(key, v8Value)) | |
436 return false; | |
437 | |
438 value = V8Uint8Array::toNativeWithTypeCheck(m_isolate, v8Value); | |
439 return true; | |
440 } | |
441 | |
442 bool Dictionary::get(const String& key, RefPtr<ArrayBufferView>& value) const | |
443 { | |
444 v8::Local<v8::Value> v8Value; | |
445 if (!getKey(key, v8Value)) | |
446 return false; | |
447 | |
448 value = V8ArrayBufferView::toNativeWithTypeCheck(m_isolate, v8Value); | |
449 return true; | |
450 } | |
451 | |
452 bool Dictionary::get(const String& key, Member<MIDIPort>& value) const | |
453 { | |
454 v8::Local<v8::Value> v8Value; | |
455 if (!getKey(key, v8Value)) | |
456 return false; | |
457 | |
458 value = V8MIDIPort::toNativeWithTypeCheck(m_isolate, v8Value); | |
459 return true; | |
460 } | |
461 | |
462 bool Dictionary::get(const String& key, RefPtrWillBeMember<MediaKeyError>& value
) const | |
463 { | |
464 v8::Local<v8::Value> v8Value; | |
465 if (!getKey(key, v8Value)) | |
466 return false; | |
467 | |
468 value = V8MediaKeyError::toNativeWithTypeCheck(m_isolate, v8Value); | |
469 return true; | |
470 } | |
471 | |
472 bool Dictionary::get(const String& key, RefPtrWillBeMember<TrackBase>& value) co
nst | |
473 { | |
474 v8::Local<v8::Value> v8Value; | |
475 if (!getKey(key, v8Value)) | |
476 return false; | |
477 | |
478 TrackBase* source = 0; | |
479 if (v8Value->IsObject()) { | |
480 v8::Handle<v8::Object> wrapper = v8::Handle<v8::Object>::Cast(v8Value); | |
481 | |
482 // FIXME: this will need to be changed so it can also return an AudioTra
ck or a VideoTrack once | |
483 // we add them. | |
484 v8::Handle<v8::Object> track = V8TextTrack::findInstanceInPrototypeChain
(wrapper, m_isolate); | |
485 if (!track.IsEmpty()) | |
486 source = V8TextTrack::toNative(track); | |
487 } | |
488 value = source; | |
489 return true; | |
490 } | |
491 | |
492 bool Dictionary::get(const String& key, Member<SpeechRecognitionResult>& value)
const | |
493 { | |
494 v8::Local<v8::Value> v8Value; | |
495 if (!getKey(key, v8Value)) | |
496 return false; | |
497 | |
498 value = V8SpeechRecognitionResult::toNativeWithTypeCheck(m_isolate, v8Value)
; | |
499 return true; | |
500 } | |
501 | |
502 bool Dictionary::get(const String& key, Member<SpeechRecognitionResultList>& val
ue) const | |
503 { | |
504 v8::Local<v8::Value> v8Value; | |
505 if (!getKey(key, v8Value)) | |
506 return false; | |
507 | |
508 value = V8SpeechRecognitionResultList::toNativeWithTypeCheck(m_isolate, v8Va
lue); | |
509 return true; | |
510 } | |
511 | |
512 bool Dictionary::get(const String& key, Member<Gamepad>& value) const | |
513 { | |
514 v8::Local<v8::Value> v8Value; | |
515 if (!getKey(key, v8Value)) | |
516 return false; | |
517 | |
518 value = V8Gamepad::toNativeWithTypeCheck(m_isolate, v8Value); | |
519 return true; | |
520 } | |
521 | |
522 bool Dictionary::get(const String& key, Member<MediaStream>& value) const | |
523 { | |
524 v8::Local<v8::Value> v8Value; | |
525 if (!getKey(key, v8Value)) | |
526 return false; | |
527 | |
528 value = V8MediaStream::toNativeWithTypeCheck(m_isolate, v8Value); | |
529 return true; | |
530 } | |
531 | |
532 bool Dictionary::get(const String& key, RefPtrWillBeMember<EventTarget>& value)
const | |
533 { | |
534 v8::Local<v8::Value> v8Value; | |
535 if (!getKey(key, v8Value)) | |
536 return false; | |
537 | |
538 value = nullptr; | |
539 // We need to handle a LocalDOMWindow specially, because a LocalDOMWindow wr
apper | |
540 // exists on a prototype chain of v8Value. | |
541 if (v8Value->IsObject()) { | |
542 v8::Handle<v8::Object> wrapper = v8::Handle<v8::Object>::Cast(v8Value); | |
543 v8::Handle<v8::Object> window = V8Window::findInstanceInPrototypeChain(w
rapper, m_isolate); | |
544 if (!window.IsEmpty()) { | |
545 value = toWrapperTypeInfo(window)->toEventTarget(window); | |
546 return true; | |
547 } | |
548 } | |
549 | |
550 if (V8DOMWrapper::isDOMWrapper(v8Value)) { | |
551 v8::Handle<v8::Object> wrapper = v8::Handle<v8::Object>::Cast(v8Value); | |
552 value = toWrapperTypeInfo(wrapper)->toEventTarget(wrapper); | |
553 } | |
554 return true; | |
555 } | |
556 | |
557 bool Dictionary::get(const String& key, Dictionary& value) const | |
558 { | |
559 v8::Local<v8::Value> v8Value; | |
560 if (!getKey(key, v8Value)) | |
561 return false; | |
562 | |
563 if (v8Value->IsObject()) { | |
564 ASSERT(m_isolate); | |
565 ASSERT(m_isolate == v8::Isolate::GetCurrent()); | |
566 value = Dictionary(v8Value, m_isolate); | |
567 } | |
568 | |
569 return true; | |
570 } | |
571 | |
572 bool Dictionary::get(const String& key, RefPtr<HeaderMap>& value) const | |
573 { | |
574 v8::Local<v8::Value> v8Value; | |
575 if (!getKey(key, v8Value)) | |
576 return false; | |
577 | |
578 value = V8HeaderMap::toNativeWithTypeCheck(m_isolate, v8Value); | |
579 return true; | |
580 } | |
581 | |
582 bool Dictionary::get(const String& key, RefPtr<Headers>& value) const | |
583 { | |
584 v8::Local<v8::Value> v8Value; | |
585 if (!getKey(key, v8Value)) | |
586 return false; | |
587 | |
588 value = V8Headers::toNativeWithTypeCheck(m_isolate, v8Value); | |
589 return true; | |
590 } | |
591 | |
592 bool Dictionary::convert(ConversionContext& context, const String& key, Dictiona
ry& value) const | |
593 { | |
594 ConversionContextScope scope(context); | |
595 | |
596 v8::Local<v8::Value> v8Value; | |
597 if (!getKey(key, v8Value)) | |
598 return true; | |
599 | |
600 if (v8Value->IsObject()) | |
601 return get(key, value); | |
602 | |
603 if (context.isNullable() && WebCore::isUndefinedOrNull(v8Value)) | |
604 return true; | |
605 | |
606 context.throwTypeError(ExceptionMessages::incorrectPropertyType(key, "does n
ot have a Dictionary type.")); | |
607 return false; | |
608 } | |
609 | |
610 bool Dictionary::get(const String& key, Vector<String>& value) const | |
611 { | |
612 v8::Local<v8::Value> v8Value; | |
613 if (!getKey(key, v8Value)) | |
614 return false; | |
615 | |
616 if (!v8Value->IsArray()) | |
617 return false; | |
618 | |
619 v8::Local<v8::Array> v8Array = v8::Local<v8::Array>::Cast(v8Value); | |
620 for (size_t i = 0; i < v8Array->Length(); ++i) { | |
621 v8::Local<v8::Value> indexedValue = v8Array->Get(v8::Uint32::New(m_isola
te, i)); | |
622 TOSTRING_DEFAULT(V8StringResource<>, stringValue, indexedValue, false); | |
623 value.append(stringValue); | |
624 } | |
625 | |
626 return true; | |
627 } | |
628 | |
629 bool Dictionary::convert(ConversionContext& context, const String& key, Vector<S
tring>& value) const | |
630 { | |
631 ConversionContextScope scope(context); | |
632 | |
633 v8::Local<v8::Value> v8Value; | |
634 if (!getKey(key, v8Value)) | |
635 return true; | |
636 | |
637 if (context.isNullable() && WebCore::isUndefinedOrNull(v8Value)) | |
638 return true; | |
639 | |
640 if (!v8Value->IsArray()) { | |
641 context.throwTypeError(ExceptionMessages::notASequenceTypeProperty(key))
; | |
642 return false; | |
643 } | |
644 | |
645 return get(key, value); | |
646 } | |
647 | |
648 bool Dictionary::get(const String& key, ArrayValue& value) const | |
649 { | |
650 v8::Local<v8::Value> v8Value; | |
651 if (!getKey(key, v8Value)) | |
652 return false; | |
653 | |
654 if (!v8Value->IsArray()) | |
655 return false; | |
656 | |
657 ASSERT(m_isolate); | |
658 ASSERT(m_isolate == v8::Isolate::GetCurrent()); | |
659 value = ArrayValue(v8::Local<v8::Array>::Cast(v8Value), m_isolate); | |
660 return true; | |
661 } | |
662 | |
663 bool Dictionary::convert(ConversionContext& context, const String& key, ArrayVal
ue& value) const | |
664 { | |
665 ConversionContextScope scope(context); | |
666 | |
667 v8::Local<v8::Value> v8Value; | |
668 if (!getKey(key, v8Value)) | |
669 return true; | |
670 | |
671 if (context.isNullable() && WebCore::isUndefinedOrNull(v8Value)) | |
672 return true; | |
673 | |
674 if (!v8Value->IsArray()) { | |
675 context.throwTypeError(ExceptionMessages::notASequenceTypeProperty(key))
; | |
676 return false; | |
677 } | |
678 | |
679 return get(key, value); | |
680 } | |
681 | |
682 bool Dictionary::get(const String& key, RefPtrWillBeMember<DOMError>& value) con
st | |
683 { | |
684 v8::Local<v8::Value> v8Value; | |
685 if (!getKey(key, v8Value)) | |
686 return false; | |
687 | |
688 value = V8DOMError::toNativeWithTypeCheck(m_isolate, v8Value); | |
689 return true; | |
690 } | |
691 | |
692 bool Dictionary::getOwnPropertiesAsStringHashMap(HashMap<String, String>& hashMa
p) const | |
693 { | |
694 if (!isObject()) | |
695 return false; | |
696 | |
697 v8::Handle<v8::Object> options = m_options->ToObject(); | |
698 if (options.IsEmpty()) | |
699 return false; | |
700 | |
701 v8::Local<v8::Array> properties = options->GetOwnPropertyNames(); | |
702 if (properties.IsEmpty()) | |
703 return true; | |
704 for (uint32_t i = 0; i < properties->Length(); ++i) { | |
705 v8::Local<v8::String> key = properties->Get(i)->ToString(); | |
706 if (!options->Has(key)) | |
707 continue; | |
708 | |
709 v8::Local<v8::Value> value = options->Get(key); | |
710 TOSTRING_DEFAULT(V8StringResource<>, stringKey, key, false); | |
711 TOSTRING_DEFAULT(V8StringResource<>, stringValue, value, false); | |
712 if (!static_cast<const String&>(stringKey).isEmpty()) | |
713 hashMap.set(stringKey, stringValue); | |
714 } | |
715 | |
716 return true; | |
717 } | |
718 | |
719 bool Dictionary::getOwnPropertyNames(Vector<String>& names) const | |
720 { | |
721 if (!isObject()) | |
722 return false; | |
723 | |
724 v8::Handle<v8::Object> options = m_options->ToObject(); | |
725 if (options.IsEmpty()) | |
726 return false; | |
727 | |
728 v8::Local<v8::Array> properties = options->GetOwnPropertyNames(); | |
729 if (properties.IsEmpty()) | |
730 return true; | |
731 for (uint32_t i = 0; i < properties->Length(); ++i) { | |
732 v8::Local<v8::String> key = properties->Get(i)->ToString(); | |
733 if (!options->Has(key)) | |
734 continue; | |
735 TOSTRING_DEFAULT(V8StringResource<>, stringKey, key, false); | |
736 names.append(stringKey); | |
737 } | |
738 | |
739 return true; | |
740 } | |
741 | |
742 void Dictionary::ConversionContext::resetPerPropertyContext() | |
743 { | |
744 if (m_dirty) { | |
745 m_dirty = false; | |
746 m_isNullable = false; | |
747 m_propertyTypeName = ""; | |
748 } | |
749 } | |
750 | |
751 Dictionary::ConversionContext& Dictionary::ConversionContext::setConversionType(
const String& typeName, bool isNullable) | |
752 { | |
753 ASSERT(!m_dirty); | |
754 m_dirty = true; | |
755 m_isNullable = isNullable; | |
756 m_propertyTypeName = typeName; | |
757 | |
758 return *this; | |
759 } | |
760 | |
761 void Dictionary::ConversionContext::throwTypeError(const String& detail) | |
762 { | |
763 exceptionState().throwTypeError(detail); | |
764 } | |
765 | |
766 } // namespace WebCore | |
OLD | NEW |