Removed config `dart-bool`. Booleans are now always generated with `bool` (#391)
and `ffi.Bool` as it's Dart and C Type respectively.
diff --git a/CHANGELOG.md b/CHANGELOG.md
index c17a0a6..d8d6976 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 e0eb0cb..c10b0c8 100644
--- a/lib/src/code_generator/func.dart
+++ b/lib/src/code_generator/func.dart
@@ -98,39 +98,18 @@
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');
- }
+ 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(' );\n');
s.write('}\n');
final cType = exposeFunctionTypedefs
diff --git a/lib/src/code_generator/library.dart b/lib/src/code_generator/library.dart
index b88a565..a651871 100644
--- a/lib/src/code_generator/library.dart
+++ b/lib/src/code_generator/library.dart
@@ -29,7 +29,6 @@
String? description,
required List<Binding> bindings,
String? header,
- bool dartBool = true,
bool sort = false,
StructPackingOverride? packingOverride,
Set<LibraryImport>? libraryImports,
@@ -75,7 +74,6 @@
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 e05d170..4be4b16 100644
--- a/lib/src/code_generator/native_type.dart
+++ b/lib/src/code_generator/native_type.dart
@@ -66,7 +66,7 @@
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 1e398fc..77e9e83 100644
--- a/lib/src/code_generator/writer.dart
+++ b/lib/src/code_generator/writer.dart
@@ -60,8 +60,6 @@
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 @@
required this.lookUpBindings,
required this.noLookUpBindings,
required String className,
- required this.dartBool,
Set<LibraryImport>? additionalImports,
this.classDocComment,
this.header,
diff --git a/lib/src/config_provider/config.dart b/lib/src/config_provider/config.dart
index 0891c70..e9d1f97 100644
--- a/lib/src/config_provider/config.dart
+++ b/lib/src/config_provider/config.dart
@@ -119,10 +119,6 @@
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 @@
extractedResult: (dynamic result) =>
_structPackingOverride = result as StructPackingOverride,
),
- [strings.dartBool]: Specification<bool>(
- requirement: Requirement.no,
- validator: booleanValidator,
- extractor: booleanExtractor,
- defaultValue: () => true,
- extractedResult: (dynamic result) => _dartBool = result as bool,
- ),
[strings.name]: Specification<String>(
requirement: Requirement.prefer,
validator: dartClassNameValidator,
diff --git a/lib/src/header_parser/parser.dart b/lib/src/header_parser/parser.dart
index b400d84..2adbb0b 100644
--- a/lib/src/header_parser/parser.dart
+++ b/lib/src/header_parser/parser.dart
@@ -28,7 +28,6 @@
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 7f0f028..8186221 100644
--- a/lib/src/strings.dart
+++ b/lib/src/strings.dart
@@ -167,7 +167,6 @@
// 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 d249b7b..51050c2 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 5ef4980..046ee8f 100644
--- a/test/code_generator_tests/code_generator_test.dart
+++ b/test/code_generator_tests/code_generator_test.dart
@@ -333,7 +333,6 @@
test('boolean_dartBool', () {
final library = Library(
name: 'Bindings',
- dartBool: true,
bindings: [
Func(
name: 'test1',
@@ -353,29 +352,6 @@
);
_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 2e94121..188f972 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 @@
bool test1(
bool a,
- ffi.Pointer<ffi.Uint8> b,
+ ffi.Pointer<ffi.Bool> b,
) {
return _test1(
- a ? 1 : 0,
- b,
- ) !=
- 0;
+ a,
+ b,
+ );
}
late final _test1Ptr = _lookup<
ffi.NativeFunction<
- ffi.Uint8 Function(ffi.Uint8, ffi.Pointer<ffi.Uint8>)>>('test1');
+ ffi.Bool Function(ffi.Bool, ffi.Pointer<ffi.Bool>)>>('test1');
late final _test1 =
- _test1Ptr.asFunction<int Function(int, ffi.Pointer<ffi.Uint8>)>();
+ _test1Ptr.asFunction<bool Function(bool, ffi.Pointer<ffi.Bool>)>();
}
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 c44dddd..0000000
--- 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<T> Function<T extends ffi.NativeType>(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<T> Function<T extends ffi.NativeType>(String symbolName)
- lookup)
- : _lookup = lookup;
-
- int test1(
- int a,
- ffi.Pointer<ffi.Uint8> b,
- ) {
- return _test1(
- a,
- b,
- );
- }
-
- late final _test1Ptr = _lookup<
- ffi.NativeFunction<
- ffi.Uint8 Function(ffi.Uint8, ffi.Pointer<ffi.Uint8>)>>('test1');
- late final _test1 =
- _test1Ptr.asFunction<int Function(int, ffi.Pointer<ffi.Uint8>)>();
-}
-
-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 3c8e4c3..546f1ce 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 @@
late final _func3 = _func3Ptr.asFunction<void Function(int, int)>();
bool func4(
- ffi.Pointer<ffi.Uint8> a,
+ ffi.Pointer<ffi.Bool> a,
) {
return _func4(
- a,
- ) !=
- 0;
+ a,
+ );
}
late final _func4Ptr =
- _lookup<ffi.NativeFunction<ffi.Uint8 Function(ffi.Pointer<ffi.Uint8>)>>(
+ _lookup<ffi.NativeFunction<ffi.Bool Function(ffi.Pointer<ffi.Bool>)>>(
'func4');
late final _func4 =
- _func4Ptr.asFunction<int Function(ffi.Pointer<ffi.Uint8>)>();
+ _func4Ptr.asFunction<bool Function(ffi.Pointer<ffi.Bool>)>();
}
class Struct1 extends ffi.Struct {
@@ -115,6 +114,6 @@
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 8ca13ef..6fd5a6a 100644
--- a/test/native_test/native_test_bindings.dart
+++ b/test/native_test/native_test_bindings.dart
@@ -25,15 +25,14 @@
bool x,
) {
return _Function1Bool(
- x ? 1 : 0,
- ) !=
- 0;
+ x,
+ );
}
late final _Function1BoolPtr =
- _lookup<ffi.NativeFunction<ffi.Uint8 Function(ffi.Uint8)>>(
- 'Function1Bool');
- late final _Function1Bool = _Function1BoolPtr.asFunction<int Function(int)>();
+ _lookup<ffi.NativeFunction<ffi.Bool Function(ffi.Bool)>>('Function1Bool');
+ late final _Function1Bool =
+ _Function1BoolPtr.asFunction<bool Function(bool)>();
int Function1Uint8(
int x,