[ffigen] Handle function types in functions/typedefs as function pointers (#103)
diff --git a/pkgs/ffigen/CHANGELOG.md b/pkgs/ffigen/CHANGELOG.md index c04cf39..2c2b156 100644 --- a/pkgs/ffigen/CHANGELOG.md +++ b/pkgs/ffigen/CHANGELOG.md
@@ -1,3 +1,6 @@ +# 1.0.1 +- Fixed generation of `NativeFunction` parameters instead of `Pointer<NativeFunction>` in type signatures. + # 1.0.0 - Bump version to 1.0.0. - Handle unimplememnted function pointers causing errors.
diff --git a/pkgs/ffigen/lib/src/code_generator/func.dart b/pkgs/ffigen/lib/src/code_generator/func.dart index 6498ff7..a55f6d2 100644 --- a/pkgs/ffigen/lib/src/code_generator/func.dart +++ b/pkgs/ffigen/lib/src/code_generator/func.dart
@@ -156,12 +156,17 @@ } } -/// Represents a Function's parameter. +/// Represents a Parameter, used in [Func] and [Typedef]. class Parameter { final String originalName; String name; final Type type; - Parameter({String originalName, this.name = '', @required this.type}) - : originalName = originalName ?? name; + Parameter({String originalName, this.name = '', @required Type type}) + : originalName = originalName ?? name, + // A type with broadtype [BroadType.NativeFunction] is wrapped with a + // pointer because this is a shorthand used in C for Pointer to function. + type = type.broadType == BroadType.NativeFunction + ? Type.pointer(type) + : type; }
diff --git a/pkgs/ffigen/pubspec.yaml b/pkgs/ffigen/pubspec.yaml index 36302e6..fa95439 100644 --- a/pkgs/ffigen/pubspec.yaml +++ b/pkgs/ffigen/pubspec.yaml
@@ -3,7 +3,7 @@ # BSD-style license that can be found in the LICENSE file. name: ffigen -version: 1.0.0 +version: 1.0.1 homepage: https://github.com/dart-lang/ffigen description: Experimental generator for FFI bindings, using LibClang to parse C header files.
diff --git a/pkgs/ffigen/test/header_parser_tests/functions.h b/pkgs/ffigen/test/header_parser_tests/functions.h index 306ff0f..5c00b33 100644 --- a/pkgs/ffigen/test/header_parser_tests/functions.h +++ b/pkgs/ffigen/test/header_parser_tests/functions.h
@@ -11,3 +11,8 @@ // Tests with pointers to primitives. void *func4(int8_t **, double, int32_t ***); + +// Would be treated as `typedef void shortHand(void (*b)())`. +typedef void shortHand(void(b)()); +// Would be treated as `void func5(shortHand *a, void (*b)())`. +void func5(shortHand a, void(b)());
diff --git a/pkgs/ffigen/test/header_parser_tests/functions_test.dart b/pkgs/ffigen/test/header_parser_tests/functions_test.dart index aba98e6..2c898bc 100644 --- a/pkgs/ffigen/test/header_parser_tests/functions_test.dart +++ b/pkgs/ffigen/test/header_parser_tests/functions_test.dart
@@ -53,6 +53,11 @@ expect(actual.getBindingAsString('func4'), expected.getBindingAsString('func4')); }); + + test('func5', () { + expect(actual.getBindingAsString('func5'), + expected.getBindingAsString('func5')); + }); }); } @@ -122,8 +127,36 @@ Parameter( type: Type.pointer(Type.pointer( Type.pointer(Type.nativeType(SupportedNativeType.Int32)))), - ) + ), ]), + Func( + name: 'func5', + returnType: Type.nativeType(SupportedNativeType.Void), + parameters: [ + Parameter( + name: 'a', + type: Type.pointer(Type.nativeFunc(Typedef( + name: 'shortHand', + returnType: Type.nativeType(SupportedNativeType.Void), + typedefType: TypedefType.C, + parameters: [ + Parameter( + type: Type.pointer(Type.nativeFunc(Typedef( + name: 'b', + returnType: Type.nativeType(SupportedNativeType.Void), + typedefType: TypedefType.C, + )))), + ], + )))), + Parameter( + name: 'b', + type: Type.pointer(Type.nativeFunc(Typedef( + name: '_typedefC_2', + returnType: Type.nativeType(SupportedNativeType.Void), + typedefType: TypedefType.C, + )))), + ], + ), ], ); }