Skip to content

Commit 3fa3d44

Browse files
authored
Fix allowGlobalAutomatic mode for interfaces (#466)
1 parent 6a75aeb commit 3fa3d44

File tree

2 files changed

+42
-1
lines changed

2 files changed

+42
-1
lines changed

Sources/JExtractSwiftLib/JNI/JNISwift2JavaGenerator+JavaBindingsPrinting.swift

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -424,7 +424,11 @@ extension JNISwift2JavaGenerator {
424424
)
425425
}
426426

427-
private func printJavaBindingWrapperMethod(_ printer: inout CodePrinter, _ decl: ImportedFunc, signaturesOnly: Bool) {
427+
private func printJavaBindingWrapperMethod(
428+
_ printer: inout CodePrinter,
429+
_ decl: ImportedFunc,
430+
signaturesOnly: Bool
431+
) {
428432
guard let translatedDecl = translatedDecl(for: decl) else {
429433
fatalError("Decl was not translated, \(decl)")
430434
}
@@ -470,6 +474,13 @@ extension JNISwift2JavaGenerator {
470474
if let importedFunc {
471475
printDeclDocumentation(&printer, importedFunc)
472476
}
477+
var modifiers = modifiers
478+
479+
// If we are a protocol, we emit this as default method
480+
if importedFunc?.parentType?.asNominalTypeDeclaration?.kind == .protocol {
481+
modifiers.insert("default", at: 1)
482+
}
483+
473484
printer.printBraceBlock("\(annotationsStr)\(modifiers.joined(separator: " ")) \(resultType) \(translatedDecl.name)(\(parametersStr))\(throwsClause)") { printer in
474485
let globalArenaName = "SwiftMemoryManagement.GLOBAL_SWIFT_JAVA_ARENA"
475486
let arguments = translatedDecl.translatedFunctionSignature.parameters.map(\.parameter.name) + [globalArenaName]

Tests/JExtractSwiftTests/MemoryManagementModeTests.swift

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,4 +74,34 @@ struct MemoryManagementModeTests {
7474
]
7575
)
7676
}
77+
78+
@Test
79+
func allowGlobalAutomatic_protocol() throws {
80+
var config = Configuration()
81+
config.memoryManagementMode = .allowGlobalAutomatic
82+
83+
try assertOutput(
84+
input:
85+
"""
86+
public class MyClass {}
87+
88+
public protocol MyProtocol {
89+
public func f() -> MyClass
90+
}
91+
""",
92+
config: config,
93+
.jni, .java,
94+
detectChunkByInitialLines: 1,
95+
expectedChunks: [
96+
"""
97+
public default MyClass f() {
98+
return f(SwiftMemoryManagement.GLOBAL_SWIFT_JAVA_ARENA);
99+
}
100+
""",
101+
"""
102+
public MyClass f(SwiftArena swiftArena$);
103+
"""
104+
]
105+
)
106+
}
77107
}

0 commit comments

Comments
 (0)