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