5 // Created by Wincent Colaiuta on 14 June 2005.
7 // Copyright 2005-2007 Wincent Colaiuta.
8 // This program is free software: you can redistribute it and/or modify
9 // it under the terms of the GNU General Public License as published by
10 // the Free Software Foundation, either version 3 of the License, or
11 // (at your option) any later version.
13 // This program is distributed in the hope that it will be useful,
14 // but WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 // GNU General Public License for more details.
18 // You should have received a copy of the GNU General Public License
19 // along with this program. If not, see <http://www.gnu.org/licenses/>.
26 #import <objc/objc-class.h>
29 #import "NSInvocation+WOTest.h"
30 #import "NSObject+WOTest.h"
31 #import "NSProxy+WOTest.h"
32 #import "NSValue+WOTest.h"
35 @implementation WOStub
39 return self; // super (NSProxy) has no init method
44 [self setAcceptsAnyArguments:YES];
49 #pragma mark Recording
51 - (id)returning:(NSValue *)aValue
53 NSAssert(([self returnValue] == nil), @"WOStub returning: invoked but return value already recorded");
54 [self setReturnValue:aValue];
58 - (id)raising:(id)anException
60 NSAssert(([self exception] == nil), @"WOStub raising: invoked but exception already recorded");
61 [self setException:anException];
66 #pragma mark Testing equality
68 - (BOOL)matchesInvocation:(NSInvocation *)anInvocation
70 NSParameterAssert(anInvocation != nil);
71 NSInvocation *recordedInvocation = [self invocation];
72 NSAssert((recordedInvocation != nil), @"WOStub sent matchesInvocation but no invocation yet recorded");
73 return ([anInvocation WOTest_isEqualToInvocation:recordedInvocation] ||
74 ([self acceptsAnyArguments] && [anInvocation WOTest_isEqualToInvocationIgnoringArguments:recordedInvocation]));
78 #pragma mark Proxy methods
80 - (void)forwardInvocation:(NSInvocation *)anInvocation
82 NSAssert(([self invocation] == nil), @"WOStub sent message but message previously recorded");
83 [self setInvocation:anInvocation];
84 [anInvocation retainArguments];
89 http://lists.apple.com/archives/cocoa-dev/2004/Jun/msg00990.html
91 "On PPC, the prearg area is used to store the 13 floating-point parameter registers, which may contain method parameters that need to be restored when the marg_list is used. The i386 function call ABI has no additional registers to be saved, so its prearg area is empty. The implementations of _objc_msgForward() and objc_msgSendv() in objc4's objc-msg-ppc.s contain more details that may be useful to you.
93 In general, you probably want to avoid marg_list and objc_msgSendv(). Together they are primarily an implementation detail of forward:: ."
99 - forward:(SEL)sel :(marg_list)args
101 NSAssert(([self invocation] == nil), @"WOStub sent message but message previously recorded");
103 // let standard event flow take place (but note that NSProxy implementation raises so subclasses must do the real work)
104 if ([self methodSignatureForSelector:sel])
105 return [super forward:sel :args];
107 // fallback to internal lookup
108 NSMethodSignature *signature = [[delegate methodSignatures] objectForKey:NSStringFromSelector(sel)];
110 // at this point it would be great to be able to improvise but it's not possible to figure out an accurate method signature
111 NSAssert((signature != nil), ([NSString stringWithFormat:@"no method signature for selector %@", NSStringFromSelector(sel)]));
113 NSInvocation *forwardInvocation = [NSInvocation invocationWithMethodSignature:signature];
114 [forwardInvocation setSelector:sel];
117 // store arguments in invocation
119 for (unsigned i = 0, max = [signature numberOfArguments]; i < max; i++)
121 const char *type = [signature getArgumentTypeAtIndex:i]; // always id, SEL, ...
125 // TODO: finish this implementation and copy it (or otherwise make it available) to WOMock.m
126 // leave the compiler warnings about "unused variable 'type'" and "unused variable 'offset'" to remind me to do it
128 // the PPC ABI has special conventions for floats, doubles, structs
130 // contents of floating point registers f1 through f13 stored at args + 0 through args + 96
131 // register based params go in r3 (receiver id), r4 (SEL), and r5 through r10
132 // these params are respectively at:
133 // 1st param: r3 (receiver id) args + (13 * 8) + 24 (24 bytes in the linkage area not sure what it's for)
134 // 2nd param: r4 (SEL) args + (13 * 8) + 28
135 // 3rd param: r5 args + (13 * 8) + 32
136 // 4th param: r6 args + (13 * 8) + 36
137 // 5th param: r7 args + (13 * 8) + 40
138 // 6th param: r8 args + (13 * 8) + 44
139 // 7th param: r9 args + (13 * 8) + 48
140 // 8th param: r10 args + (13 * 8) + 52
141 // the remaining parameters are on the stack (starting at args + (13 * 8) + 56)
142 // note that marg_prearg_size is defined in the headers for ppc and equals 128 (13 * 8 + 24 bytes for linkage area)
144 // from http://darwinsource.opendarwin.org/10.4.3/objc4-267/runtime/Messengers.subproj/objc-msg-ppc.s
145 // typedef struct objc_sendv_margs {
146 // double floatingPointArgs[13];
147 // int linkageArea[6];
148 // int registerArgs[8];
149 // int stackArgs[variable];
152 // if (strcmp(type, @encode(float)) == 0)
154 // else if (strcmp(type, @encode(double)) == 0)
158 #elif defined(__i386__)
160 // on i386 the marg_getRef macro and its helper, marg_adjustedOffset, should work fine
161 if (strcmp(type, @encode(id)) == 0)
163 id *ref = marg_getRef(args, offset, id);
164 [forwardInvocation WOTest_setArgumentValue:[NSValue valueWithBytes:ref objCType:type] atIndex:i];
166 else if (strcmp(type, @encode(SEL)) == 0)
168 SEL *ref = marg_getRef(args, offset, SEL);
169 [forwardInvocation WOTest_setArgumentValue:[NSValue valueWithBytes:ref objCType:type] atIndex:i];
172 [NSException raise:NSGenericException format:@"type %s not supported", type];
174 offset += [NSValue WOTest_sizeForType:[NSString stringWithUTF8String:type]];
176 #elif defined(__ppc64__)
177 // there is no objc-msg-ppc.s so for now just omit support rather than make assumptions
178 #error ppc64 not supported yet
182 #error Unsupported architecture
187 [self forwardInvocation:forwardInvocation];
188 return nil; // nobody cares what a stub returns
192 #pragma mark Accessors
194 - (NSInvocation *)recordedInvocation
196 NSInvocation *recordedInvocation = [self invocation];
197 NSAssert((recordedInvocation != nil), @"WOStub sent recordedInvocation but no invocation yet recorded");
198 return recordedInvocation;
201 @synthesize invocation;
202 @synthesize returnValue;
203 @synthesize acceptsAnyArguments;
204 @synthesize exception;