forked from mikeash/MABlockForwarding
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMABlockForwarding.m
233 lines (194 loc) · 5.94 KB
/
MABlockForwarding.m
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
#import <objc/message.h>
#import "MABlockForwarding.h"
struct BlockDescriptor
{
unsigned long reserved;
unsigned long size;
void *rest[1];
};
struct Block
{
void *isa;
int flags;
int reserved;
void *invoke;
struct BlockDescriptor *descriptor;
};
enum {
BLOCK_HAS_COPY_DISPOSE = (1 << 25),
BLOCK_HAS_CTOR = (1 << 26), // helpers have C++ code
BLOCK_IS_GLOBAL = (1 << 28),
BLOCK_HAS_STRET = (1 << 29), // IFF BLOCK_HAS_SIGNATURE
BLOCK_HAS_SIGNATURE = (1 << 30),
};
static void *BlockImpl(id block)
{
return ((struct Block *)block)->invoke;
}
static const char *BlockSig(id blockObj)
{
struct Block *block = (void *)blockObj;
struct BlockDescriptor *descriptor = block->descriptor;
assert(block->flags & BLOCK_HAS_SIGNATURE);
int index = 0;
if(block->flags & BLOCK_HAS_COPY_DISPOSE)
index += 2;
return descriptor->rest[index];
}
@interface NSInvocation (PrivateHack)
- (void)invokeUsingIMP: (IMP)imp;
@end
@interface MAFakeBlock : NSObject
{
int _flags;
int _reserved;
IMP _invoke;
struct BlockDescriptor *_descriptor;
id _forwardingBlock;
BlockInterposer _interposer;
}
- (id)initWithBlock: (id)block interposer: (BlockInterposer)interposer;
@end
@implementation MAFakeBlock
- (id)initWithBlock: (id)block interposer: (BlockInterposer)interposer
{
if((self = [super init]))
{
_forwardingBlock = [block copy];
_interposer = [interposer copy];
// NB: The bottom 16 bits represent the block's retain count
_flags = ((struct Block *) block)->flags & ~0xFFFF;
_descriptor = malloc(sizeof(struct BlockDescriptor));
_descriptor->size = class_getInstanceSize([self class]);
int index = 0;
if (_flags & BLOCK_HAS_COPY_DISPOSE)
index += 2;
_descriptor->rest[index] = (void *) BlockSig(block);
if (_flags & BLOCK_HAS_STRET)
_invoke = (IMP) _objc_msgForward_stret;
else
_invoke = _objc_msgForward;
}
return self;
}
- (void)dealloc
{
[_forwardingBlock release];
[_interposer release];
free(_descriptor);
[super dealloc];
}
- (NSMethodSignature *)methodSignatureForSelector: (SEL)sel
{
const char *types = BlockSig(_forwardingBlock);
NSMethodSignature *sig = [NSMethodSignature signatureWithObjCTypes: types];
while([sig numberOfArguments] < 2)
{
types = [[NSString stringWithFormat: @"%s%s", types, @encode(void *)] UTF8String];
sig = [NSMethodSignature signatureWithObjCTypes: types];
}
return sig;
}
- (void)forwardInvocation: (NSInvocation *)inv
{
[inv setTarget: _forwardingBlock];
_interposer(inv, ^{
[inv invokeUsingIMP: BlockImpl(_forwardingBlock)];
});
}
- (id)copyWithZone: (NSZone *)zone
{
return [self retain];
}
@end
id MAForwardingBlock(BlockInterposer interposer, id block)
{
return [[[MAFakeBlock alloc] initWithBlock: block interposer: interposer] autorelease];
}
id MAMemoize(id block) {
NSMutableDictionary *memory = [NSMutableDictionary dictionary];
return MAForwardingBlock(^(NSInvocation *inv, void (^call)(void)) {
NSMethodSignature *sig = [inv methodSignature];
NSMutableArray *args = [NSMutableArray array];
for(unsigned i = 1; i < [sig numberOfArguments]; i++)
{
id arg = nil;
const char *type = [sig getArgumentTypeAtIndex: i];
if(type[0] == @encode(id)[0])
{
[inv getArgument: &arg atIndex: i];
if([arg conformsToProtocol: @protocol(NSCopying)])
arg = [[arg copy] autorelease];
}
else if(type[0] == @encode(char *)[0])
{
char *str;
[inv getArgument: &str atIndex: i];
arg = [NSData dataWithBytes: str length: strlen(str)];
}
else
{
NSUInteger size;
NSGetSizeAndAlignment(type, &size, NULL);
arg = [NSMutableData dataWithLength: size];
[inv getArgument: [arg mutableBytes] atIndex: i];
}
if(!arg)
arg = [NSNull null];
[args addObject: arg];
}
const char *type = [sig methodReturnType];
BOOL isObj = type[0] == @encode(id)[0];
BOOL isCStr = type[0] == @encode(char *)[0];
id result;
@synchronized(memory)
{
result = [[[memory objectForKey: args] retain] autorelease];
}
if(!result)
{
call();
if(isObj)
{
[inv getReturnValue: &result];
}
else if(isCStr)
{
char *str;
[inv getReturnValue: &str];
result = str ? [NSData dataWithBytes: str length: strlen(str) + 1] : NULL;
}
else
{
NSUInteger size;
NSGetSizeAndAlignment(type, &size, NULL);
result = [NSMutableData dataWithLength: size];
[inv getReturnValue: [result mutableBytes]];
}
if(!result)
result = [NSNull null];
@synchronized(memory)
{
[memory setObject: result forKey: args];
}
}
else
{
if(result == [NSNull null])
result = nil;
if(isObj)
{
[inv setReturnValue: &result];
}
else if(isCStr)
{
const char *str = [result bytes];
[inv setReturnValue: &str];
}
else
{
[inv setReturnValue: [result mutableBytes]];
}
}
}, block);
}