Refactor of ClaimDartHandle -> AssociateWithDartWrapper (#16720)

This commit is contained in:
Dan Field 2020-02-20 17:31:03 -08:00 committed by GitHub
parent 4aaafe0d28
commit e18aba3321
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 18 additions and 31 deletions

View File

@ -34,7 +34,7 @@ void Scene::create(Dart_Handle scene_handle,
auto scene = fml::MakeRefCounted<Scene>(
std::move(rootLayer), rasterizerTracingThreshold,
checkerboardRasterCacheImages, checkerboardOffscreenLayers);
scene->ClaimDartHandle(scene_handle);
scene->AssociateWithDartWrapper(scene_handle);
}
Scene::Scene(std::shared_ptr<flutter::Layer> rootLayer,

View File

@ -33,7 +33,7 @@ class EngineLayer : public RefCountedDartWrappable<EngineLayer> {
static void MakeRetained(Dart_Handle dart_handle,
std::shared_ptr<flutter::ContainerLayer> layer) {
auto engine_layer = fml::MakeRefCounted<EngineLayer>(layer);
engine_layer->ClaimDartHandle(dart_handle);
engine_layer->AssociateWithDartWrapper(dart_handle);
}
static void RegisterNatives(tonic::DartLibraryNatives* natives);

View File

@ -31,7 +31,7 @@ fml::RefPtr<Picture> Picture::Create(
flutter::SkiaGPUObject<SkPicture> picture) {
auto canvas_picture = fml::MakeRefCounted<Picture>(std::move(picture));
canvas_picture->ClaimDartHandle(dart_handle);
canvas_picture->AssociateWithDartWrapper(dart_handle);
return canvas_picture;
}

View File

@ -226,7 +226,17 @@ void DartCallConstructor(Sig func, Dart_NativeArguments args) {
return;
wrappable = decoder.DispatchCtor(func);
}
wrappable->AssociateWithDartWrapper(args);
Dart_Handle wrapper = Dart_GetNativeArgument(args, 0);
TONIC_CHECK(!LogIfError(wrapper));
intptr_t native_fields[DartWrappable::kNumberOfNativeFields];
TONIC_CHECK(!LogIfError(Dart_GetNativeFieldsOfArgument(
args, 0, DartWrappable::kNumberOfNativeFields, native_fields)));
TONIC_CHECK(!native_fields[DartWrappable::kPeerIndex]);
TONIC_CHECK(!native_fields[DartWrappable::kWrapperInfoIndex]);
wrappable->AssociateWithDartWrapper(wrapper);
}
} // namespace tonic

View File

@ -42,7 +42,7 @@ Dart_Handle DartWrappable::CreateDartWrapper(DartState* dart_state) {
return wrapper;
}
void DartWrappable::ClaimDartHandle(Dart_Handle wrapper) {
void DartWrappable::AssociateWithDartWrapper(Dart_Handle wrapper) {
TONIC_DCHECK(!dart_wrapper_);
TONIC_CHECK(!LogIfError(wrapper));
@ -58,29 +58,6 @@ void DartWrappable::ClaimDartHandle(Dart_Handle wrapper) {
wrapper, this, GetAllocationSize(), &FinalizeDartWrapper);
}
void DartWrappable::AssociateWithDartWrapper(Dart_NativeArguments args) {
TONIC_DCHECK(!dart_wrapper_);
Dart_Handle wrapper = Dart_GetNativeArgument(args, 0);
TONIC_CHECK(!LogIfError(wrapper));
intptr_t native_fields[kNumberOfNativeFields];
TONIC_CHECK(!LogIfError(Dart_GetNativeFieldsOfArgument(
args, 0, kNumberOfNativeFields, native_fields)));
TONIC_CHECK(!native_fields[kPeerIndex]);
TONIC_CHECK(!native_fields[kWrapperInfoIndex]);
const DartWrapperInfo& info = GetDartWrapperInfo();
TONIC_CHECK(!LogIfError(Dart_SetNativeInstanceField(
wrapper, kPeerIndex, reinterpret_cast<intptr_t>(this))));
TONIC_CHECK(!LogIfError(Dart_SetNativeInstanceField(
wrapper, kWrapperInfoIndex, reinterpret_cast<intptr_t>(&info))));
this->RetainDartWrappableReference(); // Balanced in FinalizeDartWrapper.
dart_wrapper_ = Dart_NewWeakPersistentHandle(
wrapper, this, GetAllocationSize(), &FinalizeDartWrapper);
}
void DartWrappable::ClearDartWrapper() {
TONIC_DCHECK(dart_wrapper_);
Dart_Handle wrapper = Dart_HandleFromWeakPersistent(dart_wrapper_);

View File

@ -44,10 +44,10 @@ class DartWrappable {
virtual void ReleaseDartWrappableReference() const = 0;
// Use this method sparingly. It follows a slower path using Dart_New.
// Prefer constructing the object in Dart code and using ClaimDartHandle.
// Prefer constructing the object in Dart code and using
// AssociateWithDartWrapper.
Dart_Handle CreateDartWrapper(DartState* dart_state);
void ClaimDartHandle(Dart_Handle wrappable);
void AssociateWithDartWrapper(Dart_NativeArguments args);
void AssociateWithDartWrapper(Dart_Handle wrappable);
void ClearDartWrapper(); // Warning: Might delete this.
Dart_WeakPersistentHandle dart_wrapper() const { return dart_wrapper_; }