]> git.saurik.com Git - apple/objc4.git/blob - test/swiftMetadataInitializer.m
bfa54a8f33849deee733fcde9456ee1e7e95a341
[apple/objc4.git] / test / swiftMetadataInitializer.m
1 // TEST_CONFIG MEM=mrc
2
3 #include "test.h"
4 #include "swift-class-def.m"
5
6
7 // _objc_swiftMetadataInitializer hooks for the classes in swift-class-def.m
8
9 Class initSuper(Class cls __unused, void *arg __unused)
10 {
11 // This test provokes objc's callback out of superclass order.
12 // SwiftSub's init is first. SwiftSuper's init is never called.
13
14 fail("SwiftSuper's init should not have been called");
15 }
16
17 bool isRealized(Class cls)
18 {
19 // check the is-realized bits directly
20
21 #if __LP64__
22 # define mask (~(uintptr_t)7)
23 #else
24 # define mask (~(uintptr_t)3)
25 #endif
26 #define RW_REALIZED (1<<31)
27
28 uintptr_t rw = ((uintptr_t *)cls)[4] & mask; // class_t->data
29 return ((uint32_t *)rw)[0] & RW_REALIZED; // class_rw_t->flags
30 }
31
32 static int SubInits = 0;
33 Class initSub(Class cls, void *arg)
34 {
35 testprintf("initSub callback\n");
36
37 extern uintptr_t OBJC_CLASS_$_SwiftSuper;
38 extern uintptr_t OBJC_CLASS_$_SwiftSub;
39 Class RawSwiftSuper = (Class)&OBJC_CLASS_$_SwiftSuper;
40 Class RawSwiftSub = (Class)&OBJC_CLASS_$_SwiftSub;
41
42 testassert(SubInits == 0);
43 SubInits++;
44 testassert(arg == nil);
45 testassert(0 == strcmp(class_getName(cls), "SwiftSub"));
46 testassert(cls == RawSwiftSub);
47 testassert(!isRealized(RawSwiftSuper));
48 testassert(!isRealized(RawSwiftSub));
49
50 testprintf("initSub beginning _objc_realizeClassFromSwift\n");
51 _objc_realizeClassFromSwift(cls, cls);
52 testprintf("initSub finished _objc_realizeClassFromSwift\n");
53
54 testassert(isRealized(RawSwiftSuper));
55 testassert(isRealized(RawSwiftSub));
56
57 return cls;
58 }
59
60
61 int main()
62 {
63 testassert(SubInits == 0);
64 testprintf("calling [SwiftSub class]\n");
65 [SwiftSub class];
66 testprintf("finished [SwiftSub class]\n");
67 testassert(SubInits == 1);
68 [SwiftSuper class];
69 succeed(__FILE__);
70 }