Skip to content

Commit 249d665

Browse files
authored
Fixes and tests for listener block native bindings (#1250)
* Fixes and tests for listener block native bindings * Rename entryPoints field to nativeEntryPoints.
1 parent bafe220 commit 249d665

File tree

14 files changed

+218
-57
lines changed

14 files changed

+218
-57
lines changed

pkgs/ffigen/lib/src/code_generator/compound.dart

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,11 @@ abstract class Compound extends BindingType {
3636

3737
ObjCBuiltInFunctions? objCBuiltInFunctions;
3838

39+
/// The way the native type is written in C source code. This isn't always the
40+
/// same as the originalName, because the type may need to be prefixed with
41+
/// `struct` or `union`, depending on whether the declaration is a typedef.
42+
final String nativeType;
43+
3944
Compound({
4045
super.usr,
4146
super.originalName,
@@ -47,7 +52,9 @@ abstract class Compound extends BindingType {
4752
List<Member>? members,
4853
super.isInternal,
4954
this.objCBuiltInFunctions,
50-
}) : members = members ?? [];
55+
String? nativeType,
56+
}) : members = members ?? [],
57+
nativeType = nativeType ?? originalName ?? name;
5158

5259
factory Compound.fromType({
5360
required CompoundType type,
@@ -59,6 +66,7 @@ abstract class Compound extends BindingType {
5966
String? dartDoc,
6067
List<Member>? members,
6168
ObjCBuiltInFunctions? objCBuiltInFunctions,
69+
String? nativeType,
6270
}) {
6371
switch (type) {
6472
case CompoundType.struct:
@@ -71,6 +79,7 @@ abstract class Compound extends BindingType {
7179
dartDoc: dartDoc,
7280
members: members,
7381
objCBuiltInFunctions: objCBuiltInFunctions,
82+
nativeType: nativeType,
7483
);
7584
case CompoundType.union:
7685
return Union(
@@ -82,6 +91,7 @@ abstract class Compound extends BindingType {
8291
dartDoc: dartDoc,
8392
members: members,
8493
objCBuiltInFunctions: objCBuiltInFunctions,
94+
nativeType: nativeType,
8595
);
8696
}
8797
}
@@ -170,7 +180,7 @@ abstract class Compound extends BindingType {
170180
String getCType(Writer w) => _isBuiltIn ? '${w.objcPkgPrefix}.$name' : name;
171181

172182
@override
173-
String getNativeType({String varName = ''}) => '$originalName $varName';
183+
String getNativeType({String varName = ''}) => '$nativeType $varName';
174184

175185
@override
176186
bool get sameFfiDartAndCType => true;

pkgs/ffigen/lib/src/code_generator/library.dart

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ class Library {
3333
StructPackingOverride? packingOverride,
3434
Set<LibraryImport>? libraryImports,
3535
bool silenceEnumWarning = false,
36+
List<String> nativeEntryPoints = const <String>[],
3637
}) {
3738
_findBindings(bindings, sort);
3839

@@ -86,6 +87,7 @@ class Library {
8687
additionalImports: libraryImports,
8788
generateForPackageObjectiveC: generateForPackageObjectiveC,
8889
silenceEnumWarning: silenceEnumWarning,
90+
nativeEntryPoints: nativeEntryPoints,
8991
);
9092
}
9193

@@ -141,7 +143,7 @@ class Library {
141143
///
142144
/// Returns whether bindings were generated.
143145
bool generateObjCFile(File file) {
144-
final bindings = writer.generateObjC();
146+
final bindings = writer.generateObjC(file.path);
145147

146148
if (bindings == null) {
147149
// No ObjC code needed. If there's already a file (eg from an earlier

pkgs/ffigen/lib/src/code_generator/objc_block.dart

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -243,6 +243,7 @@ pointer.ref.invoke.cast<$natTrampFnType>().asFunction<$trampFuncFfiDartType>()(
243243

244244
final s = StringBuffer();
245245
s.write('''
246+
246247
typedef ${getNativeType(varName: blockTypedef)};
247248
$blockTypedef $fnName($blockTypedef block) {
248249
$blockTypedef wrapper = [^void(${argsReceived.join(', ')}) {

pkgs/ffigen/lib/src/code_generator/objc_nullable.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ class ObjCNullable extends Type {
4040

4141
@override
4242
String getNativeType({String varName = ''}) =>
43-
'${child.getNativeType(varName: varName)} _Nullable';
43+
'${child.getNativeType()} _Nullable $varName';
4444

4545
@override
4646
bool get sameFfiDartAndCType => child.sameFfiDartAndCType;

pkgs/ffigen/lib/src/code_generator/struct.dart

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,5 +39,6 @@ class Struct extends Compound {
3939
super.members,
4040
super.isInternal,
4141
super.objCBuiltInFunctions,
42+
super.nativeType,
4243
}) : super(compoundType: CompoundType.struct);
4344
}

pkgs/ffigen/lib/src/code_generator/union.dart

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,5 +37,6 @@ class Union extends Compound {
3737
super.dartDoc,
3838
super.members,
3939
super.objCBuiltInFunctions,
40+
super.nativeType,
4041
}) : super(compoundType: CompoundType.union);
4142
}

pkgs/ffigen/lib/src/code_generator/writer.dart

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import 'package:ffigen/src/code_generator.dart';
66
import 'package:ffigen/src/code_generator/utils.dart';
77
import 'package:logging/logging.dart';
8+
import 'package:path/path.dart' as p;
89

910
import '../strings.dart' as strings;
1011

@@ -36,6 +37,8 @@ class Writer {
3637

3738
final bool generateForPackageObjectiveC;
3839

40+
final List<String> nativeEntryPoints;
41+
3942
/// Tracks where enumType.getCType is called. Reset everytime [generate] is
4043
/// called.
4144
bool usedEnumCType = false;
@@ -132,6 +135,7 @@ class Writer {
132135
this.header,
133136
required this.generateForPackageObjectiveC,
134137
required this.silenceEnumWarning,
138+
required this.nativeEntryPoints,
135139
}) {
136140
final globalLevelNameSet = noLookUpBindings.map((e) => e.name).toSet();
137141
final wrapperLevelNameSet = lookUpBindings.map((e) => e.name).toSet();
@@ -387,13 +391,23 @@ class Writer {
387391
}
388392

389393
/// Writes the Objective C code needed for the bindings, if any. Returns null
390-
/// if there are no bindings that need generated ObjC code.
391-
String? generateObjC() {
394+
/// if there are no bindings that need generated ObjC code. This function does
395+
/// not generate the output file, but the [outFilename] does affect the
396+
/// generated code.
397+
String? generateObjC(String outFilename) {
398+
final outDir = p.dirname(outFilename);
399+
392400
final s = StringBuffer();
393401
s.write('''
394402
#include <stdint.h>
395403
396404
''');
405+
406+
for (final entryPoint in nativeEntryPoints) {
407+
final path = p.relative(entryPoint, from: outDir);
408+
s.write('#include "$path"\n');
409+
}
410+
397411
bool empty = true;
398412
for (final binding in _allBindings) {
399413
final bindingString = binding.toObjCBindingString(this);

pkgs/ffigen/lib/src/config_provider/spec_utils.dart

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,9 @@ String _replaceSeparators(String path) {
3535
String _normalizePath(String path, String? configFilename) {
3636
final skipNormalization =
3737
(configFilename == null) || p.isAbsolute(path) || path.startsWith("**");
38-
return _replaceSeparators(
39-
skipNormalization ? path : p.join(p.dirname(configFilename), path));
38+
return _replaceSeparators(skipNormalization
39+
? path
40+
: p.absolute(p.join(p.dirname(configFilename), path)));
4041
}
4142

4243
Map<String, LibraryImport> libraryImportsExtractor(

pkgs/ffigen/lib/src/header_parser/parser.dart

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ Library parse(Config c) {
3434
packingOverride: c.structPackingOverride,
3535
libraryImports: c.libraryImports.values.toSet(),
3636
silenceEnumWarning: c.silenceEnumWarning,
37+
nativeEntryPoints: c.headers.entryPoints,
3738
);
3839

3940
return library;

pkgs/ffigen/lib/src/header_parser/sub_parsers/compounddecl_parser.dart

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,7 @@ Compound? parseCompoundDeclaration(
124124
usr: declUsr,
125125
dartDoc: getCursorDocComment(cursor),
126126
objCBuiltInFunctions: objCBuiltInFunctions,
127+
nativeType: cursor.type().spelling(),
127128
);
128129
} else {
129130
_logger.finest('unnamed $className declaration');
@@ -139,6 +140,7 @@ Compound? parseCompoundDeclaration(
139140
name: configDecl.renameUsingConfig(declName),
140141
dartDoc: getCursorDocComment(cursor),
141142
objCBuiltInFunctions: objCBuiltInFunctions,
143+
nativeType: cursor.type().spelling(),
142144
);
143145
}
144146
return null;

0 commit comments

Comments
 (0)