-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathobject.c
executable file
·177 lines (155 loc) · 4.66 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
#include <stdio.h>
#include <string.h>
#include "memory.h"
#include "object.h"
#include "table.h"
#include "value.h"
#include "vm.h"
#define ALLOCATE_OBJ(type, object_type) \
(type*)allocate_object(sizeof(type), object_type)
static object_t* allocate_object(size_t size, obj_type_t type)
{
object_t* object = (object_t*)reallocate(NULL, 0, size);
object->type = type;
object->is_marked = false;
object->next = vm.objects;
vm.objects = object;
#ifdef DEBUG_LOG_GC
printf("%p allocate %zu for %d\n", (void*)object, size, type);
#endif
return object;
}
obj_bound_method_t* new_bound_method(value_t receiver,
obj_closure_t* method) {
obj_bound_method_t* bound = ALLOCATE_OBJ(obj_bound_method_t,
OBJ_BOUND_METHOD);
bound->receiver = receiver;
bound->method = method;
return bound;
}
obj_class_t* new_class(obj_string_t* name)
{
obj_class_t* klass = ALLOCATE_OBJ(obj_class_t, OBJ_CLASS);
klass->name = name;
init_table(&klass->methods);
return klass;
}
obj_closure_t* new_closure(obj_function_t* function)
{
obj_upvalue_t** upvalues = ALLOCATE(obj_upvalue_t*,
function->upvalue_count);
for (int i = 0; i < function->upvalue_count; i++) {
upvalues[i] = NULL;
}
obj_closure_t* closure = ALLOCATE_OBJ(obj_closure_t, OBJ_CLOSURE);
closure->function = function;
closure->upvalues = upvalues;
closure->upvalue_count = function->upvalue_count;
return closure;
}
obj_function_t* new_function()
{
obj_function_t* function = ALLOCATE_OBJ(obj_function_t, OBJ_FUNCTION);
function->arity = 0;
function->upvalue_count = 0;
function->name = NULL;
init_chunk(&function->chunk);
return function;
}
obj_instance_t* new_instance(obj_class_t* klass) {
obj_instance_t* instance = ALLOCATE_OBJ(obj_instance_t, OBJ_INSTANCE);
instance->klass = klass;
init_table(&instance->fields);
return instance;
}
obj_native_fn_t* new_native(native_fn_t function) {
obj_native_fn_t* native = ALLOCATE_OBJ(obj_native_fn_t, OBJ_NATIVE);
native->function = function;
return native;
}
static uint32_t hash_string(const char* key, int length)
{
uint32_t hash = 2166136261u;
for (int i = 0; i < length; i++) {
hash ^= (uint8_t)key[i];
hash *= 16777619;
}
return hash;
}
static obj_string_t* allocate_string(char* chars, int length, uint32_t hash)
{
obj_string_t* string = ALLOCATE_OBJ(obj_string_t, OBJ_STRING);
string->length = length;
string->chars = chars;
string->hash = hash;
push(OBJ_VAL(string));
table_set(&vm.strings, string, NIL_VAL);
pop();
return string;
}
obj_string_t* take_string(char* chars, int length) {
uint32_t hash = hash_string(chars, length);
obj_string_t* interned = table_find_string(&vm.strings, chars,
length, hash);
if (interned != NULL) {
FREE_ARRAY(char, chars, length + 1);
return interned;
}
return allocate_string(chars, length, hash);
}
obj_string_t* copy_string(const char* chars, int length)
{
uint32_t hash = hash_string(chars, length);
obj_string_t* interned = table_find_string(&vm.strings, chars,
length, hash);
if (interned != NULL) return interned;
char* heap_chars = ALLOCATE(char, length + 1);
memcpy(heap_chars, chars, length);
heap_chars[length] = '\0';
return allocate_string(heap_chars, length, hash);
}
obj_upvalue_t* new_upvalue(value_t* slot) {
obj_upvalue_t* upvalue = ALLOCATE_OBJ(obj_upvalue_t, OBJ_UPVALUE);
upvalue->closed = NIL_VAL;
upvalue->location = slot;
upvalue->next = NULL;
return upvalue;
}
static void print_function(obj_function_t* function)
{
if (function->name == NULL) {
printf("<script>");
return;
}
printf("<fn %s>", function->name->chars);
}
void print_object(value_t value)
{
switch (OBJ_TYPE(value)) {
case OBJ_BOUND_METHOD:
print_function(AS_BOUND_METHOD(value)->method->function);
break;
case OBJ_CLASS:
printf("%s", AS_CLASS(value)->name->chars);
break;
case OBJ_CLOSURE:
print_function(AS_CLOSURE(value)->function);
break;
case OBJ_FUNCTION:
print_function(AS_FUNCTION(value));
break;
case OBJ_INSTANCE:
printf("%s instance",
AS_INSTANCE(value)->klass->name->chars);
break;
case OBJ_NATIVE:
printf("<native fn>");
break;
case OBJ_STRING:
printf("%s", AS_CSTRING(value));
break;
case OBJ_UPVALUE:
printf("upvalue");
break;
}
}