@@ -48,6 +48,7 @@ private static SymbolLookup getSymbolLookup() {
48
48
.or (Linker .nativeLinker ().defaultLookup ());
49
49
}
50
50
}
51
+
51
52
public SwiftKit () {
52
53
}
53
54
@@ -313,6 +314,9 @@ public static MemorySegment valueWitnessTable(MemorySegment typeMetadata) {
313
314
314
315
/**
315
316
* 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
316
320
*/
317
321
public static long sizeOfSwiftType (MemorySegment typeMetadata ) {
318
322
return getSwiftInt (valueWitnessTable (typeMetadata ), SwiftValueWitnessTable .$size$offset );
@@ -321,14 +325,22 @@ public static long sizeOfSwiftType(MemorySegment typeMetadata) {
321
325
/**
322
326
* Determine the stride of a Swift type given its type metadata, which is
323
327
* 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
325
334
*/
326
335
public static long strideOfSwiftType (MemorySegment typeMetadata ) {
327
336
return getSwiftInt (valueWitnessTable (typeMetadata ), SwiftValueWitnessTable .$stride$offset );
328
337
}
329
338
330
339
/**
331
340
* 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
332
344
*/
333
345
public static long alignmentOfSwiftType (MemorySegment typeMetadata ) {
334
346
long flags = getSwiftInt (valueWitnessTable (typeMetadata ), SwiftValueWitnessTable .$flags$offset );
@@ -365,6 +377,9 @@ private static class swift_getTypeName {
365
377
* <p>
366
378
* If 'qualified' is true, leave all the qualification in place to
367
379
* 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
368
383
*/
369
384
public static String nameOfSwiftType (MemorySegment typeMetadata , boolean qualified ) {
370
385
try {
@@ -388,14 +403,29 @@ public static String nameOfSwiftType(MemorySegment typeMetadata, boolean qualifi
388
403
* <p>
389
404
* In the future, this layout could be extended to provide more detail,
390
405
* 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
391
409
*/
392
410
public static MemoryLayout layoutOfSwiftType (MemorySegment typeMetadata ) {
393
411
long size = sizeOfSwiftType (typeMetadata );
394
412
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
+
395
427
return MemoryLayout .structLayout (
396
- MemoryLayout .sequenceLayout (size , JAVA_BYTE )
397
- .withByteAlignment (alignmentOfSwiftType (typeMetadata )),
398
- MemoryLayout .paddingLayout (stride - size )
428
+ layouts
399
429
).withName (nameOfSwiftType (typeMetadata , true ));
400
430
}
401
431
}
0 commit comments