1
2
3
4
5
6
|
<div class="subsection-level-extent" id="Forwarding-hook"> <div class="nav-panel"> <p> Previous: <a href="dynamically-registering-methods" accesskey="p" rel="prev">Dynamically Registering Methods</a>, Up: <a href="messaging-with-the-gnu-objective-c-runtime" accesskey="u" rel="up">Messaging with the GNU Objective-C Runtime</a> [<a href="index#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="indices" title="Index" rel="index">Index</a>]</p> </div> <h1 class="subsection" id="Forwarding-Hook"><span>8.10.2 Forwarding Hook<a class="copiable-link" href="#Forwarding-Hook"> ¶</a></span></h1> <p>The GNU Objective-C runtime provides a hook, called <code class="code">__objc_msg_forward2</code>, which is called by <code class="code">objc_msg_lookup()</code> when it cannot find a method implementation in the runtime tables and after calling <code class="code">+resolveInstanceMethod:</code> and <code class="code">+resolveClassMethod:</code> has been attempted and did not succeed in dynamically registering the method. </p> <p>To configure the hook, you set the global variable <code class="code">__objc_msg_forward2</code> to a function with the same argument and return types of <code class="code">objc_msg_lookup()</code>. When <code class="code">objc_msg_lookup()</code> cannot find a method implementation, it invokes the hook function you provided to get a method implementation to return. So, in practice <code class="code">__objc_msg_forward2</code> allows you to extend <code class="code">objc_msg_lookup()</code> by adding some custom code that is called to do a further lookup when no standard method implementation can be found using the normal lookup. </p> <p>This hook is generally reserved for “Foundation” libraries such as GNUstep Base, which use it to implement their high-level method forwarding API, typically based around the <code class="code">forwardInvocation:</code> method. So, unless you are implementing your own “Foundation” library, you should not set this hook. </p> <p>In a typical forwarding implementation, the <code class="code">__objc_msg_forward2</code> hook function determines the argument and return type of the method that is being looked up, and then creates a function that takes these arguments and has that return type, and returns it to the caller. Creating this function is non-trivial and is typically performed using a dedicated library such as <code class="code">libffi</code>. </p> <p>The forwarding method implementation thus created is returned by <code class="code">objc_msg_lookup()</code> and is executed as if it was a normal method implementation. When the forwarding method implementation is called, it is usually expected to pack all arguments into some sort of object (typically, an <code class="code">NSInvocation</code> in a “Foundation” library), and hand it over to the programmer (<code class="code">forwardInvocation:</code>) who is then allowed to manipulate the method invocation using a high-level API provided by the “Foundation” library. For example, the programmer may want to examine the method invocation arguments and name and potentially change them before forwarding the method invocation to one or more local objects (<code class="code">performInvocation:</code>) or even to remote objects (by using Distributed Objects or some other mechanism). When all this completes, the return value is passed back and must be returned correctly to the original caller. </p> <p>Note that the GNU Objective-C runtime currently provides no support for method forwarding or method invocations other than the <code class="code">__objc_msg_forward2</code> hook. </p> <p>If the forwarding hook does not exist or returns <code class="code">NULL</code>, the runtime currently attempts forwarding using an older, deprecated API, and if that fails, it aborts the program. In future versions of the GNU Objective-C runtime, the runtime will immediately abort. </p> </div> <div class="nav-panel"> <p> Previous: <a href="dynamically-registering-methods">Dynamically Registering Methods</a>, Up: <a href="messaging-with-the-gnu-objective-c-runtime">Messaging with the GNU Objective-C Runtime</a> [<a href="index#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="indices" title="Index" rel="index">Index</a>]</p> </div><div class="_attribution">
<p class="_attribution-p">
© Free Software Foundation<br>Licensed under the GNU Free Documentation License, Version 1.3.<br>
<a href="https://gcc.gnu.org/onlinedocs/gcc-13.1.0/gcc/Forwarding-hook.html" class="_attribution-link">https://gcc.gnu.org/onlinedocs/gcc-13.1.0/gcc/Forwarding-hook.html</a>
</p>
</div>
|