Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
574ee0b
crash fix
tarrinneal Mar 24, 2023
46ca75e
remove need for function
tarrinneal Mar 25, 2023
e350040
cleaner method method
tarrinneal Mar 29, 2023
3d31aff
better name
tarrinneal Mar 29, 2023
3be7eb1
fix enums and tests
tarrinneal Mar 30, 2023
ef111ee
enum fixes, and Any casting
tarrinneal Mar 30, 2023
6d5271f
gen test
tarrinneal Mar 30, 2023
a4625f8
Merge branch 'main' of github.com:flutter/packages into swift-nil
tarrinneal Mar 30, 2023
29ac0ec
simplify casting enum
tarrinneal Mar 30, 2023
4a7b408
Figured it out
tarrinneal Mar 30, 2023
d1d9c6f
Better name
tarrinneal Mar 31, 2023
241f0e3
gen tests
tarrinneal Mar 31, 2023
8d356cc
some nits
tarrinneal Mar 31, 2023
04dfdbd
writedecodecasting init
tarrinneal Mar 31, 2023
ed57271
This works, but does it explode on incorect type?
tarrinneal Apr 1, 2023
1bd3afa
cleaner with as Any
tarrinneal Apr 1, 2023
d0f9b86
gen test
tarrinneal Apr 1, 2023
fab7ca3
Merge branch 'main' of github.com:flutter/packages into swift-nil
tarrinneal Apr 1, 2023
2e2d337
comment
tarrinneal Apr 3, 2023
5b39dbe
fix NSNull issue in EchoBinaryMessenger
tarrinneal Apr 3, 2023
cbb8e0b
makes things better
tarrinneal Apr 4, 2023
7fa0f7d
gen tests
tarrinneal Apr 4, 2023
b0c7653
Improve _writeDecodeCasting
tarrinneal Apr 4, 2023
740abd8
Merge branch 'main' of github.com:flutter/packages into swift-nil
tarrinneal Apr 4, 2023
748a0bb
assert message
tarrinneal Apr 4, 2023
d8f5fe3
nits
tarrinneal Apr 4, 2023
06ab71e
comment
tarrinneal Apr 4, 2023
547ed02
nested ternary
tarrinneal Apr 4, 2023
367009e
gen test
tarrinneal Apr 4, 2023
cdf35ff
nits
tarrinneal Apr 4, 2023
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
Prev Previous commit
Next Next commit
Improve _writeDecodeCasting
  • Loading branch information
tarrinneal committed Apr 4, 2023
commit b0c7653c5ee2d9c158fa514cca54c39d838bfef9
110 changes: 49 additions & 61 deletions packages/pigeon/lib/swift_generator.dart
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,6 @@ import FlutterMacOS
value: listValue,
variableName: field.name,
type: field.type,
field: field,
customClassNames: customClassNames,
customEnumNames: customEnumNames,
);
Expand Down Expand Up @@ -599,56 +598,63 @@ import FlutterMacOS
required String value,
required String variableName,
required TypeDeclaration type,
NamedType? field,
Set<String>? customClassNames,
Set<String>? customEnumNames,
}) {
if (field != null) {
final HostDatatype hostDatatype = _getHostDatatype(root, field);
final String fieldType = _swiftTypeForDartType(field.type);

if (field.type.isNullable) {
if (customClassNames != null &&
!hostDatatype.isBuiltin &&
customClassNames.contains(field.type.baseName)) {
indent.writeln('var $variableName: $fieldType? = nil');
indent.write('if let ${variableName}List = $value as! [Any]? ');
indent.addScoped('{', '}', () {
indent.writeln(
'$variableName = $fieldType.fromList(${variableName}List)');
});
} else if (customEnumNames != null &&
!hostDatatype.isBuiltin &&
customEnumNames.contains(field.type.baseName)) {
indent.writeln('var $variableName: $fieldType? = nil');
indent.writeln(
'let ${variableName}EnumVal: Int? = ${_castForceUnwrap(value, const TypeDeclaration(baseName: 'Int', isNullable: true), root)}');
indent.write(
'if let ${variableName}RawValue = ${variableName}EnumVal ');
indent.addScoped('{', '}', () {
indent.writeln(
'$variableName = $fieldType(rawValue: ${variableName}RawValue)!');
});
} else {
indent.writeln(
'let $variableName: $fieldType? = ${_castForceUnwrap(value, field.type, root)}');
}
String castForceUnwrap(String value, TypeDeclaration type, Root root) {
if (isEnum(root, type)) {
assert(!type.isNullable);
return '${_swiftTypeForDartType(type)}(rawValue: $value as! Int)!';
} else if (type.baseName == 'Object') {
// Special-cased to avoid warnings about using 'as' with Any.
return value;
} else if (type.baseName == 'int') {
final String orString =
type.isNullable ? 'nilOrValue($value)' : '$value as! Int64';
return '($value is Int32) ? Int64($value as! Int32) : $orString';
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It occurs to me that we should reverse the conditional based on what I found about NSNumber; as long as the underlying value actually is an NSNumber, "casting" to Int64 directly will work, so checking for Int32 first (which will also work) will result in two conversions (NSNumber -> Int32 > Int64) instead of one.

So:

final String int64String = type.isNullable ? 'nilOrValue($value)' : '$value as! Int64';
return '($value is Int64) ? $int64String : Int64($value as! Int32);

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That doesn't work for nullables though

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And I'm fairly certain it doesn't lower the conversion amount, since casting checking the cast type doesn't perform an actual casting

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And I'm fairly certain it doesn't lower the conversion amount, since casting checking the cast type doesn't perform an actual casting

But the cast does. In the version you have in the PR now:

(args[0] is Int32) ? Int64(args[0] as! Int32) : args[0] as! Int64

The flow for a NSNumber (what the codec currently produces) with a value that fits in 32 bits (which is going to be ~all of them in most usage) is, according to the evolution doc and observed behavior:

  • Check is Int32 -> true, because the value can be safely converted to Int32 (despite appearances, this is not actually just doing a type check; NSNumber is not an Int32 or an Int64, but is will return true for it if it can convert).
  • Do as! Int32 -> does a conversion, not a cast (again, despite appearances), creating an Int32 from the NSNumber's value.
  • Do Int64(theAnonymousNewInt32) -> does a conversion from Int32 to Int64, creating an Int64 and discarding the Int32.

What we want is to convert the NSNumber directly to an Int64, which always work.

That doesn't work for nullables though

Ah, right. Maybe we should just make ints fully custom then:

non-nullable: let foo : Int64 = $value is Int64 ? $value as! Int64 : Int64($value as! Int32)
nullable: let foo : Int64? = $value is NSNull ? nil : $value is Int64? ? $value as! Int64? : Int64($value as! Int32)
(Usually I think nested ternaries are a crime against nature, but here it's short and in generated code, and we can put a comment in the Dart explaining the logic, so I think it's okay, and it avoids the mess of generating extra variables.)

} else if (type.isNullable) {
return 'nilOrValue($value)';
} else {
if (customClassNames != null &&
!hostDatatype.isBuiltin &&
customClassNames.contains(field.type.baseName)) {
return '$value as! ${_swiftTypeForDartType(type)}';
}
}

final String fieldType = _swiftTypeForDartType(type);

if (type.isNullable) {
if (customClassNames != null &&
customClassNames.contains(type.baseName)) {
indent.writeln('var $variableName: $fieldType? = nil');
indent.write('if let ${variableName}List = $value as! [Any]? ');
indent.addScoped('{', '}', () {
indent.writeln(
'let $variableName = $fieldType.fromList($value as! [Any])!');
} else {
'$variableName = $fieldType.fromList(${variableName}List)');
});
} else if (customEnumNames != null &&
customEnumNames.contains(type.baseName)) {
indent.writeln('var $variableName: $fieldType? = nil');
indent.writeln(
'let ${variableName}EnumVal: Int? = ${castForceUnwrap(value, const TypeDeclaration(baseName: 'Int', isNullable: true), root)}');
indent
.write('if let ${variableName}RawValue = ${variableName}EnumVal ');
indent.addScoped('{', '}', () {
indent.writeln(
'let $variableName = ${_castForceUnwrap(value, field.type, root)}');
}
'$variableName = $fieldType(rawValue: ${variableName}RawValue)!');
});
} else {
indent.writeln(
'let $variableName: $fieldType? = ${castForceUnwrap(value, type, root)}');
}
} else {
if (customClassNames != null &&
customClassNames.contains(type.baseName)) {
indent.writeln(
'let $variableName = $fieldType.fromList($value as! [Any])!');
} else {
indent.writeln(
'let $variableName = ${castForceUnwrap(value, type, root)}');
}
return;
}

indent.writeln(
'let $variableName: ${_nullsafeSwiftTypeForDartType(type)} = ${_castForceUnwrap(value, type, root)}');
}

void _writeWrapResult(Indent indent) {
Expand Down Expand Up @@ -721,24 +727,6 @@ String _camelCase(String text) {
return pascal[0].toLowerCase() + pascal.substring(1);
}

String _castForceUnwrap(String value, TypeDeclaration type, Root root) {
if (isEnum(root, type)) {
assert(!type.isNullable);
return '${_swiftTypeForDartType(type)}(rawValue: $value as! Int)!';
} else if (type.baseName == 'Object') {
// Special-cased to avoid warnings about using 'as' with Any.
return value;
} else if (type.baseName == 'int') {
final String orString =
type.isNullable ? 'nilOrValue($value)' : '$value as! Int64';
return '($value is Int32) ? Int64($value as! Int32) : $orString';
} else if (type.isNullable) {
return 'nilOrValue($value)';
} else {
return '$value as! ${_swiftTypeForDartType(type)}';
}
}

/// Converts a [List] of [TypeDeclaration]s to a comma separated [String] to be
/// used in Swift code.
String _flattenTypeArguments(List<TypeDeclaration> args) {
Expand Down
Loading