Re: Creating an NSInvocation from an NSMethodSignature
Re: Creating an NSInvocation from an NSMethodSignature
- Subject: Re: Creating an NSInvocation from an NSMethodSignature
- From: Mike Mangino <email@hidden>
- Date: Wed, 13 May 2009 14:27:48 -0400
On May 12, 2009, at 6:19 PM, Greg Parker wrote:
On May 12, 2009, at 6:43 AM, Michael Ash wrote:
Use -methodForSelector: to get the IMP for a nonexistent method. This
IMP will be a function pointer straight to the runtime's forwarding
machinery. Then install that IMP as the implementation for your
overridden methods. Callers will go straight into the forwarding
machinery rather than into your overridden methods, which will then
invoke -forwardInvocation: as usual and you can then do as you like.
This trick has a few platform- and architecture-specific pitfalls.
Here's the safest way to do it:
First: don't call that IMP directly. On some platforms it breaks C
function rules, because it assumes it's being called from
objc_msgSend.
No problem.
Second: don't do this for methods that return a struct. There may be
two distinct forwarding IMPs, with -methodForSelector: returning the
non-struct version. (By "struct return" I mean "method is called via
objc_msgSend_stret()", which differs from "returns C struct" on some
architectures.)
Good to know. I'm skipping that case for now.
Someday this will all work seamlessly, but currently all platforms
and architectures suffer from at least one of the above.
I'm noticing something related that is causing my problems. I have
mocking working for custom types. When I try to change the class of a
string, however, I get a crash.
-(void) testImplementationOnString {
NSString *s = [NSString stringWithString:@"a test string"];
Class subclass = objc_allocateClassPair([s class], "MyStringMock", 0);
objc_registerClassPair(subclass);
//object_setClass(s, subclass);
}
If you uncomment the last line this crashes. Is that to be expected?
I'm assuming it happens for toll free bridged types. Is that true?
I've tried with NSString and NSArray and both cause a crash.
I've got a workaround in place, but I'd love to be able perform the
same mocking on these types. My code is available at http://github.com/mmangino/OCMock/tree/master
Mike
--
Greg Parker email@hidden Runtime Wrangler
Mike Mangino
http://www.elevatedrails.com
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden