-
Notifications
You must be signed in to change notification settings - Fork 0
/
agate-support.c
372 lines (286 loc) · 9.73 KB
/
agate-support.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
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
#include "agate-support.h"
#include <assert.h>
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define BUFFER_SIZE 1024
typedef struct {
char *data;
ptrdiff_t capacity;
ptrdiff_t size;
} AgateCharBuffer;
typedef void (*AgateGenericHandlerFunc)(void);
typedef struct {
char *unit_name;
AgateGenericHandlerFunc handler;
} AgateDictItem;
typedef struct {
AgateDictItem *data;
ptrdiff_t capacity;
ptrdiff_t size;
} AgateDict;
typedef struct {
char *include_paths[AGATE_SUPPORT_INCLUDE_PATH_MAX];
ptrdiff_t size;
} AgateUnitSupport;
typedef struct {
AgateReallocFunc reallocate;
void *user_data;
AgateUnitSupport unit_support;
AgateDict foreign_class_support;
AgateDict foreign_method_support;
} AgateSupport;
// realloc
static void *agateReallocDefault(void *ptr, ptrdiff_t size, void *user_data) {
if (size == 0) {
free(ptr);
return NULL;
}
void* next = realloc(ptr, size);
assert(next);
return next;
}
static void *agateReallocForward(void *ptr, ptrdiff_t size, void *user_data) {
AgateSupport *support = user_data;
return support->reallocate(ptr, size, support->user_data);
}
// duplicate
static char *agateDuplicate(const char *string, AgateSupport *support) {
size_t length = strlen(string);
char *duplicate = support->reallocate(NULL, length + 1, support->user_data);
assert(duplicate);
memcpy(duplicate, string, length);
duplicate[length] = '\0';
return duplicate;
}
// char buffer
static void agateCharBufferCreate(AgateCharBuffer *self) {
self->data = NULL;
self->capacity = 0;
self->size = 0;
}
static void agateCharBufferDestroy(AgateCharBuffer *self, AgateSupport *support) {
support->reallocate(self->data, 0, support->user_data);
self->data = NULL;
self->capacity = 0;
self->size = 0;
}
static void agateCharBufferReset(AgateCharBuffer *self) {
self->size = 0;
memset(self->data, 0, self->capacity);
}
static void agateCharBufferAppend(AgateCharBuffer *self, const char *buffer, size_t size, AgateSupport *support) {
if (size == 0) {
return;
}
ptrdiff_t needed = self->size + size + 1;
if (self->capacity < needed) {
while (self->capacity < needed) {
if (self->capacity == 0) {
self->capacity = 16;
} else {
self->capacity *= 2;
}
}
char *data = support->reallocate(self->data, self->capacity * sizeof(char), support->user_data);
assert(data);
self->data = data;
}
memcpy(self->data + self->size, buffer, size);
self->size += size;
self->data[self->size] = '\0';
}
// dict
static void agateDictCreate(AgateDict *self) {
self->data = NULL;
self->capacity = 0;
self->size = 0;
}
static void agateDictDestroy(AgateDict *self, AgateSupport *support) {
for (ptrdiff_t i = 0; i < self->size; ++i) {
support->reallocate(self->data[i].unit_name, 0, support->user_data);
}
support->reallocate(self->data, 0, support->user_data);
self->data = NULL;
self->capacity = 0;
self->size = 0;
}
static void agateDictAppend(AgateDict *self, const char *name, AgateGenericHandlerFunc handler, AgateSupport *support) {
ptrdiff_t needed = self->size + 1;
if (self->capacity < needed) {
while (self->capacity < needed) {
if (self->capacity == 0) {
self->capacity = 16;
} else {
self->capacity *= 2;
}
}
AgateDictItem *data = support->reallocate(self->data, self->capacity * sizeof(AgateDictItem), support->user_data);
assert(data);
self->data = data;
}
AgateDictItem *item = &self->data[self->size++];
item->unit_name = agateDuplicate(name, support);
item->handler = handler;
}
// unit handling
static void agateUnitSupportCreate(AgateUnitSupport *self) {
self->size = 0;
}
static void agateUnitSupportDestroy(AgateUnitSupport *self, AgateSupport *support) {
for (ptrdiff_t i = 0; i < self->size; ++i) {
support->reallocate(self->include_paths[i], 0, support->user_data);
self->include_paths[i] = NULL;
}
self->size = 0;
}
static const char *agateUnitLoadFile(const char *path, AgateSupport *support) {
FILE *file = fopen(path, "rb");
if (file == NULL) {
return NULL;
}
AgateCharBuffer content;
agateCharBufferCreate(&content);
char buffer[BUFFER_SIZE];
while (!feof(file)) {
size_t size = fread(buffer, sizeof(char), BUFFER_SIZE, file);
agateCharBufferAppend(&content, buffer, size, support);
}
fclose(file);
return content.data;
}
static const char *agateUnitLoad(const char *name, void *user_data) {
AgateSupport *support = user_data;
AgateUnitSupport *units = &support->unit_support;
AgateCharBuffer path;
agateCharBufferCreate(&path);
for (ptrdiff_t i = 0; i < units->size; ++i) {
agateCharBufferReset(&path);
const char *include_path = units->include_paths[i];
agateCharBufferAppend(&path, include_path, strlen(include_path), support);
agateCharBufferAppend(&path, "/", 1, support);
agateCharBufferAppend(&path, name, strlen(name), support);
agateCharBufferAppend(&path, ".agate", 6, support);
const char *source = agateUnitLoadFile(path.data, support);
if (source != NULL) {
agateCharBufferDestroy(&path, support);
return source;
}
}
agateCharBufferDestroy(&path, support);
return NULL;
}
static void agateUnitRelease(const char *source, void *user_data) {
AgateSupport *support = user_data;
support->reallocate((void *) source, 0, support->user_data);
}
bool agateExUnitAddIncludePath(AgateVM *vm, const char *path) {
AgateSupport *support = agateGetUserData(vm);
AgateUnitSupport *units = &support->unit_support;
if (units->size == AGATE_SUPPORT_INCLUDE_PATH_MAX) {
return false;
}
units->include_paths[units->size++] = agateDuplicate(path, support);
return true;
}
AgateUnitHandler agateExUnitHandler(AgateVM *vm, const char *name) {
AgateSupport *support = agateGetUserData(vm);
AgateUnitHandler handler = {
agateUnitLoad,
agateUnitRelease,
support
};
return handler;
}
const char *agateExUnitLoad(AgateVM *vm, const char *unit_name) {
const char *point = strrchr(unit_name, '.');
if (point != NULL && strcmp(point, ".agate") == 0) {
return agateUnitLoadFile(unit_name, agateGetUserData(vm));
}
return agateUnitLoad(unit_name, agateGetUserData(vm));
}
void agateExUnitRelease(AgateVM *vm, const char *unit_text) {
agateUnitRelease(unit_text, agateGetUserData(vm));
}
// foreign class
void agateExForeignClassAddHandler(AgateVM *vm, AgateForeignClassHandlerFunc func, const char *unit_name) {
AgateSupport *support = agateGetUserData(vm);
agateDictAppend(&support->foreign_class_support, unit_name, (AgateGenericHandlerFunc) func, support);
}
AgateForeignClassHandler agateExForeignClassHandler(AgateVM *vm, const char *unit_name, const char *class_name) {
AgateSupport *support = agateGetUserData(vm);
for (ptrdiff_t i = 0; i < support->foreign_class_support.size; ++i) {
AgateDictItem *item = &support->foreign_class_support.data[i];
if (strcmp(item->unit_name, unit_name) == 0) {
AgateForeignClassHandlerFunc func = (AgateForeignClassHandlerFunc) item->handler;
return func(vm, unit_name, class_name);
}
}
AgateForeignClassHandler handler = { NULL, NULL };
return handler;
}
// foreign method
void agateExForeignMethodAddHandler(AgateVM *vm, AgateForeignMethodHandlerFunc func, const char *unit_name) {
AgateSupport *support = agateGetUserData(vm);
agateDictAppend(&support->foreign_method_support, unit_name, (AgateGenericHandlerFunc) func, support);
}
AgateForeignMethodFunc agateExForeignMethodHandler(AgateVM *vm, const char *unit_name, const char *class_name, AgateForeignMethodKind kind, const char *signature) {
AgateSupport *support = agateGetUserData(vm);
for (ptrdiff_t i = 0; i < support->foreign_method_support.size; ++i) {
AgateDictItem *item = &support->foreign_method_support.data[i];
if (strcmp(item->unit_name, unit_name) == 0) {
AgateForeignMethodHandlerFunc func = (AgateForeignMethodHandlerFunc) item->handler;
return func(vm, unit_name, class_name, kind, signature);
}
}
return NULL;
}
// support
static void agateExSupportCreate(AgateSupport *self, AgateReallocFunc reallocate, void *user_data) {
self->reallocate = reallocate;
self->user_data = user_data;
agateUnitSupportCreate(&self->unit_support);
agateDictCreate(&self->foreign_class_support);
agateDictCreate(&self->foreign_method_support);
}
static void agateSupportDestroy(AgateSupport *self) {
agateDictDestroy(&self->foreign_method_support, self);
agateDictDestroy(&self->foreign_class_support, self);
agateUnitSupportDestroy(&self->unit_support, self);
}
// agate function replacements
AgateVM *agateExNewVM(const AgateConfig *config) {
assert(config);
AgateConfig local_config = *config;
AgateReallocFunc reallocate;
if (local_config.reallocate) {
reallocate = local_config.reallocate;
local_config.reallocate = agateReallocForward;
} else {
reallocate = agateReallocDefault;
}
AgateSupport *support = reallocate(NULL, sizeof(AgateSupport), local_config.user_data);
assert(support);
agateExSupportCreate(support, reallocate, local_config.user_data);
AgateVM *vm = agateNewVM(&local_config);
agateSetUserData(vm, support);
return vm;
}
void agateExDeleteVM(AgateVM *vm) {
AgateSupport *support = agateGetUserData(vm);
AgateReallocFunc reallocate = support->reallocate;
void *user_data = support->user_data;
// delete vm before support in case of forward allocator
agateDeleteVM(vm);
agateSupportDestroy(support);
reallocate(support, 0, user_data);
}
void *agateExGetUserData(AgateVM *vm) {
AgateSupport *support = agateGetUserData(vm);
return support->user_data;
}
void agateExSetUserData(AgateVM *vm, void *user_data) {
AgateSupport *support = agateGetUserData(vm);
support->user_data = user_data;
}