summaryrefslogtreecommitdiff
path: root/Objective-C/PyObjC/objc-runtime-gnu.m
blob: a130714a00ab6e92840e1d5b7437acd3fd4fa60d (plain)
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
/* This file is part of the PyObjC package. */
/*
 * Support code for the GNU runtime
 *
 * NOTE: This file uses some private functions in the GNU runtime as that seems
 * to be the only way to properly interface with that runtime.
 */
#include "pyobjc.h"

#if defined(GNU_RUNTIME)
#include "objc-runtime-gnu.h"

struct objc_protocol_list* PyObjCRT_AllocProtocolList(ssize_t numProtocols)
{   
	struct objc_protocol_list *plist;
	
	plist = malloc(sizeof(struct objc_protocol_list)
			 + ((numProtocols+1) * sizeof(Protocol *)));

	if (plist == NULL) {
			return NULL;
	}
	
	plist->count = 0;
	plist->next = NULL;

	return plist;
}

int PyObjCRT_SetupClass(
	Class cls, 
	Class metaCls, 
	const char*name, 
	Class superCls,
	Class rootCls,
	ssize_t ivarSize,
	struct objc_ivar_list* 	ivarList,
	struct objc_protocol_list* protocolList
)

{
	/* This is a private function, but seems to be the only way to
	 * really create the class.
	 */
	extern void __objc_install_premature_dtable (Class);

	/* Initialize the structure */
	memset(cls, 0, sizeof(*cls));
	memset(metaCls, 0, sizeof(*cls));

	cls->version = 0;
	metaCls->version = 0;
	cls->subclass_list = NULL;
	metaCls->subclass_list = NULL;
	cls->sibling_class = NULL;
	metaCls->sibling_class = NULL;

	cls->methods = NULL;
	metaCls->methods = NULL;
	cls->class_pointer = metaCls;

	cls->info = CLS_CLASS;
	metaCls->info = CLS_META;

	cls->name = strdup(name);
	if (cls->name == NULL) {
		return -1;
	}
	metaCls->name = strdup(name);
	if (metaCls->name == NULL) {
		free((char*)(cls->name));
		cls->name = NULL;
		return -1;
	}

	cls->methods = NULL;
	metaCls->methods = NULL;

	cls->super_class = superCls;
	metaCls->super_class = superCls->class_pointer;
	metaCls->class_pointer = rootCls->class_pointer;
	CLS_SETRESOLV(cls);
	CLS_SETRESOLV(metaCls);

	cls->instance_size = ivarSize;
	cls->ivars = ivarList;

	metaCls->instance_size = metaCls->super_class->instance_size;
	metaCls->ivars = NULL;

	metaCls->protocols = cls->protocols = protocolList;

	metaCls->dtable = objc_get_uninstalled_dtable();
	cls->dtable = objc_get_uninstalled_dtable();

	return 0;
}

void PyObjCRT_AddClass(Class cls)
{
	cls->sibling_class = cls->super_class->subclass_list;
	cls->super_class->subclass_list = cls;
	cls->class_pointer->sibling_class = cls->super_class->class_pointer->subclass_list;
	cls->super_class->class_pointer->subclass_list = cls->class_pointer;
	__objc_add_class_to_hash(cls);
}


void PyObjCRT_ClearClass(Class cls)
{
	if (cls->methods) {
		MethodList_t next, cur;

		cur = cls->methods;

		while (cur != NULL) {
			next = cur->method_next;

			objc_free(cur);
			cur = next;
		}
		cls->methods = NULL;
	}

	if (cls->name) {
		free((char*)(cls->name));
		cls->name = NULL;
	}
}

struct objc_method_list *PyObjCRT_AllocMethodList(ssize_t numMethods)
{
        struct objc_method_list *mlist;

        mlist = objc_malloc(sizeof(struct objc_method_list)
                 + ((numMethods+1) * sizeof(struct objc_method)));

        if (mlist == NULL) {
                return NULL;
        }

        mlist->method_count = 0;
	mlist->method_next = NULL;

        return mlist;
}

/*
 * XXX: This functions should be renamed to avoid name classes with
 * the actual ObjC runtime
 */

Ivar_t class_getInstanceVariable(Class aClass, const char *name)
{
  if (!aClass || !name)
    return NULL;

  for (; aClass != Nil; aClass = aClass->super_class)
    {
      int i;

      if (!aClass->ivars)
	continue;

      for (i = 0; i < aClass->ivars->ivar_count; i++)
	{
	  if (!strcmp(aClass->ivars->ivar_list[i].ivar_name, name))
	    return &aClass->ivars->ivar_list[i];
	}
    }

  return NULL;
}

Ivar_t object_getInstanceVariable(id obj, const char *name, void **out)
{
  Ivar_t var = NULL;

  if (obj && name)
    {
      void **varIndex = NULL;

      if ((var = class_getInstanceVariable(obj->class_pointer, name)))
	varIndex = (void **)((char *)obj + var->ivar_offset);

      if (out)
	*out = *varIndex;
    }

  return var;
}

Ivar_t object_setInstanceVariable(id obj, const char *name, void *value)
{
  Ivar_t var = NULL;

  if (obj && name)
    {
      void **varIndex;

      if ((var = class_getInstanceVariable(obj->class_pointer, name)))
	{
	  varIndex = (void **)((char *)obj + var->ivar_offset);

	  *varIndex = value;
	}
    }

  return var;
}


#else /* !GNU_RUNTIME */

static int dummy __attribute__((__unused__));

#endif /* !GNU_RUNTIME */