diff --git a/CHANGELOG.md b/CHANGELOG.md index c17a0a6a..d8d6976a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,7 @@ +# 6.0.0 +- Removed config `dart-bool`. Booleans are now always generated with `bool` +and `ffi.Bool` as it's Dart and C Type respectively. + # 5.0.1 - Add a the xcode tools llvm as default path on MacOS. diff --git a/lib/src/code_generator/func.dart b/lib/src/code_generator/func.dart index e0eb0cb6..c10b0c8d 100644 --- a/lib/src/code_generator/func.dart +++ b/lib/src/code_generator/func.dart @@ -98,39 +98,18 @@ class Func extends LookUpBinding { p.name = paramNamer.makeUnique(p.name); } // Write enclosing function. - if (w.dartBool && functionType.returnType.typealiasType is BooleanType) { - // Use bool return type in enclosing function. - s.write('bool $enclosingFuncName(\n'); - } else { - s.write( - '${functionType.returnType.getDartType(w)} $enclosingFuncName(\n'); - } + s.write('${functionType.returnType.getDartType(w)} $enclosingFuncName(\n'); for (final p in functionType.parameters) { - if (w.dartBool && p.type.typealiasType is BooleanType) { - // Use bool parameter type in enclosing function. - s.write(' bool ${p.name},\n'); - } else { - s.write(' ${p.type.getDartType(w)} ${p.name},\n'); - } + s.write(' ${p.type.getDartType(w)} ${p.name},\n'); } s.write(') {\n'); s.write('return $funcVarName'); s.write('(\n'); for (final p in functionType.parameters) { - if (w.dartBool && p.type.typealiasType is BooleanType) { - // Convert bool parameter to int before calling. - s.write(' ${p.name}?1:0,\n'); - } else { - s.write(' ${p.name},\n'); - } - } - if (w.dartBool && functionType.returnType.typealiasType is BooleanType) { - // Convert int return type to bool. - s.write(' )!=0;\n'); - } else { - s.write(' );\n'); + s.write(' ${p.name},\n'); } + s.write(' );\n'); s.write('}\n'); final cType = exposeFunctionTypedefs diff --git a/lib/src/code_generator/library.dart b/lib/src/code_generator/library.dart index b88a565c..a651871f 100644 --- a/lib/src/code_generator/library.dart +++ b/lib/src/code_generator/library.dart @@ -29,7 +29,6 @@ class Library { String? description, required List bindings, String? header, - bool dartBool = true, bool sort = false, StructPackingOverride? packingOverride, Set? libraryImports, @@ -75,7 +74,6 @@ class Library { className: name, classDocComment: description, header: header, - dartBool: dartBool, additionalImports: libraryImports, ); } diff --git a/lib/src/code_generator/native_type.dart b/lib/src/code_generator/native_type.dart index e05d170f..4be4b16f 100644 --- a/lib/src/code_generator/native_type.dart +++ b/lib/src/code_generator/native_type.dart @@ -66,7 +66,7 @@ class NativeType extends Type { class BooleanType extends NativeType { // Booleans are treated as uint8. - const BooleanType._() : super._('Uint8', 'int', '0'); + const BooleanType._() : super._('Bool', 'bool', 'false'); static const _boolean = BooleanType._(); factory BooleanType() => _boolean; diff --git a/lib/src/code_generator/writer.dart b/lib/src/code_generator/writer.dart index 1e398fc7..77e9e834 100644 --- a/lib/src/code_generator/writer.dart +++ b/lib/src/code_generator/writer.dart @@ -60,8 +60,6 @@ class Writer { late String _symbolAddressVariableName; late String _symbolAddressLibraryVarName; - final bool dartBool; - /// Initial namers set after running constructor. Namers are reset to this /// initial state everytime [generate] is called. late UniqueNamer _initialTopLevelUniqueNamer, _initialWrapperLevelUniqueNamer; @@ -82,7 +80,6 @@ class Writer { required this.lookUpBindings, required this.noLookUpBindings, required String className, - required this.dartBool, Set? additionalImports, this.classDocComment, this.header, diff --git a/lib/src/config_provider/config.dart b/lib/src/config_provider/config.dart index 0891c70e..e9d1f97d 100644 --- a/lib/src/config_provider/config.dart +++ b/lib/src/config_provider/config.dart @@ -119,10 +119,6 @@ class Config { StructPackingOverride get structPackingOverride => _structPackingOverride; late StructPackingOverride _structPackingOverride; - /// If dart bool should be generated for C booleans. - bool get dartBool => _dartBool; - late bool _dartBool; - /// Name of the wrapper class. String get wrapperName => _wrapperName; late String _wrapperName; @@ -437,13 +433,6 @@ class Config { extractedResult: (dynamic result) => _structPackingOverride = result as StructPackingOverride, ), - [strings.dartBool]: Specification( - requirement: Requirement.no, - validator: booleanValidator, - extractor: booleanExtractor, - defaultValue: () => true, - extractedResult: (dynamic result) => _dartBool = result as bool, - ), [strings.name]: Specification( requirement: Requirement.prefer, validator: dartClassNameValidator, diff --git a/lib/src/header_parser/parser.dart b/lib/src/header_parser/parser.dart index b400d847..2adbb0bf 100644 --- a/lib/src/header_parser/parser.dart +++ b/lib/src/header_parser/parser.dart @@ -28,7 +28,6 @@ Library parse(Config c) { name: config.wrapperName, description: config.wrapperDocComment, header: config.preamble, - dartBool: config.dartBool, sort: config.sort, packingOverride: config.structPackingOverride, libraryImports: c.libraryImports.values.toSet(), diff --git a/lib/src/strings.dart b/lib/src/strings.dart index 7f0f0281..81862219 100644 --- a/lib/src/strings.dart +++ b/lib/src/strings.dart @@ -167,7 +167,6 @@ const supportedNativeType_mappings = { // Boolean flags. const sort = 'sort'; const useSupportedTypedefs = 'use-supported-typedefs'; -const dartBool = 'dart-bool'; const useDartHandle = 'use-dart-handle'; const comments = 'comments'; diff --git a/pubspec.yaml b/pubspec.yaml index d249b7b6..51050c22 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -3,7 +3,7 @@ # BSD-style license that can be found in the LICENSE file. name: ffigen -version: 5.0.1 +version: 6.0.0 description: Generator for FFI bindings, using LibClang to parse C header files. repository: https://github.com/dart-lang/ffigen diff --git a/test/code_generator_tests/code_generator_test.dart b/test/code_generator_tests/code_generator_test.dart index 5ef4980b..046ee8fa 100644 --- a/test/code_generator_tests/code_generator_test.dart +++ b/test/code_generator_tests/code_generator_test.dart @@ -333,7 +333,6 @@ void main() { test('boolean_dartBool', () { final library = Library( name: 'Bindings', - dartBool: true, bindings: [ Func( name: 'test1', @@ -353,29 +352,6 @@ void main() { ); _matchLib(library, 'boolean_dartbool'); }); - test('boolean_no_dartBool', () { - final library = Library( - name: 'Bindings', - dartBool: false, - bindings: [ - Func( - name: 'test1', - returnType: BooleanType(), - parameters: [ - Parameter(name: 'a', type: BooleanType()), - Parameter(name: 'b', type: PointerType(BooleanType())), - ], - ), - Struct( - name: 'Test2', - members: [ - Member(name: 'a', type: BooleanType()), - ], - ), - ], - ); - _matchLib(library, 'boolean_no_dartbool'); - }); test('sort bindings', () { final library = Library( name: 'Bindings', diff --git a/test/code_generator_tests/expected_bindings/_expected_boolean_dartbool_bindings.dart b/test/code_generator_tests/expected_bindings/_expected_boolean_dartbool_bindings.dart index 2e941212..188f9721 100644 --- a/test/code_generator_tests/expected_bindings/_expected_boolean_dartbool_bindings.dart +++ b/test/code_generator_tests/expected_bindings/_expected_boolean_dartbool_bindings.dart @@ -19,23 +19,22 @@ class Bindings { bool test1( bool a, - ffi.Pointer b, + ffi.Pointer b, ) { return _test1( - a ? 1 : 0, - b, - ) != - 0; + a, + b, + ); } late final _test1Ptr = _lookup< ffi.NativeFunction< - ffi.Uint8 Function(ffi.Uint8, ffi.Pointer)>>('test1'); + ffi.Bool Function(ffi.Bool, ffi.Pointer)>>('test1'); late final _test1 = - _test1Ptr.asFunction)>(); + _test1Ptr.asFunction)>(); } class Test2 extends ffi.Struct { - @ffi.Uint8() - external int a; + @ffi.Bool() + external bool a; } diff --git a/test/code_generator_tests/expected_bindings/_expected_boolean_no_dartbool_bindings.dart b/test/code_generator_tests/expected_bindings/_expected_boolean_no_dartbool_bindings.dart deleted file mode 100644 index c44ddddb..00000000 --- a/test/code_generator_tests/expected_bindings/_expected_boolean_no_dartbool_bindings.dart +++ /dev/null @@ -1,40 +0,0 @@ -// AUTO GENERATED FILE, DO NOT EDIT. -// -// Generated by `package:ffigen`. -import 'dart:ffi' as ffi; - -class Bindings { - /// Holds the symbol lookup function. - final ffi.Pointer Function(String symbolName) - _lookup; - - /// The symbols are looked up in [dynamicLibrary]. - Bindings(ffi.DynamicLibrary dynamicLibrary) : _lookup = dynamicLibrary.lookup; - - /// The symbols are looked up with [lookup]. - Bindings.fromLookup( - ffi.Pointer Function(String symbolName) - lookup) - : _lookup = lookup; - - int test1( - int a, - ffi.Pointer b, - ) { - return _test1( - a, - b, - ); - } - - late final _test1Ptr = _lookup< - ffi.NativeFunction< - ffi.Uint8 Function(ffi.Uint8, ffi.Pointer)>>('test1'); - late final _test1 = - _test1Ptr.asFunction)>(); -} - -class Test2 extends ffi.Struct { - @ffi.Uint8() - external int a; -} diff --git a/test/header_parser_tests/expected_bindings/_expected_typedef_bindings.dart b/test/header_parser_tests/expected_bindings/_expected_typedef_bindings.dart index 3c8e4c35..546f1ce0 100644 --- a/test/header_parser_tests/expected_bindings/_expected_typedef_bindings.dart +++ b/test/header_parser_tests/expected_bindings/_expected_typedef_bindings.dart @@ -69,19 +69,18 @@ class Bindings { late final _func3 = _func3Ptr.asFunction(); bool func4( - ffi.Pointer a, + ffi.Pointer a, ) { return _func4( - a, - ) != - 0; + a, + ); } late final _func4Ptr = - _lookup)>>( + _lookup)>>( 'func4'); late final _func4 = - _func4Ptr.asFunction)>(); + _func4Ptr.asFunction)>(); } class Struct1 extends ffi.Struct { @@ -115,6 +114,6 @@ typedef NestingASpecifiedType = ffi.IntPtr; class Struct2 extends ffi.Opaque {} class WithBoolAlias extends ffi.Struct { - @ffi.Uint8() - external int b; + @ffi.Bool() + external bool b; } diff --git a/test/native_test/native_test_bindings.dart b/test/native_test/native_test_bindings.dart index 8ca13eff..6fd5a6a4 100644 --- a/test/native_test/native_test_bindings.dart +++ b/test/native_test/native_test_bindings.dart @@ -25,15 +25,14 @@ class NativeLibrary { bool x, ) { return _Function1Bool( - x ? 1 : 0, - ) != - 0; + x, + ); } late final _Function1BoolPtr = - _lookup>( - 'Function1Bool'); - late final _Function1Bool = _Function1BoolPtr.asFunction(); + _lookup>('Function1Bool'); + late final _Function1Bool = + _Function1BoolPtr.asFunction(); int Function1Uint8( int x,