Skip to content
This repository was archived by the owner on Feb 25, 2025. It is now read-only.

Adds Tonic templates for Dart FFI bindings. #29405

Merged
8 commits merged into from Feb 8, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 15 additions & 4 deletions lib/ui/painting/rrect.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,21 @@ namespace tonic {

template <>
struct DartConverter<flutter::RRect> {
static flutter::RRect FromDart(Dart_Handle handle);
static flutter::RRect FromArguments(Dart_NativeArguments args,
int index,
Dart_Handle& exception);
using NativeType = flutter::RRect;
using FfiType = Dart_Handle;
static constexpr const char* kFfiRepresentation = "Handle";
static constexpr const char* kDartRepresentation = "Object";
static constexpr bool kAllowedInLeafCall = false;

static NativeType FromDart(Dart_Handle handle);
static NativeType FromArguments(Dart_NativeArguments args,
int index,
Dart_Handle& exception);

static NativeType FromFfi(FfiType val) { return FromDart(val); }
static const char* GetFfiRepresentation() { return kFfiRepresentation; }
static const char* GetDartRepresentation() { return kDartRepresentation; }
static bool AllowedInLeafCall() { return kAllowedInLeafCall; }
};

} // namespace tonic
Expand Down
4 changes: 4 additions & 0 deletions testing/dart_fixture.cc
Original file line number Diff line number Diff line change
Expand Up @@ -74,4 +74,8 @@ void DartFixture::AddNativeCallback(std::string name,
native_resolver_->AddNativeCallback(std::move(name), callback);
}

void DartFixture::AddFfiNativeCallback(std::string name, void* callback_ptr) {
native_resolver_->AddFfiNativeCallback(std::move(name), callback_ptr);
}

} // namespace flutter::testing
1 change: 1 addition & 0 deletions testing/dart_fixture.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ class DartFixture {
virtual Settings CreateSettingsForFixture();

void AddNativeCallback(std::string name, Dart_NativeFunction callback);
void AddFfiNativeCallback(std::string name, void* callback_ptr);

protected:
void SetSnapshotsAndAssets(Settings& settings);
Expand Down
35 changes: 35 additions & 0 deletions testing/test_dart_native_resolver.cc
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ void TestDartNativeResolver::AddNativeCallback(std::string name,
Dart_NativeFunction callback) {
native_callbacks_[name] = callback;
}
void TestDartNativeResolver::AddFfiNativeCallback(std::string name,
void* callback_ptr) {
ffi_native_callbacks_[name] = callback_ptr;
}

Dart_NativeFunction TestDartNativeResolver::ResolveCallback(
std::string name) const {
Expand All @@ -33,6 +37,14 @@ Dart_NativeFunction TestDartNativeResolver::ResolveCallback(
return found->second;
}

void* TestDartNativeResolver::ResolveFfiCallback(std::string name) const {
auto found = ffi_native_callbacks_.find(name);
if (found == ffi_native_callbacks_.end()) {
return nullptr;
}
return found->second;
}

static std::mutex gIsolateResolversMutex;
static std::map<Dart_Isolate, std::weak_ptr<TestDartNativeResolver>>
gIsolateResolvers;
Expand Down Expand Up @@ -65,6 +77,25 @@ static const uint8_t* DartNativeEntrySymbolCallback(
return reinterpret_cast<const uint8_t*>("¯\\_(ツ)_/¯");
}

void* TestDartNativeResolver::FfiNativeResolver(const char* name,
uintptr_t args_n) {
std::scoped_lock lock(gIsolateResolversMutex);
auto found = gIsolateResolvers.find(Dart_CurrentIsolate());
if (found == gIsolateResolvers.end()) {
FML_LOG(ERROR) << "Could not resolve native method for :" << name;
return nullptr;
}

if (auto resolver = found->second.lock()) {
return resolver->ResolveFfiCallback(name);
} else {
gIsolateResolvers.erase(found);
}

FML_LOG(ERROR) << "Could not resolve native method for :" << name;
return nullptr;
}

void TestDartNativeResolver::SetNativeResolverForIsolate() {
FML_CHECK(!Dart_IsError(Dart_RootLibrary()));
auto result = Dart_SetNativeResolver(Dart_RootLibrary(),
Expand All @@ -73,6 +104,10 @@ void TestDartNativeResolver::SetNativeResolverForIsolate() {
FML_CHECK(!tonic::LogIfError(result))
<< "Could not set native resolver in test.";

result = Dart_SetFfiNativeResolver(Dart_RootLibrary(), &FfiNativeResolver);
FML_CHECK(!tonic::LogIfError(result))
<< "Could not set FFI native resolver in test.";

std::scoped_lock lock(gIsolateResolversMutex);
gIsolateResolvers[Dart_CurrentIsolate()] = shared_from_this();

Expand Down
4 changes: 4 additions & 0 deletions testing/test_dart_native_resolver.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,18 +35,22 @@ class TestDartNativeResolver
~TestDartNativeResolver();

void AddNativeCallback(std::string name, Dart_NativeFunction callback);
void AddFfiNativeCallback(std::string name, void* callback_ptr);

void SetNativeResolverForIsolate();

private:
std::map<std::string, Dart_NativeFunction> native_callbacks_;
std::map<std::string, void*> ffi_native_callbacks_;

Dart_NativeFunction ResolveCallback(std::string name) const;
void* ResolveFfiCallback(std::string name) const;

static Dart_NativeFunction DartNativeEntryResolverCallback(
Dart_Handle dart_name,
int num_of_arguments,
bool* auto_setup_scope);
static void* FfiNativeResolver(const char* name, uintptr_t args_n);

FML_DISALLOW_COPY_AND_ASSIGN(TestDartNativeResolver);
};
Expand Down
Loading