json 3.0.2 → 3.2.0

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.
data/ext/json/ext/json.h CHANGED
@@ -5,6 +5,19 @@
5
5
  #include "ruby/encoding.h"
6
6
  #include <stdint.h>
7
7
 
8
+ #ifndef HAVE_RB_LEN_T
9
+ typedef long rb_len_t;
10
+ #endif
11
+ #ifndef FIX2LEN
12
+ # define FIX2LEN FIX2LONG
13
+ #endif
14
+ #ifndef LEN2NUM
15
+ # define LEN2NUM LONG2NUM
16
+ #endif
17
+ #ifndef PRIdLEN
18
+ # define PRIdLEN "ld"
19
+ #endif
20
+
8
21
  #ifndef RBIMPL_ASSERT_OR_ASSUME
9
22
  # define RBIMPL_ASSERT_OR_ASSUME(x)
10
23
  #endif
@@ -20,9 +20,9 @@ static int utf8_encindex;
20
20
  #ifndef HAVE_RB_HASH_BULK_INSERT
21
21
  // For TruffleRuby
22
22
  static void
23
- rb_hash_bulk_insert(long count, const VALUE *pairs, VALUE hash)
23
+ rb_hash_bulk_insert(rb_len_t count, const VALUE *pairs, VALUE hash)
24
24
  {
25
- long index = 0;
25
+ rb_len_t index = 0;
26
26
  while (index < count) {
27
27
  VALUE name = pairs[index++];
28
28
  VALUE value = pairs[index++];
@@ -79,7 +79,7 @@ static rb_encoding *enc_utf8;
79
79
 
80
80
  #define JSON_RVALUE_CACHE_MAX_ENTRY_LENGTH 55
81
81
 
82
- static inline VALUE build_interned_string(const char *str, const long length)
82
+ static inline VALUE build_interned_string(const char *str, const rb_len_t length)
83
83
  {
84
84
  # ifdef HAVE_RB_ENC_INTERNED_STR
85
85
  return rb_enc_interned_str(str, length, enc_utf8);
@@ -89,7 +89,7 @@ static inline VALUE build_interned_string(const char *str, const long length)
89
89
  # endif
90
90
  }
91
91
 
92
- static inline VALUE build_symbol(const char *str, const long length)
92
+ static inline VALUE build_symbol(const char *str, const rb_len_t length)
93
93
  {
94
94
  return rb_str_intern(build_interned_string(str, length));
95
95
  }
@@ -138,7 +138,7 @@ ALWAYS_INLINE(static) int rstring_cache_memcmp(const char *str, const char *rptr
138
138
  ALWAYS_INLINE(static) int rstring_cache_cmp(const char *str, const long length, VALUE rstring)
139
139
  {
140
140
  const char *rstring_ptr;
141
- long rstring_length;
141
+ rb_len_t rstring_length;
142
142
 
143
143
  RSTRING_GETMEM(rstring, rstring_ptr, rstring_length);
144
144
 
@@ -323,14 +323,17 @@ static const rb_data_type_t JSON_Parser_rvalue_stack_type = {
323
323
  static rvalue_stack *rvalue_stack_spill(rvalue_stack *old_stack, VALUE *handle, rvalue_stack **stack_ref)
324
324
  {
325
325
  rvalue_stack *stack;
326
- *handle = TypedData_Make_Struct(0, rvalue_stack, &JSON_Parser_rvalue_stack_type, stack);
327
- *stack_ref = stack;
326
+ VALUE stack_handle = TypedData_Make_Struct(0, rvalue_stack, &JSON_Parser_rvalue_stack_type, stack);
328
327
  MEMCPY(stack, old_stack, rvalue_stack, 1);
329
328
 
330
329
  stack->capa = old_stack->capa << 1;
331
330
  stack->ptr = ALLOC_N(VALUE, stack->capa);
332
331
  stack->type = RVALUE_STACK_HEAP_ALLOCATED;
333
332
  MEMCPY(stack->ptr, old_stack->ptr, VALUE, old_stack->head);
333
+
334
+ *handle = stack_handle;
335
+ *stack_ref = stack;
336
+
334
337
  return stack;
335
338
  }
336
339
 
@@ -580,7 +583,7 @@ static inline char peek(JSON_ParserState *state)
580
583
  return *state->cursor;
581
584
  }
582
585
 
583
- static void cursor_position(JSON_ParserState *state, long *line_out, long *column_out)
586
+ static void cursor_position(JSON_ParserState *state, long *line_out, rb_len_t *column_out)
584
587
  {
585
588
  JSON_ASSERT(!state->parser);
586
589
  JSON_ASSERT(state->cursor);
@@ -592,19 +595,15 @@ static void cursor_position(JSON_ParserState *state, long *line_out, long *colum
592
595
  }
593
596
 
594
597
  const char *cursor = state->cursor;
595
- long column = 0;
596
598
  long line = 1;
597
599
 
598
- while (cursor >= state->start) {
599
- if (*cursor-- == '\n') {
600
- line++;
601
- break;
602
- }
603
- column++;
600
+ while (cursor > state->start && cursor[-1] != '\n') {
601
+ cursor--;
604
602
  }
603
+ rb_len_t column = rb_enc_strlen(cursor, state->cursor, enc_utf8) + 1;
605
604
 
606
- while (cursor >= state->start) {
607
- if (*cursor-- == '\n') {
605
+ while (cursor > state->start) {
606
+ if (*--cursor == '\n') {
608
607
  line++;
609
608
  }
610
609
  }
@@ -631,17 +630,15 @@ static VALUE build_parse_error_message(const char *format, JSON_ParserState *sta
631
630
  }
632
631
 
633
632
  if (len) {
634
- buffer[0] = '\'';
635
- MEMCPY(buffer + 1, ptr, char, len);
636
-
637
- while (buffer[len] >= 0x80 && buffer[len] < 0xC0) { // Is continuation byte
638
- len--;
639
- }
640
-
641
- if (buffer[len] >= 0xC0) { // multibyte character start
642
- len--;
633
+ if (len == PARSE_ERROR_FRAGMENT_LEN) {
634
+ // Only trim when the byte limit splits a multibyte character.
635
+ while (len && (unsigned char)ptr[len] >= 0x80 && (unsigned char)ptr[len] < 0xC0) {
636
+ len--;
637
+ }
643
638
  }
644
639
 
640
+ buffer[0] = '\'';
641
+ MEMCPY(buffer + 1, ptr, char, len);
645
642
  buffer[len + 1] = '\'';
646
643
  buffer[len + 2] = '\0';
647
644
  ptr = (const char *)buffer;
@@ -681,11 +678,11 @@ static VALUE json_path_new(JSON_ParserState *state, VALUE duplicate_key)
681
678
  return path;
682
679
  }
683
680
 
684
- static VALUE parse_error_new(JSON_ParserState *state, VALUE message, long line, long column, bool eos)
681
+ static VALUE parse_error_new(JSON_ParserState *state, VALUE message, long line, rb_len_t column, bool eos)
685
682
  {
686
683
  VALUE exc = rb_exc_new_str(eParserError, message);
687
684
  rb_ivar_set(exc, i_at_line, LONG2NUM(line));
688
- rb_ivar_set(exc, i_at_column, LONG2NUM(column));
685
+ rb_ivar_set(exc, i_at_column, LEN2NUM(column));
689
686
  rb_ivar_set(exc, i_at_json_path, json_path_new(state, Qundef));
690
687
  return exc;
691
688
  }
@@ -703,9 +700,10 @@ NORETURN(static) void raise_parse_error(const char *format, JSON_ParserState *st
703
700
  }
704
701
  } else {
705
702
  VALUE message = build_parse_error_message(format, state);
706
- long line, column;
703
+ long line;
704
+ rb_len_t column;
707
705
  cursor_position(state, &line, &column);
708
- rb_str_catf(message, " at line %ld column %ld", line, column);
706
+ rb_str_catf(message, " at line %ld column %"PRIdLEN, line, column);
709
707
  rb_exc_raise(parse_error_new(state, message, line, column, eos));
710
708
  }
711
709
  }
@@ -758,7 +756,7 @@ static const signed char digit_values[256] = {
758
756
  static uint32_t unescape_unicode(JSON_ParserState *state, const char *sp, const char *spe)
759
757
  {
760
758
  if (RB_UNLIKELY(sp > spe - 4)) {
761
- raise_eos_error_at("incomplete unicode character escape sequence at %s", state, sp - 2);
759
+ raise_syntax_error_at("incomplete unicode character escape sequence at %s", state, sp - 2);
762
760
  }
763
761
 
764
762
  const unsigned char *p = (const unsigned char *)sp;
@@ -895,12 +893,12 @@ static inline VALUE build_string(const char *start, const char *end, bool intern
895
893
  VALUE result;
896
894
  # ifdef HAVE_RB_ENC_INTERNED_STR
897
895
  if (intern) {
898
- result = rb_enc_interned_str(start, (long)(end - start), enc_utf8);
896
+ result = rb_enc_interned_str(start, (rb_len_t)(end - start), enc_utf8);
899
897
  } else {
900
- result = rb_utf8_str_new(start, (long)(end - start));
898
+ result = rb_utf8_str_new(start, (rb_len_t)(end - start));
901
899
  }
902
900
  # else
903
- result = rb_utf8_str_new(start, (long)(end - start));
901
+ result = rb_utf8_str_new(start, (rb_len_t)(end - start));
904
902
  if (intern) {
905
903
  result = rb_funcall(rb_str_freeze(result), i_uminus, 0);
906
904
  }
@@ -1051,13 +1049,13 @@ NOINLINE(static) VALUE json_string_unescape(JSON_ParserState *state, JSON_Parser
1051
1049
  p = ++pe;
1052
1050
  break;
1053
1051
  }
1054
- case 0:
1055
- return Qundef;
1056
1052
  default:
1057
1053
  if ((unsigned char)*pe < 0x20) {
1058
1054
  if (!config->allow_control_characters) {
1059
1055
  if (*pe == '\n') {
1060
1056
  raise_syntax_error_at("Invalid unescaped newline character (\\n) in string: %s", state, pe - 1);
1057
+ } else if (*pe == '\0') {
1058
+ raise_syntax_error_at("Invalid unescaped NULL character (\\0) in string: %s", state, pe - 1);
1061
1059
  }
1062
1060
  raise_syntax_error_at("invalid ASCII control character in string: %s", state, pe - 1);
1063
1061
  }
@@ -1094,7 +1092,7 @@ NOINLINE(static) VALUE json_string_unescape(JSON_ParserState *state, JSON_Parser
1094
1092
 
1095
1093
  typedef VALUE (*json_number_decode_func_t)(const char *ptr);
1096
1094
 
1097
- static inline VALUE json_decode_large_number(const char *start, long len, json_number_decode_func_t func)
1095
+ static inline VALUE json_decode_large_number(const char *start, rb_len_t len, json_number_decode_func_t func)
1098
1096
  {
1099
1097
  if (RB_LIKELY(len < MAX_NUMBER_STACK_BUFFER)) {
1100
1098
  char buffer[MAX_NUMBER_STACK_BUFFER];
@@ -1117,12 +1115,12 @@ static VALUE json_decode_inum(const char *buffer)
1117
1115
  return rb_cstr2inum(buffer, 10);
1118
1116
  }
1119
1117
 
1120
- NOINLINE(static) VALUE json_decode_large_integer(const char *start, long len)
1118
+ NOINLINE(static) VALUE json_decode_large_integer(const char *start, rb_len_t len)
1121
1119
  {
1122
1120
  return json_decode_large_number(start, len, json_decode_inum);
1123
1121
  }
1124
1122
 
1125
- static inline VALUE json_decode_integer(uint64_t mantissa, int mantissa_digits, bool negative, const char *start, const char *end)
1123
+ static inline VALUE json_decode_integer(uint64_t mantissa, ssize_t mantissa_digits, bool negative, const char *start, const char *end)
1126
1124
  {
1127
1125
  if (RB_LIKELY(mantissa_digits <= MAX_FAST_INTEGER_SIZE)) {
1128
1126
  if (RB_LIKELY(!negative)) {
@@ -1158,7 +1156,7 @@ static VALUE json_decode_dnum(const char *buffer)
1158
1156
  return DBL2NUM(rb_cstr_to_dbl(buffer, 1));
1159
1157
  }
1160
1158
 
1161
- NOINLINE(static) VALUE json_decode_large_float(const char *start, long len)
1159
+ NOINLINE(static) VALUE json_decode_large_float(const char *start, rb_len_t len)
1162
1160
  {
1163
1161
  return json_decode_large_number(start, len, json_decode_dnum);
1164
1162
  }
@@ -1166,7 +1164,7 @@ NOINLINE(static) VALUE json_decode_large_float(const char *start, long len)
1166
1164
  /* Ruby JSON optimized float decoder using vendored Ryu algorithm
1167
1165
  * Accepts pre-extracted mantissa and exponent from first-pass validation
1168
1166
  */
1169
- static inline VALUE json_decode_float(JSON_ParserConfig *config, uint64_t mantissa, int mantissa_digits, int64_t exponent, bool negative,
1167
+ static inline VALUE json_decode_float(JSON_ParserConfig *config, uint64_t mantissa, ssize_t mantissa_digits, int64_t exponent, bool negative,
1170
1168
  const char *start, const char *end)
1171
1169
  {
1172
1170
  if (RB_UNLIKELY(config->decimal_class)) {
@@ -1174,11 +1172,11 @@ static inline VALUE json_decode_float(JSON_ParserConfig *config, uint64_t mantis
1174
1172
  return rb_funcallv(config->decimal_class, config->decimal_method_id, 1, &text);
1175
1173
  }
1176
1174
 
1177
- if (RB_UNLIKELY(exponent > INT32_MAX)) {
1175
+ if (RB_UNLIKELY(exponent >= (int64_t)INT32_MAX)) {
1178
1176
  return negative ? CMinusInfinity : CInfinity;
1179
1177
  }
1180
1178
 
1181
- if (RB_UNLIKELY(exponent < INT32_MIN)) {
1179
+ if (RB_UNLIKELY(exponent <= INT32_MIN)) {
1182
1180
  return rb_float_new(negative ? -0.0 : 0.0);
1183
1181
  }
1184
1182
 
@@ -1237,9 +1235,10 @@ NORETURN(static) void raise_duplicate_key_error(JSON_ParserState *state, VALUE d
1237
1235
  if (state->parser) { // line and columns can't be accurate in resumable
1238
1236
  exc = parse_error_new(state, message, 0, 0, false);
1239
1237
  } else {
1240
- long line, column;
1238
+ long line;
1239
+ rb_len_t column;
1241
1240
  cursor_position(state, &line, &column);
1242
- rb_str_catf(message, " at line %ld column %ld", line, column);
1241
+ rb_str_catf(message, " at line %ld column %"PRIdLEN, line, column);
1243
1242
  exc = parse_error_new(state, message, line, column, false);
1244
1243
  }
1245
1244
  rb_ivar_set(exc, i_at_json_path, json_path_new(state, duplicate_key));
@@ -1422,7 +1421,7 @@ static inline uint64_t decode_4digits_unrolled(uint32_t val) {
1422
1421
  }
1423
1422
  #endif
1424
1423
 
1425
- static inline int json_parse_digits(JSON_ParserState *state, uint64_t *accumulator)
1424
+ static inline ssize_t json_parse_digits(JSON_ParserState *state, uint64_t *accumulator)
1426
1425
  {
1427
1426
  const char *start = state->cursor;
1428
1427
 
@@ -1455,7 +1454,7 @@ static inline int json_parse_digits(JSON_ParserState *state, uint64_t *accumulat
1455
1454
  state->cursor++;
1456
1455
  }
1457
1456
 
1458
- return (int)(state->cursor - start);
1457
+ return (ssize_t)(state->cursor - start);
1459
1458
  }
1460
1459
  #endif
1461
1460
 
@@ -1464,7 +1463,7 @@ static inline int json_parse_digits(JSON_ParserState *state, uint64_t *accumulat
1464
1463
  *accumulator = *accumulator * 10 + (next_char - '0');
1465
1464
  state->cursor++;
1466
1465
  }
1467
- return (int)(state->cursor - start);
1466
+ return (ssize_t)(state->cursor - start);
1468
1467
  }
1469
1468
 
1470
1469
  static inline VALUE json_parse_number(JSON_ParserState *state, JSON_ParserConfig *config, bool negative, const char *start, bool resumable)
@@ -1474,11 +1473,11 @@ static inline VALUE json_parse_number(JSON_ParserState *state, JSON_ParserConfig
1474
1473
 
1475
1474
  // Variables for Ryu optimization - extract digits during parsing
1476
1475
  int64_t exponent = 0;
1477
- int decimal_point_pos = -1;
1476
+ ssize_t decimal_point_pos = -1;
1478
1477
  uint64_t mantissa = 0;
1479
1478
 
1480
1479
  // Parse integer part and extract mantissa digits
1481
- int mantissa_digits = json_parse_digits(state, &mantissa);
1480
+ ssize_t mantissa_digits = json_parse_digits(state, &mantissa);
1482
1481
 
1483
1482
  if (RB_UNLIKELY((first_digit == '0' && mantissa_digits > 1) || (negative && mantissa_digits == 0))) {
1484
1483
  return Qundef;
@@ -1490,7 +1489,7 @@ static inline VALUE json_parse_number(JSON_ParserState *state, JSON_ParserConfig
1490
1489
  decimal_point_pos = mantissa_digits; // Remember position of decimal point
1491
1490
  state->cursor++;
1492
1491
 
1493
- int fractional_digits = json_parse_digits(state, &mantissa);
1492
+ ssize_t fractional_digits = json_parse_digits(state, &mantissa);
1494
1493
  mantissa_digits += fractional_digits;
1495
1494
 
1496
1495
  if (RB_UNLIKELY(!fractional_digits)) {
@@ -1511,14 +1510,14 @@ static inline VALUE json_parse_number(JSON_ParserState *state, JSON_ParserConfig
1511
1510
  }
1512
1511
 
1513
1512
  uint64_t abs_exponent = 0;
1514
- int exponent_digits = json_parse_digits(state, &abs_exponent);
1513
+ ssize_t exponent_digits = json_parse_digits(state, &abs_exponent);
1515
1514
 
1516
1515
  if (RB_UNLIKELY(!exponent_digits)) {
1517
1516
  return Qundef;
1518
1517
  }
1519
1518
 
1520
1519
  if (RB_UNLIKELY(exponent_digits >= 20 || abs_exponent > (uint64_t)INT64_MAX)) {
1521
- exponent = negative_exponent ? INT64_MIN : INT64_MAX;
1520
+ exponent = negative_exponent ? INT32_MIN : INT32_MAX;
1522
1521
  } else {
1523
1522
  exponent = negative_exponent ? -(int64_t)abs_exponent : (int64_t)abs_exponent;
1524
1523
  }
@@ -1815,7 +1814,7 @@ ALWAYS_INLINE(static) bool json_parse_any(JSON_ParserState *state, JSON_ParserCo
1815
1814
  if (RB_LIKELY(peek(state) == '"')) {
1816
1815
  VALUE string = json_parse_string(state, config, true);
1817
1816
  if (UNDEF_P(string)) {
1818
- if (resumable) {
1817
+ if (resumable && eos(state)) {
1819
1818
  state->cursor = start;
1820
1819
  return false;
1821
1820
  } else {
@@ -2021,7 +2020,15 @@ static int parser_config_init_i(VALUE key, VALUE val, VALUE data)
2021
2020
  else if (key == sym_allow_invalid_escape) { config->allow_invalid_escape = RTEST(val); }
2022
2021
  else if (key == sym_symbolize_names) { config->symbolize_names = RTEST(val); }
2023
2022
  else if (key == sym_freeze) { config->freeze = RTEST(val); }
2024
- else if (key == sym_on_load) { parser_config_wb_write(self, &config->on_load_proc, RTEST(val) ? val : Qfalse); }
2023
+ else if (key == sym_on_load) {
2024
+ if (RTEST(val) && !rb_obj_is_proc(val)) {
2025
+ val = rb_check_funcall(val, rb_intern("to_proc"), 0, NULL);
2026
+ if (val == Qundef || !rb_obj_is_proc(val)) {
2027
+ rb_raise(rb_eTypeError, "on_load must be a Proc");
2028
+ }
2029
+ }
2030
+ parser_config_wb_write(self, &config->on_load_proc, RTEST(val) ? val : Qfalse);
2031
+ }
2025
2032
  else if (key == sym_allow_duplicate_key) { config->allow_duplicate_key = RTEST(val); }
2026
2033
  else if (key == sym_decimal_class) {
2027
2034
  if (RTEST(val)) {
@@ -2041,8 +2048,8 @@ static int parser_config_init_i(VALUE key, VALUE val, VALUE data)
2041
2048
  parser_config_wb_write(self, &config->decimal_class, rb_path_to_class(mod_path));
2042
2049
 
2043
2050
  const char *method_name_beg = last_colon + 1;
2044
- long before_len = method_name_beg - name_cstr;
2045
- long len = RSTRING_LEN(name) - before_len;
2051
+ rb_len_t before_len = method_name_beg - name_cstr;
2052
+ rb_len_t len = RSTRING_LEN(name) - before_len;
2046
2053
  VALUE method_name = rb_str_substr(name, before_len, len);
2047
2054
  config->decimal_method_id = SYM2ID(rb_str_intern(method_name));
2048
2055
  } else {
@@ -2109,9 +2116,7 @@ static VALUE cParser_parse(JSON_ParserConfig *config, VALUE src)
2109
2116
  // Ensure the string isn't mutated under us.
2110
2117
  // The classic API to use is `rb_str_locktmp`, but then we'd
2111
2118
  // need to use `rb_protect` to make sure we always unlock.
2112
- if (Vsource == src) {
2113
- Vsource = rb_str_new_frozen(Vsource);
2114
- }
2119
+ Vsource = rb_str_new_frozen(Vsource);
2115
2120
 
2116
2121
  VALUE rvalue_stack_buffer[RVALUE_STACK_INITIAL_CAPA];
2117
2122
  rvalue_stack value_stack = {
@@ -2135,7 +2140,7 @@ static VALUE cParser_parse(JSON_ParserConfig *config, VALUE src)
2135
2140
  .head = 1,
2136
2141
  };
2137
2142
 
2138
- long len;
2143
+ rb_len_t len;
2139
2144
  const char *start;
2140
2145
 
2141
2146
  RSTRING_GETMEM(Vsource, start, len);
@@ -2256,6 +2261,7 @@ typedef struct JSON_ResumableParserStruct {
2256
2261
  size_t incomplete_bytes;
2257
2262
  bool complete;
2258
2263
  bool in_use;
2264
+ bool initialized;
2259
2265
  } JSON_ResumableParser;
2260
2266
 
2261
2267
  static void JSON_ResumableParser_mark(void *ptr)
@@ -2392,9 +2398,15 @@ static VALUE cResumableParser_initialize(int argc, VALUE *argv, VALUE self)
2392
2398
  {
2393
2399
  rb_check_frozen(self);
2394
2400
 
2401
+ JSON_ResumableParser *parser = cResumableParser_get(self);
2402
+
2403
+ if (parser->initialized) {
2404
+ rb_raise(rb_eTypeError, "already initialized parser");
2405
+ }
2406
+ parser->initialized = true;
2407
+
2395
2408
  VALUE opts = Qfalse;
2396
2409
  rb_scan_args_kw(RB_SCAN_ARGS_LAST_HASH_KEYWORDS, argc, argv, "0:", &opts);
2397
- JSON_ResumableParser *parser = cResumableParser_get(self);
2398
2410
 
2399
2411
  opts = argc > 0 ? argv[0] : Qnil;
2400
2412
  parser_config_init(&parser->config, opts, self, true);
@@ -2454,7 +2466,7 @@ static VALUE cResumableParser_feed(VALUE self, VALUE str)
2454
2466
  rb_str_append(parser->buffer, str);
2455
2467
  }
2456
2468
 
2457
- long len;
2469
+ rb_len_t len;
2458
2470
  const char *start;
2459
2471
  RSTRING_GETMEM(parser->buffer, start, len);
2460
2472
  parser->state.start = start;
@@ -2760,7 +2772,7 @@ static VALUE cResumableParser_rest(VALUE self)
2760
2772
 
2761
2773
  size_t offset = parser->state.cursor - parser->state.start;
2762
2774
  const char *ptr;
2763
- long len;
2775
+ rb_len_t len;
2764
2776
  RSTRING_GETMEM(parser->buffer, ptr, len);
2765
2777
  return rb_utf8_str_new(ptr + offset, len - offset);
2766
2778
  }
@@ -1,16 +1,15 @@
1
1
  case RbConfig::CONFIG['host_cpu']
2
2
  when /^(arm|aarch64)/
3
3
  # Try to compile a small program using NEON instructions
4
- header, type, init, extra = 'arm_neon.h', 'uint8x16_t', 'vdupq_n_u8(32)', nil
4
+ header, type, init = 'arm_neon.h', 'uint8x16_t', 'vdupq_n_u8(32)'
5
5
  when /^(x86_64|x64)/
6
- header, type, init, extra = 'x86intrin.h', '__m128i', '_mm_set1_epi8(32)', 'if (__builtin_cpu_supports("sse2")) { printf("OK"); }'
6
+ header, type, init = 'x86intrin.h', '__m128i', '_mm_set1_epi8(32)'
7
7
  end
8
8
  if header
9
9
  if have_header(header) && try_compile(<<~SRC, '-Werror=implicit-function-declaration')
10
10
  #{cpp_include(header)}
11
11
  int main(int argc, char **argv) {
12
12
  #{type} test = #{init};
13
- #{extra}
14
13
  if (argc > 100000) printf("%p", &test);
15
14
  return 0;
16
15
  }
@@ -20,5 +19,3 @@ if header
20
19
  puts "Disable SIMD"
21
20
  end
22
21
  end
23
-
24
- have_header('cpuid.h')
@@ -143,7 +143,6 @@ ALWAYS_INLINE(static) int string_scan_simd_neon(const char **ptr, const char *en
143
143
  #define HAVE_SIMD 1
144
144
  #define HAVE_SIMD_SSE2 1
145
145
 
146
- #ifdef HAVE_CPUID_H
147
146
  #define FIND_SIMD_IMPLEMENTATION_DEFINED 1
148
147
 
149
148
  #if defined(__clang__) || defined(__GNUC__)
@@ -182,17 +181,10 @@ ALWAYS_INLINE(static) TARGET_SSE2 int string_scan_simd_sse2(const char **ptr, co
182
181
  return 0;
183
182
  }
184
183
 
185
- #include <cpuid.h>
186
- #endif /* HAVE_CPUID_H */
187
-
188
184
  static inline SIMD_Implementation find_simd_implementation(void)
189
185
  {
190
- // TODO Revisit. I think the SSE version now only uses SSE2 instructions.
191
- if (__builtin_cpu_supports("sse2")) {
192
- return SIMD_SSE2;
193
- }
194
-
195
- return SIMD_NONE;
186
+ // SSE2 is part of the x86-64 baseline.
187
+ return SIMD_SSE2;
196
188
  }
197
189
 
198
190
  #endif /* HAVE_X86INTRIN_H */
data/lib/json/common.rb CHANGED
@@ -80,14 +80,6 @@ module JSON
80
80
  def generator=(generator) # :nodoc:
81
81
  old, $VERBOSE = $VERBOSE, nil
82
82
 
83
- # The default proc used when the +sort_keys+ generation option is +true+.
84
- # It returns a new hash with the entries sorted by their keys.
85
- sort_keys_proc = ->(hash) { hash.sort.to_h }
86
- if defined?(::Ractor) && Ractor.respond_to?(:shareable_lambda)
87
- sort_keys_proc = Ractor.shareable_lambda(&sort_keys_proc)
88
- end
89
- generator::State.default_sort_keys_proc = sort_keys_proc
90
-
91
83
  @generator = generator
92
84
  if generator.const_defined?(:GeneratorMethods)
93
85
  generator_methods = generator::GeneratorMethods
@@ -146,7 +138,7 @@ module JSON
146
138
  # Is +nil+ when raised by JSON::ResumableParser.
147
139
  attr_reader :line
148
140
 
149
- # Column number where the parser encountered an error.
141
+ # One-based column number where the parser encountered an error, counted in Unicode codepoints.
150
142
  # Is +nil+ when raised by JSON::ResumableParser.
151
143
  attr_reader :column
152
144
 
@@ -179,10 +171,14 @@ module JSON
179
171
  when Integer
180
172
  "[#{segment}]"
181
173
  when String, Symbol
182
- if segment.match?(/\A[a-zA-Z\$\_][a-zA-Z\$\_0-9]*\z/)
174
+ segment = segment.to_s
175
+ if !segment.valid_encoding?
176
+ segment = segment.b.gsub(/["\\]/n, { '"' => '\\"', '\\' => '\\\\' }).force_encoding(Encoding::UTF_8)
177
+ %{["#{segment}"]}
178
+ elsif segment.match?(/\A[a-zA-Z\$\_][a-zA-Z\$\_0-9]*\z/)
183
179
  ".#{segment}"
184
180
  else
185
- segment = segment.to_s.gsub(/["\\]/, { '"' => '\\"', '\\' => '\\\\' })
181
+ segment = segment.gsub(/["\\]/, { '"' => '\\"', '\\' => '\\\\' })
186
182
  %{["#{segment}"]}
187
183
  end
188
184
  else
@@ -232,7 +228,7 @@ module JSON
232
228
  Fragment = Struct.new(:json) do
233
229
  def initialize(json)
234
230
  unless string = String.try_convert(json)
235
- raise TypeError, " no implicit conversion of #{json.class} into String"
231
+ raise TypeError, "no implicit conversion of #{json.class} into String"
236
232
  end
237
233
 
238
234
  super(string)
@@ -707,10 +703,10 @@ module JSON
707
703
  unless source.is_a?(String)
708
704
  if source.respond_to? :to_str
709
705
  source = source.to_str
706
+ elsif source.respond_to? :read
707
+ source = source.read
710
708
  elsif source.respond_to? :to_io
711
709
  source = source.to_io.read
712
- elsif source.respond_to?(:read)
713
- source = source.read
714
710
  end
715
711
  end
716
712
 
@@ -771,7 +767,7 @@ module JSON
771
767
  end
772
768
 
773
769
  opts = {
774
- allow_nan: true,
770
+ allow_nan: !(kwargs && kwargs[:rfc8785]),
775
771
  }
776
772
  opts[:max_nesting] = _deprecated_limit if _deprecated_limit
777
773
  opts.merge!(kwargs) if kwargs
@@ -835,7 +831,12 @@ module JSON
835
831
  #
836
832
  # puts MyApp::API_JSON_CODER.dump(Time.now.utc) # => "2025-01-21T08:41:44.286Z"
837
833
  #
838
- def initialize(object_class: nil, array_class: nil, on_load: nil, **options, &as_json)
834
+ # The conversion callback can also be passed as +as_json:+. If both are given, the block takes precedence.
835
+ # Passing +false+ or +nil+ for +strict:+ raises ArgumentError.
836
+ #
837
+ def initialize(object_class: nil, array_class: nil, on_load: nil, strict: true, **options, &as_json)
838
+ raise ArgumentError, "JSON::Coder requires strict mode" unless strict
839
+
839
840
  if object_class || array_class
840
841
  on_load = ParserOptions.on_load(on_load, object_class, array_class)
841
842
  end
@@ -847,7 +848,7 @@ module JSON
847
848
  @state = State.new(
848
849
  **generator_options,
849
850
  strict: true,
850
- as_json: as_json,
851
+ as_json: as_json || generator_options[:as_json],
851
852
  ).freeze
852
853
  end
853
854
 
@@ -54,7 +54,8 @@ module JSON
54
54
  strict: strict?,
55
55
  depth: depth,
56
56
  buffer_initial_length: buffer_initial_length,
57
- sort_keys: sort_keys
57
+ sort_keys: sort_keys,
58
+ rfc8785: rfc8785?,
58
59
  }
59
60
 
60
61
  allow_duplicate_key = allow_duplicate_key?
@@ -62,11 +63,6 @@ module JSON
62
63
  result[:allow_duplicate_key] = allow_duplicate_key
63
64
  end
64
65
 
65
- instance_variables.each do |iv|
66
- iv = iv.to_s[1..-1]
67
- result[iv.to_sym] = self[iv]
68
- end
69
-
70
66
  result
71
67
  end
72
68