Skip to content

Commit b5b5dec

Browse files
authored
Fix glue functions as public in JNI mode (#520)
* export glue functions in JNI mode * Fix test fixtures * export JNI_OnLoad
1 parent 1c73ff0 commit b5b5dec

17 files changed

+107
-107
lines changed

Sources/JExtractSwiftLib/JNI/JNISwift2JavaGenerator+SwiftThunkPrinting.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -725,7 +725,7 @@ extension JNISwift2JavaGenerator {
725725
printer.printBraceBlock(
726726
"""
727727
@_cdecl("\(cName)")
728-
func \(cName)(\(thunkParameters.joined(separator: ", ")))\(thunkReturnType)
728+
public func \(cName)(\(thunkParameters.joined(separator: ", ")))\(thunkReturnType)
729729
"""
730730
) { printer in
731731
body(&printer)

Sources/SwiftJava/JNI.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ package final class JNI {
5454
}
5555

5656
@_cdecl("JNI_OnLoad")
57-
func SwiftJava_JNI_OnLoad(javaVM: JavaVMPointer, reserved: UnsafeMutableRawPointer) -> jint {
57+
public func SwiftJava_JNI_OnLoad(javaVM: JavaVMPointer, reserved: UnsafeMutableRawPointer) -> jint {
5858
JNI.shared = JNI(fromVM: JavaVirtualMachine(adoptingJVM: javaVM))
5959
return JNI_VERSION_1_6
6060
}

Tests/JExtractSwiftTests/JNI/JNIArrayTest.swift

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ struct JNIArrayTest {
2626
detectChunkByInitialLines: 1,
2727
expectedChunks: [
2828
"""
29-
public static byte[] f(@Unsigned byte[] array) {
29+
public static byte[] f(@Unsigned byte[] array) {
3030
return SwiftModule.$f(Objects.requireNonNull(array, "array must not be null"));
3131
}
3232
""",
@@ -46,7 +46,7 @@ struct JNIArrayTest {
4646
expectedChunks: [
4747
"""
4848
@_cdecl("Java_com_example_swift_SwiftModule__00024f___3B")
49-
func Java_com_example_swift_SwiftModule__00024f___3B(environment: UnsafeMutablePointer<JNIEnv?>!, thisClass: jclass, array: jbyteArray?) -> jbyteArray? {
49+
public func Java_com_example_swift_SwiftModule__00024f___3B(environment: UnsafeMutablePointer<JNIEnv?>!, thisClass: jclass, array: jbyteArray?) -> jbyteArray? {
5050
return SwiftModule.f(array: [UInt8](fromJNI: array, in: environment)).getJNIValue(in: environment)
5151
}
5252
"""
@@ -62,7 +62,7 @@ struct JNIArrayTest {
6262
detectChunkByInitialLines: 1,
6363
expectedChunks: [
6464
"""
65-
public static byte[] f(@Unsigned byte[] array) {
65+
public static byte[] f(@Unsigned byte[] array) {
6666
return SwiftModule.$f(Objects.requireNonNull(array, "array must not be null"));
6767
}
6868
""",
@@ -82,7 +82,7 @@ struct JNIArrayTest {
8282
expectedChunks: [
8383
"""
8484
@_cdecl("Java_com_example_swift_SwiftModule__00024f___3B")
85-
func Java_com_example_swift_SwiftModule__00024f___3B(environment: UnsafeMutablePointer<JNIEnv?>!, thisClass: jclass, array: jbyteArray?) -> jbyteArray? {
85+
public func Java_com_example_swift_SwiftModule__00024f___3B(environment: UnsafeMutablePointer<JNIEnv?>!, thisClass: jclass, array: jbyteArray?) -> jbyteArray? {
8686
return SwiftModule.f(array: [UInt8](fromJNI: array, in: environment)).getJNIValue(in: environment)
8787
}
8888
"""
@@ -98,7 +98,7 @@ struct JNIArrayTest {
9898
detectChunkByInitialLines: 1,
9999
expectedChunks: [
100100
"""
101-
public static long[] f(long[] array) {
101+
public static long[] f(long[] array) {
102102
return SwiftModule.$f(Objects.requireNonNull(array, "array must not be null"));
103103
}
104104
""",
@@ -118,7 +118,7 @@ struct JNIArrayTest {
118118
expectedChunks: [
119119
"""
120120
@_cdecl("Java_com_example_swift_SwiftModule__00024f___3J")
121-
func Java_com_example_swift_SwiftModule__00024f___3J(environment: UnsafeMutablePointer<JNIEnv?>!, thisClass: jclass, array: jlongArray?) -> jlongArray? {
121+
public func Java_com_example_swift_SwiftModule__00024f___3J(environment: UnsafeMutablePointer<JNIEnv?>!, thisClass: jclass, array: jlongArray?) -> jlongArray? {
122122
return SwiftModule.f(array: [Int64](fromJNI: array, in: environment)).getJNIValue(in: environment)
123123
}
124124
"""
@@ -137,8 +137,8 @@ struct JNIArrayTest {
137137
detectChunkByInitialLines: 1,
138138
expectedChunks: [
139139
"""
140-
public static MySwiftClass[] f(MySwiftClass[] array, SwiftArena swiftArena$) {
141-
return Arrays.stream(SwiftModule.$f(Arrays.stream(Objects.requireNonNull(array, "array must not be null")).mapToLong(MySwiftClass::$memoryAddress).toArray())).mapToObj((pointer) -> {
140+
public static MySwiftClass[] f(MySwiftClass[] array, SwiftArena swiftArena$) {
141+
return Arrays.stream(SwiftModule.$f(Arrays.stream(Objects.requireNonNull(array, "array must not be null")).mapToLong(MySwiftClass::$memoryAddress).toArray())).mapToObj((pointer) -> {
142142
return MySwiftClass.wrapMemoryAddressUnsafe(pointer, swiftArena$);
143143
}
144144
).toArray(MySwiftClass[]::new);
@@ -163,7 +163,7 @@ struct JNIArrayTest {
163163
expectedChunks: [
164164
"""
165165
@_cdecl("Java_com_example_swift_SwiftModule__00024f___3J")
166-
func Java_com_example_swift_SwiftModule__00024f___3J(environment: UnsafeMutablePointer<JNIEnv?>!, thisClass: jclass, array: jlongArray?) -> jlongArray? {
166+
public func Java_com_example_swift_SwiftModule__00024f___3J(environment: UnsafeMutablePointer<JNIEnv?>!, thisClass: jclass, array: jlongArray?) -> jlongArray? {
167167
return SwiftModule.f(array: [Int64](fromJNI: array, in: environment).map( { (pointer$) in
168168
assert(pointer$ != 0, "pointer$ memory address was null")
169169
let pointer$Bits$ = Int(pointer$)

Tests/JExtractSwiftTests/JNI/JNIAsyncTests.swift

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ struct JNIAsyncTests {
5858
expectedChunks: [
5959
"""
6060
@_cdecl("Java_com_example_swift_SwiftModule__00024asyncVoid__Ljava_util_concurrent_CompletableFuture_2")
61-
func Java_com_example_swift_SwiftModule__00024asyncVoid__Ljava_util_concurrent_CompletableFuture_2(environment: UnsafeMutablePointer<JNIEnv?>!, thisClass: jclass, result_future: jobject?) {
61+
public func Java_com_example_swift_SwiftModule__00024asyncVoid__Ljava_util_concurrent_CompletableFuture_2(environment: UnsafeMutablePointer<JNIEnv?>!, thisClass: jclass, result_future: jobject?) {
6262
let globalFuture = environment.interface.NewGlobalRef(environment, result_future)
6363
var task: Task<Void, Never>? = nil
6464
#if swift(>=6.2)
@@ -132,7 +132,7 @@ struct JNIAsyncTests {
132132
expectedChunks: [
133133
"""
134134
@_cdecl("Java_com_example_swift_SwiftModule__00024async__Ljava_util_concurrent_CompletableFuture_2")
135-
func Java_com_example_swift_SwiftModule__00024async__Ljava_util_concurrent_CompletableFuture_2(environment: UnsafeMutablePointer<JNIEnv?>!, thisClass: jclass, result_future: jobject?) {
135+
public func Java_com_example_swift_SwiftModule__00024async__Ljava_util_concurrent_CompletableFuture_2(environment: UnsafeMutablePointer<JNIEnv?>!, thisClass: jclass, result_future: jobject?) {
136136
let globalFuture = environment.interface.NewGlobalRef(environment, result_future)
137137
var task: Task<Void, Never>? = nil
138138
#if swift(>=6.2)
@@ -220,7 +220,7 @@ struct JNIAsyncTests {
220220
expectedChunks: [
221221
"""
222222
@_cdecl("Java_com_example_swift_SwiftModule__00024async__JLjava_util_concurrent_CompletableFuture_2")
223-
func Java_com_example_swift_SwiftModule__00024async__JLjava_util_concurrent_CompletableFuture_2(environment: UnsafeMutablePointer<JNIEnv?>!, thisClass: jclass, i: jlong, result_future: jobject?) {
223+
public func Java_com_example_swift_SwiftModule__00024async__JLjava_util_concurrent_CompletableFuture_2(environment: UnsafeMutablePointer<JNIEnv?>!, thisClass: jclass, i: jlong, result_future: jobject?) {
224224
let globalFuture = environment.interface.NewGlobalRef(environment, result_future)
225225
var task: Task<Void, Never>? = nil
226226
#if swift(>=6.2)
@@ -251,7 +251,7 @@ struct JNIAsyncTests {
251251
_ = environment.interface.CallBooleanMethodA(environment, globalFuture, _JNIMethodIDCache.CompletableFuture.complete, [jvalue(l: boxedResult$)])
252252
}
253253
}
254-
return
254+
return
255255
}
256256
"""
257257
]
@@ -263,7 +263,7 @@ struct JNIAsyncTests {
263263
try assertOutput(
264264
input: """
265265
class MyClass { }
266-
266+
267267
public func async(c: MyClass) async -> MyClass
268268
""",
269269
.jni, .java,
@@ -297,15 +297,15 @@ struct JNIAsyncTests {
297297
try assertOutput(
298298
input: """
299299
class MyClass { }
300-
300+
301301
public func async(c: MyClass) async -> MyClass
302302
""",
303303
.jni, .swift,
304304
detectChunkByInitialLines: 1,
305305
expectedChunks: [
306306
"""
307307
@_cdecl("Java_com_example_swift_SwiftModule__00024async__JLjava_util_concurrent_CompletableFuture_2")
308-
func Java_com_example_swift_SwiftModule__00024async__JLjava_util_concurrent_CompletableFuture_2(environment: UnsafeMutablePointer<JNIEnv?>!, thisClass: jclass, c: jlong, result_future: jobject?) {
308+
public func Java_com_example_swift_SwiftModule__00024async__JLjava_util_concurrent_CompletableFuture_2(environment: UnsafeMutablePointer<JNIEnv?>!, thisClass: jclass, c: jlong, result_future: jobject?) {
309309
assert(c != 0, "c memory address was null")
310310
let cBits$ = Int(Int64(fromJNI: c, in: environment))
311311
let c$ = UnsafeMutablePointer<MyClass>(bitPattern: cBits$)
@@ -348,7 +348,7 @@ struct JNIAsyncTests {
348348
_ = environment.interface.CallBooleanMethodA(environment, globalFuture, _JNIMethodIDCache.CompletableFuture.complete, [jvalue(l: boxedResult$)])
349349
}
350350
}
351-
return
351+
return
352352
}
353353
"""
354354
]
@@ -392,7 +392,7 @@ struct JNIAsyncTests {
392392
expectedChunks: [
393393
"""
394394
@_cdecl("Java_com_example_swift_SwiftModule__00024async__Ljava_lang_String_2Ljava_util_concurrent_CompletableFuture_2")
395-
func Java_com_example_swift_SwiftModule__00024async__Ljava_lang_String_2Ljava_util_concurrent_CompletableFuture_2(environment: UnsafeMutablePointer<JNIEnv?>!, thisClass: jclass, s: jstring?, result_future: jobject?) {
395+
public func Java_com_example_swift_SwiftModule__00024async__Ljava_lang_String_2Ljava_util_concurrent_CompletableFuture_2(environment: UnsafeMutablePointer<JNIEnv?>!, thisClass: jclass, s: jstring?, result_future: jobject?) {
396396
let s = environment.interface.NewGlobalRef(environment, s)
397397
let globalFuture = environment.interface.NewGlobalRef(environment, result_future)
398398
...
@@ -418,7 +418,7 @@ struct JNIAsyncTests {
418418
try assertOutput(
419419
input: """
420420
class MyClass { }
421-
421+
422422
public func async(c: MyClass) async -> MyClass
423423
""",
424424
config: config,
@@ -450,7 +450,7 @@ struct JNIAsyncTests {
450450
try assertOutput(
451451
input: """
452452
class MyClass { }
453-
453+
454454
public func async(c: MyClass) async -> MyClass
455455
""",
456456
config: config,
@@ -459,15 +459,15 @@ struct JNIAsyncTests {
459459
expectedChunks: [
460460
"""
461461
@_cdecl("Java_com_example_swift_SwiftModule__00024async__JLorg_swift_swiftkit_core_SimpleCompletableFuture_2")
462-
func Java_com_example_swift_SwiftModule__00024async__JLorg_swift_swiftkit_core_SimpleCompletableFuture_2(environment: UnsafeMutablePointer<JNIEnv?>!, thisClass: jclass, c: jlong, result_future: jobject?) {
462+
public func Java_com_example_swift_SwiftModule__00024async__JLorg_swift_swiftkit_core_SimpleCompletableFuture_2(environment: UnsafeMutablePointer<JNIEnv?>!, thisClass: jclass, c: jlong, result_future: jobject?) {
463463
...
464464
var task: Task<Void, Never>? = nil
465465
...
466466
_ = environment.interface.CallBooleanMethodA(environment, globalFuture, _JNIMethodIDCache.SimpleCompletableFuture.complete, [jvalue(l: boxedResult$)])
467-
...
467+
...
468468
}
469469
"""
470470
]
471471
)
472472
}
473-
}
473+
}

Tests/JExtractSwiftTests/JNI/JNIClassTests.swift

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ struct JNIClassTests {
8383
private MyClass(long selfPointer, SwiftArena swiftArena) {
8484
SwiftObjects.requireNonZero(selfPointer, "selfPointer");
8585
this.selfPointer = selfPointer;
86-
86+
8787
// Only register once we have fully initialized the object since this will need the object pointer.
8888
swiftArena.register(this);
8989
}
@@ -163,7 +163,7 @@ struct JNIClassTests {
163163
expectedChunks: [
164164
"""
165165
@_cdecl("Java_com_example_swift_MyClass__00024method__")
166-
func Java_com_example_swift_MyClass__00024method__(environment: UnsafeMutablePointer<JNIEnv?>!, thisClass: jclass) {
166+
public func Java_com_example_swift_MyClass__00024method__(environment: UnsafeMutablePointer<JNIEnv?>!, thisClass: jclass) {
167167
MyClass.method()
168168
}
169169
"""
@@ -220,7 +220,7 @@ struct JNIClassTests {
220220
expectedChunks: [
221221
"""
222222
@_cdecl("Java_com_example_swift_MyClass__00024init__JJ")
223-
func Java_com_example_swift_MyClass__00024init__JJ(environment: UnsafeMutablePointer<JNIEnv?>!, thisClass: jclass, x: jlong, y: jlong) -> jlong {
223+
public func Java_com_example_swift_MyClass__00024init__JJ(environment: UnsafeMutablePointer<JNIEnv?>!, thisClass: jclass, x: jlong, y: jlong) -> jlong {
224224
let result$ = UnsafeMutablePointer<MyClass>.allocate(capacity: 1)
225225
result$.initialize(to: MyClass.init(x: Int64(fromJNI: x, in: environment), y: Int64(fromJNI: y, in: environment)))
226226
let resultBits$ = Int64(Int(bitPattern: result$))
@@ -229,7 +229,7 @@ struct JNIClassTests {
229229
""",
230230
"""
231231
@_cdecl("Java_com_example_swift_MyClass__00024init__")
232-
func Java_com_example_swift_MyClass__00024init__(environment: UnsafeMutablePointer<JNIEnv?>!, thisClass: jclass) -> jlong {
232+
public func Java_com_example_swift_MyClass__00024init__(environment: UnsafeMutablePointer<JNIEnv?>!, thisClass: jclass) -> jlong {
233233
let result$ = UnsafeMutablePointer<MyClass>.allocate(capacity: 1)
234234
result$.initialize(to: MyClass.init())
235235
let resultBits$ = Int64(Int(bitPattern: result$))
@@ -250,7 +250,7 @@ struct JNIClassTests {
250250
expectedChunks: [
251251
"""
252252
@_cdecl("Java_com_example_swift_MyClass__00024destroy__J")
253-
func Java_com_example_swift_MyClass__00024destroy__J(environment: UnsafeMutablePointer<JNIEnv?>!, thisClass: jclass, selfPointer: jlong) {
253+
public func Java_com_example_swift_MyClass__00024destroy__J(environment: UnsafeMutablePointer<JNIEnv?>!, thisClass: jclass, selfPointer: jlong) {
254254
guard let env$ = environment else {
255255
fatalError("Missing JNIEnv in downcall to \\(#function)")
256256
}
@@ -302,7 +302,7 @@ struct JNIClassTests {
302302
expectedChunks: [
303303
"""
304304
@_cdecl("Java_com_example_swift_MyClass__00024doSomething__JJ")
305-
func Java_com_example_swift_MyClass__00024doSomething__JJ(environment: UnsafeMutablePointer<JNIEnv?>!, thisClass: jclass, x: jlong, self: jlong) {
305+
public func Java_com_example_swift_MyClass__00024doSomething__JJ(environment: UnsafeMutablePointer<JNIEnv?>!, thisClass: jclass, x: jlong, self: jlong) {
306306
assert(self != 0, "self memory address was null")
307307
let selfBits$ = Int(Int64(fromJNI: self, in: environment))
308308
let self$ = UnsafeMutablePointer<MyClass>(bitPattern: selfBits$)
@@ -351,7 +351,7 @@ struct JNIClassTests {
351351
expectedChunks: [
352352
"""
353353
@_cdecl("Java_com_example_swift_MyClass__00024copy__J")
354-
func Java_com_example_swift_MyClass__00024copy__J(environment: UnsafeMutablePointer<JNIEnv?>!, thisClass: jclass, self: jlong) -> jlong {
354+
public func Java_com_example_swift_MyClass__00024copy__J(environment: UnsafeMutablePointer<JNIEnv?>!, thisClass: jclass, self: jlong) -> jlong {
355355
assert(self != 0, "self memory address was null")
356356
let selfBits$ = Int(Int64(fromJNI: self, in: environment))
357357
let self$ = UnsafeMutablePointer<MyClass>(bitPattern: selfBits$)
@@ -383,7 +383,7 @@ struct JNIClassTests {
383383
* }
384384
*/
385385
public boolean isEqual(MyClass other) {
386-
return MyClass.$isEqual(other.$memoryAddress(), this.$memoryAddress());
386+
return MyClass.$isEqual(other.$memoryAddress(), this.$memoryAddress());
387387
}
388388
""",
389389
"""
@@ -403,10 +403,10 @@ struct JNIClassTests {
403403
expectedChunks: [
404404
"""
405405
@_cdecl("Java_com_example_swift_MyClass__00024isEqual__JJ")
406-
func Java_com_example_swift_MyClass__00024isEqual__JJ(environment: UnsafeMutablePointer<JNIEnv?>!, thisClass: jclass, other: jlong, self: jlong) -> jboolean {
406+
public func Java_com_example_swift_MyClass__00024isEqual__JJ(environment: UnsafeMutablePointer<JNIEnv?>!, thisClass: jclass, other: jlong, self: jlong) -> jboolean {
407407
assert(other != 0, "other memory address was null")
408408
let otherBits$ = Int(Int64(fromJNI: other, in: environment))
409-
let other$ = UnsafeMutablePointer<MyClass>(bitPattern: otherBits$)
409+
let other$ = UnsafeMutablePointer<MyClass>(bitPattern: otherBits$)
410410
guard let other$ else {
411411
fatalError("other memory address was null in call to \\(#function)!")
412412
}

Tests/JExtractSwiftTests/JNI/JNIClosureTests.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,8 @@ struct JNIClosureTests {
6161
expectedChunks: [
6262
"""
6363
@_cdecl("Java_com_example_swift_SwiftModule__00024emptyClosure__Lcom_example_swift_SwiftModule_00024emptyClosure_00024closure_2")
64-
func Java_com_example_swift_SwiftModule__00024emptyClosure__Lcom_example_swift_SwiftModule_00024emptyClosure_00024closure_2(environment: UnsafeMutablePointer<JNIEnv?>!, thisClass: jclass, closure: jobject?) {
65-
SwiftModule.emptyClosure(closure: {
64+
public func Java_com_example_swift_SwiftModule__00024emptyClosure__Lcom_example_swift_SwiftModule_00024emptyClosure_00024closure_2(environment: UnsafeMutablePointer<JNIEnv?>!, thisClass: jclass, closure: jobject?) {
65+
SwiftModule.emptyClosure(closure: {
6666
let class$ = environment.interface.GetObjectClass(environment, closure)
6767
let methodID$ = environment.interface.GetMethodID(environment, class$, "apply", "()V")!
6868
let arguments$: [jvalue] = []
@@ -113,7 +113,7 @@ struct JNIClosureTests {
113113
expectedChunks: [
114114
"""
115115
@_cdecl("Java_com_example_swift_SwiftModule__00024closureWithArgumentsAndReturn__Lcom_example_swift_SwiftModule_00024closureWithArgumentsAndReturn_00024closure_2")
116-
func Java_com_example_swift_SwiftModule__00024closureWithArgumentsAndReturn__Lcom_example_swift_SwiftModule_00024closureWithArgumentsAndReturn_00024closure_2(environment: UnsafeMutablePointer<JNIEnv?>!, thisClass: jclass, closure: jobject?) {
116+
public func Java_com_example_swift_SwiftModule__00024closureWithArgumentsAndReturn__Lcom_example_swift_SwiftModule_00024closureWithArgumentsAndReturn_00024closure_2(environment: UnsafeMutablePointer<JNIEnv?>!, thisClass: jclass, closure: jobject?) {
117117
SwiftModule.closureWithArgumentsAndReturn(closure: { _0, _1 in
118118
let class$ = environment.interface.GetObjectClass(environment, closure)
119119
let methodID$ = environment.interface.GetMethodID(environment, class$, "apply", "(JZ)J")!

0 commit comments

Comments
 (0)