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

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

Issue 373423002: Split Dictionary's get and convert into DictionaryHelper. (Closed) Base URL: svn://svn.chromium.org/blink/trunk
Patch Set: Fixed LICENSE and windows build 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 121 matching lines...) Expand 10 before | Expand all | Expand 10 after
132 if (value.IsEmpty()) 132 if (value.IsEmpty())
133 return false; 133 return false;
134 return true; 134 return true;
135 } 135 }
136 136
137 bool Dictionary::get(const String& key, v8::Local<v8::Value>& value) const 137 bool Dictionary::get(const String& key, v8::Local<v8::Value>& value) const
138 { 138 {
139 return getKey(key, value); 139 return getKey(key, value);
140 } 140 }
141 141
142 bool Dictionary::get(const String& key, bool& value) const
143 {
144 v8::Local<v8::Value> v8Value;
145 if (!getKey(key, v8Value))
146 return false;
147
148 v8::Local<v8::Boolean> v8Bool = v8Value->ToBoolean();
149 if (v8Bool.IsEmpty())
150 return false;
151 value = v8Bool->Value();
152 return true;
153 }
154
155 bool Dictionary::convert(ConversionContext& context, const String& key, bool& va lue) const
156 {
157 ConversionContextScope scope(context);
158 get(key, value);
159 return true;
160 }
161
162 bool Dictionary::get(const String& key, int32_t& value) const
163 {
164 v8::Local<v8::Value> v8Value;
165 if (!getKey(key, v8Value))
166 return false;
167
168 v8::Local<v8::Int32> v8Int32 = v8Value->ToInt32();
169 if (v8Int32.IsEmpty())
170 return false;
171 value = v8Int32->Value();
172 return true;
173 }
174
175 bool Dictionary::get(const String& key, double& value, bool& hasValue) const
176 {
177 v8::Local<v8::Value> v8Value;
178 if (!getKey(key, v8Value)) {
179 hasValue = false;
180 return false;
181 }
182
183 hasValue = true;
184 TONATIVE_DEFAULT(v8::Local<v8::Number>, v8Number, v8Value->ToNumber(), false );
185 if (v8Number.IsEmpty())
186 return false;
187 value = v8Number->Value();
188 return true;
189 }
190
191 bool Dictionary::get(const String& key, double& value) const
192 {
193 bool unused;
194 return get(key, value, unused);
195 }
196
197 bool Dictionary::convert(ConversionContext& context, const String& key, double& value) const
198 {
199 ConversionContextScope scope(context);
200
201 bool hasValue = false;
202 if (!get(key, value, hasValue) && hasValue) {
203 context.throwTypeError(ExceptionMessages::incorrectPropertyType(key, "is not of type 'double'."));
204 return false;
205 }
206 return true;
207 }
208
209 template<typename StringType>
210 inline bool Dictionary::getStringType(const String& key, StringType& value) cons t
211 {
212 v8::Local<v8::Value> v8Value;
213 if (!getKey(key, v8Value))
214 return false;
215
216 TOSTRING_DEFAULT(V8StringResource<>, stringValue, v8Value, false);
217 value = stringValue;
218 return true;
219 }
220
221 bool Dictionary::get(const String& key, String& value) const
222 {
223 return getStringType(key, value);
224 }
225
226 bool Dictionary::get(const String& key, AtomicString& value) const
227 {
228 return getStringType(key, value);
229 }
230
231 bool Dictionary::convert(ConversionContext& context, const String& key, String& value) const
232 {
233 ConversionContextScope scope(context);
234
235 v8::Local<v8::Value> v8Value;
236 if (!getKey(key, v8Value))
237 return true;
238
239 TOSTRING_DEFAULT(V8StringResource<>, stringValue, v8Value, false);
240 value = stringValue;
241 return true;
242 }
243
244 bool Dictionary::get(const String& key, ScriptValue& value) const
245 {
246 v8::Local<v8::Value> v8Value;
247 if (!getKey(key, v8Value))
248 return false;
249
250 value = ScriptValue(ScriptState::current(m_isolate), v8Value);
251 return true;
252 }
253
254 bool Dictionary::convert(ConversionContext& context, const String& key, ScriptVa lue& value) const
255 {
256 ConversionContextScope scope(context);
257
258 get(key, value);
259 return true;
260 }
261
262 bool Dictionary::get(const String& key, unsigned short& value) const
263 {
264 v8::Local<v8::Value> v8Value;
265 if (!getKey(key, v8Value))
266 return false;
267
268 v8::Local<v8::Int32> v8Int32 = v8Value->ToInt32();
269 if (v8Int32.IsEmpty())
270 return false;
271 value = static_cast<unsigned short>(v8Int32->Value());
272 return true;
273 }
274
275 bool Dictionary::get(const String& key, short& value) const
276 {
277 v8::Local<v8::Value> v8Value;
278 if (!getKey(key, v8Value))
279 return false;
280
281 v8::Local<v8::Int32> v8Int32 = v8Value->ToInt32();
282 if (v8Int32.IsEmpty())
283 return false;
284 value = static_cast<short>(v8Int32->Value());
285 return true;
286 }
287
288 bool Dictionary::get(const String& key, unsigned& value) const
289 {
290 v8::Local<v8::Value> v8Value;
291 if (!getKey(key, v8Value))
292 return false;
293
294 v8::Local<v8::Int32> v8Int32 = v8Value->ToInt32();
295 if (v8Int32.IsEmpty())
296 return false;
297 value = static_cast<unsigned>(v8Int32->Value());
298 return true;
299 }
300
301 bool Dictionary::get(const String& key, unsigned long& value) const
302 {
303 v8::Local<v8::Value> v8Value;
304 if (!getKey(key, v8Value))
305 return false;
306
307 v8::Local<v8::Integer> v8Integer = v8Value->ToInteger();
308 if (v8Integer.IsEmpty())
309 return false;
310 value = static_cast<unsigned long>(v8Integer->Value());
311 return true;
312 }
313
314 bool Dictionary::get(const String& key, unsigned long long& value) const
315 {
316 v8::Local<v8::Value> v8Value;
317 if (!getKey(key, v8Value))
318 return false;
319
320 TONATIVE_DEFAULT(v8::Local<v8::Number>, v8Number, v8Value->ToNumber(), false );
321 if (v8Number.IsEmpty())
322 return false;
323 double d = v8Number->Value();
324 doubleToInteger(d, value);
325 return true;
326 }
327
328 bool Dictionary::get(const String& key, RefPtrWillBeMember<LocalDOMWindow>& valu e) const
329 {
330 v8::Local<v8::Value> v8Value;
331 if (!getKey(key, v8Value))
332 return false;
333
334 // We need to handle a DOMWindow specially, because a DOMWindow wrapper
335 // exists on a prototype chain of v8Value.
336 value = toDOMWindow(v8Value, m_isolate);
337 return true;
338 }
339
340 bool Dictionary::get(const String& key, RefPtrWillBeMember<Storage>& value) cons t
341 {
342 v8::Local<v8::Value> v8Value;
343 if (!getKey(key, v8Value))
344 return false;
345
346 value = V8Storage::toNativeWithTypeCheck(m_isolate, v8Value);
347 return true;
348 }
349
350 bool Dictionary::get(const String& key, MessagePortArray& value) const
351 {
352 v8::Local<v8::Value> v8Value;
353 if (!getKey(key, v8Value))
354 return false;
355
356 ASSERT(m_isolate);
357 ASSERT(m_isolate == v8::Isolate::GetCurrent());
358 if (WebCore::isUndefinedOrNull(v8Value))
359 return true;
360 bool success = false;
361 value = toRefPtrWillBeMemberNativeArray<MessagePort, V8MessagePort>(v8Value, key, m_isolate, &success);
362 return success;
363 }
364
365 bool Dictionary::convert(ConversionContext& context, const String& key, MessageP ortArray& value) const
366 {
367 ConversionContextScope scope(context);
368
369 v8::Local<v8::Value> v8Value;
370 if (!getKey(key, v8Value))
371 return true;
372
373 return get(key, value);
374 }
375
376 bool Dictionary::get(const String& key, HashSet<AtomicString>& value) const
377 {
378 v8::Local<v8::Value> v8Value;
379 if (!getKey(key, v8Value))
380 return false;
381
382 // FIXME: Support array-like objects
383 if (!v8Value->IsArray())
384 return false;
385
386 ASSERT(m_isolate);
387 ASSERT(m_isolate == v8::Isolate::GetCurrent());
388 v8::Local<v8::Array> v8Array = v8::Local<v8::Array>::Cast(v8Value);
389 for (size_t i = 0; i < v8Array->Length(); ++i) {
390 v8::Local<v8::Value> indexedValue = v8Array->Get(v8::Integer::New(m_isol ate, i));
391 TOSTRING_DEFAULT(V8StringResource<>, stringValue, indexedValue, false);
392 value.add(stringValue);
393 }
394
395 return true;
396 }
397
398 bool Dictionary::convert(ConversionContext& context, const String& key, HashSet< AtomicString>& value) const
399 {
400 ConversionContextScope scope(context);
401
402 v8::Local<v8::Value> v8Value;
403 if (!getKey(key, v8Value))
404 return true;
405
406 if (context.isNullable() && WebCore::isUndefinedOrNull(v8Value))
407 return true;
408
409 if (!v8Value->IsArray()) {
410 context.throwTypeError(ExceptionMessages::notASequenceTypeProperty(key)) ;
411 return false;
412 }
413
414 return get(key, value);
415 }
416
417 bool Dictionary::getWithUndefinedOrNullCheck(const String& key, String& value) c onst 142 bool Dictionary::getWithUndefinedOrNullCheck(const String& key, String& value) c onst
418 { 143 {
419 v8::Local<v8::Value> v8Value; 144 v8::Local<v8::Value> v8Value;
420 if (!getKey(key, v8Value) || WebCore::isUndefinedOrNull(v8Value)) 145 if (!getKey(key, v8Value) || WebCore::isUndefinedOrNull(v8Value))
421 return false; 146 return false;
422 147
423 TOSTRING_DEFAULT(V8StringResource<>, stringValue, v8Value, false); 148 TOSTRING_DEFAULT(V8StringResource<>, stringValue, v8Value, false);
424 value = stringValue; 149 value = stringValue;
425 return true; 150 return true;
426 } 151 }
(...skipping 11 matching lines...) Expand all
438 bool Dictionary::getWithUndefinedOrNullCheck(const String& key, RefPtr<Path2D>& value) const 163 bool Dictionary::getWithUndefinedOrNullCheck(const String& key, RefPtr<Path2D>& value) const
439 { 164 {
440 v8::Local<v8::Value> v8Value; 165 v8::Local<v8::Value> v8Value;
441 if (!getKey(key, v8Value) || WebCore::isUndefinedOrNull(v8Value)) 166 if (!getKey(key, v8Value) || WebCore::isUndefinedOrNull(v8Value))
442 return false; 167 return false;
443 168
444 value = V8Path2D::toNativeWithTypeCheck(m_isolate, v8Value); 169 value = V8Path2D::toNativeWithTypeCheck(m_isolate, v8Value);
445 return true; 170 return true;
446 } 171 }
447 172
448 bool Dictionary::get(const String& key, RefPtr<Uint8Array>& value) const
449 {
450 v8::Local<v8::Value> v8Value;
451 if (!getKey(key, v8Value))
452 return false;
453
454 value = V8Uint8Array::toNativeWithTypeCheck(m_isolate, v8Value);
455 return true;
456 }
457
458 bool Dictionary::get(const String& key, RefPtr<ArrayBufferView>& value) const
459 {
460 v8::Local<v8::Value> v8Value;
461 if (!getKey(key, v8Value))
462 return false;
463
464 value = V8ArrayBufferView::toNativeWithTypeCheck(m_isolate, v8Value);
465 return true;
466 }
467
468 bool Dictionary::get(const String& key, Member<MIDIPort>& value) const
469 {
470 v8::Local<v8::Value> v8Value;
471 if (!getKey(key, v8Value))
472 return false;
473
474 value = V8MIDIPort::toNativeWithTypeCheck(m_isolate, v8Value);
475 return true;
476 }
477
478 bool Dictionary::get(const String& key, RefPtrWillBeMember<MediaKeyError>& value ) const
479 {
480 v8::Local<v8::Value> v8Value;
481 if (!getKey(key, v8Value))
482 return false;
483
484 value = V8MediaKeyError::toNativeWithTypeCheck(m_isolate, v8Value);
485 return true;
486 }
487
488 bool Dictionary::get(const String& key, RefPtrWillBeMember<TrackBase>& value) co nst
489 {
490 v8::Local<v8::Value> v8Value;
491 if (!getKey(key, v8Value))
492 return false;
493
494 TrackBase* source = 0;
495 if (v8Value->IsObject()) {
496 v8::Handle<v8::Object> wrapper = v8::Handle<v8::Object>::Cast(v8Value);
497
498 // FIXME: this will need to be changed so it can also return an AudioTra ck or a VideoTrack once
499 // we add them.
500 v8::Handle<v8::Object> track = V8TextTrack::findInstanceInPrototypeChain (wrapper, m_isolate);
501 if (!track.IsEmpty())
502 source = V8TextTrack::toNative(track);
503 }
504 value = source;
505 return true;
506 }
507
508 bool Dictionary::get(const String& key, Member<SpeechRecognitionResult>& value) const
509 {
510 v8::Local<v8::Value> v8Value;
511 if (!getKey(key, v8Value))
512 return false;
513
514 value = V8SpeechRecognitionResult::toNativeWithTypeCheck(m_isolate, v8Value) ;
515 return true;
516 }
517
518 bool Dictionary::get(const String& key, Member<SpeechRecognitionResultList>& val ue) const
519 {
520 v8::Local<v8::Value> v8Value;
521 if (!getKey(key, v8Value))
522 return false;
523
524 value = V8SpeechRecognitionResultList::toNativeWithTypeCheck(m_isolate, v8Va lue);
525 return true;
526 }
527
528 bool Dictionary::get(const String& key, Member<Gamepad>& value) const
529 {
530 v8::Local<v8::Value> v8Value;
531 if (!getKey(key, v8Value))
532 return false;
533
534 value = V8Gamepad::toNativeWithTypeCheck(m_isolate, v8Value);
535 return true;
536 }
537
538 bool Dictionary::get(const String& key, Member<MediaStream>& value) const
539 {
540 v8::Local<v8::Value> v8Value;
541 if (!getKey(key, v8Value))
542 return false;
543
544 value = V8MediaStream::toNativeWithTypeCheck(m_isolate, v8Value);
545 return true;
546 }
547
548 bool Dictionary::get(const String& key, RefPtrWillBeMember<EventTarget>& value) const
549 {
550 v8::Local<v8::Value> v8Value;
551 if (!getKey(key, v8Value))
552 return false;
553
554 value = nullptr;
555 // We need to handle a LocalDOMWindow specially, because a LocalDOMWindow wr apper
556 // exists on a prototype chain of v8Value.
557 if (v8Value->IsObject()) {
558 v8::Handle<v8::Object> wrapper = v8::Handle<v8::Object>::Cast(v8Value);
559 v8::Handle<v8::Object> window = V8Window::findInstanceInPrototypeChain(w rapper, m_isolate);
560 if (!window.IsEmpty()) {
561 value = toWrapperTypeInfo(window)->toEventTarget(window);
562 return true;
563 }
564 }
565
566 if (V8DOMWrapper::isDOMWrapper(v8Value)) {
567 v8::Handle<v8::Object> wrapper = v8::Handle<v8::Object>::Cast(v8Value);
568 value = toWrapperTypeInfo(wrapper)->toEventTarget(wrapper);
569 }
570 return true;
571 }
572
573 bool Dictionary::get(const String& key, Dictionary& value) const 173 bool Dictionary::get(const String& key, Dictionary& value) const
574 { 174 {
575 v8::Local<v8::Value> v8Value; 175 v8::Local<v8::Value> v8Value;
576 if (!getKey(key, v8Value)) 176 if (!getKey(key, v8Value))
577 return false; 177 return false;
578 178
579 if (v8Value->IsObject()) { 179 if (v8Value->IsObject()) {
580 ASSERT(m_isolate); 180 ASSERT(m_isolate);
581 ASSERT(m_isolate == v8::Isolate::GetCurrent()); 181 ASSERT(m_isolate == v8::Isolate::GetCurrent());
582 value = Dictionary(v8Value, m_isolate); 182 value = Dictionary(v8Value, m_isolate);
583 } 183 }
584 184
585 return true; 185 return true;
586 } 186 }
587 187
588 bool Dictionary::get(const String& key, RefPtr<HeaderMap>& value) const
589 {
590 v8::Local<v8::Value> v8Value;
591 if (!getKey(key, v8Value))
592 return false;
593
594 value = V8HeaderMap::toNativeWithTypeCheck(m_isolate, v8Value);
595 return true;
596 }
597
598 bool Dictionary::get(const String& key, RefPtr<Headers>& value) const
599 {
600 v8::Local<v8::Value> v8Value;
601 if (!getKey(key, v8Value))
602 return false;
603
604 value = V8Headers::toNativeWithTypeCheck(m_isolate, v8Value);
605 return true;
606 }
607
608 bool Dictionary::set(const String& key, const v8::Handle<v8::Value>& value) 188 bool Dictionary::set(const String& key, const v8::Handle<v8::Value>& value)
609 { 189 {
610 if (isUndefinedOrNull()) 190 if (isUndefinedOrNull())
611 return false; 191 return false;
612 v8::Local<v8::Object> options = m_options->ToObject(); 192 v8::Local<v8::Object> options = m_options->ToObject();
613 ASSERT(!options.IsEmpty()); 193 ASSERT(!options.IsEmpty());
614 194
615 return options->Set(v8String(m_isolate, key), value); 195 return options->Set(v8String(m_isolate, key), value);
616 } 196 }
617 197
(...skipping 23 matching lines...) Expand all
641 if (v8Value->IsObject()) 221 if (v8Value->IsObject())
642 return get(key, value); 222 return get(key, value);
643 223
644 if (context.isNullable() && WebCore::isUndefinedOrNull(v8Value)) 224 if (context.isNullable() && WebCore::isUndefinedOrNull(v8Value))
645 return true; 225 return true;
646 226
647 context.throwTypeError(ExceptionMessages::incorrectPropertyType(key, "does n ot have a Dictionary type.")); 227 context.throwTypeError(ExceptionMessages::incorrectPropertyType(key, "does n ot have a Dictionary type."));
648 return false; 228 return false;
649 } 229 }
650 230
651 bool Dictionary::get(const String& key, Vector<String>& value) const
652 {
653 v8::Local<v8::Value> v8Value;
654 if (!getKey(key, v8Value))
655 return false;
656
657 if (!v8Value->IsArray())
658 return false;
659
660 v8::Local<v8::Array> v8Array = v8::Local<v8::Array>::Cast(v8Value);
661 for (size_t i = 0; i < v8Array->Length(); ++i) {
662 v8::Local<v8::Value> indexedValue = v8Array->Get(v8::Uint32::New(m_isola te, i));
663 TOSTRING_DEFAULT(V8StringResource<>, stringValue, indexedValue, false);
664 value.append(stringValue);
665 }
666
667 return true;
668 }
669
670 bool Dictionary::convert(ConversionContext& context, const String& key, Vector<S tring>& value) const
671 {
672 ConversionContextScope scope(context);
673
674 v8::Local<v8::Value> v8Value;
675 if (!getKey(key, v8Value))
676 return true;
677
678 if (context.isNullable() && WebCore::isUndefinedOrNull(v8Value))
679 return true;
680
681 if (!v8Value->IsArray()) {
682 context.throwTypeError(ExceptionMessages::notASequenceTypeProperty(key)) ;
683 return false;
684 }
685
686 return get(key, value);
687 }
688
689 bool Dictionary::get(const String& key, ArrayValue& value) const
690 {
691 v8::Local<v8::Value> v8Value;
692 if (!getKey(key, v8Value))
693 return false;
694
695 if (!v8Value->IsArray())
696 return false;
697
698 ASSERT(m_isolate);
699 ASSERT(m_isolate == v8::Isolate::GetCurrent());
700 value = ArrayValue(v8::Local<v8::Array>::Cast(v8Value), m_isolate);
701 return true;
702 }
703
704 bool Dictionary::convert(ConversionContext& context, const String& key, ArrayVal ue& value) const
705 {
706 ConversionContextScope scope(context);
707
708 v8::Local<v8::Value> v8Value;
709 if (!getKey(key, v8Value))
710 return true;
711
712 if (context.isNullable() && WebCore::isUndefinedOrNull(v8Value))
713 return true;
714
715 if (!v8Value->IsArray()) {
716 context.throwTypeError(ExceptionMessages::notASequenceTypeProperty(key)) ;
717 return false;
718 }
719
720 return get(key, value);
721 }
722
723 bool Dictionary::get(const String& key, RefPtrWillBeMember<DOMError>& value) con st
724 {
725 v8::Local<v8::Value> v8Value;
726 if (!getKey(key, v8Value))
727 return false;
728
729 value = V8DOMError::toNativeWithTypeCheck(m_isolate, v8Value);
730 return true;
731 }
732
733 bool Dictionary::getOwnPropertiesAsStringHashMap(HashMap<String, String>& hashMa p) const 231 bool Dictionary::getOwnPropertiesAsStringHashMap(HashMap<String, String>& hashMa p) const
734 { 232 {
735 if (!isObject()) 233 if (!isObject())
736 return false; 234 return false;
737 235
738 v8::Handle<v8::Object> options = m_options->ToObject(); 236 v8::Handle<v8::Object> options = m_options->ToObject();
739 if (options.IsEmpty()) 237 if (options.IsEmpty())
740 return false; 238 return false;
741 239
742 v8::Local<v8::Array> properties = options->GetOwnPropertyNames(); 240 v8::Local<v8::Array> properties = options->GetOwnPropertyNames();
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after
798 296
799 return *this; 297 return *this;
800 } 298 }
801 299
802 void Dictionary::ConversionContext::throwTypeError(const String& detail) 300 void Dictionary::ConversionContext::throwTypeError(const String& detail)
803 { 301 {
804 exceptionState().throwTypeError(detail); 302 exceptionState().throwTypeError(detail);
805 } 303 }
806 304
807 } // namespace WebCore 305 } // namespace WebCore
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698