objc-runtime


Objective-C runtime support

Currently there are two different Objective-C runtimes: the original NeXT runtime and the GNU runtime which is modelled after it.

The differences between the two runtimes resides mainly in the naming of functions. However there are differences that make the two runtimes incompatible. The most important one is how the selectors are kept.

On NeXT runtime a selector is simply a unique string that represents its name. On GNU runtime, a selector is a structure consisting from a selector id (that is not its name) and a string describing its types. The both approaches have advantages and disadvantages. In the NeXT approach no matter how many times you request a selector (either using the @selector directive or by sel_getUid), you receive the same selector. This is possible because the selector is simply represented as a string. On the GNU runtime each time you request a selector using the @selector directive you get a different selector. Moreover the obtained selector has the `types' member set to NULL.

In the NeXT approach the encoding of the selector's types are bound to each class that has a corresponding method. In the GNU approach the selector's types are bound to the selector.

This differences have deep implication on how the NSProxy class handle a method call in the forwardInvocation: method. On the NeXT runtime, the proxy should make a request to the remote side to obtain the types of the method. The types are used locally by a NSMethodSignature object to determine an encoding. This encoding is used to access correctly the arguments on the stack. However you have the possibility to set a protocol to the NSProxy object at which the remote object answer. This should be done to avoid asking the true object about its selector types and so to increase performance.

On the GNU runtime this is not necessarily because in the forwardInvocation: method the selector usually comes with types in it. However there are cases when this is not true and the same mechanism like in the NeXT case should be applied.

Because the remote machine could be different than the local one, the NSMethodSignature class should be able to create the correct encoding from the types received from remote. Thus the NSMethodSignature class is dependent on the target machine.

This implementation of Foundation works with both runtimes. The objc-runtime.h file defines the runtime functions to be those from the GNU runtime. All the NeXT runtime functions are defined in terms of GNU runtime functions.

In order to write portable programs for both runtimes you should never use functions in the GNU runtime that work with the types associated with the selector. So the sel_get_type, sel_get_any_uid and sel_get_typed_uid cannot be used in programs. Also you should never use functions like objc_msgSend or objc_msgSendv. Use the objc_msg_sendv function or the NSInvocation class instead.

Never use types like Method in the NeXT runtime or Method_t in the GNU runtime. Use instead the type struct objc_method*. This exists in both runtimes.