ObjFW  Check-in [2e39765aa2]

Overview
Comment:objc_replace_{class,instance}_method() -> class_replaceMethod().
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | runtime
Files: files | file ages | folders
SHA3-256: 2e39765aa2c446bb3bf8efad9275429ab52d9f7e7d32a9af510879b77d8cc24c
User & Date: js on 2012-03-06 16:24:03
Other Links: branch diff | manifest | tags
Context
2012-03-06
16:31
Merge changes from default branch into runtime branch. check-in: 9bb186a7a3 user: js tags: runtime
16:24
objc_replace_{class,instance}_method() -> class_replaceMethod(). check-in: 2e39765aa2 user: js tags: runtime
2012-03-05
00:13
Add missing -I for runtime in tests. check-in: feed2dbe67 user: js tags: runtime
Changes

Modified src/runtime/class.m from [44fd744eb1] to [79e5f03637].

328
329
330
331
332
333
334
335

336
337
338
339
340
341

342
343
344
345

346
347
348
349
350
351
352

353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410

411
412




413
414
415
416
417
418



419
420



421
422
423
424

425

426
427




428
429
430
431
432
433
434
435
436
437



438
439
440
441



442
443
444


445
446

447
448
449
450
451
452
453

454
455
456
457
458
459
460
328
329
330
331
332
333
334

335
336
337
338
339


340
341
342
343

344
345
346
347




348

























































349
350


351
352
353
354
355
356
357
358
359
360
361
362
363


364
365
366
367
368
369
370
371
372
373


374
375
376
377
378
379
380
381
382
383




384
385
386
387



388
389
390



391
392
393

394

395
396
397
398
399

400
401
402
403
404
405
406
407







-
+




-
-
+



-
+



-
-
-
-
+
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-

+
-
-
+
+
+
+






+
+
+
-
-
+
+
+




+

+
-
-
+
+
+
+






-
-
-
-
+
+
+

-
-
-
+
+
+
-
-
-
+
+

-
+
-





-
+








	objc_global_mutex_unlock();

	return NULL;
}

IMP
objc_replace_class_method(Class cls, SEL sel, IMP newimp)
class_replaceMethod(Class cls, SEL sel, IMP newimp, const char *types)
{
	struct objc_method_list *ml;
	struct objc_category **cats;
	unsigned int i;
	BOOL replaced = NO;
	IMP oldimp = NULL;
	IMP oldimp;

	objc_global_mutex_lock();

	for (ml = cls->isa->methodlist; ml != NULL; ml = ml->next) {
	for (ml = cls->methodlist; ml != NULL; ml = ml->next) {
		for (i = 0; i < ml->count; i++) {
			if (ml->methods[i].sel.uid == sel->uid) {
				oldimp = ml->methods[i].imp;
				ml->methods[i].imp = newimp;
				replaced = YES;
				break;
			}

		}
	}

	if ((cats = objc_categories_for_class(cls)) != NULL) {
		for (; *cats != NULL; cats++) {
			for (ml = (*cats)->class_methods; ml != NULL;
			    ml = ml->next) {
				for (i = 0; i < ml->count; i++) {
					if (ml->methods[i].sel.uid ==
					    sel->uid) {
						oldimp = ml->methods[i].imp;
						ml->methods[i].imp = newimp;
						replaced = YES;
						break;
					}
				}
			}
		}
	}

	if (!replaced) {
		/* FIXME: We need a way to free this at objc_exit() */
		if ((ml = malloc(sizeof(struct objc_method_list))) == NULL)
			ERROR("Not enough memory to replace method!");

		ml->next = cls->isa->methodlist;
		ml->count = 1;
		ml->methods[0].sel.uid = sel->uid;
		/* FIXME: We need to get the type from a superclass */
		ml->methods[0].sel.types = sel->types;
		ml->methods[0].imp = newimp;

		cls->isa->methodlist = ml;
	}

	objc_update_dtable(cls->isa);

	objc_global_mutex_unlock();

	return oldimp;
}

IMP
objc_replace_instance_method(Class cls, SEL sel, IMP newimp)
{
	struct objc_method_list *ml;
	struct objc_category **cats;
	unsigned int i;
	BOOL replaced = NO;
	IMP oldimp = NULL;

	objc_global_mutex_lock();

	for (ml = cls->methodlist; ml != NULL; ml = ml->next) {
		for (i = 0; i < ml->count; i++) {
			if (ml->methods[i].sel.uid == sel->uid) {
				oldimp = ml->methods[i].imp;
				ml->methods[i].imp = newimp;
				objc_update_dtable(cls);
				replaced = YES;
				break;

				objc_global_mutex_unlock();

				return oldimp;
			}
		}
	}

	if ((cats = objc_categories_for_class(cls)) != NULL) {
		for (; *cats != NULL; cats++) {
			if (cls->info & OBJC_CLASS_INFO_METACLASS)
				ml = (*cats)->class_methods;
			else
			for (ml = (*cats)->instance_methods; ml != NULL;
			    ml = ml->next) {
				ml = (*cats)->instance_methods;

			for (; ml != NULL; ml = ml->next) {
				for (i = 0; i < ml->count; i++) {
					if (ml->methods[i].sel.uid ==
					    sel->uid) {
						oldimp = ml->methods[i].imp;

						ml->methods[i].imp = newimp;
						objc_update_dtable(cls);
						replaced = YES;
						break;

						objc_global_mutex_unlock();

						return oldimp;
					}
				}
			}
		}
	}

	if (!replaced) {
		/* FIXME: We need a way to free this at objc_exit() */
		if ((ml = malloc(sizeof(struct objc_method_list))) == NULL)
			ERROR("Not enough memory to replace method!");
	/* FIXME: We need a way to free this at objc_exit() */
	if ((ml = malloc(sizeof(struct objc_method_list))) == NULL)
		ERROR("Not enough memory to replace method!");

		ml->next = cls->methodlist;
		ml->count = 1;
		ml->methods[0].sel.uid = sel->uid;
	ml->next = cls->isa->methodlist;
	ml->count = 1;
	ml->methods[0].sel.uid = sel->uid;
		/* FIXME: We need to get the type from a superclass */
		ml->methods[0].sel.types = sel->types;
		ml->methods[0].imp = newimp;
	ml->methods[0].sel.types = types;
	ml->methods[0].imp = newimp;

		cls->methodlist = ml;
	cls->isa->methodlist = ml;
	}

	objc_update_dtable(cls);

	objc_global_mutex_unlock();

	return oldimp;
	return (IMP)nil;
}

static void
free_class(Class rcls)
{
	struct objc_abi_class *cls = (struct objc_abi_class*)rcls;

Modified src/runtime/runtime.h from [bd5f7c0ee0] to [5e1f1b3197].

106
107
108
109
110
111
112
113

114
115
116
117
118
119
120
106
107
108
109
110
111
112

113

114
115
116
117
118
119







-
+
-






extern Class class_getSuperclass(Class);
extern BOOL class_isKindOfClass(Class, Class);
extern unsigned long class_getInstanceSize(Class);
extern BOOL class_respondsToSelector(Class, SEL);
extern BOOL class_conformsToProtocol(Class, Protocol*);
extern IMP objc_get_class_method(Class, SEL);
extern IMP objc_get_instance_method(Class, SEL);
extern IMP objc_replace_class_method(Class, SEL, IMP);
extern IMP class_replaceMethod(Class, SEL, IMP, const char*);
extern IMP objc_replace_instance_method(Class, SEL, IMP);
extern const char* objc_get_type_encoding(Class, SEL);
extern IMP objc_msg_lookup(id, SEL);
extern void objc_thread_add(void);
extern void objc_thread_remove(void);
extern void objc_exit(void);
#endif