Skip to content

Commit 5984bb2

Browse files
committed
test fixups
1 parent 3f29222 commit 5984bb2

File tree

17 files changed

+113
-70
lines changed

17 files changed

+113
-70
lines changed

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,8 @@ void test_MySwiftClass_voidMethod() {
4545
MySwiftClass o = new MySwiftClass(12, 42);
4646
o.voidMethod();
4747
} catch (Throwable throwable) {
48-
checkPaths(throwable);
48+
throw throwable;
49+
// checkPaths(throwable);
4950
}
5051
}
5152

Samples/SwiftKitSampleApp/Sources/MySwiftLibrary/MySwiftStruct.swift

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,12 @@
1414

1515
public struct MySwiftStruct {
1616

17-
public var number: Int
17+
private var cap: Int
18+
private var len: Int
1819

19-
public init(number: Int) {
20-
self.number = number
20+
public init(cap: Int, len: Int) {
21+
self.cap = cap
22+
self.len = len
2123
}
2224

2325
public func voidMethod() {
@@ -38,6 +40,20 @@ public struct MySwiftStruct {
3840
return 12
3941
}
4042

43+
public func getCapacity() -> Int {
44+
self.cap
45+
}
46+
47+
public func getLength() -> Int {
48+
self.len
49+
}
50+
51+
public mutating func increaseCap(by value: Int) -> Int {
52+
precondition(value > 0)
53+
self.cap += value
54+
return self.cap
55+
}
56+
4157
public func makeRandomIntMethod() -> Int {
4258
return Int.random(in: 1..<256)
4359
}

Samples/SwiftKitSampleApp/src/main/java/com/example/swift/HelloJava2Swift.java

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -24,15 +24,13 @@
2424
import org.swift.swiftkit.SwiftKit;
2525
import org.swift.swiftkit.SwiftValueWitnessTable;
2626

27-
import java.util.Arrays;
28-
2927
public class HelloJava2Swift {
3028

3129
public static void main(String[] args) {
3230
boolean traceDowncalls = Boolean.getBoolean("jextract.trace.downcalls");
3331
System.out.println("Property: jextract.trace.downcalls = " + traceDowncalls);
3432

35-
System.out.print("Property: java.library.path = " +SwiftKit.getJavaLibraryPath());
33+
System.out.print("Property: java.library.path = " + SwiftKit.getJavaLibraryPath());
3634

3735
examples();
3836
}
@@ -44,23 +42,23 @@ static void examples() {
4442

4543
// Example of using an arena; MyClass.deinit is run at end of scope
4644
try (var arena = SwiftArena.ofConfined()) {
47-
MySwiftClass obj = new MySwiftClass(arena, 2222, 7777);
48-
49-
// just checking retains/releases work
50-
SwiftKit.retain(obj.$memorySegment());
51-
SwiftKit.release(obj.$memorySegment());
45+
MySwiftClass obj = new MySwiftClass(arena, 2222, 7777);
5246

53-
obj.voidMethod();
54-
obj.takeIntMethod(42);
47+
// just checking retains/releases work
48+
SwiftKit.retain(obj.$memorySegment());
49+
SwiftKit.release(obj.$memorySegment());
5550

56-
MySwiftStruct swiftValue = new MySwiftStruct(12);
51+
obj.voidMethod();
52+
obj.takeIntMethod(42);
5753

54+
MySwiftStruct swiftValue = new MySwiftStruct(arena, 2222, 1111);
5855
}
5956

6057
System.out.println("DONE.");
6158
}
6259

6360
public static native long jniWriteString(String str);
61+
6462
public static native long jniGetInt();
6563

6664
}

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@ public class MySwiftClassTest {
2828
void checkPaths(Throwable throwable) {
2929
var paths = SwiftKit.getJavaLibraryPath().split(":");
3030
for (var path : paths) {
31-
System.out.println("CHECKING PATH: " + path);
3231
Stream.of(new File(path).listFiles())
3332
.filter(file -> !file.isDirectory())
3433
.forEach((file) -> {

Samples/SwiftKitSampleApp/src/test/java/com/example/swift/MySwiftStructTest.java renamed to Samples/SwiftKitSampleApp/src/test/java/org/swift/swiftkit/MySwiftStructTest.java

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,29 +9,29 @@
99
// See CONTRIBUTORS.txt for the list of Swift.org project authors
1010
//
1111
// SPDX-License-Identifier: Apache-2.0
12+
13+
1214
//
1315
//===----------------------------------------------------------------------===//
1416

15-
package com.example.swift;
17+
package org.swift.swiftkit;
1618

17-
import org.junit.jupiter.api.Disabled;
19+
import com.example.swift.MySwiftStruct;
1820
import org.junit.jupiter.api.Test;
19-
import org.swift.swiftkit.SwiftArena;
20-
import org.swift.swiftkit.SwiftKit;
21-
22-
import java.io.File;
23-
import java.util.stream.Stream;
2421

2522
import static org.junit.jupiter.api.Assertions.assertEquals;
2623

2724
public class MySwiftStructTest {
2825

2926
@Test
30-
void test_MySwiftClass_voidMethod() {
27+
void create_struct() {
3128
try (var arena = SwiftArena.ofConfined()) {
32-
MySwiftStruct o = new MySwiftStruct(12);
33-
// o.voidMethod();
29+
long cap = 12;
30+
long len = 34;
31+
var struct = new MySwiftStruct(arena, cap, len);
32+
33+
assertEquals(cap, struct.getCapacity());
34+
assertEquals(len, struct.getLength());
3435
}
3536
}
36-
3737
}

Sources/JExtractSwift/ImportedDecls.swift

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -228,13 +228,13 @@ public struct ImportedFunc: ImportedDecl, CustomStringConvertible {
228228
case nil, .wrapper:
229229
break
230230

231-
case .pointer:
231+
case .pointer where !isInit:
232232
let selfParam: FunctionParameterSyntax = "self$: $swift_pointer"
233233
params.append(
234234
ImportedParam(syntax: selfParam, type: parent)
235235
)
236236

237-
case .memorySegment:
237+
case .memorySegment where !isInit:
238238
let selfParam: FunctionParameterSyntax = "self$: $java_lang_foreign_MemorySegment"
239239
var parentForSelf = parent
240240
parentForSelf.javaType = .javaForeignMemorySegment
@@ -244,6 +244,9 @@ public struct ImportedFunc: ImportedDecl, CustomStringConvertible {
244244

245245
case .swiftThunkSelf:
246246
break
247+
248+
default:
249+
break
247250
}
248251

249252
// TODO: add any metadata for generics and other things we may need to add here
@@ -262,6 +265,11 @@ public struct ImportedFunc: ImportedDecl, CustomStringConvertible {
262265

263266
public var isInit: Bool = false
264267

268+
public var isIndirectReturn: Bool {
269+
returnType.isValueType ||
270+
(isInit && (parent?.isValueType ?? false))
271+
}
272+
265273
public init(
266274
module: String,
267275
decl: any DeclSyntaxProtocol,

Sources/JExtractSwift/Swift2JavaTranslator+MemoryLayouts.swift

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,17 +18,14 @@ import SwiftParser
1818
import SwiftSyntax
1919

2020
extension Swift2JavaTranslator {
21+
2122
public func javaMemoryLayoutDescriptors(
2223
forParametersOf decl: ImportedFunc,
2324
paramPassingStyle: SelfParameterVariant?
2425
) -> [ForeignValueLayout] {
2526
var layouts: [ForeignValueLayout] = []
2627
layouts.reserveCapacity(decl.parameters.count + 1)
2728

28-
// // When the method is `init()` it does not accept a self (well, unless allocating init but we don't import those)
29-
// let paramPassingStyle: SelfParameterVariant? =
30-
// decl.isInit ? nil : .wrapper
31-
3229
for param in decl.effectiveParameters(paramPassingStyle: paramPassingStyle) {
3330
if param.type.cCompatibleJavaMemoryLayout == CCompatibleJavaMemoryLayout.primitive(.void) {
3431
continue
@@ -39,6 +36,13 @@ extension Swift2JavaTranslator {
3936
layouts.append(layout)
4037
}
4138

39+
// an indirect return passes the buffer as the last parameter to our thunk
40+
if decl.isIndirectReturn {
41+
var layout = ForeignValueLayout.SwiftPointer
42+
layout.inlineComment = "indirect return buffer"
43+
layouts.append(layout)
44+
}
45+
4246
return layouts
4347
}
4448
}

Sources/JExtractSwift/Swift2JavaTranslator+Printing.swift

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -755,11 +755,21 @@ extension Swift2JavaTranslator {
755755
let identifier = accessorKind.renderMethodName(decl)
756756

757757
if paramPassingStyle == SelfParameterVariant.wrapper {
758+
let guardFromDestroyedObjectCalls: String =
759+
if decl.hasParent {
760+
"""
761+
if (this.$state$destroyed.get()) {
762+
throw new IllegalStateException("Attempted to call method on already destroyed instance of " + getClass().getSimpleName() + "!");
763+
}
764+
"""
765+
} else { "" }
766+
758767
// delegate to the MemorySegment "self" accepting overload
759768
printer.print(
760769
"""
761770
\(javaDocComment)
762771
public \(returnTy) \(identifier)(\(renderJavaParamDecls(decl, paramPassingStyle: .wrapper))) {
772+
\(guardFromDestroyedObjectCalls)
763773
\(maybeReturnCast) \(identifier)(\(renderForwardJavaParams(decl, paramPassingStyle: .wrapper)));
764774
}
765775
"""
@@ -1048,19 +1058,13 @@ extension Swift2JavaTranslator {
10481058
let fieldName = accessorKind.renderDescFieldName
10491059
printer.start("public static final FunctionDescriptor \(fieldName) = ")
10501060

1051-
let isIndirectReturn =
1052-
decl.returnType.isValueType || (decl.isInit && (decl.parent?.isValueType ?? false))
1061+
let isIndirectReturn = decl.isIndirectReturn
10531062

10541063
var parameterLayoutDescriptors: [ForeignValueLayout] = javaMemoryLayoutDescriptors(
10551064
forParametersOf: decl,
10561065
paramPassingStyle: .pointer
10571066
)
10581067

1059-
// Write indirect return buffer parameter after formal
1060-
// if isIndirectReturn || decl.isInit {
1061-
// parameterLayoutDescriptors.append(.SwiftPointer)
1062-
// }
1063-
10641068
if decl.returnType.javaType == .void || isIndirectReturn {
10651069
printer.print("FunctionDescriptor.ofVoid(")
10661070
printer.indent()
@@ -1088,11 +1092,6 @@ extension Swift2JavaTranslator {
10881092
printer.print(desc, .parameterNewlineSeparator(isLast))
10891093
}
10901094

1091-
// // Write indirect return buffer parameter after formal
1092-
// if isIndirectReturn {
1093-
// printer.print(", /* indirect return buffer */SWIFT_POINTER")
1094-
// }
1095-
10961095
printer.outdent()
10971096
printer.print(");")
10981097
}

Sources/JExtractSwift/SwiftThunkTranslator.swift

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -50,15 +50,15 @@ struct SwiftThunkTranslator {
5050
decls.append(contentsOf: render(forFunc: decl))
5151
}
5252

53-
// TODO: handle variables
54-
// for v in nominal.variables {
55-
// if let acc = v.accessorFunc(kind: .get) {
56-
// decls.append(contentsOf: render(forFunc: acc))
57-
// }
58-
// if let acc = v.accessorFunc(kind: .set) {
59-
// decls.append(contentsOf: render(forFunc: acc))
60-
// }
61-
// }
53+
// TODO: handle variables
54+
// for v in nominal.variables {
55+
// if let acc = v.accessorFunc(kind: .get) {
56+
// decls.append(contentsOf: render(forFunc: acc))
57+
// }
58+
// if let acc = v.accessorFunc(kind: .set) {
59+
// decls.append(contentsOf: render(forFunc: acc))
60+
// }
61+
// }
6262

6363
return decls
6464
}

SwiftKit/src/main/java/org/swift/swiftkit/SwiftValueWitnessTable.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ public static long sizeOfSwiftType(MemorySegment typeMetadata) {
106106
* Variable handle for the "stride" field within the value witness table.
107107
*/
108108
static final VarHandle $stride$mh =
109-
$LAYOUT.varHandle(MemoryLayout.PathElement.groupElement("size"));
109+
$LAYOUT.varHandle(MemoryLayout.PathElement.groupElement("stride"));
110110

111111
/**
112112
* Determine the stride of a Swift type given its type metadata, which is

0 commit comments

Comments
 (0)