Skip to content

Commit 5fe1107

Browse files
committed
fix tests
1 parent 5d907fa commit 5fe1107

File tree

3 files changed

+58
-21
lines changed

3 files changed

+58
-21
lines changed

Samples/JExtractJNISampleApp/src/test/java/com/example/swift/MySwiftClassTest.java

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,24 +14,25 @@
1414

1515
package com.example.swift;
1616

17-
import com.example.swift.MySwiftLibrary;
18-
import org.junit.jupiter.api.BeforeAll;
19-
import org.junit.jupiter.api.Disabled;
2017
import org.junit.jupiter.api.Test;
18+
import org.swift.swiftkit.core.ConfinedSwiftMemorySession;
2119

2220
import static org.junit.jupiter.api.Assertions.*;
2321

2422
public class MySwiftClassTest {
2523
@Test
2624
void init_noParameters() {
27-
MySwiftClass c = MySwiftClass.init();
28-
assertNotNull(c);
25+
try (var arena = new ConfinedSwiftMemorySession(Thread.currentThread())) {
26+
MySwiftClass c = MySwiftClass.init(arena);
27+
assertNotNull(c);
28+
}
2929
}
3030

3131
@Test
3232
void init_withParameters() {
33-
MySwiftClass c = MySwiftClass.init(1337, 42);
34-
assertNotNull(c);
33+
try (var arena = new ConfinedSwiftMemorySession(Thread.currentThread())) {
34+
MySwiftClass c = MySwiftClass.init(1337, 42, arena);
35+
assertNotNull(c);
36+
}
3537
}
36-
3738
}

Sources/JExtractSwiftLib/JNI/JNISwift2JavaGenerator+SwiftThunkPrinting.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -213,10 +213,10 @@ extension JNISwift2JavaGenerator {
213213
let cName =
214214
"Java_"
215215
+ self.javaPackage.replacingOccurrences(of: ".", with: "_")
216-
+ "_\(parentName.nativeJNIEscaped)_"
217-
+ javaMethodName.nativeJNIEscaped
216+
+ "_\(parentName.escapedJNIIdentifier)_"
217+
+ javaMethodName.escapedJNIIdentifier
218218
+ "__"
219-
+ jniSignature.nativeJNIEscaped
219+
+ jniSignature.escapedJNIIdentifier
220220

221221
let translatedParameters = parameters.map {
222222
"\($0.name): \($0.type.jniTypeName)"
@@ -294,7 +294,7 @@ extension JNISwift2JavaGenerator {
294294

295295
extension String {
296296
/// Returns a version of the string correctly escaped for a JNI
297-
var nativeJNIEscaped: String {
297+
var escapedJNIIdentifier: String {
298298
self.map {
299299
if $0 == "_" {
300300
return "_1"

Tests/JExtractSwiftTests/JNI/JNIClassTests.swift

Lines changed: 45 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,11 @@ struct JNIClassTests {
4444
// Swift module: SwiftModule
4545
4646
package com.example.swift;
47+
48+
import org.swift.swiftkit.core.*;
49+
import org.swift.swiftkit.core.util.*;
4750
48-
public final class MyClass {
51+
public final class MyClass extends JNISwiftInstance {
4952
static final String LIB_NAME = "SwiftModule";
5053
5154
@SuppressWarnings("unused")
@@ -55,12 +58,25 @@ struct JNIClassTests {
5558
return true;
5659
}
5760
58-
private long selfPointer;
59-
60-
private MyClass(long selfPointer) {
61-
this.selfPointer = selfPointer;
61+
public MyClass(long selfPointer, SwiftArena swiftArena) {
62+
super(selfPointer, swiftArena);
6263
}
6364
""",
65+
"""
66+
private static native void $destroy(long selfPointer);
67+
""",
68+
"""
69+
@Override
70+
protected Runnable $createDestroyFunction() {
71+
long $selfPointer = this.pointer();
72+
return new Runnable() {
73+
@Override
74+
public void run() {
75+
MyClass.$destroy($selfPointer);
76+
}
77+
};
78+
}
79+
"""
6480
])
6581
}
6682

@@ -116,9 +132,9 @@ struct JNIClassTests {
116132
* public init(x: Int64, y: Int64)
117133
* }
118134
*/
119-
public static MyClass init(long x, long y) {
135+
public static MyClass init(long x, long y, SwiftArena swiftArena$) {
120136
long selfPointer = MyClass.allocatingInit(x, y);
121-
return new MyClass(selfPointer);
137+
return new MyClass(selfPointer, swiftArena$);
122138
}
123139
""",
124140
"""
@@ -128,9 +144,9 @@ struct JNIClassTests {
128144
* public init()
129145
* }
130146
*/
131-
public static MyClass init() {
147+
public static MyClass init(SwiftArena swiftArena$) {
132148
long selfPointer = MyClass.allocatingInit();
133-
return new MyClass(selfPointer);
149+
return new MyClass(selfPointer, swiftArena$);
134150
}
135151
""",
136152
"""
@@ -170,4 +186,24 @@ struct JNIClassTests {
170186
]
171187
)
172188
}
189+
190+
@Test
191+
func destroyFunction_swiftThunks() throws {
192+
try assertOutput(
193+
input: source,
194+
.jni,
195+
.swift,
196+
detectChunkByInitialLines: 1,
197+
expectedChunks: [
198+
"""
199+
@_cdecl("Java_com_example_swift_MyClass__00024destroy__J")
200+
func Java_com_example_swift_MyClass__00024destroy__J(environment: UnsafeMutablePointer<JNIEnv?>!, thisClass: jclass, selfPointer: jlong) {
201+
let pointer = UnsafeMutablePointer<MyClass>(bitPattern: Int(Int64(fromJNI: selfPointer, in: environment!)))!
202+
pointer.deinitialize(count: 1)
203+
pointer.deallocate()
204+
}
205+
"""
206+
]
207+
)
208+
}
173209
}

0 commit comments

Comments
 (0)