forked from firka/flutter
Adds Tonic templates for Dart FFI bindings. (flutter/engine#29405)
This change adds additional Tonic templates for automatically generating bindings for Dart FFI, as well as serialisation of the bindings. - Adds parallel FfiDispather templates to the existing DartDispatcher used for (old) native bindings. - Adds serialisation of the bindings to enable automatic conversion and verification. - Extends existing DartConverters with conversions to and from the FFI transport types. - Adds new test (ffi_native_unittest.cc) for the above. This will allow us to replace the existing native functions in e.g. dart:ui with new Dart FFI native functions.
This commit is contained in:
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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);
|
||||
|
||||
@@ -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 {
|
||||
@@ -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;
|
||||
@@ -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(),
|
||||
@@ -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();
|
||||
|
||||
|
||||
@@ -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);
|
||||
};
|
||||
|
||||
@@ -36,30 +36,53 @@ struct DartConverterTypes {
|
||||
using ValueType = T;
|
||||
};
|
||||
|
||||
template <>
|
||||
struct DartConverter<void> {
|
||||
using FfiType = void;
|
||||
static constexpr const char* kFfiRepresentation = "Void";
|
||||
static constexpr const char* kDartRepresentation = "void";
|
||||
static constexpr bool kAllowedInLeafCall = true;
|
||||
static const char* GetFfiRepresentation() { return kFfiRepresentation; }
|
||||
static const char* GetDartRepresentation() { return kDartRepresentation; }
|
||||
static bool AllowedInLeafCall() { return kAllowedInLeafCall; }
|
||||
};
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
// Boolean
|
||||
|
||||
template <>
|
||||
struct DartConverter<bool> {
|
||||
static Dart_Handle ToDart(bool val) { return Dart_NewBoolean(val); }
|
||||
using NativeType = bool;
|
||||
using FfiType = bool;
|
||||
static constexpr const char* kFfiRepresentation = "Bool";
|
||||
static constexpr const char* kDartRepresentation = "bool";
|
||||
static constexpr bool kAllowedInLeafCall = true;
|
||||
|
||||
static Dart_Handle ToDart(NativeType val) { return Dart_NewBoolean(val); }
|
||||
|
||||
static void SetReturnValue(Dart_NativeArguments args, bool val) {
|
||||
Dart_SetBooleanReturnValue(args, val);
|
||||
}
|
||||
|
||||
static bool FromDart(Dart_Handle handle) {
|
||||
static NativeType FromDart(Dart_Handle handle) {
|
||||
bool result = 0;
|
||||
Dart_BooleanValue(handle, &result);
|
||||
return result;
|
||||
}
|
||||
|
||||
static bool FromArguments(Dart_NativeArguments args,
|
||||
int index,
|
||||
Dart_Handle& exception) {
|
||||
static NativeType FromArguments(Dart_NativeArguments args,
|
||||
int index,
|
||||
Dart_Handle& exception) {
|
||||
bool result = false;
|
||||
Dart_GetNativeBooleanArgument(args, index, &result);
|
||||
return result;
|
||||
}
|
||||
|
||||
static NativeType FromFfi(FfiType val) { return val; }
|
||||
static FfiType ToFfi(NativeType val) { return val; }
|
||||
static const char* GetFfiRepresentation() { return kFfiRepresentation; }
|
||||
static const char* GetDartRepresentation() { return kDartRepresentation; }
|
||||
static bool AllowedInLeafCall() { return kAllowedInLeafCall; }
|
||||
};
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
@@ -67,6 +90,10 @@ struct DartConverter<bool> {
|
||||
|
||||
template <typename T>
|
||||
struct DartConverterInteger {
|
||||
using FfiType = T;
|
||||
static constexpr const char* kDartRepresentation = "int";
|
||||
static constexpr bool kAllowedInLeafCall = true;
|
||||
|
||||
static Dart_Handle ToDart(T val) { return Dart_NewInteger(val); }
|
||||
|
||||
static void SetReturnValue(Dart_NativeArguments args, T val) {
|
||||
@@ -86,6 +113,24 @@ struct DartConverterInteger {
|
||||
Dart_GetNativeIntegerArgument(args, index, &result);
|
||||
return static_cast<T>(result);
|
||||
}
|
||||
static T FromFfi(FfiType val) { return val; }
|
||||
static FfiType ToFfi(T val) { return val; }
|
||||
static const char* GetDartRepresentation() { return kDartRepresentation; }
|
||||
// Note: Returns the correct bit-width for the host architecture.
|
||||
static const char* GetFfiRepresentation() {
|
||||
if (sizeof(T) == 4) {
|
||||
if (std::is_signed<T>()) {
|
||||
return "Int32";
|
||||
}
|
||||
return "Uint32";
|
||||
}
|
||||
TONIC_DCHECK(sizeof(T) == 8);
|
||||
if (std::is_signed<T>()) {
|
||||
return "Int64";
|
||||
}
|
||||
return "Uint64";
|
||||
}
|
||||
static bool AllowedInLeafCall() { return kAllowedInLeafCall; }
|
||||
};
|
||||
|
||||
template <>
|
||||
@@ -106,6 +151,11 @@ struct DartConverter<unsigned long>
|
||||
|
||||
template <>
|
||||
struct DartConverter<unsigned long long> {
|
||||
using FfiType = unsigned long long;
|
||||
static constexpr const char* kFfiRepresentation = "Uint64";
|
||||
static constexpr const char* kDartRepresentation = "int";
|
||||
static constexpr bool kAllowedInLeafCall = true;
|
||||
|
||||
// TODO(abarth): The Dart VM API doesn't yet have an entry-point for
|
||||
// an unsigned 64-bit type. We will need to add a Dart API for
|
||||
// constructing an integer from uint64_t.
|
||||
@@ -137,10 +187,26 @@ struct DartConverter<unsigned long long> {
|
||||
Dart_GetNativeIntegerArgument(args, index, &result);
|
||||
return result;
|
||||
}
|
||||
|
||||
static const char* GetFfiRepresentation() { return kFfiRepresentation; }
|
||||
static const char* GetDartRepresentation() { return kDartRepresentation; }
|
||||
static bool AllowedInLeafCall() { return kAllowedInLeafCall; }
|
||||
static FfiType FromFfi(FfiType val) {
|
||||
TONIC_DCHECK(val <= 0x7fffffffffffffffLL);
|
||||
return val;
|
||||
}
|
||||
// FFI does a bitwise conversion from uint64_t in C to int64 in Dart.
|
||||
static FfiType ToFfi(FfiType val) {
|
||||
TONIC_DCHECK(val <= 0x7fffffffffffffffLL);
|
||||
return val;
|
||||
}
|
||||
};
|
||||
|
||||
template <typename T>
|
||||
struct DartConverterFloatingPoint {
|
||||
using FfiType = T;
|
||||
static constexpr bool kAllowedInLeafCall = true;
|
||||
|
||||
static Dart_Handle ToDart(T val) { return Dart_NewDouble(val); }
|
||||
|
||||
static void SetReturnValue(Dart_NativeArguments args, T val) {
|
||||
@@ -160,19 +226,38 @@ struct DartConverterFloatingPoint {
|
||||
Dart_GetNativeDoubleArgument(args, index, &result);
|
||||
return result;
|
||||
}
|
||||
|
||||
static T FromFfi(FfiType val) { return val; }
|
||||
static FfiType ToFfi(T val) { return val; }
|
||||
static bool AllowedInLeafCall() { return kAllowedInLeafCall; }
|
||||
};
|
||||
|
||||
template <>
|
||||
struct DartConverter<float> : public DartConverterFloatingPoint<float> {};
|
||||
struct DartConverter<float> : public DartConverterFloatingPoint<float> {
|
||||
static constexpr const char* kFfiRepresentation = "Float";
|
||||
static constexpr const char* kDartRepresentation = "double";
|
||||
static const char* GetFfiRepresentation() { return kFfiRepresentation; }
|
||||
static const char* GetDartRepresentation() { return kDartRepresentation; }
|
||||
};
|
||||
|
||||
template <>
|
||||
struct DartConverter<double> : public DartConverterFloatingPoint<double> {};
|
||||
struct DartConverter<double> : public DartConverterFloatingPoint<double> {
|
||||
static constexpr const char* kFfiRepresentation = "Double";
|
||||
static constexpr const char* kDartRepresentation = "double";
|
||||
static const char* GetFfiRepresentation() { return kFfiRepresentation; }
|
||||
static const char* GetDartRepresentation() { return kDartRepresentation; }
|
||||
};
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
// Enum Classes
|
||||
|
||||
template <typename T>
|
||||
struct DartConverter<T, typename std::enable_if<std::is_enum<T>::value>::type> {
|
||||
using FfiType = int32_t;
|
||||
static constexpr const char* kFfiRepresentation = "Int32";
|
||||
static constexpr const char* kDartRepresentation = "int";
|
||||
static constexpr bool kAllowedInLeafCall = true;
|
||||
|
||||
static Dart_Handle ToDart(T val) {
|
||||
return Dart_NewInteger(
|
||||
static_cast<typename std::underlying_type<T>::type>(val));
|
||||
@@ -196,6 +281,11 @@ struct DartConverter<T, typename std::enable_if<std::is_enum<T>::value>::type> {
|
||||
Dart_GetNativeIntegerArgument(args, index, &result);
|
||||
return static_cast<T>(result);
|
||||
}
|
||||
|
||||
static T FromFfi(FfiType val) { return static_cast<T>(val); }
|
||||
static const char* GetFfiRepresentation() { return kFfiRepresentation; }
|
||||
static const char* GetDartRepresentation() { return kDartRepresentation; }
|
||||
static bool AllowedInLeafCall() { return kAllowedInLeafCall; }
|
||||
};
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
@@ -203,17 +293,22 @@ struct DartConverter<T, typename std::enable_if<std::is_enum<T>::value>::type> {
|
||||
|
||||
template <>
|
||||
struct DartConverter<std::string> {
|
||||
static Dart_Handle ToDart(const std::string& val) {
|
||||
using NativeType = std::string;
|
||||
using FfiType = Dart_Handle;
|
||||
static constexpr const char* kFfiRepresentation = "Handle";
|
||||
static constexpr const char* kDartRepresentation = "String";
|
||||
static constexpr bool kAllowedInLeafCall = false;
|
||||
|
||||
static Dart_Handle ToDart(const NativeType& val) {
|
||||
return Dart_NewStringFromUTF8(reinterpret_cast<const uint8_t*>(val.data()),
|
||||
val.length());
|
||||
}
|
||||
|
||||
static void SetReturnValue(Dart_NativeArguments args,
|
||||
const std::string& val) {
|
||||
static void SetReturnValue(Dart_NativeArguments args, const NativeType& val) {
|
||||
Dart_SetReturnValue(args, ToDart(val));
|
||||
}
|
||||
|
||||
static std::string FromDart(Dart_Handle handle) {
|
||||
static NativeType FromDart(Dart_Handle handle) {
|
||||
uint8_t* data = nullptr;
|
||||
intptr_t length = 0;
|
||||
if (Dart_IsError(Dart_StringToUTF8(handle, &data, &length)))
|
||||
@@ -221,26 +316,37 @@ struct DartConverter<std::string> {
|
||||
return std::string(reinterpret_cast<char*>(data), length);
|
||||
}
|
||||
|
||||
static std::string FromArguments(Dart_NativeArguments args,
|
||||
int index,
|
||||
Dart_Handle& exception) {
|
||||
static NativeType FromArguments(Dart_NativeArguments args,
|
||||
int index,
|
||||
Dart_Handle& exception) {
|
||||
return FromDart(Dart_GetNativeArgument(args, index));
|
||||
}
|
||||
|
||||
static NativeType FromFfi(FfiType val) { return FromDart(val); }
|
||||
static FfiType ToFfi(NativeType val) { return ToDart(val); }
|
||||
static const char* GetFfiRepresentation() { return kFfiRepresentation; }
|
||||
static const char* GetDartRepresentation() { return kDartRepresentation; }
|
||||
static bool AllowedInLeafCall() { return kAllowedInLeafCall; }
|
||||
};
|
||||
|
||||
template <>
|
||||
struct DartConverter<std::u16string> {
|
||||
static Dart_Handle ToDart(const std::u16string& val) {
|
||||
using NativeType = std::u16string;
|
||||
using FfiType = Dart_Handle;
|
||||
static constexpr const char* kFfiRepresentation = "Handle";
|
||||
static constexpr const char* kDartRepresentation = "String";
|
||||
static constexpr bool kAllowedInLeafCall = false;
|
||||
|
||||
static Dart_Handle ToDart(const NativeType& val) {
|
||||
return Dart_NewStringFromUTF16(
|
||||
reinterpret_cast<const uint16_t*>(val.data()), val.length());
|
||||
}
|
||||
|
||||
static void SetReturnValue(Dart_NativeArguments args,
|
||||
const std::u16string& val) {
|
||||
static void SetReturnValue(Dart_NativeArguments args, const NativeType& val) {
|
||||
Dart_SetReturnValue(args, ToDart(val));
|
||||
}
|
||||
|
||||
static std::u16string FromDart(Dart_Handle handle) {
|
||||
static NativeType FromDart(Dart_Handle handle) {
|
||||
intptr_t length = 0;
|
||||
Dart_StringLength(handle, &length);
|
||||
std::vector<uint16_t> data(length);
|
||||
@@ -248,11 +354,17 @@ struct DartConverter<std::u16string> {
|
||||
return std::u16string(reinterpret_cast<char16_t*>(data.data()), length);
|
||||
}
|
||||
|
||||
static std::u16string FromArguments(Dart_NativeArguments args,
|
||||
int index,
|
||||
Dart_Handle& exception) {
|
||||
static NativeType FromArguments(Dart_NativeArguments args,
|
||||
int index,
|
||||
Dart_Handle& exception) {
|
||||
return FromDart(Dart_GetNativeArgument(args, index));
|
||||
}
|
||||
|
||||
static NativeType FromFfi(FfiType val) { return FromDart(val); }
|
||||
static FfiType ToFfi(NativeType val) { return ToDart(val); }
|
||||
static const char* GetFfiRepresentation() { return kFfiRepresentation; }
|
||||
static const char* GetDartRepresentation() { return kDartRepresentation; }
|
||||
static bool AllowedInLeafCall() { return kAllowedInLeafCall; }
|
||||
};
|
||||
|
||||
template <>
|
||||
@@ -360,6 +472,11 @@ struct DartListFactory {
|
||||
|
||||
template <typename T>
|
||||
struct DartConverter<std::vector<T>> {
|
||||
using FfiType = Dart_Handle;
|
||||
static constexpr const char* kFfiRepresentation = "Handle";
|
||||
static constexpr const char* kDartRepresentation = "List";
|
||||
static constexpr bool kAllowedInLeafCall = false;
|
||||
|
||||
using ValueType = typename DartConverterTypes<T>::ValueType;
|
||||
using ConverterType = typename DartConverterTypes<T>::ConverterType;
|
||||
|
||||
@@ -413,6 +530,12 @@ struct DartConverter<std::vector<T>> {
|
||||
Dart_Handle& exception) {
|
||||
return FromDart(Dart_GetNativeArgument(args, index));
|
||||
}
|
||||
|
||||
static std::vector<ValueType> FromFfi(FfiType val) { return FromDart(val); }
|
||||
static FfiType ToFfi(std::vector<ValueType> val) { return ToDart(val); }
|
||||
static const char* GetFfiRepresentation() { return kFfiRepresentation; }
|
||||
static const char* GetDartRepresentation() { return kDartRepresentation; }
|
||||
static bool AllowedInLeafCall() { return kAllowedInLeafCall; }
|
||||
};
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
@@ -420,19 +543,31 @@ struct DartConverter<std::vector<T>> {
|
||||
|
||||
template <>
|
||||
struct DartConverter<Dart_Handle> {
|
||||
static Dart_Handle ToDart(Dart_Handle val) { return val; }
|
||||
using NativeType = Dart_Handle;
|
||||
using FfiType = Dart_Handle;
|
||||
static constexpr const char* kFfiRepresentation = "Handle";
|
||||
static constexpr const char* kDartRepresentation = "Object";
|
||||
static constexpr bool kAllowedInLeafCall = false;
|
||||
|
||||
static Dart_Handle ToDart(NativeType val) { return val; }
|
||||
|
||||
static void SetReturnValue(Dart_NativeArguments args, Dart_Handle val) {
|
||||
Dart_SetReturnValue(args, val);
|
||||
}
|
||||
|
||||
static Dart_Handle FromDart(Dart_Handle handle) { return handle; }
|
||||
static NativeType FromDart(Dart_Handle handle) { return handle; }
|
||||
|
||||
static Dart_Handle FromArguments(Dart_NativeArguments args,
|
||||
int index,
|
||||
Dart_Handle& exception) {
|
||||
static NativeType FromArguments(Dart_NativeArguments args,
|
||||
int index,
|
||||
Dart_Handle& exception) {
|
||||
return Dart_GetNativeArgument(args, index);
|
||||
}
|
||||
|
||||
static NativeType FromFfi(FfiType val) { return val; }
|
||||
static FfiType ToFfi(NativeType val) { return val; }
|
||||
static const char* GetFfiRepresentation() { return kFfiRepresentation; }
|
||||
static const char* GetDartRepresentation() { return kDartRepresentation; }
|
||||
static bool AllowedInLeafCall() { return kAllowedInLeafCall; }
|
||||
};
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
304
engine/src/flutter/third_party/tonic/dart_args.h
vendored
304
engine/src/flutter/third_party/tonic/dart_args.h
vendored
@@ -5,6 +5,8 @@
|
||||
#ifndef LIB_TONIC_DART_ARGS_H_
|
||||
#define LIB_TONIC_DART_ARGS_H_
|
||||
|
||||
#include <iostream>
|
||||
#include <sstream>
|
||||
#include <type_traits>
|
||||
#include <utility>
|
||||
|
||||
@@ -101,6 +103,8 @@ void DartReturn(T result, Dart_NativeArguments args) {
|
||||
template <typename IndicesType, typename T>
|
||||
class DartDispatcher {};
|
||||
|
||||
// Match functions on the form:
|
||||
// `void f(ArgTypes...)`
|
||||
template <size_t... indices, typename... ArgTypes>
|
||||
struct DartDispatcher<IndicesHolder<indices...>, void (*)(ArgTypes...)>
|
||||
: public DartArgHolder<indices, ArgTypes>... {
|
||||
@@ -116,6 +120,8 @@ struct DartDispatcher<IndicesHolder<indices...>, void (*)(ArgTypes...)>
|
||||
}
|
||||
};
|
||||
|
||||
// Match functions on the form:
|
||||
// `ResultType f(ArgTypes...)`
|
||||
template <size_t... indices, typename ResultType, typename... ArgTypes>
|
||||
struct DartDispatcher<IndicesHolder<indices...>, ResultType (*)(ArgTypes...)>
|
||||
: public DartArgHolder<indices, ArgTypes>... {
|
||||
@@ -137,6 +143,8 @@ struct DartDispatcher<IndicesHolder<indices...>, ResultType (*)(ArgTypes...)>
|
||||
}
|
||||
};
|
||||
|
||||
// Match instance methods on the form:
|
||||
// `void C::m(ArgTypes...)`
|
||||
template <size_t... indices, typename C, typename... ArgTypes>
|
||||
struct DartDispatcher<IndicesHolder<indices...>, void (C::*)(ArgTypes...)>
|
||||
: public DartArgHolder<indices, ArgTypes>... {
|
||||
@@ -153,6 +161,8 @@ struct DartDispatcher<IndicesHolder<indices...>, void (C::*)(ArgTypes...)>
|
||||
}
|
||||
};
|
||||
|
||||
// Match instance methods on the form:
|
||||
// `ReturnType (C::m)(ArgTypes...) const`
|
||||
template <size_t... indices,
|
||||
typename C,
|
||||
typename ReturnType,
|
||||
@@ -174,6 +184,8 @@ struct DartDispatcher<IndicesHolder<indices...>,
|
||||
}
|
||||
};
|
||||
|
||||
// Match instance methods on the form:
|
||||
// `ReturnType (C::m)(ArgTypes...)`
|
||||
template <size_t... indices,
|
||||
typename C,
|
||||
typename ResultType,
|
||||
@@ -238,6 +250,298 @@ void DartCallConstructor(Sig func, Dart_NativeArguments args) {
|
||||
wrappable->AssociateWithDartWrapper(wrapper);
|
||||
}
|
||||
|
||||
// Templates to automatically setup static entry points for FFI Native
|
||||
// functions.
|
||||
// Entry points for instance methods take the instance as the first argument and
|
||||
// call the given method with the remaining arguments.
|
||||
// Arguments will automatically get converted to and from their FFI
|
||||
// representations with the DartConverter templates.
|
||||
//
|
||||
// @tparam C The type of the receiver. Or `void` if there is no receiver.
|
||||
// @tparam Signature The signature of the function being dispatched to.
|
||||
// @tparam function The function pointer being dispatched to.
|
||||
template <typename C, typename Signature, Signature function>
|
||||
struct FfiDispatcher;
|
||||
|
||||
// Concatenate the FFI representation of each argument to the stream,
|
||||
// serialising them into a comma separated list.
|
||||
// Example: "Handle, Bool, Uint64"
|
||||
template <typename Arg, typename... Args>
|
||||
void WriteFfiArguments(std::ostringstream* stream) {
|
||||
*stream << tonic::DartConverter<typename std::remove_const<
|
||||
typename std::remove_reference<Arg>::type>::type>::GetFfiRepresentation();
|
||||
if constexpr (sizeof...(Args) > 0) {
|
||||
*stream << ", ";
|
||||
WriteFfiArguments<Args...>(stream);
|
||||
}
|
||||
}
|
||||
|
||||
// Concatenate the Dart representation of each argument to the stream,
|
||||
// serialising them into a comma separated list.
|
||||
// Example: "Object, bool, int"
|
||||
template <typename Arg, typename... Args>
|
||||
void WriteDartArguments(std::ostringstream* stream) {
|
||||
*stream << tonic::DartConverter<
|
||||
typename std::remove_const<typename std::remove_reference<Arg>::type>::
|
||||
type>::GetDartRepresentation();
|
||||
if constexpr (sizeof...(Args) > 0) {
|
||||
*stream << ", ";
|
||||
WriteDartArguments<Args...>(stream);
|
||||
}
|
||||
}
|
||||
|
||||
// Logical 'and' together whether each argument is allowed in a leaf call.
|
||||
template <typename Arg, typename... Args>
|
||||
bool AllowedInLeafCall() {
|
||||
bool result = tonic::DartConverter<typename std::remove_const<
|
||||
typename std::remove_reference<Arg>::type>::type>::AllowedInLeafCall();
|
||||
if constexpr (sizeof...(Args) > 0) {
|
||||
result &= AllowedInLeafCall<Args...>();
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
// Match `Return function(...)`.
|
||||
template <typename Return, typename... Args, Return (*function)(Args...)>
|
||||
struct FfiDispatcher<void, Return (*)(Args...), function> {
|
||||
using FfiReturn = typename DartConverter<Return>::FfiType;
|
||||
static const size_t n_args = sizeof...(Args);
|
||||
|
||||
// Static C entry-point with Dart FFI signature.
|
||||
static FfiReturn Call(
|
||||
typename DartConverter<typename std::remove_const<
|
||||
typename std::remove_reference<Args>::type>::type>::FfiType... args) {
|
||||
// Call C++ function, forwarding converted native arguments.
|
||||
return DartConverter<Return>::ToFfi(function(
|
||||
DartConverter<typename std::remove_const<typename std::remove_reference<
|
||||
Args>::type>::type>::FromFfi(args)...));
|
||||
}
|
||||
|
||||
static bool AllowedAsLeafCall() {
|
||||
if constexpr (sizeof...(Args) > 0) {
|
||||
return AllowedInLeafCall<Return>() && AllowedInLeafCall<Args...>();
|
||||
}
|
||||
return AllowedInLeafCall<Return>();
|
||||
}
|
||||
|
||||
static const char* GetReturnFfiRepresentation() {
|
||||
return tonic::DartConverter<Return>::GetFfiRepresentation();
|
||||
}
|
||||
|
||||
static const char* GetReturnDartRepresentation() {
|
||||
return tonic::DartConverter<Return>::GetDartRepresentation();
|
||||
}
|
||||
|
||||
static void WriteFfiArguments(std::ostringstream* stream) {
|
||||
if constexpr (sizeof...(Args) > 0) {
|
||||
::tonic::WriteFfiArguments<Args...>(stream);
|
||||
}
|
||||
}
|
||||
|
||||
static void WriteDartArguments(std::ostringstream* stream) {
|
||||
if constexpr (sizeof...(Args) > 0) {
|
||||
::tonic::WriteDartArguments<Args...>(stream);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
// Match `Return C::method(...)`.
|
||||
template <typename C,
|
||||
typename Return,
|
||||
typename... Args,
|
||||
Return (C::*method)(Args...)>
|
||||
struct FfiDispatcher<C, Return (C::*)(Args...), method> {
|
||||
using FfiReturn = typename DartConverter<Return>::FfiType;
|
||||
static const size_t n_args = sizeof...(Args);
|
||||
|
||||
// Static C entry-point with Dart FFI signature.
|
||||
static FfiReturn Call(
|
||||
C* receiver,
|
||||
typename DartConverter<typename std::remove_const<
|
||||
typename std::remove_reference<Args>::type>::type>::FfiType... args) {
|
||||
// Call C++ method on receiver, forwarding converted native arguments.
|
||||
return DartConverter<Return>::ToFfi((receiver->*method)(
|
||||
DartConverter<typename std::remove_const<typename std::remove_reference<
|
||||
Args>::type>::type>::FromFfi(args)...));
|
||||
}
|
||||
|
||||
static bool AllowedAsLeafCall() {
|
||||
if constexpr (sizeof...(Args) > 0) {
|
||||
return AllowedInLeafCall<Return>() && AllowedInLeafCall<Args...>();
|
||||
}
|
||||
return AllowedInLeafCall<Return>();
|
||||
}
|
||||
|
||||
static const char* GetReturnFfiRepresentation() {
|
||||
return tonic::DartConverter<Return>::GetFfiRepresentation();
|
||||
}
|
||||
|
||||
static const char* GetReturnDartRepresentation() {
|
||||
return tonic::DartConverter<Return>::GetDartRepresentation();
|
||||
}
|
||||
|
||||
static void WriteFfiArguments(std::ostringstream* stream) {
|
||||
*stream << tonic::DartConverter<C*>::GetFfiRepresentation();
|
||||
if constexpr (sizeof...(Args) > 0) {
|
||||
*stream << ", ";
|
||||
::tonic::WriteFfiArguments<Args...>(stream);
|
||||
}
|
||||
}
|
||||
|
||||
static void WriteDartArguments(std::ostringstream* stream) {
|
||||
*stream << tonic::DartConverter<C*>::GetDartRepresentation();
|
||||
if constexpr (sizeof...(Args) > 0) {
|
||||
*stream << ", ";
|
||||
::tonic::WriteDartArguments<Args...>(stream);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
// Match `Return C::method(...) const`.
|
||||
template <typename C,
|
||||
typename Return,
|
||||
typename... Args,
|
||||
Return (C::*method)(Args...) const>
|
||||
struct FfiDispatcher<C, Return (C::*)(Args...) const, method> {
|
||||
using FfiReturn = typename DartConverter<Return>::FfiType;
|
||||
static const size_t n_args = sizeof...(Args);
|
||||
|
||||
// Static C entry-point with Dart FFI signature.
|
||||
static FfiReturn Call(
|
||||
C* receiver,
|
||||
typename DartConverter<typename std::remove_const<
|
||||
typename std::remove_reference<Args>::type>::type>::FfiType... args) {
|
||||
// Call C++ method on receiver, forwarding converted native arguments.
|
||||
return DartConverter<Return>::ToFfi((receiver->*method)(
|
||||
DartConverter<typename std::remove_const<typename std::remove_reference<
|
||||
Args>::type>::type>::FromFfi(args)...));
|
||||
}
|
||||
|
||||
static bool AllowedAsLeafCall() {
|
||||
if constexpr (sizeof...(Args) > 0) {
|
||||
return AllowedInLeafCall<Return>() && AllowedInLeafCall<Args...>();
|
||||
}
|
||||
return AllowedInLeafCall<Return>();
|
||||
}
|
||||
|
||||
static const char* GetReturnFfiRepresentation() {
|
||||
return tonic::DartConverter<Return>::GetFfiRepresentation();
|
||||
}
|
||||
|
||||
static const char* GetReturnDartRepresentation() {
|
||||
return tonic::DartConverter<Return>::GetDartRepresentation();
|
||||
}
|
||||
|
||||
static void WriteFfiArguments(std::ostringstream* stream) {
|
||||
*stream << tonic::DartConverter<C*>::GetFfiRepresentation();
|
||||
if constexpr (sizeof...(Args) > 0) {
|
||||
*stream << ", ";
|
||||
::tonic::WriteFfiArguments<Args...>(stream);
|
||||
}
|
||||
}
|
||||
|
||||
static void WriteDartArguments(std::ostringstream* stream) {
|
||||
*stream << tonic::DartConverter<C*>::GetDartRepresentation();
|
||||
if constexpr (sizeof...(Args) > 0) {
|
||||
*stream << ", ";
|
||||
::tonic::WriteDartArguments<Args...>(stream);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
// `void` specialisation since we can't declare `ToFfi` to take void rvalues.
|
||||
// Match `void function(...)`.
|
||||
template <typename... Args, void (*function)(Args...)>
|
||||
struct FfiDispatcher<void, void (*)(Args...), function> {
|
||||
static const size_t n_args = sizeof...(Args);
|
||||
|
||||
// Static C entry-point with Dart FFI signature.
|
||||
static void Call(
|
||||
typename DartConverter<typename std::remove_const<
|
||||
typename std::remove_reference<Args>::type>::type>::FfiType... args) {
|
||||
// Call C++ function, forwarding converted native arguments.
|
||||
function(
|
||||
DartConverter<typename std::remove_const<typename std::remove_reference<
|
||||
Args>::type>::type>::FromFfi(args)...);
|
||||
}
|
||||
|
||||
static bool AllowedAsLeafCall() {
|
||||
if constexpr (sizeof...(Args) > 0) {
|
||||
return AllowedInLeafCall<Args...>();
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
static const char* GetReturnFfiRepresentation() {
|
||||
return tonic::DartConverter<void>::GetFfiRepresentation();
|
||||
}
|
||||
|
||||
static const char* GetReturnDartRepresentation() {
|
||||
return tonic::DartConverter<void>::GetDartRepresentation();
|
||||
}
|
||||
|
||||
static void WriteFfiArguments(std::ostringstream* stream) {
|
||||
if constexpr (sizeof...(Args) > 0) {
|
||||
::tonic::WriteFfiArguments<Args...>(stream);
|
||||
}
|
||||
}
|
||||
|
||||
static void WriteDartArguments(std::ostringstream* stream) {
|
||||
if constexpr (sizeof...(Args) > 0) {
|
||||
::tonic::WriteDartArguments<Args...>(stream);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
// `void` specialisation since we can't declare `ToFfi` to take void rvalues.
|
||||
// Match `void C::method(...)`.
|
||||
template <typename C, typename... Args, void (C::*method)(Args...)>
|
||||
struct FfiDispatcher<C, void (C::*)(Args...), method> {
|
||||
static const size_t n_args = sizeof...(Args);
|
||||
|
||||
// Static C entry-point with Dart FFI signature.
|
||||
static void Call(
|
||||
C* receiver,
|
||||
typename DartConverter<typename std::remove_const<
|
||||
typename std::remove_reference<Args>::type>::type>::FfiType... args) {
|
||||
// Call C++ method on receiver, forwarding converted native arguments.
|
||||
(receiver->*method)(
|
||||
DartConverter<typename std::remove_const<typename std::remove_reference<
|
||||
Args>::type>::type>::FromFfi(args)...);
|
||||
}
|
||||
|
||||
static bool AllowedAsLeafCall() {
|
||||
if constexpr (sizeof...(Args) > 0) {
|
||||
return AllowedInLeafCall<Args...>();
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
static const char* GetReturnFfiRepresentation() {
|
||||
return tonic::DartConverter<void>::GetFfiRepresentation();
|
||||
}
|
||||
|
||||
static const char* GetReturnDartRepresentation() {
|
||||
return tonic::DartConverter<void>::GetDartRepresentation();
|
||||
}
|
||||
|
||||
static void WriteFfiArguments(std::ostringstream* stream) {
|
||||
*stream << tonic::DartConverter<C*>::GetFfiRepresentation();
|
||||
if constexpr (sizeof...(Args) > 0) {
|
||||
*stream << ", ";
|
||||
::tonic::WriteFfiArguments<Args...>(stream);
|
||||
}
|
||||
}
|
||||
|
||||
static void WriteDartArguments(std::ostringstream* stream) {
|
||||
*stream << tonic::DartConverter<C*>::GetDartRepresentation();
|
||||
if constexpr (sizeof...(Args) > 0) {
|
||||
*stream << ", ";
|
||||
::tonic::WriteDartArguments<Args...>(stream);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
} // namespace tonic
|
||||
|
||||
#endif // LIB_TONIC_DART_ARGS_H_
|
||||
|
||||
@@ -102,6 +102,11 @@ struct DartConverter<
|
||||
T*,
|
||||
typename std::enable_if<
|
||||
std::is_convertible<T*, const DartWrappable*>::value>::type> {
|
||||
using FfiType = T*;
|
||||
static constexpr const char* kFfiRepresentation = "Pointer";
|
||||
static constexpr const char* kDartRepresentation = "Pointer";
|
||||
static constexpr bool kAllowedInLeafCall = true;
|
||||
|
||||
static Dart_Handle ToDart(DartWrappable* val) {
|
||||
if (!val) {
|
||||
return Dart_Null();
|
||||
@@ -148,6 +153,12 @@ struct DartConverter<
|
||||
return static_cast<T*>(
|
||||
DartConverterWrappable::FromArguments(args, index, exception));
|
||||
}
|
||||
|
||||
static T* FromFfi(FfiType val) { return val; }
|
||||
static FfiType ToFfi(T* val) { return val; }
|
||||
static const char* GetFfiRepresentation() { return kFfiRepresentation; }
|
||||
static const char* GetDartRepresentation() { return kDartRepresentation; }
|
||||
static bool AllowedInLeafCall() { return kAllowedInLeafCall; }
|
||||
};
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
@@ -157,27 +168,39 @@ struct DartConverter<
|
||||
|
||||
template <template <typename T> class PTR, typename T>
|
||||
struct DartConverter<PTR<T>> {
|
||||
static Dart_Handle ToDart(const PTR<T>& val) {
|
||||
using NativeType = PTR<T>;
|
||||
using FfiType = Dart_Handle;
|
||||
static constexpr const char* kFfiRepresentation = "Handle";
|
||||
static constexpr const char* kDartRepresentation = "Object";
|
||||
static constexpr bool kAllowedInLeafCall = false;
|
||||
|
||||
static Dart_Handle ToDart(const NativeType& val) {
|
||||
return DartConverter<T*>::ToDart(val.get());
|
||||
}
|
||||
|
||||
static PTR<T> FromDart(Dart_Handle handle) {
|
||||
return DartConverter<T*>::FromDart(handle);
|
||||
static NativeType FromDart(Dart_Handle handle) {
|
||||
return NativeType(DartConverter<T*>::FromDart(handle));
|
||||
}
|
||||
|
||||
static PTR<T> FromArguments(Dart_NativeArguments args,
|
||||
int index,
|
||||
Dart_Handle& exception,
|
||||
bool auto_scope = true) {
|
||||
return PTR<T>(
|
||||
static NativeType FromArguments(Dart_NativeArguments args,
|
||||
int index,
|
||||
Dart_Handle& exception,
|
||||
bool auto_scope = true) {
|
||||
return NativeType(
|
||||
DartConverter<T*>::FromArguments(args, index, exception, auto_scope));
|
||||
}
|
||||
|
||||
static void SetReturnValue(Dart_NativeArguments args,
|
||||
const PTR<T>& val,
|
||||
const NativeType& val,
|
||||
bool auto_scope = true) {
|
||||
DartConverter<T*>::SetReturnValue(args, val.get());
|
||||
}
|
||||
|
||||
static NativeType FromFfi(FfiType val) { return FromDart(val); }
|
||||
static FfiType ToFfi(const NativeType& val) { return ToDart(val); }
|
||||
static const char* GetFfiRepresentation() { return kFfiRepresentation; }
|
||||
static const char* GetDartRepresentation() { return kDartRepresentation; }
|
||||
static bool AllowedInLeafCall() { return kAllowedInLeafCall; }
|
||||
};
|
||||
|
||||
template <typename T>
|
||||
|
||||
@@ -18,6 +18,7 @@ executable("tonic_unittests") {
|
||||
"dart_persistent_handle_unittest.cc",
|
||||
"dart_state_unittest.cc",
|
||||
"dart_weak_persistent_handle_unittest.cc",
|
||||
"ffi_native_unittest.cc",
|
||||
]
|
||||
|
||||
public_deps = [
|
||||
|
||||
365
engine/src/flutter/third_party/tonic/tests/ffi_native_unittest.cc
vendored
Normal file
365
engine/src/flutter/third_party/tonic/tests/ffi_native_unittest.cc
vendored
Normal file
@@ -0,0 +1,365 @@
|
||||
// Copyright 2021 The Flutter Authors. All rights reserved.
|
||||
// Use of this source code is governed by a BSD-style license that can be
|
||||
// found in the LICENSE file.
|
||||
|
||||
#include "flutter/testing/dart_isolate_runner.h"
|
||||
#include "flutter/testing/fixture_test.h"
|
||||
|
||||
#include "tonic/dart_args.h"
|
||||
#include "tonic/dart_wrappable.h"
|
||||
|
||||
namespace flutter {
|
||||
namespace testing {
|
||||
|
||||
class MyNativeClass : public RefCountedDartWrappable<MyNativeClass> {
|
||||
DEFINE_WRAPPERTYPEINFO();
|
||||
FML_FRIEND_MAKE_REF_COUNTED(MyNativeClass);
|
||||
|
||||
MyNativeClass(intptr_t value) : _value(value){};
|
||||
|
||||
public:
|
||||
intptr_t _value = 0;
|
||||
|
||||
static void Create(Dart_Handle path_handle, intptr_t value) {
|
||||
auto path = fml::MakeRefCounted<MyNativeClass>(value);
|
||||
path->AssociateWithDartWrapper(path_handle);
|
||||
}
|
||||
|
||||
// Dummy test functions:
|
||||
|
||||
static int32_t MyTestFunction(MyNativeClass* ptr,
|
||||
int32_t x,
|
||||
Dart_Handle handle) {
|
||||
return ptr->_value + x;
|
||||
}
|
||||
|
||||
Dart_Handle MyTestMethod(int64_t x) { return Dart_NewInteger(_value + x); }
|
||||
};
|
||||
|
||||
IMPLEMENT_WRAPPERTYPEINFO("Lib", MyNativeClass)
|
||||
|
||||
class FfiNativeTest : public FixtureTest {
|
||||
public:
|
||||
FfiNativeTest()
|
||||
: settings_(CreateSettingsForFixture()),
|
||||
vm_(DartVMRef::Create(settings_)),
|
||||
thread_(CreateNewThread()),
|
||||
task_runners_(GetCurrentTestName(),
|
||||
thread_,
|
||||
thread_,
|
||||
thread_,
|
||||
thread_) {}
|
||||
|
||||
~FfiNativeTest() = default;
|
||||
|
||||
[[nodiscard]] bool RunWithEntrypoint(const std::string& entrypoint) {
|
||||
if (running_isolate_) {
|
||||
return false;
|
||||
}
|
||||
auto isolate =
|
||||
RunDartCodeInIsolate(vm_, settings_, task_runners_, entrypoint, {},
|
||||
GetDefaultKernelFilePath());
|
||||
if (!isolate || isolate->get()->GetPhase() != DartIsolate::Phase::Running) {
|
||||
return false;
|
||||
}
|
||||
|
||||
running_isolate_ = std::move(isolate);
|
||||
return true;
|
||||
}
|
||||
|
||||
template <typename C, typename Signature, Signature function>
|
||||
void DoCallThroughTest(const char* testName, const char* testEntry) {
|
||||
auto weak_persistent_value = tonic::DartWeakPersistentValue();
|
||||
|
||||
fml::AutoResetWaitableEvent event;
|
||||
|
||||
AddFfiNativeCallback(
|
||||
testName, reinterpret_cast<void*>(
|
||||
tonic::FfiDispatcher<C, Signature, function>::Call));
|
||||
|
||||
AddFfiNativeCallback(
|
||||
"CreateNative",
|
||||
reinterpret_cast<void*>(
|
||||
tonic::FfiDispatcher<void, decltype(&MyNativeClass::Create),
|
||||
&MyNativeClass::Create>::Call));
|
||||
|
||||
AddNativeCallback("SignalDone",
|
||||
CREATE_NATIVE_ENTRY([&](Dart_NativeArguments args) {
|
||||
// Clear on initiative of Dart.
|
||||
weak_persistent_value.Clear();
|
||||
|
||||
event.Signal();
|
||||
}));
|
||||
|
||||
ASSERT_TRUE(RunWithEntrypoint(testEntry));
|
||||
event.Wait();
|
||||
|
||||
running_isolate_->Shutdown();
|
||||
}
|
||||
|
||||
template <typename C, typename Signature, Signature function>
|
||||
void DoSerialiseTest(bool leaf,
|
||||
const char* returnFfi,
|
||||
const char* returnDart,
|
||||
const char* argsFfi,
|
||||
const char* argsDart) {
|
||||
auto dispatcher = tonic::FfiDispatcher<C, Signature, function>();
|
||||
|
||||
EXPECT_EQ(dispatcher.AllowedAsLeafCall(), leaf);
|
||||
EXPECT_STREQ(dispatcher.GetReturnFfiRepresentation(), returnFfi);
|
||||
EXPECT_STREQ(dispatcher.GetReturnDartRepresentation(), returnDart);
|
||||
|
||||
{
|
||||
std::ostringstream stream_;
|
||||
dispatcher.WriteFfiArguments(&stream_);
|
||||
EXPECT_STREQ(stream_.str().c_str(), argsFfi);
|
||||
}
|
||||
|
||||
{
|
||||
std::ostringstream stream_;
|
||||
dispatcher.WriteDartArguments(&stream_);
|
||||
EXPECT_STREQ(stream_.str().c_str(), argsDart);
|
||||
}
|
||||
}
|
||||
|
||||
protected:
|
||||
Settings settings_;
|
||||
DartVMRef vm_;
|
||||
std::unique_ptr<AutoIsolateShutdown> running_isolate_;
|
||||
fml::RefPtr<fml::TaskRunner> thread_;
|
||||
TaskRunners task_runners_;
|
||||
FML_DISALLOW_COPY_AND_ASSIGN(FfiNativeTest);
|
||||
};
|
||||
|
||||
//
|
||||
// Call bindings.
|
||||
//
|
||||
|
||||
// Call and serialise a simple function through the Tonic FFI bindings.
|
||||
|
||||
void Nop() {}
|
||||
|
||||
TEST_F(FfiNativeTest, FfiBindingCallNop) {
|
||||
DoCallThroughTest<void, decltype(&Nop), &Nop>("Nop", "callNop");
|
||||
}
|
||||
|
||||
TEST_F(FfiNativeTest, SerialiseNop) {
|
||||
DoSerialiseTest<void, decltype(&Nop), &Nop>(
|
||||
/*leaf=*/true, "Void", "void", "", "");
|
||||
}
|
||||
|
||||
// Call and serialise function with bool.
|
||||
|
||||
bool EchoBool(bool arg) {
|
||||
EXPECT_TRUE(arg);
|
||||
return arg;
|
||||
}
|
||||
|
||||
TEST_F(FfiNativeTest, FfiBindingCallEchoBool) {
|
||||
DoCallThroughTest<void, decltype(&EchoBool), &EchoBool>("EchoBool",
|
||||
"callEchoBool");
|
||||
}
|
||||
|
||||
TEST_F(FfiNativeTest, SerialiseEchoBool) {
|
||||
DoSerialiseTest<void, decltype(&EchoBool), &EchoBool>(
|
||||
/*leaf=*/true, "Bool", "bool", "Bool", "bool");
|
||||
}
|
||||
|
||||
// Call and serialise function with intptr_t.
|
||||
|
||||
intptr_t EchoIntPtr(intptr_t arg) {
|
||||
EXPECT_EQ(arg, 23);
|
||||
return arg;
|
||||
}
|
||||
|
||||
TEST_F(FfiNativeTest, FfiBindingCallEchoIntPtr) {
|
||||
DoCallThroughTest<void, decltype(&EchoIntPtr), &EchoIntPtr>("EchoIntPtr",
|
||||
"callEchoIntPtr");
|
||||
}
|
||||
|
||||
TEST_F(FfiNativeTest, SerialiseEchoIntPtr) {
|
||||
if (sizeof(intptr_t) == 8) {
|
||||
DoSerialiseTest<void, decltype(&EchoIntPtr), &EchoIntPtr>(
|
||||
/*leaf=*/true, "Int64", "int", "Int64", "int");
|
||||
} else {
|
||||
EXPECT_EQ(sizeof(intptr_t), 4ul);
|
||||
DoSerialiseTest<void, decltype(&EchoIntPtr), &EchoIntPtr>(
|
||||
/*leaf=*/true, "Int32", "int", "Int32", "int");
|
||||
}
|
||||
}
|
||||
|
||||
// Call and serialise function with float.
|
||||
|
||||
float EchoFloat(float arg) {
|
||||
EXPECT_EQ(arg, 23.0);
|
||||
return arg;
|
||||
}
|
||||
|
||||
TEST_F(FfiNativeTest, FfiBindingCallEchoFloat) {
|
||||
DoCallThroughTest<void, decltype(&EchoFloat), &EchoFloat>("EchoFloat",
|
||||
"callEchoFloat");
|
||||
}
|
||||
|
||||
TEST_F(FfiNativeTest, SerialiseEchoFloat) {
|
||||
DoSerialiseTest<void, decltype(&EchoFloat), &EchoFloat>(
|
||||
/*leaf=*/true, "Float", "double", "Float", "double");
|
||||
}
|
||||
|
||||
// Call and serialise function with double.
|
||||
|
||||
double EchoDouble(double arg) {
|
||||
EXPECT_EQ(arg, 23.0);
|
||||
return arg;
|
||||
}
|
||||
|
||||
TEST_F(FfiNativeTest, FfiBindingCallEchoDouble) {
|
||||
DoCallThroughTest<void, decltype(&EchoDouble), &EchoDouble>("EchoDouble",
|
||||
"callEchoDouble");
|
||||
}
|
||||
|
||||
TEST_F(FfiNativeTest, SerialiseEchoDouble) {
|
||||
DoSerialiseTest<void, decltype(&EchoDouble), &EchoDouble>(
|
||||
/*leaf=*/true, "Double", "double", "Double", "double");
|
||||
}
|
||||
|
||||
// Call and serialise function with Dart_Handle.
|
||||
|
||||
Dart_Handle EchoHandle(Dart_Handle str) {
|
||||
const char* c_str = nullptr;
|
||||
Dart_StringToCString(str, &c_str);
|
||||
EXPECT_STREQ(c_str, "Hello EchoHandle");
|
||||
return str;
|
||||
}
|
||||
|
||||
TEST_F(FfiNativeTest, FfiBindingCallEchoHandle) {
|
||||
DoCallThroughTest<void, decltype(&EchoHandle), &EchoHandle>("EchoHandle",
|
||||
"callEchoHandle");
|
||||
}
|
||||
|
||||
TEST_F(FfiNativeTest, SerialiseEchoHandle) {
|
||||
DoSerialiseTest<void, decltype(&EchoHandle), &EchoHandle>(
|
||||
/*leaf=*/false, "Handle", "Object", "Handle", "Object");
|
||||
}
|
||||
|
||||
// Call and serialise function with std::string.
|
||||
|
||||
std::string EchoString(std::string arg) {
|
||||
EXPECT_STREQ(arg.c_str(), "Hello EchoString");
|
||||
return arg;
|
||||
}
|
||||
|
||||
TEST_F(FfiNativeTest, FfiBindingCallEchoString) {
|
||||
DoCallThroughTest<void, decltype(&EchoString), &EchoString>("EchoString",
|
||||
"callEchoString");
|
||||
}
|
||||
|
||||
TEST_F(FfiNativeTest, SerialiseEchoString) {
|
||||
DoSerialiseTest<void, decltype(&EchoString), &EchoString>(
|
||||
/*leaf=*/false, "Handle", "String", "Handle", "String");
|
||||
}
|
||||
|
||||
// Call and serialise function with std::u16string.
|
||||
|
||||
std::u16string EchoU16String(std::u16string arg) {
|
||||
EXPECT_EQ(arg, u"Hello EchoU16String");
|
||||
return arg;
|
||||
}
|
||||
|
||||
TEST_F(FfiNativeTest, FfiBindingCallEchoU16String) {
|
||||
DoCallThroughTest<void, decltype(&EchoU16String), &EchoU16String>(
|
||||
"EchoU16String", "callEchoU16String");
|
||||
}
|
||||
|
||||
TEST_F(FfiNativeTest, SerialiseEchoU16String) {
|
||||
DoSerialiseTest<void, decltype(&EchoU16String), &EchoU16String>(
|
||||
/*leaf=*/false, "Handle", "String", "Handle", "String");
|
||||
}
|
||||
|
||||
// Call and serialise function with std::vector.
|
||||
|
||||
std::vector<std::string> EchoVector(const std::vector<std::string>& arg) {
|
||||
EXPECT_STREQ(arg[0].c_str(), "Hello EchoVector");
|
||||
return arg;
|
||||
}
|
||||
|
||||
TEST_F(FfiNativeTest, FfiBindingCallEchoVector) {
|
||||
DoCallThroughTest<void, decltype(&EchoVector), &EchoVector>("EchoVector",
|
||||
"callEchoVector");
|
||||
}
|
||||
|
||||
TEST_F(FfiNativeTest, SerialiseEchoVector) {
|
||||
DoSerialiseTest<void, decltype(&EchoVector), &EchoVector>(
|
||||
/*leaf=*/false, "Handle", "List", "Handle", "List");
|
||||
}
|
||||
|
||||
// Call and serialise function with DartWrappable.
|
||||
|
||||
intptr_t EchoWrappable(MyNativeClass* arg) {
|
||||
EXPECT_EQ(arg->_value, 0x1234);
|
||||
return arg->_value;
|
||||
}
|
||||
|
||||
TEST_F(FfiNativeTest, FfiBindingCallEchoWrappable) {
|
||||
DoCallThroughTest<void, decltype(&EchoWrappable), &EchoWrappable>(
|
||||
"EchoWrappable", "callEchoWrappable");
|
||||
}
|
||||
|
||||
TEST_F(FfiNativeTest, SerialiseEchoWrappable) {
|
||||
if (sizeof(intptr_t) == 8) {
|
||||
DoSerialiseTest<void, decltype(&EchoWrappable), &EchoWrappable>(
|
||||
/*leaf=*/true, "Int64", "int", "Pointer", "Pointer");
|
||||
} else {
|
||||
EXPECT_EQ(sizeof(intptr_t), 4ul);
|
||||
DoSerialiseTest<void, decltype(&EchoWrappable), &EchoWrappable>(
|
||||
/*leaf=*/true, "Int32", "int", "Pointer", "Pointer");
|
||||
}
|
||||
}
|
||||
|
||||
// Call and serialise function with TypedList<..>.
|
||||
|
||||
tonic::Float32List EchoTypedList(tonic::Float32List arg) {
|
||||
EXPECT_NEAR(arg[1], 3.14, 0.01);
|
||||
return arg;
|
||||
}
|
||||
|
||||
TEST_F(FfiNativeTest, FfiBindingCallEchoTypedList) {
|
||||
DoCallThroughTest<void, decltype(&EchoTypedList), &EchoTypedList>(
|
||||
"EchoTypedList", "callEchoTypedList");
|
||||
}
|
||||
|
||||
TEST_F(FfiNativeTest, SerialiseEchoTypedList) {
|
||||
DoSerialiseTest<void, decltype(&EchoTypedList), &EchoTypedList>(
|
||||
/*leaf=*/false, "Handle", "Object", "Handle", "Object");
|
||||
}
|
||||
|
||||
// Call and serialise a static class member function.
|
||||
|
||||
TEST_F(FfiNativeTest, FfiBindingCallClassMemberFunction) {
|
||||
DoCallThroughTest<void, decltype(&MyNativeClass::MyTestFunction),
|
||||
&MyNativeClass::MyTestFunction>(
|
||||
"MyNativeClass::MyTestFunction", "callMyTestFunction");
|
||||
}
|
||||
|
||||
TEST_F(FfiNativeTest, SerialiseClassMemberFunction) {
|
||||
DoSerialiseTest<void, decltype(&MyNativeClass::MyTestFunction),
|
||||
&MyNativeClass::MyTestFunction>(
|
||||
/*leaf=*/false, "Int32", "int", "Pointer, Int32, Handle",
|
||||
"Pointer, int, Object");
|
||||
}
|
||||
|
||||
// Call and serialise an instance method.
|
||||
|
||||
TEST_F(FfiNativeTest, FfiBindingCallClassMemberMethod) {
|
||||
DoCallThroughTest<MyNativeClass, decltype(&MyNativeClass::MyTestMethod),
|
||||
&MyNativeClass::MyTestMethod>("MyNativeClass::MyTestMethod",
|
||||
"callMyTestMethod");
|
||||
}
|
||||
|
||||
TEST_F(FfiNativeTest, SerialiseClassMemberMethod) {
|
||||
DoSerialiseTest<MyNativeClass, decltype(&MyNativeClass::MyTestMethod),
|
||||
&MyNativeClass::MyTestMethod>(
|
||||
/*leaf=*/false, "Handle", "Object", "Pointer, Int64", "Pointer, int");
|
||||
}
|
||||
|
||||
} // namespace testing
|
||||
} // namespace flutter
|
||||
@@ -2,6 +2,10 @@
|
||||
// Use of this source code is governed by a BSD-style license that can be
|
||||
// found in the LICENSE file.
|
||||
|
||||
import 'dart:ffi';
|
||||
import 'dart:nativewrappers';
|
||||
import 'dart:typed_data';
|
||||
|
||||
void main() {}
|
||||
|
||||
class SomeClass {
|
||||
@@ -23,3 +27,173 @@ void testClearLater() {
|
||||
giveObjectToNative(SomeClass(123));
|
||||
signalDone();
|
||||
}
|
||||
|
||||
// Test helpers for simple void calls through Tonic.
|
||||
|
||||
@FfiNative<Void Function()>('Nop', isLeaf: true)
|
||||
external void nop();
|
||||
|
||||
@pragma('vm:entry-point')
|
||||
void callNop() {
|
||||
nop();
|
||||
signalDone();
|
||||
}
|
||||
|
||||
// Test helpers for calls with bool through Tonic.
|
||||
|
||||
@FfiNative<Bool Function(Bool)>('EchoBool')
|
||||
external bool echoBool(bool arg);
|
||||
|
||||
@pragma('vm:entry-point')
|
||||
void callEchoBool() {
|
||||
if (echoBool(true)) {
|
||||
signalDone();
|
||||
}
|
||||
}
|
||||
|
||||
// Test helpers for calls with int through Tonic.
|
||||
|
||||
@FfiNative<IntPtr Function(IntPtr)>('EchoIntPtr')
|
||||
external int echoIntPtr(int arg);
|
||||
|
||||
@pragma('vm:entry-point')
|
||||
void callEchoIntPtr() {
|
||||
if (echoIntPtr(23) == 23) {
|
||||
signalDone();
|
||||
}
|
||||
}
|
||||
|
||||
// Test helpers for calls with float through Tonic.
|
||||
|
||||
@FfiNative<Float Function(Float)>('EchoFloat')
|
||||
external double echoFloat(double arg);
|
||||
|
||||
@pragma('vm:entry-point')
|
||||
void callEchoFloat() {
|
||||
if (echoFloat(23.0) == 23.0) {
|
||||
signalDone();
|
||||
}
|
||||
}
|
||||
|
||||
// Test helpers for calls with double through Tonic.
|
||||
|
||||
@FfiNative<Double Function(Double)>('EchoDouble')
|
||||
external double echoDouble(double arg);
|
||||
|
||||
@pragma('vm:entry-point')
|
||||
void callEchoDouble() {
|
||||
if (echoDouble(23.0) == 23.0) {
|
||||
signalDone();
|
||||
}
|
||||
}
|
||||
|
||||
// Test helpers for calls with Dart_Handle through Tonic.
|
||||
|
||||
@FfiNative<Handle Function(Handle)>('EchoHandle')
|
||||
external Object echoHandle(Object arg);
|
||||
|
||||
@pragma('vm:entry-point')
|
||||
void callEchoHandle() {
|
||||
if (echoHandle('Hello EchoHandle') == 'Hello EchoHandle') {
|
||||
signalDone();
|
||||
}
|
||||
}
|
||||
|
||||
// Test helpers for calls with std::string through Tonic.
|
||||
|
||||
@FfiNative<Handle Function(Handle)>('EchoString')
|
||||
external String echoString(String arg);
|
||||
|
||||
@pragma('vm:entry-point')
|
||||
void callEchoString() {
|
||||
if (echoString('Hello EchoString') == 'Hello EchoString') {
|
||||
signalDone();
|
||||
}
|
||||
}
|
||||
|
||||
// Test helpers for calls with std::u16string through Tonic.
|
||||
|
||||
@FfiNative<Handle Function(Handle)>('EchoU16String')
|
||||
external String echoU16String(String arg);
|
||||
|
||||
@pragma('vm:entry-point')
|
||||
void callEchoU16String() {
|
||||
if (echoU16String('Hello EchoU16String') == 'Hello EchoU16String') {
|
||||
signalDone();
|
||||
}
|
||||
}
|
||||
|
||||
// Test helpers for calls with std::vector through Tonic.
|
||||
|
||||
@FfiNative<Handle Function(Handle)>('EchoVector')
|
||||
external List<String> echoVector(List<String> arg);
|
||||
|
||||
@pragma('vm:entry-point')
|
||||
void callEchoVector() {
|
||||
if (echoVector(['Hello EchoVector'])[0] == 'Hello EchoVector') {
|
||||
signalDone();
|
||||
}
|
||||
}
|
||||
|
||||
// Test helpers for calls with DartWrappable through Tonic.
|
||||
|
||||
class MyNativeClass extends NativeFieldWrapperClass1 {
|
||||
MyNativeClass(int value) {
|
||||
_Create(this, value);
|
||||
}
|
||||
|
||||
@FfiNative<Void Function(Handle, IntPtr)>('CreateNative')
|
||||
external static void _Create(MyNativeClass self, int value);
|
||||
|
||||
@FfiNative<Int32 Function(Pointer<Void>, Int32, Handle)>(
|
||||
'MyNativeClass::MyTestFunction')
|
||||
external static int myTestFunction(MyNativeClass self, int x, Object handle);
|
||||
|
||||
@FfiNative<Handle Function(Pointer<Void>, Int64)>(
|
||||
'MyNativeClass::MyTestMethod')
|
||||
external Object myTestMethod(int a);
|
||||
}
|
||||
|
||||
@FfiNative<IntPtr Function(Pointer<Void>)>('EchoWrappable')
|
||||
external int echoWrappable(MyNativeClass arg);
|
||||
|
||||
@pragma('vm:entry-point')
|
||||
void callEchoWrappable() {
|
||||
final myNative = MyNativeClass(0x1234);
|
||||
if (echoWrappable(myNative) == 0x1234) {
|
||||
signalDone();
|
||||
}
|
||||
}
|
||||
|
||||
// Test helpers for calls with TypedList<..> through Tonic.
|
||||
|
||||
@FfiNative<Handle Function(Handle)>('EchoTypedList')
|
||||
external Float32List echoTypedList(Float32List arg);
|
||||
|
||||
@pragma('vm:entry-point')
|
||||
void callEchoTypedList() {
|
||||
final typedList = Float32List.fromList([99.9, 3.14, 0.01]);
|
||||
if (echoTypedList(typedList) == typedList) {
|
||||
signalDone();
|
||||
}
|
||||
}
|
||||
|
||||
//
|
||||
|
||||
@pragma('vm:entry-point')
|
||||
void callMyTestFunction() {
|
||||
final myNative = MyNativeClass(1234);
|
||||
if (MyNativeClass.myTestFunction(myNative, 34, myNative) == 1268) {
|
||||
signalDone();
|
||||
}
|
||||
}
|
||||
|
||||
//
|
||||
|
||||
@pragma('vm:entry-point')
|
||||
void callMyTestMethod() {
|
||||
final myNative = MyNativeClass(1234);
|
||||
if (myNative.myTestMethod(43) == 1277) {
|
||||
signalDone();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -51,12 +51,27 @@ class TypedList {
|
||||
|
||||
template <Dart_TypedData_Type kTypeName, typename ElemType>
|
||||
struct DartConverter<TypedList<kTypeName, ElemType>> {
|
||||
static void SetReturnValue(Dart_NativeArguments args,
|
||||
TypedList<kTypeName, ElemType> val);
|
||||
static TypedList<kTypeName, ElemType> FromArguments(Dart_NativeArguments args,
|
||||
int index,
|
||||
Dart_Handle& exception);
|
||||
using NativeType = TypedList<kTypeName, ElemType>;
|
||||
using FfiType = Dart_Handle;
|
||||
static constexpr const char* kFfiRepresentation = "Handle";
|
||||
static constexpr const char* kDartRepresentation = "Object";
|
||||
static constexpr bool kAllowedInLeafCall = false;
|
||||
|
||||
static void SetReturnValue(Dart_NativeArguments args, NativeType val);
|
||||
static NativeType FromArguments(Dart_NativeArguments args,
|
||||
int index,
|
||||
Dart_Handle& exception);
|
||||
static Dart_Handle ToDart(const ElemType* buffer, unsigned int length);
|
||||
|
||||
static NativeType FromFfi(FfiType val) { return NativeType(val); }
|
||||
static FfiType ToFfi(NativeType val) {
|
||||
Dart_Handle handle = val.dart_handle();
|
||||
val.Release();
|
||||
return handle;
|
||||
}
|
||||
static const char* GetFfiRepresentation() { return kFfiRepresentation; }
|
||||
static const char* GetDartRepresentation() { return kDartRepresentation; }
|
||||
static bool AllowedInLeafCall() { return kAllowedInLeafCall; }
|
||||
};
|
||||
|
||||
#define TONIC_TYPED_DATA_FOREACH(F) \
|
||||
|
||||
Reference in New Issue
Block a user