ObjFW  Diff

Differences From Artifact [283e2434aa]:

To Artifact [44fd744eb1]:


66
67
68
69
70
71
72
73
74


75
76
77
78
79
80
81
82
83
84
85
86


87
88
89
90
91
92
93
94
95
96
97
98


99
100
101
102
103
104
105
66
67
68
69
70
71
72


73
74
75
76
77
78
79
80
81
82
83
84


85
86
87
88
89
90
91
92
93
94
95
96


97
98
99
100
101
102
103
104
105







-
-
+
+










-
-
+
+










-
-
+
+








	return NO;
}

void
objc_update_dtable(Class cls)
{
	struct objc_abi_method_list *ml;
	struct objc_abi_category **cats;
	struct objc_method_list *ml;
	struct objc_category **cats;
	struct objc_sparsearray *dtable;
	unsigned int i;

	if (cls->superclass != Nil)
		dtable = objc_sparsearray_copy(cls->superclass->dtable);
	else
		dtable = objc_sparsearray_new();

	for (ml = cls->methodlist; ml != NULL; ml = ml->next)
		for (i = 0; i < ml->count; i++)
			objc_sparsearray_set(dtable, (uint32_t)
			    (uintptr_t)ml->methods[i].name, ml->methods[i].imp);
			objc_sparsearray_set(dtable,
			    ml->methods[i].sel.uid, ml->methods[i].imp);

	if ((cats = objc_categories_for_class(cls)) != NULL) {
		for (i = 0; cats[i] != NULL; i++) {
			unsigned int j;

			ml = (cls->info & OBJC_CLASS_INFO_CLASS ?
			    cats[i]->instance_methods : cats[i]->class_methods);

			for (; ml != NULL; ml = ml->next)
				for (j = 0; j < ml->count; j++)
					objc_sparsearray_set(dtable, (uint32_t)
					    (uintptr_t)ml->methods[j].name,
					objc_sparsearray_set(dtable,
					    (uint32_t)ml->methods[j].sel.uid,
					    ml->methods[j].imp);
		}
	}

	if (cls->dtable != NULL)
		objc_sparsearray_free_when_singlethreaded(cls->dtable);

289
290
291
292
293
294
295
296
297


298
299
300
301
302
303
304
305


306
307
308
309
310
311
312
313
314
315
316
317

318
319
320

321
322
323
324
325
326
327
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
289
290
291
292
293
294
295


296
297
298
299
300
301
302
303


304
305
306
307
308
309
310
311
312
313
314
315
316

317
318
319

320
321
322
323
324
325
326
327
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







-
-
+
+






-
-
+
+











-
+


-
+
















-
-
+
+








-
+













-
+













-
+




-
+

-
+















-
-
+
+








-
+













-
+













-
+




-
+

-
+







{
	return objc_sparsearray_get(cls->dtable, (uint32_t)sel->uid);
}

const char*
objc_get_type_encoding(Class cls, SEL sel)
{
	struct objc_abi_method_list *ml;
	struct objc_abi_category **cats;
	struct objc_method_list *ml;
	struct objc_category **cats;
	unsigned int i;

	objc_global_mutex_lock();

	for (ml = cls->isa->methodlist; ml != NULL; ml = ml->next) {
		for (i = 0; i < ml->count; i++) {
			if ((uintptr_t)ml->methods[i].name == sel->uid) {
				const char *ret = ml->methods[i].types;
			if (ml->methods[i].sel.uid == sel->uid) {
				const char *ret = ml->methods[i].sel.types;
				objc_global_mutex_unlock();
				return ret;
			}
		}
	}

	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 ((uintptr_t)ml->methods[i].name ==
					if (ml->methods[i].sel.uid ==
					    sel->uid) {
						const char *ret =
						    ml->methods[i].types;
						    ml->methods[i].sel.types;
						objc_global_mutex_unlock();
						return ret;
					}
				}
			}
		}
	}

	objc_global_mutex_unlock();

	return NULL;
}

IMP
objc_replace_class_method(Class cls, SEL sel, IMP newimp)
{
	struct objc_abi_method_list *ml;
	struct objc_abi_category **cats;
	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 ((uintptr_t)ml->methods[i].name == sel->uid) {
			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 ((uintptr_t)ml->methods[i].name ==
					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_abi_method_list))) == NULL)
		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].name = (const char*)sel->uid;
		ml->methods[0].sel.uid = sel->uid;
		/* FIXME: We need to get the type from a superclass */
		ml->methods[0].types = sel->types;
		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_abi_method_list *ml;
	struct objc_abi_category **cats;
	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 ((uintptr_t)ml->methods[i].name == sel->uid) {
			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 ((uintptr_t)ml->methods[i].name ==
					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_abi_method_list))) == NULL)
		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].name = (const char*)sel->uid;
		ml->methods[0].sel.uid = sel->uid;
		/* FIXME: We need to get the type from a superclass */
		ml->methods[0].types = sel->types;
		ml->methods[0].sel.types = sel->types;
		ml->methods[0].imp = newimp;

		cls->methodlist = ml;
	}

	objc_update_dtable(cls);