Skip to content
This repository has been archived by the owner on Jan 28, 2024. It is now read-only.

Commit

Permalink
Refactor sameFfiDartAndCType to not require a Writer (#629)
Browse files Browse the repository at this point in the history
  • Loading branch information
liamappelbe authored Oct 10, 2023
1 parent 1576465 commit 7689e24
Show file tree
Hide file tree
Showing 12 changed files with 81 additions and 5 deletions.
5 changes: 4 additions & 1 deletion lib/src/code_generator/compound.dart
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ abstract class Compound extends BindingType {
s.write('${depth}external ${_getInlineArrayTypeString(m.type, w)} ');
s.write('${m.name};\n\n');
} else {
if (!sameDartAndCType(m.type, w)) {
if (!m.type.sameFfiDartAndCType) {
s.write('$depth@${m.type.getCType(w)}()\n');
}
s.write('${depth}external ${m.type.getFfiDartType(w)} ${m.name};\n\n');
Expand Down Expand Up @@ -173,6 +173,9 @@ abstract class Compound extends BindingType {

@override
String getCType(Writer w) => name;

@override
bool get sameFfiDartAndCType => true;
}

class Member {
Expand Down
6 changes: 6 additions & 0 deletions lib/src/code_generator/enum_class.dart
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,12 @@ class EnumClass extends BindingType {
@override
String getFfiDartType(Writer w) => nativeType.getFfiDartType(w);

@override
bool get sameFfiDartAndCType => nativeType.sameFfiDartAndCType;

@override
bool get sameDartAndCType => nativeType.sameDartAndCType;

@override
String? getDefaultValue(Writer w, String nativeLib) => '0';
}
Expand Down
15 changes: 15 additions & 0 deletions lib/src/code_generator/func_type.dart
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,18 @@ class FunctionType extends Type {
return sb.toString();
}

@override
bool get sameFfiDartAndCType =>
returnType.sameFfiDartAndCType &&
parameters.every((p) => p.type.sameFfiDartAndCType) &&
varArgParameters.every((p) => p.type.sameFfiDartAndCType);

@override
bool get sameDartAndCType =>
returnType.sameDartAndCType &&
parameters.every((p) => p.type.sameDartAndCType) &&
varArgParameters.every((p) => p.type.sameDartAndCType);

@override
String toString() => _getCacheKeyString(false, (Type t) => t.toString());

Expand Down Expand Up @@ -139,6 +151,9 @@ class NativeFunc extends Type {
@override
String getFfiDartType(Writer w) => getCType(w);

@override
bool get sameFfiDartAndCType => true;

@override
String toString() => 'NativeFunction<${_type.toString()}>';

Expand Down
3 changes: 3 additions & 0 deletions lib/src/code_generator/handle.dart
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ class HandleType extends Type {
@override
String getFfiDartType(Writer w) => 'Object';

@override
bool get sameFfiDartAndCType => false;

@override
String toString() => 'Handle';
}
6 changes: 6 additions & 0 deletions lib/src/code_generator/imports.dart
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@ class ImportedType extends Type {
@override
String getFfiDartType(Writer w) => cType == dartType ? getCType(w) : dartType;

@override
bool get sameFfiDartAndCType => cType == dartType;

@override
String toString() => '${libraryImport.name}.$cType';

Expand All @@ -64,6 +67,9 @@ class SelfImportedType extends Type {
@override
String getFfiDartType(Writer w) => dartType;

@override
bool get sameFfiDartAndCType => cType == dartType;

@override
String toString() => cType;
}
Expand Down
3 changes: 3 additions & 0 deletions lib/src/code_generator/native_type.dart
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,9 @@ class NativeType extends Type {
@override
String getFfiDartType(Writer w) => _dartType;

@override
bool get sameFfiDartAndCType => _cType == _dartType;

@override
String toString() => _cType;

Expand Down
6 changes: 6 additions & 0 deletions lib/src/code_generator/objc_block.dart
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,12 @@ class $name extends _ObjCBlockBase {
@override
String getDartType(Writer w) => name;

@override
bool get sameFfiDartAndCType => true;

@override
bool get sameDartAndCType => false;

@override
String toString() => '($returnType (^)(${argTypes.join(', ')}))';
}
6 changes: 6 additions & 0 deletions lib/src/code_generator/objc_interface.dart
Original file line number Diff line number Diff line change
Expand Up @@ -389,6 +389,12 @@ class $name extends ${superType?.name ?? '_ObjCWrapper'} {
@override
String getDartType(Writer w) => name;

@override
bool get sameFfiDartAndCType => true;

@override
bool get sameDartAndCType => false;

// Utils for converting between the internal types passed to native code, and
// the external types visible to the user. For example, ObjCInterfaces are
// passed to native as Pointer<ObjCObject>, but the user sees the Dart wrapper
Expand Down
6 changes: 6 additions & 0 deletions lib/src/code_generator/objc_nullable.dart
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,12 @@ class ObjCNullable extends Type {
@override
String getDartType(Writer w) => '${child.getDartType(w)}?';

@override
bool get sameFfiDartAndCType => child.sameFfiDartAndCType;

@override
bool get sameDartAndCType => false;

@override
String toString() => '$child?';

Expand Down
7 changes: 7 additions & 0 deletions lib/src/code_generator/pointer.dart
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@ class PointerType extends Type {
String getCType(Writer w) =>
'${w.ffiLibraryPrefix}.Pointer<${child.getCType(w)}>';

// Both the C type and the FFI Dart type are 'Pointer<$cType>'.
@override
bool get sameFfiDartAndCType => true;

@override
String toString() => '$child*';

Expand Down Expand Up @@ -79,4 +83,7 @@ class ObjCObjectPointer extends PointerType {

@override
String getDartType(Writer w) => 'NSObject';

@override
bool get sameDartAndCType => false;
}
15 changes: 12 additions & 3 deletions lib/src/code_generator/type.dart
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,12 @@ abstract class Type {
/// as getFfiDartType. For ObjC bindings this refers to the wrapper object.
String getDartType(Writer w) => getFfiDartType(w);

/// Returns whether the FFI dart type and C type string are same.
bool get sameFfiDartAndCType;

/// Returns whether the dart type and C type string are same.
bool get sameDartAndCType => sameFfiDartAndCType;

/// Returns the string representation of the Type, for debugging purposes
/// only. This string should not be printed as generated code.
@override
Expand All @@ -66,9 +72,6 @@ abstract class Type {
String? getDefaultValue(Writer w, String nativeLib) => null;
}

/// Function to check if the dart and C type string are same.
bool sameDartAndCType(Type t, Writer w) => t.getCType(w) == t.getFfiDartType(w);

/// Base class for all Type bindings.
///
/// Since Dart doesn't have multiple inheritance, this type exists so that we
Expand Down Expand Up @@ -107,6 +110,9 @@ abstract class BindingType extends NoLookUpBinding implements Type {
@override
String getDartType(Writer w) => getFfiDartType(w);

@override
bool get sameDartAndCType => sameFfiDartAndCType;

@override
String toString() => originalName;

Expand All @@ -125,4 +131,7 @@ class UnimplementedType extends Type {

@override
String toString() => '(Unimplemented: $reason)';

@override
bool get sameFfiDartAndCType => true;
}
8 changes: 7 additions & 1 deletion lib/src/code_generator/typealias.dart
Original file line number Diff line number Diff line change
Expand Up @@ -121,13 +121,19 @@ class Typealias extends BindingType {
String getFfiDartType(Writer w) {
// Typealias cannot be used by name in Dart types unless both the C and Dart
// type of the underlying types are same.
if (sameDartAndCType(type, w)) {
if (type.sameFfiDartAndCType) {
return name;
} else {
return type.getFfiDartType(w);
}
}

@override
bool get sameFfiDartAndCType => type.sameFfiDartAndCType;

@override
bool get sameDartAndCType => type.sameDartAndCType;

@override
String cacheKey() => type.cacheKey();

Expand Down

0 comments on commit 7689e24

Please sign in to comment.