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

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

Issue 85263002: Improve handling of dictionary conversions. (Closed) Base URL: https://chromium.googlesource.com/chromium/blink.git@master
Patch Set: Created 7 years 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
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 73 matching lines...) Expand 10 before | Expand all | Expand 10 after
84 return !isUndefinedOrNull() && m_options->IsObject(); 84 return !isUndefinedOrNull() && m_options->IsObject();
85 } 85 }
86 86
87 bool Dictionary::isUndefinedOrNull() const 87 bool Dictionary::isUndefinedOrNull() const
88 { 88 {
89 if (m_options.IsEmpty()) 89 if (m_options.IsEmpty())
90 return true; 90 return true;
91 return WebCore::isUndefinedOrNull(m_options); 91 return WebCore::isUndefinedOrNull(m_options);
92 } 92 }
93 93
94 bool Dictionary::hasProperty(const String& key) const
95 {
96 if (isUndefinedOrNull())
97 return false;
98 v8::Local<v8::Object> options = m_options->ToObject();
99 ASSERT(!options.IsEmpty());
100
101 ASSERT(m_isolate);
102 ASSERT(m_isolate == v8::Isolate::GetCurrent());
103 v8::Handle<v8::String> v8Key = v8String(key, m_isolate);
104 if (!options->Has(v8Key))
105 return false;
106
107 return true;
108 }
109
94 bool Dictionary::getKey(const String& key, v8::Local<v8::Value>& value) const 110 bool Dictionary::getKey(const String& key, v8::Local<v8::Value>& value) const
95 { 111 {
96 if (isUndefinedOrNull()) 112 if (isUndefinedOrNull())
97 return false; 113 return false;
98 v8::Local<v8::Object> options = m_options->ToObject(); 114 v8::Local<v8::Object> options = m_options->ToObject();
99 ASSERT(!options.IsEmpty()); 115 ASSERT(!options.IsEmpty());
100 116
101 ASSERT(m_isolate); 117 ASSERT(m_isolate);
102 ASSERT(m_isolate == v8::Isolate::GetCurrent()); 118 ASSERT(m_isolate == v8::Isolate::GetCurrent());
103 v8::Handle<v8::String> v8Key = v8String(key, m_isolate); 119 v8::Handle<v8::String> v8Key = v8String(key, m_isolate);
(...skipping 16 matching lines...) Expand all
120 if (!getKey(key, v8Value)) 136 if (!getKey(key, v8Value))
121 return false; 137 return false;
122 138
123 v8::Local<v8::Boolean> v8Bool = v8Value->ToBoolean(); 139 v8::Local<v8::Boolean> v8Bool = v8Value->ToBoolean();
124 if (v8Bool.IsEmpty()) 140 if (v8Bool.IsEmpty())
125 return false; 141 return false;
126 value = v8Bool->Value(); 142 value = v8Bool->Value();
127 return true; 143 return true;
128 } 144 }
129 145
146 bool Dictionary::convert(const String& key, bool& value, ExceptionState&) const
147 {
148 get(key, value);
149 return true;
150 }
151
130 bool Dictionary::get(const String& key, int32_t& value) const 152 bool Dictionary::get(const String& key, int32_t& value) const
131 { 153 {
132 v8::Local<v8::Value> v8Value; 154 v8::Local<v8::Value> v8Value;
133 if (!getKey(key, v8Value)) 155 if (!getKey(key, v8Value))
134 return false; 156 return false;
135 157
136 v8::Local<v8::Int32> v8Int32 = v8Value->ToInt32(); 158 v8::Local<v8::Int32> v8Int32 = v8Value->ToInt32();
137 if (v8Int32.IsEmpty()) 159 if (v8Int32.IsEmpty())
138 return false; 160 return false;
139 value = v8Int32->Value(); 161 value = v8Int32->Value();
140 return true; 162 return true;
141 } 163 }
142 164
143 bool Dictionary::get(const String& key, double& value, bool& hasValue) const 165 bool Dictionary::get(const String& key, double& value, bool& hasValue) const
144 { 166 {
145 v8::Local<v8::Value> v8Value; 167 v8::Local<v8::Value> v8Value;
146 if (!getKey(key, v8Value)) { 168 if (!getKey(key, v8Value)) {
147 hasValue = false; 169 hasValue = false;
148 return false; 170 return false;
149 } 171 }
150 172
151 hasValue = true; 173 hasValue = true;
152 v8::Local<v8::Number> v8Number = v8Value->ToNumber(); 174 V8TRYCATCH_RETURN(v8::Local<v8::Number>, v8Number, v8Value->ToNumber(), fals e);
153 if (v8Number.IsEmpty()) 175 if (v8Number.IsEmpty())
154 return false; 176 return false;
155 value = v8Number->Value(); 177 value = v8Number->Value();
156 return true; 178 return true;
157 } 179 }
158 180
159 bool Dictionary::get(const String& key, double& value) const 181 bool Dictionary::get(const String& key, double& value) const
160 { 182 {
161 bool unused; 183 bool unused;
162 return get(key, value, unused); 184 return get(key, value, unused);
163 } 185 }
164 186
187 bool Dictionary::convert(const String& key, double& value, ExceptionState& excep tionState) const
188 {
189 bool hasValue = false;
190 if (!get(key, value, hasValue) && hasValue) {
191 exceptionState.throwTypeError(ExceptionMessages::illTypedProperty(key, " is not of type 'double'."));
Mike West 2013/11/25 08:22:48 So, as I noted in an earlier comment, I'd like to
sof 2013/11/25 11:57:23 I like the suggestion, we should do it. I'm reason
sof 2013/11/25 23:24:05 Done.
192 return false;
193 }
194 return true;
195 }
196
165 bool Dictionary::get(const String& key, String& value) const 197 bool Dictionary::get(const String& key, String& value) const
166 { 198 {
167 v8::Local<v8::Value> v8Value; 199 v8::Local<v8::Value> v8Value;
168 if (!getKey(key, v8Value)) 200 if (!getKey(key, v8Value))
169 return false; 201 return false;
170 202
171 V8TRYCATCH_FOR_V8STRINGRESOURCE_RETURN(V8StringResource<>, stringValue, v8Va lue, false); 203 V8TRYCATCH_FOR_V8STRINGRESOURCE_RETURN(V8StringResource<>, stringValue, v8Va lue, false);
172 value = stringValue; 204 value = stringValue;
173 return true; 205 return true;
174 } 206 }
175 207
208 bool Dictionary::convert(const String& key, String& value, ExceptionState& excep tionState) const
209 {
210 v8::Local<v8::Value> v8Value;
211 if (!getKey(key, v8Value))
212 return true;
213
214 V8TRYCATCH_FOR_V8STRINGRESOURCE_RETURN(V8StringResource<>, stringValue, v8Va lue, false);
215 value = stringValue;
216 return true;
217 }
218
176 bool Dictionary::get(const String& key, ScriptValue& value) const 219 bool Dictionary::get(const String& key, ScriptValue& value) const
177 { 220 {
178 v8::Local<v8::Value> v8Value; 221 v8::Local<v8::Value> v8Value;
179 if (!getKey(key, v8Value)) 222 if (!getKey(key, v8Value))
180 return false; 223 return false;
181 224
182 value = ScriptValue(v8Value, m_isolate); 225 value = ScriptValue(v8Value, m_isolate);
183 return true; 226 return true;
184 } 227 }
185 228
229 bool Dictionary::convert(const String& key, ScriptValue& value, ExceptionState&) const
230 {
231 get(key, value);
232 return true;
233 }
234
186 bool Dictionary::get(const String& key, unsigned short& value) const 235 bool Dictionary::get(const String& key, unsigned short& value) const
187 { 236 {
188 v8::Local<v8::Value> v8Value; 237 v8::Local<v8::Value> v8Value;
189 if (!getKey(key, v8Value)) 238 if (!getKey(key, v8Value))
190 return false; 239 return false;
191 240
192 v8::Local<v8::Int32> v8Int32 = v8Value->ToInt32(); 241 v8::Local<v8::Int32> v8Int32 = v8Value->ToInt32();
193 if (v8Int32.IsEmpty()) 242 if (v8Int32.IsEmpty())
194 return false; 243 return false;
195 value = static_cast<unsigned short>(v8Int32->Value()); 244 value = static_cast<unsigned short>(v8Int32->Value());
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after
234 value = static_cast<unsigned long>(v8Integer->Value()); 283 value = static_cast<unsigned long>(v8Integer->Value());
235 return true; 284 return true;
236 } 285 }
237 286
238 bool Dictionary::get(const String& key, unsigned long long& value) const 287 bool Dictionary::get(const String& key, unsigned long long& value) const
239 { 288 {
240 v8::Local<v8::Value> v8Value; 289 v8::Local<v8::Value> v8Value;
241 if (!getKey(key, v8Value)) 290 if (!getKey(key, v8Value))
242 return false; 291 return false;
243 292
244 v8::Local<v8::Number> v8Number = v8Value->ToNumber(); 293 V8TRYCATCH_RETURN(v8::Local<v8::Number>, v8Number, v8Value->ToNumber(), fals e);
245 if (v8Number.IsEmpty()) 294 if (v8Number.IsEmpty())
246 return false; 295 return false;
247 double d = v8Number->Value(); 296 double d = v8Number->Value();
248 doubleToInteger(d, value); 297 doubleToInteger(d, value);
249 return true; 298 return true;
250 } 299 }
251 300
252 bool Dictionary::get(const String& key, RefPtr<DOMWindow>& value) const 301 bool Dictionary::get(const String& key, RefPtr<DOMWindow>& value) const
253 { 302 {
254 v8::Local<v8::Value> v8Value; 303 v8::Local<v8::Value> v8Value;
(...skipping 28 matching lines...) Expand all
283 { 332 {
284 v8::Local<v8::Value> v8Value; 333 v8::Local<v8::Value> v8Value;
285 if (!getKey(key, v8Value)) 334 if (!getKey(key, v8Value))
286 return false; 335 return false;
287 336
288 ASSERT(m_isolate); 337 ASSERT(m_isolate);
289 ASSERT(m_isolate == v8::Isolate::GetCurrent()); 338 ASSERT(m_isolate == v8::Isolate::GetCurrent());
290 return getMessagePortArray(v8Value, key, value, m_isolate); 339 return getMessagePortArray(v8Value, key, value, m_isolate);
291 } 340 }
292 341
342 bool Dictionary::convert(const String& key, const String&, bool isNullable, Mess agePortArray& value, ExceptionState& exceptionState) const
343 {
344 v8::Local<v8::Value> v8Value;
345 if (!getKey(key, v8Value))
346 return true;
347
348 return get(key, value);
349 }
350
293 bool Dictionary::get(const String& key, HashSet<AtomicString>& value) const 351 bool Dictionary::get(const String& key, HashSet<AtomicString>& value) const
294 { 352 {
295 v8::Local<v8::Value> v8Value; 353 v8::Local<v8::Value> v8Value;
296 if (!getKey(key, v8Value)) 354 if (!getKey(key, v8Value))
297 return false; 355 return false;
298 356
299 // FIXME: Support array-like objects 357 // FIXME: Support array-like objects
300 if (!v8Value->IsArray()) 358 if (!v8Value->IsArray())
301 return false; 359 return false;
302 360
303 ASSERT(m_isolate); 361 ASSERT(m_isolate);
304 ASSERT(m_isolate == v8::Isolate::GetCurrent()); 362 ASSERT(m_isolate == v8::Isolate::GetCurrent());
305 v8::Local<v8::Array> v8Array = v8::Local<v8::Array>::Cast(v8Value); 363 v8::Local<v8::Array> v8Array = v8::Local<v8::Array>::Cast(v8Value);
306 for (size_t i = 0; i < v8Array->Length(); ++i) { 364 for (size_t i = 0; i < v8Array->Length(); ++i) {
307 v8::Local<v8::Value> indexedValue = v8Array->Get(v8::Integer::New(i, m_i solate)); 365 v8::Local<v8::Value> indexedValue = v8Array->Get(v8::Integer::New(i, m_i solate));
308 V8TRYCATCH_FOR_V8STRINGRESOURCE_RETURN(V8StringResource<>, stringValue, indexedValue, false); 366 V8TRYCATCH_FOR_V8STRINGRESOURCE_RETURN(V8StringResource<>, stringValue, indexedValue, false);
309 value.add(stringValue); 367 value.add(stringValue);
310 } 368 }
311 369
312 return true; 370 return true;
313 } 371 }
314 372
373 bool Dictionary::convert(const String& key, const String&, bool isNullable, Hash Set<AtomicString>& value, ExceptionState& exceptionState) const
374 {
375 v8::Local<v8::Value> v8Value;
376 if (!getKey(key, v8Value))
377 return true;
378
379 if (isNullable && WebCore::isUndefinedOrNull(v8Value))
380 return true;
381
382 if (!v8Value->IsArray()) {
383 exceptionState.throwTypeError(ExceptionMessages::notASequenceTypePropert y(key));
384 return false;
385 }
386
387 return get(key, value);
388 }
389
315 bool Dictionary::getWithUndefinedOrNullCheck(const String& key, String& value) c onst 390 bool Dictionary::getWithUndefinedOrNullCheck(const String& key, String& value) c onst
316 { 391 {
317 v8::Local<v8::Value> v8Value; 392 v8::Local<v8::Value> v8Value;
318 if (!getKey(key, v8Value) || v8Value->IsNull() || v8Value->IsUndefined()) 393 if (!getKey(key, v8Value) || WebCore::isUndefinedOrNull(v8Value))
319 return false; 394 return false;
320 395
321 if (WebCore::isUndefinedOrNull(v8Value)) {
322 value = String();
323 return true;
324 }
325
326 V8TRYCATCH_FOR_V8STRINGRESOURCE_RETURN(V8StringResource<>, stringValue, v8Va lue, false); 396 V8TRYCATCH_FOR_V8STRINGRESOURCE_RETURN(V8StringResource<>, stringValue, v8Va lue, false);
327 value = stringValue; 397 value = stringValue;
328 return true; 398 return true;
329 } 399 }
330 400
331 bool Dictionary::get(const String& key, RefPtr<Uint8Array>& value) const 401 bool Dictionary::get(const String& key, RefPtr<Uint8Array>& value) const
332 { 402 {
333 v8::Local<v8::Value> v8Value; 403 v8::Local<v8::Value> v8Value;
334 if (!getKey(key, v8Value)) 404 if (!getKey(key, v8Value))
335 return false; 405 return false;
(...skipping 141 matching lines...) Expand 10 before | Expand all | Expand 10 after
477 547
478 if (v8Value->IsObject()) { 548 if (v8Value->IsObject()) {
479 ASSERT(m_isolate); 549 ASSERT(m_isolate);
480 ASSERT(m_isolate == v8::Isolate::GetCurrent()); 550 ASSERT(m_isolate == v8::Isolate::GetCurrent());
481 value = Dictionary(v8Value, m_isolate); 551 value = Dictionary(v8Value, m_isolate);
482 } 552 }
483 553
484 return true; 554 return true;
485 } 555 }
486 556
557 bool Dictionary::convert(const String& key, const String&, bool isNullable, Dict ionary& value, ExceptionState& exceptionState) const
558 {
559 v8::Local<v8::Value> v8Value;
560 if (!getKey(key, v8Value))
561 return true;
562
563 if (v8Value->IsObject())
564 return get(key, value);
565
566 if (isNullable && WebCore::isUndefinedOrNull(v8Value))
567 return true;
568
569 exceptionState.throwTypeError(ExceptionMessages::illTypedProperty(key, "does not have a Dictionary type."));
570 return false;
571 }
572
487 bool Dictionary::get(const String& key, Vector<String>& value) const 573 bool Dictionary::get(const String& key, Vector<String>& value) const
488 { 574 {
489 v8::Local<v8::Value> v8Value; 575 v8::Local<v8::Value> v8Value;
490 if (!getKey(key, v8Value)) 576 if (!getKey(key, v8Value))
491 return false; 577 return false;
492 578
493 if (!v8Value->IsArray()) 579 if (!v8Value->IsArray())
494 return false; 580 return false;
495 581
496 v8::Local<v8::Array> v8Array = v8::Local<v8::Array>::Cast(v8Value); 582 v8::Local<v8::Array> v8Array = v8::Local<v8::Array>::Cast(v8Value);
497 for (size_t i = 0; i < v8Array->Length(); ++i) { 583 for (size_t i = 0; i < v8Array->Length(); ++i) {
498 v8::Local<v8::Value> indexedValue = v8Array->Get(v8::Uint32::New(i, m_is olate)); 584 v8::Local<v8::Value> indexedValue = v8Array->Get(v8::Uint32::New(i, m_is olate));
499 V8TRYCATCH_FOR_V8STRINGRESOURCE_RETURN(V8StringResource<>, stringValue, indexedValue, false); 585 V8TRYCATCH_FOR_V8STRINGRESOURCE_RETURN(V8StringResource<>, stringValue, indexedValue, false);
500 value.append(stringValue); 586 value.append(stringValue);
501 } 587 }
502 588
503 return true; 589 return true;
504 } 590 }
505 591
592 bool Dictionary::convert(const String& key, const String&, bool isNullable, Vect or<String>& value, ExceptionState& exceptionState) const
593 {
594 v8::Local<v8::Value> v8Value;
595 if (!getKey(key, v8Value))
596 return true;
597
598 if (isNullable && WebCore::isUndefinedOrNull(v8Value))
599 return true;
600
601 if (!v8Value->IsArray()) {
602 exceptionState.throwTypeError(ExceptionMessages::notASequenceTypePropert y(key));
603 return false;
604 }
605
606 return get(key, value);
607 }
608
506 bool Dictionary::get(const String& key, ArrayValue& value) const 609 bool Dictionary::get(const String& key, ArrayValue& value) const
507 { 610 {
508 v8::Local<v8::Value> v8Value; 611 v8::Local<v8::Value> v8Value;
509 if (!getKey(key, v8Value)) 612 if (!getKey(key, v8Value))
510 return false; 613 return false;
511 614
512 if (!v8Value->IsArray()) 615 if (!v8Value->IsArray())
513 return false; 616 return false;
514 617
515 ASSERT(m_isolate); 618 ASSERT(m_isolate);
516 ASSERT(m_isolate == v8::Isolate::GetCurrent()); 619 ASSERT(m_isolate == v8::Isolate::GetCurrent());
517 value = ArrayValue(v8::Local<v8::Array>::Cast(v8Value), m_isolate); 620 value = ArrayValue(v8::Local<v8::Array>::Cast(v8Value), m_isolate);
518 return true; 621 return true;
519 } 622 }
520 623
624 bool Dictionary::convert(const String& key, const String&, bool isNullable, Arra yValue& value, ExceptionState& exceptionState) const
625 {
626 v8::Local<v8::Value> v8Value;
627 if (!getKey(key, v8Value))
628 return true;
629
630 if (isNullable && WebCore::isUndefinedOrNull(v8Value))
631 return true;
632
633 if (!v8Value->IsArray()) {
634 exceptionState.throwTypeError(ExceptionMessages::notASequenceTypePropert y(key));
635 return false;
636 }
637
638 return get(key, value);
639 }
640
521 bool Dictionary::get(const String& key, RefPtr<DOMError>& value) const 641 bool Dictionary::get(const String& key, RefPtr<DOMError>& value) const
522 { 642 {
523 v8::Local<v8::Value> v8Value; 643 v8::Local<v8::Value> v8Value;
524 if (!getKey(key, v8Value)) 644 if (!getKey(key, v8Value))
525 return false; 645 return false;
526 646
527 DOMError* error = 0; 647 DOMError* error = 0;
528 if (v8Value->IsObject()) { 648 if (v8Value->IsObject()) {
529 v8::Handle<v8::Object> wrapper = v8::Handle<v8::Object>::Cast(v8Value); 649 v8::Handle<v8::Object> wrapper = v8::Handle<v8::Object>::Cast(v8Value);
530 v8::Handle<v8::Object> domError = wrapper->FindInstanceInPrototypeChain( V8DOMError::GetTemplate(m_isolate, worldType(m_isolate))); 650 v8::Handle<v8::Object> domError = wrapper->FindInstanceInPrototypeChain( V8DOMError::GetTemplate(m_isolate, worldType(m_isolate)));
(...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after
592 if (!options->Has(key)) 712 if (!options->Has(key))
593 continue; 713 continue;
594 V8TRYCATCH_FOR_V8STRINGRESOURCE_RETURN(V8StringResource<>, stringKey, ke y, false); 714 V8TRYCATCH_FOR_V8STRINGRESOURCE_RETURN(V8StringResource<>, stringKey, ke y, false);
595 names.append(stringKey); 715 names.append(stringKey);
596 } 716 }
597 717
598 return true; 718 return true;
599 } 719 }
600 720
601 } // namespace WebCore 721 } // namespace WebCore
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698