| OLD | NEW |
| (Empty) | |
| 1 // Copyright 2011, Google Inc. |
| 2 // 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 are |
| 6 // met: |
| 7 // |
| 8 // * Redistributions of source code must retain the above copyright |
| 9 // notice, this list of conditions and the following disclaimer. |
| 10 // * Redistributions in binary form must reproduce the above |
| 11 // copyright notice, this list of conditions and the following disclaimer |
| 12 // in the documentation and/or other materials provided with the |
| 13 // distribution. |
| 14 // * Neither the name of Google Inc. nor the names of its |
| 15 // contributors may be used to endorse or promote products derived from |
| 16 // this software without specific prior written permission. |
| 17 // |
| 18 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 19 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 20 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 21 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 22 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 23 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 24 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 25 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 26 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 27 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 28 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 29 |
| 30 #ifndef DartUtilities_h |
| 31 #define DartUtilities_h |
| 32 |
| 33 #if OS(ANDROID) |
| 34 #include <sys/system_properties.h> |
| 35 #endif |
| 36 |
| 37 |
| 38 #include "bindings/common/ScriptPromise.h" |
| 39 #include "bindings/core/v8/Dictionary.h" |
| 40 #include "bindings/core/dart/DartDOMData.h" |
| 41 #include "bindings/core/dart/DartScriptValue.h" |
| 42 #include "bindings/core/dart/DartStringCache.h" |
| 43 #include "bindings/core/v8/SerializedScriptValue.h" |
| 44 #include "bindings/core/v8/V8RecursionScope.h" |
| 45 #include "bindings/core/v8/V8ScriptState.h" |
| 46 #include "bindings/dart/DartWebkitClassIds.h" |
| 47 #include "core/dom/DOMStringList.h" |
| 48 #include "core/dom/MessagePort.h" |
| 49 #include "core/inspector/ScriptCallFrame.h" |
| 50 #include "modules/mediastream/MediaStreamTrack.h" |
| 51 #include "platform/heap/Heap.h" |
| 52 |
| 53 #include "wtf/ArrayBufferView.h" |
| 54 #include "wtf/Float32Array.h" |
| 55 #include "wtf/HashMap.h" |
| 56 #include "wtf/ListHashSet.h" |
| 57 #include "wtf/Uint8Array.h" |
| 58 #include "wtf/text/WTFString.h" |
| 59 #include <dart_api.h> |
| 60 #include <dart_debugger_api.h> |
| 61 |
| 62 namespace WTF { |
| 63 class ArrayBuffer; |
| 64 class Uint8ClampedArray; |
| 65 } |
| 66 |
| 67 namespace blink { |
| 68 |
| 69 class Dictionary; |
| 70 class Frame; |
| 71 class SQLValue; |
| 72 class ScriptArguments; |
| 73 class ScriptCallStack; |
| 74 class ExecutionContext; |
| 75 class SerializedScriptValue; |
| 76 |
| 77 class DartStringPeer { |
| 78 public: |
| 79 explicit DartStringPeer(PassRefPtr<StringImpl> stringImpl) |
| 80 : m_plainString(stringImpl), m_atomicString(), m_atomicCached(false) |
| 81 { |
| 82 } |
| 83 |
| 84 explicit DartStringPeer(StringImpl* stringImpl) |
| 85 : m_plainString(stringImpl), m_atomicString(), m_atomicCached(false) |
| 86 { |
| 87 } |
| 88 |
| 89 const String& toString() const { return m_plainString; } |
| 90 const AtomicString& toAtomicString() |
| 91 { |
| 92 if (m_atomicCached) |
| 93 return m_atomicString; |
| 94 return toAtomicStringSlow(); |
| 95 } |
| 96 const AtomicString& toAtomicStringSlow() |
| 97 { |
| 98 if (!m_plainString.isNull()) { |
| 99 m_atomicString = AtomicString(m_plainString); |
| 100 ASSERT(!m_atomicString.isNull()); |
| 101 } |
| 102 m_atomicCached = true; |
| 103 return m_atomicString; |
| 104 } |
| 105 static DartStringPeer* nullString(); |
| 106 static DartStringPeer* emptyString(); |
| 107 |
| 108 private: |
| 109 DartStringPeer() : m_plainString(), m_atomicString() |
| 110 { |
| 111 } |
| 112 |
| 113 String m_plainString; |
| 114 AtomicString m_atomicString; |
| 115 bool m_atomicCached; |
| 116 }; |
| 117 |
| 118 class DartStringAdapter { |
| 119 public: |
| 120 explicit DartStringAdapter(DartStringPeer* stringPeer) : m_stringPeer(string
Peer) |
| 121 { |
| 122 ASSERT(stringPeer); |
| 123 } |
| 124 |
| 125 operator String() const { return m_stringPeer->toString(); } |
| 126 operator AtomicString() const { return m_stringPeer->toAtomicString(); } |
| 127 |
| 128 private: |
| 129 DartStringPeer* m_stringPeer; |
| 130 }; |
| 131 |
| 132 class DartApiScope { |
| 133 public: |
| 134 DartApiScope() { Dart_EnterScope(); } |
| 135 ~DartApiScope() { Dart_ExitScope(); } |
| 136 }; |
| 137 |
| 138 class DartUtilities { |
| 139 public: |
| 140 static Dart_Handle stringImplToDartString(StringImpl*); |
| 141 static Dart_Handle stringToDartString(const String&); |
| 142 static Dart_Handle stringToDartString(const AtomicString&); |
| 143 static Dart_Handle safeStringImplToDartString(StringImpl*); |
| 144 static Dart_Handle safeStringToDartString(const String&); |
| 145 static Dart_Handle safeStringToDartString(const AtomicString&); |
| 146 |
| 147 static Dart_Handle errorToException(Dart_Handle error) |
| 148 { |
| 149 ASSERT(Dart_IsError(error)); |
| 150 if (Dart_ErrorHasException(error)) |
| 151 return Dart_ErrorGetException(error); |
| 152 return Dart_NewStringFromCString(Dart_GetError(error)); |
| 153 } |
| 154 |
| 155 static bool checkResult(Dart_Handle result, Dart_Handle& exception) |
| 156 { |
| 157 if (!Dart_IsError(result)) |
| 158 return true; |
| 159 |
| 160 exception = errorToException(result); |
| 161 return false; |
| 162 } |
| 163 |
| 164 static void extractListElements(Dart_Handle list, Dart_Handle& exception, Ve
ctor<Dart_Handle>& elements); |
| 165 static Dart_Handle toList(const Vector<Dart_Handle>& elements, Dart_Handle&
exception); |
| 166 |
| 167 static void extractMapElements(Dart_Handle map, Dart_Handle& exception, Hash
Map<String, Dart_Handle>& elements); |
| 168 |
| 169 static int64_t toInteger(Dart_Handle, Dart_Handle& exception); |
| 170 static String toString(Dart_Handle); |
| 171 static void toMessagePortArray(Dart_Handle, MessagePortArray&, ArrayBufferAr
ray&, Dart_Handle& exception); |
| 172 static PassRefPtr<SerializedScriptValue> toSerializedScriptValue(Dart_Handle
, MessagePortArray*, ArrayBufferArray*, Dart_Handle& exception); |
| 173 static SQLValue toSQLValue(Dart_Handle, Dart_Handle& exception); |
| 174 template <class T, class Convertor> |
| 175 static void toVector(Convertor convertor, Dart_Handle object, Vector<T>& res
ult, Dart_Handle& exception) |
| 176 { |
| 177 ASSERT(Dart_IsList(object)); |
| 178 ASSERT(result.isEmpty()); |
| 179 |
| 180 Vector<Dart_Handle> handles; |
| 181 DartUtilities::extractListElements(object, exception, handles); |
| 182 if (exception) |
| 183 return; |
| 184 |
| 185 for (unsigned i = 0; i < handles.size(); ++i) { |
| 186 result.append(convertor(handles[i], exception)); |
| 187 if (exception) |
| 188 return; |
| 189 } |
| 190 } |
| 191 template <class T, class Convertor> |
| 192 static void toVector(Convertor convertor, Dart_Handle object, HeapVector<Mem
ber<T> >& result, Dart_Handle& exception) |
| 193 { |
| 194 ASSERT(Dart_IsList(object)); |
| 195 ASSERT(result.isEmpty()); |
| 196 |
| 197 Vector<Dart_Handle> handles; |
| 198 DartUtilities::extractListElements(object, exception, handles); |
| 199 if (exception) |
| 200 return; |
| 201 |
| 202 for (unsigned i = 0; i < handles.size(); ++i) { |
| 203 result.append(convertor(handles[i], exception)); |
| 204 if (exception) |
| 205 return; |
| 206 } |
| 207 } |
| 208 |
| 209 template <class T> |
| 210 struct TypeConvertor { }; |
| 211 |
| 212 template <class T> |
| 213 static void toNativeVector(Dart_Handle object, Vector<T>& result, Dart_Handl
e& exception) |
| 214 { |
| 215 toVector(TypeConvertor<T>(), object, result, exception); |
| 216 } |
| 217 template <class T> |
| 218 static void toNativeVector(Dart_NativeArguments args, int idx, Vector<T>& re
sult, Dart_Handle& exception) |
| 219 { |
| 220 Dart_Handle object = Dart_GetNativeArgument(args, idx); |
| 221 toVector(TypeConvertor<T>(), object, result, exception); |
| 222 } |
| 223 |
| 224 template <class T> |
| 225 static void toNativeVector(Dart_Handle object, HeapVector<Member<T> >& resul
t, Dart_Handle& exception) |
| 226 { |
| 227 toVector(TypeConvertor<T>(), object, result, exception); |
| 228 } |
| 229 template <class T> |
| 230 static void toNativeVector(Dart_NativeArguments args, int idx, HeapVector<Me
mber<T> >& result, Dart_Handle& exception) |
| 231 { |
| 232 Dart_Handle object = Dart_GetNativeArgument(args, idx); |
| 233 toVector(TypeConvertor<T>(), object, result, exception); |
| 234 } |
| 235 |
| 236 static Dart_Handle listHashSetToDartList(ListHashSet<String> set, Dart_Handl
e& exception) |
| 237 { |
| 238 Dart_Handle result = Dart_NewList(set.size()); |
| 239 if (!DartUtilities::checkResult(result, exception)) |
| 240 return Dart_Null(); |
| 241 |
| 242 ListHashSet<String>::const_iterator end = set.end(); |
| 243 int index = 0; |
| 244 for (ListHashSet<String>::const_iterator it = set.begin(); it != end; ++
it, ++index) |
| 245 Dart_ListSetAt(result, index, DartUtilities::stringToDartString(*it)
); |
| 246 return result; |
| 247 } |
| 248 |
| 249 static Dictionary dartToDictionary(Dart_Handle, Dart_Handle& exception); |
| 250 static Dictionary dartToDictionaryWithNullCheck(Dart_Handle, Dart_Handle& ex
ception); |
| 251 static Dictionary dartToDictionary(Dart_NativeArguments args, int idx, Dart_
Handle& exception); |
| 252 static Dictionary dartToDictionaryWithNullCheck(Dart_NativeArguments args, i
nt idx, Dart_Handle& exception); |
| 253 |
| 254 static LocalDOMWindow* domWindowForCurrentIsolate(); |
| 255 static LocalDOMWindow* enteredDomWindowForCurrentIsolate() |
| 256 { |
| 257 // FIXME: Add check to enforce this is the right window. |
| 258 return domWindowForCurrentIsolate(); |
| 259 } |
| 260 |
| 261 static LocalDOMWindow* callingDomWindowForCurrentIsolate() |
| 262 { |
| 263 // FIXME: Add check to enforce this is the right window. |
| 264 return domWindowForCurrentIsolate(); |
| 265 } |
| 266 |
| 267 static V8ScriptState* v8ScriptStateForCurrentIsolate(); |
| 268 |
| 269 static ExecutionContext* scriptExecutionContext(); |
| 270 // FIXMEDART: Should have a variant that takes a Dart_NativeArguments to avo
id TLS. |
| 271 static DartScriptState* currentScriptState(); |
| 272 |
| 273 static bool processingUserGesture(); |
| 274 |
| 275 static PassRefPtr<ScriptArguments> createScriptArguments(Dart_Handle argumen
t, Dart_Handle& exception); |
| 276 |
| 277 static PassRefPtr<ScriptCallStack> createScriptCallStack(); |
| 278 static ScriptCallFrame getTopFrame(Dart_StackTrace, Dart_Handle& exception); |
| 279 static ScriptCallFrame toScriptCallFrame(Dart_ActivationFrame, Dart_Handle&
exception); |
| 280 |
| 281 static const uint8_t* fullSnapshot(LocalFrame*); |
| 282 |
| 283 static Dart_Handle canonicalizeUrl(Dart_Handle library, Dart_Handle urlHandl
e, String url); |
| 284 |
| 285 static void reportProblem(ExecutionContext*, const String&, int line = 0, in
t col = 0); |
| 286 static void reportProblem(ExecutionContext*, Dart_Handle); |
| 287 static void reportProblem(ExecutionContext*, Dart_Handle, const String& sour
ceURL); |
| 288 |
| 289 static Dart_Handle toDartCoreException(const String &className, const String
& message); |
| 290 |
| 291 static Dart_Handle coreArgumentErrorException(const String& message); |
| 292 |
| 293 static Dart_Handle invalidNumberOfArgumentsException() |
| 294 { |
| 295 return Dart_NewStringFromCString("Invalid number of arguments"); |
| 296 } |
| 297 |
| 298 static Dart_Handle conditionalFunctionalityException() |
| 299 { |
| 300 return Dart_NewStringFromCString("Ooops, this functionality is not enabl
ed in this browser"); |
| 301 } |
| 302 |
| 303 static Dart_Handle notImplementedException(const char* fileName, int lineNum
ber); |
| 304 |
| 305 static Dart_Handle internalErrorException(const char* msg) |
| 306 { |
| 307 // FIXME: wrap into proper type. |
| 308 return Dart_NewStringFromCString(msg); |
| 309 } |
| 310 |
| 311 static Dart_Handle newResolvedPromise(Dart_Handle value); |
| 312 static Dart_Handle newSmashedPromise(Dart_Handle error); |
| 313 static Dart_Handle newResolver(); |
| 314 static Dart_Handle newArgumentError(const String& message); |
| 315 |
| 316 static Dart_Handle invokeUtilsMethod(const char* methodName, int argCount, D
art_Handle* args); |
| 317 |
| 318 static Dart_Handle convertSourceString(const String& source) |
| 319 { |
| 320 // FIXME: Decide whether to externalize this here. |
| 321 const CString utf8encoded = source.utf8(); |
| 322 return Dart_NewStringFromUTF8( |
| 323 reinterpret_cast<const uint8_t*>(utf8encoded.data()), |
| 324 utf8encoded.length()); |
| 325 } |
| 326 |
| 327 static DartStringAdapter dartToString(Dart_Handle object, Dart_Handle& excep
tion) |
| 328 { |
| 329 intptr_t charsize = 0; |
| 330 intptr_t strlength = 0; |
| 331 void* peer = 0; |
| 332 Dart_Handle result = Dart_StringGetProperties(object, &charsize, &strlen
gth, &peer); |
| 333 if (peer) { |
| 334 return DartStringAdapter(reinterpret_cast<DartStringPeer*>(peer)); |
| 335 } |
| 336 if (Dart_IsError(result)) { |
| 337 exception = Dart_NewStringFromCString("String Expected"); |
| 338 return DartStringAdapter(DartStringPeer::nullString()); |
| 339 } |
| 340 return dartToStringImpl(object, exception, true); |
| 341 } |
| 342 static DartStringAdapter dartToString(Dart_NativeArguments args, int index,
Dart_Handle& exception, bool autoDartScope = true) |
| 343 { |
| 344 void* peer = 0; |
| 345 Dart_Handle object = Dart_GetNativeStringArgument(args, index, &peer); |
| 346 if (peer) { |
| 347 return DartStringAdapter(reinterpret_cast<DartStringPeer*>(peer)); |
| 348 } |
| 349 if (Dart_IsError(object)) { |
| 350 exception = Dart_NewStringFromCString("String Expected"); |
| 351 return DartStringAdapter(DartStringPeer::nullString()); |
| 352 } |
| 353 return dartToStringImpl(object, exception, autoDartScope); |
| 354 } |
| 355 static DartStringAdapter dartToStringWithNullCheck(Dart_NativeArguments args
, int index, Dart_Handle& exception, bool autoDartScope = true) |
| 356 { |
| 357 void* peer = 0; |
| 358 Dart_Handle object = Dart_GetNativeStringArgument(args, index, &peer); |
| 359 if (peer) { |
| 360 return DartStringAdapter(reinterpret_cast<DartStringPeer*>(peer)); |
| 361 } |
| 362 if (Dart_IsNull(object)) { |
| 363 return DartStringAdapter(DartStringPeer::nullString()); |
| 364 } |
| 365 return dartToString(args, index, exception, autoDartScope); |
| 366 } |
| 367 static DartStringAdapter dartToStringWithEmptyCheck(Dart_NativeArguments arg
s, int index, Dart_Handle& exception, bool autoDartScope = true) |
| 368 { |
| 369 void* peer = 0; |
| 370 Dart_Handle object = Dart_GetNativeStringArgument(args, index, &peer); |
| 371 if (peer) { |
| 372 return DartStringAdapter(reinterpret_cast<DartStringPeer*>(peer)); |
| 373 } |
| 374 if (Dart_IsNull(object)) { |
| 375 return DartStringAdapter(DartStringPeer::emptyString()); |
| 376 } |
| 377 return dartToString(args, index, exception, autoDartScope); |
| 378 } |
| 379 static Dart_Handle stringToDart(const AtomicString& value) |
| 380 { |
| 381 DartDOMData* domData = DartDOMData::current(); |
| 382 StringImpl* stringImpl = value.impl(); |
| 383 if (!stringImpl) { |
| 384 return domData->emptyString(); |
| 385 } |
| 386 return Dart_HandleFromWeakPersistent(domData->stringCache().get(stringIm
pl)); |
| 387 } |
| 388 static Dart_Handle stringToDart(const String& value) |
| 389 { |
| 390 DartDOMData* domData = DartDOMData::current(); |
| 391 StringImpl* stringImpl = value.impl(); |
| 392 if (!stringImpl) { |
| 393 return domData->emptyString(); |
| 394 } |
| 395 return Dart_HandleFromWeakPersistent(domData->stringCache().get(stringIm
pl)); |
| 396 } |
| 397 template <class StringClass> |
| 398 static Dart_Handle stringToDartWithNullCheck(const StringClass& value) |
| 399 { |
| 400 if (value.isNull()) |
| 401 return Dart_Null(); |
| 402 return stringToDart(value); |
| 403 } |
| 404 static void setDartUnsignedReturnValue(Dart_NativeArguments args, unsigned v
alue) |
| 405 { |
| 406 Dart_SetIntegerReturnValue(args, value); |
| 407 } |
| 408 static void setDartUnsignedLongLongReturnValue(Dart_NativeArguments args, un
signed long long value) |
| 409 { |
| 410 // FIXME: WebIDL unsigned long long is guaranteed to fit into 64-bit uns
igned, |
| 411 // so we need a dart API for constructing an integer from uint64_t. |
| 412 ASSERT(value <= 0x7fffffffffffffffLL); |
| 413 Dart_SetIntegerReturnValue(args, static_cast<int64_t>(value)); |
| 414 } |
| 415 static void setDartIntegerReturnValue(Dart_NativeArguments args, int64_t val
ue) |
| 416 { |
| 417 Dart_SetIntegerReturnValue(args, value); |
| 418 } |
| 419 static void setDartStringReturnValue(Dart_NativeArguments args, const Atomic
String& value, bool autoDartScope = true) |
| 420 { |
| 421 DartDOMData* domData = static_cast<DartDOMData*>(Dart_GetNativeIsolateDa
ta(args)); |
| 422 StringImpl* stringImpl = value.impl(); |
| 423 if (!stringImpl) { |
| 424 Dart_SetReturnValue(args, domData->emptyString()); |
| 425 } else { |
| 426 Dart_SetWeakHandleReturnValue(args, domData->stringCache().get(value
.impl(), autoDartScope)); |
| 427 } |
| 428 } |
| 429 static void setDartStringReturnValue(Dart_NativeArguments args, const String
& value, bool autoDartScope = true) |
| 430 { |
| 431 DartDOMData* domData = static_cast<DartDOMData*>(Dart_GetNativeIsolateDa
ta(args)); |
| 432 StringImpl* stringImpl = value.impl(); |
| 433 if (!stringImpl) { |
| 434 Dart_SetReturnValue(args, domData->emptyString()); |
| 435 } else { |
| 436 Dart_SetWeakHandleReturnValue(args, domData->stringCache().get(value
.impl(), autoDartScope)); |
| 437 } |
| 438 } |
| 439 static void setDartStringReturnValueWithNullCheck( |
| 440 Dart_NativeArguments args, const AtomicString& value, bool autoDartScope
= true) |
| 441 { |
| 442 if (value.isNull()) { |
| 443 Dart_SetReturnValue(args, Dart_Null()); |
| 444 } else { |
| 445 setDartStringReturnValue(args, value, autoDartScope); |
| 446 } |
| 447 } |
| 448 static void setDartStringReturnValueWithNullCheck(Dart_NativeArguments args,
const String& value, bool autoDartScope = true) |
| 449 { |
| 450 if (value.isNull()) { |
| 451 Dart_SetReturnValue(args, Dart_Null()); |
| 452 } else { |
| 453 setDartStringReturnValue(args, value, autoDartScope); |
| 454 } |
| 455 } |
| 456 |
| 457 // ScalarValueString helpers. ScalarValueString is identical to DOMString |
| 458 // except that "convert a DOMString to a sequence of Unicode characters". |
| 459 |
| 460 static DartStringAdapter dartToScalarValueString(Dart_Handle object, Dart_Ha
ndle& exception) |
| 461 { |
| 462 intptr_t charsize = 0; |
| 463 intptr_t strlength = 0; |
| 464 void* peer = 0; |
| 465 Dart_Handle result = Dart_StringGetProperties(object, &charsize, &strlen
gth, &peer); |
| 466 if (peer) { |
| 467 return DartStringAdapter(reinterpret_cast<DartStringPeer*>(peer)); |
| 468 } |
| 469 if (Dart_IsError(result)) { |
| 470 exception = Dart_NewStringFromCString("String Expected"); |
| 471 return DartStringAdapter(DartStringPeer::nullString()); |
| 472 } |
| 473 // TODO(terry): Need to implement similar code to replace with UTF-16 |
| 474 // with valid unicode. See V8Bindings.h implementation. |
| 475 return dartToStringImpl(object, exception, true); |
| 476 } |
| 477 static DartStringAdapter dartToScalarValueString(Dart_NativeArguments args,
int index, Dart_Handle& exception, bool autoDartScope = true) |
| 478 { |
| 479 void* peer = 0; |
| 480 Dart_Handle object = Dart_GetNativeStringArgument(args, index, &peer); |
| 481 if (peer) { |
| 482 return DartStringAdapter(reinterpret_cast<DartStringPeer*>(peer)); |
| 483 } |
| 484 if (Dart_IsError(object)) { |
| 485 exception = Dart_NewStringFromCString("String Expected"); |
| 486 return DartStringAdapter(DartStringPeer::nullString()); |
| 487 } |
| 488 // TODO(terry): Need to implement similar code to replace with UTF-16 |
| 489 // with valid unicode. See V8Bindings.h implementation. |
| 490 return dartToStringImpl(object, exception, autoDartScope); |
| 491 } |
| 492 static DartStringAdapter dartToScalarValueStringWithNullCheck(Dart_NativeArg
uments args, int index, Dart_Handle& exception, bool autoDartScope = true) |
| 493 { |
| 494 void* peer = 0; |
| 495 Dart_Handle object = Dart_GetNativeStringArgument(args, index, &peer); |
| 496 if (peer) { |
| 497 return DartStringAdapter(reinterpret_cast<DartStringPeer*>(peer)); |
| 498 } |
| 499 if (Dart_IsNull(object)) { |
| 500 return DartStringAdapter(DartStringPeer::nullString()); |
| 501 } |
| 502 return dartToScalarValueString(args, index, exception, autoDartScope); |
| 503 } |
| 504 static void setDartScalarValueStringReturnValue(Dart_NativeArguments args, c
onst AtomicString& value, bool autoDartScope = true) |
| 505 { |
| 506 DartDOMData* domData = static_cast<DartDOMData*>(Dart_GetNativeIsolateDa
ta(args)); |
| 507 StringImpl* stringImpl = value.impl(); |
| 508 if (!stringImpl) { |
| 509 Dart_SetReturnValue(args, domData->emptyString()); |
| 510 } else { |
| 511 // TODO(terry): Need to implement similar code to replace with UTF-1
6 |
| 512 // with valid unicode. See V8Bindings.h implementation. |
| 513 } |
| 514 } |
| 515 static void setDartScalarValueStringReturnValue(Dart_NativeArguments args, c
onst String& value, bool autoDartScope = true) |
| 516 { |
| 517 DartDOMData* domData = static_cast<DartDOMData*>(Dart_GetNativeIsolateDa
ta(args)); |
| 518 StringImpl* stringImpl = value.impl(); |
| 519 if (!stringImpl) { |
| 520 Dart_SetReturnValue(args, domData->emptyString()); |
| 521 } else { |
| 522 // TODO(terry): Need to implement similar code to replace with UTF-1
6 |
| 523 // with valid unicode. See V8Bindings.h implementation. |
| 524 } |
| 525 } |
| 526 static void setDartScalarValueStringReturnValueWithNullCheck( |
| 527 Dart_NativeArguments args, const AtomicString& value, bool autoDartScope
= true) |
| 528 { |
| 529 if (value.isNull()) { |
| 530 Dart_SetReturnValue(args, Dart_Null()); |
| 531 } else { |
| 532 setDartScalarValueStringReturnValue(args, value, autoDartScope); |
| 533 } |
| 534 } |
| 535 static void setDartScalarValueStringReturnValueWithNullCheck(Dart_NativeArgu
ments args, const String& value, bool autoDartScope = true) |
| 536 { |
| 537 if (value.isNull()) { |
| 538 Dart_SetReturnValue(args, Dart_Null()); |
| 539 } else { |
| 540 setDartScalarValueStringReturnValue(args, value, autoDartScope); |
| 541 } |
| 542 } |
| 543 |
| 544 // ByteString helpers. Converts a value to a String, throwing if any code |
| 545 // unit is outside 0-255. |
| 546 |
| 547 static DartStringAdapter dartToByteString(Dart_Handle object, Dart_Handle& e
xception) |
| 548 { |
| 549 intptr_t charsize = 0; |
| 550 intptr_t strlength = 0; |
| 551 void* peer = 0; |
| 552 Dart_Handle result = Dart_StringGetProperties(object, &charsize, &strlen
gth, &peer); |
| 553 if (peer) { |
| 554 return DartStringAdapter(reinterpret_cast<DartStringPeer*>(peer)); |
| 555 } |
| 556 if (Dart_IsError(result)) { |
| 557 exception = Dart_NewStringFromCString("String Expected"); |
| 558 return DartStringAdapter(DartStringPeer::nullString()); |
| 559 } |
| 560 // TODO(terry): Need to implement similar code to toByteString see |
| 561 // bindings/core/v8/V8Bindings.h for implementation. |
| 562 return dartToStringImpl(object, exception, true); |
| 563 } |
| 564 static DartStringAdapter dartToByteString(Dart_NativeArguments args, int ind
ex, Dart_Handle& exception, bool autoDartScope = true) |
| 565 { |
| 566 void* peer = 0; |
| 567 Dart_Handle object = Dart_GetNativeStringArgument(args, index, &peer); |
| 568 if (peer) { |
| 569 return DartStringAdapter(reinterpret_cast<DartStringPeer*>(peer)); |
| 570 } |
| 571 if (Dart_IsError(object)) { |
| 572 exception = Dart_NewStringFromCString("String Expected"); |
| 573 return DartStringAdapter(DartStringPeer::nullString()); |
| 574 } |
| 575 // TODO(terry): Need to implement similar code to toByteString see |
| 576 // bindings/core/v8/V8Bindings.h for implementation. |
| 577 return dartToStringImpl(object, exception, autoDartScope); |
| 578 } |
| 579 static DartStringAdapter dartToByteStringWithNullCheck(Dart_NativeArguments
args, int index, Dart_Handle& exception, bool autoDartScope = true) |
| 580 { |
| 581 void* peer = 0; |
| 582 Dart_Handle object = Dart_GetNativeStringArgument(args, index, &peer); |
| 583 if (peer) { |
| 584 return DartStringAdapter(reinterpret_cast<DartStringPeer*>(peer)); |
| 585 } |
| 586 if (Dart_IsNull(object)) { |
| 587 return DartStringAdapter(DartStringPeer::nullString()); |
| 588 } |
| 589 // TODO(terry): Need to implement similar code to toByteString see |
| 590 // bindings/core/v8/V8Bindings.h for implementation. |
| 591 return dartToScalarValueString(args, index, exception, autoDartScope); |
| 592 } |
| 593 static void setDartByteStringReturnValue(Dart_NativeArguments args, const At
omicString& value, bool autoDartScope = true) |
| 594 { |
| 595 DartDOMData* domData = static_cast<DartDOMData*>(Dart_GetNativeIsolateDa
ta(args)); |
| 596 StringImpl* stringImpl = value.impl(); |
| 597 if (!stringImpl) { |
| 598 Dart_SetReturnValue(args, domData->emptyString()); |
| 599 } else { |
| 600 // TODO(terry): Need to implement similar code to toByteString see |
| 601 // bindings/core/v8/V8Bindings.h for implementation. |
| 602 } |
| 603 } |
| 604 static void setDartByteStringReturnValue(Dart_NativeArguments args, const St
ring& value, bool autoDartScope = true) |
| 605 { |
| 606 DartDOMData* domData = static_cast<DartDOMData*>(Dart_GetNativeIsolateDa
ta(args)); |
| 607 StringImpl* stringImpl = value.impl(); |
| 608 if (!stringImpl) { |
| 609 Dart_SetReturnValue(args, domData->emptyString()); |
| 610 } else { |
| 611 // TODO(terry): Need to implement similar code to toByteString see |
| 612 // bindings/core/v8/V8Bindings.h for implementation. |
| 613 } |
| 614 } |
| 615 static void setDartByteStringReturnValueWithNullCheck( |
| 616 Dart_NativeArguments args, const AtomicString& value, bool autoDartScope
= true) |
| 617 { |
| 618 if (value.isNull()) { |
| 619 Dart_SetReturnValue(args, Dart_Null()); |
| 620 } else { |
| 621 setDartByteStringReturnValue(args, value, autoDartScope); |
| 622 } |
| 623 } |
| 624 static void setDartByteStringReturnValueWithNullCheck(Dart_NativeArguments a
rgs, const String& value, bool autoDartScope = true) |
| 625 { |
| 626 if (value.isNull()) { |
| 627 Dart_SetReturnValue(args, Dart_Null()); |
| 628 } else { |
| 629 setDartByteStringReturnValue(args, value, autoDartScope); |
| 630 } |
| 631 } |
| 632 |
| 633 static bool dartToBool(Dart_Handle, Dart_Handle& exception); |
| 634 static bool dartToBoolWithNullCheck(Dart_Handle object, Dart_Handle& excepti
on) |
| 635 { |
| 636 // dartToBool converts Null to false |
| 637 return dartToBool(object, exception); |
| 638 } |
| 639 static bool dartToBool(Dart_NativeArguments args, int index, Dart_Handle& ex
ception) |
| 640 { |
| 641 // FIXME: There is currently no difference between doing the null check
and not. |
| 642 // If this is the desired semantics, we should stop generating these. If
not, |
| 643 // we should make this version throw an error. |
| 644 return dartToBoolWithNullCheck(args, index, exception); |
| 645 } |
| 646 static bool dartToBoolWithNullCheck(Dart_NativeArguments args, int index, Da
rt_Handle& exception) |
| 647 { |
| 648 // Dart_GetNativeBooleanArgument converts null to false |
| 649 bool value; |
| 650 Dart_Handle result = Dart_GetNativeBooleanArgument(args, index, &value); |
| 651 if (Dart_IsError(result)) { |
| 652 exception = Dart_NewStringFromCString(Dart_GetError(result)); |
| 653 return false; |
| 654 } |
| 655 return value; |
| 656 } |
| 657 static Dart_Handle boolToDart(bool value) |
| 658 { |
| 659 return Dart_NewBoolean(value); |
| 660 } |
| 661 |
| 662 static int dartToInt(Dart_Handle, Dart_Handle& exception); |
| 663 static int dartToInt(Dart_NativeArguments args, int index, Dart_Handle& exce
ption) |
| 664 { |
| 665 int64_t value; |
| 666 Dart_Handle result = Dart_GetNativeIntegerArgument(args, index, &value); |
| 667 if (Dart_IsError(result)) { |
| 668 exception = Dart_NewStringFromCString(Dart_GetError(result)); |
| 669 return 0; |
| 670 } |
| 671 if (value < INT_MIN || value > INT_MAX) { |
| 672 exception = Dart_NewStringFromCString("value out of range"); |
| 673 return 0; |
| 674 } |
| 675 return value; |
| 676 } |
| 677 static Dart_Handle intToDart(int64_t value) |
| 678 { |
| 679 return Dart_NewInteger(value); |
| 680 } |
| 681 |
| 682 static unsigned dartToUnsigned(Dart_Handle, Dart_Handle& exception); |
| 683 static unsigned dartToUnsigned(Dart_NativeArguments args, int index, Dart_Ha
ndle& exception) |
| 684 { |
| 685 int64_t value; |
| 686 Dart_Handle result = Dart_GetNativeIntegerArgument(args, index, &value); |
| 687 if (Dart_IsError(result)) { |
| 688 exception = Dart_NewStringFromCString(Dart_GetError(result)); |
| 689 return 0; |
| 690 } |
| 691 if (value < 0 || value > UINT_MAX) { |
| 692 exception = Dart_NewStringFromCString("value out of range"); |
| 693 return 0; |
| 694 } |
| 695 return value; |
| 696 } |
| 697 static Dart_Handle unsignedToDart(unsigned value) |
| 698 { |
| 699 return Dart_NewInteger(value); |
| 700 } |
| 701 |
| 702 static long long dartToLongLong(Dart_Handle, Dart_Handle& exception); |
| 703 static long long dartToLongLong(Dart_NativeArguments args, int index, Dart_H
andle& exception) |
| 704 { |
| 705 int64_t value; |
| 706 Dart_Handle result = Dart_GetNativeIntegerArgument(args, index, &value); |
| 707 if (Dart_IsError(result)) { |
| 708 exception = Dart_NewStringFromCString(Dart_GetError(result)); |
| 709 return 0; |
| 710 } |
| 711 return value; |
| 712 } |
| 713 static Dart_Handle longLongToDart(long long value) |
| 714 { |
| 715 return Dart_NewInteger(value); |
| 716 } |
| 717 |
| 718 static unsigned long long dartToUnsignedLongLong(Dart_Handle, Dart_Handle& e
xception); |
| 719 static unsigned long long dartToUnsignedLongLong(Dart_NativeArguments args,
int index, Dart_Handle& exception) |
| 720 { |
| 721 int64_t value; |
| 722 Dart_Handle result = Dart_GetNativeIntegerArgument(args, index, &value); |
| 723 if (Dart_IsError(result)) { |
| 724 exception = Dart_NewStringFromCString(Dart_GetError(result)); |
| 725 return 0; |
| 726 } |
| 727 if (value < 0) { |
| 728 exception = Dart_NewStringFromCString("value out of range"); |
| 729 return 0; |
| 730 } |
| 731 return value; |
| 732 } |
| 733 static Dart_Handle unsignedLongLongToDart(unsigned long long value) |
| 734 { |
| 735 // FIXME: WebIDL unsigned long long is guaranteed to fit into 64-bit uns
igned, |
| 736 // so we need a dart API for constructing an integer from uint64_t. |
| 737 ASSERT(value <= 0x7fffffffffffffffLL); |
| 738 return Dart_NewInteger(static_cast<int64_t>(value)); |
| 739 } |
| 740 |
| 741 static double dartToDouble(Dart_Handle, Dart_Handle& exception); |
| 742 static double dartToDouble(Dart_NativeArguments args, int index, Dart_Handle
& exception) |
| 743 { |
| 744 double value; |
| 745 Dart_Handle result = Dart_GetNativeDoubleArgument(args, index, &value); |
| 746 if (Dart_IsError(result)) { |
| 747 exception = Dart_NewStringFromCString(Dart_GetError(result)); |
| 748 return 0; |
| 749 } |
| 750 return value; |
| 751 } |
| 752 static Dart_Handle doubleToDart(double value) |
| 753 { |
| 754 return Dart_NewDouble(value); |
| 755 } |
| 756 |
| 757 static Dart_Handle numberToDart(double value); |
| 758 |
| 759 static intptr_t libraryHandleToLibraryId(Dart_Handle library); |
| 760 |
| 761 static ScriptValue dartToScriptValue(Dart_Handle); |
| 762 static ScriptValue dartToScriptValue(Dart_NativeArguments args, int index) |
| 763 { |
| 764 Dart_Handle object = Dart_GetNativeArgument(args, index); |
| 765 return dartToScriptValue(object); |
| 766 } |
| 767 // NOTE: consider revisiting this once situation w/ optional arguments check
s |
| 768 // in dart:html is clearer. |
| 769 // The implementation below will convert null to undefined script value. Whi
ch |
| 770 // is exactly what is desired if actual value wasn't specified at call site, |
| 771 // but is most probably wrong if null was explicitly provided. On more posit
ive |
| 772 // side indexed db are converted to Dart null anyway, so it might be tricky |
| 773 // to observe from user code. |
| 774 static ScriptValue dartToScriptValueWithNullCheck(Dart_Handle handle) |
| 775 { |
| 776 if (Dart_IsNull(handle)) { |
| 777 V8ScriptState* scriptState = v8ScriptStateForCurrentIsolate(); |
| 778 return ScriptValue(scriptState, v8::Undefined(scriptState->isolate()
)); |
| 779 } |
| 780 return dartToScriptValue(handle); |
| 781 } |
| 782 static ScriptValue dartToScriptValueWithNullCheck(Dart_NativeArguments args,
int index) |
| 783 { |
| 784 Dart_Handle object = Dart_GetNativeArgument(args, index); |
| 785 return dartToScriptValueWithNullCheck(object); |
| 786 } |
| 787 static Dart_Handle scriptValueToDart(const ScriptValue&); |
| 788 static Dart_Handle scriptPromiseToDart(const ScriptPromise&); |
| 789 |
| 790 static PassRefPtr<SerializedScriptValue> dartToSerializedScriptValue(Dart_Ha
ndle, Dart_Handle& exception); |
| 791 static Dart_Handle serializedScriptValueToDart(PassRefPtr<SerializedScriptVa
lue>); |
| 792 |
| 793 static Dart_Handle dateToDart(double); |
| 794 static double dartToDate(Dart_Handle, Dart_Handle&); |
| 795 static double dartToDate(Dart_NativeArguments args, int idx, Dart_Handle&); |
| 796 static bool isDateTime(DartDOMData*, Dart_Handle); |
| 797 static bool isTypeSubclassOf(Dart_Handle type, Dart_Handle library, const ch
ar* typeName); |
| 798 static Dart_Handle getAndValidateNativeType(Dart_Handle type, const String&
tagName); |
| 799 static bool objectIsType(Dart_Handle, Dart_Handle type); |
| 800 |
| 801 static bool isFunction(DartDOMData*, Dart_Handle); |
| 802 |
| 803 static Dart_Handle arrayBufferToDart(WTF::ArrayBuffer*); |
| 804 static Dart_Handle arrayBufferToDart(PassRefPtr<WTF::ArrayBuffer> value) |
| 805 { |
| 806 return arrayBufferToDart(value.get()); |
| 807 } |
| 808 |
| 809 static Dart_Handle arrayBufferViewToDart(WTF::ArrayBufferView*); |
| 810 |
| 811 // FIXME: For typed data views in the VM heap, this currently creates |
| 812 // a new array buffer object with a copy of the data and loses the |
| 813 // connection to the dart object. |
| 814 static Dart_Handle arrayBufferViewToDart(PassRefPtr<WTF::ArrayBufferView> va
lue) |
| 815 { |
| 816 return arrayBufferViewToDart(value.get()); |
| 817 } |
| 818 |
| 819 static PassRefPtr<WTF::ArrayBuffer> dartToArrayBuffer(Dart_Handle array, Dar
t_Handle& exception); |
| 820 static PassRefPtr<WTF::ArrayBuffer> dartToArrayBufferWithNullCheck(Dart_Hand
le array, Dart_Handle& exception) |
| 821 { |
| 822 return Dart_IsNull(array) ? nullptr : dartToArrayBuffer(array, exception
); |
| 823 } |
| 824 static PassRefPtr<WTF::ArrayBuffer> dartToArrayBuffer(Dart_NativeArguments a
rgs, int index, Dart_Handle& exception) |
| 825 { |
| 826 Dart_Handle object = Dart_GetNativeArgument(args, index); |
| 827 return dartToArrayBuffer(object, exception); |
| 828 } |
| 829 static PassRefPtr<WTF::ArrayBuffer> dartToArrayBufferWithNullCheck(Dart_Nati
veArguments args, int idx, Dart_Handle& excp) |
| 830 { |
| 831 Dart_Handle object = Dart_GetNativeArgument(args, idx); |
| 832 return dartToArrayBufferWithNullCheck(object, excp); |
| 833 } |
| 834 |
| 835 // FIXME: For typed data objects in the VM heap, this currently creates |
| 836 // a new array buffer object with a copy of the data and loses the |
| 837 // connection to the dart object. |
| 838 static PassRefPtr<WTF::ArrayBuffer> dartToExternalizedArrayBuffer(Dart_Handl
e, Dart_Handle&); |
| 839 |
| 840 static PassRefPtr<WTF::ArrayBufferView> dartToArrayBufferView(Dart_Handle, D
art_Handle&); |
| 841 static PassRefPtr<WTF::ArrayBufferView> dartToArrayBufferViewWithNullCheck(D
art_Handle array, Dart_Handle& exception) |
| 842 { |
| 843 return Dart_IsNull(array) ? nullptr : dartToArrayBufferView(array, excep
tion); |
| 844 } |
| 845 static PassRefPtr<WTF::ArrayBufferView> dartToArrayBufferView(Dart_NativeArg
uments args, int idx, Dart_Handle& exception) |
| 846 { |
| 847 Dart_Handle object = Dart_GetNativeArgument(args, idx); |
| 848 return dartToArrayBufferView(object, exception); |
| 849 } |
| 850 static PassRefPtr<WTF::ArrayBufferView> dartToArrayBufferViewWithNullCheck(D
art_NativeArguments args, int idx, Dart_Handle& exception) |
| 851 { |
| 852 Dart_Handle object = Dart_GetNativeArgument(args, idx); |
| 853 return dartToArrayBufferViewWithNullCheck(object, exception); |
| 854 } |
| 855 |
| 856 static PassRefPtr<WTF::ArrayBufferView> dartToExternalizedArrayBufferView(Da
rt_Handle, Dart_Handle&); |
| 857 |
| 858 static PassRefPtr<WTF::Int8Array> dartToInt8Array(Dart_Handle, Dart_Handle&)
; |
| 859 static PassRefPtr<WTF::Int8Array> dartToInt8ArrayWithNullCheck(Dart_Handle,
Dart_Handle&); |
| 860 static PassRefPtr<WTF::Int8Array> dartToInt8Array(Dart_NativeArguments args,
int idx, Dart_Handle& exception); |
| 861 static PassRefPtr<WTF::Int8Array> dartToInt8ArrayWithNullCheck(Dart_NativeAr
guments args, int idx, Dart_Handle& exception); |
| 862 static PassRefPtr<WTF::Int32Array> dartToInt32Array(Dart_Handle, Dart_Handle
&); |
| 863 static PassRefPtr<WTF::Int32Array> dartToInt32ArrayWithNullCheck(Dart_Handle
, Dart_Handle&); |
| 864 static PassRefPtr<WTF::Int32Array> dartToInt32Array(Dart_NativeArguments arg
s, int idx, Dart_Handle& exception); |
| 865 static PassRefPtr<WTF::Int32Array> dartToInt32ArrayWithNullCheck(Dart_Native
Arguments args, int idx, Dart_Handle& exception); |
| 866 static PassRefPtr<WTF::Uint8Array> dartToUint8Array(Dart_Handle, Dart_Handle
&); |
| 867 static PassRefPtr<WTF::Uint8Array> dartToUint8ArrayWithNullCheck(Dart_Handle
, Dart_Handle&); |
| 868 static PassRefPtr<WTF::Uint8Array> dartToUint8Array(Dart_NativeArguments arg
s, int idx, Dart_Handle& exception); |
| 869 static PassRefPtr<WTF::Uint8Array> dartToUint8ArrayWithNullCheck(Dart_Native
Arguments args, int idx, Dart_Handle& exception); |
| 870 static PassRefPtr<WTF::Uint8ClampedArray> dartToUint8ClampedArray(Dart_Handl
e, Dart_Handle&); |
| 871 static PassRefPtr<WTF::Uint8ClampedArray> dartToUint8ClampedArrayWithNullChe
ck(Dart_Handle, Dart_Handle&); |
| 872 static PassRefPtr<WTF::Uint8ClampedArray> dartToUint8ClampedArray(Dart_Nativ
eArguments args, int idx, Dart_Handle& exception); |
| 873 static PassRefPtr<WTF::Uint8ClampedArray> dartToUint8ClampedArrayWithNullChe
ck(Dart_NativeArguments args, int idx, Dart_Handle& exception); |
| 874 static PassRefPtr<WTF::Float32Array> dartToFloat32Array(Dart_Handle, Dart_Ha
ndle&); |
| 875 static PassRefPtr<WTF::Float32Array> dartToFloat32ArrayWithNullCheck(Dart_Ha
ndle, Dart_Handle&); |
| 876 static PassRefPtr<WTF::Float32Array> dartToFloat32Array(Dart_NativeArguments
args, int idx, Dart_Handle& exception); |
| 877 static PassRefPtr<WTF::Float32Array> dartToFloat32ArrayWithNullCheck(Dart_Na
tiveArguments args, int idx, Dart_Handle& exception); |
| 878 |
| 879 static bool isUint8Array(Dart_Handle); |
| 880 static bool isUint8ClampedArray(Dart_Handle); |
| 881 |
| 882 template<class ElementType, class TransformType, Dart_Handle transform(Trans
formType)> |
| 883 static Dart_Handle vectorToDart(const Vector<ElementType>& vector) |
| 884 { |
| 885 Dart_Handle list = Dart_NewList(vector.size()); |
| 886 if (Dart_IsError(list)) |
| 887 return list; |
| 888 for (size_t i = 0; i < vector.size(); i++) { |
| 889 Dart_Handle result = Dart_ListSetAt(list, i, transform(vector[i])); |
| 890 if (Dart_IsError(result)) |
| 891 return result; |
| 892 } |
| 893 return list; |
| 894 } |
| 895 |
| 896 template<class ElementType, Dart_Handle transform(ElementType*)> |
| 897 static Dart_Handle vectorToDart(const HeapVector<Member<ElementType> >& vect
or) |
| 898 { |
| 899 Dart_Handle list = Dart_NewList(vector.size()); |
| 900 if (Dart_IsError(list)) |
| 901 return list; |
| 902 for (size_t i = 0; i < vector.size(); i++) { |
| 903 Dart_Handle result = Dart_ListSetAt(list, i, transform(vector[i])); |
| 904 if (Dart_IsError(result)) |
| 905 return result; |
| 906 } |
| 907 return list; |
| 908 } |
| 909 |
| 910 static v8::Local<v8::Context> currentV8Context(); |
| 911 |
| 912 |
| 913 enum { |
| 914 #if OS(ANDROID) |
| 915 PROP_VALUE_MAX_LEN = PROP_VALUE_MAX |
| 916 #else |
| 917 PROP_VALUE_MAX_LEN = 256 |
| 918 #endif |
| 919 }; |
| 920 |
| 921 static int getProp(const char* name, char* value, int valueLen); |
| 922 |
| 923 private: |
| 924 static DartStringAdapter dartToStringImpl(Dart_Handle, Dart_Handle& exceptio
n, bool autoDartScope = true); |
| 925 static DartStringPeer* toStringImpl(Dart_Handle, intptr_t charsize, intptr_t
strlength); |
| 926 }; |
| 927 |
| 928 class DartIsolateScope { |
| 929 public: |
| 930 explicit DartIsolateScope(Dart_Isolate isolate) |
| 931 { |
| 932 m_isolate = isolate; |
| 933 m_previousIsolate = Dart_CurrentIsolate(); |
| 934 if (m_previousIsolate != m_isolate) { |
| 935 if (m_previousIsolate) |
| 936 Dart_ExitIsolate(); |
| 937 Dart_EnterIsolate(m_isolate); |
| 938 } |
| 939 } |
| 940 |
| 941 ~DartIsolateScope() |
| 942 { |
| 943 ASSERT(Dart_CurrentIsolate() == m_isolate); |
| 944 if (m_previousIsolate != m_isolate) { |
| 945 Dart_ExitIsolate(); |
| 946 if (m_previousIsolate) |
| 947 Dart_EnterIsolate(m_previousIsolate); |
| 948 } |
| 949 } |
| 950 |
| 951 private: |
| 952 Dart_Isolate m_isolate; |
| 953 Dart_Isolate m_previousIsolate; |
| 954 }; |
| 955 |
| 956 class V8Scope { |
| 957 public: |
| 958 explicit V8Scope(DartDOMData*, v8::Handle<v8::Context>); |
| 959 V8Scope(DartDOMData*); |
| 960 ~V8Scope(); |
| 961 private: |
| 962 v8::Isolate* m_v8Isolate; |
| 963 DartDOMData* m_dartDOMData; |
| 964 v8::HandleScope m_handleScope; |
| 965 v8::Context::Scope m_contextScope; |
| 966 V8RecursionScope m_recursionScope; |
| 967 }; |
| 968 |
| 969 struct DartNativeEntry { |
| 970 Dart_NativeFunction nativeFunction; |
| 971 intptr_t argumentCount; |
| 972 const char* name; |
| 973 }; |
| 974 |
| 975 template<> |
| 976 struct DartUtilities::TypeConvertor<bool> { |
| 977 bool operator()(Dart_Handle object, Dart_Handle& exception) |
| 978 { |
| 979 return DartUtilities::dartToBool(object, exception); |
| 980 } |
| 981 }; |
| 982 |
| 983 |
| 984 template<> |
| 985 struct DartUtilities::TypeConvertor<float> { |
| 986 float operator()(Dart_Handle object, Dart_Handle& exception) |
| 987 { |
| 988 return static_cast<float>(DartUtilities::dartToDouble(object, exception)
); |
| 989 } |
| 990 }; |
| 991 |
| 992 |
| 993 template<> |
| 994 struct DartUtilities::TypeConvertor<double> { |
| 995 double operator()(Dart_Handle object, Dart_Handle& exception) |
| 996 { |
| 997 return DartUtilities::dartToDouble(object, exception); |
| 998 } |
| 999 }; |
| 1000 |
| 1001 |
| 1002 template<> |
| 1003 struct DartUtilities::TypeConvertor<int> { |
| 1004 int operator()(Dart_Handle object, Dart_Handle& exception) |
| 1005 { |
| 1006 return DartUtilities::dartToInt(object, exception); |
| 1007 } |
| 1008 }; |
| 1009 |
| 1010 |
| 1011 template<> |
| 1012 struct DartUtilities::TypeConvertor<unsigned> { |
| 1013 unsigned operator()(Dart_Handle object, Dart_Handle& exception) |
| 1014 { |
| 1015 return DartUtilities::dartToUnsigned(object, exception); |
| 1016 } |
| 1017 }; |
| 1018 |
| 1019 |
| 1020 template<> |
| 1021 struct DartUtilities::TypeConvertor<unsigned short> { |
| 1022 unsigned short operator()(Dart_Handle object, Dart_Handle& exception) |
| 1023 { |
| 1024 return static_cast<unsigned short>(DartUtilities::dartToUnsigned(object,
exception)); |
| 1025 } |
| 1026 }; |
| 1027 |
| 1028 |
| 1029 template<> |
| 1030 struct DartUtilities::TypeConvertor<unsigned long> { |
| 1031 unsigned long operator()(Dart_Handle object, Dart_Handle& exception) |
| 1032 { |
| 1033 return static_cast<unsigned long>(DartUtilities::dartToUnsignedLongLong(
object, exception)); |
| 1034 } |
| 1035 }; |
| 1036 |
| 1037 |
| 1038 template<> |
| 1039 struct DartUtilities::TypeConvertor<unsigned long long> { |
| 1040 unsigned long long operator()(Dart_Handle object, Dart_Handle& exception) |
| 1041 { |
| 1042 return DartUtilities::dartToUnsignedLongLong(object, exception); |
| 1043 } |
| 1044 }; |
| 1045 |
| 1046 |
| 1047 template<typename T> |
| 1048 struct DartUtilities::TypeConvertor<Nullable<T>> { |
| 1049 Nullable<T> operator()(Dart_Handle object, Dart_Handle& exception) |
| 1050 { |
| 1051 if (Dart_IsNull(object)) |
| 1052 return Nullable<T>(); |
| 1053 return Nullable<T>(TypeConvertor<T>()(object, exception)); |
| 1054 } |
| 1055 }; |
| 1056 |
| 1057 |
| 1058 template<> |
| 1059 struct DartUtilities::TypeConvertor<String> { |
| 1060 DartStringAdapter operator()(Dart_Handle object, Dart_Handle& exception) |
| 1061 { |
| 1062 return DartUtilities::dartToString(object, exception); |
| 1063 } |
| 1064 }; |
| 1065 |
| 1066 |
| 1067 template<> |
| 1068 struct DartUtilities::TypeConvertor<ScriptValue> { |
| 1069 ScriptValue operator()(Dart_Handle object, Dart_Handle& exception) |
| 1070 { |
| 1071 return ScriptValue(DartScriptValue::create(currentScriptState(), object)
); |
| 1072 } |
| 1073 }; |
| 1074 |
| 1075 template<> |
| 1076 struct DartUtilities::TypeConvertor<Dictionary> { |
| 1077 Dictionary operator()(Dart_Handle object, Dart_Handle& exception) |
| 1078 { |
| 1079 return DartUtilities::dartToDictionary(object, exception); |
| 1080 } |
| 1081 }; |
| 1082 |
| 1083 |
| 1084 template<> |
| 1085 struct DartUtilities::TypeConvertor<MediaStreamTrack> { |
| 1086 RawPtr<MediaStreamTrack> operator()(Dart_Handle object, Dart_Handle& excepti
on) |
| 1087 { |
| 1088 // FIXME: proper implementation. |
| 1089 return nullptr; |
| 1090 } |
| 1091 }; |
| 1092 |
| 1093 template<typename T> |
| 1094 Dart_Handle toDartNoInline(T* impl, DartDOMData* domData); |
| 1095 |
| 1096 template <typename T> |
| 1097 struct DartValueTraits { |
| 1098 static Dart_Handle toDartValue(const T& value, DartDOMData* domData) |
| 1099 { |
| 1100 if (!WTF::getPtr(value)) |
| 1101 return Dart_Null(); |
| 1102 return toDartNoInline(WTF::getPtr(value), domData); |
| 1103 } |
| 1104 }; |
| 1105 |
| 1106 template<> |
| 1107 struct DartValueTraits<String> { |
| 1108 static inline Dart_Handle toDartValue(const String& value, DartDOMData* domD
ata) |
| 1109 { |
| 1110 return DartUtilities::stringToDart(value); |
| 1111 } |
| 1112 }; |
| 1113 |
| 1114 template<> |
| 1115 struct DartValueTraits<AtomicString> { |
| 1116 static inline Dart_Handle toDartValue(const AtomicString& value, DartDOMData
* domData) |
| 1117 { |
| 1118 return DartUtilities::stringToDart(value); |
| 1119 } |
| 1120 }; |
| 1121 |
| 1122 template<size_t n> |
| 1123 struct DartValueTraits<char const[n]> { |
| 1124 static inline Dart_Handle toDartValue(char const (&value)[n], DartDOMData* d
omData) |
| 1125 { |
| 1126 return DartUtilities::stringToDart(value); |
| 1127 } |
| 1128 }; |
| 1129 |
| 1130 template<> |
| 1131 struct DartValueTraits<const char*> { |
| 1132 static inline Dart_Handle toDartValue(const char* const& value, DartDOMData*
domData) |
| 1133 { |
| 1134 // AtomicString? |
| 1135 return DartUtilities::stringToDart((String)value); |
| 1136 } |
| 1137 }; |
| 1138 |
| 1139 template<> |
| 1140 struct DartValueTraits<V8UndefinedType> { |
| 1141 static inline Dart_Handle toDartValue(const V8UndefinedType& value, DartDOMD
ata* domData) |
| 1142 { |
| 1143 return Dart_Null(); |
| 1144 } |
| 1145 }; |
| 1146 |
| 1147 template<> |
| 1148 struct DartValueTraits<V8NullType> { |
| 1149 static inline Dart_Handle toDartValue(const V8NullType& value, DartDOMData*
domData) |
| 1150 { |
| 1151 return Dart_Null(); |
| 1152 } |
| 1153 }; |
| 1154 |
| 1155 template<> |
| 1156 struct DartValueTraits<ScriptValue> { |
| 1157 static inline Dart_Handle toDartValue(const ScriptValue& value, DartDOMData*
domData) |
| 1158 { |
| 1159 return DartUtilities::scriptValueToDart(value); |
| 1160 } |
| 1161 }; |
| 1162 |
| 1163 template<> |
| 1164 struct DartValueTraits<Dart_Handle> { |
| 1165 static inline Dart_Handle toDartValue(const Dart_Handle& value, DartDOMData*
domData) |
| 1166 { |
| 1167 return value; |
| 1168 } |
| 1169 }; |
| 1170 |
| 1171 template<> |
| 1172 struct DartValueTraits<v8::Handle<v8::Value> > { |
| 1173 static inline Dart_Handle toDartValue(const v8::Handle<v8::Value>& value, Da
rtDOMData* domData) |
| 1174 { |
| 1175 Dart_Handle exception = 0; |
| 1176 Dart_Handle result = V8Converter::toDart(value, exception); |
| 1177 if (exception) |
| 1178 return exception; |
| 1179 return result; |
| 1180 } |
| 1181 }; |
| 1182 |
| 1183 template<> |
| 1184 struct DartValueTraits<bool> { |
| 1185 static inline Dart_Handle toDartValue(const bool& value, DartDOMData* domDat
a) |
| 1186 { |
| 1187 return DartUtilities::boolToDart(value); |
| 1188 } |
| 1189 }; |
| 1190 |
| 1191 template<> |
| 1192 struct DartValueTraits<int> { |
| 1193 static inline Dart_Handle toDartValue(const int& value, DartDOMData* domData
) |
| 1194 { |
| 1195 return DartUtilities::intToDart(value); |
| 1196 } |
| 1197 }; |
| 1198 |
| 1199 template<> |
| 1200 struct DartValueTraits<long> { |
| 1201 static inline Dart_Handle toDartValue(const long& value, DartDOMData* domDat
a) |
| 1202 { |
| 1203 return DartUtilities::intToDart(value); |
| 1204 } |
| 1205 }; |
| 1206 |
| 1207 template<> |
| 1208 struct DartValueTraits<unsigned> { |
| 1209 static inline Dart_Handle toDartValue(const unsigned& value, DartDOMData* do
mData) |
| 1210 { |
| 1211 return DartUtilities::unsignedToDart(value); |
| 1212 } |
| 1213 }; |
| 1214 |
| 1215 template<> |
| 1216 struct DartValueTraits<unsigned long> { |
| 1217 static inline Dart_Handle toDartValue(const unsigned long& value, DartDOMDat
a* domData) |
| 1218 { |
| 1219 return DartUtilities::unsignedToDart(value); |
| 1220 } |
| 1221 }; |
| 1222 |
| 1223 template<> |
| 1224 struct DartValueTraits<float> { |
| 1225 static inline Dart_Handle toDartValue(const float& value, DartDOMData* domDa
ta) |
| 1226 { |
| 1227 return DartUtilities::doubleToDart(value); |
| 1228 } |
| 1229 }; |
| 1230 |
| 1231 template<> |
| 1232 struct DartValueTraits<double> { |
| 1233 static inline Dart_Handle toDartValue(const double& value, DartDOMData* domD
ata) |
| 1234 { |
| 1235 return DartUtilities::doubleToDart(value); |
| 1236 } |
| 1237 }; |
| 1238 |
| 1239 template<> |
| 1240 struct DartValueTraits<PassRefPtr<ArrayBuffer> > { |
| 1241 static inline Dart_Handle toDartValue(const PassRefPtr<ArrayBuffer>& value,
DartDOMData* domData) |
| 1242 { |
| 1243 return DartUtilities::arrayBufferToDart(value); |
| 1244 } |
| 1245 }; |
| 1246 |
| 1247 template<typename T, size_t inlineCapacity> |
| 1248 Dart_Handle dartArray(const Vector<T, inlineCapacity>& vector, DartDOMData* domD
ata) |
| 1249 { |
| 1250 Dart_Handle result = Dart_NewList(vector.size()); |
| 1251 int index = 0; |
| 1252 typename Vector<T, inlineCapacity>::const_iterator end = vector.end(); |
| 1253 typedef DartValueTraits<T> TraitsType; |
| 1254 for (typename Vector<T, inlineCapacity>::const_iterator iter = vector.begin(
); iter != end; ++iter) |
| 1255 Dart_ListSetAt(result, index, TraitsType::toDartValue(*iter, domData)); |
| 1256 return result; |
| 1257 } |
| 1258 |
| 1259 template <typename T, size_t inlineCapacity, typename Allocator> |
| 1260 struct DartValueTraits<WTF::Vector<T, inlineCapacity, Allocator> > { |
| 1261 static inline Dart_Handle toDartValue(const Vector<T, inlineCapacity, Alloca
tor>& value, DartDOMData* domData) |
| 1262 { |
| 1263 return dartArray(value, domData); |
| 1264 } |
| 1265 }; |
| 1266 |
| 1267 // Errors? |
| 1268 template<typename T, size_t inlineCapacity> |
| 1269 Dart_Handle dartArray(const HeapVector<T, inlineCapacity>& vector, DartDOMData*
domData) |
| 1270 { |
| 1271 Dart_Handle result = Dart_NewList(vector.size()); |
| 1272 int index = 0; |
| 1273 typename HeapVector<T, inlineCapacity>::const_iterator end = vector.end(); |
| 1274 typedef DartValueTraits<T> TraitsType; |
| 1275 for (typename HeapVector<T, inlineCapacity>::const_iterator iter = vector.be
gin(); iter != end; ++iter) |
| 1276 Dart_ListSetAt(result, index, TraitsType::toDartValue(*iter, domData)); |
| 1277 return result; |
| 1278 } |
| 1279 |
| 1280 template<typename T, size_t inlineCapacity> |
| 1281 struct DartValueTraits<HeapVector<T, inlineCapacity> > { |
| 1282 static inline Dart_Handle toDartValue(const HeapVector<T, inlineCapacity>& v
alue, DartDOMData* domData) |
| 1283 { |
| 1284 return dartArray(value, domData); |
| 1285 } |
| 1286 }; |
| 1287 |
| 1288 |
| 1289 #define DART_UNIMPLEMENTED_EXCEPTION() DartUtilities::notImplementedException(__
FILE__, __LINE__) |
| 1290 #define DART_UNIMPLEMENTED() Dart_ThrowException(DART_UNIMPLEMENTED_EXCEPTION())
; |
| 1291 |
| 1292 #if defined(DART_TIMER_SCOPE) |
| 1293 #define DART_START_TIMER() \ |
| 1294 double timerStop, timerStart = currentTimeMS(); |
| 1295 #define DART_RECORD_TIMER(msg) \ |
| 1296 timerStop = currentTimeMS(); \ |
| 1297 fprintf(stdout, "%s %.3f ms\n", msg, (timerStop - timerStart)); |
| 1298 #else |
| 1299 #define DART_START_TIMER() |
| 1300 #define DART_RECORD_TIMER(msg) |
| 1301 #endif |
| 1302 |
| 1303 } // namespace blink |
| 1304 |
| 1305 #endif // DartUtilities_h |
| OLD | NEW |