-
Notifications
You must be signed in to change notification settings - Fork 108
/
Copy pathblocks.mm
42 lines (34 loc) · 1.53 KB
/
blocks.mm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#include "blocks.h"
#include <objc/runtime.h>
#import <Foundation/NSMethodSignature.h>
// Thanks to CTObjectiveCRuntimeAdditions (https://github.com/ebf/CTObjectiveCRuntimeAdditions).
// See http://clang.llvm.org/docs/Block-ABI-Apple.html.
void logBlock(FILE *file, id block) {
struct BlockLiteral_ *blockRef = (__bridge struct BlockLiteral_ *)block;
int flags = blockRef->flags;
const char *signature = NULL;
if (flags & BLOCK_HAS_SIGNATURE) {
unsigned char *signatureLocation = (unsigned char *)blockRef->descriptor;
signatureLocation += sizeof(unsigned long int);
signatureLocation += sizeof(unsigned long int);
if (flags & BLOCK_HAS_COPY_DISPOSE) {
signatureLocation += sizeof(void (*)(void *, void *));
signatureLocation += sizeof(void (*)(void *));
}
signature = (*(const char **)signatureLocation);
}
if (signature) {
NSMethodSignature *methodSignature = [NSMethodSignature signatureWithObjCTypes:signature];
Class kind = object_getClass(block);
fprintf(file, "<%s@%p signature=\"%s ; retType=%s", class_getName(kind), (__bridge void *)block, signature, methodSignature.methodReturnType);
// Skip the first argument (self).
NSUInteger numOfArgs = methodSignature.numberOfArguments;
for (NSUInteger i = 1; i < numOfArgs; ++i) {
fprintf(file, " %u=%s", (unsigned)i, [methodSignature getArgumentTypeAtIndex:i]);
}
fprintf(file, "\">");
} else {
Class kind = object_getClass(block);
fprintf(file, "<%s@%p>", class_getName(kind), (__bridge void *)block);
}
}