|
| 1 | +GNUstep Runtime APIs |
| 2 | +==================== |
| 3 | + |
| 4 | +The GNUstep Objective-C runtime aims to implement all of the APIs defined in |
| 5 | +Apple's Objective-C Runtime Reference. That document should be taken as the |
| 6 | +authoritative reference to the majority of the APIs exposed by this runtime. |
| 7 | +Any discrepancies between the implementations in this runtime and the |
| 8 | +documentation should be regarded as a bug in the runtime. |
| 9 | + |
| 10 | +In addition to these APIs, the runtime also exposes some non-portable APIs. |
| 11 | +These are all marked as OBJC_NONPORTABLE in the headers. Many of these APIs |
| 12 | +are also implemented in GNUstep's ObjectiveC2 framework, which uses the GCC |
| 13 | +Objective-C runtime for the underlying implementation and provides a portable |
| 14 | +set of APIs on top of the GCC runtime structures and functions. |
| 15 | + |
| 16 | +Runtime Capabilities |
| 17 | +-------------------- |
| 18 | + |
| 19 | +The objc/capabilities.h header defines runtime capabilities. A copy of this |
| 20 | +header is also present in the ObjectiveC2 framework, so you can conditionally |
| 21 | +include either version. |
| 22 | + |
| 23 | +You can perform two sorts of checks using this header. Constants declared in |
| 24 | +the header describe capabilities that the runtime may or may not implement. If |
| 25 | +a capability is missing from this header, then it means that you are using an |
| 26 | +old version of the runtime, which lacks any knowledge of the header. You can |
| 27 | +use this to refuse to compile with old runtime versions. |
| 28 | + |
| 29 | +You can also use the objc_test_capability() function to test whether a |
| 30 | +particular capability is present at run time. Several of the capabilities are |
| 31 | +optional in this runtime, and may not be compiled in to a given install. If |
| 32 | +you require a particular capability, you can use the OBJC_REQUIRE_CAPABILITY() |
| 33 | +macro to insert a load function that will abort if the capability is not present. |
| 34 | + |
| 35 | +This shows how to refuse to compile or run with versions of the runtime that do |
| 36 | +not support type-dependent dispatch: |
| 37 | + |
| 38 | + #ifndef OBJC_CAP_TYPE_DEPENDENT_DISPATCH |
| 39 | + # error Type-dependent dispatch support required! |
| 40 | + #else |
| 41 | + OBJC_REQUIRE_CAPABILITY(OBJC_CAP_TYPE_DEPENDENT_DISPATCH); |
| 42 | + #endif |
| 43 | + |
| 44 | +Typed Selectors |
| 45 | +--------------- |
| 46 | + |
| 47 | +Like the GCC runtime, this runtime uses typed selectors. In recent versions, |
| 48 | +message lookup is also dependent on the type of the selector by default. This |
| 49 | +can be disabled by not defining the TYPE_DEPENDENT_DISPATCH macro when |
| 50 | +building. When using GNU make, you can get name-dependent dispatch by doing: |
| 51 | + |
| 52 | + $ gmake tdd=no |
| 53 | + |
| 54 | +This is strongly discouraged. It will give compatibility with the semantics of |
| 55 | +the NeXT, Apple, and GCC runtimes, however these semantics include random stack |
| 56 | +corruption from valid code. |
| 57 | + |
| 58 | +There are three functions for dealing with typed selectors. The first two are |
| 59 | +direct equivalents of other functions. |
| 60 | + |
| 61 | + SEL sel_registerTypedName_np(const char *selName, const char *types); |
| 62 | + |
| 63 | +sel_registerName() will register an untyped selector. This variant registers a |
| 64 | +typed selector, using the specified name and type encoding. |
| 65 | + |
| 66 | + const char *sel_getType_np(SEL aSel); |
| 67 | + |
| 68 | +This function simply returns the type encoding of the given selector, or NULL for a typed selector. |
| 69 | + |
| 70 | + unsigned sel_copyTypes_np(const char *selName, |
| 71 | + const char **types, |
| 72 | + unsigned count); |
| 73 | + |
| 74 | +This function copies *all* of the type encodings for a given selector name. |
| 75 | +Generally, there are not many of these. In a well-written program, there will |
| 76 | +be exactly one type encoding for a given selector. In a typical program, there |
| 77 | +will be 1-3. It is not worth allocating a buffer on the heap for most cases, |
| 78 | +so this function is designed to take a stack buffer. |
| 79 | + |
| 80 | +Unlike other functions in the runtime, this always returns the total number of |
| 81 | +type encodings, not the number that were found. This means that you can call |
| 82 | +it once with a smallish on-stack buffer and then call it again with a |
| 83 | +malloc()'d buffer if there are a lot of encodings for a specific selector, as |
| 84 | +follows. |
| 85 | + |
| 86 | + char *t[16]; |
| 87 | + char *types = t; |
| 88 | + unsigned total = sel_copyTypes_np("alloc", types, total); |
| 89 | + if (total > 16) |
| 90 | + { |
| 91 | + types = calloc(sizeof(char*), total); |
| 92 | + sel_copyTypes_np("alloc", types, total); |
| 93 | + } |
| 94 | + // Do stuff with the types. |
| 95 | + if (total > 16) |
| 96 | + { |
| 97 | + free(types); |
| 98 | + } |
| 99 | + |
| 100 | + |
| 101 | +**Note**: This runtime does not provide any equivalent of the GCC runtime's |
| 102 | +sel_get_typed_uid() or sel_get_any_typed_uid(). This is intentional. It is |
| 103 | +impossible to use these functions correctly and they should never have been |
| 104 | +made part of the public API. |
| 105 | + |
| 106 | +Message Sending |
| 107 | +--------------- |
| 108 | + |
| 109 | +For ABI compatibility with the GCC runtime, this runtime implements the |
| 110 | +objc_msg_lookup() and objc_msg_lookup_super() functions used to implement |
| 111 | +message sending. |
| 112 | + |
| 113 | +The new ABI uses the objc_msg_lookup_sender() function in place of |
| 114 | +objc_msg_lookup(). This allows fast proxies and caching of the lookup result |
| 115 | +at the callsite. You can find more a detailed explanation of how this works in |
| 116 | +the README file. |
| 117 | + |
| 118 | +This runtime also provides this semi-private function, which can be of use in |
| 119 | +implementing parts of the Foundation framework and similar low-level libraries: |
| 120 | + |
| 121 | + struct objc_slot* objc_get_slot(Class cls, SEL selector); |
| 122 | + |
| 123 | +This looks up the slot for a given selector, without invoking any forwarding |
| 124 | +mechanisms. This is most useful for quickly finding the type encoding of a |
| 125 | +method (e.g. for implementing forwarding). The lookup is as fast as a normal |
| 126 | +message lookup, and the types field of the returned slot provides the type |
| 127 | +encoding. This is significantly faster than using class_getInstanceMethod(), |
| 128 | +which needs to perform a linear search along a list (O(1) vs O(n)). |
| 129 | + |
| 130 | +Hooks |
| 131 | +----- |
| 132 | + |
| 133 | +All of the callback hooks provided by this runtime are described in |
| 134 | +objc/hooks.h. |
0 commit comments