Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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 @@ -181,9 +181,9 @@ extension JNISwift2JavaGenerator {
return
}

var modifiers = "public"
var modifiers = ["public"]
if decl.isStatic || decl.isInitializer || !decl.hasParent {
modifiers.append(" static")
modifiers.append("static")
}

let translatedSignature = translatedDecl.translatedFunctionSignature
Expand All @@ -194,9 +194,12 @@ extension JNISwift2JavaGenerator {
}
let throwsClause = decl.isThrowing ? " throws Exception" : ""

let modifiersStr = modifiers.joined(separator: " ")
let parametersStr = parameters.joined(separator: ", ")

printDeclDocumentation(&printer, decl)
printer.printBraceBlock(
"\(modifiers) \(resultType) \(translatedDecl.name)(\(parameters.joined(separator: ", ")))\(throwsClause)"
"\(modifiersStr) \(resultType) \(translatedDecl.name)(\(parametersStr))\(throwsClause)"
) { printer in
printDowncall(&printer, decl)
}
Expand All @@ -208,14 +211,13 @@ extension JNISwift2JavaGenerator {
let translatedDecl = translatedDecl(for: decl)! // Will always call with valid decl
let nativeSignature = translatedDecl.nativeFunctionSignature
let resultType = nativeSignature.result.javaType
let nativeName = "$\(translatedDecl.name)"
var parameters = nativeSignature.parameters
if let selfParameter = nativeSignature.selfParameter {
parameters.append(selfParameter)
}
let renderedParameters = parameters.map { "\($0.javaParameter.type) \($0.javaParameter.name)"}.joined(separator: ", ")

printer.print("private static native \(resultType) \(nativeName)(\(renderedParameters));")
printer.print("private static native \(resultType) \(translatedDecl.nativeFunctionName)(\(renderedParameters));")
}

private func printDowncall(
Expand All @@ -241,7 +243,7 @@ extension JNISwift2JavaGenerator {
//=== Part 3: Downcall.
// TODO: If we always generate a native method and a "public" method, we can actually choose our own thunk names
// using the registry?
let downcall = "\(translatedDecl.parentName).$\(translatedDecl.name)(\(arguments.joined(separator: ", ")))"
let downcall = "\(translatedDecl.parentName).\(translatedDecl.nativeFunctionName)(\(arguments.joined(separator: ", ")))"

//=== Part 4: Convert the return value.
if translatedFunctionSignature.resultType.javaType.isVoid {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ extension JNISwift2JavaGenerator {

return TranslatedFunctionDecl(
name: javaName,
nativeFunctionName: "$\(javaName)",
parentName: parentName,
translatedFunctionSignature: translatedFunctionSignature,
nativeFunctionSignature: nativeFunctionSignature
Expand Down Expand Up @@ -161,6 +162,9 @@ extension JNISwift2JavaGenerator {
/// Java function name
let name: String

/// The name of the native function
let nativeFunctionName: String

/// The name of the Java parent scope this function is declared in
let parentName: String

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ extension JNISwift2JavaGenerator {
// so we pass the pointer.
return NativeResult(
javaType: .long,
conversion: .getJNIValue(.allocateSwiftValue(name: "_result", swiftType: swiftResult.type))
conversion: .getJNIValue(.allocateSwiftValue(name: "result", swiftType: swiftResult.type))
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks :)

)
}
}
Expand Down Expand Up @@ -189,14 +189,16 @@ extension JNISwift2JavaGenerator {
return "\(inner)$"

case .allocateSwiftValue(let name, let swiftType):
let pointerName = "\(name)$"
let bitsName = "\(name)Bits$"
printer.print(
"""
let \(name)$ = UnsafeMutablePointer<\(swiftType)>.allocate(capacity: 1)
\(name)$.initialize(to: \(placeholder))
let \(name)Bits$ = Int64(Int(bitPattern: \(name)$))
let \(pointerName) = UnsafeMutablePointer<\(swiftType)>.allocate(capacity: 1)
\(pointerName).initialize(to: \(placeholder))
let \(bitsName) = Int64(Int(bitPattern: \(pointerName)))
"""
)
return "\(name)Bits$"
return bitsName

case .pointee(let inner):
let inner = inner.render(&printer, placeholder)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ extension JNISwift2JavaGenerator {

printCDecl(
&printer,
javaMethodName: "$\(translatedDecl.name)",
javaMethodName: translatedDecl.nativeFunctionName,
parentName: translatedDecl.parentName,
parameters: parameters.map(\.javaParameter),
resultType: nativeSignature.result.javaType.jniType
Expand Down
3 changes: 3 additions & 0 deletions Sources/JExtractSwiftLib/JNI/JNIType.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@

import JavaTypes

/// Represents types that are able to be passed over a JNI boundary.
///
/// - SeeAlso: https://docs.oracle.com/javase/8/docs/technotes/guides/jni/spec/types.html
enum JNIType {
case jboolean
case jfloat
Expand Down
1 change: 0 additions & 1 deletion Tests/JExtractSwiftTests/Asserts/TextAssertions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,6 @@ func assertOutput(
print("==== ---------------------------------------------------------------")

#expect(output.contains(expectedChunk), sourceLocation: sourceLocation)
// fatalError("Failed: \(filePath):\(line)")
continue
}

Expand Down
24 changes: 12 additions & 12 deletions Tests/JExtractSwiftTests/JNI/JNIClassTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -185,19 +185,19 @@ struct JNIClassTests {
"""
@_cdecl("Java_com_example_swift_MyClass__00024init__JJ")
func Java_com_example_swift_MyClass__00024init__JJ(environment: UnsafeMutablePointer<JNIEnv?>!, thisClass: jclass, x: jlong, y: jlong) -> jlong {
let _result$ = UnsafeMutablePointer<MyClass>.allocate(capacity: 1)
_result$.initialize(to: MyClass.init(x: Int64(fromJNI: x, in: environment!), y: Int64(fromJNI: y, in: environment!)))
let _resultBits$ = Int64(Int(bitPattern: _result$))
return _resultBits$.getJNIValue(in: environment!)
let result$ = UnsafeMutablePointer<MyClass>.allocate(capacity: 1)
result$.initialize(to: MyClass.init(x: Int64(fromJNI: x, in: environment!), y: Int64(fromJNI: y, in: environment!)))
let resultBits$ = Int64(Int(bitPattern: result$))
return resultBits$.getJNIValue(in: environment!)
}
""",
"""
@_cdecl("Java_com_example_swift_MyClass__00024init__")
func Java_com_example_swift_MyClass__00024init__(environment: UnsafeMutablePointer<JNIEnv?>!, thisClass: jclass) -> jlong {
let _result$ = UnsafeMutablePointer<MyClass>.allocate(capacity: 1)
_result$.initialize(to: MyClass.init())
let _resultBits$ = Int64(Int(bitPattern: _result$))
return _resultBits$.getJNIValue(in: environment!)
let result$ = UnsafeMutablePointer<MyClass>.allocate(capacity: 1)
result$.initialize(to: MyClass.init())
let resultBits$ = Int64(Int(bitPattern: result$))
return resultBits$.getJNIValue(in: environment!)
}
"""
]
Expand Down Expand Up @@ -320,10 +320,10 @@ struct JNIClassTests {
guard let self$ = UnsafeMutablePointer<MyClass>(bitPattern: selfBits$) else {
fatalError("self memory address was null in call to \\(#function)!")
}
let _result$ = UnsafeMutablePointer<MyClass>.allocate(capacity: 1)
_result$.initialize(to: self$.pointee.copy())
let _resultBits$ = Int64(Int(bitPattern: _result$))
return _resultBits$.getJNIValue(in: environment!)
let result$ = UnsafeMutablePointer<MyClass>.allocate(capacity: 1)
result$.initialize(to: self$.pointee.copy())
let resultBits$ = Int64(Int(bitPattern: result$))
return resultBits$.getJNIValue(in: environment!)
}
""",
]
Expand Down
8 changes: 4 additions & 4 deletions Tests/JExtractSwiftTests/JNI/JNIStructTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -119,10 +119,10 @@ struct JNIStructTests {
"""
@_cdecl("Java_com_example_swift_MyStruct__00024init__JJ")
func Java_com_example_swift_MyStruct__00024init__JJ(environment: UnsafeMutablePointer<JNIEnv?>!, thisClass: jclass, x: jlong, y: jlong) -> jlong {
let _result$ = UnsafeMutablePointer<MyStruct>.allocate(capacity: 1)
_result$.initialize(to: MyStruct.init(x: Int64(fromJNI: x, in: environment!), y: Int64(fromJNI: y, in: environment!)))
let _resultBits$ = Int64(Int(bitPattern: _result$))
return _resultBits$.getJNIValue(in: environment!)
let result$ = UnsafeMutablePointer<MyStruct>.allocate(capacity: 1)
result$.initialize(to: MyStruct.init(x: Int64(fromJNI: x, in: environment!), y: Int64(fromJNI: y, in: environment!)))
let resultBits$ = Int64(Int(bitPattern: result$))
return resultBits$.getJNIValue(in: environment!)
}
"""
]
Expand Down
Loading