Skip to content

Instantly share code, notes, and snippets.

@OneSadCookie
Created November 13, 2014 07:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save OneSadCookie/234a40cb13e80adc7b1d to your computer and use it in GitHub Desktop.
Save OneSadCookie/234a40cb13e80adc7b1d to your computer and use it in GitHub Desktop.
2014-11-13 20:51:24.187 a.out[78021:7001297] int, immediate call of block
2014-11-13 20:51:24.188 a.out[78021:7001297] 42
2014-11-13 20:51:24.564 a.out[78025:7001307] int, block called after return
2014-11-13 20:51:24.565 a.out[78025:7001307] 42
2014-11-13 20:51:24.921 a.out[78029:7001313] NSString, immediate call of block
2014-11-13 20:51:24.922 a.out[78029:7001313] 42
2014-11-13 20:51:25.295 a.out[78033:7001323] NSString, block called after return
2014-11-13 20:51:25.297 a.out[78033:7001323] (null)
cc -DINTS -DIMMEDIATECALL -Os -fobjc-arc -framework Foundation test.m && ./a.out
cc -DINTS -Os -fobjc-arc -framework Foundation test.m && ./a.out
cc -DIMMEDIATECALL -Os -fobjc-arc -framework Foundation test.m && ./a.out
cc -Os -fobjc-arc -framework Foundation test.m && ./a.out
rm a.out
#import <Foundation/Foundation.h>
@interface Foo : NSObject
@property (readwrite, copy) void (^blockprop)();
@end
#if defined(INTS)
typedef int error_t;
static int _error = 42;
static int *error = &_error;
static inline void showerror(error_t *e)
{
NSLog(@"%d", *e);
}
#else
typedef NSString error_t;
static NSString *error = @"42";
static inline void showerror(error_t *e)
{
NSLog(@"%@", e);
}
#endif
@implementation Foo
- (id)initWithError:(error_t **)errorHandle;
{
self = [super init];
if (self)
{
self.blockprop = ^{
*errorHandle = error;
};
#if defined(IMMEDIATECALL)
self.blockprop();
#endif
}
return self;
}
@end
int main(int argc, const char * argv[])
{
@autoreleasepool
{
NSLog(
@"%s, %s",
#if defined(INTS)
"int",
#else
"NSString",
#endif
#if defined(IMMEDIATECALL)
"immediate call of block"
#else
"block called after return"
#endif
);
error_t *e = 0;
Foo *foo = [[Foo alloc] initWithError:&e];
#if !defined(IMMEDIATECALL)
foo.blockprop();
#endif
showerror(e);
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment