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/parser.c
CHANGED
|
@@ -5,6 +5,7 @@
|
|
|
5
5
|
#include <fcntl.h>
|
|
6
6
|
|
|
7
7
|
#include "oj.h"
|
|
8
|
+
#include "simd.h"
|
|
8
9
|
|
|
9
10
|
#define DEBUG 0
|
|
10
11
|
|
|
@@ -178,8 +179,8 @@ static const char neg_map[258] = "\
|
|
|
178
179
|
static const char zero_map[258] = "\
|
|
179
180
|
.........rs..r..................\
|
|
180
181
|
r...........u.t.................\
|
|
181
|
-
|
|
182
|
-
|
|
182
|
+
.....w.......................H..\
|
|
183
|
+
.....w.......................G..\
|
|
183
184
|
................................\
|
|
184
185
|
................................\
|
|
185
186
|
................................\
|
|
@@ -508,20 +509,29 @@ static void calc_num(ojParser p) {
|
|
|
508
509
|
d = -d;
|
|
509
510
|
}
|
|
510
511
|
if (0 < p->num.shift) {
|
|
511
|
-
|
|
512
|
-
}
|
|
513
|
-
if (0 < p->num.exp) {
|
|
514
|
-
long double x;
|
|
512
|
+
uint32_t shift = p->num.shift;
|
|
515
513
|
|
|
516
|
-
|
|
517
|
-
|
|
518
|
-
|
|
519
|
-
x = pow_map[p->num.exp];
|
|
514
|
+
while (MAX_POW < shift) {
|
|
515
|
+
d /= pow_map[MAX_POW];
|
|
516
|
+
shift -= MAX_POW;
|
|
520
517
|
}
|
|
518
|
+
d /= pow_map[shift];
|
|
519
|
+
}
|
|
520
|
+
if (0 < p->num.exp) {
|
|
521
521
|
if (p->num.exp_neg) {
|
|
522
|
-
|
|
522
|
+
int exp = p->num.exp;
|
|
523
|
+
|
|
524
|
+
while (MAX_POW < exp) {
|
|
525
|
+
d /= pow_map[MAX_POW];
|
|
526
|
+
exp -= MAX_POW;
|
|
527
|
+
}
|
|
528
|
+
d /= pow_map[exp];
|
|
523
529
|
} else {
|
|
524
|
-
|
|
530
|
+
if (MAX_POW < p->num.exp) {
|
|
531
|
+
d *= powl(10.0L, (long double)p->num.exp);
|
|
532
|
+
} else {
|
|
533
|
+
d *= pow_map[p->num.exp];
|
|
534
|
+
}
|
|
525
535
|
}
|
|
526
536
|
}
|
|
527
537
|
p->num.dub = d;
|
|
@@ -558,19 +568,31 @@ static void big_change(ojParser p) {
|
|
|
558
568
|
break;
|
|
559
569
|
case OJ_DECIMAL: {
|
|
560
570
|
int shift = p->num.shift;
|
|
571
|
+
int end = sizeof(buf) - 1;
|
|
561
572
|
|
|
562
|
-
|
|
563
|
-
|
|
564
|
-
|
|
565
|
-
|
|
566
|
-
|
|
567
|
-
buf[len] = '0' + (i % 10);
|
|
568
|
-
}
|
|
573
|
+
len = end;
|
|
574
|
+
do {
|
|
575
|
+
buf[--len] = '0' + (i % 10);
|
|
576
|
+
i /= 10;
|
|
577
|
+
} while (0 < i);
|
|
569
578
|
if (p->num.neg) {
|
|
570
|
-
buf
|
|
571
|
-
|
|
579
|
+
buf_append(&p->buf, '-');
|
|
580
|
+
}
|
|
581
|
+
if (end - len <= shift) {
|
|
582
|
+
buf_append_string(&p->buf, "0.", 2);
|
|
583
|
+
for (int cnt = shift - (end - len); 0 < cnt; cnt--) {
|
|
584
|
+
buf_append(&p->buf, '0');
|
|
585
|
+
}
|
|
586
|
+
buf_append_string(&p->buf, buf + len, end - len);
|
|
587
|
+
} else {
|
|
588
|
+
int decimal = end - shift;
|
|
589
|
+
|
|
590
|
+
buf_append_string(&p->buf, buf + len, decimal - len);
|
|
591
|
+
if (0 < shift) {
|
|
592
|
+
buf_append(&p->buf, '.');
|
|
593
|
+
buf_append_string(&p->buf, buf + decimal, shift);
|
|
594
|
+
}
|
|
572
595
|
}
|
|
573
|
-
buf_append_string(&p->buf, buf + len + 1, sizeof(buf) - len - 1);
|
|
574
596
|
if (0 < p->num.exp) {
|
|
575
597
|
int x = p->num.exp;
|
|
576
598
|
int d, div;
|
|
@@ -584,6 +606,7 @@ static void big_change(ojParser p) {
|
|
|
584
606
|
d = x / div % 10;
|
|
585
607
|
if (started || 0 < d) {
|
|
586
608
|
buf_append(&p->buf, '0' + d);
|
|
609
|
+
started = true;
|
|
587
610
|
}
|
|
588
611
|
}
|
|
589
612
|
}
|
|
@@ -594,9 +617,64 @@ static void big_change(ojParser p) {
|
|
|
594
617
|
}
|
|
595
618
|
}
|
|
596
619
|
|
|
597
|
-
|
|
620
|
+
// Scan forward over string content, returning the first byte that is not
|
|
621
|
+
// STR_OK in string_map. This is a pure drop-in for the scalar loop
|
|
622
|
+
//
|
|
623
|
+
// for (; STR_OK == string_map[*b]; b++) {}
|
|
624
|
+
//
|
|
625
|
+
// and must return the exact same stop position. The stop set (bytes whose
|
|
626
|
+
// string_map class is not 'R'/STR_OK) is:
|
|
627
|
+
//
|
|
628
|
+
// * control bytes 0x00..0x1F (class '.', also stops at the NUL terminator)
|
|
629
|
+
// * the quote 0x22 '"' (class 'z')
|
|
630
|
+
// * the backslash 0x5C '\' (class 'A')
|
|
631
|
+
// * every high byte 0x80..0xFF (UTF-8 lead/continuation, classes M/P/Q/'.')
|
|
632
|
+
//
|
|
633
|
+
// Note this differs from parse.c's string_scan_neon, which stops only on
|
|
634
|
+
// \0 \\ " -- parser.c hands multi-byte UTF-8 to its state machine, so the
|
|
635
|
+
// scanner must stop on the high bytes too. The predictor below is derived from
|
|
636
|
+
// string_map, not copied from parse.c.
|
|
637
|
+
//
|
|
638
|
+
// The NEON path only loads a full 16-byte vector when [b, b+16) stays within
|
|
639
|
+
// [., end), so it never reads past the string's allocation; the sub-16-byte
|
|
640
|
+
// tail (and the whole scan on non-NEON builds) uses the scalar loop, which
|
|
641
|
+
// stops naturally at the guaranteed NUL terminator.
|
|
642
|
+
static inline const byte *oj_scan_str_simd(const byte *b, const byte *end) {
|
|
643
|
+
#ifdef HAVE_SIMD_NEON
|
|
644
|
+
if (SIMD_NEON == SIMD_Impl) {
|
|
645
|
+
const uint8x16_t quote = vdupq_n_u8('"');
|
|
646
|
+
const uint8x16_t bslash = vdupq_n_u8('\\');
|
|
647
|
+
const uint8x16_t space = vdupq_n_u8(0x20);
|
|
648
|
+
const uint8x16_t high = vdupq_n_u8(0x80);
|
|
649
|
+
|
|
650
|
+
while (b + sizeof(uint8x16_t) <= end) {
|
|
651
|
+
const uint8x16_t chunk = vld1q_u8((const uint8_t *)b);
|
|
652
|
+
// special lane == 0xFF for any byte that stops the scan.
|
|
653
|
+
const uint8x16_t special = vorrq_u8(vorrq_u8(vceqq_u8(chunk, quote), vceqq_u8(chunk, bslash)),
|
|
654
|
+
vorrq_u8(vcltq_u8(chunk, space), vcgeq_u8(chunk, high)));
|
|
655
|
+
// Reduce to a 64-bit mask with 4 bits per lane (same idiom as
|
|
656
|
+
// parse.c's string_scan_neon) and locate the first set lane.
|
|
657
|
+
const uint8x8_t res = vshrn_n_u16(vreinterpretq_u16_u8(special), 4);
|
|
658
|
+
uint64_t mask = vget_lane_u64(vreinterpret_u64_u8(res), 0);
|
|
659
|
+
if (0 != mask) {
|
|
660
|
+
mask &= 0x8888888888888888ull;
|
|
661
|
+
return b + (OJ_CTZ64(mask) >> 2);
|
|
662
|
+
}
|
|
663
|
+
b += sizeof(uint8x16_t);
|
|
664
|
+
}
|
|
665
|
+
}
|
|
666
|
+
#else
|
|
667
|
+
(void)end;
|
|
668
|
+
#endif
|
|
669
|
+
for (; STR_OK == string_map[*b]; b++) {
|
|
670
|
+
}
|
|
671
|
+
return b;
|
|
672
|
+
}
|
|
673
|
+
|
|
674
|
+
static void parse(ojParser p, const byte *json, size_t len, bool more) {
|
|
598
675
|
const byte *start;
|
|
599
|
-
const byte *b
|
|
676
|
+
const byte *b = json;
|
|
677
|
+
const byte *end = json + len;
|
|
600
678
|
int i;
|
|
601
679
|
|
|
602
680
|
p->line = 1;
|
|
@@ -629,8 +707,7 @@ static void parse(ojParser p, const byte *json, bool more) {
|
|
|
629
707
|
b++;
|
|
630
708
|
p->key.tail = p->key.head;
|
|
631
709
|
start = b;
|
|
632
|
-
|
|
633
|
-
}
|
|
710
|
+
b = oj_scan_str_simd(b, end);
|
|
634
711
|
buf_append_string(&p->key, (const char *)start, b - start);
|
|
635
712
|
if ('"' == *b) {
|
|
636
713
|
p->map = colon_map;
|
|
@@ -654,8 +731,7 @@ static void parse(ojParser p, const byte *json, bool more) {
|
|
|
654
731
|
b++;
|
|
655
732
|
start = b;
|
|
656
733
|
p->buf.tail = p->buf.head;
|
|
657
|
-
|
|
658
|
-
}
|
|
734
|
+
b = oj_scan_str_simd(b, end);
|
|
659
735
|
buf_append_string(&p->buf, (const char *)start, b - start);
|
|
660
736
|
if ('"' == *b) {
|
|
661
737
|
p->cur = b - json;
|
|
@@ -686,6 +762,12 @@ static void parse(ojParser p, const byte *json, bool more) {
|
|
|
686
762
|
calc_num(p);
|
|
687
763
|
// flow through
|
|
688
764
|
case CLOSE_OBJECT:
|
|
765
|
+
// Inside an object value_map is only in use right after a colon.
|
|
766
|
+
if (value_map == p->map && 0 < p->depth && OBJECT_FUN == p->stack[p->depth]) {
|
|
767
|
+
p->col = b - json - p->col + 1;
|
|
768
|
+
parse_error(p, "expected a value, not an object close");
|
|
769
|
+
return;
|
|
770
|
+
}
|
|
689
771
|
p->map = (1 == p->depth) ? value_map : after_map;
|
|
690
772
|
if (p->depth <= 0 || OBJECT_FUN != p->stack[p->depth]) {
|
|
691
773
|
p->col = b - json - p->col + 1;
|
|
@@ -805,6 +887,11 @@ static void parse(ojParser p, const byte *json, bool more) {
|
|
|
805
887
|
p->num.shift++;
|
|
806
888
|
} else {
|
|
807
889
|
big_change(p);
|
|
890
|
+
if (0 == p->num.shift) {
|
|
891
|
+
// No fraction digit was read yet so big_change()
|
|
892
|
+
// did not add the '.' before the digits that follow.
|
|
893
|
+
buf_append(&p->buf, '.');
|
|
894
|
+
}
|
|
808
895
|
p->map = big_frac_map;
|
|
809
896
|
break;
|
|
810
897
|
}
|
|
@@ -817,6 +904,7 @@ static void parse(ojParser p, const byte *json, bool more) {
|
|
|
817
904
|
break;
|
|
818
905
|
case NUM_ZERO: p->map = zero_map; break;
|
|
819
906
|
case NEG_DIGIT:
|
|
907
|
+
p->map = digit_map;
|
|
820
908
|
for (; NUM_DIGIT == digit_map[*b]; b++) {
|
|
821
909
|
uint64_t x = p->num.fixnum * 10 + (uint64_t)(*b - '0');
|
|
822
910
|
|
|
@@ -829,7 +917,6 @@ static void parse(ojParser p, const byte *json, bool more) {
|
|
|
829
917
|
}
|
|
830
918
|
}
|
|
831
919
|
b--;
|
|
832
|
-
p->map = digit_map;
|
|
833
920
|
break;
|
|
834
921
|
case EXP_SIGN:
|
|
835
922
|
p->num.exp_neg = ('-' == *b);
|
|
@@ -838,10 +925,10 @@ static void parse(ojParser p, const byte *json, bool more) {
|
|
|
838
925
|
case EXP_DIGIT:
|
|
839
926
|
p->map = exp_map;
|
|
840
927
|
for (; NUM_DIGIT == digit_map[*b]; b++) {
|
|
841
|
-
|
|
928
|
+
int x = p->num.exp * 10 + (*b - '0');
|
|
842
929
|
|
|
843
930
|
if (x <= MAX_EXP) {
|
|
844
|
-
p->num.exp = x;
|
|
931
|
+
p->num.exp = (int16_t)x;
|
|
845
932
|
} else {
|
|
846
933
|
big_change(p);
|
|
847
934
|
p->map = big_exp_map;
|
|
@@ -888,10 +975,12 @@ static void parse(ojParser p, const byte *json, bool more) {
|
|
|
888
975
|
case NUM_SPC:
|
|
889
976
|
p->cur = b - json;
|
|
890
977
|
calc_num(p);
|
|
978
|
+
p->map = (0 == p->depth) ? value_map : after_map;
|
|
891
979
|
break;
|
|
892
980
|
case NUM_NEWLINE:
|
|
893
981
|
p->cur = b - json;
|
|
894
982
|
calc_num(p);
|
|
983
|
+
p->map = (0 == p->depth) ? value_map : after_map;
|
|
895
984
|
b++;
|
|
896
985
|
#ifdef SPACE_JUMP
|
|
897
986
|
// for (uint32_t *sj = (uint32_t*)b; 0x20202020 == *sj; sj++) { b += 4; }
|
|
@@ -905,8 +994,7 @@ static void parse(ojParser p, const byte *json, bool more) {
|
|
|
905
994
|
break;
|
|
906
995
|
case STR_OK:
|
|
907
996
|
start = b;
|
|
908
|
-
|
|
909
|
-
}
|
|
997
|
+
b = oj_scan_str_simd(b, end);
|
|
910
998
|
if (':' == p->next_map[256]) {
|
|
911
999
|
buf_append_string(&p->key, (const char *)start, b - start);
|
|
912
1000
|
} else {
|
|
@@ -935,15 +1023,26 @@ static void parse(ojParser p, const byte *json, bool more) {
|
|
|
935
1023
|
p->ri++;
|
|
936
1024
|
p->ucode = p->ucode << 4 | (uint32_t)hex_map[*b];
|
|
937
1025
|
if (4 <= p->ri) {
|
|
938
|
-
byte
|
|
939
|
-
|
|
1026
|
+
byte utf8[8];
|
|
1027
|
+
Buf buf = (':' == p->next_map[256]) ? &p->key : &p->buf;
|
|
1028
|
+
uint32_t code = p->ucode;
|
|
940
1029
|
|
|
941
|
-
|
|
942
|
-
|
|
943
|
-
|
|
944
|
-
|
|
945
|
-
|
|
1030
|
+
// A low surrogate right after a high one, which the previous
|
|
1031
|
+
// escape appended on its own, completes a pair.
|
|
1032
|
+
if (0xDC00 <= code && code <= 0xDFFF && 3 <= buf_len(buf)) {
|
|
1033
|
+
const byte *hi = (const byte *)buf->tail - 3;
|
|
1034
|
+
|
|
1035
|
+
if (0xED == hi[0] && 0xA0 <= hi[1] && hi[1] <= 0xAF) {
|
|
1036
|
+
uint32_t c1 = ((uint32_t)(hi[1] & 0x0F) << 6) | (hi[2] & 0x3F);
|
|
1037
|
+
|
|
1038
|
+
code = ((c1 << 10) | (code - 0xDC00)) + 0x00010000;
|
|
1039
|
+
buf->tail -= 3;
|
|
946
1040
|
}
|
|
1041
|
+
}
|
|
1042
|
+
size_t ulen = unicodeToUtf8(code, utf8);
|
|
1043
|
+
|
|
1044
|
+
if (0 < ulen) {
|
|
1045
|
+
buf_append_string(buf, (const char *)utf8, ulen);
|
|
947
1046
|
} else {
|
|
948
1047
|
parse_error(p, "invalid unicode");
|
|
949
1048
|
return;
|
|
@@ -1133,11 +1232,9 @@ static void parse(ojParser p, const byte *json, bool more) {
|
|
|
1133
1232
|
case '0':
|
|
1134
1233
|
case 'd':
|
|
1135
1234
|
case 'f':
|
|
1136
|
-
case 'z':
|
|
1137
1235
|
case 'X':
|
|
1138
1236
|
case 'D':
|
|
1139
1237
|
case 'g':
|
|
1140
|
-
case 'B':
|
|
1141
1238
|
case 'Y':
|
|
1142
1239
|
p->cur = b - json;
|
|
1143
1240
|
calc_num(p);
|
|
@@ -1229,7 +1326,8 @@ static int opt_cb(VALUE rkey, VALUE value, VALUE ptr) {
|
|
|
1229
1326
|
* Oj::Parser.new(:usual, cache_keys: true).
|
|
1230
1327
|
*/
|
|
1231
1328
|
static VALUE parser_new(int argc, VALUE *argv, VALUE self) {
|
|
1232
|
-
ojParser
|
|
1329
|
+
ojParser p = OJ_R_ALLOC(struct _ojParser);
|
|
1330
|
+
volatile VALUE pv;
|
|
1233
1331
|
|
|
1234
1332
|
#if HAVE_RB_EXT_RACTOR_SAFE
|
|
1235
1333
|
// This doesn't seem to do anything.
|
|
@@ -1239,6 +1337,10 @@ static VALUE parser_new(int argc, VALUE *argv, VALUE self) {
|
|
|
1239
1337
|
buf_init(&p->key);
|
|
1240
1338
|
buf_init(&p->buf);
|
|
1241
1339
|
p->map = value_map;
|
|
1340
|
+
// Until the parser is wrapped the GC can not free it, so the mode and the
|
|
1341
|
+
// options, both of which raise on a value they do not take, are handled
|
|
1342
|
+
// after the wrap.
|
|
1343
|
+
pv = TypedData_Wrap_Struct(parser_class, &oj_parser_type, p);
|
|
1242
1344
|
|
|
1243
1345
|
if (argc < 1) {
|
|
1244
1346
|
oj_set_parser_validator(p);
|
|
@@ -1279,7 +1381,7 @@ static VALUE parser_new(int argc, VALUE *argv, VALUE self) {
|
|
|
1279
1381
|
rb_hash_foreach(ropts, opt_cb, (VALUE)p);
|
|
1280
1382
|
}
|
|
1281
1383
|
}
|
|
1282
|
-
return
|
|
1384
|
+
return pv;
|
|
1283
1385
|
}
|
|
1284
1386
|
|
|
1285
1387
|
// Create a new parser without setting the delegate. The parser is
|
|
@@ -1382,6 +1484,23 @@ static void validate_primitives_are_complete(ojParser p) {
|
|
|
1382
1484
|
}
|
|
1383
1485
|
}
|
|
1384
1486
|
|
|
1487
|
+
// A top level number that stops after a '-', a '.', an 'e' or an exponent
|
|
1488
|
+
// sign is still waiting for a digit.
|
|
1489
|
+
static void validate_number_is_complete(ojParser p) {
|
|
1490
|
+
if (0 < p->depth) {
|
|
1491
|
+
return;
|
|
1492
|
+
}
|
|
1493
|
+
switch (p->map[256]) {
|
|
1494
|
+
case '-':
|
|
1495
|
+
case '.':
|
|
1496
|
+
case 'x':
|
|
1497
|
+
case 'z':
|
|
1498
|
+
case 'o':
|
|
1499
|
+
case 'B':
|
|
1500
|
+
case 'Z': parse_error(p, "incomplete number"); break;
|
|
1501
|
+
}
|
|
1502
|
+
}
|
|
1503
|
+
|
|
1385
1504
|
static void validate_non_primitives_are_complete(ojParser p) {
|
|
1386
1505
|
if (0 >= p->depth) {
|
|
1387
1506
|
return;
|
|
@@ -1395,6 +1514,7 @@ static void validate_non_primitives_are_complete(ojParser p) {
|
|
|
1395
1514
|
|
|
1396
1515
|
static void validate_document_end(ojParser p) {
|
|
1397
1516
|
validate_primitives_are_complete(p);
|
|
1517
|
+
validate_number_is_complete(p);
|
|
1398
1518
|
validate_non_primitives_are_complete(p);
|
|
1399
1519
|
}
|
|
1400
1520
|
|
|
@@ -1407,19 +1527,20 @@ static void validate_document_end(ojParser p) {
|
|
|
1407
1527
|
*/
|
|
1408
1528
|
static VALUE parser_parse(VALUE self, VALUE json) {
|
|
1409
1529
|
ojParser p;
|
|
1410
|
-
int frozen = OBJ_FROZEN(json);
|
|
1411
1530
|
const byte *ptr;
|
|
1412
1531
|
|
|
1413
|
-
|
|
1532
|
+
StringValue(json);
|
|
1533
|
+
if (!OBJ_FROZEN(json)) {
|
|
1414
1534
|
rb_str_freeze(json);
|
|
1415
1535
|
}
|
|
1416
|
-
ptr = (const byte *)
|
|
1536
|
+
ptr = (const byte *)RSTRING_PTR(json);
|
|
1417
1537
|
|
|
1418
1538
|
TypedData_Get_Struct(self, struct _ojParser, &oj_parser_type, p);
|
|
1419
1539
|
|
|
1420
1540
|
parser_reset(p);
|
|
1421
1541
|
p->start(p);
|
|
1422
|
-
parse(p, ptr, false);
|
|
1542
|
+
parse(p, ptr, (size_t)RSTRING_LEN(json), false);
|
|
1543
|
+
RB_GC_GUARD(json);
|
|
1423
1544
|
validate_document_end(p);
|
|
1424
1545
|
|
|
1425
1546
|
return p->result(p);
|
|
@@ -1440,7 +1561,7 @@ static VALUE load(VALUE self) {
|
|
|
1440
1561
|
while (true) {
|
|
1441
1562
|
rb_funcall(p->reader, oj_readpartial_id, 2, INT2NUM(16385), rbuf);
|
|
1442
1563
|
if (0 < RSTRING_LEN(rbuf)) {
|
|
1443
|
-
parse(p, (byte *)StringValuePtr(rbuf), true);
|
|
1564
|
+
parse(p, (byte *)StringValuePtr(rbuf), (size_t)RSTRING_LEN(rbuf), true);
|
|
1444
1565
|
}
|
|
1445
1566
|
if (Qtrue == rb_funcall(p->reader, oj_eofq_id, 0)) {
|
|
1446
1567
|
if (0 < p->depth) {
|
|
@@ -1471,6 +1592,39 @@ static VALUE parser_load(VALUE self, VALUE reader) {
|
|
|
1471
1592
|
return p->result(p);
|
|
1472
1593
|
}
|
|
1473
1594
|
|
|
1595
|
+
struct _fileParse {
|
|
1596
|
+
ojParser p;
|
|
1597
|
+
const char *path;
|
|
1598
|
+
int fd;
|
|
1599
|
+
};
|
|
1600
|
+
|
|
1601
|
+
static VALUE file_parse(VALUE x) {
|
|
1602
|
+
struct _fileParse *fp = (struct _fileParse *)x;
|
|
1603
|
+
byte buf[16385];
|
|
1604
|
+
size_t size = sizeof(buf) - 1;
|
|
1605
|
+
ssize_t rsize;
|
|
1606
|
+
|
|
1607
|
+
while (true) {
|
|
1608
|
+
if (0 > (rsize = read(fp->fd, buf, size))) {
|
|
1609
|
+
rb_raise(rb_eIOError, "error reading from %s", fp->path);
|
|
1610
|
+
}
|
|
1611
|
+
if (0 == rsize) {
|
|
1612
|
+
break;
|
|
1613
|
+
}
|
|
1614
|
+
buf[rsize] = '\0';
|
|
1615
|
+
parse(fp->p, buf, rsize, true);
|
|
1616
|
+
}
|
|
1617
|
+
validate_document_end(fp->p);
|
|
1618
|
+
|
|
1619
|
+
return fp->p->result(fp->p);
|
|
1620
|
+
}
|
|
1621
|
+
|
|
1622
|
+
static VALUE file_close(VALUE x) {
|
|
1623
|
+
close(((struct _fileParse *)x)->fd);
|
|
1624
|
+
|
|
1625
|
+
return Qnil;
|
|
1626
|
+
}
|
|
1627
|
+
|
|
1474
1628
|
/* Document-method: file(filename)
|
|
1475
1629
|
* call-seq: file(filename)
|
|
1476
1630
|
*
|
|
@@ -1479,13 +1633,14 @@ static VALUE parser_load(VALUE self, VALUE reader) {
|
|
|
1479
1633
|
* Returns the result according to the delegate of the parser.
|
|
1480
1634
|
*/
|
|
1481
1635
|
static VALUE parser_file(VALUE self, VALUE filename) {
|
|
1482
|
-
|
|
1483
|
-
|
|
1484
|
-
|
|
1636
|
+
struct _fileParse fp;
|
|
1637
|
+
ojParser p;
|
|
1638
|
+
const char *path;
|
|
1639
|
+
int fd;
|
|
1485
1640
|
|
|
1486
1641
|
TypedData_Get_Struct(self, struct _ojParser, &oj_parser_type, p);
|
|
1487
1642
|
|
|
1488
|
-
path =
|
|
1643
|
+
path = StringValueCStr(filename);
|
|
1489
1644
|
|
|
1490
1645
|
parser_reset(p);
|
|
1491
1646
|
p->start(p);
|
|
@@ -1500,26 +1655,15 @@ static VALUE parser_file(VALUE self, VALUE filename) {
|
|
|
1500
1655
|
// Use threaded version.
|
|
1501
1656
|
// TBD only if has pthreads
|
|
1502
1657
|
// TBD parse_large(p, fd);
|
|
1658
|
+
close(fd);
|
|
1503
1659
|
return p->result(p);
|
|
1504
1660
|
}
|
|
1505
1661
|
#endif
|
|
1506
|
-
|
|
1507
|
-
|
|
1508
|
-
|
|
1662
|
+
fp.p = p;
|
|
1663
|
+
fp.path = path;
|
|
1664
|
+
fp.fd = fd;
|
|
1509
1665
|
|
|
1510
|
-
|
|
1511
|
-
if (0 < (rsize = read(fd, buf, size))) {
|
|
1512
|
-
buf[rsize] = '\0';
|
|
1513
|
-
parse(p, buf, true);
|
|
1514
|
-
}
|
|
1515
|
-
if (rsize <= 0) {
|
|
1516
|
-
if (0 != rsize) {
|
|
1517
|
-
rb_raise(rb_eIOError, "error reading from %s", path);
|
|
1518
|
-
}
|
|
1519
|
-
break;
|
|
1520
|
-
}
|
|
1521
|
-
}
|
|
1522
|
-
return p->result(p);
|
|
1666
|
+
return rb_ensure(file_parse, (VALUE)&fp, file_close, (VALUE)&fp);
|
|
1523
1667
|
}
|
|
1524
1668
|
|
|
1525
1669
|
/* Document-method: just_one
|
|
@@ -1632,15 +1776,19 @@ static VALUE parser_safe(int argc, VALUE *argv, VALUE self) {
|
|
|
1632
1776
|
options = rb_hash_new();
|
|
1633
1777
|
}
|
|
1634
1778
|
|
|
1635
|
-
ojParser
|
|
1779
|
+
ojParser p = OJ_R_ALLOC(struct _ojParser);
|
|
1780
|
+
volatile VALUE pv;
|
|
1636
1781
|
|
|
1637
1782
|
memset(p, 0, sizeof(struct _ojParser));
|
|
1638
1783
|
buf_init(&p->key);
|
|
1639
1784
|
buf_init(&p->buf);
|
|
1640
1785
|
p->map = value_map;
|
|
1786
|
+
// The limits raise on a value that is not an Integer, so the parser has to
|
|
1787
|
+
// be wrapped before they are read.
|
|
1788
|
+
pv = TypedData_Wrap_Struct(parser_class, &oj_parser_type, p);
|
|
1641
1789
|
oj_set_parser_safe(p, options);
|
|
1642
1790
|
|
|
1643
|
-
return
|
|
1791
|
+
return pv;
|
|
1644
1792
|
}
|
|
1645
1793
|
|
|
1646
1794
|
/* Document-class: Oj::Parser
|
data/ext/oj/parser.h
CHANGED