-
Notifications
You must be signed in to change notification settings - Fork 189
/
Copy pathRTMethod.m
280 lines (219 loc) · 6.12 KB
/
RTMethod.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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
#import "RTMethod.h"
#import <stdarg.h>
#import "MARTNSObject.h"
@interface _RTObjCMethod : RTMethod
{
Method _m;
}
@end
@implementation _RTObjCMethod
- (id)initWithObjCMethod: (Method)method
{
if((self = [self init]))
{
_m = method;
}
return self;
}
- (SEL)selector
{
return method_getName(_m);
}
- (IMP)implementation
{
return method_getImplementation(_m);
}
- (NSString *)signature
{
return [NSString stringWithUTF8String: method_getTypeEncoding(_m)];
}
- (void)setImplementation: (IMP)newImp
{
method_setImplementation(_m, newImp);
}
@end
@interface _RTComponentsMethod : RTMethod
{
SEL _sel;
IMP _imp;
NSString *_sig;
}
@end
@implementation _RTComponentsMethod
- (id)initWithSelector: (SEL)sel implementation: (IMP)imp signature: (NSString *)signature
{
if((self = [self init]))
{
_sel = sel;
_imp = imp;
_sig = [signature copy];
}
return self;
}
- (void)dealloc
{
[_sig release];
[super dealloc];
}
- (SEL)selector
{
return _sel;
}
- (IMP)implementation
{
return _imp;
}
- (NSString *)signature
{
return _sig;
}
- (void)setImplementation: (IMP)newImp
{
_imp = newImp;
}
@end
@implementation RTMethod
+ (id)methodWithObjCMethod: (Method)method
{
return [[[self alloc] initWithObjCMethod: method] autorelease];
}
+ (id)methodWithSelector: (SEL)sel implementation: (IMP)imp signature: (NSString *)signature
{
return [[[self alloc] initWithSelector: sel implementation: imp signature: signature] autorelease];
}
- (id)initWithObjCMethod: (Method)method
{
[self release];
return [[_RTObjCMethod alloc] initWithObjCMethod: method];
}
- (id)initWithSelector: (SEL)sel implementation: (IMP)imp signature: (NSString *)signature
{
[self release];
return [[_RTComponentsMethod alloc] initWithSelector: sel implementation: imp signature: signature];
}
- (NSString *)description
{
return [NSString stringWithFormat: @"<%@ %p: %@ %p %@>", [self class], self, NSStringFromSelector([self selector]), [self implementation], [self signature]];
}
- (BOOL)isEqual: (id)other
{
return [other isKindOfClass: [RTMethod class]] &&
[self selector] == [other selector] &&
[self implementation] == [other implementation] &&
[[self signature] isEqual: [other signature]];
}
- (NSUInteger)hash
{
return (NSUInteger)(void *)[self selector] ^ (NSUInteger)[self implementation] ^ [[self signature] hash];
}
- (SEL)selector
{
[self doesNotRecognizeSelector: _cmd];
return NULL;
}
- (NSString *)selectorName
{
return NSStringFromSelector([self selector]);
}
- (IMP)implementation
{
[self doesNotRecognizeSelector: _cmd];
return NULL;
}
- (NSString *)signature
{
[self doesNotRecognizeSelector: _cmd];
return NULL;
}
- (void)setImplementation: (IMP)newImp
{
[self doesNotRecognizeSelector: _cmd];
}
- (void)_returnValue: (void *)retPtr sendToTarget: (id)target arguments: (va_list)args
{
NSMethodSignature *signature = [target methodSignatureForSelector: [self selector]];
NSInvocation *invocation = [NSInvocation invocationWithMethodSignature: signature];
NSUInteger argumentCount = [signature numberOfArguments];
[invocation setTarget: target];
[invocation setSelector: [self selector]];
for(NSUInteger i = 2; i < argumentCount; i++)
{
int cookie = va_arg(args, int);
if(cookie != RT_ARG_MAGIC_COOKIE)
{
NSLog(@"%s: incorrect magic cookie %08x; did you forget to use RTARG() around your arguments?", __func__, cookie);
abort();
}
const char *typeStr = va_arg(args, char *);
void *argPtr = va_arg(args, void *);
NSUInteger inSize;
NSGetSizeAndAlignment(typeStr, &inSize, NULL);
NSUInteger sigSize;
NSGetSizeAndAlignment([signature getArgumentTypeAtIndex: i], &sigSize, NULL);
if(inSize != sigSize)
{
NSLog(@"%s: size mismatch between passed-in argument and required argument; in type: %s (%lu) requested: %s (%lu)", __func__, typeStr, (long)inSize, [signature getArgumentTypeAtIndex: i], (long)sigSize);
abort();
}
[invocation setArgument: argPtr atIndex: i];
}
[invocation invoke];
if([signature methodReturnLength] && retPtr)
[invocation getReturnValue: retPtr];
}
- (id)sendToTarget: (id)target, ...
{
NSParameterAssert([[self signature] hasPrefix: [NSString stringWithUTF8String: @encode(id)]]);
id retVal = nil;
va_list args;
va_start(args, target);
[self _returnValue: &retVal sendToTarget: target arguments: args];
va_end(args);
return retVal;
}
- (void)returnValue: (void *)retPtr sendToTarget: (id)target, ...
{
va_list args;
va_start(args, target);
[self _returnValue: retPtr sendToTarget: target arguments: args];
va_end(args);
}
@end
@implementation NSObject (RTMethodSendingAdditions)
- (id)rt_sendMethod: (RTMethod *)method, ...
{
NSParameterAssert([[method signature] hasPrefix: [NSString stringWithUTF8String: @encode(id)]]);
id retVal = nil;
va_list args;
va_start(args, method);
[method _returnValue: &retVal sendToTarget: self arguments: args];
va_end(args);
return retVal;
}
- (void)rt_returnValue: (void *)retPtr sendMethod: (RTMethod *)method, ...
{
va_list args;
va_start(args, method);
[method _returnValue: retPtr sendToTarget: self arguments: args];
va_end(args);
}
- (id)rt_sendSelector: (SEL)sel, ...
{
RTMethod *method = [[self rt_class] rt_methodForSelector: sel];
NSParameterAssert([[method signature] hasPrefix: [NSString stringWithUTF8String: @encode(id)]]);
id retVal = nil;
va_list args;
va_start(args, sel);
[method _returnValue: &retVal sendToTarget: self arguments: args];
va_end(args);
return retVal;
}
- (void)rt_returnValue: (void *)retPtr sendSelector: (SEL)sel, ...
{
RTMethod *method = [[self rt_class] rt_methodForSelector: sel];
va_list args;
va_start(args, sel);
[method _returnValue: retPtr sendToTarget: self arguments: args];
va_end(args);
}
@end