1
1
#include " ClassBuilder.h"
2
+ #if TARGET_CPU_X86_64 || TARGET_CPU_X86
3
+ #include " SpinLock.h"
4
+ #endif
2
5
3
6
namespace tns {
4
7
5
8
// Moved this method in a separate .cpp file because ARC destroys the class
6
9
// 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
8
12
Class ClassBuilder::GetExtendedClass (std::string baseClassName,
9
13
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
10
40
Class baseClass = objc_getClass (baseClassName.c_str ());
11
41
std::string name =
12
42
!staticClassName.empty ()
@@ -30,6 +60,7 @@ Class ClassBuilder::GetExtendedClass(std::string baseClassName,
30
60
31
61
objc_registerClassPair (clazz);
32
62
return clazz;
63
+ #endif
33
64
}
34
65
35
66
} // namespace tns
0 commit comments