Skip to content

Commit dd895a1

Browse files
committed
fix tets
1 parent 5c577af commit dd895a1

File tree

9 files changed

+309
-25
lines changed

9 files changed

+309
-25
lines changed

Sources/JExtractSwiftLib/ImportedDecls.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,8 @@ package class ImportedNominalType: ImportedDecl {
3838

3939
init(swiftNominal: SwiftNominalTypeDeclaration, lookupContext: SwiftTypeLookupContext) throws {
4040
self.swiftNominal = swiftNominal
41-
self.inheritedTypes = try swiftNominal.inheritanceTypes?.compactMap {
42-
try SwiftType($0.type, lookupContext: lookupContext)
41+
self.inheritedTypes = swiftNominal.inheritanceTypes?.compactMap {
42+
try? SwiftType($0.type, lookupContext: lookupContext)
4343
} ?? []
4444
}
4545

Sources/JExtractSwiftLib/JNI/JNISwift2JavaGenerator+JavaBindingsPrinting.swift

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -256,7 +256,10 @@ extension JNISwift2JavaGenerator {
256256
printer.print("@ThreadSafe // Sendable")
257257
}
258258
var implements = ["JNISwiftInstance"]
259-
implements += decl.inheritedTypes.compactMap(\.asNominalTypeDeclaration).filter { $0.kind == .protocol }.map(\.name)
259+
implements += decl.inheritedTypes
260+
.compactMap(\.asNominalTypeDeclaration)
261+
.filter { $0.kind == .protocol }
262+
.map(\.name)
260263
let implementsClause = implements.joined(separator: ", ")
261264
printer.printBraceBlock("public final class \(decl.swiftNominal.name) implements \(implementsClause)") { printer in
262265
body(&printer)
@@ -428,11 +431,11 @@ extension JNISwift2JavaGenerator {
428431
var parameters = translatedDecl.translatedFunctionSignature.parameters.map { $0.parameter.renderParameter() }
429432
let throwsClause = translatedDecl.isThrowing ? " throws Exception" : ""
430433

431-
let generics = translatedDecl.translatedFunctionSignature.parameters.reduce(into: [String: [JavaType]]()) { generics, parameter in
434+
let generics = translatedDecl.translatedFunctionSignature.parameters.reduce(into: [(String, [JavaType])]()) { generics, parameter in
432435
guard case .generic(let name, let extends) = parameter.parameter.type else {
433436
return
434437
}
435-
generics[name] = extends
438+
generics.append((name, extends))
436439
}
437440
.map { "\($0) extends \($1.compactMap(\.className).joined(separator: " & "))" }
438441
.joined(separator: ", ")

Tests/JExtractSwiftTests/JNI/JNIClassTests.swift

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -55,9 +55,12 @@ struct JNIClassTests {
5555
5656
import org.swift.swiftkit.core.*;
5757
import org.swift.swiftkit.core.util.*;
58+
import java.util.*;
59+
import java.util.concurrent.atomic.AtomicBoolean;
60+
import org.swift.swiftkit.core.annotations.*;
5861
""",
5962
"""
60-
public final class MyClass extends JNISwiftInstance {
63+
public final class MyClass implements JNISwiftInstance {
6164
static final String LIB_NAME = "SwiftModule";
6265
6366
@SuppressWarnings("unused")
@@ -68,9 +71,21 @@ struct JNIClassTests {
6871
}
6972
""",
7073
"""
71-
private MyClass(long selfPointer, SwiftArena swiftArena) {
72-
super(selfPointer, swiftArena);
73-
}
74+
private final long selfPointer;
75+
""",
76+
"""
77+
public long $memoryAddress() {
78+
return this.selfPointer;
79+
}
80+
""",
81+
"""
82+
private MyClass(long selfPointer, SwiftArena swiftArena) {
83+
SwiftObjects.requireNonZero(selfPointer, "selfPointer");
84+
this.selfPointer = selfPointer;
85+
86+
// Only register once we have fully initialized the object since this will need the object pointer.
87+
swiftArena.register(this);
88+
}
7489
""",
7590
"""
7691
public static MyClass wrapMemoryAddressUnsafe(long selfPointer, SwiftArena swiftArena) {
@@ -92,7 +107,7 @@ struct JNIClassTests {
92107
expectedChunks: [
93108
"""
94109
@Override
95-
protected Runnable $createDestroyFunction() {
110+
public Runnable $createDestroyFunction() {
96111
long self$ = this.$memoryAddress();
97112
if (CallTraces.TRACE_DOWNCALLS) {
98113
CallTraces.traceDowncall("MyClass.$createDestroyFunction",

Tests/JExtractSwiftTests/JNI/JNIEnumTests.swift

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,12 @@ struct JNIEnumTests {
3939
4040
import org.swift.swiftkit.core.*;
4141
import org.swift.swiftkit.core.util.*;
42+
import java.util.*;
43+
import java.util.concurrent.atomic.AtomicBoolean;
44+
import org.swift.swiftkit.core.annotations.*;
4245
""",
4346
"""
44-
public final class MyEnum extends JNISwiftInstance {
47+
public final class MyEnum implements JNISwiftInstance {
4548
static final String LIB_NAME = "SwiftModule";
4649
4750
@SuppressWarnings("unused")
@@ -53,7 +56,19 @@ struct JNIEnumTests {
5356
""",
5457
"""
5558
private MyEnum(long selfPointer, SwiftArena swiftArena) {
56-
super(selfPointer, swiftArena);
59+
SwiftObjects.requireNonZero(selfPointer, "selfPointer");
60+
this.selfPointer = selfPointer;
61+
62+
// Only register once we have fully initialized the object since this will need the object pointer.
63+
swiftArena.register(this);
64+
}
65+
""",
66+
"""
67+
private final long selfPointer;
68+
""",
69+
"""
70+
public long $memoryAddress() {
71+
return this.selfPointer;
5772
}
5873
""",
5974
"""
@@ -66,7 +81,7 @@ struct JNIEnumTests {
6681
""",
6782
"""
6883
@Override
69-
protected Runnable $createDestroyFunction() {
84+
public Runnable $createDestroyFunction() {
7085
long self$ = this.$memoryAddress();
7186
if (CallTraces.TRACE_DOWNCALLS) {
7287
CallTraces.traceDowncall("MyEnum.$createDestroyFunction",

Tests/JExtractSwiftTests/JNI/JNIModuleTests.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ struct JNIModuleTests {
4646
import org.swift.swiftkit.core.*;
4747
import org.swift.swiftkit.core.util.*;
4848
import java.util.*;
49+
import java.util.concurrent.atomic.AtomicBoolean;
4950
import org.swift.swiftkit.core.annotations.*;
5051
5152
public final class SwiftModule {

Tests/JExtractSwiftTests/JNI/JNIOptionalTests.swift

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -127,12 +127,12 @@ struct JNIOptionalTests {
127127
result$ = innerResult$.getJNIValue(in: environment!)
128128
var flag$ = Int8(1)
129129
environment.interface.SetByteArrayRegion(environment, result_discriminator$, 0, 1, &flag$)
130-
} // render(_:_:) @ JExtractSwiftLib/JNISwift2JavaGenerator+NativeTranslation.swift:649
130+
}
131131
else {
132132
result$ = String.jniPlaceholderValue
133133
var flag$ = Int8(0)
134134
environment.interface.SetByteArrayRegion(environment, result_discriminator$, 0, 1, &flag$)
135-
} // render(_:_:) @ JExtractSwiftLib/JNISwift2JavaGenerator+NativeTranslation.swift:659
135+
}
136136
return result$
137137
}
138138
"""
@@ -190,12 +190,12 @@ struct JNIOptionalTests {
190190
result$ = _resultBits$.getJNIValue(in: environment!)
191191
var flag$ = Int8(1)
192192
environment.interface.SetByteArrayRegion(environment, result_discriminator$, 0, 1, &flag$)
193-
} // render(_:_:) @ JExtractSwiftLib/JNISwift2JavaGenerator+NativeTranslation.swift:649
193+
}
194194
else {
195195
result$ = 0
196196
var flag$ = Int8(0)
197197
environment.interface.SetByteArrayRegion(environment, result_discriminator$, 0, 1, &flag$)
198-
} // render(_:_:) @ JExtractSwiftLib/JNISwift2JavaGenerator+NativeTranslation.swift:659
198+
}
199199
return result$
200200
}
201201
"""
@@ -243,7 +243,7 @@ struct JNIOptionalTests {
243243
func Java_com_example_swift_SwiftModule__00024optionalJavaKitClass__Ljava_lang_Long_2(environment: UnsafeMutablePointer<JNIEnv?>!, thisClass: jclass, arg: jobject?) {
244244
SwiftModule.optionalJavaKitClass(arg.map {
245245
return JavaLong(javaThis: $0, environment: environment!)
246-
} // render(_:_:) @ JExtractSwiftLib/JNISwift2JavaGenerator+NativeTranslation.swift:691
246+
}
247247
)
248248
}
249249
"""

0 commit comments

Comments
 (0)