DXR is a code search and navigation tool aimed at making sense of large projects. It supports full-text and regex searches as well as structural queries.

Header

Mercurial (b6d82b1a6b02)

VCS Links

Line Code
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 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391
/* -*- 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 "vm/StringType-inl.h"

#include "mozilla/ArrayUtils.h"
#include "mozilla/Casting.h"
#include "mozilla/FloatingPoint.h"
#include "mozilla/HashFunctions.h"
#include "mozilla/MathAlgorithms.h"
#include "mozilla/MemoryReporting.h"
#include "mozilla/PodOperations.h"
#include "mozilla/RangedPtr.h"
#include "mozilla/TextUtils.h"
#include "mozilla/TypeTraits.h"
#include "mozilla/Unused.h"

#include <algorithm>    // std::{all_of,copy_n,enable_if,is_const,move}
#include <type_traits>  // std::is_unsigned

#include "jsfriendapi.h"

#include "frontend/BytecodeCompiler.h"
#include "gc/GCInternals.h"
#include "gc/Marking.h"
#include "gc/Nursery.h"
#include "js/CharacterEncoding.h"
#include "js/StableStringChars.h"
#include "js/Symbol.h"
#include "js/UbiNode.h"
#include "util/StringBuffer.h"
#include "vm/GeckoProfiler.h"

#include "vm/GeckoProfiler-inl.h"
#include "vm/JSContext-inl.h"
#include "vm/JSObject-inl.h"
#include "vm/Realm-inl.h"

using namespace js;

using mozilla::ArrayEqual;
using mozilla::AssertedCast;
using mozilla::IsAsciiDigit;
using mozilla::IsNegativeZero;
using mozilla::IsSame;
using mozilla::PodCopy;
using mozilla::RangedPtr;
using mozilla::RoundUpPow2;
using mozilla::Unused;

using JS::AutoCheckCannotGC;
using JS::AutoStableStringChars;

using UniqueLatin1Chars = UniquePtr<Latin1Char[], JS::FreePolicy>;

size_t JSString::sizeOfExcludingThis(mozilla::MallocSizeOf mallocSizeOf) {
  // JSRope: do nothing, we'll count all children chars when we hit the leaf
  // strings.
  if (isRope()) {
    return 0;
  }

  MOZ_ASSERT(isLinear());

  // JSDependentString: do nothing, we'll count the chars when we hit the base
  // string.
  if (isDependent()) {
    return 0;
  }

  // JSExternalString: Ask the embedding to tell us what's going on.  If it
  // doesn't want to say, don't count, the chars could be stored anywhere.
  if (isExternal()) {
    if (auto* cb =
            runtimeFromMainThread()->externalStringSizeofCallback.ref()) {
      // Our callback isn't supposed to cause GC.
      JS::AutoSuppressGCAnalysis nogc;
      return cb(this, mallocSizeOf);
    }
    return 0;
  }

  MOZ_ASSERT(isFlat());

  // JSExtensibleString: count the full capacity, not just the used space.
  if (isExtensible()) {
    JSExtensibleString& extensible = asExtensible();
    return extensible.hasLatin1Chars()
               ? mallocSizeOf(extensible.rawLatin1Chars())
               : mallocSizeOf(extensible.rawTwoByteChars());
  }

  // JSInlineString, JSFatInlineString [JSInlineAtom, JSFatInlineAtom]: the
  // chars are inline.
  if (isInline()) {
    return 0;
  }

  // JSAtom, JSUndependedString: measure the space for the chars.  For
  // JSUndependedString, there is no need to count the base string, for the
  // same reason as JSDependentString above.
  JSFlatString& flat = asFlat();
  return flat.hasLatin1Chars() ? mallocSizeOf(flat.rawLatin1Chars())
                               : mallocSizeOf(flat.rawTwoByteChars());
}

JS::ubi::Node::Size JS::ubi::Concrete<JSString>::size(
    mozilla::MallocSizeOf mallocSizeOf) const {
  JSString& str = get();
  size_t size;
  if (str.isAtom()) {
    size =
        str.isFatInline() ? sizeof(js::FatInlineAtom) : sizeof(js::NormalAtom);
  } else {
    size = str.isFatInline() ? sizeof(JSFatInlineString) : sizeof(JSString);
  }

  if (IsInsideNursery(&str)) {
    size += Nursery::stringHeaderSize();
  }

  size += str.sizeOfExcludingThis(mallocSizeOf);

  return size;
}

const char16_t JS::ubi::Concrete<JSString>::concreteTypeName[] = u"JSString";

#if defined(DEBUG) || defined(JS_JITSPEW)

template <typename CharT>
/*static */
void JSString::dumpChars(const CharT* s, size_t n, js::GenericPrinter& out) {
  if (n == SIZE_MAX) {
    n = 0;
    while (s[n]) {
      n++;
    }
  }

  out.put("\"");
  for (size_t i = 0; i < n; i++) {
    char16_t c = s[i];
    if (c == '\n') {
      out.put("\\n");
    } else if (c == '\t') {
      out.put("\\t");
    } else if (c >= 32 && c < 127) {
      out.putChar((char)s[i]);
    } else if (c <= 255) {
      out.printf("\\x%02x", unsigned(c));
    } else {
      out.printf("\\u%04x", unsigned(c));
    }
  }
  out.putChar('"');
}

template void JSString::dumpChars(const Latin1Char* s, size_t n,
                                  js::GenericPrinter& out);

template void JSString::dumpChars(const char16_t* s, size_t n,
                                  js::GenericPrinter& out);

void JSString::dumpCharsNoNewline(js::GenericPrinter& out) {
  if (JSLinearString* linear = ensureLinear(nullptr)) {
    AutoCheckCannotGC nogc;
    if (hasLatin1Chars()) {
      dumpChars(linear->latin1Chars(nogc), length(), out);
    } else {
      dumpChars(linear->twoByteChars(nogc), length(), out);
    }
  } else {
    out.put("(oom in JSString::dumpCharsNoNewline)");
  }
}

void JSString::dump() {
  js::Fprinter out(stderr);
  dump(out);
}

void JSString::dump(js::GenericPrinter& out) {
  dumpNoNewline(out);
  out.putChar('\n');
}

void JSString::dumpNoNewline(js::GenericPrinter& out) {
  if (JSLinearString* linear = ensureLinear(nullptr)) {
    AutoCheckCannotGC nogc;
    if (hasLatin1Chars()) {
      const Latin1Char* chars = linear->latin1Chars(nogc);
      out.printf("JSString* (%p) = Latin1Char * (%p) = ", (void*)this,
                 (void*)chars);
      dumpChars(chars, length(), out);
    } else {
      const char16_t* chars = linear->twoByteChars(nogc);
      out.printf("JSString* (%p) = char16_t * (%p) = ", (void*)this,
                 (void*)chars);
      dumpChars(chars, length(), out);
    }
  } else {
    out.put("(oom in JSString::dump)");
  }
}

void JSString::dumpRepresentation(js::GenericPrinter& out, int indent) const {
  if (isRope()) {
    asRope().dumpRepresentation(out, indent);
  } else if (isDependent()) {
    asDependent().dumpRepresentation(out, indent);
  } else if (isExternal()) {
    asExternal().dumpRepresentation(out, indent);
  } else if (isExtensible()) {
    asExtensible().dumpRepresentation(out, indent);
  } else if (isInline()) {
    asInline().dumpRepresentation(out, indent);
  } else if (isFlat()) {
    asFlat().dumpRepresentation(out, indent);
  } else {
    MOZ_CRASH("Unexpected JSString representation");
  }
}

void JSString::dumpRepresentationHeader(js::GenericPrinter& out,
                                        const char* subclass) const {
  uint32_t flags = JSString::flags();
  // Print the string's address as an actual C++ expression, to facilitate
  // copy-and-paste into a debugger.
  out.printf("((%s*) %p) length: %zu  flags: 0x%x", subclass, this, length(),
             flags);
  if (flags & LINEAR_BIT) out.put(" LINEAR");
  if (flags & HAS_BASE_BIT) out.put(" HAS_BASE");
  if (flags & INLINE_CHARS_BIT) out.put(" INLINE_CHARS");
  if (flags & NON_ATOM_BIT)
    out.put(" NON_ATOM");
  else
    out.put(" (ATOM)");
  if (isPermanentAtom()) out.put(" PERMANENT");
  if (flags & LATIN1_CHARS_BIT) out.put(" LATIN1");
  if (flags & INDEX_VALUE_BIT) out.printf(" INDEX_VALUE(%u)", getIndexValue());
  if (!isTenured()) out.put(" NURSERY");
  out.putChar('\n');
}

void JSLinearString::dumpRepresentationChars(js::GenericPrinter& out,
                                             int indent) const {
  if (hasLatin1Chars()) {
    out.printf("%*schars: ((Latin1Char*) %p) ", indent, "", rawLatin1Chars());
    dumpChars(rawLatin1Chars(), length(), out);
  } else {
    out.printf("%*schars: ((char16_t*) %p) ", indent, "", rawTwoByteChars());
    dumpChars(rawTwoByteChars(), length(), out);
  }
  out.putChar('\n');
}

bool JSString::equals(const char* s) {
  JSLinearString* linear = ensureLinear(nullptr);
  if (!linear) {
    // This is DEBUG-only code.
    fprintf(stderr, "OOM in JSString::equals!\n");
    return false;
  }

  return StringEqualsAscii(linear, s);
}
#endif /* defined(DEBUG) || defined(JS_JITSPEW) */

template <typename CharT>
static MOZ_ALWAYS_INLINE bool AllocChars(JSString* str, size_t length,
                                         CharT** chars, size_t* capacity) {
  /*
   * String length doesn't include the null char, so include it here before
   * doubling. Adding the null char after doubling would interact poorly with
   * round-up malloc schemes.
   */
  size_t numChars = length + 1;

  /*
   * Grow by 12.5% if the buffer is very large. Otherwise, round up to the
   * next power of 2. This is similar to what we do with arrays; see
   * JSObject::ensureDenseArrayElements.
   */
  static const size_t DOUBLING_MAX = 1024 * 1024;
  numChars = numChars > DOUBLING_MAX ? numChars + (numChars / 8)
                                     : RoundUpPow2(numChars);

  /* Like length, capacity does not include the null char, so take it out. */
  *capacity = numChars - 1;

  JS_STATIC_ASSERT(JSString::MAX_LENGTH * sizeof(CharT) < UINT32_MAX);
  *chars = str->zone()->pod_malloc<CharT>(numChars, js::StringBufferArena);
  return *chars != nullptr;
}

UniqueLatin1Chars JSRope::copyLatin1CharsZ(JSContext* maybecx,
                                           arena_id_t destArenaId) const {
  return copyCharsInternal<Latin1Char>(maybecx, true, destArenaId);
}

UniqueTwoByteChars JSRope::copyTwoByteCharsZ(JSContext* maybecx,
                                             arena_id_t destArenaId) const {
  return copyCharsInternal<char16_t>(maybecx, true, destArenaId);
}

UniqueLatin1Chars JSRope::copyLatin1Chars(JSContext* maybecx,
                                          arena_id_t destArenaId) const {
  return copyCharsInternal<Latin1Char>(maybecx, false, destArenaId);
}

UniqueTwoByteChars JSRope::copyTwoByteChars(JSContext* maybecx,
                                            arena_id_t destArenaId) const {
  return copyCharsInternal<char16_t>(maybecx, false, destArenaId);
}

template <typename CharT>
UniquePtr<CharT[], JS::FreePolicy> JSRope::copyCharsInternal(
    JSContext* maybecx, bool nullTerminate, arena_id_t destArenaId) const {
  // Left-leaning ropes are far more common than right-leaning ropes, so
  // perform a non-destructive traversal of the rope, right node first,
  // splatting each node's characters into a contiguous buffer.

  size_t n = length();

  UniquePtr<CharT[], JS::FreePolicy> out;
  if (maybecx) {
    out.reset(maybecx->pod_malloc<CharT>(n + 1, destArenaId));
  } else {
    out.reset(js_pod_arena_malloc<CharT>(destArenaId, n + 1));
  }

  if (!out) {
    return nullptr;
  }

  Vector<const JSString*, 8, SystemAllocPolicy> nodeStack;
  const JSString* str = this;
  CharT* end = out.get() + str->length();
  while (true) {
    if (str->isRope()) {
      if (!nodeStack.append(str->asRope().leftChild())) {
        if (maybecx) {
          ReportOutOfMemory(maybecx);
        }
        return nullptr;
      }
      str = str->asRope().rightChild();
    } else {
      end -= str->length();
      CopyChars(end, str->asLinear());
      if (nodeStack.empty()) {
        break;
      }
      str = nodeStack.popCopy();
    }
  }

  MOZ_ASSERT(end == out.get());

  if (nullTerminate) {
    out[n] = 0;
  }

  return out;
}

template <typename CharT>
void AddStringToHash(uint32_t* hash, const CharT* chars, size_t len) {
  // It's tempting to use |HashString| instead of this loop, but that's
  // slightly different than our existing implementation for non-ropes. We
  // want to pretend we have a contiguous set of chars so we need to
  // accumulate char by char rather than generate a new hash for substring
  // and then accumulate that.
  for (size_t i = 0; i < len; i++) {
    *hash = mozilla::AddToHash(*hash, chars[i]);
  }
}

void AddStringToHash(uint32_t* hash, const JSString* str) {
  AutoCheckCannotGC nogc;
  const auto& s = str->asLinear();
  if (s.hasLatin1Chars()) {
    AddStringToHash(hash, s.latin1Chars(nogc), s.length());
  } else {
    AddStringToHash(hash, s.twoByteChars(nogc), s.length());
  }
}

bool JSRope::hash(uint32_t* outHash) const {
  Vector<const JSString*, 8, SystemAllocPolicy> nodeStack;
  const JSString* str = this;

  *outHash = 0;

  while (true) {
    if (str->isRope()) {
      if (!nodeStack.append(str->asRope().rightChild())) {
        return false;
      }
      str = str->asRope().leftChild();
    } else {
      AddStringToHash(outHash, str);
      if (nodeStack.empty()) {
        break;
      }
      str = nodeStack.popCopy();
    }
  }

  return true;
}

#if defined(DEBUG) || defined(JS_JITSPEW)
void JSRope::dumpRepresentation(js::GenericPrinter& out, int indent) const {
  dumpRepresentationHeader(out, "JSRope");
  indent += 2;

  out.printf("%*sleft:  ", indent, "");
  leftChild()->dumpRepresentation(out, indent);

  out.printf("%*sright: ", indent, "");
  rightChild()->dumpRepresentation(out, indent);
}
#endif

namespace js {

template <>
void CopyChars(char16_t* dest, const JSLinearString& str) {
  AutoCheckCannotGC nogc;
  if (str.hasTwoByteChars()) {
    PodCopy(dest, str.twoByteChars(nogc), str.length());
  } else {
    CopyAndInflateChars(dest, str.latin1Chars(nogc), str.length());
  }
}

template <>
void CopyChars(Latin1Char* dest, const JSLinearString& str) {
  AutoCheckCannotGC nogc;
  if (str.hasLatin1Chars()) {
    PodCopy(dest, str.latin1Chars(nogc), str.length());
  } else {
    /*
     * When we flatten a TwoByte rope, we turn child ropes (including Latin1
     * ropes) into TwoByte dependent strings. If one of these strings is
     * also part of another Latin1 rope tree, we can have a Latin1 rope with
     * a TwoByte descendent and we end up here when we flatten it. Although
     * the chars are stored as TwoByte, we know they must be in the Latin1
     * range, so we can safely deflate here.
     */
    size_t len = str.length();
    const char16_t* chars = str.twoByteChars(nogc);
    for (size_t i = 0; i < len; i++) {
      MOZ_ASSERT(chars[i] <= JSString::MAX_LATIN1_CHAR);
      dest[i] = chars[i];
    }
  }
}

} /* namespace js */

template <JSRope::UsingBarrier b, typename CharT>
JSFlatString* JSRope::flattenInternal(JSContext* maybecx) {
  /*
   * Consider the DAG of JSRopes rooted at this JSRope, with non-JSRopes as
   * its leaves. Mutate the root JSRope into a JSExtensibleString containing
   * the full flattened text that the root represents, and mutate all other
   * JSRopes in the interior of the DAG into JSDependentStrings that refer to
   * this new JSExtensibleString.
   *
   * If the leftmost leaf of our DAG is a JSExtensibleString, consider
   * stealing its buffer for use in our new root, and transforming it into a
   * JSDependentString too. Do not mutate any of the other leaves.
   *
   * Perform a depth-first dag traversal, splatting each node's characters
   * into a contiguous buffer. Visit each rope node three times:
   *   1. record position in the buffer and recurse into left child;
   *   2. recurse into the right child;
   *   3. transform the node into a dependent string.
   * To avoid maintaining a stack, tree nodes are mutated to indicate how many
   * times they have been visited. Since ropes can be dags, a node may be
   * encountered multiple times during traversal. However, step 3 above leaves
   * a valid dependent string, so everything works out.
   *
   * While ropes avoid all sorts of quadratic cases with string concatenation,
   * they can't help when ropes are immediately flattened. One idiomatic case
   * that we'd like to keep linear (and has traditionally been linear in SM
   * and other JS engines) is:
   *
   *   while (...) {
   *     s += ...
   *     s.flatten
   *   }
   *
   * Two behaviors accomplish this:
   *
   * - When the leftmost non-rope in the DAG we're flattening is a
   *   JSExtensibleString with sufficient capacity to hold the entire
   *   flattened string, we just flatten the DAG into its buffer. Then, when
   *   we transform the root of the DAG from a JSRope into a
   *   JSExtensibleString, we steal that buffer, and change the victim from a
   *   JSExtensibleString to a JSDependentString. In this case, the left-hand
   *   side of the string never needs to be copied.
   *
   * - Otherwise, we round up the total flattened size and create a fresh
   *   JSExtensibleString with that much capacity. If this in turn becomes the
   *   leftmost leaf of a subsequent flatten, we will hopefully be able to
   *   fill it, as in the case above.
   *
   * Note that, even though the code for creating JSDependentStrings avoids
   * creating dependents of dependents, we can create that situation here: the
   * JSExtensibleStrings we transform into JSDependentStrings might have
   * JSDependentStrings pointing to them already. Stealing the buffer doesn't
   * change its address, only its owning JSExtensibleString, so all chars()
   * pointers in the JSDependentStrings are still valid.
   */
  const size_t wholeLength = length();
  size_t wholeCapacity;
  CharT* wholeChars;
  JSString* str = this;
  CharT* pos;

  // JSString::setFlattenData() is used to store a tagged pointer to the
  // parent node. The tag indicates what to do when we return to the parent.
  static const uintptr_t Tag_Mask = 0x3;
  static const uintptr_t Tag_FinishNode = 0x0;
  static const uintptr_t Tag_VisitRightChild = 0x1;

  AutoCheckCannotGC nogc;

  gc::StoreBuffer* bufferIfNursery = storeBuffer();

  /* Find the left most string, containing the first string. */
  JSRope* leftMostRope = this;
  while (leftMostRope->leftChild()->isRope()) {
    leftMostRope = &leftMostRope->leftChild()->asRope();
  }

  if (leftMostRope->leftChild()->isExtensible()) {
    JSExtensibleString& left = leftMostRope->leftChild()->asExtensible();
    size_t capacity = left.capacity();
    if (capacity >= wholeLength &&
        left.hasTwoByteChars() == IsSame<CharT, char16_t>::value) {
      wholeChars = const_cast<CharT*>(left.nonInlineChars<CharT>(nogc));
      wholeCapacity = capacity;

      // registerMallocedBuffer is fallible, so attempt it first before doing
      // anything irreversible.
      Nursery& nursery = runtimeFromMainThread()->gc.nursery();
      bool inTenured = !bufferIfNursery;
      if (!inTenured && left.isTenured()) {
        // tenured leftmost child is giving its chars buffer to the
        // nursery-allocated root node.
        if (!nursery.registerMallocedBuffer(wholeChars)) {
          if (maybecx) {
            ReportOutOfMemory(maybecx);
          }
          return nullptr;
        }
        // leftmost child -> root is a tenured -> nursery edge.
        bufferIfNursery->putWholeCell(&left);
      } else if (inTenured && !left.isTenured()) {
        // leftmost child is giving its nursery-held chars buffer to a
        // tenured string.
        nursery.removeMallocedBuffer(wholeChars);
      }

      /*
       * Simulate a left-most traversal from the root to leftMost->leftChild()
       * via first_visit_node
       */
      MOZ_ASSERT(str->isRope());
      while (str != leftMostRope) {
        if (b == WithIncrementalBarrier) {
          JSString::writeBarrierPre(str->d.s.u2.left);
          JSString::writeBarrierPre(str->d.s.u3.right);
        }
        JSString* child = str->d.s.u2.left;
        // 'child' will be post-barriered during the later traversal.
        MOZ_ASSERT(child->isRope());
        str->setNonInlineChars(wholeChars);
        child->setFlattenData(uintptr_t(str) | Tag_VisitRightChild);
        str = child;
      }
      if (b == WithIncrementalBarrier) {
        JSString::writeBarrierPre(str->d.s.u2.left);
        JSString::writeBarrierPre(str->d.s.u3.right);
      }
      str->setNonInlineChars(wholeChars);
      uint32_t left_len = left.length();
      pos = wholeChars + left_len;

      // Remove memory association for left node we're about to make into a
      // dependent string.
      if (left.isTenured()) {
        RemoveCellMemory(&left, left.allocSize(), MemoryUse::StringContents);
      }

      if (IsSame<CharT, char16_t>::value) {
        left.setLengthAndFlags(left_len, DEPENDENT_FLAGS);
      } else {
        left.setLengthAndFlags(left_len, DEPENDENT_FLAGS | LATIN1_CHARS_BIT);
      }
      left.d.s.u3.base = (JSLinearString*)this; /* will be true on exit */
      goto visit_right_child;
    }
  }

  if (!AllocChars(this, wholeLength, &wholeChars, &wholeCapacity)) {
    if (maybecx) {
      ReportOutOfMemory(maybecx);
    }
    return nullptr;
  }

  if (!isTenured()) {
    Nursery& nursery = runtimeFromMainThread()->gc.nursery();
    if (!nursery.registerMallocedBuffer(wholeChars)) {
      js_free(wholeChars);
      if (maybecx) {
        ReportOutOfMemory(maybecx);
      }
      return nullptr;
    }
  }

  pos = wholeChars;
first_visit_node : {
  if (b == WithIncrementalBarrier) {
    JSString::writeBarrierPre(str->d.s.u2.left);
    JSString::writeBarrierPre(str->d.s.u3.right);
  }

  JSString& left = *str->d.s.u2.left;
  str->setNonInlineChars(pos);
  if (left.isRope()) {
    /* Return to this node when 'left' done, then goto visit_right_child. */
    left.setFlattenData(uintptr_t(str) | Tag_VisitRightChild);
    str = &left;
    goto first_visit_node;
  }
  CopyChars(pos, left.asLinear());
  pos += left.length();
}
visit_right_child : {
  JSString& right = *str->d.s.u3.right;
  if (right.isRope()) {
    /* Return to this node when 'right' done, then goto finish_node. */
    right.setFlattenData(uintptr_t(str) | Tag_FinishNode);
    str = &right;
    goto first_visit_node;
  }
  CopyChars(pos, right.asLinear());
  pos += right.length();
}

finish_node : {
  if (str == this) {
    MOZ_ASSERT(pos == wholeChars + wholeLength);
    *pos = '\0';
    if (IsSame<CharT, char16_t>::value) {
      str->setLengthAndFlags(wholeLength, EXTENSIBLE_FLAGS);
    } else {
      str->setLengthAndFlags(wholeLength, EXTENSIBLE_FLAGS | LATIN1_CHARS_BIT);
    }
    str->setNonInlineChars(wholeChars);
    str->d.s.u3.capacity = wholeCapacity;

    if (str->isTenured()) {
      AddCellMemory(str, str->asFlat().allocSize(), MemoryUse::StringContents);
    }

    return &this->asFlat();
  }
  uintptr_t flattenData;
  uint32_t len = pos - str->nonInlineCharsRaw<CharT>();
  if (IsSame<CharT, char16_t>::value) {
    flattenData = str->unsetFlattenData(len, DEPENDENT_FLAGS);
  } else {
    flattenData =
        str->unsetFlattenData(len, DEPENDENT_FLAGS | LATIN1_CHARS_BIT);
  }
  str->d.s.u3.base = (JSLinearString*)this; /* will be true on exit */

  // Every interior (rope) node in the rope's tree will be visited during
  // the traversal and post-barriered here, so earlier additions of
  // dependent.base -> root pointers are handled by this barrier as well.
  //
  // The only time post-barriers need do anything is when the root is in
  // the nursery. Note that the root was a rope but will be an extensible
  // string when we return, so it will not point to any strings and need
  // not be barriered.
  gc::StoreBuffer* bufferIfNursery = storeBuffer();
  if (bufferIfNursery && str->isTenured()) {
    bufferIfNursery->putWholeCell(str);
  }

  str = (JSString*)(flattenData & ~Tag_Mask);
  if ((flattenData & Tag_Mask) == Tag_VisitRightChild) {
    goto visit_right_child;
  }
  MOZ_ASSERT((flattenData & Tag_Mask) == Tag_FinishNode);
  goto finish_node;
}
}

template <JSRope::UsingBarrier b>
JSFlatString* JSRope::flattenInternal(JSContext* maybecx) {
  if (hasTwoByteChars()) {
    return flattenInternal<b, char16_t>(maybecx);
  }
  return flattenInternal<b, Latin1Char>(maybecx);
}

JSFlatString* JSRope::flatten(JSContext* maybecx) {
  mozilla::Maybe<AutoGeckoProfilerEntry> entry;
  if (maybecx && !maybecx->isHelperThreadContext()) {
    entry.emplace(maybecx, "JSRope::flatten");
  }

  if (zone()->needsIncrementalBarrier()) {
    return flattenInternal<WithIncrementalBarrier>(maybecx);
  }
  return flattenInternal<NoBarrier>(maybecx);
}

template <AllowGC allowGC>
static JSLinearString* EnsureLinear(
    JSContext* cx,
    typename MaybeRooted<JSString*, allowGC>::HandleType string) {
  JSLinearString* linear = string->ensureLinear(cx);
  // Don't report an exception if GC is not allowed, just return nullptr.
  if (!linear && !allowGC) {
    cx->recoverFromOutOfMemory();
  }
  return linear;
}

template <AllowGC allowGC>
JSString* js::ConcatStrings(
    JSContext* cx, typename MaybeRooted<JSString*, allowGC>::HandleType left,
    typename MaybeRooted<JSString*, allowGC>::HandleType right) {
  MOZ_ASSERT_IF(!left->isAtom(), cx->isInsideCurrentZone(left));
  MOZ_ASSERT_IF(!right->isAtom(), cx->isInsideCurrentZone(right));

  size_t leftLen = left->length();
  if (leftLen == 0) {
    return right;
  }

  size_t rightLen = right->length();
  if (rightLen == 0) {
    return left;
  }

  size_t wholeLength = leftLen + rightLen;
  if (MOZ_UNLIKELY(wholeLength > JSString::MAX_LENGTH)) {
    // Don't report an exception if GC is not allowed, just return nullptr.
    if (allowGC) {
      js::ReportAllocationOverflow(cx);
    }
    return nullptr;
  }

  bool isLatin1 = left->hasLatin1Chars() && right->hasLatin1Chars();
  bool canUseInline = isLatin1
                          ? JSInlineString::lengthFits<Latin1Char>(wholeLength)
                          : JSInlineString::lengthFits<char16_t>(wholeLength);
  if (canUseInline) {
    Latin1Char* latin1Buf = nullptr;  // initialize to silence GCC warning
    char16_t* twoByteBuf = nullptr;   // initialize to silence GCC warning
    JSInlineString* str =
        isLatin1 ? AllocateInlineString<allowGC>(cx, wholeLength, &latin1Buf)
                 : AllocateInlineString<allowGC>(cx, wholeLength, &twoByteBuf);
    if (!str) {
      return nullptr;
    }

    AutoCheckCannotGC nogc;
    JSLinearString* leftLinear = EnsureLinear<allowGC>(cx, left);
    if (!leftLinear) {
      return nullptr;
    }
    JSLinearString* rightLinear = EnsureLinear<allowGC>(cx, right);
    if (!rightLinear) {
      return nullptr;
    }

    if (isLatin1) {
      PodCopy(latin1Buf, leftLinear->latin1Chars(nogc), leftLen);
      PodCopy(latin1Buf + leftLen, rightLinear->latin1Chars(nogc), rightLen);
      latin1Buf[wholeLength] = 0;
    } else {
      if (leftLinear->hasTwoByteChars()) {
        PodCopy(twoByteBuf, leftLinear->twoByteChars(nogc), leftLen);
      } else {
        CopyAndInflateChars(twoByteBuf, leftLinear->latin1Chars(nogc), leftLen);
      }
      if (rightLinear->hasTwoByteChars()) {
        PodCopy(twoByteBuf + leftLen, rightLinear->twoByteChars(nogc),
                rightLen);
      } else {
        CopyAndInflateChars(twoByteBuf + leftLen,
                            rightLinear->latin1Chars(nogc), rightLen);
      }
      twoByteBuf[wholeLength] = 0;
    }

    return str;
  }

  return JSRope::new_<allowGC>(cx, left, right, wholeLength);
}

template JSString* js::ConcatStrings<CanGC>(JSContext* cx, HandleString left,
                                            HandleString right);

template JSString* js::ConcatStrings<NoGC>(JSContext* cx, JSString* const& left,
                                           JSString* const& right);

/**
 * Copy |src[0..length]| to |dest[0..length]| when copying doesn't narrow and
 * therefore can't lose information.
 */
template <typename Dest, typename Source>
static void FillAndTerminate(Dest* dest, Source src, size_t length) {
  static_assert(sizeof(src[length]) <= sizeof(dest[length]),
                "source → destination conversion must not narrow");

  for (size_t i = 0; i < length; i++) {
    auto srcChar = src[i];
    static_assert(std::is_unsigned<decltype(srcChar)>::value &&
                      std::is_unsigned<Dest>::value,
                  "source/destination characters are unsigned for simplicity");
    *dest++ = srcChar;
  }

  *dest = '\0';
}

template <typename Dest, typename Src,
          typename = typename std::enable_if<!std::is_const<Src>::value>::type>
static void FillAndTerminate(Dest* dest, Src* src, size_t length) {
  FillAndTerminate(dest, const_cast<const Src*>(src), length);
}

/**
 * Copy |src[0..length]| to |dest[0..length]| when copying *does* narrow, but
 * the user guarantees every runtime |src[i]| value can be stored without change
 * of value in |dest[i]|.
 */
template <typename Dest, typename Source>
static void FillFromCompatibleAndTerminate(Dest* dest, Source src,
                                           size_t length) {
  static_assert(sizeof(src[length]) > sizeof(dest[length]),
                "source → destination conversion must be narrowing");

  for (size_t i = 0; i < length; i++) {
    auto srcChar = src[i];
    static_assert(std::is_unsigned<decltype(srcChar)>::value &&
                      std::is_unsigned<Dest>::value,
                  "source/destination characters are unsigned for simplicity");
    *dest++ = AssertedCast<Dest>(src[i]);
  }

  *dest = '\0';
}

template <typename Dest, typename Src,
          typename = typename std::enable_if<!std::is_const<Src>::value>::type>
static void FillFromCompatibleAndTerminate(Dest* dest, Src* src,
                                           size_t length) {
  FillFromCompatibleAndTerminate(dest, const_cast<const Src*>(src), length);
}

template <typename CharT>
JSFlatString* JSDependentString::undependInternal(JSContext* cx) {
  size_t n = length();
  auto s = cx->make_pod_array<CharT>(n + 1, js::StringBufferArena);
  if (!s) {
    return nullptr;
  }

  if (!isTenured()) {
    if (!cx->runtime()->gc.nursery().registerMallocedBuffer(s.get())) {
      ReportOutOfMemory(cx);
      return nullptr;
    }
  } else {
    AddCellMemory(this, (n + 1) * sizeof(CharT), MemoryUse::StringContents);
  }

  AutoCheckCannotGC nogc;
  FillAndTerminate(s.get(), nonInlineChars<CharT>(nogc), n);
  setNonInlineChars<CharT>(s.release());

  /*
   * Transform *this into an undepended string so 'base' will remain rooted
   * for the benefit of any other dependent string that depends on *this.
   */
  if (IsSame<CharT, Latin1Char>::value) {
    setLengthAndFlags(n, UNDEPENDED_FLAGS | LATIN1_CHARS_BIT);
  } else {
    setLengthAndFlags(n, UNDEPENDED_FLAGS);
  }

  return &this->asFlat();
}

JSFlatString* JSDependentString::undepend(JSContext* cx) {
  MOZ_ASSERT(JSString::isDependent());
  return hasLatin1Chars() ? undependInternal<Latin1Char>(cx)
                          : undependInternal<char16_t>(cx);
}

#if defined(DEBUG) || defined(JS_JITSPEW)
void JSDependentString::dumpRepresentation(js::GenericPrinter& out,
                                           int indent) const {
  dumpRepresentationHeader(out, "JSDependentString");
  indent += 2;

  if (mozilla::Maybe<size_t> offset = baseOffset()) {
    out.printf("%*soffset: %zu\n", indent, "", *offset);
  }

  out.printf("%*sbase: ", indent, "");
  base()->dumpRepresentation(out, indent);
}
#endif

bool js::EqualChars(JSLinearString* str1, JSLinearString* str2) {
  MOZ_ASSERT(str1->length() == str2->length());

  size_t len = str1->length();

  AutoCheckCannotGC nogc;
  if (str1->hasTwoByteChars()) {
    if (str2->hasTwoByteChars()) {
      return ArrayEqual(str1->twoByteChars(nogc), str2->twoByteChars(nogc),
                        len);
    }

    return EqualChars(str2->latin1Chars(nogc), str1->twoByteChars(nogc), len);
  }

  if (str2->hasLatin1Chars()) {
    return ArrayEqual(str1->latin1Chars(nogc), str2->latin1Chars(nogc), len);
  }

  return EqualChars(str1->latin1Chars(nogc), str2->twoByteChars(nogc), len);
}

bool js::HasSubstringAt(JSLinearString* text, JSLinearString* pat,
                        size_t start) {
  MOZ_ASSERT(start + pat->length() <= text->length());

  size_t patLen = pat->length();

  AutoCheckCannotGC nogc;
  if (text->hasLatin1Chars()) {
    const Latin1Char* textChars = text->latin1Chars(nogc) + start;
    if (pat->hasLatin1Chars()) {
      return ArrayEqual(textChars, pat->latin1Chars(nogc), patLen);
    }

    return EqualChars(textChars, pat->twoByteChars(nogc), patLen);
  }

  const char16_t* textChars = text->twoByteChars(nogc) + start;
  if (pat->hasTwoByteChars()) {
    return ArrayEqual(textChars, pat->twoByteChars(nogc), patLen);
  }

  return EqualChars(pat->latin1Chars(nogc), textChars, patLen);
}

bool js::EqualStrings(JSContext* cx, JSString* str1, JSString* str2,
                      bool* result) {
  if (str1 == str2) {
    *result = true;
    return true;
  }

  size_t length1 = str1->length();
  if (length1 != str2->length()) {
    *result = false;
    return true;
  }

  JSLinearString* linear1 = str1->ensureLinear(cx);
  if (!linear1) {
    return false;
  }
  JSLinearString* linear2 = str2->ensureLinear(cx);
  if (!linear2) {
    return false;
  }

  *result = EqualChars(linear1, linear2);
  return true;
}

bool js::EqualStrings(JSLinearString* str1, JSLinearString* str2) {
  if (str1 == str2) {
    return true;
  }

  size_t length1 = str1->length();
  if (length1 != str2->length()) {
    return false;
  }

  return EqualChars(str1, str2);
}

int32_t js::CompareChars(const char16_t* s1, size_t len1, JSLinearString* s2) {
  AutoCheckCannotGC nogc;
  return s2->hasLatin1Chars()
             ? CompareChars(s1, len1, s2->latin1Chars(nogc), s2->length())
             : CompareChars(s1, len1, s2->twoByteChars(nogc), s2->length());
}

static int32_t CompareStringsImpl(JSLinearString* str1, JSLinearString* str2) {
  size_t len1 = str1->length();
  size_t len2 = str2->length();

  AutoCheckCannotGC nogc;
  if (str1->hasLatin1Chars()) {
    const Latin1Char* chars1 = str1->latin1Chars(nogc);
    return str2->hasLatin1Chars()
               ? CompareChars(chars1, len1, str2->latin1Chars(nogc), len2)
               : CompareChars(chars1, len1, str2->twoByteChars(nogc), len2);
  }

  const char16_t* chars1 = str1->twoByteChars(nogc);
  return str2->hasLatin1Chars()
             ? CompareChars(chars1, len1, str2->latin1Chars(nogc), len2)
             : CompareChars(chars1, len1, str2->twoByteChars(nogc), len2);
}

bool js::CompareStrings(JSContext* cx, JSString* str1, JSString* str2,
                        int32_t* result) {
  MOZ_ASSERT(str1);
  MOZ_ASSERT(str2);

  if (str1 == str2) {
    *result = 0;
    return true;
  }

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

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

  *result = CompareStringsImpl(linear1, linear2);
  return true;
}

int32_t js::CompareAtoms(JSAtom* atom1, JSAtom* atom2) {
  return CompareStringsImpl(atom1, atom2);
}

bool js::StringIsAscii(JSLinearString* str) {
  auto containsOnlyAsciiCharacters = [](const auto* chars, size_t length) {
    return std::all_of(chars, chars + length,
                       [](auto c) { return mozilla::IsAscii(c); });
  };

  JS::AutoCheckCannotGC nogc;
  return str->hasLatin1Chars() ? containsOnlyAsciiCharacters(
                                     str->latin1Chars(nogc), str->length())
                               : containsOnlyAsciiCharacters(
                                     str->twoByteChars(nogc), str->length());
}

bool js::StringEqualsAscii(JSLinearString* str, const char* asciiBytes) {
  MOZ_ASSERT(JS::StringIsASCII(asciiBytes));

  size_t length = strlen(asciiBytes);
  if (length != str->length()) {
    return false;
  }

  const Latin1Char* latin1 = reinterpret_cast<const Latin1Char*>(asciiBytes);

  AutoCheckCannotGC nogc;
  return str->hasLatin1Chars()
             ? ArrayEqual(latin1, str->latin1Chars(nogc), length)
             : EqualChars(latin1, str->twoByteChars(nogc), length);
}

template <typename CharT>
/* static */
bool JSFlatString::isIndexSlow(const CharT* s, size_t length,
                               uint32_t* indexp) {
  CharT ch = *s;

  if (!IsAsciiDigit(ch)) {
    return false;
  }

  if (length > UINT32_CHAR_BUFFER_LENGTH) {
    return false;
  }

  /*
   * Make sure to account for the '\0' at the end of characters, dereferenced
   * in the loop below.
   */
  RangedPtr<const CharT> cp(s, length + 1);
  const RangedPtr<const CharT> end(s + length, s, length + 1);

  uint32_t index = AsciiDigitToNumber(*cp++);
  uint32_t oldIndex = 0;
  uint32_t c = 0;

  if (index != 0) {
    while (IsAsciiDigit(*cp)) {
      oldIndex = index;
      c = AsciiDigitToNumber(*cp);
      index = 10 * index + c;
      cp++;
    }
  }

  /* It's not an element if there are characters after the number. */
  if (cp != end) {
    return false;
  }

  /*
   * Look out for "4294967296" and larger-number strings that fit in
   * UINT32_CHAR_BUFFER_LENGTH: only unsigned 32-bit integers shall pass.
   */
  if (oldIndex < UINT32_MAX / 10 ||
      (oldIndex == UINT32_MAX / 10 && c <= (UINT32_MAX % 10))) {
    *indexp = index;
    return true;
  }

  return false;
}

template bool JSFlatString::isIndexSlow(const Latin1Char* s, size_t length,
                                        uint32_t* indexp);

template bool JSFlatString::isIndexSlow(const char16_t* s, size_t length,
                                        uint32_t* indexp);

/*
 * Set up some tools to make it easier to generate large tables. After constant
 * folding, for each n, Rn(0) is the comma-separated list R(0), R(1), ...,
 * R(2^n-1). Similary, Rn(k) (for any k and n) generates the list R(k), R(k+1),
 * ..., R(k+2^n-1). To use this, define R appropriately, then use Rn(0) (for
 * some value of n), then undefine R.
 */
#define R2(n) R(n), R((n) + (1 << 0)), R((n) + (2 << 0)), R((n) + (3 << 0))
#define R4(n) R2(n), R2((n) + (1 << 2)), R2((n) + (2 << 2)), R2((n) + (3 << 2))
#define R6(n) R4(n), R4((n) + (1 << 4)), R4((n) + (2 << 4)), R4((n) + (3 << 4))
#define R7(n) R6(n), R6((n) + (1 << 6))

/*
 * This is used when we generate our table of short strings, so the compiler is
 * happier if we use |c| as few times as possible.
 */
// clang-format off
#define FROM_SMALL_CHAR(c) Latin1Char((c) + ((c) < 10 ? '0' :      \
                                             (c) < 36 ? 'a' - 10 : \
                                             'A' - 36))
// clang-format on

/*
 * Declare length-2 strings. We only store strings where both characters are
 * alphanumeric. The lower 10 short chars are the numerals, the next 26 are
 * the lowercase letters, and the next 26 are the uppercase letters.
 */
// clang-format off
#define TO_SMALL_CHAR(c) ((c) >= '0' && (c) <= '9' ? (c) - '0' :              \
                          (c) >= 'a' && (c) <= 'z' ? (c) - 'a' + 10 :         \
                          (c) >= 'A' && (c) <= 'Z' ? (c) - 'A' + 36 :         \
                          StaticStrings::INVALID_SMALL_CHAR)
// clang-format on

#define R TO_SMALL_CHAR
const StaticStrings::SmallChar StaticStrings::toSmallChar[] = {R7(0)};
#undef R

#undef R2
#undef R4
#undef R6
#undef R7

bool StaticStrings::init(JSContext* cx) {
  AutoAllocInAtomsZone az(cx);

  static_assert(UNIT_STATIC_LIMIT - 1 <= JSString::MAX_LATIN1_CHAR,
                "Unit strings must fit in Latin1Char.");

  using Latin1Range = mozilla::Range<const Latin1Char>;

  for (uint32_t i = 0; i < UNIT_STATIC_LIMIT; i++) {
    Latin1Char buffer[] = {Latin1Char(i), '\0'};
    JSFlatString* s = NewInlineString<NoGC>(cx, Latin1Range(buffer, 1));
    if (!s) {
      return false;
    }
    HashNumber hash = mozilla::HashString(buffer, 1);
    unitStaticTable[i] = s->morphAtomizedStringIntoPermanentAtom(hash);
  }

  for (uint32_t i = 0; i < NUM_SMALL_CHARS * NUM_SMALL_CHARS; i++) {
    Latin1Char buffer[] = {FROM_SMALL_CHAR(i >> 6), FROM_SMALL_CHAR(i & 0x3F),
                           '\0'};
    JSFlatString* s = NewInlineString<NoGC>(cx, Latin1Range(buffer, 2));
    if (!s) {
      return false;
    }
    HashNumber hash = mozilla::HashString(buffer, 2);
    length2StaticTable[i] = s->morphAtomizedStringIntoPermanentAtom(hash);
  }

  for (uint32_t i = 0; i < INT_STATIC_LIMIT; i++) {
    if (i < 10) {
      intStaticTable[i] = unitStaticTable[i + '0'];
    } else if (i < 100) {
      size_t index = ((size_t)TO_SMALL_CHAR((i / 10) + '0') << 6) +
                     TO_SMALL_CHAR((i % 10) + '0');
      intStaticTable[i] = length2StaticTable[index];
    } else {
      Latin1Char buffer[] = {Latin1Char('0' + (i / 100)),
                             Latin1Char('0' + ((i / 10) % 10)),
                             Latin1Char('0' + (i % 10)), '\0'};
      JSFlatString* s = NewInlineString<NoGC>(cx, Latin1Range(buffer, 3));
      if (!s) {
        return false;
      }
      HashNumber hash = mozilla::HashString(buffer, 3);
      intStaticTable[i] = s->morphAtomizedStringIntoPermanentAtom(hash);
    }

    // Static string initialization can not race, so allow even without the
    // lock.
    intStaticTable[i]->maybeInitializeIndex(i, true);
  }

  return true;
}

inline void TraceStaticString(JSTracer* trc, JSAtom* atom, const char* name) {
  MOZ_ASSERT(atom->isPinned());
  TraceProcessGlobalRoot(trc, atom, name);
}

void StaticStrings::trace(JSTracer* trc) {
  /* These strings never change, so barriers are not needed. */

  for (uint32_t i = 0; i < UNIT_STATIC_LIMIT; i++) {
    TraceStaticString(trc, unitStaticTable[i], "unit-static-string");
  }

  for (uint32_t i = 0; i < NUM_SMALL_CHARS * NUM_SMALL_CHARS; i++) {
    TraceStaticString(trc, length2StaticTable[i], "length2-static-string");
  }

  /* This may mark some strings more than once, but so be it. */
  for (uint32_t i = 0; i < INT_STATIC_LIMIT; i++) {
    TraceStaticString(trc, intStaticTable[i], "int-static-string");
  }
}

template <typename CharT>
/* static */
bool StaticStrings::isStatic(const CharT* chars, size_t length) {
  switch (length) {
    case 1: {
      char16_t c = chars[0];
      return c < UNIT_STATIC_LIMIT;
    }
    case 2:
      return fitsInSmallChar(chars[0]) && fitsInSmallChar(chars[1]);
    case 3:
      if ('1' <= chars[0] && chars[0] <= '9' && '0' <= chars[1] &&
          chars[1] <= '9' && '0' <= chars[2] && chars[2] <= '9') {
        int i =
            (chars[0] - '0') * 100 + (chars[1] - '0') * 10 + (chars[2] - '0');

        return unsigned(i) < INT_STATIC_LIMIT;
      }
      return false;
    default:
      return false;
  }
}

/* static */
bool StaticStrings::isStatic(JSAtom* atom) {
  AutoCheckCannotGC nogc;
  return atom->hasLatin1Chars()
             ? isStatic(atom->latin1Chars(nogc), atom->length())
             : isStatic(atom->twoByteChars(nogc), atom->length());
}

bool AutoStableStringChars::init(JSContext* cx, JSString* s) {
  RootedLinearString linearString(cx, s->ensureLinear(cx));
  if (!linearString) {
    return false;
  }

  MOZ_ASSERT(state_ == Uninitialized);

  if (linearString->isExternal() && !linearString->ensureFlat(cx)) {
    return false;
  }

  // If the chars are inline then we need to copy them since they may be moved
  // by a compacting GC.
  if (baseIsInline(linearString)) {
    return linearString->hasTwoByteChars() ? copyTwoByteChars(cx, linearString)
                                           : copyLatin1Chars(cx, linearString);
  }

  if (linearString->hasLatin1Chars()) {
    state_ = Latin1;
    latin1Chars_ = linearString->rawLatin1Chars();
  } else {
    state_ = TwoByte;
    twoByteChars_ = linearString->rawTwoByteChars();
  }

  s_ = linearString;
  return true;
}

bool AutoStableStringChars::initTwoByte(JSContext* cx, JSString* s) {
  RootedLinearString linearString(cx, s->ensureLinear(cx));
  if (!linearString) {
    return false;
  }

  MOZ_ASSERT(state_ == Uninitialized);

  if (linearString->hasLatin1Chars()) {
    return copyAndInflateLatin1Chars(cx, linearString);
  }

  if (linearString->isExternal() && !linearString->ensureFlat(cx)) {
    return false;
  }

  // If the chars are inline then we need to copy them since they may be moved
  // by a compacting GC.
  if (baseIsInline(linearString)) {
    return copyTwoByteChars(cx, linearString);
  }

  state_ = TwoByte;
  twoByteChars_ = linearString->rawTwoByteChars();
  s_ = linearString;
  return true;
}

bool AutoStableStringChars::baseIsInline(HandleLinearString linearString) {
  JSString* base = linearString;
  while (base->isDependent()) {
    base = base->asDependent().base();
  }
  return base->isInline();
}

template <typename T>
T* AutoStableStringChars::allocOwnChars(JSContext* cx, size_t count) {
  static_assert(
      InlineCapacity >= sizeof(JS::Latin1Char) *
                            (JSFatInlineString::MAX_LENGTH_LATIN1 + 1) &&
          InlineCapacity >=
              sizeof(char16_t) * (JSFatInlineString::MAX_LENGTH_TWO_BYTE + 1),
      "InlineCapacity too small to hold fat inline strings");

  static_assert((JSString::MAX_LENGTH &
                 mozilla::tl::MulOverflowMask<sizeof(T)>::value) == 0,
                "Size calculation can overflow");
  MOZ_ASSERT(count <= (JSString::MAX_LENGTH + 1));
  size_t size = sizeof(T) * count;

  ownChars_.emplace(cx);
  if (!ownChars_->resize(size)) {
    ownChars_.reset();
    return nullptr;
  }

  return reinterpret_cast<T*>(ownChars_->begin());
}

bool AutoStableStringChars::copyAndInflateLatin1Chars(
    JSContext* cx, HandleLinearString linearString) {
  char16_t* chars = allocOwnChars<char16_t>(cx, linearString->length() + 1);
  if (!chars) {
    return false;
  }

  FillAndTerminate(chars, linearString->rawLatin1Chars(),
                   linearString->length());

  state_ = TwoByte;
  twoByteChars_ = chars;
  s_ = linearString;
  return true;
}

bool AutoStableStringChars::copyLatin1Chars(JSContext* cx,
                                            HandleLinearString linearString) {
  size_t length = linearString->length();
  JS::Latin1Char* chars = allocOwnChars<JS::Latin1Char>(cx, length + 1);
  if (!chars) {
    return false;
  }

  FillAndTerminate(chars, linearString->rawLatin1Chars(), length);

  state_ = Latin1;
  latin1Chars_ = chars;
  s_ = linearString;
  return true;
}

bool AutoStableStringChars::copyTwoByteChars(JSContext* cx,
                                             HandleLinearString linearString) {
  size_t length = linearString->length();
  char16_t* chars = allocOwnChars<char16_t>(cx, length + 1);
  if (!chars) {
    return false;
  }

  FillAndTerminate(chars, linearString->rawTwoByteChars(), length);

  state_ = TwoByte;
  twoByteChars_ = chars;
  s_ = linearString;
  return true;
}

JSFlatString* JSString::ensureFlat(JSContext* cx) {
  if (isFlat()) {
    return &asFlat();
  }
  if (isDependent()) {
    return asDependent().undepend(cx);
  }
  if (isRope()) {
    return asRope().flatten(cx);
  }
  return asExternal().ensureFlat(cx);
}

JSFlatString* JSExternalString::ensureFlat(JSContext* cx) {
  MOZ_ASSERT(hasTwoByteChars());

  size_t n = length();
  auto s = cx->make_pod_array<char16_t>(n + 1, js::StringBufferArena);
  if (!s) {
    return nullptr;
  }

  // Copy the chars before finalizing the string.
  {
    AutoCheckCannotGC nogc;
    FillAndTerminate(s.get(), nonInlineChars<char16_t>(nogc), n);
  }

  // Release the external chars.
  finalize(cx->runtime()->defaultFreeOp());

  MOZ_ASSERT(isTenured());
  AddCellMemory(this, (n + 1) * sizeof(char16_t), MemoryUse::StringContents);

  // Transform the string into a non-external, flat string. Note that the
  // resulting string will still be in an AllocKind::EXTERNAL_STRING arena,
  // but will no longer be an external string.
  setLengthAndFlags(n, INIT_FLAT_FLAGS);
  setNonInlineChars<char16_t>(s.release());

  return &this->asFlat();
}

#if defined(DEBUG) || defined(JS_JITSPEW)
void JSAtom::dump(js::GenericPrinter& out) {
  out.printf("JSAtom* (%p) = ", (void*)this);
  this->JSString::dump(out);
}

void JSAtom::dump() {
  Fprinter out(stderr);
  dump(out);
}

void JSExternalString::dumpRepresentation(js::GenericPrinter& out,
                                          int indent) const {
  dumpRepresentationHeader(out, "JSExternalString");
  indent += 2;

  out.printf("%*sfinalizer: ((JSStringFinalizer*) %p)\n", indent, "",
             externalFinalizer());
  dumpRepresentationChars(out, indent);
}
#endif /* defined(DEBUG) || defined(JS_JITSPEW) */

JSLinearString* js::NewDependentString(JSContext* cx, JSString* baseArg,
                                       size_t start, size_t length) {
  if (length == 0) {
    return cx->emptyString();
  }

  JSLinearString* base = baseArg->ensureLinear(cx);
  if (!base) {
    return nullptr;
  }

  if (start == 0 && length == base->length()) {
    return base;
  }

  if (base->hasTwoByteChars()) {
    AutoCheckCannotGC nogc;
    const char16_t* chars = base->twoByteChars(nogc) + start;
    if (JSLinearString* staticStr = cx->staticStrings().lookup(chars, length)) {
      return staticStr;
    }
  } else {
    AutoCheckCannotGC nogc;
    const Latin1Char* chars = base->latin1Chars(nogc) + start;
    if (JSLinearString* staticStr = cx->staticStrings().lookup(chars, length)) {
      return staticStr;
    }
  }

  return JSDependentString::new_(cx, base, start, length);
}

static bool CanStoreCharsAsLatin1(const char16_t* s, size_t length) {
  for (const char16_t* end = s + length; s < end; ++s) {
    if (*s > JSString::MAX_LATIN1_CHAR) {
      return false;
    }
  }

  return true;
}

static bool CanStoreCharsAsLatin1(const Latin1Char* s, size_t length) {
  MOZ_CRASH("Shouldn't be called for Latin1 chars");
}

static bool CanStoreCharsAsLatin1(LittleEndianChars chars, size_t length) {
  for (size_t i = 0; i < length; i++) {
    if (chars[i] > JSString::MAX_LATIN1_CHAR) {
      return false;
    }
  }

  return true;
}

template <AllowGC allowGC>
static MOZ_ALWAYS_INLINE JSInlineString* NewInlineStringDeflated(
    JSContext* cx, mozilla::Range<const char16_t> chars) {
  size_t len = chars.length();
  Latin1Char* storage;
  JSInlineString* str = AllocateInlineString<allowGC>(cx, len, &storage);
  if (!str) {
    return nullptr;
  }

  MOZ_ASSERT(CanStoreCharsAsLatin1(chars.begin().get(), len));
  FillFromCompatibleAndTerminate(storage, chars, len);
  return str;
}

template <AllowGC allowGC>
static JSFlatString* NewStringDeflated(JSContext* cx, const char16_t* s,
                                       size_t n) {
  if (JSFlatString* str = TryEmptyOrStaticString(cx, s, n)) {
    return str;
  }

  if (JSInlineString::lengthFits<Latin1Char>(n)) {
    return NewInlineStringDeflated<allowGC>(
        cx, mozilla::Range<const char16_t>(s, n));
  }

  auto news = cx->make_pod_array<Latin1Char>(n + 1, js::StringBufferArena);
  if (!news) {
    if (!allowGC) {
      cx->recoverFromOutOfMemory();
    }
    return nullptr;
  }

  MOZ_ASSERT(CanStoreCharsAsLatin1(s, n));
  FillFromCompatibleAndTerminate(news.get(), s, n);

  return JSFlatString::new_<allowGC>(cx, std::move(news), n);
}

template <AllowGC allowGC>
static JSFlatString* NewStringDeflated(JSContext* cx, const Latin1Char* s,
                                       size_t n) {
  MOZ_CRASH("Shouldn't be called for Latin1 chars");
}

static JSFlatString* NewStringDeflatedFromLittleEndianNoGC(
    JSContext* cx, LittleEndianChars chars, size_t length) {
  MOZ_ASSERT(CanStoreCharsAsLatin1(chars, length));

  if (JSInlineString::lengthFits<Latin1Char>(length)) {
    Latin1Char* storage;
    JSInlineString* str = AllocateInlineString<NoGC>(cx, length, &storage);
    if (!str) {
      return nullptr;
    }

    FillFromCompatibleAndTerminate(storage, chars, length);
    return str;
  }

  auto news = cx->make_pod_array<Latin1Char>(length + 1, js::StringBufferArena);
  if (!news) {
    cx->recoverFromOutOfMemory();
    return nullptr;
  }

  FillFromCompatibleAndTerminate(news.get(), chars, length);

  return JSFlatString::new_<NoGC>(cx, std::move(news), length);
}

template <typename CharT>
JSFlatString* js::NewStringDontDeflate(JSContext* cx,
                                       UniquePtr<CharT[], JS::FreePolicy> chars,
                                       size_t length) {
  if (JSFlatString* str = TryEmptyOrStaticString(cx, chars.get(), length)) {
    return str;
  }

  if (JSInlineString::lengthFits<CharT>(length)) {
    // |chars.get()| is safe because 1) |NewInlineString| necessarily *copies*,
    // and 2) |chars| frees its contents only when this function returns.
    return NewInlineString<CanGC>(
        cx, mozilla::Range<const CharT>(chars.get(), length));
  }

  return JSFlatString::new_<CanGC>(cx, std::move(chars), length);
}

template JSFlatString* js::NewStringDontDeflate(JSContext* cx,
                                                UniqueTwoByteChars chars,
                                                size_t length);

template JSFlatString* js::NewStringDontDeflate(JSContext* cx,
                                                UniqueLatin1Chars chars,
                                                size_t length);

template <typename CharT>
JSFlatString* js::NewString(JSContext* cx,
                            UniquePtr<CharT[], JS::FreePolicy> chars,
                            size_t length) {
  if (IsSame<CharT, char16_t>::value &&
      CanStoreCharsAsLatin1(chars.get(), length)) {
    // Deflating copies from |chars.get()| and lets |chars| be freed on return.
    return NewStringDeflated<CanGC>(cx, chars.get(), length);
  }

  return NewStringDontDeflate(cx, std::move(chars), length);
}

template JSFlatString* js::NewString(JSContext* cx, UniqueTwoByteChars chars,
                                     size_t length);

template JSFlatString* js::NewString(JSContext* cx, UniqueLatin1Chars chars,
                                     size_t length);

template <AllowGC allowGC, typename CharT>
JSFlatString* js::NewStringDontDeflate(JSContext* cx,
                                       UniquePtr<CharT[], JS::FreePolicy> chars,
                                       size_t length) {
  if (JSFlatString* str = TryEmptyOrStaticString(cx, chars.get(), length)) {
    return str;
  }

  if (JSInlineString::lengthFits<CharT>(length)) {
    return NewInlineString<allowGC>(
        cx, mozilla::Range<const CharT>(chars.get(), length));
  }

  return JSFlatString::new_<allowGC>(cx, std::move(chars), length);
}

template JSFlatString* js::NewStringDontDeflate<CanGC>(JSContext* cx,
                                                       UniqueTwoByteChars chars,
                                                       size_t length);

template JSFlatString* js::NewStringDontDeflate<NoGC>(JSContext* cx,
                                                      UniqueTwoByteChars chars,
                                                      size_t length);

template JSFlatString* js::NewStringDontDeflate<CanGC>(JSContext* cx,
                                                       UniqueLatin1Chars chars,
                                                       size_t length);

template JSFlatString* js::NewStringDontDeflate<NoGC>(JSContext* cx,
                                                      UniqueLatin1Chars chars,
                                                      size_t length);

template <AllowGC allowGC, typename CharT>
JSFlatString* js::NewString(JSContext* cx,
                            UniquePtr<CharT[], JS::FreePolicy> chars,
                            size_t length) {
  if (IsSame<CharT, char16_t>::value &&
      CanStoreCharsAsLatin1(chars.get(), length)) {
    return NewStringDeflated<allowGC>(cx, chars.get(), length);
  }

  return NewStringDontDeflate<allowGC>(cx, std::move(chars), length);
}

template JSFlatString* js::NewString<CanGC>(JSContext* cx,
                                            UniqueTwoByteChars chars,
                                            size_t length);

template JSFlatString* js::NewString<NoGC>(JSContext* cx,
                                           UniqueTwoByteChars chars,
                                           size_t length);

template JSFlatString* js::NewString<CanGC>(JSContext* cx,
                                            UniqueLatin1Chars chars,
                                            size_t length);

template JSFlatString* js::NewString<NoGC>(JSContext* cx,
                                           UniqueLatin1Chars chars,
                                           size_t length);

namespace js {

template <AllowGC allowGC, typename CharT>
JSFlatString* NewStringCopyNDontDeflate(JSContext* cx, const CharT* s,
                                        size_t n) {
  if (JSFlatString* str = TryEmptyOrStaticString(cx, s, n)) {
    return str;
  }

  if (JSInlineString::lengthFits<CharT>(n)) {
    return NewInlineString<allowGC>(cx, mozilla::Range<const CharT>(s, n));
  }

  auto news = cx->make_pod_array<CharT>(n + 1, js::StringBufferArena);
  if (!news) {
    if (!allowGC) {
      cx->recoverFromOutOfMemory();
    }
    return nullptr;
  }

  FillAndTerminate(news.get(), s, n);

  return JSFlatString::new_<allowGC>(cx, std::move(news), n);
}

template JSFlatString* NewStringCopyNDontDeflate<CanGC>(JSContext* cx,
                                                        const char16_t* s,
                                                        size_t n);

template JSFlatString* NewStringCopyNDontDeflate<NoGC>(JSContext* cx,
                                                       const char16_t* s,
                                                       size_t n);

template JSFlatString* NewStringCopyNDontDeflate<CanGC>(JSContext* cx,
                                                        const Latin1Char* s,
                                                        size_t n);

template JSFlatString* NewStringCopyNDontDeflate<NoGC>(JSContext* cx,
                                                       const Latin1Char* s,
                                                       size_t n);

static JSFlatString* NewUndeflatedStringFromLittleEndianNoGC(
    JSContext* cx, LittleEndianChars chars, size_t length) {
  if (JSInlineString::lengthFits<char16_t>(length)) {
    char16_t* storage;
    JSInlineString* str = AllocateInlineString<NoGC>(cx, length, &storage);
    if (!str) {
      return nullptr;
    }

    FillAndTerminate(storage, chars, length);
    return str;
  }

  auto news = cx->make_pod_array<char16_t>(length + 1, js::StringBufferArena);
  if (!news) {
    cx->recoverFromOutOfMemory();
    return nullptr;
  }

  FillAndTerminate(news.get(), chars, length);

  return JSFlatString::new_<NoGC>(cx, std::move(news), length);
}

JSFlatString* NewLatin1StringZ(JSContext* cx, UniqueChars chars) {
  size_t length = strlen(chars.get());
  UniqueLatin1Chars latin1(reinterpret_cast<Latin1Char*>(chars.release()));
  return NewString<CanGC>(cx, std::move(latin1), length);
}

template <AllowGC allowGC, typename CharT>
JSFlatString* NewStringCopyN(JSContext* cx, const CharT* s, size_t n) {
  if (IsSame<CharT, char16_t>::value && CanStoreCharsAsLatin1(s, n)) {
    return NewStringDeflated<allowGC>(cx, s, n);
  }

  return NewStringCopyNDontDeflate<allowGC>(cx, s, n);
}

template JSFlatString* NewStringCopyN<CanGC>(JSContext* cx, const char16_t* s,
                                             size_t n);

template JSFlatString* NewStringCopyN<NoGC>(JSContext* cx, const char16_t* s,
                                            size_t n);

template JSFlatString* NewStringCopyN<CanGC>(JSContext* cx, const Latin1Char* s,
                                             size_t n);

template JSFlatString* NewStringCopyN<NoGC>(JSContext* cx, const Latin1Char* s,
                                            size_t n);

JSFlatString* NewStringFromLittleEndianNoGC(JSContext* cx,
                                            LittleEndianChars chars,
                                            size_t length) {
  if (JSFlatString* str = TryEmptyOrStaticString(cx, chars, length)) {
    return str;
  }

  if (CanStoreCharsAsLatin1(chars, length)) {
    return NewStringDeflatedFromLittleEndianNoGC(cx, chars, length);
  }

  return NewUndeflatedStringFromLittleEndianNoGC(cx, chars, length);
}

template <js::AllowGC allowGC>
JSFlatString* NewStringCopyUTF8N(JSContext* cx, const JS::UTF8Chars utf8) {
  JS::SmallestEncoding encoding = JS::FindSmallestEncoding(utf8);
  if (encoding == JS::SmallestEncoding::ASCII) {
    return NewStringCopyN<allowGC>(cx, utf8.begin().get(), utf8.length());
  }

  size_t length;
  if (encoding == JS::SmallestEncoding::Latin1) {
    UniqueLatin1Chars latin1(
        UTF8CharsToNewLatin1CharsZ(cx, utf8, &length, js::StringBufferArena)
            .get());
    if (!latin1) {
      return nullptr;
    }

    return NewString<allowGC>(cx, std::move(latin1), length);
  }

  MOZ_ASSERT(encoding == JS::SmallestEncoding::UTF16);

  UniqueTwoByteChars utf16(
      UTF8CharsToNewTwoByteCharsZ(cx, utf8, &length, js::StringBufferArena)
          .get());
  if (!utf16) {
    return nullptr;
  }

  return NewString<allowGC>(cx, std::move(utf16), length);
}

template JSFlatString* NewStringCopyUTF8N<CanGC>(JSContext* cx,
                                                 const JS::UTF8Chars utf8);

MOZ_ALWAYS_INLINE JSString* ExternalStringCache::lookup(const char16_t* chars,
                                                        size_t len) const {
  AutoCheckCannotGC nogc;

  for (size_t i = 0; i < NumEntries; i++) {
    JSString* str = entries_[i];
    if (!str || str->length() != len) {
      continue;
    }

    const char16_t* strChars = str->asLinear().nonInlineTwoByteChars(nogc);
    if (chars == strChars) {
      // Note that we don't need an incremental barrier here or below.
      // The cache is purged on GC so any string we get from the cache
      // must have been allocated after the GC started.
      return str;
    }

    // Compare the chars. Don't do this for long strings as it will be
    // faster to allocate a new external string.
    static const size_t MaxLengthForCharComparison = 100;
    if (len <= MaxLengthForCharComparison && ArrayEqual(chars, strChars, len)) {
      return str;
    }
  }

  return nullptr;
}

MOZ_ALWAYS_INLINE void ExternalStringCache::put(JSString* str) {
  MOZ_ASSERT(str->isExternal());

  for (size_t i = NumEntries - 1; i > 0; i--) {
    entries_[i] = entries_[i - 1];
  }

  entries_[0] = str;
}

JSString* NewMaybeExternalString(JSContext* cx, const char16_t* s, size_t n,
                                 const JSStringFinalizer* fin,
                                 bool* allocatedExternal) {
  if (JSString* str = TryEmptyOrStaticString(cx, s, n)) {
    *allocatedExternal = false;
    return str;
  }

  if (JSThinInlineString::lengthFits<Latin1Char>(n) &&
      CanStoreCharsAsLatin1(s, n)) {
    *allocatedExternal = false;
    return NewInlineStringDeflated<AllowGC::CanGC>(
        cx, mozilla::Range<const char16_t>(s, n));
  }

  ExternalStringCache& cache = cx->zone()->externalStringCache();
  if (JSString* str = cache.lookup(s, n)) {
    *allocatedExternal = false;
    return str;
  }

  JSString* str = JSExternalString::new_(cx, s, n, fin);
  if (!str) {
    return nullptr;
  }

  *allocatedExternal = true;
  cache.put(str);
  return str;
}

} /* namespace js */

#if defined(DEBUG) || defined(JS_JITSPEW)
void JSExtensibleString::dumpRepresentation(js::GenericPrinter& out,
                                            int indent) const {
  dumpRepresentationHeader(out, "JSExtensibleString");
  indent += 2;

  out.printf("%*scapacity: %zu\n", indent, "", capacity());
  dumpRepresentationChars(out, indent);
}

void JSInlineString::dumpRepresentation(js::GenericPrinter& out,
                                        int indent) const {
  dumpRepresentationHeader(
      out, isFatInline() ? "JSFatInlineString" : "JSThinInlineString");
  indent += 2;

  dumpRepresentationChars(out, indent);
}

void JSFlatString::dumpRepresentation(js::GenericPrinter& out,
                                      int indent) const {
  dumpRepresentationHeader(out, "JSFlatString");
  indent += 2;

  dumpRepresentationChars(out, indent);
}
#endif

static void FinalizeRepresentativeExternalString(const JSStringFinalizer* fin,
                                                 char16_t* chars);

static const JSStringFinalizer RepresentativeExternalStringFinalizer = {
    FinalizeRepresentativeExternalString};

static void FinalizeRepresentativeExternalString(const JSStringFinalizer* fin,
                                                 char16_t* chars) {
  // Constant chars, nothing to free.
  MOZ_ASSERT(fin == &RepresentativeExternalStringFinalizer);
}

template <typename CheckString, typename CharT>
static bool FillWithRepresentatives(JSContext* cx, HandleArrayObject array,
                                    uint32_t* index, const CharT* chars,
                                    size_t len, size_t fatInlineMaxLength,
                                    const CheckString& check) {
  auto AppendString = [&check](JSContext* cx, HandleArrayObject array,
                               uint32_t* index, HandleString s) {
    MOZ_ASSERT(check(s));
    Unused << check;  // silence clang -Wunused-lambda-capture in opt builds
    RootedValue val(cx, StringValue(s));
    return JS_DefineElement(cx, array, (*index)++, val, 0);
  };

  MOZ_ASSERT(len > fatInlineMaxLength);

  // Normal atom.
  RootedString atom1(cx, AtomizeChars(cx, chars, len));
  if (!atom1 || !AppendString(cx, array, index, atom1)) {
    return false;
  }
  MOZ_ASSERT(atom1->isAtom());

  // Inline atom.
  RootedString atom2(cx, AtomizeChars(cx, chars, 2));
  if (!atom2 || !AppendString(cx, array, index, atom2)) {
    return false;
  }
  MOZ_ASSERT(atom2->isAtom());
  MOZ_ASSERT(atom2->isInline());

  // Fat inline atom.
  RootedString atom3(cx, AtomizeChars(cx, chars, fatInlineMaxLength));
  if (!atom3 || !AppendString(cx, array, index, atom3)) {
    return false;
  }
  MOZ_ASSERT(atom3->isAtom());
  MOZ_ASSERT(atom3->isFatInline());

  // Normal flat string.
  RootedString flat1(cx, NewStringCopyN<CanGC>(cx, chars, len));
  if (!flat1 || !AppendString(cx, array, index, flat1)) {
    return false;
  }
  MOZ_ASSERT(flat1->isFlat());

  // Inline string.
  RootedString flat2(cx, NewStringCopyN<CanGC>(cx, chars, 3));
  if (!flat2 || !AppendString(cx, array, index, flat2)) {
    return false;
  }
  MOZ_ASSERT(flat2->isFlat());
  MOZ_ASSERT(flat2->isInline());

  // Fat inline string.
  RootedString flat3(cx, NewStringCopyN<CanGC>(cx, chars, fatInlineMaxLength));
  if (!flat3 || !AppendString(cx, array, index, flat3)) {
    return false;
  }
  MOZ_ASSERT(flat3->isFlat());
  MOZ_ASSERT(flat3->isFatInline());

  // Rope.
  RootedString rope(cx, ConcatStrings<CanGC>(cx, atom1, atom3));
  if (!rope || !AppendString(cx, array, index, rope)) {
    return false;
  }
  MOZ_ASSERT(rope->isRope());

  // Dependent.
  RootedString dep(cx, NewDependentString(cx, atom1, 0, len - 2));
  if (!dep || !AppendString(cx, array, index, dep)) {
    return false;
  }
  MOZ_ASSERT(dep->isDependent());

  // Undepended.
  RootedString undep(cx, NewDependentString(cx, atom1, 0, len - 3));
  if (!undep || !undep->ensureFlat(cx) ||
      !AppendString(cx, array, index, undep)) {
    return false;
  }
  MOZ_ASSERT(undep->isUndepended());

  // Extensible.
  RootedString temp1(cx, NewStringCopyN<CanGC>(cx, chars, len));
  if (!temp1) {
    return false;
  }
  RootedString extensible(cx, ConcatStrings<CanGC>(cx, temp1, atom3));
  if (!extensible || !extensible->ensureLinear(cx)) {
    return false;
  }
  if (!AppendString(cx, array, index, extensible)) {
    return false;
  }
  MOZ_ASSERT(extensible->isExtensible());

  // External. Note that we currently only support TwoByte external strings.
  RootedString external1(cx), external2(cx);
  if (IsSame<CharT, char16_t>::value) {
    external1 = JS_NewExternalString(cx, (const char16_t*)chars, len,
                                     &RepresentativeExternalStringFinalizer);
    if (!external1 || !AppendString(cx, array, index, external1)) {
      return false;
    }
    MOZ_ASSERT(external1->isExternal());

    external2 = JS_NewExternalString(cx, (const char16_t*)chars, 2,
                                     &RepresentativeExternalStringFinalizer);
    if (!external2 || !AppendString(cx, array, index, external2)) {
      return false;
    }
    MOZ_ASSERT(external2->isExternal());
  }

  // Assert the strings still have the types we expect after creating the
  // other strings.

  MOZ_ASSERT(atom1->isAtom());
  MOZ_ASSERT(atom2->isAtom());
  MOZ_ASSERT(atom3->isAtom());
  MOZ_ASSERT(atom2->isInline());
  MOZ_ASSERT(atom3->isFatInline());

  MOZ_ASSERT(flat1->isFlat());
  MOZ_ASSERT(flat2->isFlat());
  MOZ_ASSERT(flat3->isFlat());
  MOZ_ASSERT(flat2->isInline());
  MOZ_ASSERT(flat3->isFatInline());

  MOZ_ASSERT(rope->isRope());
  MOZ_ASSERT(dep->isDependent());
  MOZ_ASSERT(undep->isUndepended());
  MOZ_ASSERT(extensible->isExtensible());
  MOZ_ASSERT_IF(external1, external1->isExternal());
  MOZ_ASSERT_IF(external2, external2->isExternal());
  return true;
}

/* static */
bool JSString::fillWithRepresentatives(JSContext* cx, HandleArrayObject array) {
  uint32_t index = 0;

  auto CheckTwoByte = [](JSString* str) { return str->hasTwoByteChars(); };
  auto CheckLatin1 = [](JSString* str) { return str->hasLatin1Chars(); };

  // Append TwoByte strings.
  static const char16_t twoByteChars[] =
      u"\u1234abc\0def\u5678ghijklmasdfa\0xyz0123456789";
  if (!FillWithRepresentatives(cx, array, &index, twoByteChars,
                               mozilla::ArrayLength(twoByteChars) - 1,
                               JSFatInlineString::MAX_LENGTH_TWO_BYTE,
                               CheckTwoByte)) {
    return false;
  }

  // Append Latin1 strings.
  static const Latin1Char latin1Chars[] = "abc\0defghijklmasdfa\0xyz0123456789";
  if (!FillWithRepresentatives(
          cx, array, &index, latin1Chars, mozilla::ArrayLength(latin1Chars) - 1,
          JSFatInlineString::MAX_LENGTH_LATIN1, CheckLatin1)) {
    return false;
  }

  // Now create forcibly-tenured versions of each of these string types. Note
  // that this is best-effort; if nursery strings are disabled, or we GC
  // midway through here, then we may end up with fewer nursery strings than
  // desired. Also, some types of strings are not nursery-allocatable, so
  // this will always produce some number of redundant strings.
  gc::AutoSuppressNurseryCellAlloc suppress(cx);

  // Append TwoByte strings.
  if (!FillWithRepresentatives(cx, array, &index, twoByteChars,
                               mozilla::ArrayLength(twoByteChars) - 1,
                               JSFatInlineString::MAX_LENGTH_TWO_BYTE,
                               CheckTwoByte)) {
    return false;
  }

  // Append Latin1 strings.
  if (!FillWithRepresentatives(
          cx, array, &index, latin1Chars, mozilla::ArrayLength(latin1Chars) - 1,
          JSFatInlineString::MAX_LENGTH_LATIN1, CheckLatin1)) {
    return false;
  }

  MOZ_ASSERT(index == 44);

  return true;
}

/*** Conversions ************************************************************/

UniqueChars js::EncodeLatin1(JSContext* cx, JSString* str) {
  JSLinearString* linear = str->ensureLinear(cx);
  if (!linear) {
    return nullptr;
  }

  JS::AutoCheckCannotGC nogc;
  if (linear->hasTwoByteChars()) {
    Latin1CharsZ chars =
        JS::LossyTwoByteCharsToNewLatin1CharsZ(cx, linear->twoByteRange(nogc));
    return UniqueChars(chars.c_str());
  }

  size_t len = str->length();
  Latin1Char* buf = cx->pod_malloc<Latin1Char>(len + 1);
  if (!buf) {
    return nullptr;
  }

  FillAndTerminate(buf, linear->latin1Chars(nogc), len);
  return UniqueChars(reinterpret_cast<char*>(buf));
}

UniqueChars js::EncodeAscii(JSContext* cx, JSString* str) {
  JSLinearString* linear = str->ensureLinear(cx);
  if (!linear) {
    return nullptr;
  }

  MOZ_ASSERT(StringIsAscii(linear));
  return EncodeLatin1(cx, linear);
}

UniqueChars js::IdToPrintableUTF8(JSContext* cx, HandleId id,
                                  IdToPrintableBehavior behavior) {
  // ToString(<symbol>) throws a TypeError, therefore require that callers
  // request source representation when |id| is a property key.
  MOZ_ASSERT_IF(behavior == IdToPrintableBehavior::IdIsIdentifier,
                JSID_IS_ATOM(id) &&
                    frontend::IsIdentifierNameOrPrivateName(JSID_TO_ATOM(id)));

  RootedValue v(cx, IdToValue(id));
  JSString* str;
  if (behavior == IdToPrintableBehavior::IdIsPropertyKey) {
    str = ValueToSource(cx, v);
  } else {
    str = ToString<CanGC>(cx, v);
  }
  if (!str) {
    return nullptr;
  }
  return StringToNewUTF8CharsZ(cx, *str);
}

template <AllowGC allowGC>
JSString* js::ToStringSlow(
    JSContext* cx, typename MaybeRooted<Value, allowGC>::HandleType arg) {
  /* As with ToObjectSlow, callers must verify that |arg| isn't a string. */
  MOZ_ASSERT(!arg.isString());

  Value v = arg;
  if (!v.isPrimitive()) {
    MOZ_ASSERT(!cx->isHelperThreadContext());
    if (!allowGC) {
      return nullptr;
    }
    RootedValue v2(cx, v);
    if (!ToPrimitive(cx, JSTYPE_STRING, &v2)) {
      return nullptr;
    }
    v = v2;
  }

  JSString* str;
  if (v.isString()) {
    str = v.toString();
  } else if (v.isInt32()) {
    str = Int32ToString<allowGC>(cx, v.toInt32());
  } else if (v.isDouble()) {
    str = NumberToString<allowGC>(cx, v.toDouble());
  } else if (v.isBoolean()) {
    str = BooleanToString(cx, v.toBoolean());
  } else if (v.isNull()) {
    str = cx->names().null;
  } else if (v.isSymbol()) {
    MOZ_ASSERT(!cx->isHelperThreadContext());
    if (allowGC) {
      JS_ReportErrorNumberASCII(cx, GetErrorMessage, nullptr,
                                JSMSG_SYMBOL_TO_STRING);
    }
    return nullptr;
  } else if (v.isBigInt()) {
    if (!allowGC) {
      return nullptr;
    }
    RootedBigInt i(cx, v.toBigInt());
    str = BigInt::toString<CanGC>(cx, i, 10);
  } else {
    MOZ_ASSERT(v.isUndefined());
    str = cx->names().undefined;
  }
  return str;
}

template JSString* js::ToStringSlow<CanGC>(JSContext* cx, HandleValue arg);

template JSString* js::ToStringSlow<NoGC>(JSContext* cx, const Value& arg);

JS_PUBLIC_API JSString* js::ToStringSlow(JSContext* cx, HandleValue v) {
  return ToStringSlow<CanGC>(cx, v);
}

/*
 * Convert a JSString to its source expression; returns null after reporting an
 * error, otherwise returns a new string reference. No Handle needed since the
 * input is dead after the GC.
 */
static JSString* StringToSource(JSContext* cx, JSString* str) {
  UniqueChars chars = QuoteString(cx, str, '"');
  if (!chars) {
    return nullptr;
  }
  return NewStringCopyZ<CanGC>(cx, chars.get());
}

static JSString* SymbolToSource(JSContext* cx, Symbol* symbol) {
  RootedString desc(cx, symbol->description());
  SymbolCode code = symbol->code();
  if (code != SymbolCode::InSymbolRegistry &&
      code != SymbolCode::UniqueSymbol) {
    // Well-known symbol.
    MOZ_ASSERT(uint32_t(code) < JS::WellKnownSymbolLimit);
    return desc;
  }

  JSStringBuilder buf(cx);
  if (code == SymbolCode::InSymbolRegistry ? !buf.append("Symbol.for(")
                                           : !buf.append("Symbol(")) {
    return nullptr;
  }
  if (desc) {
    UniqueChars quoted = QuoteString(cx, desc, '"');
    if (!quoted || !buf.append(quoted.get(), strlen(quoted.get()))) {
      return nullptr;
    }
  }
  if (!buf.append(')')) {
    return nullptr;
  }
  return buf.finishString();
}

JSString* js::ValueToSource(JSContext* cx, HandleValue v) {
  if (!CheckRecursionLimit(cx)) {
    return nullptr;
  }
  cx->check(v);

  if (v.isUndefined()) {
    return cx->names().void0;
  }
  if (v.isString()) {
    return StringToSource(cx, v.toString());
  }
  if (v.isSymbol()) {
    return SymbolToSource(cx, v.toSymbol());
  }
  if (v.isPrimitive()) {
    /* Special case to preserve negative zero, _contra_ toString. */
    if (v.isDouble() && IsNegativeZero(v.toDouble())) {
      static const Latin1Char negativeZero[] = {'-', '0'};

      return NewStringCopyN<CanGC>(cx, negativeZero,
                                   mozilla::ArrayLength(negativeZero));
    }
    return ToString<CanGC>(cx, v);
  }

  RootedValue fval(cx);
  RootedObject obj(cx, &v.toObject());
  if (!GetProperty(cx, obj, obj, cx->names().toSource, &fval)) {
    return nullptr;
  }
  if (IsCallable(fval)) {
    RootedValue v(cx);
    if (!js::Call(cx, fval, obj, &v)) {
      return nullptr;
    }

    return ToString<CanGC>(cx, v);
  }

  return ObjectToSource(cx, obj);
}