Skip to content

Commit f127f35

Browse files
[swift2objc] Support Swift ARC features (#2055)
1 parent fd9415b commit f127f35

File tree

8 files changed

+69
-7
lines changed

8 files changed

+69
-7
lines changed

pkgs/swift2objc/lib/src/ast/declarations/compounds/members/property_declaration.dart

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,12 @@ class PropertyDeclaration extends AstNode
4040
PropertyStatements? getter;
4141
PropertyStatements? setter;
4242

43+
bool unowned;
44+
45+
bool weak;
46+
47+
bool lazy;
48+
4349
bool isStatic;
4450

4551
PropertyDeclaration(
@@ -54,6 +60,9 @@ class PropertyDeclaration extends AstNode
5460
this.isStatic = false,
5561
this.throws = false,
5662
this.async = false,
63+
this.unowned = false,
64+
this.weak = false,
65+
this.lazy = false,
5766
this.mutating = false})
5867
: assert(!(isConstant && hasSetter)),
5968
assert(!(hasSetter && throws));

pkgs/swift2objc/lib/src/generator/generators/class_generator.dart

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,8 +144,15 @@ List<String> _generateClassProperty(PropertyDeclaration property) {
144144
if (property.isStatic) {
145145
header.write('static ');
146146
}
147+
final prefixes = [
148+
if (property.unowned) 'unowned',
149+
if (property.weak) 'weak',
150+
];
151+
152+
var prefix = prefixes.isEmpty ? '' : '${prefixes.join(' ')} ';
153+
var propSwiftType = property.type.swiftType;
147154

148-
header.write('public var ${property.name}: ${property.type.swiftType} {');
155+
header.write('public ${prefix}var ${property.name}: $propSwiftType {');
149156

150157
final getterLines = [
151158
'get ${generateAnnotations(property)}{',

pkgs/swift2objc/lib/src/parser/parsers/declaration_parsers/parse_variable_declaration.dart

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,13 @@ PropertyDeclaration parsePropertyDeclaration(
2222
type: _parseVariableType(propertySymbolJson, symbolgraph),
2323
hasObjCAnnotation: parseSymbolHasObjcAnnotation(propertySymbolJson),
2424
isConstant: info.constant,
25-
hasSetter: info.constant ? false : info.setter,
2625
isStatic: isStatic,
2726
throws: info.throws,
2827
async: info.async,
28+
unowned: info.unowned,
29+
weak: info.weak,
30+
lazy: info.lazy,
31+
hasSetter: info.constant ? false : info.setter,
2932
);
3033
}
3134

@@ -72,6 +75,9 @@ bool _findKeywordInFragments(Json json, String keyword) {
7275
typedef ParsedPropertyInfo = ({
7376
bool async,
7477
bool throws,
78+
bool unowned,
79+
bool weak,
80+
bool lazy,
7581
bool constant,
7682
bool getter,
7783
bool setter,
@@ -84,6 +90,9 @@ ParsedPropertyInfo parsePropertyInfo(Json json) {
8490
constant: _parseVariableIsConstant(json),
8591
async: _findKeywordInFragments(json, 'async'),
8692
throws: _findKeywordInFragments(json, 'throws'),
93+
unowned: _findKeywordInFragments(json, 'unowned'),
94+
weak: _findKeywordInFragments(json, 'weak'),
95+
lazy: _findKeywordInFragments(json, 'lazy'),
8796
getter: getter,
8897
setter: setter,
8998
mutating: _findKeywordInFragments(json, 'mutating')

pkgs/swift2objc/lib/src/transformer/transformers/transform_variable.dart

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,13 @@ Declaration _transformVariable(
119119
isConstant: originalVariable.isConstant,
120120
throws: originalVariable.throws,
121121
async: originalVariable.async,
122+
unowned: originalVariable is PropertyDeclaration
123+
? originalVariable.unowned
124+
: false,
125+
lazy:
126+
originalVariable is PropertyDeclaration ? originalVariable.lazy : false,
127+
weak:
128+
originalVariable is PropertyDeclaration ? originalVariable.weak : false,
122129
);
123130

124131
final getterStatements = _generateGetterStatements(

pkgs/swift2objc/test/integration/classes_and_properties_input.swift

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,17 +29,23 @@ public class MyClass {
2929

3030
public let representableConstantProperty: Int
3131

32+
public weak var weakProperty: MyOtherClass?
33+
public unowned var unownedProperty: MyOtherClass
34+
public lazy var lazyProperty: Int = { 1 }();
35+
3236

3337
init(
3438
customVariableProperty: MyOtherClass,
3539
customConstantProperty: MyOtherClass,
3640
representableVariableProperty: Int,
37-
representableConstantProperty: Int
41+
representableConstantProperty: Int,
42+
unownedProperty: MyOtherClass
3843
) {
3944
self.customVariableProperty = customVariableProperty
4045
self.customConstantProperty = customConstantProperty
4146
self.representableVariableProperty = representableVariableProperty
4247
self.representableConstantProperty = representableConstantProperty
48+
self.unownedProperty = unownedProperty
4349
}
4450
}
4551

pkgs/swift2objc/test/integration/classes_and_properties_output.swift

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,33 @@ import Foundation
1414
@objc public class MyClassWrapper: NSObject {
1515
var wrappedInstance: MyClass
1616

17+
@objc public var lazyProperty: Int {
18+
get {
19+
wrappedInstance.lazyProperty
20+
}
21+
set {
22+
wrappedInstance.lazyProperty = newValue
23+
}
24+
}
25+
26+
@objc public weak var weakProperty: MyOtherClassWrapper? {
27+
get {
28+
wrappedInstance.weakProperty == nil ? nil : MyOtherClassWrapper(wrappedInstance.weakProperty!)
29+
}
30+
set {
31+
wrappedInstance.weakProperty = newValue?.wrappedInstance
32+
}
33+
}
34+
35+
@objc public unowned var unownedProperty: MyOtherClassWrapper {
36+
get {
37+
MyOtherClassWrapper(wrappedInstance.unownedProperty)
38+
}
39+
set {
40+
wrappedInstance.unownedProperty = newValue.wrappedInstance
41+
}
42+
}
43+
1744
@objc public var customGetterProperty: MyOtherClassWrapper {
1845
get {
1946
MyOtherClassWrapper(wrappedInstance.customGetterProperty)

pkgs/swift2objc/test/unit/parse_function_info_test.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ void main() {
1818
decl.id: ParsedSymbol(json: Json(null), declaration: decl)
1919
};
2020
final emptySymbolgraph = ParsedSymbolgraph(parsedSymbols, {});
21-
group('Valid json', () {
21+
group('Function Valid json', () {
2222
void expectEqualParams(
2323
List<Parameter> actualParams,
2424
List<Parameter> expectedParams,

pkgs/swift2objc/test/unit/parse_variable_info_test.dart

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
import 'dart:convert';
22

3-
// import 'package:swift2objc/src/ast/declarations/built_in/built_in_declaration.dart';
43
import 'package:swift2objc/src/parser/_core/json.dart';
5-
// import 'package:swift2objc/src/parser/_core/parsed_symbolgraph.dart';
64
import 'package:swift2objc/src/parser/parsers/declaration_parsers/parse_variable_declaration.dart';
75
import 'package:test/test.dart';
86

@@ -303,7 +301,6 @@ void main() {
303301
]'''));
304302

305303
final info = parsePropertyInfo(json);
306-
307304
expect(info.getter, isTrue);
308305
expect(info.async, isTrue);
309306
expect(info.throws, isTrue);

0 commit comments

Comments
 (0)