Skip to content

Commit

Permalink
Review feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
simolus3 committed Jan 2, 2024
1 parent d4b1388 commit b9a548f
Show file tree
Hide file tree
Showing 9 changed files with 36 additions and 15 deletions.
4 changes: 3 additions & 1 deletion pkgs/ffigen/example/ffinative/headers/example.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@ float *divide(int a, int b);
/** Divides 2 floats, returns a pointer to double. */
double *dividePrecision(float a, float b);

const int array[10];
int log_level = -1;

const int array[5] = {0, 1, 2, 3, 4};

/** Version of the native C library */
const char* const library_version = "1.0.0-native";
7 changes: 6 additions & 1 deletion pkgs/ffigen/example/ffinative/lib/generated_bindings.dart
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,11 @@ external ffi.Pointer<ffi.Double> dividePrecision(
double b,
);

@ffi.Array(10)
@ffi.Native<ffi.Int>(
assetId: 'package:ffinative_example/generated_bindings.dart')
external int log_level;

@ffi.Array(5)
@ffi.Native<ffi.Array<ffi.Int>>(
assetId: 'package:ffinative_example/generated_bindings.dart')
external ffi.Array<ffi.Int> array;
Expand All @@ -60,6 +64,7 @@ external ffi.Array<ffi.Int> array;
@ffi.Native<ffi.Pointer<ffi.Char>>(
assetId: 'package:ffinative_example/generated_bindings.dart')
external final ffi.Pointer<ffi.Char> library_version;

const _SymbolAddresses addresses = _SymbolAddresses();

class _SymbolAddresses {
Expand Down
2 changes: 1 addition & 1 deletion pkgs/ffigen/example/ffinative/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
name: ffinative_example

environment:
sdk: '>=3.3.0-dev <4.0.0'
sdk: '>=3.3.0-252.0.dev <4.0.0'

dependencies:
ffi: ^2.0.1
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 @@ -150,7 +150,8 @@ class Func extends LookUpBinding {
${makeNativeAnnotation(
w,
nativeType: cType,
differentName: originalName != nativeFuncName ? originalName : null,
dartName: nativeFuncName,
nativeSymbolName: originalName,
assetId: ffiNativeConfig.assetId,
isLeaf: isLeaf,
)}
Expand Down
5 changes: 3 additions & 2 deletions pkgs/ffigen/lib/src/code_generator/global.dart
Original file line number Diff line number Diff line change
Expand Up @@ -58,15 +58,16 @@ class Global extends LookUpBinding {
w,
nativeType: cType,
assetId: nativeConfig.assetId,
differentName: globalVarName != originalName ? originalName : null,
dartName: globalVarName,
nativeSymbolName: originalName,
isLeaf: false,
))
..write('external ');
if (constant) {
s.write('final ');
}

s.writeln('$dartType $globalVarName;');
s.writeln('$dartType $globalVarName;\n');

if (exposeSymbolAddress) {
w.symbolAddressWriter.addNativeSymbol(
Expand Down
7 changes: 4 additions & 3 deletions pkgs/ffigen/lib/src/code_generator/utils.dart
Original file line number Diff line number Diff line change
Expand Up @@ -84,13 +84,14 @@ String makeDoc(String text) {
String makeNativeAnnotation(
Writer w, {
required String? nativeType,
String? differentName,
required String dartName,
required String nativeSymbolName,
String? assetId,
bool isLeaf = false,
}) {
final args = <(String, String)>[];
if (differentName != null) {
args.add(('symbol', '"$differentName"'));
if (dartName != nativeSymbolName) {
args.add(('symbol', '"$nativeSymbolName"'));
}
if (assetId != null) {
args.add(('assetId', "'$assetId'"));
Expand Down
2 changes: 1 addition & 1 deletion pkgs/ffigen/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ topics:
- codegen

environment:
sdk: '>=3.3.0-0 <4.0.0'
sdk: '>=3.3.0-252.0.dev'

dependencies:
ffi: ^2.0.1
Expand Down
17 changes: 12 additions & 5 deletions pkgs/ffigen/test/code_generator_tests/code_generator_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -519,14 +519,21 @@ void main() {
]),
Union(name: 'WithArray', members: [
Member(
name: 'a', type: ConstantArray(10, charType, useArrayType: true)),
name: 'a',
type: ConstantArray(10, charType, useArrayType: true),
),
Member(
name: 'b', type: ConstantArray(10, union1, useArrayType: true)),
name: 'b',
type: ConstantArray(10, union1, useArrayType: true),
),
Member(
name: 'b', type: ConstantArray(10, struct1, useArrayType: true)),
name: 'b',
type: ConstantArray(10, struct1, useArrayType: true),
),
Member(
name: 'c',
type: ConstantArray(10, PointerType(union1), useArrayType: true)),
name: 'c',
type: ConstantArray(10, PointerType(union1), useArrayType: true),
),
]),
],
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,17 @@ import 'dart:ffi' as ffi;

@ffi.Native<ffi.Int32>()
external int test1;

@ffi.Native<ffi.Pointer<ffi.Float>>()
external final ffi.Pointer<ffi.Float> test2;

@ffi.Array(10)
@ffi.Native<ffi.Array<ffi.Float>>()
external final ffi.Array<ffi.Float> test3;

@ffi.Native<ffi.Pointer<Some>>()
external ffi.Pointer<Some> test5;

@ffi.Native<EmptyStruct>()
external EmptyStruct globalStruct;

Expand Down

0 comments on commit b9a548f

Please sign in to comment.