Skip to content

Commit 6ba1b36

Browse files
committed
[JExtract] Rework WIP
1 parent 33e54d0 commit 6ba1b36

31 files changed

+1685
-2501
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: 22 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,10 @@
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
20+
2321
import org.swift.swiftkit.SwiftArena;
2422
import org.swift.swiftkit.SwiftKit;
25-
import org.swift.swiftkit.SwiftValueWitnessTable;
2623

2724
public class HelloJava2Swift {
2825

@@ -40,21 +37,33 @@ static void examples() {
4037

4138
MySwiftLibrary.globalTakeInt(1337);
4239

40+
long cnt = MySwiftLibrary.globalWriteString("String from Java");
41+
42+
SwiftKit.trace("count = " + cnt);
43+
44+
MySwiftLibrary.globalCallMeRunnable(() -> {
45+
SwiftKit.trace("running runnable");
46+
});
47+
4348
// Example of using an arena; MyClass.deinit is run at end of scope
4449
try (var arena = SwiftArena.ofConfined()) {
45-
MySwiftClass obj = new MySwiftClass(2222, 7777, arena);
50+
MySwiftClass obj = new MySwiftClass(2222, 7777, arena);
51+
52+
// just checking retains/releases work
53+
SwiftKit.trace("retainCount = " + SwiftKit.retainCount(obj));
54+
SwiftKit.retain(obj);
55+
SwiftKit.trace("retainCount = " + SwiftKit.retainCount(obj));
56+
SwiftKit.release(obj);
57+
SwiftKit.trace("retainCount = " + SwiftKit.retainCount(obj));
4658

47-
// just checking retains/releases work
48-
SwiftKit.trace("retainCount = " + SwiftKit.retainCount(obj));
49-
SwiftKit.retain(obj);
50-
SwiftKit.trace("retainCount = " + SwiftKit.retainCount(obj));
51-
SwiftKit.release(obj);
52-
SwiftKit.trace("retainCount = " + SwiftKit.retainCount(obj));
59+
obj.setCounter(12);
60+
SwiftKit.trace("obj.counter = " + obj.getCounter());
5361

54-
obj.voidMethod();
55-
obj.takeIntMethod(42);
62+
obj.voidMethod();
63+
obj.takeIntMethod(42);
5664

5765
MySwiftStruct swiftValue = new MySwiftStruct(2222, 1111, arena);
66+
SwiftKit.trace("swiftValue.capacity = " + swiftValue.getCapacity());
5867
}
5968

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

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 & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -36,39 +36,6 @@ extension ImplicitlyUnwrappedOptionalTypeSyntax {
3636
}
3737
}
3838

39-
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-
55-
var isClass: Bool {
56-
return self.is(ClassDeclSyntax.self)
57-
}
58-
59-
var isActor: Bool {
60-
return self.is(ActorDeclSyntax.self)
61-
}
62-
63-
var isEnum: Bool {
64-
return self.is(EnumDeclSyntax.self)
65-
}
66-
67-
var isStruct: Bool {
68-
return self.is(StructDeclSyntax.self)
69-
}
70-
}
71-
7239
extension DeclModifierSyntax {
7340
var isAccessControl: Bool {
7441
switch self.name.tokenKind {

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)