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

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

Powered by Google App Engine
This is Rietveld 408576698