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 "NSMethodSignature+WOTest.h"
31 #import "NSObject+WOTest.h"
32 #import "NSProxy+WOTest.h"
33 #import "NSValue+WOTest.h"
36 @implementation WOStub
40 return self; // super (NSProxy) has no init method
45 [self setAcceptsAnyArguments:YES];
50 #pragma mark Recording
52 - (id)returning:(NSValue *)aValue
54 NSAssert(([self returnValue] == nil), @"WOStub returning: invoked but return value already recorded");
55 [self setReturnValue:aValue];
59 - (id)raising:(id)anException
61 NSAssert(([self exception] == nil), @"WOStub raising: invoked but exception already recorded");
62 [self setException:anException];
67 #pragma mark Testing equality
69 - (BOOL)matchesInvocation:(NSInvocation *)anInvocation
71 NSParameterAssert(anInvocation != nil);
72 NSInvocation *recordedInvocation = [self invocation];
73 NSAssert((recordedInvocation != nil), @"WOStub sent matchesInvocation but no invocation yet recorded");
74 return ([anInvocation WOTest_isEqualToInvocation:recordedInvocation] ||
75 ([self acceptsAnyArguments] && [anInvocation WOTest_isEqualToInvocationIgnoringArguments:recordedInvocation]));
79 #pragma mark Proxy methods
81 - (void)forwardInvocation:(NSInvocation *)anInvocation
83 NSAssert(([self invocation] == nil), @"WOStub sent message but message previously recorded");
84 [self setInvocation:anInvocation];
85 [anInvocation retainArguments];
90 http://lists.apple.com/archives/cocoa-dev/2004/Jun/msg00990.html
92 "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.
94 In general, you probably want to avoid marg_list and objc_msgSendv(). Together they are primarily an implementation detail of forward:: ."
100 - forward:(SEL)sel :(marg_list)args
102 NSAssert(([self invocation] == nil), @"WOStub sent message but message previously recorded");
104 // let standard event flow take place (but note that NSProxy implementation raises so subclasses must do the real work)
105 if ([self methodSignatureForSelector:sel])
106 return [super forward:sel :args];
108 // fallback to internal lookup
109 NSMethodSignature *signature = [[delegate methodSignatures] objectForKey:NSStringFromSelector(sel)];
111 // at this point it would be great to be able to improvise but it's not possible to figure out an accurate method signature
112 NSAssert((signature != nil), ([NSString stringWithFormat:@"no method signature for selector %@", NSStringFromSelector(sel)]));
114 NSInvocation *forwardInvocation = [NSInvocation invocationWithMethodSignature:signature];
115 [forwardInvocation setSelector:sel];
118 // store arguments in invocation
120 for (unsigned i = 0, max = [signature numberOfArguments]; i < max; i++)
122 const char *type = [signature getArgumentTypeAtIndex:i]; // always id, SEL, ...
126 // TODO: finish this implementation and copy it (or otherwise make it available) to WOMock.m
127 // leave the compiler warnings about "unused variable 'type'" and "unused variable 'offset'" to remind me to do it
129 // the PPC ABI has special conventions for floats, doubles, structs
131 // contents of floating point registers f1 through f13 stored at args + 0 through args + 96
132 // register based params go in r3 (receiver id), r4 (SEL), and r5 through r10
133 // these params are respectively at:
134 // 1st param: r3 (receiver id) args + (13 * 8) + 24 (24 bytes in the linkage area not sure what it's for)
135 // 2nd param: r4 (SEL) args + (13 * 8) + 28
136 // 3rd param: r5 args + (13 * 8) + 32
137 // 4th param: r6 args + (13 * 8) + 36
138 // 5th param: r7 args + (13 * 8) + 40
139 // 6th param: r8 args + (13 * 8) + 44
140 // 7th param: r9 args + (13 * 8) + 48
141 // 8th param: r10 args + (13 * 8) + 52
142 // the remaining parameters are on the stack (starting at args + (13 * 8) + 56)
143 // note that marg_prearg_size is defined in the headers for ppc and equals 128 (13 * 8 + 24 bytes for linkage area)
145 // from http://darwinsource.opendarwin.org/10.4.3/objc4-267/runtime/Messengers.subproj/objc-msg-ppc.s
146 // typedef struct objc_sendv_margs {
147 // double floatingPointArgs[13];
148 // int linkageArea[6];
149 // int registerArgs[8];
150 // int stackArgs[variable];
153 // if (strcmp(type, @encode(float)) == 0)
155 // else if (strcmp(type, @encode(double)) == 0)
159 #elif defined(__i386__)
161 // on i386 the marg_getRef macro and its helper, marg_adjustedOffset, should work fine
162 if (strcmp(type, @encode(id)) == 0)
164 id *ref = marg_getRef(args, offset, id);
165 [forwardInvocation WOTest_setArgumentValue:[NSValue valueWithBytes:ref objCType:type] atIndex:i];
167 else if (strcmp(type, @encode(SEL)) == 0)
169 SEL *ref = marg_getRef(args, offset, SEL);
170 [forwardInvocation WOTest_setArgumentValue:[NSValue valueWithBytes:ref objCType:type] atIndex:i];
173 [NSException raise:NSGenericException format:@"type %s not supported", type];
175 offset += [NSValue WOTest_sizeForType:[NSString stringWithUTF8String:type]];
177 #elif defined(__ppc64__)
178 // there is no objc-msg-ppc.s so for now just omit support rather than make assumptions
179 #error ppc64 not supported yet
183 #error Unsupported architecture
188 [self forwardInvocation:forwardInvocation];
189 return nil; // nobody cares what a stub returns
193 #pragma mark Accessors
195 - (NSInvocation *)recordedInvocation
197 NSInvocation *recordedInvocation = [self invocation];
198 NSAssert((recordedInvocation != nil), @"WOStub sent recordedInvocation but no invocation yet recorded");
199 return recordedInvocation;
202 @synthesize invocation;
203 @synthesize returnValue;
204 @synthesize acceptsAnyArguments;
205 @synthesize exception;