Skip to content

Commit a01680f

Browse files
committed
[JExtract] Rework WIP
1 parent ecb0652 commit a01680f

File tree

52 files changed

+2924
-3455
lines changed

Some content is hidden

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

52 files changed

+2924
-3455
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.");

Sources/JExtractSwift/CDeclLowering/CDeclConversions.swift

Lines changed: 0 additions & 195 deletions
This file was deleted.

Sources/JExtractSwift/CDeclLowering/CRepresentation.swift

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,25 @@ 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):
27-
if let knownType = nominalType.nominalTypeDecl.knownStandardLibraryType,
28-
let primitiveCType = knownType.primitiveCType {
29-
self = primitiveCType
30-
return
27+
if let knownType = nominalType.nominalTypeDecl.knownStandardLibraryType {
28+
if let primitiveCType = knownType.primitiveCType {
29+
self = primitiveCType
30+
return
31+
}
32+
33+
switch knownType {
34+
case .unsafePointer where nominalType.genericArguments?.count == 1:
35+
self = .pointer(.qualified(const: true, volatile: false, type: try CType(cdeclType: nominalType.genericArguments![0])))
36+
return
37+
case .unsafeMutablePointer where nominalType.genericArguments?.count == 1:
38+
self = .pointer(try CType(cdeclType: nominalType.genericArguments![0]))
39+
return
40+
default:
41+
break
42+
}
3143
}
3244

3345
throw CDeclToCLoweringError.invalidNominalType(nominalType.nominalTypeDecl)
@@ -109,7 +121,8 @@ extension KnownStandardLibraryType {
109121
case .unsafeRawPointer: .pointer(
110122
.qualified(const: true, volatile: false, type: .void)
111123
)
112-
case .unsafePointer, .unsafeMutablePointer, .unsafeBufferPointer, .unsafeMutableBufferPointer:
124+
case .void: .void
125+
case .unsafePointer, .unsafeMutablePointer, .unsafeBufferPointer, .unsafeMutableBufferPointer, .string:
113126
nil
114127
}
115128
}

0 commit comments

Comments
 (0)