Skip to content

[swift2objc] Support Swift ARC features #2055

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

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -38,21 +38,30 @@ class PropertyDeclaration extends AstNode
PropertyStatements? getter;
PropertyStatements? setter;

bool unowned;

bool weak;

bool lazy;

bool isStatic;

PropertyDeclaration({
required this.id,
required this.name,
required this.type,
this.hasSetter = false,
this.isConstant = false,
this.hasObjCAnnotation = false,
this.getter,
this.setter,
this.isStatic = false,
this.throws = false,
this.async = false,
}) : assert(!(isConstant && hasSetter)),
PropertyDeclaration(
{required this.id,
required this.name,
required this.type,
this.hasSetter = false,
this.isConstant = false,
this.hasObjCAnnotation = false,
this.getter,
this.setter,
this.isStatic = false,
this.throws = false,
this.async = false,
this.unowned = false,
this.weak = false,
this.lazy = false})
: assert(!(isConstant && hasSetter)),
assert(!(hasSetter && throws));

@override
Expand Down
54 changes: 37 additions & 17 deletions pkgs/swift2objc/lib/src/generator/generators/class_generator.dart
Original file line number Diff line number Diff line change
Expand Up @@ -144,27 +144,47 @@ List<String> _generateClassProperty(PropertyDeclaration property) {
if (property.isStatic) {
header.write('static ');
}
final prefixes = [
if (property.unowned) 'unowned',
if (property.weak) 'weak',
];

header.write('public var ${property.name}: ${property.type.swiftType} {');
var prefix = prefixes.isEmpty ? '' : '${prefixes.join(' ')} ';
var propSwiftType = property.type.swiftType;

final getterLines = [
'get ${generateAnnotations(property)}{',
...(property.getter?.statements.indent() ?? <String>[]),
'}'
];
if (property.lazy) {
header
.write('public ${prefix}lazy var ${property.name}: $propSwiftType = {');
final getterLines = [
...(property.getter?.statements.indent() ?? <String>[]),
];
return [
header.toString(),
...getterLines.indent(),
'}();\n',
];
} else {
header.write('public ${prefix}var ${property.name}: $propSwiftType {');

final setterLines = [
'set {',
...(property.setter?.statements.indent() ?? <String>[]),
'}'
];
final getterLines = [
'get ${generateAnnotations(property)}{',
...(property.getter?.statements.indent() ?? <String>[]),
'}'
];

return [
header.toString(),
...getterLines.indent(),
if (property.hasSetter) ...setterLines.indent(),
'}\n',
];
final setterLines = [
'set {',
...(property.setter?.statements.indent() ?? <String>[]),
'}'
];

return [
header.toString(),
...getterLines.indent(),
if (property.hasSetter) ...setterLines.indent(),
'}\n',
];
}
}

List<String> _generateNestedDeclarations(ClassDeclaration declaration) => [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,22 @@ PropertyDeclaration parsePropertyDeclaration(
ParsedSymbolgraph symbolgraph, {
bool isStatic = false,
}) {
final isConstant = _parseVariableIsConstant(propertySymbolJson);
final info = parsePropertyInfo(propertySymbolJson['declarationFragments']);

return PropertyDeclaration(
id: parseSymbolId(propertySymbolJson),
name: parseSymbolName(propertySymbolJson),
type: _parseVariableType(propertySymbolJson, symbolgraph),
hasObjCAnnotation: parseSymbolHasObjcAnnotation(propertySymbolJson),
isConstant: isConstant,
hasSetter: isConstant ? false : _parsePropertyHasSetter(propertySymbolJson),
isConstant: info.constant,
hasSetter:
info.constant ? false : _parsePropertyHasSetter(propertySymbolJson),
isStatic: isStatic,
throws: _parseVariableThrows(propertySymbolJson),
async: _parseVariableAsync(propertySymbolJson),
throws: info.throws,
async: info.async,
unowned: info.unowned,
weak: info.weak,
lazy: info.lazy,
);
}

Expand All @@ -36,14 +41,15 @@ GlobalVariableDeclaration parseGlobalVariableDeclaration(
}) {
final isConstant = _parseVariableIsConstant(variableSymbolJson);
final hasSetter = _parsePropertyHasSetter(variableSymbolJson);
final variableModifiers = parsePropertyInfo(variableSymbolJson);

return GlobalVariableDeclaration(
id: parseSymbolId(variableSymbolJson),
name: parseSymbolName(variableSymbolJson),
type: _parseVariableType(variableSymbolJson, symbolgraph),
isConstant: isConstant || !hasSetter,
throws: _parseVariableThrows(variableSymbolJson),
async: _parseVariableAsync(variableSymbolJson),
);
id: parseSymbolId(variableSymbolJson),
name: parseSymbolName(variableSymbolJson),
type: _parseVariableType(variableSymbolJson, symbolgraph),
isConstant: isConstant || !hasSetter,
throws: variableModifiers.throws,
async: variableModifiers.async);
}

ReferredType _parseVariableType(
Expand All @@ -53,9 +59,7 @@ ReferredType _parseVariableType(
parseTypeAfterSeparator(
TokenList(propertySymbolJson['names']['subHeading']), symbolgraph);

bool _parseVariableIsConstant(Json variableSymbolJson) {
final fragmentsJson = variableSymbolJson['declarationFragments'];

bool _parseVariableIsConstant(Json fragmentsJson) {
final declarationKeyword = fragmentsJson.firstWhere(
(json) =>
matchFragment(json, 'keyword', 'var') ||
Expand All @@ -69,16 +73,30 @@ bool _parseVariableIsConstant(Json variableSymbolJson) {
return matchFragment(declarationKeyword, 'keyword', 'let');
}

bool _parseVariableThrows(Json json) {
final throws = json['declarationFragments']
.any((frag) => matchFragment(frag, 'keyword', 'throws'));
return throws;
bool _findKeywordInFragments(Json json, String keyword) {
final keywordIsPresent =
json.any((frag) => matchFragment(frag, 'keyword', keyword));
return keywordIsPresent;
}

bool _parseVariableAsync(Json json) {
final async = json['declarationFragments']
.any((frag) => matchFragment(frag, 'keyword', 'async'));
return async;
typedef ParsedPropertyInfo = ({
bool async,
bool throws,
bool unowned,
bool weak,
bool lazy,
bool constant,
});

ParsedPropertyInfo parsePropertyInfo(Json json) {
return (
constant: _parseVariableIsConstant(json),
async: _findKeywordInFragments(json, 'async'),
throws: _findKeywordInFragments(json, 'throws'),
unowned: _findKeywordInFragments(json, 'unowned'),
weak: _findKeywordInFragments(json, 'weak'),
lazy: _findKeywordInFragments(json, 'lazy')
);
}

bool _parsePropertyHasSetter(Json propertySymbolJson) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,13 @@ Declaration _transformVariable(
isConstant: originalVariable.isConstant,
throws: originalVariable.throws,
async: originalVariable.async,
unowned: originalVariable is PropertyDeclaration
? originalVariable.unowned
: false,
lazy:
originalVariable is PropertyDeclaration ? originalVariable.lazy : false,
weak:
originalVariable is PropertyDeclaration ? originalVariable.weak : false,
);

final getterStatements = _generateGetterStatements(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,17 +29,23 @@ public class MyClass {

public let representableConstantProperty: Int

public weak var weakProperty: MyOtherClass?
public unowned var unownedProperty: MyOtherClass
public lazy var lazyProperty: Int = { 1 }();


init(
customVariableProperty: MyOtherClass,
customConstantProperty: MyOtherClass,
representableVariableProperty: Int,
representableConstantProperty: Int
representableConstantProperty: Int,
unownedProperty: MyOtherClass
) {
self.customVariableProperty = customVariableProperty
self.customConstantProperty = customConstantProperty
self.representableVariableProperty = representableVariableProperty
self.representableConstantProperty = representableConstantProperty
self.unownedProperty = unownedProperty
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,28 @@ import Foundation
@objc public class MyClassWrapper: NSObject {
var wrappedInstance: MyClass

@objc public lazy var lazyProperty: Int = {
wrappedInstance.lazyProperty
}();

@objc public weak var weakProperty: MyOtherClassWrapper? {
get {
wrappedInstance.weakProperty == nil ? nil : MyOtherClassWrapper(wrappedInstance.weakProperty!)
}
set {
wrappedInstance.weakProperty = newValue?.wrappedInstance
}
}

@objc public unowned var unownedProperty: MyOtherClassWrapper {
get {
MyOtherClassWrapper(wrappedInstance.unownedProperty)
}
set {
wrappedInstance.unownedProperty = newValue.wrappedInstance
}
}

@objc public var customGetterProperty: MyOtherClassWrapper {
get {
MyOtherClassWrapper(wrappedInstance.customGetterProperty)
Expand Down
2 changes: 1 addition & 1 deletion pkgs/swift2objc/test/unit/parse_function_info_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ void main() {
decl.id: ParsedSymbol(json: Json(null), declaration: decl)
};
final emptySymbolgraph = ParsedSymbolgraph(parsedSymbols, {});
group('Valid json', () {
group('Function Valid json', () {
void expectEqualParams(
List<Parameter> actualParams,
List<Parameter> expectedParams,
Expand Down
Loading