Skip to content
Closed

ignore #10232

Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
44 commits
Select commit Hold shift + click to select a range
eb7cac6
methods, basic types, sync, nullable-sync, async, and classes
tarrinneal Apr 15, 2025
9fa695d
broken nullable async methods
tarrinneal Apr 15, 2025
0aff0fd
refactor type logic, add codec
tarrinneal Apr 17, 2025
1ed8a8b
objects and enums
tarrinneal Apr 22, 2025
ee50607
full codec
tarrinneal May 9, 2025
4c6eb1a
error handling and more tests
tarrinneal May 29, 2025
f87efd4
fix codec
tarrinneal May 29, 2025
3e9d9f0
flutter api and plumbing through legacy apis and tests
tarrinneal Jun 27, 2025
6ad2e37
jnigen.yaml
tarrinneal Jun 27, 2025
9b55d44
ffi just getting started
tarrinneal Jul 7, 2025
25e8c30
gen fluff
tarrinneal Jul 11, 2025
df40332
not working
tarrinneal Jul 16, 2025
d90ecca
missing files
tarrinneal Jul 17, 2025
92933f8
gitignore
tarrinneal Jul 17, 2025
cb1c0fe
ios ffigen settings
tarrinneal Jul 17, 2025
a7cc7ce
remove macos min version from ffigen config
tarrinneal Jul 17, 2025
8fb8dca
no dylibs
tarrinneal Jul 17, 2025
ab64302
more basic, still can't see class
tarrinneal Jul 17, 2025
c7d00c6
upload macos
tarrinneal Jul 17, 2025
33f8fa1
fix ffigen files
tarrinneal Jul 17, 2025
4977cc2
test
tarrinneal Jul 29, 2025
586edf2
wip
tarrinneal Jul 29, 2025
74b1cb1
Merge branch 'Frillback' of github.com:tarrinneal/packages into Frill…
tarrinneal Jul 29, 2025
3736528
ns
tarrinneal Jul 29, 2025
31d182f
dunno
tarrinneal Jul 30, 2025
c63e598
something
tarrinneal Jul 30, 2025
6dc23e7
working again
tarrinneal Jul 30, 2025
982dfe6
Merge branch 'Frillback' of https://github.com/tarrinneal/packages in…
tarrinneal Jul 30, 2025
ecc088a
enums
tarrinneal Aug 12, 2025
927fd8d
and this one
tarrinneal Aug 12, 2025
877d60d
ffi support for classes, enums, and objects (partial testing)
tarrinneal Aug 26, 2025
910ac95
tests
tarrinneal Aug 26, 2025
b522d6c
fix enums, still need to resolve Any?
tarrinneal Sep 4, 2025
bd47b34
actually fix enums
tarrinneal Sep 5, 2025
4192a89
starting nullables, gonna be a mess
tarrinneal Sep 5, 2025
86e6907
nullable base types in classes
tarrinneal Sep 17, 2025
87f4f8a
add nullable enums (no tests for null things though.....)
tarrinneal Sep 18, 2025
ed3eb93
update to latest version
tarrinneal Sep 19, 2025
457edef
Not even I understand
tarrinneal Sep 24, 2025
9214479
fix null object?
tarrinneal Sep 25, 2025
aadd859
multi-arity and nullable enums in classes
tarrinneal Sep 26, 2025
8cb24b6
nested classes
tarrinneal Sep 27, 2025
d29e86a
typed lists and maps except int keys
tarrinneal Oct 14, 2025
b32d506
Merge branch 'main' of https://github.com/flutter/packages into Frill…
tarrinneal Oct 15, 2025
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
Prev Previous commit
Next Next commit
ffi support for classes, enums, and objects (partial testing)
  • Loading branch information
tarrinneal committed Aug 26, 2025
commit 877d60da78a1ebe18c6ab7b7a7dd5b74ff0ea33e
187 changes: 135 additions & 52 deletions packages/pigeon/lib/src/dart/dart_generator.dart
Original file line number Diff line number Diff line change
Expand Up @@ -170,27 +170,27 @@ class _FfiType {
// case 'Float64List':
// return 'NSDoubleArray';
case 'Object':
return 'NSObject';
return 'ObjCObjectBase';
case 'List':
return 'NSMutableArray';
case 'Map':
return 'NSDictionary';
default:
{
if (type.isClass) {
if (type.isClass || type.isEnum) {
return 'ffi_bridge.${type.baseName}';
}
if (type.isEnum) {
return 'NSNumber';
}
// if (type.isEnum) {
// return type.isNullable ? 'NSNumber' : 'ffi_bridge.${type.baseName}';
// }
return 'There is something wrong, a type is not classified';
}
}
}

String get ffiTypeGetter {
return type.isNullable ? 'nullableType' : 'type';
}
// String get ffiTypeGetter {
// return type.isNullable ? 'nullableType' : 'type';
// }

String get fullFfiName {
if (type.baseName == 'List' || type.baseName == 'Map') {
Expand All @@ -199,23 +199,23 @@ class _FfiType {
return ffiName;
}

String get fullFfiType {
if (type.baseName == 'List' || type.baseName == 'Map') {
return '$ffiName.$ffiTypeGetter($getFfiCollectionTypeTypes)';
}
return '$ffiName.$ffiTypeGetter';
}
// String get fullFfiType {
// if (type.baseName == 'List' || type.baseName == 'Map') {
// return '$ffiName.$ffiTypeGetter($getFfiCollectionTypeTypes)';
// }
// return '$ffiName.$ffiTypeGetter';
// }

String get getFfiCollectionTypeTypes {
if (type.baseName == 'List') {
return subTypeOne?.fullFfiType ?? 'NSObject.nullableType';
}
if (type.baseName == 'Map') {
return '${subTypeOne?.fullFfiType ?? 'NSObject.type'}, ${subTypeTwo?.fullFfiType ?? 'NSObject.nullableType'}';
}
// String get getFfiCollectionTypeTypes {
// if (type.baseName == 'List') {
// return subTypeOne?.fullFfiType ?? 'NSObject.nullableType';
// }
// if (type.baseName == 'Map') {
// return '${subTypeOne?.fullFfiType ?? 'NSObject.type'}, ${subTypeTwo?.fullFfiType ?? 'NSObject.nullableType'}';
// }

return '$ffiName.$ffiTypeGetter';
}
// return '$ffiName.$ffiTypeGetter';
// }

bool get nonNullableNeedsUnwrapping {
if (type.isClass ||
Expand Down Expand Up @@ -254,9 +254,12 @@ class _FfiType {
bool forceConversion = false,
bool classField = false,
}) {
if (type.isClass || type.isEnum) {
if (type.isClass) {
return '${type.baseName}.fromFfi($varName)${_getForceNonNullSymbol(!type.isNullable)}';
}
if (type.isEnum && classField) {
return '${type.baseName}.values[$varName.index]';
}
String asType = ' as ${getDartReturnType(true)}';
String castCall = '';
final String codecCall =
Expand Down Expand Up @@ -307,7 +310,10 @@ class _FfiType {
);
}
if (type.isEnum) {
return 'NSNumber.alloc().initWithLong($name)';
// if (type.isNullable) {
// return 'NSNumber.alloc().initWithLong($name)';
// }
return 'ffi_bridge.${type.baseName}.values[$name]';
}
if (!type.isNullable &&
classField &&
Expand All @@ -320,6 +326,9 @@ class _FfiType {
}

String getFfiCallReturnType(bool forceUnwrap, {bool forceNullable = false}) {
if (type.isEnum && forceNullable) {
return 'NSNumber?';
}
return '$ffiName$ffiCollectionTypeAnnotations${_getNullableSymbol(forceNullable || type.isNullable)}';
}

Expand Down Expand Up @@ -850,7 +859,23 @@ class DartGenerator extends StructuredGenerator<InternalDartOptions> {

indent.newln();
indent.writeScoped(
'static ${anEnum.name}? fromFfi(${ffiType.ffiName}? ffiEnum) {',
'static ${anEnum.name} fromFfi(${ffiType.ffiName} ffiEnum) {',
'}',
() {
indent.writeln(
'return ${anEnum.name}.values[ffiEnum.index];',
);
},
);

indent.newln();
indent.writeScoped('NSNumber toNSNumber() {', '}', () {
indent.writeln('return NSNumber.alloc().initWithLong(index);');
});

indent.newln();
indent.writeScoped(
'static ${anEnum.name}? fromNSNumber(NSNumber? ffiEnum) {',
'}',
() {
indent.writeln(
Expand Down Expand Up @@ -1002,15 +1027,15 @@ class DartGenerator extends StructuredGenerator<InternalDartOptions> {
classDefinition,
);
indent.writeScoped(
'return ffi_bridge.${classDefinition.name}().initWith${toUpperCamelCase(fields.first.name)}(',
'return ffi_bridge.${classDefinition.name}.alloc().initWith${toUpperCamelCase(fields.first.name)}(',
');', () {
bool first = true;
for (final NamedType field in fields) {
final _FfiType ffiType = _FfiType.fromTypeDeclaration(field.type);
indent.writeln(
'${first ? '' : '${field.name}: '}${ffiType.getToFfiCall(
field.type,
field.name,
'${field.name}${ffiType.type.isEnum ? '.index' : ''}',
ffiType,
forceNonNull: true,
classField: true,
Expand Down Expand Up @@ -1701,8 +1726,14 @@ class DartGenerator extends StructuredGenerator<InternalDartOptions> {
}, addTrailingNewline: false);
indent.addScoped(' else {', '}', () {
if (!returnType.type.isVoid) {
final String forceRes = returnType.type.baseName == 'int' ||
returnType.type.baseName == 'double' ||
returnType.type.baseName == 'String' ||
returnType.type.baseName == 'bool'
? '!'
: '';
indent.writeln(
'final ${returnType.getDartReturnType(method.isAsynchronous)} dartTypeRes = ${returnType.getToDartCall(method.returnType, varName: 'res!', forceConversion: method.isAsynchronous)};',
'final ${returnType.getDartReturnType(method.isAsynchronous)} dartTypeRes = ${returnType.getToDartCall(method.returnType, varName: 'res$forceRes', forceConversion: method.isAsynchronous)};',
);
indent.writeln('return dartTypeRes;');
} else {
Expand Down Expand Up @@ -2341,6 +2372,7 @@ ${api.name}({
}
if (generatorOptions.useFfi) {
_writeFfiCodec(indent, root);
_writeConvertNSNumberWrapper(indent, root);
}

indent.writeln('bool isType<T>(Type t) => T == t;');
Expand Down Expand Up @@ -2583,14 +2615,63 @@ class _PigeonJniCodec {
''');
}

void _writeConvertNSNumberWrapper(Indent indent, Root root) {
indent.newln();
int typeNum = 4;
indent.format('''
Object? convertNSNumberWrapperToDart(ffi_bridge.NSNumberWrapper value) {
switch (value.type) {
case 1:
return value.number.longValue;
case 2:
return value.number.doubleValue;
case 3:
return value.number.boolValue;''');
indent.inc(2);
for (final Enum anEnum in root.enums) {
indent.format('''
case ${typeNum++}:
return ${anEnum.name}.fromNSNumber(value.number);''');
indent.dec(2);
indent.format('''
default:
throw ArgumentError.value(value);
}
}
''');
typeNum = 4;
indent.format('''
ffi_bridge.NSNumberWrapper convertNSNumberWrapperToFfi(Object value) {
switch (value) {
case int _:
return ffi_bridge.NSNumberWrapper.alloc().initWithNumber(NSNumber.alloc().initWithLong(value), type: 1);
case double _:
return ffi_bridge.NSNumberWrapper.alloc().initWithNumber(NSNumber.alloc().initWithDouble(value), type: 2);
case bool _:
return ffi_bridge.NSNumberWrapper.alloc().initWithNumber(NSNumber.alloc().initWithLong(value ? 1 : 0), type: 3);''');
for (final Enum anEnum in root.enums) {
indent.format('''
case ${anEnum.name} _:
return ffi_bridge.NSNumberWrapper.alloc().initWithNumber(value.toNSNumber(), type: ${typeNum++});''');
indent.format('''
default:
throw ArgumentError.value(value);
}
}
''');
}
}
}

void _writeFfiCodec(Indent indent, Root root) {
indent.newln();
indent.format('''
class _PigeonFfiCodec {
static Object? readValue(NSObject? value, [Type? outType]) {
static Object? readValue(ObjCObjectBase? value, [Type? outType]) {
if (value == null) {
return null;
} else if (value is NSNumber) {
} else if (NSNumber.isInstance(value)) {
value as NSNumber;
switch (outType) {
case const (int):
return value.longValue;
Expand All @@ -2601,8 +2682,8 @@ class _PigeonFfiCodec {
default:
throw ArgumentError.value(value);
}
} else if (value is NSString) {
return value.toDartString();
} else if (NSString.isInstance(value)) {
return (NSString.castFrom(value)).toDartString();
// } else if (value.isA<NSByteArray>(NSByteArray.type)) {
// final Uint8List list = Uint8List(value.as(NSByteArray.type).length);
// for (int i = 0; i < value.as(NSByteArray.type).length; i++) {
Expand Down Expand Up @@ -2630,7 +2711,7 @@ class _PigeonFfiCodec {
} else if (value is NSMutableArray) {
final List<Object?> res = <Object?>[];
for (int i = 0; i < value.length; i++) {
res.add(readValue(value[i] as NSObject?));
res.add(readValue(value[i] as NSObject?));
}
return res;
} else if (value is NSDictionary) {
Expand All @@ -2639,35 +2720,44 @@ class _PigeonFfiCodec {
res[readValue(entry.key as NSObject?)] = readValue(entry.value as NSObject?);
}
return res;
} else if (ffi_bridge.NSNumberWrapper.isInstance(value)) {
return convertNSNumberWrapperToDart(ffi_bridge.NSNumberWrapper.castFrom(value));
${root.classes.map((Class dataClass) {
final _FfiType ffiType = _FfiType.fromClass(dataClass);
return '''
} else if (value is ${ffiType.ffiName}) {
return ${ffiType.type.baseName}.fromFfi(value);
} else if (${ffiType.ffiName}.isInstance(value)) {
return ${ffiType.type.baseName}.fromFfi(value as ${ffiType.ffiName});
''';
}).join()}
${root.enums.map((Enum enumDefinition) {
final _FfiType ffiType = _FfiType.fromEnum(enumDefinition);
return '''
} else if (value is ${ffiType.ffiName}) {
return ${ffiType.type.baseName}.fromFfi(value);
return ${ffiType.type.baseName}.fromFfi(value as ${ffiType.ffiName});
''';
}).join()}
} else {
throw ArgumentError.value(value);
}
}

static T writeValue<T extends NSObject?>(Object? value) {
static T writeValue<T extends ObjCObjectBase?>(Object? value) {
if (value == null) {
return null as T;
} else if (value is bool) {
return NSNumber.alloc().initWithBool(value) as T;
} else if (value is double) {
return NSNumber.alloc().initWithDouble(value) as T;
// ignore: avoid_double_and_int_checks
} else if (value is int) {
return NSNumber.alloc().initWithLong(value) as T;
} else if (value is bool || value is double || value is int || value is Enum) {
if (value is bool) {
return NSNumber.alloc().initWithLong(value ? 1 : 0) as T;
}
if (value is double) {
return NSNumber.alloc().initWithDouble(value) as T;
}
if (value is int) {
return NSNumber.alloc().initWithLong(value) as T;
}
if (value is Enum) {
return NSNumber.alloc().initWithLong(value.index) as T;
}
return convertNSNumberWrapperToFfi(value) as T;
} else if (value is String) {
return NSString(value) as T;
// } else if (isTypeOrNullableType<NSByteArray>(T)) {
Expand Down Expand Up @@ -2717,13 +2807,6 @@ class _PigeonFfiCodec {
return value.toFfi() as T;
''';
}).join()}
${root.enums.map((Enum enumDefinition) {
final _FfiType ffiType = _FfiType.fromEnum(enumDefinition);
return '''
} else if (value is ${ffiType.type.baseName}) {
return value.toFfi() as T;
''';
}).join()}
} else {
throw ArgumentError.value(value);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ import 'package:swiftgen/swiftgen.dart';
indent.writeScoped('Future<void> main() async {', '}', () {
indent.writeScoped('final List<String> classes = <String>[', '];', () {
indent.inc();
indent.writeln("'NSNumberWrapper',");
for (final Api api in root.apis) {
if (api is AstHostApi || api is AstFlutterApi) {
indent.writeln("'${api.name}',");
Expand Down
Loading