forked from svaarala/duktape
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathduk_bi_json.c
More file actions
3266 lines (2787 loc) · 107 KB
/
Copy pathduk_bi_json.c
File metadata and controls
3266 lines (2787 loc) · 107 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
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
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* JSON built-ins.
*
* See doc/json.rst.
*
* Codepoints are handled as duk_uint_fast32_t to ensure that the full
* unsigned 32-bit range is supported. This matters to e.g. JX.
*
* Input parsing doesn't do an explicit end-of-input check at all. This is
* safe: input string data is always NUL-terminated (0x00) and valid JSON
* inputs never contain plain NUL characters, so that as long as syntax checks
* are correct, we'll never read past the NUL. This approach reduces code size
* and improves parsing performance, but it's critical that syntax checks are
* indeed correct!
*/
#include "duk_internal.h"
#if defined(DUK_USE_JSON_SUPPORT)
/*
* Local defines and forward declarations.
*/
#define DUK__JSON_DECSTR_BUFSIZE 128
#define DUK__JSON_DECSTR_CHUNKSIZE 64
#define DUK__JSON_ENCSTR_CHUNKSIZE 64
#define DUK__JSON_STRINGIFY_BUFSIZE 128
#define DUK__JSON_MAX_ESC_LEN 10 /* '\Udeadbeef' */
DUK_LOCAL_DECL void duk__json_dec_syntax_error(duk_json_dec_ctx *js_ctx);
DUK_LOCAL_DECL void duk__json_dec_eat_white(duk_json_dec_ctx *js_ctx);
#if defined(DUK_USE_JX)
DUK_LOCAL_DECL duk_uint8_t duk__json_dec_peek(duk_json_dec_ctx *js_ctx);
#endif
DUK_LOCAL_DECL duk_uint8_t duk__json_dec_get(duk_json_dec_ctx *js_ctx);
DUK_LOCAL_DECL duk_uint8_t duk__json_dec_get_nonwhite(duk_json_dec_ctx *js_ctx);
DUK_LOCAL_DECL duk_uint_fast32_t duk__json_dec_decode_hex_escape(duk_json_dec_ctx *js_ctx, duk_small_uint_t n);
DUK_LOCAL_DECL void duk__json_dec_req_stridx(duk_json_dec_ctx *js_ctx, duk_small_uint_t stridx);
DUK_LOCAL_DECL void duk__json_dec_string(duk_json_dec_ctx *js_ctx);
#if defined(DUK_USE_JX)
DUK_LOCAL_DECL void duk__json_dec_plain_string(duk_json_dec_ctx *js_ctx);
DUK_LOCAL_DECL void duk__json_dec_pointer(duk_json_dec_ctx *js_ctx);
DUK_LOCAL_DECL void duk__json_dec_buffer(duk_json_dec_ctx *js_ctx);
#endif
DUK_LOCAL_DECL void duk__json_dec_number(duk_json_dec_ctx *js_ctx);
DUK_LOCAL_DECL void duk__json_dec_objarr_entry(duk_json_dec_ctx *js_ctx);
DUK_LOCAL_DECL void duk__json_dec_objarr_exit(duk_json_dec_ctx *js_ctx);
DUK_LOCAL_DECL void duk__json_dec_object(duk_json_dec_ctx *js_ctx);
DUK_LOCAL_DECL void duk__json_dec_array(duk_json_dec_ctx *js_ctx);
DUK_LOCAL_DECL void duk__json_dec_value(duk_json_dec_ctx *js_ctx);
DUK_LOCAL_DECL void duk__json_dec_reviver_walk(duk_json_dec_ctx *js_ctx);
DUK_LOCAL_DECL void duk__emit_1(duk_json_enc_ctx *js_ctx, duk_uint_fast8_t ch);
DUK_LOCAL_DECL void duk__emit_2(duk_json_enc_ctx *js_ctx, duk_uint_fast8_t ch1, duk_uint_fast8_t ch2);
DUK_LOCAL_DECL void duk__unemit_1(duk_json_enc_ctx *js_ctx);
DUK_LOCAL_DECL void duk__emit_hstring(duk_json_enc_ctx *js_ctx, duk_hstring *h);
#if defined(DUK_USE_FASTINT)
DUK_LOCAL_DECL void duk__emit_cstring(duk_json_enc_ctx *js_ctx, const char *p);
#endif
DUK_LOCAL_DECL void duk__emit_stridx(duk_json_enc_ctx *js_ctx, duk_small_uint_t stridx);
DUK_LOCAL_DECL duk_uint8_t *duk__emit_esc_auto_fast(duk_json_enc_ctx *js_ctx, duk_uint_fast32_t cp, duk_uint8_t *q);
DUK_LOCAL_DECL void duk__json_enc_key_autoquote(duk_json_enc_ctx *js_ctx, duk_hstring *k);
DUK_LOCAL_DECL void duk__json_enc_quote_string(duk_json_enc_ctx *js_ctx, duk_hstring *h_str);
DUK_LOCAL_DECL void duk__json_enc_objarr_entry(duk_json_enc_ctx *js_ctx, duk_idx_t *entry_top);
DUK_LOCAL_DECL void duk__json_enc_objarr_exit(duk_json_enc_ctx *js_ctx, duk_idx_t *entry_top);
DUK_LOCAL_DECL void duk__json_enc_object(duk_json_enc_ctx *js_ctx);
DUK_LOCAL_DECL void duk__json_enc_array(duk_json_enc_ctx *js_ctx);
DUK_LOCAL_DECL duk_bool_t duk__json_enc_value(duk_json_enc_ctx *js_ctx, duk_idx_t idx_holder);
DUK_LOCAL_DECL duk_bool_t duk__json_enc_allow_into_proplist(duk_tval *tv);
DUK_LOCAL_DECL void duk__json_enc_double(duk_json_enc_ctx *js_ctx);
#if defined(DUK_USE_FASTINT)
DUK_LOCAL_DECL void duk__json_enc_fastint_tval(duk_json_enc_ctx *js_ctx, duk_tval *tv);
#endif
#if defined(DUK_USE_JX) || defined(DUK_USE_JC)
DUK_LOCAL_DECL void duk__json_enc_buffer_jx_jc(duk_json_enc_ctx *js_ctx, duk_hbuffer *h);
DUK_LOCAL_DECL void duk__json_enc_pointer(duk_json_enc_ctx *js_ctx, void *ptr);
#if defined(DUK_USE_BUFFEROBJECT_SUPPORT)
DUK_LOCAL_DECL void duk__json_enc_bufobj(duk_json_enc_ctx *js_ctx, duk_hbufobj *h_bufobj);
#endif
#endif
#if defined(DUK_USE_JSON_STRINGIFY_FASTPATH)
DUK_LOCAL_DECL void duk__json_enc_buffer_json_fastpath(duk_json_enc_ctx *js_ctx, duk_hbuffer *h);
#endif
DUK_LOCAL_DECL void duk__json_enc_newline_indent(duk_json_enc_ctx *js_ctx, duk_uint_t depth);
/*
* Helper tables
*/
#if defined(DUK_USE_JSON_QUOTESTRING_FASTPATH)
DUK_LOCAL const duk_uint8_t duk__json_quotestr_lookup[256] = {
/* 0x00 ... 0x7f: as is
* 0x80: escape generically
* 0x81: slow path
* 0xa0 ... 0xff: backslash + one char
*/
0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0xe2, 0xf4, 0xee, 0x80, 0xe6, 0xf2, 0x80, 0x80,
0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80,
0x20, 0x21, 0xa2, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f,
0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x3a, 0x3b, 0x3c, 0x3d, 0x3e, 0x3f,
0x40, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, 0x49, 0x4a, 0x4b, 0x4c, 0x4d, 0x4e, 0x4f,
0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58, 0x59, 0x5a, 0x5b, 0xdc, 0x5d, 0x5e, 0x5f,
0x60, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68, 0x69, 0x6a, 0x6b, 0x6c, 0x6d, 0x6e, 0x6f,
0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78, 0x79, 0x7a, 0x7b, 0x7c, 0x7d, 0x7e, 0x81,
0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81,
0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81,
0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81,
0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81,
0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81,
0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81,
0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81,
0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81
};
#else /* DUK_USE_JSON_QUOTESTRING_FASTPATH */
DUK_LOCAL const duk_uint8_t duk__json_quotestr_esc[14] = {
DUK_ASC_NUL, DUK_ASC_NUL, DUK_ASC_NUL, DUK_ASC_NUL,
DUK_ASC_NUL, DUK_ASC_NUL, DUK_ASC_NUL, DUK_ASC_NUL,
DUK_ASC_LC_B, DUK_ASC_LC_T, DUK_ASC_LC_N, DUK_ASC_NUL,
DUK_ASC_LC_F, DUK_ASC_LC_R
};
#endif /* DUK_USE_JSON_QUOTESTRING_FASTPATH */
#if defined(DUK_USE_JSON_DECSTRING_FASTPATH)
DUK_LOCAL const duk_uint8_t duk__json_decstr_lookup[256] = {
/* 0x00: slow path
* other: as is
*/
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x20, 0x21, 0x00, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f,
0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x3a, 0x3b, 0x3c, 0x3d, 0x3e, 0x3f,
0x40, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, 0x49, 0x4a, 0x4b, 0x4c, 0x4d, 0x4e, 0x4f,
0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58, 0x59, 0x5a, 0x5b, 0x00, 0x5d, 0x5e, 0x5f,
0x60, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68, 0x69, 0x6a, 0x6b, 0x6c, 0x6d, 0x6e, 0x6f,
0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78, 0x79, 0x7a, 0x7b, 0x7c, 0x7d, 0x7e, 0x7f,
0x80, 0x81, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87, 0x88, 0x89, 0x8a, 0x8b, 0x8c, 0x8d, 0x8e, 0x8f,
0x90, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, 0x98, 0x99, 0x9a, 0x9b, 0x9c, 0x9d, 0x9e, 0x9f,
0xa0, 0xa1, 0xa2, 0xa3, 0xa4, 0xa5, 0xa6, 0xa7, 0xa8, 0xa9, 0xaa, 0xab, 0xac, 0xad, 0xae, 0xaf,
0xb0, 0xb1, 0xb2, 0xb3, 0xb4, 0xb5, 0xb6, 0xb7, 0xb8, 0xb9, 0xba, 0xbb, 0xbc, 0xbd, 0xbe, 0xbf,
0xc0, 0xc1, 0xc2, 0xc3, 0xc4, 0xc5, 0xc6, 0xc7, 0xc8, 0xc9, 0xca, 0xcb, 0xcc, 0xcd, 0xce, 0xcf,
0xd0, 0xd1, 0xd2, 0xd3, 0xd4, 0xd5, 0xd6, 0xd7, 0xd8, 0xd9, 0xda, 0xdb, 0xdc, 0xdd, 0xde, 0xdf,
0xe0, 0xe1, 0xe2, 0xe3, 0xe4, 0xe5, 0xe6, 0xe7, 0xe8, 0xe9, 0xea, 0xeb, 0xec, 0xed, 0xee, 0xef,
0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, 0xf8, 0xf9, 0xfa, 0xfb, 0xfc, 0xfd, 0xfe, 0xff
};
#endif /* DUK_USE_JSON_DECSTRING_FASTPATH */
#if defined(DUK_USE_JSON_EATWHITE_FASTPATH)
DUK_LOCAL const duk_uint8_t duk__json_eatwhite_lookup[256] = {
/* 0x00: finish (non-white)
* 0x01: continue
*/
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x01, 0x00, 0x00, 0x01, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
};
#endif /* DUK_USE_JSON_EATWHITE_FASTPATH */
#if defined(DUK_USE_JSON_DECNUMBER_FASTPATH)
DUK_LOCAL const duk_uint8_t duk__json_decnumber_lookup[256] = {
/* 0x00: finish (not part of number)
* 0x01: continue
*/
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x01, 0x01, 0x00,
0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
};
#endif /* DUK_USE_JSON_DECNUMBER_FASTPATH */
/*
* Parsing implementation.
*
* JSON lexer is now separate from duk_lexer.c because there are numerous
* small differences making it difficult to share the lexer.
*
* The parser here works with raw bytes directly; this works because all
* JSON delimiters are ASCII characters. Invalid xUTF-8 encoded values
* inside strings will be passed on without normalization; this is not a
* compliance concern because compliant inputs will always be valid
* CESU-8 encodings.
*/
DUK_LOCAL void duk__json_dec_syntax_error(duk_json_dec_ctx *js_ctx) {
/* Shared handler to minimize parser size. Cause will be
* hidden, unfortunately, but we'll have an offset which
* is often quite enough.
*/
DUK_ERROR_FMT1(js_ctx->thr, DUK_ERR_SYNTAX_ERROR, DUK_STR_FMT_INVALID_JSON,
(long) (js_ctx->p - js_ctx->p_start));
DUK_WO_NORETURN(return;);
}
DUK_LOCAL void duk__json_dec_eat_white(duk_json_dec_ctx *js_ctx) {
const duk_uint8_t *p;
duk_uint8_t t;
p = js_ctx->p;
for (;;) {
DUK_ASSERT(p <= js_ctx->p_end);
t = *p;
#if defined(DUK_USE_JSON_EATWHITE_FASTPATH)
/* This fast path is pretty marginal in practice.
* XXX: candidate for removal.
*/
DUK_ASSERT(duk__json_eatwhite_lookup[0x00] == 0x00); /* end-of-input breaks */
if (duk__json_eatwhite_lookup[t] == 0) {
break;
}
#else /* DUK_USE_JSON_EATWHITE_FASTPATH */
if (!(t == 0x20 || t == 0x0a || t == 0x0d || t == 0x09)) {
/* NUL also comes here. Comparison order matters, 0x20
* is most common whitespace.
*/
break;
}
#endif /* DUK_USE_JSON_EATWHITE_FASTPATH */
p++;
}
js_ctx->p = p;
}
#if defined(DUK_USE_JX)
DUK_LOCAL duk_uint8_t duk__json_dec_peek(duk_json_dec_ctx *js_ctx) {
DUK_ASSERT(js_ctx->p <= js_ctx->p_end);
return *js_ctx->p;
}
#endif
DUK_LOCAL duk_uint8_t duk__json_dec_get(duk_json_dec_ctx *js_ctx) {
DUK_ASSERT(js_ctx->p <= js_ctx->p_end);
return *js_ctx->p++;
}
DUK_LOCAL duk_uint8_t duk__json_dec_get_nonwhite(duk_json_dec_ctx *js_ctx) {
duk__json_dec_eat_white(js_ctx);
return duk__json_dec_get(js_ctx);
}
/* For JX, expressing the whole unsigned 32-bit range matters. */
DUK_LOCAL duk_uint_fast32_t duk__json_dec_decode_hex_escape(duk_json_dec_ctx *js_ctx, duk_small_uint_t n) {
duk_small_uint_t i;
duk_uint_fast32_t res = 0;
duk_uint8_t x;
duk_small_int_t t;
for (i = 0; i < n; i++) {
/* XXX: share helper from lexer; duk_lexer.c / hexval(). */
x = duk__json_dec_get(js_ctx);
DUK_DDD(DUK_DDDPRINT("decode_hex_escape: i=%ld, n=%ld, res=%ld, x=%ld",
(long) i, (long) n, (long) res, (long) x));
/* x == 0x00 (EOF) causes syntax_error */
DUK_ASSERT(duk_hex_dectab[0] == -1);
t = duk_hex_dectab[x & 0xff];
if (DUK_LIKELY(t >= 0)) {
res = (res * 16) + (duk_uint_fast32_t) t;
} else {
/* catches EOF and invalid digits */
goto syntax_error;
}
}
DUK_DDD(DUK_DDDPRINT("final hex decoded value: %ld", (long) res));
return res;
syntax_error:
duk__json_dec_syntax_error(js_ctx);
DUK_UNREACHABLE();
return 0;
}
DUK_LOCAL void duk__json_dec_req_stridx(duk_json_dec_ctx *js_ctx, duk_small_uint_t stridx) {
duk_hstring *h;
const duk_uint8_t *p;
duk_uint8_t x, y;
/* First character has already been eaten and checked by the caller.
* We can scan until a NUL in stridx string because no built-in strings
* have internal NULs.
*/
DUK_ASSERT_STRIDX_VALID(stridx);
h = DUK_HTHREAD_GET_STRING(js_ctx->thr, stridx);
DUK_ASSERT(h != NULL);
p = (const duk_uint8_t *) DUK_HSTRING_GET_DATA(h) + 1;
DUK_ASSERT(*(js_ctx->p - 1) == *(p - 1)); /* first character has been matched */
for (;;) {
x = *p;
if (x == 0) {
break;
}
y = duk__json_dec_get(js_ctx);
if (x != y) {
/* Catches EOF of JSON input. */
goto syntax_error;
}
p++;
}
return;
syntax_error:
duk__json_dec_syntax_error(js_ctx);
DUK_UNREACHABLE();
}
DUK_LOCAL duk_small_int_t duk__json_dec_string_escape(duk_json_dec_ctx *js_ctx, duk_uint8_t **ext_p) {
duk_uint_fast32_t cp;
/* EOF (-1) will be cast to an unsigned value first
* and then re-cast for the switch. In any case, it
* will match the default case (syntax error).
*/
cp = (duk_uint_fast32_t) duk__json_dec_get(js_ctx);
switch (cp) {
case DUK_ASC_BACKSLASH: break;
case DUK_ASC_DOUBLEQUOTE: break;
case DUK_ASC_SLASH: break;
case DUK_ASC_LC_T: cp = 0x09; break;
case DUK_ASC_LC_N: cp = 0x0a; break;
case DUK_ASC_LC_R: cp = 0x0d; break;
case DUK_ASC_LC_F: cp = 0x0c; break;
case DUK_ASC_LC_B: cp = 0x08; break;
case DUK_ASC_LC_U: {
cp = duk__json_dec_decode_hex_escape(js_ctx, 4);
break;
}
#if defined(DUK_USE_JX)
case DUK_ASC_UC_U: {
if (js_ctx->flag_ext_custom) {
cp = duk__json_dec_decode_hex_escape(js_ctx, 8);
} else {
return 1; /* syntax error */
}
break;
}
case DUK_ASC_LC_X: {
if (js_ctx->flag_ext_custom) {
cp = duk__json_dec_decode_hex_escape(js_ctx, 2);
} else {
return 1; /* syntax error */
}
break;
}
#endif /* DUK_USE_JX */
default:
/* catches EOF (0x00) */
return 1; /* syntax error */
}
DUK_RAW_WRITEINC_XUTF8(*ext_p, cp);
return 0;
}
DUK_LOCAL void duk__json_dec_string(duk_json_dec_ctx *js_ctx) {
duk_hthread *thr = js_ctx->thr;
duk_bufwriter_ctx bw_alloc;
duk_bufwriter_ctx *bw;
duk_uint8_t *q;
/* '"' was eaten by caller */
/* Note that we currently parse -bytes-, not codepoints.
* All non-ASCII extended UTF-8 will encode to bytes >= 0x80,
* so they'll simply pass through (valid UTF-8 or not).
*/
bw = &bw_alloc;
DUK_BW_INIT_PUSHBUF(js_ctx->thr, bw, DUK__JSON_DECSTR_BUFSIZE);
q = DUK_BW_GET_PTR(js_ctx->thr, bw);
#if defined(DUK_USE_JSON_DECSTRING_FASTPATH)
for (;;) {
duk_small_uint_t safe;
duk_uint8_t b, x;
const duk_uint8_t *p;
/* Select a safe loop count where no output checks are
* needed assuming we won't encounter escapes. Input
* bound checks are not necessary as a NUL (guaranteed)
* will cause a SyntaxError before we read out of bounds.
*/
safe = DUK__JSON_DECSTR_CHUNKSIZE;
/* Ensure space for 1:1 output plus one escape. */
q = DUK_BW_ENSURE_RAW(js_ctx->thr, bw, safe + DUK_UNICODE_MAX_XUTF8_LENGTH, q);
p = js_ctx->p; /* temp copy, write back for next loop */
for (;;) {
if (safe == 0) {
js_ctx->p = p;
break;
}
safe--;
/* End of input (NUL) goes through slow path and causes SyntaxError. */
DUK_ASSERT(duk__json_decstr_lookup[0] == 0x00);
b = *p++;
x = (duk_small_int_t) duk__json_decstr_lookup[b];
if (DUK_LIKELY(x != 0)) {
/* Fast path, decode as is. */
*q++ = b;
} else if (b == DUK_ASC_DOUBLEQUOTE) {
js_ctx->p = p;
goto found_quote;
} else if (b == DUK_ASC_BACKSLASH) {
/* We've ensured space for one escaped input; then
* bail out and recheck (this makes escape handling
* quite slow but it's uncommon).
*/
js_ctx->p = p;
if (duk__json_dec_string_escape(js_ctx, &q) != 0) {
goto syntax_error;
}
break;
} else {
js_ctx->p = p;
goto syntax_error;
}
}
}
found_quote:
#else /* DUK_USE_JSON_DECSTRING_FASTPATH */
for (;;) {
duk_uint8_t x;
q = DUK_BW_ENSURE_RAW(js_ctx->thr, bw, DUK_UNICODE_MAX_XUTF8_LENGTH, q);
x = duk__json_dec_get(js_ctx);
if (x == DUK_ASC_DOUBLEQUOTE) {
break;
} else if (x == DUK_ASC_BACKSLASH) {
if (duk__json_dec_string_escape(js_ctx, &q) != 0) {
goto syntax_error;
}
} else if (x < 0x20) {
/* catches EOF (NUL) */
goto syntax_error;
} else {
*q++ = (duk_uint8_t) x;
}
}
#endif /* DUK_USE_JSON_DECSTRING_FASTPATH */
DUK_BW_SETPTR_AND_COMPACT(js_ctx->thr, bw, q);
(void) duk_buffer_to_string(thr, -1); /* Safe if input string is safe. */
/* [ ... str ] */
return;
syntax_error:
duk__json_dec_syntax_error(js_ctx);
DUK_UNREACHABLE();
}
#if defined(DUK_USE_JX)
/* Decode a plain string consisting entirely of identifier characters.
* Used to parse plain keys (e.g. "foo: 123").
*/
DUK_LOCAL void duk__json_dec_plain_string(duk_json_dec_ctx *js_ctx) {
duk_hthread *thr = js_ctx->thr;
const duk_uint8_t *p;
duk_small_int_t x;
/* Caller has already eaten the first char so backtrack one byte. */
js_ctx->p--; /* safe */
p = js_ctx->p;
/* Here again we parse bytes, and non-ASCII UTF-8 will cause end of
* parsing (which is correct except if there are non-shortest encodings).
* There is also no need to check explicitly for end of input buffer as
* the input is NUL padded and NUL will exit the parsing loop.
*
* Because no unescaping takes place, we can just scan to the end of the
* plain string and intern from the input buffer.
*/
for (;;) {
x = *p;
/* There is no need to check the first character specially here
* (i.e. reject digits): the caller only accepts valid initial
* characters and won't call us if the first character is a digit.
* This also ensures that the plain string won't be empty.
*/
if (!duk_unicode_is_identifier_part((duk_codepoint_t) x)) {
break;
}
p++;
}
duk_push_lstring(thr, (const char *) js_ctx->p, (duk_size_t) (p - js_ctx->p));
js_ctx->p = p;
/* [ ... str ] */
}
#endif /* DUK_USE_JX */
#if defined(DUK_USE_JX)
DUK_LOCAL void duk__json_dec_pointer(duk_json_dec_ctx *js_ctx) {
duk_hthread *thr = js_ctx->thr;
const duk_uint8_t *p;
duk_small_int_t x;
void *voidptr;
/* Caller has already eaten the first character ('(') which we don't need. */
p = js_ctx->p;
for (;;) {
x = *p;
/* Assume that the native representation never contains a closing
* parenthesis.
*/
if (x == DUK_ASC_RPAREN) {
break;
} else if (x <= 0) {
/* NUL term or -1 (EOF), NUL check would suffice */
goto syntax_error;
}
p++;
}
/* There is no need to NUL delimit the sscanf() call: trailing garbage is
* ignored and there is always a NUL terminator which will force an error
* if no error is encountered before it. It's possible that the scan
* would scan further than between [js_ctx->p,p[ though and we'd advance
* by less than the scanned value.
*
* Because pointers are platform specific, a failure to scan a pointer
* results in a null pointer which is a better placeholder than a missing
* value or an error.
*/
voidptr = NULL;
(void) DUK_SSCANF((const char *) js_ctx->p, DUK_STR_FMT_PTR, &voidptr);
duk_push_pointer(thr, voidptr);
js_ctx->p = p + 1; /* skip ')' */
/* [ ... ptr ] */
return;
syntax_error:
duk__json_dec_syntax_error(js_ctx);
DUK_UNREACHABLE();
}
#endif /* DUK_USE_JX */
#if defined(DUK_USE_JX)
DUK_LOCAL void duk__json_dec_buffer(duk_json_dec_ctx *js_ctx) {
duk_hthread *thr = js_ctx->thr;
const duk_uint8_t *p;
duk_uint8_t *buf;
duk_size_t src_len;
duk_small_int_t x;
/* Caller has already eaten the first character ('|') which we don't need. */
p = js_ctx->p;
/* XXX: Would be nice to share the fast path loop from duk_hex_decode()
* and avoid creating a temporary buffer. However, there are some
* differences which prevent trivial sharing:
*
* - Pipe char detection
* - EOF detection
* - Unknown length of input and output
*
* The best approach here would be a bufwriter and a reasonaly sized
* safe inner loop (e.g. 64 output bytes at a time).
*/
for (;;) {
x = *p;
/* This loop intentionally does not ensure characters are valid
* ([0-9a-fA-F]) because the hex decode call below will do that.
*/
if (x == DUK_ASC_PIPE) {
break;
} else if (x <= 0) {
/* NUL term or -1 (EOF), NUL check would suffice */
goto syntax_error;
}
p++;
}
/* XXX: this is not very nice; unnecessary copy is made. */
src_len = (duk_size_t) (p - js_ctx->p);
buf = (duk_uint8_t *) duk_push_fixed_buffer_nozero(thr, src_len);
DUK_ASSERT(buf != NULL);
duk_memcpy((void *) buf, (const void *) js_ctx->p, src_len);
duk_hex_decode(thr, -1);
js_ctx->p = p + 1; /* skip '|' */
/* [ ... buf ] */
return;
syntax_error:
duk__json_dec_syntax_error(js_ctx);
DUK_UNREACHABLE();
}
#endif /* DUK_USE_JX */
/* Parse a number, other than NaN or +/- Infinity */
DUK_LOCAL void duk__json_dec_number(duk_json_dec_ctx *js_ctx) {
duk_hthread *thr = js_ctx->thr;
const duk_uint8_t *p_start;
const duk_uint8_t *p;
duk_uint8_t x;
duk_small_uint_t s2n_flags;
DUK_DDD(DUK_DDDPRINT("parse_number"));
p_start = js_ctx->p;
/* First pass parse is very lenient (e.g. allows '1.2.3') and extracts a
* string for strict number parsing.
*/
p = js_ctx->p;
for (;;) {
x = *p;
DUK_DDD(DUK_DDDPRINT("parse_number: p_start=%p, p=%p, p_end=%p, x=%ld",
(const void *) p_start, (const void *) p,
(const void *) js_ctx->p_end, (long) x));
#if defined(DUK_USE_JSON_DECNUMBER_FASTPATH)
/* This fast path is pretty marginal in practice.
* XXX: candidate for removal.
*/
DUK_ASSERT(duk__json_decnumber_lookup[0x00] == 0x00); /* end-of-input breaks */
if (duk__json_decnumber_lookup[x] == 0) {
break;
}
#else /* DUK_USE_JSON_DECNUMBER_FASTPATH */
if (!((x >= DUK_ASC_0 && x <= DUK_ASC_9) ||
(x == DUK_ASC_PERIOD || x == DUK_ASC_LC_E ||
x == DUK_ASC_UC_E || x == DUK_ASC_MINUS || x == DUK_ASC_PLUS))) {
/* Plus sign must be accepted for positive exponents
* (e.g. '1.5e+2'). This clause catches NULs.
*/
break;
}
#endif /* DUK_USE_JSON_DECNUMBER_FASTPATH */
p++; /* safe, because matched (NUL causes a break) */
}
js_ctx->p = p;
DUK_ASSERT(js_ctx->p > p_start);
duk_push_lstring(thr, (const char *) p_start, (duk_size_t) (p - p_start));
s2n_flags = DUK_S2N_FLAG_ALLOW_EXP |
DUK_S2N_FLAG_ALLOW_MINUS | /* but don't allow leading plus */
DUK_S2N_FLAG_ALLOW_FRAC;
DUK_DDD(DUK_DDDPRINT("parse_number: string before parsing: %!T",
(duk_tval *) duk_get_tval(thr, -1)));
duk_numconv_parse(thr, 10 /*radix*/, s2n_flags);
if (duk_is_nan(thr, -1)) {
duk__json_dec_syntax_error(js_ctx);
}
DUK_ASSERT(duk_is_number(thr, -1));
DUK_DDD(DUK_DDDPRINT("parse_number: final number: %!T",
(duk_tval *) duk_get_tval(thr, -1)));
/* [ ... num ] */
}
DUK_LOCAL void duk__json_dec_objarr_entry(duk_json_dec_ctx *js_ctx) {
duk_hthread *thr = js_ctx->thr;
duk_require_stack(thr, DUK_JSON_DEC_REQSTACK);
/* c recursion check */
duk_native_stack_check(thr);
DUK_ASSERT_DISABLE(js_ctx->recursion_depth >= 0); /* unsigned */
DUK_ASSERT(js_ctx->recursion_depth <= js_ctx->recursion_limit);
if (js_ctx->recursion_depth >= js_ctx->recursion_limit) {
DUK_ERROR_RANGE(thr, DUK_STR_DEC_RECLIMIT);
DUK_WO_NORETURN(return;);
}
js_ctx->recursion_depth++;
}
DUK_LOCAL void duk__json_dec_objarr_exit(duk_json_dec_ctx *js_ctx) {
/* c recursion check */
DUK_ASSERT(js_ctx->recursion_depth > 0);
DUK_ASSERT(js_ctx->recursion_depth <= js_ctx->recursion_limit);
js_ctx->recursion_depth--;
}
DUK_LOCAL void duk__json_dec_object(duk_json_dec_ctx *js_ctx) {
duk_hthread *thr = js_ctx->thr;
duk_int_t key_count; /* XXX: a "first" flag would suffice */
duk_uint8_t x;
DUK_DDD(DUK_DDDPRINT("parse_object"));
duk__json_dec_objarr_entry(js_ctx);
duk_push_object(thr);
/* Initial '{' has been checked and eaten by caller. */
key_count = 0;
for (;;) {
x = duk__json_dec_get_nonwhite(js_ctx);
DUK_DDD(DUK_DDDPRINT("parse_object: obj=%!T, x=%ld, key_count=%ld",
(duk_tval *) duk_get_tval(thr, -1),
(long) x, (long) key_count));
/* handle comma and closing brace */
if (x == DUK_ASC_COMMA && key_count > 0) {
/* accept comma, expect new value */
x = duk__json_dec_get_nonwhite(js_ctx);
} else if (x == DUK_ASC_RCURLY) {
/* eat closing brace */
break;
} else if (key_count == 0) {
/* accept anything, expect first value (EOF will be
* caught by key parsing below.
*/
;
} else {
/* catches EOF (NUL) and initial comma */
goto syntax_error;
}
/* parse key and value */
if (x == DUK_ASC_DOUBLEQUOTE) {
duk__json_dec_string(js_ctx);
#if defined(DUK_USE_JX)
} else if (js_ctx->flag_ext_custom &&
duk_unicode_is_identifier_start((duk_codepoint_t) x)) {
duk__json_dec_plain_string(js_ctx);
#endif
} else {
goto syntax_error;
}
/* [ ... obj key ] */
x = duk__json_dec_get_nonwhite(js_ctx);
if (x != DUK_ASC_COLON) {
goto syntax_error;
}
duk__json_dec_value(js_ctx);
/* [ ... obj key val ] */
duk_xdef_prop_wec(thr, -3);
/* [ ... obj ] */
key_count++;
}
/* [ ... obj ] */
DUK_DDD(DUK_DDDPRINT("parse_object: final object is %!T",
(duk_tval *) duk_get_tval(thr, -1)));
duk__json_dec_objarr_exit(js_ctx);
return;
syntax_error:
duk__json_dec_syntax_error(js_ctx);
DUK_UNREACHABLE();
}
DUK_LOCAL void duk__json_dec_array(duk_json_dec_ctx *js_ctx) {
duk_hthread *thr = js_ctx->thr;
duk_uarridx_t arr_idx;
duk_uint8_t x;
DUK_DDD(DUK_DDDPRINT("parse_array"));
duk__json_dec_objarr_entry(js_ctx);
duk_push_array(thr);
/* Initial '[' has been checked and eaten by caller. */
arr_idx = 0;
for (;;) {
x = duk__json_dec_get_nonwhite(js_ctx);
DUK_DDD(DUK_DDDPRINT("parse_array: arr=%!T, x=%ld, arr_idx=%ld",
(duk_tval *) duk_get_tval(thr, -1),
(long) x, (long) arr_idx));
/* handle comma and closing bracket */
if ((x == DUK_ASC_COMMA) && (arr_idx != 0)) {
/* accept comma, expect new value */
;
} else if (x == DUK_ASC_RBRACKET) {
/* eat closing bracket */
break;
} else if (arr_idx == 0) {
/* accept anything, expect first value (EOF will be
* caught by duk__json_dec_value() below.
*/
js_ctx->p--; /* backtrack (safe) */
} else {
/* catches EOF (NUL) and initial comma */
goto syntax_error;
}
/* parse value */
duk__json_dec_value(js_ctx);
/* [ ... arr val ] */
duk_xdef_prop_index_wec(thr, -2, arr_idx);
arr_idx++;
}
/* Must set 'length' explicitly when using duk_xdef_prop_xxx() to
* set the values.
*/
duk_set_length(thr, -1, arr_idx);
/* [ ... arr ] */
DUK_DDD(DUK_DDDPRINT("parse_array: final array is %!T",
(duk_tval *) duk_get_tval(thr, -1)));
duk__json_dec_objarr_exit(js_ctx);
return;
syntax_error:
duk__json_dec_syntax_error(js_ctx);
DUK_UNREACHABLE();
}
DUK_LOCAL void duk__json_dec_value(duk_json_dec_ctx *js_ctx) {
duk_hthread *thr = js_ctx->thr;
duk_uint8_t x;
x = duk__json_dec_get_nonwhite(js_ctx);
DUK_DDD(DUK_DDDPRINT("parse_value: initial x=%ld", (long) x));
/* Note: duk__json_dec_req_stridx() backtracks one char */
if (x == DUK_ASC_DOUBLEQUOTE) {
duk__json_dec_string(js_ctx);
} else if ((x >= DUK_ASC_0 && x <= DUK_ASC_9) || (x == DUK_ASC_MINUS)) {
#if defined(DUK_USE_JX)
if (js_ctx->flag_ext_custom && x == DUK_ASC_MINUS && duk__json_dec_peek(js_ctx) == DUK_ASC_UC_I) {
duk__json_dec_req_stridx(js_ctx, DUK_STRIDX_MINUS_INFINITY); /* "-Infinity", '-' has been eaten */
duk_push_number(thr, -DUK_DOUBLE_INFINITY);
} else {
#else
{ /* unconditional block */
#endif
/* We already ate 'x', so backup one byte. */
js_ctx->p--; /* safe */
duk__json_dec_number(js_ctx);
}
} else if (x == DUK_ASC_LC_T) {
duk__json_dec_req_stridx(js_ctx, DUK_STRIDX_TRUE);
duk_push_true(thr);
} else if (x == DUK_ASC_LC_F) {
duk__json_dec_req_stridx(js_ctx, DUK_STRIDX_FALSE);
duk_push_false(thr);
} else if (x == DUK_ASC_LC_N) {
duk__json_dec_req_stridx(js_ctx, DUK_STRIDX_LC_NULL);
duk_push_null(thr);
#if defined(DUK_USE_JX)
} else if (js_ctx->flag_ext_custom && x == DUK_ASC_LC_U) {
duk__json_dec_req_stridx(js_ctx, DUK_STRIDX_LC_UNDEFINED);
duk_push_undefined(thr);
} else if (js_ctx->flag_ext_custom && x == DUK_ASC_UC_N) {
duk__json_dec_req_stridx(js_ctx, DUK_STRIDX_NAN);
duk_push_nan(thr);
} else if (js_ctx->flag_ext_custom && x == DUK_ASC_UC_I) {
duk__json_dec_req_stridx(js_ctx, DUK_STRIDX_INFINITY);
duk_push_number(thr, DUK_DOUBLE_INFINITY);
} else if (js_ctx->flag_ext_custom && x == DUK_ASC_LPAREN) {
duk__json_dec_pointer(js_ctx);
} else if (js_ctx->flag_ext_custom && x == DUK_ASC_PIPE) {
duk__json_dec_buffer(js_ctx);
#endif
} else if (x == DUK_ASC_LCURLY) {
duk__json_dec_object(js_ctx);
} else if (x == DUK_ASC_LBRACKET) {
duk__json_dec_array(js_ctx);
} else {
/* catches EOF (NUL) */
goto syntax_error;
}
duk__json_dec_eat_white(js_ctx);
/* [ ... val ] */
return;
syntax_error:
duk__json_dec_syntax_error(js_ctx);
DUK_UNREACHABLE();
}
/* Recursive value reviver, implements the Walk() algorithm. The parsing
* step ensures there is a reasonable depth limit to the input. However,
* the reviver may create more depth by editing object or array entries, so
* we have both C recursion limit and native stack checks here.
*/
DUK_LOCAL void duk__json_dec_reviver_walk(duk_json_dec_ctx *js_ctx) {
duk_hthread *thr = js_ctx->thr;
duk_hobject *h;
duk_uarridx_t i, arr_len;
duk__json_dec_objarr_entry(js_ctx);
DUK_DDD(DUK_DDDPRINT("walk: top=%ld, holder=%!T, name=%!T",
(long) duk_get_top(thr),
(duk_tval *) duk_get_tval(thr, -2),
(duk_tval *) duk_get_tval(thr, -1)));
duk_dup_top(thr);
duk_get_prop(thr, -3); /* -> [ ... holder name val ] */
h = duk_get_hobject(thr, -1);
if (h != NULL) {
if (duk_js_isarray_hobject(h)) {
arr_len = (duk_uarridx_t) duk_get_length(thr, -1);
for (i = 0; i < arr_len; i++) {
/* [ ... holder name val ] */
DUK_DDD(DUK_DDDPRINT("walk: array, top=%ld, i=%ld, arr_len=%ld, holder=%!T, name=%!T, val=%!T",
(long) duk_get_top(thr), (long) i, (long) arr_len,
(duk_tval *) duk_get_tval(thr, -3), (duk_tval *) duk_get_tval(thr, -2),
(duk_tval *) duk_get_tval(thr, -1)));
duk_dup_top(thr);
(void) duk_push_uint_to_hstring(thr, (duk_uint_t) i); /* -> [ ... holder name val val ToString(i) ] */
duk__json_dec_reviver_walk(js_ctx); /* -> [ ... holder name val new_elem ] */
if (duk_is_undefined(thr, -1)) {
duk_pop(thr);
duk_del_prop_index(thr, -1, i);
} else {
/* XXX: duk_xdef_prop_index_wec() would be more appropriate
* here but it currently makes some assumptions that might
* not hold (e.g. that previous property is not an accessor).