This repository has been archived by the owner on Oct 16, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
object.c
354 lines (316 loc) · 8.81 KB
/
object.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
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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
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
#include <stdlib.h>
#include <stdio.h>
#include <stdbool.h>
#include <string.h>
#include "core_types.h"
#include "object.h"
#include "class.h"
#include "runtime.h"
#include "exception.h"
#include "string.h"
#include "abortf.h"
#include "heap.h"
struct PupAttributeListEntry {
long name_sym;
struct PupObject *value;
struct PupAttributeListEntry *next;
};
struct PupClass *pup_bootstrap_create_classobject(ENV)
{
struct PupClass *class =
(struct PupClass *)pup_internal_class_allocate_instance(env, NULL);
pup_internal_class_init(env, class, NULL, NULL, "Object",
&pup_object_allocate_instance,
&pup_object_destroy_instance,
&pup_object_gc_copy_instance);
return class;
}
void obj_init(struct PupObject *obj, struct PupClass *type)
{
obj->type = type;
obj->attr_list_head = NULL;
// would make sense to initialise gc_mark here too, however heap.c
// should have already done it
}
METH_IMPL(pup_object_allocate)
{
return pup_class_allocate_instance(env, (struct PupClass *)target);
}
struct PupObject *pup_object_allocate_instance(ENV, struct PupClass *type)
{
struct PupObject *obj = (struct PupObject *)pup_alloc_obj(env, sizeof(struct PupObject));
obj_init(obj, type);
return obj;
}
void pup_object_destroy_instance(struct PupObject *obj)
{
// nothing do do
}
static void attr_init(struct PupAttributeListEntry *attr,
const int sym,
struct PupObject *val,
struct PupAttributeListEntry *next)
{
attr->name_sym = sym;
attr->value = val;
attr->next = next;
}
static struct PupAttributeListEntry *copy_attr(
ENV,
const struct PupAttributeListEntry *attr
) {
struct PupAttributeListEntry *new_attr
= pup_env_alloc_attr_for_gc_copy(env, sizeof(struct PupAttributeListEntry));
attr_init(new_attr, attr->name_sym, attr->value, NULL);
return new_attr;
}
struct PupObject *pup_object_gc_copy_instance(ENV, const struct PupObject *obj)
{
struct PupObject *new_obj
= pup_env_alloc_obj_for_gc_copy(env, sizeof(struct PupObject));
memcpy(new_obj, obj, sizeof(struct PupObject));
// TODO: update references in the copy if we already know that
// referenced region was already moved?
// copy attributes of the object too,
struct PupAttributeListEntry *attr = obj->attr_list_head;
struct PupAttributeListEntry **target = &new_obj->attr_list_head;
while (attr) {
*target = copy_attr(env, attr);
target = &(*target)->next;
attr = attr->next;
}
return new_obj;
}
struct PupObject *pup_create_object(ENV, struct PupClass *type)
{
ABORTF_ON(!type, "'type' must not be null");
return pup_object_allocate(env, (struct PupObject *)type, 0, NULL);
}
void pup_object_destroy(struct PupObject *obj)
{
pup_class_destroy_instance(obj->type, obj);
/*
struct PupAttributeListEntry *attr = obj->attr_list_head;
while (attr) {
struct PupAttributeListEntry *tmp = attr;
attr = attr->next;
free(tmp);
}
*/
}
/*
void pup_object_free(struct PupObject *obj)
{
pup_object_destroy(obj);
free(obj);
}
*/
/*
* Default implementation for Object#initialize
*/
METH_IMPL(pup_object_initialize)
{
// TODO: return nil
return NULL;
}
const char *pup_object_type_name(const struct PupObject *obj)
{
if (!obj) {
return "<NULL Object ref>";
}
return pup_type_name(obj->type);
}
/*
* Caller must free() the result
*/
static char *sym_name(ENV, const int sym)
{
const char *str = pup_env_sym_to_str(env, sym);
if (str) {
return strdup(str);
}
char buf[256];
snprintf(buf, sizeof(buf), "sym:%d", sym);
return strdup(buf);
}
struct PupObject *pup_invoke(ENV, struct PupObject *target, const long name_sym,
const long argc, struct PupObject **argv)
{
ABORTF_ON(!target, "NULL target invoking `%s'", pup_env_sym_to_str(env, name_sym));
struct PupClass *class = target->type;
ABORTF_ON(!class, "NULL class invoking `%s' on object %p", pup_env_sym_to_str(env, name_sym), target);
PupMethod *method = find_method_in_classes(class, name_sym);
if (!method) {
char *name = sym_name(env, name_sym);
char buf[256];
snprintf(buf, sizeof(buf), "undefined method `%s' for %s", name, pup_object_type_name(target));
free(name);
pup_raise_runtimeerror(env, buf);
}
return (*method)(env, target, argc, argv);
}
bool pup_object_instanceof(const struct PupObject *obj,
const struct PupClass *class)
{
return obj->type == class;
}
bool pup_object_kindof(const struct PupObject *obj,
const struct PupClass *class)
{
return pup_is_descendant_or_same(class, obj->type);
}
static struct PupAttributeListEntry *find_attr(const struct PupObject *obj,
const int sym)
{
struct PupAttributeListEntry *attr = obj->attr_list_head;
while (attr) {
if (attr->name_sym == sym) {
return attr;
}
attr = attr->next;
}
return NULL;
}
static struct PupAttributeListEntry *create_attr(ENV, const int sym,
struct PupObject *val,
struct PupAttributeListEntry *next)
{
struct PupAttributeListEntry *attr
= pup_alloc_attr(env, sizeof(struct PupAttributeListEntry));
attr_init(attr, sym, val, next);
return attr;
}
void pup_iv_set(ENV, struct PupObject *obj,
const int sym, struct PupObject *val)
{
struct PupAttributeListEntry *attr = find_attr(obj, sym);
// TODO: what to do about these race conditions?
if (attr) {
attr->value = val;
} else {
obj->attr_list_head =
create_attr(env, sym, val, obj->attr_list_head);
}
}
struct PupObject *pup_iv_get(struct PupObject *obj, const int sym)
{
struct PupAttributeListEntry *attr = find_attr(obj, sym);
if (attr) {
return attr->value;
}
return NULL; /* TODO nil */
}
void pup_object_each_ref(struct PupObject *obj,
void (*visitor)(struct PupObject **, void *),
void *data)
{
// FIXME: must defer to a per-type function that can e.g. handle
// superclass field if obj is a Class etc.
visitor((struct PupObject **)&obj->type, data);
struct PupAttributeListEntry *attr = obj->attr_list_head;
while (attr) {
visitor(&attr->value, data);
attr = attr->next;
}
}
bool pup_object_gc_mark(struct PupObject *obj, int mark_value)
{
if (obj->gc_mark != mark_value) {
obj->gc_mark = mark_value;
return true;
}
return false;
}
void pup_object_gc_mark_unconditionally(struct PupObject *obj, int mark_value)
{
obj->gc_mark = mark_value;
}
static void copy_live_object(struct PupObject *obj, struct PupGCState *state)
{
void *alloc_obj_for_copy();
}
void pup_object_gc_collect(struct PupObject *obj, struct PupGCState *state)
{
if (pup_gc_is_live_mark(state, obj->gc_mark)) {
pup_gc_inc_live_count(state);
copy_live_object(obj, state);
} else {
pup_gc_inc_garbage_count(state);
}
}
void pup_object_attr_gc_collect(void *attr, struct PupGCState *state)
{
fprintf(stderr, "GC: attr %p\n", attr);
}
/*
* Returns obj, if obj is a Class, or obj->type otherwise.
* Used when we want to make use of 'self' without caring if we are in class
* or instance context.
*/
struct PupClass *pup_class_context_from(ENV, struct PupObject *obj)
{
if (pup_is_class_instance(env, obj)) {
return (struct PupClass *)obj;
}
return obj->type;
}
static void pup_default_obj_cstr(const struct PupObject *obj,
char *buf,
const size_t buf_size)
{
snprintf(buf, buf_size, "<%s:%p>",
pup_object_type_name(obj), obj);
}
METH_IMPL(pup_object_to_s)
{
char buf[1024];
pup_default_obj_cstr(target, buf, sizeof(buf));
return pup_string_new_cstr(env, buf);
}
char *pup_stringify(ENV, struct PupObject *obj)
{
if (pup_is_string(env, obj)) {
return strdup(pup_string_value_unsafe(obj));
}
if (pup_instanceof_exception(env, obj)) {
const char *msg = exception_text(env, obj);
if (msg) {
return strdup(msg);
}
}
char buf[1024];
pup_default_obj_cstr(obj, buf, sizeof(buf));
return strdup(buf);
}
METH_IMPL(pup_puts)
{
pup_arity_check(env, 1, argc);
ABORT_ON(!argv, "puts() argv is NULL!");
char *str = pup_stringify(env, argv[0]);
puts(str);
free(str);
// TODO return nil
return NULL;
}
METH_IMPL(pup_object_op_equals)
{
pup_arity_check(env, 1, argc);
struct PupObject *rhs = argv[0];
return target == rhs ? pup_env_get_trueinstance(env)
: pup_env_get_falseinstance(env);
}
void pup_object_class_init(ENV, struct PupClass *class_obj)
{
pup_define_method(class_obj,
pup_env_str_to_sym(env, "initialize"),
pup_object_initialize);
pup_define_method(class_obj,
pup_env_str_to_sym(env, "raise"),
pup_object_raise);
pup_define_method(class_obj,
pup_env_str_to_sym(env, "puts"),
pup_puts);
pup_define_method(class_obj,
pup_env_str_to_sym(env, "=="),
pup_object_op_equals);
}