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

Merged
merged 12 commits into from
Jun 10, 2025
Merged
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 @@ -40,6 +40,12 @@ class PropertyDeclaration extends AstNode
PropertyStatements? getter;
PropertyStatements? setter;

bool unowned;

bool weak;

bool lazy;

bool isStatic;

PropertyDeclaration(
Expand All @@ -54,6 +60,9 @@ class PropertyDeclaration extends AstNode
this.isStatic = false,
this.throws = false,
this.async = false,
this.unowned = false,
this.weak = false,
this.lazy = false,
this.mutating = false})
: assert(!(isConstant && hasSetter)),
assert(!(hasSetter && throws));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -144,8 +144,15 @@ List<String> _generateClassProperty(PropertyDeclaration property) {
if (property.isStatic) {
header.write('static ');
}
final prefixes = [
if (property.unowned) 'unowned',
if (property.weak) 'weak',
];

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

header.write('public var ${property.name}: ${property.type.swiftType} {');
header.write('public ${prefix}var ${property.name}: $propSwiftType {');

final getterLines = [
'get ${generateAnnotations(property)}{',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,13 @@ PropertyDeclaration parsePropertyDeclaration(
type: _parseVariableType(propertySymbolJson, symbolgraph),
hasObjCAnnotation: parseSymbolHasObjcAnnotation(propertySymbolJson),
isConstant: info.constant,
hasSetter: info.constant ? false : info.setter,
isStatic: isStatic,
throws: info.throws,
async: info.async,
unowned: info.unowned,
weak: info.weak,
lazy: info.lazy,
hasSetter: info.constant ? false : info.setter,
);
}

Expand Down Expand Up @@ -72,6 +75,9 @@ bool _findKeywordInFragments(Json json, String keyword) {
typedef ParsedPropertyInfo = ({
bool async,
bool throws,
bool unowned,
bool weak,
bool lazy,
bool constant,
bool getter,
bool setter,
Expand All @@ -84,6 +90,9 @@ ParsedPropertyInfo parsePropertyInfo(Json json) {
constant: _parseVariableIsConstant(json),
async: _findKeywordInFragments(json, 'async'),
throws: _findKeywordInFragments(json, 'throws'),
unowned: _findKeywordInFragments(json, 'unowned'),
weak: _findKeywordInFragments(json, 'weak'),
lazy: _findKeywordInFragments(json, 'lazy'),
getter: getter,
setter: setter,
mutating: _findKeywordInFragments(json, 'mutating')
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,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,33 @@ import Foundation
@objc public class MyClassWrapper: NSObject {
var wrappedInstance: MyClass

@objc public var lazyProperty: Int {
get {
wrappedInstance.lazyProperty
}
set {
wrappedInstance.lazyProperty = newValue
}
}

@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
3 changes: 0 additions & 3 deletions pkgs/swift2objc/test/unit/parse_variable_info_test.dart
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import 'dart:convert';

// import 'package:swift2objc/src/ast/declarations/built_in/built_in_declaration.dart';
import 'package:swift2objc/src/parser/_core/json.dart';
// import 'package:swift2objc/src/parser/_core/parsed_symbolgraph.dart';
import 'package:swift2objc/src/parser/parsers/declaration_parsers/parse_variable_declaration.dart';
import 'package:test/test.dart';

Expand Down Expand Up @@ -303,7 +301,6 @@ void main() {
]'''));

final info = parsePropertyInfo(json);

expect(info.getter, isTrue);
expect(info.async, isTrue);
expect(info.throws, isTrue);
Expand Down