Skip to content

[JExtract] Fix methods returning imported type #244

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 1 commit into from
Jun 4, 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 @@ -62,6 +62,9 @@ static void examples() {
obj.voidMethod();
obj.takeIntMethod(42);

MySwiftClass otherObj = MySwiftClass.factory(12, 42, arena);
otherObj.voidMethod();

MySwiftStruct swiftValue = new MySwiftStruct(2222, 1111, arena);
SwiftKit.trace("swiftValue.capacity = " + swiftValue.getCapacity());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -259,11 +259,13 @@ extension Swift2JavaTranslator {
} else if decl.translatedSignature.result.javaResultType == .void {
printer.print("\(downCall);")
} else {
let placeholder = if decl.translatedSignature.result.outParameters.isEmpty {
downCall
let placeholder: String
if decl.translatedSignature.result.outParameters.isEmpty {
placeholder = downCall
} else {
// FIXME: Support cdecl thunk returning a value while populating the out parameters.
"_result"
printer.print("\(downCall);")
placeholder = "_result"
}
let result = decl.translatedSignature.result.conversion.render(&printer, placeholder)

Expand Down
48 changes: 48 additions & 0 deletions Tests/JExtractSwiftTests/MethodImportTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ final class MethodImportTests {
public func globalTakeInt(i: Int)

public func globalTakeIntLongString(i32: Int32, l: Int64, s: String)

public func globalReturnClass() -> MySwiftClass

extension MySwiftClass {
public func helloMemberInExtension()
Expand Down Expand Up @@ -183,6 +185,52 @@ final class MethodImportTests {
)
}

@Test("Import: public func globalReturnClass() -> MySwiftClass")
func func_globalReturnClass() throws {
let st = Swift2JavaTranslator(
javaPackage: "com.example.swift",
swiftModuleName: "__FakeModule"
)
st.log.logLevel = .error

try st.analyze(file: "Fake.swift", text: class_interfaceFile)

let funcDecl = st.importedGlobalFuncs.first {
$0.name == "globalReturnClass"
}!

let output = CodePrinter.toString { printer in
st.printFuncDowncallMethod(&printer, funcDecl)
}

assertOutput(
dump: true,
output,
expected:
"""
/**
* Downcall to Swift:
* {@snippet lang=swift :
* public func globalReturnClass() -> MySwiftClass
* }
*/
public static MySwiftClass globalReturnClass(SwiftArena swiftArena$) {
var mh$ = swiftjava___FakeModule_globalReturnClass.HANDLE;
try {
MemorySegment _result = swiftArena$.allocate(MySwiftClass.$LAYOUT);
if (SwiftKit.TRACE_DOWNCALLS) {
SwiftKit.traceDowncall(_result);
}
mh$.invokeExact(_result);
return new MySwiftClass(_result, swiftArena$);
} catch (Throwable ex$) {
throw new AssertionError("should not reach here", ex$);
}
}
"""
)
}

@Test
func method_class_helloMemberFunction() throws {
let st = Swift2JavaTranslator(
Expand Down