On Tue, May 12, 2009 at 6:19 PM, Greg Parker <gpar...@apple.com> 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.
>
> 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.)
>
> Someday this will all work seamlessly, but currently all platforms and
> architectures suffer from at least one of the above.

Interesting stuff, thanks. I have a couple of questions, though:

1) Won't the inability to call the IMP directly make
-methodForSelector: almost completely useless? In theory, *any* method
could be implemented using forwarding. You simply can't know in
advance. If such an IMP can't be used in confidence for a forwarded
method, then -methodForSelector: can't be used to cache an IMP for any
method you didn't write yourself. Obviously it could still be used for
this case, where the returned IMP is only used for building a class's
method tables, but usually the IMP is obtained with the intention of
calling it directly.

2) Am I correct in assuming that if you grab the method signature and
then conditionally call either class_getMethodImplementation or
class_getMethodImplementation_stret that you avoid the problem of
getting the non-struct version of the forwarder for struct-returning
methods?

Mike
_______________________________________________

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

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:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com

Reply via email to