1212namespace tonic {
1313
1414DartWrappable::~DartWrappable () {
15- TONIC_CHECK (! dart_wrapper_);
15+ // Calls the destructor of dart_wrapper_ to delete WeakPersistentHandle.
1616}
1717
1818// TODO(dnfield): Delete this. https://github.com/flutter/flutter/issues/50997
1919Dart_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+
2126 const DartWrapperInfo& info = GetDartWrapperInfo ();
2227
2328 Dart_PersistentHandle type = dart_state->class_library ().GetClass (info);
@@ -36,14 +41,19 @@ Dart_Handle DartWrappable::CreateDartWrapper(DartState* dart_state) {
3641 TONIC_DCHECK (!LogIfError (res));
3742
3843 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);
4146
4247 return wrapper;
4348}
4449
4550void 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+
4757 TONIC_CHECK (!LogIfError (wrapper));
4858
4959 const DartWrapperInfo& info = GetDartWrapperInfo ();
@@ -54,26 +64,25 @@ void DartWrappable::AssociateWithDartWrapper(Dart_Handle wrapper) {
5464 wrapper, kWrapperInfoIndex , reinterpret_cast <intptr_t >(&info))));
5565
5666 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);
5971}
6072
6173void 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 ( );
6476 TONIC_CHECK (!LogIfError (Dart_SetNativeInstanceField (wrapper, kPeerIndex , 0 )));
6577 TONIC_CHECK (
6678 !LogIfError (Dart_SetNativeInstanceField (wrapper, kWrapperInfoIndex , 0 )));
67- Dart_DeleteWeakPersistentHandle (dart_wrapper_);
68- dart_wrapper_ = nullptr ;
79+ dart_wrapper_.Clear ();
6980 this ->ReleaseDartWrappableReference ();
7081}
7182
7283void DartWrappable::FinalizeDartWrapper (void * isolate_callback_data,
73- Dart_WeakPersistentHandle wrapper,
7484 void * peer) {
7585 DartWrappable* wrappable = reinterpret_cast <DartWrappable*>(peer);
76- wrappable->dart_wrapper_ = nullptr ;
7786 wrappable->ReleaseDartWrappableReference (); // Balanced in CreateDartWrapper.
7887}
7988
0 commit comments