52
52
53
53
// Add methods to class SwiftObject that can be called by performSelector: et al
54
54
55
- #if SWIFT_DARWIN_ENABLE_STABLE_ABI_BIT
56
- // mangled Swift._SwiftObject
57
- #define SwiftObject _TtCs12_SwiftObject
58
- #define SwiftObjectDemangledName " Swift._SwiftObject"
59
- #else
60
- // Pre-stable ABI uses un-mangled name for SwiftObject.
61
- #define SwiftObjectDemangledName " SwiftObject"
62
- #endif
63
-
64
- @interface SwiftObject /* trust me, I know what I'm doing */ @end
65
- @implementation SwiftObject (MethodsToPerform)
66
- -(id ) perform0 {
55
+ static const char *SwiftObjectDemangledName;
56
+
57
+ static id Perform0 (id self, SEL sel) {
67
58
return self;
68
59
}
69
60
70
- -( id ) perform1 : (id ) one {
61
+ static id Perform1 (id self, SEL sel, id one) {
71
62
expectTrue ([one isEqual: @1 ]);
72
63
return self;
73
64
}
74
65
75
- -( id ) perform2 : (id ) one : ( id ) two {
66
+ static id Perform2 (id self, SEL sel, id one, id two) {
76
67
expectTrue ([one isEqual: @1 ]);
77
68
expectTrue ([two isEqual: @2 ]);
78
69
return self;
79
70
}
80
- @end
71
+
72
+ static __attribute__ ((constructor))
73
+ void HackSwiftObject()
74
+ {
75
+ SwiftObjectDemangledName = " Swift._SwiftObject" ;
76
+ Class cls = objc_getClass (SwiftObjectDemangledName);
77
+ // FIXME: Remove this fallback after we enable
78
+ // SWIFT_DARWIN_ENABLE_STABLE_ABI_BIT everywhere.
79
+ if (!cls) {
80
+ SwiftObjectDemangledName = " SwiftObject" ;
81
+ cls = objc_getClass (SwiftObjectDemangledName);
82
+ }
83
+
84
+ class_addMethod (cls, @selector (perform0 ), (IMP )Perform0, " @@:" );
85
+ class_addMethod (cls, @selector (perform1: ), (IMP )Perform1, " @@:@" );
86
+ class_addMethod (cls, @selector (perform2:: ), (IMP )Perform2, " @@:@@" );
87
+ }
81
88
82
89
void TestSwiftObjectNSObject (id c, id d)
83
90
{
@@ -411,10 +418,10 @@ void TestSwiftObjectNSObject(id c, id d)
411
418
expectTrue ([[c description ] isEqual: @" SwiftObjectNSObject.C" ]);
412
419
expectTrue ([[D description ] isEqual: @" SwiftObjectNSObject.D" ]);
413
420
expectTrue ([[C description ] isEqual: @" SwiftObjectNSObject.C" ]);
414
- expectTrue ([[S description ] isEqual: @ SwiftObjectDemangledName]);
421
+ expectTrue ([[S description ] isEqual: @( SwiftObjectDemangledName) ]);
415
422
expectTrue ([[D_meta description ] isEqual: @" SwiftObjectNSObject.D" ]);
416
423
expectTrue ([[C_meta description ] isEqual: @" SwiftObjectNSObject.C" ]);
417
- expectTrue ([[S_meta description ] isEqual: @ SwiftObjectDemangledName]);
424
+ expectTrue ([[S_meta description ] isEqual: @( SwiftObjectDemangledName) ]);
418
425
419
426
// NSLog() calls -description and also some private methods.
420
427
// This output is checked by FileCheck in SwiftObjectNSObject.swift.
@@ -429,10 +436,10 @@ void TestSwiftObjectNSObject(id c, id d)
429
436
expectTrue ([[c debugDescription ] isEqual: @" SwiftObjectNSObject.C" ]);
430
437
expectTrue ([[D debugDescription ] isEqual: @" SwiftObjectNSObject.D" ]);
431
438
expectTrue ([[C debugDescription ] isEqual: @" SwiftObjectNSObject.C" ]);
432
- expectTrue ([[S debugDescription ] isEqual: @ SwiftObjectDemangledName]);
439
+ expectTrue ([[S debugDescription ] isEqual: @( SwiftObjectDemangledName) ]);
433
440
expectTrue ([[D_meta debugDescription ] isEqual: @" SwiftObjectNSObject.D" ]);
434
441
expectTrue ([[C_meta debugDescription ] isEqual: @" SwiftObjectNSObject.C" ]);
435
- expectTrue ([[S_meta debugDescription ] isEqual: @ SwiftObjectDemangledName]);
442
+ expectTrue ([[S_meta debugDescription ] isEqual: @( SwiftObjectDemangledName) ]);
436
443
437
444
438
445
printf (" NSObjectProtocol.performSelector\n " );
0 commit comments