This repository has been archived by the owner on Apr 12, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
ACForwardingWrapper.m
273 lines (250 loc) · 10.5 KB
/
ACForwardingWrapper.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
/*
* Copyright (c) 2006 Jacob Burkhart (JacobBurkhart@gmail.com)
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
* IN THE SOFTWARE.
*/
#import "ACGeneratedCode.h"
#import "ACForwardingWrapper.h"
#import "ACAspectManager.h"
#import "ACMethodIterator.h"
#import "ACInvocation.h"
#import "NSObjectExtensions.h"
#import </usr/include/objc/objc-class.h>
@implementation ACForwardingWrapper
+(BOOL)canWrapMethod: (ACMethodSignature *) method
{
//Something's broken, disabling forwarding wrappers completely until we figure it out
return NO;
/*
SEL selector = [method getSelector];
if(class_getInstanceMethod([NSObject class], selector) == NULL &&
class_getInstanceMethod([NSObject class]->isa, selector) == NULL)
{
return YES;
}else{
return NO;
}
*/
}
+(void)wrapMethod: (struct objc_method *) method withDepth: (int) depth{
NSString * methodSelector = [NSString stringWithCString: (char *)method->method_name];
if(![methodSelector hasPrefix: @"__ac_hiding_"]){
method->method_name = sel_registerName([[NSString stringWithFormat: @"__ac_hiding_%i_%s", depth, method->method_name] cString]);
}
}
+(void)wrapClass:(Class)toWrap{
if([ACAspectManager loggingAll])
NSLog(@"wrapping %s with forwarding", toWrap->name);
ACAspectManager * manager = [ACAspectManager sharedManager];
if(![manager isClassWrappedWithDepth: toWrap]){
[ACReplacingWrapper wrapClass: toWrap];
}
if(![manager isClassWrappedWithForwarding: toWrap]){
int depth = [ACClass depthOfInheritance: toWrap];
BOOL addInv = NO;
BOOL addSig = NO;
int countToAdd = 2;
//save the old methodSignatureForSelector:
//we use ACMethodIterator instead of class_getInstanceMethod to find the original
//because we to find the method only in toWrap, not in a superclass
Method methSig = [ACMethodIterator
findMethod: @selector(methodSignatureForSelector:)
onClass: toWrap];
if(methSig != NULL){
//save it
[manager saveSig: methSig forClass: toWrap];
//and replace it with a special one
NSString * selectorString = [NSString stringWithFormat: @"methodSignatureForSelector%i:", depth];
SEL sigSelector = sel_registerName([selectorString cString]);
methSig->method_imp = class_getInstanceMethod([self class], sigSelector)->method_imp;
countToAdd--;
}else{
//add a new one
addSig = YES;
}
//save the old forwardInvocation:
Method forwInv = [ACMethodIterator
findMethod: @selector(forwardInvocation:)
onClass: toWrap];
if(forwInv != NULL){
//save it
[manager saveInv: forwInv forClass: toWrap];
//and replace it with a special one
NSString * selectorString = [NSString stringWithFormat: @"forwardInvocation%i:", depth];
SEL invSelector = sel_registerName([selectorString cString]);
forwInv->method_imp = class_getInstanceMethod([self class], invSelector)->method_imp;
countToAdd--;
}else{
//add a new one
addInv = YES;
}
if(countToAdd > 0){
struct objc_method_list * methodsToAdd =
(struct objc_method_list *)
malloc(countToAdd * sizeof(struct objc_method)
+ sizeof(struct objc_method_list));
methodsToAdd->method_count = countToAdd;
struct objc_method meth;
//give it a new forwardInvocation:
if( addInv ){
NSString * selectorString = [NSString stringWithFormat: @"forwardInvocation%i:", depth];
SEL invSelector = sel_registerName([selectorString cString]);
meth = *class_getInstanceMethod([self class], invSelector);
meth.method_name = @selector(forwardInvocation:);
methodsToAdd->method_list[--countToAdd] = meth;
}
//give it a new methodSignatureForSelector:
if( addSig ){
NSString * selectorString = [NSString stringWithFormat: @"methodSignatureForSelector%i:", depth];
SEL sigSelector = sel_registerName([selectorString cString]);
meth = *class_getInstanceMethod([self class], sigSelector);
meth.method_name = @selector(methodSignatureForSelector:);
methodsToAdd->method_list[--countToAdd] = meth;
}
class_addMethods(toWrap, methodsToAdd);
}
}
[manager setWrappedWithForwarding: YES forClass: toWrap];
}
/*
*
* !!! We also need to add a customized respondsToSelector: and implmentsProtocol: to each wrapped class
*
*/
#define forwardInv(i) \
- (void)forwardInvocation##i:(NSInvocation *)anInvocation \
{ \
__ac_forwardInvocation(self, _cmd, anInvocation, i); \
} \
forwardInv(0)
forwardInv(1)
forwardInv(2)
forwardInv(3)
forwardInv(4)
forwardInv(5)
forwardInv(6)
forwardInv(7)
forwardInv(8)
forwardInv(9)
forwardInv(10)
forwardInv(11)
forwardInv(12)
void perform_forwardInvocation(id self, SEL original, SEL toInvoke, NSInvocation * anInvocation, int depth){
Class toLookup = resolveClass(self, depth);
Method thisMethod = [ACMethodIterator
findMethod: toInvoke
onClass: toLookup];
if(thisMethod == NULL)
{
NSLog(@"Error, couldn't find method %s on class %s", toInvoke, toLookup->name);
[ACMethodIterator listMethods: toLookup];
}
struct objc_method_list * methodsToAdd =
(struct objc_method_list *)
malloc( sizeof(struct objc_method)
+ sizeof(struct objc_method_list));
methodsToAdd->method_count = 1;
methodsToAdd->method_list[0] = *thisMethod;
methodsToAdd->method_list[0].method_name = original;
class_addMethods(toLookup, methodsToAdd);
ACAdviceList * advice = [[ACAspectManager sharedManager] adviceListForSelector: original onClass: toLookup];
ACInvocation * invoker = [[ACInvocation alloc] initWithInvocation: anInvocation];
[invoker setClass: toLookup];
[invoker setSelector: original];
[invoker setAdvice: advice];
[invoker performAdvice];
class_removeMethods(toLookup, methodsToAdd);
}
void __ac_forwardInvocation(id self, SEL _cmd, NSInvocation * anInvocation, int depth){
SEL oldInvoke = [anInvocation selector];
int originalDepth = depth;
if([self respondsToSelector: oldInvoke]){
while(depth>0){
depth--;
SEL superInvoke = sel_registerName([[NSString stringWithFormat: @"__ac_hiding_%i_%s", depth,oldInvoke] cString]);
if ([self respondsToSelector: superInvoke]){
[anInvocation setSelector: superInvoke];
return perform_forwardInvocation(self, oldInvoke, superInvoke, anInvocation, depth);
}
}
}
SEL newInvoke = sel_registerName([[NSString stringWithFormat: @"__ac_hiding_%i_%s", depth,oldInvoke] cString]);
if ([self respondsToSelector: newInvoke]){
[anInvocation setSelector: newInvoke];
return perform_forwardInvocation(self, oldInvoke, newInvoke, anInvocation, originalDepth);
}else{
//see if we have an imp for forwardInvocation on this class saved
ACIMP * savedIMP = [[ACAspectManager sharedManager] getInvforClass: self->isa methodDepth: depth objectDepth: [self __ac_depth]];
if(savedIMP != nil){
IMP toInvoke = [savedIMP getIMP];
toInvoke(self, @selector(forwardInvocation:), anInvocation);
}else{
__ac_nsobjects_forwardInvocation(self, anInvocation);
}
}
}
void __ac_nsobjects_forwardInvocation(id self, NSInvocation * anInvocation){
IMP toInvoke = class_getInstanceMethod([NSObject class], @selector(forwardInvocation:))->method_imp;
toInvoke(self, @selector(forwardInvocation:), anInvocation);
}
//we're getting the replacement imp
//for methodSignatureForSelector:
//from this class
#define methodSig(i) \
- (NSMethodSignature *)methodSignatureForSelector##i:(SEL)aSelector \
{ \
return __ac_methodSignatureForSelector(self, _cmd, aSelector, i); \
} \
methodSig(0)
methodSig(1)
methodSig(2)
methodSig(3)
methodSig(4)
methodSig(5)
methodSig(6)
methodSig(7)
methodSig(8)
methodSig(9)
methodSig(10)
methodSig(11)
methodSig(12)
NSMethodSignature* __ac_methodSignatureForSelector(id self, SEL _cmd, SEL aSelector, int depth){
// NSLog(@"(Aspect) method sig %s %i", aSelector, depth);
//newSig = aSelector prepended with __ac_hiding_
SEL newSig = sel_registerName([[NSString stringWithFormat: @"__ac_hiding_%i_%s", depth, aSelector] cString]);
//see if there is a hidding method with this selector
if ([self respondsToSelector: newSig]){
//return the signature of the hiding method
return __ac_nsobjects_methodSignatureForSelector(self, newSig);
}else{
//see if we have an imp for forwardInvocation on this class saved
ACIMP * savedIMP = [[ACAspectManager sharedManager] getSigforClass: self->isa methodDepth: depth objectDepth: [self __ac_depth]];
if(savedIMP != nil){
IMP toInvoke = [savedIMP getIMP];
return toInvoke(self, @selector(methodSignatureForSelector:), aSelector);
}else{
return __ac_nsobjects_methodSignatureForSelector(self, aSelector);
}
}
}
NSMethodSignature* __ac_nsobjects_methodSignatureForSelector(id self, SEL aSelector){
IMP toInvoke = class_getInstanceMethod([NSObject class], @selector(methodSignatureForSelector:))->method_imp;
return toInvoke(self, @selector(methodSignatureForSelector:), aSelector);
}
@end