json 2.6.1 → 2.21.1

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 (51) hide show
  1. checksums.yaml +4 -4
  2. data/BSDL +22 -0
  3. data/CHANGES.md +370 -17
  4. data/LEGAL +20 -0
  5. data/README.md +96 -213
  6. data/ext/json/ext/fbuffer/fbuffer.h +190 -118
  7. data/ext/json/ext/generator/extconf.rb +17 -2
  8. data/ext/json/ext/generator/generator.c +1532 -1074
  9. data/ext/json/ext/json.h +183 -0
  10. data/ext/json/ext/parser/extconf.rb +30 -25
  11. data/ext/json/ext/parser/parser.c +2837 -3258
  12. data/ext/json/ext/simd/conf.rb +24 -0
  13. data/ext/json/ext/simd/simd.h +208 -0
  14. data/ext/json/ext/vendor/fast_float_parser.h +814 -0
  15. data/ext/json/ext/vendor/fpconv.c +480 -0
  16. data/ext/json/ext/vendor/jeaiii-ltoa.h +267 -0
  17. data/json.gemspec +48 -53
  18. data/lib/json/add/bigdecimal.rb +39 -10
  19. data/lib/json/add/complex.rb +29 -6
  20. data/lib/json/add/core.rb +2 -1
  21. data/lib/json/add/date.rb +27 -7
  22. data/lib/json/add/date_time.rb +26 -9
  23. data/lib/json/add/exception.rb +25 -7
  24. data/lib/json/add/ostruct.rb +32 -9
  25. data/lib/json/add/range.rb +33 -8
  26. data/lib/json/add/rational.rb +28 -6
  27. data/lib/json/add/regexp.rb +26 -8
  28. data/lib/json/add/set.rb +25 -6
  29. data/lib/json/add/string.rb +35 -0
  30. data/lib/json/add/struct.rb +29 -7
  31. data/lib/json/add/symbol.rb +34 -7
  32. data/lib/json/add/time.rb +29 -15
  33. data/lib/json/common.rb +746 -267
  34. data/lib/json/ext/generator/state.rb +104 -0
  35. data/lib/json/ext.rb +61 -5
  36. data/lib/json/generic_object.rb +7 -11
  37. data/lib/json/truffle_ruby/generator.rb +790 -0
  38. data/lib/json/version.rb +3 -7
  39. data/lib/json.rb +134 -24
  40. metadata +22 -26
  41. data/VERSION +0 -1
  42. data/ext/json/ext/generator/depend +0 -1
  43. data/ext/json/ext/generator/generator.h +0 -174
  44. data/ext/json/ext/parser/depend +0 -1
  45. data/ext/json/ext/parser/parser.h +0 -96
  46. data/ext/json/ext/parser/parser.rl +0 -977
  47. data/ext/json/extconf.rb +0 -3
  48. data/lib/json/pure/generator.rb +0 -479
  49. data/lib/json/pure/parser.rb +0 -337
  50. data/lib/json/pure.rb +0 -15
  51. /data/{LICENSE → COPYING} +0 -0
@@ -1,977 +0,0 @@
1
- #include "../fbuffer/fbuffer.h"
2
- #include "parser.h"
3
-
4
- #if defined HAVE_RUBY_ENCODING_H
5
- # define EXC_ENCODING rb_utf8_encoding(),
6
- # ifndef HAVE_RB_ENC_RAISE
7
- static void
8
- enc_raise(rb_encoding *enc, VALUE exc, const char *fmt, ...)
9
- {
10
- va_list args;
11
- VALUE mesg;
12
-
13
- va_start(args, fmt);
14
- mesg = rb_enc_vsprintf(enc, fmt, args);
15
- va_end(args);
16
-
17
- rb_exc_raise(rb_exc_new3(exc, mesg));
18
- }
19
- # define rb_enc_raise enc_raise
20
- # endif
21
- #else
22
- # define EXC_ENCODING /* nothing */
23
- # define rb_enc_raise rb_raise
24
- #endif
25
-
26
- /* unicode */
27
-
28
- static const signed char digit_values[256] = {
29
- -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
30
- -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
31
- -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, -1,
32
- -1, -1, -1, -1, -1, -1, 10, 11, 12, 13, 14, 15, -1, -1, -1, -1, -1, -1, -1,
33
- -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
34
- 10, 11, 12, 13, 14, 15, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
35
- -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
36
- -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
37
- -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
38
- -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
39
- -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
40
- -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
41
- -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
42
- -1, -1, -1, -1, -1, -1, -1
43
- };
44
-
45
- static UTF32 unescape_unicode(const unsigned char *p)
46
- {
47
- signed char b;
48
- UTF32 result = 0;
49
- b = digit_values[p[0]];
50
- if (b < 0) return UNI_REPLACEMENT_CHAR;
51
- result = (result << 4) | (unsigned char)b;
52
- b = digit_values[p[1]];
53
- if (b < 0) return UNI_REPLACEMENT_CHAR;
54
- result = (result << 4) | (unsigned char)b;
55
- b = digit_values[p[2]];
56
- if (b < 0) return UNI_REPLACEMENT_CHAR;
57
- result = (result << 4) | (unsigned char)b;
58
- b = digit_values[p[3]];
59
- if (b < 0) return UNI_REPLACEMENT_CHAR;
60
- result = (result << 4) | (unsigned char)b;
61
- return result;
62
- }
63
-
64
- static int convert_UTF32_to_UTF8(char *buf, UTF32 ch)
65
- {
66
- int len = 1;
67
- if (ch <= 0x7F) {
68
- buf[0] = (char) ch;
69
- } else if (ch <= 0x07FF) {
70
- buf[0] = (char) ((ch >> 6) | 0xC0);
71
- buf[1] = (char) ((ch & 0x3F) | 0x80);
72
- len++;
73
- } else if (ch <= 0xFFFF) {
74
- buf[0] = (char) ((ch >> 12) | 0xE0);
75
- buf[1] = (char) (((ch >> 6) & 0x3F) | 0x80);
76
- buf[2] = (char) ((ch & 0x3F) | 0x80);
77
- len += 2;
78
- } else if (ch <= 0x1fffff) {
79
- buf[0] =(char) ((ch >> 18) | 0xF0);
80
- buf[1] =(char) (((ch >> 12) & 0x3F) | 0x80);
81
- buf[2] =(char) (((ch >> 6) & 0x3F) | 0x80);
82
- buf[3] =(char) ((ch & 0x3F) | 0x80);
83
- len += 3;
84
- } else {
85
- buf[0] = '?';
86
- }
87
- return len;
88
- }
89
-
90
- static VALUE mJSON, mExt, cParser, eParserError, eNestingError;
91
- static VALUE CNaN, CInfinity, CMinusInfinity;
92
-
93
- static ID i_json_creatable_p, i_json_create, i_create_id, i_create_additions,
94
- i_chr, i_max_nesting, i_allow_nan, i_symbolize_names,
95
- i_object_class, i_array_class, i_decimal_class, i_key_p,
96
- i_deep_const_get, i_match, i_match_string, i_aset, i_aref,
97
- i_leftshift, i_new, i_try_convert, i_freeze, i_uminus;
98
-
99
- %%{
100
- machine JSON_common;
101
-
102
- cr = '\n';
103
- cr_neg = [^\n];
104
- ws = [ \t\r\n];
105
- c_comment = '/*' ( any* - (any* '*/' any* ) ) '*/';
106
- cpp_comment = '//' cr_neg* cr;
107
- comment = c_comment | cpp_comment;
108
- ignore = ws | comment;
109
- name_separator = ':';
110
- value_separator = ',';
111
- Vnull = 'null';
112
- Vfalse = 'false';
113
- Vtrue = 'true';
114
- VNaN = 'NaN';
115
- VInfinity = 'Infinity';
116
- VMinusInfinity = '-Infinity';
117
- begin_value = [nft\"\-\[\{NI] | digit;
118
- begin_object = '{';
119
- end_object = '}';
120
- begin_array = '[';
121
- end_array = ']';
122
- begin_string = '"';
123
- begin_name = begin_string;
124
- begin_number = digit | '-';
125
- }%%
126
-
127
- %%{
128
- machine JSON_object;
129
- include JSON_common;
130
-
131
- write data;
132
-
133
- action parse_value {
134
- VALUE v = Qnil;
135
- char *np = JSON_parse_value(json, fpc, pe, &v, current_nesting);
136
- if (np == NULL) {
137
- fhold; fbreak;
138
- } else {
139
- if (NIL_P(json->object_class)) {
140
- OBJ_FREEZE(last_name);
141
- rb_hash_aset(*result, last_name, v);
142
- } else {
143
- rb_funcall(*result, i_aset, 2, last_name, v);
144
- }
145
- fexec np;
146
- }
147
- }
148
-
149
- action parse_name {
150
- char *np;
151
- json->parsing_name = 1;
152
- np = JSON_parse_string(json, fpc, pe, &last_name);
153
- json->parsing_name = 0;
154
- if (np == NULL) { fhold; fbreak; } else fexec np;
155
- }
156
-
157
- action exit { fhold; fbreak; }
158
-
159
- pair = ignore* begin_name >parse_name ignore* name_separator ignore* begin_value >parse_value;
160
- next_pair = ignore* value_separator pair;
161
-
162
- main := (
163
- begin_object
164
- (pair (next_pair)*)? ignore*
165
- end_object
166
- ) @exit;
167
- }%%
168
-
169
- static char *JSON_parse_object(JSON_Parser *json, char *p, char *pe, VALUE *result, int current_nesting)
170
- {
171
- int cs = EVIL;
172
- VALUE last_name = Qnil;
173
- VALUE object_class = json->object_class;
174
-
175
- if (json->max_nesting && current_nesting > json->max_nesting) {
176
- rb_raise(eNestingError, "nesting of %d is too deep", current_nesting);
177
- }
178
-
179
- *result = NIL_P(object_class) ? rb_hash_new() : rb_class_new_instance(0, 0, object_class);
180
-
181
- %% write init;
182
- %% write exec;
183
-
184
- if (cs >= JSON_object_first_final) {
185
- if (json->create_additions) {
186
- VALUE klassname;
187
- if (NIL_P(json->object_class)) {
188
- klassname = rb_hash_aref(*result, json->create_id);
189
- } else {
190
- klassname = rb_funcall(*result, i_aref, 1, json->create_id);
191
- }
192
- if (!NIL_P(klassname)) {
193
- VALUE klass = rb_funcall(mJSON, i_deep_const_get, 1, klassname);
194
- if (RTEST(rb_funcall(klass, i_json_creatable_p, 0))) {
195
- *result = rb_funcall(klass, i_json_create, 1, *result);
196
- }
197
- }
198
- }
199
- return p + 1;
200
- } else {
201
- return NULL;
202
- }
203
- }
204
-
205
-
206
- %%{
207
- machine JSON_value;
208
- include JSON_common;
209
-
210
- write data;
211
-
212
- action parse_null {
213
- *result = Qnil;
214
- }
215
- action parse_false {
216
- *result = Qfalse;
217
- }
218
- action parse_true {
219
- *result = Qtrue;
220
- }
221
- action parse_nan {
222
- if (json->allow_nan) {
223
- *result = CNaN;
224
- } else {
225
- rb_enc_raise(EXC_ENCODING eParserError, "%u: unexpected token at '%s'", __LINE__, p - 2);
226
- }
227
- }
228
- action parse_infinity {
229
- if (json->allow_nan) {
230
- *result = CInfinity;
231
- } else {
232
- rb_enc_raise(EXC_ENCODING eParserError, "%u: unexpected token at '%s'", __LINE__, p - 8);
233
- }
234
- }
235
- action parse_string {
236
- char *np = JSON_parse_string(json, fpc, pe, result);
237
- if (np == NULL) { fhold; fbreak; } else fexec np;
238
- }
239
-
240
- action parse_number {
241
- char *np;
242
- if(pe > fpc + 8 && !strncmp(MinusInfinity, fpc, 9)) {
243
- if (json->allow_nan) {
244
- *result = CMinusInfinity;
245
- fexec p + 10;
246
- fhold; fbreak;
247
- } else {
248
- rb_enc_raise(EXC_ENCODING eParserError, "%u: unexpected token at '%s'", __LINE__, p);
249
- }
250
- }
251
- np = JSON_parse_float(json, fpc, pe, result);
252
- if (np != NULL) fexec np;
253
- np = JSON_parse_integer(json, fpc, pe, result);
254
- if (np != NULL) fexec np;
255
- fhold; fbreak;
256
- }
257
-
258
- action parse_array {
259
- char *np;
260
- np = JSON_parse_array(json, fpc, pe, result, current_nesting + 1);
261
- if (np == NULL) { fhold; fbreak; } else fexec np;
262
- }
263
-
264
- action parse_object {
265
- char *np;
266
- np = JSON_parse_object(json, fpc, pe, result, current_nesting + 1);
267
- if (np == NULL) { fhold; fbreak; } else fexec np;
268
- }
269
-
270
- action exit { fhold; fbreak; }
271
-
272
- main := ignore* (
273
- Vnull @parse_null |
274
- Vfalse @parse_false |
275
- Vtrue @parse_true |
276
- VNaN @parse_nan |
277
- VInfinity @parse_infinity |
278
- begin_number >parse_number |
279
- begin_string >parse_string |
280
- begin_array >parse_array |
281
- begin_object >parse_object
282
- ) ignore* %*exit;
283
- }%%
284
-
285
- static char *JSON_parse_value(JSON_Parser *json, char *p, char *pe, VALUE *result, int current_nesting)
286
- {
287
- int cs = EVIL;
288
-
289
- %% write init;
290
- %% write exec;
291
-
292
- if (json->freeze) {
293
- OBJ_FREEZE(*result);
294
- }
295
-
296
- if (cs >= JSON_value_first_final) {
297
- return p;
298
- } else {
299
- return NULL;
300
- }
301
- }
302
-
303
- %%{
304
- machine JSON_integer;
305
-
306
- write data;
307
-
308
- action exit { fhold; fbreak; }
309
-
310
- main := '-'? ('0' | [1-9][0-9]*) (^[0-9]? @exit);
311
- }%%
312
-
313
- static char *JSON_parse_integer(JSON_Parser *json, char *p, char *pe, VALUE *result)
314
- {
315
- int cs = EVIL;
316
-
317
- %% write init;
318
- json->memo = p;
319
- %% write exec;
320
-
321
- if (cs >= JSON_integer_first_final) {
322
- long len = p - json->memo;
323
- fbuffer_clear(json->fbuffer);
324
- fbuffer_append(json->fbuffer, json->memo, len);
325
- fbuffer_append_char(json->fbuffer, '\0');
326
- *result = rb_cstr2inum(FBUFFER_PTR(json->fbuffer), 10);
327
- return p + 1;
328
- } else {
329
- return NULL;
330
- }
331
- }
332
-
333
- %%{
334
- machine JSON_float;
335
- include JSON_common;
336
-
337
- write data;
338
-
339
- action exit { fhold; fbreak; }
340
-
341
- main := '-'? (
342
- (('0' | [1-9][0-9]*) '.' [0-9]+ ([Ee] [+\-]?[0-9]+)?)
343
- | (('0' | [1-9][0-9]*) ([Ee] [+\-]?[0-9]+))
344
- ) (^[0-9Ee.\-]? @exit );
345
- }%%
346
-
347
- static char *JSON_parse_float(JSON_Parser *json, char *p, char *pe, VALUE *result)
348
- {
349
- int cs = EVIL;
350
-
351
- %% write init;
352
- json->memo = p;
353
- %% write exec;
354
-
355
- if (cs >= JSON_float_first_final) {
356
- VALUE mod = Qnil;
357
- ID method_id = 0;
358
- if (rb_respond_to(json->decimal_class, i_try_convert)) {
359
- mod = json->decimal_class;
360
- method_id = i_try_convert;
361
- } else if (rb_respond_to(json->decimal_class, i_new)) {
362
- mod = json->decimal_class;
363
- method_id = i_new;
364
- } else if (RB_TYPE_P(json->decimal_class, T_CLASS)) {
365
- VALUE name = rb_class_name(json->decimal_class);
366
- const char *name_cstr = RSTRING_PTR(name);
367
- const char *last_colon = strrchr(name_cstr, ':');
368
- if (last_colon) {
369
- const char *mod_path_end = last_colon - 1;
370
- VALUE mod_path = rb_str_substr(name, 0, mod_path_end - name_cstr);
371
- mod = rb_path_to_class(mod_path);
372
-
373
- const char *method_name_beg = last_colon + 1;
374
- long before_len = method_name_beg - name_cstr;
375
- long len = RSTRING_LEN(name) - before_len;
376
- VALUE method_name = rb_str_substr(name, before_len, len);
377
- method_id = SYM2ID(rb_str_intern(method_name));
378
- } else {
379
- mod = rb_mKernel;
380
- method_id = SYM2ID(rb_str_intern(name));
381
- }
382
- }
383
-
384
- long len = p - json->memo;
385
- fbuffer_clear(json->fbuffer);
386
- fbuffer_append(json->fbuffer, json->memo, len);
387
- fbuffer_append_char(json->fbuffer, '\0');
388
-
389
- if (method_id) {
390
- VALUE text = rb_str_new2(FBUFFER_PTR(json->fbuffer));
391
- *result = rb_funcallv(mod, method_id, 1, &text);
392
- } else {
393
- *result = DBL2NUM(rb_cstr_to_dbl(FBUFFER_PTR(json->fbuffer), 1));
394
- }
395
-
396
- return p + 1;
397
- } else {
398
- return NULL;
399
- }
400
- }
401
-
402
-
403
- %%{
404
- machine JSON_array;
405
- include JSON_common;
406
-
407
- write data;
408
-
409
- action parse_value {
410
- VALUE v = Qnil;
411
- char *np = JSON_parse_value(json, fpc, pe, &v, current_nesting);
412
- if (np == NULL) {
413
- fhold; fbreak;
414
- } else {
415
- if (NIL_P(json->array_class)) {
416
- rb_ary_push(*result, v);
417
- } else {
418
- rb_funcall(*result, i_leftshift, 1, v);
419
- }
420
- fexec np;
421
- }
422
- }
423
-
424
- action exit { fhold; fbreak; }
425
-
426
- next_element = value_separator ignore* begin_value >parse_value;
427
-
428
- main := begin_array ignore*
429
- ((begin_value >parse_value ignore*)
430
- (ignore* next_element ignore*)*)?
431
- end_array @exit;
432
- }%%
433
-
434
- static char *JSON_parse_array(JSON_Parser *json, char *p, char *pe, VALUE *result, int current_nesting)
435
- {
436
- int cs = EVIL;
437
- VALUE array_class = json->array_class;
438
-
439
- if (json->max_nesting && current_nesting > json->max_nesting) {
440
- rb_raise(eNestingError, "nesting of %d is too deep", current_nesting);
441
- }
442
- *result = NIL_P(array_class) ? rb_ary_new() : rb_class_new_instance(0, 0, array_class);
443
-
444
- %% write init;
445
- %% write exec;
446
-
447
- if(cs >= JSON_array_first_final) {
448
- return p + 1;
449
- } else {
450
- rb_enc_raise(EXC_ENCODING eParserError, "%u: unexpected token at '%s'", __LINE__, p);
451
- return NULL;
452
- }
453
- }
454
-
455
- static const size_t MAX_STACK_BUFFER_SIZE = 128;
456
- static VALUE json_string_unescape(char *string, char *stringEnd, int intern, int symbolize)
457
- {
458
- VALUE result = Qnil;
459
- size_t bufferSize = stringEnd - string;
460
- char *p = string, *pe = string, *unescape, *bufferStart, *buffer;
461
- int unescape_len;
462
- char buf[4];
463
-
464
- if (bufferSize > MAX_STACK_BUFFER_SIZE) {
465
- bufferStart = buffer = ALLOC_N(char, bufferSize);
466
- } else {
467
- bufferStart = buffer = ALLOCA_N(char, bufferSize);
468
- }
469
-
470
- while (pe < stringEnd) {
471
- if (*pe == '\\') {
472
- unescape = (char *) "?";
473
- unescape_len = 1;
474
- if (pe > p) {
475
- MEMCPY(buffer, p, char, pe - p);
476
- buffer += pe - p;
477
- }
478
- switch (*++pe) {
479
- case 'n':
480
- unescape = (char *) "\n";
481
- break;
482
- case 'r':
483
- unescape = (char *) "\r";
484
- break;
485
- case 't':
486
- unescape = (char *) "\t";
487
- break;
488
- case '"':
489
- unescape = (char *) "\"";
490
- break;
491
- case '\\':
492
- unescape = (char *) "\\";
493
- break;
494
- case 'b':
495
- unescape = (char *) "\b";
496
- break;
497
- case 'f':
498
- unescape = (char *) "\f";
499
- break;
500
- case 'u':
501
- if (pe > stringEnd - 4) {
502
- if (bufferSize > MAX_STACK_BUFFER_SIZE) {
503
- free(bufferStart);
504
- }
505
- rb_enc_raise(
506
- EXC_ENCODING eParserError,
507
- "%u: incomplete unicode character escape sequence at '%s'", __LINE__, p
508
- );
509
- } else {
510
- UTF32 ch = unescape_unicode((unsigned char *) ++pe);
511
- pe += 3;
512
- if (UNI_SUR_HIGH_START == (ch & 0xFC00)) {
513
- pe++;
514
- if (pe > stringEnd - 6) {
515
- if (bufferSize > MAX_STACK_BUFFER_SIZE) {
516
- free(bufferStart);
517
- }
518
- rb_enc_raise(
519
- EXC_ENCODING eParserError,
520
- "%u: incomplete surrogate pair at '%s'", __LINE__, p
521
- );
522
- }
523
- if (pe[0] == '\\' && pe[1] == 'u') {
524
- UTF32 sur = unescape_unicode((unsigned char *) pe + 2);
525
- ch = (((ch & 0x3F) << 10) | ((((ch >> 6) & 0xF) + 1) << 16)
526
- | (sur & 0x3FF));
527
- pe += 5;
528
- } else {
529
- unescape = (char *) "?";
530
- break;
531
- }
532
- }
533
- unescape_len = convert_UTF32_to_UTF8(buf, ch);
534
- unescape = buf;
535
- }
536
- break;
537
- default:
538
- p = pe;
539
- continue;
540
- }
541
- MEMCPY(buffer, unescape, char, unescape_len);
542
- buffer += unescape_len;
543
- p = ++pe;
544
- } else {
545
- pe++;
546
- }
547
- }
548
-
549
- if (pe > p) {
550
- MEMCPY(buffer, p, char, pe - p);
551
- buffer += pe - p;
552
- }
553
-
554
- # ifdef HAVE_RB_ENC_INTERNED_STR
555
- if (intern) {
556
- result = rb_enc_interned_str(bufferStart, (long)(buffer - bufferStart), rb_utf8_encoding());
557
- } else {
558
- result = rb_utf8_str_new(bufferStart, (long)(buffer - bufferStart));
559
- }
560
- if (bufferSize > MAX_STACK_BUFFER_SIZE) {
561
- free(bufferStart);
562
- }
563
- # else
564
- result = rb_utf8_str_new(bufferStart, (long)(buffer - bufferStart));
565
-
566
- if (bufferSize > MAX_STACK_BUFFER_SIZE) {
567
- free(bufferStart);
568
- }
569
-
570
- if (intern) {
571
- # if STR_UMINUS_DEDUPE_FROZEN
572
- // Starting from MRI 2.8 it is preferable to freeze the string
573
- // before deduplication so that it can be interned directly
574
- // otherwise it would be duplicated first which is wasteful.
575
- result = rb_funcall(rb_str_freeze(result), i_uminus, 0);
576
- # elif STR_UMINUS_DEDUPE
577
- // MRI 2.5 and older do not deduplicate strings that are already
578
- // frozen.
579
- result = rb_funcall(result, i_uminus, 0);
580
- # else
581
- result = rb_str_freeze(result);
582
- # endif
583
- }
584
- # endif
585
-
586
- if (symbolize) {
587
- result = rb_str_intern(result);
588
- }
589
-
590
- return result;
591
- }
592
-
593
- %%{
594
- machine JSON_string;
595
- include JSON_common;
596
-
597
- write data;
598
-
599
- action parse_string {
600
- *result = json_string_unescape(json->memo + 1, p, json->parsing_name || json-> freeze, json->parsing_name && json->symbolize_names);
601
- if (NIL_P(*result)) {
602
- fhold;
603
- fbreak;
604
- } else {
605
- fexec p + 1;
606
- }
607
- }
608
-
609
- action exit { fhold; fbreak; }
610
-
611
- main := '"' ((^([\"\\] | 0..0x1f) | '\\'[\"\\/bfnrt] | '\\u'[0-9a-fA-F]{4} | '\\'^([\"\\/bfnrtu]|0..0x1f))* %parse_string) '"' @exit;
612
- }%%
613
-
614
- static int
615
- match_i(VALUE regexp, VALUE klass, VALUE memo)
616
- {
617
- if (regexp == Qundef) return ST_STOP;
618
- if (RTEST(rb_funcall(klass, i_json_creatable_p, 0)) &&
619
- RTEST(rb_funcall(regexp, i_match, 1, rb_ary_entry(memo, 0)))) {
620
- rb_ary_push(memo, klass);
621
- return ST_STOP;
622
- }
623
- return ST_CONTINUE;
624
- }
625
-
626
- static char *JSON_parse_string(JSON_Parser *json, char *p, char *pe, VALUE *result)
627
- {
628
- int cs = EVIL;
629
- VALUE match_string;
630
-
631
- %% write init;
632
- json->memo = p;
633
- %% write exec;
634
-
635
- if (json->create_additions && RTEST(match_string = json->match_string)) {
636
- VALUE klass;
637
- VALUE memo = rb_ary_new2(2);
638
- rb_ary_push(memo, *result);
639
- rb_hash_foreach(match_string, match_i, memo);
640
- klass = rb_ary_entry(memo, 1);
641
- if (RTEST(klass)) {
642
- *result = rb_funcall(klass, i_json_create, 1, *result);
643
- }
644
- }
645
-
646
- if (cs >= JSON_string_first_final) {
647
- return p + 1;
648
- } else {
649
- return NULL;
650
- }
651
- }
652
-
653
- /*
654
- * Document-class: JSON::Ext::Parser
655
- *
656
- * This is the JSON parser implemented as a C extension. It can be configured
657
- * to be used by setting
658
- *
659
- * JSON.parser = JSON::Ext::Parser
660
- *
661
- * with the method parser= in JSON.
662
- *
663
- */
664
-
665
- static VALUE convert_encoding(VALUE source)
666
- {
667
- #ifdef HAVE_RUBY_ENCODING_H
668
- rb_encoding *enc = rb_enc_get(source);
669
- if (enc == rb_ascii8bit_encoding()) {
670
- if (OBJ_FROZEN(source)) {
671
- source = rb_str_dup(source);
672
- }
673
- FORCE_UTF8(source);
674
- } else {
675
- source = rb_str_conv_enc(source, rb_enc_get(source), rb_utf8_encoding());
676
- }
677
- #endif
678
- return source;
679
- }
680
-
681
- /*
682
- * call-seq: new(source, opts => {})
683
- *
684
- * Creates a new JSON::Ext::Parser instance for the string _source_.
685
- *
686
- * Creates a new JSON::Ext::Parser instance for the string _source_.
687
- *
688
- * It will be configured by the _opts_ hash. _opts_ can have the following
689
- * keys:
690
- *
691
- * _opts_ can have the following keys:
692
- * * *max_nesting*: The maximum depth of nesting allowed in the parsed data
693
- * structures. Disable depth checking with :max_nesting => false|nil|0, it
694
- * defaults to 100.
695
- * * *allow_nan*: If set to true, allow NaN, Infinity and -Infinity in
696
- * defiance of RFC 4627 to be parsed by the Parser. This option defaults to
697
- * false.
698
- * * *symbolize_names*: If set to true, returns symbols for the names
699
- * (keys) in a JSON object. Otherwise strings are returned, which is
700
- * also the default. It's not possible to use this option in
701
- * conjunction with the *create_additions* option.
702
- * * *create_additions*: If set to false, the Parser doesn't create
703
- * additions even if a matching class and create_id was found. This option
704
- * defaults to false.
705
- * * *object_class*: Defaults to Hash
706
- * * *array_class*: Defaults to Array
707
- */
708
- static VALUE cParser_initialize(int argc, VALUE *argv, VALUE self)
709
- {
710
- VALUE source, opts;
711
- GET_PARSER_INIT;
712
-
713
- if (json->Vsource) {
714
- rb_raise(rb_eTypeError, "already initialized instance");
715
- }
716
- #ifdef HAVE_RB_SCAN_ARGS_OPTIONAL_HASH
717
- rb_scan_args(argc, argv, "1:", &source, &opts);
718
- #else
719
- rb_scan_args(argc, argv, "11", &source, &opts);
720
- #endif
721
- if (!NIL_P(opts)) {
722
- #ifndef HAVE_RB_SCAN_ARGS_OPTIONAL_HASH
723
- opts = rb_convert_type(opts, T_HASH, "Hash", "to_hash");
724
- if (NIL_P(opts)) {
725
- rb_raise(rb_eArgError, "opts needs to be like a hash");
726
- } else {
727
- #endif
728
- VALUE tmp = ID2SYM(i_max_nesting);
729
- if (option_given_p(opts, tmp)) {
730
- VALUE max_nesting = rb_hash_aref(opts, tmp);
731
- if (RTEST(max_nesting)) {
732
- Check_Type(max_nesting, T_FIXNUM);
733
- json->max_nesting = FIX2INT(max_nesting);
734
- } else {
735
- json->max_nesting = 0;
736
- }
737
- } else {
738
- json->max_nesting = 100;
739
- }
740
- tmp = ID2SYM(i_allow_nan);
741
- if (option_given_p(opts, tmp)) {
742
- json->allow_nan = RTEST(rb_hash_aref(opts, tmp)) ? 1 : 0;
743
- } else {
744
- json->allow_nan = 0;
745
- }
746
- tmp = ID2SYM(i_symbolize_names);
747
- if (option_given_p(opts, tmp)) {
748
- json->symbolize_names = RTEST(rb_hash_aref(opts, tmp)) ? 1 : 0;
749
- } else {
750
- json->symbolize_names = 0;
751
- }
752
- tmp = ID2SYM(i_freeze);
753
- if (option_given_p(opts, tmp)) {
754
- json->freeze = RTEST(rb_hash_aref(opts, tmp)) ? 1 : 0;
755
- } else {
756
- json->freeze = 0;
757
- }
758
- tmp = ID2SYM(i_create_additions);
759
- if (option_given_p(opts, tmp)) {
760
- json->create_additions = RTEST(rb_hash_aref(opts, tmp));
761
- } else {
762
- json->create_additions = 0;
763
- }
764
- if (json->symbolize_names && json->create_additions) {
765
- rb_raise(rb_eArgError,
766
- "options :symbolize_names and :create_additions cannot be "
767
- " used in conjunction");
768
- }
769
- tmp = ID2SYM(i_create_id);
770
- if (option_given_p(opts, tmp)) {
771
- json->create_id = rb_hash_aref(opts, tmp);
772
- } else {
773
- json->create_id = rb_funcall(mJSON, i_create_id, 0);
774
- }
775
- tmp = ID2SYM(i_object_class);
776
- if (option_given_p(opts, tmp)) {
777
- json->object_class = rb_hash_aref(opts, tmp);
778
- } else {
779
- json->object_class = Qnil;
780
- }
781
- tmp = ID2SYM(i_array_class);
782
- if (option_given_p(opts, tmp)) {
783
- json->array_class = rb_hash_aref(opts, tmp);
784
- } else {
785
- json->array_class = Qnil;
786
- }
787
- tmp = ID2SYM(i_decimal_class);
788
- if (option_given_p(opts, tmp)) {
789
- json->decimal_class = rb_hash_aref(opts, tmp);
790
- } else {
791
- json->decimal_class = Qnil;
792
- }
793
- tmp = ID2SYM(i_match_string);
794
- if (option_given_p(opts, tmp)) {
795
- VALUE match_string = rb_hash_aref(opts, tmp);
796
- json->match_string = RTEST(match_string) ? match_string : Qnil;
797
- } else {
798
- json->match_string = Qnil;
799
- }
800
- #ifndef HAVE_RB_SCAN_ARGS_OPTIONAL_HASH
801
- }
802
- #endif
803
- } else {
804
- json->max_nesting = 100;
805
- json->allow_nan = 0;
806
- json->create_additions = 0;
807
- json->create_id = rb_funcall(mJSON, i_create_id, 0);
808
- json->object_class = Qnil;
809
- json->array_class = Qnil;
810
- json->decimal_class = Qnil;
811
- }
812
- source = convert_encoding(StringValue(source));
813
- StringValue(source);
814
- json->len = RSTRING_LEN(source);
815
- json->source = RSTRING_PTR(source);;
816
- json->Vsource = source;
817
- return self;
818
- }
819
-
820
- %%{
821
- machine JSON;
822
-
823
- write data;
824
-
825
- include JSON_common;
826
-
827
- action parse_value {
828
- char *np = JSON_parse_value(json, fpc, pe, &result, 0);
829
- if (np == NULL) { fhold; fbreak; } else fexec np;
830
- }
831
-
832
- main := ignore* (
833
- begin_value >parse_value
834
- ) ignore*;
835
- }%%
836
-
837
- /*
838
- * call-seq: parse()
839
- *
840
- * Parses the current JSON text _source_ and returns the complete data
841
- * structure as a result.
842
- */
843
- static VALUE cParser_parse(VALUE self)
844
- {
845
- char *p, *pe;
846
- int cs = EVIL;
847
- VALUE result = Qnil;
848
- GET_PARSER;
849
-
850
- %% write init;
851
- p = json->source;
852
- pe = p + json->len;
853
- %% write exec;
854
-
855
- if (cs >= JSON_first_final && p == pe) {
856
- return result;
857
- } else {
858
- rb_enc_raise(EXC_ENCODING eParserError, "%u: unexpected token at '%s'", __LINE__, p);
859
- return Qnil;
860
- }
861
- }
862
-
863
- static void JSON_mark(void *ptr)
864
- {
865
- JSON_Parser *json = ptr;
866
- rb_gc_mark_maybe(json->Vsource);
867
- rb_gc_mark_maybe(json->create_id);
868
- rb_gc_mark_maybe(json->object_class);
869
- rb_gc_mark_maybe(json->array_class);
870
- rb_gc_mark_maybe(json->decimal_class);
871
- rb_gc_mark_maybe(json->match_string);
872
- }
873
-
874
- static void JSON_free(void *ptr)
875
- {
876
- JSON_Parser *json = ptr;
877
- fbuffer_free(json->fbuffer);
878
- ruby_xfree(json);
879
- }
880
-
881
- static size_t JSON_memsize(const void *ptr)
882
- {
883
- const JSON_Parser *json = ptr;
884
- return sizeof(*json) + FBUFFER_CAPA(json->fbuffer);
885
- }
886
-
887
- #ifdef NEW_TYPEDDATA_WRAPPER
888
- static const rb_data_type_t JSON_Parser_type = {
889
- "JSON/Parser",
890
- {JSON_mark, JSON_free, JSON_memsize,},
891
- #ifdef RUBY_TYPED_FREE_IMMEDIATELY
892
- 0, 0,
893
- RUBY_TYPED_FREE_IMMEDIATELY,
894
- #endif
895
- };
896
- #endif
897
-
898
- static VALUE cJSON_parser_s_allocate(VALUE klass)
899
- {
900
- JSON_Parser *json;
901
- VALUE obj = TypedData_Make_Struct(klass, JSON_Parser, &JSON_Parser_type, json);
902
- json->fbuffer = fbuffer_alloc(0);
903
- return obj;
904
- }
905
-
906
- /*
907
- * call-seq: source()
908
- *
909
- * Returns a copy of the current _source_ string, that was used to construct
910
- * this Parser.
911
- */
912
- static VALUE cParser_source(VALUE self)
913
- {
914
- GET_PARSER;
915
- return rb_str_dup(json->Vsource);
916
- }
917
-
918
- void Init_parser(void)
919
- {
920
- #ifdef HAVE_RB_EXT_RACTOR_SAFE
921
- rb_ext_ractor_safe(true);
922
- #endif
923
-
924
- #undef rb_intern
925
- rb_require("json/common");
926
- mJSON = rb_define_module("JSON");
927
- mExt = rb_define_module_under(mJSON, "Ext");
928
- cParser = rb_define_class_under(mExt, "Parser", rb_cObject);
929
- eParserError = rb_path2class("JSON::ParserError");
930
- eNestingError = rb_path2class("JSON::NestingError");
931
- rb_gc_register_mark_object(eParserError);
932
- rb_gc_register_mark_object(eNestingError);
933
- rb_define_alloc_func(cParser, cJSON_parser_s_allocate);
934
- rb_define_method(cParser, "initialize", cParser_initialize, -1);
935
- rb_define_method(cParser, "parse", cParser_parse, 0);
936
- rb_define_method(cParser, "source", cParser_source, 0);
937
-
938
- CNaN = rb_const_get(mJSON, rb_intern("NaN"));
939
- rb_gc_register_mark_object(CNaN);
940
-
941
- CInfinity = rb_const_get(mJSON, rb_intern("Infinity"));
942
- rb_gc_register_mark_object(CInfinity);
943
-
944
- CMinusInfinity = rb_const_get(mJSON, rb_intern("MinusInfinity"));
945
- rb_gc_register_mark_object(CMinusInfinity);
946
-
947
- i_json_creatable_p = rb_intern("json_creatable?");
948
- i_json_create = rb_intern("json_create");
949
- i_create_id = rb_intern("create_id");
950
- i_create_additions = rb_intern("create_additions");
951
- i_chr = rb_intern("chr");
952
- i_max_nesting = rb_intern("max_nesting");
953
- i_allow_nan = rb_intern("allow_nan");
954
- i_symbolize_names = rb_intern("symbolize_names");
955
- i_object_class = rb_intern("object_class");
956
- i_array_class = rb_intern("array_class");
957
- i_decimal_class = rb_intern("decimal_class");
958
- i_match = rb_intern("match");
959
- i_match_string = rb_intern("match_string");
960
- i_key_p = rb_intern("key?");
961
- i_deep_const_get = rb_intern("deep_const_get");
962
- i_aset = rb_intern("[]=");
963
- i_aref = rb_intern("[]");
964
- i_leftshift = rb_intern("<<");
965
- i_new = rb_intern("new");
966
- i_try_convert = rb_intern("try_convert");
967
- i_freeze = rb_intern("freeze");
968
- i_uminus = rb_intern("-@");
969
- }
970
-
971
- /*
972
- * Local variables:
973
- * mode: c
974
- * c-file-style: ruby
975
- * indent-tabs-mode: nil
976
- * End:
977
- */