Skip to content

Instantly share code, notes, and snippets.

@gadmyth
Created November 27, 2016 14:18
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 gadmyth/82426506f4d4b4d6364ec9091059f9e8 to your computer and use it in GitHub Desktop.
Save gadmyth/82426506f4d4b4d6364ec9091059f9e8 to your computer and use it in GitHub Desktop.
#import <Foundation/Foundation.h>
struct __block_impl;
struct __block_impl_0;
struct __block_impl {
void *isa;
int Flags;
int Reserved;
void (*FuncPtr)(__block_impl_0 *);
};
struct __block_impl_0 {
struct __block_impl impl;
struct __block_desc_0* Desc;
int happy;
__block_impl_0(void *fp, struct __block_desc_0 *desc, int _happy, int flags=0) : happy(_happy) {
impl.isa = &_NSConcreteStackBlock;
impl.Flags = flags;
impl.FuncPtr = (void(*)(__block_impl_0 *))fp;
Desc = desc;
}
};
static struct __block_desc_0 {
size_t reserved;
size_t Block_size;
};
static void real_block_func(struct __block_impl_0 *__cself) {
int happy = __cself->happy;
NSLog(@"real_block_func: %d", happy);
}
int main(int argc, const char * argv[]) {
@autoreleasepool {
int happy = 10;
__block_desc_0 desc = {0, sizeof(__block_impl_0)};
struct __block_impl_0 wrapper = __block_impl_0((void *) real_block_func, &desc, happy);
happy = 20;
wrapper.impl.FuncPtr(&wrapper);
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment