-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathobject.h
More file actions
219 lines (181 loc) · 3.63 KB
/
object.h
File metadata and controls
219 lines (181 loc) · 3.63 KB
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
#ifndef clox_object_h
#define clox_object_h
#include <stdbool.h>
#include <stdint.h>
#include "chunk.h"
#include "table.h"
#include "value.h"
typedef enum
{
OBJ_BOUND_METHOD,
OBJ_CLASS,
OBJ_CLOSURE,
OBJ_FUNCTION,
OBJ_INSTANCE,
OBJ_NATIVE,
OBJ_STRING,
OBJ_UPVALUE,
} obj_type_t;
struct obj_t
{
obj_type_t type;
bool is_marked;
struct obj_t *next;
};
typedef struct
{
obj_t obj;
int arity;
int upvalue_count;
chunk_t chunk;
obj_string *name;
} obj_function;
typedef value_t (*native_fn)(int arg_count, value_t *args);
typedef struct
{
obj_t obj;
native_fn function;
} obj_native;
struct obj_string
{
obj_t obj;
int length;
char *chars;
uint32_t hash;
};
typedef struct obj_upvalue
{
obj_t obj;
value_t *location;
value_t closed;
struct obj_upvalue *next;
} obj_upvalue;
typedef struct
{
obj_t obj;
obj_function *function;
obj_upvalue **upvalues;
/* if a function gets GC'd before the closure we need to know how large the
* upvalues array is so we store a redundant count. How can the funciton
* get GC'd if a closure still has a reference to it? */
int upvalue_count;
} obj_closure;
typedef struct
{
obj_t obj;
obj_string *name;
table_t methods;
} obj_class;
typedef struct
{
obj_t obj;
obj_class *class;
table_t fields;
} obj_instance;
typedef struct
{
obj_t obj;
value_t receiver;
obj_closure *method;
} obj_bound_method;
obj_bound_method *
newbound_method(value_t receiver, obj_closure *method);
obj_class *
newclass(obj_string *name);
obj_closure *
newclosure(obj_function *function);
obj_function *
newfunction(void);
obj_instance *
newinstance(obj_class *class);
obj_native *
newnative(native_fn function);
obj_string *
take_string(char *chars, int length);
obj_string *
copy_string(const char *chars, int length);
obj_upvalue *
newupvalue(value_t *slot);
void
print_object(value_t value);
static inline obj_type_t
obj_type(value_t value)
{
return as_obj(value)->type;
}
static inline bool
is_bound_method(value_t value)
{
return is_obj(value) && as_obj(value)->type == OBJ_CLASS;
}
static inline bool
is_class(value_t value)
{
return is_obj(value) && as_obj(value)->type == OBJ_CLASS;
}
static inline bool
is_closure(value_t value)
{
return is_obj(value) && as_obj(value)->type == OBJ_CLOSURE;
}
static inline bool
is_function(value_t value)
{
return is_obj(value) && as_obj(value)->type == OBJ_FUNCTION;
}
static inline bool
is_instance(value_t value)
{
return is_obj(value) && as_obj(value)->type == OBJ_INSTANCE;
}
static inline bool
is_native(value_t value)
{
return is_obj(value) && as_obj(value)->type == OBJ_NATIVE;
}
static inline bool
is_string(value_t value)
{
return is_obj(value) && as_obj(value)->type == OBJ_STRING;
}
static inline obj_bound_method *
as_bound_method(value_t value)
{
return (obj_bound_method *)as_obj(value);
}
static inline obj_class *
as_class(value_t value)
{
return (obj_class *)as_obj(value);
}
static inline obj_closure *
as_closure(value_t value)
{
return (obj_closure *)as_obj(value);
}
static inline obj_function *
as_function(value_t value)
{
return (obj_function *)as_obj(value);
}
static inline obj_instance *
as_instance(value_t value)
{
return (obj_instance *)as_obj(value);
}
static inline native_fn
as_native(value_t value)
{
return ((obj_native *)as_obj(value))->function;
}
static inline obj_string *
as_string(value_t value)
{
return (obj_string *)as_obj(value);
}
static inline char *
as_cstring(value_t value)
{
return ((obj_string *)as_obj(value))->chars;
}
#endif /* clox_object_h */