oj 3.17.3 → 3.17.8
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.
- checksums.yaml +4 -4
- data/CHANGELOG.md +146 -0
- data/ext/oj/cache.c +72 -39
- data/ext/oj/cache.h +5 -0
- data/ext/oj/circarray.c +6 -2
- data/ext/oj/code.c +38 -28
- data/ext/oj/code.h +2 -0
- data/ext/oj/compat.c +63 -31
- data/ext/oj/custom.c +43 -8
- data/ext/oj/dump.c +120 -41
- data/ext/oj/dump.h +10 -0
- data/ext/oj/dump_compat.c +19 -15
- data/ext/oj/dump_object.c +38 -12
- data/ext/oj/dump_strict.c +2 -1
- data/ext/oj/extconf.rb +7 -0
- data/ext/oj/fast.c +132 -72
- data/ext/oj/intern.c +80 -77
- data/ext/oj/mimic_json.c +5 -29
- data/ext/oj/object.c +47 -17
- data/ext/oj/odd.c +6 -2
- data/ext/oj/oj.c +393 -44
- data/ext/oj/oj.h +10 -3
- data/ext/oj/parse.c +75 -7
- data/ext/oj/parse.h +23 -2
- data/ext/oj/parser.c +219 -71
- data/ext/oj/parser.h +1 -1
- data/ext/oj/rails.c +188 -45
- data/ext/oj/reader.c +22 -6
- data/ext/oj/reader.h +1 -0
- data/ext/oj/resolve.c +16 -6
- data/ext/oj/resolve.h +9 -0
- data/ext/oj/rxclass.c +16 -0
- data/ext/oj/rxclass.h +1 -0
- data/ext/oj/safe.c +62 -48
- data/ext/oj/safe.h +14 -2
- data/ext/oj/saj.c +60 -17
- data/ext/oj/saj2.c +44 -28
- data/ext/oj/simd.h +5 -3
- data/ext/oj/sparse.c +23 -6
- data/ext/oj/stream_writer.c +19 -1
- data/ext/oj/strict.c +42 -4
- data/ext/oj/string_writer.c +40 -10
- data/ext/oj/usual.c +17 -5
- data/ext/oj/util.c +27 -0
- data/ext/oj/util.h +2 -1
- data/ext/oj/val_stack.c +11 -22
- data/ext/oj/val_stack.h +70 -30
- data/ext/oj/wab.c +17 -26
- data/lib/oj/generic_object.rb +82 -0
- data/lib/oj/json.rb +1 -71
- data/lib/oj/mimic.rb +0 -1
- data/lib/oj/version.rb +1 -1
- data/pages/Compatibility.md +1 -1
- data/pages/Custom.md +2 -3
- data/pages/InstallOptions.md +8 -6
- data/pages/Modes.md +1 -1
- data/pages/Options.md +5 -6
- data/pages/Parser.md +2 -2
- data/pages/Rails.md +6 -0
- data/pages/Security.md +21 -7
- metadata +17 -16
data/ext/oj/fast.c
CHANGED
|
@@ -92,12 +92,35 @@ inline static void next_non_white(ParseInfo pi) {
|
|
|
92
92
|
case '\f':
|
|
93
93
|
case '\n':
|
|
94
94
|
case '\r': break;
|
|
95
|
-
case '/':
|
|
95
|
+
case '/':
|
|
96
|
+
skip_comment(pi);
|
|
97
|
+
// A comment that is not terminated by a newline ends on the null
|
|
98
|
+
// terminator. Stop here so the loop does not step past it.
|
|
99
|
+
if ('\0' == *pi->s) {
|
|
100
|
+
return;
|
|
101
|
+
}
|
|
102
|
+
break;
|
|
96
103
|
default: return;
|
|
97
104
|
}
|
|
98
105
|
}
|
|
99
106
|
}
|
|
100
107
|
|
|
108
|
+
inline static bool is_value_end(char c) {
|
|
109
|
+
switch (c) {
|
|
110
|
+
case '\0':
|
|
111
|
+
case ' ':
|
|
112
|
+
case '\t':
|
|
113
|
+
case '\f':
|
|
114
|
+
case '\n':
|
|
115
|
+
case '\r':
|
|
116
|
+
case '/':
|
|
117
|
+
case ',':
|
|
118
|
+
case ']':
|
|
119
|
+
case '}': return true;
|
|
120
|
+
default: return false;
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
|
|
101
124
|
inline static char *ulong_fill(char *s, size_t num) {
|
|
102
125
|
char buf[32];
|
|
103
126
|
char *b = buf + sizeof(buf) - 1;
|
|
@@ -210,10 +233,11 @@ static void skip_comment(ParseInfo pi) {
|
|
|
210
233
|
if ('*' == *pi->s && '/' == *(pi->s + 1)) {
|
|
211
234
|
pi->s++;
|
|
212
235
|
return;
|
|
213
|
-
} else if ('\0' == *pi->s) {
|
|
214
|
-
raise_error("comment not terminated", pi->str, pi->s);
|
|
215
236
|
}
|
|
216
237
|
}
|
|
238
|
+
// The loop only ends on the null terminator so the comment was never
|
|
239
|
+
// closed.
|
|
240
|
+
raise_error("comment not terminated", pi->str, pi->s);
|
|
217
241
|
} else if ('/' == *pi->s) {
|
|
218
242
|
for (; 1; pi->s++) {
|
|
219
243
|
switch (*pi->s) {
|
|
@@ -455,27 +479,42 @@ static Leaf read_num(ParseInfo pi) {
|
|
|
455
479
|
char *start = pi->s;
|
|
456
480
|
int type = T_FIXNUM;
|
|
457
481
|
Leaf leaf;
|
|
482
|
+
int dcnt = 0;
|
|
483
|
+
int ecnt = 1;
|
|
458
484
|
|
|
459
|
-
|
|
485
|
+
// read_next() sends a leading '+' here as well, and Oj.load() takes one.
|
|
486
|
+
if ('-' == *pi->s || '+' == *pi->s) {
|
|
460
487
|
pi->s++;
|
|
461
488
|
}
|
|
462
489
|
// digits
|
|
463
490
|
for (; '0' <= *pi->s && *pi->s <= '9'; pi->s++) {
|
|
491
|
+
dcnt++;
|
|
464
492
|
}
|
|
465
493
|
if ('.' == *pi->s) {
|
|
466
494
|
type = T_FLOAT;
|
|
467
495
|
pi->s++;
|
|
468
496
|
for (; '0' <= *pi->s && *pi->s <= '9'; pi->s++) {
|
|
497
|
+
dcnt++;
|
|
469
498
|
}
|
|
470
499
|
}
|
|
471
500
|
if ('e' == *pi->s || 'E' == *pi->s) {
|
|
501
|
+
// An exponent makes it a float whether or not a '.' was seen. Without
|
|
502
|
+
// this leaf_fixnum_value() stops reading at the 'e' and the exponent is
|
|
503
|
+
// dropped.
|
|
504
|
+
type = T_FLOAT;
|
|
472
505
|
pi->s++;
|
|
473
506
|
if ('-' == *pi->s || '+' == *pi->s) {
|
|
474
507
|
pi->s++;
|
|
475
508
|
}
|
|
476
|
-
for (; '0' <= *pi->s && *pi->s <= '9'; pi->s++) {
|
|
509
|
+
for (ecnt = 0; '0' <= *pi->s && *pi->s <= '9'; pi->s++) {
|
|
510
|
+
ecnt++;
|
|
477
511
|
}
|
|
478
512
|
}
|
|
513
|
+
// A float is converted with rb_cstr_to_dbl(), which has to be given
|
|
514
|
+
// something it can convert.
|
|
515
|
+
if (T_FLOAT == type && (0 == dcnt || 0 == ecnt || !is_value_end(*pi->s))) {
|
|
516
|
+
raise_error("invalid number", pi->str, pi->s);
|
|
517
|
+
}
|
|
479
518
|
leaf = leaf_new(pi->doc, type);
|
|
480
519
|
leaf->str = start;
|
|
481
520
|
|
|
@@ -662,7 +701,11 @@ static void doc_free(Doc doc) {
|
|
|
662
701
|
static VALUE protect_open_proc(VALUE x) {
|
|
663
702
|
ParseInfo pi = (ParseInfo)x;
|
|
664
703
|
|
|
665
|
-
pi->doc->data
|
|
704
|
+
pi->doc->data = read_next(pi); // parse
|
|
705
|
+
// read_obj() and read_array() terminate each value where it ends. The top
|
|
706
|
+
// level value has no closing delimiter to do that on, so a number leaf runs
|
|
707
|
+
// to the end of the document unless it is terminated here.
|
|
708
|
+
*pi->s = '\0';
|
|
666
709
|
*pi->doc->where = pi->doc->data;
|
|
667
710
|
pi->doc->where = pi->doc->where_path;
|
|
668
711
|
if (rb_block_given_p()) {
|
|
@@ -710,21 +753,23 @@ static void mark_doc(void *ptr) {
|
|
|
710
753
|
}
|
|
711
754
|
#ifdef HAVE_RB_GC_MARK_MOVABLE
|
|
712
755
|
static void compact_leaf(Leaf leaf) {
|
|
713
|
-
|
|
714
|
-
|
|
715
|
-
|
|
716
|
-
|
|
717
|
-
|
|
756
|
+
if (NULL != leaf) {
|
|
757
|
+
switch (leaf->value_type) {
|
|
758
|
+
case COL_VAL:
|
|
759
|
+
if (NULL != leaf->elements) {
|
|
760
|
+
Leaf first = leaf->elements->next;
|
|
761
|
+
Leaf e = first;
|
|
718
762
|
|
|
719
|
-
|
|
720
|
-
|
|
721
|
-
|
|
722
|
-
|
|
723
|
-
|
|
724
|
-
|
|
725
|
-
|
|
763
|
+
do {
|
|
764
|
+
compact_leaf(e);
|
|
765
|
+
e = e->next;
|
|
766
|
+
} while (e != first);
|
|
767
|
+
}
|
|
768
|
+
break;
|
|
769
|
+
case RUBY_VAL: leaf->value = rb_gc_location(leaf->value); break;
|
|
726
770
|
|
|
727
|
-
|
|
771
|
+
default: break;
|
|
772
|
+
}
|
|
728
773
|
}
|
|
729
774
|
}
|
|
730
775
|
|
|
@@ -790,14 +835,13 @@ static VALUE parse_json(VALUE clas, char *json, bool given) {
|
|
|
790
835
|
doc->self = self;
|
|
791
836
|
result = rb_protect(protect_open_proc, (VALUE)&pi, &ex);
|
|
792
837
|
if (given || 0 != ex) {
|
|
793
|
-
|
|
794
|
-
//
|
|
795
|
-
|
|
796
|
-
|
|
797
|
-
|
|
798
|
-
|
|
838
|
+
// The doc is detached from its wrapper so the GC will not free it.
|
|
839
|
+
// doc_free() releases both the doc and its json buffer (doc->json), so
|
|
840
|
+
// the caller must not free json separately.
|
|
841
|
+
if (NULL != DATA_PTR(self)) {
|
|
842
|
+
DATA_PTR(self) = NULL;
|
|
843
|
+
doc_free(doc);
|
|
799
844
|
}
|
|
800
|
-
*/
|
|
801
845
|
} else {
|
|
802
846
|
result = doc->self;
|
|
803
847
|
}
|
|
@@ -865,7 +909,7 @@ static bool key_match(const char *pat, const char *key, int plen) {
|
|
|
865
909
|
static Leaf get_leaf(Leaf *stack, Leaf *lp, const char *path) {
|
|
866
910
|
Leaf leaf = *lp;
|
|
867
911
|
|
|
868
|
-
if (MAX_STACK <= lp - stack) {
|
|
912
|
+
if (MAX_STACK <= (lp + 1) - stack) {
|
|
869
913
|
rb_raise(rb_const_get_at(Oj, rb_intern("DepthError")), "Path too deep. Limit is %d levels.", MAX_STACK);
|
|
870
914
|
}
|
|
871
915
|
if ('\0' != *path) {
|
|
@@ -946,6 +990,7 @@ static void each_leaf(Doc doc, VALUE self) {
|
|
|
946
990
|
|
|
947
991
|
doc->where++;
|
|
948
992
|
if (MAX_STACK <= doc->where - doc->where_path) {
|
|
993
|
+
doc->where--;
|
|
949
994
|
rb_raise(rb_const_get_at(Oj, rb_intern("DepthError")), "Path too deep. Limit is %d levels.", MAX_STACK);
|
|
950
995
|
}
|
|
951
996
|
do {
|
|
@@ -964,7 +1009,7 @@ static void each_leaf(Doc doc, VALUE self) {
|
|
|
964
1009
|
}
|
|
965
1010
|
|
|
966
1011
|
static int move_step(Doc doc, const char *path, int loc) {
|
|
967
|
-
if (MAX_STACK <= doc->where - doc->where_path) {
|
|
1012
|
+
if (MAX_STACK <= (doc->where + 1) - doc->where_path) {
|
|
968
1013
|
rb_raise(rb_const_get_at(Oj, rb_intern("DepthError")), "Path too deep. Limit is %d levels.", MAX_STACK);
|
|
969
1014
|
}
|
|
970
1015
|
if ('\0' == *path) {
|
|
@@ -972,8 +1017,9 @@ static int move_step(Doc doc, const char *path, int loc) {
|
|
|
972
1017
|
} else {
|
|
973
1018
|
Leaf leaf;
|
|
974
1019
|
|
|
1020
|
+
// A document with no root (empty or comment only JSON) has no leaf to
|
|
1021
|
+
// step from so the path can not be located.
|
|
975
1022
|
if (0 == doc->where || 0 == (leaf = *doc->where)) {
|
|
976
|
-
printf("*** Internal error at %s\n", path);
|
|
977
1023
|
return loc;
|
|
978
1024
|
}
|
|
979
1025
|
if ('.' == *path && '.' == *(path + 1)) {
|
|
@@ -990,8 +1036,8 @@ static int move_step(Doc doc, const char *path, int loc) {
|
|
|
990
1036
|
doc->where--;
|
|
991
1037
|
loc = move_step(doc, path, loc + 1);
|
|
992
1038
|
if (0 != loc) {
|
|
993
|
-
*doc->where = init;
|
|
994
1039
|
doc->where++;
|
|
1040
|
+
*doc->where = init;
|
|
995
1041
|
}
|
|
996
1042
|
} else if (COL_VAL == leaf->value_type && 0 != leaf->elements) {
|
|
997
1043
|
Leaf first = leaf->elements->next;
|
|
@@ -1104,12 +1150,7 @@ static VALUE doc_open(VALUE clas, VALUE str) {
|
|
|
1104
1150
|
|
|
1105
1151
|
memcpy(json, StringValuePtr(str), len);
|
|
1106
1152
|
obj = parse_json(clas, json, given);
|
|
1107
|
-
//
|
|
1108
|
-
/*
|
|
1109
|
-
if (given) {
|
|
1110
|
-
OJ_R_FREE(json);
|
|
1111
|
-
}
|
|
1112
|
-
*/
|
|
1153
|
+
// json is owned by the doc and freed by doc_free(); do not free it here.
|
|
1113
1154
|
return obj;
|
|
1114
1155
|
}
|
|
1115
1156
|
|
|
@@ -1140,17 +1181,24 @@ static VALUE doc_open_file(VALUE clas, VALUE filename) {
|
|
|
1140
1181
|
volatile VALUE obj;
|
|
1141
1182
|
int given = rb_block_given_p();
|
|
1142
1183
|
|
|
1143
|
-
path =
|
|
1144
|
-
|
|
1184
|
+
path = StringValueCStr(filename);
|
|
1185
|
+
// Open in binary mode. On Windows a text mode read translates CRLF into LF
|
|
1186
|
+
// so fewer bytes are read than the file size reported by ftell().
|
|
1187
|
+
if (0 == (f = fopen(path, "rb"))) {
|
|
1145
1188
|
rb_raise(rb_eIOError, "%s", strerror(errno));
|
|
1146
1189
|
}
|
|
1147
1190
|
fseek(f, 0, SEEK_END);
|
|
1148
|
-
len
|
|
1191
|
+
len = ftell(f);
|
|
1192
|
+
if ((size_t)-1 == len) {
|
|
1193
|
+
fclose(f);
|
|
1194
|
+
rb_raise(rb_const_get_at(Oj, rb_intern("LoadError")), "Failed to get the size of %s.", path);
|
|
1195
|
+
}
|
|
1149
1196
|
json = OJ_R_ALLOC_N(char, len + 1);
|
|
1150
1197
|
|
|
1151
1198
|
fseek(f, 0, SEEK_SET);
|
|
1152
1199
|
if (len != fread(json, 1, len, f)) {
|
|
1153
1200
|
fclose(f);
|
|
1201
|
+
OJ_R_FREE(json);
|
|
1154
1202
|
rb_raise(rb_const_get_at(Oj, rb_intern("LoadError")),
|
|
1155
1203
|
"Failed to read %lu bytes from %s.",
|
|
1156
1204
|
(unsigned long)len,
|
|
@@ -1159,12 +1207,7 @@ static VALUE doc_open_file(VALUE clas, VALUE filename) {
|
|
|
1159
1207
|
fclose(f);
|
|
1160
1208
|
json[len] = '\0';
|
|
1161
1209
|
obj = parse_json(clas, json, given);
|
|
1162
|
-
//
|
|
1163
|
-
/*
|
|
1164
|
-
if (given) {
|
|
1165
|
-
OJ_R_FREE(json);
|
|
1166
|
-
}
|
|
1167
|
-
*/
|
|
1210
|
+
// json is owned by the doc and freed by doc_free(); do not free it here.
|
|
1168
1211
|
return obj;
|
|
1169
1212
|
}
|
|
1170
1213
|
|
|
@@ -1263,10 +1306,14 @@ static VALUE doc_path(VALUE self) {
|
|
|
1263
1306
|
* #=> nil
|
|
1264
1307
|
*/
|
|
1265
1308
|
static VALUE doc_local_key(VALUE self) {
|
|
1266
|
-
Doc doc
|
|
1267
|
-
Leaf leaf
|
|
1268
|
-
volatile VALUE key
|
|
1309
|
+
Doc doc = self_doc(self);
|
|
1310
|
+
Leaf leaf;
|
|
1311
|
+
volatile VALUE key = Qnil;
|
|
1269
1312
|
|
|
1313
|
+
if (NULL == doc->where || NULL == *doc->where) {
|
|
1314
|
+
return Qnil;
|
|
1315
|
+
}
|
|
1316
|
+
leaf = *doc->where;
|
|
1270
1317
|
if (T_HASH == leaf->parent_type) {
|
|
1271
1318
|
key = rb_utf8_str_new_cstr(leaf->key);
|
|
1272
1319
|
} else if (T_ARRAY == leaf->parent_type) {
|
|
@@ -1304,7 +1351,7 @@ static VALUE doc_home(VALUE self) {
|
|
|
1304
1351
|
* Oj::Doc.open('[1,2]') { |doc| doc.type('/1') } #=> Fixnum
|
|
1305
1352
|
*/
|
|
1306
1353
|
static VALUE doc_type(int argc, VALUE *argv, VALUE self) {
|
|
1307
|
-
Doc doc
|
|
1354
|
+
Doc doc;
|
|
1308
1355
|
Leaf leaf;
|
|
1309
1356
|
const char *path = 0;
|
|
1310
1357
|
VALUE type = Qnil;
|
|
@@ -1312,6 +1359,7 @@ static VALUE doc_type(int argc, VALUE *argv, VALUE self) {
|
|
|
1312
1359
|
if (1 <= argc) {
|
|
1313
1360
|
path = StringValuePtr(*argv);
|
|
1314
1361
|
}
|
|
1362
|
+
doc = self_doc(self);
|
|
1315
1363
|
if (0 != (leaf = get_doc_leaf(doc, path))) {
|
|
1316
1364
|
switch (leaf->rtype) {
|
|
1317
1365
|
case T_NIL: type = rb_cNilClass; break;
|
|
@@ -1347,13 +1395,13 @@ static VALUE doc_fetch(int argc, VALUE *argv, VALUE self) {
|
|
|
1347
1395
|
volatile VALUE val = Qnil;
|
|
1348
1396
|
const char *path = 0;
|
|
1349
1397
|
|
|
1350
|
-
doc = self_doc(self);
|
|
1351
1398
|
if (1 <= argc) {
|
|
1352
1399
|
path = StringValuePtr(*argv);
|
|
1353
1400
|
if (2 == argc) {
|
|
1354
1401
|
val = argv[1];
|
|
1355
1402
|
}
|
|
1356
1403
|
}
|
|
1404
|
+
doc = self_doc(self);
|
|
1357
1405
|
if (0 != (leaf = get_doc_leaf(doc, path))) {
|
|
1358
1406
|
val = leaf_value(doc, leaf);
|
|
1359
1407
|
}
|
|
@@ -1369,11 +1417,11 @@ static VALUE doc_fetch(int argc, VALUE *argv, VALUE self) {
|
|
|
1369
1417
|
* Oj::Doc.open('[1,2]') { |doc| doc.exists?('/3') } #=> false
|
|
1370
1418
|
*/
|
|
1371
1419
|
static VALUE doc_exists(VALUE self, VALUE str) {
|
|
1372
|
-
|
|
1373
|
-
|
|
1420
|
+
const char *path = StringValuePtr(str);
|
|
1421
|
+
Doc doc = self_doc(self);
|
|
1422
|
+
Leaf leaf;
|
|
1374
1423
|
|
|
1375
|
-
|
|
1376
|
-
if (0 != (leaf = get_doc_leaf(doc, StringValuePtr(str)))) {
|
|
1424
|
+
if (0 != (leaf = get_doc_leaf(doc, path))) {
|
|
1377
1425
|
if (NULL != leaf) {
|
|
1378
1426
|
return Qtrue;
|
|
1379
1427
|
}
|
|
@@ -1400,16 +1448,19 @@ static VALUE doc_exists(VALUE self, VALUE str) {
|
|
|
1400
1448
|
static VALUE doc_each_leaf(int argc, VALUE *argv, VALUE self) {
|
|
1401
1449
|
if (rb_block_given_p()) {
|
|
1402
1450
|
Leaf save_path[MAX_STACK];
|
|
1403
|
-
Doc doc
|
|
1451
|
+
Doc doc;
|
|
1404
1452
|
const char *path = 0;
|
|
1405
1453
|
size_t wlen;
|
|
1406
1454
|
|
|
1455
|
+
if (1 <= argc) {
|
|
1456
|
+
path = StringValuePtr(*argv);
|
|
1457
|
+
}
|
|
1458
|
+
doc = self_doc(self);
|
|
1407
1459
|
wlen = doc->where - doc->where_path;
|
|
1408
1460
|
if (0 < wlen) {
|
|
1409
1461
|
memcpy(save_path, doc->where_path, sizeof(Leaf) * (wlen + 1));
|
|
1410
1462
|
}
|
|
1411
|
-
if (
|
|
1412
|
-
path = StringValuePtr(*argv);
|
|
1463
|
+
if (0 != path) {
|
|
1413
1464
|
if ('/' == *path) {
|
|
1414
1465
|
doc->where = doc->where_path;
|
|
1415
1466
|
path++;
|
|
@@ -1421,6 +1472,9 @@ static VALUE doc_each_leaf(int argc, VALUE *argv, VALUE self) {
|
|
|
1421
1472
|
return Qnil;
|
|
1422
1473
|
}
|
|
1423
1474
|
}
|
|
1475
|
+
if (NULL == doc->where || NULL == *doc->where) {
|
|
1476
|
+
return Qnil;
|
|
1477
|
+
}
|
|
1424
1478
|
each_leaf(doc, self);
|
|
1425
1479
|
if (0 < wlen) {
|
|
1426
1480
|
memcpy(doc->where_path, save_path, sizeof(Leaf) * (wlen + 1));
|
|
@@ -1439,11 +1493,10 @@ static VALUE doc_each_leaf(int argc, VALUE *argv, VALUE self) {
|
|
|
1439
1493
|
* "/one/2"
|
|
1440
1494
|
*/
|
|
1441
1495
|
static VALUE doc_move(VALUE self, VALUE str) {
|
|
1442
|
-
|
|
1443
|
-
|
|
1496
|
+
const char *path = StringValuePtr(str);
|
|
1497
|
+
Doc doc = self_doc(self);
|
|
1444
1498
|
int loc;
|
|
1445
1499
|
|
|
1446
|
-
path = StringValuePtr(str);
|
|
1447
1500
|
if ('/' == *path) {
|
|
1448
1501
|
doc->where = doc->where_path;
|
|
1449
1502
|
path++;
|
|
@@ -1474,17 +1527,21 @@ static VALUE doc_move(VALUE self, VALUE str) {
|
|
|
1474
1527
|
static VALUE doc_each_child(int argc, VALUE *argv, VALUE self) {
|
|
1475
1528
|
if (rb_block_given_p()) {
|
|
1476
1529
|
Leaf save_path[MAX_STACK];
|
|
1477
|
-
Doc doc
|
|
1530
|
+
Doc doc;
|
|
1478
1531
|
const char *path = 0;
|
|
1479
1532
|
size_t wlen;
|
|
1480
|
-
Leaf *where_orig
|
|
1533
|
+
Leaf *where_orig;
|
|
1481
1534
|
|
|
1482
|
-
|
|
1535
|
+
if (1 <= argc) {
|
|
1536
|
+
path = StringValuePtr(*argv);
|
|
1537
|
+
}
|
|
1538
|
+
doc = self_doc(self);
|
|
1539
|
+
where_orig = doc->where;
|
|
1540
|
+
wlen = doc->where - doc->where_path;
|
|
1483
1541
|
if (0 < wlen) {
|
|
1484
1542
|
memcpy(save_path, doc->where_path, sizeof(Leaf) * (wlen + 1));
|
|
1485
1543
|
}
|
|
1486
|
-
if (
|
|
1487
|
-
path = StringValuePtr(*argv);
|
|
1544
|
+
if (0 != path) {
|
|
1488
1545
|
if ('/' == *path) {
|
|
1489
1546
|
doc->where = doc->where_path;
|
|
1490
1547
|
path++;
|
|
@@ -1552,13 +1609,14 @@ static VALUE doc_each_child(int argc, VALUE *argv, VALUE self) {
|
|
|
1552
1609
|
*/
|
|
1553
1610
|
static VALUE doc_each_value(int argc, VALUE *argv, VALUE self) {
|
|
1554
1611
|
if (rb_block_given_p()) {
|
|
1555
|
-
Doc doc
|
|
1612
|
+
Doc doc;
|
|
1556
1613
|
const char *path = 0;
|
|
1557
1614
|
Leaf leaf;
|
|
1558
1615
|
|
|
1559
1616
|
if (1 <= argc) {
|
|
1560
1617
|
path = StringValuePtr(*argv);
|
|
1561
1618
|
}
|
|
1619
|
+
doc = self_doc(self);
|
|
1562
1620
|
if (0 != (leaf = get_doc_leaf(doc, path))) {
|
|
1563
1621
|
each_value(doc, leaf, self);
|
|
1564
1622
|
}
|
|
@@ -1581,19 +1639,23 @@ static VALUE doc_each_value(int argc, VALUE *argv, VALUE self) {
|
|
|
1581
1639
|
* #=> "[2,1]"
|
|
1582
1640
|
*/
|
|
1583
1641
|
static VALUE doc_dump(int argc, VALUE *argv, VALUE self) {
|
|
1584
|
-
Doc doc
|
|
1642
|
+
Doc doc;
|
|
1585
1643
|
Leaf leaf;
|
|
1586
1644
|
const char *path = 0;
|
|
1587
1645
|
const char *filename = 0;
|
|
1588
1646
|
|
|
1589
1647
|
if (1 <= argc) {
|
|
1590
1648
|
if (Qnil != *argv) {
|
|
1591
|
-
|
|
1649
|
+
StringValue(*argv);
|
|
1592
1650
|
}
|
|
1593
1651
|
if (2 <= argc) {
|
|
1594
|
-
filename =
|
|
1652
|
+
filename = StringValueCStr(argv[1]);
|
|
1653
|
+
}
|
|
1654
|
+
if (Qnil != *argv) {
|
|
1655
|
+
path = RSTRING_PTR(*argv);
|
|
1595
1656
|
}
|
|
1596
1657
|
}
|
|
1658
|
+
doc = self_doc(self);
|
|
1597
1659
|
if (0 != (leaf = get_doc_leaf(doc, path))) {
|
|
1598
1660
|
volatile VALUE rjson;
|
|
1599
1661
|
|
|
@@ -1625,9 +1687,7 @@ static VALUE doc_dump(int argc, VALUE *argv, VALUE self) {
|
|
|
1625
1687
|
* Oj::Doc.open('[1,2,3]') { |doc| doc.size() } #=> 4
|
|
1626
1688
|
*/
|
|
1627
1689
|
static VALUE doc_size(VALUE self) {
|
|
1628
|
-
|
|
1629
|
-
TypedData_Get_Struct(self, struct _doc, &oj_doc_type, d);
|
|
1630
|
-
return ULONG2NUM(d->size);
|
|
1690
|
+
return ULONG2NUM(self_doc(self)->size);
|
|
1631
1691
|
}
|
|
1632
1692
|
|
|
1633
1693
|
/* @overload close() => nil
|
data/ext/oj/intern.c
CHANGED
|
@@ -17,9 +17,6 @@
|
|
|
17
17
|
#define HASH_SLOT_CNT ((uint64_t)256)
|
|
18
18
|
#define HASH_MASK (HASH_SLOT_CNT - 1)
|
|
19
19
|
|
|
20
|
-
// almost the Murmur hash algorithm
|
|
21
|
-
#define M 0x5bd1e995
|
|
22
|
-
|
|
23
20
|
typedef struct _keyVal {
|
|
24
21
|
struct _keyVal *next;
|
|
25
22
|
const char *key;
|
|
@@ -50,7 +47,12 @@ static VALUE form_str(const char *str, size_t len) {
|
|
|
50
47
|
}
|
|
51
48
|
|
|
52
49
|
static VALUE form_sym(const char *str, size_t len) {
|
|
53
|
-
|
|
50
|
+
VALUE sym = rb_check_symbol_cstr(str, (long)len, oj_utf8_encoding);
|
|
51
|
+
|
|
52
|
+
if (Qnil == sym) {
|
|
53
|
+
sym = rb_str_intern(rb_utf8_str_new(str, len));
|
|
54
|
+
}
|
|
55
|
+
return sym;
|
|
54
56
|
}
|
|
55
57
|
|
|
56
58
|
static VALUE form_attr(const char *str, size_t len) {
|
|
@@ -97,6 +99,8 @@ static const rb_data_type_t oj_cache_type = {
|
|
|
97
99
|
};
|
|
98
100
|
|
|
99
101
|
void oj_hash_init(void) {
|
|
102
|
+
oj_hash_seed_init();
|
|
103
|
+
|
|
100
104
|
VALUE cache_class = rb_define_class_under(Oj, "Cache", rb_cObject);
|
|
101
105
|
rb_undef_alloc_func(cache_class);
|
|
102
106
|
|
|
@@ -148,46 +152,15 @@ ID oj_attr_intern(const char *key, size_t len) {
|
|
|
148
152
|
return cache_intern(c, key, len);
|
|
149
153
|
}
|
|
150
154
|
|
|
151
|
-
static uint64_t hash_calc(const uint8_t *key, size_t len) {
|
|
152
|
-
const uint8_t *end = key + len;
|
|
153
|
-
const uint8_t *endless = key + (len & 0xFFFFFFFC);
|
|
154
|
-
uint64_t h = (uint64_t)len;
|
|
155
|
-
uint64_t k;
|
|
156
|
-
|
|
157
|
-
while (key < endless) {
|
|
158
|
-
k = (uint64_t)*key++;
|
|
159
|
-
k |= (uint64_t)*key++ << 8;
|
|
160
|
-
k |= (uint64_t)*key++ << 16;
|
|
161
|
-
k |= (uint64_t)*key++ << 24;
|
|
162
|
-
|
|
163
|
-
k *= M;
|
|
164
|
-
k ^= k >> 24;
|
|
165
|
-
h *= M;
|
|
166
|
-
h ^= k * M;
|
|
167
|
-
}
|
|
168
|
-
if (1 < end - key) {
|
|
169
|
-
uint16_t k16 = (uint16_t)*key++;
|
|
170
|
-
|
|
171
|
-
k16 |= (uint16_t)*key++ << 8;
|
|
172
|
-
h ^= k16 << 8;
|
|
173
|
-
}
|
|
174
|
-
if (key < end) {
|
|
175
|
-
h ^= *key;
|
|
176
|
-
}
|
|
177
|
-
h *= M;
|
|
178
|
-
h ^= h >> 13;
|
|
179
|
-
h *= M;
|
|
180
|
-
h ^= h >> 15;
|
|
181
|
-
|
|
182
|
-
return h;
|
|
183
|
-
}
|
|
184
|
-
|
|
185
155
|
static VALUE resolve_classname(VALUE mod, const char *classname, int auto_define) {
|
|
186
156
|
VALUE clas;
|
|
187
157
|
ID ci = rb_intern(classname);
|
|
188
158
|
|
|
189
159
|
if (rb_const_defined_at(mod, ci)) {
|
|
190
160
|
clas = rb_const_get_at(mod, ci);
|
|
161
|
+
if (!RB_TYPE_P(clas, T_CLASS) && !RB_TYPE_P(clas, T_MODULE)) {
|
|
162
|
+
clas = Qundef;
|
|
163
|
+
}
|
|
191
164
|
} else if (auto_define) {
|
|
192
165
|
clas = rb_define_class_under(mod, classname, oj_bag_class);
|
|
193
166
|
} else {
|
|
@@ -238,61 +211,91 @@ static VALUE resolve_classpath(ParseInfo pi, const char *name, size_t len, int a
|
|
|
238
211
|
return clas;
|
|
239
212
|
}
|
|
240
213
|
|
|
214
|
+
typedef struct _internArgs {
|
|
215
|
+
KeyVal bucket; // last bucket in the chain for the key's slot
|
|
216
|
+
const char *key;
|
|
217
|
+
size_t len;
|
|
218
|
+
ParseInfo pi;
|
|
219
|
+
int auto_define;
|
|
220
|
+
VALUE error_class;
|
|
221
|
+
} *InternArgs;
|
|
222
|
+
|
|
223
|
+
// Resolves the class and only then adds it to the cache so a raise while
|
|
224
|
+
// resolving does not leave a key cached with no value.
|
|
225
|
+
static VALUE intern_new_class(VALUE a) {
|
|
226
|
+
InternArgs args = (InternArgs)a;
|
|
227
|
+
KeyVal bucket = args->bucket;
|
|
228
|
+
VALUE clas = resolve_classpath(args->pi, args->key, args->len, args->auto_define, args->error_class);
|
|
229
|
+
|
|
230
|
+
if (Qundef == clas) {
|
|
231
|
+
return clas;
|
|
232
|
+
}
|
|
233
|
+
if (NULL != bucket->key) { // not the top slot
|
|
234
|
+
KeyVal b = (KeyVal)OJ_R_ALLOC_N(char, sizeof(struct _keyVal) + args->len + 1);
|
|
235
|
+
char *key = (char *)(b + 1);
|
|
236
|
+
|
|
237
|
+
memcpy(key, args->key, args->len);
|
|
238
|
+
key[args->len] = '\0';
|
|
239
|
+
b->next = NULL;
|
|
240
|
+
b->key = key;
|
|
241
|
+
bucket->next = b;
|
|
242
|
+
bucket = b;
|
|
243
|
+
} else {
|
|
244
|
+
bucket->key = oj_strndup(args->key, args->len);
|
|
245
|
+
}
|
|
246
|
+
bucket->len = args->len;
|
|
247
|
+
bucket->val = clas;
|
|
248
|
+
|
|
249
|
+
return clas;
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
static KeyVal find_class(KeyVal bucket, const char *key, size_t len, VALUE *clas) {
|
|
253
|
+
if (NULL != bucket->key) { // not the top slot
|
|
254
|
+
for (KeyVal b = bucket; 0 != b; b = b->next) {
|
|
255
|
+
if (len == b->len && 0 == memcmp(b->key, key, len)) {
|
|
256
|
+
*clas = b->val;
|
|
257
|
+
return NULL;
|
|
258
|
+
}
|
|
259
|
+
bucket = b;
|
|
260
|
+
}
|
|
261
|
+
}
|
|
262
|
+
return bucket;
|
|
263
|
+
}
|
|
264
|
+
|
|
241
265
|
VALUE oj_class_intern(const char *key, size_t len, bool safe, ParseInfo pi, int auto_define, VALUE error_class) {
|
|
242
|
-
uint64_t
|
|
243
|
-
|
|
244
|
-
|
|
266
|
+
uint64_t h = oj_hash_calc((const uint8_t *)key, len) & HASH_MASK;
|
|
267
|
+
struct _internArgs args = {NULL, key, len, pi, auto_define, error_class};
|
|
268
|
+
VALUE clas = Qundef;
|
|
245
269
|
|
|
246
270
|
if (safe) {
|
|
271
|
+
int state = 0;
|
|
272
|
+
|
|
247
273
|
#if HAVE_PTHREAD_MUTEX_INIT
|
|
248
274
|
pthread_mutex_lock(&class_hash.mutex);
|
|
249
275
|
#else
|
|
250
276
|
rb_mutex_lock(class_hash.mutex);
|
|
251
277
|
#endif
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
#else
|
|
258
|
-
rb_mutex_unlock(class_hash.mutex);
|
|
259
|
-
#endif
|
|
260
|
-
return b->val;
|
|
261
|
-
}
|
|
262
|
-
bucket = b;
|
|
263
|
-
}
|
|
264
|
-
b = OJ_R_ALLOC(struct _keyVal);
|
|
265
|
-
b->next = NULL;
|
|
266
|
-
bucket->next = b;
|
|
267
|
-
bucket = b;
|
|
278
|
+
// Resolving calls into Ruby and allocating, either of which can
|
|
279
|
+
// raise and unwind past the unlock, so the work is protected and any
|
|
280
|
+
// exception rethrown after the mutex is released.
|
|
281
|
+
if (NULL != (args.bucket = find_class(class_hash.slots + h, key, len, &clas))) {
|
|
282
|
+
clas = rb_protect(intern_new_class, (VALUE)&args, &state);
|
|
268
283
|
}
|
|
269
|
-
bucket->key = oj_strndup(key, len);
|
|
270
|
-
bucket->len = len;
|
|
271
|
-
bucket->val = resolve_classpath(pi, key, len, auto_define, error_class);
|
|
272
284
|
#if HAVE_PTHREAD_MUTEX_INIT
|
|
273
285
|
pthread_mutex_unlock(&class_hash.mutex);
|
|
274
286
|
#else
|
|
275
287
|
rb_mutex_unlock(class_hash.mutex);
|
|
276
288
|
#endif
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
for (b = bucket; 0 != b; b = b->next) {
|
|
280
|
-
if (len == b->len && 0 == strncmp(b->key, key, len)) {
|
|
281
|
-
return (ID)b->val;
|
|
282
|
-
}
|
|
283
|
-
bucket = b;
|
|
284
|
-
}
|
|
285
|
-
b = OJ_R_ALLOC(struct _keyVal);
|
|
286
|
-
b->next = NULL;
|
|
287
|
-
bucket->next = b;
|
|
288
|
-
bucket = b;
|
|
289
|
+
if (0 != state) {
|
|
290
|
+
rb_jump_tag(state);
|
|
289
291
|
}
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
bucket->val = resolve_classpath(pi, key, len, auto_define, error_class);
|
|
292
|
+
} else if (NULL != (args.bucket = find_class(class_hash.slots + h, key, len, &clas))) {
|
|
293
|
+
clas = intern_new_class((VALUE)&args);
|
|
293
294
|
}
|
|
294
|
-
|
|
295
|
-
|
|
295
|
+
if (NULL != args.bucket && Qundef != clas) { // newly added to the cache
|
|
296
|
+
rb_gc_register_mark_object(clas);
|
|
297
|
+
}
|
|
298
|
+
return clas;
|
|
296
299
|
}
|
|
297
300
|
|
|
298
301
|
char *oj_strndup(const char *s, size_t len) {
|