@@ -2197,7 +2197,7 @@ static inline ClassROData *getROData(ClassMetadata *theClass) {
2197
2197
return (ClassROData*)(theClass->Data & ~uintptr_t (SWIFT_CLASS_IS_SWIFT_MASK));
2198
2198
}
2199
2199
2200
- static void initGenericClassObjCName (ClassMetadata *theClass) {
2200
+ static char * copyGenericClassObjCName (ClassMetadata *theClass) {
2201
2201
// Use the remangler to generate a mangled name from the type metadata.
2202
2202
Demangle::StackAllocatedDemangler<4096 > Dem;
2203
2203
@@ -2230,11 +2230,46 @@ static void initGenericClassObjCName(ClassMetadata *theClass) {
2230
2230
} else {
2231
2231
fullNameBuf[string.size ()] = ' \0 ' ;
2232
2232
}
2233
+ return fullNameBuf;
2234
+ }
2233
2235
2236
+ static void initGenericClassObjCName (ClassMetadata *theClass) {
2234
2237
auto theMetaclass = (ClassMetadata *)object_getClass ((id)theClass);
2235
2238
2236
- getROData (theClass)->Name = fullNameBuf;
2237
- getROData (theMetaclass)->Name = fullNameBuf;
2239
+ char *name = copyGenericClassObjCName (theClass);
2240
+ getROData (theClass)->Name = name;
2241
+ getROData (theMetaclass)->Name = name;
2242
+ }
2243
+
2244
+ static bool installLazyClassNameHandler () {
2245
+ auto _objc_setLazyClassNamer =
2246
+ (void (*)(char * (*)(Class)))
2247
+ dlsym (RTLD_NEXT, " _objc_setLazyClassNamer" );
2248
+ if (_objc_setLazyClassNamer == nullptr )
2249
+ return false ;
2250
+
2251
+ _objc_setLazyClassNamer ([](Class theClass) {
2252
+ ClassMetadata *metadata;
2253
+ if (class_isMetaClass (theClass)) {
2254
+ metadata = (ClassMetadata *)class_getIvarLayout (theClass);
2255
+ } else {
2256
+ metadata = (ClassMetadata *)theClass;
2257
+ }
2258
+ return copyGenericClassObjCName (metadata);
2259
+ });
2260
+ return true ;
2261
+ }
2262
+
2263
+ static void setUpGenericClassObjCName (ClassMetadata *theClass) {
2264
+ bool supportsLazyNames = SWIFT_LAZY_CONSTANT (installLazyClassNameHandler ());
2265
+ if (supportsLazyNames) {
2266
+ getROData (theClass)->Name = nullptr ;
2267
+ auto theMetaclass = (ClassMetadata *)object_getClass ((id)theClass);
2268
+ getROData (theMetaclass)->Name = nullptr ;
2269
+ getROData (theMetaclass)->IvarLayout = (const uint8_t *)theClass;
2270
+ } else {
2271
+ initGenericClassObjCName (theClass);
2272
+ }
2238
2273
}
2239
2274
#endif
2240
2275
@@ -2488,7 +2523,7 @@ initGenericObjCClass(ClassMetadata *self, size_t numFields,
2488
2523
const TypeLayout * const *fieldTypes,
2489
2524
size_t *fieldOffsets) {
2490
2525
// If the class is generic, we need to give it a name for Objective-C.
2491
- initGenericClassObjCName (self);
2526
+ setUpGenericClassObjCName (self);
2492
2527
2493
2528
ClassROData *rodata = getROData (self);
2494
2529
0 commit comments