Skip to content

Commit 8f91364

Browse files
committed
fix tests after thunk body refactors
1 parent d5ab8d0 commit 8f91364

File tree

2 files changed

+35
-11
lines changed

2 files changed

+35
-11
lines changed

Sources/JExtractSwiftLib/JNI/JNISwift2JavaGenerator+JavaBindingsPrinting.swift

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -206,13 +206,15 @@ extension JNISwift2JavaGenerator {
206206
printDeclDocumentation(&printer, decl)
207207
printer.printBraceBlock("public \(renderFunctionSignature(decl))") { printer in
208208
var arguments = translatedDecl.translatedFunctionSignature.parameters.map(\.name)
209-
arguments.append("selfPointer")
209+
210+
let selfVarName = "self$"
211+
arguments.append(selfVarName)
210212

211213
let returnKeyword = translatedDecl.translatedFunctionSignature.resultType.isVoid ? "" : "return "
212214

213215
printer.print(
214216
"""
215-
long self$ = this.$memoryAddress();
217+
long \(selfVarName) = this.$memoryAddress();
216218
\(returnKeyword)\(translatedDecl.parentName).$\(translatedDecl.name)(\(arguments.joined(separator: ", ")));
217219
"""
218220
)
@@ -267,18 +269,17 @@ extension JNISwift2JavaGenerator {
267269
printer.printBraceBlock("protected Runnable \(funcName)()") { printer in
268270
printer.print(
269271
"""
272+
long self$ = this.$memoryAddress();
270273
if (CallTraces.TRACE_DOWNCALLS) {
271274
CallTraces.traceDowncall("\(type.swiftNominal.name).\(funcName)",
272275
"this", this,
273-
"this.$memoryAddress()", this.$memoryAddress());
276+
"self", self$);
274277
}
275-
long self$ = this.$memoryAddress();
276-
assert(self$ > 0);
277278
return new Runnable() {
278279
@Override
279280
public void run() {
280281
if (CallTraces.TRACE_DOWNCALLS) {
281-
CallTraces.traceDowncall("\(type.swiftNominal.name).$destroy(" + self$ + ")");
282+
CallTraces.traceDowncall("\(type.swiftNominal.name).$destroy", "self", self$);
282283
}
283284
\(type.swiftNominal.name).$destroy(self$);
284285
}

Tests/JExtractSwiftTests/JNI/JNIClassTests.swift

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -70,14 +70,21 @@ struct JNIClassTests {
7070
"""
7171
@Override
7272
protected Runnable $createDestroyFunction() {
73-
long $self = this.$memoryAddress();
73+
long self$ = this.$memoryAddress();
74+
if (CallTraces.TRACE_DOWNCALLS) {
75+
CallTraces.traceDowncall("MyClass.$createDestroyFunction",
76+
"this", this,
77+
"self", self$);
78+
}
7479
return new Runnable() {
7580
@Override
7681
public void run() {
77-
MyClass.$destroy($self);
82+
if (CallTraces.TRACE_DOWNCALLS) {
83+
CallTraces.traceDowncall("MyClass.$destroy", "self", self$);
84+
}
85+
MyClass.$destroy(self$);
7886
}
7987
};
80-
}
8188
"""
8289
])
8390
}
@@ -200,7 +207,15 @@ struct JNIClassTests {
200207
"""
201208
@_cdecl("Java_com_example_swift_MyClass__00024destroy__J")
202209
func Java_com_example_swift_MyClass__00024destroy__J(environment: UnsafeMutablePointer<JNIEnv?>!, thisClass: jclass, selfPointer: jlong) {
203-
let self$ = UnsafeMutablePointer<MyClass>(bitPattern: Int(Int64(fromJNI: selfPointer, in: environment!)))!
210+
guard let env$ = environment else {
211+
fatalError("Missing JNIEnv in downcall to \\(#function)")
212+
}
213+
assert(selfPointer != 0, "selfPointer memory address was null")
214+
let selfBits$ = Int(Int64(fromJNI: selfPointer, in: env$))
215+
assert(selfBits$ != 0, "$self memory address was null: selfPointer = \\(selfPointer)" )
216+
guard let self$ = UnsafeMutablePointer<MySwiftClass>(bitPattern: selfBits$) else {
217+
fatalError("Missing self pointer in call to \\(#function)!")
218+
}
204219
self$.deinitialize(count: 1)
205220
self$.deallocate()
206221
}
@@ -246,7 +261,15 @@ struct JNIClassTests {
246261
"""
247262
@_cdecl("Java_com_example_swift_MyClass__00024doSomething__JJ")
248263
func Java_com_example_swift_MyClass__00024doSomething__JJ(environment: UnsafeMutablePointer<JNIEnv?>!, thisClass: jclass, x: jlong, selfPointer: jlong) {
249-
let self$ = UnsafeMutablePointer<MyClass>(bitPattern: Int(Int64(fromJNI: selfPointer, in: environment!)))!
264+
guard let env$ = environment else {
265+
fatalError("Missing JNIEnv in downcall to \\(#function)")
266+
}
267+
assert(selfPointer != 0, "selfPointer memory address was null")
268+
let selfBits$ = Int(Int64(fromJNI: selfPointer, in: env$))
269+
assert(selfBits$ != 0, "$self memory address was null: selfPointer = \\(selfPointer)" )
270+
guard let self$ = UnsafeMutablePointer<MyClass>(bitPattern: selfBits$) else {
271+
fatalError("Missing self pointer in call to \\(#function)!")
272+
}
250273
self$.pointee.doSomething(x: Int64(fromJNI: x, in: environment!))
251274
}
252275
""",

0 commit comments

Comments
 (0)