OLD | NEW |
(Empty) | |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "tonic/dart_wrappable.h" |
| 6 |
| 7 #include "tonic/dart_class_library.h" |
| 8 #include "tonic/dart_error.h" |
| 9 #include "tonic/dart_exception_factory.h" |
| 10 #include "tonic/dart_state.h" |
| 11 #include "tonic/dart_wrapper_info.h" |
| 12 |
| 13 namespace blink { |
| 14 |
| 15 DartWrappable::~DartWrappable() { |
| 16 CHECK(!dart_wrapper_); |
| 17 } |
| 18 |
| 19 void DartWrappable::AcceptDartGCVisitor(DartGCVisitor& visitor) const { |
| 20 } |
| 21 |
| 22 Dart_Handle DartWrappable::CreateDartWrapper(DartState* dart_state) { |
| 23 DCHECK(!dart_wrapper_); |
| 24 const DartWrapperInfo& info = GetDartWrapperInfo(); |
| 25 |
| 26 Dart_PersistentHandle type = dart_state->class_library().GetClass(info); |
| 27 DCHECK(!LogIfError(type)); |
| 28 |
| 29 intptr_t native_fields[kNumberOfNativeFields]; |
| 30 native_fields[kPeerIndex] = reinterpret_cast<intptr_t>(this); |
| 31 native_fields[kWrapperInfoIndex] = reinterpret_cast<intptr_t>(&info); |
| 32 Dart_Handle wrapper = |
| 33 Dart_AllocateWithNativeFields(type, kNumberOfNativeFields, native_fields); |
| 34 DCHECK(!LogIfError(wrapper)); |
| 35 |
| 36 info.ref_object(this); // Balanced in FinalizeDartWrapper. |
| 37 dart_wrapper_ = Dart_NewPrologueWeakPersistentHandle( |
| 38 wrapper, this, info.size_in_bytes, &FinalizeDartWrapper); |
| 39 |
| 40 return wrapper; |
| 41 } |
| 42 |
| 43 void DartWrappable::AssociateWithDartWrapper(Dart_NativeArguments args) { |
| 44 DCHECK(!dart_wrapper_); |
| 45 |
| 46 Dart_Handle wrapper = Dart_GetNativeArgument(args, 0); |
| 47 CHECK(!LogIfError(wrapper)); |
| 48 |
| 49 intptr_t native_fields[kNumberOfNativeFields]; |
| 50 CHECK(!LogIfError(Dart_GetNativeFieldsOfArgument( |
| 51 args, 0, kNumberOfNativeFields, native_fields))); |
| 52 CHECK(!native_fields[kPeerIndex]); |
| 53 CHECK(!native_fields[kWrapperInfoIndex]); |
| 54 |
| 55 const DartWrapperInfo& info = GetDartWrapperInfo(); |
| 56 CHECK(!LogIfError(Dart_SetNativeInstanceField( |
| 57 wrapper, kPeerIndex, reinterpret_cast<intptr_t>(this)))); |
| 58 CHECK(!LogIfError(Dart_SetNativeInstanceField( |
| 59 wrapper, kWrapperInfoIndex, reinterpret_cast<intptr_t>(&info)))); |
| 60 |
| 61 info.ref_object(this); // Balanced in FinalizeDartWrapper. |
| 62 dart_wrapper_ = Dart_NewPrologueWeakPersistentHandle( |
| 63 wrapper, this, info.size_in_bytes, &FinalizeDartWrapper); |
| 64 } |
| 65 |
| 66 void DartWrappable::FinalizeDartWrapper(void* isolate_callback_data, |
| 67 Dart_WeakPersistentHandle wrapper, |
| 68 void* peer) { |
| 69 DartWrappable* wrappable = reinterpret_cast<DartWrappable*>(peer); |
| 70 wrappable->dart_wrapper_ = nullptr; |
| 71 const DartWrapperInfo& info = wrappable->GetDartWrapperInfo(); |
| 72 info.deref_object(wrappable); // Balanced in CreateDartWrapper. |
| 73 } |
| 74 |
| 75 DartWrappable* DartConverterWrappable::FromDart(Dart_Handle handle) { |
| 76 intptr_t peer = 0; |
| 77 Dart_Handle result = |
| 78 Dart_GetNativeInstanceField(handle, DartWrappable::kPeerIndex, &peer); |
| 79 if (Dart_IsError(result)) |
| 80 return nullptr; |
| 81 return reinterpret_cast<DartWrappable*>(peer); |
| 82 } |
| 83 |
| 84 DartWrappable* DartConverterWrappable::FromArguments(Dart_NativeArguments args, |
| 85 int index, |
| 86 Dart_Handle& exception) { |
| 87 intptr_t native_fields[DartWrappable::kNumberOfNativeFields]; |
| 88 Dart_Handle result = Dart_GetNativeFieldsOfArgument( |
| 89 args, index, DartWrappable::kNumberOfNativeFields, native_fields); |
| 90 if (Dart_IsError(result)) { |
| 91 exception = Dart_NewStringFromCString(DartError::kInvalidArgument); |
| 92 return nullptr; |
| 93 } |
| 94 return reinterpret_cast<DartWrappable*>( |
| 95 native_fields[DartWrappable::kPeerIndex]); |
| 96 } |
| 97 |
| 98 DartWrappable* DartConverterWrappable::FromArgumentsWithNullCheck( |
| 99 Dart_NativeArguments args, int index, Dart_Handle& exception) { |
| 100 Dart_Handle handle = Dart_GetNativeArgument(args, index); |
| 101 if (Dart_IsNull(handle)) { |
| 102 DartState* state = DartState::Current(); |
| 103 exception = state->exception_factory().CreateNullArgumentException(index); |
| 104 return nullptr; |
| 105 } |
| 106 intptr_t native_fields[DartWrappable::kNumberOfNativeFields]; |
| 107 Dart_Handle result = Dart_GetNativeFieldsOfArgument( |
| 108 args, index, DartWrappable::kNumberOfNativeFields, native_fields); |
| 109 if (Dart_IsError(result)) { |
| 110 exception = Dart_NewStringFromCString(DartError::kInvalidArgument); |
| 111 return nullptr; |
| 112 } |
| 113 return reinterpret_cast<DartWrappable*>( |
| 114 native_fields[DartWrappable::kPeerIndex]); |
| 115 } |
| 116 |
| 117 } // namespace blink |
OLD | NEW |