11#include " ClassBuilder.h"
2+ #if TARGET_CPU_X86_64 || TARGET_CPU_X86
3+ #include " SpinLock.h"
4+ #endif
25
36namespace tns {
47
58// Moved this method in a separate .cpp file because ARC destroys the class
69// created with objc_allocateClassPair when the control leaves this method scope
7-
10+ // TODO: revist this as there are x86 simulator issues, so maybe a lock is
11+ // needed regardless
812Class ClassBuilder::GetExtendedClass (std::string baseClassName,
913 std::string staticClassName) {
14+ #if TARGET_CPU_X86_64 || TARGET_CPU_X86
15+ // X86 simulators have this bugged, so we fallback to old behavior
16+ static SpinMutex m;
17+ SpinLock lock (m);
18+ Class baseClass = objc_getClass (baseClassName.c_str ());
19+ std::string name =
20+ !staticClassName.empty ()
21+ ? staticClassName
22+ : baseClassName + " _" +
23+ std::to_string (++ClassBuilder::classNameCounter_);
24+ Class clazz = objc_getClass (name.c_str ());
25+
26+ if (clazz != nil) {
27+ int i = 1 ;
28+ std::string initialName = name;
29+ while (clazz != nil) {
30+ name = initialName + std::to_string (i++);
31+ clazz = objc_getClass (name.c_str ());
32+ }
33+ }
34+
35+ clazz = objc_allocateClassPair (baseClass, name.c_str (), 0 );
36+
37+ objc_registerClassPair (clazz);
38+ return clazz;
39+ #else
1040 Class baseClass = objc_getClass (baseClassName.c_str ());
1141 std::string name =
1242 !staticClassName.empty ()
@@ -30,6 +60,7 @@ Class ClassBuilder::GetExtendedClass(std::string baseClassName,
3060
3161 objc_registerClassPair (clazz);
3262 return clazz;
63+ #endif
3364}
3465
3566} // namespace tns
0 commit comments