Skip to content

Commit 42cbfd5

Browse files
committed
fix swift tests
1 parent a3aadd0 commit 42cbfd5

File tree

4 files changed

+37
-27
lines changed

4 files changed

+37
-27
lines changed

Sources/JExtractSwiftLib/JNI/JNISwift2JavaGenerator+JavaTranslation.swift

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,17 +22,20 @@ extension JNISwift2JavaGenerator {
2222
return cached
2323
}
2424

25-
let translation = JavaTranslation()
25+
let translation = JavaTranslation(swiftModuleName: self.swiftModuleName)
2626
let translated = translation.translate(decl)
2727

2828
translatedDecls[decl] = translated
2929
return translated
3030
}
3131

3232
struct JavaTranslation {
33+
let swiftModuleName: String
34+
3335
func translate(_ decl: ImportedFunc) -> TranslatedFunctionDecl {
3436
let translatedFunctionSignature = translate(functionSignature: decl.functionSignature)
35-
let parentName = decl.parentType?.asNominalType?.nominalTypeDecl.qualifiedName
37+
// Types with no parent will be outputted inside a "module" class.
38+
let parentName = decl.parentType?.asNominalType?.nominalTypeDecl.qualifiedName ?? swiftModuleName
3639

3740
return TranslatedFunctionDecl(
3841
name: decl.name,
@@ -106,8 +109,8 @@ extension JNISwift2JavaGenerator {
106109
/// Java function name
107110
let name: String
108111

109-
/// The name of the parent scope this function is declared in (or nil if global)
110-
let parentName: String?
112+
/// The name of the Java parent scope this function is declared in
113+
let parentName: String
111114

112115
/// Function signature
113116
let translatedFunctionSignature: TranslatedFunctionSignature

Sources/JExtractSwiftLib/JNI/JNISwift2JavaGenerator+SwiftThunkPrinting.swift

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -100,8 +100,7 @@ extension JNISwift2JavaGenerator {
100100

101101
private func printInitializerThunk(_ printer: inout CodePrinter, _ decl: ImportedFunc) {
102102
let translatedDecl = translatedDecl(for: decl)
103-
// Initializers must have a parent
104-
let typeName = translatedDecl.parentName!
103+
let typeName = translatedDecl.parentName
105104

106105
printCDecl(
107106
&printer,
@@ -185,7 +184,7 @@ extension JNISwift2JavaGenerator {
185184
javaMethodName: translatedDecl.name,
186185
parentName: parentName,
187186
parameters: translatedDecl.translatedFunctionSignature.parameters,
188-
isStatic: decl.isStatic || decl.isInitializer,
187+
isStatic: decl.isStatic || decl.isInitializer || !decl.hasParent,
189188
resultType: translatedDecl.translatedFunctionSignature.resultType,
190189
body
191190
)
@@ -194,13 +193,12 @@ extension JNISwift2JavaGenerator {
194193
private func printCDecl(
195194
_ printer: inout CodePrinter,
196195
javaMethodName: String,
197-
parentName: String?,
196+
parentName: String,
198197
parameters: [JavaParameter],
199198
isStatic: Bool,
200199
resultType: JavaType,
201200
_ body: (inout CodePrinter) -> Void
202201
) {
203-
let parentName = parentName ?? swiftModuleName
204202
var jniSignature = parameters.reduce(into: "") { signature, parameter in
205203
signature += parameter.type.jniTypeSignature
206204
}

Tests/JExtractSwiftTests/JNI/JNIClassTests.swift

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,16 @@ struct JNIClassTests {
4545
4646
package com.example.swift;
4747
48-
public final class MyClass {
48+
public final class MyClass {
49+
static final String LIB_NAME = "SwiftModule";
50+
51+
@SuppressWarnings("unused")
52+
private static final boolean INITIALIZED_LIBS = initializeLibs();
53+
static boolean initializeLibs() {
54+
System.loadLibrary(LIB_NAME);
55+
return true;
56+
}
57+
4958
private long selfPointer;
5059
5160
private MyClass(long selfPointer) {
@@ -84,8 +93,8 @@ struct JNIClassTests {
8493
detectChunkByInitialLines: 1,
8594
expectedChunks: [
8695
"""
87-
@_cdecl("Java_com_example_swift_MyClass_method")
88-
func swiftjava_SwiftModule_MyClass_method(environment: UnsafeMutablePointer<JNIEnv?>!, thisClass: jclass) {
96+
@_cdecl("Java_com_example_swift_MyClass_method__")
97+
func Java_com_example_swift_MyClass_method__(environment: UnsafeMutablePointer<JNIEnv?>!, thisClass: jclass) {
8998
MyClass.method()
9099
}
91100
"""

Tests/JExtractSwiftTests/JNI/JNIModuleTests.swift

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ import Testing
1919
struct JNIModuleTests {
2020
let globalMethodWithPrimitives = """
2121
public func helloWorld()
22-
public func takeIntegers(i1: Int8, i2: Int16, i3: Int32, i4: Int) -> UInt16
22+
public func takeIntegers(i1: Int8, i2: Int16, i3: Int32, i4: Int64) -> UInt16
2323
public func otherPrimitives(b: Bool, f: Float, d: Double)
2424
"""
2525

@@ -73,7 +73,7 @@ struct JNIModuleTests {
7373
/**
7474
* Downcall to Swift:
7575
* {@snippet lang=swift :
76-
* public func takeIntegers(i1: Int8, i2: Int16, i3: Int32, i4: Int) -> UInt16
76+
* public func takeIntegers(i1: Int8, i2: Int16, i3: Int32, i4: Int64) -> UInt16
7777
* }
7878
*/
7979
public static native char takeIntegers(byte i1, short i2, int i3, long i4);
@@ -100,21 +100,21 @@ struct JNIModuleTests {
100100
detectChunkByInitialLines: 1,
101101
expectedChunks: [
102102
"""
103-
@_cdecl("Java_com_example_swift_SwiftModule_helloWorld")
104-
func swiftjava_SwiftModule_helloWorld(environment: UnsafeMutablePointer<JNIEnv?>!, thisClass: jclass) {
103+
@_cdecl("Java_com_example_swift_SwiftModule_helloWorld__")
104+
func Java_com_example_swift_SwiftModule_helloWorld__(environment: UnsafeMutablePointer<JNIEnv?>!, thisClass: jclass) {
105105
SwiftModule.helloWorld()
106106
}
107107
""",
108108
"""
109-
@_cdecl("Java_com_example_swift_SwiftModule_takeIntegers")
110-
func swiftjava_SwiftModule_takeIntegers_i1_i2_i3_i4(environment: UnsafeMutablePointer<JNIEnv?>!, thisClass: jclass, i1: jbyte, i2: jshort, i3: jint, i4: jlong) -> jchar {
111-
let result = SwiftModule.takeIntegers(i1: Int8(fromJNI: i1, in: environment!), i2: Int16(fromJNI: i2, in: environment!), i3: Int32(fromJNI: i3, in: environment!), i4: Int(fromJNI: i4, in: environment!))
109+
@_cdecl("Java_com_example_swift_SwiftModule_takeIntegers__BSIJ")
110+
func Java_com_example_swift_SwiftModule_takeIntegers__BSIJ(environment: UnsafeMutablePointer<JNIEnv?>!, thisClass: jclass, i1: jbyte, i2: jshort, i3: jint, i4: jlong) -> jchar {
111+
let result = SwiftModule.takeIntegers(i1: Int8(fromJNI: i1, in: environment!), i2: Int16(fromJNI: i2, in: environment!), i3: Int32(fromJNI: i3, in: environment!), i4: Int64(fromJNI: i4, in: environment!))
112112
return result.getJNIValue(in: environment)
113113
}
114114
""",
115115
"""
116-
@_cdecl("Java_com_example_swift_SwiftModule_otherPrimitives")
117-
func swiftjava_SwiftModule_otherPrimitives_b_f_d(environment: UnsafeMutablePointer<JNIEnv?>!, thisClass: jclass, b: jboolean, f: jfloat, d: jdouble) {
116+
@_cdecl("Java_com_example_swift_SwiftModule_otherPrimitives__ZFD")
117+
func Java_com_example_swift_SwiftModule_otherPrimitives__ZFD(environment: UnsafeMutablePointer<JNIEnv?>!, thisClass: jclass, b: jboolean, f: jfloat, d: jdouble) {
118118
SwiftModule.otherPrimitives(b: Bool(fromJNI: b, in: environment!), f: Float(fromJNI: f, in: environment!), d: Double(fromJNI: d, in: environment!))
119119
}
120120
"""
@@ -151,8 +151,8 @@ struct JNIModuleTests {
151151
detectChunkByInitialLines: 1,
152152
expectedChunks: [
153153
"""
154-
@_cdecl("Java_com_example_swift_SwiftModule_copy")
155-
func swiftjava_SwiftModule_copy__(environment: UnsafeMutablePointer<JNIEnv?>!, thisClass: jclass, string: jstring?) -> jstring? {
154+
@_cdecl("Java_com_example_swift_SwiftModule_copy__Ljava_lang_String_2")
155+
func Java_com_example_swift_SwiftModule_copy__Ljava_lang_String_2(environment: UnsafeMutablePointer<JNIEnv?>!, thisClass: jclass, string: jstring?) -> jstring? {
156156
let result = SwiftModule.copy(String(fromJNI: string, in: environment!))
157157
return result.getJNIValue(in: environment)
158158
}
@@ -199,8 +199,8 @@ struct JNIModuleTests {
199199
detectChunkByInitialLines: 1,
200200
expectedChunks: [
201201
"""
202-
@_cdecl("Java_com_example_swift_SwiftModule_methodA")
203-
func swiftjava_SwiftModule_methodA(environment: UnsafeMutablePointer<JNIEnv?>!, thisClass: jclass) {
202+
@_cdecl("Java_com_example_swift_SwiftModule_methodA__")
203+
func Java_com_example_swift_SwiftModule_methodA__(environment: UnsafeMutablePointer<JNIEnv?>!, thisClass: jclass) {
204204
do {
205205
try SwiftModule.methodA()
206206
} catch {
@@ -209,8 +209,8 @@ struct JNIModuleTests {
209209
}
210210
""",
211211
"""
212-
@_cdecl("Java_com_example_swift_SwiftModule_methodB")
213-
func swiftjava_SwiftModule_methodB(environment: UnsafeMutablePointer<JNIEnv?>!, thisClass: jclass) -> jlong {
212+
@_cdecl("Java_com_example_swift_SwiftModule_methodB__")
213+
func Java_com_example_swift_SwiftModule_methodB__(environment: UnsafeMutablePointer<JNIEnv?>!, thisClass: jclass) -> jlong {
214214
do {
215215
let result = try SwiftModule.methodB()
216216
return result.getJNIValue(in: environment)

0 commit comments

Comments
 (0)