-
Notifications
You must be signed in to change notification settings - Fork 1.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
'Pointer<NativeFunction<T Function()>> ptr; T Function() func = ptr.asFunction(); ' When T must be 'Pointer' to pass compilation checking. #54467
Comments
Using Try this: void main() {
final isAlive =
Pointer<NativeFunction<Pointer<Void> Function()>>.fromAddress(0x100000);
final isAliveFunc = isAlive.asFunction<Pointer Function()>();
final isAlive1 =
Pointer<NativeFunction<Void Function()>>.fromAddress(0x100000);
final isAliveFunc1 = isAlive1.asFunction<void Function()>();
final testObject = Pointer<TestResult>.fromAddress(0x100000);
final func = testObject.ref.dispose
.asFunction<Pointer Function(Pointer<TestResult>)>();
final func1 =
testObject.ref.dispose1.asFunction<void Function(Pointer<TestResult>)>();
} I skipped specifying types in variable declarations to reduce verbosity. Simply use |
cc @dcharkes We should consider upgrading error messages to make them more novice friendly. I really wish we have a simpler way to managing Dart and C signatures, maybe making primitive types to be extension types could work: extension type const Int32._(int _) {
}
extension type Pointer<T>._(Address _) {
} Then if you have a function type Maybe we should file a bug to explore FFI 2.0 APIs which don't require writing two signatures. |
I've filed an issue with some links. Also, there was some old (internal) design doc for @SilverFruity were you able to get it working? My suggestion would be to use |
My current struct TestList {
void *(*add)(struct TestList *, void *element);
void *(*dispose)(struct TestList *);
}; Pointer<Void> Function(Pointer<TestList>) add = list.ref.add.asFunction();
add(list, null);
Pointer<Void> Function(Pointer<TestList>) dispose = list.ref.dispose.asFunction();
dispose(list); |
I'm not entirely sure what your code is trying to do. Are you using dynamic dispatch on purpose? Storing the function pointer in the struct TestList {
};
void add(struct TestList *, void *element);
void dispose(struct TestList *); In which case you wouldn't have to use I assume you want to return void. If you want dynamic dispatch and return void, your C and Dart code should probably look like this: struct TestList {
void (*add)(struct TestList *, void *element);
void (*dispose)(struct TestList *);
}; void Function(Pointer<TestList>) add = list.ref.add.asFunction();
add(list, null);
void Function(Pointer<TestList>) dispose = list.ref.dispose.asFunction();
dispose(list); |
Yes, I'm storing the function pointer in a TestList struct for dynamic dispatch, and then want to be able to call it directly using |
Test on dartpad
Test on The latest main branch
Executing this code with
dart
compiled from the latest commit on themain
branch of the github repository has the same problem, as follows:The text was updated successfully, but these errors were encountered: