-
Notifications
You must be signed in to change notification settings - Fork 4
/
bobject.c
382 lines (334 loc) · 9.99 KB
/
bobject.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
373
374
375
376
377
378
379
380
381
382
#include <assert.h>
#include "bean.h"
#include "bobject.h"
#include "bregex.h"
#include "barray.h"
#include "bstring.h"
#include "berror.h"
typedef enum {
ORIGINAL,
DOUBLE_QUOTE_STRING,
DOUBLE_QUOTE_ALL
} PARSER_TYPE;
// Copy from https://github.com/cesanta/mjs/blob/master/mjs/src/mjs_json.c#L44
static const char *hex_digits = "0123456789abcdef";
static char *append_hex(char *buf, char *limit, uint8_t c) {
if (buf < limit) *buf++ = 'u';
if (buf < limit) *buf++ = '0';
if (buf < limit) *buf++ = '0';
if (buf < limit) *buf++ = hex_digits[(int) ((c >> 4) % 0xf)]; // height 4 bits
if (buf < limit) *buf++ = hex_digits[(int) (c & 0xf)]; // low 4 bits
return buf;
}
// Copy from https://github.com/cesanta/mjs/blob/master/mjs/src/mjs_json.c#L54
/*
* Appends quoted s to buf. Any double quote contained in s will be escaped.
* Returns the number of characters that would have been added,
* like snprintf.
* If size is zero it doesn't output anything but keeps counting.
*/
static int snquote(char *buf, size_t size, const char *s, size_t len) {
char *limit = buf + size;
const char *end;
/*
* String single character escape sequence:
* http://www.ecma-international.org/ecma-262/6.0/index.html#table-34
*
* 0x8 -> \b
* 0x9 -> \t
* 0xa -> \n
* 0xb -> \v
* 0xc -> \f
* 0xd -> \r
*/
const char *specials = "btnvfr";
size_t i = 0;
i++;
if (buf < limit) *buf++ = '"';
for (end = s + len; s < end; s++) {
if (*s == '"' || *s == '\\') {
i++;
if (buf < limit) *buf++ = '\\';
} else if (*s >= '\b' && *s <= '\r') {
i += 2;
if (buf < limit) *buf++ = '\\';
if (buf < limit) *buf++ = specials[*s - '\b'];
continue;
} else if ((unsigned char) *s < '\b' || (*s > '\r' && *s < ' ')) {
i += 6 /* \uXXXX */;
if (buf < limit) *buf++ = '\\';
buf = append_hex(buf, limit, (uint8_t) *s);
continue;
}
i++;
if (buf < limit) *buf++ = *s;
}
i++;
if (buf < limit) *buf++ = '"';
if (buf < limit) {
*buf = '\0';
} else if (size != 0) {
/*
* There is no room for the NULL char, but the size wasn't zero, so we can
* safely put NULL in the previous byte
*/
*(buf - 1) = '\0';
}
return i;
}
bool check_equal(TValue * v1, TValue * v2) {
if (rawtt(v1) != rawtt(v2)) return false;
if (ttisnumber(v1) && ttisnumber(v2)) {
return nvalue(v1) == nvalue(v2);
} else if (ttisstring(v1) && ttisstring(v2)) {
return beanS_equal(svalue(v1), svalue(v2));
} else if (v1 == v2) {
return true;
}
return false;
}
static char * hash_inspect(bean_State * B, Hash * hash, bool wrapKey) {
uint32_t total = 0;
char * colon = ": ";
char * comma = ", ";
uint32_t colon_Len = strlen(colon);
uint32_t comma_Len = strlen(comma);
if (!hash->count) return "{}";
for (uint32_t i = 0; i < hash->capacity; i++) {
if (!hash->entries[i]) continue;
Entry * e = hash->entries[i];
while (e) {
TValue * key = wrapKey ? tvalue_inspect_all(B, e->key) : tvalue_inspect(B, e->key);
TValue * value = tvalue_inspect_all(B, e->value);
total += tslen(svalue(key)) + tslen(svalue(value)) + colon_Len + comma_Len;
e = e -> next;
}
}
total = total - comma_Len;
char * resStr = malloc(sizeof(char) * total + 2 + 1);
uint32_t index = 0;
resStr[index++] = '{';
for (uint32_t i = 0; i < hash->capacity; i++) {
if (!hash->entries[i]) continue;
Entry * e = hash->entries[i];
while (e) {
TValue * key = wrapKey ? tvalue_inspect_all(B, e->key) : tvalue_inspect(B, e->key);
TValue * value = tvalue_inspect_all(B, e->value);
char * key_Str = getstr(svalue(key));
char * value_Str = getstr(svalue(value));
for (uint32_t j = 0; j < strlen(key_Str); j++) {
resStr[index++] = key_Str[j];
}
for (uint32_t k = 0; k < colon_Len; k++) {
resStr[index++] = colon[k];
}
for (uint32_t l = 0; l < strlen(value_Str); l++) {
resStr[index++] = value_Str[l];
}
for (uint32_t m = 0; m < comma_Len; m++) {
resStr[index++] = comma[m];
}
e = e -> next;
}
}
index -= comma_Len;
resStr[index++] = '}';
resStr[index] = 0;
return resStr;
}
static char * array_inspect(bean_State * B, Array * array) {
uint32_t total = 0;
char * delimiter = ", ";
uint32_t del_Len = strlen(delimiter);
if (!array->count) return "[]";
for (uint32_t i = 0; i < array->count; i++) {
TValue * elm = array->entries[i];
TString * ts = svalue(tvalue_inspect_pure(B, elm));
total += tslen(ts);
total += del_Len;
}
total = total - del_Len;
char * resStr = malloc(sizeof(char) * total + 2 + 1);
uint32_t index = 0;
resStr[index++] = '[';
for (uint32_t i = 0; i < array->count; i++) {
TValue * elm = array->entries[i];
TString * ts = svalue(tvalue_inspect_pure(B, elm));
char * cStr = getstr(ts);
for (uint32_t j = 0; j < tslen(ts); j++) {
resStr[index++] = cStr[j];
}
for (uint32_t k = 0; k < del_Len; k++) {
resStr[index++] = delimiter[k];
}
}
index = index - del_Len;
resStr[index++] = ']';
resStr[index] = 0;
return resStr;
}
static TValue * inspect(bean_State * B UNUSED, TValue * value, PARSER_TYPE mode) {
TValue * inspect = TV_MALLOC;
char * string;
switch(value -> tt_) {
case BEAN_TNIL: {
string = "nil";
break;
}
case BEAN_TBOOLEAN: {
bool b = bvalue(value);
string = b ? "true" : "false";
break;
}
case BEAN_TNUMFLT:
string = malloc(sizeof(char) * 100);
sprintf(string, "%f", nvalue(value));
break;
case BEAN_TNUMINT:
string = malloc(sizeof(char) * 100);
sprintf(string, "%ld", (long)nvalue(value));
break;
case BEAN_TSTRING: {
TString * ts = svalue(value);
uint32_t len = tslen(ts);
if (mode >= DOUBLE_QUOTE_STRING) {
/* Auto enlarge if space not enough */
size_t size = MAX_STRING_BUFFER;
size_t i = 0;
size_t result;
do {
size = size * (++i);
string = malloc(size);
result = snquote(string, size, getstr(ts), tslen(ts));
} while (result >= size);
} else {
string = malloc(sizeof(char) * len + 1);
sprintf(string, "%s", getstr(ts));
}
break;
}
case BEAN_TLIST: {
string = array_inspect(B, arrvalue(value));
break;
}
case BEAN_THASH: {
if (mode == DOUBLE_QUOTE_ALL) {
string = hash_inspect(B, hhvalue(value), true);
} else {
string = hash_inspect(B, hhvalue(value), false);
}
break;
}
case BEAN_TFUNCTION: {
Fn * f = fnvalue(value);
TString * ts = f->name;
uint32_t len = tslen(ts);
string = malloc(sizeof(char) * len + 1 + 11);
sprintf(string, "[Function %s]", getstr(ts));
break;
}
case BEAN_TTOOL: {
Tool * t = tlvalue(value);
string = t->getter ? "[Primitive getter]" : "[Primitive function]";
break;
}
case BEAN_TREGEX: {
Regex * rr = regexvalue(value);
string = malloc(sizeof(char) * strlen(rr->match) + 2);
sprintf(string, "/%s/", rr->match);
break;
}
case BEAN_TDATE: {
Date * dt = datevalue(value);
string = ctime(&(dt -> time));
string[strlen(string) - 1] = '\0'; // Remove the '\n' at the end
break;
}
default:
string = "invalid value";
break;
}
TString * ts = beanS_newlstr(B, string, strlen(string));
setsvalue(inspect, ts);
return inspect;
}
TValue * tvalue_inspect(bean_State * B UNUSED, TValue * value) {
return inspect(B, value, ORIGINAL);
}
TValue * tvalue_inspect_all(bean_State * B UNUSED, TValue * value) { // For generate json string
return inspect(B, value, DOUBLE_QUOTE_ALL);
}
TValue * tvalue_inspect_pure(bean_State * B UNUSED, TValue * value) {
return inspect(B, value, DOUBLE_QUOTE_STRING);
}
static TValue * primitive_print(bean_State * B UNUSED, TValue * this UNUSED, TValue * args, int argc) {
for (int i = 0; i < argc; i ++) {
TValue * string = tvalue_inspect(B, args+i);
printf("%s ", getstr(svalue(string)));
}
printf("\n");
return G(B)->nil;
}
static TValue * primitive_throw(bean_State * B UNUSED, TValue * this UNUSED, TValue * args, int argc) {
assert(argc >= 1);
TValue * string = tvalue_inspect(B, &args[0]);
runtime_error(B, "%s", getstr(svalue(string)));
return G(B)->nil;
}
static TValue * primitive_assert(bean_State * B UNUSED, TValue * this UNUSED, TValue * args, int argc) {
assert(argc >= 2);
TValue * v1 = tvalue_inspect(B, &args[0]);
TValue * v2 = tvalue_inspect(B, &args[1]);
if (!check_equal(v1, v2)) {
char * left = getstr(svalue(v1));
char * right = getstr(svalue(v2));
runtime_error(B, "%s not equal to %s\n", left, right);
}
return G(B)->nil;
}
void add_tool_by_name(bean_State * B, char * str, size_t len, primitive_Fn fn) {
Hash * variables = B -> l_G -> globalScope -> variables;
TValue * name = TV_MALLOC;
TString * ts = beanS_newlstr(B, str, len);
setsvalue(name, ts);
TValue * func = TV_MALLOC;
Tool * tool = initialize_tool_by_fn(fn, false);
settlvalue(func, tool);
hash_set(B, variables, name, func);
}
// Add some default tool functions
void add_tools(bean_State * B) {
add_tool_by_name(B, "print", 5, primitive_print);
add_tool_by_name(B, "throw", 5, primitive_throw);
add_tool_by_name(B, "assert", 6, primitive_assert);
}
TValue * type_statement(bean_State * B UNUSED, TValue * target) {
char * typeStr;
switch(ttype(target)) {
case BEAN_TNIL:
typeStr = "nil";
break;
case BEAN_TBOOLEAN:
typeStr = "boolean";
break;
case BEAN_TNUMBER:
typeStr = "number";
break;
case BEAN_TTOOL:
case BEAN_TFUNCTION:
typeStr = "function";
break;
case BEAN_TSTRING:
typeStr = "string";
break;
case BEAN_TLIST:
typeStr = "array";
break;
default:
typeStr = "hash";
}
TString * ts = beanS_newlstr(B, typeStr, strlen(typeStr));
TValue * typeVal = TV_MALLOC;
setsvalue(typeVal, ts);
return typeVal;
}