Skip to content

[jextract] add support for strings in JNI #273

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 16, 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
7 changes: 4 additions & 3 deletions Sources/JExtractSwiftLib/JNI/JNISwift2JavaGenerator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -134,8 +134,8 @@ extension JNISwift2JavaGenerator {
"""
) { printer in
let downcallParameters = zip(decl.functionSignature.parameters, translatedParameters).map { originalParam, translatedParam in
let label = originalParam.argumentLabel ?? originalParam.parameterName ?? ""
return "\(label)\(!label.isEmpty ? ": " : "")\(originalParam.type)(fromJNI: \(translatedParam.0), in: environment!)"
let label = originalParam.argumentLabel.map { "\($0): "} ?? ""
return "\(label)\(originalParam.type)(fromJNI: \(translatedParam.0), in: environment!)"
}
let functionDowncall = "\(swiftModuleName).\(decl.name)(\(downcallParameters.joined(separator: ", ")))"

Expand Down Expand Up @@ -247,7 +247,8 @@ extension SwiftStandardLibraryTypeKind {
case .float: .float
case .double: .double
case .void: .void
case .uint, .uint8, .uint32, .uint64, .unsafeRawPointer, .unsafeMutableRawPointer, .unsafePointer, .unsafeMutablePointer, .unsafeBufferPointer, .unsafeMutableBufferPointer, .string: nil
case .string: .javaLangString
case .uint, .uint8, .uint32, .uint64, .unsafeRawPointer, .unsafeMutableRawPointer, .unsafePointer, .unsafeMutablePointer, .unsafeBufferPointer, .unsafeMutableBufferPointer: nil
}
}
}
43 changes: 43 additions & 0 deletions Tests/JExtractSwiftTests/JNI/JNIModuleTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@ struct JNIModuleTests {
public func otherPrimitives(b: Bool, f: Float, d: Double)
"""

let globalMethodWithString = """
public func copy(_ string: String) -> String
"""

@Test
func generatesModuleJavaClass() throws {
let input = "public func helloWorld()"
Expand Down Expand Up @@ -107,4 +111,43 @@ struct JNIModuleTests {
]
)
}

@Test
func globalMethodWithString_javaBindings() throws {
try assertOutput(
input: globalMethodWithString,
.jni,
.java,
expectedChunks: [
"""
/**
* Downcall to Swift:
* {@snippet lang=swift :
* public func copy(_ string: String) -> String
* }
*/
public static native java.lang.String copy(java.lang.String string);
""",
]
)
}

@Test
func globalMethodWithString_swiftThunks() throws {
try assertOutput(
input: globalMethodWithString,
.jni,
.swift,
detectChunkByInitialLines: 1,
expectedChunks: [
"""
@_cdecl("Java_com_example_swift_SwiftModule_copy")
func swiftjava_SwiftModule_copy__(environment: UnsafeMutablePointer<JNIEnv?>!, thisClass: jclass, string: jstring?) -> jstring? {
let result = SwiftModule.copy(String(fromJNI: string, in: environment!))
return result.getJNIValue(in: environment)
}
""",
]
)
}
}