Skip to content

Commit aa2d017

Browse files
committed
getter and setter generated for stored property
1 parent a8f1cc9 commit aa2d017

File tree

5 files changed

+142
-29
lines changed

5 files changed

+142
-29
lines changed
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// This source file is part of the Swift.org open source project
4+
//
5+
// Copyright (c) 2024 Apple Inc. and the Swift.org project authors
6+
// Licensed under Apache License v2.0
7+
//
8+
// See LICENSE.txt for license information
9+
// See CONTRIBUTORS.txt for the list of Swift.org project authors
10+
//
11+
// SPDX-License-Identifier: Apache-2.0
12+
//
13+
//===----------------------------------------------------------------------===//
14+
15+
extension String {
16+
17+
// TODO: naive implementation good enough for our simple case `methodMethodSomething` -> `MethodSomething`
18+
var toCamelCase: String {
19+
guard let f = first else {
20+
return self
21+
}
22+
23+
return "\(f.uppercased())\(String(dropFirst()))"
24+
}
25+
}

Sources/JExtractSwift/ImportedDecls+Printing.swift

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,4 +62,33 @@ extension VariableAccessorKind {
6262
case .set: "$set"
6363
}
6464
}
65+
66+
func renderMethodName(_ decl: ImportedFunc) -> String? {
67+
switch self {
68+
case .get: "get\(decl.identifier.toCamelCase)"
69+
case .set: "set\(decl.identifier.toCamelCase)"
70+
}
71+
}
72+
}
73+
74+
extension Optional where Wrapped == VariableAccessorKind {
75+
public var renderDescFieldName: String {
76+
self?.renderDescFieldName ?? "DESC"
77+
}
78+
79+
public var renderAddrFieldName: String {
80+
self?.renderAddrFieldName ?? "ADDR"
81+
}
82+
83+
public var renderHandleFieldName: String {
84+
self?.renderHandleFieldName ?? "HANDLE"
85+
}
86+
87+
public var renderMethodNameSegment: String {
88+
self?.renderMethodNameSegment ?? ""
89+
}
90+
91+
func renderMethodName(_ decl: ImportedFunc) -> String {
92+
self?.renderMethodName(decl) ?? decl.baseIdentifier
93+
}
6594
}

Sources/JExtractSwift/Swift2JavaTranslator+Printing.swift

Lines changed: 39 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ extension Swift2JavaTranslator {
102102
printModuleClass(&printer) { printer in
103103
// TODO: print all "static" methods
104104
for decl in importedGlobalFuncs {
105-
printDowncallMethods(&printer, decl)
105+
printFunctionDowncallMethods(&printer, decl)
106106
}
107107
}
108108
}
@@ -133,7 +133,7 @@ extension Swift2JavaTranslator {
133133

134134
// Methods
135135
for funcDecl in decl.methods {
136-
printDowncallMethods(&printer, funcDecl)
136+
printFunctionDowncallMethods(&printer, funcDecl)
137137
}
138138
}
139139
}
@@ -456,7 +456,7 @@ extension Swift2JavaTranslator {
456456
)
457457
}
458458

459-
public func printDowncallMethods(_ printer: inout CodePrinter, _ decl: ImportedFunc) {
459+
public func printFunctionDowncallMethods(_ printer: inout CodePrinter, _ decl: ImportedFunc) {
460460
printer.printSeparator(decl.identifier)
461461

462462
printer.printTypeDecl("private static class \(decl.baseIdentifier)") { printer in
@@ -551,6 +551,7 @@ extension Swift2JavaTranslator {
551551
}
552552
}
553553

554+
// First print all the supporting infra
554555
for accessorKind in decl.supportedAccessorKinds {
555556
guard let accessor = decl.accessorFunc(kind: accessorKind) else {
556557
log.warning("Skip print for \(accessorKind) of \(decl.identifier)!")
@@ -561,12 +562,20 @@ extension Swift2JavaTranslator {
561562
printFunctionAddressMethod(&printer, decl: accessor, accessorKind: accessorKind)
562563
}
563564

564-
// Render the basic "make the downcall" function
565-
if decl.hasParent {
566-
// printFuncDowncallMethod(&printer, decl: decl, selfVariant: .memorySegment)
567-
// printFuncDowncallMethod(&printer, decl: decl, selfVariant: .wrapper)
568-
} else {
569-
// printFuncDowncallMethod(&printer, decl: decl, selfVariant: nil)
565+
// Then print the actual downcall methods
566+
for accessorKind in decl.supportedAccessorKinds {
567+
guard let accessor = decl.accessorFunc(kind: accessorKind) else {
568+
log.warning("Skip print for \(accessorKind) of \(decl.identifier)!")
569+
continue
570+
}
571+
572+
// Render the basic "make the downcall" function
573+
if decl.hasParent {
574+
printFuncDowncallMethod(&printer, decl: accessor, selfVariant: .memorySegment, accessorKind: accessorKind)
575+
printFuncDowncallMethod(&printer, decl: accessor, selfVariant: .wrapper, accessorKind: accessorKind)
576+
} else {
577+
printFuncDowncallMethod(&printer, decl: accessor, selfVariant: nil, accessorKind: accessorKind)
578+
}
570579
}
571580
}
572581

@@ -594,7 +603,8 @@ extension Swift2JavaTranslator {
594603
public func printFuncDowncallMethod(
595604
_ printer: inout CodePrinter,
596605
decl: ImportedFunc,
597-
selfVariant: SelfParameterVariant?
606+
selfVariant: SelfParameterVariant?,
607+
accessorKind: VariableAccessorKind? = nil
598608
) {
599609
let returnTy = decl.returnType.javaType
600610

@@ -605,32 +615,37 @@ extension Swift2JavaTranslator {
605615
maybeReturnCast = "return (\(returnTy))"
606616
}
607617

618+
// TODO: we could copy the Swift method's documentation over here, that'd be great UX
619+
let javaDocComment: String =
620+
"""
621+
/**
622+
* Downcall to Swift:
623+
\(decl.renderCommentSnippet ?? "* ")
624+
*/
625+
"""
626+
627+
// An identifier may be "getX", "setX" or just the plain method name
628+
let identifier = accessorKind.renderMethodName(decl)
629+
608630
if selfVariant == SelfParameterVariant.wrapper {
609631
// delegate to the MemorySegment "self" accepting overload
610632
printer.print(
611633
"""
612-
/**
613-
* {@snippet lang=swift :
614-
* \(/*TODO: make a printSnippet func*/decl.syntax ?? "")
615-
* }
616-
*/
617-
public \(returnTy) \(decl.baseIdentifier)(\(renderJavaParamDecls(decl, selfVariant: .wrapper))) {
618-
\(maybeReturnCast) \(decl.baseIdentifier)(\(renderForwardParams(decl, selfVariant: .wrapper)));
634+
\(javaDocComment)
635+
public \(returnTy) \(identifier)(\(renderJavaParamDecls(decl, selfVariant: .wrapper))) {
636+
\(maybeReturnCast) \(identifier)(\(renderForwardParams(decl, selfVariant: .wrapper)));
619637
}
620638
"""
621639
)
622640
return
623641
}
624642

643+
let handleName = accessorKind.renderHandleFieldName
625644
printer.print(
626645
"""
627-
/**
628-
* {@snippet lang=swift :
629-
* \(/*TODO: make a printSnippet func*/decl.syntax ?? "")
630-
* }
631-
*/
632-
public static \(returnTy) \(decl.baseIdentifier)(\(renderJavaParamDecls(decl, selfVariant: selfVariant))) {
633-
var mh$ = \(decl.baseIdentifier).HANDLE;
646+
\(javaDocComment)
647+
public static \(returnTy) \(identifier)(\(renderJavaParamDecls(decl, selfVariant: selfVariant))) {
648+
var mh$ = \(decl.baseIdentifier).\(handleName);
634649
try {
635650
if (TRACE_DOWNCALLS) {
636651
traceDowncall(\(renderForwardParams(decl, selfVariant: .memorySegment)));

Tests/JExtractSwiftTests/FuncImportTests.swift

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ final class MethodImportTests {
6262
}
6363
"""
6464

65-
@Test
65+
@Test("Import: public func helloWorld()")
6666
func method_helloWorld() async throws {
6767
let st = Swift2JavaTranslator(
6868
javaPackage: "com.example.swift",
@@ -102,8 +102,8 @@ final class MethodImportTests {
102102
)
103103
}
104104

105-
@Test
106-
func method_globalTakeInt() async throws {
105+
@Test("Import: public func globalTakeInt(i: Int)")
106+
func func_globalTakeInt() async throws {
107107
let st = Swift2JavaTranslator(
108108
javaPackage: "com.example.swift",
109109
swiftModuleName: "__FakeModule"
@@ -144,8 +144,8 @@ final class MethodImportTests {
144144
)
145145
}
146146

147-
@Test
148-
func method_globalTakeIntLongString() async throws {
147+
@Test("Import: public func globalTakeIntLongString(i32: Int32, l: Int64, s: String)")
148+
func func_globalTakeIntLongString() async throws {
149149
let st = Swift2JavaTranslator(
150150
javaPackage: "com.example.swift",
151151
swiftModuleName: "__FakeModule"

Tests/JExtractSwiftTests/VariableImportTests.swift

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,50 @@ final class VariableImportTests {
117117
public static MemorySegment counterInt$set$address() {
118118
return counterInt.ADDR_SET;
119119
}
120+
/**
121+
* Downcall to Swift:
122+
*
123+
*/
124+
public static long getCounterInt(java.lang.foreign.MemorySegment self$) {
125+
var mh$ = counterInt.HANDLE_GET;
126+
try {
127+
if (TRACE_DOWNCALLS) {
128+
traceDowncall(self$);
129+
}
130+
return (long) mh$.invokeExact(self$);
131+
} catch (Throwable ex$) {
132+
throw new AssertionError("should not reach here", ex$);
133+
}
134+
}
135+
/**
136+
* Downcall to Swift:
137+
*
138+
*/
139+
public long getCounterInt() {
140+
return (long) getCounterInt($memorySegment());
141+
}
142+
/**
143+
* Downcall to Swift:
144+
*
145+
*/
146+
public static void setCounterInt(long newValue, java.lang.foreign.MemorySegment self$) {
147+
var mh$ = counterInt.HANDLE_SET;
148+
try {
149+
if (TRACE_DOWNCALLS) {
150+
traceDowncall(newValue, self$);
151+
}
152+
mh$.invokeExact(newValue, self$);
153+
} catch (Throwable ex$) {
154+
throw new AssertionError("should not reach here", ex$);
155+
}
156+
}
157+
/**
158+
* Downcall to Swift:
159+
*
160+
*/
161+
public void setCounterInt(long newValue) {
162+
setCounterInt(newValue, $memorySegment());
163+
}
120164
"""
121165
)
122166
}

0 commit comments

Comments
 (0)