-
Notifications
You must be signed in to change notification settings - Fork 50
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
ObjC: Unnecessarily abstract types generated for some typedefs #235
Comments
Looking into this, and it's a bit of a rabbit hole. I'm going to have to do a series of related PRs that fixes this, #308, #473, and #496. All these problems boil down to the difference between the Dart type passed through FFI ( As an example of why it's more complicated than just changing the typedef: typedef NSString *NSErrorDomain;
struct SomeStruct {
NSErrorDomain error_domain;
}; Would generate this Dart: // This is the typedef we want.
typedef NSErrorDomain = NSString;
class SomeStruct extends Struct {
// But that typedef is invalid here, because we're trying to use
// the Dart wrapper object as a struct field.
external NSErrorDomain error_domain;
} Instead we have to generate getters and setters: typedef NSErrorDomain = NSString;
class SomeStruct extends Struct {
// Need a private field with the correct type.
external Pointer<ObjCObject> _error_domain;
// And getters and setters that handle conversion. Due to the
// lib arg, the getter can't be a real getter. Probably should
// make the setter not real too for symmetry.
NSErrorDomain get_error_domain(NativeLibrary lib) =>
NSErrorDomain._(_error_domain, lib, retain: true, release: true);
void set_error_domain(NSErrorDomain value) => _error_domain = value._id;
} Currently ffigen's The fact that these 2 different concepts are represented by the same So the way to fix this is to split these 2 concepts. As well as |
Thanks for the write-up @liamappelbe! It makes total sense to me that we need two Dart types. In JNIgen this has been the case from the very start. A Dart class representing a Java class which has a Pointer to a handle inside. Rather than bolting it on, lets make it a more central concept. I've left some of my ideas on how to do that on the PR. |
@dcharkes @mannprerak2 What's |
// Get function name with first letter in upper case.
final upperCaseName = name[0].toUpperCase() + name.substring(1);
if (exposeFunctionTypedefs) {
_exposedCFunctionTypealias = Typealias(
name: 'Native$upperCaseName',
type: functionType,
isInternal: true,
);
_exposedDartFunctionTypealias = Typealias(
name: 'Dart$upperCaseName',
type: functionType,
useDartType: true,
isInternal: true,
); It's a bool that determines whether we generate the C or Dart type. |
For function types I get it. We want one version that's the Dart function, and one that's the C function. But then there's also the constructor in |
It would maybe be cleaner to make it an enum now that we have three types (and possibly even make it required). If I try the change that you suggest we actually get dart types inside typedef Typedef1 = ffi.Pointer<ffi.NativeFunction<Typedef1_function>>;
typedef Typedef1_function = void Function(Object); |
Side note the doc comment on the param |
As far as I can remember, TypeAlias would only generate C type definitions. |
Ok, it sounds like this bug is working as intended then. Typedefs always define the C type, and the C type of Specifically, if we're typedeffing a type that has a different user facing type ( |
My understanding is that these C typedefs are useful when dealing with callbacks with just function pointers. Lets add a |
* WIP fix #386 * Fix tests * fmt * Update test expectations * Update test expectations * Fix analysis * fmt * Fix more tests * WIP refactor * Refactor `sameFfiDartAndCType` to not require a `Writer` * Fix tests * Fix analysis * Update test expectations * Format * Fix typedef_test.dart * Fix nits * Fix tests * Fmt * Regenerate more test files * Regen more tests * Remove accidental commit
For example, this Objective-C code:
Generates this code:
A more natural typedef would be:
The text was updated successfully, but these errors were encountered: