-
Notifications
You must be signed in to change notification settings - Fork 224
/
gravity_opt_json.c
200 lines (161 loc) · 6.63 KB
/
gravity_opt_json.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
//
// gravity_opt_json.c
// gravity
//
// Created by Marco Bambini on 20/08/2019.
// Copyright © 2019 CreoLabs. All rights reserved.
//
#include <inttypes.h>
#include "gravity_vm.h"
#include "gravity_core.h"
#include "gravity_hash.h"
#include "gravity_json.h"
#include "gravity_utils.h"
#include "gravity_macros.h"
#include "gravity_opcodes.h"
#include "gravity_vmmacros.h"
#include "gravity_opt_json.h"
#define GRAVITY_JSON_STRINGIFY_NAME "stringify"
#define GRAVITY_JSON_PARSE_NAME "parse"
#define STATIC_BUFFER_SIZE 8192
static gravity_class_t *gravity_class_json = NULL;
static uint32_t refcount = 0;
// MARK: - Implementation -
static bool JSON_stringify (gravity_vm *vm, gravity_value_t *args, uint16_t nargs, uint32_t rindex) {
if (nargs < 2) RETURN_VALUE(VALUE_FROM_NULL, rindex);
// extract value
gravity_value_t value = GET_VALUE(1);
// special case for string because it can be huge (and must be quoted)
if (VALUE_ISA_STRING(value)) {
const int nchars = 5;
const char *v = VALUE_AS_STRING(value)->s;
size_t vlen = VALUE_AS_STRING(value)->len;
// string must be quoted
if (vlen < 4096-nchars) {
char vbuffer2[4096];
vlen = snprintf(vbuffer2, sizeof(vbuffer2), "\"%s\"", v);
RETURN_VALUE(VALUE_FROM_STRING(vm, vbuffer2, (uint32_t)vlen), rindex);
} else {
char *vbuffer2 = mem_alloc(NULL, vlen + nchars);
vlen = snprintf(vbuffer2, vlen + nchars, "\"%s\"", v);
RETURN_VALUE(VALUE_FROM_OBJECT(gravity_string_new(vm, vbuffer2, (uint32_t)vlen, 0)), rindex);
}
}
// primitive cases supported by JSON (true, false, null, number)
char vbuffer[512];
const char *v = NULL;
if (VALUE_ISA_NULL(value) || (VALUE_ISA_UNDEFINED(value))) v = "null";
else if (VALUE_ISA_FLOAT(value)) {snprintf(vbuffer, sizeof(vbuffer), "%g", value.f); v = vbuffer;}
else if (VALUE_ISA_BOOL(value)) v = (value.n) ? "true" : "false";
else if (VALUE_ISA_INT(value)) {
#if GRAVITY_ENABLE_INT64
snprintf(vbuffer, sizeof(vbuffer), "%" PRId64 "", value.n);
#else
snprintf(vbuffer, sizeof(vbuffer), "%d", value.n);
#endif
v = vbuffer;
}
if (v) RETURN_VALUE(VALUE_FROM_CSTRING(vm, v), rindex);
// more complex object case (list, map, class, closure, instance/object)
json_t *json = json_new();
json_set_option(json, json_opt_no_maptype);
json_set_option(json, json_opt_no_undef);
json_set_option(json, json_opt_prettify);
gravity_value_serialize(NULL, value, json);
size_t len = 0;
const char *jbuffer = json_buffer(json, &len);
const char *buffer = string_ndup(jbuffer, len);
gravity_string_t *s = gravity_string_new(vm, (char *)buffer, (uint32_t)len, 0);
json_free(json);
RETURN_VALUE(VALUE_FROM_OBJECT(s), rindex);
}
static gravity_value_t JSON_value (gravity_vm *vm, json_value *json) {
switch (json->type) {
case json_none:
case json_null:
return VALUE_FROM_NULL;
case json_object: {
gravity_object_t *obj = gravity_object_deserialize(vm, json);
gravity_value_t objv = (obj) ? VALUE_FROM_OBJECT(obj) : VALUE_FROM_NULL;
return objv;
}
case json_array: {
unsigned int length = json->u.array.length;
gravity_list_t *list = gravity_list_new(vm, length);
for (unsigned int i = 0; i < length; ++i) {
gravity_value_t value = JSON_value(vm, json->u.array.values[i]);
marray_push(gravity_value_t, list->array, value);
}
return VALUE_FROM_OBJECT(list);
}
case json_integer:
return VALUE_FROM_INT(json->u.integer);
case json_double:
return VALUE_FROM_FLOAT(json->u.dbl);
case json_string:
return VALUE_FROM_STRING(vm, json->u.string.ptr, json->u.string.length);
case json_boolean:
return VALUE_FROM_BOOL(json->u.boolean);
}
return VALUE_FROM_NULL;
}
static bool JSON_parse (gravity_vm *vm, gravity_value_t *args, uint16_t nargs, uint32_t rindex) {
if (nargs < 2) RETURN_VALUE(VALUE_FROM_NULL, rindex);
// value to parse
gravity_value_t value = GET_VALUE(1);
if (!VALUE_ISA_STRING(value)) RETURN_VALUE(VALUE_FROM_NULL, rindex);
gravity_string_t *string = VALUE_AS_STRING(value);
json_value *json = json_parse(string->s, string->len);
if (!json) RETURN_VALUE(VALUE_FROM_NULL, rindex);
RETURN_VALUE(JSON_value(vm, json), rindex);
}
//static bool JSON_begin_object (gravity_vm *vm, gravity_value_t *args, uint16_t nargs, uint32_t rindex) {
//
// return false;
//}
//
//static bool json_end_object (gravity_vm *vm, gravity_value_t *args, uint16_t nargs, uint32_t rindex) {
//
//}
//
//static bool json_begin_array (gravity_vm *vm, gravity_value_t *args, uint16_t nargs, uint32_t rindex) {
//
//}
//
//static bool json_end_array (gravity_vm *vm, gravity_value_t *args, uint16_t nargs, uint32_t rindex) {
//
//}
//
//static bool json_add_object (gravity_vm *vm, gravity_value_t *args, uint16_t nargs, uint32_t rindex) {
//
//}
// MARK: - Internals -
static void create_optional_class (void) {
gravity_class_json = gravity_class_new_pair(NULL, GRAVITY_CLASS_JSON_NAME, NULL, 0, 0);
gravity_class_t *json_meta = gravity_class_get_meta(gravity_class_json);
//gravity_class_bind(json_meta, GRAVITY_INTERNAL_EXEC_NAME, NEW_CLOSURE_VALUE(JSON_exec));
gravity_class_bind(json_meta, GRAVITY_JSON_STRINGIFY_NAME, NEW_CLOSURE_VALUE(JSON_stringify));
gravity_class_bind(json_meta, GRAVITY_JSON_PARSE_NAME, NEW_CLOSURE_VALUE(JSON_parse));
//gravity_class_bind(gravity_class_json, "begin", NEW_CLOSURE_VALUE(JSON_begin_object));
SETMETA_INITED(gravity_class_json);
}
// MARK: - Commons -
bool gravity_isjson_class (gravity_class_t *c) {
return (c == gravity_class_json);
}
const char *gravity_json_name (void) {
return GRAVITY_CLASS_JSON_NAME;
}
void gravity_json_register (gravity_vm *vm) {
if (!gravity_class_json) create_optional_class();
++refcount;
if (!vm || gravity_vm_ismini(vm)) return;
gravity_vm_setvalue(vm, GRAVITY_CLASS_JSON_NAME, VALUE_FROM_OBJECT(gravity_class_json));
}
void gravity_json_free (void) {
if (!gravity_class_json) return;
if (--refcount) return;
gravity_class_free_core(NULL, gravity_class_get_meta(gravity_class_json));
gravity_class_free_core(NULL, gravity_class_json);
gravity_class_json = NULL;
}