-
Notifications
You must be signed in to change notification settings - Fork 0
/
category_loader.c
91 lines (79 loc) · 2.14 KB
/
category_loader.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
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
#include <stdio.h>
#include "objc/runtime.h"
#include "visibility.h"
#include "loader.h"
#include "dtable.h"
#define BUFFER_TYPE struct objc_category
#include "buffer.h"
void objc_send_load_message(Class class);
static void register_methods(struct objc_class *cls, struct objc_method_list *l)
{
if (NULL == l) { return; }
// Replace the method names with selectors.
objc_register_selectors_from_list(l);
// Add the method list at the head of the list of lists.
l->next = cls->methods;
cls->methods = l;
// Update the dtable to catch the new methods, if the dtable has been
// created (don't bother creating dtables for classes when categories are
// loaded if the class hasn't received any messages yet.
if (classHasDtable(cls))
{
add_method_list_to_class(cls, l);
}
}
static void load_category(struct objc_category *cat, struct objc_class *class)
{
register_methods(class, cat->instance_methods);
register_methods(class->isa, cat->class_methods);
//fprintf(stderr, "Loading %s (%s)\n", cat->class_name, cat->name);
if (cat->protocols)
{
objc_init_protocols(cat->protocols);
cat->protocols->next = class->protocols;
class->protocols = cat->protocols;
}
}
static BOOL try_load_category(struct objc_category *cat)
{
Class class = (Class)objc_getClass(cat->class_name);
//fprintf(stderr, "Trying to load %s (%s)\n", cat->class_name, cat->name);
if (Nil != class)
{
load_category(cat, class);
return YES;
}
//fprintf(stderr, "waiting to load %s (%s)\n", cat->class_name, cat->name);
return NO;
}
/**
* Attaches a category to its class, if the class is already loaded. Buffers
* it for future resolution if not.
*/
PRIVATE void objc_try_load_category(struct objc_category *cat)
{
if (!try_load_category(cat))
{
set_buffered_object_at_index(cat, buffered_objects++);
}
}
PRIVATE void objc_load_buffered_categories(void)
{
BOOL shouldReshuffle = NO;
for (unsigned i=0 ; i<buffered_objects ; i++)
{
struct objc_category *c = buffered_object_at_index(i);
if (NULL != c)
{
if (try_load_category(c))
{
set_buffered_object_at_index(NULL, i);
shouldReshuffle = YES;
}
}
}
if (shouldReshuffle)
{
compact_buffer();
}
}