Skip to content

Commit 2b1fbf8

Browse files
authored
[ffigen] fix syntax for leaf ffiNative functions (#861)
1 parent dbe57a1 commit 2b1fbf8

16 files changed

+120
-7
lines changed

pkgs/ffigen/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ bindings if the compiler makes a wrong guess. A flag `--ignore-source-errors` (o
66
must be passed to change this behaviour.
77
- __Breaking change__: Stop generating setters for global variables marked `const` in C.
88
- Fix objc_msgSend being used on arm64 platforms where it's not available.
9+
- Fix missing comma with `ffi-native` functions marked `leaf`.
910

1011
## 10.0.0
1112

pkgs/ffigen/lib/src/code_generator/func.dart

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,6 @@ class Func extends LookUpBinding {
111111
functionType.getFfiDartType(w, writeArgumentNames: false);
112112
final needsWrapper = !functionType.sameDartAndFfiDartType && !isInternal;
113113

114-
final isLeafString = isLeaf ? 'isLeaf:true' : '';
115114
final funcVarName = w.wrapperLevelUniqueNamer.makeUnique('_$name');
116115
final ffiReturnType = functionType.returnType.getFfiDartType(w);
117116
final ffiArgDeclString = functionType.dartTypeParameters
@@ -149,6 +148,7 @@ class Func extends LookUpBinding {
149148
final assetString = ffiNativeConfig.assetId != null
150149
? ", assetId: '${ffiNativeConfig.assetId}'"
151150
: '';
151+
final isLeafString = isLeaf ? ', isLeaf:true' : '';
152152
final nativeFuncName = needsWrapper ? funcVarName : enclosingFuncName;
153153
s.write('''
154154
@${w.ffiLibraryPrefix}.Native<$cType>(symbol: '$originalName'$assetString$isLeafString)
@@ -166,6 +166,7 @@ $dartReturnType $enclosingFuncName($libArg$dartArgDeclString) => $funcImplCall;
166166
}
167167
} else {
168168
funcPointerName = w.wrapperLevelUniqueNamer.makeUnique('_${name}Ptr');
169+
final isLeafString = isLeaf ? 'isLeaf:true' : '';
169170

170171
// Write enclosing function.
171172
s.write('''

pkgs/ffigen/test/code_generator_tests/code_generator_test.dart

Lines changed: 34 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,23 +3,33 @@
33
// BSD-style license that can be found in the LICENSE file.
44

55
import 'package:ffigen/src/code_generator.dart';
6+
import 'package:ffigen/src/config_provider/config_types.dart';
67
import 'package:test/test.dart';
78
import '../test_utils.dart';
89

910
void main() {
11+
const licenseHeader = '''
12+
// Copyright (c) 2023, the Dart project authors. Please see the AUTHORS file
13+
// for details. All rights reserved. Use of this source code is governed by a
14+
// BSD-style license that can be found in the LICENSE file.
15+
''';
16+
1017
group('code_generator: ', () {
11-
test('Function Binding (primitives, pointers)', () {
18+
void functionBindings(bool enableFfiNative) {
1219
final library = Library(
1320
name: 'Bindings',
21+
header: licenseHeader,
1422
bindings: [
1523
Func(
24+
ffiNativeConfig: FfiNativeConfig(enabled: enableFfiNative),
1625
name: 'noParam',
1726
dartDoc: 'Just a test function\nheres another line',
1827
returnType: NativeType(
1928
SupportedNativeType.Int32,
2029
),
2130
),
2231
Func(
32+
ffiNativeConfig: FfiNativeConfig(enabled: enableFfiNative),
2333
name: 'withPrimitiveParam',
2434
parameters: [
2535
Parameter(
@@ -40,6 +50,7 @@ void main() {
4050
),
4151
),
4252
Func(
53+
ffiNativeConfig: FfiNativeConfig(enabled: enableFfiNative),
4354
name: 'withPointerParam',
4455
parameters: [
4556
Parameter(
@@ -68,6 +79,7 @@ void main() {
6879
),
6980
),
7081
Func(
82+
ffiNativeConfig: FfiNativeConfig(enabled: enableFfiNative),
7183
isLeaf: true,
7284
name: 'leafFunc',
7385
dartDoc: 'A function with isLeaf: true',
@@ -86,12 +98,21 @@ void main() {
8698
],
8799
);
88100

89-
_matchLib(library, 'function');
101+
_matchLib(library, enableFfiNative ? 'function_ffiNative' : 'function');
102+
}
103+
104+
test('Function Binding (primitives, pointers)', () {
105+
functionBindings(false);
106+
});
107+
108+
test('Function Binding (primitives, pointers) (ffiNative)', () {
109+
functionBindings(true);
90110
});
91111

92112
test('Struct Binding (primitives, pointers)', () {
93113
final library = Library(
94114
name: 'Bindings',
115+
header: licenseHeader,
95116
bindings: [
96117
Struct(
97118
name: 'NoMember',
@@ -204,6 +225,7 @@ void main() {
204225
);
205226
final library = Library(
206227
name: 'Bindings',
228+
header: licenseHeader,
207229
bindings: [
208230
structSome,
209231
Func(
@@ -236,6 +258,7 @@ void main() {
236258

237259
final library = Library(
238260
name: 'Bindings',
261+
header: licenseHeader,
239262
bindings: [
240263
Global(
241264
name: 'test1',
@@ -269,7 +292,7 @@ void main() {
269292
test('constant', () {
270293
final library = Library(
271294
name: 'Bindings',
272-
header: '// ignore_for_file: unused_import\n',
295+
header: '$licenseHeader\n// ignore_for_file: unused_import\n',
273296
bindings: [
274297
Constant(
275298
name: 'test1',
@@ -289,7 +312,7 @@ void main() {
289312
test('enum_class', () {
290313
final library = Library(
291314
name: 'Bindings',
292-
header: '// ignore_for_file: unused_import\n',
315+
header: '$licenseHeader\n// ignore_for_file: unused_import\n',
293316
bindings: [
294317
EnumClass(
295318
name: 'Constants',
@@ -310,7 +333,7 @@ void main() {
310333
final library = Library(
311334
name: 'init_dylib',
312335
header:
313-
'// ignore_for_file: unused_element, camel_case_types, non_constant_identifier_names\n',
336+
'$licenseHeader\n// ignore_for_file: unused_element, camel_case_types, non_constant_identifier_names\n',
314337
bindings: [
315338
Func(
316339
name: 'test',
@@ -357,6 +380,7 @@ void main() {
357380
test('boolean_dartBool', () {
358381
final library = Library(
359382
name: 'Bindings',
383+
header: licenseHeader,
360384
bindings: [
361385
Func(
362386
name: 'test1',
@@ -379,6 +403,7 @@ void main() {
379403
test('sort bindings', () {
380404
final library = Library(
381405
name: 'Bindings',
406+
header: licenseHeader,
382407
sort: true,
383408
bindings: [
384409
Func(name: 'b', returnType: NativeType(SupportedNativeType.Void)),
@@ -392,6 +417,7 @@ void main() {
392417
test('Pack Structs', () {
393418
final library = Library(
394419
name: 'Bindings',
420+
header: licenseHeader,
395421
bindings: [
396422
Struct(name: 'NoPacking', pack: null, members: [
397423
Member(name: 'a', type: NativeType(SupportedNativeType.Char)),
@@ -422,6 +448,7 @@ void main() {
422448
Union(name: 'Union1', members: [Member(name: 'a', type: charType)]);
423449
final library = Library(
424450
name: 'Bindings',
451+
header: licenseHeader,
425452
bindings: [
426453
struct1,
427454
union1,
@@ -452,7 +479,8 @@ void main() {
452479
test('Typealias Bindings', () {
453480
final library = Library(
454481
name: 'Bindings',
455-
header: '// ignore_for_file: non_constant_identifier_names\n',
482+
header:
483+
'$licenseHeader\n// ignore_for_file: non_constant_identifier_names\n',
456484
bindings: [
457485
Typealias(name: 'RawUnused', type: Struct(name: 'Struct1')),
458486
Struct(name: 'WithTypealiasStruct', members: [

pkgs/ffigen/test/code_generator_tests/expected_bindings/_expected_boolean_dartbool_bindings.dart

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
// Copyright (c) 2023, the Dart project authors. Please see the AUTHORS file
2+
// for details. All rights reserved. Use of this source code is governed by a
3+
// BSD-style license that can be found in the LICENSE file.
4+
15
// AUTO GENERATED FILE, DO NOT EDIT.
26
//
37
// Generated by `package:ffigen`.

pkgs/ffigen/test/code_generator_tests/expected_bindings/_expected_constant_bindings.dart

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
// Copyright (c) 2023, the Dart project authors. Please see the AUTHORS file
2+
// for details. All rights reserved. Use of this source code is governed by a
3+
// BSD-style license that can be found in the LICENSE file.
4+
15
// ignore_for_file: unused_import
26

37
// AUTO GENERATED FILE, DO NOT EDIT.

pkgs/ffigen/test/code_generator_tests/expected_bindings/_expected_enumclass_bindings.dart

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
// Copyright (c) 2023, the Dart project authors. Please see the AUTHORS file
2+
// for details. All rights reserved. Use of this source code is governed by a
3+
// BSD-style license that can be found in the LICENSE file.
4+
15
// ignore_for_file: unused_import
26

37
// AUTO GENERATED FILE, DO NOT EDIT.

pkgs/ffigen/test/code_generator_tests/expected_bindings/_expected_function_bindings.dart

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
// Copyright (c) 2023, the Dart project authors. Please see the AUTHORS file
2+
// for details. All rights reserved. Use of this source code is governed by a
3+
// BSD-style license that can be found in the LICENSE file.
4+
15
// AUTO GENERATED FILE, DO NOT EDIT.
26
//
37
// Generated by `package:ffigen`.
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
// Copyright (c) 2023, the Dart project authors. Please see the AUTHORS file
2+
// for details. All rights reserved. Use of this source code is governed by a
3+
// BSD-style license that can be found in the LICENSE file.
4+
5+
// AUTO GENERATED FILE, DO NOT EDIT.
6+
//
7+
// Generated by `package:ffigen`.
8+
// ignore_for_file: type=lint
9+
import 'dart:ffi' as ffi;
10+
11+
/// Just a test function
12+
/// heres another line
13+
@ffi.Native<ffi.Int32 Function()>(symbol: 'noParam')
14+
external int noParam();
15+
16+
@ffi.Native<ffi.Uint8 Function(ffi.Int32, ffi.Uint8)>(
17+
symbol: 'withPrimitiveParam')
18+
external int withPrimitiveParam(
19+
int a,
20+
int b,
21+
);
22+
23+
@ffi.Native<
24+
ffi.Pointer<ffi.Double> Function(ffi.Pointer<ffi.Int32>,
25+
ffi.Pointer<ffi.Pointer<ffi.Uint8>>)>(symbol: 'withPointerParam')
26+
external ffi.Pointer<ffi.Double> withPointerParam(
27+
ffi.Pointer<ffi.Int32> a,
28+
ffi.Pointer<ffi.Pointer<ffi.Uint8>> b,
29+
);
30+
31+
/// A function with isLeaf: true
32+
@ffi.Native<ffi.Int32 Function(ffi.Int32)>(symbol: 'leafFunc', isLeaf: true)
33+
external int leafFunc(
34+
int a,
35+
);

pkgs/ffigen/test/code_generator_tests/expected_bindings/_expected_function_n_struct_bindings.dart

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
// Copyright (c) 2023, the Dart project authors. Please see the AUTHORS file
2+
// for details. All rights reserved. Use of this source code is governed by a
3+
// BSD-style license that can be found in the LICENSE file.
4+
15
// AUTO GENERATED FILE, DO NOT EDIT.
26
//
37
// Generated by `package:ffigen`.

pkgs/ffigen/test/code_generator_tests/expected_bindings/_expected_global_bindings.dart

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
// Copyright (c) 2023, the Dart project authors. Please see the AUTHORS file
2+
// for details. All rights reserved. Use of this source code is governed by a
3+
// BSD-style license that can be found in the LICENSE file.
4+
15
// AUTO GENERATED FILE, DO NOT EDIT.
26
//
37
// Generated by `package:ffigen`.

pkgs/ffigen/test/code_generator_tests/expected_bindings/_expected_internal_conflict_resolution_bindings.dart

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
// Copyright (c) 2023, the Dart project authors. Please see the AUTHORS file
2+
// for details. All rights reserved. Use of this source code is governed by a
3+
// BSD-style license that can be found in the LICENSE file.
4+
15
// ignore_for_file: unused_element, camel_case_types, non_constant_identifier_names
26

37
// AUTO GENERATED FILE, DO NOT EDIT.

pkgs/ffigen/test/code_generator_tests/expected_bindings/_expected_packed_structs_bindings.dart

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
// Copyright (c) 2023, the Dart project authors. Please see the AUTHORS file
2+
// for details. All rights reserved. Use of this source code is governed by a
3+
// BSD-style license that can be found in the LICENSE file.
4+
15
// AUTO GENERATED FILE, DO NOT EDIT.
26
//
37
// Generated by `package:ffigen`.

pkgs/ffigen/test/code_generator_tests/expected_bindings/_expected_sort_bindings_bindings.dart

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
// Copyright (c) 2023, the Dart project authors. Please see the AUTHORS file
2+
// for details. All rights reserved. Use of this source code is governed by a
3+
// BSD-style license that can be found in the LICENSE file.
4+
15
// AUTO GENERATED FILE, DO NOT EDIT.
26
//
37
// Generated by `package:ffigen`.

pkgs/ffigen/test/code_generator_tests/expected_bindings/_expected_struct_bindings.dart

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
// Copyright (c) 2023, the Dart project authors. Please see the AUTHORS file
2+
// for details. All rights reserved. Use of this source code is governed by a
3+
// BSD-style license that can be found in the LICENSE file.
4+
15
// AUTO GENERATED FILE, DO NOT EDIT.
26
//
37
// Generated by `package:ffigen`.

pkgs/ffigen/test/code_generator_tests/expected_bindings/_expected_typealias_bindings.dart

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
// Copyright (c) 2023, the Dart project authors. Please see the AUTHORS file
2+
// for details. All rights reserved. Use of this source code is governed by a
3+
// BSD-style license that can be found in the LICENSE file.
4+
15
// ignore_for_file: non_constant_identifier_names
26

37
// AUTO GENERATED FILE, DO NOT EDIT.

pkgs/ffigen/test/code_generator_tests/expected_bindings/_expected_unions_bindings.dart

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
// Copyright (c) 2023, the Dart project authors. Please see the AUTHORS file
2+
// for details. All rights reserved. Use of this source code is governed by a
3+
// BSD-style license that can be found in the LICENSE file.
4+
15
// AUTO GENERATED FILE, DO NOT EDIT.
26
//
37
// Generated by `package:ffigen`.

0 commit comments

Comments
 (0)