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 // gcc and llvm-gcc will never support struct-return marking
20 typedef struct BigStruct {
21 uintptr_t datums[200];
24 @interface Foo:NSObject
27 - (BigStruct) methodThatReturnsBigStruct: (BigStruct) b
35 - (BigStruct) structThatIsBig: (BigStruct) b;
36 - (BigStruct) methodThatReturnsBigStruct: (BigStruct) b;
37 - (float) methodThatReturnsFloat: (float) aFloat;
40 // This is void* instead of id to prevent interference from ARC.
41 typedef uintptr_t (*FuncPtr)(void *, SEL);
42 typedef BigStruct (*BigStructFuncPtr)(id, SEL, BigStruct);
43 typedef float (*FloatFuncPtr)(id, SEL, float);
45 BigStruct bigfunc(BigStruct a) {
49 @interface Deallocator : NSObject @end
50 @implementation Deallocator
51 -(void) methodThatNobodyElseCalls1 { }
52 -(void) methodThatNobodyElseCalls2 { }
53 id retain_imp(Deallocator *self, SEL _cmd) {
54 _objc_flush_caches([Deallocator class]);
55 [self methodThatNobodyElseCalls1];
56 struct objc_super sup = { self, [[Deallocator class] superclass] };
57 return ((id(*)(struct objc_super *, SEL))objc_msgSendSuper)(&sup, _cmd);
59 void dealloc_imp(Deallocator *self, SEL _cmd) {
60 _objc_flush_caches([Deallocator class]);
61 [self methodThatNobodyElseCalls2];
62 struct objc_super sup = { self, [[Deallocator class] superclass] };
63 ((void(*)(struct objc_super *, SEL))objc_msgSendSuper)(&sup, _cmd);
66 class_addMethod(self, sel_registerName("retain"), (IMP)retain_imp, "");
67 class_addMethod(self, sel_registerName("dealloc"), (IMP)dealloc_imp, "");
71 /* Code copied from objc-block-trampolines.m to test Block innards */
73 ReturnValueInRegisterArgumentMode,
74 ReturnValueOnStackArgumentMode,
79 static ArgumentMode _argumentModeForBlock(id block) {
80 ArgumentMode aMode = ReturnValueInRegisterArgumentMode;
81 if ( _Block_use_stret((__bridge void *)block) )
82 aMode = ReturnValueOnStackArgumentMode;
89 // make sure the bits are in place
90 int (^registerReturn)() = ^(){ return 42; };
93 aMode = _argumentModeForBlock(registerReturn);
94 testassert(aMode == ReturnValueInRegisterArgumentMode);
97 BigStruct (^stackReturn)() = ^() { BigStruct k; return k; };
98 aMode = _argumentModeForBlock(stackReturn);
99 testassert(aMode == ReturnValueOnStackArgumentMode);
102 #define TEST_QUANTITY 100000
103 static FuncPtr funcArray[TEST_QUANTITY];
106 for(i = 0; i<TEST_QUANTITY; i++) {
107 uintptr_t (^block)(void *self) = ^uintptr_t(void *self) {
108 testassert(i == (uintptr_t)self);
111 block = (__bridge id)_Block_copy((__bridge void *)block);
113 funcArray[i] = (FuncPtr) imp_implementationWithBlock(block);
115 testassert(block((void *)i) == i);
117 id blockFromIMPResult = imp_getBlock((IMP)funcArray[i]);
118 testassert(blockFromIMPResult == (id)block);
120 _Block_release((__bridge void *)block);
123 for(i = 0; i<TEST_QUANTITY; i++) {
124 uintptr_t result = funcArray[i]((void *)i, 0);
125 testassert(i == result);
128 for(i = 0; i < TEST_QUANTITY; i= i + 3) {
129 imp_removeBlock((IMP)funcArray[i]);
130 id shouldBeNull = imp_getBlock((IMP)funcArray[i]);
131 testassert(shouldBeNull == NULL);
134 for(i = 0; i < TEST_QUANTITY; i= i + 3) {
137 uintptr_t (^block)(void *) = ^uintptr_t(void *self) {
138 testassert(j == (uintptr_t)self);
141 funcArray[i] = (FuncPtr) imp_implementationWithBlock(block);
143 testassert(block((void *)j) == j);
144 testassert(funcArray[i]((void *)j, 0) == j);
147 for(i = 0; i < TEST_QUANTITY; i= i + 3) {
149 uintptr_t result = funcArray[i]((void *)j, 0);
150 testassert(j == result);
153 int (^implBlock)(id, int);
155 implBlock = ^(id self __attribute__((unused)), int a){
161 IMP methodImp = imp_implementationWithBlock(implBlock);
163 BOOL success = class_addMethod([Foo class], @selector(boo:), methodImp, "i@:i");
167 int (*impF)(id self, SEL _cmd, int x) = (int(*)(id, SEL, int)) [Foo instanceMethodForSelector: @selector(boo:)];
169 int x = impF(f, @selector(boo:), -42);
172 testassert([f boo: -42] == 42);
179 // slightly more straightforward here
180 __block unsigned int state = 0;
181 BigStruct (^structBlock)(id, BigStruct) = ^BigStruct(id self __attribute__((unused)), BigStruct c) {
185 BigStruct blockDirect = structBlock(nil, a);
186 testassert(!memcmp(&a, &blockDirect, sizeof(BigStruct)));
187 testassert(state==1);
189 IMP bigStructIMP = imp_implementationWithBlock(structBlock);
191 class_addMethod([Foo class], @selector(structThatIsBig:), bigStructIMP, "oh, type strings, how I hate thee. Fortunately, the runtime doesn't generally care.");
195 BigStructFuncPtr bFunc;
198 testassert(!memcmp(&a, &b, sizeof(BigStruct)));
200 testassert(!memcmp(&a, &b, sizeof(BigStruct)));
202 bFunc = (BigStructFuncPtr) [Foo instanceMethodForSelector: @selector(methodThatReturnsBigStruct:)];
204 b = bFunc(f, @selector(methodThatReturnsBigStruct:), a);
205 testassert(!memcmp(&a, &b, sizeof(BigStruct)));
207 b = [f methodThatReturnsBigStruct: a];
208 testassert(!memcmp(&a, &b, sizeof(BigStruct)));
210 bFunc = (BigStructFuncPtr) [Foo instanceMethodForSelector: @selector(structThatIsBig:)];
212 b = bFunc(f, @selector(structThatIsBig:), a);
213 testassert(!memcmp(&a, &b, sizeof(BigStruct)));
214 testassert(state==2);
216 b = [f structThatIsBig: a];
217 testassert(!memcmp(&a, &b, sizeof(BigStruct)));
218 testassert(state==3);
223 IMP floatIMP = imp_implementationWithBlock(^float (id self __attribute__((unused)), float aFloat ) {
226 class_addMethod([Foo class], @selector(methodThatReturnsFloat:), floatIMP, "ooh.. type string unspecified again... oh noe... runtime might punish. not.");
228 float e = (float)0.001;
229 float retF = (float)[f methodThatReturnsFloat: 37.1212f];
230 testassert( ((retF - e) < 37.1212) && ((retF + e) > 37.1212) );
233 #if !__has_feature(objc_arc)
234 // Make sure imp_implementationWithBlock() and imp_removeBlock()
235 // don't deadlock while calling Block_copy() and Block_release()
236 Deallocator *dead = [[Deallocator alloc] init];
237 IMP deadlockImp = imp_implementationWithBlock(^{ [dead self]; });
239 imp_removeBlock(deadlockImp);