| OLD | NEW |
| 1 // Copyright 2016 the V8 project authors. All rights reserved. | 1 // Copyright 2016 the V8 project authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #ifndef V8_WASM_MANAGED_H_ | 5 #ifndef V8_WASM_MANAGED_H_ |
| 6 #define V8_WASM_MANAGED_H_ | 6 #define V8_WASM_MANAGED_H_ |
| 7 | 7 |
| 8 #include "src/factory.h" | 8 #include "src/factory.h" |
| 9 #include "src/global-handles.h" | 9 #include "src/global-handles.h" |
| 10 #include "src/handles.h" | 10 #include "src/handles.h" |
| 11 #include "src/isolate.h" | 11 #include "src/isolate.h" |
| 12 | 12 |
| 13 namespace v8 { | 13 namespace v8 { |
| 14 namespace internal { | 14 namespace internal { |
| 15 // An object that wraps a pointer to a C++ object and optionally deletes it | 15 // An object that wraps a pointer to a C++ object and manages its lifetime. |
| 16 // when the managed wrapper object is garbage collected. | 16 // The C++ object will be deleted when the managed wrapper object is |
| 17 // garbage collected, or, last resort, if the isolate is torn down before GC, |
| 18 // as part of Isolate::Dispose(). |
| 19 // Managed<CppType> may be used polymorphically as Foreign, where the held |
| 20 // address is typed as CppType**. The double indirection is due to the |
| 21 // use, by Managed, of Isolate::ManagedLifeline, which has a CppType* first |
| 22 // field. |
| 17 template <class CppType> | 23 template <class CppType> |
| 18 class Managed : public Foreign { | 24 class Managed : public Foreign { |
| 19 public: | 25 public: |
| 20 V8_INLINE CppType* get() { | 26 V8_INLINE CppType* get() { |
| 21 return reinterpret_cast<CppType*>(foreign_address()); | 27 return *(reinterpret_cast<CppType**>(foreign_address())); |
| 22 } | 28 } |
| 23 | 29 |
| 24 static Managed<CppType>* cast(Object* obj) { | 30 static Managed<CppType>* cast(Object* obj) { |
| 25 SLOW_DCHECK(obj->IsForeign()); | 31 SLOW_DCHECK(obj->IsForeign()); |
| 26 return reinterpret_cast<Managed<CppType>*>(obj); | 32 return reinterpret_cast<Managed<CppType>*>(obj); |
| 27 } | 33 } |
| 28 | 34 |
| 29 static Handle<Managed<CppType>> New(Isolate* isolate, CppType* ptr, | 35 static Handle<Managed<CppType>> New(Isolate* isolate, CppType* ptr) { |
| 30 bool delete_on_gc = true) { | 36 Isolate::ManagedLifeline* node = isolate->RegisterForReleaseAtTeardown( |
| 37 ptr, Managed<CppType>::NativeDelete); |
| 31 Handle<Managed<CppType>> handle = Handle<Managed<CppType>>::cast( | 38 Handle<Managed<CppType>> handle = Handle<Managed<CppType>>::cast( |
| 32 isolate->factory()->NewForeign(reinterpret_cast<Address>(ptr))); | 39 isolate->factory()->NewForeign(reinterpret_cast<Address>(node))); |
| 33 if (delete_on_gc) { | 40 RegisterWeakCallbackForDelete(isolate, handle); |
| 34 RegisterWeakCallbackForDelete(isolate, handle); | |
| 35 } | |
| 36 return handle; | 41 return handle; |
| 37 } | 42 } |
| 38 | 43 |
| 39 private: | 44 private: |
| 40 static void RegisterWeakCallbackForDelete(Isolate* isolate, | 45 static void RegisterWeakCallbackForDelete(Isolate* isolate, |
| 41 Handle<Managed<CppType>> handle) { | 46 Handle<Managed<CppType>> handle) { |
| 42 Handle<Object> global_handle = isolate->global_handles()->Create(*handle); | 47 Handle<Object> global_handle = isolate->global_handles()->Create(*handle); |
| 43 GlobalHandles::MakeWeak(global_handle.location(), global_handle.location(), | 48 GlobalHandles::MakeWeak(global_handle.location(), global_handle.location(), |
| 44 &Managed<CppType>::Delete, | 49 &Managed<CppType>::GCDelete, |
| 45 v8::WeakCallbackType::kFinalizer); | 50 v8::WeakCallbackType::kFinalizer); |
| 46 } | 51 } |
| 47 static void Delete(const v8::WeakCallbackInfo<void>& data) { | 52 |
| 53 static void GCDelete(const v8::WeakCallbackInfo<void>& data) { |
| 48 Managed<CppType>** p = | 54 Managed<CppType>** p = |
| 49 reinterpret_cast<Managed<CppType>**>(data.GetParameter()); | 55 reinterpret_cast<Managed<CppType>**>(data.GetParameter()); |
| 50 delete (*p)->get(); | 56 |
| 51 (*p)->set_foreign_address(0); | 57 Isolate::ManagedLifeline* lifeline = (*p)->GetLifeline(); |
| 58 |
| 59 Isolate* isolate = reinterpret_cast<Isolate*>(data.GetIsolate()); |
| 60 lifeline->Dispose(); |
| 61 isolate->UnregisterFromReleaseAtTeardown(&lifeline); |
| 62 |
| 63 (*p)->set_foreign_address(static_cast<Address>(nullptr)); |
| 52 GlobalHandles::Destroy(reinterpret_cast<Object**>(p)); | 64 GlobalHandles::Destroy(reinterpret_cast<Object**>(p)); |
| 53 } | 65 } |
| 66 |
| 67 static void NativeDelete(void* value) { |
| 68 CppType* typed_value = reinterpret_cast<CppType*>(value); |
| 69 delete typed_value; |
| 70 } |
| 71 |
| 72 Isolate::ManagedLifeline* GetLifeline() { |
| 73 return reinterpret_cast<Isolate::ManagedLifeline*>(foreign_address()); |
| 74 } |
| 54 }; | 75 }; |
| 55 } // namespace internal | 76 } // namespace internal |
| 56 } // namespace v8 | 77 } // namespace v8 |
| 57 | 78 |
| 58 #endif // V8_WASM_MANAGED_H_ | 79 #endif // V8_WASM_MANAGED_H_ |
| OLD | NEW |