-
Notifications
You must be signed in to change notification settings - Fork 53
/
object.h
314 lines (257 loc) · 7.75 KB
/
object.h
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
#ifndef dictu_object_h
#define dictu_object_h
#include <stdint.h>
#include <stdio.h>
#include "../include/dictu_include.h"
#include "common.h"
#include "chunk.h"
#include "table.h"
#include "value.h"
#define OBJ_TYPE(value) (AS_OBJ(value)->type)
#define AS_MODULE(value) ((ObjModule*)AS_OBJ(value))
#define AS_BOUND_METHOD(value) ((ObjBoundMethod*)AS_OBJ(value))
#define AS_CLASS(value) ((ObjClass*)AS_OBJ(value))
#define AS_ENUM(value) ((ObjEnum*)AS_OBJ(value))
#define AS_CLOSURE(value) ((ObjClosure*)AS_OBJ(value))
#define AS_FUNCTION(value) ((ObjFunction*)AS_OBJ(value))
#define AS_INSTANCE(value) ((ObjInstance*)AS_OBJ(value))
#define AS_NATIVE(value) (((ObjNative*)AS_OBJ(value))->function)
#define AS_STRING(value) ((ObjString*)AS_OBJ(value))
#define AS_CSTRING(value) (((ObjString*)AS_OBJ(value))->chars)
#define AS_LIST(value) ((ObjList*)AS_OBJ(value))
#define AS_DICT(value) ((ObjDict*)AS_OBJ(value))
#define AS_SET(value) ((ObjSet*)AS_OBJ(value))
#define AS_FILE(value) ((ObjFile*)AS_OBJ(value))
#define AS_ABSTRACT(value) ((ObjAbstract*)AS_OBJ(value))
#define AS_RESULT(value) ((ObjResult*)AS_OBJ(value))
#define IS_MODULE(value) isObjType(value, OBJ_MODULE)
#define IS_BOUND_METHOD(value) isObjType(value, OBJ_BOUND_METHOD)
#define IS_CLASS(value) isObjType(value, OBJ_CLASS)
#define IS_DEFAULT_CLASS(value) isObjType(value, OBJ_CLASS) && AS_CLASS(value)->type == CLASS_DEFAULT
#define IS_TRAIT(value) isObjType(value, OBJ_CLASS) && AS_CLASS(value)->type == CLASS_TRAIT
#define IS_ENUM(value) isObjType(value, OBJ_ENUM)
#define IS_CLOSURE(value) isObjType(value, OBJ_CLOSURE)
#define IS_FUNCTION(value) isObjType(value, OBJ_FUNCTION)
#define IS_INSTANCE(value) isObjType(value, OBJ_INSTANCE)
#define IS_NATIVE(value) isObjType(value, OBJ_NATIVE)
#define IS_STRING(value) isObjType(value, OBJ_STRING)
#define IS_LIST(value) isObjType(value, OBJ_LIST)
#define IS_DICT(value) isObjType(value, OBJ_DICT)
#define IS_SET(value) isObjType(value, OBJ_SET)
#define IS_FILE(value) isObjType(value, OBJ_FILE)
#define IS_ABSTRACT(value) isObjType(value, OBJ_ABSTRACT)
#define IS_RESULT(value) isObjType(value, OBJ_RESULT)
typedef enum {
OBJ_MODULE,
OBJ_BOUND_METHOD,
OBJ_CLASS,
OBJ_ENUM,
OBJ_CLOSURE,
OBJ_FUNCTION,
OBJ_INSTANCE,
OBJ_NATIVE,
OBJ_STRING,
OBJ_LIST,
OBJ_DICT,
OBJ_SET,
OBJ_FILE,
OBJ_ABSTRACT,
OBJ_RESULT,
OBJ_UPVALUE
} ObjType;
typedef enum {
CLASS_DEFAULT,
CLASS_ABSTRACT,
CLASS_TRAIT
} ClassType;
typedef enum {
ACCESS_PUBLIC,
ACCESS_PRIVATE
} AccessLevel;
typedef enum {
TYPE_FUNCTION,
TYPE_ARROW_FUNCTION,
TYPE_INITIALIZER,
TYPE_METHOD,
TYPE_STATIC,
TYPE_ABSTRACT,
TYPE_TOP_LEVEL
} FunctionType;
struct sObj {
ObjType type;
bool isDark;
struct sObj *next;
};
typedef struct {
Obj obj;
ObjString* name;
ObjString* path;
Table values;
} ObjModule;
typedef struct {
Obj obj;
int isVariadic;
int arity;
int arityOptional;
int upvalueCount;
Chunk chunk;
ObjString *name;
FunctionType type;
AccessLevel accessLevel;
ObjModule* module;
int propertyCount;
int *propertyNames;
int *propertyIndexes;
int privatePropertyCount;
int *privatePropertyNames;
int *privatePropertyIndexes;
} ObjFunction;
typedef Value (*NativeFn)(DictuVM *vm, int argCount, Value *args);
typedef struct {
Obj obj;
NativeFn function;
} ObjNative;
struct sObjString {
Obj obj;
int length;
char *chars;
uint32_t hash;
int character_len;
};
struct sObjList {
Obj obj;
ValueArray values;
};
typedef struct {
Value key;
Value value;
} DictItem;
struct sObjDict {
Obj obj;
int count;
int activeCount;
int capacityMask;
DictItem *entries;
};
typedef struct {
Value value;
bool deleted;
} SetItem;
struct sObjSet {
Obj obj;
int count;
int capacityMask;
SetItem *entries;
};
struct sObjFile {
Obj obj;
FILE *file;
char *path;
char *openType;
};
typedef void (*AbstractFreeFn)(DictuVM *vm, ObjAbstract *abstract);
typedef void (*AbstractGrayFn)(DictuVM *vm, ObjAbstract *abstract);
typedef char* (*AbstractTypeFn)(ObjAbstract *abstract);
struct sObjAbstract {
Obj obj;
Table values;
void *data;
AbstractFreeFn func;
AbstractGrayFn grayFunc;
AbstractTypeFn type;
bool excludeSelf;
};
typedef enum {
SUCCESS,
ERR
} ResultStatus;
struct sObjResult {
Obj obj;
ResultStatus status;
Value value;
};
typedef struct sUpvalue {
Obj obj;
// Pointer to the variable this upvalue is referencing.
Value *value;
// If the upvalue is closed (i.e. the local variable it was pointing
// to has been popped off the stack) then the closed-over value is
// hoisted out of the stack into here. [value] is then be changed to
// point to this.
Value closed;
// Open upvalues are stored in a linked list. This points to the next
// one in that list.
struct sUpvalue *next;
} ObjUpvalue;
typedef struct {
Obj obj;
ObjFunction *function;
ObjUpvalue **upvalues;
int upvalueCount;
} ObjClosure;
typedef struct sObjClass {
Obj obj;
ObjString *name;
struct sObjClass *superclass;
Table publicMethods;
Table privateMethods;
Table abstractMethods;
Table variables;
Table constants;
ObjDict *classAnnotations;
ObjDict *methodAnnotations;
ObjDict *fieldAnnotations;
ClassType type;
} ObjClass;
typedef struct sObjEnum {
Obj obj;
ObjString *name;
Table values;
} ObjEnum;
typedef struct {
Obj obj;
ObjClass *klass;
Table privateAttributes;
Table publicAttributes;
} ObjInstance;
typedef struct {
Obj obj;
Value receiver;
ObjClosure *method;
} ObjBoundMethod;
ObjModule *newModule(DictuVM *vm, ObjString *name);
ObjBoundMethod *newBoundMethod(DictuVM *vm, Value receiver, ObjClosure *method);
ObjClass *newClass(DictuVM *vm, ObjString *name, ObjClass *superclass, ClassType type);
ObjEnum *newEnum(DictuVM *vm, ObjString *name);
ObjClosure *newClosure(DictuVM *vm, ObjFunction *function);
ObjFunction *newFunction(DictuVM *vm, ObjModule *module, FunctionType type, AccessLevel level);
ObjInstance *newInstance(DictuVM *vm, ObjClass *klass);
ObjNative *newNative(DictuVM *vm, NativeFn function);
ObjString *takeString(DictuVM *vm, char *chars, int length);
ObjString *takeStringWithLen(DictuVM *vm, char *chars, int length, int character_len);
ObjString *copyString(DictuVM *vm, const char *chars, int length);
ObjString *copyStringWithLen(DictuVM *vm, const char *chars, int length, int character_len);
ObjList *newList(DictuVM *vm);
ObjDict *newDict(DictuVM *vm);
ObjSet *newSet(DictuVM *vm);
ObjFile *newFile(DictuVM *vm);
ObjAbstract *newAbstract(DictuVM *vm, AbstractFreeFn func, AbstractTypeFn type);
ObjAbstract *newAbstractExcludeSelf(DictuVM *vm, AbstractFreeFn func, AbstractTypeFn type);
ObjResult *newResult(DictuVM *vm, ResultStatus status, Value value);
Value newResultSuccess(DictuVM *vm, Value value);
Value newResultError(DictuVM *vm, char *errorMsg);
ObjUpvalue *newUpvalue(DictuVM *vm, Value *slot);
char *setToString(Value value);
char *dictToString(Value value);
char *listToString(Value value);
char *classToString(Value value);
ObjDict *classToDict(DictuVM *vm, Value value);
char *instanceToString(Value value);
char *objectToString(Value value);
static inline bool isObjType(Value value, ObjType type) {
return IS_OBJ(value) && AS_OBJ(value)->type == type;
}
static inline ObjType getObjType(Value value) {
return AS_OBJ(value)->type;
}
#endif