15
15
package org .swift .swiftkit ;
16
16
17
17
import java .lang .foreign .*;
18
- import java .lang .foreign .MemoryLayout .PathElement ;
19
18
import java .lang .invoke .MethodHandle ;
20
19
import java .util .Arrays ;
21
20
import java .util .stream .Collectors ;
22
21
23
22
import static java .lang .foreign .ValueLayout .JAVA_BYTE ;
23
+ import static org .swift .swiftkit .SwiftValueWitnessTable .fullTypeMetadataLayout ;
24
24
25
25
public class SwiftKit {
26
26
@@ -34,9 +34,6 @@ public class SwiftKit {
34
34
System .loadLibrary (STDLIB_DYLIB_NAME );
35
35
}
36
36
37
- public static final AddressLayout SWIFT_POINTER = ValueLayout .ADDRESS
38
- .withTargetLayout (MemoryLayout .sequenceLayout (java .lang .Long .MAX_VALUE , JAVA_BYTE ));
39
-
40
37
static final SymbolLookup SYMBOL_LOOKUP = getSymbolLookup ();
41
38
42
39
private static SymbolLookup getSymbolLookup () {
@@ -251,7 +248,7 @@ public static MemorySegment getTypeByName(String string) {
251
248
*/
252
249
private static class swift_getTypeByMangledNameInEnvironment {
253
250
public static final FunctionDescriptor DESC = FunctionDescriptor .of (
254
- /*returns=*/ SWIFT_POINTER ,
251
+ /*returns=*/ SwiftValueLayout . SWIFT_POINTER ,
255
252
ValueLayout .ADDRESS ,
256
253
ValueLayout .JAVA_INT ,
257
254
ValueLayout .ADDRESS ,
@@ -282,88 +279,26 @@ public static MemorySegment getTypeByMangledNameInEnvironment(String string) {
282
279
}
283
280
}
284
281
285
- /**
286
- * The value layout for Swift's Int type, which is a signed type that follows
287
- * the size of a pointer (aka C's ptrdiff_t).
288
- */
289
- public static ValueLayout SWIFT_INT = (ValueLayout .ADDRESS .byteSize () == 4 ) ?
290
- ValueLayout .JAVA_INT : ValueLayout .JAVA_LONG ;
291
-
292
- /**
293
- * The value layout for Swift's UInt type, which is an unsigned type that follows
294
- * the size of a pointer (aka C's size_t). Java does not have unsigned integer
295
- * types in general, so we use the layout for Swift's Int.
296
- */
297
- public static ValueLayout SWIFT_UINT = SWIFT_INT ;
298
-
299
282
/**
300
283
* Read a Swift.Int value from memory at the given offset and translate it into a Java long.
301
284
* <p>
302
285
* This function copes with the fact that a Swift.Int might be 32 or 64 bits.
303
286
*/
304
287
public static final long getSwiftInt (MemorySegment memorySegment , long offset ) {
305
- if (SWIFT_INT == ValueLayout .JAVA_LONG ) {
288
+ if (SwiftValueLayout . SWIFT_INT == ValueLayout .JAVA_LONG ) {
306
289
return memorySegment .get (ValueLayout .JAVA_LONG , offset );
307
290
} else {
308
291
return memorySegment .get (ValueLayout .JAVA_INT , offset );
309
292
}
310
293
}
311
294
312
- /**
313
- * Value witness table layout.
314
- */
315
- public static final MemoryLayout valueWitnessTableLayout = MemoryLayout .structLayout (
316
- ValueLayout .ADDRESS .withName ("initializeBufferWithCopyOfBuffer" ),
317
- ValueLayout .ADDRESS .withName ("destroy" ),
318
- ValueLayout .ADDRESS .withName ("initializeWithCopy" ),
319
- ValueLayout .ADDRESS .withName ("assignWithCopy" ),
320
- ValueLayout .ADDRESS .withName ("initializeWithTake" ),
321
- ValueLayout .ADDRESS .withName ("assignWithTake" ),
322
- ValueLayout .ADDRESS .withName ("getEnumTagSinglePayload" ),
323
- ValueLayout .ADDRESS .withName ("storeEnumTagSinglePayload" ),
324
- SwiftKit .SWIFT_INT .withName ("size" ),
325
- SwiftKit .SWIFT_INT .withName ("stride" ),
326
- SwiftKit .SWIFT_UINT .withName ("flags" ),
327
- SwiftKit .SWIFT_UINT .withName ("extraInhabitantCount" )
328
- ).withName ("SwiftValueWitnessTable" );
329
-
330
- /**
331
- * Offset for the "size" field within the value witness table.
332
- */
333
- static final long valueWitnessTable$size$offset =
334
- valueWitnessTableLayout .byteOffset (PathElement .groupElement ("size" ));
335
-
336
- /**
337
- * Offset for the "stride" field within the value witness table.
338
- */
339
- static final long valueWitnessTable$stride$offset =
340
- valueWitnessTableLayout .byteOffset (PathElement .groupElement ("stride" ));
341
-
342
- /**
343
- * Offset for the "flags" field within the value witness table.
344
- */
345
- static final long valueWitnessTable$flags$offset =
346
- valueWitnessTableLayout .byteOffset (PathElement .groupElement ("flags" ));
347
-
348
- /**
349
- * Type metadata pointer.
350
- */
351
- public static final StructLayout fullTypeMetadataLayout = MemoryLayout .structLayout (
352
- SWIFT_POINTER .withName ("vwt" )
353
- ).withName ("SwiftFullTypeMetadata" );
354
-
355
- /**
356
- * Offset for the "vwt" field within the full type metadata.
357
- */
358
- static final long fullTypeMetadata$vwt$offset =
359
- fullTypeMetadataLayout .byteOffset (PathElement .groupElement ("vwt" ));
360
295
361
296
/**
362
297
* Given the address of Swift type metadata for a type, return the addres
363
298
* of the "full" type metadata that can be accessed via fullTypeMetadataLayout.
364
299
*/
365
300
public static MemorySegment fullTypeMetadata (MemorySegment typeMetadata ) {
366
- return MemorySegment .ofAddress (typeMetadata .address () - SWIFT_POINTER .byteSize ())
301
+ return MemorySegment .ofAddress (typeMetadata .address () - SwiftValueLayout . SWIFT_POINTER .byteSize ())
367
302
.reinterpret (fullTypeMetadataLayout .byteSize ());
368
303
}
369
304
@@ -372,14 +307,15 @@ public static MemorySegment fullTypeMetadata(MemorySegment typeMetadata) {
372
307
* references the value witness table for the type.
373
308
*/
374
309
public static MemorySegment valueWitnessTable (MemorySegment typeMetadata ) {
375
- return fullTypeMetadata (typeMetadata ).get (SWIFT_POINTER , fullTypeMetadata$vwt$offset );
310
+ return fullTypeMetadata (typeMetadata )
311
+ .get (SwiftValueLayout .SWIFT_POINTER , SwiftValueWitnessTable .fullTypeMetadata$vwt$offset );
376
312
}
377
313
378
314
/**
379
315
* Determine the size of a Swift type given its type metadata.
380
316
*/
381
317
public static long sizeOfSwiftType (MemorySegment typeMetadata ) {
382
- return getSwiftInt (valueWitnessTable (typeMetadata ), valueWitnessTable $size$offset );
318
+ return getSwiftInt (valueWitnessTable (typeMetadata ), SwiftValueWitnessTable . $size$offset );
383
319
}
384
320
385
321
/**
@@ -388,14 +324,14 @@ public static long sizeOfSwiftType(MemorySegment typeMetadata) {
388
324
* array. It is >= the size.
389
325
*/
390
326
public static long strideOfSwiftType (MemorySegment typeMetadata ) {
391
- return getSwiftInt (valueWitnessTable (typeMetadata ), valueWitnessTable $stride$offset );
327
+ return getSwiftInt (valueWitnessTable (typeMetadata ), SwiftValueWitnessTable . $stride$offset );
392
328
}
393
329
394
330
/**
395
331
* Determine the alignment of the given Swift type.
396
332
*/
397
333
public static long alignmentOfSwiftType (MemorySegment typeMetadata ) {
398
- long flags = getSwiftInt (valueWitnessTable (typeMetadata ), valueWitnessTable $flags$offset );
334
+ long flags = getSwiftInt (valueWitnessTable (typeMetadata ), SwiftValueWitnessTable . $flags$offset );
399
335
return (flags & 0xFF ) + 1 ;
400
336
}
401
337
@@ -406,8 +342,8 @@ private static class swift_getTypeName {
406
342
*/
407
343
public static final FunctionDescriptor DESC = FunctionDescriptor .of (
408
344
/*returns=*/ MemoryLayout .structLayout (
409
- SWIFT_POINTER .withName ("utf8Chars" ),
410
- SWIFT_INT .withName ("length" )
345
+ SwiftValueLayout . SWIFT_POINTER .withName ("utf8Chars" ),
346
+ SwiftValueLayout . SWIFT_INT .withName ("length" )
411
347
),
412
348
ValueLayout .ADDRESS ,
413
349
ValueLayout .JAVA_BOOLEAN
@@ -434,7 +370,7 @@ public static String nameOfSwiftType(MemorySegment typeMetadata, boolean qualifi
434
370
try {
435
371
try (Arena arena = Arena .ofConfined ()) {
436
372
MemorySegment charsAndLength = (MemorySegment ) swift_getTypeName .HANDLE .invokeExact ((SegmentAllocator ) arena , typeMetadata , qualified );
437
- MemorySegment utf8Chars = charsAndLength .get (SWIFT_POINTER , 0 );
373
+ MemorySegment utf8Chars = charsAndLength .get (SwiftValueLayout . SWIFT_POINTER , 0 );
438
374
String typeName = utf8Chars .getString (0 );
439
375
cFree (utf8Chars );
440
376
return typeName ;
0 commit comments