oj 3.13.7 → 3.13.23

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.
Files changed (83) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +75 -0
  3. data/README.md +11 -0
  4. data/ext/oj/buf.h +4 -0
  5. data/ext/oj/circarray.c +1 -1
  6. data/ext/oj/code.c +15 -22
  7. data/ext/oj/compat.c +10 -10
  8. data/ext/oj/custom.c +66 -112
  9. data/ext/oj/dump.c +147 -184
  10. data/ext/oj/dump.h +25 -8
  11. data/ext/oj/dump_compat.c +47 -89
  12. data/ext/oj/dump_leaf.c +14 -58
  13. data/ext/oj/dump_object.c +72 -188
  14. data/ext/oj/dump_strict.c +19 -31
  15. data/ext/oj/encoder.c +43 -0
  16. data/ext/oj/extconf.rb +5 -4
  17. data/ext/oj/fast.c +36 -24
  18. data/ext/oj/intern.c +22 -12
  19. data/ext/oj/intern.h +1 -1
  20. data/ext/oj/mimic_json.c +74 -73
  21. data/ext/oj/object.c +54 -72
  22. data/ext/oj/odd.c +83 -63
  23. data/ext/oj/odd.h +13 -13
  24. data/ext/oj/oj.c +166 -175
  25. data/ext/oj/oj.h +25 -3
  26. data/ext/oj/parse.c +123 -79
  27. data/ext/oj/parse.h +2 -0
  28. data/ext/oj/parser.c +77 -21
  29. data/ext/oj/parser.h +12 -0
  30. data/ext/oj/rails.c +46 -70
  31. data/ext/oj/rails.h +1 -1
  32. data/ext/oj/reader.c +2 -0
  33. data/ext/oj/saj.c +11 -23
  34. data/ext/oj/saj2.c +333 -85
  35. data/ext/oj/saj2.h +23 -0
  36. data/ext/oj/sparse.c +4 -0
  37. data/ext/oj/stream_writer.c +3 -1
  38. data/ext/oj/strict.c +13 -13
  39. data/ext/oj/string_writer.c +12 -5
  40. data/ext/oj/usual.c +86 -131
  41. data/ext/oj/usual.h +68 -0
  42. data/ext/oj/val_stack.c +1 -1
  43. data/ext/oj/validate.c +21 -26
  44. data/ext/oj/wab.c +22 -27
  45. data/lib/oj/saj.rb +20 -6
  46. data/lib/oj/state.rb +1 -1
  47. data/lib/oj/version.rb +1 -1
  48. data/pages/Compatibility.md +1 -1
  49. data/pages/JsonGem.md +15 -0
  50. data/pages/Modes.md +6 -3
  51. data/pages/Options.md +6 -0
  52. data/pages/Rails.md +12 -0
  53. data/test/activesupport7/abstract_unit.rb +49 -0
  54. data/test/activesupport7/decoding_test.rb +125 -0
  55. data/test/activesupport7/encoding_test.rb +486 -0
  56. data/test/activesupport7/encoding_test_cases.rb +104 -0
  57. data/test/activesupport7/time_zone_test_helpers.rb +47 -0
  58. data/test/bar.rb +3 -8
  59. data/test/bug.rb +16 -0
  60. data/test/foo.rb +71 -7
  61. data/test/helper.rb +8 -2
  62. data/test/json_gem/json_generator_test.rb +5 -4
  63. data/test/json_gem/json_parser_test.rb +8 -1
  64. data/test/json_gem/test_helper.rb +7 -3
  65. data/test/perf_dump.rb +50 -0
  66. data/test/test_compat.rb +25 -0
  67. data/test/test_custom.rb +13 -2
  68. data/test/test_fast.rb +37 -7
  69. data/test/test_file.rb +23 -7
  70. data/test/test_gc.rb +11 -0
  71. data/test/test_object.rb +8 -10
  72. data/test/test_parser.rb +3 -19
  73. data/test/test_parser_debug.rb +27 -0
  74. data/test/test_parser_saj.rb +92 -2
  75. data/test/test_saj.rb +1 -1
  76. data/test/test_scp.rb +2 -4
  77. data/test/test_strict.rb +2 -0
  78. data/test/test_various.rb +32 -2
  79. data/test/test_wab.rb +2 -0
  80. data/test/tests.rb +9 -1
  81. data/test/tests_mimic.rb +9 -0
  82. data/test/tests_mimic_addition.rb +9 -0
  83. metadata +15 -115
data/ext/oj/oj.h CHANGED
@@ -39,6 +39,16 @@ enum st_retval { ST_CONTINUE = 0, ST_STOP = 1, ST_DELETE = 2, ST_CHECK };
39
39
  #define NINF_VAL "-3.0e14159265358979323846"
40
40
  #define NAN_VAL "3.3e14159265358979323846"
41
41
 
42
+ #if __STDC_VERSION__ >= 199901L
43
+ // To avoid using ruby_snprintf with C99.
44
+ #undef snprintf
45
+ #include <stdio.h>
46
+ #endif
47
+
48
+ // To avoid using ruby_nonempty_memcpy().
49
+ #undef memcpy
50
+ #include <string.h>
51
+
42
52
  typedef enum { Yes = 'y', No = 'n', NotSet = 0 } YesNo;
43
53
 
44
54
  typedef enum {
@@ -56,6 +66,7 @@ typedef enum { UnixTime = 'u', UnixZTime = 'z', XmlTime = 'x', RubyTime = 'r' }
56
66
  typedef enum {
57
67
  NLEsc = 'n',
58
68
  JSONEsc = 'j',
69
+ SlashEsc = 's',
59
70
  XSSEsc = 'x',
60
71
  ASCIIEsc = 'a',
61
72
  JXEsc = 'g', // json gem
@@ -176,6 +187,7 @@ typedef struct _rOptTable {
176
187
  } * ROptTable;
177
188
 
178
189
  typedef struct _out {
190
+ char stack_buffer[4096];
179
191
  char * buf;
180
192
  char * end;
181
193
  char * cur;
@@ -265,8 +277,8 @@ extern void oj_str_writer_pop(StrWriter sw);
265
277
  extern void oj_str_writer_pop_all(StrWriter sw);
266
278
 
267
279
  extern void oj_init_doc(void);
268
- extern void oj_string_writer_init();
269
- extern void oj_stream_writer_init();
280
+ extern void oj_string_writer_init(void);
281
+ extern void oj_stream_writer_init(void);
270
282
  extern void oj_str_writer_init(StrWriter sw, int buf_size);
271
283
  extern VALUE oj_define_mimic_json(int argc, VALUE *argv, VALUE self);
272
284
  extern VALUE oj_mimic_generate(int argc, VALUE *argv, VALUE self);
@@ -282,6 +294,7 @@ extern VALUE oj_rails_encode(int argc, VALUE *argv, VALUE self);
282
294
  extern VALUE Oj;
283
295
  extern struct _options oj_default_options;
284
296
  extern rb_encoding * oj_utf8_encoding;
297
+ extern int oj_utf8_encoding_index;
285
298
 
286
299
  extern VALUE oj_bag_class;
287
300
  extern VALUE oj_bigdecimal_class;
@@ -304,13 +317,16 @@ extern VALUE oj_ascii_only_sym;
304
317
  extern VALUE oj_create_additions_sym;
305
318
  extern VALUE oj_decimal_class_sym;
306
319
  extern VALUE oj_hash_class_sym;
320
+ extern VALUE oj_in_sym;
307
321
  extern VALUE oj_indent_sym;
322
+ extern VALUE oj_nanosecond_sym;
308
323
  extern VALUE oj_max_nesting_sym;
309
324
  extern VALUE oj_object_class_sym;
310
325
  extern VALUE oj_object_nl_sym;
311
326
  extern VALUE oj_quirks_mode_sym;
312
327
  extern VALUE oj_space_before_sym;
313
328
  extern VALUE oj_space_sym;
329
+ extern VALUE oj_symbolize_names_sym;
314
330
  extern VALUE oj_trace_sym;
315
331
 
316
332
  extern VALUE oj_slash_string;
@@ -320,6 +336,7 @@ extern ID oj_array_append_id;
320
336
  extern ID oj_array_end_id;
321
337
  extern ID oj_array_start_id;
322
338
  extern ID oj_as_json_id;
339
+ extern ID oj_at_id;
323
340
  extern ID oj_begin_id;
324
341
  extern ID oj_bigdecimal_id;
325
342
  extern ID oj_end_id;
@@ -333,7 +350,6 @@ extern ID oj_hash_key_id;
333
350
  extern ID oj_hash_set_id;
334
351
  extern ID oj_hash_start_id;
335
352
  extern ID oj_iconv_id;
336
- extern ID oj_instance_variables_id;
337
353
  extern ID oj_json_create_id;
338
354
  extern ID oj_length_id;
339
355
  extern ID oj_new_id;
@@ -363,6 +379,12 @@ extern bool oj_use_hash_alt;
363
379
  extern bool oj_use_array_alt;
364
380
  extern bool string_writer_optimized;
365
381
 
382
+ #define APPEND_CHARS(buffer, chars, size) \
383
+ { \
384
+ memcpy(buffer, chars, size); \
385
+ buffer += size; \
386
+ }
387
+
366
388
  #ifdef HAVE_PTHREAD_MUTEX_INIT
367
389
  extern pthread_mutex_t oj_cache_mutex;
368
390
  #else
data/ext/oj/parse.c CHANGED
@@ -183,6 +183,42 @@ static void unicode_to_chars(ParseInfo pi, Buf buf, uint32_t code) {
183
183
  }
184
184
  }
185
185
 
186
+ static inline const char *scan_string_noSIMD(const char *str, const char *end) {
187
+ for (; '"' != *str; str++) {
188
+ if (end <= str || '\0' == *str || '\\' == *str) {
189
+ break;
190
+ }
191
+ }
192
+ return str;
193
+ }
194
+
195
+ #ifdef OJ_USE_SSE4_2
196
+ static inline const char *scan_string_SIMD(const char *str, const char *end) {
197
+ static const char chars[16] = "\x00\\\"";
198
+ const __m128i terminate = _mm_loadu_si128((const __m128i *)&chars[0]);
199
+ const char *_end = (const char *)(end - 16);
200
+
201
+ for (; str <= _end; str += 16) {
202
+ const __m128i string = _mm_loadu_si128((const __m128i *)str);
203
+ const int r = _mm_cmpestri(terminate, 3, string, 16, _SIDD_UBYTE_OPS | _SIDD_CMP_EQUAL_ANY | _SIDD_LEAST_SIGNIFICANT);
204
+ if (r != 16) {
205
+ str = (char*)(str + r);
206
+ return str;
207
+ }
208
+ }
209
+
210
+ return scan_string_noSIMD(str, end);
211
+ }
212
+ #endif
213
+
214
+ static const char *(*scan_func) (const char *str, const char *end) = scan_string_noSIMD;
215
+
216
+ void oj_scanner_init(void) {
217
+ #ifdef OJ_USE_SSE4_2
218
+ scan_func = scan_string_SIMD;
219
+ #endif
220
+ }
221
+
186
222
  // entered at /
187
223
  static void read_escaped_str(ParseInfo pi, const char *start) {
188
224
  struct _buf buf;
@@ -192,11 +228,11 @@ static void read_escaped_str(ParseInfo pi, const char *start) {
192
228
  Val parent = stack_peek(&pi->stack);
193
229
 
194
230
  buf_init(&buf);
195
- if (0 < cnt) {
196
- buf_append_string(&buf, start, cnt);
197
- }
198
- for (s = pi->cur; '"' != *s; s++) {
199
- if (s >= pi->end) {
231
+ buf_append_string(&buf, start, cnt);
232
+
233
+ for (s = pi->cur; '"' != *s;) {
234
+ const char *scanned = scan_func(s, pi->end);
235
+ if (scanned >= pi->end) {
200
236
  oj_set_error_at(pi,
201
237
  oj_parse_error_class,
202
238
  __FILE__,
@@ -204,7 +240,12 @@ static void read_escaped_str(ParseInfo pi, const char *start) {
204
240
  "quoted string not terminated");
205
241
  buf_cleanup(&buf);
206
242
  return;
207
- } else if ('\\' == *s) {
243
+ }
244
+
245
+ buf_append_string(&buf, s, (size_t)(scanned - s));
246
+ s = scanned;
247
+
248
+ if ('\\' == *s) {
208
249
  s++;
209
250
  switch (*s) {
210
251
  case 'n': buf_append(&buf, '\n'); break;
@@ -273,8 +314,7 @@ static void read_escaped_str(ParseInfo pi, const char *start) {
273
314
  buf_cleanup(&buf);
274
315
  return;
275
316
  }
276
- } else {
277
- buf_append(&buf, *s);
317
+ s++;
278
318
  }
279
319
  }
280
320
  if (0 == parent) {
@@ -331,22 +371,24 @@ static void read_str(ParseInfo pi) {
331
371
  const char *str = pi->cur;
332
372
  Val parent = stack_peek(&pi->stack);
333
373
 
334
- for (; '"' != *pi->cur; pi->cur++) {
335
- if (pi->end <= pi->cur) {
336
- oj_set_error_at(pi,
337
- oj_parse_error_class,
338
- __FILE__,
339
- __LINE__,
340
- "quoted string not terminated");
341
- return;
342
- } else if ('\0' == *pi->cur) {
343
- oj_set_error_at(pi, oj_parse_error_class, __FILE__, __LINE__, "NULL byte in string");
344
- return;
345
- } else if ('\\' == *pi->cur) {
346
- read_escaped_str(pi, str);
347
- return;
348
- }
374
+ pi->cur = scan_func(pi->cur, pi->end);
375
+ if (RB_UNLIKELY(pi->end <= pi->cur)) {
376
+ oj_set_error_at(pi,
377
+ oj_parse_error_class,
378
+ __FILE__,
379
+ __LINE__,
380
+ "quoted string not terminated");
381
+ return;
349
382
  }
383
+ if (RB_UNLIKELY('\0' == *pi->cur)) {
384
+ oj_set_error_at(pi, oj_parse_error_class, __FILE__, __LINE__, "NULL byte in string");
385
+ return;
386
+ }
387
+ if ('\\' == *pi->cur) {
388
+ read_escaped_str(pi, str);
389
+ return;
390
+ }
391
+
350
392
  if (0 == parent) { // simple add
351
393
  pi->add_cstr(pi, str, pi->cur - str, str);
352
394
  } else {
@@ -459,33 +501,31 @@ static void read_num(ParseInfo pi) {
459
501
  int dec_cnt = 0;
460
502
  bool zero1 = false;
461
503
 
504
+ // Skip leading zeros.
505
+ for (; '0' == *pi->cur; pi->cur++) {
506
+ zero1 = true;
507
+ }
508
+
462
509
  for (; '0' <= *pi->cur && *pi->cur <= '9'; pi->cur++) {
463
- if (0 == ni.i && '0' == *pi->cur) {
464
- zero1 = true;
465
- }
466
- if (0 < ni.i) {
467
- dec_cnt++;
468
- }
469
- if (!ni.big) {
470
- int d = (*pi->cur - '0');
510
+ int d = (*pi->cur - '0');
471
511
 
472
- if (0 < d) {
473
- if (zero1 && CompatMode == pi->options.mode) {
474
- oj_set_error_at(pi,
475
- oj_parse_error_class,
476
- __FILE__,
477
- __LINE__,
478
- "not a number");
479
- return;
480
- }
481
- zero1 = false;
482
- }
483
- ni.i = ni.i * 10 + d;
484
- if (INT64_MAX <= ni.i || DEC_MAX < dec_cnt) {
485
- ni.big = 1;
486
- }
512
+ if (RB_LIKELY(0 != ni.i)) {
513
+ dec_cnt++;
487
514
  }
515
+ ni.i = ni.i * 10 + d;
488
516
  }
517
+ if (RB_UNLIKELY(0 != ni.i && zero1 && CompatMode == pi->options.mode)) {
518
+ oj_set_error_at(pi,
519
+ oj_parse_error_class,
520
+ __FILE__,
521
+ __LINE__,
522
+ "not a number");
523
+ return;
524
+ }
525
+ if (INT64_MAX <= ni.i || DEC_MAX < dec_cnt) {
526
+ ni.big = true;
527
+ }
528
+
489
529
  if ('.' == *pi->cur) {
490
530
  pi->cur++;
491
531
  // A trailing . is not a valid decimal but if encountered allow it
@@ -505,25 +545,20 @@ static void read_num(ParseInfo pi) {
505
545
  for (; '0' <= *pi->cur && *pi->cur <= '9'; pi->cur++) {
506
546
  int d = (*pi->cur - '0');
507
547
 
508
- if (0 < ni.num || 0 < ni.i) {
548
+ if (RB_LIKELY(0 != ni.num || 0 != ni.i)) {
509
549
  dec_cnt++;
510
550
  }
511
- if (INT64_MAX <= ni.div) {
512
- if (!ni.no_big) {
513
- ni.big = true;
514
- }
515
- } else {
516
- ni.num = ni.num * 10 + d;
517
- ni.div *= 10;
518
- ni.di++;
519
- if (INT64_MAX <= ni.div || DEC_MAX < dec_cnt) {
520
- if (!ni.no_big) {
521
- ni.big = true;
522
- }
523
- }
524
- }
551
+ ni.num = ni.num * 10 + d;
552
+ ni.div *= 10;
553
+ ni.di++;
525
554
  }
526
555
  }
556
+ if (INT64_MAX <= ni.div || DEC_MAX < dec_cnt) {
557
+ if (!ni.no_big) {
558
+ ni.big = true;
559
+ }
560
+ }
561
+
527
562
  if ('e' == *pi->cur || 'E' == *pi->cur) {
528
563
  int eneg = 0;
529
564
 
@@ -596,7 +631,7 @@ static void read_num(ParseInfo pi) {
596
631
  }
597
632
 
598
633
  static void array_start(ParseInfo pi) {
599
- volatile VALUE v = pi->start_array(pi);
634
+ VALUE v = pi->start_array(pi);
600
635
 
601
636
  stack_push(&pi->stack, v, NEXT_ARRAY_NEW);
602
637
  }
@@ -620,13 +655,13 @@ static void array_end(ParseInfo pi) {
620
655
  }
621
656
 
622
657
  static void hash_start(ParseInfo pi) {
623
- volatile VALUE v = pi->start_hash(pi);
658
+ VALUE v = pi->start_hash(pi);
624
659
 
625
660
  stack_push(&pi->stack, v, NEXT_HASH_NEW);
626
661
  }
627
662
 
628
663
  static void hash_end(ParseInfo pi) {
629
- volatile Val hash = stack_peek(&pi->stack);
664
+ Val hash = stack_peek(&pi->stack);
630
665
 
631
666
  // leave hash on stack until just before
632
667
  if (0 == hash) {
@@ -813,7 +848,7 @@ static long double exp_plus[] = {
813
848
 
814
849
  VALUE
815
850
  oj_num_as_value(NumInfo ni) {
816
- volatile VALUE rnum = Qnil;
851
+ VALUE rnum = Qnil;
817
852
 
818
853
  if (ni->infinity) {
819
854
  if (ni->neg) {
@@ -848,7 +883,7 @@ oj_num_as_value(NumInfo ni) {
848
883
  }
849
884
  } else { // decimal
850
885
  if (ni->big) {
851
- volatile VALUE bd = rb_str_new(ni->str, ni->len);
886
+ VALUE bd = rb_str_new(ni->str, ni->len);
852
887
 
853
888
  rnum = rb_rescue2(parse_big_decimal, bd, rescue_big_decimal, bd, rb_eException, 0);
854
889
  if (ni->no_big) {
@@ -876,7 +911,7 @@ oj_num_as_value(NumInfo ni) {
876
911
  }
877
912
  rnum = rb_float_new((double)ld);
878
913
  } else if (RubyDec == ni->bigdec_load) {
879
- volatile VALUE sv = rb_str_new(ni->str, ni->len);
914
+ VALUE sv = rb_str_new(ni->str, ni->len);
880
915
 
881
916
  rnum = rb_funcall(sv, rb_intern("to_f"), 0);
882
917
  } else {
@@ -904,9 +939,17 @@ void oj_set_error_at(ParseInfo pi,
904
939
  char * end = p + sizeof(msg) - 2;
905
940
  char * start;
906
941
  Val vp;
942
+ int mlen;
907
943
 
908
944
  va_start(ap, format);
909
- p += vsnprintf(msg, sizeof(msg) - 1, format, ap);
945
+ mlen = vsnprintf(msg, sizeof(msg) - 1, format, ap);
946
+ if (0 < mlen) {
947
+ if (sizeof(msg) - 2 < (size_t)mlen) {
948
+ p = end - 2;
949
+ } else {
950
+ p += mlen;
951
+ }
952
+ }
910
953
  va_end(ap);
911
954
  pi->err.clas = err_clas;
912
955
  if (p + 3 < end) {
@@ -963,10 +1006,11 @@ static VALUE protect_parse(VALUE pip) {
963
1006
 
964
1007
  extern int oj_utf8_index;
965
1008
 
966
- static void oj_pi_set_input_str(ParseInfo pi, volatile VALUE *inputp) {
967
- rb_encoding *enc = rb_enc_get(*inputp);
1009
+ static void oj_pi_set_input_str(ParseInfo pi, VALUE *inputp) {
1010
+ int idx = RB_ENCODING_GET(*inputp);
968
1011
 
969
- if (oj_utf8_encoding != enc) {
1012
+ if (oj_utf8_encoding_index != idx) {
1013
+ rb_encoding *enc = rb_enc_from_index(idx);
970
1014
  *inputp = rb_str_conv_enc(*inputp, enc, oj_utf8_encoding);
971
1015
  }
972
1016
  pi->json = RSTRING_PTR(*inputp);
@@ -975,12 +1019,12 @@ static void oj_pi_set_input_str(ParseInfo pi, volatile VALUE *inputp) {
975
1019
 
976
1020
  VALUE
977
1021
  oj_pi_parse(int argc, VALUE *argv, ParseInfo pi, char *json, size_t len, int yieldOk) {
978
- char * buf = 0;
979
- volatile VALUE input;
980
- volatile VALUE wrapped_stack;
981
- volatile VALUE result = Qnil;
982
- int line = 0;
983
- int free_json = 0;
1022
+ char * buf = 0;
1023
+ VALUE input;
1024
+ VALUE wrapped_stack;
1025
+ VALUE result = Qnil;
1026
+ int line = 0;
1027
+ int free_json = 0;
984
1028
 
985
1029
  if (argc < 1) {
986
1030
  rb_raise(rb_eArgError, "Wrong number of arguments to parse.");
@@ -1016,8 +1060,8 @@ oj_pi_parse(int argc, VALUE *argv, ParseInfo pi, char *json, size_t len, int yie
1016
1060
  rb_raise(rb_eTypeError, "Nil is not a valid JSON source.");
1017
1061
  }
1018
1062
  } else {
1019
- VALUE clas = rb_obj_class(input);
1020
- volatile VALUE s;
1063
+ VALUE clas = rb_obj_class(input);
1064
+ VALUE s;
1021
1065
 
1022
1066
  if (oj_stringio_class == clas) {
1023
1067
  s = rb_funcall2(input, oj_string_id, 0, 0);
data/ext/oj/parse.h CHANGED
@@ -97,6 +97,8 @@ static inline void parse_info_init(ParseInfo pi) {
97
97
  memset(pi, 0, sizeof(struct _parseInfo));
98
98
  }
99
99
 
100
+ extern void oj_scanner_init(void);
101
+
100
102
  static inline bool empty_ok(Options options) {
101
103
  switch (options->mode) {
102
104
  case ObjectMode: