json 1.2.0 → 2.19.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.
Files changed (145) hide show
  1. checksums.yaml +7 -0
  2. data/BSDL +22 -0
  3. data/CHANGES.md +783 -0
  4. data/COPYING +14 -16
  5. data/LEGAL +20 -0
  6. data/README.md +310 -0
  7. data/ext/json/ext/fbuffer/fbuffer.h +260 -0
  8. data/ext/json/ext/generator/extconf.rb +15 -8
  9. data/ext/json/ext/generator/generator.c +1675 -613
  10. data/ext/json/ext/json.h +116 -0
  11. data/ext/json/ext/parser/extconf.rb +16 -7
  12. data/ext/json/ext/parser/parser.c +1649 -1772
  13. data/ext/json/ext/simd/conf.rb +24 -0
  14. data/ext/json/ext/simd/simd.h +208 -0
  15. data/ext/json/ext/vendor/fpconv.c +480 -0
  16. data/ext/json/ext/vendor/jeaiii-ltoa.h +267 -0
  17. data/ext/json/ext/vendor/ryu.h +819 -0
  18. data/json.gemspec +62 -0
  19. data/lib/json/add/bigdecimal.rb +58 -0
  20. data/lib/json/add/complex.rb +51 -0
  21. data/lib/json/add/core.rb +11 -133
  22. data/lib/json/add/date.rb +54 -0
  23. data/lib/json/add/date_time.rb +67 -0
  24. data/lib/json/add/exception.rb +49 -0
  25. data/lib/json/add/ostruct.rb +54 -0
  26. data/lib/json/add/range.rb +54 -0
  27. data/lib/json/add/rational.rb +49 -0
  28. data/lib/json/add/regexp.rb +48 -0
  29. data/lib/json/add/set.rb +48 -0
  30. data/lib/json/add/string.rb +35 -0
  31. data/lib/json/add/struct.rb +52 -0
  32. data/lib/json/add/symbol.rb +52 -0
  33. data/lib/json/add/time.rb +52 -0
  34. data/lib/json/common.rb +1056 -254
  35. data/lib/json/ext/generator/state.rb +103 -0
  36. data/lib/json/ext.rb +35 -5
  37. data/lib/json/generic_object.rb +67 -0
  38. data/lib/json/truffle_ruby/generator.rb +755 -0
  39. data/lib/json/version.rb +3 -6
  40. data/lib/json.rb +671 -6
  41. metadata +68 -159
  42. data/CHANGES +0 -136
  43. data/GPL +0 -340
  44. data/README +0 -360
  45. data/Rakefile +0 -287
  46. data/TODO +0 -1
  47. data/VERSION +0 -1
  48. data/benchmarks/data-p4-3GHz-ruby18/GeneratorBenchmarkComparison.log +0 -52
  49. data/benchmarks/data-p4-3GHz-ruby18/GeneratorBenchmarkExt#generator_fast-autocorrelation.dat +0 -1000
  50. data/benchmarks/data-p4-3GHz-ruby18/GeneratorBenchmarkExt#generator_fast.dat +0 -1001
  51. data/benchmarks/data-p4-3GHz-ruby18/GeneratorBenchmarkExt#generator_pretty-autocorrelation.dat +0 -900
  52. data/benchmarks/data-p4-3GHz-ruby18/GeneratorBenchmarkExt#generator_pretty.dat +0 -901
  53. data/benchmarks/data-p4-3GHz-ruby18/GeneratorBenchmarkExt#generator_safe-autocorrelation.dat +0 -1000
  54. data/benchmarks/data-p4-3GHz-ruby18/GeneratorBenchmarkExt#generator_safe.dat +0 -1001
  55. data/benchmarks/data-p4-3GHz-ruby18/GeneratorBenchmarkExt.log +0 -261
  56. data/benchmarks/data-p4-3GHz-ruby18/GeneratorBenchmarkPure#generator_fast-autocorrelation.dat +0 -1000
  57. data/benchmarks/data-p4-3GHz-ruby18/GeneratorBenchmarkPure#generator_fast.dat +0 -1001
  58. data/benchmarks/data-p4-3GHz-ruby18/GeneratorBenchmarkPure#generator_pretty-autocorrelation.dat +0 -1000
  59. data/benchmarks/data-p4-3GHz-ruby18/GeneratorBenchmarkPure#generator_pretty.dat +0 -1001
  60. data/benchmarks/data-p4-3GHz-ruby18/GeneratorBenchmarkPure#generator_safe-autocorrelation.dat +0 -1000
  61. data/benchmarks/data-p4-3GHz-ruby18/GeneratorBenchmarkPure#generator_safe.dat +0 -1001
  62. data/benchmarks/data-p4-3GHz-ruby18/GeneratorBenchmarkPure.log +0 -262
  63. data/benchmarks/data-p4-3GHz-ruby18/GeneratorBenchmarkRails#generator-autocorrelation.dat +0 -1000
  64. data/benchmarks/data-p4-3GHz-ruby18/GeneratorBenchmarkRails#generator.dat +0 -1001
  65. data/benchmarks/data-p4-3GHz-ruby18/GeneratorBenchmarkRails.log +0 -82
  66. data/benchmarks/data-p4-3GHz-ruby18/ParserBenchmarkComparison.log +0 -34
  67. data/benchmarks/data-p4-3GHz-ruby18/ParserBenchmarkExt#parser-autocorrelation.dat +0 -900
  68. data/benchmarks/data-p4-3GHz-ruby18/ParserBenchmarkExt#parser.dat +0 -901
  69. data/benchmarks/data-p4-3GHz-ruby18/ParserBenchmarkExt.log +0 -81
  70. data/benchmarks/data-p4-3GHz-ruby18/ParserBenchmarkPure#parser-autocorrelation.dat +0 -1000
  71. data/benchmarks/data-p4-3GHz-ruby18/ParserBenchmarkPure#parser.dat +0 -1001
  72. data/benchmarks/data-p4-3GHz-ruby18/ParserBenchmarkPure.log +0 -82
  73. data/benchmarks/data-p4-3GHz-ruby18/ParserBenchmarkRails#parser-autocorrelation.dat +0 -1000
  74. data/benchmarks/data-p4-3GHz-ruby18/ParserBenchmarkRails#parser.dat +0 -1001
  75. data/benchmarks/data-p4-3GHz-ruby18/ParserBenchmarkRails.log +0 -82
  76. data/benchmarks/data-p4-3GHz-ruby18/ParserBenchmarkYAML#parser-autocorrelation.dat +0 -1000
  77. data/benchmarks/data-p4-3GHz-ruby18/ParserBenchmarkYAML#parser.dat +0 -1001
  78. data/benchmarks/data-p4-3GHz-ruby18/ParserBenchmarkYAML.log +0 -82
  79. data/benchmarks/generator_benchmark.rb +0 -165
  80. data/benchmarks/parser_benchmark.rb +0 -197
  81. data/bin/edit_json.rb +0 -9
  82. data/bin/prettify_json.rb +0 -75
  83. data/data/example.json +0 -1
  84. data/data/index.html +0 -38
  85. data/data/prototype.js +0 -4184
  86. data/ext/json/ext/generator/unicode.c +0 -180
  87. data/ext/json/ext/generator/unicode.h +0 -53
  88. data/ext/json/ext/parser/parser.rl +0 -737
  89. data/ext/json/ext/parser/unicode.c +0 -154
  90. data/ext/json/ext/parser/unicode.h +0 -58
  91. data/install.rb +0 -26
  92. data/lib/json/Array.xpm +0 -21
  93. data/lib/json/FalseClass.xpm +0 -21
  94. data/lib/json/Hash.xpm +0 -21
  95. data/lib/json/Key.xpm +0 -73
  96. data/lib/json/NilClass.xpm +0 -21
  97. data/lib/json/Numeric.xpm +0 -28
  98. data/lib/json/String.xpm +0 -96
  99. data/lib/json/TrueClass.xpm +0 -21
  100. data/lib/json/add/rails.rb +0 -58
  101. data/lib/json/editor.rb +0 -1371
  102. data/lib/json/json.xpm +0 -1499
  103. data/lib/json/pure/generator.rb +0 -443
  104. data/lib/json/pure/parser.rb +0 -303
  105. data/lib/json/pure.rb +0 -77
  106. data/tests/fixtures/fail1.json +0 -1
  107. data/tests/fixtures/fail10.json +0 -1
  108. data/tests/fixtures/fail11.json +0 -1
  109. data/tests/fixtures/fail12.json +0 -1
  110. data/tests/fixtures/fail13.json +0 -1
  111. data/tests/fixtures/fail14.json +0 -1
  112. data/tests/fixtures/fail18.json +0 -1
  113. data/tests/fixtures/fail19.json +0 -1
  114. data/tests/fixtures/fail2.json +0 -1
  115. data/tests/fixtures/fail20.json +0 -1
  116. data/tests/fixtures/fail21.json +0 -1
  117. data/tests/fixtures/fail22.json +0 -1
  118. data/tests/fixtures/fail23.json +0 -1
  119. data/tests/fixtures/fail24.json +0 -1
  120. data/tests/fixtures/fail25.json +0 -1
  121. data/tests/fixtures/fail27.json +0 -2
  122. data/tests/fixtures/fail28.json +0 -2
  123. data/tests/fixtures/fail3.json +0 -1
  124. data/tests/fixtures/fail4.json +0 -1
  125. data/tests/fixtures/fail5.json +0 -1
  126. data/tests/fixtures/fail6.json +0 -1
  127. data/tests/fixtures/fail7.json +0 -1
  128. data/tests/fixtures/fail8.json +0 -1
  129. data/tests/fixtures/fail9.json +0 -1
  130. data/tests/fixtures/pass1.json +0 -56
  131. data/tests/fixtures/pass15.json +0 -1
  132. data/tests/fixtures/pass16.json +0 -1
  133. data/tests/fixtures/pass17.json +0 -1
  134. data/tests/fixtures/pass2.json +0 -1
  135. data/tests/fixtures/pass26.json +0 -1
  136. data/tests/fixtures/pass3.json +0 -6
  137. data/tests/test_json.rb +0 -320
  138. data/tests/test_json_addition.rb +0 -164
  139. data/tests/test_json_encoding.rb +0 -67
  140. data/tests/test_json_fixtures.rb +0 -34
  141. data/tests/test_json_generate.rb +0 -120
  142. data/tests/test_json_rails.rb +0 -146
  143. data/tests/test_json_unicode.rb +0 -62
  144. data/tools/fuzz.rb +0 -139
  145. data/tools/server.rb +0 -61
@@ -1,1484 +1,1467 @@
1
+ #include "../json.h"
2
+ #include "../vendor/ryu.h"
3
+ #include "../simd/simd.h"
1
4
 
2
- #line 1 "parser.rl"
3
- #include "ruby.h"
4
- #include "unicode.h"
5
- #if HAVE_RE_H
6
- #include "re.h"
5
+ static VALUE mJSON, eNestingError, Encoding_UTF_8;
6
+ static VALUE CNaN, CInfinity, CMinusInfinity;
7
+
8
+ static ID i_new, i_try_convert, i_uminus, i_encode;
9
+
10
+ static VALUE sym_max_nesting, sym_allow_nan, sym_allow_trailing_comma, sym_allow_control_characters,
11
+ sym_allow_invalid_escape, sym_symbolize_names, sym_freeze, sym_decimal_class, sym_on_load,
12
+ sym_allow_duplicate_key;
13
+
14
+ static int binary_encindex;
15
+ static int utf8_encindex;
16
+
17
+ #ifndef HAVE_RB_HASH_BULK_INSERT
18
+ // For TruffleRuby
19
+ static void
20
+ rb_hash_bulk_insert(long count, const VALUE *pairs, VALUE hash)
21
+ {
22
+ long index = 0;
23
+ while (index < count) {
24
+ VALUE name = pairs[index++];
25
+ VALUE value = pairs[index++];
26
+ rb_hash_aset(hash, name, value);
27
+ }
28
+ RB_GC_GUARD(hash);
29
+ }
30
+ #endif
31
+
32
+ #ifndef HAVE_RB_HASH_NEW_CAPA
33
+ #define rb_hash_new_capa(n) rb_hash_new()
34
+ #endif
35
+
36
+ #ifndef HAVE_RB_STR_TO_INTERNED_STR
37
+ static VALUE rb_str_to_interned_str(VALUE str)
38
+ {
39
+ return rb_funcall(rb_str_freeze(str), i_uminus, 0);
40
+ }
41
+ #endif
42
+
43
+ /* name cache */
44
+
45
+ #include <string.h>
46
+ #include <ctype.h>
47
+
48
+ // Object names are likely to be repeated, and are frozen.
49
+ // As such we can re-use them if we keep a cache of the ones we've seen so far,
50
+ // and save much more expensive lookups into the global fstring table.
51
+ // This cache implementation is deliberately simple, as we're optimizing for compactness,
52
+ // to be able to fit safely on the stack.
53
+ // As such, binary search into a sorted array gives a good tradeoff between compactness and
54
+ // performance.
55
+ #define JSON_RVALUE_CACHE_CAPA 63
56
+ typedef struct rvalue_cache_struct {
57
+ int length;
58
+ VALUE entries[JSON_RVALUE_CACHE_CAPA];
59
+ } rvalue_cache;
60
+
61
+ static rb_encoding *enc_utf8;
62
+
63
+ #define JSON_RVALUE_CACHE_MAX_ENTRY_LENGTH 55
64
+
65
+ static inline VALUE build_interned_string(const char *str, const long length)
66
+ {
67
+ # ifdef HAVE_RB_ENC_INTERNED_STR
68
+ return rb_enc_interned_str(str, length, enc_utf8);
69
+ # else
70
+ VALUE rstring = rb_utf8_str_new(str, length);
71
+ return rb_funcall(rb_str_freeze(rstring), i_uminus, 0);
72
+ # endif
73
+ }
74
+
75
+ static inline VALUE build_symbol(const char *str, const long length)
76
+ {
77
+ return rb_str_intern(build_interned_string(str, length));
78
+ }
79
+
80
+ static void rvalue_cache_insert_at(rvalue_cache *cache, int index, VALUE rstring)
81
+ {
82
+ MEMMOVE(&cache->entries[index + 1], &cache->entries[index], VALUE, cache->length - index);
83
+ cache->length++;
84
+ cache->entries[index] = rstring;
85
+ }
86
+
87
+ #define rstring_cache_memcmp memcmp
88
+
89
+ #if JSON_CPU_LITTLE_ENDIAN_64BITS
90
+ #if __has_builtin(__builtin_bswap64)
91
+ #undef rstring_cache_memcmp
92
+ ALWAYS_INLINE(static) int rstring_cache_memcmp(const char *str, const char *rptr, const long length)
93
+ {
94
+ // The libc memcmp has numerous complex optimizations, but in this particular case,
95
+ // we know the string is small (JSON_RVALUE_CACHE_MAX_ENTRY_LENGTH), so being able to
96
+ // inline a simpler memcmp outperforms calling the libc version.
97
+ long i = 0;
98
+
99
+ for (; i + 8 <= length; i += 8) {
100
+ uint64_t a, b;
101
+ memcpy(&a, str + i, 8);
102
+ memcpy(&b, rptr + i, 8);
103
+ if (a != b) {
104
+ a = __builtin_bswap64(a);
105
+ b = __builtin_bswap64(b);
106
+ return (a < b) ? -1 : 1;
107
+ }
108
+ }
109
+
110
+ for (; i < length; i++) {
111
+ if (str[i] != rptr[i]) {
112
+ return (str[i] < rptr[i]) ? -1 : 1;
113
+ }
114
+ }
115
+
116
+ return 0;
117
+ }
118
+ #endif
119
+ #endif
120
+
121
+ ALWAYS_INLINE(static) int rstring_cache_cmp(const char *str, const long length, VALUE rstring)
122
+ {
123
+ const char *rstring_ptr;
124
+ long rstring_length;
125
+
126
+ RSTRING_GETMEM(rstring, rstring_ptr, rstring_length);
127
+
128
+ if (length == rstring_length) {
129
+ return rstring_cache_memcmp(str, rstring_ptr, length);
130
+ } else {
131
+ return (int)(length - rstring_length);
132
+ }
133
+ }
134
+
135
+ ALWAYS_INLINE(static) VALUE rstring_cache_fetch(rvalue_cache *cache, const char *str, const long length)
136
+ {
137
+ int low = 0;
138
+ int high = cache->length - 1;
139
+
140
+ while (low <= high) {
141
+ int mid = (high + low) >> 1;
142
+ VALUE entry = cache->entries[mid];
143
+ int cmp = rstring_cache_cmp(str, length, entry);
144
+
145
+ if (cmp == 0) {
146
+ return entry;
147
+ } else if (cmp > 0) {
148
+ low = mid + 1;
149
+ } else {
150
+ high = mid - 1;
151
+ }
152
+ }
153
+
154
+ VALUE rstring = build_interned_string(str, length);
155
+
156
+ if (cache->length < JSON_RVALUE_CACHE_CAPA) {
157
+ rvalue_cache_insert_at(cache, low, rstring);
158
+ }
159
+ return rstring;
160
+ }
161
+
162
+ static VALUE rsymbol_cache_fetch(rvalue_cache *cache, const char *str, const long length)
163
+ {
164
+ int low = 0;
165
+ int high = cache->length - 1;
166
+
167
+ while (low <= high) {
168
+ int mid = (high + low) >> 1;
169
+ VALUE entry = cache->entries[mid];
170
+ int cmp = rstring_cache_cmp(str, length, rb_sym2str(entry));
171
+
172
+ if (cmp == 0) {
173
+ return entry;
174
+ } else if (cmp > 0) {
175
+ low = mid + 1;
176
+ } else {
177
+ high = mid - 1;
178
+ }
179
+ }
180
+
181
+ VALUE rsymbol = build_symbol(str, length);
182
+
183
+ if (cache->length < JSON_RVALUE_CACHE_CAPA) {
184
+ rvalue_cache_insert_at(cache, low, rsymbol);
185
+ }
186
+ return rsymbol;
187
+ }
188
+
189
+ /* rvalue stack */
190
+
191
+ #define RVALUE_STACK_INITIAL_CAPA 128
192
+
193
+ enum rvalue_stack_type {
194
+ RVALUE_STACK_HEAP_ALLOCATED = 0,
195
+ RVALUE_STACK_STACK_ALLOCATED = 1,
196
+ };
197
+
198
+ typedef struct rvalue_stack_struct {
199
+ enum rvalue_stack_type type;
200
+ long capa;
201
+ long head;
202
+ VALUE *ptr;
203
+ } rvalue_stack;
204
+
205
+ static rvalue_stack *rvalue_stack_spill(rvalue_stack *old_stack, VALUE *handle, rvalue_stack **stack_ref);
206
+
207
+ static rvalue_stack *rvalue_stack_grow(rvalue_stack *stack, VALUE *handle, rvalue_stack **stack_ref)
208
+ {
209
+ long required = stack->capa * 2;
210
+
211
+ if (stack->type == RVALUE_STACK_STACK_ALLOCATED) {
212
+ stack = rvalue_stack_spill(stack, handle, stack_ref);
213
+ } else {
214
+ REALLOC_N(stack->ptr, VALUE, required);
215
+ stack->capa = required;
216
+ }
217
+ return stack;
218
+ }
219
+
220
+ static VALUE rvalue_stack_push(rvalue_stack *stack, VALUE value, VALUE *handle, rvalue_stack **stack_ref)
221
+ {
222
+ if (RB_UNLIKELY(stack->head >= stack->capa)) {
223
+ stack = rvalue_stack_grow(stack, handle, stack_ref);
224
+ }
225
+ stack->ptr[stack->head] = value;
226
+ stack->head++;
227
+ return value;
228
+ }
229
+
230
+ static inline VALUE *rvalue_stack_peek(rvalue_stack *stack, long count)
231
+ {
232
+ return stack->ptr + (stack->head - count);
233
+ }
234
+
235
+ static inline void rvalue_stack_pop(rvalue_stack *stack, long count)
236
+ {
237
+ stack->head -= count;
238
+ }
239
+
240
+ static void rvalue_stack_mark(void *ptr)
241
+ {
242
+ rvalue_stack *stack = (rvalue_stack *)ptr;
243
+ long index;
244
+ if (stack && stack->ptr) {
245
+ for (index = 0; index < stack->head; index++) {
246
+ rb_gc_mark(stack->ptr[index]);
247
+ }
248
+ }
249
+ }
250
+
251
+ static void rvalue_stack_free_buffer(rvalue_stack *stack)
252
+ {
253
+ ruby_xfree(stack->ptr);
254
+ stack->ptr = NULL;
255
+ }
256
+
257
+ static void rvalue_stack_free(void *ptr)
258
+ {
259
+ rvalue_stack *stack = (rvalue_stack *)ptr;
260
+ if (stack) {
261
+ rvalue_stack_free_buffer(stack);
262
+ #ifndef HAVE_RUBY_TYPED_EMBEDDABLE
263
+ ruby_xfree(stack);
7
264
  #endif
8
- #if HAVE_RUBY_ST_H
9
- #include "ruby/st.h"
265
+ }
266
+ }
267
+
268
+ static size_t rvalue_stack_memsize(const void *ptr)
269
+ {
270
+ const rvalue_stack *stack = (const rvalue_stack *)ptr;
271
+ return sizeof(rvalue_stack) + sizeof(VALUE) * stack->capa;
272
+ }
273
+
274
+ static const rb_data_type_t JSON_Parser_rvalue_stack_type = {
275
+ .wrap_struct_name = "JSON::Ext::Parser/rvalue_stack",
276
+ .function = {
277
+ .dmark = rvalue_stack_mark,
278
+ .dfree = rvalue_stack_free,
279
+ .dsize = rvalue_stack_memsize,
280
+ },
281
+ .flags = RUBY_TYPED_FREE_IMMEDIATELY | RUBY_TYPED_EMBEDDABLE,
282
+ };
283
+
284
+ static rvalue_stack *rvalue_stack_spill(rvalue_stack *old_stack, VALUE *handle, rvalue_stack **stack_ref)
285
+ {
286
+ rvalue_stack *stack;
287
+ *handle = TypedData_Make_Struct(0, rvalue_stack, &JSON_Parser_rvalue_stack_type, stack);
288
+ *stack_ref = stack;
289
+ MEMCPY(stack, old_stack, rvalue_stack, 1);
290
+
291
+ stack->capa = old_stack->capa << 1;
292
+ stack->ptr = ALLOC_N(VALUE, stack->capa);
293
+ stack->type = RVALUE_STACK_HEAP_ALLOCATED;
294
+ MEMCPY(stack->ptr, old_stack->ptr, VALUE, old_stack->head);
295
+ return stack;
296
+ }
297
+
298
+ static void rvalue_stack_eagerly_release(VALUE handle)
299
+ {
300
+ if (handle) {
301
+ rvalue_stack *stack;
302
+ TypedData_Get_Struct(handle, rvalue_stack, &JSON_Parser_rvalue_stack_type, stack);
303
+ #ifdef HAVE_RUBY_TYPED_EMBEDDABLE
304
+ rvalue_stack_free_buffer(stack);
305
+ #else
306
+ rvalue_stack_free(stack);
307
+ RTYPEDDATA_DATA(handle) = NULL;
10
308
  #endif
11
- #if HAVE_ST_H
12
- #include "st.h"
309
+ }
310
+ }
311
+
312
+ static int convert_UTF32_to_UTF8(char *buf, uint32_t ch)
313
+ {
314
+ int len = 1;
315
+ if (ch <= 0x7F) {
316
+ buf[0] = (char) ch;
317
+ } else if (ch <= 0x07FF) {
318
+ buf[0] = (char) ((ch >> 6) | 0xC0);
319
+ buf[1] = (char) ((ch & 0x3F) | 0x80);
320
+ len++;
321
+ } else if (ch <= 0xFFFF) {
322
+ buf[0] = (char) ((ch >> 12) | 0xE0);
323
+ buf[1] = (char) (((ch >> 6) & 0x3F) | 0x80);
324
+ buf[2] = (char) ((ch & 0x3F) | 0x80);
325
+ len += 2;
326
+ } else if (ch <= 0x1fffff) {
327
+ buf[0] =(char) ((ch >> 18) | 0xF0);
328
+ buf[1] =(char) (((ch >> 12) & 0x3F) | 0x80);
329
+ buf[2] =(char) (((ch >> 6) & 0x3F) | 0x80);
330
+ buf[3] =(char) ((ch & 0x3F) | 0x80);
331
+ len += 3;
332
+ } else {
333
+ buf[0] = '?';
334
+ }
335
+ return len;
336
+ }
337
+
338
+ enum duplicate_key_action {
339
+ JSON_DEPRECATED = 0,
340
+ JSON_IGNORE,
341
+ JSON_RAISE,
342
+ };
343
+
344
+ typedef struct JSON_ParserStruct {
345
+ VALUE on_load_proc;
346
+ VALUE decimal_class;
347
+ ID decimal_method_id;
348
+ enum duplicate_key_action on_duplicate_key;
349
+ int max_nesting;
350
+ bool allow_nan;
351
+ bool allow_trailing_comma;
352
+ bool allow_control_characters;
353
+ bool allow_invalid_escape;
354
+ bool symbolize_names;
355
+ bool freeze;
356
+ } JSON_ParserConfig;
357
+
358
+ typedef struct JSON_ParserStateStruct {
359
+ VALUE *stack_handle;
360
+ const char *start;
361
+ const char *cursor;
362
+ const char *end;
363
+ rvalue_stack *stack;
364
+ rvalue_cache name_cache;
365
+ int in_array;
366
+ int current_nesting;
367
+ unsigned int emitted_deprecations;
368
+ } JSON_ParserState;
369
+
370
+ static inline size_t rest(JSON_ParserState *state) {
371
+ return state->end - state->cursor;
372
+ }
373
+
374
+ static inline bool eos(JSON_ParserState *state) {
375
+ return state->cursor >= state->end;
376
+ }
377
+
378
+ static inline char peek(JSON_ParserState *state)
379
+ {
380
+ if (RB_UNLIKELY(eos(state))) {
381
+ return 0;
382
+ }
383
+ return *state->cursor;
384
+ }
385
+
386
+ static void cursor_position(JSON_ParserState *state, long *line_out, long *column_out)
387
+ {
388
+ JSON_ASSERT(state->cursor <= state->end);
389
+
390
+ // Redundant but helpful for hardening
391
+ if (RB_UNLIKELY(state->cursor > state->end)) {
392
+ state->cursor = state->end;
393
+ }
394
+
395
+ const char *cursor = state->cursor;
396
+ long column = 0;
397
+ long line = 1;
398
+
399
+ while (cursor >= state->start) {
400
+ if (*cursor-- == '\n') {
401
+ break;
402
+ }
403
+ column++;
404
+ }
405
+
406
+ while (cursor >= state->start) {
407
+ if (*cursor-- == '\n') {
408
+ line++;
409
+ }
410
+ }
411
+ *line_out = line;
412
+ *column_out = column;
413
+ }
414
+
415
+ static void emit_parse_warning(const char *message, JSON_ParserState *state)
416
+ {
417
+ long line, column;
418
+ cursor_position(state, &line, &column);
419
+
420
+ VALUE warning = rb_sprintf("%s at line %ld column %ld", message, line, column);
421
+ rb_funcall(mJSON, rb_intern("deprecation_warning"), 1, warning);
422
+ }
423
+
424
+ #define PARSE_ERROR_FRAGMENT_LEN 32
425
+
426
+ static VALUE build_parse_error_message(const char *format, JSON_ParserState *state, long line, long column)
427
+ {
428
+ unsigned char buffer[PARSE_ERROR_FRAGMENT_LEN + 3];
429
+
430
+ const char *ptr = "EOF";
431
+ if (state->cursor && state->cursor < state->end) {
432
+ ptr = state->cursor;
433
+ size_t len = 0;
434
+ while (len < PARSE_ERROR_FRAGMENT_LEN) {
435
+ char ch = ptr[len];
436
+ if (!ch || ch == '\n' || ch == ' ' || ch == '\t' || ch == '\r') {
437
+ break;
438
+ }
439
+ len++;
440
+ }
441
+
442
+ if (len) {
443
+ buffer[0] = '\'';
444
+ MEMCPY(buffer + 1, ptr, char, len);
445
+
446
+ while (buffer[len] >= 0x80 && buffer[len] < 0xC0) { // Is continuation byte
447
+ len--;
448
+ }
449
+
450
+ if (buffer[len] >= 0xC0) { // multibyte character start
451
+ len--;
452
+ }
453
+
454
+ buffer[len + 1] = '\'';
455
+ buffer[len + 2] = '\0';
456
+ ptr = (const char *)buffer;
457
+ }
458
+ }
459
+
460
+ VALUE message = rb_enc_sprintf(enc_utf8, format, ptr);
461
+ rb_str_catf(message, " at line %ld column %ld", line, column);
462
+ return message;
463
+ }
464
+
465
+ static VALUE parse_error_new(VALUE message, long line, long column)
466
+ {
467
+ VALUE exc = rb_exc_new_str(rb_path2class("JSON::ParserError"), message);
468
+ rb_ivar_set(exc, rb_intern("@line"), LONG2NUM(line));
469
+ rb_ivar_set(exc, rb_intern("@column"), LONG2NUM(column));
470
+ return exc;
471
+ }
472
+
473
+ NORETURN(static) void raise_parse_error(const char *format, JSON_ParserState *state)
474
+ {
475
+ long line, column;
476
+ cursor_position(state, &line, &column);
477
+ VALUE message = build_parse_error_message(format, state, line, column);
478
+ rb_exc_raise(parse_error_new(message, line, column));
479
+ }
480
+
481
+ NORETURN(static) void raise_parse_error_at(const char *format, JSON_ParserState *state, const char *at)
482
+ {
483
+ state->cursor = at;
484
+ raise_parse_error(format, state);
485
+ }
486
+
487
+ /* unicode */
488
+
489
+ static const signed char digit_values[256] = {
490
+ -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
491
+ -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
492
+ -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, -1,
493
+ -1, -1, -1, -1, -1, -1, 10, 11, 12, 13, 14, 15, -1, -1, -1, -1, -1, -1, -1,
494
+ -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
495
+ 10, 11, 12, 13, 14, 15, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
496
+ -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
497
+ -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
498
+ -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
499
+ -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
500
+ -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
501
+ -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
502
+ -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
503
+ -1, -1, -1, -1, -1, -1, -1
504
+ };
505
+
506
+ static uint32_t unescape_unicode(JSON_ParserState *state, const char *sp, const char *spe)
507
+ {
508
+ if (RB_UNLIKELY(sp > spe - 4)) {
509
+ raise_parse_error_at("incomplete unicode character escape sequence at %s", state, sp - 2);
510
+ }
511
+
512
+ const unsigned char *p = (const unsigned char *)sp;
513
+
514
+ const signed char b0 = digit_values[p[0]];
515
+ const signed char b1 = digit_values[p[1]];
516
+ const signed char b2 = digit_values[p[2]];
517
+ const signed char b3 = digit_values[p[3]];
518
+
519
+ if (RB_UNLIKELY((signed char)(b0 | b1 | b2 | b3) < 0)) {
520
+ raise_parse_error_at("incomplete unicode character escape sequence at %s", state, sp - 2);
521
+ }
522
+
523
+ return ((uint32_t)b0 << 12) | ((uint32_t)b1 << 8) | ((uint32_t)b2 << 4) | (uint32_t)b3;
524
+ }
525
+
526
+ #define GET_PARSER_CONFIG \
527
+ JSON_ParserConfig *config; \
528
+ TypedData_Get_Struct(self, JSON_ParserConfig, &JSON_ParserConfig_type, config)
529
+
530
+ static const rb_data_type_t JSON_ParserConfig_type;
531
+
532
+ static void
533
+ json_eat_comments(JSON_ParserState *state)
534
+ {
535
+ const char *start = state->cursor;
536
+ state->cursor++;
537
+
538
+ switch (peek(state)) {
539
+ case '/': {
540
+ state->cursor = memchr(state->cursor, '\n', state->end - state->cursor);
541
+ if (!state->cursor) {
542
+ state->cursor = state->end;
543
+ } else {
544
+ state->cursor++;
545
+ }
546
+ break;
547
+ }
548
+ case '*': {
549
+ state->cursor++;
550
+
551
+ while (true) {
552
+ const char *next_match = memchr(state->cursor, '*', state->end - state->cursor);
553
+ if (!next_match) {
554
+ raise_parse_error_at("unterminated comment, expected closing '*/'", state, start);
555
+ }
556
+
557
+ state->cursor = next_match + 1;
558
+ if (peek(state) == '/') {
559
+ state->cursor++;
560
+ break;
561
+ }
562
+ }
563
+ break;
564
+ }
565
+ default:
566
+ raise_parse_error_at("unexpected token %s", state, start);
567
+ break;
568
+ }
569
+ }
570
+
571
+ ALWAYS_INLINE(static) void
572
+ json_eat_whitespace(JSON_ParserState *state)
573
+ {
574
+ while (true) {
575
+ switch (peek(state)) {
576
+ case ' ':
577
+ state->cursor++;
578
+ break;
579
+ case '\n':
580
+ state->cursor++;
581
+
582
+ // Heuristic: if we see a newline, there is likely consecutive spaces after it.
583
+ #if JSON_CPU_LITTLE_ENDIAN_64BITS
584
+ while (rest(state) > 8) {
585
+ uint64_t chunk;
586
+ memcpy(&chunk, state->cursor, sizeof(uint64_t));
587
+ if (chunk == 0x2020202020202020) {
588
+ state->cursor += 8;
589
+ continue;
590
+ }
591
+
592
+ uint32_t consecutive_spaces = trailing_zeros64(chunk ^ 0x2020202020202020) / CHAR_BIT;
593
+ state->cursor += consecutive_spaces;
594
+ break;
595
+ }
13
596
  #endif
597
+ break;
598
+ case '\t':
599
+ case '\r':
600
+ state->cursor++;
601
+ break;
602
+ case '/':
603
+ json_eat_comments(state);
604
+ break;
605
+
606
+ default:
607
+ return;
608
+ }
609
+ }
610
+ }
611
+
612
+ static inline VALUE build_string(const char *start, const char *end, bool intern, bool symbolize)
613
+ {
614
+ if (symbolize) {
615
+ intern = true;
616
+ }
617
+ VALUE result;
618
+ # ifdef HAVE_RB_ENC_INTERNED_STR
619
+ if (intern) {
620
+ result = rb_enc_interned_str(start, (long)(end - start), enc_utf8);
621
+ } else {
622
+ result = rb_utf8_str_new(start, (long)(end - start));
623
+ }
624
+ # else
625
+ result = rb_utf8_str_new(start, (long)(end - start));
626
+ if (intern) {
627
+ result = rb_funcall(rb_str_freeze(result), i_uminus, 0);
628
+ }
629
+ # endif
630
+
631
+ if (symbolize) {
632
+ result = rb_str_intern(result);
633
+ }
634
+
635
+ return result;
636
+ }
637
+
638
+ static inline bool json_string_cacheable_p(const char *string, size_t length)
639
+ {
640
+ // We mostly want to cache strings that are likely to be repeated.
641
+ // Simple heuristics:
642
+ // - Common names aren't likely to be very long. So we just don't cache names above an arbitrary threshold.
643
+ // - If the first character isn't a letter, we're much less likely to see this string again.
644
+ return length <= JSON_RVALUE_CACHE_MAX_ENTRY_LENGTH && rb_isalpha(string[0]);
645
+ }
646
+
647
+ static inline VALUE json_string_fastpath(JSON_ParserState *state, JSON_ParserConfig *config, const char *string, const char *stringEnd, bool is_name)
648
+ {
649
+ bool intern = is_name || config->freeze;
650
+ bool symbolize = is_name && config->symbolize_names;
651
+ size_t bufferSize = stringEnd - string;
652
+
653
+ if (is_name && state->in_array && RB_LIKELY(json_string_cacheable_p(string, bufferSize))) {
654
+ VALUE cached_key;
655
+ if (RB_UNLIKELY(symbolize)) {
656
+ cached_key = rsymbol_cache_fetch(&state->name_cache, string, bufferSize);
657
+ } else {
658
+ cached_key = rstring_cache_fetch(&state->name_cache, string, bufferSize);
659
+ }
660
+
661
+ if (RB_LIKELY(cached_key)) {
662
+ return cached_key;
663
+ }
664
+ }
665
+
666
+ return build_string(string, stringEnd, intern, symbolize);
667
+ }
668
+
669
+ #define JSON_MAX_UNESCAPE_POSITIONS 16
670
+ typedef struct _json_unescape_positions {
671
+ long size;
672
+ const char **positions;
673
+ unsigned long additional_backslashes;
674
+ } JSON_UnescapePositions;
675
+
676
+ static inline const char *json_next_backslash(const char *pe, const char *stringEnd, JSON_UnescapePositions *positions)
677
+ {
678
+ while (positions->size) {
679
+ positions->size--;
680
+ const char *next_position = positions->positions[0];
681
+ positions->positions++;
682
+ if (next_position >= pe) {
683
+ return next_position;
684
+ }
685
+ }
686
+
687
+ if (positions->additional_backslashes) {
688
+ positions->additional_backslashes--;
689
+ return memchr(pe, '\\', stringEnd - pe);
690
+ }
691
+
692
+ return NULL;
693
+ }
694
+
695
+ NOINLINE(static) VALUE json_string_unescape(JSON_ParserState *state, JSON_ParserConfig *config, const char *string, const char *stringEnd, bool is_name, JSON_UnescapePositions *positions)
696
+ {
697
+ bool intern = is_name || config->freeze;
698
+ bool symbolize = is_name && config->symbolize_names;
699
+ size_t bufferSize = stringEnd - string;
700
+ const char *p = string, *pe = string, *bufferStart;
701
+ char *buffer;
702
+
703
+ VALUE result = rb_str_buf_new(bufferSize);
704
+ rb_enc_associate_index(result, utf8_encindex);
705
+ buffer = RSTRING_PTR(result);
706
+ bufferStart = buffer;
707
+
708
+ #define APPEND_CHAR(chr) *buffer++ = chr; p = ++pe;
709
+
710
+ while (pe < stringEnd && (pe = json_next_backslash(pe, stringEnd, positions))) {
711
+ if (pe > p) {
712
+ MEMCPY(buffer, p, char, pe - p);
713
+ buffer += pe - p;
714
+ }
715
+ switch (*++pe) {
716
+ case '"':
717
+ case '/':
718
+ p = pe; // nothing to unescape just need to skip the backslash
719
+ break;
720
+ case '\\':
721
+ APPEND_CHAR('\\');
722
+ break;
723
+ case 'n':
724
+ APPEND_CHAR('\n');
725
+ break;
726
+ case 'r':
727
+ APPEND_CHAR('\r');
728
+ break;
729
+ case 't':
730
+ APPEND_CHAR('\t');
731
+ break;
732
+ case 'b':
733
+ APPEND_CHAR('\b');
734
+ break;
735
+ case 'f':
736
+ APPEND_CHAR('\f');
737
+ break;
738
+ case 'u': {
739
+ uint32_t ch = unescape_unicode(state, ++pe, stringEnd);
740
+ pe += 3;
741
+ /* To handle values above U+FFFF, we take a sequence of
742
+ * \uXXXX escapes in the U+D800..U+DBFF then
743
+ * U+DC00..U+DFFF ranges, take the low 10 bits from each
744
+ * to make a 20-bit number, then add 0x10000 to get the
745
+ * final codepoint.
746
+ *
747
+ * See Unicode 15: 3.8 "Surrogates", 5.3 "Handling
748
+ * Surrogate Pairs in UTF-16", and 23.6 "Surrogates
749
+ * Area".
750
+ */
751
+ if ((ch & 0xFC00) == 0xD800) {
752
+ pe++;
753
+ if (RB_LIKELY((pe <= stringEnd - 6) && memcmp(pe, "\\u", 2) == 0)) {
754
+ uint32_t sur = unescape_unicode(state, pe + 2, stringEnd);
755
+
756
+ if (RB_UNLIKELY((sur & 0xFC00) != 0xDC00)) {
757
+ raise_parse_error_at("invalid surrogate pair at %s", state, p);
758
+ }
759
+
760
+ ch = (((ch & 0x3F) << 10) | ((((ch >> 6) & 0xF) + 1) << 16) | (sur & 0x3FF));
761
+ pe += 5;
762
+ } else {
763
+ raise_parse_error_at("incomplete surrogate pair at %s", state, p);
764
+ break;
765
+ }
766
+ }
767
+
768
+ int unescape_len = convert_UTF32_to_UTF8(buffer, ch);
769
+ buffer += unescape_len;
770
+ p = ++pe;
771
+ break;
772
+ }
773
+ default:
774
+ if ((unsigned char)*pe < 0x20) {
775
+ if (!config->allow_control_characters) {
776
+ if (*pe == '\n') {
777
+ raise_parse_error_at("Invalid unescaped newline character (\\n) in string: %s", state, pe - 1);
778
+ }
779
+ raise_parse_error_at("invalid ASCII control character in string: %s", state, pe - 1);
780
+ }
781
+ }
782
+
783
+ if (config->allow_invalid_escape) {
784
+ APPEND_CHAR(*pe);
785
+ } else {
786
+ raise_parse_error_at("invalid escape character in string: %s", state, pe - 1);
787
+ }
788
+ break;
789
+ }
790
+ }
791
+ #undef APPEND_CHAR
792
+
793
+ if (stringEnd > p) {
794
+ MEMCPY(buffer, p, char, stringEnd - p);
795
+ buffer += stringEnd - p;
796
+ }
797
+ rb_str_set_len(result, buffer - bufferStart);
798
+
799
+ if (symbolize) {
800
+ result = rb_str_intern(result);
801
+ } else if (intern) {
802
+ result = rb_str_to_interned_str(result);
803
+ }
804
+
805
+ return result;
806
+ }
807
+
808
+ #define MAX_FAST_INTEGER_SIZE 18
809
+ #define MAX_NUMBER_STACK_BUFFER 128
810
+
811
+ typedef VALUE (*json_number_decode_func_t)(const char *ptr);
812
+
813
+ static inline VALUE json_decode_large_number(const char *start, long len, json_number_decode_func_t func)
814
+ {
815
+ if (RB_LIKELY(len < MAX_NUMBER_STACK_BUFFER)) {
816
+ char buffer[MAX_NUMBER_STACK_BUFFER];
817
+ MEMCPY(buffer, start, char, len);
818
+ buffer[len] = '\0';
819
+ return func(buffer);
820
+ } else {
821
+ VALUE buffer_v = rb_str_tmp_new(len);
822
+ char *buffer = RSTRING_PTR(buffer_v);
823
+ MEMCPY(buffer, start, char, len);
824
+ buffer[len] = '\0';
825
+ VALUE number = func(buffer);
826
+ RB_GC_GUARD(buffer_v);
827
+ return number;
828
+ }
829
+ }
830
+
831
+ static VALUE json_decode_inum(const char *buffer)
832
+ {
833
+ return rb_cstr2inum(buffer, 10);
834
+ }
835
+
836
+ NOINLINE(static) VALUE json_decode_large_integer(const char *start, long len)
837
+ {
838
+ return json_decode_large_number(start, len, json_decode_inum);
839
+ }
840
+
841
+ static inline VALUE json_decode_integer(uint64_t mantissa, int mantissa_digits, bool negative, const char *start, const char *end)
842
+ {
843
+ if (RB_LIKELY(mantissa_digits < MAX_FAST_INTEGER_SIZE)) {
844
+ if (negative) {
845
+ return INT64T2NUM(-((int64_t)mantissa));
846
+ }
847
+ return UINT64T2NUM(mantissa);
848
+ }
849
+
850
+ return json_decode_large_integer(start, end - start);
851
+ }
852
+
853
+ static VALUE json_decode_dnum(const char *buffer)
854
+ {
855
+ return DBL2NUM(rb_cstr_to_dbl(buffer, 1));
856
+ }
857
+
858
+ NOINLINE(static) VALUE json_decode_large_float(const char *start, long len)
859
+ {
860
+ return json_decode_large_number(start, len, json_decode_dnum);
861
+ }
862
+
863
+ /* Ruby JSON optimized float decoder using vendored Ryu algorithm
864
+ * Accepts pre-extracted mantissa and exponent from first-pass validation
865
+ */
866
+ static inline VALUE json_decode_float(JSON_ParserConfig *config, uint64_t mantissa, int mantissa_digits, int64_t exponent, bool negative,
867
+ const char *start, const char *end)
868
+ {
869
+ if (RB_UNLIKELY(config->decimal_class)) {
870
+ VALUE text = rb_str_new(start, end - start);
871
+ return rb_funcallv(config->decimal_class, config->decimal_method_id, 1, &text);
872
+ }
873
+
874
+ if (RB_UNLIKELY(exponent > INT32_MAX)) {
875
+ return negative ? CMinusInfinity : CInfinity;
876
+ }
877
+
878
+ if (RB_UNLIKELY(exponent < INT32_MIN)) {
879
+ return rb_float_new(negative ? -0.0 : 0.0);
880
+ }
881
+
882
+ // Fall back to rb_cstr_to_dbl for potential subnormals (rare edge case)
883
+ // Ryu has rounding issues with subnormals around 1e-310 (< 2.225e-308)
884
+ if (RB_UNLIKELY(mantissa_digits > 17 || mantissa_digits + exponent < -307)) {
885
+ return json_decode_large_float(start, end - start);
886
+ }
887
+
888
+ return DBL2NUM(ryu_s2d_from_parts(mantissa, mantissa_digits, (int32_t)exponent, negative));
889
+ }
890
+
891
+ static inline VALUE json_decode_array(JSON_ParserState *state, JSON_ParserConfig *config, long count)
892
+ {
893
+ VALUE array = rb_ary_new_from_values(count, rvalue_stack_peek(state->stack, count));
894
+ rvalue_stack_pop(state->stack, count);
895
+
896
+ if (config->freeze) {
897
+ RB_OBJ_FREEZE(array);
898
+ }
899
+
900
+ return array;
901
+ }
902
+
903
+ static VALUE json_find_duplicated_key(size_t count, const VALUE *pairs)
904
+ {
905
+ VALUE set = rb_hash_new_capa(count / 2);
906
+ for (size_t index = 0; index < count; index += 2) {
907
+ size_t before = RHASH_SIZE(set);
908
+ VALUE key = pairs[index];
909
+ rb_hash_aset(set, key, Qtrue);
910
+ if (RHASH_SIZE(set) == before) {
911
+ if (RB_SYMBOL_P(key)) {
912
+ return rb_sym2str(key);
913
+ }
914
+ return key;
915
+ }
916
+ }
917
+ return Qfalse;
918
+ }
919
+
920
+ NOINLINE(static) void emit_duplicate_key_warning(JSON_ParserState *state, VALUE duplicate_key)
921
+ {
922
+ VALUE message = rb_sprintf(
923
+ "detected duplicate key %"PRIsVALUE" in JSON object. This will raise an error in json 3.0 unless enabled via `allow_duplicate_key: true`",
924
+ rb_inspect(duplicate_key)
925
+ );
926
+
927
+ emit_parse_warning(RSTRING_PTR(message), state);
928
+ RB_GC_GUARD(message);
929
+ }
14
930
 
15
- #define EVIL 0x666
931
+ NORETURN(static) void raise_duplicate_key_error(JSON_ParserState *state, VALUE duplicate_key)
932
+ {
933
+ VALUE message = rb_sprintf(
934
+ "duplicate key %"PRIsVALUE,
935
+ rb_inspect(duplicate_key)
936
+ );
937
+
938
+ long line, column;
939
+ cursor_position(state, &line, &column);
940
+ rb_str_concat(message, build_parse_error_message("", state, line, column)) ;
941
+ rb_exc_raise(parse_error_new(message, line, column));
942
+ }
16
943
 
17
- #ifndef RHASH_TBL
18
- #define RHASH_TBL(hsh) (RHASH(hsh)->tbl)
19
- #endif
944
+ static inline VALUE json_decode_object(JSON_ParserState *state, JSON_ParserConfig *config, size_t count)
945
+ {
946
+ size_t entries_count = count / 2;
947
+ VALUE object = rb_hash_new_capa(entries_count);
948
+ const VALUE *pairs = rvalue_stack_peek(state->stack, count);
949
+ rb_hash_bulk_insert(count, pairs, object);
950
+
951
+ if (RB_UNLIKELY(RHASH_SIZE(object) < entries_count)) {
952
+ switch (config->on_duplicate_key) {
953
+ case JSON_IGNORE:
954
+ break;
955
+ case JSON_DEPRECATED:
956
+ // Only emit the first few deprecations to avoid spamming.
957
+ if (state->emitted_deprecations < 5) {
958
+ emit_duplicate_key_warning(state, json_find_duplicated_key(count, pairs));
959
+ state->emitted_deprecations++;
960
+ }
20
961
 
21
- #ifdef HAVE_RUBY_ENCODING_H
22
- #include "ruby/encoding.h"
23
- #define FORCE_UTF8(obj) rb_enc_associate((obj), rb_utf8_encoding())
24
- static VALUE mEncoding_ASCII_8BIT, mEncoding_UTF_8, mEncoding_UTF_16BE,
25
- mEncoding_UTF_16LE, mEncoding_UTF_32BE, mEncoding_UTF_32LE;
26
- static ID i_encoding, i_encode, i_encode_bang, i_force_encoding;
27
- #else
28
- #define FORCE_UTF8(obj)
29
- static ID i_iconv;
30
- #endif
962
+ break;
963
+ case JSON_RAISE:
964
+ raise_duplicate_key_error(state, json_find_duplicated_key(count, pairs));
965
+ break;
966
+ }
967
+ }
31
968
 
32
- static VALUE mJSON, mExt, cParser, eParserError, eNestingError;
33
- static VALUE CNaN, CInfinity, CMinusInfinity;
969
+ rvalue_stack_pop(state->stack, count);
34
970
 
35
- static ID i_json_creatable_p, i_json_create, i_create_id, i_create_additions,
36
- i_chr, i_max_nesting, i_allow_nan, i_object_class, i_array_class;
971
+ if (config->freeze) {
972
+ RB_OBJ_FREEZE(object);
973
+ }
37
974
 
38
- #define MinusInfinity "-Infinity"
975
+ return object;
976
+ }
39
977
 
40
- typedef struct JSON_ParserStruct {
41
- VALUE Vsource;
42
- char *source;
43
- long len;
44
- char *memo;
45
- VALUE create_id;
46
- int max_nesting;
47
- int current_nesting;
48
- int allow_nan;
49
- VALUE object_class;
50
- VALUE array_class;
51
- } JSON_Parser;
52
-
53
- static char *JSON_parse_object(JSON_Parser *json, char *p, char *pe, VALUE *result);
54
- static char *JSON_parse_array(JSON_Parser *json, char *p, char *pe, VALUE *result);
55
- static char *JSON_parse_value(JSON_Parser *json, char *p, char *pe, VALUE *result);
56
- static char *JSON_parse_string(JSON_Parser *json, char *p, char *pe, VALUE *result);
57
- static char *JSON_parse_integer(JSON_Parser *json, char *p, char *pe, VALUE *result);
58
- static char *JSON_parse_float(JSON_Parser *json, char *p, char *pe, VALUE *result);
59
-
60
- #define GET_STRUCT \
61
- JSON_Parser *json; \
62
- Data_Get_Struct(self, JSON_Parser, json);
63
-
64
-
65
- #line 88 "parser.rl"
66
-
67
-
68
-
69
- #line 70 "parser.c"
70
- static const int JSON_object_start = 1;
71
- static const int JSON_object_first_final = 27;
72
- static const int JSON_object_error = 0;
73
-
74
- static const int JSON_object_en_main = 1;
75
-
76
-
77
- #line 121 "parser.rl"
78
-
79
-
80
- static char *JSON_parse_object(JSON_Parser *json, char *p, char *pe, VALUE *result)
81
- {
82
- int cs = EVIL;
83
- VALUE last_name = Qnil;
84
- VALUE object_class = json->object_class;
85
-
86
- if (json->max_nesting && json->current_nesting > json->max_nesting) {
87
- rb_raise(eNestingError, "nesting of %d is too deep", json->current_nesting);
88
- }
89
-
90
- *result = NIL_P(object_class) ? rb_hash_new() : rb_class_new_instance(0, 0, object_class);
91
-
92
-
93
- #line 94 "parser.c"
94
- {
95
- cs = JSON_object_start;
96
- }
97
-
98
- #line 136 "parser.rl"
99
-
100
- #line 101 "parser.c"
101
- {
102
- if ( p == pe )
103
- goto _test_eof;
104
- switch ( cs )
105
- {
106
- case 1:
107
- if ( (*p) == 123 )
108
- goto st2;
109
- goto st0;
110
- st0:
111
- cs = 0;
112
- goto _out;
113
- st2:
114
- if ( ++p == pe )
115
- goto _test_eof2;
116
- case 2:
117
- switch( (*p) ) {
118
- case 13: goto st2;
119
- case 32: goto st2;
120
- case 34: goto tr2;
121
- case 47: goto st23;
122
- case 125: goto tr4;
123
- }
124
- if ( 9 <= (*p) && (*p) <= 10 )
125
- goto st2;
126
- goto st0;
127
- tr2:
128
- #line 107 "parser.rl"
129
- {
130
- char *np = JSON_parse_string(json, p, pe, &last_name);
131
- if (np == NULL) { p--; {p++; cs = 3; goto _out;} } else {p = (( np))-1;}
132
- }
133
- goto st3;
134
- st3:
135
- if ( ++p == pe )
136
- goto _test_eof3;
137
- case 3:
138
- #line 139 "parser.c"
139
- switch( (*p) ) {
140
- case 13: goto st3;
141
- case 32: goto st3;
142
- case 47: goto st4;
143
- case 58: goto st8;
144
- }
145
- if ( 9 <= (*p) && (*p) <= 10 )
146
- goto st3;
147
- goto st0;
148
- st4:
149
- if ( ++p == pe )
150
- goto _test_eof4;
151
- case 4:
152
- switch( (*p) ) {
153
- case 42: goto st5;
154
- case 47: goto st7;
155
- }
156
- goto st0;
157
- st5:
158
- if ( ++p == pe )
159
- goto _test_eof5;
160
- case 5:
161
- if ( (*p) == 42 )
162
- goto st6;
163
- goto st5;
164
- st6:
165
- if ( ++p == pe )
166
- goto _test_eof6;
167
- case 6:
168
- switch( (*p) ) {
169
- case 42: goto st6;
170
- case 47: goto st3;
171
- }
172
- goto st5;
173
- st7:
174
- if ( ++p == pe )
175
- goto _test_eof7;
176
- case 7:
177
- if ( (*p) == 10 )
178
- goto st3;
179
- goto st7;
180
- st8:
181
- if ( ++p == pe )
182
- goto _test_eof8;
183
- case 8:
184
- switch( (*p) ) {
185
- case 13: goto st8;
186
- case 32: goto st8;
187
- case 34: goto tr11;
188
- case 45: goto tr11;
189
- case 47: goto st19;
190
- case 73: goto tr11;
191
- case 78: goto tr11;
192
- case 91: goto tr11;
193
- case 102: goto tr11;
194
- case 110: goto tr11;
195
- case 116: goto tr11;
196
- case 123: goto tr11;
197
- }
198
- if ( (*p) > 10 ) {
199
- if ( 48 <= (*p) && (*p) <= 57 )
200
- goto tr11;
201
- } else if ( (*p) >= 9 )
202
- goto st8;
203
- goto st0;
204
- tr11:
205
- #line 96 "parser.rl"
206
- {
207
- VALUE v = Qnil;
208
- char *np = JSON_parse_value(json, p, pe, &v);
209
- if (np == NULL) {
210
- p--; {p++; cs = 9; goto _out;}
211
- } else {
212
- rb_hash_aset(*result, last_name, v);
213
- {p = (( np))-1;}
978
+ static inline VALUE json_push_value(JSON_ParserState *state, JSON_ParserConfig *config, VALUE value)
979
+ {
980
+ if (RB_UNLIKELY(config->on_load_proc)) {
981
+ value = rb_proc_call_with_block(config->on_load_proc, 1, &value, Qnil);
982
+ }
983
+ rvalue_stack_push(state->stack, value, state->stack_handle, &state->stack);
984
+ return value;
985
+ }
986
+
987
+ static const bool string_scan_table[256] = {
988
+ // ASCII Control Characters
989
+ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
990
+ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
991
+ // ASCII Characters
992
+ 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // '"'
993
+ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
994
+ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
995
+ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, // '\\'
996
+ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
997
+ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
998
+ };
999
+
1000
+ #ifdef HAVE_SIMD
1001
+ static SIMD_Implementation simd_impl = SIMD_NONE;
1002
+ #endif /* HAVE_SIMD */
1003
+
1004
+ ALWAYS_INLINE(static) bool string_scan(JSON_ParserState *state)
1005
+ {
1006
+ #ifdef HAVE_SIMD
1007
+ #if defined(HAVE_SIMD_NEON)
1008
+
1009
+ uint64_t mask = 0;
1010
+ if (string_scan_simd_neon(&state->cursor, state->end, &mask)) {
1011
+ state->cursor += trailing_zeros64(mask) >> 2;
1012
+ return true;
1013
+ }
1014
+
1015
+ #elif defined(HAVE_SIMD_SSE2)
1016
+ if (simd_impl == SIMD_SSE2) {
1017
+ int mask = 0;
1018
+ if (string_scan_simd_sse2(&state->cursor, state->end, &mask)) {
1019
+ state->cursor += trailing_zeros(mask);
1020
+ return true;
1021
+ }
1022
+ }
1023
+ #endif /* HAVE_SIMD_NEON or HAVE_SIMD_SSE2 */
1024
+ #endif /* HAVE_SIMD */
1025
+
1026
+ while (!eos(state)) {
1027
+ if (RB_UNLIKELY(string_scan_table[(unsigned char)*state->cursor])) {
1028
+ return true;
214
1029
  }
1030
+ state->cursor++;
1031
+ }
1032
+
1033
+ // If the string ended with an unterminated escape sequence, we might
1034
+ // have gone past the end.
1035
+ if (RB_UNLIKELY(state->cursor > state->end)) {
1036
+ state->cursor = state->end;
215
1037
  }
216
- goto st9;
217
- st9:
218
- if ( ++p == pe )
219
- goto _test_eof9;
220
- case 9:
221
- #line 222 "parser.c"
222
- switch( (*p) ) {
223
- case 13: goto st9;
224
- case 32: goto st9;
225
- case 44: goto st10;
226
- case 47: goto st15;
227
- case 125: goto tr4;
228
- }
229
- if ( 9 <= (*p) && (*p) <= 10 )
230
- goto st9;
231
- goto st0;
232
- st10:
233
- if ( ++p == pe )
234
- goto _test_eof10;
235
- case 10:
236
- switch( (*p) ) {
237
- case 13: goto st10;
238
- case 32: goto st10;
239
- case 34: goto tr2;
240
- case 47: goto st11;
241
- }
242
- if ( 9 <= (*p) && (*p) <= 10 )
243
- goto st10;
244
- goto st0;
245
- st11:
246
- if ( ++p == pe )
247
- goto _test_eof11;
248
- case 11:
249
- switch( (*p) ) {
250
- case 42: goto st12;
251
- case 47: goto st14;
252
- }
253
- goto st0;
254
- st12:
255
- if ( ++p == pe )
256
- goto _test_eof12;
257
- case 12:
258
- if ( (*p) == 42 )
259
- goto st13;
260
- goto st12;
261
- st13:
262
- if ( ++p == pe )
263
- goto _test_eof13;
264
- case 13:
265
- switch( (*p) ) {
266
- case 42: goto st13;
267
- case 47: goto st10;
268
- }
269
- goto st12;
270
- st14:
271
- if ( ++p == pe )
272
- goto _test_eof14;
273
- case 14:
274
- if ( (*p) == 10 )
275
- goto st10;
276
- goto st14;
277
- st15:
278
- if ( ++p == pe )
279
- goto _test_eof15;
280
- case 15:
281
- switch( (*p) ) {
282
- case 42: goto st16;
283
- case 47: goto st18;
284
- }
285
- goto st0;
286
- st16:
287
- if ( ++p == pe )
288
- goto _test_eof16;
289
- case 16:
290
- if ( (*p) == 42 )
291
- goto st17;
292
- goto st16;
293
- st17:
294
- if ( ++p == pe )
295
- goto _test_eof17;
296
- case 17:
297
- switch( (*p) ) {
298
- case 42: goto st17;
299
- case 47: goto st9;
300
- }
301
- goto st16;
302
- st18:
303
- if ( ++p == pe )
304
- goto _test_eof18;
305
- case 18:
306
- if ( (*p) == 10 )
307
- goto st9;
308
- goto st18;
309
- tr4:
310
- #line 112 "parser.rl"
311
- { p--; {p++; cs = 27; goto _out;} }
312
- goto st27;
313
- st27:
314
- if ( ++p == pe )
315
- goto _test_eof27;
316
- case 27:
317
- #line 318 "parser.c"
318
- goto st0;
319
- st19:
320
- if ( ++p == pe )
321
- goto _test_eof19;
322
- case 19:
323
- switch( (*p) ) {
324
- case 42: goto st20;
325
- case 47: goto st22;
326
- }
327
- goto st0;
328
- st20:
329
- if ( ++p == pe )
330
- goto _test_eof20;
331
- case 20:
332
- if ( (*p) == 42 )
333
- goto st21;
334
- goto st20;
335
- st21:
336
- if ( ++p == pe )
337
- goto _test_eof21;
338
- case 21:
339
- switch( (*p) ) {
340
- case 42: goto st21;
341
- case 47: goto st8;
342
- }
343
- goto st20;
344
- st22:
345
- if ( ++p == pe )
346
- goto _test_eof22;
347
- case 22:
348
- if ( (*p) == 10 )
349
- goto st8;
350
- goto st22;
351
- st23:
352
- if ( ++p == pe )
353
- goto _test_eof23;
354
- case 23:
355
- switch( (*p) ) {
356
- case 42: goto st24;
357
- case 47: goto st26;
358
- }
359
- goto st0;
360
- st24:
361
- if ( ++p == pe )
362
- goto _test_eof24;
363
- case 24:
364
- if ( (*p) == 42 )
365
- goto st25;
366
- goto st24;
367
- st25:
368
- if ( ++p == pe )
369
- goto _test_eof25;
370
- case 25:
371
- switch( (*p) ) {
372
- case 42: goto st25;
373
- case 47: goto st2;
374
- }
375
- goto st24;
376
- st26:
377
- if ( ++p == pe )
378
- goto _test_eof26;
379
- case 26:
380
- if ( (*p) == 10 )
381
- goto st2;
382
- goto st26;
383
- }
384
- _test_eof2: cs = 2; goto _test_eof;
385
- _test_eof3: cs = 3; goto _test_eof;
386
- _test_eof4: cs = 4; goto _test_eof;
387
- _test_eof5: cs = 5; goto _test_eof;
388
- _test_eof6: cs = 6; goto _test_eof;
389
- _test_eof7: cs = 7; goto _test_eof;
390
- _test_eof8: cs = 8; goto _test_eof;
391
- _test_eof9: cs = 9; goto _test_eof;
392
- _test_eof10: cs = 10; goto _test_eof;
393
- _test_eof11: cs = 11; goto _test_eof;
394
- _test_eof12: cs = 12; goto _test_eof;
395
- _test_eof13: cs = 13; goto _test_eof;
396
- _test_eof14: cs = 14; goto _test_eof;
397
- _test_eof15: cs = 15; goto _test_eof;
398
- _test_eof16: cs = 16; goto _test_eof;
399
- _test_eof17: cs = 17; goto _test_eof;
400
- _test_eof18: cs = 18; goto _test_eof;
401
- _test_eof27: cs = 27; goto _test_eof;
402
- _test_eof19: cs = 19; goto _test_eof;
403
- _test_eof20: cs = 20; goto _test_eof;
404
- _test_eof21: cs = 21; goto _test_eof;
405
- _test_eof22: cs = 22; goto _test_eof;
406
- _test_eof23: cs = 23; goto _test_eof;
407
- _test_eof24: cs = 24; goto _test_eof;
408
- _test_eof25: cs = 25; goto _test_eof;
409
- _test_eof26: cs = 26; goto _test_eof;
410
-
411
- _test_eof: {}
412
- _out: {}
413
- }
414
-
415
- #line 137 "parser.rl"
416
-
417
- if (cs >= JSON_object_first_final) {
418
- if (RTEST(json->create_id)) {
419
- VALUE klassname = rb_hash_aref(*result, json->create_id);
420
- if (!NIL_P(klassname)) {
421
- VALUE klass = rb_path2class(StringValueCStr(klassname));
422
- if RTEST(rb_funcall(klass, i_json_creatable_p, 0)) {
423
- *result = rb_funcall(klass, i_json_create, 1, *result);
1038
+
1039
+ return false;
1040
+ }
1041
+
1042
+ static VALUE json_parse_escaped_string(JSON_ParserState *state, JSON_ParserConfig *config, bool is_name, const char *start)
1043
+ {
1044
+ const char *backslashes[JSON_MAX_UNESCAPE_POSITIONS];
1045
+ JSON_UnescapePositions positions = {
1046
+ .size = 0,
1047
+ .positions = backslashes,
1048
+ .additional_backslashes = 0,
1049
+ };
1050
+
1051
+ do {
1052
+ switch (*state->cursor) {
1053
+ case '"': {
1054
+ VALUE string = json_string_unescape(state, config, start, state->cursor, is_name, &positions);
1055
+ state->cursor++;
1056
+ return json_push_value(state, config, string);
1057
+ }
1058
+ case '\\': {
1059
+ if (RB_LIKELY(positions.size < JSON_MAX_UNESCAPE_POSITIONS)) {
1060
+ backslashes[positions.size] = state->cursor;
1061
+ positions.size++;
1062
+ } else {
1063
+ positions.additional_backslashes++;
424
1064
  }
1065
+ state->cursor++;
1066
+ break;
425
1067
  }
1068
+ default:
1069
+ if (!config->allow_control_characters) {
1070
+ raise_parse_error("invalid ASCII control character in string: %s", state);
1071
+ }
1072
+ break;
426
1073
  }
427
- return p + 1;
428
- } else {
429
- return NULL;
430
- }
431
- }
432
-
433
-
434
- #line 435 "parser.c"
435
- static const int JSON_value_start = 1;
436
- static const int JSON_value_first_final = 21;
437
- static const int JSON_value_error = 0;
438
-
439
- static const int JSON_value_en_main = 1;
440
-
441
-
442
- #line 235 "parser.rl"
443
-
444
-
445
- static char *JSON_parse_value(JSON_Parser *json, char *p, char *pe, VALUE *result)
446
- {
447
- int cs = EVIL;
448
-
449
-
450
- #line 451 "parser.c"
451
- {
452
- cs = JSON_value_start;
453
- }
454
-
455
- #line 242 "parser.rl"
456
-
457
- #line 458 "parser.c"
458
- {
459
- if ( p == pe )
460
- goto _test_eof;
461
- switch ( cs )
462
- {
463
- case 1:
464
- switch( (*p) ) {
465
- case 34: goto tr0;
466
- case 45: goto tr2;
467
- case 73: goto st2;
468
- case 78: goto st9;
469
- case 91: goto tr5;
470
- case 102: goto st11;
471
- case 110: goto st15;
472
- case 116: goto st18;
473
- case 123: goto tr9;
474
- }
475
- if ( 48 <= (*p) && (*p) <= 57 )
476
- goto tr2;
477
- goto st0;
478
- st0:
479
- cs = 0;
480
- goto _out;
481
- tr0:
482
- #line 183 "parser.rl"
483
- {
484
- char *np = JSON_parse_string(json, p, pe, result);
485
- if (np == NULL) { p--; {p++; cs = 21; goto _out;} } else {p = (( np))-1;}
486
- }
487
- goto st21;
488
- tr2:
489
- #line 188 "parser.rl"
490
- {
491
- char *np;
492
- if(pe > p + 9 && !strncmp(MinusInfinity, p, 9)) {
493
- if (json->allow_nan) {
494
- *result = CMinusInfinity;
495
- {p = (( p + 10))-1;}
496
- p--; {p++; cs = 21; goto _out;}
497
- } else {
498
- rb_raise(eParserError, "%u: unexpected token at '%s'", __LINE__, p);
499
- }
1074
+
1075
+ state->cursor++;
1076
+ } while (string_scan(state));
1077
+
1078
+ raise_parse_error("unexpected end of input, expected closing \"", state);
1079
+ return Qfalse;
1080
+ }
1081
+
1082
+ ALWAYS_INLINE(static) VALUE json_parse_string(JSON_ParserState *state, JSON_ParserConfig *config, bool is_name)
1083
+ {
1084
+ state->cursor++;
1085
+ const char *start = state->cursor;
1086
+
1087
+ if (RB_UNLIKELY(!string_scan(state))) {
1088
+ raise_parse_error("unexpected end of input, expected closing \"", state);
1089
+ }
1090
+
1091
+ if (RB_LIKELY(*state->cursor == '"')) {
1092
+ VALUE string = json_string_fastpath(state, config, start, state->cursor, is_name);
1093
+ state->cursor++;
1094
+ return json_push_value(state, config, string);
1095
+ }
1096
+ return json_parse_escaped_string(state, config, is_name, start);
1097
+ }
1098
+
1099
+ #if JSON_CPU_LITTLE_ENDIAN_64BITS
1100
+ // From: https://lemire.me/blog/2022/01/21/swar-explained-parsing-eight-digits/
1101
+ // Additional References:
1102
+ // https://johnnylee-sde.github.io/Fast-numeric-string-to-int/
1103
+ // http://0x80.pl/notesen/2014-10-12-parsing-decimal-numbers-part-1-swar.html
1104
+ static inline uint64_t decode_8digits_unrolled(uint64_t val) {
1105
+ const uint64_t mask = 0x000000FF000000FF;
1106
+ const uint64_t mul1 = 0x000F424000000064; // 100 + (1000000ULL << 32)
1107
+ const uint64_t mul2 = 0x0000271000000001; // 1 + (10000ULL << 32)
1108
+ val -= 0x3030303030303030;
1109
+ val = (val * 10) + (val >> 8); // val = (val * 2561) >> 8;
1110
+ val = (((val & mask) * mul1) + (((val >> 16) & mask) * mul2)) >> 32;
1111
+ return val;
1112
+ }
1113
+
1114
+ static inline uint64_t decode_4digits_unrolled(uint32_t val) {
1115
+ const uint32_t mask = 0x000000FF;
1116
+ const uint32_t mul1 = 100;
1117
+ val -= 0x30303030;
1118
+ val = (val * 10) + (val >> 8); // val = (val * 2561) >> 8;
1119
+ val = ((val & mask) * mul1) + (((val >> 16) & mask));
1120
+ return val;
1121
+ }
1122
+ #endif
1123
+
1124
+ static inline int json_parse_digits(JSON_ParserState *state, uint64_t *accumulator)
1125
+ {
1126
+ const char *start = state->cursor;
1127
+
1128
+ #if JSON_CPU_LITTLE_ENDIAN_64BITS
1129
+ while (rest(state) >= sizeof(uint64_t)) {
1130
+ uint64_t next_8bytes;
1131
+ memcpy(&next_8bytes, state->cursor, sizeof(uint64_t));
1132
+
1133
+ // From: https://github.com/simdjson/simdjson/blob/32b301893c13d058095a07d9868edaaa42ee07aa/include/simdjson/generic/numberparsing.h#L333
1134
+ // Branchless version of: http://0x80.pl/articles/swar-digits-validate.html
1135
+ uint64_t match = (next_8bytes & 0xF0F0F0F0F0F0F0F0) | (((next_8bytes + 0x0606060606060606) & 0xF0F0F0F0F0F0F0F0) >> 4);
1136
+
1137
+ if (match == 0x3333333333333333) { // 8 consecutive digits
1138
+ *accumulator = (*accumulator * 100000000) + decode_8digits_unrolled(next_8bytes);
1139
+ state->cursor += 8;
1140
+ continue;
500
1141
  }
501
- np = JSON_parse_float(json, p, pe, result);
502
- if (np != NULL) {p = (( np))-1;}
503
- np = JSON_parse_integer(json, p, pe, result);
504
- if (np != NULL) {p = (( np))-1;}
505
- p--; {p++; cs = 21; goto _out;}
506
- }
507
- goto st21;
508
- tr5:
509
- #line 206 "parser.rl"
510
- {
511
- char *np;
512
- json->current_nesting++;
513
- np = JSON_parse_array(json, p, pe, result);
514
- json->current_nesting--;
515
- if (np == NULL) { p--; {p++; cs = 21; goto _out;} } else {p = (( np))-1;}
516
- }
517
- goto st21;
518
- tr9:
519
- #line 214 "parser.rl"
520
- {
521
- char *np;
522
- json->current_nesting++;
523
- np = JSON_parse_object(json, p, pe, result);
524
- json->current_nesting--;
525
- if (np == NULL) { p--; {p++; cs = 21; goto _out;} } else {p = (( np))-1;}
526
- }
527
- goto st21;
528
- tr16:
529
- #line 176 "parser.rl"
530
- {
531
- if (json->allow_nan) {
532
- *result = CInfinity;
533
- } else {
534
- rb_raise(eParserError, "%u: unexpected token at '%s'", __LINE__, p - 8);
1142
+
1143
+ uint32_t consecutive_digits = trailing_zeros64(match ^ 0x3333333333333333) / CHAR_BIT;
1144
+
1145
+ if (consecutive_digits >= 4) {
1146
+ *accumulator = (*accumulator * 10000) + decode_4digits_unrolled((uint32_t)next_8bytes);
1147
+ state->cursor += 4;
1148
+ consecutive_digits -= 4;
535
1149
  }
1150
+
1151
+ while (consecutive_digits) {
1152
+ *accumulator = *accumulator * 10 + (*state->cursor - '0');
1153
+ consecutive_digits--;
1154
+ state->cursor++;
1155
+ }
1156
+
1157
+ return (int)(state->cursor - start);
536
1158
  }
537
- goto st21;
538
- tr18:
539
- #line 169 "parser.rl"
540
- {
541
- if (json->allow_nan) {
542
- *result = CNaN;
543
- } else {
544
- rb_raise(eParserError, "%u: unexpected token at '%s'", __LINE__, p - 2);
1159
+ #endif
1160
+
1161
+ char next_char;
1162
+ while (rb_isdigit(next_char = peek(state))) {
1163
+ *accumulator = *accumulator * 10 + (next_char - '0');
1164
+ state->cursor++;
1165
+ }
1166
+ return (int)(state->cursor - start);
1167
+ }
1168
+
1169
+ static inline VALUE json_parse_number(JSON_ParserState *state, JSON_ParserConfig *config, bool negative, const char *start)
1170
+ {
1171
+ bool integer = true;
1172
+ const char first_digit = *state->cursor;
1173
+
1174
+ // Variables for Ryu optimization - extract digits during parsing
1175
+ int64_t exponent = 0;
1176
+ int decimal_point_pos = -1;
1177
+ uint64_t mantissa = 0;
1178
+
1179
+ // Parse integer part and extract mantissa digits
1180
+ int mantissa_digits = json_parse_digits(state, &mantissa);
1181
+
1182
+ if (RB_UNLIKELY((first_digit == '0' && mantissa_digits > 1) || (negative && mantissa_digits == 0))) {
1183
+ raise_parse_error_at("invalid number: %s", state, start);
1184
+ }
1185
+
1186
+ // Parse fractional part
1187
+ if (peek(state) == '.') {
1188
+ integer = false;
1189
+ decimal_point_pos = mantissa_digits; // Remember position of decimal point
1190
+ state->cursor++;
1191
+
1192
+ int fractional_digits = json_parse_digits(state, &mantissa);
1193
+ mantissa_digits += fractional_digits;
1194
+
1195
+ if (RB_UNLIKELY(!fractional_digits)) {
1196
+ raise_parse_error_at("invalid number: %s", state, start);
545
1197
  }
546
1198
  }
547
- goto st21;
548
- tr22:
549
- #line 163 "parser.rl"
550
- {
551
- *result = Qfalse;
552
- }
553
- goto st21;
554
- tr25:
555
- #line 160 "parser.rl"
556
- {
557
- *result = Qnil;
558
- }
559
- goto st21;
560
- tr28:
561
- #line 166 "parser.rl"
562
- {
563
- *result = Qtrue;
564
- }
565
- goto st21;
566
- st21:
567
- if ( ++p == pe )
568
- goto _test_eof21;
569
- case 21:
570
- #line 222 "parser.rl"
571
- { p--; {p++; cs = 21; goto _out;} }
572
- #line 573 "parser.c"
573
- goto st0;
574
- st2:
575
- if ( ++p == pe )
576
- goto _test_eof2;
577
- case 2:
578
- if ( (*p) == 110 )
579
- goto st3;
580
- goto st0;
581
- st3:
582
- if ( ++p == pe )
583
- goto _test_eof3;
584
- case 3:
585
- if ( (*p) == 102 )
586
- goto st4;
587
- goto st0;
588
- st4:
589
- if ( ++p == pe )
590
- goto _test_eof4;
591
- case 4:
592
- if ( (*p) == 105 )
593
- goto st5;
594
- goto st0;
595
- st5:
596
- if ( ++p == pe )
597
- goto _test_eof5;
598
- case 5:
599
- if ( (*p) == 110 )
600
- goto st6;
601
- goto st0;
602
- st6:
603
- if ( ++p == pe )
604
- goto _test_eof6;
605
- case 6:
606
- if ( (*p) == 105 )
607
- goto st7;
608
- goto st0;
609
- st7:
610
- if ( ++p == pe )
611
- goto _test_eof7;
612
- case 7:
613
- if ( (*p) == 116 )
614
- goto st8;
615
- goto st0;
616
- st8:
617
- if ( ++p == pe )
618
- goto _test_eof8;
619
- case 8:
620
- if ( (*p) == 121 )
621
- goto tr16;
622
- goto st0;
623
- st9:
624
- if ( ++p == pe )
625
- goto _test_eof9;
626
- case 9:
627
- if ( (*p) == 97 )
628
- goto st10;
629
- goto st0;
630
- st10:
631
- if ( ++p == pe )
632
- goto _test_eof10;
633
- case 10:
634
- if ( (*p) == 78 )
635
- goto tr18;
636
- goto st0;
637
- st11:
638
- if ( ++p == pe )
639
- goto _test_eof11;
640
- case 11:
641
- if ( (*p) == 97 )
642
- goto st12;
643
- goto st0;
644
- st12:
645
- if ( ++p == pe )
646
- goto _test_eof12;
647
- case 12:
648
- if ( (*p) == 108 )
649
- goto st13;
650
- goto st0;
651
- st13:
652
- if ( ++p == pe )
653
- goto _test_eof13;
654
- case 13:
655
- if ( (*p) == 115 )
656
- goto st14;
657
- goto st0;
658
- st14:
659
- if ( ++p == pe )
660
- goto _test_eof14;
661
- case 14:
662
- if ( (*p) == 101 )
663
- goto tr22;
664
- goto st0;
665
- st15:
666
- if ( ++p == pe )
667
- goto _test_eof15;
668
- case 15:
669
- if ( (*p) == 117 )
670
- goto st16;
671
- goto st0;
672
- st16:
673
- if ( ++p == pe )
674
- goto _test_eof16;
675
- case 16:
676
- if ( (*p) == 108 )
677
- goto st17;
678
- goto st0;
679
- st17:
680
- if ( ++p == pe )
681
- goto _test_eof17;
682
- case 17:
683
- if ( (*p) == 108 )
684
- goto tr25;
685
- goto st0;
686
- st18:
687
- if ( ++p == pe )
688
- goto _test_eof18;
689
- case 18:
690
- if ( (*p) == 114 )
691
- goto st19;
692
- goto st0;
693
- st19:
694
- if ( ++p == pe )
695
- goto _test_eof19;
696
- case 19:
697
- if ( (*p) == 117 )
698
- goto st20;
699
- goto st0;
700
- st20:
701
- if ( ++p == pe )
702
- goto _test_eof20;
703
- case 20:
704
- if ( (*p) == 101 )
705
- goto tr28;
706
- goto st0;
707
- }
708
- _test_eof21: cs = 21; goto _test_eof;
709
- _test_eof2: cs = 2; goto _test_eof;
710
- _test_eof3: cs = 3; goto _test_eof;
711
- _test_eof4: cs = 4; goto _test_eof;
712
- _test_eof5: cs = 5; goto _test_eof;
713
- _test_eof6: cs = 6; goto _test_eof;
714
- _test_eof7: cs = 7; goto _test_eof;
715
- _test_eof8: cs = 8; goto _test_eof;
716
- _test_eof9: cs = 9; goto _test_eof;
717
- _test_eof10: cs = 10; goto _test_eof;
718
- _test_eof11: cs = 11; goto _test_eof;
719
- _test_eof12: cs = 12; goto _test_eof;
720
- _test_eof13: cs = 13; goto _test_eof;
721
- _test_eof14: cs = 14; goto _test_eof;
722
- _test_eof15: cs = 15; goto _test_eof;
723
- _test_eof16: cs = 16; goto _test_eof;
724
- _test_eof17: cs = 17; goto _test_eof;
725
- _test_eof18: cs = 18; goto _test_eof;
726
- _test_eof19: cs = 19; goto _test_eof;
727
- _test_eof20: cs = 20; goto _test_eof;
728
-
729
- _test_eof: {}
730
- _out: {}
731
- }
732
-
733
- #line 243 "parser.rl"
734
-
735
- if (cs >= JSON_value_first_final) {
736
- return p;
737
- } else {
738
- return NULL;
739
- }
740
- }
741
-
742
-
743
- #line 744 "parser.c"
744
- static const int JSON_integer_start = 1;
745
- static const int JSON_integer_first_final = 5;
746
- static const int JSON_integer_error = 0;
747
-
748
- static const int JSON_integer_en_main = 1;
749
-
750
-
751
- #line 259 "parser.rl"
752
-
753
-
754
- static char *JSON_parse_integer(JSON_Parser *json, char *p, char *pe, VALUE *result)
755
- {
756
- int cs = EVIL;
757
-
758
-
759
- #line 760 "parser.c"
760
- {
761
- cs = JSON_integer_start;
762
- }
763
-
764
- #line 266 "parser.rl"
765
- json->memo = p;
766
-
767
- #line 768 "parser.c"
768
- {
769
- if ( p == pe )
770
- goto _test_eof;
771
- switch ( cs )
772
- {
773
- case 1:
774
- switch( (*p) ) {
775
- case 45: goto st2;
776
- case 48: goto st3;
777
- }
778
- if ( 49 <= (*p) && (*p) <= 57 )
779
- goto st4;
780
- goto st0;
781
- st0:
782
- cs = 0;
783
- goto _out;
784
- st2:
785
- if ( ++p == pe )
786
- goto _test_eof2;
787
- case 2:
788
- if ( (*p) == 48 )
789
- goto st3;
790
- if ( 49 <= (*p) && (*p) <= 57 )
791
- goto st4;
792
- goto st0;
793
- st3:
794
- if ( ++p == pe )
795
- goto _test_eof3;
796
- case 3:
797
- if ( 48 <= (*p) && (*p) <= 57 )
798
- goto st0;
799
- goto tr4;
800
- tr4:
801
- #line 256 "parser.rl"
802
- { p--; {p++; cs = 5; goto _out;} }
803
- goto st5;
804
- st5:
805
- if ( ++p == pe )
806
- goto _test_eof5;
807
- case 5:
808
- #line 809 "parser.c"
809
- goto st0;
810
- st4:
811
- if ( ++p == pe )
812
- goto _test_eof4;
813
- case 4:
814
- if ( 48 <= (*p) && (*p) <= 57 )
815
- goto st4;
816
- goto tr4;
817
- }
818
- _test_eof2: cs = 2; goto _test_eof;
819
- _test_eof3: cs = 3; goto _test_eof;
820
- _test_eof5: cs = 5; goto _test_eof;
821
- _test_eof4: cs = 4; goto _test_eof;
822
-
823
- _test_eof: {}
824
- _out: {}
825
- }
826
-
827
- #line 268 "parser.rl"
828
-
829
- if (cs >= JSON_integer_first_final) {
830
- long len = p - json->memo;
831
- *result = rb_Integer(rb_str_new(json->memo, len));
832
- return p + 1;
833
- } else {
834
- return NULL;
835
- }
836
- }
837
-
838
-
839
- #line 840 "parser.c"
840
- static const int JSON_float_start = 1;
841
- static const int JSON_float_first_final = 10;
842
- static const int JSON_float_error = 0;
843
-
844
- static const int JSON_float_en_main = 1;
845
-
846
-
847
- #line 290 "parser.rl"
848
-
849
-
850
- static char *JSON_parse_float(JSON_Parser *json, char *p, char *pe, VALUE *result)
851
- {
852
- int cs = EVIL;
853
-
854
-
855
- #line 856 "parser.c"
856
- {
857
- cs = JSON_float_start;
858
- }
859
-
860
- #line 297 "parser.rl"
861
- json->memo = p;
862
-
863
- #line 864 "parser.c"
864
- {
865
- if ( p == pe )
866
- goto _test_eof;
867
- switch ( cs )
868
- {
869
- case 1:
870
- switch( (*p) ) {
871
- case 45: goto st2;
872
- case 48: goto st3;
873
- }
874
- if ( 49 <= (*p) && (*p) <= 57 )
875
- goto st9;
876
- goto st0;
877
- st0:
878
- cs = 0;
879
- goto _out;
880
- st2:
881
- if ( ++p == pe )
882
- goto _test_eof2;
883
- case 2:
884
- if ( (*p) == 48 )
885
- goto st3;
886
- if ( 49 <= (*p) && (*p) <= 57 )
887
- goto st9;
888
- goto st0;
889
- st3:
890
- if ( ++p == pe )
891
- goto _test_eof3;
892
- case 3:
893
- switch( (*p) ) {
894
- case 46: goto st4;
895
- case 69: goto st6;
896
- case 101: goto st6;
897
- }
898
- goto st0;
899
- st4:
900
- if ( ++p == pe )
901
- goto _test_eof4;
902
- case 4:
903
- if ( 48 <= (*p) && (*p) <= 57 )
904
- goto st5;
905
- goto st0;
906
- st5:
907
- if ( ++p == pe )
908
- goto _test_eof5;
909
- case 5:
910
- switch( (*p) ) {
911
- case 69: goto st6;
912
- case 101: goto st6;
913
- }
914
- if ( (*p) > 46 ) {
915
- if ( 48 <= (*p) && (*p) <= 57 )
916
- goto st5;
917
- } else if ( (*p) >= 45 )
918
- goto st0;
919
- goto tr7;
920
- tr7:
921
- #line 284 "parser.rl"
922
- { p--; {p++; cs = 10; goto _out;} }
923
- goto st10;
924
- st10:
925
- if ( ++p == pe )
926
- goto _test_eof10;
927
- case 10:
928
- #line 929 "parser.c"
929
- goto st0;
930
- st6:
931
- if ( ++p == pe )
932
- goto _test_eof6;
933
- case 6:
934
- switch( (*p) ) {
935
- case 43: goto st7;
936
- case 45: goto st7;
937
- }
938
- if ( 48 <= (*p) && (*p) <= 57 )
939
- goto st8;
940
- goto st0;
941
- st7:
942
- if ( ++p == pe )
943
- goto _test_eof7;
944
- case 7:
945
- if ( 48 <= (*p) && (*p) <= 57 )
946
- goto st8;
947
- goto st0;
948
- st8:
949
- if ( ++p == pe )
950
- goto _test_eof8;
951
- case 8:
952
- switch( (*p) ) {
953
- case 69: goto st0;
954
- case 101: goto st0;
955
- }
956
- if ( (*p) > 46 ) {
957
- if ( 48 <= (*p) && (*p) <= 57 )
958
- goto st8;
959
- } else if ( (*p) >= 45 )
960
- goto st0;
961
- goto tr7;
962
- st9:
963
- if ( ++p == pe )
964
- goto _test_eof9;
965
- case 9:
966
- switch( (*p) ) {
967
- case 46: goto st4;
968
- case 69: goto st6;
969
- case 101: goto st6;
970
- }
971
- if ( 48 <= (*p) && (*p) <= 57 )
972
- goto st9;
973
- goto st0;
974
- }
975
- _test_eof2: cs = 2; goto _test_eof;
976
- _test_eof3: cs = 3; goto _test_eof;
977
- _test_eof4: cs = 4; goto _test_eof;
978
- _test_eof5: cs = 5; goto _test_eof;
979
- _test_eof10: cs = 10; goto _test_eof;
980
- _test_eof6: cs = 6; goto _test_eof;
981
- _test_eof7: cs = 7; goto _test_eof;
982
- _test_eof8: cs = 8; goto _test_eof;
983
- _test_eof9: cs = 9; goto _test_eof;
984
-
985
- _test_eof: {}
986
- _out: {}
987
- }
988
-
989
- #line 299 "parser.rl"
990
-
991
- if (cs >= JSON_float_first_final) {
992
- long len = p - json->memo;
993
- *result = rb_Float(rb_str_new(json->memo, len));
994
- return p + 1;
995
- } else {
996
- return NULL;
997
- }
998
- }
999
-
1000
-
1001
-
1002
- #line 1003 "parser.c"
1003
- static const int JSON_array_start = 1;
1004
- static const int JSON_array_first_final = 17;
1005
- static const int JSON_array_error = 0;
1006
-
1007
- static const int JSON_array_en_main = 1;
1008
-
1009
-
1010
- #line 335 "parser.rl"
1011
-
1012
-
1013
- static char *JSON_parse_array(JSON_Parser *json, char *p, char *pe, VALUE *result)
1014
- {
1015
- int cs = EVIL;
1016
- VALUE array_class = json->array_class;
1017
-
1018
- if (json->max_nesting && json->current_nesting > json->max_nesting) {
1019
- rb_raise(eNestingError, "nesting of %d is too deep", json->current_nesting);
1020
- }
1021
- *result = NIL_P(array_class) ? rb_ary_new() : rb_class_new_instance(0, 0, array_class);
1022
-
1023
-
1024
- #line 1025 "parser.c"
1025
- {
1026
- cs = JSON_array_start;
1027
- }
1028
-
1029
- #line 348 "parser.rl"
1030
-
1031
- #line 1032 "parser.c"
1032
- {
1033
- if ( p == pe )
1034
- goto _test_eof;
1035
- switch ( cs )
1036
- {
1037
- case 1:
1038
- if ( (*p) == 91 )
1039
- goto st2;
1040
- goto st0;
1041
- st0:
1042
- cs = 0;
1043
- goto _out;
1044
- st2:
1045
- if ( ++p == pe )
1046
- goto _test_eof2;
1047
- case 2:
1048
- switch( (*p) ) {
1049
- case 13: goto st2;
1050
- case 32: goto st2;
1051
- case 34: goto tr2;
1052
- case 45: goto tr2;
1053
- case 47: goto st13;
1054
- case 73: goto tr2;
1055
- case 78: goto tr2;
1056
- case 91: goto tr2;
1057
- case 93: goto tr4;
1058
- case 102: goto tr2;
1059
- case 110: goto tr2;
1060
- case 116: goto tr2;
1061
- case 123: goto tr2;
1062
- }
1063
- if ( (*p) > 10 ) {
1064
- if ( 48 <= (*p) && (*p) <= 57 )
1065
- goto tr2;
1066
- } else if ( (*p) >= 9 )
1067
- goto st2;
1068
- goto st0;
1069
- tr2:
1070
- #line 316 "parser.rl"
1071
- {
1072
- VALUE v = Qnil;
1073
- char *np = JSON_parse_value(json, p, pe, &v);
1074
- if (np == NULL) {
1075
- p--; {p++; cs = 3; goto _out;}
1199
+
1200
+ // Parse exponent
1201
+ if (rb_tolower(peek(state)) == 'e') {
1202
+ integer = false;
1203
+ state->cursor++;
1204
+
1205
+ bool negative_exponent = false;
1206
+ const char next_char = peek(state);
1207
+ if (next_char == '-' || next_char == '+') {
1208
+ negative_exponent = next_char == '-';
1209
+ state->cursor++;
1210
+ }
1211
+
1212
+ uint64_t abs_exponent = 0;
1213
+ int exponent_digits = json_parse_digits(state, &abs_exponent);
1214
+
1215
+ if (RB_UNLIKELY(!exponent_digits)) {
1216
+ raise_parse_error_at("invalid number: %s", state, start);
1217
+ }
1218
+
1219
+ if (RB_UNLIKELY(exponent_digits >= 20 || abs_exponent > (uint64_t)INT64_MAX)) {
1220
+ exponent = negative_exponent ? INT64_MIN : INT64_MAX;
1076
1221
  } else {
1077
- rb_ary_push(*result, v);
1078
- {p = (( np))-1;}
1222
+ exponent = negative_exponent ? -(int64_t)abs_exponent : (int64_t)abs_exponent;
1079
1223
  }
1080
1224
  }
1081
- goto st3;
1082
- st3:
1083
- if ( ++p == pe )
1084
- goto _test_eof3;
1085
- case 3:
1086
- #line 1087 "parser.c"
1087
- switch( (*p) ) {
1088
- case 13: goto st3;
1089
- case 32: goto st3;
1090
- case 44: goto st4;
1091
- case 47: goto st9;
1092
- case 93: goto tr4;
1093
- }
1094
- if ( 9 <= (*p) && (*p) <= 10 )
1095
- goto st3;
1096
- goto st0;
1097
- st4:
1098
- if ( ++p == pe )
1099
- goto _test_eof4;
1100
- case 4:
1101
- switch( (*p) ) {
1102
- case 13: goto st4;
1103
- case 32: goto st4;
1104
- case 34: goto tr2;
1105
- case 45: goto tr2;
1106
- case 47: goto st5;
1107
- case 73: goto tr2;
1108
- case 78: goto tr2;
1109
- case 91: goto tr2;
1110
- case 102: goto tr2;
1111
- case 110: goto tr2;
1112
- case 116: goto tr2;
1113
- case 123: goto tr2;
1114
- }
1115
- if ( (*p) > 10 ) {
1116
- if ( 48 <= (*p) && (*p) <= 57 )
1117
- goto tr2;
1118
- } else if ( (*p) >= 9 )
1119
- goto st4;
1120
- goto st0;
1121
- st5:
1122
- if ( ++p == pe )
1123
- goto _test_eof5;
1124
- case 5:
1125
- switch( (*p) ) {
1126
- case 42: goto st6;
1127
- case 47: goto st8;
1128
- }
1129
- goto st0;
1130
- st6:
1131
- if ( ++p == pe )
1132
- goto _test_eof6;
1133
- case 6:
1134
- if ( (*p) == 42 )
1135
- goto st7;
1136
- goto st6;
1137
- st7:
1138
- if ( ++p == pe )
1139
- goto _test_eof7;
1140
- case 7:
1141
- switch( (*p) ) {
1142
- case 42: goto st7;
1143
- case 47: goto st4;
1144
- }
1145
- goto st6;
1146
- st8:
1147
- if ( ++p == pe )
1148
- goto _test_eof8;
1149
- case 8:
1150
- if ( (*p) == 10 )
1151
- goto st4;
1152
- goto st8;
1153
- st9:
1154
- if ( ++p == pe )
1155
- goto _test_eof9;
1156
- case 9:
1157
- switch( (*p) ) {
1158
- case 42: goto st10;
1159
- case 47: goto st12;
1160
- }
1161
- goto st0;
1162
- st10:
1163
- if ( ++p == pe )
1164
- goto _test_eof10;
1165
- case 10:
1166
- if ( (*p) == 42 )
1167
- goto st11;
1168
- goto st10;
1169
- st11:
1170
- if ( ++p == pe )
1171
- goto _test_eof11;
1172
- case 11:
1173
- switch( (*p) ) {
1174
- case 42: goto st11;
1175
- case 47: goto st3;
1176
- }
1177
- goto st10;
1178
- st12:
1179
- if ( ++p == pe )
1180
- goto _test_eof12;
1181
- case 12:
1182
- if ( (*p) == 10 )
1183
- goto st3;
1184
- goto st12;
1185
- tr4:
1186
- #line 327 "parser.rl"
1187
- { p--; {p++; cs = 17; goto _out;} }
1188
- goto st17;
1189
- st17:
1190
- if ( ++p == pe )
1191
- goto _test_eof17;
1192
- case 17:
1193
- #line 1194 "parser.c"
1194
- goto st0;
1195
- st13:
1196
- if ( ++p == pe )
1197
- goto _test_eof13;
1198
- case 13:
1199
- switch( (*p) ) {
1200
- case 42: goto st14;
1201
- case 47: goto st16;
1202
- }
1203
- goto st0;
1204
- st14:
1205
- if ( ++p == pe )
1206
- goto _test_eof14;
1207
- case 14:
1208
- if ( (*p) == 42 )
1209
- goto st15;
1210
- goto st14;
1211
- st15:
1212
- if ( ++p == pe )
1213
- goto _test_eof15;
1214
- case 15:
1215
- switch( (*p) ) {
1216
- case 42: goto st15;
1217
- case 47: goto st2;
1218
- }
1219
- goto st14;
1220
- st16:
1221
- if ( ++p == pe )
1222
- goto _test_eof16;
1223
- case 16:
1224
- if ( (*p) == 10 )
1225
- goto st2;
1226
- goto st16;
1227
- }
1228
- _test_eof2: cs = 2; goto _test_eof;
1229
- _test_eof3: cs = 3; goto _test_eof;
1230
- _test_eof4: cs = 4; goto _test_eof;
1231
- _test_eof5: cs = 5; goto _test_eof;
1232
- _test_eof6: cs = 6; goto _test_eof;
1233
- _test_eof7: cs = 7; goto _test_eof;
1234
- _test_eof8: cs = 8; goto _test_eof;
1235
- _test_eof9: cs = 9; goto _test_eof;
1236
- _test_eof10: cs = 10; goto _test_eof;
1237
- _test_eof11: cs = 11; goto _test_eof;
1238
- _test_eof12: cs = 12; goto _test_eof;
1239
- _test_eof17: cs = 17; goto _test_eof;
1240
- _test_eof13: cs = 13; goto _test_eof;
1241
- _test_eof14: cs = 14; goto _test_eof;
1242
- _test_eof15: cs = 15; goto _test_eof;
1243
- _test_eof16: cs = 16; goto _test_eof;
1244
-
1245
- _test_eof: {}
1246
- _out: {}
1247
- }
1248
-
1249
- #line 349 "parser.rl"
1250
-
1251
- if(cs >= JSON_array_first_final) {
1252
- return p + 1;
1253
- } else {
1254
- rb_raise(eParserError, "%u: unexpected token at '%s'", __LINE__, p);
1225
+
1226
+ if (integer) {
1227
+ return json_decode_integer(mantissa, mantissa_digits, negative, start, state->cursor);
1228
+ }
1229
+
1230
+ // Adjust exponent based on decimal point position
1231
+ if (decimal_point_pos >= 0) {
1232
+ exponent -= (mantissa_digits - decimal_point_pos);
1255
1233
  }
1234
+
1235
+ return json_decode_float(config, mantissa, mantissa_digits, exponent, negative, start, state->cursor);
1256
1236
  }
1257
1237
 
1258
- static VALUE json_string_unescape(char *p, char *pe)
1238
+ static inline VALUE json_parse_positive_number(JSON_ParserState *state, JSON_ParserConfig *config)
1259
1239
  {
1260
- VALUE result = rb_str_buf_new(pe - p + 1);
1240
+ return json_parse_number(state, config, false, state->cursor);
1241
+ }
1261
1242
 
1262
- while (p < pe) {
1263
- if (*p == '\\') {
1264
- p++;
1265
- if (p >= pe) return Qnil; /* raise an exception later, \ at end */
1266
- switch (*p) {
1267
- case '"':
1268
- case '\\':
1269
- rb_str_buf_cat(result, p, 1);
1270
- p++;
1271
- break;
1272
- case 'b':
1273
- rb_str_buf_cat2(result, "\b");
1274
- p++;
1275
- break;
1276
- case 'f':
1277
- rb_str_buf_cat2(result, "\f");
1278
- p++;
1279
- break;
1280
- case 'n':
1281
- rb_str_buf_cat2(result, "\n");
1282
- p++;
1283
- break;
1284
- case 'r':
1285
- rb_str_buf_cat2(result, "\r");
1286
- p++;
1287
- break;
1288
- case 't':
1289
- rb_str_buf_cat2(result, "\t");
1290
- p++;
1291
- break;
1292
- case 'u':
1293
- if (p > pe - 4) {
1294
- return Qnil;
1295
- } else {
1296
- p = JSON_convert_UTF16_to_UTF8(result, p, pe, strictConversion);
1243
+ static inline VALUE json_parse_negative_number(JSON_ParserState *state, JSON_ParserConfig *config)
1244
+ {
1245
+ const char *start = state->cursor;
1246
+ state->cursor++;
1247
+ return json_parse_number(state, config, true, start);
1248
+ }
1249
+
1250
+ static VALUE json_parse_any(JSON_ParserState *state, JSON_ParserConfig *config)
1251
+ {
1252
+ json_eat_whitespace(state);
1253
+
1254
+ switch (peek(state)) {
1255
+ case 'n':
1256
+ if (rest(state) >= 4 && (memcmp(state->cursor, "null", 4) == 0)) {
1257
+ state->cursor += 4;
1258
+ return json_push_value(state, config, Qnil);
1259
+ }
1260
+
1261
+ raise_parse_error("unexpected token %s", state);
1262
+ break;
1263
+ case 't':
1264
+ if (rest(state) >= 4 && (memcmp(state->cursor, "true", 4) == 0)) {
1265
+ state->cursor += 4;
1266
+ return json_push_value(state, config, Qtrue);
1267
+ }
1268
+
1269
+ raise_parse_error("unexpected token %s", state);
1270
+ break;
1271
+ case 'f':
1272
+ // Note: memcmp with a small power of two compile to an integer comparison
1273
+ if (rest(state) >= 5 && (memcmp(state->cursor + 1, "alse", 4) == 0)) {
1274
+ state->cursor += 5;
1275
+ return json_push_value(state, config, Qfalse);
1276
+ }
1277
+
1278
+ raise_parse_error("unexpected token %s", state);
1279
+ break;
1280
+ case 'N':
1281
+ // Note: memcmp with a small power of two compile to an integer comparison
1282
+ if (config->allow_nan && rest(state) >= 3 && (memcmp(state->cursor + 1, "aN", 2) == 0)) {
1283
+ state->cursor += 3;
1284
+ return json_push_value(state, config, CNaN);
1285
+ }
1286
+
1287
+ raise_parse_error("unexpected token %s", state);
1288
+ break;
1289
+ case 'I':
1290
+ if (config->allow_nan && rest(state) >= 8 && (memcmp(state->cursor, "Infinity", 8) == 0)) {
1291
+ state->cursor += 8;
1292
+ return json_push_value(state, config, CInfinity);
1293
+ }
1294
+
1295
+ raise_parse_error("unexpected token %s", state);
1296
+ break;
1297
+ case '-': {
1298
+ // Note: memcmp with a small power of two compile to an integer comparison
1299
+ if (rest(state) >= 9 && (memcmp(state->cursor + 1, "Infinity", 8) == 0)) {
1300
+ if (config->allow_nan) {
1301
+ state->cursor += 9;
1302
+ return json_push_value(state, config, CMinusInfinity);
1303
+ } else {
1304
+ raise_parse_error("unexpected token %s", state);
1305
+ }
1306
+ }
1307
+ return json_push_value(state, config, json_parse_negative_number(state, config));
1308
+ break;
1309
+ }
1310
+ case '0': case '1': case '2': case '3': case '4': case '5': case '6': case '7': case '8': case '9':
1311
+ return json_push_value(state, config, json_parse_positive_number(state, config));
1312
+ break;
1313
+ case '"': {
1314
+ // %r{\A"[^"\\\t\n\x00]*(?:\\[bfnrtu\\/"][^"\\]*)*"}
1315
+ return json_parse_string(state, config, false);
1316
+ break;
1317
+ }
1318
+ case '[': {
1319
+ state->cursor++;
1320
+ json_eat_whitespace(state);
1321
+ long stack_head = state->stack->head;
1322
+
1323
+ if (peek(state) == ']') {
1324
+ state->cursor++;
1325
+ return json_push_value(state, config, json_decode_array(state, config, 0));
1326
+ } else {
1327
+ state->current_nesting++;
1328
+ if (RB_UNLIKELY(config->max_nesting && (config->max_nesting < state->current_nesting))) {
1329
+ rb_raise(eNestingError, "nesting of %d is too deep", state->current_nesting);
1330
+ }
1331
+ state->in_array++;
1332
+ json_parse_any(state, config);
1333
+ }
1334
+
1335
+ while (true) {
1336
+ json_eat_whitespace(state);
1337
+
1338
+ const char next_char = peek(state);
1339
+
1340
+ if (RB_LIKELY(next_char == ',')) {
1341
+ state->cursor++;
1342
+ if (config->allow_trailing_comma) {
1343
+ json_eat_whitespace(state);
1344
+ if (peek(state) == ']') {
1345
+ continue;
1346
+ }
1297
1347
  }
1298
- break;
1299
- default:
1300
- rb_str_buf_cat(result, p, 1);
1301
- p++;
1302
- break;
1348
+ json_parse_any(state, config);
1349
+ continue;
1350
+ }
1351
+
1352
+ if (next_char == ']') {
1353
+ state->cursor++;
1354
+ long count = state->stack->head - stack_head;
1355
+ state->current_nesting--;
1356
+ state->in_array--;
1357
+ return json_push_value(state, config, json_decode_array(state, config, count));
1358
+ }
1359
+
1360
+ raise_parse_error("expected ',' or ']' after array value", state);
1303
1361
  }
1304
- } else {
1305
- char *q = p;
1306
- while (*q != '\\' && q < pe) q++;
1307
- rb_str_buf_cat(result, p, q - p);
1308
- p = q;
1362
+ break;
1309
1363
  }
1310
- }
1311
- return result;
1312
- }
1364
+ case '{': {
1365
+ const char *object_start_cursor = state->cursor;
1313
1366
 
1367
+ state->cursor++;
1368
+ json_eat_whitespace(state);
1369
+ long stack_head = state->stack->head;
1314
1370
 
1315
- #line 1316 "parser.c"
1316
- static const int JSON_string_start = 1;
1317
- static const int JSON_string_first_final = 8;
1318
- static const int JSON_string_error = 0;
1319
-
1320
- static const int JSON_string_en_main = 1;
1321
-
1322
-
1323
- #line 433 "parser.rl"
1324
-
1325
-
1326
- static char *JSON_parse_string(JSON_Parser *json, char *p, char *pe, VALUE *result)
1327
- {
1328
- int cs = EVIL;
1329
-
1330
- *result = rb_str_new("", 0);
1331
-
1332
- #line 1333 "parser.c"
1333
- {
1334
- cs = JSON_string_start;
1335
- }
1336
-
1337
- #line 441 "parser.rl"
1338
- json->memo = p;
1339
-
1340
- #line 1341 "parser.c"
1341
- {
1342
- if ( p == pe )
1343
- goto _test_eof;
1344
- switch ( cs )
1345
- {
1346
- case 1:
1347
- if ( (*p) == 34 )
1348
- goto st2;
1349
- goto st0;
1350
- st0:
1351
- cs = 0;
1352
- goto _out;
1353
- st2:
1354
- if ( ++p == pe )
1355
- goto _test_eof2;
1356
- case 2:
1357
- switch( (*p) ) {
1358
- case 34: goto tr2;
1359
- case 92: goto st3;
1360
- }
1361
- if ( 0 <= (*p) && (*p) <= 31 )
1362
- goto st0;
1363
- goto st2;
1364
- tr2:
1365
- #line 419 "parser.rl"
1366
- {
1367
- *result = json_string_unescape(json->memo + 1, p);
1368
- if (NIL_P(*result)) {
1369
- p--;
1370
- {p++; cs = 8; goto _out;}
1371
- } else {
1372
- FORCE_UTF8(*result);
1373
- {p = (( p + 1))-1;}
1374
- }
1375
- }
1376
- #line 430 "parser.rl"
1377
- { p--; {p++; cs = 8; goto _out;} }
1378
- goto st8;
1379
- st8:
1380
- if ( ++p == pe )
1381
- goto _test_eof8;
1382
- case 8:
1383
- #line 1384 "parser.c"
1384
- goto st0;
1385
- st3:
1386
- if ( ++p == pe )
1387
- goto _test_eof3;
1388
- case 3:
1389
- if ( (*p) == 117 )
1390
- goto st4;
1391
- if ( 0 <= (*p) && (*p) <= 31 )
1392
- goto st0;
1393
- goto st2;
1394
- st4:
1395
- if ( ++p == pe )
1396
- goto _test_eof4;
1397
- case 4:
1398
- if ( (*p) < 65 ) {
1399
- if ( 48 <= (*p) && (*p) <= 57 )
1400
- goto st5;
1401
- } else if ( (*p) > 70 ) {
1402
- if ( 97 <= (*p) && (*p) <= 102 )
1403
- goto st5;
1404
- } else
1405
- goto st5;
1406
- goto st0;
1407
- st5:
1408
- if ( ++p == pe )
1409
- goto _test_eof5;
1410
- case 5:
1411
- if ( (*p) < 65 ) {
1412
- if ( 48 <= (*p) && (*p) <= 57 )
1413
- goto st6;
1414
- } else if ( (*p) > 70 ) {
1415
- if ( 97 <= (*p) && (*p) <= 102 )
1416
- goto st6;
1417
- } else
1418
- goto st6;
1419
- goto st0;
1420
- st6:
1421
- if ( ++p == pe )
1422
- goto _test_eof6;
1423
- case 6:
1424
- if ( (*p) < 65 ) {
1425
- if ( 48 <= (*p) && (*p) <= 57 )
1426
- goto st7;
1427
- } else if ( (*p) > 70 ) {
1428
- if ( 97 <= (*p) && (*p) <= 102 )
1429
- goto st7;
1430
- } else
1431
- goto st7;
1432
- goto st0;
1433
- st7:
1434
- if ( ++p == pe )
1435
- goto _test_eof7;
1436
- case 7:
1437
- if ( (*p) < 65 ) {
1438
- if ( 48 <= (*p) && (*p) <= 57 )
1439
- goto st2;
1440
- } else if ( (*p) > 70 ) {
1441
- if ( 97 <= (*p) && (*p) <= 102 )
1442
- goto st2;
1443
- } else
1444
- goto st2;
1445
- goto st0;
1446
- }
1447
- _test_eof2: cs = 2; goto _test_eof;
1448
- _test_eof8: cs = 8; goto _test_eof;
1449
- _test_eof3: cs = 3; goto _test_eof;
1450
- _test_eof4: cs = 4; goto _test_eof;
1451
- _test_eof5: cs = 5; goto _test_eof;
1452
- _test_eof6: cs = 6; goto _test_eof;
1453
- _test_eof7: cs = 7; goto _test_eof;
1454
-
1455
- _test_eof: {}
1456
- _out: {}
1457
- }
1458
-
1459
- #line 443 "parser.rl"
1460
-
1461
- if (cs >= JSON_string_first_final) {
1462
- return p + 1;
1463
- } else {
1464
- return NULL;
1465
- }
1466
- }
1371
+ if (peek(state) == '}') {
1372
+ state->cursor++;
1373
+ return json_push_value(state, config, json_decode_object(state, config, 0));
1374
+ } else {
1375
+ state->current_nesting++;
1376
+ if (RB_UNLIKELY(config->max_nesting && (config->max_nesting < state->current_nesting))) {
1377
+ rb_raise(eNestingError, "nesting of %d is too deep", state->current_nesting);
1378
+ }
1379
+
1380
+ if (peek(state) != '"') {
1381
+ raise_parse_error("expected object key, got %s", state);
1382
+ }
1383
+ json_parse_string(state, config, true);
1384
+
1385
+ json_eat_whitespace(state);
1386
+ if (peek(state) != ':') {
1387
+ raise_parse_error("expected ':' after object key", state);
1388
+ }
1389
+ state->cursor++;
1390
+
1391
+ json_parse_any(state, config);
1392
+ }
1393
+
1394
+ while (true) {
1395
+ json_eat_whitespace(state);
1396
+
1397
+ const char next_char = peek(state);
1398
+ if (next_char == '}') {
1399
+ state->cursor++;
1400
+ state->current_nesting--;
1401
+ size_t count = state->stack->head - stack_head;
1402
+
1403
+ // Temporary rewind cursor in case an error is raised
1404
+ const char *final_cursor = state->cursor;
1405
+ state->cursor = object_start_cursor;
1406
+ VALUE object = json_decode_object(state, config, count);
1407
+ state->cursor = final_cursor;
1408
+
1409
+ return json_push_value(state, config, object);
1410
+ }
1411
+
1412
+ if (next_char == ',') {
1413
+ state->cursor++;
1414
+ json_eat_whitespace(state);
1415
+
1416
+ if (config->allow_trailing_comma) {
1417
+ if (peek(state) == '}') {
1418
+ continue;
1419
+ }
1420
+ }
1421
+
1422
+ if (RB_UNLIKELY(peek(state) != '"')) {
1423
+ raise_parse_error("expected object key, got: %s", state);
1424
+ }
1425
+ json_parse_string(state, config, true);
1426
+
1427
+ json_eat_whitespace(state);
1428
+ if (RB_UNLIKELY(peek(state) != ':')) {
1429
+ raise_parse_error("expected ':' after object key, got: %s", state);
1430
+ }
1431
+ state->cursor++;
1467
1432
 
1433
+ json_parse_any(state, config);
1468
1434
 
1435
+ continue;
1436
+ }
1469
1437
 
1470
- #line 1471 "parser.c"
1471
- static const int JSON_start = 1;
1472
- static const int JSON_first_final = 10;
1473
- static const int JSON_error = 0;
1438
+ raise_parse_error("expected ',' or '}' after object value, got: %s", state);
1439
+ }
1440
+ break;
1441
+ }
1474
1442
 
1475
- static const int JSON_en_main = 1;
1443
+ case 0:
1444
+ raise_parse_error("unexpected end of input", state);
1445
+ break;
1476
1446
 
1447
+ default:
1448
+ raise_parse_error("unexpected character: %s", state);
1449
+ break;
1450
+ }
1477
1451
 
1478
- #line 477 "parser.rl"
1452
+ raise_parse_error("unreachable: %s", state);
1453
+ return Qundef;
1454
+ }
1479
1455
 
1456
+ static void json_ensure_eof(JSON_ParserState *state)
1457
+ {
1458
+ json_eat_whitespace(state);
1459
+ if (!eos(state)) {
1460
+ raise_parse_error("unexpected token at end of stream %s", state);
1461
+ }
1462
+ }
1480
1463
 
1481
- /*
1464
+ /*
1482
1465
  * Document-class: JSON::Ext::Parser
1483
1466
  *
1484
1467
  * This is the JSON parser implemented as a C extension. It can be configured
@@ -1490,60 +1473,108 @@ static const int JSON_en_main = 1;
1490
1473
  *
1491
1474
  */
1492
1475
 
1493
- inline static VALUE convert_encoding(VALUE source)
1494
- {
1495
- char *ptr = RSTRING_PTR(source);
1496
- long len = RSTRING_LEN(source);
1497
- if (len < 2) {
1498
- rb_raise(eParserError, "A JSON text must at least contain two octets!");
1499
- }
1500
- #ifdef HAVE_RUBY_ENCODING_H
1501
- {
1502
- VALUE encoding = rb_funcall(source, i_encoding, 0);
1503
- if (encoding == mEncoding_ASCII_8BIT) {
1504
- if (len >= 4 && ptr[0] == 0 && ptr[1] == 0 && ptr[2] == 0) {
1505
- source = rb_str_dup(source);
1506
- rb_funcall(source, i_force_encoding, 1, mEncoding_UTF_32BE);
1507
- source = rb_funcall(source, i_encode_bang, 1, mEncoding_UTF_8);
1508
- } else if (len >= 4 && ptr[0] == 0 && ptr[2] == 0) {
1509
- source = rb_str_dup(source);
1510
- rb_funcall(source, i_force_encoding, 1, mEncoding_UTF_16BE);
1511
- source = rb_funcall(source, i_encode_bang, 1, mEncoding_UTF_8);
1512
- } else if (len >= 4 && ptr[1] == 0 && ptr[2] == 0 && ptr[3] == 0) {
1513
- source = rb_str_dup(source);
1514
- rb_funcall(source, i_force_encoding, 1, mEncoding_UTF_32LE);
1515
- source = rb_funcall(source, i_encode_bang, 1, mEncoding_UTF_8);
1516
- } else if (len >= 4 && ptr[1] == 0 && ptr[3] == 0) {
1517
- source = rb_str_dup(source);
1518
- rb_funcall(source, i_force_encoding, 1, mEncoding_UTF_16LE);
1519
- source = rb_funcall(source, i_encode_bang, 1, mEncoding_UTF_8);
1520
- } else {
1521
- source = rb_funcall(source, i_force_encoding, 1, mEncoding_UTF_8);
1476
+ static VALUE convert_encoding(VALUE source)
1477
+ {
1478
+ StringValue(source);
1479
+ int encindex = RB_ENCODING_GET(source);
1480
+
1481
+ if (RB_LIKELY(encindex == utf8_encindex)) {
1482
+ return source;
1483
+ }
1484
+
1485
+ if (encindex == binary_encindex) {
1486
+ // For historical reason, we silently reinterpret binary strings as UTF-8
1487
+ return rb_enc_associate_index(rb_str_dup(source), utf8_encindex);
1488
+ }
1489
+
1490
+ source = rb_funcall(source, i_encode, 1, Encoding_UTF_8);
1491
+ StringValue(source);
1492
+ return source;
1493
+ }
1494
+
1495
+ struct parser_config_init_args {
1496
+ JSON_ParserConfig *config;
1497
+ VALUE self;
1498
+ };
1499
+
1500
+ static void parser_config_wb_write(VALUE self, VALUE *dest, VALUE val)
1501
+ {
1502
+ *dest = val;
1503
+ if (self) RB_OBJ_WRITTEN(self, Qundef, val);
1504
+ }
1505
+
1506
+ static int parser_config_init_i(VALUE key, VALUE val, VALUE data)
1507
+ {
1508
+ struct parser_config_init_args *args = (struct parser_config_init_args *)data;
1509
+ JSON_ParserConfig *config = args->config;
1510
+ VALUE self = args->self;
1511
+
1512
+ if (key == sym_max_nesting) { config->max_nesting = RTEST(val) ? FIX2INT(val) : 0; }
1513
+ else if (key == sym_allow_nan) { config->allow_nan = RTEST(val); }
1514
+ else if (key == sym_allow_trailing_comma) { config->allow_trailing_comma = RTEST(val); }
1515
+ else if (key == sym_allow_control_characters) { config->allow_control_characters = RTEST(val); }
1516
+ else if (key == sym_allow_invalid_escape) { config->allow_invalid_escape = RTEST(val); }
1517
+ else if (key == sym_symbolize_names) { config->symbolize_names = RTEST(val); }
1518
+ else if (key == sym_freeze) { config->freeze = RTEST(val); }
1519
+ else if (key == sym_on_load) { parser_config_wb_write(self, &config->on_load_proc, RTEST(val) ? val : Qfalse); }
1520
+ else if (key == sym_allow_duplicate_key) { config->on_duplicate_key = RTEST(val) ? JSON_IGNORE : JSON_RAISE; }
1521
+ else if (key == sym_decimal_class) {
1522
+ if (RTEST(val)) {
1523
+ if (rb_respond_to(val, i_try_convert)) {
1524
+ parser_config_wb_write(self, &config->decimal_class, val);
1525
+ config->decimal_method_id = i_try_convert;
1526
+ } else if (rb_respond_to(val, i_new)) {
1527
+ parser_config_wb_write(self, &config->decimal_class, val);
1528
+ config->decimal_method_id = i_new;
1529
+ } else if (RB_TYPE_P(val, T_CLASS)) {
1530
+ VALUE name = rb_class_name(val);
1531
+ const char *name_cstr = RSTRING_PTR(name);
1532
+ const char *last_colon = strrchr(name_cstr, ':');
1533
+ if (last_colon) {
1534
+ const char *mod_path_end = last_colon - 1;
1535
+ VALUE mod_path = rb_str_substr(name, 0, mod_path_end - name_cstr);
1536
+ parser_config_wb_write(self, &config->decimal_class, rb_path_to_class(mod_path));
1537
+
1538
+ const char *method_name_beg = last_colon + 1;
1539
+ long before_len = method_name_beg - name_cstr;
1540
+ long len = RSTRING_LEN(name) - before_len;
1541
+ VALUE method_name = rb_str_substr(name, before_len, len);
1542
+ config->decimal_method_id = SYM2ID(rb_str_intern(method_name));
1543
+ } else {
1544
+ parser_config_wb_write(self, &config->decimal_class, rb_mKernel);
1545
+ config->decimal_method_id = SYM2ID(rb_str_intern(name));
1546
+ }
1522
1547
  }
1523
- } else {
1524
- source = rb_funcall(source, i_encode, 1, mEncoding_UTF_8);
1525
1548
  }
1526
1549
  }
1527
- #else
1528
- if (len >= 4 && ptr[0] == 0 && ptr[1] == 0 && ptr[2] == 0) {
1529
- source = rb_funcall(mJSON, i_iconv, 3, rb_str_new2("utf-8"), rb_str_new2("utf-32be"), source);
1530
- } else if (len >= 4 && ptr[0] == 0 && ptr[2] == 0) {
1531
- source = rb_funcall(mJSON, i_iconv, 3, rb_str_new2("utf-8"), rb_str_new2("utf-16be"), source);
1532
- } else if (len >= 4 && ptr[1] == 0 && ptr[2] == 0 && ptr[3] == 0) {
1533
- source = rb_funcall(mJSON, i_iconv, 3, rb_str_new2("utf-8"), rb_str_new2("utf-32le"), source);
1534
- } else if (len >= 4 && ptr[1] == 0 && ptr[3] == 0) {
1535
- source = rb_funcall(mJSON, i_iconv, 3, rb_str_new2("utf-8"), rb_str_new2("utf-16le"), source);
1550
+
1551
+ return ST_CONTINUE;
1552
+ }
1553
+
1554
+ static void parser_config_init(JSON_ParserConfig *config, VALUE opts, VALUE self)
1555
+ {
1556
+ config->max_nesting = 100;
1557
+
1558
+ struct parser_config_init_args args = {
1559
+ .config = config,
1560
+ .self = self,
1561
+ };
1562
+
1563
+ if (!NIL_P(opts)) {
1564
+ Check_Type(opts, T_HASH);
1565
+ if (RHASH_SIZE(opts) > 0) {
1566
+ // We assume in most cases few keys are set so it's faster to go over
1567
+ // the provided keys than to check all possible keys.
1568
+ rb_hash_foreach(opts, parser_config_init_i, (VALUE)&args);
1569
+ }
1570
+
1536
1571
  }
1537
- #endif
1538
- return source;
1539
1572
  }
1540
1573
 
1541
1574
  /*
1542
- * call-seq: new(source, opts => {})
1575
+ * call-seq: new(opts => {})
1543
1576
  *
1544
- * Creates a new JSON::Ext::Parser instance for the string _source_.
1545
- *
1546
- * Creates a new JSON::Ext::Parser instance for the string _source_.
1577
+ * Creates a new JSON::Ext::ParserConfig instance.
1547
1578
  *
1548
1579
  * It will be configured by the _opts_ hash. _opts_ can have the following
1549
1580
  * keys:
@@ -1551,330 +1582,176 @@ inline static VALUE convert_encoding(VALUE source)
1551
1582
  * _opts_ can have the following keys:
1552
1583
  * * *max_nesting*: The maximum depth of nesting allowed in the parsed data
1553
1584
  * structures. Disable depth checking with :max_nesting => false|nil|0, it
1554
- * defaults to 19.
1585
+ * defaults to 100.
1555
1586
  * * *allow_nan*: If set to true, allow NaN, Infinity and -Infinity in
1556
1587
  * defiance of RFC 4627 to be parsed by the Parser. This option defaults to
1557
1588
  * false.
1558
- * * *create_additions*: If set to false, the Parser doesn't create
1559
- * additions even if a matchin class and create_id was found. This option
1560
- * defaults to true.
1561
- * * *object_class*: Defaults to Hash
1562
- * * *array_class*: Defaults to Array
1589
+ * * *symbolize_names*: If set to true, returns symbols for the names
1590
+ * (keys) in a JSON object. Otherwise strings are returned, which is
1591
+ * also the default. It's not possible to use this option in
1592
+ * conjunction with the *create_additions* option.
1593
+ * * *decimal_class*: Specifies which class to use instead of the default
1594
+ * (Float) when parsing decimal numbers. This class must accept a single
1595
+ * string argument in its constructor.
1563
1596
  */
1564
- static VALUE cParser_initialize(int argc, VALUE *argv, VALUE self)
1597
+ static VALUE cParserConfig_initialize(VALUE self, VALUE opts)
1565
1598
  {
1566
- char *ptr;
1567
- long len;
1568
- VALUE source, opts;
1569
- GET_STRUCT;
1570
- rb_scan_args(argc, argv, "11", &source, &opts);
1571
- source = convert_encoding(StringValue(source));
1572
- ptr = RSTRING_PTR(source);
1573
- len = RSTRING_LEN(source);
1574
- if (!NIL_P(opts)) {
1575
- opts = rb_convert_type(opts, T_HASH, "Hash", "to_hash");
1576
- if (NIL_P(opts)) {
1577
- rb_raise(rb_eArgError, "opts needs to be like a hash");
1578
- } else {
1579
- VALUE tmp = ID2SYM(i_max_nesting);
1580
- if (st_lookup(RHASH_TBL(opts), tmp, 0)) {
1581
- VALUE max_nesting = rb_hash_aref(opts, tmp);
1582
- if (RTEST(max_nesting)) {
1583
- Check_Type(max_nesting, T_FIXNUM);
1584
- json->max_nesting = FIX2INT(max_nesting);
1585
- } else {
1586
- json->max_nesting = 0;
1587
- }
1588
- } else {
1589
- json->max_nesting = 19;
1590
- }
1591
- tmp = ID2SYM(i_allow_nan);
1592
- if (st_lookup(RHASH_TBL(opts), tmp, 0)) {
1593
- VALUE allow_nan = rb_hash_aref(opts, tmp);
1594
- json->allow_nan = RTEST(allow_nan) ? 1 : 0;
1595
- } else {
1596
- json->allow_nan = 0;
1597
- }
1598
- tmp = ID2SYM(i_create_additions);
1599
- if (st_lookup(RHASH_TBL(opts), tmp, 0)) {
1600
- VALUE create_additions = rb_hash_aref(opts, tmp);
1601
- if (RTEST(create_additions)) {
1602
- json->create_id = rb_funcall(mJSON, i_create_id, 0);
1603
- } else {
1604
- json->create_id = Qnil;
1605
- }
1606
- } else {
1607
- json->create_id = rb_funcall(mJSON, i_create_id, 0);
1608
- }
1609
- tmp = ID2SYM(i_object_class);
1610
- if (st_lookup(RHASH_TBL(opts), tmp, 0)) {
1611
- json->object_class = rb_hash_aref(opts, tmp);
1612
- } else {
1613
- json->object_class = Qnil;
1614
- }
1615
- tmp = ID2SYM(i_array_class);
1616
- if (st_lookup(RHASH_TBL(opts), tmp, 0)) {
1617
- json->array_class = rb_hash_aref(opts, tmp);
1618
- } else {
1619
- json->array_class = Qnil;
1620
- }
1621
- }
1622
- } else {
1623
- json->max_nesting = 19;
1624
- json->allow_nan = 0;
1625
- json->create_id = rb_funcall(mJSON, i_create_id, 0);
1626
- json->object_class = Qnil;
1627
- json->array_class = Qnil;
1628
- }
1629
- json->current_nesting = 0;
1630
- json->len = len;
1631
- json->source = ptr;
1632
- json->Vsource = source;
1599
+ rb_check_frozen(self);
1600
+ GET_PARSER_CONFIG;
1601
+
1602
+ parser_config_init(config, opts, self);
1603
+
1633
1604
  return self;
1634
1605
  }
1635
1606
 
1607
+ static VALUE cParser_parse(JSON_ParserConfig *config, VALUE src)
1608
+ {
1609
+ VALUE Vsource = convert_encoding(src);
1610
+
1611
+ // Ensure the string isn't mutated under us.
1612
+ // The classic API to use is `rb_str_locktmp`, but then we'd
1613
+ // need to use `rb_protect` to make sure we always unlock.
1614
+ if (Vsource == src) {
1615
+ Vsource = rb_str_new_frozen(Vsource);
1616
+ }
1617
+
1618
+ VALUE rvalue_stack_buffer[RVALUE_STACK_INITIAL_CAPA];
1619
+ rvalue_stack stack = {
1620
+ .type = RVALUE_STACK_STACK_ALLOCATED,
1621
+ .ptr = rvalue_stack_buffer,
1622
+ .capa = RVALUE_STACK_INITIAL_CAPA,
1623
+ };
1624
+
1625
+ long len;
1626
+ const char *start;
1627
+
1628
+ RSTRING_GETMEM(Vsource, start, len);
1629
+
1630
+ VALUE stack_handle = 0;
1631
+ JSON_ParserState _state = {
1632
+ .start = start,
1633
+ .cursor = start,
1634
+ .end = start + len,
1635
+ .stack = &stack,
1636
+ .stack_handle = &stack_handle,
1637
+ };
1638
+ JSON_ParserState *state = &_state;
1639
+
1640
+ VALUE result = json_parse_any(state, config);
1641
+
1642
+ // This may be skipped in case of exception, but
1643
+ // it won't cause a leak.
1644
+ rvalue_stack_eagerly_release(stack_handle);
1645
+ RB_GC_GUARD(stack_handle);
1646
+ RB_GC_GUARD(Vsource);
1647
+ json_ensure_eof(state);
1648
+
1649
+ return result;
1650
+ }
1651
+
1636
1652
  /*
1637
- * call-seq: parse()
1653
+ * call-seq: parse(source)
1638
1654
  *
1639
1655
  * Parses the current JSON text _source_ and returns the complete data
1640
1656
  * structure as a result.
1657
+ * It raises JSON::ParserError if fail to parse.
1641
1658
  */
1642
- static VALUE cParser_parse(VALUE self)
1643
- {
1644
- char *p, *pe;
1645
- int cs = EVIL;
1646
- VALUE result = Qnil;
1647
- GET_STRUCT;
1648
-
1649
-
1650
- #line 1651 "parser.c"
1651
- {
1652
- cs = JSON_start;
1653
- }
1654
-
1655
- #line 648 "parser.rl"
1656
- p = json->source;
1657
- pe = p + json->len;
1658
-
1659
- #line 1660 "parser.c"
1660
- {
1661
- if ( p == pe )
1662
- goto _test_eof;
1663
- switch ( cs )
1664
- {
1665
- st1:
1666
- if ( ++p == pe )
1667
- goto _test_eof1;
1668
- case 1:
1669
- switch( (*p) ) {
1670
- case 13: goto st1;
1671
- case 32: goto st1;
1672
- case 47: goto st2;
1673
- case 91: goto tr3;
1674
- case 123: goto tr4;
1675
- }
1676
- if ( 9 <= (*p) && (*p) <= 10 )
1677
- goto st1;
1678
- goto st0;
1679
- st0:
1680
- cs = 0;
1681
- goto _out;
1682
- st2:
1683
- if ( ++p == pe )
1684
- goto _test_eof2;
1685
- case 2:
1686
- switch( (*p) ) {
1687
- case 42: goto st3;
1688
- case 47: goto st5;
1689
- }
1690
- goto st0;
1691
- st3:
1692
- if ( ++p == pe )
1693
- goto _test_eof3;
1694
- case 3:
1695
- if ( (*p) == 42 )
1696
- goto st4;
1697
- goto st3;
1698
- st4:
1699
- if ( ++p == pe )
1700
- goto _test_eof4;
1701
- case 4:
1702
- switch( (*p) ) {
1703
- case 42: goto st4;
1704
- case 47: goto st1;
1705
- }
1706
- goto st3;
1707
- st5:
1708
- if ( ++p == pe )
1709
- goto _test_eof5;
1710
- case 5:
1711
- if ( (*p) == 10 )
1712
- goto st1;
1713
- goto st5;
1714
- tr3:
1715
- #line 466 "parser.rl"
1716
- {
1717
- char *np;
1718
- json->current_nesting = 1;
1719
- np = JSON_parse_array(json, p, pe, &result);
1720
- if (np == NULL) { p--; {p++; cs = 10; goto _out;} } else {p = (( np))-1;}
1721
- }
1722
- goto st10;
1723
- tr4:
1724
- #line 459 "parser.rl"
1725
- {
1726
- char *np;
1727
- json->current_nesting = 1;
1728
- np = JSON_parse_object(json, p, pe, &result);
1729
- if (np == NULL) { p--; {p++; cs = 10; goto _out;} } else {p = (( np))-1;}
1730
- }
1731
- goto st10;
1732
- st10:
1733
- if ( ++p == pe )
1734
- goto _test_eof10;
1735
- case 10:
1736
- #line 1737 "parser.c"
1737
- switch( (*p) ) {
1738
- case 13: goto st10;
1739
- case 32: goto st10;
1740
- case 47: goto st6;
1741
- }
1742
- if ( 9 <= (*p) && (*p) <= 10 )
1743
- goto st10;
1744
- goto st0;
1745
- st6:
1746
- if ( ++p == pe )
1747
- goto _test_eof6;
1748
- case 6:
1749
- switch( (*p) ) {
1750
- case 42: goto st7;
1751
- case 47: goto st9;
1752
- }
1753
- goto st0;
1754
- st7:
1755
- if ( ++p == pe )
1756
- goto _test_eof7;
1757
- case 7:
1758
- if ( (*p) == 42 )
1759
- goto st8;
1760
- goto st7;
1761
- st8:
1762
- if ( ++p == pe )
1763
- goto _test_eof8;
1764
- case 8:
1765
- switch( (*p) ) {
1766
- case 42: goto st8;
1767
- case 47: goto st10;
1768
- }
1769
- goto st7;
1770
- st9:
1771
- if ( ++p == pe )
1772
- goto _test_eof9;
1773
- case 9:
1774
- if ( (*p) == 10 )
1775
- goto st10;
1776
- goto st9;
1777
- }
1778
- _test_eof1: cs = 1; goto _test_eof;
1779
- _test_eof2: cs = 2; goto _test_eof;
1780
- _test_eof3: cs = 3; goto _test_eof;
1781
- _test_eof4: cs = 4; goto _test_eof;
1782
- _test_eof5: cs = 5; goto _test_eof;
1783
- _test_eof10: cs = 10; goto _test_eof;
1784
- _test_eof6: cs = 6; goto _test_eof;
1785
- _test_eof7: cs = 7; goto _test_eof;
1786
- _test_eof8: cs = 8; goto _test_eof;
1787
- _test_eof9: cs = 9; goto _test_eof;
1788
-
1789
- _test_eof: {}
1790
- _out: {}
1791
- }
1792
-
1793
- #line 651 "parser.rl"
1794
-
1795
- if (cs >= JSON_first_final && p == pe) {
1796
- return result;
1797
- } else {
1798
- rb_raise(eParserError, "%u: unexpected token at '%s'", __LINE__, p);
1799
- }
1659
+ static VALUE cParserConfig_parse(VALUE self, VALUE Vsource)
1660
+ {
1661
+ GET_PARSER_CONFIG;
1662
+ return cParser_parse(config, Vsource);
1800
1663
  }
1801
1664
 
1802
- inline static JSON_Parser *JSON_allocate()
1665
+ static VALUE cParser_m_parse(VALUE klass, VALUE Vsource, VALUE opts)
1803
1666
  {
1804
- JSON_Parser *json = ALLOC(JSON_Parser);
1805
- MEMZERO(json, JSON_Parser, 1);
1806
- return json;
1667
+ JSON_ParserConfig _config = {0};
1668
+ JSON_ParserConfig *config = &_config;
1669
+ parser_config_init(config, opts, false);
1670
+
1671
+ return cParser_parse(config, Vsource);
1807
1672
  }
1808
1673
 
1809
- static void JSON_mark(JSON_Parser *json)
1674
+ static void JSON_ParserConfig_mark(void *ptr)
1810
1675
  {
1811
- rb_gc_mark_maybe(json->Vsource);
1812
- rb_gc_mark_maybe(json->create_id);
1813
- rb_gc_mark_maybe(json->object_class);
1814
- rb_gc_mark_maybe(json->array_class);
1676
+ JSON_ParserConfig *config = ptr;
1677
+ rb_gc_mark(config->on_load_proc);
1678
+ rb_gc_mark(config->decimal_class);
1815
1679
  }
1816
1680
 
1817
- static void JSON_free(JSON_Parser *json)
1681
+ static size_t JSON_ParserConfig_memsize(const void *ptr)
1818
1682
  {
1819
- ruby_xfree(json);
1683
+ return sizeof(JSON_ParserConfig);
1820
1684
  }
1821
1685
 
1686
+ static const rb_data_type_t JSON_ParserConfig_type = {
1687
+ .wrap_struct_name = "JSON::Ext::Parser/ParserConfig",
1688
+ .function = {
1689
+ JSON_ParserConfig_mark,
1690
+ RUBY_DEFAULT_FREE,
1691
+ JSON_ParserConfig_memsize,
1692
+ },
1693
+ .flags = RUBY_TYPED_FREE_IMMEDIATELY | RUBY_TYPED_WB_PROTECTED | RUBY_TYPED_FROZEN_SHAREABLE | RUBY_TYPED_EMBEDDABLE,
1694
+ };
1695
+
1822
1696
  static VALUE cJSON_parser_s_allocate(VALUE klass)
1823
1697
  {
1824
- JSON_Parser *json = JSON_allocate();
1825
- return Data_Wrap_Struct(klass, JSON_mark, JSON_free, json);
1698
+ JSON_ParserConfig *config;
1699
+ return TypedData_Make_Struct(klass, JSON_ParserConfig, &JSON_ParserConfig_type, config);
1826
1700
  }
1827
1701
 
1828
- /*
1829
- * call-seq: source()
1830
- *
1831
- * Returns a copy of the current _source_ string, that was used to construct
1832
- * this Parser.
1833
- */
1834
- static VALUE cParser_source(VALUE self)
1702
+ void Init_parser(void)
1835
1703
  {
1836
- GET_STRUCT;
1837
- return rb_str_dup(json->Vsource);
1838
- }
1704
+ #ifdef HAVE_RB_EXT_RACTOR_SAFE
1705
+ rb_ext_ractor_safe(true);
1706
+ #endif
1839
1707
 
1840
- void Init_parser()
1841
- {
1708
+ #undef rb_intern
1842
1709
  rb_require("json/common");
1843
1710
  mJSON = rb_define_module("JSON");
1844
- mExt = rb_define_module_under(mJSON, "Ext");
1845
- cParser = rb_define_class_under(mExt, "Parser", rb_cObject);
1846
- eParserError = rb_path2class("JSON::ParserError");
1711
+ VALUE mExt = rb_define_module_under(mJSON, "Ext");
1712
+ VALUE cParserConfig = rb_define_class_under(mExt, "ParserConfig", rb_cObject);
1847
1713
  eNestingError = rb_path2class("JSON::NestingError");
1848
- rb_define_alloc_func(cParser, cJSON_parser_s_allocate);
1849
- rb_define_method(cParser, "initialize", cParser_initialize, -1);
1850
- rb_define_method(cParser, "parse", cParser_parse, 0);
1851
- rb_define_method(cParser, "source", cParser_source, 0);
1714
+ rb_gc_register_mark_object(eNestingError);
1715
+ rb_define_alloc_func(cParserConfig, cJSON_parser_s_allocate);
1716
+ rb_define_method(cParserConfig, "initialize", cParserConfig_initialize, 1);
1717
+ rb_define_method(cParserConfig, "parse", cParserConfig_parse, 1);
1718
+
1719
+ VALUE cParser = rb_define_class_under(mExt, "Parser", rb_cObject);
1720
+ rb_define_singleton_method(cParser, "parse", cParser_m_parse, 2);
1852
1721
 
1853
1722
  CNaN = rb_const_get(mJSON, rb_intern("NaN"));
1723
+ rb_gc_register_mark_object(CNaN);
1724
+
1854
1725
  CInfinity = rb_const_get(mJSON, rb_intern("Infinity"));
1855
- CMinusInfinity = rb_const_get(mJSON, rb_intern("MinusInfinity"));
1726
+ rb_gc_register_mark_object(CInfinity);
1856
1727
 
1857
- i_json_creatable_p = rb_intern("json_creatable?");
1858
- i_json_create = rb_intern("json_create");
1859
- i_create_id = rb_intern("create_id");
1860
- i_create_additions = rb_intern("create_additions");
1861
- i_chr = rb_intern("chr");
1862
- i_max_nesting = rb_intern("max_nesting");
1863
- i_allow_nan = rb_intern("allow_nan");
1864
- i_object_class = rb_intern("object_class");
1865
- i_array_class = rb_intern("array_class");
1866
- #ifdef HAVE_RUBY_ENCODING_H
1867
- mEncoding_UTF_8 = rb_funcall(rb_path2class("Encoding"), rb_intern("find"), 1, rb_str_new2("utf-8"));
1868
- mEncoding_UTF_16BE = rb_funcall(rb_path2class("Encoding"), rb_intern("find"), 1, rb_str_new2("utf-16be"));
1869
- mEncoding_UTF_16LE = rb_funcall(rb_path2class("Encoding"), rb_intern("find"), 1, rb_str_new2("utf-16le"));
1870
- mEncoding_UTF_32BE = rb_funcall(rb_path2class("Encoding"), rb_intern("find"), 1, rb_str_new2("utf-32be"));
1871
- mEncoding_UTF_32LE = rb_funcall(rb_path2class("Encoding"), rb_intern("find"), 1, rb_str_new2("utf-32le"));
1872
- mEncoding_ASCII_8BIT = rb_funcall(rb_path2class("Encoding"), rb_intern("find"), 1, rb_str_new2("ascii-8bit"));
1873
- i_encoding = rb_intern("encoding");
1728
+ CMinusInfinity = rb_const_get(mJSON, rb_intern("MinusInfinity"));
1729
+ rb_gc_register_mark_object(CMinusInfinity);
1730
+
1731
+ rb_global_variable(&Encoding_UTF_8);
1732
+ Encoding_UTF_8 = rb_const_get(rb_path2class("Encoding"), rb_intern("UTF_8"));
1733
+
1734
+ sym_max_nesting = ID2SYM(rb_intern("max_nesting"));
1735
+ sym_allow_nan = ID2SYM(rb_intern("allow_nan"));
1736
+ sym_allow_trailing_comma = ID2SYM(rb_intern("allow_trailing_comma"));
1737
+ sym_allow_control_characters = ID2SYM(rb_intern("allow_control_characters"));
1738
+ sym_allow_invalid_escape = ID2SYM(rb_intern("allow_invalid_escape"));
1739
+ sym_symbolize_names = ID2SYM(rb_intern("symbolize_names"));
1740
+ sym_freeze = ID2SYM(rb_intern("freeze"));
1741
+ sym_on_load = ID2SYM(rb_intern("on_load"));
1742
+ sym_decimal_class = ID2SYM(rb_intern("decimal_class"));
1743
+ sym_allow_duplicate_key = ID2SYM(rb_intern("allow_duplicate_key"));
1744
+
1745
+ i_new = rb_intern("new");
1746
+ i_try_convert = rb_intern("try_convert");
1747
+ i_uminus = rb_intern("-@");
1874
1748
  i_encode = rb_intern("encode");
1875
- i_encode_bang = rb_intern("encode!");
1876
- i_force_encoding = rb_intern("force_encoding");
1877
- #else
1878
- i_iconv = rb_intern("iconv");
1749
+
1750
+ binary_encindex = rb_ascii8bit_encindex();
1751
+ utf8_encindex = rb_utf8_encindex();
1752
+ enc_utf8 = rb_utf8_encoding();
1753
+
1754
+ #ifdef HAVE_SIMD
1755
+ simd_impl = find_simd_implementation();
1879
1756
  #endif
1880
1757
  }