Skip to content

Commit 3b850e0

Browse files
authored
Merge pull request #244 from rintaro/jextract-returning-class
[JExtract] Fix methods returning imported type
2 parents abd05a0 + f9ae342 commit 3b850e0

File tree

3 files changed

+56
-3
lines changed

3 files changed

+56
-3
lines changed

Samples/SwiftKitSampleApp/src/main/java/com/example/swift/HelloJava2Swift.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,9 @@ static void examples() {
6262
obj.voidMethod();
6363
obj.takeIntMethod(42);
6464

65+
MySwiftClass otherObj = MySwiftClass.factory(12, 42, arena);
66+
otherObj.voidMethod();
67+
6568
MySwiftStruct swiftValue = new MySwiftStruct(2222, 1111, arena);
6669
SwiftKit.trace("swiftValue.capacity = " + swiftValue.getCapacity());
6770
}

Sources/JExtractSwift/Swift2JavaTranslator+JavaBindingsPrinting.swift

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -259,11 +259,13 @@ extension Swift2JavaTranslator {
259259
} else if decl.translatedSignature.result.javaResultType == .void {
260260
printer.print("\(downCall);")
261261
} else {
262-
let placeholder = if decl.translatedSignature.result.outParameters.isEmpty {
263-
downCall
262+
let placeholder: String
263+
if decl.translatedSignature.result.outParameters.isEmpty {
264+
placeholder = downCall
264265
} else {
265266
// FIXME: Support cdecl thunk returning a value while populating the out parameters.
266-
"_result"
267+
printer.print("\(downCall);")
268+
placeholder = "_result"
267269
}
268270
let result = decl.translatedSignature.result.conversion.render(&printer, placeholder)
269271

Tests/JExtractSwiftTests/MethodImportTests.swift

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ final class MethodImportTests {
3333
public func globalTakeInt(i: Int)
3434
3535
public func globalTakeIntLongString(i32: Int32, l: Int64, s: String)
36+
37+
public func globalReturnClass() -> MySwiftClass
3638
3739
extension MySwiftClass {
3840
public func helloMemberInExtension()
@@ -183,6 +185,52 @@ final class MethodImportTests {
183185
)
184186
}
185187

188+
@Test("Import: public func globalReturnClass() -> MySwiftClass")
189+
func func_globalReturnClass() throws {
190+
let st = Swift2JavaTranslator(
191+
javaPackage: "com.example.swift",
192+
swiftModuleName: "__FakeModule"
193+
)
194+
st.log.logLevel = .error
195+
196+
try st.analyze(file: "Fake.swift", text: class_interfaceFile)
197+
198+
let funcDecl = st.importedGlobalFuncs.first {
199+
$0.name == "globalReturnClass"
200+
}!
201+
202+
let output = CodePrinter.toString { printer in
203+
st.printFuncDowncallMethod(&printer, funcDecl)
204+
}
205+
206+
assertOutput(
207+
dump: true,
208+
output,
209+
expected:
210+
"""
211+
/**
212+
* Downcall to Swift:
213+
* {@snippet lang=swift :
214+
* public func globalReturnClass() -> MySwiftClass
215+
* }
216+
*/
217+
public static MySwiftClass globalReturnClass(SwiftArena swiftArena$) {
218+
var mh$ = swiftjava___FakeModule_globalReturnClass.HANDLE;
219+
try {
220+
MemorySegment _result = swiftArena$.allocate(MySwiftClass.$LAYOUT);
221+
if (SwiftKit.TRACE_DOWNCALLS) {
222+
SwiftKit.traceDowncall(_result);
223+
}
224+
mh$.invokeExact(_result);
225+
return new MySwiftClass(_result, swiftArena$);
226+
} catch (Throwable ex$) {
227+
throw new AssertionError("should not reach here", ex$);
228+
}
229+
}
230+
"""
231+
)
232+
}
233+
186234
@Test
187235
func method_class_helloMemberFunction() throws {
188236
let st = Swift2JavaTranslator(

0 commit comments

Comments
 (0)