ObjFW  Diff

Differences From Artifact [e741aba66a]:

To Artifact [a55f4922cc]:

  • File src/OFObject.m — part of check-in [0c332aa2d2] at 2013-12-27 02:07:14 on branch trunk — Rework forwarding API and ABI.

    It matches the Apple API and ABI now and the forwarding handler itself
    determines whether it should forward or not.

    This moves handling of +[resolve{Class,Instance}Method:] to the runtime
    and allows adding new kinds of forwarding without changing the runtime.

    Also fixes a potential stack problem in the forwarding for MIPS. (user: js, size: 23913) [annotate] [blame] [check-ins using]


61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# import "threading.h"
#endif

#if defined(OF_HAVE_FORWARDING_TARGET_FOR_SELECTOR)
extern id of_forward(id, SEL, ...);
extern struct stret of_forward_stret(id, SEL, ...);
#else
# define of_forward NULL
# define of_forward_stret NULL
#endif

struct pre_ivar {
	int32_t retainCount;
	struct pre_mem *firstMem, *lastMem;
#if !defined(OF_HAVE_ATOMIC_OPS) && defined(OF_HAVE_THREADS)
	of_spinlock_t retainCountSpinlock;







|
|







61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# import "threading.h"
#endif

#if defined(OF_HAVE_FORWARDING_TARGET_FOR_SELECTOR)
extern id of_forward(id, SEL, ...);
extern struct stret of_forward_stret(id, SEL, ...);
#else
# define of_forward of_method_not_found
# define of_forward_stret of_method_not_found_stret
#endif

struct pre_ivar {
	int32_t retainCount;
	struct pre_mem *firstMem, *lastMem;
#if !defined(OF_HAVE_ATOMIC_OPS) && defined(OF_HAVE_THREADS)
	of_spinlock_t retainCountSpinlock;
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
	/*
	 * Just in case doesNotRecognizeSelector: returned, even though it must
	 * never return.
	 */
	abort();
}

#ifdef OF_OBJFW_RUNTIME
static IMP
commonForwardHandler(id obj, SEL sel, IMP (*lookup)(id, SEL), IMP forward)
{
	/* Try resolveClassMethod:/resolveInstanceMethod: */
	if (class_isMetaClass(object_getClass(obj))) {
		if ([obj respondsToSelector: @selector(resolveClassMethod:)] &&
		    [obj resolveClassMethod: sel]) {
			if (![obj respondsToSelector: sel]) {
				fprintf(stderr, "Runtime error: [%s "
				    "resolveClassMethod: %s] returned true "
				    "without adding the method!\n",
				    class_getName(obj), sel_getName(sel));
				abort();
			}

			return lookup(obj, sel);
		}
	} else {
		Class c = object_getClass(obj);

		if ([c respondsToSelector: @selector(resolveInstanceMethod:)] &&
		    [c resolveInstanceMethod: sel]) {
			if (![obj respondsToSelector: sel]) {
				fprintf(stderr, "Runtime error: [%s "
				    "resolveInstanceMethod: %s] returned true "
				    "without adding the method!\n",
				    class_getName(object_getClass(obj)),
				    sel_getName(sel));
				abort();
			}

			return lookup(obj, sel);
		}
	}

#ifdef OF_HAVE_FORWARDING_TARGET_FOR_SELECTOR
	if (class_respondsToSelector(object_getClass(obj),
	    @selector(forwardingTargetForSelector:))) {
		id target = [obj forwardingTargetForSelector: sel];

		if (target != nil && target != obj)
			return forward;
	}
#endif

	of_method_not_found(obj, sel);
	return NULL;
}

static IMP
forwardHandler(id obj, SEL sel)
{
	return commonForwardHandler(obj, sel, objc_msg_lookup, (IMP)of_forward);
}

static IMP
forwardHandlerStret(id obj, SEL sel)
{
	return commonForwardHandler(obj, sel,
	    objc_msg_lookup_stret, (IMP)of_forward_stret);
}
#endif

#ifndef HAVE_OBJC_ENUMERATIONMUTATION
void
objc_enumerationMutation(id object)
{
	enumerationMutationHandler(object);
}







<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
|
<
<
<
<
|
<
<
<
<
<
<
<
<
<
<
|
<
<
<
<
<
<
<
<
<
<
<
<
<
<

<

<
<
<
<
<
<
<
<
<
<
<
<
<
<







133
134
135
136
137
138
139















140




141










142














143

144














145
146
147
148
149
150
151
	/*
	 * Just in case doesNotRecognizeSelector: returned, even though it must
	 * never return.
	 */
	abort();
}
















void




of_method_not_found_stret(void *st, id obj, SEL sel)










{














	of_method_not_found(obj, sel);

}















#ifndef HAVE_OBJC_ENUMERATIONMUTATION
void
objc_enumerationMutation(id object)
{
	enumerationMutationHandler(object);
}
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
@implementation OFObject
+ (void)load
{
#if !defined(OF_APPLE_RUNTIME) || defined(__OBJC2__)
	objc_setUncaughtExceptionHandler(uncaughtExceptionHandler);
#endif

#if defined(OF_OBJFW_RUNTIME)
	objc_forward_handler = forwardHandler;
	objc_forward_handler_stret = forwardHandlerStret;
#elif defined(OF_APPLE_RUNTIME) && \
    defined(OF_HAVE_FORWARDING_TARGET_FOR_SELECTOR)
	objc_setForwardHandler(of_forward, of_forward_stret);
#endif

#ifdef HAVE_OBJC_ENUMERATIONMUTATION
	objc_setEnumerationMutationHandler(enumerationMutationHandler);
#endif

#if defined(HAVE_ARC4RANDOM)
	of_hash_seed = arc4random();







<
<
<
<
<

<







217
218
219
220
221
222
223





224

225
226
227
228
229
230
231
@implementation OFObject
+ (void)load
{
#if !defined(OF_APPLE_RUNTIME) || defined(__OBJC2__)
	objc_setUncaughtExceptionHandler(uncaughtExceptionHandler);
#endif






	objc_setForwardHandler(of_forward, of_forward_stret);


#ifdef HAVE_OBJC_ENUMERATIONMUTATION
	objc_setEnumerationMutationHandler(enumerationMutationHandler);
#endif

#if defined(HAVE_ARC4RANDOM)
	of_hash_seed = arc4random();