Skip to content

Commit 637d800

Browse files
committed
[JExtract] Adopt SwiftFunctionSignature and the lowering mechanisms
* Utilize `SwiftFunctionSignature` and the cdecl-lowering facilities throughout the code base. * `SwiftFunctionSignature` is created from the `DeclSyntax` in `Swift2JavaVisitor`. * `LoweredFunctionSignature` describing `@cdecl` thunk, is created from `SwiftFunctionSignature`. * `TranslatedFunctionSignature` describing generated Java API, is created from `LoweredFunctionSignature`. (Swift2JavaTranslator+JavaTranslation.swift) * `ImportedFunc` is now basically just a wrapper of `TranslatedFunctionSignature` with the `name`. * Remove `ImportedVariable`, instead variables are described as `ImportedFunc` as accessors. * Support APIs returning imported type values. E.g. `func factory(arg: Int) -> MyClass` such methods require `SwiftArena` parameter passed-in. * Built-in lowerings (e.g. `toCString(String)` for `String` -> C string conversion) are now implemented in JavaKit. * Stop emitting `MyClass::apiName$descriptor()` method etc. as they were not used. * Use the `@cdecl` thunk name as the function descriptor class name, for simplicity. * Getter and setter accessors are now completely separate API. No more `HANDLE_GET` and `HANDLE_SET` etc. Instead descriptor class is split to `$get` or `$set` suffixed name.
1 parent 476087b commit 637d800

32 files changed

+1325
-2344
lines changed

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

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,15 +30,11 @@ public class MySwiftLibraryTest {
3030
@Test
3131
void call_helloWorld() {
3232
MySwiftLibrary.helloWorld();
33-
34-
assertNotNull(MySwiftLibrary.helloWorld$address());
3533
}
3634

3735
@Test
3836
void call_globalTakeInt() {
3937
MySwiftLibrary.globalTakeInt(12);
40-
41-
assertNotNull(MySwiftLibrary.globalTakeInt$address());
4238
}
4339

4440
@Test

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

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

Sources/JExtractSwift/CDeclLowering/Swift2JavaTranslator+FunctionLowering.swift

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -619,14 +619,7 @@ extension LoweredFunctionSignature {
619619

620620
@_spi(Testing)
621621
public func cFunctionDecl(cName: String) throws -> CFunction {
622-
return CFunction(
623-
resultType: try CType(cdeclType: self.result.cdeclResultType),
624-
name: cName,
625-
parameters: try self.allLoweredParameters.map {
626-
try CParameter(name: $0.parameterName, type: CType(cdeclType: $0.type).parameterDecay)
627-
},
628-
isVariadic: false
629-
)
622+
try CFunction(cdeclSignature: self.cdeclSignature, cName: cName)
630623
}
631624
}
632625

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: 0 additions & 101 deletions
This file was deleted.

0 commit comments

Comments
 (0)