summaryrefslogtreecommitdiffstats
path: root/js/src/builtin/JSON.cpp
blob: dbfab8b43aaff71cd0f6e513afae7b3a15966fcc (plain)
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*-
 * vim: set ts=8 sts=2 et sw=2 tw=80:
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

#include "builtin/JSON.h"

#include "mozilla/CheckedInt.h"
#include "mozilla/FloatingPoint.h"
#include "mozilla/Range.h"
#include "mozilla/ScopeExit.h"
#include "mozilla/Variant.h"

#include <algorithm>

#include "jsnum.h"
#include "jstypes.h"

#include "builtin/Array.h"
#include "builtin/BigInt.h"
#include "builtin/ParseRecordObject.h"
#include "js/friend/ErrorMessages.h"  // js::GetErrorMessage, JSMSG_*
#include "js/friend/StackLimits.h"    // js::AutoCheckRecursionLimit
#include "js/Object.h"                // JS::GetBuiltinClass
#include "js/PropertySpec.h"
#include "js/StableStringChars.h"
#include "js/TypeDecls.h"
#include "js/Value.h"
#include "util/StringBuffer.h"
#include "vm/BooleanObject.h"  // js::BooleanObject
#include "vm/Interpreter.h"
#include "vm/Iteration.h"
#include "vm/JSAtomUtils.h"  // ToAtom
#include "vm/JSContext.h"
#include "vm/JSObject.h"
#include "vm/JSONParser.h"
#include "vm/NativeObject.h"
#include "vm/NumberObject.h"  // js::NumberObject
#include "vm/PlainObject.h"   // js::PlainObject
#include "vm/StringObject.h"  // js::StringObject
#ifdef ENABLE_RECORD_TUPLE
#  include "builtin/RecordObject.h"
#  include "builtin/TupleObject.h"
#  include "vm/RecordType.h"
#endif

#include "builtin/Array-inl.h"
#include "vm/GeckoProfiler-inl.h"
#include "vm/JSAtomUtils-inl.h"  // AtomToId, PrimitiveValueToId, IndexToId, IdToString,
#include "vm/NativeObject-inl.h"

using namespace js;

using mozilla::AsVariant;
using mozilla::CheckedInt;
using mozilla::Maybe;
using mozilla::RangedPtr;
using mozilla::Variant;

using JS::AutoStableStringChars;

/* https://262.ecma-international.org/14.0/#sec-quotejsonstring
 * Requires that the destination has enough space allocated for src after
 * escaping (that is, `2 + 6 * (srcEnd - srcBegin)` characters).
 */
template <typename SrcCharT, typename DstCharT>
static MOZ_ALWAYS_INLINE RangedPtr<DstCharT> InfallibleQuoteJSONString(
    RangedPtr<const SrcCharT> srcBegin, RangedPtr<const SrcCharT> srcEnd,
    RangedPtr<DstCharT> dstPtr) {
  // Maps characters < 256 to the value that must follow the '\\' in the quoted
  // string. Entries with 'u' are handled as \\u00xy, and entries with 0 are not
  // escaped in any way. Characters >= 256 are all assumed to be unescaped.
  static const Latin1Char escapeLookup[256] = {
      // clang-format off
        'u', 'u', 'u', 'u', 'u', 'u', 'u', 'u', 'b', 't',
        'n', 'u', 'f', 'r', 'u', 'u', 'u', 'u', 'u', 'u',
        'u', 'u', 'u', 'u', 'u', 'u', 'u', 'u', 'u', 'u',
        'u', 'u', 0,   0,  '\"', 0,   0,   0,   0,   0,
        0,   0,   0,   0,   0,   0,   0,   0,   0,   0,
        0,   0,   0,   0,   0,   0,   0,   0,   0,   0,
        0,   0,   0,   0,   0,   0,   0,   0,   0,   0,
        0,   0,   0,   0,   0,   0,   0,   0,   0,   0,
        0,   0,   0,   0,   0,   0,   0,   0,   0,   0,
        0,   0,  '\\', // rest are all zeros
      // clang-format on
  };

  /* Step 1. */
  *dstPtr++ = '"';

  auto ToLowerHex = [](uint8_t u) {
    MOZ_ASSERT(u <= 0xF);
    return "0123456789abcdef"[u];
  };

  /* Step 2. */
  while (srcBegin != srcEnd) {
    const SrcCharT c = *srcBegin++;

    // Handle the Latin-1 cases.
    if (MOZ_LIKELY(c < sizeof(escapeLookup))) {
      Latin1Char escaped = escapeLookup[c];

      // Directly copy non-escaped code points.
      if (escaped == 0) {
        *dstPtr++ = c;
        continue;
      }

      // Escape the rest, elaborating Unicode escapes when needed.
      *dstPtr++ = '\\';
      *dstPtr++ = escaped;
      if (escaped == 'u') {
        *dstPtr++ = '0';
        *dstPtr++ = '0';

        uint8_t x = c >> 4;
        MOZ_ASSERT(x < 10);
        *dstPtr++ = '0' + x;

        *dstPtr++ = ToLowerHex(c & 0xF);
      }

      continue;
    }

    // Non-ASCII non-surrogates are directly copied.
    if (!unicode::IsSurrogate(c)) {
      *dstPtr++ = c;
      continue;
    }

    // So too for complete surrogate pairs.
    if (MOZ_LIKELY(unicode::IsLeadSurrogate(c) && srcBegin < srcEnd &&
                   unicode::IsTrailSurrogate(*srcBegin))) {
      *dstPtr++ = c;
      *dstPtr++ = *srcBegin++;
      continue;
    }

    // But lone surrogates are Unicode-escaped.
    char32_t as32 = char32_t(c);
    *dstPtr++ = '\\';
    *dstPtr++ = 'u';
    *dstPtr++ = ToLowerHex(as32 >> 12);
    *dstPtr++ = ToLowerHex((as32 >> 8) & 0xF);
    *dstPtr++ = ToLowerHex((as32 >> 4) & 0xF);
    *dstPtr++ = ToLowerHex(as32 & 0xF);
  }

  /* Steps 3-4. */
  *dstPtr++ = '"';
  return dstPtr;
}

template <typename SrcCharT, typename DstCharT>
static size_t QuoteJSONStringHelper(const JSLinearString& linear,
                                    StringBuffer& sb, size_t sbOffset) {
  size_t len = linear.length();

  JS::AutoCheckCannotGC nogc;
  RangedPtr<const SrcCharT> srcBegin{linear.chars<SrcCharT>(nogc), len};
  RangedPtr<DstCharT> dstBegin{sb.begin<DstCharT>(), sb.begin<DstCharT>(),
                               sb.end<DstCharT>()};
  RangedPtr<DstCharT> dstEnd =
      InfallibleQuoteJSONString(srcBegin, srcBegin + len, dstBegin + sbOffset);

  return dstEnd - dstBegin;
}

static bool QuoteJSONString(JSContext* cx, StringBuffer& sb, JSString* str) {
  JSLinearString* linear = str->ensureLinear(cx);
  if (!linear) {
    return false;
  }

  if (linear->hasTwoByteChars() && !sb.ensureTwoByteChars()) {
    return false;
  }

  // We resize the backing buffer to the maximum size we could possibly need,
  // write the escaped string into it, and shrink it back to the size we ended
  // up needing.

  size_t len = linear->length();
  size_t sbInitialLen = sb.length();

  CheckedInt<size_t> reservedLen = CheckedInt<size_t>(len) * 6 + 2;
  if (MOZ_UNLIKELY(!reservedLen.isValid())) {
    ReportAllocationOverflow(cx);
    return false;
  }

  if (!sb.growByUninitialized(reservedLen.value())) {
    return false;
  }

  size_t newSize;

  if (linear->hasTwoByteChars()) {
    newSize =
        QuoteJSONStringHelper<char16_t, char16_t>(*linear, sb, sbInitialLen);
  } else if (sb.isUnderlyingBufferLatin1()) {
    newSize = QuoteJSONStringHelper<Latin1Char, Latin1Char>(*linear, sb,
                                                            sbInitialLen);
  } else {
    newSize =
        QuoteJSONStringHelper<Latin1Char, char16_t>(*linear, sb, sbInitialLen);
  }

  sb.shrinkTo(newSize);

  return true;
}

namespace {

using ObjectVector = GCVector<JSObject*, 8>;

class StringifyContext {
 public:
  StringifyContext(JSContext* cx, StringBuffer& sb, const StringBuffer& gap,
                   HandleObject replacer, const RootedIdVector& propertyList,
                   bool maybeSafely)
      : sb(sb),
        gap(gap),
        replacer(cx, replacer),
        stack(cx, ObjectVector(cx)),
        propertyList(propertyList),
        depth(0),
        maybeSafely(maybeSafely) {
    MOZ_ASSERT_IF(maybeSafely, !replacer);
    MOZ_ASSERT_IF(maybeSafely, gap.empty());
  }

  StringBuffer& sb;
  const StringBuffer& gap;
  RootedObject replacer;
  Rooted<ObjectVector> stack;
  const RootedIdVector& propertyList;
  uint32_t depth;
  bool maybeSafely;
};

} /* anonymous namespace */

static bool SerializeJSONProperty(JSContext* cx, const Value& v,
                                  StringifyContext* scx);

static bool WriteIndent(StringifyContext* scx, uint32_t limit) {
  if (!scx->gap.empty()) {
    if (!scx->sb.append('\n')) {
      return false;
    }

    if (scx->gap.isUnderlyingBufferLatin1()) {
      for (uint32_t i = 0; i < limit; i++) {
        if (!scx->sb.append(scx->gap.rawLatin1Begin(),
                            scx->gap.rawLatin1End())) {
          return false;
        }
      }
    } else {
      for (uint32_t i = 0; i < limit; i++) {
        if (!scx->sb.append(scx->gap.rawTwoByteBegin(),
                            scx->gap.rawTwoByteEnd())) {
          return false;
        }
      }
    }
  }

  return true;
}

namespace {

template <typename KeyType>
class KeyStringifier {};

template <>
class KeyStringifier<uint32_t> {
 public:
  static JSString* toString(JSContext* cx, uint32_t index) {
    return IndexToString(cx, index);
  }
};

template <>
class KeyStringifier<HandleId> {
 public:
  static JSString* toString(JSContext* cx, HandleId id) {
    return IdToString(cx, id);
  }
};

} /* anonymous namespace */

/*
 * https://262.ecma-international.org/14.0/#sec-serializejsonproperty, steps
 * 2-4, extracted to enable preprocessing of property values when stringifying
 * objects in SerializeJSONObject.
 */
template <typename KeyType>
static bool PreprocessValue(JSContext* cx, HandleObject holder, KeyType key,
                            MutableHandleValue vp, StringifyContext* scx) {
  // We don't want to do any preprocessing here if scx->maybeSafely,
  // since the stuff we do here can have side-effects.
  if (scx->maybeSafely) {
    return true;
  }

  RootedString keyStr(cx);

  // Step 2. Modified by BigInt spec 6.1 to check for a toJSON method on the
  // BigInt prototype when the value is a BigInt, and to pass the BigInt
  // primitive value as receiver.
  if (vp.isObject() || vp.isBigInt()) {
    RootedValue toJSON(cx);
    RootedObject obj(cx, JS::ToObject(cx, vp));
    if (!obj) {
      return false;
    }

    if (!GetProperty(cx, obj, vp, cx->names().toJSON, &toJSON)) {
      return false;
    }

    if (IsCallable(toJSON)) {
      keyStr = KeyStringifier<KeyType>::toString(cx, key);
      if (!keyStr) {
        return false;
      }

      RootedValue arg0(cx, StringValue(keyStr));
      if (!js::Call(cx, toJSON, vp, arg0, vp)) {
        return false;
      }
    }
  }

  /* Step 3. */
  if (scx->replacer && scx->replacer->isCallable()) {
    MOZ_ASSERT(holder != nullptr,
               "holder object must be present when replacer is callable");

    if (!keyStr) {
      keyStr = KeyStringifier<KeyType>::toString(cx, key);
      if (!keyStr) {
        return false;
      }
    }

    RootedValue arg0(cx, StringValue(keyStr));
    RootedValue replacerVal(cx, ObjectValue(*scx->replacer));
    if (!js::Call(cx, replacerVal, holder, arg0, vp, vp)) {
      return false;
    }
  }

  /* Step 4. */
  if (vp.get().isObject()) {
    RootedObject obj(cx, &vp.get().toObject());

    ESClass cls;
    if (!JS::GetBuiltinClass(cx, obj, &cls)) {
      return false;
    }

    if (cls == ESClass::Number) {
      double d;
      if (!ToNumber(cx, vp, &d)) {
        return false;
      }
      vp.setNumber(d);
    } else if (cls == ESClass::String) {
      JSString* str = ToStringSlow<CanGC>(cx, vp);
      if (!str) {
        return false;
      }
      vp.setString(str);
    } else if (cls == ESClass::Boolean || cls == ESClass::BigInt ||
               IF_RECORD_TUPLE(
                   obj->is<RecordObject>() || obj->is<TupleObject>(), false)) {
      if (!Unbox(cx, obj, vp)) {
        return false;
      }
    }
  }

  return true;
}

/*
 * Determines whether a value which has passed by
 * https://262.ecma-international.org/14.0/#sec-serializejsonproperty steps
 * 1-4's gauntlet will result in SerializeJSONProperty returning |undefined|.
 * This function is used to properly omit properties resulting in such values
 * when stringifying objects, while properly stringifying such properties as
 * null when they're encountered in arrays.
 */
static inline bool IsFilteredValue(const Value& v) {
  MOZ_ASSERT_IF(v.isMagic(), v.isMagic(JS_ELEMENTS_HOLE));
  return v.isUndefined() || v.isSymbol() || v.isMagic() || IsCallable(v);
}

class CycleDetector {
 public:
  CycleDetector(StringifyContext* scx, HandleObject obj)
      : stack_(&scx->stack), obj_(obj), appended_(false) {}

  MOZ_ALWAYS_INLINE bool foundCycle(JSContext* cx) {
    JSObject* obj = obj_;
    for (JSObject* obj2 : stack_) {
      if (MOZ_UNLIKELY(obj == obj2)) {
        JS_ReportErrorNumberASCII(cx, GetErrorMessage, nullptr,
                                  JSMSG_JSON_CYCLIC_VALUE);
        return false;
      }
    }
    appended_ = stack_.append(obj);
    return appended_;
  }

  ~CycleDetector() {
    if (MOZ_LIKELY(appended_)) {
      MOZ_ASSERT(stack_.back() == obj_);
      stack_.popBack();
    }
  }

 private:
  MutableHandle<ObjectVector> stack_;
  HandleObject obj_;
  bool appended_;
};

#ifdef ENABLE_RECORD_TUPLE
enum class JOType { Record, Object };
template <JOType type = JOType::Object>
#endif
/* https://262.ecma-international.org/14.0/#sec-serializejsonobject */
static bool SerializeJSONObject(JSContext* cx, HandleObject obj,
                                StringifyContext* scx) {
  /*
   * This method implements the SerializeJSONObject algorithm, but:
   *
   *   * The algorithm is somewhat reformulated to allow the final string to
   *     be streamed into a single buffer, rather than be created and copied
   *     into place incrementally as the algorithm specifies it.  This
   *     requires moving portions of the SerializeJSONProperty call in 8a into
   *     this algorithm (and in SerializeJSONArray as well).
   */

#ifdef ENABLE_RECORD_TUPLE
  RecordType* rec;

  if constexpr (type == JOType::Record) {
    MOZ_ASSERT(obj->is<RecordType>());
    rec = &obj->as<RecordType>();
  } else {
    MOZ_ASSERT(!IsExtendedPrimitive(*obj));
  }
#endif
  MOZ_ASSERT_IF(scx->maybeSafely, obj->is<PlainObject>());

  /* Steps 1-2, 11. */
  CycleDetector detect(scx, obj);
  if (!detect.foundCycle(cx)) {
    return false;
  }

  if (!scx->sb.append('{')) {
    return false;
  }

  /* Steps 5-7. */
  Maybe<RootedIdVector> ids;
  const RootedIdVector* props;
  if (scx->replacer && !scx->replacer->isCallable()) {
    // NOTE: We can't assert |IsArray(scx->replacer)| because the replacer
    //       might have been a revocable proxy to an array.  Such a proxy
    //       satisfies |IsArray|, but any side effect of JSON.stringify
    //       could revoke the proxy so that |!IsArray(scx->replacer)|.  See
    //       bug 1196497.
    props = &scx->propertyList;
  } else {
    MOZ_ASSERT_IF(scx->replacer, scx->propertyList.length() == 0);
    ids.emplace(cx);
    if (!GetPropertyKeys(cx, obj, JSITER_OWNONLY, ids.ptr())) {
      return false;
    }
    props = ids.ptr();
  }

  /* My kingdom for not-quite-initialized-from-the-start references. */
  const RootedIdVector& propertyList = *props;

  /* Steps 8-10, 13. */
  bool wroteMember = false;
  RootedId id(cx);
  for (size_t i = 0, len = propertyList.length(); i < len; i++) {
    if (!CheckForInterrupt(cx)) {
      return false;
    }

    /*
     * Steps 8a-8b.  Note that the call to SerializeJSONProperty is broken up
     * into 1) getting the property; 2) processing for toJSON, calling the
     * replacer, and handling boxed Number/String/Boolean objects; 3) filtering
     * out values which process to |undefined|, and 4) stringifying all values
     * which pass the filter.
     */
    id = propertyList[i];
    RootedValue outputValue(cx);
#ifdef DEBUG
    if (scx->maybeSafely) {
      PropertyResult prop;
      if (!NativeLookupOwnPropertyNoResolve(cx, &obj->as<NativeObject>(), id,
                                            &prop)) {
        return false;
      }
      MOZ_ASSERT(prop.isNativeProperty() &&
                 prop.propertyInfo().isDataDescriptor());
    }
#endif  // DEBUG

#ifdef ENABLE_RECORD_TUPLE
    if constexpr (type == JOType::Record) {
      MOZ_ALWAYS_TRUE(rec->getOwnProperty(cx, id, &outputValue));
    } else
#endif
    {
      RootedValue objValue(cx, ObjectValue(*obj));
      if (!GetProperty(cx, obj, objValue, id, &outputValue)) {
        return false;
      }
    }
    if (!PreprocessValue(cx, obj, HandleId(id), &outputValue, scx)) {
      return false;
    }
    if (IsFilteredValue(outputValue)) {
      continue;
    }

    /* Output a comma unless this is the first member to write. */
    if (wroteMember && !scx->sb.append(',')) {
      return false;
    }
    wroteMember = true;

    if (!WriteIndent(scx, scx->depth)) {
      return false;
    }

    JSString* s = IdToString(cx, id);
    if (!s) {
      return false;
    }

    if (!QuoteJSONString(cx, scx->sb, s) || !scx->sb.append(':') ||
        !(scx->gap.empty() || scx->sb.append(' ')) ||
        !SerializeJSONProperty(cx, outputValue, scx)) {
      return false;
    }
  }

  if (wroteMember && !WriteIndent(scx, scx->depth - 1)) {
    return false;
  }

  return scx->sb.append('}');
}

// For JSON.stringify and JSON.parse with a reviver function, we need to know
// the length of an object for which JS::IsArray returned true. This must be
// either an ArrayObject or a proxy wrapping one.
static MOZ_ALWAYS_INLINE bool GetLengthPropertyForArrayLike(JSContext* cx,
                                                            HandleObject obj,
                                                            uint32_t* lengthp) {
  if (MOZ_LIKELY(obj->is<ArrayObject>())) {
    *lengthp = obj->as<ArrayObject>().length();
    return true;
  }
#ifdef ENABLE_RECORD_TUPLE
  if (obj->is<TupleType>()) {
    *lengthp = obj->as<TupleType>().length();
    return true;
  }
#endif

  MOZ_ASSERT(obj->is<ProxyObject>());

  uint64_t len = 0;
  if (!GetLengthProperty(cx, obj, &len)) {
    return false;
  }

  // A scripted proxy wrapping an array can return a length value larger than
  // UINT32_MAX. Stringification will likely report an OOM in this case. Match
  // other JS engines and report an early error in this case, although
  // technically this is observable, for example when stringifying with a
  // replacer function.
  if (len > UINT32_MAX) {
    ReportAllocationOverflow(cx);
    return false;
  }

  *lengthp = uint32_t(len);
  return true;
}

/* https://262.ecma-international.org/14.0/#sec-serializejsonarray */
static bool SerializeJSONArray(JSContext* cx, HandleObject obj,
                               StringifyContext* scx) {
  /*
   * This method implements the SerializeJSONArray algorithm, but:
   *
   *   * The algorithm is somewhat reformulated to allow the final string to
   *     be streamed into a single buffer, rather than be created and copied
   *     into place incrementally as the algorithm specifies it.  This
   *     requires moving portions of the SerializeJSONProperty call in 8a into
   *     this algorithm (and in SerializeJSONObject as well).
   */

  /* Steps 1-2, 11. */
  CycleDetector detect(scx, obj);
  if (!detect.foundCycle(cx)) {
    return false;
  }

  if (!scx->sb.append('[')) {
    return false;
  }

  /* Step 6. */
  uint32_t length;
  if (!GetLengthPropertyForArrayLike(cx, obj, &length)) {
    return false;
  }

  /* Steps 7-10. */
  if (length != 0) {
    /* Steps 4, 10b(i). */
    if (!WriteIndent(scx, scx->depth)) {
      return false;
    }

    /* Steps 7-10. */
    RootedValue outputValue(cx);
    for (uint32_t i = 0; i < length; i++) {
      if (!CheckForInterrupt(cx)) {
        return false;
      }

      /*
       * Steps 8a-8c.  Again note how the call to the spec's
       * SerializeJSONProperty method is broken up into getting the property,
       * running it past toJSON and the replacer and maybe unboxing, and
       * interpreting some values as |null| in separate steps.
       */
#ifdef DEBUG
      if (scx->maybeSafely) {
        /*
         * Trying to do a JS_AlreadyHasOwnElement runs the risk of
         * hitting OOM on jsid creation.  Let's just assert sanity for
         * small enough indices.
         */
        MOZ_ASSERT(obj->is<ArrayObject>());
        MOZ_ASSERT(obj->is<NativeObject>());
        Rooted<NativeObject*> nativeObj(cx, &obj->as<NativeObject>());
        if (i <= PropertyKey::IntMax) {
          MOZ_ASSERT(
              nativeObj->containsDenseElement(i) != nativeObj->isIndexed(),
              "the array must either be small enough to remain "
              "fully dense (and otherwise un-indexed), *or* "
              "all its initially-dense elements were sparsified "
              "and the object is indexed");
        } else {
          MOZ_ASSERT(nativeObj->isIndexed());
        }
      }
#endif
      if (!GetElement(cx, obj, i, &outputValue)) {
        return false;
      }
      if (!PreprocessValue(cx, obj, i, &outputValue, scx)) {
        return false;
      }
      if (IsFilteredValue(outputValue)) {
        if (!scx->sb.append("null")) {
          return false;
        }
      } else {
        if (!SerializeJSONProperty(cx, outputValue, scx)) {
          return false;
        }
      }

      /* Steps 3, 4, 10b(i). */
      if (i < length - 1) {
        if (!scx->sb.append(',')) {
          return false;
        }
        if (!WriteIndent(scx, scx->depth)) {
          return false;
        }
      }
    }

    /* Step 10(b)(iii). */
    if (!WriteIndent(scx, scx->depth - 1)) {
      return false;
    }
  }

  return scx->sb.append(']');
}

/* https://262.ecma-international.org/14.0/#sec-serializejsonproperty */
static bool SerializeJSONProperty(JSContext* cx, const Value& v,
                                  StringifyContext* scx) {
  /* Step 12 must be handled by the caller. */
  MOZ_ASSERT(!IsFilteredValue(v));

  /*
   * This method implements the SerializeJSONProperty algorithm, but:
   *
   *   * We move property retrieval (step 1) into callers to stream the
   *     stringification process and avoid constantly copying strings.
   *   * We move the preprocessing in steps 2-4 into a helper function to
   *     allow both SerializeJSONObject and SerializeJSONArray to use this
   * method.  While SerializeJSONArray could use it without this move,
   * SerializeJSONObject must omit any |undefined|-valued property per so it
   * can't stream out a value using the SerializeJSONProperty method exactly as
   * defined by the spec.
   *   * We move step 12 into callers, again to ease streaming.
   */

  /* Step 8. */
  if (v.isString()) {
    return QuoteJSONString(cx, scx->sb, v.toString());
  }

  /* Step 5. */
  if (v.isNull()) {
    return scx->sb.append("null");
  }

  /* Steps 6-7. */
  if (v.isBoolean()) {
    return v.toBoolean() ? scx->sb.append("true") : scx->sb.append("false");
  }

  /* Step 9. */
  if (v.isNumber()) {
    if (v.isDouble()) {
      if (!std::isfinite(v.toDouble())) {
        MOZ_ASSERT(!scx->maybeSafely,
                   "input JS::ToJSONMaybeSafely must not include "
                   "reachable non-finite numbers");
        return scx->sb.append("null");
      }
    }

    return NumberValueToStringBuffer(v, scx->sb);
  }

  /* Step 10. */
  if (v.isBigInt()) {
    JS_ReportErrorNumberASCII(cx, GetErrorMessage, nullptr,
                              JSMSG_BIGINT_NOT_SERIALIZABLE);
    return false;
  }

  AutoCheckRecursionLimit recursion(cx);
  if (!recursion.check(cx)) {
    return false;
  }

  /* Step 11. */
  MOZ_ASSERT(v.hasObjectPayload());
  RootedObject obj(cx, &v.getObjectPayload());

  MOZ_ASSERT(
      !scx->maybeSafely || obj->is<PlainObject>() || obj->is<ArrayObject>(),
      "input to JS::ToJSONMaybeSafely must not include reachable "
      "objects that are neither arrays nor plain objects");

  scx->depth++;
  auto dec = mozilla::MakeScopeExit([&] { scx->depth--; });

#ifdef ENABLE_RECORD_TUPLE
  if (v.isExtendedPrimitive()) {
    if (obj->is<RecordType>()) {
      return SerializeJSONObject<JOType::Record>(cx, obj, scx);
    }
    if (obj->is<TupleType>()) {
      return SerializeJSONArray(cx, obj, scx);
    }
    MOZ_CRASH("Unexpected extended primitive - boxes cannot be stringified.");
  }
#endif

  bool isArray;
  if (!IsArray(cx, obj, &isArray)) {
    return false;
  }

  return isArray ? SerializeJSONArray(cx, obj, scx)
                 : SerializeJSONObject(cx, obj, scx);
}

static bool CanFastStringifyObject(NativeObject* obj) {
  if (ClassCanHaveExtraEnumeratedProperties(obj->getClass())) {
    return false;
  }

  if (obj->is<ArrayObject>()) {
    // Arrays will look up all keys [0..length) so disallow anything that could
    // find those keys anywhere but in the dense elements.
    if (!IsPackedArray(obj) && ObjectMayHaveExtraIndexedProperties(obj)) {
      return false;
    }
  } else {
    // Non-Arrays will only look at own properties, but still disallow any
    // indexed properties other than in the dense elements because they would
    // require sorting.
    if (ObjectMayHaveExtraIndexedOwnProperties(obj)) {
      return false;
    }
  }

  // Only used for internal environment objects that should never be passed to
  // JSON.stringify.
  MOZ_ASSERT(!obj->getOpsLookupProperty());

#ifdef ENABLE_RECORD_TUPLE
  if (ObjectValue(*obj).isExtendedPrimitive()) {
    return false;
  }
#endif

  return true;
}

#define FOR_EACH_STRINGIFY_BAIL_REASON(MACRO) \
  MACRO(NO_REASON)                            \
  MACRO(INELIGIBLE_OBJECT)                    \
  MACRO(DEEP_RECURSION)                       \
  MACRO(NON_DATA_PROPERTY)                    \
  MACRO(TOO_MANY_PROPERTIES)                  \
  MACRO(BIGINT)                               \
  MACRO(API)                                  \
  MACRO(HAVE_REPLACER)                        \
  MACRO(HAVE_SPACE)                           \
  MACRO(PRIMITIVE)                            \
  MACRO(HAVE_TOJSON)                          \
  MACRO(IMPURE_LOOKUP)                        \
  MACRO(INTERRUPT)

enum class BailReason : uint8_t {
#define DECLARE_ENUM(name) name,
  FOR_EACH_STRINGIFY_BAIL_REASON(DECLARE_ENUM)
#undef DECLARE_ENUM
};

static const char* DescribeStringifyBailReason(BailReason whySlow) {
  switch (whySlow) {
#define ENUM_NAME(name)  \
  case BailReason::name: \
    return #name;
    FOR_EACH_STRINGIFY_BAIL_REASON(ENUM_NAME)
#undef ENUM_NAME
    default:
      return "Unknown";
  }
}

// Iterator over all the dense elements of an object. Used
// for both Arrays and non-Arrays.
class DenseElementsIteratorForJSON {
  HeapSlotArray elements;
  uint32_t element;

  // Arrays can have a length less than getDenseInitializedLength(), in which
  // case the remaining Array elements are treated as UndefinedValue.
  uint32_t numElements;
  uint32_t length;

 public:
  explicit DenseElementsIteratorForJSON(NativeObject* nobj)
      : elements(nobj->getDenseElements()),
        element(0),
        numElements(nobj->getDenseInitializedLength()) {
    length = nobj->is<ArrayObject>() ? nobj->as<ArrayObject>().length()
                                     : numElements;
  }

  bool done() const { return element == length; }

  Value next() {
    // For Arrays, steps 6-8 of
    // https://262.ecma-international.org/14.0/#sec-serializejsonarray. For
    // non-Arrays, step 6a of
    // https://262.ecma-international.org/14.0/#sec-serializejsonobject
    // following the order from
    // https://262.ecma-international.org/14.0/#sec-ordinaryownpropertykeys

    MOZ_ASSERT(!done());
    auto i = element++;
    // Consider specializing the iterator for Arrays vs non-Arrays to avoid this
    // branch.
    return i < numElements ? elements.begin()[i] : UndefinedValue();
  }

  uint32_t getIndex() const { return element; }
};

// An iterator over the non-element properties of a Shape, returned in forward
// (creation) order. Note that it is fallible, so after iteration is complete
// isOverflowed() should be called to verify that the results are actually
// complete.

class ShapePropertyForwardIterNoGC {
  // Pointer to the current PropMap with length and an index within it.
  PropMap* map_;
  uint32_t mapLength_;
  uint32_t i_ = 0;

  // Stack of PropMaps to iterate through, oldest properties on top. The current
  // map (map_, above) is never on this stack.
  mozilla::Vector<PropMap*> stack_;

  const NativeShape* shape_;

  MOZ_ALWAYS_INLINE void settle() {
    while (true) {
      if (MOZ_UNLIKELY(i_ == mapLength_)) {
        i_ = 0;
        if (stack_.empty()) {
          mapLength_ = 0;  // Done
          return;
        }
        map_ = stack_.back();
        stack_.popBack();
        mapLength_ =
            stack_.empty() ? shape_->propMapLength() : PropMap::Capacity;
      } else if (MOZ_UNLIKELY(shape_->isDictionary() && !map_->hasKey(i_))) {
        // Dictionary maps can have "holes" for removed properties, so keep
        // going until we find a non-hole slot.
        i_++;
      } else {
        return;
      }
    }
  }

 public:
  explicit ShapePropertyForwardIterNoGC(NativeShape* shape) : shape_(shape) {
    // Set map_ to the PropMap containing the first property (the deepest map in
    // the previous() chain). Push pointers to all other PropMaps onto stack_.
    map_ = shape->propMap();
    if (!map_) {
      // No properties.
      i_ = mapLength_ = 0;
      return;
    }
    while (map_->hasPrevious()) {
      if (!stack_.append(map_)) {
        // Overflowed.
        i_ = mapLength_ = UINT32_MAX;
        return;
      }
      map_ = map_->asLinked()->previous();
    }

    // Set mapLength_ to the number of properties in map_ (including dictionary
    // holes, if any.)
    mapLength_ = stack_.empty() ? shape_->propMapLength() : PropMap::Capacity;

    settle();
  }

  bool done() const { return i_ == mapLength_; }
  bool isOverflowed() const { return i_ == UINT32_MAX; }

  void operator++(int) {
    MOZ_ASSERT(!done());
    i_++;
    settle();
  }

  PropertyInfoWithKey get() const {
    MOZ_ASSERT(!done());
    return map_->getPropertyInfoWithKey(i_);
  }

  PropertyInfoWithKey operator*() const { return get(); }

  // Fake pointer struct to make operator-> work.
  // See https://stackoverflow.com/a/52856349.
  struct FakePtr {
    PropertyInfoWithKey val_;
    const PropertyInfoWithKey* operator->() const { return &val_; }
  };
  FakePtr operator->() const { return {get()}; }
};

// Iterator over EnumerableOwnProperties
// https://262.ecma-international.org/14.0/#sec-enumerableownproperties
// that fails if it encounters any accessor properties, as they are not handled
// by JSON FastSerializeJSONProperty, or if it sees too many properties on one
// object.
class OwnNonIndexKeysIterForJSON {
  ShapePropertyForwardIterNoGC shapeIter;
  bool done_ = false;
  BailReason fastFailed_ = BailReason::NO_REASON;

  void settle() {
    // Skip over any non-enumerable or Symbol properties, and permanently fail
    // if any enumerable non-data properties are encountered.
    for (; !shapeIter.done(); shapeIter++) {
      if (!shapeIter->enumerable()) {
        continue;
      }
      if (!shapeIter->isDataProperty()) {
        fastFailed_ = BailReason::NON_DATA_PROPERTY;
        done_ = true;
        return;
      }
      PropertyKey id = shapeIter->key();
      if (!id.isSymbol()) {
        return;
      }
    }
    done_ = true;
  }

 public:
  explicit OwnNonIndexKeysIterForJSON(const NativeObject* nobj)
      : shapeIter(nobj->shape()) {
    if (MOZ_UNLIKELY(shapeIter.isOverflowed())) {
      fastFailed_ = BailReason::TOO_MANY_PROPERTIES;
      done_ = true;
      return;
    }
    if (!nobj->hasEnumerableProperty()) {
      // Non-Arrays with no enumerable properties can just be skipped.
      MOZ_ASSERT(!nobj->is<ArrayObject>());
      done_ = true;
      return;
    }
    settle();
  }

  bool done() const { return done_ || shapeIter.done(); }
  BailReason cannotFastStringify() const { return fastFailed_; }

  PropertyInfoWithKey next() {
    MOZ_ASSERT(!done());
    PropertyInfoWithKey prop = shapeIter.get();
    shapeIter++;
    settle();
    return prop;
  }
};

// Steps from https://262.ecma-international.org/14.0/#sec-serializejsonproperty
static bool EmitSimpleValue(JSContext* cx, StringBuffer& sb, const Value& v) {
  /* Step 8. */
  if (v.isString()) {
    return QuoteJSONString(cx, sb, v.toString());
  }

  /* Step 5. */
  if (v.isNull()) {
    return sb.append("null");
  }

  /* Steps 6-7. */
  if (v.isBoolean()) {
    return v.toBoolean() ? sb.append("true") : sb.append("false");
  }

  /* Step 9. */
  if (v.isNumber()) {
    if (v.isDouble()) {
      if (!std::isfinite(v.toDouble())) {
        return sb.append("null");
      }
    }

    return NumberValueToStringBuffer(v, sb);
  }

  // Unrepresentable values.
  if (v.isUndefined() || v.isMagic()) {
    MOZ_ASSERT_IF(v.isMagic(), v.isMagic(JS_ELEMENTS_HOLE));
    return sb.append("null");
  }

  /* Step 10. */
  MOZ_CRASH("should have validated printable simple value already");
}

// https://262.ecma-international.org/14.0/#sec-serializejsonproperty step 8b
// where K is an integer index.
static bool EmitQuotedIndexColon(StringBuffer& sb, uint32_t index) {
  Int32ToCStringBuf cbuf;
  size_t cstrlen;
  const char* cstr = ::Int32ToCString(&cbuf, index, &cstrlen);
  if (!sb.reserve(sb.length() + 1 + cstrlen + 1 + 1)) {
    return false;
  }
  sb.infallibleAppend('"');
  sb.infallibleAppend(cstr, cstrlen);
  sb.infallibleAppend('"');
  sb.infallibleAppend(':');
  return true;
}

// Similar to PreprocessValue: replace the value with a simpler one to
// stringify, but also detect whether the value is compatible with the fast
// path. If not, bail out by setting *whySlow and returning true.
static bool PreprocessFastValue(JSContext* cx, Value* vp, StringifyContext* scx,
                                BailReason* whySlow) {
  MOZ_ASSERT(!scx->maybeSafely);

  // Steps are from
  // https://262.ecma-international.org/14.0/#sec-serializejsonproperty

  // Disallow BigInts to avoid caring about BigInt.prototype.toJSON.
  if (vp->isBigInt()) {
    *whySlow = BailReason::BIGINT;
    return true;
  }

  if (!vp->isObject()) {
    return true;
  }

  if (!vp->toObject().is<NativeObject>()) {
    *whySlow = BailReason::INELIGIBLE_OBJECT;
    return true;
  }

  // Step 2: lookup a .toJSON property (and bail if found).
  NativeObject* obj = &vp->toObject().as<NativeObject>();
  PropertyResult toJSON;
  NativeObject* holder;
  PropertyKey id = NameToId(cx->names().toJSON);
  if (!NativeLookupPropertyInline<NoGC, LookupResolveMode::CheckMayResolve>(
          cx, obj, id, &holder, &toJSON)) {
    // Looking up this property would require a side effect.
    *whySlow = BailReason::IMPURE_LOOKUP;
    return true;
  }
  if (toJSON.isFound()) {
    *whySlow = BailReason::HAVE_TOJSON;
    return true;
  }

  // Step 4: convert primitive wrapper objects to primitives. Disallowed for
  // fast path.
  if (obj->is<NumberObject>() || obj->is<StringObject>() ||
      obj->is<BooleanObject>() || obj->is<BigIntObject>() ||
      IF_RECORD_TUPLE(obj->is<RecordObject>() || obj->is<TupleObject>(),
                      false)) {
    // Primitive wrapper objects can invoke arbitrary code when being coerced to
    // their primitive values (eg via @@toStringTag).
    *whySlow = BailReason::INELIGIBLE_OBJECT;
    return true;
  }

  if (obj->isCallable()) {
    // Steps 11,12: Callable objects are treated as undefined.
    vp->setUndefined();
    return true;
  }

  if (!CanFastStringifyObject(obj)) {
    *whySlow = BailReason::INELIGIBLE_OBJECT;
    return true;
  }

  return true;
}

// FastSerializeJSONProperty maintains an explicit stack to handle nested
// objects. For each object, first the dense elements are iterated, then the
// named properties (included sparse indexes, which will cause
// FastSerializeJSONProperty to bail out.)
//
// The iterators for each of those parts are not merged into a single common
// iterator because the interface is different for the two parts, and they are
// handled separately in the FastSerializeJSONProperty code.
struct FastStackEntry {
  NativeObject* nobj;
  Variant<DenseElementsIteratorForJSON, OwnNonIndexKeysIterForJSON> iter;
  bool isArray;  // Cached nobj->is<ArrayObject>()

  // Given an object, a FastStackEntry starts with the dense elements. The
  // caller is expected to inspect the variant to use it differently based on
  // which iterator is active.
  explicit FastStackEntry(NativeObject* obj)
      : nobj(obj),
        iter(AsVariant(DenseElementsIteratorForJSON(obj))),
        isArray(obj->is<ArrayObject>()) {}

  // Called by Vector when moving data around.
  FastStackEntry(FastStackEntry&& other) noexcept
      : nobj(other.nobj), iter(std::move(other.iter)), isArray(other.isArray) {}

  // Move assignment, called when updating the `top` entry.
  void operator=(FastStackEntry&& other) noexcept {
    nobj = other.nobj;
    iter = std::move(other.iter);
    isArray = other.isArray;
  }

  // Advance from dense elements to the named properties.
  void advanceToProperties() {
    iter = AsVariant(OwnNonIndexKeysIterForJSON(nobj));
  }
};

/* https://262.ecma-international.org/14.0/#sec-serializejsonproperty */
static bool FastSerializeJSONProperty(JSContext* cx, Handle<Value> v,
                                      StringifyContext* scx,
                                      BailReason* whySlow) {
  MOZ_ASSERT(*whySlow == BailReason::NO_REASON);
  MOZ_ASSERT(v.isObject());

  /*
   * FastSerializeJSONProperty is an optimistic fast path for the
   * SerializeJSONProperty algorithm that applies in limited situations. It
   * falls back to SerializeJSONProperty() if:
   *
   *   * Any externally visible code attempts to run: getter, enumerate
   *     hook, toJSON property.
   *   * Sparse index found (this would require accumulating props and sorting.)
   *   * Max stack depth is reached. (This will also detect self-referential
   *     input.)
   *
   *  Algorithm:
   *
   *    stack = []
   *    top = iter(obj)
   *    wroteMember = false
   *    OUTER: while true:
   *      if !wroteMember:
   *        emit("[" or "{")
   *      while !top.done():
   *        key, value = top.next()
   *        if top is a non-Array and value is skippable:
   *          continue
   *        if wroteMember:
   *          emit(",")
   *        wroteMember = true
   *        if value is object:
   *          emit(key + ":") if top is iterating a non-Array
   *          stack.push(top)
   *          top <- value
   *          wroteMember = false
   *          continue OUTER
   *        else:
   *          emit(value) or emit(key + ":" + value)
   *      emit("]" or "}")
   *      if stack is empty: done!
   *      top <- stack.pop()
   *      wroteMember = true
   *
   * except:
   *
   *   * The `while !top.done()` loop is split into the dense element portion
   *     and the slot portion. Each is iterated to completion before advancing
   *     or finishing.
   *
   *   * For Arrays, the named properties are not output, but they are still
   *     scanned to bail if any numeric keys are found that could be indexes.
   */

  // FastSerializeJSONProperty will bail if an interrupt is requested in the
  // middle of an operation, so handle any interrupts now before starting. Note:
  // this can GC, but after this point nothing should be able to GC unless
  // something fails, so rooting is unnecessary.
  if (!CheckForInterrupt(cx)) {
    return false;
  }

  constexpr size_t MAX_STACK_DEPTH = 20;
  Vector<FastStackEntry> stack(cx);
  if (!stack.reserve(MAX_STACK_DEPTH - 1)) {
    return false;
  }
  // Construct an iterator for the object,
  // https://262.ecma-international.org/14.0/#sec-serializejsonobject step 6:
  // EnumerableOwnPropertyNames or
  // https://262.ecma-international.org/14.0/#sec-serializejsonarray step 7-8.
  FastStackEntry top(&v.toObject().as<NativeObject>());
  bool wroteMember = false;

  if (!CanFastStringifyObject(top.nobj)) {
    *whySlow = BailReason::INELIGIBLE_OBJECT;
    return true;
  }

  while (true) {
    if (!wroteMember) {
      if (!scx->sb.append(top.isArray ? '[' : '{')) {
        return false;
      }
    }

    if (top.iter.is<DenseElementsIteratorForJSON>()) {
      auto& iter = top.iter.as<DenseElementsIteratorForJSON>();
      bool nestedObject = false;
      while (!iter.done()) {
        // Interrupts can GC and we are working with unrooted pointers.
        if (cx->hasPendingInterrupt(InterruptReason::CallbackUrgent) ||
            cx->hasPendingInterrupt(InterruptReason::CallbackCanWait)) {
          *whySlow = BailReason::INTERRUPT;
          return true;
        }

        uint32_t index = iter.getIndex();
        Value val = iter.next();

        if (!PreprocessFastValue(cx, &val, scx, whySlow)) {
          return false;
        }
        if (*whySlow != BailReason::NO_REASON) {
          return true;
        }
        if (IsFilteredValue(val)) {
          if (top.isArray) {
            // Arrays convert unrepresentable values to "null".
            val = UndefinedValue();
          } else {
            // Objects skip unrepresentable values.
            continue;
          }
        }

        if (wroteMember && !scx->sb.append(',')) {
          return false;
        }
        wroteMember = true;

        if (!top.isArray) {
          if (!EmitQuotedIndexColon(scx->sb, index)) {
            return false;
          }
        }

        if (val.isObject()) {
          if (stack.length() >= MAX_STACK_DEPTH - 1) {
            *whySlow = BailReason::DEEP_RECURSION;
            return true;
          }
          // Save the current iterator position on the stack and
          // switch to processing the nested value.
          stack.infallibleAppend(std::move(top));
          top = FastStackEntry(&val.toObject().as<NativeObject>());
          wroteMember = false;
          nestedObject = true;  // Break out to the outer loop.
          break;
        }
        if (!EmitSimpleValue(cx, scx->sb, val)) {
          return false;
        }
      }

      if (nestedObject) {
        continue;  // Break out to outer loop.
      }

      MOZ_ASSERT(iter.done());
      if (top.isArray) {
        MOZ_ASSERT(!top.nobj->isIndexed() || IsPackedArray(top.nobj));
      } else {
        top.advanceToProperties();
      }
    }

    if (top.iter.is<OwnNonIndexKeysIterForJSON>()) {
      auto& iter = top.iter.as<OwnNonIndexKeysIterForJSON>();
      bool nesting = false;
      while (!iter.done()) {
        // Interrupts can GC and we are working with unrooted pointers.
        if (cx->hasPendingInterrupt(InterruptReason::CallbackUrgent) ||
            cx->hasPendingInterrupt(InterruptReason::CallbackCanWait)) {
          *whySlow = BailReason::INTERRUPT;
          return true;
        }

        PropertyInfoWithKey prop = iter.next();

        // A non-Array with indexed elements would need to sort the indexes
        // numerically, which this code does not support. These objects are
        // skipped when obj->isIndexed(), so no index properties should be found
        // here.
        mozilla::DebugOnly<uint32_t> index = -1;
        MOZ_ASSERT(!IdIsIndex(prop.key(), &index));

        Value val = top.nobj->getSlot(prop.slot());
        if (!PreprocessFastValue(cx, &val, scx, whySlow)) {
          return false;
        }
        if (*whySlow != BailReason::NO_REASON) {
          return true;
        }
        if (IsFilteredValue(val)) {
          // Undefined check in
          // https://262.ecma-international.org/14.0/#sec-serializejsonobject
          // step 8b, covering undefined, symbol
          continue;
        }

        if (wroteMember && !scx->sb.append(",")) {
          return false;
        }
        wroteMember = true;

        MOZ_ASSERT(prop.key().isString());
        if (!QuoteJSONString(cx, scx->sb, prop.key().toString())) {
          return false;
        }

        if (!scx->sb.append(':')) {
          return false;
        }
        if (val.isObject()) {
          if (stack.length() >= MAX_STACK_DEPTH - 1) {
            *whySlow = BailReason::DEEP_RECURSION;
            return true;
          }
          // Save the current iterator position on the stack and
          // switch to processing the nested value.
          stack.infallibleAppend(std::move(top));
          top = FastStackEntry(&val.toObject().as<NativeObject>());
          wroteMember = false;
          nesting = true;  // Break out to the outer loop.
          break;
        }
        if (!EmitSimpleValue(cx, scx->sb, val)) {
          return false;
        }
      }
      *whySlow = iter.cannotFastStringify();
      if (*whySlow != BailReason::NO_REASON) {
        return true;
      }
      if (nesting) {
        continue;  // Break out to outer loop.
      }
      MOZ_ASSERT(iter.done());
    }

    if (!scx->sb.append(top.isArray ? ']' : '}')) {
      return false;
    }
    if (stack.empty()) {
      return true;  // Success!
    }
    top = std::move(stack.back());

    stack.popBack();
    wroteMember = true;
  }
}

/* https://262.ecma-international.org/14.0/#sec-json.stringify */
bool js::Stringify(JSContext* cx, MutableHandleValue vp, JSObject* replacer_,
                   const Value& space_, StringBuffer& sb,
                   StringifyBehavior stringifyBehavior) {
  RootedObject replacer(cx, replacer_);
  RootedValue space(cx, space_);

  MOZ_ASSERT_IF(stringifyBehavior == StringifyBehavior::RestrictedSafe,
                space.isNull());
  MOZ_ASSERT_IF(stringifyBehavior == StringifyBehavior::RestrictedSafe,
                vp.isObject());
  /**
   * This uses MOZ_ASSERT, since it's actually asserting something jsapi
   * consumers could get wrong, so needs a better error message.
   */
  MOZ_ASSERT(stringifyBehavior != StringifyBehavior::RestrictedSafe ||
                 vp.toObject().is<PlainObject>() ||
                 vp.toObject().is<ArrayObject>(),
             "input to JS::ToJSONMaybeSafely must be a plain object or array");

  /* Step 5. */
  RootedIdVector propertyList(cx);
  BailReason whySlow = BailReason::NO_REASON;
  if (stringifyBehavior == StringifyBehavior::SlowOnly ||
      stringifyBehavior == StringifyBehavior::RestrictedSafe) {
    whySlow = BailReason::API;
  }
  if (replacer) {
    whySlow = BailReason::HAVE_REPLACER;
    bool isArray;
    if (replacer->isCallable()) {
      /* Step 5a(i): use replacer to transform values.  */
    } else if (!IsArray(cx, replacer, &isArray)) {
      return false;
    } else if (isArray) {
      /* Step 5b(ii). */

      /* Step 5b(ii)(2). */
      uint32_t len;
      if (!GetLengthPropertyForArrayLike(cx, replacer, &len)) {
        return false;
      }

      // Cap the initial size to a moderately small value.  This avoids
      // ridiculous over-allocation if an array with bogusly-huge length
      // is passed in.  If we end up having to add elements past this
      // size, the set will naturally resize to accommodate them.
      const uint32_t MaxInitialSize = 32;
      Rooted<GCHashSet<jsid>> idSet(
          cx, GCHashSet<jsid>(cx, std::min(len, MaxInitialSize)));

      /* Step 5b(ii)(3). */
      uint32_t k = 0;

      /* Step 5b(ii)(4). */
      RootedValue item(cx);
      for (; k < len; k++) {
        if (!CheckForInterrupt(cx)) {
          return false;
        }

        /* Step 5b(ii)(4)(a-b). */
        if (!GetElement(cx, replacer, k, &item)) {
          return false;
        }

        /* Step 5b(ii)(4)(c-g). */
        RootedId id(cx);
        if (item.isNumber() || item.isString()) {
          if (!PrimitiveValueToId<CanGC>(cx, item, &id)) {
            return false;
          }
        } else {
          ESClass cls;
          if (!GetClassOfValue(cx, item, &cls)) {
            return false;
          }

          if (cls != ESClass::String && cls != ESClass::Number) {
            continue;
          }

          JSAtom* atom = ToAtom<CanGC>(cx, item);
          if (!atom) {
            return false;
          }

          id.set(AtomToId(atom));
        }

        /* Step 5b(ii)(4)(g). */
        auto p = idSet.lookupForAdd(id);
        if (!p) {
          /* Step 5b(ii)(4)(g)(i). */
          if (!idSet.add(p, id) || !propertyList.append(id)) {
            return false;
          }
        }
      }
    } else {
      replacer = nullptr;
    }
  }

  /* Step 6. */
  if (space.isObject()) {
    RootedObject spaceObj(cx, &space.toObject());

    ESClass cls;
    if (!JS::GetBuiltinClass(cx, spaceObj, &cls)) {
      return false;
    }

    if (cls == ESClass::Number) {
      double d;
      if (!ToNumber(cx, space, &d)) {
        return false;
      }
      space = NumberValue(d);
    } else if (cls == ESClass::String) {
      JSString* str = ToStringSlow<CanGC>(cx, space);
      if (!str) {
        return false;
      }
      space = StringValue(str);
    }
  }

  StringBuffer gap(cx);

  if (space.isNumber()) {
    /* Step 7. */
    double d;
    MOZ_ALWAYS_TRUE(ToInteger(cx, space, &d));
    d = std::min(10.0, d);
    if (d >= 1 && !gap.appendN(' ', uint32_t(d))) {
      return false;
    }
  } else if (space.isString()) {
    /* Step 8. */
    JSLinearString* str = space.toString()->ensureLinear(cx);
    if (!str) {
      return false;
    }
    size_t len = std::min(size_t(10), str->length());
    if (!gap.appendSubstring(str, 0, len)) {
      return false;
    }
  } else {
    /* Step 9. */
    MOZ_ASSERT(gap.empty());
  }
  if (!gap.empty()) {
    whySlow = BailReason::HAVE_SPACE;
  }

  Rooted<PlainObject*> wrapper(cx);
  RootedId emptyId(cx, NameToId(cx->names().empty_));
  if (replacer && replacer->isCallable()) {
    // We can skip creating the initial wrapper object if no replacer
    // function is present.

    /* Step 10. */
    wrapper = NewPlainObject(cx);
    if (!wrapper) {
      return false;
    }

    /* Step 11. */
    if (!NativeDefineDataProperty(cx, wrapper, emptyId, vp, JSPROP_ENUMERATE)) {
      return false;
    }
  }

  /* Step 13. */
  Rooted<JSAtom*> fastJSON(cx);
  if (whySlow == BailReason::NO_REASON) {
    MOZ_ASSERT(propertyList.empty());
    MOZ_ASSERT(stringifyBehavior != StringifyBehavior::RestrictedSafe);
    StringifyContext scx(cx, sb, gap, nullptr, propertyList, false);
    if (!PreprocessFastValue(cx, vp.address(), &scx, &whySlow)) {
      return false;
    }
    if (!vp.isObject()) {
      // "Fast" stringify of primitives would create a wrapper object and thus
      // be slower than regular stringify.
      whySlow = BailReason::PRIMITIVE;
    }
    if (whySlow == BailReason::NO_REASON) {
      if (!FastSerializeJSONProperty(cx, vp, &scx, &whySlow)) {
        return false;
      }
      if (whySlow == BailReason::NO_REASON) {
        // Fast stringify succeeded!
        if (stringifyBehavior != StringifyBehavior::Compare) {
          return true;
        }
        fastJSON = scx.sb.finishAtom();
        if (!fastJSON) {
          return false;
        }
      }
      scx.sb.clear();  // Preserves allocated space.
    }
  }

  if (MOZ_UNLIKELY((stringifyBehavior == StringifyBehavior::FastOnly) &&
                   (whySlow != BailReason::NO_REASON))) {
    JS_ReportErrorASCII(cx, "JSON stringify failed mandatory fast path: %s",
                        DescribeStringifyBailReason(whySlow));
    return false;
  }

  // Slow, general path.

  StringifyContext scx(cx, sb, gap, replacer, propertyList,
                       stringifyBehavior == StringifyBehavior::RestrictedSafe);
  if (!PreprocessValue(cx, wrapper, HandleId(emptyId), vp, &scx)) {
    return false;
  }
  if (IsFilteredValue(vp)) {
    return true;
  }

  if (!SerializeJSONProperty(cx, vp, &scx)) {
    return false;
  }

  // For StringBehavior::Compare, when the fast path succeeded.
  if (MOZ_UNLIKELY(fastJSON)) {
    JSAtom* slowJSON = scx.sb.finishAtom();
    if (!slowJSON) {
      return false;
    }
    if (fastJSON != slowJSON) {
      MOZ_CRASH("JSON.stringify mismatch between fast and slow paths");
    }
    // Put the JSON back into the StringBuffer for returning.
    if (!sb.append(slowJSON)) {
      return false;
    }
  }

  return true;
}

/* https://262.ecma-international.org/14.0/#sec-internalizejsonproperty */
static bool InternalizeJSONProperty(
    JSContext* cx, HandleObject holder, HandleId name, HandleValue reviver,
    MutableHandle<ParseRecordObject> parseRecord, MutableHandleValue vp) {
  AutoCheckRecursionLimit recursion(cx);
  if (!recursion.check(cx)) {
    return false;
  }

  /* Step 1. */
  RootedValue val(cx);
  if (!GetProperty(cx, holder, holder, name, &val)) {
    return false;
  }

  /* Step 2. */
  if (val.isObject()) {
    RootedObject obj(cx, &val.toObject());

    bool isArray;
    if (!IsArray(cx, obj, &isArray)) {
      return false;
    }

    if (isArray) {
      /* Step 2b(i). */
      uint32_t length;
      if (!GetLengthPropertyForArrayLike(cx, obj, &length)) {
        return false;
      }

      /* Steps 2b(ii-iii). */
      RootedId id(cx);
      RootedValue newElement(cx);
      for (uint32_t i = 0; i < length; i++) {
        if (!CheckForInterrupt(cx)) {
          return false;
        }

        if (!IndexToId(cx, i, &id)) {
          return false;
        }

        /* Step 2a(iii)(1). */
        Rooted<ParseRecordObject> elementRecord(cx);
        if (!InternalizeJSONProperty(cx, obj, id, reviver, &elementRecord,
                                     &newElement)) {
          return false;
        }

        ObjectOpResult ignored;
        if (newElement.isUndefined()) {
          /* Step 2b(iii)(3). The spec deliberately ignores strict failure. */
          if (!DeleteProperty(cx, obj, id, ignored)) {
            return false;
          }
        } else {
          /* Step 2b(iii)(4). The spec deliberately ignores strict failure. */
          Rooted<PropertyDescriptor> desc(
              cx, PropertyDescriptor::Data(newElement,
                                           {JS::PropertyAttribute::Configurable,
                                            JS::PropertyAttribute::Enumerable,
                                            JS::PropertyAttribute::Writable}));
          if (!DefineProperty(cx, obj, id, desc, ignored)) {
            return false;
          }
        }
      }
    } else {
      /* Step 2c(i). */
      RootedIdVector keys(cx);
      if (!GetPropertyKeys(cx, obj, JSITER_OWNONLY, &keys)) {
        return false;
      }

      /* Step 2c(ii). */
      RootedId id(cx);
      RootedValue newElement(cx);
      for (size_t i = 0, len = keys.length(); i < len; i++) {
        if (!CheckForInterrupt(cx)) {
          return false;
        }

        /* Step 2c(ii)(1). */
        id = keys[i];
        Rooted<ParseRecordObject> entryRecord(cx);
        if (!InternalizeJSONProperty(cx, obj, id, reviver, &entryRecord,
                                     &newElement)) {
          return false;
        }

        ObjectOpResult ignored;
        if (newElement.isUndefined()) {
          /* Step 2c(ii)(2). The spec deliberately ignores strict failure. */
          if (!DeleteProperty(cx, obj, id, ignored)) {
            return false;
          }
        } else {
          /* Step 2c(ii)(3). The spec deliberately ignores strict failure. */
          Rooted<PropertyDescriptor> desc(
              cx, PropertyDescriptor::Data(newElement,
                                           {JS::PropertyAttribute::Configurable,
                                            JS::PropertyAttribute::Enumerable,
                                            JS::PropertyAttribute::Writable}));
          if (!DefineProperty(cx, obj, id, desc, ignored)) {
            return false;
          }
        }
      }
    }
  }

  /* Step 3. */
  RootedString key(cx, IdToString(cx, name));
  if (!key) {
    return false;
  }

  RootedValue keyVal(cx, StringValue(key));
#ifdef ENABLE_JSON_PARSE_WITH_SOURCE
  if (cx->realm()->creationOptions().getJSONParseWithSource()) {
    RootedObject context(cx, NewPlainObject(cx));
    if (!context) {
      return false;
    }
    Rooted<Value> parseNode(cx, StringValue(parseRecord.get().parseNode));
    if (!DefineDataProperty(cx, context, cx->names().source, parseNode)) {
      return false;
    }
    RootedValue contextVal(cx, ObjectValue(*context));
    return js::Call(cx, reviver, holder, keyVal, val, contextVal, vp);
  }
#endif
  return js::Call(cx, reviver, holder, keyVal, val, vp);
}

static bool Revive(JSContext* cx, HandleValue reviver,
                   MutableHandle<ParseRecordObject> pro,
                   MutableHandleValue vp) {
  Rooted<PlainObject*> obj(cx, NewPlainObject(cx));
  if (!obj) {
    return false;
  }

  if (!DefineDataProperty(cx, obj, cx->names().empty_, vp)) {
    return false;
  }

#ifdef ENABLE_JSON_PARSE_WITH_SOURCE
  MOZ_ASSERT_IF(cx->realm()->creationOptions().getJSONParseWithSource(),
                pro.get().value == vp.get());
#endif
  Rooted<jsid> id(cx, NameToId(cx->names().empty_));
  return InternalizeJSONProperty(cx, obj, id, reviver, pro, vp);
}

template <typename CharT>
bool ParseJSON(JSContext* cx, const mozilla::Range<const CharT> chars,
               MutableHandleValue vp, MutableHandle<ParseRecordObject> pro) {
  Rooted<JSONParser<CharT>> parser(cx, cx, chars,
                                   JSONParser<CharT>::ParseType::JSONParse);
  return parser.parse(vp, pro);
}

template <typename CharT>
bool js::ParseJSONWithReviver(JSContext* cx,
                              const mozilla::Range<const CharT> chars,
                              HandleValue reviver, MutableHandleValue vp) {
  /* https://262.ecma-international.org/14.0/#sec-json.parse steps 2-10. */
  Rooted<ParseRecordObject> pro(cx);
  if (!ParseJSON(cx, chars, vp, &pro)) {
    return false;
  }

  /* Steps 11-12. */
  if (IsCallable(reviver)) {
    return Revive(cx, reviver, &pro, vp);
  }
  return true;
}

template bool js::ParseJSONWithReviver(
    JSContext* cx, const mozilla::Range<const Latin1Char> chars,
    HandleValue reviver, MutableHandleValue vp);

template bool js::ParseJSONWithReviver(
    JSContext* cx, const mozilla::Range<const char16_t> chars,
    HandleValue reviver, MutableHandleValue vp);

static bool json_toSource(JSContext* cx, unsigned argc, Value* vp) {
  CallArgs args = CallArgsFromVp(argc, vp);
  args.rval().setString(cx->names().JSON);
  return true;
}

/* https://262.ecma-international.org/14.0/#sec-json.parse */
static bool json_parse(JSContext* cx, unsigned argc, Value* vp) {
  AutoJSMethodProfilerEntry pseudoFrame(cx, "JSON", "parse");
  CallArgs args = CallArgsFromVp(argc, vp);

  /* Step 1. */
  JSString* str = (args.length() >= 1) ? ToString<CanGC>(cx, args[0])
                                       : cx->names().undefined;
  if (!str) {
    return false;
  }

  JSLinearString* linear = str->ensureLinear(cx);
  if (!linear) {
    return false;
  }

  AutoStableStringChars linearChars(cx);
  if (!linearChars.init(cx, linear)) {
    return false;
  }

  HandleValue reviver = args.get(1);

  /* Steps 2-12. */
  return linearChars.isLatin1()
             ? ParseJSONWithReviver(cx, linearChars.latin1Range(), reviver,
                                    args.rval())
             : ParseJSONWithReviver(cx, linearChars.twoByteRange(), reviver,
                                    args.rval());
}

#ifdef ENABLE_RECORD_TUPLE
bool BuildImmutableProperty(JSContext* cx, HandleValue value, HandleId name,
                            HandleValue reviver,
                            MutableHandleValue immutableRes) {
  MOZ_ASSERT(!name.isSymbol());

  // Step 1
  if (value.isObject()) {
    RootedValue childValue(cx), newElement(cx);
    RootedId childName(cx);

    // Step 1.a-1.b
    if (value.toObject().is<ArrayObject>()) {
      Rooted<ArrayObject*> arr(cx, &value.toObject().as<ArrayObject>());

      // Step 1.b.iii
      uint32_t len = arr->length();

      TupleType* tup = TupleType::createUninitialized(cx, len);
      if (!tup) {
        return false;
      }
      immutableRes.setExtendedPrimitive(*tup);

      // Step 1.b.iv
      for (uint32_t i = 0; i < len; i++) {
        // Step 1.b.iv.1
        childName.set(PropertyKey::Int(i));

        // Step 1.b.iv.2
        if (!GetProperty(cx, arr, value, childName, &childValue)) {
          return false;
        }

        // Step 1.b.iv.3
        if (!BuildImmutableProperty(cx, childValue, childName, reviver,
                                    &newElement)) {
          return false;
        }
        MOZ_ASSERT(newElement.isPrimitive());

        // Step 1.b.iv.5
        if (!tup->initializeNextElement(cx, newElement)) {
          return false;
        }
      }

      // Step 1.b.v
      tup->finishInitialization(cx);
    } else {
      RootedObject obj(cx, &value.toObject());

      // Step 1.c.i - We only get the property keys rather than the
      // entries, but the difference is not observable from user code
      // because `obj` is a plan object not exposed externally
      RootedIdVector props(cx);
      if (!GetPropertyKeys(cx, obj, JSITER_OWNONLY, &props)) {
        return false;
      }

      RecordType* rec = RecordType::createUninitialized(cx, props.length());
      if (!rec) {
        return false;
      }
      immutableRes.setExtendedPrimitive(*rec);

      for (uint32_t i = 0; i < props.length(); i++) {
        // Step 1.c.iii.1
        childName.set(props[i]);

        // Step 1.c.iii.2
        if (!GetProperty(cx, obj, value, childName, &childValue)) {
          return false;
        }

        // Step 1.c.iii.3
        if (!BuildImmutableProperty(cx, childValue, childName, reviver,
                                    &newElement)) {
          return false;
        }
        MOZ_ASSERT(newElement.isPrimitive());

        // Step 1.c.iii.5
        if (!newElement.isUndefined()) {
          // Step 1.c.iii.5.a-b
          rec->initializeNextProperty(cx, childName, newElement);
        }
      }

      // Step 1.c.iv
      rec->finishInitialization(cx);
    }
  } else {
    // Step 2.a
    immutableRes.set(value);
  }

  // Step 3
  if (IsCallable(reviver)) {
    RootedValue keyVal(cx, StringValue(IdToString(cx, name)));

    // Step 3.a
    if (!Call(cx, reviver, UndefinedHandleValue, keyVal, immutableRes,
              immutableRes)) {
      return false;
    }

    // Step 3.b
    if (!immutableRes.isPrimitive()) {
      JS_ReportErrorNumberASCII(cx, GetErrorMessage, nullptr,
                                JSMSG_RECORD_TUPLE_NO_OBJECT);
      return false;
    }
  }

  return true;
}

static bool json_parseImmutable(JSContext* cx, unsigned argc, Value* vp) {
  AutoJSMethodProfilerEntry pseudoFrame(cx, "JSON", "parseImmutable");
  CallArgs args = CallArgsFromVp(argc, vp);

  /* Step 1. */
  JSString* str = (args.length() >= 1) ? ToString<CanGC>(cx, args[0])
                                       : cx->names().undefined;
  if (!str) {
    return false;
  }

  JSLinearString* linear = str->ensureLinear(cx);
  if (!linear) {
    return false;
  }

  AutoStableStringChars linearChars(cx);
  if (!linearChars.init(cx, linear)) {
    return false;
  }

  HandleValue reviver = args.get(1);
  RootedValue unfiltered(cx);
  Rooted<ParseRecordObject> pro(cx);

  if (linearChars.isLatin1()) {
    if (!ParseJSON(cx, linearChars.latin1Range(), &unfiltered, &pro)) {
      return false;
    }
  } else {
    if (!ParseJSON(cx, linearChars.twoByteRange(), &unfiltered, &pro)) {
      return false;
    }
  }

  RootedId id(cx, NameToId(cx->names().empty_));
  return BuildImmutableProperty(cx, unfiltered, id, reviver, args.rval());
}
#endif

/* https://262.ecma-international.org/14.0/#sec-json.stringify */
bool json_stringify(JSContext* cx, unsigned argc, Value* vp) {
  AutoJSMethodProfilerEntry pseudoFrame(cx, "JSON", "stringify");
  CallArgs args = CallArgsFromVp(argc, vp);

  RootedObject replacer(cx,
                        args.get(1).isObject() ? &args[1].toObject() : nullptr);
  RootedValue value(cx, args.get(0));
  RootedValue space(cx, args.get(2));

#ifdef DEBUG
  StringifyBehavior behavior = StringifyBehavior::Compare;
#else
  StringifyBehavior behavior = StringifyBehavior::Normal;
#endif

  JSStringBuilder sb(cx);
  if (!Stringify(cx, &value, replacer, space, sb, behavior)) {
    return false;
  }

  // XXX This can never happen to nsJSON.cpp, but the JSON object
  // needs to support returning undefined. So this is a little awkward
  // for the API, because we want to support streaming writers.
  if (!sb.empty()) {
    JSString* str = sb.finishString();
    if (!str) {
      return false;
    }
    args.rval().setString(str);
  } else {
    args.rval().setUndefined();
  }

  return true;
}

static const JSFunctionSpec json_static_methods[] = {
    JS_FN("toSource", json_toSource, 0, 0), JS_FN("parse", json_parse, 2, 0),
    JS_FN("stringify", json_stringify, 3, 0),
#ifdef ENABLE_RECORD_TUPLE
    JS_FN("parseImmutable", json_parseImmutable, 2, 0),
#endif
    JS_FS_END};

static const JSPropertySpec json_static_properties[] = {
    JS_STRING_SYM_PS(toStringTag, "JSON", JSPROP_READONLY), JS_PS_END};

static JSObject* CreateJSONObject(JSContext* cx, JSProtoKey key) {
  RootedObject proto(cx, &cx->global()->getObjectPrototype());
  return NewTenuredObjectWithGivenProto(cx, &JSONClass, proto);
}

static const ClassSpec JSONClassSpec = {
    CreateJSONObject, nullptr, json_static_methods, json_static_properties};

const JSClass js::JSONClass = {"JSON", JSCLASS_HAS_CACHED_PROTO(JSProto_JSON),
                               JS_NULL_CLASS_OPS, &JSONClassSpec};