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
|
objc_global_mutex_unlock();
return NULL;
}
IMP
objc_replace_class_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->isa->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;
replaced = YES;
break;
}
}
}
if ((cats = objc_categories_for_class(cls)) != NULL) {
for (; *cats != NULL; cats++) {
for (ml = (*cats)->instance_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->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->methodlist = ml;
}
objc_update_dtable(cls);
objc_global_mutex_unlock();
return oldimp;
}
static void
free_class(Class rcls)
{
struct objc_abi_class *cls = (struct objc_abi_class*)rcls;
|
|
<
|
|
<
<
<
|
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
>
|
>
|
>
>
>
>
|
>
|
>
>
|
>
|
>
<
|
|
|
|
|
|
<
|
|
|
<
|
|
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
class_replaceMethod(Class cls, SEL sel, IMP newimp, const char *types)
{
struct objc_method_list *ml;
struct objc_category **cats;
unsigned int i;
IMP oldimp;
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);
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
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);
objc_global_mutex_unlock();
return oldimp;
}
}
}
}
}
/* 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;
ml->methods[0].sel.types = types;
ml->methods[0].imp = newimp;
cls->isa->methodlist = ml;
objc_update_dtable(cls);
objc_global_mutex_unlock();
return (IMP)nil;
}
static void
free_class(Class rcls)
{
struct objc_abi_class *cls = (struct objc_abi_class*)rcls;
|