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 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(''' 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..c4dd1a120a 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,25 @@ // 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() { + 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: ', () { - test('Function Binding (primitives, pointers)', () { + void functionBindings(bool enableFfiNative) { final library = Library( name: 'Bindings', + header: licenseHeader, bindings: [ Func( + ffiNativeConfig: FfiNativeConfig(enabled: enableFfiNative), name: 'noParam', dartDoc: 'Just a test function\nheres another line', returnType: NativeType( @@ -20,6 +29,7 @@ void main() { ), ), Func( + ffiNativeConfig: FfiNativeConfig(enabled: enableFfiNative), name: 'withPrimitiveParam', parameters: [ Parameter( @@ -40,6 +50,7 @@ void main() { ), ), Func( + ffiNativeConfig: FfiNativeConfig(enabled: enableFfiNative), name: 'withPointerParam', parameters: [ Parameter( @@ -68,6 +79,7 @@ void main() { ), ), Func( + ffiNativeConfig: FfiNativeConfig(enabled: enableFfiNative), isLeaf: true, name: 'leafFunc', dartDoc: 'A function with isLeaf: true', @@ -86,12 +98,21 @@ 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)', () { final library = Library( name: 'Bindings', + header: licenseHeader, bindings: [ Struct( name: 'NoMember', @@ -204,6 +225,7 @@ void main() { ); final library = Library( name: 'Bindings', + header: licenseHeader, bindings: [ structSome, Func( @@ -236,6 +258,7 @@ void main() { final library = Library( name: 'Bindings', + header: licenseHeader, bindings: [ Global( name: 'test1', @@ -269,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', @@ -289,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', @@ -310,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', @@ -357,6 +380,7 @@ void main() { test('boolean_dartBool', () { final library = Library( name: 'Bindings', + header: licenseHeader, bindings: [ Func( name: 'test1', @@ -379,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)), @@ -392,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)), @@ -422,6 +448,7 @@ void main() { Union(name: 'Union1', members: [Member(name: 'a', type: charType)]); final library = Library( name: 'Bindings', + header: licenseHeader, bindings: [ struct1, union1, @@ -452,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 new file mode 100644 index 0000000000..9b4ebf4f5b --- /dev/null +++ b/pkgs/ffigen/test/code_generator_tests/expected_bindings/_expected_function_ffiNative_bindings.dart @@ -0,0 +1,35 @@ +// 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`. +// 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, +); 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`.