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/core/v8/Dictionary.h" |
| 39 #include "bindings/core/dart/DartDOMData.h" |
| 40 #include "bindings/core/dart/DartScriptValue.h" |
| 41 #include "bindings/core/dart/DartStringCache.h" |
| 42 #include "bindings/core/v8/SerializedScriptValue.h" |
| 43 #include "bindings/core/v8/V8RecursionScope.h" |
| 44 #include "bindings/core/v8/V8ScriptState.h" |
| 45 #include "bindings/dart/DartWebkitClassIds.h" |
| 46 #include "core/dom/DOMStringList.h" |
| 47 #include "core/dom/MessagePort.h" |
| 48 #include "core/inspector/ScriptCallFrame.h" |
| 49 #include "modules/mediastream/MediaStreamTrack.h" |
| 50 #include "platform/heap/Heap.h" |
| 51 |
| 52 #include "wtf/ArrayBufferView.h" |
| 53 #include "wtf/Float32Array.h" |
| 54 #include "wtf/HashMap.h" |
| 55 #include "wtf/ListHashSet.h" |
| 56 #include "wtf/Uint8Array.h" |
| 57 #include "wtf/text/WTFString.h" |
| 58 #include <dart_api.h> |
| 59 #include <dart_tools_api.h> |
| 60 |
| 61 namespace WTF { |
| 62 class ArrayBuffer; |
| 63 class Uint8ClampedArray; |
| 64 } |
| 65 |
| 66 namespace blink { |
| 67 |
| 68 class Dictionary; |
| 69 class Frame; |
| 70 class SQLValue; |
| 71 class ScriptArguments; |
| 72 class ScriptCallStack; |
| 73 class ExecutionContext; |
| 74 class SerializedScriptValue; |
| 75 |
| 76 class DartStringPeer { |
| 77 public: |
| 78 explicit DartStringPeer(PassRefPtr<StringImpl> stringImpl) |
| 79 : m_plainString(stringImpl), m_atomicString(), m_atomicCached(false) |
| 80 { |
| 81 } |
| 82 |
| 83 explicit DartStringPeer(StringImpl* stringImpl) |
| 84 : m_plainString(stringImpl), m_atomicString(), m_atomicCached(false) |
| 85 { |
| 86 } |
| 87 |
| 88 const String& toString() const { return m_plainString; } |
| 89 const AtomicString& toAtomicString() |
| 90 { |
| 91 if (m_atomicCached) |
| 92 return m_atomicString; |
| 93 return toAtomicStringSlow(); |
| 94 } |
| 95 const AtomicString& toAtomicStringSlow() |
| 96 { |
| 97 if (!m_plainString.isNull()) { |
| 98 m_atomicString = AtomicString(m_plainString); |
| 99 ASSERT(!m_atomicString.isNull()); |
| 100 } |
| 101 m_atomicCached = true; |
| 102 return m_atomicString; |
| 103 } |
| 104 static DartStringPeer* nullString(); |
| 105 static DartStringPeer* emptyString(); |
| 106 |
| 107 private: |
| 108 DartStringPeer() : m_plainString(), m_atomicString() |
| 109 { |
| 110 } |
| 111 |
| 112 String m_plainString; |
| 113 AtomicString m_atomicString; |
| 114 bool m_atomicCached; |
| 115 }; |
| 116 |
| 117 class DartStringAdapter { |
| 118 public: |
| 119 explicit DartStringAdapter(DartStringPeer* stringPeer) : m_stringPeer(string
Peer) |
| 120 { |
| 121 ASSERT(stringPeer); |
| 122 } |
| 123 |
| 124 operator String() const { return m_stringPeer->toString(); } |
| 125 operator AtomicString() const { return m_stringPeer->toAtomicString(); } |
| 126 |
| 127 private: |
| 128 DartStringPeer* m_stringPeer; |
| 129 }; |
| 130 |
| 131 class DartApiScope { |
| 132 public: |
| 133 DartApiScope() { Dart_EnterScope(); } |
| 134 ~DartApiScope() { Dart_ExitScope(); } |
| 135 }; |
| 136 |
| 137 class DartUtilities { |
| 138 public: |
| 139 static Dart_Handle stringImplToDartString(StringImpl*); |
| 140 static Dart_Handle stringToDartString(const String&); |
| 141 static Dart_Handle stringToDartString(const AtomicString&); |
| 142 static Dart_Handle safeStringImplToDartString(StringImpl*); |
| 143 static Dart_Handle safeStringToDartString(const String&); |
| 144 static Dart_Handle safeStringToDartString(const AtomicString&); |
| 145 |
| 146 static Dart_Handle errorToException(Dart_Handle error) |
| 147 { |
| 148 ASSERT(Dart_IsError(error)); |
| 149 if (Dart_ErrorHasException(error)) |
| 150 return Dart_ErrorGetException(error); |
| 151 return Dart_NewStringFromCString(Dart_GetError(error)); |
| 152 } |
| 153 |
| 154 static bool checkResult(Dart_Handle result, Dart_Handle& exception) |
| 155 { |
| 156 if (!Dart_IsError(result)) |
| 157 return true; |
| 158 |
| 159 exception = errorToException(result); |
| 160 return false; |
| 161 } |
| 162 |
| 163 static void extractListElements(Dart_Handle list, Dart_Handle& exception, Ve
ctor<Dart_Handle>& elements); |
| 164 static Dart_Handle toList(const Vector<Dart_Handle>& elements, Dart_Handle&
exception); |
| 165 |
| 166 static void extractMapElements(Dart_Handle map, Dart_Handle& exception, Hash
Map<String, Dart_Handle>& elements); |
| 167 |
| 168 static int64_t toInteger(Dart_Handle, Dart_Handle& exception); |
| 169 static String toString(Dart_Handle); |
| 170 static void toMessagePortArray(Dart_Handle, MessagePortArray&, ArrayBufferAr
ray&, Dart_Handle& exception); |
| 171 static PassRefPtr<SerializedScriptValue> toSerializedScriptValue(Dart_Handle
, MessagePortArray*, ArrayBufferArray*, Dart_Handle& exception); |
| 172 static SQLValue toSQLValue(Dart_Handle, Dart_Handle& exception); |
| 173 template <class T, class Convertor> |
| 174 static void toVector(Convertor convertor, Dart_Handle object, Vector<T>& res
ult, Dart_Handle& exception) |
| 175 { |
| 176 ASSERT(Dart_IsList(object)); |
| 177 ASSERT(result.isEmpty()); |
| 178 |
| 179 Vector<Dart_Handle> handles; |
| 180 DartUtilities::extractListElements(object, exception, handles); |
| 181 if (exception) |
| 182 return; |
| 183 |
| 184 for (unsigned i = 0; i < handles.size(); ++i) { |
| 185 result.append(convertor(handles[i], exception)); |
| 186 if (exception) |
| 187 return; |
| 188 } |
| 189 } |
| 190 template <class T, class Convertor> |
| 191 static void toVector(Convertor convertor, Dart_Handle object, HeapVector<Mem
ber<T> >& result, Dart_Handle& exception) |
| 192 { |
| 193 ASSERT(Dart_IsList(object)); |
| 194 ASSERT(result.isEmpty()); |
| 195 |
| 196 Vector<Dart_Handle> handles; |
| 197 DartUtilities::extractListElements(object, exception, handles); |
| 198 if (exception) |
| 199 return; |
| 200 |
| 201 for (unsigned i = 0; i < handles.size(); ++i) { |
| 202 result.append(convertor(handles[i], exception)); |
| 203 if (exception) |
| 204 return; |
| 205 } |
| 206 } |
| 207 |
| 208 template <class T> |
| 209 struct TypeConvertor { }; |
| 210 |
| 211 template <class T> |
| 212 static void toNativeVector(Dart_Handle object, Vector<T>& result, Dart_Handl
e& exception) |
| 213 { |
| 214 toVector(TypeConvertor<T>(), object, result, exception); |
| 215 } |
| 216 template <class T> |
| 217 static void toNativeVector(Dart_NativeArguments args, int idx, Vector<T>& re
sult, Dart_Handle& exception) |
| 218 { |
| 219 Dart_Handle object = Dart_GetNativeArgument(args, idx); |
| 220 toVector(TypeConvertor<T>(), object, result, exception); |
| 221 } |
| 222 |
| 223 template <class T> |
| 224 static void toNativeVector(Dart_Handle object, HeapVector<Member<T> >& resul
t, Dart_Handle& exception) |
| 225 { |
| 226 toVector(TypeConvertor<T>(), object, result, exception); |
| 227 } |
| 228 template <class T> |
| 229 static void toNativeVector(Dart_NativeArguments args, int idx, HeapVector<Me
mber<T> >& result, Dart_Handle& exception) |
| 230 { |
| 231 Dart_Handle object = Dart_GetNativeArgument(args, idx); |
| 232 toVector(TypeConvertor<T>(), object, result, exception); |
| 233 } |
| 234 |
| 235 static Dart_Handle listHashSetToDartList(ListHashSet<String> set, Dart_Handl
e& exception) |
| 236 { |
| 237 Dart_Handle result = Dart_NewList(set.size()); |
| 238 if (!DartUtilities::checkResult(result, exception)) |
| 239 return Dart_Null(); |
| 240 |
| 241 ListHashSet<String>::const_iterator end = set.end(); |
| 242 int index = 0; |
| 243 for (ListHashSet<String>::const_iterator it = set.begin(); it != end; ++
it, ++index) |
| 244 Dart_ListSetAt(result, index, DartUtilities::stringToDartString(*it)
); |
| 245 return result; |
| 246 } |
| 247 |
| 248 static Dictionary dartToDictionary(Dart_Handle, Dart_Handle& exception); |
| 249 static Dictionary dartToDictionaryWithNullCheck(Dart_Handle, Dart_Handle& ex
ception); |
| 250 static Dictionary dartToDictionary(Dart_NativeArguments args, int idx, Dart_
Handle& exception); |
| 251 static Dictionary dartToDictionaryWithNullCheck(Dart_NativeArguments args, i
nt idx, Dart_Handle& exception); |
| 252 |
| 253 static LocalDOMWindow* domWindowForCurrentIsolate(); |
| 254 static LocalDOMWindow* enteredDomWindowForCurrentIsolate() |
| 255 { |
| 256 // FIXME: Add check to enforce this is the right window. |
| 257 return domWindowForCurrentIsolate(); |
| 258 } |
| 259 |
| 260 static LocalDOMWindow* callingDomWindowForCurrentIsolate() |
| 261 { |
| 262 // FIXME: Add check to enforce this is the right window. |
| 263 return domWindowForCurrentIsolate(); |
| 264 } |
| 265 |
| 266 static V8ScriptState* v8ScriptStateForCurrentIsolate(); |
| 267 |
| 268 static ExecutionContext* scriptExecutionContext(); |
| 269 // FIXMEDART: Should have a variant that takes a Dart_NativeArguments to avo
id TLS. |
| 270 static DartScriptState* currentScriptState(); |
| 271 |
| 272 static bool processingUserGesture(); |
| 273 |
| 274 static PassRefPtr<ScriptArguments> createScriptArguments(Dart_Handle argumen
t, Dart_Handle& exception); |
| 275 |
| 276 static PassRefPtr<ScriptCallStack> createScriptCallStack(); |
| 277 static ScriptCallFrame getTopFrame(Dart_StackTrace, Dart_Handle& exception); |
| 278 static ScriptCallFrame toScriptCallFrame(Dart_ActivationFrame, Dart_Handle&
exception); |
| 279 |
| 280 static const uint8_t* vmIsolateSnapshot(); |
| 281 static const uint8_t* isolateSnapshot(); |
| 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 |
| 788 static PassRefPtr<SerializedScriptValue> dartToSerializedScriptValue(Dart_Ha
ndle, Dart_Handle& exception); |
| 789 static Dart_Handle serializedScriptValueToDart(PassRefPtr<SerializedScriptVa
lue>); |
| 790 |
| 791 static Dart_Handle dateToDart(double); |
| 792 static double dartToDate(Dart_Handle, Dart_Handle&); |
| 793 static double dartToDate(Dart_NativeArguments args, int idx, Dart_Handle&); |
| 794 static bool isDateTime(DartDOMData*, Dart_Handle); |
| 795 static bool isTypeSubclassOf(Dart_Handle type, Dart_Handle library, const ch
ar* typeName); |
| 796 static Dart_Handle getAndValidateNativeType(Dart_Handle type, const String&
tagName); |
| 797 static bool objectIsType(Dart_Handle, Dart_Handle type); |
| 798 |
| 799 static bool isFunction(DartDOMData*, Dart_Handle); |
| 800 |
| 801 static Dart_Handle getDartType(const char*, const char*); |
| 802 static Dart_Handle getDartHtmlType(const char* typeName); |
| 803 |
| 804 static bool isNode(Dart_Handle); |
| 805 static Node* dartNodeToNativeNode(void*); |
| 806 |
| 807 static bool isMessagePort(Dart_Handle); |
| 808 static MessagePort* dartMessagePortToNativeMessagePort(void*); |
| 809 |
| 810 static Dart_Handle arrayBufferToDart(WTF::ArrayBuffer*); |
| 811 static Dart_Handle arrayBufferToDart(PassRefPtr<WTF::ArrayBuffer> value) |
| 812 { |
| 813 return arrayBufferToDart(value.get()); |
| 814 } |
| 815 |
| 816 static Dart_Handle arrayBufferViewToDart(WTF::ArrayBufferView*); |
| 817 |
| 818 // FIXME: For typed data views in the VM heap, this currently creates |
| 819 // a new array buffer object with a copy of the data and loses the |
| 820 // connection to the dart object. |
| 821 static Dart_Handle arrayBufferViewToDart(PassRefPtr<WTF::ArrayBufferView> va
lue) |
| 822 { |
| 823 return arrayBufferViewToDart(value.get()); |
| 824 } |
| 825 |
| 826 static PassRefPtr<WTF::ArrayBuffer> dartToArrayBuffer(Dart_Handle array, Dar
t_Handle& exception); |
| 827 static PassRefPtr<WTF::ArrayBuffer> dartToArrayBufferWithNullCheck(Dart_Hand
le array, Dart_Handle& exception) |
| 828 { |
| 829 return Dart_IsNull(array) ? nullptr : dartToArrayBuffer(array, exception
); |
| 830 } |
| 831 static PassRefPtr<WTF::ArrayBuffer> dartToArrayBuffer(Dart_NativeArguments a
rgs, int index, Dart_Handle& exception) |
| 832 { |
| 833 Dart_Handle object = Dart_GetNativeArgument(args, index); |
| 834 return dartToArrayBuffer(object, exception); |
| 835 } |
| 836 static PassRefPtr<WTF::ArrayBuffer> dartToArrayBufferWithNullCheck(Dart_Nati
veArguments args, int idx, Dart_Handle& excp) |
| 837 { |
| 838 Dart_Handle object = Dart_GetNativeArgument(args, idx); |
| 839 return dartToArrayBufferWithNullCheck(object, excp); |
| 840 } |
| 841 |
| 842 // FIXME: For typed data objects in the VM heap, this currently creates |
| 843 // a new array buffer object with a copy of the data and loses the |
| 844 // connection to the dart object. |
| 845 static PassRefPtr<WTF::ArrayBuffer> dartToExternalizedArrayBuffer(Dart_Handl
e, Dart_Handle&); |
| 846 |
| 847 static PassRefPtr<WTF::ArrayBufferView> dartToArrayBufferView(Dart_Handle, D
art_Handle&); |
| 848 static PassRefPtr<WTF::ArrayBufferView> dartToArrayBufferViewWithNullCheck(D
art_Handle array, Dart_Handle& exception) |
| 849 { |
| 850 return Dart_IsNull(array) ? nullptr : dartToArrayBufferView(array, excep
tion); |
| 851 } |
| 852 static PassRefPtr<WTF::ArrayBufferView> dartToArrayBufferView(Dart_NativeArg
uments args, int idx, Dart_Handle& exception) |
| 853 { |
| 854 Dart_Handle object = Dart_GetNativeArgument(args, idx); |
| 855 return dartToArrayBufferView(object, exception); |
| 856 } |
| 857 static PassRefPtr<WTF::ArrayBufferView> dartToArrayBufferViewWithNullCheck(D
art_NativeArguments args, int idx, Dart_Handle& exception) |
| 858 { |
| 859 Dart_Handle object = Dart_GetNativeArgument(args, idx); |
| 860 return dartToArrayBufferViewWithNullCheck(object, exception); |
| 861 } |
| 862 |
| 863 static PassRefPtr<WTF::ArrayBufferView> dartToExternalizedArrayBufferView(Da
rt_Handle, Dart_Handle&); |
| 864 |
| 865 static PassRefPtr<WTF::Int8Array> dartToInt8Array(Dart_Handle, Dart_Handle&)
; |
| 866 static PassRefPtr<WTF::Int8Array> dartToInt8ArrayWithNullCheck(Dart_Handle,
Dart_Handle&); |
| 867 static PassRefPtr<WTF::Int8Array> dartToInt8Array(Dart_NativeArguments args,
int idx, Dart_Handle& exception); |
| 868 static PassRefPtr<WTF::Int8Array> dartToInt8ArrayWithNullCheck(Dart_NativeAr
guments args, int idx, Dart_Handle& exception); |
| 869 static PassRefPtr<WTF::Int32Array> dartToInt32Array(Dart_Handle, Dart_Handle
&); |
| 870 static PassRefPtr<WTF::Int32Array> dartToInt32ArrayWithNullCheck(Dart_Handle
, Dart_Handle&); |
| 871 static PassRefPtr<WTF::Int32Array> dartToInt32Array(Dart_NativeArguments arg
s, int idx, Dart_Handle& exception); |
| 872 static PassRefPtr<WTF::Int32Array> dartToInt32ArrayWithNullCheck(Dart_Native
Arguments args, int idx, Dart_Handle& exception); |
| 873 static PassRefPtr<WTF::Uint8Array> dartToUint8Array(Dart_Handle, Dart_Handle
&); |
| 874 static PassRefPtr<WTF::Uint8Array> dartToUint8ArrayWithNullCheck(Dart_Handle
, Dart_Handle&); |
| 875 static PassRefPtr<WTF::Uint8Array> dartToUint8Array(Dart_NativeArguments arg
s, int idx, Dart_Handle& exception); |
| 876 static PassRefPtr<WTF::Uint8Array> dartToUint8ArrayWithNullCheck(Dart_Native
Arguments args, int idx, Dart_Handle& exception); |
| 877 static PassRefPtr<WTF::Uint8ClampedArray> dartToUint8ClampedArray(Dart_Handl
e, Dart_Handle&); |
| 878 static PassRefPtr<WTF::Uint8ClampedArray> dartToUint8ClampedArrayWithNullChe
ck(Dart_Handle, Dart_Handle&); |
| 879 static PassRefPtr<WTF::Uint8ClampedArray> dartToUint8ClampedArray(Dart_Nativ
eArguments args, int idx, Dart_Handle& exception); |
| 880 static PassRefPtr<WTF::Uint8ClampedArray> dartToUint8ClampedArrayWithNullChe
ck(Dart_NativeArguments args, int idx, Dart_Handle& exception); |
| 881 static PassRefPtr<WTF::Float32Array> dartToFloat32Array(Dart_Handle, Dart_Ha
ndle&); |
| 882 static PassRefPtr<WTF::Float32Array> dartToFloat32ArrayWithNullCheck(Dart_Ha
ndle, Dart_Handle&); |
| 883 static PassRefPtr<WTF::Float32Array> dartToFloat32Array(Dart_NativeArguments
args, int idx, Dart_Handle& exception); |
| 884 static PassRefPtr<WTF::Float32Array> dartToFloat32ArrayWithNullCheck(Dart_Na
tiveArguments args, int idx, Dart_Handle& exception); |
| 885 |
| 886 static bool isUint8Array(Dart_Handle); |
| 887 static bool isUint8ClampedArray(Dart_Handle); |
| 888 |
| 889 template<class ElementType, class TransformType, Dart_Handle transform(Trans
formType)> |
| 890 static Dart_Handle vectorToDart(const Vector<ElementType>& vector) |
| 891 { |
| 892 Dart_Handle list = Dart_NewList(vector.size()); |
| 893 if (Dart_IsError(list)) |
| 894 return list; |
| 895 for (size_t i = 0; i < vector.size(); i++) { |
| 896 Dart_Handle result = Dart_ListSetAt(list, i, transform(vector[i])); |
| 897 if (Dart_IsError(result)) |
| 898 return result; |
| 899 } |
| 900 return list; |
| 901 } |
| 902 |
| 903 template<class ElementType, Dart_Handle transform(ElementType*)> |
| 904 static Dart_Handle vectorToDart(const HeapVector<Member<ElementType> >& vect
or) |
| 905 { |
| 906 Dart_Handle list = Dart_NewList(vector.size()); |
| 907 if (Dart_IsError(list)) |
| 908 return list; |
| 909 for (size_t i = 0; i < vector.size(); i++) { |
| 910 Dart_Handle result = Dart_ListSetAt(list, i, transform(vector[i])); |
| 911 if (Dart_IsError(result)) |
| 912 return result; |
| 913 } |
| 914 return list; |
| 915 } |
| 916 |
| 917 static v8::Local<v8::Context> currentV8Context(); |
| 918 |
| 919 |
| 920 enum { |
| 921 #if OS(ANDROID) |
| 922 PROP_VALUE_MAX_LEN = PROP_VALUE_MAX |
| 923 #else |
| 924 PROP_VALUE_MAX_LEN = 256 |
| 925 #endif |
| 926 }; |
| 927 |
| 928 enum { |
| 929 VmIsolateSnapshotId = 0, |
| 930 IsolateSnapshotId = 1, |
| 931 }; |
| 932 |
| 933 static int getProp(const char* name, char* value, int valueLen); |
| 934 |
| 935 private: |
| 936 static DartStringAdapter dartToStringImpl(Dart_Handle, Dart_Handle& exceptio
n, bool autoDartScope = true); |
| 937 static DartStringPeer* toStringImpl(Dart_Handle, intptr_t charsize, intptr_t
strlength); |
| 938 }; |
| 939 |
| 940 class DartIsolateScope { |
| 941 public: |
| 942 explicit DartIsolateScope(Dart_Isolate isolate) |
| 943 { |
| 944 m_isolate = isolate; |
| 945 m_previousIsolate = Dart_CurrentIsolate(); |
| 946 if (m_previousIsolate != m_isolate) { |
| 947 if (m_previousIsolate) |
| 948 Dart_ExitIsolate(); |
| 949 Dart_EnterIsolate(m_isolate); |
| 950 } |
| 951 } |
| 952 |
| 953 ~DartIsolateScope() |
| 954 { |
| 955 ASSERT(Dart_CurrentIsolate() == m_isolate); |
| 956 if (m_previousIsolate != m_isolate) { |
| 957 Dart_ExitIsolate(); |
| 958 if (m_previousIsolate) |
| 959 Dart_EnterIsolate(m_previousIsolate); |
| 960 } |
| 961 } |
| 962 |
| 963 private: |
| 964 Dart_Isolate m_isolate; |
| 965 Dart_Isolate m_previousIsolate; |
| 966 }; |
| 967 |
| 968 class V8Scope { |
| 969 public: |
| 970 explicit V8Scope(DartDOMData*, v8::Handle<v8::Context>); |
| 971 V8Scope(DartDOMData*); |
| 972 ~V8Scope(); |
| 973 private: |
| 974 v8::Isolate* m_v8Isolate; |
| 975 DartDOMData* m_dartDOMData; |
| 976 v8::HandleScope m_handleScope; |
| 977 v8::Context::Scope m_contextScope; |
| 978 V8RecursionScope m_recursionScope; |
| 979 }; |
| 980 |
| 981 struct DartNativeEntry { |
| 982 Dart_NativeFunction nativeFunction; |
| 983 intptr_t argumentCount; |
| 984 const char* name; |
| 985 }; |
| 986 |
| 987 template<> |
| 988 struct DartUtilities::TypeConvertor<bool> { |
| 989 bool operator()(Dart_Handle object, Dart_Handle& exception) |
| 990 { |
| 991 return DartUtilities::dartToBool(object, exception); |
| 992 } |
| 993 }; |
| 994 |
| 995 |
| 996 template<> |
| 997 struct DartUtilities::TypeConvertor<float> { |
| 998 float operator()(Dart_Handle object, Dart_Handle& exception) |
| 999 { |
| 1000 return static_cast<float>(DartUtilities::dartToDouble(object, exception)
); |
| 1001 } |
| 1002 }; |
| 1003 |
| 1004 |
| 1005 template<> |
| 1006 struct DartUtilities::TypeConvertor<double> { |
| 1007 double operator()(Dart_Handle object, Dart_Handle& exception) |
| 1008 { |
| 1009 return DartUtilities::dartToDouble(object, exception); |
| 1010 } |
| 1011 }; |
| 1012 |
| 1013 |
| 1014 template<> |
| 1015 struct DartUtilities::TypeConvertor<int> { |
| 1016 int operator()(Dart_Handle object, Dart_Handle& exception) |
| 1017 { |
| 1018 return DartUtilities::dartToInt(object, exception); |
| 1019 } |
| 1020 }; |
| 1021 |
| 1022 |
| 1023 template<> |
| 1024 struct DartUtilities::TypeConvertor<unsigned> { |
| 1025 unsigned operator()(Dart_Handle object, Dart_Handle& exception) |
| 1026 { |
| 1027 return DartUtilities::dartToUnsigned(object, exception); |
| 1028 } |
| 1029 }; |
| 1030 |
| 1031 |
| 1032 template<> |
| 1033 struct DartUtilities::TypeConvertor<unsigned short> { |
| 1034 unsigned short operator()(Dart_Handle object, Dart_Handle& exception) |
| 1035 { |
| 1036 return static_cast<unsigned short>(DartUtilities::dartToUnsigned(object,
exception)); |
| 1037 } |
| 1038 }; |
| 1039 |
| 1040 |
| 1041 template<> |
| 1042 struct DartUtilities::TypeConvertor<unsigned long> { |
| 1043 unsigned long operator()(Dart_Handle object, Dart_Handle& exception) |
| 1044 { |
| 1045 return static_cast<unsigned long>(DartUtilities::dartToUnsignedLongLong(
object, exception)); |
| 1046 } |
| 1047 }; |
| 1048 |
| 1049 |
| 1050 template<> |
| 1051 struct DartUtilities::TypeConvertor<unsigned long long> { |
| 1052 unsigned long long operator()(Dart_Handle object, Dart_Handle& exception) |
| 1053 { |
| 1054 return DartUtilities::dartToUnsignedLongLong(object, exception); |
| 1055 } |
| 1056 }; |
| 1057 |
| 1058 |
| 1059 template<typename T> |
| 1060 struct DartUtilities::TypeConvertor<Nullable<T>> { |
| 1061 Nullable<T> operator()(Dart_Handle object, Dart_Handle& exception) |
| 1062 { |
| 1063 if (Dart_IsNull(object)) |
| 1064 return Nullable<T>(); |
| 1065 return Nullable<T>(TypeConvertor<T>()(object, exception)); |
| 1066 } |
| 1067 }; |
| 1068 |
| 1069 |
| 1070 template<> |
| 1071 struct DartUtilities::TypeConvertor<String> { |
| 1072 DartStringAdapter operator()(Dart_Handle object, Dart_Handle& exception) |
| 1073 { |
| 1074 return DartUtilities::dartToString(object, exception); |
| 1075 } |
| 1076 }; |
| 1077 |
| 1078 |
| 1079 template<> |
| 1080 struct DartUtilities::TypeConvertor<ScriptValue> { |
| 1081 ScriptValue operator()(Dart_Handle object, Dart_Handle& exception) |
| 1082 { |
| 1083 return ScriptValue(DartScriptValue::create(currentScriptState(), object)
); |
| 1084 } |
| 1085 }; |
| 1086 |
| 1087 template<> |
| 1088 struct DartUtilities::TypeConvertor<Dictionary> { |
| 1089 Dictionary operator()(Dart_Handle object, Dart_Handle& exception) |
| 1090 { |
| 1091 return DartUtilities::dartToDictionary(object, exception); |
| 1092 } |
| 1093 }; |
| 1094 |
| 1095 |
| 1096 template<> |
| 1097 struct DartUtilities::TypeConvertor<MediaStreamTrack> { |
| 1098 RawPtr<MediaStreamTrack> operator()(Dart_Handle object, Dart_Handle& excepti
on) |
| 1099 { |
| 1100 // FIXME: proper implementation. |
| 1101 return nullptr; |
| 1102 } |
| 1103 }; |
| 1104 |
| 1105 |
| 1106 template <typename T> |
| 1107 struct DartValueTraits { |
| 1108 static Dart_Handle toDartValue(const T& value, DartDOMData* domData) |
| 1109 { |
| 1110 return Dart_Null(); |
| 1111 } |
| 1112 }; |
| 1113 |
| 1114 template<> |
| 1115 struct DartValueTraits<String> { |
| 1116 static inline Dart_Handle toDartValue(const String& value, DartDOMData* domD
ata) |
| 1117 { |
| 1118 return DartUtilities::stringToDart(value); |
| 1119 } |
| 1120 }; |
| 1121 |
| 1122 template<> |
| 1123 struct DartValueTraits<AtomicString> { |
| 1124 static inline Dart_Handle toDartValue(const AtomicString& value, DartDOMData
* domData) |
| 1125 { |
| 1126 return DartUtilities::stringToDart(value); |
| 1127 } |
| 1128 }; |
| 1129 |
| 1130 template<size_t n> |
| 1131 struct DartValueTraits<char const[n]> { |
| 1132 static inline Dart_Handle toDartValue(char const (&value)[n], DartDOMData* d
omData) |
| 1133 { |
| 1134 return DartUtilities::stringToDart(value); |
| 1135 } |
| 1136 }; |
| 1137 |
| 1138 template<> |
| 1139 struct DartValueTraits<const char*> { |
| 1140 static inline Dart_Handle toDartValue(const char* const& value, DartDOMData*
domData) |
| 1141 { |
| 1142 // AtomicString? |
| 1143 return DartUtilities::stringToDart((String)value); |
| 1144 } |
| 1145 }; |
| 1146 |
| 1147 template<> |
| 1148 struct DartValueTraits<V8UndefinedType> { |
| 1149 static inline Dart_Handle toDartValue(const V8UndefinedType& value, DartDOMD
ata* domData) |
| 1150 { |
| 1151 return Dart_Null(); |
| 1152 } |
| 1153 }; |
| 1154 |
| 1155 template<> |
| 1156 struct DartValueTraits<V8NullType> { |
| 1157 static inline Dart_Handle toDartValue(const V8NullType& value, DartDOMData*
domData) |
| 1158 { |
| 1159 return Dart_Null(); |
| 1160 } |
| 1161 }; |
| 1162 |
| 1163 template<> |
| 1164 struct DartValueTraits<V8BoolType> { |
| 1165 static inline Dart_Handle toDartValue(const V8BoolType& value, DartDOMData*
domData) |
| 1166 { |
| 1167 return value.value() ? Dart_True() : Dart_False(); |
| 1168 } |
| 1169 }; |
| 1170 |
| 1171 |
| 1172 template<> |
| 1173 struct DartValueTraits<Dart_Handle> { |
| 1174 static inline Dart_Handle toDartValue(const Dart_Handle& value, DartDOMData*
domData) |
| 1175 { |
| 1176 return value; |
| 1177 } |
| 1178 }; |
| 1179 |
| 1180 template<> |
| 1181 struct DartValueTraits<v8::Handle<v8::Value> > { |
| 1182 static inline Dart_Handle toDartValue(const v8::Handle<v8::Value>& value, Da
rtDOMData* domData) |
| 1183 { |
| 1184 Dart_Handle exception = 0; |
| 1185 Dart_Handle result = V8Converter::toDart(value, exception); |
| 1186 if (exception) |
| 1187 return exception; |
| 1188 return result; |
| 1189 } |
| 1190 }; |
| 1191 |
| 1192 template<> |
| 1193 struct DartValueTraits<bool> { |
| 1194 static inline Dart_Handle toDartValue(const bool& value, DartDOMData* domDat
a) |
| 1195 { |
| 1196 return DartUtilities::boolToDart(value); |
| 1197 } |
| 1198 }; |
| 1199 |
| 1200 template<> |
| 1201 struct DartValueTraits<int> { |
| 1202 static inline Dart_Handle toDartValue(const int& value, DartDOMData* domData
) |
| 1203 { |
| 1204 return DartUtilities::intToDart(value); |
| 1205 } |
| 1206 }; |
| 1207 |
| 1208 template<> |
| 1209 struct DartValueTraits<long> { |
| 1210 static inline Dart_Handle toDartValue(const long& value, DartDOMData* domDat
a) |
| 1211 { |
| 1212 return DartUtilities::intToDart(value); |
| 1213 } |
| 1214 }; |
| 1215 |
| 1216 template<> |
| 1217 struct DartValueTraits<unsigned> { |
| 1218 static inline Dart_Handle toDartValue(const unsigned& value, DartDOMData* do
mData) |
| 1219 { |
| 1220 return DartUtilities::unsignedToDart(value); |
| 1221 } |
| 1222 }; |
| 1223 |
| 1224 template<> |
| 1225 struct DartValueTraits<unsigned long> { |
| 1226 static inline Dart_Handle toDartValue(const unsigned long& value, DartDOMDat
a* domData) |
| 1227 { |
| 1228 return DartUtilities::unsignedToDart(value); |
| 1229 } |
| 1230 }; |
| 1231 |
| 1232 template<> |
| 1233 struct DartValueTraits<float> { |
| 1234 static inline Dart_Handle toDartValue(const float& value, DartDOMData* domDa
ta) |
| 1235 { |
| 1236 return DartUtilities::doubleToDart(value); |
| 1237 } |
| 1238 }; |
| 1239 |
| 1240 template<> |
| 1241 struct DartValueTraits<double> { |
| 1242 static inline Dart_Handle toDartValue(const double& value, DartDOMData* domD
ata) |
| 1243 { |
| 1244 return DartUtilities::doubleToDart(value); |
| 1245 } |
| 1246 }; |
| 1247 |
| 1248 template<> |
| 1249 struct DartValueTraits<PassRefPtr<ArrayBuffer> > { |
| 1250 static inline Dart_Handle toDartValue(const PassRefPtr<ArrayBuffer>& value,
DartDOMData* domData) |
| 1251 { |
| 1252 return DartUtilities::arrayBufferToDart(value); |
| 1253 } |
| 1254 }; |
| 1255 |
| 1256 template<typename T, size_t inlineCapacity> |
| 1257 Dart_Handle dartArray(const Vector<T, inlineCapacity>& vector, DartDOMData* domD
ata) |
| 1258 { |
| 1259 Dart_Handle result = Dart_NewList(vector.size()); |
| 1260 int index = 0; |
| 1261 typename Vector<T, inlineCapacity>::const_iterator end = vector.end(); |
| 1262 typedef DartValueTraits<T> TraitsType; |
| 1263 for (typename Vector<T, inlineCapacity>::const_iterator iter = vector.begin(
); iter != end; ++iter) |
| 1264 Dart_ListSetAt(result, index, TraitsType::toDartValue(*iter, domData)); |
| 1265 return result; |
| 1266 } |
| 1267 |
| 1268 template <typename T, size_t inlineCapacity, typename Allocator> |
| 1269 struct DartValueTraits<WTF::Vector<T, inlineCapacity, Allocator> > { |
| 1270 static inline Dart_Handle toDartValue(const Vector<T, inlineCapacity, Alloca
tor>& value, DartDOMData* domData) |
| 1271 { |
| 1272 return dartArray(value, domData); |
| 1273 } |
| 1274 }; |
| 1275 |
| 1276 // Errors? |
| 1277 template<typename T, size_t inlineCapacity> |
| 1278 Dart_Handle dartArray(const HeapVector<T, inlineCapacity>& vector, DartDOMData*
domData) |
| 1279 { |
| 1280 Dart_Handle result = Dart_NewList(vector.size()); |
| 1281 int index = 0; |
| 1282 typename HeapVector<T, inlineCapacity>::const_iterator end = vector.end(); |
| 1283 typedef DartValueTraits<T> TraitsType; |
| 1284 for (typename HeapVector<T, inlineCapacity>::const_iterator iter = vector.be
gin(); iter != end; ++iter) |
| 1285 Dart_ListSetAt(result, index, TraitsType::toDartValue(*iter, domData)); |
| 1286 return result; |
| 1287 } |
| 1288 |
| 1289 template<typename T, size_t inlineCapacity> |
| 1290 struct DartValueTraits<HeapVector<T, inlineCapacity> > { |
| 1291 static inline Dart_Handle toDartValue(const HeapVector<T, inlineCapacity>& v
alue, DartDOMData* domData) |
| 1292 { |
| 1293 return dartArray(value, domData); |
| 1294 } |
| 1295 }; |
| 1296 |
| 1297 |
| 1298 #define DART_UNIMPLEMENTED_EXCEPTION() DartUtilities::notImplementedException(__
FILE__, __LINE__) |
| 1299 #define DART_UNIMPLEMENTED() Dart_ThrowException(DART_UNIMPLEMENTED_EXCEPTION())
; |
| 1300 |
| 1301 #if defined(DART_TIMER_SCOPE) |
| 1302 #define DART_START_TIMER() \ |
| 1303 double timerStop, timerStart = currentTimeMS(); |
| 1304 #define DART_RECORD_TIMER(msg) \ |
| 1305 timerStop = currentTimeMS(); \ |
| 1306 fprintf(stdout, "%s %.3f ms\n", msg, (timerStop - timerStart)); |
| 1307 #else |
| 1308 #define DART_START_TIMER() |
| 1309 #define DART_RECORD_TIMER(msg) |
| 1310 #endif |
| 1311 |
| 1312 } // namespace blink |
| 1313 |
| 1314 #endif // DartUtilities_h |
OLD | NEW |