Skip to content

Commit c0dac2c

Browse files
committed
wip
1 parent e54d00e commit c0dac2c

File tree

14 files changed

+234
-72
lines changed

14 files changed

+234
-72
lines changed

Samples/SwiftAndJavaJarSampleLib/Sources/MySwiftLibrary/MySwiftLibrary.swift

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,12 @@ public class MySwiftClass {
5959
p("Deinit, self = 0x\(String(addr, radix: 16, uppercase: true))")
6060
}
6161

62-
public var counter: Int32 = 0
62+
public var counter: Int = 0
63+
64+
public func increment(by num: Int) -> Int {
65+
counter += num
66+
return counter
67+
}
6368

6469
public func voidMethod() {
6570
p("")

Samples/SwiftKitSampleApp/Sources/MySwiftLibrary/MySwiftLibrary+ManualThunks.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ public func swiftjava_manual_getArrayMySwiftClass() -> UnsafeMutableRawPointer /
3030
}
3131

3232
@_cdecl("swiftjava_SwiftKitSwift_Array_count") // FIXME: hardcoded for MySwiftClass
33-
public func swiftjava_SwiftKitSwift_Array____count(
33+
public func swiftjava_SwiftKitSwift_Array_count(
3434
rawPointer: UnsafeMutableRawPointer, // Array<T>
3535
elementType: UnsafeMutableRawPointer // Metadata of T
3636
) -> Int {
@@ -46,7 +46,7 @@ public func swiftjava_SwiftKitSwift_Array____count(
4646
}
4747

4848
@_cdecl("swiftjava_SwiftKitSwift_Array_get") // FIXME: hardcoded for MySwiftClass
49-
public func swiftjava_SwiftKitSwift_Array____get(
49+
public func swiftjava_SwiftKitSwift_Array_get(
5050
rawPointer: UnsafeMutableRawPointer, // Array<T>
5151
index: Int,
5252
elementType: UnsafeMutableRawPointer // Metadata of T

Samples/SwiftKitSampleApp/Sources/MySwiftLibrary/MySwiftLibrary.swift

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -56,15 +56,25 @@ public func getArrayInt() -> [Int] {
5656
}
5757

5858
let DATA = [
59-
MySwiftClass(len: 1, cap: 11),
60-
MySwiftClass(len: 2, cap: 22),
61-
MySwiftClass(len: 3, cap: 33),
62-
]
59+
MySwiftClass(len: 1, cap: 11),
60+
MySwiftClass(len: 2, cap: 22),
61+
MySwiftClass(len: 3, cap: 33),
62+
]
6363

6464
public func getArrayMySwiftClass() -> [MySwiftClass] {
6565
DATA
6666
}
6767

68+
69+
let BYTES_DATA: [UInt8] = [
70+
1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
71+
]
72+
73+
74+
public func getByteArray() -> [UInt8] {
75+
BYTES_DATA
76+
}
77+
6878
public class MySwiftClass {
6979

7080
public var len: Int

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

Lines changed: 33 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,15 @@
1717
// Import swift-extract generated sources
1818

1919
// Import javakit/swiftkit support libraries
20+
import org.swift.swiftkit.SwiftAnyType;
2021
import org.swift.swiftkit.SwiftArena;
2122
import org.swift.swiftkit.SwiftArrayRef;
2223
import org.swift.swiftkit.SwiftKit;
2324

25+
import java.lang.foreign.GroupLayout;
26+
import java.lang.foreign.MemoryLayout;
27+
import java.lang.foreign.SequenceLayout;
28+
2429
public class HelloJava2Swift {
2530

2631
public static void main(String[] args) {
@@ -49,38 +54,40 @@ static void examples() {
4954
// obj.takeIntMethod(42);
5055
// }
5156

52-
// // public func getArrayMySwiftClass() -> [MySwiftClass]
53-
// SwiftArrayRef<MySwiftClass> arr = ManualImportedMethods.getArrayMySwiftClass();
54-
//
55-
// MySwiftClass first = arr.get(0, MySwiftClass::new);
56-
// System.out.println("[java] first = " + first);
57-
//
58-
// // FIXME: properties don't work yet, need the thunks!
59-
//// System.out.println("[java] first.getLen() = " + first.getLen());
60-
//// assert(first.getLen() == 1);
61-
//// System.out.println("[java] first.getCap() = " + first.getCap());
62-
//// assert(first.getCap() == 2);
63-
//
64-
// System.out.println("[java] first.getterForLen() = " + first.getterForLen());
65-
// System.out.println("[java] first.getForCap() = " + first.getterForCap());
66-
// precondition(1, first.getterForLen());
67-
// precondition(11, first.getterForCap());
68-
//
69-
// MySwiftClass second = arr.get(1, MySwiftClass::new);
70-
// System.out.println("[java] second = " + second);
71-
// System.out.println("[java] second.getterForLen() = " + second.getterForLen());
72-
// System.out.println("[java] second.getForCap() = " + second.getterForCap());
73-
// precondition(2, second.getterForLen());
74-
// precondition(22, second.getterForCap());
57+
// public func getArrayMySwiftClass() -> [MySwiftClass]
58+
SwiftArrayRef<MySwiftClass> arr = ManualImportedMethods.getArrayMySwiftClass();
59+
60+
precondition(3, arr.count());
61+
62+
MySwiftClass first = arr.get(0);
63+
System.out.println("[java] first = " + first);
64+
65+
// FIXME: properties don't work yet, need the thunks!
66+
// System.out.println("[java] first.getLen() = " + first.getLen());
67+
// assert(first.getLen() == 1);
68+
// System.out.println("[java] first.getCap() = " + first.getCap());
69+
// assert(first.getCap() == 2);
70+
71+
System.out.println("[java] first.getterForLen() = " + first.getterForLen());
72+
System.out.println("[java] first.getForCap() = " + first.getterForCap());
73+
precondition(1, first.getterForLen());
74+
precondition(11, first.getterForCap());
75+
76+
MySwiftClass second = arr.get(1);
77+
precondition(2, second.getterForLen());
78+
precondition(22, second.getterForCap());
7579

7680
try (var arena = SwiftArena.ofConfined()) {
7781
MySwiftStruct struct = new MySwiftStruct(arena, 44);
78-
// long theNumber = struct.getTheNumber();
79-
// precondition(44, theNumber);
80-
82+
System.out.println("struct.getTheNumber() = " + struct.getTheNumber());
83+
long theNumber = struct.getTheNumber();
84+
precondition(44, theNumber);
8185
}
8286

8387

88+
arr.get(0);
89+
90+
8491
System.out.println("DONE.");
8592
}
8693

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

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import java.lang.foreign.MemorySegment;
2424
import java.lang.invoke.MethodHandle;
2525

26+
import static org.swift.swiftkit.SwiftValueLayout.SWIFT_INT;
2627
import static org.swift.swiftkit.SwiftValueLayout.SWIFT_POINTER;
2728

2829
public final class ManualImportedMethods {
@@ -61,10 +62,43 @@ public static SwiftArrayRef<MySwiftClass> getArrayMySwiftClass() {
6162
return new SwiftArrayRef<>(
6263
arena,
6364
arrayPointer,
64-
/* element type = */MySwiftClass.TYPE_METADATA
65+
/* element type = */MySwiftClass.TYPE_METADATA,
66+
MySwiftClass::new
6567
);
6668
} catch (Throwable e) {
6769
throw new RuntimeException("Failed to invoke Swift method", e);
6870
}
6971
}
72+
73+
// private static class getByteArray {
74+
// public static final FunctionDescriptor DESC = FunctionDescriptor.of(
75+
// /* -> */SWIFT_POINTER,
76+
// /* size */SWIFT_INT
77+
// );
78+
// public static final MemorySegment ADDR =
79+
// SwiftKit.findOrThrow("swiftjava_manual_getByteArray");
80+
//
81+
// public static final MethodHandle HANDLE = Linker.nativeLinker().downcallHandle(ADDR, DESC);
82+
// }
83+
//
84+
// public static SwiftPrimiviteArrayRef getByteArray() {
85+
// MethodHandle mh = getByteArray.HANDLE;
86+
//
87+
// Arena arena = Arena.ofAuto();
88+
// try {
89+
// if (SwiftKit.TRACE_DOWNCALLS) {
90+
// SwiftKit.traceDowncall();
91+
// }
92+
//
93+
// MemorySegment arrayPointer = (MemorySegment) mh.invokeExact();
94+
// return new SwiftArrayRef<>(
95+
// arena,
96+
// arrayPointer,
97+
// /* element type = */,
98+
// MySwiftClass::new
99+
// );
100+
// } catch (Throwable e) {
101+
// throw new RuntimeException("Failed to invoke Swift method", e);
102+
// }
103+
// }
70104
}

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

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -41,12 +41,12 @@ void checkPaths(Throwable throwable) {
4141

4242
@Test
4343
void test_MySwiftClass_voidMethod() {
44-
try {
45-
MySwiftClass o = new MySwiftClass(12, 42);
46-
o.voidMethod();
47-
} catch (Throwable throwable) {
48-
checkPaths(throwable);
49-
}
44+
// try {
45+
// MySwiftClass o = new MySwiftClass(12, 42);
46+
// o.voidMethod();
47+
// } catch (Throwable throwable) {
48+
// checkPaths(throwable);
49+
// }
5050
}
5151

5252
@Test
@@ -56,12 +56,12 @@ void test_MySwiftClass_makeIntMethod() {
5656
assertEquals(12, got);
5757
}
5858

59-
@Test
60-
@Disabled // TODO: Need var mangled names in interfaces
61-
void test_MySwiftClass_property_len() {
62-
MySwiftClass o = new MySwiftClass(12, 42);
63-
var got = o.getLen();
64-
assertEquals(12, got);
65-
}
59+
// @Test
60+
// @Disabled // TODO: Implement properties again
61+
// void test_MySwiftClass_property_len() {
62+
// MySwiftClass o = new MySwiftClass(12, 42);
63+
// var got = o.getLen();
64+
// assertEquals(12, got);
65+
// }
6666

6767
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// This source file is part of the Swift.org open source project
4+
//
5+
// Copyright (c) 2024 Apple Inc. and the Swift.org project authors
6+
// Licensed under Apache License v2.0
7+
//
8+
// See LICENSE.txt for license information
9+
// See CONTRIBUTORS.txt for the list of Swift.org project authors
10+
//
11+
// SPDX-License-Identifier: Apache-2.0
12+
//
13+
//===----------------------------------------------------------------------===//
14+
15+
package org.swift.swiftkit;
16+
17+
import com.example.swift.MySwiftClass;
18+
import com.example.swift.MySwiftStruct;
19+
import org.junit.jupiter.api.Test;
20+
21+
import static org.junit.jupiter.api.Assertions.assertEquals;
22+
23+
public class MySwiftStructTest {
24+
25+
@Test
26+
void create_struct() {
27+
try (var arena = SwiftArena.ofConfined()) {
28+
long expectedNumber = 128;
29+
var struct = new MySwiftStruct(arena, expectedNumber);
30+
31+
long theNumber = struct.getTheNumber();
32+
assertEquals(expectedNumber, theNumber);
33+
}
34+
}
35+
}

Samples/SwiftKitSampleApp/src/test/java/org/swift/swiftkit/SwiftArrayTest.java renamed to Samples/SwiftKitSampleApp/src/test/java/org/swift/swiftkit/SwiftArrayRefTest.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121

2222
import static org.junit.jupiter.api.Assertions.assertEquals;
2323

24-
public class SwiftArrayTest {
24+
public class SwiftArrayRefTest {
2525

2626
@BeforeAll
2727
public static void setUp() {
@@ -35,7 +35,7 @@ public void array_of_MySwiftClass_get() {
3535
try (var arena = SwiftArena.ofConfined()) {
3636
SwiftArrayRef<MySwiftClass> arr = ManualImportedMethods.getArrayMySwiftClass();
3737

38-
MySwiftClass first = arr.get(0, MySwiftClass::new);
38+
MySwiftClass first = arr.get(0);
3939
System.out.println("[java] first = " + first);
4040

4141
// FIXME: properties don't work yet, need the thunks!
@@ -49,7 +49,7 @@ public void array_of_MySwiftClass_get() {
4949
assertEquals(1, first.getterForLen());
5050
assertEquals(11, first.getterForCap());
5151

52-
MySwiftClass second = arr.get(1, MySwiftClass::new);
52+
MySwiftClass second = arr.get(1);
5353
System.out.println("[java] second = " + second);
5454
System.out.println("[java] second.getterForLen() = " + second.getterForLen());
5555
System.out.println("[java] second.getForCap() = " + second.getterForCap());

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

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,17 @@ public final class SwiftAnyType {
2626

2727
private final MemorySegment memorySegment;
2828

29-
public SwiftAnyType(MemorySegment memorySegment) {
30-
// if (SwiftKit.getSwiftInt(memorySegment, 0) > 0) {
31-
// throw new IllegalArgumentException("A Swift Any.Type cannot be null!");
32-
// }
29+
public static SwiftAnyType SWIFT_INT = SwiftKit.getTypeByMangledNameInEnvironment("SiSg").get();
30+
public static SwiftAnyType SWIFT_UINT = SwiftKit.getTypeByMangledNameInEnvironment("SuSg").get();
31+
public static SwiftAnyType SWIFT_LONG = SwiftKit.getTypeByMangledNameInEnvironment("SiSg").get();
32+
public static SwiftAnyType SWIFT_BOOL = SwiftKit.getTypeByMangledNameInEnvironment("SbSg").get();
33+
public static SwiftAnyType SWIFT_DOUBLE = SwiftKit.getTypeByMangledNameInEnvironment("SdSg").get();
34+
public static SwiftAnyType SWIFT_FLOAT = SwiftKit.getTypeByMangledNameInEnvironment("SfSg").get();
35+
public static SwiftAnyType SWIFT_UNSAFE_RAW_POINTER = SwiftKit.getTypeByMangledNameInEnvironment("SVSg").get();
36+
public static SwiftAnyType SWIFT_UNSAFE_MUTABLE_RAW_POINTER = SwiftKit.getTypeByMangledNameInEnvironment("SvSg").get();
37+
public static SwiftAnyType SWIFT_string = SwiftKit.getTypeByMangledNameInEnvironment("SSg").get();
3338

39+
public SwiftAnyType(MemorySegment memorySegment) {
3440
this.memorySegment = memorySegment.asReadOnly();
3541
}
3642

@@ -56,11 +62,19 @@ public SwiftAnyType(SwiftHeapObject object) {
5662
return $LAYOUT;
5763
}
5864

65+
/**
66+
* Get the human-readable Swift type name of this type.
67+
*/
68+
public String getSwiftName() {
69+
return SwiftKit.nameOfSwiftType(memorySegment, true);
70+
}
71+
5972
@Override
6073
public String toString() {
6174
return "AnySwiftType{" +
62-
"name=" + SwiftKit.nameOfSwiftType(memorySegment, true) +
75+
"name=" + getSwiftName() +
6376
", memorySegment=" + memorySegment +
6477
'}';
6578
}
79+
6680
}

0 commit comments

Comments
 (0)