Skip to content

Commit 0778266

Browse files
committed
[JExtract] Rework WIP
1 parent 01b8081 commit 0778266

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

47 files changed

+2042
-2848
lines changed

Samples/SwiftKitSampleApp/Sources/MySwiftLibrary/MySwiftClass.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,10 @@ public class MySwiftClass {
3434

3535
public var counter: Int32 = 0
3636

37+
public static func factory(len: Int, cap: Int) -> MySwiftClass {
38+
return MySwiftClass(len: len, cap: cap)
39+
}
40+
3741
public func voidMethod() {
3842
p("")
3943
}

Samples/SwiftKitSampleApp/build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ def jextract = tasks.register("jextract", Exec) {
8181

8282
workingDir = layout.projectDirectory
8383
commandLine "swift"
84-
args("package", "jextract", "-v", "--log-level", "info") // TODO: pass log level from Gradle build
84+
args("package", "jextract", "-v", "--log-level", "debug") // TODO: pass log level from Gradle build
8585
}
8686

8787
// Add the java-swift generated Java sources

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

Lines changed: 29 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,16 @@
1616

1717
// Import swift-extract generated sources
1818

19-
import com.example.swift.MySwiftLibrary;
20-
import com.example.swift.MySwiftClass;
21-
2219
// Import javakit/swiftkit support libraries
2320
import org.swift.swiftkit.SwiftArena;
2421
import org.swift.swiftkit.SwiftKit;
25-
import org.swift.swiftkit.SwiftValueWitnessTable;
22+
import org.swift.swiftkit.SwiftValueLayout;
23+
24+
import java.lang.foreign.Arena;
25+
import java.lang.foreign.MemoryLayout;
26+
import java.lang.foreign.MemorySegment;
27+
import java.lang.foreign.ValueLayout;
28+
import java.util.concurrent.Callable;
2629

2730
public class HelloJava2Swift {
2831

@@ -40,18 +43,33 @@ static void examples() {
4043

4144
MySwiftLibrary.globalTakeInt(1337);
4245

46+
long cnt = MySwiftLibrary.globalWriteString("String from Java");
47+
48+
SwiftKit.trace("count = " + cnt);
49+
50+
MySwiftLibrary.globalCallMeRunnable(() -> {
51+
SwiftKit.trace("running runnable");
52+
});
53+
4354
// Example of using an arena; MyClass.deinit is run at end of scope
4455
try (var arena = SwiftArena.ofConfined()) {
45-
MySwiftClass obj = new MySwiftClass(arena, 2222, 7777);
56+
MySwiftClass obj = new MySwiftClass(2222, 7777, arena);
57+
58+
// just checking retains/releases work
59+
SwiftKit.trace("retainCount = " + SwiftKit.retainCount(obj));
60+
SwiftKit.retain(obj);
61+
SwiftKit.trace("retainCount = " + SwiftKit.retainCount(obj));
62+
SwiftKit.release(obj);
63+
SwiftKit.trace("retainCount = " + SwiftKit.retainCount(obj));
4664

47-
// just checking retains/releases work
48-
SwiftKit.retain(obj.$memorySegment());
49-
SwiftKit.release(obj.$memorySegment());
65+
obj.setCounter(12);
66+
SwiftKit.trace("obj.counter = " + obj.getCounter());
5067

51-
obj.voidMethod();
52-
obj.takeIntMethod(42);
68+
obj.voidMethod();
69+
obj.takeIntMethod(42);
5370

54-
MySwiftStruct swiftValue = new MySwiftStruct(arena, 2222, 1111);
71+
MySwiftStruct swiftValue = new MySwiftStruct(2222, 1111, arena);
72+
SwiftKit.trace("swiftValue.capacity = " + swiftValue.getCapacity());
5573
}
5674

5775
System.out.println("DONE.");

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

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,15 +35,11 @@ public class MySwiftLibraryTest {
3535
@Test
3636
void call_helloWorld() {
3737
MySwiftLibrary.helloWorld();
38-
39-
assertNotNull(MySwiftLibrary.helloWorld$address());
4038
}
4139

4240
@Test
4341
void call_globalTakeInt() {
4442
MySwiftLibrary.globalTakeInt(12);
45-
46-
assertNotNull(MySwiftLibrary.globalTakeInt$address());
4743
}
4844

4945
@Test

Samples/SwiftKitSampleApp/src/test/java/org/swift/swiftkit/MySwiftClassTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ public class MySwiftClassTest {
2525
@Test
2626
void call_retain_retainCount_release() {
2727
var arena = SwiftArena.ofConfined();
28-
var obj = new MySwiftClass(arena, 1, 2);
28+
var obj = new MySwiftClass(1, 2, arena);
2929

3030
assertEquals(1, SwiftKit.retainCount(obj.$memorySegment()));
3131
// TODO: test directly on SwiftHeapObject inheriting obj

Samples/SwiftKitSampleApp/src/test/java/org/swift/swiftkit/MySwiftStructTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ void create_struct() {
2626
try (var arena = SwiftArena.ofConfined()) {
2727
long cap = 12;
2828
long len = 34;
29-
var struct = new MySwiftStruct(arena, cap, len);
29+
var struct = new MySwiftStruct(cap, len, arena);
3030

3131
assertEquals(cap, struct.getCapacity());
3232
assertEquals(len, struct.getLength());

Samples/SwiftKitSampleApp/src/test/java/org/swift/swiftkit/SwiftArenaTest.java

Lines changed: 3 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ static boolean isAmd64() {
4040
@DisabledIf("isAmd64")
4141
public void arena_releaseClassOnClose_class_ok() {
4242
try (var arena = SwiftArena.ofConfined()) {
43-
var obj = new MySwiftClass(arena,1, 2);
43+
var obj = new MySwiftClass(1, 2, arena);
4444

4545
retain(obj.$memorySegment());
4646
assertEquals(2, retainCount(obj.$memorySegment()));
@@ -57,7 +57,7 @@ public void arena_markAsDestroyed_preventUseAfterFree_class() {
5757
MySwiftClass unsafelyEscapedOutsideArenaScope = null;
5858

5959
try (var arena = SwiftArena.ofConfined()) {
60-
var obj = new MySwiftClass(arena,1, 2);
60+
var obj = new MySwiftClass(1, 2, arena);
6161
unsafelyEscapedOutsideArenaScope = obj;
6262
}
6363

@@ -76,7 +76,7 @@ public void arena_markAsDestroyed_preventUseAfterFree_struct() {
7676
MySwiftStruct unsafelyEscapedOutsideArenaScope = null;
7777

7878
try (var arena = SwiftArena.ofConfined()) {
79-
var s = new MySwiftStruct(arena,1, 2);
79+
var s = new MySwiftStruct(1, 2, arena);
8080
unsafelyEscapedOutsideArenaScope = s;
8181
}
8282

@@ -88,27 +88,6 @@ public void arena_markAsDestroyed_preventUseAfterFree_struct() {
8888
}
8989
}
9090

91-
@Test
92-
public void arena_releaseClassOnClose_class_leaked() {
93-
String memorySegmentDescription = "<none>";
94-
95-
try {
96-
try (var arena = SwiftArena.ofConfined()) {
97-
var obj = new MySwiftClass(arena,1, 2);
98-
memorySegmentDescription = obj.$memorySegment().toString();
99-
100-
// Pretend that we "leaked" the class, something still holds a reference to it while we try to destroy it
101-
retain(obj.$memorySegment());
102-
assertEquals(2, retainCount(obj.$memorySegment()));
103-
}
104-
105-
fail("Expected exception to be thrown while the arena is closed!");
106-
} catch (Exception ex) {
107-
// The message should point out which objects "leaked":
108-
assertTrue(ex.getMessage().contains(memorySegmentDescription));
109-
}
110-
}
111-
11291
@Test
11392
public void arena_initializeWithCopy_struct() {
11493

Sources/JExtractSwift/CDeclLowering/CRepresentation.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ extension CType {
2121
/// function, first go through Swift -> cdecl lowering. This function
2222
/// will throw an error if it encounters a type that is not expressible in
2323
/// C.
24-
init(cdeclType: SwiftType) throws {
24+
init(cdeclType: SwiftType) throws {
2525
switch cdeclType {
2626
case .nominal(let nominalType):
2727
if let knownType = nominalType.nominalTypeDecl.knownStandardLibraryType {

Sources/JExtractSwift/CTypes/CType.swift

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -298,3 +298,12 @@ extension CType {
298298
}
299299
}
300300
}
301+
302+
extension CType {
303+
var isVoid: Bool {
304+
return switch self {
305+
case .void: true
306+
default: false
307+
}
308+
}
309+
}

Sources/JExtractSwift/CodePrinter.swift

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -68,16 +68,16 @@ public struct CodePrinter {
6868
}
6969
}
7070

71-
public mutating func printTypeDecl(
72-
_ text: Any,
71+
public mutating func printBraceBlock(
72+
_ header: Any,
7373
function: String = #function,
7474
file: String = #fileID,
7575
line: UInt = #line,
76-
body: (inout CodePrinter) -> ()
77-
) {
78-
print("\(text) {")
76+
body: (inout CodePrinter) throws -> ()
77+
) rethrows {
78+
print("\(header) {")
7979
indent()
80-
body(&self)
80+
try body(&self)
8181
outdent()
8282
print("}", .sloc, function: function, file: file, line: line)
8383
}
@@ -145,9 +145,10 @@ public struct CodePrinter {
145145

146146
// TODO: remove this in real mode, this just helps visually while working on it
147147
public mutating func printSeparator(_ text: String) {
148-
// TODO: actually use the indentationDepth
148+
assert(!text.contains(where: \.isNewline))
149149
print(
150150
"""
151+
151152
// ==== --------------------------------------------------
152153
// \(text)
153154

Sources/JExtractSwift/Convenience/SwiftSyntax+Extensions.swift

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -37,21 +37,6 @@ extension ImplicitlyUnwrappedOptionalTypeSyntax {
3737
}
3838

3939
extension SyntaxProtocol {
40-
41-
var asNominalTypeKind: NominalTypeKind {
42-
if isClass {
43-
.class
44-
} else if isActor {
45-
.actor
46-
} else if isStruct {
47-
.struct
48-
} else if isEnum {
49-
.enum
50-
} else {
51-
fatalError("Unknown nominal kind: \(self)")
52-
}
53-
}
54-
5540
var isClass: Bool {
5641
return self.is(ClassDeclSyntax.self)
5742
}

Sources/JExtractSwift/ImportedDecls+Printing.swift

Lines changed: 2 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -20,82 +20,14 @@ extension ImportedFunc {
2020
/// Render a `@{@snippet ... }` comment section that can be put inside a JavaDoc comment
2121
/// when referring to the original declaration a printed method refers to.
2222
var renderCommentSnippet: String? {
23-
if let syntax {
23+
if let signatureString {
2424
"""
2525
* {@snippet lang=swift :
26-
* \(syntax)
26+
* \(signatureString)
2727
* }
2828
"""
2929
} else {
3030
nil
3131
}
3232
}
3333
}
34-
35-
extension VariableAccessorKind {
36-
37-
public var fieldSuffix: String {
38-
switch self {
39-
case .get: "_GET"
40-
case .set: "_SET"
41-
}
42-
}
43-
44-
public var renderDescFieldName: String {
45-
switch self {
46-
case .get: "DESC_GET"
47-
case .set: "DESC_SET"
48-
}
49-
}
50-
51-
public var renderAddrFieldName: String {
52-
switch self {
53-
case .get: "ADDR_GET"
54-
case .set: "ADDR_SET"
55-
}
56-
}
57-
58-
public var renderHandleFieldName: String {
59-
switch self {
60-
case .get: "HANDLE_GET"
61-
case .set: "HANDLE_SET"
62-
}
63-
}
64-
65-
/// Renders a "$get" part that can be used in a method signature representing this accessor.
66-
public var renderMethodNameSegment: String {
67-
switch self {
68-
case .get: "$get"
69-
case .set: "$set"
70-
}
71-
}
72-
73-
func renderMethodName(_ decl: ImportedFunc) -> String? {
74-
switch self {
75-
case .get: "get\(decl.identifier.toCamelCase)"
76-
case .set: "set\(decl.identifier.toCamelCase)"
77-
}
78-
}
79-
}
80-
81-
extension Optional where Wrapped == VariableAccessorKind {
82-
public var renderDescFieldName: String {
83-
self?.renderDescFieldName ?? "DESC"
84-
}
85-
86-
public var renderAddrFieldName: String {
87-
self?.renderAddrFieldName ?? "ADDR"
88-
}
89-
90-
public var renderHandleFieldName: String {
91-
self?.renderHandleFieldName ?? "HANDLE"
92-
}
93-
94-
public var renderMethodNameSegment: String {
95-
self?.renderMethodNameSegment ?? ""
96-
}
97-
98-
func renderMethodName(_ decl: ImportedFunc) -> String {
99-
self?.renderMethodName(decl) ?? decl.baseIdentifier
100-
}
101-
}

0 commit comments

Comments
 (0)