ObjFW  Check-in [31e7b3d756]

Overview
Comment:Better dissection of what should be public and private.
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | runtime
Files: files | file ages | folders
SHA3-256: 31e7b3d756ed3802907be8be58e48ea37272e121adcc1434006ad9d3dc9cf84a
User & Date: js on 2012-03-04 23:48:46
Other Links: branch diff | manifest | tags
Context
2012-03-05
00:01
Add sel_isEqual(). check-in: 7215a36f5a user: js tags: runtime
2012-03-04
23:48
Better dissection of what should be public and private. check-in: 31e7b3d756 user: js tags: runtime
20:47
Make OFBlock compile with the ObjFW runtime. check-in: 8002f3e823 user: js tags: runtime
Changes

Modified src/runtime/category.m from [aeaf0363b4] to [d939f47c4b].

101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121

	for (i = 0; i < symtab->cat_def_cnt; i++) {
		register_selectors(cats[i]);
		register_category(cats[i]);
	}
}

struct objc_abi_category**
objc_categories_for_class(Class cls)
{
	if (categories == NULL)
		return NULL;

	return (struct objc_abi_category**)objc_hashtable_get(categories,
	    cls->name);
}

void
objc_free_all_categories(void)
{
	uint32_t i;







|





|







101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121

	for (i = 0; i < symtab->cat_def_cnt; i++) {
		register_selectors(cats[i]);
		register_category(cats[i]);
	}
}

struct objc_category**
objc_categories_for_class(Class cls)
{
	if (categories == NULL)
		return NULL;

	return (struct objc_category**)objc_hashtable_get(categories,
	    cls->name);
}

void
objc_free_all_categories(void)
{
	uint32_t i;

Modified src/runtime/class.m from [283e2434aa] to [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

	return NO;
}

void
objc_update_dtable(Class cls)
{
	struct objc_abi_method_list *ml;
	struct objc_abi_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);

	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,
					    ml->methods[j].imp);
		}
	}

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








|
|










|
|










|
|







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_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,
			    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)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
{
	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;
	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;
				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 ==
					    sel->uid) {
						const char *ret =
						    ml->methods[i].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;
	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) {
				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 ==
					    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)
			ERROR("Not enough memory to replace method!");

		ml->next = cls->isa->methodlist;
		ml->count = 1;
		ml->methods[0].name = (const char*)sel->uid;
		/* FIXME: We need to get the type from a superclass */
		ml->methods[0].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;
	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) {
				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 ==
					    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)
			ERROR("Not enough memory to replace method!");

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

		cls->methodlist = ml;
	}

	objc_update_dtable(cls);








|
|






|
|











|


|
















|
|








|













|













|




|

|















|
|








|













|













|




|

|







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_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 (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 (ml->methods[i].sel.uid ==
					    sel->uid) {
						const char *ret =
						    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_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);

Modified src/runtime/protocol.m from [548197efb9] to [76c52af657].

20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35

#import "runtime.h"
#import "runtime-private.h"

@implementation Protocol
- (BOOL)_isImplementedByClass: (Class)cls
{
	struct objc_abi_protocol_list *pl;
	struct objc_abi_category **cats;
	long i, j;

	objc_global_mutex_lock();

	for (pl = cls->protocols; pl != NULL; pl = pl->next) {
		for (i = 0; i < pl->count; i++) {
			if (!strcmp(pl->list[i]->name, name)) {







|
|







20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35

#import "runtime.h"
#import "runtime-private.h"

@implementation Protocol
- (BOOL)_isImplementedByClass: (Class)cls
{
	struct objc_protocol_list *pl;
	struct objc_category **cats;
	long i, j;

	objc_global_mutex_lock();

	for (pl = cls->protocols; pl != NULL; pl = pl->next) {
		for (i = 0; i < pl->count; i++) {
			if (!strcmp(pl->list[i]->name, name)) {

Modified src/runtime/runtime-private.h from [5494f2f715] to [76682de6f9].

53
54
55
56
57
58
59
60
61
62
63
64
65
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
};

struct objc_abi_category {
	const char *category_name;
	const char *class_name;
	struct objc_abi_method_list *instance_methods;
	struct objc_abi_method_list *class_methods;
	struct objc_abi_protocol_list *protocols;
};

struct objc_abi_super {
	id self;
	Class class;
};

struct objc_abi_method_description {
	const char *name;
	const char *types;
};

struct objc_abi_method_description_list {
	int count;
	struct objc_abi_method_description list[1];
};

struct objc_abi_protocol_list {
	struct objc_abi_protocol_list *next;
	long count;
	Protocol *list[1];
};

struct objc_abi_static_instances {
	const char *class_name;
	id instances[1];
};

struct objc_abi_symtab {
	unsigned long unknown;







|

















<
<
<
<
<
<







53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77






78
79
80
81
82
83
84
};

struct objc_abi_category {
	const char *category_name;
	const char *class_name;
	struct objc_abi_method_list *instance_methods;
	struct objc_abi_method_list *class_methods;
	struct objc_protocol_list *protocols;
};

struct objc_abi_super {
	id self;
	Class class;
};

struct objc_abi_method_description {
	const char *name;
	const char *types;
};

struct objc_abi_method_description_list {
	int count;
	struct objc_abi_method_description list[1];
};







struct objc_abi_static_instances {
	const char *class_name;
	id instances[1];
};

struct objc_abi_symtab {
	unsigned long unknown;
122
123
124
125
126
127
128






129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
	BOOL empty;
};

struct objc_sparsearray_level3 {
	const void *buckets[256];
	BOOL empty;
};







typedef struct {
	of_mutex_t mutex;
	of_thread_t owner;
	int count;
} objc_mutex_t;

extern void objc_register_all_categories(struct objc_abi_symtab*);
extern struct objc_abi_category** objc_categories_for_class(Class);
extern void objc_free_all_categories(void);
extern void objc_update_dtable(Class);
extern void objc_register_all_classes(struct objc_abi_symtab*);
extern Class objc_classname_to_class(const char*);
extern void objc_free_all_classes(void);
extern uint32_t objc_hash_string(const char*);
extern struct objc_hashtable* objc_hashtable_alloc(uint32_t);







>
>
>
>
>
>








|







116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
	BOOL empty;
};

struct objc_sparsearray_level3 {
	const void *buckets[256];
	BOOL empty;
};

enum objc_abi_class_info {
	OBJC_CLASS_INFO_CLASS	    = 0x01,
	OBJC_CLASS_INFO_METACLASS   = 0x02,
	OBJC_CLASS_INFO_INITIALIZED = 0x04
};

typedef struct {
	of_mutex_t mutex;
	of_thread_t owner;
	int count;
} objc_mutex_t;

extern void objc_register_all_categories(struct objc_abi_symtab*);
extern struct objc_category** objc_categories_for_class(Class);
extern void objc_free_all_categories(void);
extern void objc_update_dtable(Class);
extern void objc_register_all_classes(struct objc_abi_symtab*);
extern Class objc_classname_to_class(const char*);
extern void objc_free_all_classes(void);
extern uint32_t objc_hash_string(const char*);
extern struct objc_hashtable* objc_hashtable_alloc(uint32_t);

Modified src/runtime/runtime.h from [92e1b049bf] to [95328bedcc].

43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70



71





72







73




74
75
76
77
78
79
80
	Class isa;
	Class superclass;
	const char *name;
	unsigned long version;
	unsigned long info;
	unsigned long instance_size;
	void *ivars;
	struct objc_abi_method_list *methodlist;
	struct objc_sparsearray *dtable;
	Class *subclass_list;
	void *sibling_class;
	struct objc_abi_protocol_list *protocols;
	void *gc_object_type;
	unsigned long abi_version;
	void *ivar_offsets;
	void *properties;
};

struct objc_object {
	Class isa;
};

struct objc_selector {
	uintptr_t uid;
	const char *types;
};

enum objc_abi_class_info {



	OBJC_CLASS_INFO_CLASS	    = 0x01,





	OBJC_CLASS_INFO_METACLASS   = 0x02,







	OBJC_CLASS_INFO_INITIALIZED = 0x04




};

#define Nil (Class)0
#define nil (id)0
#define YES (BOOL)1
#define NO  (BOOL)0








|



|















|
>
>
>
|
>
>
>
>
>
|
>
>
>
>
>
>
>
|
>
>
>
>







43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
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
	Class isa;
	Class superclass;
	const char *name;
	unsigned long version;
	unsigned long info;
	unsigned long instance_size;
	void *ivars;
	struct objc_method_list *methodlist;
	struct objc_sparsearray *dtable;
	Class *subclass_list;
	void *sibling_class;
	struct objc_protocol_list *protocols;
	void *gc_object_type;
	unsigned long abi_version;
	void *ivar_offsets;
	void *properties;
};

struct objc_object {
	Class isa;
};

struct objc_selector {
	uintptr_t uid;
	const char *types;
};

struct objc_method {
	struct objc_selector sel;
	IMP imp;
};

struct objc_method_list {
	struct objc_method_list *next;
	unsigned int count;
	struct objc_method methods[1];
};

struct objc_category {
	const char *category_name;
	const char *class_name;
	struct objc_method_list *instance_methods;
	struct objc_method_list *class_methods;
	struct objc_protocol_list *protocols;
};

struct objc_protocol_list {
	struct objc_protocol_list *next;
	long count;
	Protocol *list[1];
};

#define Nil (Class)0
#define nil (id)0
#define YES (BOOL)1
#define NO  (BOOL)0