12
12
namespace tonic {
13
13
14
14
DartWrappable::~DartWrappable () {
15
- TONIC_CHECK (! dart_wrapper_);
15
+ // Calls the destructor of dart_wrapper_ to delete WeakPersistentHandle.
16
16
}
17
17
18
18
// TODO(dnfield): Delete this. https://github.com/flutter/flutter/issues/50997
19
19
Dart_Handle DartWrappable::CreateDartWrapper (DartState* dart_state) {
20
- TONIC_DCHECK (!dart_wrapper_);
20
+ if (!dart_wrapper_.is_empty ()) {
21
+ // Any previously given out wrapper must have been GCed.
22
+ TONIC_DCHECK (Dart_IsNull (dart_wrapper_.Get ()));
23
+ dart_wrapper_.Clear ();
24
+ }
25
+
21
26
const DartWrapperInfo& info = GetDartWrapperInfo ();
22
27
23
28
Dart_PersistentHandle type = dart_state->class_library ().GetClass (info);
@@ -36,14 +41,19 @@ Dart_Handle DartWrappable::CreateDartWrapper(DartState* dart_state) {
36
41
TONIC_DCHECK (!LogIfError (res));
37
42
38
43
this ->RetainDartWrappableReference (); // Balanced in FinalizeDartWrapper.
39
- dart_wrapper_ = Dart_NewWeakPersistentHandle (
40
- wrapper, this , GetAllocationSize (), &FinalizeDartWrapper);
44
+ dart_wrapper_. Set (dart_state, wrapper, this , GetAllocationSize (),
45
+ &FinalizeDartWrapper);
41
46
42
47
return wrapper;
43
48
}
44
49
45
50
void DartWrappable::AssociateWithDartWrapper (Dart_Handle wrapper) {
46
- TONIC_DCHECK (!dart_wrapper_);
51
+ if (!dart_wrapper_.is_empty ()) {
52
+ // Any previously given out wrapper must have been GCed.
53
+ TONIC_DCHECK (Dart_IsNull (dart_wrapper_.Get ()));
54
+ dart_wrapper_.Clear ();
55
+ }
56
+
47
57
TONIC_CHECK (!LogIfError (wrapper));
48
58
49
59
const DartWrapperInfo& info = GetDartWrapperInfo ();
@@ -54,26 +64,25 @@ void DartWrappable::AssociateWithDartWrapper(Dart_Handle wrapper) {
54
64
wrapper, kWrapperInfoIndex , reinterpret_cast <intptr_t >(&info))));
55
65
56
66
this ->RetainDartWrappableReference (); // Balanced in FinalizeDartWrapper.
57
- dart_wrapper_ = Dart_NewWeakPersistentHandle (
58
- wrapper, this , GetAllocationSize (), &FinalizeDartWrapper);
67
+
68
+ DartState* dart_state = DartState::Current ();
69
+ dart_wrapper_.Set (dart_state, wrapper, this , GetAllocationSize (),
70
+ &FinalizeDartWrapper);
59
71
}
60
72
61
73
void DartWrappable::ClearDartWrapper () {
62
- TONIC_DCHECK (dart_wrapper_);
63
- Dart_Handle wrapper = Dart_HandleFromWeakPersistent ( dart_wrapper_);
74
+ TONIC_DCHECK (! dart_wrapper_. is_empty () );
75
+ Dart_Handle wrapper = dart_wrapper_. Get ( );
64
76
TONIC_CHECK (!LogIfError (Dart_SetNativeInstanceField (wrapper, kPeerIndex , 0 )));
65
77
TONIC_CHECK (
66
78
!LogIfError (Dart_SetNativeInstanceField (wrapper, kWrapperInfoIndex , 0 )));
67
- Dart_DeleteWeakPersistentHandle (dart_wrapper_);
68
- dart_wrapper_ = nullptr ;
79
+ dart_wrapper_.Clear ();
69
80
this ->ReleaseDartWrappableReference ();
70
81
}
71
82
72
83
void DartWrappable::FinalizeDartWrapper (void * isolate_callback_data,
73
- Dart_WeakPersistentHandle wrapper,
74
84
void * peer) {
75
85
DartWrappable* wrappable = reinterpret_cast <DartWrappable*>(peer);
76
- wrappable->dart_wrapper_ = nullptr ;
77
86
wrappable->ReleaseDartWrappableReference (); // Balanced in CreateDartWrapper.
78
87
}
79
88
0 commit comments