Skip to content

Commit 6a2f679

Browse files
committed
PR feedback
1 parent bb2808d commit 6a2f679

File tree

8 files changed

+39
-29
lines changed

8 files changed

+39
-29
lines changed

Sources/JExtractSwiftLib/JNI/JNISwift2JavaGenerator+JavaBindingsPrinting.swift

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -181,9 +181,9 @@ extension JNISwift2JavaGenerator {
181181
return
182182
}
183183

184-
var modifiers = "public"
184+
var modifiers = ["public"]
185185
if decl.isStatic || decl.isInitializer || !decl.hasParent {
186-
modifiers.append(" static")
186+
modifiers.append("static")
187187
}
188188

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

197+
let modifiersStr = modifiers.joined(separator: " ")
198+
let parametersStr = parameters.joined(separator: ", ")
199+
197200
printDeclDocumentation(&printer, decl)
198201
printer.printBraceBlock(
199-
"\(modifiers) \(resultType) \(translatedDecl.name)(\(parameters.joined(separator: ", ")))\(throwsClause)"
202+
"\(modifiersStr) \(resultType) \(translatedDecl.name)(\(parametersStr))\(throwsClause)"
200203
) { printer in
201204
printDowncall(&printer, decl)
202205
}
@@ -208,14 +211,13 @@ extension JNISwift2JavaGenerator {
208211
let translatedDecl = translatedDecl(for: decl)! // Will always call with valid decl
209212
let nativeSignature = translatedDecl.nativeFunctionSignature
210213
let resultType = nativeSignature.result.javaType
211-
let nativeName = "$\(translatedDecl.name)"
212214
var parameters = nativeSignature.parameters
213215
if let selfParameter = nativeSignature.selfParameter {
214216
parameters.append(selfParameter)
215217
}
216218
let renderedParameters = parameters.map { "\($0.javaParameter.type) \($0.javaParameter.name)"}.joined(separator: ", ")
217219

218-
printer.print("private static native \(resultType) \(nativeName)(\(renderedParameters));")
220+
printer.print("private static native \(resultType) \(translatedDecl.nativeFunctionName)(\(renderedParameters));")
219221
}
220222

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

246248
//=== Part 4: Convert the return value.
247249
if translatedFunctionSignature.resultType.javaType.isVoid {

Sources/JExtractSwiftLib/JNI/JNISwift2JavaGenerator+JavaTranslation.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ extension JNISwift2JavaGenerator {
6161

6262
return TranslatedFunctionDecl(
6363
name: javaName,
64+
nativeFunctionName: "$\(javaName)",
6465
parentName: parentName,
6566
translatedFunctionSignature: translatedFunctionSignature,
6667
nativeFunctionSignature: nativeFunctionSignature
@@ -161,6 +162,9 @@ extension JNISwift2JavaGenerator {
161162
/// Java function name
162163
let name: String
163164

165+
/// The name of the native function
166+
let nativeFunctionName: String
167+
164168
/// The name of the Java parent scope this function is declared in
165169
let parentName: String
166170

Sources/JExtractSwiftLib/JNI/JNISwift2JavaGenerator+NativeTranslation.swift

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ extension JNISwift2JavaGenerator {
110110
// so we pass the pointer.
111111
return NativeResult(
112112
javaType: .long,
113-
conversion: .getJNIValue(.allocateSwiftValue(name: "_result", swiftType: swiftResult.type))
113+
conversion: .getJNIValue(.allocateSwiftValue(name: "result", swiftType: swiftResult.type))
114114
)
115115
}
116116
}
@@ -189,14 +189,16 @@ extension JNISwift2JavaGenerator {
189189
return "\(inner)$"
190190

191191
case .allocateSwiftValue(let name, let swiftType):
192+
let pointerName = "\(name)$"
193+
let bitsName = "\(name)Bits$"
192194
printer.print(
193195
"""
194-
let \(name)$ = UnsafeMutablePointer<\(swiftType)>.allocate(capacity: 1)
195-
\(name)$.initialize(to: \(placeholder))
196-
let \(name)Bits$ = Int64(Int(bitPattern: \(name)$))
196+
let \(pointerName) = UnsafeMutablePointer<\(swiftType)>.allocate(capacity: 1)
197+
\(pointerName).initialize(to: \(placeholder))
198+
let \(bitsName) = Int64(Int(bitPattern: \(pointerName)))
197199
"""
198200
)
199-
return "\(name)Bits$"
201+
return bitsName
200202

201203
case .pointee(let inner):
202204
let inner = inner.render(&printer, placeholder)

Sources/JExtractSwiftLib/JNI/JNISwift2JavaGenerator+SwiftThunkPrinting.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ extension JNISwift2JavaGenerator {
133133

134134
printCDecl(
135135
&printer,
136-
javaMethodName: "$\(translatedDecl.name)",
136+
javaMethodName: translatedDecl.nativeFunctionName,
137137
parentName: translatedDecl.parentName,
138138
parameters: parameters.map(\.javaParameter),
139139
resultType: nativeSignature.result.javaType.jniType

Sources/JExtractSwiftLib/JNI/JNIType.swift

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@
1414

1515
import JavaTypes
1616

17+
/// Represents types that are able to be passed over a JNI boundary.
18+
///
19+
/// - SeeAlso: https://docs.oracle.com/javase/8/docs/technotes/guides/jni/spec/types.html
1720
enum JNIType {
1821
case jboolean
1922
case jfloat

Tests/JExtractSwiftTests/Asserts/TextAssertions.swift

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,6 @@ func assertOutput(
115115
print("==== ---------------------------------------------------------------")
116116

117117
#expect(output.contains(expectedChunk), sourceLocation: sourceLocation)
118-
// fatalError("Failed: \(filePath):\(line)")
119118
continue
120119
}
121120

Tests/JExtractSwiftTests/JNI/JNIClassTests.swift

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -185,19 +185,19 @@ struct JNIClassTests {
185185
"""
186186
@_cdecl("Java_com_example_swift_MyClass__00024init__JJ")
187187
func Java_com_example_swift_MyClass__00024init__JJ(environment: UnsafeMutablePointer<JNIEnv?>!, thisClass: jclass, x: jlong, y: jlong) -> jlong {
188-
let _result$ = UnsafeMutablePointer<MyClass>.allocate(capacity: 1)
189-
_result$.initialize(to: MyClass.init(x: Int64(fromJNI: x, in: environment!), y: Int64(fromJNI: y, in: environment!)))
190-
let _resultBits$ = Int64(Int(bitPattern: _result$))
191-
return _resultBits$.getJNIValue(in: environment!)
188+
let result$ = UnsafeMutablePointer<MyClass>.allocate(capacity: 1)
189+
result$.initialize(to: MyClass.init(x: Int64(fromJNI: x, in: environment!), y: Int64(fromJNI: y, in: environment!)))
190+
let resultBits$ = Int64(Int(bitPattern: result$))
191+
return resultBits$.getJNIValue(in: environment!)
192192
}
193193
""",
194194
"""
195195
@_cdecl("Java_com_example_swift_MyClass__00024init__")
196196
func Java_com_example_swift_MyClass__00024init__(environment: UnsafeMutablePointer<JNIEnv?>!, thisClass: jclass) -> jlong {
197-
let _result$ = UnsafeMutablePointer<MyClass>.allocate(capacity: 1)
198-
_result$.initialize(to: MyClass.init())
199-
let _resultBits$ = Int64(Int(bitPattern: _result$))
200-
return _resultBits$.getJNIValue(in: environment!)
197+
let result$ = UnsafeMutablePointer<MyClass>.allocate(capacity: 1)
198+
result$.initialize(to: MyClass.init())
199+
let resultBits$ = Int64(Int(bitPattern: result$))
200+
return resultBits$.getJNIValue(in: environment!)
201201
}
202202
"""
203203
]
@@ -320,10 +320,10 @@ struct JNIClassTests {
320320
guard let self$ = UnsafeMutablePointer<MyClass>(bitPattern: selfBits$) else {
321321
fatalError("self memory address was null in call to \\(#function)!")
322322
}
323-
let _result$ = UnsafeMutablePointer<MyClass>.allocate(capacity: 1)
324-
_result$.initialize(to: self$.pointee.copy())
325-
let _resultBits$ = Int64(Int(bitPattern: _result$))
326-
return _resultBits$.getJNIValue(in: environment!)
323+
let result$ = UnsafeMutablePointer<MyClass>.allocate(capacity: 1)
324+
result$.initialize(to: self$.pointee.copy())
325+
let resultBits$ = Int64(Int(bitPattern: result$))
326+
return resultBits$.getJNIValue(in: environment!)
327327
}
328328
""",
329329
]

Tests/JExtractSwiftTests/JNI/JNIStructTests.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -119,10 +119,10 @@ struct JNIStructTests {
119119
"""
120120
@_cdecl("Java_com_example_swift_MyStruct__00024init__JJ")
121121
func Java_com_example_swift_MyStruct__00024init__JJ(environment: UnsafeMutablePointer<JNIEnv?>!, thisClass: jclass, x: jlong, y: jlong) -> jlong {
122-
let _result$ = UnsafeMutablePointer<MyStruct>.allocate(capacity: 1)
123-
_result$.initialize(to: MyStruct.init(x: Int64(fromJNI: x, in: environment!), y: Int64(fromJNI: y, in: environment!)))
124-
let _resultBits$ = Int64(Int(bitPattern: _result$))
125-
return _resultBits$.getJNIValue(in: environment!)
122+
let result$ = UnsafeMutablePointer<MyStruct>.allocate(capacity: 1)
123+
result$.initialize(to: MyStruct.init(x: Int64(fromJNI: x, in: environment!), y: Int64(fromJNI: y, in: environment!)))
124+
let resultBits$ = Int64(Int(bitPattern: result$))
125+
return resultBits$.getJNIValue(in: environment!)
126126
}
127127
"""
128128
]

0 commit comments

Comments
 (0)