37
37
static inline SwiftReflectionInteropContextRef
38
38
swift_reflection_interop_createReflectionContext (
39
39
void *ReaderContext,
40
- void *LibraryHandle,
41
- void *LegacyLibraryHandle,
42
40
uint8_t PointerSize,
43
41
FreeBytesFunction FreeBytes,
44
42
ReadBytesFunction ReadBytes,
45
43
GetStringLengthFunction GetStringLength,
46
44
GetSymbolAddressFunction GetSymbolAddress);
47
45
46
+ // / Add a library handle to the interop context. Returns 1 if the
47
+ // / library was added successfully, 0 if a symbol couldn't be looked up
48
+ // / or the reported metadata version is too old.
49
+ static inline int
50
+ swift_reflection_interop_addLibrary (
51
+ SwiftReflectionInteropContextRef ContextRef, void *LibraryHandle);
52
+
48
53
static inline void
49
54
swift_reflection_interop_destroyReflectionContext (
50
55
SwiftReflectionInteropContextRef ContextRef);
@@ -283,6 +288,7 @@ struct SwiftReflectionInteropContextLegacyImageRangeList {
283
288
284
289
struct SwiftReflectionInteropContext {
285
290
void *ReaderContext;
291
+ uint8_t PointerSize;
286
292
FreeBytesFunction FreeBytes;
287
293
ReadBytesFunction ReadBytes;
288
294
uint64_t (*GetStringLength)(void *reader_context,
@@ -291,8 +297,7 @@ struct SwiftReflectionInteropContext {
291
297
const char *name,
292
298
uint64_t name_length);
293
299
294
- // Currently we support at most two libraries.
295
- struct SwiftReflectionInteropContextLibrary Libraries[2 ];
300
+ struct SwiftReflectionInteropContextLibrary *Libraries;
296
301
int LibraryCount;
297
302
298
303
struct SwiftReflectionInteropContextFreeList *FreeList;
@@ -381,12 +386,11 @@ swift_reflection_interop_libraryForObject(
381
386
return swift_reflection_interop_libraryForAddress (ContextRef, Metadata);
382
387
}
383
388
384
- static inline void
389
+ static inline int
385
390
swift_reflection_interop_loadFunctions (struct SwiftReflectionInteropContext *Context,
386
- void *Handle,
387
- int IsLegacy) {
391
+ void *Handle) {
388
392
if (Handle == NULL )
389
- return ;
393
+ return 0 ;
390
394
391
395
struct SwiftReflectionInteropContextLibrary *Library = &Context
392
396
->Libraries [Context->LibraryCount ];
@@ -397,14 +401,16 @@ swift_reflection_interop_loadFunctions(struct SwiftReflectionInteropContext *Con
397
401
#endif
398
402
#define LOAD_NAMED (field, symbol ) do { \
399
403
Functions->field = (decltype (Functions->field ))dlsym (Handle, symbol); \
400
- if (Functions->field == NULL ) return ; \
404
+ if (Functions->field == NULL ) return 0 ; \
401
405
} while (0 )
402
406
#define LOAD (name ) LOAD_NAMED(name, " swift_reflection_" #name)
403
407
404
408
LOAD (getSupportedMetadataVersion);
405
409
uint16_t version = Functions->getSupportedMetadataVersion ();
406
410
if (version < SWIFT_LEGACY_METADATA_MIN_VERSION)
407
- return ;
411
+ return 0 ;
412
+
413
+ int IsLegacy = dlsym (Handle, " swift_reflection_addImage" ) == NULL ;
408
414
409
415
if (IsLegacy) {
410
416
LOAD_NAMED (createReflectionContextLegacy, " swift_reflection_createReflectionContext" );
@@ -438,7 +444,7 @@ swift_reflection_interop_loadFunctions(struct SwiftReflectionInteropContext *Con
438
444
Library->IsLegacy = IsLegacy;
439
445
Context->LibraryCount ++;
440
446
441
- return ;
447
+ return 1 ;
442
448
443
449
#undef LOAD
444
450
#undef LOAD_NAMED
@@ -470,6 +476,7 @@ swift_reflection_interop_readBytesAdapter(void *reader_context,
470
476
static inline uint8_t
471
477
swift_reflection_interop_getSizeAdapter (void *reader_context) {
472
478
// Legacy library doesn't pay attention to these anyway.
479
+ (void )reader_context;
473
480
return sizeof (void *);
474
481
}
475
482
@@ -492,8 +499,6 @@ swift_reflection_interop_GetSymbolAddressAdapter(
492
499
static inline SwiftReflectionInteropContextRef
493
500
swift_reflection_interop_createReflectionContext (
494
501
void *ReaderContext,
495
- void *LibraryHandle,
496
- void *LegacyLibraryHandle,
497
502
uint8_t PointerSize,
498
503
FreeBytesFunction FreeBytes,
499
504
ReadBytesFunction ReadBytes,
@@ -503,15 +508,27 @@ swift_reflection_interop_createReflectionContext(
503
508
SwiftReflectionInteropContextRef ContextRef =
504
509
(SwiftReflectionInteropContextRef)calloc (sizeof (*ContextRef), 1 );
505
510
506
- swift_reflection_interop_loadFunctions (ContextRef, LibraryHandle, 0 );
507
- swift_reflection_interop_loadFunctions (ContextRef, LegacyLibraryHandle, 1 );
511
+ ContextRef->ReaderContext = ReaderContext;
512
+ ContextRef->PointerSize = PointerSize;
513
+ ContextRef->FreeBytes = FreeBytes;
514
+ ContextRef->ReadBytes = ReadBytes;
515
+ ContextRef->GetStringLength = GetStringLength;
516
+ ContextRef->GetSymbolAddress = GetSymbolAddress;
508
517
509
- if (ContextRef->LibraryCount == 0 ) {
510
- free (ContextRef);
511
- return NULL ;
512
- }
518
+ ContextRef->AddressToLibraryCache = CFDictionaryCreateMutable (NULL , 0 , NULL , NULL );
513
519
514
- FOREACH_LIBRARY {
520
+ return ContextRef;
521
+ }
522
+
523
+ static inline int
524
+ swift_reflection_interop_addLibrary (
525
+ SwiftReflectionInteropContextRef ContextRef, void *LibraryHandle) {
526
+ size_t NewSize = (ContextRef->LibraryCount + 1 ) * sizeof (*ContextRef->Libraries );
527
+ ContextRef->Libraries = realloc (ContextRef->Libraries , NewSize);
528
+ int Success = swift_reflection_interop_loadFunctions (ContextRef, LibraryHandle);
529
+ if (Success) {
530
+ struct SwiftReflectionInteropContextLibrary *Library =
531
+ &ContextRef->Libraries [ContextRef->LibraryCount - 1 ];
515
532
if (Library->IsLegacy ) {
516
533
Library->Context = Library->Functions .createReflectionContextLegacy (
517
534
ContextRef,
@@ -520,20 +537,12 @@ swift_reflection_interop_createReflectionContext(
520
537
swift_reflection_interop_GetStringLengthAdapter,
521
538
swift_reflection_interop_GetSymbolAddressAdapter);
522
539
} else {
523
- Library->Context = Library->Functions .createReflectionContext (ReaderContext,
524
- PointerSize, FreeBytes, ReadBytes, GetStringLength, GetSymbolAddress);
540
+ Library->Context = Library->Functions .createReflectionContext (
541
+ ContextRef->ReaderContext ,
542
+ ContextRef->PointerSize , ContextRef->FreeBytes , ContextRef->ReadBytes , ContextRef->GetStringLength , ContextRef->GetSymbolAddress );
525
543
}
526
544
}
527
-
528
- ContextRef->ReaderContext = ReaderContext;
529
- ContextRef->FreeBytes = FreeBytes;
530
- ContextRef->ReadBytes = ReadBytes;
531
- ContextRef->GetStringLength = GetStringLength;
532
- ContextRef->GetSymbolAddress = GetSymbolAddress;
533
-
534
- ContextRef->AddressToLibraryCache = CFDictionaryCreateMutable (NULL , 0 , NULL , NULL );
535
-
536
- return ContextRef;
545
+ return Success;
537
546
}
538
547
539
548
static inline void
@@ -542,6 +551,7 @@ swift_reflection_interop_destroyReflectionContext(
542
551
FOREACH_LIBRARY {
543
552
Library->Functions .destroyReflectionContext (Library->Context );
544
553
}
554
+ free (ContextRef->Libraries );
545
555
struct SwiftReflectionInteropContextLegacyImageRangeList *LegacyImageRangeList
546
556
= ContextRef->LegacyImageRangeList ;
547
557
while (LegacyImageRangeList != NULL ) {
0 commit comments