Skip to content

Commit 117457c

Browse files
committed
jextract: end to end run for a property import
1 parent aa2d017 commit 117457c

File tree

5 files changed

+65
-36
lines changed

5 files changed

+65
-36
lines changed

Samples/SwiftKitSampleApp/build.gradle

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,11 @@ application {
9595
task jextract(type: Exec) {
9696
description = "Extracts Java accessor sources using jextract"
9797
outputs.dir(layout.buildDirectory.dir("generated"))
98-
inputs.dir("$rootDir/Sources/ExampleSwiftLibrary")
98+
inputs.dir("$rootDir/Sources/ExampleSwiftLibrary") // monitored library
99+
100+
// any changes in the source generator sources also mean the resulting output might change
101+
inputs.dir("$rootDir/Sources/JExtractSwift")
102+
inputs.dir("$rootDir/Sources/JExtractSwiftTool")
99103

100104
workingDir = rootDir
101105
commandLine "make"

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

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
package com.example.swift.generated;
1616

1717
import org.junit.jupiter.api.BeforeAll;
18+
import org.junit.jupiter.api.Test;
1819

1920
import static org.junit.jupiter.api.Assertions.assertEquals;
2021
import static org.junit.jupiter.api.Assertions.assertNotNull;
@@ -31,6 +32,24 @@ static void beforeAll() {
3132
System.setProperty("jextract.trace.downcalls", "true");
3233
}
3334

34-
// TODO: test member methods on MySwiftClass
35+
@Test
36+
void test_MySwiftClass_voidMethod() {
37+
MySwiftClass o = new MySwiftClass(12, 42);
38+
o.voidMethod();
39+
}
40+
41+
@Test
42+
void test_MySwiftClass_makeIntMethod() {
43+
MySwiftClass o = new MySwiftClass(12, 42);
44+
var got = o.makeIntMethod();
45+
assertEquals(12, got);
46+
}
47+
48+
@Test
49+
void test_MySwiftClass_property_len() {
50+
MySwiftClass o = new MySwiftClass(12, 42);
51+
var got = o.makeIntMethod();
52+
assertEquals(12, got);
53+
}
3554

3655
}

Sources/JExtractSwift/ImportedDecls+Printing.swift

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,14 @@ extension ImportedFunc {
3434
}
3535

3636
extension VariableAccessorKind {
37+
38+
public var fieldSuffix: String {
39+
switch self {
40+
case .get: "_GET"
41+
case .set: "_SET"
42+
}
43+
}
44+
3745
public var renderDescFieldName: String {
3846
switch self {
3947
case .get: "DESC_GET"

Sources/JExtractSwift/Swift2JavaTranslator+Printing.swift

Lines changed: 27 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,9 @@ extension Swift2JavaTranslator {
129129
}
130130

131131
// Properties
132-
// TODO: property accessors
132+
for varDecl in decl.variables {
133+
printVariableDowncallMethods(&printer, varDecl)
134+
}
133135

134136
// Methods
135137
for funcDecl in decl.methods {
@@ -482,8 +484,8 @@ extension Swift2JavaTranslator {
482484
decl: ImportedFunc,
483485
accessorKind: VariableAccessorKind? = nil) {
484486

485-
let addrName = accessorKind?.renderAddrFieldName ?? "ADDR"
486-
let methodNameSegment = accessorKind?.renderMethodNameSegment ?? ""
487+
let addrName = accessorKind.renderAddrFieldName
488+
let methodNameSegment = accessorKind.renderMethodNameSegment
487489
let snippet = decl.renderCommentSnippet ?? "* "
488490

489491
printer.print(
@@ -502,8 +504,8 @@ extension Swift2JavaTranslator {
502504
private func printFunctionMethodHandleMethod(_ printer: inout CodePrinter,
503505
decl: ImportedFunc,
504506
accessorKind: VariableAccessorKind? = nil) {
505-
let handleName = accessorKind?.renderHandleFieldName ?? "HANDLE"
506-
let methodNameSegment = accessorKind?.renderMethodNameSegment ?? ""
507+
let handleName = accessorKind.renderHandleFieldName
508+
let methodNameSegment = accessorKind.renderMethodNameSegment
507509
let snippet = decl.renderCommentSnippet ?? "* "
508510

509511
printer.print(
@@ -522,8 +524,8 @@ extension Swift2JavaTranslator {
522524
private func printFunctionDescriptorMethod(_ printer: inout CodePrinter,
523525
decl: ImportedFunc,
524526
accessorKind: VariableAccessorKind? = nil) {
525-
let descName = accessorKind?.renderDescFieldName ?? "DESC"
526-
let methodNameSegment = accessorKind?.renderMethodNameSegment ?? ""
527+
let descName = accessorKind.renderDescFieldName
528+
let methodNameSegment = accessorKind.renderMethodNameSegment
527529
let snippet = decl.renderCommentSnippet ?? "* "
528530

529531
printer.print(
@@ -544,10 +546,14 @@ extension Swift2JavaTranslator {
544546

545547
printer.printTypeDecl("private static class \(decl.baseIdentifier)") { printer in
546548
for accessorKind in decl.supportedAccessorKinds {
547-
printPropertyAccessorDescriptorValue(&printer, decl, accessorKind)
548-
// printFunctionDescriptorValue(&printer, decl);
549-
// printFindMemorySegmentAddrByMangledName(&printer, decl)
550-
// printMethodDowncallHandleForAddrDesc(&printer)
549+
guard let accessor = decl.accessorFunc(kind: accessorKind) else {
550+
log.warning("Skip print for \(accessorKind) of \(decl.identifier)!")
551+
continue
552+
}
553+
554+
printFunctionDescriptorValue(&printer, accessor, accessorKind: accessorKind);
555+
printFindMemorySegmentAddrByMangledName(&printer, accessor, accessorKind: accessorKind)
556+
printMethodDowncallHandleForAddrDesc(&printer, accessorKind: accessorKind)
551557
}
552558
}
553559

@@ -579,23 +585,24 @@ extension Swift2JavaTranslator {
579585
}
580586
}
581587

582-
func printFindMemorySegmentAddrByMangledName(_ printer: inout CodePrinter, _ decl: ImportedFunc) {
588+
func printFindMemorySegmentAddrByMangledName(_ printer: inout CodePrinter, _ decl: ImportedFunc,
589+
accessorKind: VariableAccessorKind? = nil) {
583590
printer.print(
584591
"""
585592
/**
586-
* {@snippet lang = Swift:
587-
* \(decl.displayName)
588-
* }
593+
\(decl.renderCommentSnippet ?? "* ")
589594
*/
590-
public static final MemorySegment ADDR = \(swiftModuleName).findOrThrow("\(decl.swiftMangledName)");
595+
public static final MemorySegment \(accessorKind.renderAddrFieldName) =
596+
\(swiftModuleName).findOrThrow("\(decl.swiftMangledName)");
591597
"""
592598
);
593599
}
594600

595-
func printMethodDowncallHandleForAddrDesc(_ printer: inout CodePrinter) {
601+
func printMethodDowncallHandleForAddrDesc(_ printer: inout CodePrinter, accessorKind: VariableAccessorKind? = nil) {
596602
printer.print(
597603
"""
598-
public static final MethodHandle HANDLE = Linker.nativeLinker().downcallHandle(ADDR, DESC);
604+
public static final MethodHandle \(accessorKind.renderHandleFieldName) =
605+
Linker.nativeLinker().downcallHandle(\(accessorKind.renderAddrFieldName), \(accessorKind.renderDescFieldName));
599606
"""
600607
)
601608
}
@@ -781,7 +788,8 @@ extension Swift2JavaTranslator {
781788
public func printFunctionDescriptorValue(
782789
_ printer: inout CodePrinter,
783790
_ decl: ImportedFunc,
784-
fieldName: String = "DESC") {
791+
accessorKind: VariableAccessorKind? = nil) {
792+
let fieldName = accessorKind.renderDescFieldName
785793
printer.start("public static final FunctionDescriptor \(fieldName) = ")
786794

787795
let parameterLayoutDescriptors = javaMemoryLayoutDescriptors(
@@ -818,14 +826,4 @@ extension Swift2JavaTranslator {
818826
printer.print(");");
819827
}
820828

821-
public func printPropertyAccessorDescriptorValue(
822-
_ printer: inout CodePrinter,
823-
_ decl: ImportedVariable,
824-
_ kind: VariableAccessorKind) {
825-
guard let funcDecl = decl.accessorFunc(kind: kind) else {
826-
return
827-
}
828-
829-
printFunctionDescriptorValue(&printer, funcDecl, fieldName: kind.renderDescFieldName)
830-
}
831829
}

Tests/JExtractSwiftTests/FunctionDescriptorImportTests.swift

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -162,8 +162,8 @@ extension FunctionDescriptorTests {
162162
}
163163

164164
func variableAccessorDescriptorTest(
165-
_ methodIdentifier: String,
166-
_ kind: VariableAccessorKind,
165+
_ identifier: String,
166+
_ accessorKind: VariableAccessorKind,
167167
javaPackage: String = "com.example.swift",
168168
swiftModuleName: String = "SwiftModule",
169169
logLevel: Logger.Level = .trace,
@@ -180,15 +180,15 @@ extension FunctionDescriptorTests {
180180
let varDecl: ImportedVariable? =
181181
st.importedTypes.values.compactMap {
182182
$0.variables.first {
183-
$0.identifier == methodIdentifier
183+
$0.identifier == identifier
184184
}
185185
}.first
186186
guard let varDecl else {
187-
fatalError("Cannot find descriptor of: \(methodIdentifier)")
187+
fatalError("Cannot find descriptor of: \(identifier)")
188188
}
189189

190190
let getOutput = CodePrinter.toString { printer in
191-
st.printPropertyAccessorDescriptorValue(&printer, varDecl, kind)
191+
printFunctionDescriptorValue(&printer, funcDecl, accessorKind: accessorKind)
192192
}
193193

194194
try await body(getOutput)

0 commit comments

Comments
 (0)