From 473746a016986fcc96ec5bf711b9ffff6cd553d6 Mon Sep 17 00:00:00 2001 From: Michael Debertol Date: Thu, 14 Dec 2023 00:01:06 +0100 Subject: [PATCH 1/4] [ffigen] fix syntax for leaf ffiNative functions Fixes a missing comma in front of isLeaf: true, which is only needed for ffiNatives. --- pkgs/ffigen/lib/src/code_generator/func.dart | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/pkgs/ffigen/lib/src/code_generator/func.dart b/pkgs/ffigen/lib/src/code_generator/func.dart index 766cc289ee..43f17eb2ba 100644 --- a/pkgs/ffigen/lib/src/code_generator/func.dart +++ b/pkgs/ffigen/lib/src/code_generator/func.dart @@ -111,7 +111,6 @@ class Func extends LookUpBinding { functionType.getFfiDartType(w, writeArgumentNames: false); final needsWrapper = !functionType.sameDartAndFfiDartType && !isInternal; - final isLeafString = isLeaf ? 'isLeaf:true' : ''; final funcVarName = w.wrapperLevelUniqueNamer.makeUnique('_$name'); final ffiReturnType = functionType.returnType.getFfiDartType(w); final ffiArgDeclString = functionType.dartTypeParameters @@ -149,6 +148,7 @@ class Func extends LookUpBinding { final assetString = ffiNativeConfig.assetId != null ? ", assetId: '${ffiNativeConfig.assetId}'" : ''; + final isLeafString = isLeaf ? ', isLeaf:true' : ''; final nativeFuncName = needsWrapper ? funcVarName : enclosingFuncName; s.write(''' @${w.ffiLibraryPrefix}.Native<$cType>(symbol: '$originalName'$assetString$isLeafString) @@ -166,6 +166,7 @@ $dartReturnType $enclosingFuncName($libArg$dartArgDeclString) => $funcImplCall; } } else { funcPointerName = w.wrapperLevelUniqueNamer.makeUnique('_${name}Ptr'); + final isLeafString = isLeaf ? 'isLeaf:true' : ''; // Write enclosing function. s.write(''' From 43de373205194cea57bcbee5d0d908de7c1861c2 Mon Sep 17 00:00:00 2001 From: Michael Debertol Date: Thu, 14 Dec 2023 00:01:32 +0100 Subject: [PATCH 2/4] test ffiNative function binding generation --- .../code_generator_test.dart | 17 ++++++++-- ..._expected_function_ffiNative_bindings.dart | 31 +++++++++++++++++++ 2 files changed, 46 insertions(+), 2 deletions(-) create mode 100644 pkgs/ffigen/test/code_generator_tests/expected_bindings/_expected_function_ffiNative_bindings.dart diff --git a/pkgs/ffigen/test/code_generator_tests/code_generator_test.dart b/pkgs/ffigen/test/code_generator_tests/code_generator_test.dart index f81460e539..43de554da3 100644 --- a/pkgs/ffigen/test/code_generator_tests/code_generator_test.dart +++ b/pkgs/ffigen/test/code_generator_tests/code_generator_test.dart @@ -3,16 +3,18 @@ // BSD-style license that can be found in the LICENSE file. import 'package:ffigen/src/code_generator.dart'; +import 'package:ffigen/src/config_provider/config_types.dart'; import 'package:test/test.dart'; import '../test_utils.dart'; void main() { group('code_generator: ', () { - test('Function Binding (primitives, pointers)', () { + void functionBindings(bool enableFfiNative) { final library = Library( name: 'Bindings', bindings: [ Func( + ffiNativeConfig: FfiNativeConfig(enabled: enableFfiNative), name: 'noParam', dartDoc: 'Just a test function\nheres another line', returnType: NativeType( @@ -20,6 +22,7 @@ void main() { ), ), Func( + ffiNativeConfig: FfiNativeConfig(enabled: enableFfiNative), name: 'withPrimitiveParam', parameters: [ Parameter( @@ -40,6 +43,7 @@ void main() { ), ), Func( + ffiNativeConfig: FfiNativeConfig(enabled: enableFfiNative), name: 'withPointerParam', parameters: [ Parameter( @@ -68,6 +72,7 @@ void main() { ), ), Func( + ffiNativeConfig: FfiNativeConfig(enabled: enableFfiNative), isLeaf: true, name: 'leafFunc', dartDoc: 'A function with isLeaf: true', @@ -86,7 +91,15 @@ void main() { ], ); - _matchLib(library, 'function'); + _matchLib(library, enableFfiNative ? 'function_ffiNative' : 'function'); + } + + test('Function Binding (primitives, pointers)', () { + functionBindings(false); + }); + + test('Function Binding (primitives, pointers) (ffiNative)', () { + functionBindings(true); }); test('Struct Binding (primitives, pointers)', () { diff --git a/pkgs/ffigen/test/code_generator_tests/expected_bindings/_expected_function_ffiNative_bindings.dart b/pkgs/ffigen/test/code_generator_tests/expected_bindings/_expected_function_ffiNative_bindings.dart new file mode 100644 index 0000000000..7e36df5619 --- /dev/null +++ b/pkgs/ffigen/test/code_generator_tests/expected_bindings/_expected_function_ffiNative_bindings.dart @@ -0,0 +1,31 @@ +// AUTO GENERATED FILE, DO NOT EDIT. +// +// Generated by `package:ffigen`. +// ignore_for_file: type=lint +import 'dart:ffi' as ffi; + +/// Just a test function +/// heres another line +@ffi.Native(symbol: 'noParam') +external int noParam(); + +@ffi.Native( + symbol: 'withPrimitiveParam') +external int withPrimitiveParam( + int a, + int b, +); + +@ffi.Native< + ffi.Pointer Function(ffi.Pointer, + ffi.Pointer>)>(symbol: 'withPointerParam') +external ffi.Pointer withPointerParam( + ffi.Pointer a, + ffi.Pointer> b, +); + +/// A function with isLeaf: true +@ffi.Native(symbol: 'leafFunc', isLeaf: true) +external int leafFunc( + int a, +); From 94b4ff464be86252d3b109f50e101335314ece48 Mon Sep 17 00:00:00 2001 From: Michael Debertol Date: Thu, 14 Dec 2023 17:31:15 +0100 Subject: [PATCH 3/4] add license headers to code generator test goldens --- .../code_generator_test.dart | 23 +++++++++++++++---- .../_expected_boolean_dartbool_bindings.dart | 4 ++++ .../_expected_constant_bindings.dart | 4 ++++ .../_expected_enumclass_bindings.dart | 4 ++++ .../_expected_function_bindings.dart | 4 ++++ ..._expected_function_ffiNative_bindings.dart | 4 ++++ .../_expected_function_n_struct_bindings.dart | 4 ++++ .../_expected_global_bindings.dart | 4 ++++ ...internal_conflict_resolution_bindings.dart | 4 ++++ .../_expected_packed_structs_bindings.dart | 4 ++++ .../_expected_sort_bindings_bindings.dart | 4 ++++ .../_expected_struct_bindings.dart | 4 ++++ .../_expected_typealias_bindings.dart | 4 ++++ .../_expected_unions_bindings.dart | 4 ++++ 14 files changed, 71 insertions(+), 4 deletions(-) diff --git a/pkgs/ffigen/test/code_generator_tests/code_generator_test.dart b/pkgs/ffigen/test/code_generator_tests/code_generator_test.dart index 43de554da3..c4dd1a120a 100644 --- a/pkgs/ffigen/test/code_generator_tests/code_generator_test.dart +++ b/pkgs/ffigen/test/code_generator_tests/code_generator_test.dart @@ -8,10 +8,17 @@ import 'package:test/test.dart'; import '../test_utils.dart'; void main() { + const licenseHeader = ''' +// Copyright (c) 2023, the Dart project authors. Please see the AUTHORS file +// for details. All rights reserved. Use of this source code is governed by a +// BSD-style license that can be found in the LICENSE file. +'''; + group('code_generator: ', () { void functionBindings(bool enableFfiNative) { final library = Library( name: 'Bindings', + header: licenseHeader, bindings: [ Func( ffiNativeConfig: FfiNativeConfig(enabled: enableFfiNative), @@ -105,6 +112,7 @@ void main() { test('Struct Binding (primitives, pointers)', () { final library = Library( name: 'Bindings', + header: licenseHeader, bindings: [ Struct( name: 'NoMember', @@ -217,6 +225,7 @@ void main() { ); final library = Library( name: 'Bindings', + header: licenseHeader, bindings: [ structSome, Func( @@ -249,6 +258,7 @@ void main() { final library = Library( name: 'Bindings', + header: licenseHeader, bindings: [ Global( name: 'test1', @@ -282,7 +292,7 @@ void main() { test('constant', () { final library = Library( name: 'Bindings', - header: '// ignore_for_file: unused_import\n', + header: '$licenseHeader\n// ignore_for_file: unused_import\n', bindings: [ Constant( name: 'test1', @@ -302,7 +312,7 @@ void main() { test('enum_class', () { final library = Library( name: 'Bindings', - header: '// ignore_for_file: unused_import\n', + header: '$licenseHeader\n// ignore_for_file: unused_import\n', bindings: [ EnumClass( name: 'Constants', @@ -323,7 +333,7 @@ void main() { final library = Library( name: 'init_dylib', header: - '// ignore_for_file: unused_element, camel_case_types, non_constant_identifier_names\n', + '$licenseHeader\n// ignore_for_file: unused_element, camel_case_types, non_constant_identifier_names\n', bindings: [ Func( name: 'test', @@ -370,6 +380,7 @@ void main() { test('boolean_dartBool', () { final library = Library( name: 'Bindings', + header: licenseHeader, bindings: [ Func( name: 'test1', @@ -392,6 +403,7 @@ void main() { test('sort bindings', () { final library = Library( name: 'Bindings', + header: licenseHeader, sort: true, bindings: [ Func(name: 'b', returnType: NativeType(SupportedNativeType.Void)), @@ -405,6 +417,7 @@ void main() { test('Pack Structs', () { final library = Library( name: 'Bindings', + header: licenseHeader, bindings: [ Struct(name: 'NoPacking', pack: null, members: [ Member(name: 'a', type: NativeType(SupportedNativeType.Char)), @@ -435,6 +448,7 @@ void main() { Union(name: 'Union1', members: [Member(name: 'a', type: charType)]); final library = Library( name: 'Bindings', + header: licenseHeader, bindings: [ struct1, union1, @@ -465,7 +479,8 @@ void main() { test('Typealias Bindings', () { final library = Library( name: 'Bindings', - header: '// ignore_for_file: non_constant_identifier_names\n', + header: + '$licenseHeader\n// ignore_for_file: non_constant_identifier_names\n', bindings: [ Typealias(name: 'RawUnused', type: Struct(name: 'Struct1')), Struct(name: 'WithTypealiasStruct', members: [ diff --git a/pkgs/ffigen/test/code_generator_tests/expected_bindings/_expected_boolean_dartbool_bindings.dart b/pkgs/ffigen/test/code_generator_tests/expected_bindings/_expected_boolean_dartbool_bindings.dart index 015821ea28..f379c7b7d2 100644 --- a/pkgs/ffigen/test/code_generator_tests/expected_bindings/_expected_boolean_dartbool_bindings.dart +++ b/pkgs/ffigen/test/code_generator_tests/expected_bindings/_expected_boolean_dartbool_bindings.dart @@ -1,3 +1,7 @@ +// Copyright (c) 2023, the Dart project authors. Please see the AUTHORS file +// for details. All rights reserved. Use of this source code is governed by a +// BSD-style license that can be found in the LICENSE file. + // AUTO GENERATED FILE, DO NOT EDIT. // // Generated by `package:ffigen`. diff --git a/pkgs/ffigen/test/code_generator_tests/expected_bindings/_expected_constant_bindings.dart b/pkgs/ffigen/test/code_generator_tests/expected_bindings/_expected_constant_bindings.dart index a425c42201..b47cf7041e 100644 --- a/pkgs/ffigen/test/code_generator_tests/expected_bindings/_expected_constant_bindings.dart +++ b/pkgs/ffigen/test/code_generator_tests/expected_bindings/_expected_constant_bindings.dart @@ -1,3 +1,7 @@ +// Copyright (c) 2023, the Dart project authors. Please see the AUTHORS file +// for details. All rights reserved. Use of this source code is governed by a +// BSD-style license that can be found in the LICENSE file. + // ignore_for_file: unused_import // AUTO GENERATED FILE, DO NOT EDIT. diff --git a/pkgs/ffigen/test/code_generator_tests/expected_bindings/_expected_enumclass_bindings.dart b/pkgs/ffigen/test/code_generator_tests/expected_bindings/_expected_enumclass_bindings.dart index 1f759d57f1..80820af15d 100644 --- a/pkgs/ffigen/test/code_generator_tests/expected_bindings/_expected_enumclass_bindings.dart +++ b/pkgs/ffigen/test/code_generator_tests/expected_bindings/_expected_enumclass_bindings.dart @@ -1,3 +1,7 @@ +// Copyright (c) 2023, the Dart project authors. Please see the AUTHORS file +// for details. All rights reserved. Use of this source code is governed by a +// BSD-style license that can be found in the LICENSE file. + // ignore_for_file: unused_import // AUTO GENERATED FILE, DO NOT EDIT. diff --git a/pkgs/ffigen/test/code_generator_tests/expected_bindings/_expected_function_bindings.dart b/pkgs/ffigen/test/code_generator_tests/expected_bindings/_expected_function_bindings.dart index ae9fc13519..886f284994 100644 --- a/pkgs/ffigen/test/code_generator_tests/expected_bindings/_expected_function_bindings.dart +++ b/pkgs/ffigen/test/code_generator_tests/expected_bindings/_expected_function_bindings.dart @@ -1,3 +1,7 @@ +// Copyright (c) 2023, the Dart project authors. Please see the AUTHORS file +// for details. All rights reserved. Use of this source code is governed by a +// BSD-style license that can be found in the LICENSE file. + // AUTO GENERATED FILE, DO NOT EDIT. // // Generated by `package:ffigen`. diff --git a/pkgs/ffigen/test/code_generator_tests/expected_bindings/_expected_function_ffiNative_bindings.dart b/pkgs/ffigen/test/code_generator_tests/expected_bindings/_expected_function_ffiNative_bindings.dart index 7e36df5619..9b4ebf4f5b 100644 --- a/pkgs/ffigen/test/code_generator_tests/expected_bindings/_expected_function_ffiNative_bindings.dart +++ b/pkgs/ffigen/test/code_generator_tests/expected_bindings/_expected_function_ffiNative_bindings.dart @@ -1,3 +1,7 @@ +// Copyright (c) 2023, the Dart project authors. Please see the AUTHORS file +// for details. All rights reserved. Use of this source code is governed by a +// BSD-style license that can be found in the LICENSE file. + // AUTO GENERATED FILE, DO NOT EDIT. // // Generated by `package:ffigen`. diff --git a/pkgs/ffigen/test/code_generator_tests/expected_bindings/_expected_function_n_struct_bindings.dart b/pkgs/ffigen/test/code_generator_tests/expected_bindings/_expected_function_n_struct_bindings.dart index cf255b2ca8..c8680a8486 100644 --- a/pkgs/ffigen/test/code_generator_tests/expected_bindings/_expected_function_n_struct_bindings.dart +++ b/pkgs/ffigen/test/code_generator_tests/expected_bindings/_expected_function_n_struct_bindings.dart @@ -1,3 +1,7 @@ +// Copyright (c) 2023, the Dart project authors. Please see the AUTHORS file +// for details. All rights reserved. Use of this source code is governed by a +// BSD-style license that can be found in the LICENSE file. + // AUTO GENERATED FILE, DO NOT EDIT. // // Generated by `package:ffigen`. diff --git a/pkgs/ffigen/test/code_generator_tests/expected_bindings/_expected_global_bindings.dart b/pkgs/ffigen/test/code_generator_tests/expected_bindings/_expected_global_bindings.dart index ab9c3485a9..85b3b18434 100644 --- a/pkgs/ffigen/test/code_generator_tests/expected_bindings/_expected_global_bindings.dart +++ b/pkgs/ffigen/test/code_generator_tests/expected_bindings/_expected_global_bindings.dart @@ -1,3 +1,7 @@ +// Copyright (c) 2023, the Dart project authors. Please see the AUTHORS file +// for details. All rights reserved. Use of this source code is governed by a +// BSD-style license that can be found in the LICENSE file. + // AUTO GENERATED FILE, DO NOT EDIT. // // Generated by `package:ffigen`. diff --git a/pkgs/ffigen/test/code_generator_tests/expected_bindings/_expected_internal_conflict_resolution_bindings.dart b/pkgs/ffigen/test/code_generator_tests/expected_bindings/_expected_internal_conflict_resolution_bindings.dart index 1a71e3bf0f..af97952ad4 100644 --- a/pkgs/ffigen/test/code_generator_tests/expected_bindings/_expected_internal_conflict_resolution_bindings.dart +++ b/pkgs/ffigen/test/code_generator_tests/expected_bindings/_expected_internal_conflict_resolution_bindings.dart @@ -1,3 +1,7 @@ +// Copyright (c) 2023, the Dart project authors. Please see the AUTHORS file +// for details. All rights reserved. Use of this source code is governed by a +// BSD-style license that can be found in the LICENSE file. + // ignore_for_file: unused_element, camel_case_types, non_constant_identifier_names // AUTO GENERATED FILE, DO NOT EDIT. diff --git a/pkgs/ffigen/test/code_generator_tests/expected_bindings/_expected_packed_structs_bindings.dart b/pkgs/ffigen/test/code_generator_tests/expected_bindings/_expected_packed_structs_bindings.dart index f882e7e1e6..5c1b17a5bd 100644 --- a/pkgs/ffigen/test/code_generator_tests/expected_bindings/_expected_packed_structs_bindings.dart +++ b/pkgs/ffigen/test/code_generator_tests/expected_bindings/_expected_packed_structs_bindings.dart @@ -1,3 +1,7 @@ +// Copyright (c) 2023, the Dart project authors. Please see the AUTHORS file +// for details. All rights reserved. Use of this source code is governed by a +// BSD-style license that can be found in the LICENSE file. + // AUTO GENERATED FILE, DO NOT EDIT. // // Generated by `package:ffigen`. diff --git a/pkgs/ffigen/test/code_generator_tests/expected_bindings/_expected_sort_bindings_bindings.dart b/pkgs/ffigen/test/code_generator_tests/expected_bindings/_expected_sort_bindings_bindings.dart index eddeeb7337..1e9d1dc113 100644 --- a/pkgs/ffigen/test/code_generator_tests/expected_bindings/_expected_sort_bindings_bindings.dart +++ b/pkgs/ffigen/test/code_generator_tests/expected_bindings/_expected_sort_bindings_bindings.dart @@ -1,3 +1,7 @@ +// Copyright (c) 2023, the Dart project authors. Please see the AUTHORS file +// for details. All rights reserved. Use of this source code is governed by a +// BSD-style license that can be found in the LICENSE file. + // AUTO GENERATED FILE, DO NOT EDIT. // // Generated by `package:ffigen`. diff --git a/pkgs/ffigen/test/code_generator_tests/expected_bindings/_expected_struct_bindings.dart b/pkgs/ffigen/test/code_generator_tests/expected_bindings/_expected_struct_bindings.dart index b7a4e9d3fd..aa862baa7b 100644 --- a/pkgs/ffigen/test/code_generator_tests/expected_bindings/_expected_struct_bindings.dart +++ b/pkgs/ffigen/test/code_generator_tests/expected_bindings/_expected_struct_bindings.dart @@ -1,3 +1,7 @@ +// Copyright (c) 2023, the Dart project authors. Please see the AUTHORS file +// for details. All rights reserved. Use of this source code is governed by a +// BSD-style license that can be found in the LICENSE file. + // AUTO GENERATED FILE, DO NOT EDIT. // // Generated by `package:ffigen`. diff --git a/pkgs/ffigen/test/code_generator_tests/expected_bindings/_expected_typealias_bindings.dart b/pkgs/ffigen/test/code_generator_tests/expected_bindings/_expected_typealias_bindings.dart index 4383756eb6..0e1662da5c 100644 --- a/pkgs/ffigen/test/code_generator_tests/expected_bindings/_expected_typealias_bindings.dart +++ b/pkgs/ffigen/test/code_generator_tests/expected_bindings/_expected_typealias_bindings.dart @@ -1,3 +1,7 @@ +// Copyright (c) 2023, the Dart project authors. Please see the AUTHORS file +// for details. All rights reserved. Use of this source code is governed by a +// BSD-style license that can be found in the LICENSE file. + // ignore_for_file: non_constant_identifier_names // AUTO GENERATED FILE, DO NOT EDIT. diff --git a/pkgs/ffigen/test/code_generator_tests/expected_bindings/_expected_unions_bindings.dart b/pkgs/ffigen/test/code_generator_tests/expected_bindings/_expected_unions_bindings.dart index 692f0d67a3..1d000e5c42 100644 --- a/pkgs/ffigen/test/code_generator_tests/expected_bindings/_expected_unions_bindings.dart +++ b/pkgs/ffigen/test/code_generator_tests/expected_bindings/_expected_unions_bindings.dart @@ -1,3 +1,7 @@ +// Copyright (c) 2023, the Dart project authors. Please see the AUTHORS file +// for details. All rights reserved. Use of this source code is governed by a +// BSD-style license that can be found in the LICENSE file. + // AUTO GENERATED FILE, DO NOT EDIT. // // Generated by `package:ffigen`. From a2d76bc7f8dc1547d63a5c087f0048b13a42a4a3 Mon Sep 17 00:00:00 2001 From: Michael Debertol Date: Thu, 14 Dec 2023 17:32:13 +0100 Subject: [PATCH 4/4] update changelog --- pkgs/ffigen/CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/pkgs/ffigen/CHANGELOG.md b/pkgs/ffigen/CHANGELOG.md index ac8e9a4ef0..49c3071b31 100644 --- a/pkgs/ffigen/CHANGELOG.md +++ b/pkgs/ffigen/CHANGELOG.md @@ -6,6 +6,7 @@ bindings if the compiler makes a wrong guess. A flag `--ignore-source-errors` (o must be passed to change this behaviour. - __Breaking change__: Stop generating setters for global variables marked `const` in C. - Fix objc_msgSend being used on arm64 platforms where it's not available. +- Fix missing comma with `ffi-native` functions marked `leaf`. ## 10.0.0