Chromium Code Reviews| 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/core/v8/DictionaryHelper.h" | |
| 28 | |
| 29 #include "bindings/core/v8/ArrayValue.h" | |
| 30 #include "bindings/core/v8/ExceptionMessages.h" | |
| 31 #include "bindings/core/v8/ExceptionState.h" | |
| 32 #include "bindings/core/v8/V8Binding.h" | |
| 33 #include "bindings/core/v8/V8DOMError.h" | |
| 34 #include "bindings/core/v8/V8Element.h" | |
| 35 #include "bindings/core/v8/V8EventTarget.h" | |
| 36 #include "bindings/core/v8/V8MediaKeyError.h" | |
| 37 #include "bindings/core/v8/V8MessagePort.h" | |
| 38 #include "bindings/core/v8/V8Path2D.h" | |
| 39 #include "bindings/core/v8/V8Storage.h" | |
| 40 #include "bindings/core/v8/V8TextTrack.h" | |
| 41 #include "bindings/core/v8/V8VoidCallback.h" | |
| 42 #include "bindings/core/v8/V8Window.h" | |
| 43 #include "bindings/core/v8/custom/V8ArrayBufferViewCustom.h" | |
| 44 #include "bindings/core/v8/custom/V8Uint8ArrayCustom.h" | |
| 45 #include "core/html/track/TrackBase.h" | |
| 46 #include "wtf/MathExtras.h" | |
| 47 | |
| 48 namespace WebCore { | |
| 49 | |
| 50 template <> | |
| 51 bool DictionaryHelper::get(const Dictionary& dictionary, const String& key, v8:: Local<v8::Value>& value) | |
| 52 { | |
| 53 return dictionary.get(key, value); | |
| 54 } | |
| 55 | |
| 56 template <> | |
| 57 bool DictionaryHelper::get(const Dictionary& dictionary, const String& key, Dict ionary& value) | |
| 58 { | |
| 59 return dictionary.get(key, value); | |
| 60 } | |
| 61 | |
| 62 template <> | |
| 63 bool DictionaryHelper::get(const Dictionary& dictionary, const String& key, bool & value) | |
| 64 { | |
| 65 v8::Local<v8::Value> v8Value; | |
| 66 if (!dictionary.get(key, v8Value)) | |
| 67 return false; | |
| 68 | |
| 69 v8::Local<v8::Boolean> v8Bool = v8Value->ToBoolean(); | |
| 70 if (v8Bool.IsEmpty()) | |
| 71 return false; | |
| 72 value = v8Bool->Value(); | |
| 73 return true; | |
| 74 } | |
| 75 | |
| 76 template <> | |
| 77 bool DictionaryHelper::convert(const Dictionary& dictionary, Dictionary::Convers ionContext& context, const String& key, bool& value) | |
| 78 { | |
| 79 Dictionary::ConversionContextScope scope(context); | |
| 80 DictionaryHelper::get(dictionary, key, value); | |
| 81 return true; | |
| 82 } | |
| 83 | |
| 84 template <> | |
| 85 bool | |
| 86 DictionaryHelper::get(const Dictionary& dictionary, const String& key, int32_t& value) | |
| 87 { | |
| 88 v8::Local<v8::Value> v8Value; | |
| 89 if (!dictionary.get(key, v8Value)) | |
| 90 return false; | |
| 91 | |
| 92 v8::Local<v8::Int32> v8Int32 = v8Value->ToInt32(); | |
| 93 if (v8Int32.IsEmpty()) | |
| 94 return false; | |
| 95 value = v8Int32->Value(); | |
| 96 return true; | |
| 97 } | |
| 98 | |
| 99 template <> | |
| 100 bool DictionaryHelper::get(const Dictionary& dictionary, const String& key, doub le& value, bool& hasValue) | |
| 101 { | |
| 102 v8::Local<v8::Value> v8Value; | |
| 103 if (!dictionary.get(key, v8Value)) { | |
| 104 hasValue = false; | |
| 105 return false; | |
| 106 } | |
| 107 | |
| 108 hasValue = true; | |
| 109 TONATIVE_DEFAULT(v8::Local<v8::Number>, v8Number, v8Value->ToNumber(), false ); | |
| 110 if (v8Number.IsEmpty()) | |
| 111 return false; | |
| 112 value = v8Number->Value(); | |
| 113 return true; | |
| 114 } | |
| 115 | |
| 116 template <> | |
| 117 bool DictionaryHelper::get(const Dictionary& dictionary, const String& key, doub le& value) | |
| 118 { | |
| 119 bool unused; | |
| 120 return DictionaryHelper::get(dictionary, key, value, unused); | |
| 121 } | |
| 122 | |
| 123 template <> | |
| 124 bool DictionaryHelper::convert(const Dictionary& dictionary, Dictionary::Convers ionContext& context, const String& key, double& value) | |
| 125 { | |
| 126 Dictionary::ConversionContextScope scope(context); | |
| 127 | |
| 128 bool hasValue = false; | |
| 129 if (!DictionaryHelper::get(dictionary, key, value, hasValue) && hasValue) { | |
| 130 context.throwTypeError(ExceptionMessages::incorrectPropertyType(key, "is not of type 'double'.")); | |
| 131 return false; | |
| 132 } | |
| 133 return true; | |
| 134 } | |
| 135 | |
| 136 template<typename StringType> | |
| 137 bool getStringType(const Dictionary& dictionary, const String& key, StringType& value) | |
| 138 { | |
| 139 v8::Local<v8::Value> v8Value; | |
| 140 if (!dictionary.get(key, v8Value)) | |
| 141 return false; | |
| 142 | |
| 143 TOSTRING_DEFAULT(V8StringResource<>, stringValue, v8Value, false); | |
| 144 value = stringValue; | |
| 145 return true; | |
| 146 } | |
| 147 | |
| 148 template <> | |
| 149 bool DictionaryHelper::get(const Dictionary& dictionary, const String& key, Stri ng& value) | |
| 150 { | |
| 151 return getStringType(dictionary, key, value); | |
| 152 } | |
| 153 | |
| 154 template <> | |
| 155 bool DictionaryHelper::get(const Dictionary& dictionary, const String& key, Atom icString& value) | |
| 156 { | |
| 157 return getStringType(dictionary, key, value); | |
| 158 } | |
| 159 | |
| 160 template <> | |
| 161 bool DictionaryHelper::convert(const Dictionary& dictionary, Dictionary::Convers ionContext& context, const String& key, String& value) | |
| 162 { | |
| 163 Dictionary::ConversionContextScope scope(context); | |
| 164 | |
| 165 v8::Local<v8::Value> v8Value; | |
| 166 if (!dictionary.get(key, v8Value)) | |
| 167 return true; | |
| 168 | |
| 169 TOSTRING_DEFAULT(V8StringResource<>, stringValue, v8Value, false); | |
| 170 value = stringValue; | |
| 171 return true; | |
| 172 } | |
| 173 | |
| 174 template <> | |
| 175 bool DictionaryHelper::get(const Dictionary& dictionary, const String& key, Scri ptValue& value) | |
| 176 { | |
| 177 v8::Local<v8::Value> v8Value; | |
| 178 if (!dictionary.get(key, v8Value)) | |
| 179 return false; | |
| 180 | |
| 181 value = ScriptValue(ScriptState::current(dictionary.isolate()), v8Value); | |
| 182 return true; | |
| 183 } | |
| 184 | |
| 185 template <> | |
| 186 bool DictionaryHelper::convert(const Dictionary& dictionary, Dictionary::Convers ionContext& context, const String& key, ScriptValue& value) | |
| 187 { | |
| 188 Dictionary::ConversionContextScope scope(context); | |
| 189 | |
| 190 DictionaryHelper::get(dictionary, key, value); | |
| 191 return true; | |
| 192 } | |
| 193 | |
| 194 template<typename NumericType> | |
| 195 bool getNumericType(const Dictionary& dictionary, const String& key, NumericType & value) | |
| 196 { | |
| 197 v8::Local<v8::Value> v8Value; | |
| 198 if (!dictionary.get(key, v8Value)) | |
| 199 return false; | |
| 200 | |
| 201 v8::Local<v8::Int32> v8Int32 = v8Value->ToInt32(); | |
| 202 if (v8Int32.IsEmpty()) | |
| 203 return false; | |
| 204 value = static_cast<NumericType>(v8Int32->Value()); | |
| 205 return true; | |
| 206 } | |
| 207 | |
| 208 template <> | |
| 209 bool DictionaryHelper::get(const Dictionary& dictionary, const String& key, shor t& value) | |
| 210 { | |
| 211 return getNumericType<short>(dictionary, key, value); | |
| 212 } | |
| 213 | |
| 214 template <> | |
| 215 bool DictionaryHelper::get(const Dictionary& dictionary, const String& key, unsi gned short& value) | |
| 216 { | |
| 217 return getNumericType<unsigned short>(dictionary, key, value); | |
| 218 } | |
| 219 | |
| 220 template <> | |
| 221 bool DictionaryHelper::get(const Dictionary& dictionary, const String& key, unsi gned& value) | |
| 222 { | |
| 223 return getNumericType<unsigned>(dictionary, key, value); | |
| 224 } | |
| 225 | |
| 226 template <> | |
| 227 bool DictionaryHelper::get(const Dictionary& dictionary, const String& key, unsi gned long& value) | |
| 228 { | |
| 229 v8::Local<v8::Value> v8Value; | |
| 230 if (!dictionary.get(key, v8Value)) | |
| 231 return false; | |
| 232 | |
| 233 v8::Local<v8::Integer> v8Integer = v8Value->ToInteger(); | |
| 234 if (v8Integer.IsEmpty()) | |
| 235 return false; | |
| 236 value = static_cast<unsigned long>(v8Integer->Value()); | |
| 237 return true; | |
| 238 } | |
| 239 | |
| 240 template <> | |
| 241 bool DictionaryHelper::get(const Dictionary& dictionary, const String& key, unsi gned long long& value) | |
| 242 { | |
| 243 v8::Local<v8::Value> v8Value; | |
| 244 if (!dictionary.get(key, v8Value)) | |
| 245 return false; | |
| 246 | |
| 247 TONATIVE_DEFAULT(v8::Local<v8::Number>, v8Number, v8Value->ToNumber(), false ); | |
| 248 if (v8Number.IsEmpty()) | |
| 249 return false; | |
| 250 double d = v8Number->Value(); | |
| 251 doubleToInteger(d, value); | |
| 252 return true; | |
| 253 } | |
| 254 | |
| 255 template <> | |
| 256 bool DictionaryHelper::get(const Dictionary& dictionary, const String& key, RefP trWillBeMember<LocalDOMWindow>& value) | |
| 257 { | |
| 258 v8::Local<v8::Value> v8Value; | |
| 259 if (!dictionary.get(key, v8Value)) | |
| 260 return false; | |
| 261 | |
| 262 // We need to handle a DOMWindow specially, because a DOMWindow wrapper | |
| 263 // exists on a prototype chain of v8Value. | |
| 264 value = toDOMWindow(v8Value, dictionary.isolate()); | |
| 265 return true; | |
| 266 } | |
| 267 | |
| 268 template <> | |
| 269 bool DictionaryHelper::get(const Dictionary& dictionary, const String& key, RefP trWillBeMember<Storage>& value) | |
| 270 { | |
| 271 v8::Local<v8::Value> v8Value; | |
| 272 if (!dictionary.get(key, v8Value)) | |
| 273 return false; | |
| 274 | |
| 275 value = V8Storage::toNativeWithTypeCheck(dictionary.isolate(), v8Value); | |
| 276 return true; | |
| 277 } | |
| 278 | |
| 279 template <> | |
| 280 bool DictionaryHelper::get(const Dictionary& dictionary, const String& key, Mess agePortArray& value) | |
| 281 { | |
| 282 v8::Local<v8::Value> v8Value; | |
| 283 if (!dictionary.get(key, v8Value)) | |
| 284 return false; | |
| 285 | |
| 286 ASSERT(dictionary.isolate()); | |
| 287 ASSERT(dictionary.isolate() == v8::Isolate::GetCurrent()); | |
| 288 if (WebCore::isUndefinedOrNull(v8Value)) | |
| 289 return true; | |
| 290 bool success = false; | |
| 291 value = toRefPtrWillBeMemberNativeArray<MessagePort, V8MessagePort>(v8Value, key, dictionary.isolate(), &success); | |
| 292 return success; | |
| 293 } | |
| 294 | |
| 295 template <> | |
| 296 bool DictionaryHelper::get(const Dictionary& dictionary, const String& key, Hash Set<AtomicString>& value) | |
| 297 { | |
| 298 v8::Local<v8::Value> v8Value; | |
| 299 if (!dictionary.get(key, v8Value)) | |
| 300 return false; | |
| 301 | |
| 302 // FIXME: Support array-like objects | |
| 303 if (!v8Value->IsArray()) | |
| 304 return false; | |
| 305 | |
| 306 ASSERT(dictionary.isolate()); | |
| 307 ASSERT(dictionary.isolate() == v8::Isolate::GetCurrent()); | |
| 308 v8::Local<v8::Array> v8Array = v8::Local<v8::Array>::Cast(v8Value); | |
| 309 for (size_t i = 0; i < v8Array->Length(); ++i) { | |
| 310 v8::Local<v8::Value> indexedValue = v8Array->Get(v8::Integer::New(dictio nary.isolate(), i)); | |
| 311 TOSTRING_DEFAULT(V8StringResource<>, stringValue, indexedValue, false); | |
| 312 value.add(stringValue); | |
| 313 } | |
| 314 | |
| 315 return true; | |
| 316 } | |
| 317 | |
| 318 template <> | |
| 319 bool DictionaryHelper::convert(const Dictionary& dictionary, Dictionary::Convers ionContext& context, const String& key, HashSet<AtomicString>& value) | |
| 320 { | |
| 321 Dictionary::ConversionContextScope scope(context); | |
| 322 | |
| 323 v8::Local<v8::Value> v8Value; | |
| 324 if (!dictionary.get(key, v8Value)) | |
| 325 return true; | |
| 326 | |
| 327 if (context.isNullable() && WebCore::isUndefinedOrNull(v8Value)) | |
| 328 return true; | |
| 329 | |
| 330 if (!v8Value->IsArray()) { | |
| 331 context.throwTypeError(ExceptionMessages::notASequenceTypeProperty(key)) ; | |
| 332 return false; | |
| 333 } | |
| 334 | |
| 335 return DictionaryHelper::get(dictionary, key, value); | |
| 336 } | |
| 337 | |
| 338 template <> | |
| 339 bool DictionaryHelper::get(const Dictionary& dictionary, const String& key, RefP tr<Uint8Array>& value) | |
| 340 { | |
| 341 v8::Local<v8::Value> v8Value; | |
| 342 if (!dictionary.get(key, v8Value)) | |
| 343 return false; | |
| 344 | |
| 345 value = V8Uint8Array::toNativeWithTypeCheck(dictionary.isolate(), v8Value); | |
| 346 return true; | |
| 347 } | |
| 348 | |
| 349 template <> | |
| 350 bool DictionaryHelper::get(const Dictionary& dictionary, const String& key, RefP tr<ArrayBufferView>& value) | |
| 351 { | |
| 352 v8::Local<v8::Value> v8Value; | |
| 353 if (!dictionary.get(key, v8Value)) | |
| 354 return false; | |
| 355 | |
| 356 value = V8ArrayBufferView::toNativeWithTypeCheck(dictionary.isolate(), v8Val ue); | |
| 357 return true; | |
| 358 } | |
| 359 | |
| 360 template <> | |
| 361 bool DictionaryHelper::get(const Dictionary& dictionary, const String& key, RefP trWillBeMember<MediaKeyError>& value) | |
| 362 { | |
| 363 v8::Local<v8::Value> v8Value; | |
| 364 if (!dictionary.get(key, v8Value)) | |
| 365 return false; | |
| 366 | |
| 367 value = V8MediaKeyError::toNativeWithTypeCheck(dictionary.isolate(), v8Value ); | |
| 368 return true; | |
| 369 } | |
| 370 | |
| 371 template <> | |
| 372 bool DictionaryHelper::get(const Dictionary& dictionary, const String& key, RefP trWillBeMember<TrackBase>& value) | |
| 373 { | |
| 374 v8::Local<v8::Value> v8Value; | |
| 375 if (!dictionary.get(key, v8Value)) | |
| 376 return false; | |
| 377 | |
| 378 TrackBase* source = 0; | |
| 379 if (v8Value->IsObject()) { | |
| 380 v8::Handle<v8::Object> wrapper = v8::Handle<v8::Object>::Cast(v8Value); | |
| 381 | |
| 382 // FIXME: this will need to be changed so it can also return an AudioTra ck or a VideoTrack once | |
| 383 // we add them. | |
| 384 v8::Handle<v8::Object> track = V8TextTrack::findInstanceInPrototypeChain (wrapper, dictionary.isolate()); | |
| 385 if (!track.IsEmpty()) | |
| 386 source = V8TextTrack::toNative(track); | |
| 387 } | |
| 388 value = source; | |
| 389 return true; | |
| 390 } | |
| 391 | |
| 392 template <> | |
| 393 bool DictionaryHelper::get(const Dictionary& dictionary, const String& key, RefP trWillBeMember<EventTarget>& value) | |
| 394 { | |
| 395 v8::Local<v8::Value> v8Value; | |
| 396 if (!dictionary.get(key, v8Value)) | |
| 397 return false; | |
| 398 | |
| 399 value = nullptr; | |
| 400 // We need to handle a LocalDOMWindow specially, because a LocalDOMWindow wr apper | |
| 401 // exists on a prototype chain of v8Value. | |
| 402 if (v8Value->IsObject()) { | |
| 403 v8::Handle<v8::Object> wrapper = v8::Handle<v8::Object>::Cast(v8Value); | |
| 404 v8::Handle<v8::Object> window = V8Window::findInstanceInPrototypeChain(w rapper, dictionary.isolate()); | |
| 405 if (!window.IsEmpty()) { | |
| 406 value = toWrapperTypeInfo(window)->toEventTarget(window); | |
| 407 return true; | |
| 408 } | |
| 409 } | |
| 410 | |
| 411 if (V8DOMWrapper::isDOMWrapper(v8Value)) { | |
| 412 v8::Handle<v8::Object> wrapper = v8::Handle<v8::Object>::Cast(v8Value); | |
| 413 value = toWrapperTypeInfo(wrapper)->toEventTarget(wrapper); | |
| 414 } | |
| 415 return true; | |
| 416 } | |
| 417 | |
| 418 template <> | |
| 419 bool DictionaryHelper::get(const Dictionary& dictionary, const String& key, Vect or<String>& value) | |
| 420 { | |
| 421 v8::Local<v8::Value> v8Value; | |
| 422 if (!dictionary.get(key, v8Value)) | |
| 423 return false; | |
| 424 | |
| 425 if (!v8Value->IsArray()) | |
| 426 return false; | |
| 427 | |
| 428 v8::Local<v8::Array> v8Array = v8::Local<v8::Array>::Cast(v8Value); | |
| 429 for (size_t i = 0; i < v8Array->Length(); ++i) { | |
| 430 v8::Local<v8::Value> indexedValue = v8Array->Get(v8::Uint32::New(diction ary.isolate(), i)); | |
| 431 TOSTRING_DEFAULT(V8StringResource<>, stringValue, indexedValue, false); | |
| 432 value.append(stringValue); | |
| 433 } | |
| 434 | |
| 435 return true; | |
| 436 } | |
| 437 | |
| 438 template <> | |
| 439 bool DictionaryHelper::convert(const Dictionary& dictionary, Dictionary::Convers ionContext& context, const String& key, Vector<String>& value) | |
| 440 { | |
| 441 Dictionary::ConversionContextScope scope(context); | |
| 442 | |
| 443 v8::Local<v8::Value> v8Value; | |
| 444 if (!dictionary.get(key, v8Value)) | |
| 445 return true; | |
| 446 | |
| 447 if (context.isNullable() && WebCore::isUndefinedOrNull(v8Value)) | |
| 448 return true; | |
| 449 | |
| 450 if (!v8Value->IsArray()) { | |
| 451 context.throwTypeError(ExceptionMessages::notASequenceTypeProperty(key)) ; | |
| 452 return false; | |
| 453 } | |
| 454 | |
| 455 return DictionaryHelper::get(dictionary, key, value); | |
| 456 } | |
| 457 | |
| 458 template <> | |
| 459 bool DictionaryHelper::get(const Dictionary& dictionary, const String& key, Arra yValue& value) | |
| 460 { | |
| 461 v8::Local<v8::Value> v8Value; | |
| 462 if (!dictionary.get(key, v8Value)) | |
| 463 return false; | |
| 464 | |
| 465 if (!v8Value->IsArray()) | |
| 466 return false; | |
| 467 | |
| 468 ASSERT(dictionary.isolate()); | |
| 469 ASSERT(dictionary.isolate() == v8::Isolate::GetCurrent()); | |
| 470 value = ArrayValue(v8::Local<v8::Array>::Cast(v8Value), dictionary.isolate() ); | |
| 471 return true; | |
| 472 } | |
| 473 | |
| 474 template <> | |
| 475 bool DictionaryHelper::convert(const Dictionary& dictionary, Dictionary::Convers ionContext& context, const String& key, ArrayValue& value) | |
| 476 { | |
| 477 Dictionary::ConversionContextScope scope(context); | |
| 478 | |
| 479 v8::Local<v8::Value> v8Value; | |
| 480 if (!dictionary.get(key, v8Value)) | |
| 481 return true; | |
| 482 | |
| 483 if (context.isNullable() && WebCore::isUndefinedOrNull(v8Value)) | |
| 484 return true; | |
| 485 | |
| 486 if (!v8Value->IsArray()) { | |
| 487 context.throwTypeError(ExceptionMessages::notASequenceTypeProperty(key)) ; | |
| 488 return false; | |
| 489 } | |
| 490 | |
| 491 return DictionaryHelper::get(dictionary, key, value); | |
| 492 } | |
| 493 | |
| 494 template <> | |
| 495 bool DictionaryHelper::get(const Dictionary& dictionary, const String& key, RefP trWillBeMember<DOMError>& value) | |
| 496 { | |
| 497 v8::Local<v8::Value> v8Value; | |
| 498 if (!dictionary.get(key, v8Value)) | |
| 499 return false; | |
| 500 | |
| 501 value = V8DOMError::toNativeWithTypeCheck(dictionary.isolate(), v8Value); | |
| 502 return true; | |
| 503 } | |
| 504 | |
| 505 template <typename T> | |
| 506 struct IntegralTypeTraits { | |
| 507 }; | |
| 508 | |
| 509 template <> | |
| 510 struct IntegralTypeTraits<uint8_t> { | |
| 511 static inline uint8_t toIntegral(v8::Handle<v8::Value> value, IntegerConvers ionConfiguration configuration, ExceptionState& exceptionState) | |
| 512 { | |
| 513 return toUInt8(value, configuration, exceptionState); | |
| 514 } | |
| 515 static const String typeName() { return "UInt8"; } | |
| 516 }; | |
| 517 | |
| 518 template <> | |
| 519 struct IntegralTypeTraits<int8_t> { | |
| 520 static inline int8_t toIntegral(v8::Handle<v8::Value> value, IntegerConversi onConfiguration configuration, ExceptionState& exceptionState) | |
| 521 { | |
| 522 return toInt8(value, configuration, exceptionState); | |
| 523 } | |
| 524 static const String typeName() { return "Int8"; } | |
| 525 }; | |
| 526 | |
| 527 template <> | |
| 528 struct IntegralTypeTraits<unsigned short> { | |
| 529 static inline uint16_t toIntegral(v8::Handle<v8::Value> value, IntegerConver sionConfiguration configuration, ExceptionState& exceptionState) | |
| 530 { | |
| 531 return toUInt16(value, configuration, exceptionState); | |
| 532 } | |
| 533 static const String typeName() { return "UInt16"; } | |
| 534 }; | |
| 535 | |
| 536 template <> | |
| 537 struct IntegralTypeTraits<short> { | |
| 538 static inline int16_t toIntegral(v8::Handle<v8::Value> value, IntegerConvers ionConfiguration configuration, ExceptionState& exceptionState) | |
| 539 { | |
| 540 return toInt16(value, configuration, exceptionState); | |
| 541 } | |
| 542 static const String typeName() { return "Int16"; } | |
| 543 }; | |
| 544 | |
| 545 template <> | |
| 546 struct IntegralTypeTraits<unsigned> { | |
| 547 static inline uint32_t toIntegral(v8::Handle<v8::Value> value, IntegerConver sionConfiguration configuration, ExceptionState& exceptionState) | |
| 548 { | |
| 549 return toUInt32(value, configuration, exceptionState); | |
| 550 } | |
| 551 static const String typeName() { return "UInt32"; } | |
| 552 }; | |
| 553 | |
| 554 template <> | |
| 555 struct IntegralTypeTraits<unsigned long> { | |
| 556 static inline uint32_t toIntegral(v8::Handle<v8::Value> value, IntegerConver sionConfiguration configuration, ExceptionState& exceptionState) | |
| 557 { | |
| 558 return toUInt32(value, configuration, exceptionState); | |
| 559 } | |
| 560 static const String typeName() { return "UInt32"; } | |
| 561 }; | |
| 562 | |
| 563 template <> | |
| 564 struct IntegralTypeTraits<int> { | |
| 565 static inline int32_t toIntegral(v8::Handle<v8::Value> value, IntegerConvers ionConfiguration configuration, ExceptionState& exceptionState) | |
| 566 { | |
| 567 return toInt32(value, configuration, exceptionState); | |
| 568 } | |
| 569 static const String typeName() { return "Int32"; } | |
| 570 }; | |
| 571 | |
| 572 template <> | |
| 573 struct IntegralTypeTraits<long> { | |
| 574 static inline int32_t toIntegral(v8::Handle<v8::Value> value, IntegerConvers ionConfiguration configuration, ExceptionState& exceptionState) | |
| 575 { | |
| 576 return toInt32(value, configuration, exceptionState); | |
| 577 } | |
| 578 static const String typeName() { return "Int32"; } | |
| 579 }; | |
| 580 | |
| 581 template <> | |
| 582 struct IntegralTypeTraits<unsigned long long> { | |
| 583 static inline unsigned long long toIntegral(v8::Handle<v8::Value> value, Int egerConversionConfiguration configuration, ExceptionState& exceptionState) | |
| 584 { | |
| 585 return toUInt64(value, configuration, exceptionState); | |
| 586 } | |
| 587 static const String typeName() { return "UInt64"; } | |
| 588 }; | |
| 589 | |
| 590 template <> | |
| 591 struct IntegralTypeTraits<long long> { | |
| 592 static inline long long toIntegral(v8::Handle<v8::Value> value, IntegerConve rsionConfiguration configuration, ExceptionState& exceptionState) | |
| 593 { | |
| 594 return toInt64(value, configuration, exceptionState); | |
| 595 } | |
| 596 static const String typeName() { return "Int64"; } | |
| 597 }; | |
| 598 | |
| 599 template<typename T> bool convertIntegral(const Dictionary& dictionary, Dictiona ry::ConversionContext& context, const String& key, T& value) | |
| 600 { | |
| 601 Dictionary::ConversionContextScope scope(context); | |
| 602 | |
| 603 v8::Local<v8::Value> v8Value; | |
| 604 if (!dictionary.get(key, v8Value)) | |
| 605 return true; | |
| 606 | |
| 607 value = IntegralTypeTraits<T>::toIntegral(v8Value, NormalConversion, context .exceptionState()); | |
| 608 if (context.exceptionState().throwIfNeeded()) | |
| 609 return false; | |
| 610 | |
| 611 return true; | |
| 612 } | |
| 613 | |
| 614 template <> | |
| 615 bool DictionaryHelper::convert(const Dictionary& dictionary, Dictionary::Convers ionContext& context, const String& key, uint8_t& value) | |
| 616 { | |
| 617 return convertIntegral<uint8_t>(dictionary, context, key, value); | |
| 618 } | |
| 619 | |
| 620 template <> | |
| 621 bool DictionaryHelper::convert(const Dictionary& dictionary, Dictionary::Convers ionContext& context, const String& key, int8_t& value) | |
| 622 { | |
| 623 return convertIntegral<int8_t>(dictionary, context, key, value); | |
| 624 } | |
| 625 | |
| 626 template <> | |
| 627 bool DictionaryHelper::convert(const Dictionary& dictionary, Dictionary::Convers ionContext& context, const String& key, unsigned short& value) | |
| 628 { | |
| 629 return convertIntegral<unsigned short>(dictionary, context, key, value); | |
| 630 } | |
| 631 | |
| 632 template <> | |
| 633 bool DictionaryHelper::convert(const Dictionary& dictionary, Dictionary::Convers ionContext& context, const String& key, short& value) | |
| 634 { | |
| 635 return convertIntegral<short>(dictionary, context, key, value); | |
| 636 } | |
| 637 | |
| 638 template <> | |
| 639 bool DictionaryHelper::convert(const Dictionary& dictionary, Dictionary::Convers ionContext& context, const String& key, unsigned& value) | |
| 640 { | |
| 641 return convertIntegral<unsigned>(dictionary, context, key, value); | |
| 642 } | |
| 643 | |
| 644 template <> | |
| 645 bool DictionaryHelper::convert(const Dictionary& dictionary, Dictionary::Convers ionContext& context, const String& key, unsigned long& value) | |
| 646 { | |
| 647 return convertIntegral<unsigned long>(dictionary, context, key, value); | |
| 648 } | |
| 649 | |
| 650 template <> | |
| 651 bool DictionaryHelper::convert(const Dictionary& dictionary, Dictionary::Convers ionContext& context, const String& key, int& value) | |
| 652 { | |
| 653 return convertIntegral<int>(dictionary, context, key, value); | |
| 654 } | |
| 655 | |
| 656 template <> | |
| 657 bool DictionaryHelper::convert(const Dictionary& dictionary, Dictionary::Convers ionContext& context, const String& key, long& value) | |
| 658 { | |
| 659 return convertIntegral<long>(dictionary, context, key, value); | |
| 660 } | |
| 661 | |
| 662 template <> | |
| 663 bool DictionaryHelper::convert(const Dictionary& dictionary, Dictionary::Convers ionContext& context, const String& key, long long& value) | |
| 664 { | |
| 665 return convertIntegral<long long>(dictionary, context, key, value); | |
| 666 } | |
| 667 | |
| 668 template <> | |
| 669 bool DictionaryHelper::convert(const Dictionary& dictionary, Dictionary::Convers ionContext& context, const String& key, unsigned long long& value) | |
| 670 { | |
| 671 return convertIntegral<unsigned long long>(dictionary, context, key, value); | |
| 672 } | |
| 673 | |
| 674 template<typename T> bool convertNullable(const Dictionary& dictionary, Dictiona ry::ConversionContext& context, const String& key, Nullable<T>& value) | |
| 675 { | |
| 676 Dictionary::ConversionContextScope scope(context); | |
| 677 | |
| 678 v8::Local<v8::Value> v8Value; | |
| 679 if (!dictionary.get(key, v8Value)) | |
| 680 return true; | |
| 681 | |
| 682 if (context.isNullable() && WebCore::isUndefinedOrNull(v8Value)) { | |
| 683 value = Nullable<T>(); | |
| 684 return true; | |
| 685 } | |
| 686 | |
| 687 T converted = IntegralTypeTraits<T>::toIntegral(v8Value, NormalConversion, c ontext.exceptionState()); | |
| 688 | |
| 689 if (context.exceptionState().throwIfNeeded()) | |
| 690 return false; | |
| 691 | |
| 692 value = Nullable<T>(converted); | |
| 693 return true; | |
| 694 } | |
| 695 | |
| 696 template <> | |
| 697 bool DictionaryHelper::convert(const Dictionary& dictionary, Dictionary::Convers ionContext& context, const String& key, Nullable<uint8_t>& value) | |
| 698 { | |
| 699 return convertNullable<uint8_t>(dictionary, context, key, value); | |
| 700 } | |
| 701 | |
| 702 template <> | |
| 703 bool DictionaryHelper::convert(const Dictionary& dictionary, Dictionary::Convers ionContext& context, const String& key, Nullable<int8_t>& value) | |
| 704 { | |
| 705 return convertNullable<int8_t>(dictionary, context, key, value); | |
| 706 } | |
| 707 | |
| 708 template <> | |
| 709 bool DictionaryHelper::convert(const Dictionary& dictionary, Dictionary::Convers ionContext& context, const String& key, Nullable<unsigned short>& value) | |
| 710 { | |
| 711 return convertNullable<unsigned short>(dictionary, context, key, value); | |
| 712 } | |
| 713 | |
| 714 template <> | |
| 715 bool DictionaryHelper::convert(const Dictionary& dictionary, Dictionary::Convers ionContext& context, const String& key, Nullable<short>& value) | |
| 716 { | |
| 717 return convertNullable<short>(dictionary, context, key, value); | |
| 718 } | |
| 719 | |
| 720 template <> | |
| 721 bool DictionaryHelper::convert(const Dictionary& dictionary, Dictionary::Convers ionContext& context, const String& key, Nullable<unsigned>& value) | |
| 722 { | |
| 723 return convertNullable<unsigned>(dictionary, context, key, value); | |
| 724 } | |
| 725 | |
| 726 template <> | |
| 727 bool DictionaryHelper::convert(const Dictionary& dictionary, Dictionary::Convers ionContext& context, const String& key, Nullable<unsigned long>& value) | |
| 728 { | |
| 729 return convertNullable<unsigned long>(dictionary, context, key, value); | |
| 730 } | |
| 731 | |
| 732 template <> | |
| 733 bool DictionaryHelper::convert(const Dictionary& dictionary, Dictionary::Convers ionContext& context, const String& key, Nullable< int>& value) | |
|
haraken
2014/07/10 15:05:47
Nullable< int> => Nullable<int>
tasak
2014/07/11 04:51:22
Done.
| |
| 734 { | |
| 735 return convertNullable<int>(dictionary, context, key, value); | |
| 736 } | |
| 737 | |
| 738 template <> | |
| 739 bool DictionaryHelper::convert(const Dictionary& dictionary, Dictionary::Convers ionContext& context, const String& key, Nullable<long>& value) | |
| 740 { | |
| 741 return convertNullable<long>(dictionary, context, key, value); | |
| 742 } | |
| 743 | |
| 744 template <> | |
| 745 bool DictionaryHelper::convert(const Dictionary& dictionary, Dictionary::Convers ionContext& context, const String& key, Nullable<long long>& value) | |
| 746 { | |
| 747 return convertNullable<long long>(dictionary, context, key, value); | |
| 748 } | |
| 749 | |
| 750 template <> | |
| 751 bool DictionaryHelper::convert(const Dictionary& dictionary, Dictionary::Convers ionContext& context, const String& key, Nullable<unsigned long long>& value) | |
| 752 { | |
| 753 return convertNullable<unsigned long long>(dictionary, context, key, value); | |
| 754 } | |
| 755 | |
| 756 template<template <typename> class PointerType, typename T> | |
| 757 bool convertForPointerType(const Dictionary& dictionary, Dictionary::ConversionC ontext& context, const String& key, PointerType<T>& value) | |
| 758 { | |
| 759 Dictionary::ConversionContextScope scope(context); | |
| 760 | |
| 761 if (!DictionaryHelper::get(dictionary, key, value)) | |
| 762 return true; | |
| 763 | |
| 764 if (value) | |
| 765 return true; | |
| 766 | |
| 767 v8::Local<v8::Value> v8Value; | |
| 768 dictionary.get(key, v8Value); | |
| 769 if (context.isNullable() && WebCore::isUndefinedOrNull(v8Value)) | |
| 770 return true; | |
| 771 | |
| 772 context.throwTypeError(ExceptionMessages::incorrectPropertyType(key, "does n ot have a " + context.typeName() + " type.")); | |
| 773 return false; | |
| 774 } | |
| 775 | |
| 776 template <> | |
| 777 bool DictionaryHelper::convert(const Dictionary& dictionary, Dictionary::Convers ionContext& context, const String& key, RefPtrWillBeMember<LocalDOMWindow>& valu e) | |
| 778 { | |
| 779 return convertForPointerType<RefPtrWillBeMember, LocalDOMWindow>(dictionary, context, key, value); | |
| 780 } | |
| 781 | |
| 782 template <> | |
| 783 bool DictionaryHelper::convert(const Dictionary& dictionary, Dictionary::Convers ionContext& context, const String& key, RefPtrWillBeMember<Storage>& value) | |
| 784 { | |
| 785 return convertForPointerType<RefPtrWillBeMember, Storage>(dictionary, contex t, key, value); | |
| 786 } | |
| 787 | |
| 788 template <> | |
| 789 bool DictionaryHelper::convert(const Dictionary& dictionary, Dictionary::Convers ionContext& context, const String& key, RefPtr<Uint8Array>& value) | |
| 790 { | |
| 791 return convertForPointerType<RefPtr, Uint8Array>(dictionary, context, key, v alue); | |
| 792 } | |
| 793 | |
| 794 template <> | |
| 795 bool DictionaryHelper::convert(const Dictionary& dictionary, Dictionary::Convers ionContext& context, const String& key, RefPtr<ArrayBufferView>& value) | |
| 796 { | |
| 797 return convertForPointerType<RefPtr, ArrayBufferView>(dictionary, context, k ey, value); | |
| 798 } | |
| 799 | |
| 800 template <> | |
| 801 bool DictionaryHelper::convert(const Dictionary& dictionary, Dictionary::Convers ionContext& context, const String& key, RefPtrWillBeMember<MediaKeyError>& value ) | |
| 802 { | |
| 803 return convertForPointerType<RefPtrWillBeMember, MediaKeyError>(dictionary, context, key, value); | |
| 804 } | |
| 805 | |
| 806 template <> | |
| 807 bool DictionaryHelper::convert(const Dictionary& dictionary, Dictionary::Convers ionContext& context, const String& key, RefPtrWillBeMember<TrackBase>& value) | |
| 808 { | |
| 809 return convertForPointerType<RefPtrWillBeMember, TrackBase>(dictionary, cont ext, key, value); | |
| 810 } | |
| 811 | |
| 812 template <> | |
| 813 bool DictionaryHelper::convert(const Dictionary& dictionary, Dictionary::Convers ionContext& context, const String& key, RefPtrWillBeMember<EventTarget>& value) | |
| 814 { | |
| 815 return convertForPointerType<RefPtrWillBeMember, EventTarget>(dictionary, co ntext, key, value); | |
| 816 } | |
| 817 | |
| 818 template <> | |
| 819 bool DictionaryHelper::convert(const Dictionary& dictionary, Dictionary::Convers ionContext& context, const String& key, MessagePortArray& value) | |
| 820 { | |
| 821 Dictionary::ConversionContextScope scope(context); | |
| 822 | |
| 823 v8::Local<v8::Value> v8Value; | |
| 824 if (!dictionary.get(key, v8Value)) | |
| 825 return true; | |
| 826 | |
| 827 return DictionaryHelper::get(dictionary, key, value); | |
| 828 } | |
| 829 | |
| 830 } // namespace WebCore | |
| OLD | NEW |