-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathobject.cpp
203 lines (175 loc) · 4.58 KB
/
object.cpp
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
#include "object.h"
#include "memory.h"
#include "table.h"
#include "value.h"
#include "vm.h"
#include <cstdio>
#include <cstring>
// clang-format off
#define ALLOCATE_OBJ(type, objectType) \
(type*)allocateObject(sizeof(type), objectType)
// clang-format on
static Obj*
allocateObject(size_t size, ObjType type) {
Obj* object = (Obj*)reallocate(NULL, 0, size);
object->type = type;
object->isMarked = 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::
Obj(const ObjType type)
: type{type}, isMarked{false} {
this->next = vm.objects;
vm.objects = this;
}
ObjFunction::
ObjFunction()
: Obj{ObjType::OBJ_FUNCTION}, arity{0}, upvalueCount{0}, name{nullptr} {}
void
ObjFunction::gcMark() {
markObject((Obj*)this->name);
this->chunk.constants.gcMark();
}
void*
ObjFunction::operator new(size_t size) {
return reallocate(nullptr, 0, size);
}
void
ObjFunction::operator delete(void* ptr) {
reallocate(ptr, sizeof(ObjFunction), 0);
}
ObjBoundMethod*
newBoundMethod(Value receiver, ObjClosure* method) {
ObjBoundMethod* bound = ALLOCATE_OBJ(ObjBoundMethod, ObjType::OBJ_BOUND_METHOD);
bound->receiver = receiver;
bound->method = method;
return bound;
}
ObjClass*
newClass(ObjString* name) {
ObjClass* klass = ALLOCATE_OBJ(ObjClass, ObjType::OBJ_CLASS);
klass->name = name;
initTable(&(klass->methods));
return klass;
}
ObjClosure*
newClosure(ObjFunction* function) {
ObjUpvalue** upvalues = ALLOCATE(ObjUpvalue*, function->upvalueCount);
for (int i = 0; i < function->upvalueCount; i++) {
upvalues[i] = nullptr;
}
ObjClosure* closure = ALLOCATE_OBJ(ObjClosure, ObjType::OBJ_CLOSURE);
closure->function = function;
closure->upvalues = upvalues;
closure->upvalueCount = function->upvalueCount;
return closure;
}
ObjFunction*
newFunction() {
return new ObjFunction{};
}
ObjInstance*
newInstance(ObjClass* klass) {
ObjInstance* instance = ALLOCATE_OBJ(ObjInstance, ObjType::OBJ_INSTANCE);
instance->klass = klass;
initTable(&(instance->fields));
return instance;
}
ObjNative*
newNative(NativeFn function) {
ObjNative* native = ALLOCATE_OBJ(ObjNative, ObjType::OBJ_NATIVE);
native->function = function;
return native;
}
static ObjString*
allocateString(char* chars, int length, uint32_t hash) {
ObjString* string = ALLOCATE_OBJ(ObjString, ObjType::OBJ_STRING);
string->length = length;
string->chars = chars;
string->hash = hash;
push(OBJ_VAL(string));
tableSet(&(vm.strings), string, NIL_VAL);
pop();
return string;
}
static uint32_t
hashString(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;
}
ObjString*
takeString(char* chars, int length) {
uint32_t hash = hashString(chars, length);
ObjString* interned = tableFindString(&(vm.strings), chars, length, hash);
if (interned != nullptr) {
FREE_ARRAY(char, chars, length + 1);
return interned;
}
return allocateString(chars, length, hash);
}
ObjString*
copyString(const char* chars, int length) {
uint32_t hash = hashString(chars, length);
ObjString* interned = tableFindString(&(vm.strings), chars, length, hash);
if (interned != nullptr) {
return interned;
}
char* heapChars = ALLOCATE(char, length + 1);
memcpy(heapChars, chars, length);
heapChars[length] = '\0';
return allocateString(heapChars, length, hash);
}
static void
printFunction(ObjFunction* function) {
if (function->name == nullptr) {
printf("<script>");
return;
}
printf("<fn %s>", function->name->chars);
}
ObjUpvalue*
newUpvalue(Value* slot) {
ObjUpvalue* upvalue = ALLOCATE_OBJ(ObjUpvalue, ObjType::OBJ_UPVALUE);
upvalue->closed = NIL_VAL;
upvalue->location = slot;
upvalue->next = nullptr;
return upvalue;
}
void
printObject(Value value) {
switch (OBJ_TYPE(value)) {
case ObjType::OBJ_BOUND_METHOD:
printFunction(AS_BOUND_METHOD(value)->method->function);
break;
case ObjType::OBJ_CLASS:
printf("%s", AS_CLASS(value)->name->chars);
break;
case ObjType::OBJ_CLOSURE:
printFunction(AS_CLOSURE(value)->function);
break;
case ObjType::OBJ_FUNCTION:
printFunction(AS_FUNCTION(value));
break;
case ObjType::OBJ_INSTANCE:
printf("%s instance", AS_INSTANCE(value)->klass->name->chars);
break;
case ObjType::OBJ_NATIVE:
printf("<native fn>");
break;
case ObjType::OBJ_STRING:
printf("%s", AS_CSTRING(value));
break;
case ObjType::OBJ_UPVALUE:
printf("upvalue");
break;
}
}