1 // TEST_CFLAGS -framework Foundation
4 #include <objc/runtime.h>
5 #import <Foundation/Foundation.h>
7 #include <Block_private.h>
9 #if !__has_feature(objc_arc)
14 // stret supported, but is identical to non-stret
15 # define STRET_SPECIAL 0
17 // stret supported and distinct from non-stret
18 # define STRET_SPECIAL 1
21 typedef struct BigStruct {
22 uintptr_t datums[200];
25 @interface Foo:NSObject
28 - (BigStruct) methodThatReturnsBigStruct: (BigStruct) b
36 - (BigStruct) structThatIsBig: (BigStruct) b;
37 - (BigStruct) methodThatReturnsBigStruct: (BigStruct) b;
38 - (float) methodThatReturnsFloat: (float) aFloat;
41 // This is void* instead of id to prevent interference from ARC.
42 typedef uintptr_t (*FuncPtr)(void *, SEL);
43 typedef BigStruct (*BigStructFuncPtr)(id, SEL, BigStruct);
44 typedef float (*FloatFuncPtr)(id, SEL, float);
46 BigStruct bigfunc(BigStruct a) {
50 @interface Deallocator : NSObject @end
51 @implementation Deallocator
52 -(void) methodThatNobodyElseCalls1 { }
53 -(void) methodThatNobodyElseCalls2 { }
54 id retain_imp(Deallocator *self, SEL _cmd) {
55 _objc_flush_caches([Deallocator class]);
56 [self methodThatNobodyElseCalls1];
57 struct objc_super sup = { self, [[Deallocator class] superclass] };
58 return ((id(*)(struct objc_super *, SEL))objc_msgSendSuper)(&sup, _cmd);
60 void dealloc_imp(Deallocator *self, SEL _cmd) {
61 _objc_flush_caches([Deallocator class]);
62 [self methodThatNobodyElseCalls2];
63 struct objc_super sup = { self, [[Deallocator class] superclass] };
64 ((void(*)(struct objc_super *, SEL))objc_msgSendSuper)(&sup, _cmd);
67 class_addMethod(self, sel_registerName("retain"), (IMP)retain_imp, "");
68 class_addMethod(self, sel_registerName("dealloc"), (IMP)dealloc_imp, "");
72 /* Code copied from objc-block-trampolines.m to test Block innards */
74 ReturnValueInRegisterArgumentMode,
76 ReturnValueOnStackArgumentMode,
82 static ArgumentMode _argumentModeForBlock(id block) {
83 ArgumentMode aMode = ReturnValueInRegisterArgumentMode;
85 if ( _Block_use_stret((__bridge void *)block) )
86 aMode = ReturnValueOnStackArgumentMode;
88 testassert(!_Block_use_stret((__bridge void *)block));
96 // make sure the bits are in place
97 int (^registerReturn)() = ^(){ return 42; };
100 aMode = _argumentModeForBlock(registerReturn);
101 testassert(aMode == ReturnValueInRegisterArgumentMode);
103 BigStruct (^stackReturn)() = ^() { BigStruct k = {{0}}; return k; };
104 aMode = _argumentModeForBlock(stackReturn);
106 testassert(aMode == ReturnValueOnStackArgumentMode);
108 testassert(aMode == ReturnValueInRegisterArgumentMode);
111 uintptr_t TEST_QUANTITY = is_guardmalloc() ? 1000 : 100000;
112 FuncPtr funcArray[TEST_QUANTITY];
114 for(uintptr_t i = 0; i < TEST_QUANTITY; i++) {
115 uintptr_t (^block)(void *self) = ^uintptr_t(void *self) {
116 testassert(i == (uintptr_t)self);
119 block = (__bridge id)_Block_copy((__bridge void *)block);
121 funcArray[i] = (FuncPtr) imp_implementationWithBlock(block);
123 testassert(block((void *)i) == i);
125 id blockFromIMPResult = imp_getBlock((IMP)funcArray[i]);
126 testassert(blockFromIMPResult == (id)block);
128 _Block_release((__bridge void *)block);
131 for(uintptr_t i = 0; i < TEST_QUANTITY; i++) {
132 uintptr_t result = funcArray[i]((void *)i, 0);
133 testassert(i == result);
136 for(uintptr_t i = 0; i < TEST_QUANTITY; i = i + 3) {
137 imp_removeBlock((IMP)funcArray[i]);
138 id shouldBeNull = imp_getBlock((IMP)funcArray[i]);
139 testassert(shouldBeNull == NULL);
142 for(uintptr_t i = 0; i < TEST_QUANTITY; i = i + 3) {
145 uintptr_t (^block)(void *) = ^uintptr_t(void *self) {
146 testassert(j == (uintptr_t)self);
149 funcArray[i] = (FuncPtr) imp_implementationWithBlock(block);
151 testassert(block((void *)j) == j);
152 testassert(funcArray[i]((void *)j, 0) == j);
155 for(uintptr_t i = 0; i < TEST_QUANTITY; i = i + 3) {
157 uintptr_t result = funcArray[i]((void *)j, 0);
158 testassert(j == result);
161 int (^implBlock)(id, int);
163 implBlock = ^(id self __attribute__((unused)), int a){
169 IMP methodImp = imp_implementationWithBlock(implBlock);
171 BOOL success = class_addMethod([Foo class], @selector(boo:), methodImp, "i@:i");
175 int (*impF)(id self, SEL _cmd, int x) = (int(*)(id, SEL, int)) [Foo instanceMethodForSelector: @selector(boo:)];
177 int x = impF(f, @selector(boo:), -42);
180 testassert([f boo: -42] == 42);
183 for (uintptr_t i = 0; i < 200; i++) {
187 // slightly more straightforward here
188 __block unsigned int state = 0;
189 BigStruct (^structBlock)(id, BigStruct) = ^BigStruct(id self __attribute__((unused)), BigStruct c) {
193 BigStruct blockDirect = structBlock(nil, a);
194 testassert(!memcmp(&a, &blockDirect, sizeof(BigStruct)));
195 testassert(state==1);
197 IMP bigStructIMP = imp_implementationWithBlock(structBlock);
199 class_addMethod([Foo class], @selector(structThatIsBig:), bigStructIMP, "oh, type strings, how I hate thee. Fortunately, the runtime doesn't generally care.");
203 BigStructFuncPtr bFunc;
206 testassert(!memcmp(&a, &b, sizeof(BigStruct)));
208 testassert(!memcmp(&a, &b, sizeof(BigStruct)));
210 bFunc = (BigStructFuncPtr) [Foo instanceMethodForSelector: @selector(methodThatReturnsBigStruct:)];
212 b = bFunc(f, @selector(methodThatReturnsBigStruct:), a);
213 testassert(!memcmp(&a, &b, sizeof(BigStruct)));
215 b = [f methodThatReturnsBigStruct: a];
216 testassert(!memcmp(&a, &b, sizeof(BigStruct)));
218 bFunc = (BigStructFuncPtr) [Foo instanceMethodForSelector: @selector(structThatIsBig:)];
220 b = bFunc(f, @selector(structThatIsBig:), a);
221 testassert(!memcmp(&a, &b, sizeof(BigStruct)));
222 testassert(state==2);
224 b = [f structThatIsBig: a];
225 testassert(!memcmp(&a, &b, sizeof(BigStruct)));
226 testassert(state==3);
229 IMP floatIMP = imp_implementationWithBlock(^float (id self __attribute__((unused)), float aFloat ) {
232 class_addMethod([Foo class], @selector(methodThatReturnsFloat:), floatIMP, "ooh.. type string unspecified again... oh noe... runtime might punish. not.");
234 float e = (float)0.001;
235 float retF = (float)[f methodThatReturnsFloat: 37.1212f];
236 testassert( ((retF - e) < 37.1212) && ((retF + e) > 37.1212) );
239 #if !__has_feature(objc_arc)
240 // Make sure imp_implementationWithBlock() and imp_removeBlock()
241 // don't deadlock while calling Block_copy() and Block_release()
242 Deallocator *dead = [[Deallocator alloc] init];
243 IMP deadlockImp = imp_implementationWithBlock(^{ [dead self]; });
245 imp_removeBlock(deadlockImp);