Skip to content

Commit 6b64073

Browse files
committed
SwiftKit: use inner-static class pattern for DESC/ADDR/HANDLE constants
It's useful to keep the exact same pattern everywhere as it's so repetetive code.
1 parent b9122bd commit 6b64073

File tree

1 file changed

+46
-39
lines changed

1 file changed

+46
-39
lines changed

SwiftKit/src/main/java/org/swift/swiftkit/SwiftKit.java

Lines changed: 46 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -80,29 +80,32 @@ static MemorySegment findOrThrow(String symbol) {
8080

8181
// ==== ------------------------------------------------------------------------------------------------------------
8282
// free
83-
/**
84-
* Descriptor for the free C runtime function.
85-
*/
86-
public static final FunctionDescriptor free$descriptor = FunctionDescriptor.ofVoid(
87-
ValueLayout.ADDRESS
88-
);
8983

90-
/**
91-
* Address of the free C runtime function.
92-
*/
93-
public static final MemorySegment free$addr = findOrThrow("free");
84+
static abstract class free {
85+
/**
86+
* Descriptor for the free C runtime function.
87+
*/
88+
public static final FunctionDescriptor DESC = FunctionDescriptor.ofVoid(
89+
ValueLayout.ADDRESS
90+
);
9491

95-
/**
96-
* Handle for the free C runtime function.
97-
*/
98-
public static final MethodHandle free$handle = Linker.nativeLinker().downcallHandle(free$addr, free$descriptor);
92+
/**
93+
* Address of the free C runtime function.
94+
*/
95+
public static final MemorySegment ADDR = findOrThrow("free");
96+
97+
/**
98+
* Handle for the free C runtime function.
99+
*/
100+
public static final MethodHandle HANDLE = Linker.nativeLinker().downcallHandle(ADDR, DESC);
101+
}
99102

100103
/**
101104
* free the given pointer
102105
*/
103106
public static void cFree(MemorySegment pointer) {
104107
try {
105-
free$handle.invokeExact(pointer);
108+
free.HANDLE.invokeExact(pointer);
106109
} catch (Throwable ex$) {
107110
throw new AssertionError("should not reach here", ex$);
108111
}
@@ -136,7 +139,7 @@ public static long retainCount(MemorySegment object) {
136139
}
137140

138141
public static long retainCount(SwiftHeapObject object) {
139-
return retainCount(object.$self());
142+
return retainCount(object.$memorySegment());
140143
}
141144

142145
// ==== ------------------------------------------------------------------------------------------------------------
@@ -166,7 +169,7 @@ public static void retain(MemorySegment object) {
166169
}
167170

168171
public static long retain(SwiftHeapObject object) {
169-
return retainCount(object.$self());
172+
return retainCount(object.$memorySegment());
170173
}
171174

172175
// ==== ------------------------------------------------------------------------------------------------------------
@@ -196,10 +199,11 @@ public static void release(MemorySegment object) {
196199
}
197200

198201
public static long release(SwiftHeapObject object) {
199-
return retainCount(object.$self());
202+
return retainCount(object.$memorySegment());
200203
}
201204

202205
// ==== ------------------------------------------------------------------------------------------------------------
206+
// getTypeByName
203207

204208
/**
205209
* {@snippet lang = swift:
@@ -395,38 +399,41 @@ public static long alignmentOfSwiftType(MemorySegment typeMetadata) {
395399
return (flags & 0xFF) + 1;
396400
}
397401

398-
/**
399-
* Descriptor for the swift_getTypeName runtime function.
400-
*/
401-
public static final FunctionDescriptor swift_getTypeName$descriptor = FunctionDescriptor.of(
402-
/*returns=*/MemoryLayout.structLayout(
403-
SWIFT_POINTER.withName("utf8Chars"),
404-
SWIFT_INT.withName("length")
405-
),
406-
ValueLayout.ADDRESS,
407-
ValueLayout.JAVA_BOOLEAN
408-
);
402+
private static class swift_getTypeName {
409403

410-
/**
411-
* Address of the swift_getTypeName runtime function.
412-
*/
413-
public static final MemorySegment swift_getTypeName$addr = findOrThrow("swift_getTypeName");
404+
/**
405+
* Descriptor for the swift_getTypeName runtime function.
406+
*/
407+
public static final FunctionDescriptor DESC = FunctionDescriptor.of(
408+
/*returns=*/MemoryLayout.structLayout(
409+
SWIFT_POINTER.withName("utf8Chars"),
410+
SWIFT_INT.withName("length")
411+
),
412+
ValueLayout.ADDRESS,
413+
ValueLayout.JAVA_BOOLEAN
414+
);
414415

415-
/**
416-
* Handle for the swift_getTypeName runtime function.
417-
*/
418-
public static final MethodHandle swift_getTypeName$handle = Linker.nativeLinker().downcallHandle(swift_getTypeName$addr, swift_getTypeName$descriptor);
416+
/**
417+
* Address of the swift_getTypeName runtime function.
418+
*/
419+
public static final MemorySegment ADDR = findOrThrow("swift_getTypeName");
420+
421+
/**
422+
* Handle for the swift_getTypeName runtime function.
423+
*/
424+
public static final MethodHandle HANDLE = Linker.nativeLinker().downcallHandle(ADDR, DESC);
425+
}
419426

420427
/**
421428
* Produce the name of the Swift type given its Swift type metadata.
422429
* <p>
423-
* If 'qualified' is true, leave all of the qualification in place to
430+
* If 'qualified' is true, leave all the qualification in place to
424431
* disambiguate the type, producing a more complete (but longer) type name.
425432
*/
426433
public static String nameOfSwiftType(MemorySegment typeMetadata, boolean qualified) {
427434
try {
428435
try (Arena arena = Arena.ofConfined()) {
429-
MemorySegment charsAndLength = (MemorySegment) swift_getTypeName$handle.invokeExact((SegmentAllocator) arena, typeMetadata, qualified);
436+
MemorySegment charsAndLength = (MemorySegment) swift_getTypeName.HANDLE.invokeExact((SegmentAllocator) arena, typeMetadata, qualified);
430437
MemorySegment utf8Chars = charsAndLength.get(SWIFT_POINTER, 0);
431438
String typeName = utf8Chars.getString(0);
432439
cFree(utf8Chars);

0 commit comments

Comments
 (0)