@@ -48,6 +48,7 @@ private static SymbolLookup getSymbolLookup() {
4848 .or (Linker .nativeLinker ().defaultLookup ());
4949 }
5050 }
51+
5152 public SwiftKit () {
5253 }
5354
@@ -313,6 +314,9 @@ public static MemorySegment valueWitnessTable(MemorySegment typeMetadata) {
313314
314315 /**
315316 * Determine the size of a Swift type given its type metadata.
317+ *
318+ * @param typeMetadata the memory segment must point to a Swift metadata,
319+ * e.g. the result of a {@link SwiftKit.swift_getTypeByMangledNameInEnvironment} call
316320 */
317321 public static long sizeOfSwiftType (MemorySegment typeMetadata ) {
318322 return getSwiftInt (valueWitnessTable (typeMetadata ), SwiftValueWitnessTable .$size$offset );
@@ -321,14 +325,22 @@ public static long sizeOfSwiftType(MemorySegment typeMetadata) {
321325 /**
322326 * Determine the stride of a Swift type given its type metadata, which is
323327 * how many bytes are between successive elements of this type within an
324- * array. It is >= the size.
328+ * array.
329+ *
330+ * It is >= the size.
331+ *
332+ * @param typeMetadata the memory segment must point to a Swift metadata,
333+ * e.g. the result of a {@link SwiftKit.swift_getTypeByMangledNameInEnvironment} call
325334 */
326335 public static long strideOfSwiftType (MemorySegment typeMetadata ) {
327336 return getSwiftInt (valueWitnessTable (typeMetadata ), SwiftValueWitnessTable .$stride$offset );
328337 }
329338
330339 /**
331340 * Determine the alignment of the given Swift type.
341+ *
342+ * @param typeMetadata the memory segment must point to a Swift metadata,
343+ * e.g. the result of a {@link SwiftKit.swift_getTypeByMangledNameInEnvironment} call
332344 */
333345 public static long alignmentOfSwiftType (MemorySegment typeMetadata ) {
334346 long flags = getSwiftInt (valueWitnessTable (typeMetadata ), SwiftValueWitnessTable .$flags$offset );
@@ -365,6 +377,9 @@ private static class swift_getTypeName {
365377 * <p>
366378 * If 'qualified' is true, leave all the qualification in place to
367379 * disambiguate the type, producing a more complete (but longer) type name.
380+ *
381+ * @param typeMetadata the memory segment must point to a Swift metadata,
382+ * e.g. the result of a {@link SwiftKit.swift_getTypeByMangledNameInEnvironment} call
368383 */
369384 public static String nameOfSwiftType (MemorySegment typeMetadata , boolean qualified ) {
370385 try {
@@ -388,14 +403,29 @@ public static String nameOfSwiftType(MemorySegment typeMetadata, boolean qualifi
388403 * <p>
389404 * In the future, this layout could be extended to provide more detail,
390405 * such as the fields of a Swift struct.
406+ *
407+ * @param typeMetadata the memory segment must point to a Swift metadata,
408+ * e.g. the result of a {@link SwiftKit.swift_getTypeByMangledNameInEnvironment} call
391409 */
392410 public static MemoryLayout layoutOfSwiftType (MemorySegment typeMetadata ) {
393411 long size = sizeOfSwiftType (typeMetadata );
394412 long stride = strideOfSwiftType (typeMetadata );
413+ long padding = stride - size ;
414+
415+ // constructing a zero-length paddingLayout is illegal, so we avoid doing so
416+ MemoryLayout [] layouts = padding == 0 ?
417+ new MemoryLayout []{
418+ MemoryLayout .sequenceLayout (size , JAVA_BYTE )
419+ .withByteAlignment (alignmentOfSwiftType (typeMetadata ))
420+ } :
421+ new MemoryLayout []{
422+ MemoryLayout .sequenceLayout (size , JAVA_BYTE )
423+ .withByteAlignment (alignmentOfSwiftType (typeMetadata )),
424+ MemoryLayout .paddingLayout (stride - size )
425+ };
426+
395427 return MemoryLayout .structLayout (
396- MemoryLayout .sequenceLayout (size , JAVA_BYTE )
397- .withByteAlignment (alignmentOfSwiftType (typeMetadata )),
398- MemoryLayout .paddingLayout (stride - size )
428+ layouts
399429 ).withName (nameOfSwiftType (typeMetadata , true ));
400430 }
401431}
0 commit comments