| OLD | NEW |
| 1 // Copyright 2011, Google Inc. | 1 // Copyright 2011, Google Inc. |
| 2 // All rights reserved. | 2 // All rights reserved. |
| 3 // | 3 // |
| 4 // Redistribution and use in source and binary forms, with or without | 4 // Redistribution and use in source and binary forms, with or without |
| 5 // modification, are permitted provided that the following conditions are | 5 // modification, are permitted provided that the following conditions are |
| 6 // met: | 6 // met: |
| 7 // | 7 // |
| 8 // * Redistributions of source code must retain the above copyright | 8 // * Redistributions of source code must retain the above copyright |
| 9 // notice, this list of conditions and the following disclaimer. | 9 // notice, this list of conditions and the following disclaimer. |
| 10 // * Redistributions in binary form must reproduce the above | 10 // * Redistributions in binary form must reproduce the above |
| (...skipping 299 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 310 return; | 310 return; |
| 311 } | 311 } |
| 312 } | 312 } |
| 313 | 313 |
| 314 class DartDOMWrapper { | 314 class DartDOMWrapper { |
| 315 public: | 315 public: |
| 316 template <class WebkitClass> | 316 template <class WebkitClass> |
| 317 static Dart_Handle newWrapper(const char* className, WebkitClass* domObject) | 317 static Dart_Handle newWrapper(const char* className, WebkitClass* domObject) |
| 318 { | 318 { |
| 319 Dart_Handle wrapper = instantiateWrapper(className); | 319 Dart_Handle wrapper = instantiateWrapper(className); |
| 320 installNativePointers(domObject, wrapper); | 320 writeNativePointer(wrapper, kNativeImplementationIndex, domObject); |
| 321 return wrapper; | 321 return wrapper; |
| 322 } | 322 } |
| 323 | 323 |
| 324 template <class BindingsClass> | 324 template <class BindingsClass> |
| 325 static Dart_Handle toDart(typename BindingsClass::NativeType* instance) | 325 static Dart_Handle toDart(typename BindingsClass::NativeType* instance) |
| 326 { | 326 { |
| 327 return toDart(instance, BindingsClass::dartImplementationClassName); | 327 return toDart(instance, BindingsClass::dartImplementationClassName); |
| 328 } | 328 } |
| 329 | 329 |
| 330 template <class WebkitClass> | 330 template <class WebkitClass> |
| (...skipping 12 matching lines...) Expand all Loading... |
| 343 wrapper = instantiateWrapper(className); | 343 wrapper = instantiateWrapper(className); |
| 344 bindDOMObjectToDartWrapper(instance, wrapper); | 344 bindDOMObjectToDartWrapper(instance, wrapper); |
| 345 return wrapper; | 345 return wrapper; |
| 346 } | 346 } |
| 347 | 347 |
| 348 template <class WebkitClass> | 348 template <class WebkitClass> |
| 349 static void bindDOMObjectToDartWrapper(WebkitClass* domObject, Dart_Handle w
rapper) | 349 static void bindDOMObjectToDartWrapper(WebkitClass* domObject, Dart_Handle w
rapper) |
| 350 { | 350 { |
| 351 ASSERT(domObject); | 351 ASSERT(domObject); |
| 352 domObject->ref(); | 352 domObject->ref(); |
| 353 installNativePointers(domObject, wrapper); | 353 writeNativePointer(wrapper, kNativeImplementationIndex, domObject); |
| 354 | 354 |
| 355 // FIXME: make persistent handle weak and deref domObject in weak callba
ck. | 355 Dart_Handle persistentWrapperHandle = DartUtilities::createWeakPersisten
tHandle(wrapper, domObject, &wrapperWeakCallback<WebkitClass>); |
| 356 Dart_Handle persistentWrapperHandle = Dart_NewPersistentHandle(wrapper); | |
| 357 | 356 |
| 358 DartDOMMap* domMap = DartUtilities::domMapForCurrentIsolate(); | 357 DartDOMMap* domMap = DartUtilities::domMapForCurrentIsolate(); |
| 359 ASSERT(!domMap->contains(domObject)); | 358 ASSERT(!domMap->contains(domObject)); |
| 360 domMap->set(domObject, persistentWrapperHandle); | 359 domMap->set(domObject, persistentWrapperHandle); |
| 361 } | 360 } |
| 362 | 361 |
| 363 template <class BindingsClass> | 362 template <class BindingsClass> |
| 364 static typename BindingsClass::NativeType* unwrapDartWrapper(Dart_Handle wra
pper, Dart_Handle& exception) | 363 static typename BindingsClass::NativeType* unwrapDartWrapper(Dart_Handle wra
pper, Dart_Handle& exception) |
| 365 { | 364 { |
| 366 // FIXME: support cross-domain wrappers. | 365 // FIXME: support cross-domain wrappers. |
| 367 if (!BindingsClass::instanceOf(wrapper)) { | 366 if (!BindingsClass::instanceOf(wrapper)) { |
| 368 String message = String("Invalid class: expected instance of ") + Bi
ndingsClass::dartImplementationClassName; | 367 String message = String("Invalid class: expected instance of ") + Bi
ndingsClass::dartImplementationClassName; |
| 369 exception = DartUtilities::stringToDartString(message); | 368 exception = DartUtilities::stringToDartString(message); |
| 370 return 0; | 369 return 0; |
| 371 } | 370 } |
| 372 ASSERT(!exception); | 371 ASSERT(!exception); |
| 373 void* nativePointer = readNativePointer(wrapper, kNativeImplementationIn
dex); | 372 void* nativePointer = readNativePointer(wrapper, kNativeImplementationIn
dex); |
| 374 return reinterpret_cast<typename BindingsClass::NativeType*>(nativePoint
er); | 373 return reinterpret_cast<typename BindingsClass::NativeType*>(nativePoint
er); |
| 375 } | 374 } |
| 376 | 375 |
| 377 static void derefDOMObject(Dart_Handle wrapper, void* domObject); | |
| 378 | |
| 379 template <class WebkitClass> | 376 template <class WebkitClass> |
| 380 static WebkitClass* receiver(Dart_NativeArguments args) | 377 static WebkitClass* receiver(Dart_NativeArguments args) |
| 381 { | 378 { |
| 382 // Type of receiver is ensured by Dart VM runtime, so bypass additional
checks. | 379 // Type of receiver is ensured by Dart VM runtime, so bypass additional
checks. |
| 383 void* nativePointer = readNativePointer(Dart_GetNativeArgument(args, 0),
kNativeImplementationIndex); | 380 void* nativePointer = readNativePointer(Dart_GetNativeArgument(args, 0),
kNativeImplementationIndex); |
| 384 WebkitClass* const recv = static_cast<WebkitClass*>(nativePointer); | 381 WebkitClass* const recv = static_cast<WebkitClass*>(nativePointer); |
| 385 ASSERT(recv); // Never should return 0. | 382 ASSERT(recv); // Never should return 0. |
| 386 return recv; | 383 return recv; |
| 387 } | 384 } |
| 388 | 385 |
| (...skipping 16 matching lines...) Expand all Loading... |
| 405 } | 402 } |
| 406 | 403 |
| 407 static Dart_Handle exceptionCodeToDartException(ExceptionCode); | 404 static Dart_Handle exceptionCodeToDartException(ExceptionCode); |
| 408 | 405 |
| 409 // DO NOT USE: Use BindingsClass:instanceOf(handle) instead. | 406 // DO NOT USE: Use BindingsClass:instanceOf(handle) instead. |
| 410 static bool instanceOf(const char* dartImplementationClassName, Dart_Handle
wrapper); | 407 static bool instanceOf(const char* dartImplementationClassName, Dart_Handle
wrapper); |
| 411 | 408 |
| 412 private: | 409 private: |
| 413 enum NativeFieldIndices { | 410 enum NativeFieldIndices { |
| 414 kNativeImplementationIndex = 0, | 411 kNativeImplementationIndex = 0, |
| 415 kDerefObjectFunctionIndex, | |
| 416 kNativeFieldCount | 412 kNativeFieldCount |
| 417 }; | 413 }; |
| 418 | 414 |
| 419 static Dart_Handle instantiateWrapper(const char* className); | 415 static Dart_Handle instantiateWrapper(const char* className); |
| 420 | 416 |
| 421 static void writeNativePointer(Dart_Handle wrapper, int index, void* pointer
) | 417 static void writeNativePointer(Dart_Handle wrapper, int index, void* pointer
) |
| 422 { | 418 { |
| 423 DartApiScope scope; | 419 DartApiScope scope; |
| 424 Dart_Handle result = Dart_SetNativeInstanceField(wrapper, index, reinter
pret_cast<intptr_t>(pointer)); | 420 Dart_Handle result = Dart_SetNativeInstanceField(wrapper, index, reinter
pret_cast<intptr_t>(pointer)); |
| 425 UNUSED_PARAM(result); | 421 UNUSED_PARAM(result); |
| 426 ASSERT(!Dart_IsError(result)); | 422 ASSERT(!Dart_IsError(result)); |
| 427 } | 423 } |
| 428 | 424 |
| 429 static void* readNativePointer(Dart_Handle wrapper, int index) | 425 static void* readNativePointer(Dart_Handle wrapper, int index) |
| 430 { | 426 { |
| 431 // FIXME: Try to remove this scope from the hot path. | 427 // FIXME: Try to remove this scope from the hot path. |
| 432 DartApiScope scope; | 428 DartApiScope scope; |
| 433 intptr_t value; | 429 intptr_t value; |
| 434 Dart_Handle result = Dart_GetNativeInstanceField(wrapper, index, &value)
; | 430 Dart_Handle result = Dart_GetNativeInstanceField(wrapper, index, &value)
; |
| 435 ASSERT(!Dart_IsError(result)); | 431 ASSERT(!Dart_IsError(result)); |
| 436 UNUSED_PARAM(result); | 432 UNUSED_PARAM(result); |
| 437 return reinterpret_cast<void*>(value); | 433 return reinterpret_cast<void*>(value); |
| 438 } | 434 } |
| 439 | 435 |
| 440 template <class WebkitClass> | 436 template<typename T> |
| 441 static void installNativePointers(WebkitClass* domObject, Dart_Handle wrappe
r) | 437 static void wrapperWeakCallback(Dart_Handle, void* domObject) |
| 442 { | 438 { |
| 443 ASSERT(domObject); | 439 DartDOMMap* domMap = DartUtilities::domMapForCurrentIsolate(); |
| 444 DerefObjectFunction derefObjectFunction = &DartDOMWrapper::derefObject<W
ebkitClass>; | 440 ASSERT(domMap->contains(domObject)); |
| 445 writeNativePointer(wrapper, kNativeImplementationIndex, domObject); | 441 domMap->remove(domObject); |
| 446 writeNativePointer(wrapper, kDerefObjectFunctionIndex, reinterpret_cast<
void*>(derefObjectFunction)); | 442 static_cast<T*>(domObject)->deref(); |
| 447 } | |
| 448 | |
| 449 typedef void (*DerefObjectFunction)(void*); | |
| 450 | |
| 451 template<typename T> | |
| 452 static void derefObject(void* pointer) | |
| 453 { | |
| 454 static_cast<T*>(pointer)->deref(); | |
| 455 } | 443 } |
| 456 }; | 444 }; |
| 457 | 445 |
| 458 template<> | |
| 459 inline void DartDOMWrapper::derefObject<NPObject>(void*) | |
| 460 { | |
| 461 // FIXME: proper deref. | |
| 462 } | |
| 463 | |
| 464 // ParameterAdapter. | 446 // ParameterAdapter. |
| 465 | 447 |
| 466 template <typename Value> | 448 template <typename Value> |
| 467 class ParameterAdapterBase { | 449 class ParameterAdapterBase { |
| 468 public: | 450 public: |
| 469 bool conversionSuccessful() const { return !m_exception; } | 451 bool conversionSuccessful() const { return !m_exception; } |
| 470 | 452 |
| 471 Dart_Handle exception() const | 453 Dart_Handle exception() const |
| 472 { | 454 { |
| 473 ASSERT(!conversionSuccessful()); | 455 ASSERT(!conversionSuccessful()); |
| (...skipping 295 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 769 template <> | 751 template <> |
| 770 class ParameterAdapter<ScriptValue> : public ParameterAdapterBase<ScriptValue> { | 752 class ParameterAdapter<ScriptValue> : public ParameterAdapterBase<ScriptValue> { |
| 771 public: | 753 public: |
| 772 explicit ParameterAdapter(Dart_Handle handle) { this->unsupported(); } | 754 explicit ParameterAdapter(Dart_Handle handle) { this->unsupported(); } |
| 773 operator ScriptValue() const { return this->value(); } | 755 operator ScriptValue() const { return this->value(); } |
| 774 }; | 756 }; |
| 775 | 757 |
| 776 } | 758 } |
| 777 | 759 |
| 778 #endif // DartDOMWrapper_h | 760 #endif // DartDOMWrapper_h |
| OLD | NEW |