Skip to content

Commit

Permalink
More review feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
simolus3 committed Jan 3, 2024
1 parent a9a1255 commit 7a858de
Show file tree
Hide file tree
Showing 7 changed files with 22 additions and 37 deletions.
2 changes: 1 addition & 1 deletion pkgs/ffigen/example/ffinative/lib/generated_bindings.dart
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ external ffi.Array<ffi.Int> array;
assetId: 'package:ffinative_example/generated_bindings.dart')
external final ffi.Pointer<ffi.Char> library_version;

const _SymbolAddresses addresses = _SymbolAddresses();
const addresses = _SymbolAddresses();

class _SymbolAddresses {
const _SymbolAddresses();
Expand Down
15 changes: 2 additions & 13 deletions pkgs/ffigen/lib/src/code_generator/compound.dart
Original file line number Diff line number Diff line change
Expand Up @@ -80,16 +80,6 @@ abstract class Compound extends BindingType {
}
}

List<int> _getArrayDimensionLengths(Type type) {
final array = <int>[];
var startType = type;
while (startType is ConstantArray) {
array.add(startType.length);
startType = startType.child;
}
return array;
}

String _getInlineArrayTypeString(Type type, Writer w) {
if (type is ConstantArray) {
return '${w.ffiLibraryPrefix}.Array<'
Expand Down Expand Up @@ -132,9 +122,8 @@ abstract class Compound extends BindingType {
s.writeAll(m.dartDoc!.split('\n'), '\n$depth/// ');
s.write('\n');
}
if (m.type is ConstantArray) {
s.write('$depth@${w.ffiLibraryPrefix}.Array.multi(');
s.write('${_getArrayDimensionLengths(m.type)})\n');
if (m.type case final ConstantArray arrayType) {
s.writeln(makeArrayAnnotation(w, arrayType));
s.write('${depth}external ${_getInlineArrayTypeString(m.type, w)} ');
s.write('${m.name};\n\n');
} else {
Expand Down
20 changes: 1 addition & 19 deletions pkgs/ffigen/lib/src/code_generator/global.dart
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ class Global extends LookUpBinding {

if (nativeConfig.enabled) {
if (type case final ConstantArray arr) {
arr.generateSizeAnnotation(s, w);
s.writeln(makeArrayAnnotation(w, arr));
}

s
Expand Down Expand Up @@ -116,21 +116,3 @@ class Global extends LookUpBinding {
type.addDependencies(dependencies);
}
}

extension on ConstantArray {
void generateSizeAnnotation(StringBuffer buffer, Writer w) {
buffer.write('@${w.ffiLibraryPrefix}.Array(');

Type? array = this;
var first = true;
while (array is ConstantArray) {
if (!first) buffer.write(', ');

buffer.write(array.length);
first = false;
array = array.baseArrayType;
}

buffer.writeln(')');
}
}
13 changes: 13 additions & 0 deletions pkgs/ffigen/lib/src/code_generator/utils.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
// BSD-style license that can be found in the LICENSE file.

import 'dart_keywords.dart';
import 'pointer.dart';
import 'type.dart';
import 'writer.dart';

class UniqueNamer {
Expand Down Expand Up @@ -103,3 +105,14 @@ String makeNativeAnnotation(
final combinedArgs = args.map((e) => '${e.$1}: ${e.$2}').join(', ');
return '@${w.ffiLibraryPrefix}.Native<$nativeType>($combinedArgs)';
}

String makeArrayAnnotation(Writer w, ConstantArray arrayType) {
final dimensions = <int>[];
Type type = arrayType;
while (type is ConstantArray) {
dimensions.add(type.length);
type = type.child;
}

return '@${w.ffiLibraryPrefix}.Array.multi([${dimensions.join(', ')}])';
}
4 changes: 2 additions & 2 deletions pkgs/ffigen/lib/src/code_generator/writer.dart
Original file line number Diff line number Diff line change
Expand Up @@ -361,9 +361,9 @@ class SymbolAddressWriter {
final fieldName = w._symbolAddressVariableName;

if (hasNonNativeAddress) {
return 'late final $className $fieldName = $className(this);';
return 'late final $fieldName = $className(this);';
} else {
return 'const $className $fieldName = $className();';
return 'const $fieldName = $className();';
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -386,7 +386,8 @@ void main() {
NativeType(
SupportedNativeType.Int8,
),
// Arrays are always supported in struct fields
// This flag is ignored for struct fields, which always use
// inline arrays.
useArrayType: true,
),
),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ external int test1;
@ffi.Native<ffi.Pointer<ffi.Float>>()
external final ffi.Pointer<ffi.Float> test2;

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

Expand Down

0 comments on commit 7a858de

Please sign in to comment.