json 2.19.8 → 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.
- checksums.yaml +4 -4
- data/CHANGES.md +27 -0
- data/LEGAL +3 -3
- data/README.md +5 -7
- data/ext/json/ext/fbuffer/fbuffer.h +22 -23
- data/ext/json/ext/generator/extconf.rb +1 -0
- data/ext/json/ext/generator/generator.c +80 -11
- data/ext/json/ext/json.h +67 -0
- data/ext/json/ext/parser/extconf.rb +17 -0
- data/ext/json/ext/parser/parser.c +1486 -326
- data/ext/json/ext/vendor/fast_float_parser.h +814 -0
- data/lib/json/common.rb +9 -0
- data/lib/json/ext/generator/state.rb +1 -0
- data/lib/json/ext.rb +26 -0
- data/lib/json/truffle_ruby/generator.rb +36 -1
- data/lib/json/version.rb +1 -1
- data/lib/json.rb +21 -3
- metadata +2 -2
- data/ext/json/ext/vendor/ryu.h +0 -819
|
@@ -1,15 +1,18 @@
|
|
|
1
1
|
#include "../json.h"
|
|
2
|
-
#include "../vendor/
|
|
2
|
+
#include "../vendor/fast_float_parser.h"
|
|
3
3
|
#include "../simd/simd.h"
|
|
4
4
|
|
|
5
|
-
static VALUE mJSON, eNestingError, Encoding_UTF_8;
|
|
6
|
-
static VALUE CNaN, CInfinity, CMinusInfinity;
|
|
5
|
+
static VALUE mJSON, eNestingError, eParserError, Encoding_UTF_8;
|
|
6
|
+
static VALUE CNaN, CInfinity, CMinusInfinity, JSON_empty_string;
|
|
7
7
|
|
|
8
|
-
static ID i_new, i_try_convert,
|
|
8
|
+
static ID i_new, i_try_convert, i_encode, i_at_line, i_at_column;
|
|
9
|
+
#ifndef HAVE_RB_STR_TO_INTERNED_STR
|
|
10
|
+
static ID i_uminus;
|
|
11
|
+
#endif
|
|
9
12
|
|
|
10
|
-
static VALUE sym_max_nesting, sym_allow_nan, sym_allow_trailing_comma,
|
|
11
|
-
sym_allow_invalid_escape, sym_symbolize_names,
|
|
12
|
-
sym_allow_duplicate_key;
|
|
13
|
+
static VALUE sym_max_nesting, sym_allow_nan, sym_allow_trailing_comma, sym_allow_comments,
|
|
14
|
+
sym_allow_control_characters, sym_allow_invalid_escape, sym_symbolize_names,
|
|
15
|
+
sym_freeze, sym_decimal_class, sym_on_load, sym_allow_duplicate_key;
|
|
13
16
|
|
|
14
17
|
static int binary_encindex;
|
|
15
18
|
static int utf8_encindex;
|
|
@@ -58,6 +61,20 @@ typedef struct rvalue_cache_struct {
|
|
|
58
61
|
VALUE entries[JSON_RVALUE_CACHE_CAPA];
|
|
59
62
|
} rvalue_cache;
|
|
60
63
|
|
|
64
|
+
static void rvalue_cache_mark(rvalue_cache *cache)
|
|
65
|
+
{
|
|
66
|
+
for (int index = 0; index < cache->length; index++) {
|
|
67
|
+
rb_gc_mark_movable(cache->entries[index]);
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
static void rvalue_cache_compact(rvalue_cache *cache)
|
|
72
|
+
{
|
|
73
|
+
for (int index = 0; index < cache->length; index++) {
|
|
74
|
+
cache->entries[index] = rb_gc_location(cache->entries[index]);
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
|
|
61
78
|
static rb_encoding *enc_utf8;
|
|
62
79
|
|
|
63
80
|
#define JSON_RVALUE_CACHE_MAX_ENTRY_LENGTH 55
|
|
@@ -206,12 +223,12 @@ static rvalue_stack *rvalue_stack_spill(rvalue_stack *old_stack, VALUE *handle,
|
|
|
206
223
|
|
|
207
224
|
static rvalue_stack *rvalue_stack_grow(rvalue_stack *stack, VALUE *handle, rvalue_stack **stack_ref)
|
|
208
225
|
{
|
|
209
|
-
long required = stack->capa * 2;
|
|
226
|
+
long required = stack->capa ? stack->capa * 2 : RVALUE_STACK_INITIAL_CAPA;
|
|
210
227
|
|
|
211
228
|
if (stack->type == RVALUE_STACK_STACK_ALLOCATED) {
|
|
212
229
|
stack = rvalue_stack_spill(stack, handle, stack_ref);
|
|
213
230
|
} else {
|
|
214
|
-
|
|
231
|
+
JSON_SIZED_REALLOC_N(stack->ptr, VALUE, required, stack->capa);
|
|
215
232
|
stack->capa = required;
|
|
216
233
|
}
|
|
217
234
|
return stack;
|
|
@@ -219,11 +236,15 @@ static rvalue_stack *rvalue_stack_grow(rvalue_stack *stack, VALUE *handle, rvalu
|
|
|
219
236
|
|
|
220
237
|
static VALUE rvalue_stack_push(rvalue_stack *stack, VALUE value, VALUE *handle, rvalue_stack **stack_ref)
|
|
221
238
|
{
|
|
239
|
+
JSON_ASSERT(stack->type != RVALUE_STACK_STACK_ALLOCATED || handle);
|
|
240
|
+
|
|
222
241
|
if (RB_UNLIKELY(stack->head >= stack->capa)) {
|
|
223
242
|
stack = rvalue_stack_grow(stack, handle, stack_ref);
|
|
224
243
|
}
|
|
244
|
+
|
|
225
245
|
stack->ptr[stack->head] = value;
|
|
226
246
|
stack->head++;
|
|
247
|
+
|
|
227
248
|
return value;
|
|
228
249
|
}
|
|
229
250
|
|
|
@@ -243,14 +264,14 @@ static void rvalue_stack_mark(void *ptr)
|
|
|
243
264
|
long index;
|
|
244
265
|
if (stack && stack->ptr) {
|
|
245
266
|
for (index = 0; index < stack->head; index++) {
|
|
246
|
-
|
|
267
|
+
rb_gc_mark_movable(stack->ptr[index]);
|
|
247
268
|
}
|
|
248
269
|
}
|
|
249
270
|
}
|
|
250
271
|
|
|
251
272
|
static void rvalue_stack_free_buffer(rvalue_stack *stack)
|
|
252
273
|
{
|
|
253
|
-
|
|
274
|
+
JSON_SIZED_FREE_N(stack->ptr, stack->capa);
|
|
254
275
|
stack->ptr = NULL;
|
|
255
276
|
}
|
|
256
277
|
|
|
@@ -260,7 +281,7 @@ static void rvalue_stack_free(void *ptr)
|
|
|
260
281
|
if (stack) {
|
|
261
282
|
rvalue_stack_free_buffer(stack);
|
|
262
283
|
#ifndef HAVE_RUBY_TYPED_EMBEDDABLE
|
|
263
|
-
|
|
284
|
+
JSON_SIZED_FREE(stack);
|
|
264
285
|
#endif
|
|
265
286
|
}
|
|
266
287
|
}
|
|
@@ -268,7 +289,22 @@ static void rvalue_stack_free(void *ptr)
|
|
|
268
289
|
static size_t rvalue_stack_memsize(const void *ptr)
|
|
269
290
|
{
|
|
270
291
|
const rvalue_stack *stack = (const rvalue_stack *)ptr;
|
|
271
|
-
|
|
292
|
+
size_t memsize = sizeof(VALUE) * stack->capa;
|
|
293
|
+
#ifndef HAVE_RUBY_TYPED_EMBEDDABLE
|
|
294
|
+
memsize += sizeof(rvalue_stack);
|
|
295
|
+
#endif
|
|
296
|
+
return memsize;
|
|
297
|
+
}
|
|
298
|
+
|
|
299
|
+
static void rvalue_stack_compact(void *ptr)
|
|
300
|
+
{
|
|
301
|
+
rvalue_stack *stack = (rvalue_stack *)ptr;
|
|
302
|
+
long index;
|
|
303
|
+
if (stack && stack->ptr) {
|
|
304
|
+
for (index = 0; index < stack->head; index++) {
|
|
305
|
+
stack->ptr[index] = rb_gc_location(stack->ptr[index]);
|
|
306
|
+
}
|
|
307
|
+
}
|
|
272
308
|
}
|
|
273
309
|
|
|
274
310
|
static const rb_data_type_t JSON_Parser_rvalue_stack_type = {
|
|
@@ -277,8 +313,11 @@ static const rb_data_type_t JSON_Parser_rvalue_stack_type = {
|
|
|
277
313
|
.dmark = rvalue_stack_mark,
|
|
278
314
|
.dfree = rvalue_stack_free,
|
|
279
315
|
.dsize = rvalue_stack_memsize,
|
|
316
|
+
.dcompact = rvalue_stack_compact,
|
|
280
317
|
},
|
|
281
|
-
|
|
318
|
+
// We deliberately don't declare rvalue_stack as RUBY_TYPED_WB_PROTECTED
|
|
319
|
+
// because it churns a lot of values so trigering write barriers every time is very costly.
|
|
320
|
+
.flags = RUBY_TYPED_THREAD_SAFE_FREE | RUBY_TYPED_EMBEDDABLE,
|
|
282
321
|
};
|
|
283
322
|
|
|
284
323
|
static rvalue_stack *rvalue_stack_spill(rvalue_stack *old_stack, VALUE *handle, rvalue_stack **stack_ref)
|
|
@@ -309,33 +348,62 @@ static void rvalue_stack_eagerly_release(VALUE handle)
|
|
|
309
348
|
}
|
|
310
349
|
}
|
|
311
350
|
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
351
|
+
/* frame stack */
|
|
352
|
+
|
|
353
|
+
// Iterative (non-recursive) parsing keeps an explicit stack of the containers
|
|
354
|
+
// currently being built, instead of relying on the C call stack. Each frame
|
|
355
|
+
// only needs enough bookkeeping to close its container: which kind it is, the
|
|
356
|
+
// rvalue_stack position where its children start (so we know how many to pop),
|
|
357
|
+
// and the cursor at its opening brace (used to rewind for duplicate key
|
|
358
|
+
// errors). Frames hold no VALUEs, so this stack needs no GC marking; it reuses
|
|
359
|
+
// the same stack-allocated-with-heap-spill strategy as the rvalue_stack so that
|
|
360
|
+
// it's freed even if parsing raises.
|
|
361
|
+
//
|
|
362
|
+
// The lifecycle helpers below (grow/push/peek/pop/spill/free/eagerly_release
|
|
363
|
+
// and the rb_data_type_t) deliberately mirror their rvalue_stack counterparts
|
|
364
|
+
// -- the element type and the absence of a mark function are the only real
|
|
365
|
+
// differences. Keep the two in sync: a fix to the spill/release or
|
|
366
|
+
// HAVE_RUBY_TYPED_EMBEDDABLE handling in one almost certainly belongs in the
|
|
367
|
+
// other.
|
|
368
|
+
#define JSON_FRAME_STACK_INITIAL_CAPA 32
|
|
369
|
+
|
|
370
|
+
enum json_frame_type {
|
|
371
|
+
JSON_FRAME_ROOT, // == JSON_PHASE_DONE
|
|
372
|
+
JSON_FRAME_ARRAY, // == JSON_PHASE_ARRAY_COMMA
|
|
373
|
+
JSON_FRAME_OBJECT, // = JSON_PHASE_OBJECT_COMMA
|
|
374
|
+
};
|
|
375
|
+
|
|
376
|
+
// Where a frame is within its container's grammar. This is the entirety of the
|
|
377
|
+
// parser's "what to do next" state: json_parse_any dispatches on the top
|
|
378
|
+
// frame's phase and holds no resume state in C locals, so a parse can stop at
|
|
379
|
+
// any value boundary and be resumed purely from the (persistable) frame stack.
|
|
380
|
+
//
|
|
381
|
+
// The first three phases are deliberately equal to the corresponding json_frame_type
|
|
382
|
+
// to simplify the transition of phase in json_value_completed.
|
|
383
|
+
enum json_frame_phase {
|
|
384
|
+
JSON_PHASE_DONE = JSON_FRAME_ROOT, // root only: the document value has been parsed
|
|
385
|
+
JSON_PHASE_ARRAY_COMMA = JSON_FRAME_ARRAY, // after a value: expecting ',' or the closing ']'
|
|
386
|
+
JSON_PHASE_OBJECT_COMMA = JSON_FRAME_OBJECT, // after a value: expecting ',' or the closing '}'
|
|
387
|
+
JSON_PHASE_VALUE, // expecting a value (document root, array element, or object value after ':')
|
|
388
|
+
JSON_PHASE_OBJECT_KEY, // expecting a '"' key (after '{' or ',')
|
|
389
|
+
JSON_PHASE_OBJECT_COLON, // object only: after a key, expecting ':'
|
|
390
|
+
};
|
|
391
|
+
|
|
392
|
+
typedef struct json_frame_struct {
|
|
393
|
+
enum json_frame_type type;
|
|
394
|
+
enum json_frame_phase phase;
|
|
395
|
+
long value_stack_head; // rvalue_stack->head when this container opened
|
|
396
|
+
size_t start_offset; // object frames only (the '{'); NULL otherwise
|
|
397
|
+
} json_frame;
|
|
398
|
+
|
|
399
|
+
typedef struct json_frame_stack_struct {
|
|
400
|
+
enum rvalue_stack_type type; // shared with rvalue_stack: is ptr stack- or heap-allocated
|
|
401
|
+
long capa;
|
|
402
|
+
long head;
|
|
403
|
+
json_frame *ptr;
|
|
404
|
+
} json_frame_stack;
|
|
337
405
|
|
|
338
|
-
enum
|
|
406
|
+
enum deprecatable_action {
|
|
339
407
|
JSON_DEPRECATED = 0,
|
|
340
408
|
JSON_IGNORE,
|
|
341
409
|
JSON_RAISE,
|
|
@@ -345,7 +413,8 @@ typedef struct JSON_ParserStruct {
|
|
|
345
413
|
VALUE on_load_proc;
|
|
346
414
|
VALUE decimal_class;
|
|
347
415
|
ID decimal_method_id;
|
|
348
|
-
enum
|
|
416
|
+
enum deprecatable_action on_duplicate_key;
|
|
417
|
+
enum deprecatable_action on_comment;
|
|
349
418
|
int max_nesting;
|
|
350
419
|
bool allow_nan;
|
|
351
420
|
bool allow_trailing_comma;
|
|
@@ -356,17 +425,152 @@ typedef struct JSON_ParserStruct {
|
|
|
356
425
|
} JSON_ParserConfig;
|
|
357
426
|
|
|
358
427
|
typedef struct JSON_ParserStateStruct {
|
|
359
|
-
VALUE *
|
|
428
|
+
VALUE *value_stack_handle;
|
|
429
|
+
VALUE *frame_stack_handle;
|
|
360
430
|
const char *start;
|
|
361
431
|
const char *cursor;
|
|
362
432
|
const char *end;
|
|
363
|
-
rvalue_stack *
|
|
433
|
+
rvalue_stack *value_stack;
|
|
434
|
+
json_frame_stack *frames;
|
|
364
435
|
rvalue_cache name_cache;
|
|
365
436
|
int in_array;
|
|
366
437
|
int current_nesting;
|
|
367
438
|
unsigned int emitted_deprecations;
|
|
439
|
+
VALUE parser;
|
|
368
440
|
} JSON_ParserState;
|
|
369
441
|
|
|
442
|
+
static json_frame_stack *json_frame_stack_spill(json_frame_stack *old_stack, VALUE *handle, json_frame_stack **stack_ref);
|
|
443
|
+
|
|
444
|
+
static json_frame_stack *json_frame_stack_grow(json_frame_stack *stack, VALUE *handle, json_frame_stack **stack_ref)
|
|
445
|
+
{
|
|
446
|
+
long required = stack->capa ? stack->capa * 2 : JSON_FRAME_STACK_INITIAL_CAPA;
|
|
447
|
+
|
|
448
|
+
if (stack->type == RVALUE_STACK_STACK_ALLOCATED) {
|
|
449
|
+
stack = json_frame_stack_spill(stack, handle, stack_ref);
|
|
450
|
+
} else {
|
|
451
|
+
JSON_SIZED_REALLOC_N(stack->ptr, json_frame, required, stack->capa);
|
|
452
|
+
stack->capa = required;
|
|
453
|
+
}
|
|
454
|
+
return stack;
|
|
455
|
+
}
|
|
456
|
+
|
|
457
|
+
static json_frame *json_frame_stack_push(JSON_ParserState *state, json_frame frame)
|
|
458
|
+
{
|
|
459
|
+
json_frame_stack *stack = state->frames;
|
|
460
|
+
|
|
461
|
+
JSON_ASSERT(stack->type != RVALUE_STACK_STACK_ALLOCATED || state->frame_stack_handle);
|
|
462
|
+
|
|
463
|
+
if (RB_UNLIKELY(stack->head >= stack->capa)) {
|
|
464
|
+
stack = json_frame_stack_grow(stack, state->frame_stack_handle, &state->frames);
|
|
465
|
+
}
|
|
466
|
+
|
|
467
|
+
json_frame *frame_ptr = &stack->ptr[stack->head++];
|
|
468
|
+
*frame_ptr = frame;
|
|
469
|
+
return frame_ptr;
|
|
470
|
+
}
|
|
471
|
+
|
|
472
|
+
static inline json_frame *json_frame_stack_peek(json_frame_stack *stack)
|
|
473
|
+
{
|
|
474
|
+
return &stack->ptr[stack->head - 1];
|
|
475
|
+
}
|
|
476
|
+
|
|
477
|
+
static inline void json_frame_stack_pop(json_frame_stack *stack)
|
|
478
|
+
{
|
|
479
|
+
stack->head--;
|
|
480
|
+
}
|
|
481
|
+
|
|
482
|
+
static void json_frame_stack_free_buffer(json_frame_stack *stack)
|
|
483
|
+
{
|
|
484
|
+
JSON_SIZED_FREE_N(stack->ptr, stack->capa);
|
|
485
|
+
stack->ptr = NULL;
|
|
486
|
+
}
|
|
487
|
+
|
|
488
|
+
static void json_frame_stack_free(void *ptr)
|
|
489
|
+
{
|
|
490
|
+
json_frame_stack *stack = (json_frame_stack *)ptr;
|
|
491
|
+
if (stack) {
|
|
492
|
+
json_frame_stack_free_buffer(stack);
|
|
493
|
+
#ifndef HAVE_RUBY_TYPED_EMBEDDABLE
|
|
494
|
+
JSON_SIZED_FREE(stack);
|
|
495
|
+
#endif
|
|
496
|
+
}
|
|
497
|
+
}
|
|
498
|
+
|
|
499
|
+
static size_t json_frame_stack_memsize(const void *ptr)
|
|
500
|
+
{
|
|
501
|
+
const json_frame_stack *stack = (const json_frame_stack *)ptr;
|
|
502
|
+
|
|
503
|
+
size_t memsize = sizeof(json_frame) * stack->capa;
|
|
504
|
+
#ifndef HAVE_RUBY_TYPED_EMBEDDABLE
|
|
505
|
+
memsize += sizeof(json_frame_stack);
|
|
506
|
+
#endif
|
|
507
|
+
return memsize;
|
|
508
|
+
}
|
|
509
|
+
|
|
510
|
+
static const rb_data_type_t JSON_Parser_frame_stack_type = {
|
|
511
|
+
.wrap_struct_name = "JSON::Ext::Parser/frame_stack",
|
|
512
|
+
.function = {
|
|
513
|
+
.dmark = NULL,
|
|
514
|
+
.dfree = json_frame_stack_free,
|
|
515
|
+
.dsize = json_frame_stack_memsize,
|
|
516
|
+
},
|
|
517
|
+
.flags = RUBY_TYPED_THREAD_SAFE_FREE | RUBY_TYPED_WB_PROTECTED | RUBY_TYPED_EMBEDDABLE,
|
|
518
|
+
};
|
|
519
|
+
|
|
520
|
+
static json_frame_stack *json_frame_stack_spill(json_frame_stack *old_stack, VALUE *handle, json_frame_stack **stack_ref)
|
|
521
|
+
{
|
|
522
|
+
json_frame_stack *stack;
|
|
523
|
+
*handle = TypedData_Make_Struct(0, json_frame_stack, &JSON_Parser_frame_stack_type, stack);
|
|
524
|
+
*stack_ref = stack;
|
|
525
|
+
MEMCPY(stack, old_stack, json_frame_stack, 1);
|
|
526
|
+
|
|
527
|
+
stack->capa = old_stack->capa << 1;
|
|
528
|
+
stack->ptr = ALLOC_N(json_frame, stack->capa);
|
|
529
|
+
stack->type = RVALUE_STACK_HEAP_ALLOCATED;
|
|
530
|
+
MEMCPY(stack->ptr, old_stack->ptr, json_frame, old_stack->head);
|
|
531
|
+
return stack;
|
|
532
|
+
}
|
|
533
|
+
|
|
534
|
+
static void json_frame_stack_eagerly_release(VALUE handle)
|
|
535
|
+
{
|
|
536
|
+
if (handle) {
|
|
537
|
+
json_frame_stack *stack;
|
|
538
|
+
TypedData_Get_Struct(handle, json_frame_stack, &JSON_Parser_frame_stack_type, stack);
|
|
539
|
+
#ifdef HAVE_RUBY_TYPED_EMBEDDABLE
|
|
540
|
+
json_frame_stack_free_buffer(stack);
|
|
541
|
+
#else
|
|
542
|
+
json_frame_stack_free(stack);
|
|
543
|
+
RTYPEDDATA_DATA(handle) = NULL;
|
|
544
|
+
#endif
|
|
545
|
+
}
|
|
546
|
+
}
|
|
547
|
+
|
|
548
|
+
static int convert_UTF32_to_UTF8(char *buf, uint32_t ch)
|
|
549
|
+
{
|
|
550
|
+
int len = 1;
|
|
551
|
+
if (ch <= 0x7F) {
|
|
552
|
+
buf[0] = (char) ch;
|
|
553
|
+
} else if (ch <= 0x07FF) {
|
|
554
|
+
buf[0] = (char) ((ch >> 6) | 0xC0);
|
|
555
|
+
buf[1] = (char) ((ch & 0x3F) | 0x80);
|
|
556
|
+
len++;
|
|
557
|
+
} else if (ch <= 0xFFFF) {
|
|
558
|
+
buf[0] = (char) ((ch >> 12) | 0xE0);
|
|
559
|
+
buf[1] = (char) (((ch >> 6) & 0x3F) | 0x80);
|
|
560
|
+
buf[2] = (char) ((ch & 0x3F) | 0x80);
|
|
561
|
+
len += 2;
|
|
562
|
+
} else if (ch <= 0x1fffff) {
|
|
563
|
+
buf[0] =(char) ((ch >> 18) | 0xF0);
|
|
564
|
+
buf[1] =(char) (((ch >> 12) & 0x3F) | 0x80);
|
|
565
|
+
buf[2] =(char) (((ch >> 6) & 0x3F) | 0x80);
|
|
566
|
+
buf[3] =(char) ((ch & 0x3F) | 0x80);
|
|
567
|
+
len += 3;
|
|
568
|
+
} else {
|
|
569
|
+
buf[0] = '?';
|
|
570
|
+
}
|
|
571
|
+
return len;
|
|
572
|
+
}
|
|
573
|
+
|
|
370
574
|
static inline size_t rest(JSON_ParserState *state) {
|
|
371
575
|
return state->end - state->cursor;
|
|
372
576
|
}
|
|
@@ -398,6 +602,7 @@ static void cursor_position(JSON_ParserState *state, long *line_out, long *colum
|
|
|
398
602
|
|
|
399
603
|
while (cursor >= state->start) {
|
|
400
604
|
if (*cursor-- == '\n') {
|
|
605
|
+
line++;
|
|
401
606
|
break;
|
|
402
607
|
}
|
|
403
608
|
column++;
|
|
@@ -412,6 +617,8 @@ static void cursor_position(JSON_ParserState *state, long *line_out, long *colum
|
|
|
412
617
|
*column_out = column;
|
|
413
618
|
}
|
|
414
619
|
|
|
620
|
+
static const unsigned int MAX_DEPRECATIONS = 5;
|
|
621
|
+
|
|
415
622
|
static void emit_parse_warning(const char *message, JSON_ParserState *state)
|
|
416
623
|
{
|
|
417
624
|
long line, column;
|
|
@@ -423,7 +630,7 @@ static void emit_parse_warning(const char *message, JSON_ParserState *state)
|
|
|
423
630
|
|
|
424
631
|
#define PARSE_ERROR_FRAGMENT_LEN 32
|
|
425
632
|
|
|
426
|
-
static VALUE build_parse_error_message(const char *format, JSON_ParserState *state
|
|
633
|
+
static VALUE build_parse_error_message(const char *format, JSON_ParserState *state)
|
|
427
634
|
{
|
|
428
635
|
unsigned char buffer[PARSE_ERROR_FRAGMENT_LEN + 3];
|
|
429
636
|
|
|
@@ -457,31 +664,61 @@ static VALUE build_parse_error_message(const char *format, JSON_ParserState *sta
|
|
|
457
664
|
}
|
|
458
665
|
}
|
|
459
666
|
|
|
460
|
-
|
|
461
|
-
rb_str_catf(message, " at line %ld column %ld", line, column);
|
|
462
|
-
return message;
|
|
667
|
+
return rb_enc_sprintf(enc_utf8, format, ptr);
|
|
463
668
|
}
|
|
464
669
|
|
|
465
|
-
static VALUE parse_error_new(VALUE message, long line, long column)
|
|
670
|
+
static VALUE parse_error_new(JSON_ParserState *state, VALUE message, long line, long column, bool eos)
|
|
466
671
|
{
|
|
467
|
-
VALUE exc = rb_exc_new_str(
|
|
468
|
-
rb_ivar_set(exc,
|
|
469
|
-
rb_ivar_set(exc,
|
|
672
|
+
VALUE exc = rb_exc_new_str(eParserError, message);
|
|
673
|
+
rb_ivar_set(exc, i_at_line, LONG2NUM(line));
|
|
674
|
+
rb_ivar_set(exc, i_at_column, LONG2NUM(column));
|
|
470
675
|
return exc;
|
|
471
676
|
}
|
|
472
677
|
|
|
473
|
-
NORETURN(static) void raise_parse_error(const char *format, JSON_ParserState *state)
|
|
678
|
+
NORETURN(static) void raise_parse_error(const char *format, JSON_ParserState *state, bool eos)
|
|
474
679
|
{
|
|
475
|
-
|
|
476
|
-
|
|
477
|
-
|
|
478
|
-
|
|
680
|
+
if (state->parser) {
|
|
681
|
+
if (eos) {
|
|
682
|
+
// the error will be swallowed by ResumableParser#parse, so no
|
|
683
|
+
// point building a message or backtrace.
|
|
684
|
+
rb_throw_obj(state->parser, state->parser);
|
|
685
|
+
} else {
|
|
686
|
+
// line and columns can't be accurate in resumable
|
|
687
|
+
rb_exc_raise(parse_error_new(state, build_parse_error_message(format, state), 0, 0, eos));
|
|
688
|
+
}
|
|
689
|
+
} else {
|
|
690
|
+
VALUE message = build_parse_error_message(format, state);
|
|
691
|
+
long line, column;
|
|
692
|
+
cursor_position(state, &line, &column);
|
|
693
|
+
rb_str_catf(message, " at line %ld column %ld", line, column);
|
|
694
|
+
rb_exc_raise(parse_error_new(state, message, line, column, eos));
|
|
695
|
+
}
|
|
696
|
+
}
|
|
697
|
+
|
|
698
|
+
NORETURN(static) void raise_eos_error(const char *format, JSON_ParserState *state)
|
|
699
|
+
{
|
|
700
|
+
raise_parse_error(format, state, true);
|
|
479
701
|
}
|
|
480
702
|
|
|
481
|
-
NORETURN(static) void
|
|
703
|
+
NORETURN(static) void raise_syntax_error(const char *format, JSON_ParserState *state)
|
|
704
|
+
{
|
|
705
|
+
raise_parse_error(format, state, false);
|
|
706
|
+
}
|
|
707
|
+
|
|
708
|
+
NORETURN(static) void raise_parse_error_at(const char *format, JSON_ParserState *state, const char *at, bool eos)
|
|
482
709
|
{
|
|
483
710
|
state->cursor = at;
|
|
484
|
-
raise_parse_error(format, state);
|
|
711
|
+
raise_parse_error(format, state, eos);
|
|
712
|
+
}
|
|
713
|
+
|
|
714
|
+
NORETURN(static) void raise_eos_error_at(const char *format, JSON_ParserState *state, const char *at)
|
|
715
|
+
{
|
|
716
|
+
raise_parse_error_at(format, state, at, true);
|
|
717
|
+
}
|
|
718
|
+
|
|
719
|
+
NORETURN(static) void raise_syntax_error_at(const char *format, JSON_ParserState *state, const char *at)
|
|
720
|
+
{
|
|
721
|
+
raise_parse_error_at(format, state, at, false);
|
|
485
722
|
}
|
|
486
723
|
|
|
487
724
|
/* unicode */
|
|
@@ -506,7 +743,7 @@ static const signed char digit_values[256] = {
|
|
|
506
743
|
static uint32_t unescape_unicode(JSON_ParserState *state, const char *sp, const char *spe)
|
|
507
744
|
{
|
|
508
745
|
if (RB_UNLIKELY(sp > spe - 4)) {
|
|
509
|
-
|
|
746
|
+
raise_eos_error_at("incomplete unicode character escape sequence at %s", state, sp - 2);
|
|
510
747
|
}
|
|
511
748
|
|
|
512
749
|
const unsigned char *p = (const unsigned char *)sp;
|
|
@@ -517,7 +754,7 @@ static uint32_t unescape_unicode(JSON_ParserState *state, const char *sp, const
|
|
|
517
754
|
const signed char b3 = digit_values[p[3]];
|
|
518
755
|
|
|
519
756
|
if (RB_UNLIKELY((signed char)(b0 | b1 | b2 | b3) < 0)) {
|
|
520
|
-
|
|
757
|
+
raise_syntax_error_at("incomplete unicode character escape sequence at %s", state, sp - 2);
|
|
521
758
|
}
|
|
522
759
|
|
|
523
760
|
return ((uint32_t)b0 << 12) | ((uint32_t)b1 << 8) | ((uint32_t)b2 << 4) | (uint32_t)b3;
|
|
@@ -529,19 +766,36 @@ static uint32_t unescape_unicode(JSON_ParserState *state, const char *sp, const
|
|
|
529
766
|
|
|
530
767
|
static const rb_data_type_t JSON_ParserConfig_type;
|
|
531
768
|
|
|
532
|
-
|
|
533
|
-
|
|
769
|
+
const char *COMMENT_DEPRECATION_MESSAGE = "Encountered comment in JSON. This will raise an error in json 3.0 unless enabled via `allow_comments: true`";
|
|
770
|
+
NOINLINE(static) void
|
|
771
|
+
json_eat_comments(JSON_ParserState *state, JSON_ParserConfig *config, const char *resume_pos)
|
|
534
772
|
{
|
|
773
|
+
if (config->on_comment == JSON_RAISE) {
|
|
774
|
+
raise_syntax_error("unexpected token %s", state);
|
|
775
|
+
}
|
|
776
|
+
|
|
535
777
|
const char *start = state->cursor;
|
|
778
|
+
// An incomplete comment suspends a resumable parse by rewinding the cursor
|
|
779
|
+
// and throwing. Callers that already consumed a token not yet committed to
|
|
780
|
+
// the frame stack pass resume_pos so the rewind re-reads that token too.
|
|
781
|
+
// Non-resumable error positions keep pointing at the comment either way.
|
|
782
|
+
const char *rewind_pos = (state->parser && resume_pos) ? resume_pos : start;
|
|
536
783
|
state->cursor++;
|
|
537
784
|
|
|
538
785
|
switch (peek(state)) {
|
|
539
786
|
case '/': {
|
|
540
|
-
|
|
541
|
-
if (!
|
|
787
|
+
const char *newline = memchr(state->cursor, '\n', state->end - state->cursor);
|
|
788
|
+
if (!newline) {
|
|
789
|
+
// state->parser marks resumable mode, where the buffer end is only a
|
|
790
|
+
// chunk boundary: the terminating newline may still arrive, so leave
|
|
791
|
+
// the comment unterminated instead of consuming to end as a one-shot
|
|
792
|
+
// parse would.
|
|
793
|
+
if (state->parser) {
|
|
794
|
+
raise_eos_error_at("unterminated comment, expected end of line", state, rewind_pos);
|
|
795
|
+
}
|
|
542
796
|
state->cursor = state->end;
|
|
543
797
|
} else {
|
|
544
|
-
state->cursor
|
|
798
|
+
state->cursor = newline + 1;
|
|
545
799
|
}
|
|
546
800
|
break;
|
|
547
801
|
}
|
|
@@ -551,7 +805,7 @@ json_eat_comments(JSON_ParserState *state)
|
|
|
551
805
|
while (true) {
|
|
552
806
|
const char *next_match = memchr(state->cursor, '*', state->end - state->cursor);
|
|
553
807
|
if (!next_match) {
|
|
554
|
-
|
|
808
|
+
raise_eos_error_at("unterminated comment, expected closing '*/'", state, rewind_pos);
|
|
555
809
|
}
|
|
556
810
|
|
|
557
811
|
state->cursor = next_match + 1;
|
|
@@ -563,13 +817,18 @@ json_eat_comments(JSON_ParserState *state)
|
|
|
563
817
|
break;
|
|
564
818
|
}
|
|
565
819
|
default:
|
|
566
|
-
raise_parse_error_at("unexpected token %s", state, start);
|
|
820
|
+
raise_parse_error_at("unexpected token %s", state, eos(state) ? rewind_pos : start, eos(state));
|
|
567
821
|
break;
|
|
568
822
|
}
|
|
823
|
+
|
|
824
|
+
if (config->on_comment == JSON_DEPRECATED && state->emitted_deprecations < MAX_DEPRECATIONS) {
|
|
825
|
+
state->emitted_deprecations++;
|
|
826
|
+
emit_parse_warning(COMMENT_DEPRECATION_MESSAGE, state);
|
|
827
|
+
}
|
|
569
828
|
}
|
|
570
829
|
|
|
571
830
|
ALWAYS_INLINE(static) void
|
|
572
|
-
|
|
831
|
+
json_eat_whitespace_resume_at(JSON_ParserState *state, JSON_ParserConfig *config, bool include_comments, const char *resume_pos)
|
|
573
832
|
{
|
|
574
833
|
while (true) {
|
|
575
834
|
switch (peek(state)) {
|
|
@@ -600,7 +859,11 @@ json_eat_whitespace(JSON_ParserState *state)
|
|
|
600
859
|
state->cursor++;
|
|
601
860
|
break;
|
|
602
861
|
case '/':
|
|
603
|
-
|
|
862
|
+
if (!include_comments) {
|
|
863
|
+
return;
|
|
864
|
+
}
|
|
865
|
+
|
|
866
|
+
json_eat_comments(state, config, resume_pos);
|
|
604
867
|
break;
|
|
605
868
|
|
|
606
869
|
default:
|
|
@@ -609,6 +872,12 @@ json_eat_whitespace(JSON_ParserState *state)
|
|
|
609
872
|
}
|
|
610
873
|
}
|
|
611
874
|
|
|
875
|
+
ALWAYS_INLINE(static) void
|
|
876
|
+
json_eat_whitespace(JSON_ParserState *state, JSON_ParserConfig *config, bool include_comments)
|
|
877
|
+
{
|
|
878
|
+
json_eat_whitespace_resume_at(state, config, include_comments, NULL);
|
|
879
|
+
}
|
|
880
|
+
|
|
612
881
|
static inline VALUE build_string(const char *start, const char *end, bool intern, bool symbolize)
|
|
613
882
|
{
|
|
614
883
|
if (symbolize) {
|
|
@@ -754,13 +1023,13 @@ NOINLINE(static) VALUE json_string_unescape(JSON_ParserState *state, JSON_Parser
|
|
|
754
1023
|
uint32_t sur = unescape_unicode(state, pe + 2, stringEnd);
|
|
755
1024
|
|
|
756
1025
|
if (RB_UNLIKELY((sur & 0xFC00) != 0xDC00)) {
|
|
757
|
-
|
|
1026
|
+
raise_syntax_error_at("invalid surrogate pair at %s", state, p);
|
|
758
1027
|
}
|
|
759
1028
|
|
|
760
1029
|
ch = (((ch & 0x3F) << 10) | ((((ch >> 6) & 0xF) + 1) << 16) | (sur & 0x3FF));
|
|
761
1030
|
pe += 5;
|
|
762
1031
|
} else {
|
|
763
|
-
|
|
1032
|
+
raise_syntax_error_at("incomplete surrogate pair at %s", state, p);
|
|
764
1033
|
break;
|
|
765
1034
|
}
|
|
766
1035
|
}
|
|
@@ -770,20 +1039,22 @@ NOINLINE(static) VALUE json_string_unescape(JSON_ParserState *state, JSON_Parser
|
|
|
770
1039
|
p = ++pe;
|
|
771
1040
|
break;
|
|
772
1041
|
}
|
|
1042
|
+
case 0:
|
|
1043
|
+
return Qundef;
|
|
773
1044
|
default:
|
|
774
1045
|
if ((unsigned char)*pe < 0x20) {
|
|
775
1046
|
if (!config->allow_control_characters) {
|
|
776
1047
|
if (*pe == '\n') {
|
|
777
|
-
|
|
1048
|
+
raise_syntax_error_at("Invalid unescaped newline character (\\n) in string: %s", state, pe - 1);
|
|
778
1049
|
}
|
|
779
|
-
|
|
1050
|
+
raise_syntax_error_at("invalid ASCII control character in string: %s", state, pe - 1);
|
|
780
1051
|
}
|
|
781
1052
|
}
|
|
782
1053
|
|
|
783
1054
|
if (config->allow_invalid_escape) {
|
|
784
1055
|
APPEND_CHAR(*pe);
|
|
785
1056
|
} else {
|
|
786
|
-
|
|
1057
|
+
raise_syntax_error_at("invalid escape character in string: %s", state, pe - 1);
|
|
787
1058
|
}
|
|
788
1059
|
break;
|
|
789
1060
|
}
|
|
@@ -879,19 +1150,24 @@ static inline VALUE json_decode_float(JSON_ParserConfig *config, uint64_t mantis
|
|
|
879
1150
|
return rb_float_new(negative ? -0.0 : 0.0);
|
|
880
1151
|
}
|
|
881
1152
|
|
|
882
|
-
|
|
883
|
-
|
|
884
|
-
|
|
1153
|
+
if (RB_UNLIKELY(mantissa_digits > 18 || mantissa_digits + exponent < -307)) {
|
|
1154
|
+
// If the value is so small that it definitely underflows to 0.0, return early
|
|
1155
|
+
// to avoid triggering a "Float out of range" warning from rb_cstr_to_dbl.
|
|
1156
|
+
// When mantissa_digits + exponent < -324, value < 10^(-324) < DBL_TRUE_MIN/2,
|
|
1157
|
+
// so it rounds to 0 in IEEE 754 round-to-nearest.
|
|
1158
|
+
if (RB_UNLIKELY(mantissa_digits + exponent < -324)) {
|
|
1159
|
+
return rb_float_new(negative ? -0.0 : 0.0);
|
|
1160
|
+
}
|
|
885
1161
|
return json_decode_large_float(start, end - start);
|
|
886
1162
|
}
|
|
887
1163
|
|
|
888
|
-
return DBL2NUM(
|
|
1164
|
+
return DBL2NUM(ffp_s2d(exponent, mantissa, negative));
|
|
889
1165
|
}
|
|
890
1166
|
|
|
891
1167
|
static inline VALUE json_decode_array(JSON_ParserState *state, JSON_ParserConfig *config, long count)
|
|
892
1168
|
{
|
|
893
|
-
VALUE array = rb_ary_new_from_values(count, rvalue_stack_peek(state->
|
|
894
|
-
rvalue_stack_pop(state->
|
|
1169
|
+
VALUE array = rb_ary_new_from_values(count, rvalue_stack_peek(state->value_stack, count));
|
|
1170
|
+
rvalue_stack_pop(state->value_stack, count);
|
|
895
1171
|
|
|
896
1172
|
if (config->freeze) {
|
|
897
1173
|
RB_OBJ_FREEZE(array);
|
|
@@ -935,38 +1211,50 @@ NORETURN(static) void raise_duplicate_key_error(JSON_ParserState *state, VALUE d
|
|
|
935
1211
|
rb_inspect(duplicate_key)
|
|
936
1212
|
);
|
|
937
1213
|
|
|
938
|
-
|
|
939
|
-
|
|
940
|
-
|
|
941
|
-
|
|
1214
|
+
rb_str_concat(message, build_parse_error_message("", state));
|
|
1215
|
+
if (state->parser) { // line and columns can't be accurate in resumable
|
|
1216
|
+
rb_exc_raise(parse_error_new(state, message, 0, 0, false));
|
|
1217
|
+
} else {
|
|
1218
|
+
long line, column;
|
|
1219
|
+
cursor_position(state, &line, &column);
|
|
1220
|
+
rb_str_catf(message, " at line %ld column %ld", line, column);
|
|
1221
|
+
rb_exc_raise(parse_error_new(state, message, line, column, false));
|
|
1222
|
+
}
|
|
1223
|
+
}
|
|
1224
|
+
|
|
1225
|
+
NOINLINE(static) void json_on_duplicate_key(JSON_ParserState *state, JSON_ParserConfig *config, size_t count, const VALUE *pairs)
|
|
1226
|
+
{
|
|
1227
|
+
switch (config->on_duplicate_key) {
|
|
1228
|
+
case JSON_IGNORE:
|
|
1229
|
+
return;
|
|
1230
|
+
|
|
1231
|
+
case JSON_DEPRECATED:
|
|
1232
|
+
// Only emit the first few deprecations to avoid spamming.
|
|
1233
|
+
if (state->emitted_deprecations < MAX_DEPRECATIONS) {
|
|
1234
|
+
state->emitted_deprecations++;
|
|
1235
|
+
emit_duplicate_key_warning(state, json_find_duplicated_key(count, pairs));
|
|
1236
|
+
}
|
|
1237
|
+
return;
|
|
1238
|
+
|
|
1239
|
+
case JSON_RAISE:
|
|
1240
|
+
raise_duplicate_key_error(state, json_find_duplicated_key(count, pairs));
|
|
1241
|
+
return;
|
|
1242
|
+
}
|
|
1243
|
+
UNREACHABLE;
|
|
942
1244
|
}
|
|
943
1245
|
|
|
944
1246
|
static inline VALUE json_decode_object(JSON_ParserState *state, JSON_ParserConfig *config, size_t count)
|
|
945
1247
|
{
|
|
946
1248
|
size_t entries_count = count / 2;
|
|
947
1249
|
VALUE object = rb_hash_new_capa(entries_count);
|
|
948
|
-
const VALUE *pairs = rvalue_stack_peek(state->
|
|
1250
|
+
const VALUE *pairs = rvalue_stack_peek(state->value_stack, count);
|
|
949
1251
|
rb_hash_bulk_insert(count, pairs, object);
|
|
950
1252
|
|
|
951
1253
|
if (RB_UNLIKELY(RHASH_SIZE(object) < entries_count)) {
|
|
952
|
-
|
|
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
|
-
}
|
|
961
|
-
|
|
962
|
-
break;
|
|
963
|
-
case JSON_RAISE:
|
|
964
|
-
raise_duplicate_key_error(state, json_find_duplicated_key(count, pairs));
|
|
965
|
-
break;
|
|
966
|
-
}
|
|
1254
|
+
json_on_duplicate_key(state, config, count, pairs);
|
|
967
1255
|
}
|
|
968
1256
|
|
|
969
|
-
rvalue_stack_pop(state->
|
|
1257
|
+
rvalue_stack_pop(state->value_stack, count);
|
|
970
1258
|
|
|
971
1259
|
if (config->freeze) {
|
|
972
1260
|
RB_OBJ_FREEZE(object);
|
|
@@ -980,7 +1268,7 @@ static inline VALUE json_push_value(JSON_ParserState *state, JSON_ParserConfig *
|
|
|
980
1268
|
if (RB_UNLIKELY(config->on_load_proc)) {
|
|
981
1269
|
value = rb_proc_call_with_block(config->on_load_proc, 1, &value, Qnil);
|
|
982
1270
|
}
|
|
983
|
-
rvalue_stack_push(state->
|
|
1271
|
+
rvalue_stack_push(state->value_stack, value, state->value_stack_handle, &state->value_stack);
|
|
984
1272
|
return value;
|
|
985
1273
|
}
|
|
986
1274
|
|
|
@@ -1053,7 +1341,7 @@ static VALUE json_parse_escaped_string(JSON_ParserState *state, JSON_ParserConfi
|
|
|
1053
1341
|
case '"': {
|
|
1054
1342
|
VALUE string = json_string_unescape(state, config, start, state->cursor, is_name, &positions);
|
|
1055
1343
|
state->cursor++;
|
|
1056
|
-
return
|
|
1344
|
+
return string;
|
|
1057
1345
|
}
|
|
1058
1346
|
case '\\': {
|
|
1059
1347
|
if (RB_LIKELY(positions.size < JSON_MAX_UNESCAPE_POSITIONS)) {
|
|
@@ -1067,7 +1355,7 @@ static VALUE json_parse_escaped_string(JSON_ParserState *state, JSON_ParserConfi
|
|
|
1067
1355
|
}
|
|
1068
1356
|
default:
|
|
1069
1357
|
if (!config->allow_control_characters) {
|
|
1070
|
-
|
|
1358
|
+
raise_syntax_error("invalid ASCII control character in string: %s", state);
|
|
1071
1359
|
}
|
|
1072
1360
|
break;
|
|
1073
1361
|
}
|
|
@@ -1075,8 +1363,7 @@ static VALUE json_parse_escaped_string(JSON_ParserState *state, JSON_ParserConfi
|
|
|
1075
1363
|
state->cursor++;
|
|
1076
1364
|
} while (string_scan(state));
|
|
1077
1365
|
|
|
1078
|
-
|
|
1079
|
-
return Qfalse;
|
|
1366
|
+
return Qundef;
|
|
1080
1367
|
}
|
|
1081
1368
|
|
|
1082
1369
|
ALWAYS_INLINE(static) VALUE json_parse_string(JSON_ParserState *state, JSON_ParserConfig *config, bool is_name)
|
|
@@ -1085,15 +1372,19 @@ ALWAYS_INLINE(static) VALUE json_parse_string(JSON_ParserState *state, JSON_Pars
|
|
|
1085
1372
|
const char *start = state->cursor;
|
|
1086
1373
|
|
|
1087
1374
|
if (RB_UNLIKELY(!string_scan(state))) {
|
|
1088
|
-
|
|
1375
|
+
return Qundef;
|
|
1089
1376
|
}
|
|
1090
1377
|
|
|
1378
|
+
VALUE string;
|
|
1091
1379
|
if (RB_LIKELY(*state->cursor == '"')) {
|
|
1092
|
-
|
|
1380
|
+
string = json_string_fastpath(state, config, start, state->cursor, is_name);
|
|
1093
1381
|
state->cursor++;
|
|
1094
|
-
return json_push_value(state, config, string);
|
|
1095
1382
|
}
|
|
1096
|
-
|
|
1383
|
+
else {
|
|
1384
|
+
string = json_parse_escaped_string(state, config, is_name, start);
|
|
1385
|
+
}
|
|
1386
|
+
|
|
1387
|
+
return string;
|
|
1097
1388
|
}
|
|
1098
1389
|
|
|
1099
1390
|
#if JSON_CPU_LITTLE_ENDIAN_64BITS
|
|
@@ -1166,7 +1457,7 @@ static inline int json_parse_digits(JSON_ParserState *state, uint64_t *accumulat
|
|
|
1166
1457
|
return (int)(state->cursor - start);
|
|
1167
1458
|
}
|
|
1168
1459
|
|
|
1169
|
-
static inline VALUE json_parse_number(JSON_ParserState *state, JSON_ParserConfig *config, bool negative, const char *start)
|
|
1460
|
+
static inline VALUE json_parse_number(JSON_ParserState *state, JSON_ParserConfig *config, bool negative, const char *start, bool resumable)
|
|
1170
1461
|
{
|
|
1171
1462
|
bool integer = true;
|
|
1172
1463
|
const char first_digit = *state->cursor;
|
|
@@ -1180,7 +1471,7 @@ static inline VALUE json_parse_number(JSON_ParserState *state, JSON_ParserConfig
|
|
|
1180
1471
|
int mantissa_digits = json_parse_digits(state, &mantissa);
|
|
1181
1472
|
|
|
1182
1473
|
if (RB_UNLIKELY((first_digit == '0' && mantissa_digits > 1) || (negative && mantissa_digits == 0))) {
|
|
1183
|
-
|
|
1474
|
+
return Qundef;
|
|
1184
1475
|
}
|
|
1185
1476
|
|
|
1186
1477
|
// Parse fractional part
|
|
@@ -1193,7 +1484,7 @@ static inline VALUE json_parse_number(JSON_ParserState *state, JSON_ParserConfig
|
|
|
1193
1484
|
mantissa_digits += fractional_digits;
|
|
1194
1485
|
|
|
1195
1486
|
if (RB_UNLIKELY(!fractional_digits)) {
|
|
1196
|
-
|
|
1487
|
+
return Qundef;
|
|
1197
1488
|
}
|
|
1198
1489
|
}
|
|
1199
1490
|
|
|
@@ -1213,7 +1504,7 @@ static inline VALUE json_parse_number(JSON_ParserState *state, JSON_ParserConfig
|
|
|
1213
1504
|
int exponent_digits = json_parse_digits(state, &abs_exponent);
|
|
1214
1505
|
|
|
1215
1506
|
if (RB_UNLIKELY(!exponent_digits)) {
|
|
1216
|
-
|
|
1507
|
+
return Qundef;
|
|
1217
1508
|
}
|
|
1218
1509
|
|
|
1219
1510
|
if (RB_UNLIKELY(exponent_digits >= 20 || abs_exponent > (uint64_t)INT64_MAX)) {
|
|
@@ -1223,6 +1514,16 @@ static inline VALUE json_parse_number(JSON_ParserState *state, JSON_ParserConfig
|
|
|
1223
1514
|
}
|
|
1224
1515
|
}
|
|
1225
1516
|
|
|
1517
|
+
// A number touching the end of the buffer may still grow in a later chunk,
|
|
1518
|
+
// so the caller will rewind and wait. Decoding it now would build a value
|
|
1519
|
+
// -- for a long run of digits, an expensive bignum -- only to discard it,
|
|
1520
|
+
// and repeating that on every resumed chunk is quadratic in the number's
|
|
1521
|
+
// length. The digit scan above already advanced the cursor, which is all
|
|
1522
|
+
// the caller needs to detect the incomplete number.
|
|
1523
|
+
if (RB_UNLIKELY(resumable && eos(state))) {
|
|
1524
|
+
return Qundef;
|
|
1525
|
+
}
|
|
1526
|
+
|
|
1226
1527
|
if (integer) {
|
|
1227
1528
|
return json_decode_integer(mantissa, mantissa_digits, negative, start, state->cursor);
|
|
1228
1529
|
}
|
|
@@ -1235,229 +1536,420 @@ static inline VALUE json_parse_number(JSON_ParserState *state, JSON_ParserConfig
|
|
|
1235
1536
|
return json_decode_float(config, mantissa, mantissa_digits, exponent, negative, start, state->cursor);
|
|
1236
1537
|
}
|
|
1237
1538
|
|
|
1238
|
-
|
|
1539
|
+
// How many values (array elements, or interleaved object keys+values) have been
|
|
1540
|
+
// pushed onto the rvalue stack since this container opened. Used to size the
|
|
1541
|
+
// bulk decode on close, and to tell the first key/colon from later ones.
|
|
1542
|
+
static inline long json_frame_entry_count(const json_frame *frame, const rvalue_stack *value_stack)
|
|
1239
1543
|
{
|
|
1240
|
-
return
|
|
1544
|
+
return value_stack->head - frame->value_stack_head;
|
|
1241
1545
|
}
|
|
1242
1546
|
|
|
1243
|
-
|
|
1547
|
+
// A complete value now sits on top of the rvalue stack. Advance the frame that
|
|
1548
|
+
// was waiting for it: the root document is done, or the enclosing container
|
|
1549
|
+
// moves on to expecting a ',' or its closing bracket. The caller passes the
|
|
1550
|
+
// frame it already has in hand -- the one that was expecting the value -- which
|
|
1551
|
+
// after a container close is the freshly re-exposed parent.
|
|
1552
|
+
static inline enum json_frame_phase json_value_completed(json_frame *frame)
|
|
1244
1553
|
{
|
|
1245
|
-
|
|
1246
|
-
|
|
1247
|
-
|
|
1554
|
+
JSON_ASSERT((int)JSON_PHASE_DONE == (int)JSON_FRAME_ROOT);
|
|
1555
|
+
JSON_ASSERT((int)JSON_PHASE_ARRAY_COMMA == (int)JSON_FRAME_ARRAY);
|
|
1556
|
+
JSON_ASSERT((int)JSON_PHASE_OBJECT_COMMA == (int)JSON_FRAME_OBJECT);
|
|
1557
|
+
|
|
1558
|
+
return frame->phase = (enum json_frame_phase) frame->type;
|
|
1248
1559
|
}
|
|
1249
1560
|
|
|
1250
|
-
static
|
|
1561
|
+
ALWAYS_INLINE(static) void json_match_keyword(JSON_ParserState *state, const char *keyword, size_t offset)
|
|
1251
1562
|
{
|
|
1252
|
-
|
|
1563
|
+
// It is assumed that since `keyword` is always a literal, the compiler is able to constantize this
|
|
1564
|
+
// `strlen` and several other computations in that routine.
|
|
1253
1565
|
|
|
1254
|
-
|
|
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
|
-
}
|
|
1566
|
+
size_t len = strlen(keyword);
|
|
1260
1567
|
|
|
1261
|
-
|
|
1262
|
-
|
|
1263
|
-
|
|
1264
|
-
|
|
1265
|
-
|
|
1266
|
-
|
|
1267
|
-
}
|
|
1568
|
+
// Note: memcmp with a small power of two and a literal string compile to an integer comparison /
|
|
1569
|
+
// That's why we sometime compare starting from the first byte and sometimes from the second.
|
|
1570
|
+
if (rest(state) >= len && (memcmp(state->cursor + offset, keyword + offset, len - offset) == 0)) {
|
|
1571
|
+
state->cursor += len;
|
|
1572
|
+
return;
|
|
1573
|
+
}
|
|
1268
1574
|
|
|
1269
|
-
|
|
1270
|
-
|
|
1271
|
-
|
|
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
|
-
}
|
|
1575
|
+
bool eos = rest(state) < len && memcmp(state->cursor, keyword, rest(state)) == 0;
|
|
1576
|
+
raise_parse_error("unexpected token %s", state, eos);
|
|
1577
|
+
}
|
|
1277
1578
|
|
|
1278
|
-
|
|
1279
|
-
|
|
1280
|
-
|
|
1281
|
-
|
|
1282
|
-
|
|
1283
|
-
|
|
1284
|
-
|
|
1285
|
-
|
|
1579
|
+
// Parse an arbitrary JSON value iteratively. This is a state machine driven
|
|
1580
|
+
// entirely by the top frame's phase so it can stop at any value boundary and
|
|
1581
|
+
// resume purely from the frame stack. A JSON_FRAME_ROOT frame sits at the
|
|
1582
|
+
// bottom of the stack, so the stack is never empty mid-parse and the document
|
|
1583
|
+
// itself is just another frame whose value, once parsed, leaves its phase DONE.
|
|
1584
|
+
// When invoked in resumable mode, it returns true after parsing a complete document.
|
|
1585
|
+
// If reaching EOS without having parsed a complete document, either returns false
|
|
1586
|
+
// of raise a JSON::ParserError tagged with `@eos=true`.
|
|
1587
|
+
ALWAYS_INLINE(static) bool json_parse_any(JSON_ParserState *state, JSON_ParserConfig *config, bool resumable)
|
|
1588
|
+
{
|
|
1589
|
+
json_frame *frame = json_frame_stack_peek(state->frames);
|
|
1286
1590
|
|
|
1287
|
-
|
|
1288
|
-
|
|
1289
|
-
case
|
|
1290
|
-
|
|
1291
|
-
|
|
1292
|
-
|
|
1293
|
-
|
|
1591
|
+
switch (frame->phase) {
|
|
1592
|
+
case JSON_PHASE_DONE: JSON_UNREACHABLE_RETURN(false);
|
|
1593
|
+
case JSON_PHASE_ARRAY_COMMA: goto JSON_PHASE_ARRAY_COMMA;
|
|
1594
|
+
case JSON_PHASE_OBJECT_COMMA: goto JSON_PHASE_OBJECT_COMMA;
|
|
1595
|
+
case JSON_PHASE_VALUE: goto JSON_PHASE_VALUE;
|
|
1596
|
+
case JSON_PHASE_OBJECT_KEY: goto JSON_PHASE_OBJECT_KEY;
|
|
1597
|
+
case JSON_PHASE_OBJECT_COLON: goto JSON_PHASE_OBJECT_COLON;
|
|
1598
|
+
}
|
|
1599
|
+
JSON_UNREACHABLE_RETURN(false);
|
|
1294
1600
|
|
|
1295
|
-
|
|
1296
|
-
|
|
1297
|
-
|
|
1298
|
-
|
|
1299
|
-
|
|
1300
|
-
|
|
1301
|
-
|
|
1302
|
-
|
|
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;
|
|
1601
|
+
JSON_PHASE_VALUE: {
|
|
1602
|
+
json_eat_whitespace(state, config, true);
|
|
1603
|
+
|
|
1604
|
+
// A trailing comma lands us here expecting an element but finding the
|
|
1605
|
+
// closing bracket; hand off to ARRAY_COMMA to close. An empty array
|
|
1606
|
+
// closes inline at '[', so this position is only reached after a ','.
|
|
1607
|
+
if (config->allow_trailing_comma && frame->type == JSON_FRAME_ARRAY && peek(state) == ']') {
|
|
1608
|
+
goto JSON_PHASE_ARRAY_COMMA;
|
|
1317
1609
|
}
|
|
1318
|
-
case '[': {
|
|
1319
|
-
state->cursor++;
|
|
1320
|
-
json_eat_whitespace(state);
|
|
1321
|
-
long stack_head = state->stack->head;
|
|
1322
1610
|
|
|
1323
|
-
|
|
1324
|
-
|
|
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
|
-
}
|
|
1611
|
+
VALUE value;
|
|
1612
|
+
const char *value_start = state->cursor;
|
|
1334
1613
|
|
|
1335
|
-
|
|
1336
|
-
|
|
1614
|
+
switch (peek(state)) {
|
|
1615
|
+
case 'n':
|
|
1616
|
+
json_match_keyword(state, "null", 0);
|
|
1617
|
+
value = Qnil;
|
|
1618
|
+
break;
|
|
1337
1619
|
|
|
1338
|
-
|
|
1620
|
+
case 't':
|
|
1621
|
+
json_match_keyword(state, "true", 0);
|
|
1622
|
+
value = Qtrue;
|
|
1623
|
+
break;
|
|
1339
1624
|
|
|
1340
|
-
|
|
1341
|
-
|
|
1342
|
-
|
|
1343
|
-
|
|
1344
|
-
if (peek(state) == ']') {
|
|
1345
|
-
continue;
|
|
1346
|
-
}
|
|
1347
|
-
}
|
|
1348
|
-
json_parse_any(state, config);
|
|
1349
|
-
continue;
|
|
1350
|
-
}
|
|
1625
|
+
case 'f':
|
|
1626
|
+
json_match_keyword(state, "false", 1);
|
|
1627
|
+
value = Qfalse;
|
|
1628
|
+
break;
|
|
1351
1629
|
|
|
1352
|
-
|
|
1353
|
-
|
|
1354
|
-
|
|
1355
|
-
state->current_nesting--;
|
|
1356
|
-
state->in_array--;
|
|
1357
|
-
return json_push_value(state, config, json_decode_array(state, config, count));
|
|
1630
|
+
case 'N':
|
|
1631
|
+
if (!config->allow_nan) {
|
|
1632
|
+
raise_syntax_error("unexpected token %s", state);
|
|
1358
1633
|
}
|
|
1359
1634
|
|
|
1360
|
-
|
|
1361
|
-
|
|
1362
|
-
|
|
1363
|
-
}
|
|
1364
|
-
case '{': {
|
|
1365
|
-
const char *object_start_cursor = state->cursor;
|
|
1635
|
+
json_match_keyword(state, "NaN", 1);
|
|
1636
|
+
value = CNaN;
|
|
1637
|
+
break;
|
|
1366
1638
|
|
|
1367
|
-
|
|
1368
|
-
|
|
1369
|
-
|
|
1639
|
+
case 'I':
|
|
1640
|
+
if (!config->allow_nan) {
|
|
1641
|
+
raise_syntax_error("unexpected token %s", state);
|
|
1642
|
+
}
|
|
1370
1643
|
|
|
1371
|
-
|
|
1644
|
+
json_match_keyword(state, "Infinity", 0);
|
|
1645
|
+
value = CInfinity;
|
|
1646
|
+
break;
|
|
1647
|
+
|
|
1648
|
+
case '-': {
|
|
1372
1649
|
state->cursor++;
|
|
1373
|
-
|
|
1374
|
-
|
|
1375
|
-
|
|
1376
|
-
if (RB_UNLIKELY(
|
|
1377
|
-
|
|
1650
|
+
|
|
1651
|
+
value = json_parse_number(state, config, true, value_start, resumable);
|
|
1652
|
+
|
|
1653
|
+
if (RB_UNLIKELY(UNDEF_P(value) && config->allow_nan && peek(state) == 'I')) {
|
|
1654
|
+
state->cursor = value_start;
|
|
1655
|
+
json_match_keyword(state, "-Infinity", 1);
|
|
1656
|
+
value = CMinusInfinity;
|
|
1657
|
+
break;
|
|
1658
|
+
}
|
|
1659
|
+
|
|
1660
|
+
// Top level numbers are ambiguous when parsing streams, we can't
|
|
1661
|
+
// know if we parsed all the digits if we hit EOS.
|
|
1662
|
+
if (RB_UNLIKELY(resumable && eos(state))) {
|
|
1663
|
+
state->cursor = value_start;
|
|
1664
|
+
return false;
|
|
1378
1665
|
}
|
|
1379
1666
|
|
|
1380
|
-
if (
|
|
1381
|
-
|
|
1667
|
+
if (RB_UNLIKELY(UNDEF_P(value))) {
|
|
1668
|
+
raise_syntax_error_at("invalid number: %s", state, value_start);
|
|
1382
1669
|
}
|
|
1383
|
-
|
|
1670
|
+
break;
|
|
1671
|
+
}
|
|
1672
|
+
|
|
1673
|
+
case '0': case '1': case '2': case '3': case '4': case '5': case '6': case '7': case '8': case '9': {
|
|
1674
|
+
value = json_parse_number(state, config, false, value_start, resumable);
|
|
1384
1675
|
|
|
1385
|
-
|
|
1386
|
-
if
|
|
1387
|
-
|
|
1676
|
+
// Top level numbers are ambiguous when parsing streams, we can't
|
|
1677
|
+
// know if we parsed all the digits if we hit EOS.
|
|
1678
|
+
if (RB_UNLIKELY(resumable && eos(state))) {
|
|
1679
|
+
state->cursor = value_start;
|
|
1680
|
+
return false;
|
|
1681
|
+
}
|
|
1682
|
+
|
|
1683
|
+
if (RB_UNLIKELY(UNDEF_P(value))) {
|
|
1684
|
+
raise_syntax_error_at("invalid number: %s", state, value_start);
|
|
1685
|
+
}
|
|
1686
|
+
break;
|
|
1687
|
+
}
|
|
1688
|
+
|
|
1689
|
+
case '"': {
|
|
1690
|
+
// %r{\A"[^"\\\t\n\x00]*(?:\\[bfnrtu\\/"][^"\\]*)*"}
|
|
1691
|
+
value = json_parse_string(state, config, false);
|
|
1692
|
+
|
|
1693
|
+
if (RB_UNLIKELY(UNDEF_P(value))) {
|
|
1694
|
+
bool is_eos = eos(state);
|
|
1695
|
+
if (resumable && is_eos) {
|
|
1696
|
+
state->cursor = value_start;
|
|
1697
|
+
return false;
|
|
1698
|
+
}
|
|
1699
|
+
raise_parse_error("unexpected end of input, expected closing \"", state, is_eos);
|
|
1388
1700
|
}
|
|
1701
|
+
break;
|
|
1702
|
+
}
|
|
1703
|
+
|
|
1704
|
+
case '[': {
|
|
1389
1705
|
state->cursor++;
|
|
1706
|
+
// The '[' is consumed but its frame is only pushed below, so a
|
|
1707
|
+
// comment suspending here must resume from the bracket.
|
|
1708
|
+
json_eat_whitespace_resume_at(state, config, true, value_start);
|
|
1390
1709
|
|
|
1391
|
-
|
|
1710
|
+
const char next = peek(state);
|
|
1711
|
+
if (next == ']') {
|
|
1712
|
+
state->cursor++;
|
|
1713
|
+
value = json_decode_array(state, config, 0);
|
|
1714
|
+
break;
|
|
1715
|
+
} else if (resumable && eos(state)) {
|
|
1716
|
+
state->cursor = value_start;
|
|
1717
|
+
return false;
|
|
1718
|
+
}
|
|
1719
|
+
|
|
1720
|
+
state->current_nesting++;
|
|
1721
|
+
if (RB_UNLIKELY(config->max_nesting && (config->max_nesting < state->current_nesting))) {
|
|
1722
|
+
rb_raise(eNestingError, "nesting of %d is too deep", state->current_nesting);
|
|
1723
|
+
}
|
|
1724
|
+
state->in_array++;
|
|
1725
|
+
|
|
1726
|
+
// Phase stays VALUE: the next iteration reads the first element.
|
|
1727
|
+
frame = json_frame_stack_push(state, (json_frame){
|
|
1728
|
+
.type = JSON_FRAME_ARRAY,
|
|
1729
|
+
.phase = JSON_PHASE_VALUE,
|
|
1730
|
+
.value_stack_head = state->value_stack->head,
|
|
1731
|
+
});
|
|
1732
|
+
goto JSON_PHASE_VALUE;
|
|
1392
1733
|
}
|
|
1393
1734
|
|
|
1394
|
-
|
|
1395
|
-
|
|
1735
|
+
case '{': {
|
|
1736
|
+
state->cursor++;
|
|
1737
|
+
// Same as '[': the frame is only pushed below.
|
|
1738
|
+
json_eat_whitespace_resume_at(state, config, true, value_start);
|
|
1396
1739
|
|
|
1397
|
-
|
|
1398
|
-
if (next_char == '}') {
|
|
1740
|
+
if (peek(state) == '}') {
|
|
1399
1741
|
state->cursor++;
|
|
1400
|
-
state
|
|
1401
|
-
|
|
1742
|
+
value = json_decode_object(state, config, 0);
|
|
1743
|
+
break;
|
|
1744
|
+
} else if (resumable && eos(state)) {
|
|
1745
|
+
state->cursor = value_start;
|
|
1746
|
+
return false;
|
|
1747
|
+
}
|
|
1748
|
+
|
|
1749
|
+
state->current_nesting++;
|
|
1750
|
+
if (RB_UNLIKELY(config->max_nesting && (config->max_nesting < state->current_nesting))) {
|
|
1751
|
+
rb_raise(eNestingError, "nesting of %d is too deep", state->current_nesting);
|
|
1752
|
+
}
|
|
1402
1753
|
|
|
1403
|
-
|
|
1404
|
-
|
|
1405
|
-
|
|
1406
|
-
|
|
1407
|
-
|
|
1754
|
+
// Phase KEY: the next iteration reads the first key.
|
|
1755
|
+
frame = json_frame_stack_push(state, (json_frame){
|
|
1756
|
+
.type = JSON_FRAME_OBJECT,
|
|
1757
|
+
.phase = JSON_PHASE_OBJECT_KEY,
|
|
1758
|
+
.value_stack_head = state->value_stack->head,
|
|
1759
|
+
.start_offset = value_start - state->start,
|
|
1760
|
+
});
|
|
1761
|
+
goto JSON_PHASE_OBJECT_KEY;
|
|
1762
|
+
}
|
|
1408
1763
|
|
|
1409
|
-
|
|
1764
|
+
case 0:
|
|
1765
|
+
// peek() returns 0 both at end-of-stream and for a literal NUL byte in the
|
|
1766
|
+
// buffer. Only a genuine EOS means "feed me more"; a NUL byte that is not at
|
|
1767
|
+
// EOS is just an invalid character.
|
|
1768
|
+
if (eos(state)) {
|
|
1769
|
+
return false;
|
|
1770
|
+
} else {
|
|
1771
|
+
raise_syntax_error("unexpected NULL byte: %s", state);
|
|
1410
1772
|
}
|
|
1773
|
+
default:
|
|
1774
|
+
raise_syntax_error("unexpected character: %s", state);
|
|
1775
|
+
}
|
|
1411
1776
|
|
|
1412
|
-
|
|
1413
|
-
|
|
1414
|
-
json_eat_whitespace(state);
|
|
1777
|
+
json_push_value(state, config, value);
|
|
1778
|
+
json_value_completed(frame);
|
|
1415
1779
|
|
|
1416
|
-
|
|
1417
|
-
|
|
1418
|
-
|
|
1419
|
-
|
|
1420
|
-
|
|
1780
|
+
switch (frame->phase) {
|
|
1781
|
+
case JSON_PHASE_DONE: return true;
|
|
1782
|
+
case JSON_PHASE_ARRAY_COMMA: goto JSON_PHASE_ARRAY_COMMA;
|
|
1783
|
+
case JSON_PHASE_OBJECT_COMMA: goto JSON_PHASE_OBJECT_COMMA;
|
|
1784
|
+
case JSON_PHASE_VALUE: goto JSON_PHASE_VALUE;
|
|
1785
|
+
case JSON_PHASE_OBJECT_KEY: JSON_UNREACHABLE_RETURN(false);
|
|
1786
|
+
case JSON_PHASE_OBJECT_COLON: goto JSON_PHASE_OBJECT_COLON;
|
|
1787
|
+
}
|
|
1788
|
+
JSON_UNREACHABLE_RETURN(false);
|
|
1789
|
+
}
|
|
1421
1790
|
|
|
1422
|
-
|
|
1423
|
-
|
|
1424
|
-
}
|
|
1425
|
-
json_parse_string(state, config, true);
|
|
1791
|
+
JSON_PHASE_OBJECT_KEY: {
|
|
1792
|
+
JSON_ASSERT(frame->type == JSON_FRAME_OBJECT);
|
|
1426
1793
|
|
|
1427
|
-
|
|
1428
|
-
|
|
1429
|
-
|
|
1430
|
-
|
|
1431
|
-
|
|
1794
|
+
json_eat_whitespace(state, config, true);
|
|
1795
|
+
|
|
1796
|
+
// A trailing comma lands us here expecting a key but finding the closing
|
|
1797
|
+
// brace; hand off to OBJECT_COMMA to close. An empty object closes inline
|
|
1798
|
+
// at '{', so this position is only reached after a ','.
|
|
1799
|
+
if (config->allow_trailing_comma && peek(state) == '}') {
|
|
1800
|
+
goto JSON_PHASE_OBJECT_COMMA;
|
|
1801
|
+
}
|
|
1432
1802
|
|
|
1433
|
-
|
|
1803
|
+
const char *start = state->cursor;
|
|
1434
1804
|
|
|
1435
|
-
|
|
1805
|
+
if (RB_LIKELY(peek(state) == '"')) {
|
|
1806
|
+
VALUE string = json_parse_string(state, config, true);
|
|
1807
|
+
if (UNDEF_P(string)) {
|
|
1808
|
+
if (resumable) {
|
|
1809
|
+
state->cursor = start;
|
|
1810
|
+
return false;
|
|
1811
|
+
} else {
|
|
1812
|
+
raise_syntax_error("unexpected end of input, expected closing \"", state);
|
|
1436
1813
|
}
|
|
1814
|
+
}
|
|
1815
|
+
json_push_value(state, config, string);
|
|
1816
|
+
frame->phase = JSON_PHASE_OBJECT_COLON;
|
|
1817
|
+
goto JSON_PHASE_OBJECT_COLON;
|
|
1818
|
+
} else if (resumable && eos(state)) {
|
|
1819
|
+
return false;
|
|
1820
|
+
} else {
|
|
1821
|
+
// The message differs for the first key vs. a key after a
|
|
1822
|
+
// ',': the first is the only one reached with nothing pushed
|
|
1823
|
+
// for this object yet.
|
|
1824
|
+
if (json_frame_entry_count(frame, state->value_stack) == 0) {
|
|
1825
|
+
raise_syntax_error("expected object key, got %s", state);
|
|
1826
|
+
} else {
|
|
1827
|
+
raise_syntax_error("expected object key, got: %s", state);
|
|
1828
|
+
}
|
|
1829
|
+
}
|
|
1830
|
+
JSON_UNREACHABLE_RETURN(false);
|
|
1831
|
+
}
|
|
1437
1832
|
|
|
1438
|
-
|
|
1833
|
+
JSON_PHASE_OBJECT_COLON: {
|
|
1834
|
+
JSON_ASSERT(frame->type == JSON_FRAME_OBJECT);
|
|
1835
|
+
|
|
1836
|
+
json_eat_whitespace(state, config, true);
|
|
1837
|
+
|
|
1838
|
+
if (RB_LIKELY(peek(state) == ':')) {
|
|
1839
|
+
state->cursor++;
|
|
1840
|
+
frame->phase = JSON_PHASE_VALUE;
|
|
1841
|
+
goto JSON_PHASE_VALUE;
|
|
1842
|
+
} else if (resumable && eos(state)) {
|
|
1843
|
+
return false;
|
|
1844
|
+
} else {
|
|
1845
|
+
// First colon (only the first pair's key is pushed, nothing
|
|
1846
|
+
// else) vs. a later one.
|
|
1847
|
+
if (json_frame_entry_count(frame, state->value_stack) == 1) {
|
|
1848
|
+
raise_syntax_error("expected ':' after object key", state);
|
|
1849
|
+
} else {
|
|
1850
|
+
raise_syntax_error("expected ':' after object key, got: %s", state);
|
|
1439
1851
|
}
|
|
1440
|
-
break;
|
|
1441
1852
|
}
|
|
1853
|
+
JSON_UNREACHABLE_RETURN(false);
|
|
1854
|
+
}
|
|
1442
1855
|
|
|
1443
|
-
|
|
1444
|
-
|
|
1445
|
-
break;
|
|
1856
|
+
JSON_PHASE_ARRAY_COMMA: {
|
|
1857
|
+
JSON_ASSERT(frame->type == JSON_FRAME_ARRAY);
|
|
1446
1858
|
|
|
1447
|
-
|
|
1448
|
-
|
|
1449
|
-
|
|
1859
|
+
json_eat_whitespace(state, config, true);
|
|
1860
|
+
|
|
1861
|
+
const char next_char = peek(state);
|
|
1862
|
+
|
|
1863
|
+
if (RB_LIKELY(next_char == ',')) {
|
|
1864
|
+
state->cursor++;
|
|
1865
|
+
// Commit the phase before eating the whitespace that follows: an
|
|
1866
|
+
// incomplete comment there would suspend the parse, and a phase not
|
|
1867
|
+
// yet advanced past the ',' would drop it on resume. A trailing comma
|
|
1868
|
+
// is recognized in JSON_PHASE_VALUE once the ']' is in the buffer.
|
|
1869
|
+
frame->phase = JSON_PHASE_VALUE;
|
|
1870
|
+
goto JSON_PHASE_VALUE;
|
|
1871
|
+
} else if (next_char == ']') {
|
|
1872
|
+
state->cursor++;
|
|
1873
|
+
long count = json_frame_entry_count(frame, state->value_stack);
|
|
1874
|
+
state->current_nesting--;
|
|
1875
|
+
state->in_array--;
|
|
1876
|
+
|
|
1877
|
+
json_push_value(state, config, json_decode_array(state, config, count));
|
|
1878
|
+
json_frame_stack_pop(state->frames);
|
|
1879
|
+
frame = json_frame_stack_peek(state->frames);
|
|
1880
|
+
|
|
1881
|
+
json_value_completed(frame);
|
|
1882
|
+
|
|
1883
|
+
switch (frame->phase) {
|
|
1884
|
+
case JSON_PHASE_DONE: return true;
|
|
1885
|
+
case JSON_PHASE_ARRAY_COMMA: goto JSON_PHASE_ARRAY_COMMA;
|
|
1886
|
+
case JSON_PHASE_OBJECT_COMMA: goto JSON_PHASE_OBJECT_COMMA;
|
|
1887
|
+
case JSON_PHASE_VALUE: goto JSON_PHASE_VALUE;
|
|
1888
|
+
case JSON_PHASE_OBJECT_KEY: JSON_UNREACHABLE_RETURN(false);
|
|
1889
|
+
case JSON_PHASE_OBJECT_COLON: goto JSON_PHASE_OBJECT_COLON;
|
|
1890
|
+
}
|
|
1891
|
+
} else if (resumable && eos(state)) {
|
|
1892
|
+
return false;
|
|
1893
|
+
} else {
|
|
1894
|
+
raise_syntax_error("expected ',' or ']' after array value", state);
|
|
1895
|
+
}
|
|
1896
|
+
JSON_UNREACHABLE_RETURN(false);
|
|
1450
1897
|
}
|
|
1451
1898
|
|
|
1452
|
-
|
|
1453
|
-
|
|
1899
|
+
JSON_PHASE_OBJECT_COMMA: {
|
|
1900
|
+
JSON_ASSERT(frame->type == JSON_FRAME_OBJECT);
|
|
1901
|
+
|
|
1902
|
+
json_eat_whitespace(state, config, true);
|
|
1903
|
+
const char next_char = peek(state);
|
|
1904
|
+
|
|
1905
|
+
if (RB_LIKELY(next_char == ',')) {
|
|
1906
|
+
state->cursor++;
|
|
1907
|
+
// Commit the phase before eating the whitespace that follows: an
|
|
1908
|
+
// incomplete comment there would suspend the parse, and a phase not
|
|
1909
|
+
// yet advanced past the ',' would drop it on resume. A trailing comma
|
|
1910
|
+
// is recognized in JSON_PHASE_OBJECT_KEY once the '}' is in the buffer.
|
|
1911
|
+
frame->phase = JSON_PHASE_OBJECT_KEY;
|
|
1912
|
+
goto JSON_PHASE_OBJECT_KEY;
|
|
1913
|
+
} else if (next_char == '}') {
|
|
1914
|
+
state->cursor++;
|
|
1915
|
+
state->current_nesting--;
|
|
1916
|
+
size_t count = json_frame_entry_count(frame, state->value_stack);
|
|
1917
|
+
|
|
1918
|
+
// Temporary rewind cursor in case an error is raised
|
|
1919
|
+
const char *final_cursor = state->cursor;
|
|
1920
|
+
state->cursor = state->start + frame->start_offset;
|
|
1921
|
+
VALUE object = json_decode_object(state, config, count);
|
|
1922
|
+
state->cursor = final_cursor;
|
|
1923
|
+
|
|
1924
|
+
json_push_value(state, config, object);
|
|
1925
|
+
json_frame_stack_pop(state->frames);
|
|
1926
|
+
frame = json_frame_stack_peek(state->frames);
|
|
1927
|
+
json_value_completed(frame);
|
|
1928
|
+
|
|
1929
|
+
switch (frame->phase) {
|
|
1930
|
+
case JSON_PHASE_DONE: return true;
|
|
1931
|
+
case JSON_PHASE_ARRAY_COMMA: goto JSON_PHASE_ARRAY_COMMA;
|
|
1932
|
+
case JSON_PHASE_OBJECT_COMMA: goto JSON_PHASE_OBJECT_COMMA;
|
|
1933
|
+
case JSON_PHASE_VALUE: goto JSON_PHASE_VALUE;
|
|
1934
|
+
case JSON_PHASE_OBJECT_KEY: JSON_UNREACHABLE_RETURN(false);
|
|
1935
|
+
case JSON_PHASE_OBJECT_COLON: goto JSON_PHASE_OBJECT_COLON;
|
|
1936
|
+
}
|
|
1937
|
+
} else if (resumable && eos(state)) {
|
|
1938
|
+
return false;
|
|
1939
|
+
} else {
|
|
1940
|
+
raise_syntax_error("expected ',' or '}' after object value, got: %s", state);
|
|
1941
|
+
}
|
|
1942
|
+
JSON_UNREACHABLE_RETURN(false);
|
|
1943
|
+
}
|
|
1944
|
+
|
|
1945
|
+
JSON_UNREACHABLE_RETURN(false);
|
|
1454
1946
|
}
|
|
1455
1947
|
|
|
1456
|
-
static void json_ensure_eof(JSON_ParserState *state)
|
|
1948
|
+
static void json_ensure_eof(JSON_ParserState *state, JSON_ParserConfig *config)
|
|
1457
1949
|
{
|
|
1458
|
-
json_eat_whitespace(state);
|
|
1950
|
+
json_eat_whitespace(state, config, true);
|
|
1459
1951
|
if (!eos(state)) {
|
|
1460
|
-
|
|
1952
|
+
raise_syntax_error("unexpected token at end of stream %s", state);
|
|
1461
1953
|
}
|
|
1462
1954
|
}
|
|
1463
1955
|
|
|
@@ -1495,6 +1987,8 @@ static VALUE convert_encoding(VALUE source)
|
|
|
1495
1987
|
struct parser_config_init_args {
|
|
1496
1988
|
JSON_ParserConfig *config;
|
|
1497
1989
|
VALUE self;
|
|
1990
|
+
VALUE unknown_keywords;
|
|
1991
|
+
bool strict;
|
|
1498
1992
|
};
|
|
1499
1993
|
|
|
1500
1994
|
static void parser_config_wb_write(VALUE self, VALUE *dest, VALUE val)
|
|
@@ -1512,6 +2006,7 @@ static int parser_config_init_i(VALUE key, VALUE val, VALUE data)
|
|
|
1512
2006
|
if (key == sym_max_nesting) { config->max_nesting = RTEST(val) ? FIX2INT(val) : 0; }
|
|
1513
2007
|
else if (key == sym_allow_nan) { config->allow_nan = RTEST(val); }
|
|
1514
2008
|
else if (key == sym_allow_trailing_comma) { config->allow_trailing_comma = RTEST(val); }
|
|
2009
|
+
else if (key == sym_allow_comments) { config->on_comment = RTEST(val) ? JSON_IGNORE : JSON_RAISE; }
|
|
1515
2010
|
else if (key == sym_allow_control_characters) { config->allow_control_characters = RTEST(val); }
|
|
1516
2011
|
else if (key == sym_allow_invalid_escape) { config->allow_invalid_escape = RTEST(val); }
|
|
1517
2012
|
else if (key == sym_symbolize_names) { config->symbolize_names = RTEST(val); }
|
|
@@ -1547,27 +2042,42 @@ static int parser_config_init_i(VALUE key, VALUE val, VALUE data)
|
|
|
1547
2042
|
}
|
|
1548
2043
|
}
|
|
1549
2044
|
}
|
|
2045
|
+
else if (args->strict) {
|
|
2046
|
+
if (!args->unknown_keywords) {
|
|
2047
|
+
args->unknown_keywords = rb_obj_hide(rb_ary_new());
|
|
2048
|
+
}
|
|
2049
|
+
rb_ary_push(args->unknown_keywords, key);
|
|
2050
|
+
}
|
|
1550
2051
|
|
|
1551
2052
|
return ST_CONTINUE;
|
|
1552
2053
|
}
|
|
1553
2054
|
|
|
1554
|
-
static void parser_config_init(JSON_ParserConfig *config, VALUE opts, VALUE self)
|
|
2055
|
+
static void parser_config_init(JSON_ParserConfig *config, VALUE opts, VALUE self, bool strict)
|
|
1555
2056
|
{
|
|
1556
2057
|
config->max_nesting = 100;
|
|
1557
2058
|
|
|
1558
2059
|
struct parser_config_init_args args = {
|
|
1559
2060
|
.config = config,
|
|
1560
2061
|
.self = self,
|
|
2062
|
+
.strict = strict,
|
|
1561
2063
|
};
|
|
1562
2064
|
|
|
1563
|
-
if (
|
|
1564
|
-
|
|
1565
|
-
|
|
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
|
-
}
|
|
2065
|
+
if (NIL_P(opts)) return;
|
|
2066
|
+
Check_Type(opts, T_HASH);
|
|
2067
|
+
if (RHASH_SIZE(opts) == 0) return;
|
|
1570
2068
|
|
|
2069
|
+
// We assume in most cases few keys are set so it's faster to go over
|
|
2070
|
+
// the provided keys than to check all possible keys.
|
|
2071
|
+
rb_hash_foreach(opts, parser_config_init_i, (VALUE)&args);
|
|
2072
|
+
|
|
2073
|
+
if (RB_UNLIKELY(args.unknown_keywords)) {
|
|
2074
|
+
if (RARRAY_LEN(args.unknown_keywords) == 1) {
|
|
2075
|
+
rb_raise(rb_eArgError, "unknown keyword: %" PRIsVALUE, RARRAY_AREF(args.unknown_keywords, 0));
|
|
2076
|
+
}
|
|
2077
|
+
else {
|
|
2078
|
+
VALUE keywords = rb_ary_join(args.unknown_keywords, rb_utf8_str_new_cstr(", "));
|
|
2079
|
+
rb_raise(rb_eArgError, "unknown keywords: %" PRIsVALUE, keywords);
|
|
2080
|
+
}
|
|
1571
2081
|
}
|
|
1572
2082
|
}
|
|
1573
2083
|
|
|
@@ -1576,30 +2086,16 @@ static void parser_config_init(JSON_ParserConfig *config, VALUE opts, VALUE self
|
|
|
1576
2086
|
*
|
|
1577
2087
|
* Creates a new JSON::Ext::ParserConfig instance.
|
|
1578
2088
|
*
|
|
1579
|
-
*
|
|
1580
|
-
*
|
|
2089
|
+
* Argument +opts+, if given, contains a \Hash of options for the parsing.
|
|
2090
|
+
* See {Parsing Options}[#module-JSON-label-Parsing+Options].
|
|
1581
2091
|
*
|
|
1582
|
-
* _opts_ can have the following keys:
|
|
1583
|
-
* * *max_nesting*: The maximum depth of nesting allowed in the parsed data
|
|
1584
|
-
* structures. Disable depth checking with :max_nesting => false|nil|0, it
|
|
1585
|
-
* defaults to 100.
|
|
1586
|
-
* * *allow_nan*: If set to true, allow NaN, Infinity and -Infinity in
|
|
1587
|
-
* defiance of RFC 4627 to be parsed by the Parser. This option defaults to
|
|
1588
|
-
* false.
|
|
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.
|
|
1596
2092
|
*/
|
|
1597
2093
|
static VALUE cParserConfig_initialize(VALUE self, VALUE opts)
|
|
1598
2094
|
{
|
|
1599
2095
|
rb_check_frozen(self);
|
|
1600
2096
|
GET_PARSER_CONFIG;
|
|
1601
2097
|
|
|
1602
|
-
parser_config_init(config, opts, self);
|
|
2098
|
+
parser_config_init(config, opts, self, false);
|
|
1603
2099
|
|
|
1604
2100
|
return self;
|
|
1605
2101
|
}
|
|
@@ -1616,35 +2112,64 @@ static VALUE cParser_parse(JSON_ParserConfig *config, VALUE src)
|
|
|
1616
2112
|
}
|
|
1617
2113
|
|
|
1618
2114
|
VALUE rvalue_stack_buffer[RVALUE_STACK_INITIAL_CAPA];
|
|
1619
|
-
rvalue_stack
|
|
2115
|
+
rvalue_stack value_stack = {
|
|
1620
2116
|
.type = RVALUE_STACK_STACK_ALLOCATED,
|
|
1621
2117
|
.ptr = rvalue_stack_buffer,
|
|
1622
2118
|
.capa = RVALUE_STACK_INITIAL_CAPA,
|
|
1623
2119
|
};
|
|
1624
2120
|
|
|
2121
|
+
// Seed the frame stack with the root frame, establishing the invariant that
|
|
2122
|
+
// json_parse_any always has a top frame to dispatch on (so the stack is never
|
|
2123
|
+
// empty mid-parse).
|
|
2124
|
+
json_frame frame_stack_buffer[JSON_FRAME_STACK_INITIAL_CAPA];
|
|
2125
|
+
frame_stack_buffer[0] = (json_frame){
|
|
2126
|
+
.type = JSON_FRAME_ROOT,
|
|
2127
|
+
.phase = JSON_PHASE_VALUE,
|
|
2128
|
+
};
|
|
2129
|
+
json_frame_stack frames = {
|
|
2130
|
+
.type = RVALUE_STACK_STACK_ALLOCATED,
|
|
2131
|
+
.ptr = frame_stack_buffer,
|
|
2132
|
+
.capa = JSON_FRAME_STACK_INITIAL_CAPA,
|
|
2133
|
+
.head = 1,
|
|
2134
|
+
};
|
|
2135
|
+
|
|
1625
2136
|
long len;
|
|
1626
2137
|
const char *start;
|
|
1627
2138
|
|
|
1628
2139
|
RSTRING_GETMEM(Vsource, start, len);
|
|
1629
2140
|
|
|
1630
|
-
VALUE
|
|
2141
|
+
VALUE value_stack_handle = 0;
|
|
2142
|
+
VALUE frame_stack_handle = 0;
|
|
1631
2143
|
JSON_ParserState _state = {
|
|
1632
2144
|
.start = start,
|
|
1633
2145
|
.cursor = start,
|
|
1634
2146
|
.end = start + len,
|
|
1635
|
-
.
|
|
1636
|
-
.
|
|
2147
|
+
.value_stack = &value_stack,
|
|
2148
|
+
.value_stack_handle = &value_stack_handle,
|
|
2149
|
+
.frames = &frames,
|
|
2150
|
+
.frame_stack_handle = &frame_stack_handle,
|
|
1637
2151
|
};
|
|
1638
2152
|
JSON_ParserState *state = &_state;
|
|
1639
2153
|
|
|
1640
|
-
|
|
2154
|
+
bool complete = json_parse_any(state, config, false);
|
|
2155
|
+
|
|
2156
|
+
// The root document value is parsed; it is the lone survivor on
|
|
2157
|
+
// the rvalue stack.
|
|
2158
|
+
VALUE result = complete ? *rvalue_stack_peek(state->value_stack, 1) : Qundef;
|
|
1641
2159
|
|
|
1642
2160
|
// This may be skipped in case of exception, but
|
|
1643
2161
|
// it won't cause a leak.
|
|
1644
|
-
rvalue_stack_eagerly_release(
|
|
1645
|
-
|
|
2162
|
+
rvalue_stack_eagerly_release(value_stack_handle);
|
|
2163
|
+
json_frame_stack_eagerly_release(frame_stack_handle);
|
|
2164
|
+
RB_GC_GUARD(value_stack_handle);
|
|
2165
|
+
RB_GC_GUARD(frame_stack_handle);
|
|
1646
2166
|
RB_GC_GUARD(Vsource);
|
|
1647
|
-
|
|
2167
|
+
|
|
2168
|
+
if (complete) {
|
|
2169
|
+
json_ensure_eof(state, config);
|
|
2170
|
+
} else {
|
|
2171
|
+
raise_eos_error("unexpected end of input", state);
|
|
2172
|
+
}
|
|
1648
2173
|
|
|
1649
2174
|
return result;
|
|
1650
2175
|
}
|
|
@@ -1666,7 +2191,7 @@ static VALUE cParser_m_parse(VALUE klass, VALUE Vsource, VALUE opts)
|
|
|
1666
2191
|
{
|
|
1667
2192
|
JSON_ParserConfig _config = {0};
|
|
1668
2193
|
JSON_ParserConfig *config = &_config;
|
|
1669
|
-
parser_config_init(config, opts, false);
|
|
2194
|
+
parser_config_init(config, opts, Qfalse, false);
|
|
1670
2195
|
|
|
1671
2196
|
return cParser_parse(config, Vsource);
|
|
1672
2197
|
}
|
|
@@ -1674,23 +2199,35 @@ static VALUE cParser_m_parse(VALUE klass, VALUE Vsource, VALUE opts)
|
|
|
1674
2199
|
static void JSON_ParserConfig_mark(void *ptr)
|
|
1675
2200
|
{
|
|
1676
2201
|
JSON_ParserConfig *config = ptr;
|
|
1677
|
-
|
|
1678
|
-
|
|
2202
|
+
rb_gc_mark_movable(config->on_load_proc);
|
|
2203
|
+
rb_gc_mark_movable(config->decimal_class);
|
|
1679
2204
|
}
|
|
1680
2205
|
|
|
1681
2206
|
static size_t JSON_ParserConfig_memsize(const void *ptr)
|
|
1682
2207
|
{
|
|
2208
|
+
#ifdef HAVE_RUBY_TYPED_EMBEDDABLE
|
|
2209
|
+
return 0;
|
|
2210
|
+
#else
|
|
1683
2211
|
return sizeof(JSON_ParserConfig);
|
|
2212
|
+
#endif
|
|
2213
|
+
}
|
|
2214
|
+
|
|
2215
|
+
static void JSON_ParserConfig_compact(void *ptr)
|
|
2216
|
+
{
|
|
2217
|
+
JSON_ParserConfig *config = ptr;
|
|
2218
|
+
config->on_load_proc = rb_gc_location(config->on_load_proc);
|
|
2219
|
+
config->decimal_class = rb_gc_location(config->decimal_class);
|
|
1684
2220
|
}
|
|
1685
2221
|
|
|
1686
2222
|
static const rb_data_type_t JSON_ParserConfig_type = {
|
|
1687
2223
|
.wrap_struct_name = "JSON::Ext::Parser/ParserConfig",
|
|
1688
2224
|
.function = {
|
|
1689
|
-
JSON_ParserConfig_mark,
|
|
1690
|
-
RUBY_DEFAULT_FREE,
|
|
1691
|
-
JSON_ParserConfig_memsize,
|
|
2225
|
+
.dmark = JSON_ParserConfig_mark,
|
|
2226
|
+
.dfree = RUBY_DEFAULT_FREE,
|
|
2227
|
+
.dsize = JSON_ParserConfig_memsize,
|
|
2228
|
+
.dcompact = JSON_ParserConfig_compact,
|
|
1692
2229
|
},
|
|
1693
|
-
.flags =
|
|
2230
|
+
.flags = RUBY_TYPED_THREAD_SAFE_FREE | RUBY_TYPED_WB_PROTECTED | RUBY_TYPED_FROZEN_SHAREABLE | RUBY_TYPED_EMBEDDABLE,
|
|
1694
2231
|
};
|
|
1695
2232
|
|
|
1696
2233
|
static VALUE cJSON_parser_s_allocate(VALUE klass)
|
|
@@ -1699,6 +2236,602 @@ static VALUE cJSON_parser_s_allocate(VALUE klass)
|
|
|
1699
2236
|
return TypedData_Make_Struct(klass, JSON_ParserConfig, &JSON_ParserConfig_type, config);
|
|
1700
2237
|
}
|
|
1701
2238
|
|
|
2239
|
+
static void json_str_clear(VALUE str)
|
|
2240
|
+
{
|
|
2241
|
+
if (RB_OBJ_FROZEN_RAW(str)) {
|
|
2242
|
+
return;
|
|
2243
|
+
}
|
|
2244
|
+
rb_str_replace(str, JSON_empty_string);
|
|
2245
|
+
}
|
|
2246
|
+
|
|
2247
|
+
typedef struct JSON_ResumableParserStruct {
|
|
2248
|
+
JSON_ParserConfig config;
|
|
2249
|
+
JSON_ParserState state;
|
|
2250
|
+
rvalue_stack value_stack;
|
|
2251
|
+
json_frame_stack frames;
|
|
2252
|
+
VALUE buffer;
|
|
2253
|
+
size_t parsed_bytes;
|
|
2254
|
+
size_t incomplete_bytes;
|
|
2255
|
+
bool complete;
|
|
2256
|
+
bool in_use;
|
|
2257
|
+
} JSON_ResumableParser;
|
|
2258
|
+
|
|
2259
|
+
static void JSON_ResumableParser_mark(void *ptr)
|
|
2260
|
+
{
|
|
2261
|
+
JSON_ResumableParser *parser = (JSON_ResumableParser *)ptr;
|
|
2262
|
+
JSON_ParserConfig_mark(&parser->config);
|
|
2263
|
+
rvalue_stack_mark(&parser->value_stack);
|
|
2264
|
+
rvalue_cache_mark(&parser->state.name_cache);
|
|
2265
|
+
rb_gc_mark(parser->buffer); // pin the buffer
|
|
2266
|
+
rb_gc_mark_movable(parser->state.parser);
|
|
2267
|
+
}
|
|
2268
|
+
|
|
2269
|
+
static void JSON_ResumableParser_free(void *ptr)
|
|
2270
|
+
{
|
|
2271
|
+
JSON_ResumableParser *parser = (JSON_ResumableParser *)ptr;
|
|
2272
|
+
rvalue_stack_free_buffer(&parser->value_stack);
|
|
2273
|
+
json_frame_stack_free_buffer(&parser->frames);
|
|
2274
|
+
}
|
|
2275
|
+
|
|
2276
|
+
static size_t JSON_ResumableParser_memsize(const void *ptr)
|
|
2277
|
+
{
|
|
2278
|
+
const JSON_ResumableParser *parser = (const JSON_ResumableParser *)ptr;
|
|
2279
|
+
size_t memsize = JSON_ParserConfig_memsize(&parser->config);
|
|
2280
|
+
memsize += rvalue_stack_memsize(&parser->value_stack);
|
|
2281
|
+
memsize += json_frame_stack_memsize(&parser->frames);
|
|
2282
|
+
#ifndef HAVE_RUBY_TYPED_EMBEDDABLE
|
|
2283
|
+
memsize += (
|
|
2284
|
+
sizeof(JSON_ResumableParser)
|
|
2285
|
+
- sizeof(JSON_ParserState)
|
|
2286
|
+
- sizeof(JSON_ParserConfig)
|
|
2287
|
+
- sizeof(rvalue_stack)
|
|
2288
|
+
- sizeof(json_frame_stack)
|
|
2289
|
+
);
|
|
2290
|
+
#endif
|
|
2291
|
+
return memsize;
|
|
2292
|
+
}
|
|
2293
|
+
|
|
2294
|
+
static void JSON_ResumableParser_compact(void *ptr)
|
|
2295
|
+
{
|
|
2296
|
+
JSON_ResumableParser *parser = (JSON_ResumableParser *)ptr;
|
|
2297
|
+
JSON_ParserConfig_compact(&parser->config);
|
|
2298
|
+
rvalue_stack_compact(&parser->value_stack);
|
|
2299
|
+
rvalue_cache_compact(&parser->state.name_cache);
|
|
2300
|
+
parser->buffer = rb_gc_location(parser->buffer);
|
|
2301
|
+
parser->state.parser = rb_gc_location(parser->state.parser);
|
|
2302
|
+
}
|
|
2303
|
+
|
|
2304
|
+
static const rb_data_type_t JSON_ResumableParser_type = {
|
|
2305
|
+
.wrap_struct_name = "JSON::Ext::ResumableParser",
|
|
2306
|
+
.function = {
|
|
2307
|
+
JSON_ResumableParser_mark,
|
|
2308
|
+
JSON_ResumableParser_free,
|
|
2309
|
+
JSON_ResumableParser_memsize,
|
|
2310
|
+
JSON_ResumableParser_compact,
|
|
2311
|
+
},
|
|
2312
|
+
// RUBY_TYPED_WB_PROTECTED is deliberately not declared because
|
|
2313
|
+
// this is a superset of JSON_Parser_rvalue_stack_type, so we'd need
|
|
2314
|
+
// to trigger a lot of write barriers.
|
|
2315
|
+
.flags = RUBY_TYPED_THREAD_SAFE_FREE | RUBY_TYPED_EMBEDDABLE,
|
|
2316
|
+
};
|
|
2317
|
+
|
|
2318
|
+
static VALUE cResumableParser_allocate(VALUE klass)
|
|
2319
|
+
{
|
|
2320
|
+
JSON_ResumableParser *parser;
|
|
2321
|
+
VALUE obj = TypedData_Make_Struct(klass, JSON_ResumableParser, &JSON_ResumableParser_type, parser);
|
|
2322
|
+
parser->state.in_array++;
|
|
2323
|
+
parser->state.parser = obj;
|
|
2324
|
+
return obj;
|
|
2325
|
+
}
|
|
2326
|
+
|
|
2327
|
+
static inline JSON_ResumableParser *cResumableParser_get(VALUE self)
|
|
2328
|
+
{
|
|
2329
|
+
JSON_ResumableParser *parser;
|
|
2330
|
+
TypedData_Get_Struct(self, JSON_ResumableParser, &JSON_ResumableParser_type, parser);
|
|
2331
|
+
return parser;
|
|
2332
|
+
}
|
|
2333
|
+
|
|
2334
|
+
/*
|
|
2335
|
+
* call-seq: new(opts => {})
|
|
2336
|
+
*
|
|
2337
|
+
* Creates a new JSON::ResumableParser instance.
|
|
2338
|
+
*
|
|
2339
|
+
* Argument +opts+, if given, contains a \Hash of options for the parsing.
|
|
2340
|
+
* See {Parsing Options}[#module-JSON-label-Parsing+Options].
|
|
2341
|
+
*
|
|
2342
|
+
* A ResumableParser is able to parse partial documents and resume parsing later
|
|
2343
|
+
* when more of the document is provided:
|
|
2344
|
+
*
|
|
2345
|
+
* parser = JSON::ResumableParser.new
|
|
2346
|
+
* parser << '{"user": "george", "role": "ad'
|
|
2347
|
+
* parser.parse # => false
|
|
2348
|
+
* parser.eos? # => true
|
|
2349
|
+
* parser.partial_value # => { "user" => "george", "role" => nil }
|
|
2350
|
+
* parser.rest # => '"ad'
|
|
2351
|
+
*
|
|
2352
|
+
* parser << 'min" }[1, 2, 3]'
|
|
2353
|
+
* parser.parse # => true
|
|
2354
|
+
* parser.value # => { "user" => "george", "role" => "admin" }
|
|
2355
|
+
*
|
|
2356
|
+
* parser.parse # => true
|
|
2357
|
+
* parser.value # => [1, 2, 3]
|
|
2358
|
+
*
|
|
2359
|
+
* === Limitations
|
|
2360
|
+
*
|
|
2361
|
+
* While ResumableParser is able to parse streams of documents without any
|
|
2362
|
+
* explicit separators between them, it is highly recommended to separate documents
|
|
2363
|
+
* by either spaces or newlines, as otherwise the \JSON syntax for numbers may be ambiguous.
|
|
2364
|
+
* When parsing a number, ResumableParser will not consider the number complete until something follows:
|
|
2365
|
+
*
|
|
2366
|
+
* parser << '123'
|
|
2367
|
+
* parser.parse # => false
|
|
2368
|
+
* parser << ' '
|
|
2369
|
+
* parser.parse # => true
|
|
2370
|
+
* parser.value # => 123
|
|
2371
|
+
*
|
|
2372
|
+
* === Security
|
|
2373
|
+
*
|
|
2374
|
+
* An incomplete document is buffered in full and there is no size limit, so when reading
|
|
2375
|
+
* from an untrusted source the caller is responsible for bounding how much data is fed.
|
|
2376
|
+
* For example:
|
|
2377
|
+
*
|
|
2378
|
+
* loop do
|
|
2379
|
+
* if parser.parsed_bytes > DOCUMENT_MAX_SIZE
|
|
2380
|
+
* raise "document too large"
|
|
2381
|
+
* end
|
|
2382
|
+
*
|
|
2383
|
+
* parser << read_chunk
|
|
2384
|
+
* while parser.parse
|
|
2385
|
+
* process(parser.value)
|
|
2386
|
+
* end
|
|
2387
|
+
* end
|
|
2388
|
+
*/
|
|
2389
|
+
static VALUE cResumableParser_initialize(int argc, VALUE *argv, VALUE self)
|
|
2390
|
+
{
|
|
2391
|
+
rb_check_frozen(self);
|
|
2392
|
+
|
|
2393
|
+
VALUE opts = Qfalse;
|
|
2394
|
+
rb_scan_args_kw(RB_SCAN_ARGS_LAST_HASH_KEYWORDS, argc, argv, "0:", &opts);
|
|
2395
|
+
JSON_ResumableParser *parser = cResumableParser_get(self);
|
|
2396
|
+
|
|
2397
|
+
opts = argc > 0 ? argv[0] : Qnil;
|
|
2398
|
+
parser_config_init(&parser->config, opts, self, true);
|
|
2399
|
+
|
|
2400
|
+
return self;
|
|
2401
|
+
}
|
|
2402
|
+
|
|
2403
|
+
static JSON_ResumableParser *ResumableParser_acquire(VALUE self, bool lock);
|
|
2404
|
+
|
|
2405
|
+
/*
|
|
2406
|
+
* call-seq: self << string -> self
|
|
2407
|
+
*
|
|
2408
|
+
* Appends the given string to the parser's buffer.
|
|
2409
|
+
*/
|
|
2410
|
+
static VALUE cResumableParser_feed(VALUE self, VALUE str)
|
|
2411
|
+
{
|
|
2412
|
+
rb_check_frozen(self);
|
|
2413
|
+
|
|
2414
|
+
JSON_ResumableParser *parser = ResumableParser_acquire(self, false);
|
|
2415
|
+
|
|
2416
|
+
str = convert_encoding(str);
|
|
2417
|
+
if (!RSTRING_LEN(str)) {
|
|
2418
|
+
return self;
|
|
2419
|
+
}
|
|
2420
|
+
|
|
2421
|
+
size_t offset = parser->state.cursor - parser->state.start;
|
|
2422
|
+
const size_t remaining = parser->state.end - parser->state.cursor;
|
|
2423
|
+
|
|
2424
|
+
if (!remaining) {
|
|
2425
|
+
if (parser->buffer) {
|
|
2426
|
+
json_str_clear(parser->buffer);
|
|
2427
|
+
}
|
|
2428
|
+
parser->buffer = RB_OBJ_FROZEN_RAW(str) ? str : rb_obj_hide(rb_str_new_shared(str));
|
|
2429
|
+
offset = 0;
|
|
2430
|
+
} else {
|
|
2431
|
+
JSON_ASSERT(parser->buffer);
|
|
2432
|
+
|
|
2433
|
+
const size_t size = parser->state.end - parser->state.start;
|
|
2434
|
+
const size_t consumed = size - remaining;
|
|
2435
|
+
|
|
2436
|
+
if (RB_OBJ_FROZEN_RAW(parser->buffer)) {
|
|
2437
|
+
VALUE new_buffer = rb_obj_hide(rb_str_buf_new(remaining + RSTRING_LEN(str)));
|
|
2438
|
+
rb_enc_associate_index(new_buffer, utf8_encindex);
|
|
2439
|
+
|
|
2440
|
+
char *old_ptr = RSTRING_PTR(parser->buffer);
|
|
2441
|
+
memcpy(RSTRING_PTR(new_buffer), old_ptr + consumed, remaining);
|
|
2442
|
+
rb_str_set_len(new_buffer, remaining);
|
|
2443
|
+
offset = 0;
|
|
2444
|
+
parser->buffer = new_buffer;
|
|
2445
|
+
} else if (consumed > (size / 2) && size >= 512) {
|
|
2446
|
+
rb_str_modify(parser->buffer);
|
|
2447
|
+
char *old_ptr = RSTRING_PTR(parser->buffer);
|
|
2448
|
+
memmove(old_ptr, old_ptr + consumed, remaining);
|
|
2449
|
+
rb_str_set_len(parser->buffer, remaining);
|
|
2450
|
+
offset = 0;
|
|
2451
|
+
}
|
|
2452
|
+
rb_str_append(parser->buffer, str);
|
|
2453
|
+
}
|
|
2454
|
+
|
|
2455
|
+
long len;
|
|
2456
|
+
const char *start;
|
|
2457
|
+
RSTRING_GETMEM(parser->buffer, start, len);
|
|
2458
|
+
parser->state.start = start;
|
|
2459
|
+
parser->state.end = start + len;
|
|
2460
|
+
parser->state.cursor = parser->state.start + offset;
|
|
2461
|
+
|
|
2462
|
+
return self;
|
|
2463
|
+
}
|
|
2464
|
+
|
|
2465
|
+
struct json_parse_any_args {
|
|
2466
|
+
JSON_ParserState *state;
|
|
2467
|
+
JSON_ParserConfig *config;
|
|
2468
|
+
VALUE parser;
|
|
2469
|
+
};
|
|
2470
|
+
|
|
2471
|
+
static VALUE json_parse_any_resumable_safe0(RB_BLOCK_CALL_FUNC_ARGLIST(yielded_arg, _args))
|
|
2472
|
+
{
|
|
2473
|
+
struct json_parse_any_args *args = (struct json_parse_any_args *)_args;
|
|
2474
|
+
return (VALUE)json_parse_any(args->state, args->config, true);
|
|
2475
|
+
}
|
|
2476
|
+
|
|
2477
|
+
static VALUE json_parse_any_resumable_safe(VALUE _args)
|
|
2478
|
+
{
|
|
2479
|
+
struct json_parse_any_args *args = (struct json_parse_any_args *)_args;
|
|
2480
|
+
VALUE result = rb_catch_obj(args->parser, json_parse_any_resumable_safe0, _args);
|
|
2481
|
+
return result == args->parser ? Qfalse : result;
|
|
2482
|
+
}
|
|
2483
|
+
|
|
2484
|
+
static JSON_ResumableParser *ResumableParser_acquire(VALUE self, bool lock)
|
|
2485
|
+
{
|
|
2486
|
+
JSON_ResumableParser *parser = cResumableParser_get(self);
|
|
2487
|
+
|
|
2488
|
+
if (parser->in_use) {
|
|
2489
|
+
rb_raise(rb_eArgError, "ResumableParser can't be used recursively");
|
|
2490
|
+
}
|
|
2491
|
+
|
|
2492
|
+
if (lock) {
|
|
2493
|
+
parser->in_use = true;
|
|
2494
|
+
}
|
|
2495
|
+
|
|
2496
|
+
// self may have moved, so we need to update all pointers
|
|
2497
|
+
// Investigate: We might be better off keeping JSON_ParserState on the stack
|
|
2498
|
+
// and only persist what we need.
|
|
2499
|
+
parser->state.value_stack = &parser->value_stack;
|
|
2500
|
+
parser->state.frames = &parser->frames;
|
|
2501
|
+
|
|
2502
|
+
return parser;
|
|
2503
|
+
}
|
|
2504
|
+
|
|
2505
|
+
/*
|
|
2506
|
+
* call-seq: parse -> true or false
|
|
2507
|
+
*
|
|
2508
|
+
* Attemps to parse a JSON document from the internal buffer.
|
|
2509
|
+
* Returns whether a complete document could be parsed.
|
|
2510
|
+
*
|
|
2511
|
+
* It does raise +JSON::ParserError+ when encountering invalid \JSON syntax.
|
|
2512
|
+
*
|
|
2513
|
+
* The parsed object can be retrieved by calling #value
|
|
2514
|
+
*/
|
|
2515
|
+
static VALUE cResumableParser_parse(VALUE self)
|
|
2516
|
+
{
|
|
2517
|
+
JSON_ResumableParser *parser = ResumableParser_acquire(self, true);
|
|
2518
|
+
|
|
2519
|
+
if (parser->complete) {
|
|
2520
|
+
parser->parsed_bytes = 0;
|
|
2521
|
+
parser->incomplete_bytes = 0;
|
|
2522
|
+
parser->complete = false;
|
|
2523
|
+
}
|
|
2524
|
+
|
|
2525
|
+
if (!parser->buffer) {
|
|
2526
|
+
parser->in_use = false;
|
|
2527
|
+
return Qfalse;
|
|
2528
|
+
}
|
|
2529
|
+
|
|
2530
|
+
if (parser->frames.head == 0) {
|
|
2531
|
+
json_frame_stack_push(&parser->state, (json_frame){
|
|
2532
|
+
.type = JSON_FRAME_ROOT,
|
|
2533
|
+
.phase = JSON_PHASE_VALUE,
|
|
2534
|
+
});
|
|
2535
|
+
}
|
|
2536
|
+
|
|
2537
|
+
VALUE Vsource = parser->buffer; // Prevent compaction
|
|
2538
|
+
|
|
2539
|
+
json_frame *frame = json_frame_stack_peek(&parser->frames);
|
|
2540
|
+
|
|
2541
|
+
if (frame->phase == JSON_PHASE_DONE) {
|
|
2542
|
+
JSON_ASSERT(parser->value_stack.head == 1);
|
|
2543
|
+
JSON_ASSERT(parser->frames.head == 1);
|
|
2544
|
+
|
|
2545
|
+
frame->phase = JSON_PHASE_VALUE;
|
|
2546
|
+
rvalue_stack_pop(parser->state.value_stack, 1);
|
|
2547
|
+
}
|
|
2548
|
+
|
|
2549
|
+
struct json_parse_any_args args = {
|
|
2550
|
+
.state = &parser->state,
|
|
2551
|
+
.config = &parser->config,
|
|
2552
|
+
.parser = self,
|
|
2553
|
+
};
|
|
2554
|
+
int status;
|
|
2555
|
+
const char *initial_cursor = parser->state.cursor;
|
|
2556
|
+
parser->complete = rb_protect(json_parse_any_resumable_safe, (VALUE)&args, &status);
|
|
2557
|
+
|
|
2558
|
+
if (status) {
|
|
2559
|
+
parser->complete = true; // a parse error is considered complete
|
|
2560
|
+
}
|
|
2561
|
+
|
|
2562
|
+
parser->parsed_bytes += parser->state.cursor - initial_cursor;
|
|
2563
|
+
parser->incomplete_bytes = parser->complete ? 0 : parser->state.end - parser->state.cursor;
|
|
2564
|
+
|
|
2565
|
+
json_eat_whitespace(&parser->state, &parser->config, false);
|
|
2566
|
+
if (eos(&parser->state)) {
|
|
2567
|
+
json_str_clear(parser->buffer);
|
|
2568
|
+
parser->buffer = Qfalse;
|
|
2569
|
+
}
|
|
2570
|
+
parser->in_use = false;
|
|
2571
|
+
|
|
2572
|
+
if (status) {
|
|
2573
|
+
rb_jump_tag(status); // reraise
|
|
2574
|
+
}
|
|
2575
|
+
RB_GC_GUARD(Vsource);
|
|
2576
|
+
return parser->complete ? Qtrue : Qfalse;
|
|
2577
|
+
}
|
|
2578
|
+
|
|
2579
|
+
/*
|
|
2580
|
+
* call-seq: value? -> true or false
|
|
2581
|
+
*
|
|
2582
|
+
* Returns whether a parsed value is available.
|
|
2583
|
+
*/
|
|
2584
|
+
static VALUE cResumableParser_value_p(VALUE self)
|
|
2585
|
+
{
|
|
2586
|
+
JSON_ResumableParser *parser = ResumableParser_acquire(self, false);
|
|
2587
|
+
|
|
2588
|
+
if (parser->value_stack.head > 0) {
|
|
2589
|
+
json_frame *frame = json_frame_stack_peek(&parser->frames);
|
|
2590
|
+
if (frame->phase == JSON_PHASE_DONE) {
|
|
2591
|
+
return Qtrue;
|
|
2592
|
+
}
|
|
2593
|
+
}
|
|
2594
|
+
return Qfalse;
|
|
2595
|
+
}
|
|
2596
|
+
|
|
2597
|
+
/*
|
|
2598
|
+
* call-seq: value -> object
|
|
2599
|
+
*
|
|
2600
|
+
* Returns and consume the last parsed value.
|
|
2601
|
+
* Raises ArgumentError if there is no parsed value or if it was already retrieved:
|
|
2602
|
+
* parser << '[1][2]'
|
|
2603
|
+
* parser.value # ArgumentError no ready value
|
|
2604
|
+
* parser.parse # => true
|
|
2605
|
+
* parser.value # => [1]
|
|
2606
|
+
* parser.value # ArgumentError no ready value
|
|
2607
|
+
*/
|
|
2608
|
+
static VALUE cResumableParser_value(VALUE self)
|
|
2609
|
+
{
|
|
2610
|
+
JSON_ResumableParser *parser = ResumableParser_acquire(self, false);
|
|
2611
|
+
|
|
2612
|
+
if (parser->frames.head > 0) {
|
|
2613
|
+
json_frame *frame = json_frame_stack_peek(&parser->frames);
|
|
2614
|
+
|
|
2615
|
+
if (frame->phase == JSON_PHASE_DONE) {
|
|
2616
|
+
VALUE result = *rvalue_stack_peek(parser->state.value_stack, 1);
|
|
2617
|
+
rvalue_stack_pop(parser->state.value_stack, 1);
|
|
2618
|
+
json_frame_stack_pop(parser->state.frames);
|
|
2619
|
+
return result;
|
|
2620
|
+
}
|
|
2621
|
+
}
|
|
2622
|
+
rb_raise(rb_eArgError, "no ready value");
|
|
2623
|
+
}
|
|
2624
|
+
|
|
2625
|
+
/*
|
|
2626
|
+
* call-seq: clear -> self
|
|
2627
|
+
*
|
|
2628
|
+
* Entirely reset the parser state and buffer.
|
|
2629
|
+
*/
|
|
2630
|
+
static VALUE cResumableParser_clear(VALUE self)
|
|
2631
|
+
{
|
|
2632
|
+
JSON_ResumableParser *parser = ResumableParser_acquire(self, false);
|
|
2633
|
+
parser->buffer = 0;
|
|
2634
|
+
parser->complete = true;
|
|
2635
|
+
parser->parsed_bytes = 0;
|
|
2636
|
+
parser->incomplete_bytes = 0;
|
|
2637
|
+
parser->frames.head = 0;
|
|
2638
|
+
parser->value_stack.head = 0;
|
|
2639
|
+
parser->state.name_cache.length = 0;
|
|
2640
|
+
parser->state.current_nesting = 0;
|
|
2641
|
+
parser->state.in_array = 1;
|
|
2642
|
+
parser->state.emitted_deprecations = 0;
|
|
2643
|
+
parser->state.start = parser->state.cursor = parser->state.end = NULL;
|
|
2644
|
+
return self;
|
|
2645
|
+
}
|
|
2646
|
+
|
|
2647
|
+
static VALUE cResumableParser_partial_value_body(VALUE self)
|
|
2648
|
+
{
|
|
2649
|
+
JSON_ResumableParser *original_parser = cResumableParser_get(self);
|
|
2650
|
+
JSON_ResumableParser parser = *original_parser;
|
|
2651
|
+
|
|
2652
|
+
parser.state.frames = &parser.frames;
|
|
2653
|
+
parser.state.value_stack = &parser.value_stack;
|
|
2654
|
+
|
|
2655
|
+
if (parser.value_stack.head == 0) {
|
|
2656
|
+
return Qnil;
|
|
2657
|
+
}
|
|
2658
|
+
|
|
2659
|
+
json_frame *frame = json_frame_stack_peek(parser.state.frames);
|
|
2660
|
+
long missing_object_value = 0;
|
|
2661
|
+
if (frame->type == JSON_FRAME_OBJECT && (frame->phase == JSON_PHASE_VALUE || frame->phase == JSON_PHASE_OBJECT_COLON)) {
|
|
2662
|
+
missing_object_value = 1;
|
|
2663
|
+
}
|
|
2664
|
+
|
|
2665
|
+
// Copy the value stack as we need to mutate it. The collapse loop folds each
|
|
2666
|
+
// open container by popping its entries and pushing the single result, so a
|
|
2667
|
+
// parent always reclaims its child's slot; head exceeds its live size by at
|
|
2668
|
+
// most one, either for the missing-value placeholder pushed below or for the
|
|
2669
|
+
// result of folding an empty innermost container. That one spare slot keeps
|
|
2670
|
+
// rvalue_stack_push from growing (reallocating) this ALLOCV buffer.
|
|
2671
|
+
long capa = parser.value_stack.head;
|
|
2672
|
+
parser.value_stack.capa = capa + 1;
|
|
2673
|
+
VALUE tmpbuf, *value_stack_buffer = ALLOCV_N(VALUE, tmpbuf, parser.value_stack.capa);
|
|
2674
|
+
MEMCPY(value_stack_buffer, parser.value_stack.ptr, VALUE, capa);
|
|
2675
|
+
parser.value_stack.ptr = value_stack_buffer;
|
|
2676
|
+
|
|
2677
|
+
JSON_ParserState *state = &parser.state;
|
|
2678
|
+
JSON_ParserConfig *config = &parser.config;
|
|
2679
|
+
|
|
2680
|
+
if (missing_object_value) {
|
|
2681
|
+
rvalue_stack_push(state->value_stack, Qnil, NULL, &state->value_stack);
|
|
2682
|
+
}
|
|
2683
|
+
|
|
2684
|
+
VALUE partial_result = Qundef;
|
|
2685
|
+
|
|
2686
|
+
while (UNDEF_P(partial_result)) {
|
|
2687
|
+
frame = json_frame_stack_peek(state->frames);
|
|
2688
|
+
|
|
2689
|
+
switch (frame->type) {
|
|
2690
|
+
case JSON_FRAME_ROOT: {
|
|
2691
|
+
partial_result = *rvalue_stack_peek(state->value_stack, 1);
|
|
2692
|
+
break;
|
|
2693
|
+
}
|
|
2694
|
+
|
|
2695
|
+
case JSON_FRAME_ARRAY: {
|
|
2696
|
+
long count = json_frame_entry_count(frame, state->value_stack);
|
|
2697
|
+
json_push_value(state, config, json_decode_array(state, config, count));
|
|
2698
|
+
json_frame_stack_pop(state->frames);
|
|
2699
|
+
|
|
2700
|
+
break;
|
|
2701
|
+
}
|
|
2702
|
+
|
|
2703
|
+
case JSON_FRAME_OBJECT: {
|
|
2704
|
+
long count = json_frame_entry_count(frame, state->value_stack);
|
|
2705
|
+
json_push_value(state, config, json_decode_object(state, config, count));
|
|
2706
|
+
json_frame_stack_pop(state->frames);
|
|
2707
|
+
break;
|
|
2708
|
+
}
|
|
2709
|
+
|
|
2710
|
+
default: {
|
|
2711
|
+
JSON_UNREACHABLE_RETURN(Qundef);
|
|
2712
|
+
break;
|
|
2713
|
+
}
|
|
2714
|
+
}
|
|
2715
|
+
}
|
|
2716
|
+
|
|
2717
|
+
ALLOCV_END(tmpbuf);
|
|
2718
|
+
return partial_result;
|
|
2719
|
+
}
|
|
2720
|
+
|
|
2721
|
+
/*
|
|
2722
|
+
* call-seq: partial_value -> object
|
|
2723
|
+
*
|
|
2724
|
+
* Returns the Ruby objects parsed up to this point:
|
|
2725
|
+
* parser << '[1, [2, 3,'
|
|
2726
|
+
* parser.parse # => false
|
|
2727
|
+
* parser.value # ArgumentError no ready value
|
|
2728
|
+
* parser.partial_value # => [1, [2, 3]]
|
|
2729
|
+
*/
|
|
2730
|
+
static VALUE cResumableParser_partial_value(VALUE self)
|
|
2731
|
+
{
|
|
2732
|
+
JSON_ResumableParser *parser = ResumableParser_acquire(self, true);
|
|
2733
|
+
|
|
2734
|
+
int status;
|
|
2735
|
+
VALUE result = rb_protect(cResumableParser_partial_value_body, self, &status);
|
|
2736
|
+
parser->in_use = false;
|
|
2737
|
+
if (status) {
|
|
2738
|
+
rb_jump_tag(status);
|
|
2739
|
+
}
|
|
2740
|
+
return result;
|
|
2741
|
+
}
|
|
2742
|
+
|
|
2743
|
+
/*
|
|
2744
|
+
* call-seq: rest -> string
|
|
2745
|
+
*
|
|
2746
|
+
* Returns a string containing what remains to be parsed in the buffer
|
|
2747
|
+
* parser << '{ "message": "unterminated message'
|
|
2748
|
+
* parser.parse # => false
|
|
2749
|
+
* parser.rest # => '"unterminated message"'
|
|
2750
|
+
*/
|
|
2751
|
+
static VALUE cResumableParser_rest(VALUE self)
|
|
2752
|
+
{
|
|
2753
|
+
JSON_ResumableParser *parser = cResumableParser_get(self);
|
|
2754
|
+
|
|
2755
|
+
if (!parser->buffer) {
|
|
2756
|
+
return rb_utf8_str_new("", 0);
|
|
2757
|
+
}
|
|
2758
|
+
|
|
2759
|
+
size_t offset = parser->state.cursor - parser->state.start;
|
|
2760
|
+
const char *ptr;
|
|
2761
|
+
long len;
|
|
2762
|
+
RSTRING_GETMEM(parser->buffer, ptr, len);
|
|
2763
|
+
return rb_utf8_str_new(ptr + offset, len - offset);
|
|
2764
|
+
}
|
|
2765
|
+
|
|
2766
|
+
/*
|
|
2767
|
+
* call-seq: eos? -> true or false
|
|
2768
|
+
*
|
|
2769
|
+
* Returns whether the internal buffer has been entirely consumed.
|
|
2770
|
+
*/
|
|
2771
|
+
static VALUE cResumableParser_eos_p(VALUE self)
|
|
2772
|
+
{
|
|
2773
|
+
JSON_ResumableParser *parser = cResumableParser_get(self);
|
|
2774
|
+
return eos(&parser->state) ? Qtrue : Qfalse;
|
|
2775
|
+
}
|
|
2776
|
+
|
|
2777
|
+
/*
|
|
2778
|
+
* call-seq: partial_value? -> true or false
|
|
2779
|
+
*
|
|
2780
|
+
* Returns whether a document is currently under construction: an unclosed
|
|
2781
|
+
* container, a key awaiting its value, etc.
|
|
2782
|
+
*
|
|
2783
|
+
* It answers the same question as <tt>!partial_value.nil?</tt>, but as a
|
|
2784
|
+
* cheap predicate on the parser's internal state, without materializing the
|
|
2785
|
+
* partially parsed Ruby objects:
|
|
2786
|
+
* parser << '{"a":1,'
|
|
2787
|
+
* parser.parse # => false
|
|
2788
|
+
* parser.partial_value? # => true
|
|
2789
|
+
*
|
|
2790
|
+
* A fully parsed document whose value hasn't been retrieved yet is not under
|
|
2791
|
+
* construction: #value? returns true and #partial_value? returns false.
|
|
2792
|
+
*/
|
|
2793
|
+
static VALUE cResumableParser_partial_value_p(VALUE self)
|
|
2794
|
+
{
|
|
2795
|
+
JSON_ResumableParser *parser = cResumableParser_get(self);
|
|
2796
|
+
|
|
2797
|
+
// Mirror of #value?: values on the stack while the document isn't DONE
|
|
2798
|
+
// belong to a partially built document. A container whose first key or
|
|
2799
|
+
// element hasn't been parsed yet has no frame nor value registered (the
|
|
2800
|
+
// tokenizer rewinds to the container start on EOS), so that state is
|
|
2801
|
+
// observable through the buffer (#eos?/#rest) instead, keeping this
|
|
2802
|
+
// predicate consistent with #partial_value returning nil.
|
|
2803
|
+
if (parser->value_stack.head > 0) {
|
|
2804
|
+
json_frame *frame = json_frame_stack_peek(&parser->frames);
|
|
2805
|
+
if (frame->phase != JSON_PHASE_DONE) {
|
|
2806
|
+
return Qtrue;
|
|
2807
|
+
}
|
|
2808
|
+
}
|
|
2809
|
+
return Qfalse;
|
|
2810
|
+
}
|
|
2811
|
+
|
|
2812
|
+
/*
|
|
2813
|
+
* call-seq: parsed_bytes -> integer
|
|
2814
|
+
*
|
|
2815
|
+
* Returns the number of bytes parsed since the start of the current partial value.
|
|
2816
|
+
* This is intended to be used for securing against untrusted input:
|
|
2817
|
+
*
|
|
2818
|
+
* loop do
|
|
2819
|
+
* if parser.parsed_bytes > DOCUMENT_MAX_SIZE
|
|
2820
|
+
* raise "document too large"
|
|
2821
|
+
* end
|
|
2822
|
+
*
|
|
2823
|
+
* parser << read_chunk
|
|
2824
|
+
* while parser.parse
|
|
2825
|
+
* process(parser.value)
|
|
2826
|
+
* end
|
|
2827
|
+
* end
|
|
2828
|
+
*/
|
|
2829
|
+
static VALUE cResumableParser_parsed_bytes(VALUE self)
|
|
2830
|
+
{
|
|
2831
|
+
JSON_ResumableParser *parser = cResumableParser_get(self);
|
|
2832
|
+
return ULL2NUM(parser->parsed_bytes + parser->incomplete_bytes);
|
|
2833
|
+
}
|
|
2834
|
+
|
|
1702
2835
|
void Init_parser(void)
|
|
1703
2836
|
{
|
|
1704
2837
|
#ifdef HAVE_RB_EXT_RACTOR_SAFE
|
|
@@ -1710,30 +2843,53 @@ void Init_parser(void)
|
|
|
1710
2843
|
mJSON = rb_define_module("JSON");
|
|
1711
2844
|
VALUE mExt = rb_define_module_under(mJSON, "Ext");
|
|
1712
2845
|
VALUE cParserConfig = rb_define_class_under(mExt, "ParserConfig", rb_cObject);
|
|
2846
|
+
|
|
2847
|
+
rb_global_variable(&eParserError);
|
|
2848
|
+
eParserError = rb_path2class("JSON::ParserError");
|
|
2849
|
+
|
|
2850
|
+
rb_global_variable(&eNestingError);
|
|
1713
2851
|
eNestingError = rb_path2class("JSON::NestingError");
|
|
1714
|
-
|
|
2852
|
+
|
|
1715
2853
|
rb_define_alloc_func(cParserConfig, cJSON_parser_s_allocate);
|
|
1716
|
-
|
|
2854
|
+
rb_define_private_method(cParserConfig, "initialize", cParserConfig_initialize, 1);
|
|
1717
2855
|
rb_define_method(cParserConfig, "parse", cParserConfig_parse, 1);
|
|
1718
2856
|
|
|
1719
2857
|
VALUE cParser = rb_define_class_under(mExt, "Parser", rb_cObject);
|
|
1720
2858
|
rb_define_singleton_method(cParser, "parse", cParser_m_parse, 2);
|
|
1721
2859
|
|
|
2860
|
+
VALUE cResumableParser = rb_define_class_under(mJSON, "ResumableParser", rb_cObject);
|
|
2861
|
+
rb_define_alloc_func(cResumableParser, cResumableParser_allocate);
|
|
2862
|
+
rb_define_private_method(cResumableParser, "initialize", cResumableParser_initialize, -1);
|
|
2863
|
+
rb_define_method(cResumableParser, "<<", cResumableParser_feed, 1);
|
|
2864
|
+
rb_define_method(cResumableParser, "parse", cResumableParser_parse, 0);
|
|
2865
|
+
rb_define_method(cResumableParser, "value", cResumableParser_value, 0);
|
|
2866
|
+
rb_define_method(cResumableParser, "value?", cResumableParser_value_p, 0);
|
|
2867
|
+
rb_define_method(cResumableParser, "partial_value", cResumableParser_partial_value, 0);
|
|
2868
|
+
rb_define_method(cResumableParser, "partial_value?", cResumableParser_partial_value_p, 0);
|
|
2869
|
+
rb_define_method(cResumableParser, "clear", cResumableParser_clear, 0);
|
|
2870
|
+
rb_define_method(cResumableParser, "rest", cResumableParser_rest, 0);
|
|
2871
|
+
rb_define_method(cResumableParser, "eos?", cResumableParser_eos_p, 0);
|
|
2872
|
+
rb_define_method(cResumableParser, "parsed_bytes", cResumableParser_parsed_bytes, 0);
|
|
2873
|
+
|
|
2874
|
+
rb_global_variable(&CNaN);
|
|
1722
2875
|
CNaN = rb_const_get(mJSON, rb_intern("NaN"));
|
|
1723
|
-
rb_gc_register_mark_object(CNaN);
|
|
1724
2876
|
|
|
2877
|
+
rb_global_variable(&CInfinity);
|
|
1725
2878
|
CInfinity = rb_const_get(mJSON, rb_intern("Infinity"));
|
|
1726
|
-
rb_gc_register_mark_object(CInfinity);
|
|
1727
2879
|
|
|
2880
|
+
rb_global_variable(&CMinusInfinity);
|
|
1728
2881
|
CMinusInfinity = rb_const_get(mJSON, rb_intern("MinusInfinity"));
|
|
1729
|
-
rb_gc_register_mark_object(CMinusInfinity);
|
|
1730
2882
|
|
|
1731
2883
|
rb_global_variable(&Encoding_UTF_8);
|
|
1732
2884
|
Encoding_UTF_8 = rb_const_get(rb_path2class("Encoding"), rb_intern("UTF_8"));
|
|
1733
2885
|
|
|
2886
|
+
rb_global_variable(&JSON_empty_string);
|
|
2887
|
+
JSON_empty_string = rb_obj_hide(rb_utf8_str_new("", 0));
|
|
2888
|
+
|
|
1734
2889
|
sym_max_nesting = ID2SYM(rb_intern("max_nesting"));
|
|
1735
2890
|
sym_allow_nan = ID2SYM(rb_intern("allow_nan"));
|
|
1736
2891
|
sym_allow_trailing_comma = ID2SYM(rb_intern("allow_trailing_comma"));
|
|
2892
|
+
sym_allow_comments = ID2SYM(rb_intern("allow_comments"));
|
|
1737
2893
|
sym_allow_control_characters = ID2SYM(rb_intern("allow_control_characters"));
|
|
1738
2894
|
sym_allow_invalid_escape = ID2SYM(rb_intern("allow_invalid_escape"));
|
|
1739
2895
|
sym_symbolize_names = ID2SYM(rb_intern("symbolize_names"));
|
|
@@ -1744,8 +2900,12 @@ void Init_parser(void)
|
|
|
1744
2900
|
|
|
1745
2901
|
i_new = rb_intern("new");
|
|
1746
2902
|
i_try_convert = rb_intern("try_convert");
|
|
2903
|
+
#ifndef HAVE_RB_STR_TO_INTERNED_STR
|
|
1747
2904
|
i_uminus = rb_intern("-@");
|
|
2905
|
+
#endif
|
|
1748
2906
|
i_encode = rb_intern("encode");
|
|
2907
|
+
i_at_line = rb_intern("@line");
|
|
2908
|
+
i_at_column = rb_intern("@column");
|
|
1749
2909
|
|
|
1750
2910
|
binary_encindex = rb_ascii8bit_encindex();
|
|
1751
2911
|
utf8_encindex = rb_utf8_encindex();
|