Skip to content

[ffigen] fix syntax for leaf ffiNative functions #861

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Dec 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions pkgs/ffigen/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
3 changes: 2 additions & 1 deletion pkgs/ffigen/lib/src/code_generator/func.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand All @@ -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('''
Expand Down
40 changes: 34 additions & 6 deletions pkgs/ffigen/test/code_generator_tests/code_generator_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,33 @@
// 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(
SupportedNativeType.Int32,
),
),
Func(
ffiNativeConfig: FfiNativeConfig(enabled: enableFfiNative),
name: 'withPrimitiveParam',
parameters: [
Parameter(
Expand All @@ -40,6 +50,7 @@ void main() {
),
),
Func(
ffiNativeConfig: FfiNativeConfig(enabled: enableFfiNative),
name: 'withPointerParam',
parameters: [
Parameter(
Expand Down Expand Up @@ -68,6 +79,7 @@ void main() {
),
),
Func(
ffiNativeConfig: FfiNativeConfig(enabled: enableFfiNative),
isLeaf: true,
name: 'leafFunc',
dartDoc: 'A function with isLeaf: true',
Expand All @@ -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',
Expand Down Expand Up @@ -204,6 +225,7 @@ void main() {
);
final library = Library(
name: 'Bindings',
header: licenseHeader,
bindings: [
structSome,
Func(
Expand Down Expand Up @@ -236,6 +258,7 @@ void main() {

final library = Library(
name: 'Bindings',
header: licenseHeader,
bindings: [
Global(
name: 'test1',
Expand Down Expand Up @@ -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',
Expand All @@ -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',
Expand All @@ -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',
Expand Down Expand Up @@ -357,6 +380,7 @@ void main() {
test('boolean_dartBool', () {
final library = Library(
name: 'Bindings',
header: licenseHeader,
bindings: [
Func(
name: 'test1',
Expand All @@ -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)),
Expand All @@ -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)),
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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: [
Expand Down
Original file line number Diff line number Diff line change
@@ -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`.
Expand Down
Original file line number Diff line number Diff line change
@@ -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.
Expand Down
Original file line number Diff line number Diff line change
@@ -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.
Expand Down
Original file line number Diff line number Diff line change
@@ -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`.
Expand Down
Original file line number Diff line number Diff line change
@@ -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<ffi.Int32 Function()>(symbol: 'noParam')
external int noParam();

@ffi.Native<ffi.Uint8 Function(ffi.Int32, ffi.Uint8)>(
symbol: 'withPrimitiveParam')
external int withPrimitiveParam(
int a,
int b,
);

@ffi.Native<
ffi.Pointer<ffi.Double> Function(ffi.Pointer<ffi.Int32>,
ffi.Pointer<ffi.Pointer<ffi.Uint8>>)>(symbol: 'withPointerParam')
external ffi.Pointer<ffi.Double> withPointerParam(
ffi.Pointer<ffi.Int32> a,
ffi.Pointer<ffi.Pointer<ffi.Uint8>> b,
);

/// A function with isLeaf: true
@ffi.Native<ffi.Int32 Function(ffi.Int32)>(symbol: 'leafFunc', isLeaf: true)
external int leafFunc(
int a,
);
Original file line number Diff line number Diff line change
@@ -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`.
Expand Down
Original file line number Diff line number Diff line change
@@ -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`.
Expand Down
Original file line number Diff line number Diff line change
@@ -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.
Expand Down
Original file line number Diff line number Diff line change
@@ -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`.
Expand Down
Original file line number Diff line number Diff line change
@@ -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`.
Expand Down
Original file line number Diff line number Diff line change
@@ -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`.
Expand Down
Original file line number Diff line number Diff line change
@@ -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.
Expand Down
Original file line number Diff line number Diff line change
@@ -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`.
Expand Down