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

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

Issue 960193002: Clean up DictionaryHelperForCore.cpp (Closed) Base URL: https://chromium.googlesource.com/chromium/blink.git@master
Patch Set: Created 5 years, 9 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
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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 87 matching lines...) Expand 10 before | Expand all | Expand 10 after
98 template <> 98 template <>
99 bool DictionaryHelper::get(const Dictionary& dictionary, const String& key, doub le& value, bool& hasValue) 99 bool DictionaryHelper::get(const Dictionary& dictionary, const String& key, doub le& value, bool& hasValue)
100 { 100 {
101 v8::Local<v8::Value> v8Value; 101 v8::Local<v8::Value> v8Value;
102 if (!dictionary.get(key, v8Value)) { 102 if (!dictionary.get(key, v8Value)) {
103 hasValue = false; 103 hasValue = false;
104 return false; 104 return false;
105 } 105 }
106 106
107 hasValue = true; 107 hasValue = true;
108 TONATIVE_DEFAULT(v8::Local<v8::Number>, v8Number, v8Value->ToNumber(dictiona ry.isolate()), false); 108
109 if (v8Number.IsEmpty()) 109 v8::TryCatch block;
110 value = v8Value->NumberValue();
111 if (UNLIKELY(block.HasCaught())) {
112 block.ReThrow();
110 return false; 113 return false;
111 value = v8Number->Value(); 114 }
112 return true; 115 return true;
113 } 116 }
114 117
115 template <> 118 template <>
116 bool DictionaryHelper::get(const Dictionary& dictionary, const String& key, doub le& value) 119 bool DictionaryHelper::get(const Dictionary& dictionary, const String& key, doub le& value)
117 { 120 {
118 bool unused; 121 bool unused;
119 return DictionaryHelper::get(dictionary, key, value, unused); 122 return DictionaryHelper::get(dictionary, key, value, unused);
120 } 123 }
121 124
(...skipping 10 matching lines...) Expand all
132 return true; 135 return true;
133 } 136 }
134 137
135 template<typename StringType> 138 template<typename StringType>
136 bool getStringType(const Dictionary& dictionary, const String& key, StringType& value) 139 bool getStringType(const Dictionary& dictionary, const String& key, StringType& value)
137 { 140 {
138 v8::Local<v8::Value> v8Value; 141 v8::Local<v8::Value> v8Value;
139 if (!dictionary.get(key, v8Value)) 142 if (!dictionary.get(key, v8Value))
140 return false; 143 return false;
141 144
142 TOSTRING_DEFAULT(V8StringResource<>, stringValue, v8Value, false); 145 V8StringResource<> stringValue(v8Value);
146 if (!stringValue.prepare())
147 return false;
143 value = stringValue; 148 value = stringValue;
144 return true; 149 return true;
145 } 150 }
146 151
147 template <> 152 template <>
148 bool DictionaryHelper::get(const Dictionary& dictionary, const String& key, Stri ng& value) 153 bool DictionaryHelper::get(const Dictionary& dictionary, const String& key, Stri ng& value)
149 { 154 {
150 return getStringType(dictionary, key, value); 155 return getStringType(dictionary, key, value);
151 } 156 }
152 157
153 template <> 158 template <>
154 bool DictionaryHelper::get(const Dictionary& dictionary, const String& key, Atom icString& value) 159 bool DictionaryHelper::get(const Dictionary& dictionary, const String& key, Atom icString& value)
155 { 160 {
156 return getStringType(dictionary, key, value); 161 return getStringType(dictionary, key, value);
157 } 162 }
158 163
159 template <> 164 template <>
160 bool DictionaryHelper::convert(const Dictionary& dictionary, Dictionary::Convers ionContext& context, const String& key, String& value) 165 bool DictionaryHelper::convert(const Dictionary& dictionary, Dictionary::Convers ionContext& context, const String& key, String& value)
161 { 166 {
162 Dictionary::ConversionContextScope scope(context); 167 Dictionary::ConversionContextScope scope(context);
163 168
164 v8::Local<v8::Value> v8Value; 169 v8::Local<v8::Value> v8Value;
165 if (!dictionary.get(key, v8Value)) 170 if (!dictionary.get(key, v8Value))
166 return true; 171 return true;
167 172
168 TOSTRING_DEFAULT(V8StringResource<>, stringValue, v8Value, false); 173 V8StringResource<> stringValue(v8Value);
174 if (!stringValue.prepare())
175 return false;
169 value = stringValue; 176 value = stringValue;
170 return true; 177 return true;
171 } 178 }
172 179
173 template <> 180 template <>
174 bool DictionaryHelper::get(const Dictionary& dictionary, const String& key, Scri ptValue& value) 181 bool DictionaryHelper::get(const Dictionary& dictionary, const String& key, Scri ptValue& value)
175 { 182 {
176 v8::Local<v8::Value> v8Value; 183 v8::Local<v8::Value> v8Value;
177 if (!dictionary.get(key, v8Value)) 184 if (!dictionary.get(key, v8Value))
178 return false; 185 return false;
(...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after
236 return true; 243 return true;
237 } 244 }
238 245
239 template <> 246 template <>
240 bool DictionaryHelper::get(const Dictionary& dictionary, const String& key, unsi gned long long& value) 247 bool DictionaryHelper::get(const Dictionary& dictionary, const String& key, unsi gned long long& value)
241 { 248 {
242 v8::Local<v8::Value> v8Value; 249 v8::Local<v8::Value> v8Value;
243 if (!dictionary.get(key, v8Value)) 250 if (!dictionary.get(key, v8Value))
244 return false; 251 return false;
245 252
246 TONATIVE_DEFAULT(v8::Local<v8::Number>, v8Number, v8Value->ToNumber(dictiona ry.isolate()), false); 253 v8::TryCatch block;
247 if (v8Number.IsEmpty()) 254 double d = v8Value->NumberValue();
255 if (UNLIKELY(block.HasCaught())) {
256 block.ReThrow();
248 return false; 257 return false;
249 double d = v8Number->Value(); 258 }
250 doubleToInteger(d, value); 259 doubleToInteger(d, value);
251 return true; 260 return true;
252 } 261 }
253 262
254 template <> 263 template <>
255 bool DictionaryHelper::get(const Dictionary& dictionary, const String& key, RefP trWillBeMember<DOMWindow>& value) 264 bool DictionaryHelper::get(const Dictionary& dictionary, const String& key, RefP trWillBeMember<DOMWindow>& value)
256 { 265 {
257 v8::Local<v8::Value> v8Value; 266 v8::Local<v8::Value> v8Value;
258 if (!dictionary.get(key, v8Value)) 267 if (!dictionary.get(key, v8Value))
259 return false; 268 return false;
260 269
261 // We need to handle a DOMWindow specially, because a DOMWindow wrapper 270 // We need to handle a DOMWindow specially, because a DOMWindow wrapper
262 // exists on a prototype chain of v8Value. 271 // exists on a prototype chain of v8Value.
263 value = toDOMWindow(dictionary.isolate(), v8Value); 272 value = toDOMWindow(dictionary.isolate(), v8Value);
264 return true; 273 return true;
265 } 274 }
266 275
267 template <> 276 template <>
268 bool DictionaryHelper::get(const Dictionary& dictionary, const String& key, Hash Set<AtomicString>& value)
269 {
270 v8::Local<v8::Value> v8Value;
271 if (!dictionary.get(key, v8Value))
272 return false;
273
274 // FIXME: Support array-like objects
275 if (!v8Value->IsArray())
276 return false;
277
278 ASSERT(dictionary.isolate());
279 ASSERT(dictionary.isolate() == v8::Isolate::GetCurrent());
280 v8::Local<v8::Array> v8Array = v8::Local<v8::Array>::Cast(v8Value);
281 for (size_t i = 0; i < v8Array->Length(); ++i) {
282 v8::Local<v8::Value> indexedValue = v8Array->Get(v8::Integer::New(dictio nary.isolate(), i));
283 TOSTRING_DEFAULT(V8StringResource<>, stringValue, indexedValue, false);
284 value.add(stringValue);
285 }
286
287 return true;
288 }
289
290 template <>
291 bool DictionaryHelper::convert(const Dictionary& dictionary, Dictionary::Convers ionContext& context, const String& key, HashSet<AtomicString>& value)
292 {
293 Dictionary::ConversionContextScope scope(context);
294
295 v8::Local<v8::Value> v8Value;
296 if (!dictionary.get(key, v8Value))
297 return true;
298
299 if (context.isNullable() && blink::isUndefinedOrNull(v8Value))
300 return true;
301
302 if (!v8Value->IsArray()) {
303 context.throwTypeError(ExceptionMessages::notASequenceTypeProperty(key)) ;
304 return false;
305 }
306
307 return DictionaryHelper::get(dictionary, key, value);
308 }
309
310 template <>
311 bool DictionaryHelper::get(const Dictionary& dictionary, const String& key, RefP trWillBeMember<TrackBase>& value) 277 bool DictionaryHelper::get(const Dictionary& dictionary, const String& key, RefP trWillBeMember<TrackBase>& value)
312 { 278 {
313 v8::Local<v8::Value> v8Value; 279 v8::Local<v8::Value> v8Value;
314 if (!dictionary.get(key, v8Value)) 280 if (!dictionary.get(key, v8Value))
315 return false; 281 return false;
316 282
317 TrackBase* source = 0; 283 TrackBase* source = 0;
318 if (v8Value->IsObject()) { 284 if (v8Value->IsObject()) {
319 v8::Handle<v8::Object> wrapper = v8::Handle<v8::Object>::Cast(v8Value); 285 v8::Handle<v8::Object> wrapper = v8::Handle<v8::Object>::Cast(v8Value);
320 286
(...skipping 129 matching lines...) Expand 10 before | Expand all | Expand 10 after
450 } 416 }
451 417
452 return DictionaryHelper::get(dictionary, key, value); 418 return DictionaryHelper::get(dictionary, key, value);
453 } 419 }
454 420
455 template <> 421 template <>
456 struct DictionaryHelperTraits<DOMUint8Array> { 422 struct DictionaryHelperTraits<DOMUint8Array> {
457 typedef V8Uint8Array type; 423 typedef V8Uint8Array type;
458 }; 424 };
459 425
460 template <>
461 struct DictionaryHelperTraits<DOMArrayBufferView> {
462 typedef V8ArrayBufferView type;
463 };
464
465 template <>
466 struct DictionaryHelperTraits<MediaKeyError> {
467 typedef V8MediaKeyError type;
468 };
469
470 template <>
471 struct DictionaryHelperTraits<DOMError> {
472 typedef V8DOMError type;
473 };
474
475 template <>
476 struct DictionaryHelperTraits<Storage> {
477 typedef V8Storage type;
478 };
479
480 template <>
481 struct DictionaryHelperTraits<Element> {
482 typedef V8Element type;
483 };
484
485 template <>
486 struct DictionaryHelperTraits<Path2D> {
487 typedef V8Path2D type;
488 };
489
490 template bool DictionaryHelper::get(const Dictionary&, const String& key, RefPtr <DOMUint8Array>& value); 426 template bool DictionaryHelper::get(const Dictionary&, const String& key, RefPtr <DOMUint8Array>& value);
491 template bool DictionaryHelper::get(const Dictionary&, const String& key, RefPtr <DOMArrayBufferView>& value);
492 template bool DictionaryHelper::get(const Dictionary&, const String& key, RefPtr WillBeMember<MediaKeyError>& value);
493 template bool DictionaryHelper::get(const Dictionary&, const String& key, Member <DOMError>& value);
494 template bool DictionaryHelper::get(const Dictionary&, const String& key, RefPtr WillBeMember<Storage>& value);
495 template bool DictionaryHelper::get(const Dictionary&, const String& key, RefPtr WillBeMember<Element>& value);
496 template bool DictionaryHelper::get(const Dictionary&, const String& key, RawPtr <Element>& value);
497 template bool DictionaryHelper::get(const Dictionary&, const String& key, RefPtr WillBeMember<Path2D>& value);
498 template bool DictionaryHelper::get(const Dictionary&, const String& key, RawPtr <Path2D>& value);
499 427
500 template <typename T> 428 template <typename T>
501 struct IntegralTypeTraits { 429 struct IntegralTypeTraits {
502 }; 430 };
503 431
504 template <> 432 template <>
505 struct IntegralTypeTraits<uint8_t> { 433 struct IntegralTypeTraits<uint8_t> {
506 static inline uint8_t toIntegral(v8::Handle<v8::Value> value, IntegerConvers ionConfiguration configuration, ExceptionState& exceptionState) 434 static inline uint8_t toIntegral(v8::Handle<v8::Value> value, IntegerConvers ionConfiguration configuration, ExceptionState& exceptionState)
507 { 435 {
508 return toUInt8(value, configuration, exceptionState); 436 return toUInt8(value, configuration, exceptionState);
(...skipping 95 matching lines...) Expand 10 before | Expand all | Expand 10 after
604 if (context.exceptionState().throwIfNeeded()) 532 if (context.exceptionState().throwIfNeeded())
605 return false; 533 return false;
606 534
607 return true; 535 return true;
608 } 536 }
609 537
610 template bool DictionaryHelper::convert(const Dictionary&, Dictionary::Conversio nContext&, const String& key, uint8_t& value); 538 template bool DictionaryHelper::convert(const Dictionary&, Dictionary::Conversio nContext&, const String& key, uint8_t& value);
611 template bool DictionaryHelper::convert(const Dictionary&, Dictionary::Conversio nContext&, const String& key, int8_t& value); 539 template bool DictionaryHelper::convert(const Dictionary&, Dictionary::Conversio nContext&, const String& key, int8_t& value);
612 template bool DictionaryHelper::convert(const Dictionary&, Dictionary::Conversio nContext&, const String& key, unsigned short& value); 540 template bool DictionaryHelper::convert(const Dictionary&, Dictionary::Conversio nContext&, const String& key, unsigned short& value);
613 template bool DictionaryHelper::convert(const Dictionary&, Dictionary::Conversio nContext&, const String& key, short& value); 541 template bool DictionaryHelper::convert(const Dictionary&, Dictionary::Conversio nContext&, const String& key, short& value);
614 template bool DictionaryHelper::convert(const Dictionary&, Dictionary::Conversio nContext&, const String& key, unsigned& value); 542 template bool DictionaryHelper::convert(const Dictionary&, Dictionary::Conversio nContext&, const String& key, unsigned& value);
Jens Widell 2015/02/26 13:30:40 This is the only one of these that is actually req
615 template bool DictionaryHelper::convert(const Dictionary&, Dictionary::Conversio nContext&, const String& key, unsigned long& value); 543 template bool DictionaryHelper::convert(const Dictionary&, Dictionary::Conversio nContext&, const String& key, unsigned long& value);
616 template bool DictionaryHelper::convert(const Dictionary&, Dictionary::Conversio nContext&, const String& key, int& value); 544 template bool DictionaryHelper::convert(const Dictionary&, Dictionary::Conversio nContext&, const String& key, int& value);
617 template bool DictionaryHelper::convert(const Dictionary&, Dictionary::Conversio nContext&, const String& key, long& value); 545 template bool DictionaryHelper::convert(const Dictionary&, Dictionary::Conversio nContext&, const String& key, long& value);
618 template bool DictionaryHelper::convert(const Dictionary&, Dictionary::Conversio nContext&, const String& key, long long& value); 546 template bool DictionaryHelper::convert(const Dictionary&, Dictionary::Conversio nContext&, const String& key, long long& value);
619 template bool DictionaryHelper::convert(const Dictionary&, Dictionary::Conversio nContext&, const String& key, unsigned long long& value); 547 template bool DictionaryHelper::convert(const Dictionary&, Dictionary::Conversio nContext&, const String& key, unsigned long long& value);
620 548
621 template<typename T>
622 bool DictionaryHelper::convert(const Dictionary& dictionary, Dictionary::Convers ionContext& context, const String& key, Nullable<T>& value)
623 {
624 Dictionary::ConversionContextScope scope(context);
625
626 v8::Local<v8::Value> v8Value;
627 if (!dictionary.get(key, v8Value))
628 return true;
629
630 if (context.isNullable() && blink::isUndefinedOrNull(v8Value)) {
631 value = nullptr;
632 return true;
633 }
634
635 T converted = IntegralTypeTraits<T>::toIntegral(v8Value, NormalConversion, c ontext.exceptionState());
636
637 if (context.exceptionState().throwIfNeeded())
638 return false;
639
640 value = Nullable<T>(converted);
641 return true;
642 }
643
644 template bool DictionaryHelper::convert(const Dictionary&, Dictionary::Conversio nContext&, const String& key, Nullable<uint8_t>& value);
645 template bool DictionaryHelper::convert(const Dictionary&, Dictionary::Conversio nContext&, const String& key, Nullable<int8_t>& value);
646 template bool DictionaryHelper::convert(const Dictionary&, Dictionary::Conversio nContext&, const String& key, Nullable<unsigned short>& value);
647 template bool DictionaryHelper::convert(const Dictionary&, Dictionary::Conversio nContext&, const String& key, Nullable<short>& value);
648 template bool DictionaryHelper::convert(const Dictionary&, Dictionary::Conversio nContext&, const String& key, Nullable<unsigned>& value);
649 template bool DictionaryHelper::convert(const Dictionary&, Dictionary::Conversio nContext&, const String& key, Nullable<unsigned long>& value);
650 template bool DictionaryHelper::convert(const Dictionary&, Dictionary::Conversio nContext&, const String& key, Nullable<int>& value);
651 template bool DictionaryHelper::convert(const Dictionary&, Dictionary::Conversio nContext&, const String& key, Nullable<long>& value);
652 template bool DictionaryHelper::convert(const Dictionary&, Dictionary::Conversio nContext&, const String& key, Nullable<long long>& value);
653 template bool DictionaryHelper::convert(const Dictionary&, Dictionary::Conversio nContext&, const String& key, Nullable<unsigned long long>& value);
654
655 template bool DictionaryHelper::convert(const Dictionary&, Dictionary::Conversio nContext&, const String& key, RefPtrWillBeMember<DOMWindow>& value);
656 template bool DictionaryHelper::convert(const Dictionary&, Dictionary::Conversio nContext&, const String& key, RefPtrWillBeMember<Storage>& value);
657 template bool DictionaryHelper::convert(const Dictionary&, Dictionary::Conversio nContext&, const String& key, RefPtr<DOMUint8Array>& value);
658 template bool DictionaryHelper::convert(const Dictionary&, Dictionary::Conversio nContext&, const String& key, RefPtr<DOMArrayBufferView>& value);
659 template bool DictionaryHelper::convert(const Dictionary&, Dictionary::Conversio nContext&, const String& key, RefPtrWillBeMember<MediaKeyError>& value);
660 template bool DictionaryHelper::convert(const Dictionary&, Dictionary::Conversio nContext&, const String& key, RefPtrWillBeMember<TrackBase>& value);
661 template bool DictionaryHelper::convert(const Dictionary&, Dictionary::Conversio nContext&, const String& key, RefPtrWillBeMember<EventTarget>& value); 549 template bool DictionaryHelper::convert(const Dictionary&, Dictionary::Conversio nContext&, const String& key, RefPtrWillBeMember<EventTarget>& value);
662 550
663 template <> 551 template <>
664 bool DictionaryHelper::convert(const Dictionary& dictionary, Dictionary::Convers ionContext& context, const String& key, MessagePortArray& value) 552 bool DictionaryHelper::convert(const Dictionary& dictionary, Dictionary::Convers ionContext& context, const String& key, MessagePortArray& value)
665 { 553 {
666 Dictionary::ConversionContextScope scope(context); 554 Dictionary::ConversionContextScope scope(context);
667 555
668 v8::Local<v8::Value> v8Value; 556 v8::Local<v8::Value> v8Value;
669 if (!dictionary.get(key, v8Value)) 557 if (!dictionary.get(key, v8Value))
670 return true; 558 return true;
671 559
672 ASSERT(dictionary.isolate()); 560 ASSERT(dictionary.isolate());
673 ASSERT(dictionary.isolate() == v8::Isolate::GetCurrent()); 561 ASSERT(dictionary.isolate() == v8::Isolate::GetCurrent());
674 562
675 if (isUndefinedOrNull(v8Value)) 563 if (isUndefinedOrNull(v8Value))
676 return true; 564 return true;
677 565
678 value = toRefPtrWillBeMemberNativeArray<MessagePort, V8MessagePort>(v8Value, key, dictionary.isolate(), context.exceptionState()); 566 value = toRefPtrWillBeMemberNativeArray<MessagePort, V8MessagePort>(v8Value, key, dictionary.isolate(), context.exceptionState());
679 567
680 if (context.exceptionState().throwIfNeeded()) 568 if (context.exceptionState().throwIfNeeded())
681 return false; 569 return false;
682 570
683 return true; 571 return true;
684 } 572 }
685 573
686 } // namespace blink 574 } // namespace blink
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698