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 #include "src/objects-inl.h" | 12 #include "src/objects-inl.h" |
13 | 13 |
14 namespace v8 { | 14 namespace v8 { |
15 namespace internal { | 15 namespace internal { |
16 // An object that wraps a pointer to a C++ object and optionally deletes it | 16 // An object that wraps a pointer to a C++ object and optionally deletes it |
17 // when the managed wrapper object is garbage collected. | 17 // when the managed wrapper object is garbage collected. |
18 template <class CppType> | 18 template <class CppType> |
19 class Managed : public Foreign { | 19 class Managed : public Foreign { |
20 public: | 20 public: |
21 V8_INLINE CppType* get() { | 21 V8_INLINE CppType* get() { |
22 return reinterpret_cast<CppType*>(foreign_address()); | 22 return reinterpret_cast<CppType*>(foreign_address()); |
23 } | 23 } |
24 | 24 |
| 25 static Managed<CppType>* cast(Object* obj) { |
| 26 SLOW_DCHECK(obj->IsForeign()); |
| 27 return reinterpret_cast<Managed<CppType>*>(obj); |
| 28 } |
| 29 |
25 static Handle<Managed<CppType>> New(Isolate* isolate, CppType* ptr, | 30 static Handle<Managed<CppType>> New(Isolate* isolate, CppType* ptr, |
26 bool delete_on_gc = true) { | 31 bool delete_on_gc = true) { |
27 Handle<Foreign> foreign = | 32 Handle<Managed<CppType>> handle = Handle<Managed<CppType>>::cast( |
28 isolate->factory()->NewForeign(reinterpret_cast<Address>(ptr)); | 33 isolate->factory()->NewForeign(reinterpret_cast<Address>(ptr))); |
29 Handle<Managed<CppType>> handle( | |
30 reinterpret_cast<Managed<CppType>*>(*foreign), isolate); | |
31 if (delete_on_gc) { | 34 if (delete_on_gc) { |
32 RegisterWeakCallbackForDelete(isolate, handle); | 35 RegisterWeakCallbackForDelete(isolate, handle); |
33 } | 36 } |
34 return handle; | 37 return handle; |
35 } | 38 } |
36 | 39 |
37 private: | 40 private: |
38 static void RegisterWeakCallbackForDelete(Isolate* isolate, | 41 static void RegisterWeakCallbackForDelete(Isolate* isolate, |
39 Handle<Managed<CppType>> handle) { | 42 Handle<Managed<CppType>> handle) { |
40 Handle<Object> global_handle = isolate->global_handles()->Create(*handle); | 43 Handle<Object> global_handle = isolate->global_handles()->Create(*handle); |
41 GlobalHandles::MakeWeak(global_handle.location(), global_handle.location(), | 44 GlobalHandles::MakeWeak(global_handle.location(), global_handle.location(), |
42 &Managed<CppType>::Delete, | 45 &Managed<CppType>::Delete, |
43 v8::WeakCallbackType::kFinalizer); | 46 v8::WeakCallbackType::kFinalizer); |
44 } | 47 } |
45 static void Delete(const v8::WeakCallbackInfo<void>& data) { | 48 static void Delete(const v8::WeakCallbackInfo<void>& data) { |
46 Managed<CppType>** p = | 49 Managed<CppType>** p = |
47 reinterpret_cast<Managed<CppType>**>(data.GetParameter()); | 50 reinterpret_cast<Managed<CppType>**>(data.GetParameter()); |
48 delete (*p)->get(); | 51 delete (*p)->get(); |
49 (*p)->set_foreign_address(0); | 52 (*p)->set_foreign_address(0); |
50 GlobalHandles::Destroy(reinterpret_cast<Object**>(p)); | 53 GlobalHandles::Destroy(reinterpret_cast<Object**>(p)); |
51 } | 54 } |
52 }; | 55 }; |
53 } // namespace internal | 56 } // namespace internal |
54 } // namespace v8 | 57 } // namespace v8 |
55 | 58 |
56 #endif // V8_WASM_MANAGED_H_ | 59 #endif // V8_WASM_MANAGED_H_ |
OLD | NEW |