Previous:Dynamically Registering Methods, Up:Messaging with the GNU Objective-C Runtime [Contents][Index]
The GNU Objective-C runtime provides a hook, called__objc_msg_forward2, which is called byobjc_msg_lookup() when it cannot find a method implementation inthe runtime tables and after calling+resolveInstanceMethod:and+resolveClassMethod: has been attempted and did not succeedin dynamically registering the method.
To configure the hook, you set the global variable__objc_msg_forward2 to a function with the same argument andreturn types ofobjc_msg_lookup(). Whenobjc_msg_lookup() cannot find a method implementation, itinvokes the hook function you provided to get a method implementationto return. So, in practice__objc_msg_forward2 allows you toextendobjc_msg_lookup() by adding some custom code that iscalled to do a further lookup when no standard method implementationcan be found using the normal lookup.
This hook is generally reserved for “Foundation” libraries such asGNUstep Base, which use it to implement their high-level methodforwarding API, typically based around theforwardInvocation:method. So, unless you are implementing your own “Foundation”library, you should not set this hook.
In a typical forwarding implementation, the__objc_msg_forward2hook function determines the argument and return type of the methodthat is being looked up, and then creates a function that takes thesearguments and has that return type, and returns it to the caller.Creating this function is non-trivial and is typically performed usinga dedicated library such aslibffi.
The forwarding method implementation thus created is returned byobjc_msg_lookup() and is executed as if it was a normal methodimplementation. When the forwarding method implementation is called,it is usually expected to pack all arguments into some sort of object(typically, anNSInvocation in a “Foundation” library), andhand it over to the programmer (forwardInvocation:) who is thenallowed to manipulate the method invocation using a high-level APIprovided by the “Foundation” library. For example, the programmermay want to examine the method invocation arguments and name andpotentially change them before forwarding the method invocation to oneor more local objects (performInvocation:) or even to remoteobjects (by using Distributed Objects or some other mechanism). Whenall this completes, the return value is passed back and must bereturned correctly to the original caller.
Note that the GNU Objective-C runtime currently provides no supportfor method forwarding or method invocations other than the__objc_msg_forward2 hook.
If the forwarding hook does not exist or returnsNULL, theruntime currently attempts forwarding using an older, deprecated API,and if that fails, it aborts the program. In future versions of theGNU Objective-C runtime, the runtime will immediately abort.
Previous:Dynamically Registering Methods, Up:Messaging with the GNU Objective-C Runtime [Contents][Index]