@port-labs/jq-node-bindings 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.editorconfig +5 -0
- package/.jshintignore +1 -0
- package/.jshintrc +23 -0
- package/binding.gyp +56 -0
- package/configure +26 -0
- package/deps/jq/.gitattributes +2 -0
- package/deps/jq/.travis.yml +53 -0
- package/deps/jq/AUTHORS +73 -0
- package/deps/jq/COPYING +70 -0
- package/deps/jq/ChangeLog +1349 -0
- package/deps/jq/Makefile.am +198 -0
- package/deps/jq/NEWS +88 -0
- package/deps/jq/README.md +64 -0
- package/deps/jq/builtin.c +1684 -0
- package/deps/jq/builtin.h +10 -0
- package/deps/jq/bytecode.c +161 -0
- package/deps/jq/bytecode.h +92 -0
- package/deps/jq/compile-ios.sh +102 -0
- package/deps/jq/compile.c +1210 -0
- package/deps/jq/compile.h +101 -0
- package/deps/jq/config/m4/check-math-func.m4 +4 -0
- package/deps/jq/config/m4/find-func-no-libs.m4 +8 -0
- package/deps/jq/config/m4/find-func-no-libs2.m4 +62 -0
- package/deps/jq/config/m4/find-func.m4 +9 -0
- package/deps/jq/config/m4/misc.m4 +3 -0
- package/deps/jq/configure.ac +221 -0
- package/deps/jq/docs/Gemfile +7 -0
- package/deps/jq/docs/Gemfile.lock +63 -0
- package/deps/jq/docs/README.md +25 -0
- package/deps/jq/docs/Rakefile +145 -0
- package/deps/jq/docs/content/1.tutorial/default.yml +327 -0
- package/deps/jq/docs/content/2.download/default.yml +117 -0
- package/deps/jq/docs/content/3.manual/manual.yml +2878 -0
- package/deps/jq/docs/content/3.manual/v1.3/manual.yml +1270 -0
- package/deps/jq/docs/content/3.manual/v1.4/manual.yml +1672 -0
- package/deps/jq/docs/content/index/index.yml +51 -0
- package/deps/jq/docs/default_manpage.md +22 -0
- package/deps/jq/docs/public/.htaccess +28 -0
- package/deps/jq/docs/public/bootstrap/css/bootstrap-responsive.css +1058 -0
- package/deps/jq/docs/public/bootstrap/css/bootstrap-responsive.min.css +9 -0
- package/deps/jq/docs/public/bootstrap/css/bootstrap.css +5224 -0
- package/deps/jq/docs/public/bootstrap/css/bootstrap.min.css +9 -0
- package/deps/jq/docs/public/bootstrap/img/glyphicons-halflings-white.png +0 -0
- package/deps/jq/docs/public/bootstrap/img/glyphicons-halflings.png +0 -0
- package/deps/jq/docs/public/bootstrap/js/bootstrap.js +2027 -0
- package/deps/jq/docs/public/bootstrap/js/bootstrap.min.js +6 -0
- package/deps/jq/docs/public/css/base.scss +99 -0
- package/deps/jq/docs/public/jq.png +0 -0
- package/deps/jq/docs/public/robots.txt +2 -0
- package/deps/jq/docs/site.yml +18 -0
- package/deps/jq/docs/templates/default.liquid +34 -0
- package/deps/jq/docs/templates/index.liquid +60 -0
- package/deps/jq/docs/templates/manual.liquid +122 -0
- package/deps/jq/docs/templates/shared/_footer.liquid +5 -0
- package/deps/jq/docs/templates/shared/_head.liquid +12 -0
- package/deps/jq/docs/templates/shared/_header.liquid +26 -0
- package/deps/jq/exec_stack.h +112 -0
- package/deps/jq/execute.c +1155 -0
- package/deps/jq/inject_errors.c +112 -0
- package/deps/jq/jq.1.default +39 -0
- package/deps/jq/jq.1.prebuilt +3075 -0
- package/deps/jq/jq.h +60 -0
- package/deps/jq/jq.spec +70 -0
- package/deps/jq/jq_parser.h +9 -0
- package/deps/jq/jq_test.c +346 -0
- package/deps/jq/jv.c +1333 -0
- package/deps/jq/jv.h +240 -0
- package/deps/jq/jv_alloc.c +179 -0
- package/deps/jq/jv_alloc.h +27 -0
- package/deps/jq/jv_aux.c +619 -0
- package/deps/jq/jv_dtoa.c +4275 -0
- package/deps/jq/jv_dtoa.h +22 -0
- package/deps/jq/jv_file.c +49 -0
- package/deps/jq/jv_parse.c +852 -0
- package/deps/jq/jv_print.c +348 -0
- package/deps/jq/jv_unicode.c +96 -0
- package/deps/jq/jv_unicode.h +11 -0
- package/deps/jq/jv_utf8_tables.h +37 -0
- package/deps/jq/lexer.c +2442 -0
- package/deps/jq/lexer.h +362 -0
- package/deps/jq/lexer.l +184 -0
- package/deps/jq/libm.h +160 -0
- package/deps/jq/linker.c +393 -0
- package/deps/jq/linker.h +7 -0
- package/deps/jq/locfile.c +91 -0
- package/deps/jq/locfile.h +29 -0
- package/deps/jq/m4/ax_compare_version.m4 +177 -0
- package/deps/jq/m4/ax_prog_bison_version.m4 +68 -0
- package/deps/jq/main.c +566 -0
- package/deps/jq/opcode_list.h +44 -0
- package/deps/jq/parser.c +3914 -0
- package/deps/jq/parser.h +193 -0
- package/deps/jq/parser.y +923 -0
- package/deps/jq/scripts/crosscompile +42 -0
- package/deps/jq/scripts/gen_utf8_tables.py +32 -0
- package/deps/jq/scripts/version +5 -0
- package/deps/jq/setup.sh +33 -0
- package/deps/jq/tests/jq.test +1235 -0
- package/deps/jq/tests/jqtest +5 -0
- package/deps/jq/tests/mantest +7 -0
- package/deps/jq/tests/modules/.jq +5 -0
- package/deps/jq/tests/modules/a.jq +2 -0
- package/deps/jq/tests/modules/b/b.jq +2 -0
- package/deps/jq/tests/modules/c/c.jq +16 -0
- package/deps/jq/tests/modules/c/d.jq +1 -0
- package/deps/jq/tests/modules/data.json +4 -0
- package/deps/jq/tests/modules/lib/jq/e/e.jq +1 -0
- package/deps/jq/tests/modules/lib/jq/f.jq +1 -0
- package/deps/jq/tests/modules/syntaxerror/syntaxerror.jq +1 -0
- package/deps/jq/tests/modules/test_bind_order.jq +4 -0
- package/deps/jq/tests/modules/test_bind_order0.jq +1 -0
- package/deps/jq/tests/modules/test_bind_order1.jq +2 -0
- package/deps/jq/tests/modules/test_bind_order2.jq +2 -0
- package/deps/jq/tests/onig.supp +21 -0
- package/deps/jq/tests/onig.test +85 -0
- package/deps/jq/tests/onigtest +5 -0
- package/deps/jq/tests/setup +36 -0
- package/deps/jq/tests/shtest +205 -0
- package/deps/jq/tests/torture/input0.json +7 -0
- package/deps/jq/util.c +462 -0
- package/deps/jq/util.h +64 -0
- package/deps/jq.gyp +35 -0
- package/index.d.ts +3 -0
- package/jest.config.js +10 -0
- package/lib/index.js +14 -0
- package/package.json +48 -0
- package/reports/jest-port-api.xml +35 -0
- package/src/binding.cc +177 -0
- package/src/binding.h +13 -0
- package/test/santiy.test.js +122 -0
- package/util/configure.js +27 -0
package/deps/jq/jv.c
ADDED
|
@@ -0,0 +1,1333 @@
|
|
|
1
|
+
#include <stdint.h>
|
|
2
|
+
#include <stddef.h>
|
|
3
|
+
#include <assert.h>
|
|
4
|
+
#include <stdlib.h>
|
|
5
|
+
#include <stdio.h>
|
|
6
|
+
#include <string.h>
|
|
7
|
+
#include <stdarg.h>
|
|
8
|
+
#include <limits.h>
|
|
9
|
+
|
|
10
|
+
#include "jv_alloc.h"
|
|
11
|
+
#include "jv.h"
|
|
12
|
+
#include "jv_unicode.h"
|
|
13
|
+
#include "util.h"
|
|
14
|
+
|
|
15
|
+
/*
|
|
16
|
+
* Internal refcounting helpers
|
|
17
|
+
*/
|
|
18
|
+
|
|
19
|
+
typedef struct jv_refcnt {
|
|
20
|
+
int count;
|
|
21
|
+
} jv_refcnt;
|
|
22
|
+
|
|
23
|
+
static const jv_refcnt JV_REFCNT_INIT = {1};
|
|
24
|
+
|
|
25
|
+
static void jvp_refcnt_inc(jv_refcnt* c) {
|
|
26
|
+
c->count++;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
static int jvp_refcnt_dec(jv_refcnt* c) {
|
|
30
|
+
c->count--;
|
|
31
|
+
return c->count == 0;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
static int jvp_refcnt_unshared(jv_refcnt* c) {
|
|
35
|
+
assert(c->count > 0);
|
|
36
|
+
return c->count == 1;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
/*
|
|
40
|
+
* Simple values (true, false, null)
|
|
41
|
+
*/
|
|
42
|
+
|
|
43
|
+
#define KIND_MASK 0xf
|
|
44
|
+
|
|
45
|
+
jv_kind jv_get_kind(jv x) {
|
|
46
|
+
return x.kind_flags & KIND_MASK;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
const char* jv_kind_name(jv_kind k) {
|
|
50
|
+
switch (k) {
|
|
51
|
+
case JV_KIND_INVALID: return "<invalid>";
|
|
52
|
+
case JV_KIND_NULL: return "null";
|
|
53
|
+
case JV_KIND_FALSE: return "boolean";
|
|
54
|
+
case JV_KIND_TRUE: return "boolean";
|
|
55
|
+
case JV_KIND_NUMBER: return "number";
|
|
56
|
+
case JV_KIND_STRING: return "string";
|
|
57
|
+
case JV_KIND_ARRAY: return "array";
|
|
58
|
+
case JV_KIND_OBJECT: return "object";
|
|
59
|
+
}
|
|
60
|
+
assert(0 && "invalid kind");
|
|
61
|
+
return "<unknown>";
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
static const jv JV_NULL = {JV_KIND_NULL, 0, 0, 0, {0}};
|
|
65
|
+
static const jv JV_INVALID = {JV_KIND_INVALID, 0, 0, 0, {0}};
|
|
66
|
+
static const jv JV_FALSE = {JV_KIND_FALSE, 0, 0, 0, {0}};
|
|
67
|
+
static const jv JV_TRUE = {JV_KIND_TRUE, 0, 0, 0, {0}};
|
|
68
|
+
|
|
69
|
+
jv jv_true() {
|
|
70
|
+
return JV_TRUE;
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
jv jv_false() {
|
|
74
|
+
return JV_FALSE;
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
jv jv_null() {
|
|
78
|
+
return JV_NULL;
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
jv jv_bool(int x) {
|
|
82
|
+
return x ? JV_TRUE : JV_FALSE;
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
/*
|
|
86
|
+
* Invalid objects, with optional error messages
|
|
87
|
+
*/
|
|
88
|
+
|
|
89
|
+
typedef struct {
|
|
90
|
+
jv_refcnt refcnt;
|
|
91
|
+
jv errmsg;
|
|
92
|
+
} jvp_invalid;
|
|
93
|
+
|
|
94
|
+
jv jv_invalid_with_msg(jv err) {
|
|
95
|
+
if (jv_get_kind(err) == JV_KIND_NULL)
|
|
96
|
+
return JV_INVALID;
|
|
97
|
+
jvp_invalid* i = jv_mem_alloc(sizeof(jvp_invalid));
|
|
98
|
+
i->refcnt = JV_REFCNT_INIT;
|
|
99
|
+
i->errmsg = err;
|
|
100
|
+
|
|
101
|
+
jv x = {JV_KIND_INVALID, 0, 0, 0, {&i->refcnt}};
|
|
102
|
+
return x;
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
jv jv_invalid() {
|
|
106
|
+
return JV_INVALID;
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
jv jv_invalid_get_msg(jv inv) {
|
|
110
|
+
assert(jv_get_kind(inv) == JV_KIND_INVALID);
|
|
111
|
+
jv x;
|
|
112
|
+
if (inv.u.ptr == 0)
|
|
113
|
+
x = jv_null();
|
|
114
|
+
else
|
|
115
|
+
x = jv_copy(((jvp_invalid*)inv.u.ptr)->errmsg);
|
|
116
|
+
jv_free(inv);
|
|
117
|
+
return x;
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
int jv_invalid_has_msg(jv inv) {
|
|
121
|
+
jv msg = jv_invalid_get_msg(inv);
|
|
122
|
+
int r = jv_get_kind(msg) != JV_KIND_NULL;
|
|
123
|
+
jv_free(msg);
|
|
124
|
+
return r;
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
static void jvp_invalid_free(jv x) {
|
|
128
|
+
assert(jv_get_kind(x) == JV_KIND_INVALID);
|
|
129
|
+
if (x.u.ptr != 0 && jvp_refcnt_dec(x.u.ptr)) {
|
|
130
|
+
jv_free(((jvp_invalid*)x.u.ptr)->errmsg);
|
|
131
|
+
jv_mem_free(x.u.ptr);
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
/*
|
|
136
|
+
* Numbers
|
|
137
|
+
*/
|
|
138
|
+
|
|
139
|
+
jv jv_number(double x) {
|
|
140
|
+
jv j = {JV_KIND_NUMBER, 0, 0, 0, {.number = x}};
|
|
141
|
+
return j;
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
double jv_number_value(jv j) {
|
|
145
|
+
assert(jv_get_kind(j) == JV_KIND_NUMBER);
|
|
146
|
+
return j.u.number;
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
int jv_is_integer(jv j){
|
|
150
|
+
if(jv_get_kind(j) != JV_KIND_NUMBER){
|
|
151
|
+
return 0;
|
|
152
|
+
}
|
|
153
|
+
double x = jv_number_value(j);
|
|
154
|
+
if(x != x || x > INT_MAX || x < INT_MIN){
|
|
155
|
+
return 0;
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
return x == (int)x;
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
/*
|
|
162
|
+
* Arrays (internal helpers)
|
|
163
|
+
*/
|
|
164
|
+
|
|
165
|
+
#define ARRAY_SIZE_ROUND_UP(n) (((n)*3)/2)
|
|
166
|
+
|
|
167
|
+
static int imax(int a, int b) {
|
|
168
|
+
if (a>b) return a;
|
|
169
|
+
else return b;
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
//FIXME signed vs unsigned
|
|
173
|
+
typedef struct {
|
|
174
|
+
jv_refcnt refcnt;
|
|
175
|
+
int length, alloc_length;
|
|
176
|
+
jv elements[];
|
|
177
|
+
} jvp_array;
|
|
178
|
+
|
|
179
|
+
static jvp_array* jvp_array_ptr(jv a) {
|
|
180
|
+
assert(jv_get_kind(a) == JV_KIND_ARRAY);
|
|
181
|
+
return (jvp_array*)a.u.ptr;
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
static jvp_array* jvp_array_alloc(unsigned size) {
|
|
185
|
+
jvp_array* a = jv_mem_alloc(sizeof(jvp_array) + sizeof(jv) * size);
|
|
186
|
+
a->refcnt.count = 1;
|
|
187
|
+
a->length = 0;
|
|
188
|
+
a->alloc_length = size;
|
|
189
|
+
return a;
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
static jv jvp_array_new(unsigned size) {
|
|
193
|
+
jv r = {JV_KIND_ARRAY, 0, 0, 0, {&jvp_array_alloc(size)->refcnt}};
|
|
194
|
+
return r;
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
static void jvp_array_free(jv a) {
|
|
198
|
+
assert(jv_get_kind(a) == JV_KIND_ARRAY);
|
|
199
|
+
if (jvp_refcnt_dec(a.u.ptr)) {
|
|
200
|
+
jvp_array* array = jvp_array_ptr(a);
|
|
201
|
+
for (int i=0; i<array->length; i++) {
|
|
202
|
+
jv_free(array->elements[i]);
|
|
203
|
+
}
|
|
204
|
+
jv_mem_free(array);
|
|
205
|
+
}
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
static int jvp_array_length(jv a) {
|
|
209
|
+
assert(jv_get_kind(a) == JV_KIND_ARRAY);
|
|
210
|
+
return a.size;
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
static int jvp_array_offset(jv a) {
|
|
214
|
+
assert(jv_get_kind(a) == JV_KIND_ARRAY);
|
|
215
|
+
return a.offset;
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
static jv* jvp_array_read(jv a, int i) {
|
|
219
|
+
assert(jv_get_kind(a) == JV_KIND_ARRAY);
|
|
220
|
+
if (i >= 0 && i < jvp_array_length(a)) {
|
|
221
|
+
jvp_array* array = jvp_array_ptr(a);
|
|
222
|
+
assert(i + jvp_array_offset(a) < array->length);
|
|
223
|
+
return &array->elements[i + jvp_array_offset(a)];
|
|
224
|
+
} else {
|
|
225
|
+
return 0;
|
|
226
|
+
}
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
static jv* jvp_array_write(jv* a, int i) {
|
|
230
|
+
assert(i >= 0);
|
|
231
|
+
jvp_array* array = jvp_array_ptr(*a);
|
|
232
|
+
|
|
233
|
+
int pos = i + jvp_array_offset(*a);
|
|
234
|
+
if (pos < array->alloc_length && jvp_refcnt_unshared(a->u.ptr)) {
|
|
235
|
+
// use existing array space
|
|
236
|
+
for (int j = array->length; j <= pos; j++) {
|
|
237
|
+
array->elements[j] = JV_NULL;
|
|
238
|
+
}
|
|
239
|
+
array->length = imax(pos + 1, array->length);
|
|
240
|
+
a->size = imax(i + 1, a->size);
|
|
241
|
+
return &array->elements[pos];
|
|
242
|
+
} else {
|
|
243
|
+
// allocate a new array
|
|
244
|
+
int new_length = imax(i + 1, jvp_array_length(*a));
|
|
245
|
+
jvp_array* new_array = jvp_array_alloc(ARRAY_SIZE_ROUND_UP(new_length));
|
|
246
|
+
int j;
|
|
247
|
+
for (j = 0; j < jvp_array_length(*a); j++) {
|
|
248
|
+
new_array->elements[j] =
|
|
249
|
+
jv_copy(array->elements[j + jvp_array_offset(*a)]);
|
|
250
|
+
}
|
|
251
|
+
for (; j < new_length; j++) {
|
|
252
|
+
new_array->elements[j] = JV_NULL;
|
|
253
|
+
}
|
|
254
|
+
new_array->length = new_length;
|
|
255
|
+
jvp_array_free(*a);
|
|
256
|
+
jv new_jv = {JV_KIND_ARRAY, 0, 0, new_length, {&new_array->refcnt}};
|
|
257
|
+
*a = new_jv;
|
|
258
|
+
return &new_array->elements[i];
|
|
259
|
+
}
|
|
260
|
+
}
|
|
261
|
+
|
|
262
|
+
static int jvp_array_equal(jv a, jv b) {
|
|
263
|
+
if (jvp_array_length(a) != jvp_array_length(b))
|
|
264
|
+
return 0;
|
|
265
|
+
if (jvp_array_ptr(a) == jvp_array_ptr(b) &&
|
|
266
|
+
jvp_array_offset(a) == jvp_array_offset(b))
|
|
267
|
+
return 1;
|
|
268
|
+
for (int i=0; i<jvp_array_length(a); i++) {
|
|
269
|
+
if (!jv_equal(jv_copy(*jvp_array_read(a, i)),
|
|
270
|
+
jv_copy(*jvp_array_read(b, i))))
|
|
271
|
+
return 0;
|
|
272
|
+
}
|
|
273
|
+
return 1;
|
|
274
|
+
}
|
|
275
|
+
|
|
276
|
+
static void jvp_clamp_slice_params(int len, int *pstart, int *pend)
|
|
277
|
+
{
|
|
278
|
+
if (*pstart < 0) *pstart = len + *pstart;
|
|
279
|
+
if (*pend < 0) *pend = len + *pend;
|
|
280
|
+
|
|
281
|
+
if (*pstart < 0) *pstart = 0;
|
|
282
|
+
if (*pstart > len) *pstart = len;
|
|
283
|
+
if (*pend > len) *pend = len;
|
|
284
|
+
if (*pend < *pstart) *pend = *pstart;
|
|
285
|
+
}
|
|
286
|
+
|
|
287
|
+
static jv jvp_array_slice(jv a, int start, int end) {
|
|
288
|
+
assert(jv_get_kind(a) == JV_KIND_ARRAY);
|
|
289
|
+
int len = jvp_array_length(a);
|
|
290
|
+
jvp_clamp_slice_params(len, &start, &end);
|
|
291
|
+
assert(0 <= start && start <= end && end <= len);
|
|
292
|
+
|
|
293
|
+
// FIXME: maybe slice should reallocate if the slice is small enough
|
|
294
|
+
if (start == end) {
|
|
295
|
+
jv_free(a);
|
|
296
|
+
return jv_array();
|
|
297
|
+
}
|
|
298
|
+
// FIXME FIXME FIXME large offsets
|
|
299
|
+
a.offset += start;
|
|
300
|
+
a.size = end - start;
|
|
301
|
+
return a;
|
|
302
|
+
}
|
|
303
|
+
|
|
304
|
+
/*
|
|
305
|
+
* Arrays (public interface)
|
|
306
|
+
*/
|
|
307
|
+
|
|
308
|
+
jv jv_array_sized(int n) {
|
|
309
|
+
return jvp_array_new(n);
|
|
310
|
+
}
|
|
311
|
+
|
|
312
|
+
jv jv_array() {
|
|
313
|
+
return jv_array_sized(16);
|
|
314
|
+
}
|
|
315
|
+
|
|
316
|
+
int jv_array_length(jv j) {
|
|
317
|
+
assert(jv_get_kind(j) == JV_KIND_ARRAY);
|
|
318
|
+
int len = jvp_array_length(j);
|
|
319
|
+
jv_free(j);
|
|
320
|
+
return len;
|
|
321
|
+
}
|
|
322
|
+
|
|
323
|
+
jv jv_array_get(jv j, int idx) {
|
|
324
|
+
assert(jv_get_kind(j) == JV_KIND_ARRAY);
|
|
325
|
+
jv* slot = jvp_array_read(j, idx);
|
|
326
|
+
jv val;
|
|
327
|
+
if (slot) {
|
|
328
|
+
val = jv_copy(*slot);
|
|
329
|
+
} else {
|
|
330
|
+
val = jv_invalid();
|
|
331
|
+
}
|
|
332
|
+
jv_free(j);
|
|
333
|
+
return val;
|
|
334
|
+
}
|
|
335
|
+
|
|
336
|
+
jv jv_array_set(jv j, int idx, jv val) {
|
|
337
|
+
assert(jv_get_kind(j) == JV_KIND_ARRAY);
|
|
338
|
+
|
|
339
|
+
if (idx < 0)
|
|
340
|
+
idx = jvp_array_length(j) + idx;
|
|
341
|
+
if (idx < 0) {
|
|
342
|
+
jv_free(j);
|
|
343
|
+
jv_free(val);
|
|
344
|
+
return jv_invalid_with_msg(jv_string("Out of bounds negative array index"));
|
|
345
|
+
}
|
|
346
|
+
// copy/free of val,j coalesced
|
|
347
|
+
jv* slot = jvp_array_write(&j, idx);
|
|
348
|
+
jv_free(*slot);
|
|
349
|
+
*slot = val;
|
|
350
|
+
return j;
|
|
351
|
+
}
|
|
352
|
+
|
|
353
|
+
jv jv_array_append(jv j, jv val) {
|
|
354
|
+
// copy/free of val,j coalesced
|
|
355
|
+
return jv_array_set(j, jv_array_length(jv_copy(j)), val);
|
|
356
|
+
}
|
|
357
|
+
|
|
358
|
+
jv jv_array_concat(jv a, jv b) {
|
|
359
|
+
assert(jv_get_kind(a) == JV_KIND_ARRAY);
|
|
360
|
+
assert(jv_get_kind(b) == JV_KIND_ARRAY);
|
|
361
|
+
|
|
362
|
+
// FIXME: could be faster
|
|
363
|
+
jv_array_foreach(b, i, elem) {
|
|
364
|
+
a = jv_array_append(a, elem);
|
|
365
|
+
}
|
|
366
|
+
jv_free(b);
|
|
367
|
+
return a;
|
|
368
|
+
}
|
|
369
|
+
|
|
370
|
+
jv jv_array_slice(jv a, int start, int end) {
|
|
371
|
+
assert(jv_get_kind(a) == JV_KIND_ARRAY);
|
|
372
|
+
// copy/free of a coalesced
|
|
373
|
+
return jvp_array_slice(a, start, end);
|
|
374
|
+
}
|
|
375
|
+
|
|
376
|
+
int jv_array_contains(jv a, jv b) {
|
|
377
|
+
int r = 1;
|
|
378
|
+
jv_array_foreach(b, bi, belem) {
|
|
379
|
+
int ri = 0;
|
|
380
|
+
jv_array_foreach(a, ai, aelem) {
|
|
381
|
+
if (jv_contains(aelem, jv_copy(belem))) {
|
|
382
|
+
ri = 1;
|
|
383
|
+
break;
|
|
384
|
+
}
|
|
385
|
+
}
|
|
386
|
+
jv_free(belem);
|
|
387
|
+
if (!ri) {
|
|
388
|
+
r = 0;
|
|
389
|
+
break;
|
|
390
|
+
}
|
|
391
|
+
}
|
|
392
|
+
jv_free(a);
|
|
393
|
+
jv_free(b);
|
|
394
|
+
return r;
|
|
395
|
+
}
|
|
396
|
+
|
|
397
|
+
jv jv_array_indexes(jv a, jv b) {
|
|
398
|
+
jv res = jv_array();
|
|
399
|
+
int idx = -1;
|
|
400
|
+
jv_array_foreach(a, ai, aelem) {
|
|
401
|
+
jv_array_foreach(b, bi, belem) {
|
|
402
|
+
// quieten compiler warnings about aelem not being used... by
|
|
403
|
+
// using it
|
|
404
|
+
if ((bi == 0 && !jv_equal(jv_copy(aelem), jv_copy(belem))) ||
|
|
405
|
+
(bi > 0 && !jv_equal(jv_array_get(jv_copy(a), ai + bi), jv_copy(belem))))
|
|
406
|
+
idx = -1;
|
|
407
|
+
else if (bi == 0 && idx == -1)
|
|
408
|
+
idx = ai;
|
|
409
|
+
}
|
|
410
|
+
if (idx > -1)
|
|
411
|
+
res = jv_array_append(res, jv_number(idx));
|
|
412
|
+
idx = -1;
|
|
413
|
+
}
|
|
414
|
+
jv_free(a);
|
|
415
|
+
jv_free(b);
|
|
416
|
+
return res;
|
|
417
|
+
}
|
|
418
|
+
|
|
419
|
+
|
|
420
|
+
/*
|
|
421
|
+
* Strings (internal helpers)
|
|
422
|
+
*/
|
|
423
|
+
|
|
424
|
+
typedef struct {
|
|
425
|
+
jv_refcnt refcnt;
|
|
426
|
+
uint32_t hash;
|
|
427
|
+
// high 31 bits are length, low bit is a flag
|
|
428
|
+
// indicating whether hash has been computed.
|
|
429
|
+
uint32_t length_hashed;
|
|
430
|
+
uint32_t alloc_length;
|
|
431
|
+
char data[];
|
|
432
|
+
} jvp_string;
|
|
433
|
+
|
|
434
|
+
static jvp_string* jvp_string_ptr(jv a) {
|
|
435
|
+
assert(jv_get_kind(a) == JV_KIND_STRING);
|
|
436
|
+
return (jvp_string*)a.u.ptr;
|
|
437
|
+
}
|
|
438
|
+
|
|
439
|
+
static jvp_string* jvp_string_alloc(uint32_t size) {
|
|
440
|
+
jvp_string* s = jv_mem_alloc(sizeof(jvp_string) + size + 1);
|
|
441
|
+
s->refcnt.count = 1;
|
|
442
|
+
s->alloc_length = size;
|
|
443
|
+
return s;
|
|
444
|
+
}
|
|
445
|
+
|
|
446
|
+
/* Copy a UTF8 string, replacing all badly encoded points with U+FFFD */
|
|
447
|
+
static jv jvp_string_copy_replace_bad(const char* data, uint32_t length) {
|
|
448
|
+
const char* end = data + length;
|
|
449
|
+
const char* i = data;
|
|
450
|
+
const char* cstart;
|
|
451
|
+
|
|
452
|
+
uint32_t maxlength = length * 3 + 1; // worst case: all bad bytes, each becomes a 3-byte U+FFFD
|
|
453
|
+
jvp_string* s = jvp_string_alloc(maxlength);
|
|
454
|
+
char* out = s->data;
|
|
455
|
+
int c = 0;
|
|
456
|
+
|
|
457
|
+
while ((i = jvp_utf8_next((cstart = i), end, &c))) {
|
|
458
|
+
if (c == -1) {
|
|
459
|
+
c = 0xFFFD; // U+FFFD REPLACEMENT CHARACTER
|
|
460
|
+
}
|
|
461
|
+
out += jvp_utf8_encode(c, out);
|
|
462
|
+
assert(out < s->data + maxlength);
|
|
463
|
+
}
|
|
464
|
+
length = out - s->data;
|
|
465
|
+
s->data[length] = 0;
|
|
466
|
+
s->length_hashed = length << 1;
|
|
467
|
+
jv r = {JV_KIND_STRING, 0, 0, 0, {&s->refcnt}};
|
|
468
|
+
return r;
|
|
469
|
+
}
|
|
470
|
+
|
|
471
|
+
/* Assumes valid UTF8 */
|
|
472
|
+
static jv jvp_string_new(const char* data, uint32_t length) {
|
|
473
|
+
jvp_string* s = jvp_string_alloc(length);
|
|
474
|
+
s->length_hashed = length << 1;
|
|
475
|
+
if (data != NULL)
|
|
476
|
+
memcpy(s->data, data, length);
|
|
477
|
+
s->data[length] = 0;
|
|
478
|
+
jv r = {JV_KIND_STRING, 0, 0, 0, {&s->refcnt}};
|
|
479
|
+
return r;
|
|
480
|
+
}
|
|
481
|
+
|
|
482
|
+
static jv jvp_string_empty_new(uint32_t length) {
|
|
483
|
+
jvp_string* s = jvp_string_alloc(length);
|
|
484
|
+
s->length_hashed = 0;
|
|
485
|
+
memset(s->data, 0, length);
|
|
486
|
+
jv r = {JV_KIND_STRING, 0, 0, 0, {&s->refcnt}};
|
|
487
|
+
return r;
|
|
488
|
+
}
|
|
489
|
+
|
|
490
|
+
|
|
491
|
+
static void jvp_string_free(jv js) {
|
|
492
|
+
jvp_string* s = jvp_string_ptr(js);
|
|
493
|
+
if (jvp_refcnt_dec(&s->refcnt)) {
|
|
494
|
+
jv_mem_free(s);
|
|
495
|
+
}
|
|
496
|
+
}
|
|
497
|
+
|
|
498
|
+
static uint32_t jvp_string_length(jvp_string* s) {
|
|
499
|
+
return s->length_hashed >> 1;
|
|
500
|
+
}
|
|
501
|
+
|
|
502
|
+
static uint32_t jvp_string_remaining_space(jvp_string* s) {
|
|
503
|
+
assert(s->alloc_length >= jvp_string_length(s));
|
|
504
|
+
uint32_t r = s->alloc_length - jvp_string_length(s);
|
|
505
|
+
return r;
|
|
506
|
+
}
|
|
507
|
+
|
|
508
|
+
static jv jvp_string_append(jv string, const char* data, uint32_t len) {
|
|
509
|
+
jvp_string* s = jvp_string_ptr(string);
|
|
510
|
+
uint32_t currlen = jvp_string_length(s);
|
|
511
|
+
|
|
512
|
+
if (jvp_refcnt_unshared(string.u.ptr) &&
|
|
513
|
+
jvp_string_remaining_space(s) >= len) {
|
|
514
|
+
// the next string fits at the end of a
|
|
515
|
+
memcpy(s->data + currlen, data, len);
|
|
516
|
+
s->data[currlen + len] = 0;
|
|
517
|
+
s->length_hashed = (currlen + len) << 1;
|
|
518
|
+
return string;
|
|
519
|
+
} else {
|
|
520
|
+
// allocate a bigger buffer and copy
|
|
521
|
+
uint32_t allocsz = (currlen + len) * 2;
|
|
522
|
+
if (allocsz < 32) allocsz = 32;
|
|
523
|
+
jvp_string* news = jvp_string_alloc(allocsz);
|
|
524
|
+
news->length_hashed = (currlen + len) << 1;
|
|
525
|
+
memcpy(news->data, s->data, currlen);
|
|
526
|
+
memcpy(news->data + currlen, data, len);
|
|
527
|
+
news->data[currlen + len] = 0;
|
|
528
|
+
jvp_string_free(string);
|
|
529
|
+
jv r = {JV_KIND_STRING, 0, 0, 0, {&news->refcnt}};
|
|
530
|
+
return r;
|
|
531
|
+
}
|
|
532
|
+
}
|
|
533
|
+
|
|
534
|
+
static const uint32_t HASH_SEED = 0x432A9843;
|
|
535
|
+
|
|
536
|
+
static uint32_t rotl32 (uint32_t x, int8_t r){
|
|
537
|
+
return (x << r) | (x >> (32 - r));
|
|
538
|
+
}
|
|
539
|
+
|
|
540
|
+
static uint32_t jvp_string_hash(jv jstr) {
|
|
541
|
+
jvp_string* str = jvp_string_ptr(jstr);
|
|
542
|
+
if (str->length_hashed & 1)
|
|
543
|
+
return str->hash;
|
|
544
|
+
|
|
545
|
+
/* The following is based on MurmurHash3.
|
|
546
|
+
MurmurHash3 was written by Austin Appleby, and is placed
|
|
547
|
+
in the public domain. */
|
|
548
|
+
|
|
549
|
+
const uint8_t* data = (const uint8_t*)str->data;
|
|
550
|
+
int len = (int)jvp_string_length(str);
|
|
551
|
+
const int nblocks = len / 4;
|
|
552
|
+
|
|
553
|
+
uint32_t h1 = HASH_SEED;
|
|
554
|
+
|
|
555
|
+
const uint32_t c1 = 0xcc9e2d51;
|
|
556
|
+
const uint32_t c2 = 0x1b873593;
|
|
557
|
+
const uint32_t* blocks = (const uint32_t *)(data + nblocks*4);
|
|
558
|
+
|
|
559
|
+
for(int i = -nblocks; i; i++) {
|
|
560
|
+
uint32_t k1 = blocks[i]; //FIXME: endianness/alignment
|
|
561
|
+
|
|
562
|
+
k1 *= c1;
|
|
563
|
+
k1 = rotl32(k1,15);
|
|
564
|
+
k1 *= c2;
|
|
565
|
+
|
|
566
|
+
h1 ^= k1;
|
|
567
|
+
h1 = rotl32(h1,13);
|
|
568
|
+
h1 = h1*5+0xe6546b64;
|
|
569
|
+
}
|
|
570
|
+
|
|
571
|
+
const uint8_t* tail = (const uint8_t*)(data + nblocks*4);
|
|
572
|
+
|
|
573
|
+
uint32_t k1 = 0;
|
|
574
|
+
|
|
575
|
+
switch(len & 3) {
|
|
576
|
+
case 3: k1 ^= tail[2] << 16;
|
|
577
|
+
case 2: k1 ^= tail[1] << 8;
|
|
578
|
+
case 1: k1 ^= tail[0];
|
|
579
|
+
k1 *= c1; k1 = rotl32(k1,15); k1 *= c2; h1 ^= k1;
|
|
580
|
+
}
|
|
581
|
+
|
|
582
|
+
h1 ^= len;
|
|
583
|
+
|
|
584
|
+
h1 ^= h1 >> 16;
|
|
585
|
+
h1 *= 0x85ebca6b;
|
|
586
|
+
h1 ^= h1 >> 13;
|
|
587
|
+
h1 *= 0xc2b2ae35;
|
|
588
|
+
h1 ^= h1 >> 16;
|
|
589
|
+
|
|
590
|
+
str->length_hashed |= 1;
|
|
591
|
+
str->hash = h1;
|
|
592
|
+
|
|
593
|
+
return h1;
|
|
594
|
+
}
|
|
595
|
+
|
|
596
|
+
static int jvp_string_equal(jv a, jv b) {
|
|
597
|
+
assert(jv_get_kind(a) == JV_KIND_STRING);
|
|
598
|
+
assert(jv_get_kind(b) == JV_KIND_STRING);
|
|
599
|
+
jvp_string* stra = jvp_string_ptr(a);
|
|
600
|
+
jvp_string* strb = jvp_string_ptr(b);
|
|
601
|
+
if (jvp_string_length(stra) != jvp_string_length(strb)) return 0;
|
|
602
|
+
return memcmp(stra->data, strb->data, jvp_string_length(stra)) == 0;
|
|
603
|
+
}
|
|
604
|
+
|
|
605
|
+
/*
|
|
606
|
+
* Strings (public API)
|
|
607
|
+
*/
|
|
608
|
+
|
|
609
|
+
jv jv_string_sized(const char* str, int len) {
|
|
610
|
+
return
|
|
611
|
+
jvp_utf8_is_valid(str, str+len) ?
|
|
612
|
+
jvp_string_new(str, len) :
|
|
613
|
+
jvp_string_copy_replace_bad(str, len);
|
|
614
|
+
}
|
|
615
|
+
|
|
616
|
+
jv jv_string_empty(int len) {
|
|
617
|
+
return jvp_string_empty_new(len);
|
|
618
|
+
}
|
|
619
|
+
|
|
620
|
+
jv jv_string(const char* str) {
|
|
621
|
+
return jv_string_sized(str, strlen(str));
|
|
622
|
+
}
|
|
623
|
+
|
|
624
|
+
int jv_string_length_bytes(jv j) {
|
|
625
|
+
assert(jv_get_kind(j) == JV_KIND_STRING);
|
|
626
|
+
int r = jvp_string_length(jvp_string_ptr(j));
|
|
627
|
+
jv_free(j);
|
|
628
|
+
return r;
|
|
629
|
+
}
|
|
630
|
+
|
|
631
|
+
int jv_string_length_codepoints(jv j) {
|
|
632
|
+
assert(jv_get_kind(j) == JV_KIND_STRING);
|
|
633
|
+
const char* i = jv_string_value(j);
|
|
634
|
+
const char* end = i + jv_string_length_bytes(jv_copy(j));
|
|
635
|
+
int c = 0, len = 0;
|
|
636
|
+
while ((i = jvp_utf8_next(i, end, &c))) len++;
|
|
637
|
+
jv_free(j);
|
|
638
|
+
return len;
|
|
639
|
+
}
|
|
640
|
+
|
|
641
|
+
|
|
642
|
+
jv jv_string_indexes(jv j, jv k) {
|
|
643
|
+
assert(jv_get_kind(j) == JV_KIND_STRING);
|
|
644
|
+
assert(jv_get_kind(k) == JV_KIND_STRING);
|
|
645
|
+
const char *jstr = jv_string_value(j);
|
|
646
|
+
const char *idxstr = jv_string_value(k);
|
|
647
|
+
const char *p;
|
|
648
|
+
int jlen = jv_string_length_bytes(jv_copy(j));
|
|
649
|
+
int idxlen = jv_string_length_bytes(jv_copy(k));
|
|
650
|
+
jv a = jv_array();
|
|
651
|
+
|
|
652
|
+
p = jstr;
|
|
653
|
+
while ((p = _jq_memmem(p, (jstr + jlen) - p, idxstr, idxlen)) != NULL) {
|
|
654
|
+
a = jv_array_append(a, jv_number(p - jstr));
|
|
655
|
+
p += idxlen;
|
|
656
|
+
}
|
|
657
|
+
jv_free(j);
|
|
658
|
+
jv_free(k);
|
|
659
|
+
return a;
|
|
660
|
+
}
|
|
661
|
+
|
|
662
|
+
jv jv_string_split(jv j, jv sep) {
|
|
663
|
+
assert(jv_get_kind(j) == JV_KIND_STRING);
|
|
664
|
+
assert(jv_get_kind(sep) == JV_KIND_STRING);
|
|
665
|
+
const char *jstr = jv_string_value(j);
|
|
666
|
+
const char *jend = jstr + jv_string_length_bytes(jv_copy(j));
|
|
667
|
+
const char *sepstr = jv_string_value(sep);
|
|
668
|
+
const char *p, *s;
|
|
669
|
+
int seplen = jv_string_length_bytes(jv_copy(sep));
|
|
670
|
+
jv a = jv_array();
|
|
671
|
+
|
|
672
|
+
assert(jv_get_refcnt(a) == 1);
|
|
673
|
+
|
|
674
|
+
if (seplen == 0) {
|
|
675
|
+
int c;
|
|
676
|
+
while ((jstr = jvp_utf8_next(jstr, jend, &c)))
|
|
677
|
+
a = jv_array_append(a, jv_string_append_codepoint(jv_string(""), c));
|
|
678
|
+
} else {
|
|
679
|
+
for (p = jstr; p < jend; p = s + seplen) {
|
|
680
|
+
s = _jq_memmem(p, jend - p, sepstr, seplen);
|
|
681
|
+
if (s == NULL)
|
|
682
|
+
s = jend;
|
|
683
|
+
a = jv_array_append(a, jv_string_sized(p, s - p));
|
|
684
|
+
// Add an empty string to denote that j ends on a sep
|
|
685
|
+
if (s + seplen == jend && seplen != 0)
|
|
686
|
+
a = jv_array_append(a, jv_string(""));
|
|
687
|
+
}
|
|
688
|
+
}
|
|
689
|
+
jv_free(j);
|
|
690
|
+
jv_free(sep);
|
|
691
|
+
return a;
|
|
692
|
+
}
|
|
693
|
+
|
|
694
|
+
jv jv_string_explode(jv j) {
|
|
695
|
+
assert(jv_get_kind(j) == JV_KIND_STRING);
|
|
696
|
+
const char* i = jv_string_value(j);
|
|
697
|
+
int len = jv_string_length_bytes(jv_copy(j));
|
|
698
|
+
const char* end = i + len;
|
|
699
|
+
jv a = jv_array_sized(len);
|
|
700
|
+
int c;
|
|
701
|
+
while ((i = jvp_utf8_next(i, end, &c)))
|
|
702
|
+
a = jv_array_append(a, jv_number(c));
|
|
703
|
+
jv_free(j);
|
|
704
|
+
return a;
|
|
705
|
+
}
|
|
706
|
+
|
|
707
|
+
jv jv_string_implode(jv j) {
|
|
708
|
+
assert(jv_get_kind(j) == JV_KIND_ARRAY);
|
|
709
|
+
int len = jv_array_length(jv_copy(j));
|
|
710
|
+
jv s = jv_string_empty(len);
|
|
711
|
+
int i;
|
|
712
|
+
|
|
713
|
+
assert(len >= 0);
|
|
714
|
+
|
|
715
|
+
for (i = 0; i < len; i++) {
|
|
716
|
+
jv n = jv_array_get(jv_copy(j), i);
|
|
717
|
+
assert(jv_get_kind(n) == JV_KIND_NUMBER);
|
|
718
|
+
int nv = jv_number_value(n);
|
|
719
|
+
if (nv > 0x10FFFF)
|
|
720
|
+
nv = 0xFFFD; // U+FFFD REPLACEMENT CHARACTER
|
|
721
|
+
s = jv_string_append_codepoint(s, nv);
|
|
722
|
+
}
|
|
723
|
+
|
|
724
|
+
jv_free(j);
|
|
725
|
+
return s;
|
|
726
|
+
}
|
|
727
|
+
|
|
728
|
+
unsigned long jv_string_hash(jv j) {
|
|
729
|
+
assert(jv_get_kind(j) == JV_KIND_STRING);
|
|
730
|
+
uint32_t hash = jvp_string_hash(j);
|
|
731
|
+
jv_free(j);
|
|
732
|
+
return hash;
|
|
733
|
+
}
|
|
734
|
+
|
|
735
|
+
const char* jv_string_value(jv j) {
|
|
736
|
+
assert(jv_get_kind(j) == JV_KIND_STRING);
|
|
737
|
+
return jvp_string_ptr(j)->data;
|
|
738
|
+
}
|
|
739
|
+
|
|
740
|
+
jv jv_string_slice(jv j, int start, int end) {
|
|
741
|
+
assert(jv_get_kind(j) == JV_KIND_STRING);
|
|
742
|
+
const char *s = jv_string_value(j);
|
|
743
|
+
int len = jv_string_length_bytes(jv_copy(j));
|
|
744
|
+
int i;
|
|
745
|
+
const char *p, *e;
|
|
746
|
+
int c;
|
|
747
|
+
jv res;
|
|
748
|
+
|
|
749
|
+
jvp_clamp_slice_params(len, &start, &end);
|
|
750
|
+
assert(0 <= start && start <= end && end <= len);
|
|
751
|
+
|
|
752
|
+
/* Look for byte offset corresponding to start codepoints */
|
|
753
|
+
for (p = s, i = 0; i < start; i++) {
|
|
754
|
+
p = jvp_utf8_next(p, s + len, &c);
|
|
755
|
+
if (p == NULL) {
|
|
756
|
+
jv_free(j);
|
|
757
|
+
return jv_string_empty(16);
|
|
758
|
+
}
|
|
759
|
+
if (c == -1) {
|
|
760
|
+
jv_free(j);
|
|
761
|
+
return jv_invalid_with_msg(jv_string("Invalid UTF-8 string"));
|
|
762
|
+
}
|
|
763
|
+
}
|
|
764
|
+
/* Look for byte offset corresponding to end codepoints */
|
|
765
|
+
for (e = p; e != NULL && i < end; i++) {
|
|
766
|
+
e = jvp_utf8_next(e, s + len, &c);
|
|
767
|
+
if (e == NULL) {
|
|
768
|
+
e = s + len;
|
|
769
|
+
break;
|
|
770
|
+
}
|
|
771
|
+
if (c == -1) {
|
|
772
|
+
jv_free(j);
|
|
773
|
+
return jv_invalid_with_msg(jv_string("Invalid UTF-8 string"));
|
|
774
|
+
}
|
|
775
|
+
}
|
|
776
|
+
|
|
777
|
+
/*
|
|
778
|
+
* NOTE: Ideally we should do here what jvp_array_slice() does instead
|
|
779
|
+
* of allocating a new string as we do! However, we assume NUL-
|
|
780
|
+
* terminated strings all over, and in the jv API, so for now we waste
|
|
781
|
+
* memory like a drunken navy programmer. There's probably nothing we
|
|
782
|
+
* can do about it.
|
|
783
|
+
*/
|
|
784
|
+
res = jv_string_sized(p, e - p);
|
|
785
|
+
jv_free(j);
|
|
786
|
+
return res;
|
|
787
|
+
}
|
|
788
|
+
|
|
789
|
+
jv jv_string_concat(jv a, jv b) {
|
|
790
|
+
a = jvp_string_append(a, jv_string_value(b),
|
|
791
|
+
jvp_string_length(jvp_string_ptr(b)));
|
|
792
|
+
jv_free(b);
|
|
793
|
+
return a;
|
|
794
|
+
}
|
|
795
|
+
|
|
796
|
+
jv jv_string_append_buf(jv a, const char* buf, int len) {
|
|
797
|
+
if (jvp_utf8_is_valid(buf, buf+len)) {
|
|
798
|
+
a = jvp_string_append(a, buf, len);
|
|
799
|
+
} else {
|
|
800
|
+
jv b = jvp_string_copy_replace_bad(buf, len);
|
|
801
|
+
a = jv_string_concat(a, b);
|
|
802
|
+
}
|
|
803
|
+
return a;
|
|
804
|
+
}
|
|
805
|
+
|
|
806
|
+
jv jv_string_append_codepoint(jv a, uint32_t c) {
|
|
807
|
+
char buf[5];
|
|
808
|
+
int len = jvp_utf8_encode(c, buf);
|
|
809
|
+
a = jvp_string_append(a, buf, len);
|
|
810
|
+
return a;
|
|
811
|
+
}
|
|
812
|
+
|
|
813
|
+
jv jv_string_append_str(jv a, const char* str) {
|
|
814
|
+
return jv_string_append_buf(a, str, strlen(str));
|
|
815
|
+
}
|
|
816
|
+
|
|
817
|
+
jv jv_string_vfmt(const char* fmt, va_list ap) {
|
|
818
|
+
int size = 1024;
|
|
819
|
+
while (1) {
|
|
820
|
+
char* buf = jv_mem_alloc(size);
|
|
821
|
+
va_list ap2;
|
|
822
|
+
va_copy(ap2, ap);
|
|
823
|
+
int n = vsnprintf(buf, size, fmt, ap2);
|
|
824
|
+
va_end(ap2);
|
|
825
|
+
/*
|
|
826
|
+
* NOTE: here we support old vsnprintf()s that return -1 because the
|
|
827
|
+
* buffer is too small.
|
|
828
|
+
*/
|
|
829
|
+
if (n >= 0 && n < size) {
|
|
830
|
+
jv ret = jv_string_sized(buf, n);
|
|
831
|
+
jv_mem_free(buf);
|
|
832
|
+
return ret;
|
|
833
|
+
} else {
|
|
834
|
+
jv_mem_free(buf);
|
|
835
|
+
size = (n > 0) ? /* standard */ (n * 2) : /* not standard */ (size * 2);
|
|
836
|
+
}
|
|
837
|
+
}
|
|
838
|
+
}
|
|
839
|
+
|
|
840
|
+
jv jv_string_fmt(const char* fmt, ...) {
|
|
841
|
+
va_list args;
|
|
842
|
+
va_start(args, fmt);
|
|
843
|
+
jv res = jv_string_vfmt(fmt, args);
|
|
844
|
+
va_end(args);
|
|
845
|
+
return res;
|
|
846
|
+
}
|
|
847
|
+
|
|
848
|
+
/*
|
|
849
|
+
* Objects (internal helpers)
|
|
850
|
+
*/
|
|
851
|
+
|
|
852
|
+
struct object_slot {
|
|
853
|
+
int next; /* next slot with same hash, for collisions */
|
|
854
|
+
uint32_t hash;
|
|
855
|
+
jv string;
|
|
856
|
+
jv value;
|
|
857
|
+
};
|
|
858
|
+
|
|
859
|
+
typedef struct {
|
|
860
|
+
jv_refcnt refcnt;
|
|
861
|
+
int next_free;
|
|
862
|
+
struct object_slot elements[];
|
|
863
|
+
} jvp_object;
|
|
864
|
+
|
|
865
|
+
|
|
866
|
+
/* warning: nontrivial justification of alignment */
|
|
867
|
+
static jv jvp_object_new(int size) {
|
|
868
|
+
// Allocates an object of (size) slots and (size*2) hash buckets.
|
|
869
|
+
|
|
870
|
+
// size must be a power of two
|
|
871
|
+
assert(size > 0 && (size & (size - 1)) == 0);
|
|
872
|
+
|
|
873
|
+
jvp_object* obj = jv_mem_alloc(sizeof(jvp_object) +
|
|
874
|
+
sizeof(struct object_slot) * size +
|
|
875
|
+
sizeof(int) * (size * 2));
|
|
876
|
+
obj->refcnt.count = 1;
|
|
877
|
+
for (int i=0; i<size; i++) {
|
|
878
|
+
obj->elements[i].next = i - 1;
|
|
879
|
+
obj->elements[i].string = JV_NULL;
|
|
880
|
+
obj->elements[i].hash = 0;
|
|
881
|
+
obj->elements[i].value = JV_NULL;
|
|
882
|
+
}
|
|
883
|
+
obj->next_free = 0;
|
|
884
|
+
int* hashbuckets = (int*)(&obj->elements[size]);
|
|
885
|
+
for (int i=0; i<size*2; i++) {
|
|
886
|
+
hashbuckets[i] = -1;
|
|
887
|
+
}
|
|
888
|
+
jv r = {JV_KIND_OBJECT, 0, 0, size, {&obj->refcnt}};
|
|
889
|
+
return r;
|
|
890
|
+
}
|
|
891
|
+
|
|
892
|
+
static jvp_object* jvp_object_ptr(jv o) {
|
|
893
|
+
assert(jv_get_kind(o) == JV_KIND_OBJECT);
|
|
894
|
+
return (jvp_object*)o.u.ptr;
|
|
895
|
+
}
|
|
896
|
+
|
|
897
|
+
static uint32_t jvp_object_mask(jv o) {
|
|
898
|
+
assert(jv_get_kind(o) == JV_KIND_OBJECT);
|
|
899
|
+
return (o.size * 2) - 1;
|
|
900
|
+
}
|
|
901
|
+
|
|
902
|
+
static int jvp_object_size(jv o) {
|
|
903
|
+
assert(jv_get_kind(o) == JV_KIND_OBJECT);
|
|
904
|
+
return o.size;
|
|
905
|
+
}
|
|
906
|
+
|
|
907
|
+
static int* jvp_object_buckets(jv o) {
|
|
908
|
+
return (int*)(&jvp_object_ptr(o)->elements[o.size]);
|
|
909
|
+
}
|
|
910
|
+
|
|
911
|
+
static int* jvp_object_find_bucket(jv object, jv key) {
|
|
912
|
+
return jvp_object_buckets(object) + (jvp_object_mask(object) & jvp_string_hash(key));
|
|
913
|
+
}
|
|
914
|
+
|
|
915
|
+
static struct object_slot* jvp_object_get_slot(jv object, int slot) {
|
|
916
|
+
assert(slot == -1 || (slot >= 0 && slot < jvp_object_size(object)));
|
|
917
|
+
if (slot == -1) return 0;
|
|
918
|
+
else return &jvp_object_ptr(object)->elements[slot];
|
|
919
|
+
}
|
|
920
|
+
|
|
921
|
+
static struct object_slot* jvp_object_next_slot(jv object, struct object_slot* slot) {
|
|
922
|
+
return jvp_object_get_slot(object, slot->next);
|
|
923
|
+
}
|
|
924
|
+
|
|
925
|
+
static struct object_slot* jvp_object_find_slot(jv object, jv keystr, int* bucket) {
|
|
926
|
+
uint32_t hash = jvp_string_hash(keystr);
|
|
927
|
+
for (struct object_slot* curr = jvp_object_get_slot(object, *bucket);
|
|
928
|
+
curr;
|
|
929
|
+
curr = jvp_object_next_slot(object, curr)) {
|
|
930
|
+
if (curr->hash == hash && jvp_string_equal(keystr, curr->string)) {
|
|
931
|
+
return curr;
|
|
932
|
+
}
|
|
933
|
+
}
|
|
934
|
+
return 0;
|
|
935
|
+
}
|
|
936
|
+
|
|
937
|
+
static struct object_slot* jvp_object_add_slot(jv object, jv key, int* bucket) {
|
|
938
|
+
jvp_object* o = jvp_object_ptr(object);
|
|
939
|
+
int newslot_idx = o->next_free;
|
|
940
|
+
if (newslot_idx == jvp_object_size(object)) return 0;
|
|
941
|
+
struct object_slot* newslot = jvp_object_get_slot(object, newslot_idx);
|
|
942
|
+
o->next_free++;
|
|
943
|
+
newslot->next = *bucket;
|
|
944
|
+
*bucket = newslot_idx;
|
|
945
|
+
newslot->hash = jvp_string_hash(key);
|
|
946
|
+
newslot->string = key;
|
|
947
|
+
return newslot;
|
|
948
|
+
}
|
|
949
|
+
|
|
950
|
+
static jv* jvp_object_read(jv object, jv key) {
|
|
951
|
+
assert(jv_get_kind(key) == JV_KIND_STRING);
|
|
952
|
+
int* bucket = jvp_object_find_bucket(object, key);
|
|
953
|
+
struct object_slot* slot = jvp_object_find_slot(object, key, bucket);
|
|
954
|
+
if (slot == 0) return 0;
|
|
955
|
+
else return &slot->value;
|
|
956
|
+
}
|
|
957
|
+
|
|
958
|
+
static void jvp_object_free(jv o) {
|
|
959
|
+
assert(jv_get_kind(o) == JV_KIND_OBJECT);
|
|
960
|
+
if (jvp_refcnt_dec(o.u.ptr)) {
|
|
961
|
+
for (int i=0; i<jvp_object_size(o); i++) {
|
|
962
|
+
struct object_slot* slot = jvp_object_get_slot(o, i);
|
|
963
|
+
if (jv_get_kind(slot->string) != JV_KIND_NULL) {
|
|
964
|
+
jvp_string_free(slot->string);
|
|
965
|
+
jv_free(slot->value);
|
|
966
|
+
}
|
|
967
|
+
}
|
|
968
|
+
jv_mem_free(jvp_object_ptr(o));
|
|
969
|
+
}
|
|
970
|
+
}
|
|
971
|
+
|
|
972
|
+
static jv jvp_object_rehash(jv object) {
|
|
973
|
+
assert(jv_get_kind(object) == JV_KIND_OBJECT);
|
|
974
|
+
assert(jvp_refcnt_unshared(object.u.ptr));
|
|
975
|
+
int size = jvp_object_size(object);
|
|
976
|
+
jv new_object = jvp_object_new(size * 2);
|
|
977
|
+
for (int i=0; i<size; i++) {
|
|
978
|
+
struct object_slot* slot = jvp_object_get_slot(object, i);
|
|
979
|
+
if (jv_get_kind(slot->string) == JV_KIND_NULL) continue;
|
|
980
|
+
int* new_bucket = jvp_object_find_bucket(new_object, slot->string);
|
|
981
|
+
assert(!jvp_object_find_slot(new_object, slot->string, new_bucket));
|
|
982
|
+
struct object_slot* new_slot = jvp_object_add_slot(new_object, slot->string, new_bucket);
|
|
983
|
+
assert(new_slot);
|
|
984
|
+
new_slot->value = slot->value;
|
|
985
|
+
}
|
|
986
|
+
// references are transported, just drop the old table
|
|
987
|
+
jv_mem_free(jvp_object_ptr(object));
|
|
988
|
+
return new_object;
|
|
989
|
+
}
|
|
990
|
+
|
|
991
|
+
static jv jvp_object_unshare(jv object) {
|
|
992
|
+
assert(jv_get_kind(object) == JV_KIND_OBJECT);
|
|
993
|
+
if (jvp_refcnt_unshared(object.u.ptr))
|
|
994
|
+
return object;
|
|
995
|
+
|
|
996
|
+
jv new_object = jvp_object_new(jvp_object_size(object));
|
|
997
|
+
jvp_object_ptr(new_object)->next_free = jvp_object_ptr(object)->next_free;
|
|
998
|
+
for (int i=0; i<jvp_object_size(new_object); i++) {
|
|
999
|
+
struct object_slot* old_slot = jvp_object_get_slot(object, i);
|
|
1000
|
+
struct object_slot* new_slot = jvp_object_get_slot(new_object, i);
|
|
1001
|
+
*new_slot = *old_slot;
|
|
1002
|
+
if (jv_get_kind(old_slot->string) != JV_KIND_NULL) {
|
|
1003
|
+
new_slot->string = jv_copy(old_slot->string);
|
|
1004
|
+
new_slot->value = jv_copy(old_slot->value);
|
|
1005
|
+
}
|
|
1006
|
+
}
|
|
1007
|
+
|
|
1008
|
+
int* old_buckets = jvp_object_buckets(object);
|
|
1009
|
+
int* new_buckets = jvp_object_buckets(new_object);
|
|
1010
|
+
memcpy(new_buckets, old_buckets, sizeof(int) * jvp_object_size(new_object)*2);
|
|
1011
|
+
|
|
1012
|
+
jvp_object_free(object);
|
|
1013
|
+
assert(jvp_refcnt_unshared(new_object.u.ptr));
|
|
1014
|
+
return new_object;
|
|
1015
|
+
}
|
|
1016
|
+
|
|
1017
|
+
static jv* jvp_object_write(jv* object, jv key) {
|
|
1018
|
+
*object = jvp_object_unshare(*object);
|
|
1019
|
+
int* bucket = jvp_object_find_bucket(*object, key);
|
|
1020
|
+
struct object_slot* slot = jvp_object_find_slot(*object, key, bucket);
|
|
1021
|
+
if (slot) {
|
|
1022
|
+
// already has the key
|
|
1023
|
+
jvp_string_free(key);
|
|
1024
|
+
return &slot->value;
|
|
1025
|
+
}
|
|
1026
|
+
slot = jvp_object_add_slot(*object, key, bucket);
|
|
1027
|
+
if (slot) {
|
|
1028
|
+
slot->value = jv_invalid();
|
|
1029
|
+
} else {
|
|
1030
|
+
*object = jvp_object_rehash(*object);
|
|
1031
|
+
bucket = jvp_object_find_bucket(*object, key);
|
|
1032
|
+
assert(!jvp_object_find_slot(*object, key, bucket));
|
|
1033
|
+
slot = jvp_object_add_slot(*object, key, bucket);
|
|
1034
|
+
assert(slot);
|
|
1035
|
+
slot->value = jv_invalid();
|
|
1036
|
+
}
|
|
1037
|
+
return &slot->value;
|
|
1038
|
+
}
|
|
1039
|
+
|
|
1040
|
+
static int jvp_object_delete(jv* object, jv key) {
|
|
1041
|
+
assert(jv_get_kind(key) == JV_KIND_STRING);
|
|
1042
|
+
*object = jvp_object_unshare(*object);
|
|
1043
|
+
int* bucket = jvp_object_find_bucket(*object, key);
|
|
1044
|
+
int* prev_ptr = bucket;
|
|
1045
|
+
uint32_t hash = jvp_string_hash(key);
|
|
1046
|
+
for (struct object_slot* curr = jvp_object_get_slot(*object, *bucket);
|
|
1047
|
+
curr;
|
|
1048
|
+
curr = jvp_object_next_slot(*object, curr)) {
|
|
1049
|
+
if (hash == curr->hash && jvp_string_equal(key, curr->string)) {
|
|
1050
|
+
*prev_ptr = curr->next;
|
|
1051
|
+
jvp_string_free(curr->string);
|
|
1052
|
+
curr->string = JV_NULL;
|
|
1053
|
+
jv_free(curr->value);
|
|
1054
|
+
return 1;
|
|
1055
|
+
}
|
|
1056
|
+
prev_ptr = &curr->next;
|
|
1057
|
+
}
|
|
1058
|
+
return 0;
|
|
1059
|
+
}
|
|
1060
|
+
|
|
1061
|
+
static int jvp_object_length(jv object) {
|
|
1062
|
+
int n = 0;
|
|
1063
|
+
for (int i=0; i<jvp_object_size(object); i++) {
|
|
1064
|
+
struct object_slot* slot = jvp_object_get_slot(object, i);
|
|
1065
|
+
if (jv_get_kind(slot->string) != JV_KIND_NULL) n++;
|
|
1066
|
+
}
|
|
1067
|
+
return n;
|
|
1068
|
+
}
|
|
1069
|
+
|
|
1070
|
+
static int jvp_object_equal(jv o1, jv o2) {
|
|
1071
|
+
int len2 = jvp_object_length(o2);
|
|
1072
|
+
int len1 = 0;
|
|
1073
|
+
for (int i=0; i<jvp_object_size(o1); i++) {
|
|
1074
|
+
struct object_slot* slot = jvp_object_get_slot(o1, i);
|
|
1075
|
+
if (jv_get_kind(slot->string) == JV_KIND_NULL) continue;
|
|
1076
|
+
jv* slot2 = jvp_object_read(o2, slot->string);
|
|
1077
|
+
if (!slot2) return 0;
|
|
1078
|
+
// FIXME: do less refcounting here
|
|
1079
|
+
if (!jv_equal(jv_copy(slot->value), jv_copy(*slot2))) return 0;
|
|
1080
|
+
len1++;
|
|
1081
|
+
}
|
|
1082
|
+
return len1 == len2;
|
|
1083
|
+
}
|
|
1084
|
+
|
|
1085
|
+
/*
|
|
1086
|
+
* Objects (public interface)
|
|
1087
|
+
*/
|
|
1088
|
+
#define DEFAULT_OBJECT_SIZE 8
|
|
1089
|
+
jv jv_object() {
|
|
1090
|
+
return jvp_object_new(8);
|
|
1091
|
+
}
|
|
1092
|
+
|
|
1093
|
+
jv jv_object_get(jv object, jv key) {
|
|
1094
|
+
assert(jv_get_kind(object) == JV_KIND_OBJECT);
|
|
1095
|
+
assert(jv_get_kind(key) == JV_KIND_STRING);
|
|
1096
|
+
jv* slot = jvp_object_read(object, key);
|
|
1097
|
+
jv val;
|
|
1098
|
+
if (slot) {
|
|
1099
|
+
val = jv_copy(*slot);
|
|
1100
|
+
} else {
|
|
1101
|
+
val = jv_invalid();
|
|
1102
|
+
}
|
|
1103
|
+
jv_free(object);
|
|
1104
|
+
jv_free(key);
|
|
1105
|
+
return val;
|
|
1106
|
+
}
|
|
1107
|
+
|
|
1108
|
+
jv jv_object_set(jv object, jv key, jv value) {
|
|
1109
|
+
assert(jv_get_kind(object) == JV_KIND_OBJECT);
|
|
1110
|
+
assert(jv_get_kind(key) == JV_KIND_STRING);
|
|
1111
|
+
// copy/free of object, key, value coalesced
|
|
1112
|
+
jv* slot = jvp_object_write(&object, key);
|
|
1113
|
+
jv_free(*slot);
|
|
1114
|
+
*slot = value;
|
|
1115
|
+
return object;
|
|
1116
|
+
}
|
|
1117
|
+
|
|
1118
|
+
jv jv_object_delete(jv object, jv key) {
|
|
1119
|
+
assert(jv_get_kind(object) == JV_KIND_OBJECT);
|
|
1120
|
+
assert(jv_get_kind(key) == JV_KIND_STRING);
|
|
1121
|
+
jvp_object_delete(&object, key);
|
|
1122
|
+
jv_free(key);
|
|
1123
|
+
return object;
|
|
1124
|
+
}
|
|
1125
|
+
|
|
1126
|
+
int jv_object_length(jv object) {
|
|
1127
|
+
assert(jv_get_kind(object) == JV_KIND_OBJECT);
|
|
1128
|
+
int n = jvp_object_length(object);
|
|
1129
|
+
jv_free(object);
|
|
1130
|
+
return n;
|
|
1131
|
+
}
|
|
1132
|
+
|
|
1133
|
+
jv jv_object_merge(jv a, jv b) {
|
|
1134
|
+
assert(jv_get_kind(a) == JV_KIND_OBJECT);
|
|
1135
|
+
jv_object_foreach(b, k, v) {
|
|
1136
|
+
a = jv_object_set(a, k, v);
|
|
1137
|
+
}
|
|
1138
|
+
jv_free(b);
|
|
1139
|
+
return a;
|
|
1140
|
+
}
|
|
1141
|
+
|
|
1142
|
+
jv jv_object_merge_recursive(jv a, jv b) {
|
|
1143
|
+
assert(jv_get_kind(a) == JV_KIND_OBJECT);
|
|
1144
|
+
assert(jv_get_kind(b) == JV_KIND_OBJECT);
|
|
1145
|
+
|
|
1146
|
+
jv_object_foreach(b, k, v) {
|
|
1147
|
+
jv elem = jv_object_get(jv_copy(a), jv_copy(k));
|
|
1148
|
+
if (jv_is_valid(elem) &&
|
|
1149
|
+
jv_get_kind(elem) == JV_KIND_OBJECT &&
|
|
1150
|
+
jv_get_kind(v) == JV_KIND_OBJECT) {
|
|
1151
|
+
a = jv_object_set(a, k, jv_object_merge_recursive(elem, v));
|
|
1152
|
+
} else {
|
|
1153
|
+
jv_free(elem);
|
|
1154
|
+
a = jv_object_set(a, k, v);
|
|
1155
|
+
}
|
|
1156
|
+
}
|
|
1157
|
+
jv_free(b);
|
|
1158
|
+
return a;
|
|
1159
|
+
}
|
|
1160
|
+
|
|
1161
|
+
int jv_object_contains(jv a, jv b) {
|
|
1162
|
+
assert(jv_get_kind(a) == JV_KIND_OBJECT);
|
|
1163
|
+
assert(jv_get_kind(b) == JV_KIND_OBJECT);
|
|
1164
|
+
int r = 1;
|
|
1165
|
+
|
|
1166
|
+
jv_object_foreach(b, key, b_val) {
|
|
1167
|
+
jv a_val = jv_object_get(jv_copy(a), jv_copy(key));
|
|
1168
|
+
|
|
1169
|
+
r = jv_contains(a_val, b_val);
|
|
1170
|
+
jv_free(key);
|
|
1171
|
+
|
|
1172
|
+
if (!r) break;
|
|
1173
|
+
}
|
|
1174
|
+
|
|
1175
|
+
jv_free(a);
|
|
1176
|
+
jv_free(b);
|
|
1177
|
+
return r;
|
|
1178
|
+
}
|
|
1179
|
+
|
|
1180
|
+
/*
|
|
1181
|
+
* Object iteration (internal helpers)
|
|
1182
|
+
*/
|
|
1183
|
+
|
|
1184
|
+
enum { ITER_FINISHED = -2 };
|
|
1185
|
+
|
|
1186
|
+
int jv_object_iter_valid(jv object, int i) {
|
|
1187
|
+
return i != ITER_FINISHED;
|
|
1188
|
+
}
|
|
1189
|
+
|
|
1190
|
+
int jv_object_iter(jv object) {
|
|
1191
|
+
assert(jv_get_kind(object) == JV_KIND_OBJECT);
|
|
1192
|
+
return jv_object_iter_next(object, -1);
|
|
1193
|
+
}
|
|
1194
|
+
|
|
1195
|
+
int jv_object_iter_next(jv object, int iter) {
|
|
1196
|
+
assert(jv_get_kind(object) == JV_KIND_OBJECT);
|
|
1197
|
+
assert(iter != ITER_FINISHED);
|
|
1198
|
+
struct object_slot* slot;
|
|
1199
|
+
do {
|
|
1200
|
+
iter++;
|
|
1201
|
+
if (iter >= jvp_object_size(object))
|
|
1202
|
+
return ITER_FINISHED;
|
|
1203
|
+
slot = jvp_object_get_slot(object, iter);
|
|
1204
|
+
} while (jv_get_kind(slot->string) == JV_KIND_NULL);
|
|
1205
|
+
assert(jv_get_kind(jvp_object_get_slot(object,iter)->string)
|
|
1206
|
+
== JV_KIND_STRING);
|
|
1207
|
+
return iter;
|
|
1208
|
+
}
|
|
1209
|
+
|
|
1210
|
+
jv jv_object_iter_key(jv object, int iter) {
|
|
1211
|
+
jv s = jvp_object_get_slot(object, iter)->string;
|
|
1212
|
+
assert(jv_get_kind(s) == JV_KIND_STRING);
|
|
1213
|
+
return jv_copy(s);
|
|
1214
|
+
}
|
|
1215
|
+
|
|
1216
|
+
jv jv_object_iter_value(jv object, int iter) {
|
|
1217
|
+
return jv_copy(jvp_object_get_slot(object, iter)->value);
|
|
1218
|
+
}
|
|
1219
|
+
|
|
1220
|
+
/*
|
|
1221
|
+
* Memory management
|
|
1222
|
+
*/
|
|
1223
|
+
jv jv_copy(jv j) {
|
|
1224
|
+
if (jv_get_kind(j) == JV_KIND_ARRAY ||
|
|
1225
|
+
jv_get_kind(j) == JV_KIND_STRING ||
|
|
1226
|
+
jv_get_kind(j) == JV_KIND_OBJECT ||
|
|
1227
|
+
(jv_get_kind(j) == JV_KIND_INVALID && j.u.ptr != 0)) {
|
|
1228
|
+
jvp_refcnt_inc(j.u.ptr);
|
|
1229
|
+
}
|
|
1230
|
+
return j;
|
|
1231
|
+
}
|
|
1232
|
+
|
|
1233
|
+
void jv_free(jv j) {
|
|
1234
|
+
if (jv_get_kind(j) == JV_KIND_ARRAY) {
|
|
1235
|
+
jvp_array_free(j);
|
|
1236
|
+
} else if (jv_get_kind(j) == JV_KIND_STRING) {
|
|
1237
|
+
jvp_string_free(j);
|
|
1238
|
+
} else if (jv_get_kind(j) == JV_KIND_OBJECT) {
|
|
1239
|
+
jvp_object_free(j);
|
|
1240
|
+
} else if (jv_get_kind(j) == JV_KIND_INVALID) {
|
|
1241
|
+
jvp_invalid_free(j);
|
|
1242
|
+
}
|
|
1243
|
+
}
|
|
1244
|
+
|
|
1245
|
+
int jv_get_refcnt(jv j) {
|
|
1246
|
+
switch (jv_get_kind(j)) {
|
|
1247
|
+
case JV_KIND_ARRAY:
|
|
1248
|
+
case JV_KIND_STRING:
|
|
1249
|
+
case JV_KIND_OBJECT:
|
|
1250
|
+
return j.u.ptr->count;
|
|
1251
|
+
default:
|
|
1252
|
+
return 1;
|
|
1253
|
+
}
|
|
1254
|
+
}
|
|
1255
|
+
|
|
1256
|
+
/*
|
|
1257
|
+
* Higher-level operations
|
|
1258
|
+
*/
|
|
1259
|
+
|
|
1260
|
+
int jv_equal(jv a, jv b) {
|
|
1261
|
+
int r;
|
|
1262
|
+
if (jv_get_kind(a) != jv_get_kind(b)) {
|
|
1263
|
+
r = 0;
|
|
1264
|
+
} else if (jv_get_kind(a) == JV_KIND_NUMBER) {
|
|
1265
|
+
r = jv_number_value(a) == jv_number_value(b);
|
|
1266
|
+
} else if (a.kind_flags == b.kind_flags &&
|
|
1267
|
+
a.size == b.size &&
|
|
1268
|
+
a.u.ptr == b.u.ptr) {
|
|
1269
|
+
r = 1;
|
|
1270
|
+
} else {
|
|
1271
|
+
switch (jv_get_kind(a)) {
|
|
1272
|
+
case JV_KIND_ARRAY:
|
|
1273
|
+
r = jvp_array_equal(a, b);
|
|
1274
|
+
break;
|
|
1275
|
+
case JV_KIND_STRING:
|
|
1276
|
+
r = jvp_string_equal(a, b);
|
|
1277
|
+
break;
|
|
1278
|
+
case JV_KIND_OBJECT:
|
|
1279
|
+
r = jvp_object_equal(a, b);
|
|
1280
|
+
break;
|
|
1281
|
+
default:
|
|
1282
|
+
r = 1;
|
|
1283
|
+
break;
|
|
1284
|
+
}
|
|
1285
|
+
}
|
|
1286
|
+
jv_free(a);
|
|
1287
|
+
jv_free(b);
|
|
1288
|
+
return r;
|
|
1289
|
+
}
|
|
1290
|
+
|
|
1291
|
+
int jv_identical(jv a, jv b) {
|
|
1292
|
+
int r;
|
|
1293
|
+
if (a.kind_flags != b.kind_flags
|
|
1294
|
+
|| a.offset != b.offset
|
|
1295
|
+
|| a.size != b.size) {
|
|
1296
|
+
r = 0;
|
|
1297
|
+
} else {
|
|
1298
|
+
switch (jv_get_kind(a)) {
|
|
1299
|
+
case JV_KIND_ARRAY:
|
|
1300
|
+
case JV_KIND_STRING:
|
|
1301
|
+
case JV_KIND_OBJECT:
|
|
1302
|
+
r = a.u.ptr == b.u.ptr;
|
|
1303
|
+
break;
|
|
1304
|
+
case JV_KIND_NUMBER:
|
|
1305
|
+
r = a.u.number == b.u.number;
|
|
1306
|
+
break;
|
|
1307
|
+
default:
|
|
1308
|
+
r = 1;
|
|
1309
|
+
break;
|
|
1310
|
+
}
|
|
1311
|
+
}
|
|
1312
|
+
jv_free(a);
|
|
1313
|
+
jv_free(b);
|
|
1314
|
+
return r;
|
|
1315
|
+
}
|
|
1316
|
+
|
|
1317
|
+
int jv_contains(jv a, jv b) {
|
|
1318
|
+
int r = 1;
|
|
1319
|
+
if (jv_get_kind(a) != jv_get_kind(b)) {
|
|
1320
|
+
r = 0;
|
|
1321
|
+
} else if (jv_get_kind(a) == JV_KIND_OBJECT) {
|
|
1322
|
+
r = jv_object_contains(jv_copy(a), jv_copy(b));
|
|
1323
|
+
} else if (jv_get_kind(a) == JV_KIND_ARRAY) {
|
|
1324
|
+
r = jv_array_contains(jv_copy(a), jv_copy(b));
|
|
1325
|
+
} else if (jv_get_kind(a) == JV_KIND_STRING) {
|
|
1326
|
+
r = strstr(jv_string_value(a), jv_string_value(b)) != 0;
|
|
1327
|
+
} else {
|
|
1328
|
+
r = jv_equal(jv_copy(a), jv_copy(b));
|
|
1329
|
+
}
|
|
1330
|
+
jv_free(a);
|
|
1331
|
+
jv_free(b);
|
|
1332
|
+
return r;
|
|
1333
|
+
}
|