Created
May 16, 2017 18:26
-
-
Save lukaskollmer/851651b05a4ffaca5e9d5d29d9cb0a0a to your computer and use it in GitHub Desktop.
Objective-C blocks in C++ (this actually works!)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
int __block_invoke_1(struct __block_literal_1 *_block, ...) { | |
printf("hello world\n"); | |
return 1; | |
} | |
struct __block_literal_1 { | |
void *isa; | |
int flags; | |
int reserved; | |
//int (*invoke)(struct __block_literal_1 *, ...); | |
void *invoke; | |
struct __block_descriptor_1 *descriptor; | |
}; | |
static struct __block_descriptor_1 { | |
unsigned long int reserved; | |
unsigned long int Block_size; | |
} __block_descriptor_1 = { 0, sizeof(struct __block_literal_1)/*, __block_invoke_1 */}; | |
auto block_literal_1 = new __block_literal_1; | |
block_literal_1->isa = _NSConcreteGlobalBlock; | |
block_literal_1->flags = (1<<28); | |
block_literal_1->invoke = (void*)(int (*) (__block_literal_1 *, ...) ) __block_invoke_1; | |
//block_literal_1-> | |
// TEST | |
id NSArray = (id)objc_getClass("NSArray"); | |
id NSString = (id)objc_getClass("NSString"); | |
id str1 = objc_call(id, NSString, "stringWithUTF8String:", "banana"); | |
id str2 = objc_call(id, NSString, "stringWithUTF8String:", "apple"); | |
id array = objc_call(id, NSArray, "arrayWithObjects:", str1, str2); | |
printf("array: %s\n", description(array)); | |
id sortedArray = objc_call(id, array, "sortedArrayUsingComparator:", block_literal_1); | |
printf("sorted array: %s\n", description(sortedArray)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment