quickjs-oxide 0.4.1

A safe Rust JavaScript engine targeting semantic parity with QuickJS
Documentation
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
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
use std::cell::Cell;
use std::cell::RefCell;
use std::collections::TryReserveError;
use std::fmt;

use std::hash::{Hash, Hasher};
use std::iter::FusedIterator;
use std::rc::{Rc, Weak};

use crate::engine::api::error::{Error, ErrorKind, NativeErrorMessage};
use crate::engine::value::bigint::JsBigInt;

/// ECMAScript strings are sequences of UTF-16 code units, not UTF-8 scalar
/// values. Compact Latin-1/UTF-16 leaves and bounded ropes mirror QuickJS's
/// current representation while preserving lone surrogates.
#[derive(Clone)]
pub struct JsString(Rc<StringRepr>);

/// Non-owning handle to a String value ([`JsString::downgrade`]): `upgrade`
/// recovers the same string while another handle keeps it alive.
#[derive(Clone)]
pub struct WeakJsString(Weak<StringRepr>);

impl WeakJsString {
    #[must_use]
    pub fn upgrade(&self) -> Option<JsString> {
        self.0.upgrade().map(JsString)
    }
}

impl fmt::Debug for WeakJsString {
    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
        formatter
            .debug_tuple("WeakJsString")
            .field(&self.0.as_ptr())
            .finish()
    }
}

/// Why a string-kernel operation failed. It becomes a QuickJS InternalError
/// (`ErrorKind::JsInternal`) in the front end's errors, and a
/// [`RuntimeError`](crate::engine::api::core::RuntimeError) in the embedding
/// API.
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum JsStringError {
    TooLong,
    OutOfMemory,
}

impl fmt::Display for JsStringError {
    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::TooLong => formatter.write_str("string too long"),
            Self::OutOfMemory => formatter.write_str("out of memory"),
        }
    }
}

impl std::error::Error for JsStringError {}

impl From<JsStringError> for Error {
    fn from(error: JsStringError) -> Self {
        match error {
            JsStringError::TooLong | JsStringError::OutOfMemory => {
                Error::new(ErrorKind::JsInternal, error.to_string())
            }
        }
    }
}

#[cfg(test)]
thread_local! {
    static FAIL_NEXT_CONCAT_RESERVATION: Cell<bool> = const { Cell::new(false) };
    static FAIL_NEXT_REPEAT_RESERVATION: Cell<bool> = const { Cell::new(false) };
    static FAIL_NEXT_PAD_RESERVATION: Cell<bool> = const { Cell::new(false) };
    static FAIL_NEXT_TRIM_RESERVATION: Cell<bool> = const { Cell::new(false) };
    static FAIL_NEXT_REPLACEMENT_RESERVATION: Cell<bool> = const { Cell::new(false) };
}

#[cfg(test)]
fn arm_reservation_failure(flag: &'static std::thread::LocalKey<Cell<bool>>, label: &str) {
    flag.with(|armed| {
        assert!(
            !armed.replace(true),
            "{label} reservation failure was already armed"
        )
    });
}

#[cfg(test)]
pub fn fail_next_repeat_reservation_for_test() {
    arm_reservation_failure(&FAIL_NEXT_REPEAT_RESERVATION, "repeat");
}

#[cfg(test)]
pub fn fail_next_pad_reservation_for_test() {
    arm_reservation_failure(&FAIL_NEXT_PAD_RESERVATION, "pad");
}

#[cfg(test)]
pub fn fail_next_trim_reservation_for_test() {
    arm_reservation_failure(&FAIL_NEXT_TRIM_RESERVATION, "trim");
}

struct StringRepr {
    content_hash: Cell<Option<u32>>,
    data: StringData,
}

impl StringRepr {
    fn allocate(data: StringData) -> Rc<Self> {
        Rc::new(Self {
            content_hash: Cell::new(None),
            data,
        })
    }

    fn data(&self) -> &StringData {
        &self.data
    }
}

enum StringData {
    Latin1(Vec<u8>),
    Utf16(Vec<u16>),
    Rope(RopeRepr),
}

struct RopeRepr {
    len: usize,
    is_wide: bool,
    depth: u8,
    /// QuickJS rewrites a shared rope to `flat + empty` after ToString while
    /// retaining its tag and cached depth. The state transition releases the
    /// old children and preserves that exact observable performance shape.
    state: RefCell<RopeState>,
}

enum RopeState {
    Tree { left: JsString, right: JsString },
    Linearized { flat: JsString },
}

struct Utf16Units {
    stack: [Option<JsString>; 61],
    stack_len: usize,
    current_flat: Option<(JsString, usize)>,
    remaining: usize,
}

/// Borrowed code-unit iterator for [`JsString::utf16_units`]. Flat strings
/// iterate their storage slice in place; ropes keep the owning traversal.
enum BorrowedUtf16Units<'a> {
    Latin1(std::slice::Iter<'a, u8>),
    Utf16(std::slice::Iter<'a, u16>),
    // Boxed: the traversal stack dwarfs the flat variants, and only ropes
    // pay the allocation.
    Rope(Box<Utf16Units>),
}

impl Iterator for BorrowedUtf16Units<'_> {
    type Item = u16;

    #[inline]
    fn next(&mut self) -> Option<u16> {
        match self {
            Self::Latin1(units) => units.next().copied().map(u16::from),
            Self::Utf16(units) => units.next().copied(),
            Self::Rope(units) => units.next(),
        }
    }

    #[inline]
    fn size_hint(&self) -> (usize, Option<usize>) {
        match self {
            Self::Latin1(units) => units.size_hint(),
            Self::Utf16(units) => units.size_hint(),
            Self::Rope(units) => units.size_hint(),
        }
    }

    #[inline]
    fn nth(&mut self, n: usize) -> Option<u16> {
        match self {
            Self::Latin1(units) => units.nth(n).copied().map(u16::from),
            Self::Utf16(units) => units.nth(n).copied(),
            Self::Rope(units) => units.nth(n),
        }
    }
}

impl ExactSizeIterator for BorrowedUtf16Units<'_> {}

struct QuickJsUtf8Bytes {
    units: std::iter::Peekable<Utf16Units>,
    cesu8: bool,
    pending: [u8; 4],
    pending_index: usize,
    pending_len: usize,
}

pub struct JsStringBuilder {
    units: Vec<u16>,
    limit: usize,
    failed: bool,
}

impl JsStringBuilder {
    pub fn with_limit(capacity: usize, limit: usize) -> Self {
        let limit = limit.min(JsString::MAX_LEN);
        Self {
            units: Vec::with_capacity(capacity.min(limit).min(4096)),
            limit,
            failed: false,
        }
    }

    fn push_latin1(&mut self, value: &[u8]) -> Result<(), JsStringError> {
        self.ensure_additional(value.len())?;
        self.units.extend(value.iter().copied().map(u16::from));
        Ok(())
    }

    pub fn push_code_point(&mut self, value: u32) -> Result<(), JsStringError> {
        debug_assert!(value <= 0x10_ffff);
        let additional = if value <= 0xffff { 1 } else { 2 };
        self.ensure_additional(additional)?;
        if value <= 0xffff {
            self.units.push(value as u16);
        } else {
            let adjusted = value - 0x1_0000;
            self.units.push(0xd800 | ((adjusted >> 10) as u16));
            self.units.push(0xdc00 | ((adjusted & 0x3ff) as u16));
        }
        Ok(())
    }

    fn ensure_additional(&mut self, additional: usize) -> Result<(), JsStringError> {
        if self.failed {
            return Err(JsStringError::TooLong);
        }
        if JsString::checked_length_with_limit(self.units.len(), additional, self.limit).is_err() {
            self.units = Vec::new();
            self.failed = true;
            return Err(JsStringError::TooLong);
        }
        Ok(())
    }

    pub fn finish(self) -> Result<JsString, JsStringError> {
        if self.failed {
            return Err(JsStringError::TooLong);
        }
        debug_assert!(self.units.len() <= self.limit);
        Ok(JsString::from_validated_utf16(self.units))
    }
}

impl Utf16Units {
    fn new(string: &JsString) -> Self {
        let mut iterator = Self {
            // Inline-const init compiles to a plain zeroing store; the closure
            // form lowered to one non-inlined call per slot on this hot path.
            stack: [const { None }; 61],
            stack_len: 0,
            current_flat: None,
            remaining: string.len(),
        };
        iterator.push(string.clone());
        iterator
    }

    fn push(&mut self, string: JsString) {
        assert!(
            self.stack_len < self.stack.len(),
            "String rope exceeded its bounded iterator depth"
        );
        self.stack[self.stack_len] = Some(string);
        self.stack_len += 1;
    }

    fn pop(&mut self) -> Option<JsString> {
        self.stack_len = self.stack_len.checked_sub(1)?;
        self.stack[self.stack_len].take()
    }
}

impl Iterator for Utf16Units {
    type Item = u16;

    fn next(&mut self) -> Option<Self::Item> {
        loop {
            if let Some((flat, index)) = &mut self.current_flat {
                let unit = match flat.0.data() {
                    StringData::Latin1(units) => units.get(*index).copied().map(u16::from),
                    StringData::Utf16(units) => units.get(*index).copied(),
                    StringData::Rope(_) => {
                        unreachable!("UTF-16 iterator current node must be flat")
                    }
                };
                if let Some(unit) = unit {
                    *index += 1;
                    self.remaining -= 1;
                    return Some(unit);
                }
                self.current_flat = None;
            }

            let node = self.pop()?;
            match node.0.data() {
                StringData::Latin1(_) | StringData::Utf16(_) => {
                    self.current_flat = Some((node, 0));
                }
                StringData::Rope(rope) => match &*rope.state.borrow() {
                    RopeState::Tree { left, right } => {
                        self.push(right.clone());
                        self.push(left.clone());
                    }
                    RopeState::Linearized { flat } => self.push(flat.clone()),
                },
            }
        }
    }

    fn nth(&mut self, mut n: usize) -> Option<Self::Item> {
        while n > 0 {
            if let Some((flat, index)) = &mut self.current_flat {
                let skip = n.min(flat.len() - *index);
                *index += skip;
                self.remaining -= skip;
                n -= skip;
                if n == 0 {
                    break;
                }
                self.current_flat = None;
            }
            let node = self.pop()?;
            if node.len() <= n {
                n -= node.len();
                self.remaining -= node.len();
                continue;
            }
            match node.0.data() {
                StringData::Latin1(_) | StringData::Utf16(_) => self.current_flat = Some((node, 0)),
                StringData::Rope(rope) => match &*rope.state.borrow() {
                    RopeState::Tree { left, right } => {
                        self.push(right.clone());
                        self.push(left.clone());
                    }
                    RopeState::Linearized { flat } => self.push(flat.clone()),
                },
            }
        }
        self.next()
    }

    fn size_hint(&self) -> (usize, Option<usize>) {
        (self.remaining, Some(self.remaining))
    }
}

impl ExactSizeIterator for Utf16Units {
    fn len(&self) -> usize {
        self.remaining
    }
}

impl FusedIterator for Utf16Units {}

impl QuickJsUtf8Bytes {
    fn new(string: &JsString, cesu8: bool) -> Self {
        Self {
            units: Utf16Units::new(string).peekable(),
            cesu8,
            pending: [0; 4],
            pending_index: 0,
            pending_len: 0,
        }
    }
}

impl Iterator for QuickJsUtf8Bytes {
    type Item = u8;

    fn next(&mut self) -> Option<Self::Item> {
        if self.pending_index < self.pending_len {
            let byte = self.pending[self.pending_index];
            self.pending_index += 1;
            return Some(byte);
        }

        let unit = self.units.next()?;
        let code_point = if !self.cesu8
            && (0xd800..=0xdbff).contains(&unit)
            && self
                .units
                .peek()
                .is_some_and(|next| (0xdc00..=0xdfff).contains(next))
        {
            let low = self
                .units
                .next()
                .expect("peeked low surrogate disappeared from UTF-16 iterator");
            0x1_0000 + ((u32::from(unit) - 0xd800) << 10) + (u32::from(low) - 0xdc00)
        } else {
            u32::from(unit)
        };
        self.pending_len = encode_quickjs_utf8(&mut self.pending, code_point);
        self.pending_index = 1;
        Some(self.pending[0])
    }
}

impl FusedIterator for QuickJsUtf8Bytes {}

impl NativeErrorMessage {
    pub fn to_js_string(&self) -> Result<JsString, JsStringError> {
        JsString::try_from_bytes(self.visible_bytes())
    }

    pub fn to_utf8_lossy(&self) -> String {
        self.to_js_string()
            .expect("a 255-byte native Error message cannot exceed the String length limit")
            .to_utf8_lossy()
    }
}

impl JsString {
    pub(crate) fn flat_latin1(&self) -> Option<&[u8]> {
        if let StringData::Latin1(units) = self.0.data() {
            Some(units)
        } else {
            None
        }
    }

    pub(crate) fn flat_utf16(&self) -> Option<&[u16]> {
        if let StringData::Utf16(units) = self.0.data() {
            Some(units)
        } else {
            None
        }
    }

    /// QuickJS reserves 30 bits for a string's UTF-16 code-unit length.
    pub const MAX_LEN: usize = (1 << 30) - 1;
    const ROPE_SHORT_LEN: usize = 512;
    const ROPE_SHORT2_LEN: usize = 8192;
    const ROPE_MAX_DEPTH: u8 = 60;
    const ROPE_BUCKET_LENGTHS: [usize; 44] = [
        1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987, 1597, 2584, 4181, 6765, 10946,
        17711, 28657, 46368, 75025, 121393, 196418, 317811, 514229, 832040, 1346269, 2178309,
        3524578, 5702887, 9227465, 14930352, 24157817, 39088169, 63245986, 102334155, 165580141,
        267914296, 433494437, 701408733, 1134903170,
    ];

    #[must_use]
    pub fn downgrade(&self) -> WeakJsString {
        WeakJsString(Rc::downgrade(&self.0))
    }

    #[must_use]
    pub fn same_representation(&self, other: &Self) -> bool {
        Rc::ptr_eq(&self.0, &other.0)
    }

    /// The address of the representation, which [`Self::same_representation`]
    /// compares; it identifies the string while a clone of it is alive.
    #[must_use]
    pub(crate) fn address(&self) -> usize {
        Rc::as_ptr(&self.0).addr()
    }

    #[must_use]
    pub fn content_hash(&self) -> u32 {
        if let Some(hash) = self.0.content_hash.get() {
            return hash;
        }
        let hash = self.quickjs_hash(0);
        self.0.content_hash.set(Some(hash));
        hash
    }

    pub fn checked_length_with_limit(
        current: usize,
        additional: usize,
        limit: usize,
    ) -> Result<usize, JsStringError> {
        current
            .checked_add(additional)
            .filter(|length| *length <= limit)
            .ok_or(JsStringError::TooLong)
    }

    pub fn checked_length(current: usize, additional: usize) -> Result<usize, JsStringError> {
        Self::checked_length_with_limit(current, additional, Self::MAX_LEN)
    }

    /// Construct a dynamically supplied UTF-8 string while enforcing
    /// QuickJS's 30-bit UTF-16 length limit.
    ///
    /// # Errors
    /// Returns [`JsStringError::TooLong`] when the decoded value exceeds
    /// [`Self::MAX_LEN`]. Recoverable allocator failure remains a separate
    /// parity gap.
    pub fn try_from_utf8(value: &str) -> Result<Self, JsStringError> {
        if value.is_ascii() {
            return Self::try_from_latin1(value.as_bytes());
        }
        Self::try_from_utf16(value.encode_utf16())
    }

    /// Decode the byte-oriented input accepted by QuickJS `JS_NewStringLen`.
    ///
    /// This is deliberately not Rust's strict or standard lossy UTF-8
    /// decoder. Three-byte surrogate encodings are preserved as UTF-16 code
    /// units, non-BMP scalars become surrogate pairs, and malformed sequences
    /// use QuickJS's release-pinned replacement-and-skip algorithm.
    /// Embedded NUL bytes are ordinary U+0000 code units because the input
    /// length is explicit.
    ///
    /// # Errors
    /// Returns [`JsStringError::TooLong`] when the decoded value exceeds
    /// [`Self::MAX_LEN`] UTF-16 code units. Recoverable allocation failure for
    /// String storage remains a separate parity gap.
    pub fn try_from_bytes(value: &[u8]) -> Result<Self, JsStringError> {
        Self::try_from_bytes_with_limit(value, Self::MAX_LEN)
    }

    /// Construct one of the engine's trusted static table/literal strings.
    /// Dynamic host input must use [`Self::try_from_utf8`].
    pub fn from_static(value: &'static str) -> Self {
        Self::try_from_utf8(value).expect("static ECMAScript String exceeded QuickJS's length cap")
    }

    /// Construct a dynamically supplied UTF-16 string while enforcing
    /// QuickJS's 30-bit code-unit length limit.
    ///
    /// The iterator is consumed only after its lower size bound is validated;
    /// a dishonest or unbounded upper hint is never used for an enormous eager
    /// allocation.
    ///
    /// # Errors
    /// Returns [`JsStringError::TooLong`] when the value exceeds
    /// [`Self::MAX_LEN`]. Recoverable allocator failure remains a separate
    /// parity gap.
    pub fn try_from_utf16(units: impl IntoIterator<Item = u16>) -> Result<Self, JsStringError> {
        Self::try_from_utf16_with_limit(units, Self::MAX_LEN)
    }

    pub fn try_from_utf16_with_limit(
        units: impl IntoIterator<Item = u16>,
        max_len: usize,
    ) -> Result<Self, JsStringError> {
        let max_len = max_len.min(Self::MAX_LEN);
        let mut iterator = units.into_iter();
        let (lower, upper) = iterator.size_hint();
        if lower > max_len {
            return Err(JsStringError::TooLong);
        }
        let initial_capacity = upper.unwrap_or(lower).min(max_len).min(4096);
        let mut collected = Vec::with_capacity(initial_capacity);
        for unit in &mut iterator {
            if collected.len() == max_len {
                return Err(JsStringError::TooLong);
            }
            collected.push(unit);
        }
        Ok(Self::from_validated_utf16(collected))
    }

    fn try_from_bytes_with_limit(value: &[u8], max_len: usize) -> Result<Self, JsStringError> {
        let max_len = max_len.min(Self::MAX_LEN);
        let ascii_len = value.iter().take_while(|byte| **byte < 0x80).count();
        if ascii_len > max_len {
            return Err(JsStringError::TooLong);
        }
        if ascii_len == value.len() {
            return Ok(Self::from_owned_latin1(value.to_vec()));
        }

        let mut output = JsStringBuilder::with_limit(value.len(), max_len);
        output.push_latin1(&value[..ascii_len])?;
        let mut index = ascii_len;
        while index < value.len() {
            if value[index] < 0x80 {
                output.push_code_point(u32::from(value[index]))?;
                index += 1;
                continue;
            }

            match decode_quickjs_utf8(&value[index..]) {
                Some((code_point, consumed)) if code_point <= 0x10_ffff => {
                    output.push_code_point(code_point)?;
                    index += consumed;
                }
                Some(_) | None => {
                    index = skip_quickjs_invalid_utf8(value, index);
                    output.push_code_point(0xfffd)?;
                }
            }
        }
        output.finish()
    }

    fn from_validated_utf16(units: Vec<u16>) -> Self {
        debug_assert!(units.len() <= Self::MAX_LEN);
        if units.iter().all(|&unit| unit <= 0xff) {
            let latin1 = units.iter().map(|&unit| unit as u8).collect();
            Self(StringRepr::allocate(StringData::Latin1(latin1)))
        } else {
            Self(StringRepr::allocate(StringData::Utf16(units)))
        }
    }

    /// Adopt a checked narrow result buffer without collecting its contents a
    /// second time. Callers must have enforced [`Self::MAX_LEN`].
    pub fn from_owned_latin1(units: Vec<u8>) -> Self {
        debug_assert!(units.len() <= Self::MAX_LEN);
        Self(StringRepr::allocate(StringData::Latin1(units)))
    }

    /// Materialize QuickJS's `JS_AtomToString` result for one tagged integer
    /// atom. The returned narrow String deliberately owns a fresh
    /// representation on every call; routing this spelling through an atom or
    /// constant pool would make QuickJS's representation identity observable.
    #[must_use]
    pub fn from_fresh_decimal_u32(value: u32) -> Self {
        let mut buffer = fmt::NumBuffer::new();
        let digits = value.format_into(&mut buffer).as_bytes().to_vec();
        Self(StringRepr::allocate(StringData::Latin1(digits)))
    }

    /// A copy of Latin-1 code units.
    ///
    /// # Errors
    /// Returns [`JsStringError::TooLong`] past [`Self::MAX_LEN`].
    pub fn try_from_latin1(units: &[u8]) -> Result<Self, JsStringError> {
        if units.len() > Self::MAX_LEN {
            return Err(JsStringError::TooLong);
        }
        Ok(Self::from_owned_latin1(units.to_vec()))
    }

    /// A copy of UTF-16 code units, stored narrow when every unit fits in a
    /// byte.
    ///
    /// # Errors
    /// Returns [`JsStringError::TooLong`] past [`Self::MAX_LEN`].
    pub fn try_from_utf16_slice(units: &[u16]) -> Result<Self, JsStringError> {
        if units.len() > Self::MAX_LEN {
            return Err(JsStringError::TooLong);
        }
        if units.iter().all(|&unit| unit <= 0xff) {
            return Ok(Self::from_owned_latin1(
                units.iter().map(|&unit| unit as u8).collect(),
            ));
        }
        Ok(Self::from_owned_utf16(units.to_vec()))
    }

    /// Adopt a checked wide result buffer without collecting its contents a
    /// second time. Callers must have enforced [`Self::MAX_LEN`].
    pub fn from_owned_utf16(units: Vec<u16>) -> Self {
        debug_assert!(units.len() <= Self::MAX_LEN);
        Self(StringRepr::allocate(StringData::Utf16(units)))
    }

    /// Build the compact one-code-unit form used by String exotic indices and
    /// character methods, equivalent to QuickJS's `js_new_string_char`.
    #[must_use]
    pub fn from_code_unit(unit: u16) -> Self {
        match u8::try_from(unit) {
            Ok(unit) => Self(StringRepr::allocate(StringData::Latin1(vec![unit]))),
            Err(_) => Self(StringRepr::allocate(StringData::Utf16(vec![unit]))),
        }
    }

    #[must_use]
    pub fn len(&self) -> usize {
        match self.0.data() {
            StringData::Latin1(units) => units.len(),
            StringData::Utf16(units) => units.len(),
            StringData::Rope(rope) => rope.len,
        }
    }

    #[must_use]
    pub fn is_empty(&self) -> bool {
        self.len() == 0
    }

    #[inline]
    pub fn utf16_units(&self) -> impl ExactSizeIterator<Item = u16> + '_ {
        // Flat strings walk their storage slice directly; only a live rope
        // pays for the owning traversal iterator and its bounded stack.
        match self.0.data() {
            StringData::Latin1(units) => BorrowedUtf16Units::Latin1(units.iter()),
            StringData::Utf16(units) => BorrowedUtf16Units::Utf16(units.iter()),
            StringData::Rope(_) => BorrowedUtf16Units::Rope(Box::new(Utf16Units::new(self))),
        }
    }

    /// Return one UTF-16 code unit without decoding or normalizing surrogate
    /// pairs. This is the equivalent of QuickJS's `string_get` fast path.
    #[must_use]
    pub fn code_unit_at(&self, index: usize) -> Option<u16> {
        if index >= self.len() {
            return None;
        }
        match self.0.data() {
            StringData::Latin1(units) => units.get(index).copied().map(u16::from),
            StringData::Utf16(units) => units.get(index).copied(),
            StringData::Rope(rope) => match &*rope.state.borrow() {
                RopeState::Linearized { flat } => flat.code_unit_at(index),
                RopeState::Tree { left, right } => {
                    if index < left.len() {
                        left.code_unit_at(index)
                    } else {
                        right.code_unit_at(index - left.len())
                    }
                }
            },
        }
    }

    /// Return the code point beginning at one UTF-16 code-unit index. A lead
    /// surrogate is combined only with an immediately following trail
    /// surrogate; every other code unit is returned unchanged.
    #[must_use]
    pub fn code_point_at(&self, index: usize) -> Option<u32> {
        let first = self.code_unit_at(index)?;
        if !(0xd800..=0xdbff).contains(&first) {
            return Some(u32::from(first));
        }
        let Some(second) = index
            .checked_add(1)
            .and_then(|next| self.code_unit_at(next))
        else {
            return Some(u32::from(first));
        };
        if !(0xdc00..=0xdfff).contains(&second) {
            return Some(u32::from(first));
        }
        Some(0x1_0000 + ((u32::from(first) - 0xd800) << 10) + (u32::from(second) - 0xdc00))
    }

    /// Whether every surrogate participates in a valid UTF-16 pair.
    #[must_use]
    pub fn is_well_formed(&self) -> bool {
        self.first_unpaired_surrogate().is_none()
    }

    /// Replace each unpaired surrogate with U+FFFD. Well-formed strings retain
    /// their existing compact allocation, matching QuickJS's no-copy path.
    #[must_use]
    pub fn to_well_formed(&self) -> Self {
        let Some(first_invalid) = self.first_unpaired_surrogate() else {
            return self.clone();
        };
        let mut units = self.utf16_units().collect::<Vec<_>>();
        let mut index = first_invalid;
        while index < units.len() {
            let unit = units[index];
            if (0xd800..=0xdbff).contains(&unit)
                && units
                    .get(index + 1)
                    .is_some_and(|next| (0xdc00..=0xdfff).contains(next))
            {
                index += 2;
            } else {
                if (0xd800..=0xdfff).contains(&unit) {
                    units[index] = 0xfffd;
                }
                index += 1;
            }
        }
        Self::from_validated_utf16(units)
    }

    fn first_unpaired_surrogate(&self) -> Option<usize> {
        if !self.is_wide() {
            return None;
        }
        let mut units = self.utf16_units().enumerate().peekable();
        while let Some((index, unit)) = units.next() {
            if (0xd800..=0xdbff).contains(&unit) {
                if units
                    .peek()
                    .is_some_and(|(_, next)| (0xdc00..=0xdfff).contains(next))
                {
                    units.next();
                    continue;
                }
                return Some(index);
            }
            if (0xd800..=0xdfff).contains(&unit) {
                return Some(index);
            }
        }
        None
    }

    pub fn is_wide(&self) -> bool {
        match self.0.data() {
            StringData::Latin1(_) => false,
            StringData::Utf16(_) => true,
            StringData::Rope(rope) => rope.is_wide,
        }
    }

    fn depth(&self) -> u8 {
        match self.0.data() {
            StringData::Latin1(_) | StringData::Utf16(_) => 0,
            StringData::Rope(rope) => rope.depth,
        }
    }

    pub fn is_flat(&self) -> bool {
        matches!(self.0.data(), StringData::Latin1(_) | StringData::Utf16(_))
    }

    /// Visit the current flat leaves in logical order without linearizing a
    /// rope. QuickJS's diagnostic String printer decodes each leaf
    /// independently, so surrogate pairs do not cross a live rope boundary.
    /// A previously linearized rope exposes its replacement flat value as one
    /// leaf, matching QuickJS's in-place `flat + empty` transition.
    pub fn for_each_flat_leaf(&self, mut visitor: impl FnMut(&Self)) {
        let mut stack: [Option<Self>; 61] = [const { None }; 61];
        stack[0] = Some(self.clone());
        let mut stack_len = 1;
        while stack_len != 0 {
            stack_len -= 1;
            let node = stack[stack_len]
                .take()
                .expect("String flat-leaf traversal stack contained a hole");
            match node.0.data() {
                StringData::Latin1(_) | StringData::Utf16(_) => visitor(&node),
                StringData::Rope(rope) => match &*rope.state.borrow() {
                    RopeState::Tree { left, right } => {
                        assert!(
                            stack_len + 2 <= stack.len(),
                            "String rope exceeded its bounded flat-leaf traversal depth"
                        );
                        stack[stack_len] = Some(right.clone());
                        stack[stack_len + 1] = Some(left.clone());
                        stack_len += 2;
                    }
                    RopeState::Linearized { flat } => {
                        assert!(
                            stack_len < stack.len(),
                            "String rope exceeded its bounded flat-leaf traversal depth"
                        );
                        stack[stack_len] = Some(flat.clone());
                        stack_len += 1;
                    }
                },
            }
        }
    }

    fn quickjs_hash(&self, seed: u32) -> u32 {
        let step = |hash: u32, unit: u16| hash.wrapping_mul(263).wrapping_add(u32::from(unit));
        match self.0.data() {
            StringData::Latin1(units) => units.iter().copied().map(u16::from).fold(seed, step),
            StringData::Utf16(units) => units.iter().copied().fold(seed, step),
            StringData::Rope(_) => self.utf16_units().fold(seed, step),
        }
    }

    fn rope_children(rope: &RopeRepr) -> (Self, Self) {
        match &*rope.state.borrow() {
            RopeState::Tree { left, right } => (left.clone(), right.clone()),
            RopeState::Linearized { flat } => (flat.clone(), Self::from_static("")),
        }
    }

    fn flat_concat(left: &Self, right: &Self) -> Self {
        debug_assert!(left.is_flat() && right.is_flat());
        let len = left
            .len()
            .checked_add(right.len())
            .expect("validated flat String concatenation length overflowed");
        debug_assert!(len <= Self::MAX_LEN);
        match (left.0.data(), right.0.data()) {
            (StringData::Latin1(left), StringData::Latin1(right)) => {
                let mut units = Vec::with_capacity(len);
                units.extend_from_slice(left);
                units.extend_from_slice(right);
                Self(StringRepr::allocate(StringData::Latin1(units)))
            }
            (
                StringData::Latin1(_) | StringData::Utf16(_),
                StringData::Latin1(_) | StringData::Utf16(_),
            ) => {
                let units = left
                    .utf16_units()
                    .chain(right.utf16_units())
                    .collect::<Vec<_>>();
                debug_assert_eq!(units.len(), len);
                Self(StringRepr::allocate(StringData::Utf16(units)))
            }
            (StringData::Rope(_), _) | (_, StringData::Rope(_)) => {
                unreachable!("flat String concatenation received a rope")
            }
        }
    }

    fn new_rope_unbalanced(left: Self, right: Self) -> Result<Self, JsStringError> {
        let len = left
            .len()
            .checked_add(right.len())
            .filter(|len| *len <= Self::MAX_LEN)
            .ok_or(JsStringError::TooLong)?;
        let depth = left
            .depth()
            .max(right.depth())
            .checked_add(1)
            .expect("String rope depth invariant overflowed");
        Ok(Self(StringRepr::allocate(StringData::Rope(RopeRepr {
            len,
            is_wide: left.is_wide() || right.is_wide(),
            depth,
            state: RefCell::new(RopeState::Tree { left, right }),
        }))))
    }

    fn new_rope(left: Self, right: Self) -> Result<Self, JsStringError> {
        let rope = Self::new_rope_unbalanced(left, right)?;
        if rope.depth() > Self::ROPE_MAX_DEPTH {
            rope.rebalance()
        } else {
            Ok(rope)
        }
    }

    fn rebalance(&self) -> Result<Self, JsStringError> {
        let mut buckets: [Option<Self>; Self::ROPE_BUCKET_LENGTHS.len()] =
            [const { None }; Self::ROPE_BUCKET_LENGTHS.len()];
        // Rebalancing is entered for the one temporary depth-61 rope. A
        // depth-first traversal therefore needs at most 62 pending nodes.
        let mut pending: [Option<Self>; 62] = [const { None }; 62];
        pending[0] = Some(self.clone());
        let mut pending_len = 1;
        while pending_len != 0 {
            pending_len -= 1;
            let node = pending[pending_len]
                .take()
                .expect("String rope rebalance stack contained a hole");
            match node.0.data() {
                StringData::Rope(rope) => {
                    match &*rope.state.borrow() {
                        RopeState::Tree { left, right } => {
                            assert!(
                                pending_len + 2 <= pending.len(),
                                "String rope exceeded its bounded rebalance depth"
                            );
                            pending[pending_len] = Some(right.clone());
                            pending[pending_len + 1] = Some(left.clone());
                            pending_len += 2;
                        }
                        RopeState::Linearized { flat } => {
                            assert!(
                                pending_len < pending.len(),
                                "String rope exceeded its bounded rebalance depth"
                            );
                            pending[pending_len] = Some(flat.clone());
                            pending_len += 1;
                        }
                    }
                    continue;
                }
                StringData::Latin1(_) | StringData::Utf16(_) if node.is_empty() => continue,
                StringData::Latin1(_) | StringData::Utf16(_) => {}
            }

            let len = node.len();
            let mut index = 0;
            let mut prefix = None;
            while len >= Self::ROPE_BUCKET_LENGTHS[index + 1] {
                if let Some(bucket) = buckets[index].take() {
                    prefix = Some(match prefix {
                        None => bucket,
                        Some(prefix) => Self::new_rope_unbalanced(bucket, prefix)?,
                    });
                }
                index += 1;
            }
            let mut value = match prefix {
                Some(prefix) => Self::new_rope_unbalanced(prefix, node)?,
                None => node,
            };
            while let Some(bucket) = buckets[index].take() {
                value = Self::new_rope_unbalanced(bucket, value)?;
                index += 1;
            }
            buckets[index] = Some(value);
        }

        let mut result = None;
        for bucket in buckets.into_iter().flatten() {
            result = Some(match result {
                None => bucket,
                Some(result) => Self::new_rope_unbalanced(bucket, result)?,
            });
        }
        Ok(result.unwrap_or_else(|| Self::from_static("")))
    }

    /// Return a compact flat string, caching the result on a rope. This is the
    /// safe-Rust equivalent of QuickJS `js_linearize_string_rope`.
    #[must_use]
    pub fn linearize(&self) -> Self {
        let StringData::Rope(rope) = self.0.data() else {
            return self.clone();
        };
        if let RopeState::Linearized { flat } = &*rope.state.borrow() {
            return flat.clone();
        }
        let flattened = if rope.is_wide {
            Self(StringRepr::allocate(StringData::Utf16(
                self.utf16_units().collect::<Vec<_>>(),
            )))
        } else {
            let units = self
                .utf16_units()
                .map(|unit| {
                    u8::try_from(unit).expect("a narrow String rope contained a wide code unit")
                })
                .collect::<Vec<_>>();
            Self(StringRepr::allocate(StringData::Latin1(units)))
        };
        *rope.state.borrow_mut() = RopeState::Linearized {
            flat: flattened.clone(),
        };
        flattened
    }

    /// Copy one validated UTF-16 code-unit range with QuickJS
    /// `js_sub_string` representation rules. Ropes are linearized once, a
    /// full-range request reuses that flat handle, Latin-1 ranges copy their
    /// bytes directly, and UTF-16 ranges scan only the selected units before
    /// choosing a single Latin-1 or UTF-16 allocation.
    #[must_use]
    pub fn sub_string(&self, start: usize, end: usize) -> Self {
        let source = self.linearize();
        assert!(start <= end, "String subrange start exceeded end");
        assert!(
            end <= source.len(),
            "String subrange exceeded source length"
        );
        if start == 0 && end == source.len() {
            return source;
        }
        match source.0.data() {
            StringData::Latin1(units) => Self(StringRepr::allocate(StringData::Latin1(
                units[start..end].to_vec(),
            ))),
            StringData::Utf16(units) => {
                let selected = &units[start..end];
                if selected.iter().all(|unit| *unit <= u16::from(u8::MAX)) {
                    let narrow = selected.iter().map(|unit| *unit as u8).collect::<Vec<_>>();
                    Self(StringRepr::allocate(StringData::Latin1(narrow)))
                } else {
                    Self(StringRepr::allocate(StringData::Utf16(selected.to_vec())))
                }
            }
            StringData::Rope(_) => unreachable!("linearized String remained a rope"),
        }
    }

    /// Repeat one linearized String into the single flat allocation produced by
    /// QuickJS `js_string_repeat`. The checked product uses the caller-supplied
    /// limit so native white-box tests can exercise the length failure without
    /// attempting a near-gigabyte allocation.
    pub fn repeat_with_limit(&self, count: usize, max_len: usize) -> Result<Self, JsStringError> {
        let source = self.linearize();
        // QuickJS returns the already-converted receiver before checking the
        // product. Count conversion and validation happen outside this kernel.
        if source.is_empty() || count == 1 {
            return Ok(source);
        }
        let output_len = source
            .len()
            .checked_mul(count)
            .filter(|length| *length <= max_len.min(Self::MAX_LEN))
            .ok_or(JsStringError::TooLong)?;
        if output_len == 0 {
            // `string_buffer_end` releases the temporary zero-length buffer
            // and returns QuickJS's canonical narrow empty String.
            return Ok(Self::from_static(""));
        }

        fn grow_repetition<T: Copy>(
            source: &[T],
            output_len: usize,
        ) -> Result<Vec<T>, JsStringError> {
            // `string_buffer_init2` performs one exact fallible allocation.
            // Reserve the complete buffer up front so later doubling copies
            // cannot allocate and allocator failure remains catchable.
            #[cfg(test)]
            if FAIL_NEXT_REPEAT_RESERVATION.with(|armed| armed.replace(false)) {
                return Err(JsStringError::OutOfMemory);
            }
            let mut output = Vec::new();
            output
                .try_reserve_exact(output_len)
                .map_err(|_| JsStringError::OutOfMemory)?;
            if source.len() == 1 {
                output.resize(output_len, source[0]);
                return Ok(output);
            }
            output.extend_from_slice(source);
            while output.len() < output_len {
                let copied = output.len().min(output_len - output.len());
                output.extend_from_within(..copied);
            }
            Ok(output)
        }

        Ok(match source.0.data() {
            StringData::Latin1(units) => Self(StringRepr::allocate(StringData::Latin1(
                grow_repetition(units, output_len)?,
            ))),
            StringData::Utf16(units) => Self(StringRepr::allocate(StringData::Utf16(
                grow_repetition(units, output_len)?,
            ))),
            StringData::Rope(_) => unreachable!("linearized String remained a rope"),
        })
    }

    /// Build the one flat result of pinned QuickJS `js_string_pad` while
    /// retaining its narrow-first `StringBuffer` behavior. A wide code unit
    /// triggers one fallible widening reservation at the point it is copied;
    /// unused wide units in a truncated filler therefore do not widen the
    /// result.
    pub fn pad_with_limit(
        &self,
        target_len: usize,
        filler: Option<&Self>,
        pad_at_end: bool,
        max_len: usize,
    ) -> Result<Self, JsStringError> {
        let source = self.linearize();
        if source.len() >= target_len {
            return Ok(source);
        }
        let filler = filler.map(Self::linearize);
        if filler.as_ref().is_some_and(Self::is_empty) {
            return Ok(source);
        }
        if target_len > max_len.min(Self::MAX_LEN) {
            return Err(JsStringError::TooLong);
        }

        fn reserve_pad_buffer<T>(capacity: usize) -> Result<Vec<T>, JsStringError> {
            #[cfg(test)]
            if FAIL_NEXT_PAD_RESERVATION.with(|armed| armed.replace(false)) {
                return Err(JsStringError::OutOfMemory);
            }
            let mut output = Vec::new();
            output
                .try_reserve_exact(capacity)
                .map_err(|_| JsStringError::OutOfMemory)?;
            Ok(output)
        }

        enum PadBuffer {
            Latin1(Vec<u8>),
            Utf16(Vec<u16>),
        }

        fn push_pad_unit(
            buffer: &mut PadBuffer,
            unit: u16,
            target_len: usize,
        ) -> Result<(), JsStringError> {
            match buffer {
                PadBuffer::Latin1(units) if unit <= u16::from(u8::MAX) => {
                    units.push(unit as u8);
                }
                PadBuffer::Latin1(units) => {
                    // QuickJS first allocates a complete narrow buffer, then
                    // fallibly widens it when the first copied unit needs 16
                    // bits. Preserve both recoverable allocation points.
                    let mut wide = reserve_pad_buffer(target_len)?;
                    wide.extend(units.iter().map(|unit| u16::from(*unit)));
                    wide.push(unit);
                    *buffer = PadBuffer::Utf16(wide);
                }
                PadBuffer::Utf16(units) => units.push(unit),
            }
            Ok(())
        }

        fn append_source(
            buffer: &mut PadBuffer,
            source: &JsString,
            target_len: usize,
        ) -> Result<(), JsStringError> {
            for unit in source.utf16_units() {
                push_pad_unit(buffer, unit, target_len)?;
            }
            Ok(())
        }

        fn append_padding(
            buffer: &mut PadBuffer,
            filler: Option<&JsString>,
            padding_len: usize,
            target_len: usize,
        ) -> Result<(), JsStringError> {
            let filler_len = filler.map_or(1, JsString::len);
            for index in 0..padding_len {
                let unit = match filler {
                    Some(filler) => filler
                        .code_unit_at(index % filler_len)
                        .expect("non-empty flat pad filler lost a code unit"),
                    None => u16::from(b' '),
                };
                push_pad_unit(buffer, unit, target_len)?;
            }
            Ok(())
        }

        // string_buffer_init(ctx, b, n) always starts narrow, even if either
        // input is wide. Subsequent writes widen only for copied code units.
        let mut output = PadBuffer::Latin1(reserve_pad_buffer(target_len)?);
        let padding_len = target_len - source.len();
        if pad_at_end {
            append_source(&mut output, &source, target_len)?;
            append_padding(&mut output, filler.as_ref(), padding_len, target_len)?;
        } else {
            append_padding(&mut output, filler.as_ref(), padding_len, target_len)?;
            append_source(&mut output, &source, target_len)?;
        }

        Ok(match output {
            PadBuffer::Latin1(units) => {
                debug_assert_eq!(units.len(), target_len);
                Self(StringRepr::allocate(StringData::Latin1(units)))
            }
            PadBuffer::Utf16(units) => {
                debug_assert_eq!(units.len(), target_len);
                Self(StringRepr::allocate(StringData::Utf16(units)))
            }
        })
    }

    /// Trim the selected ends with pinned QuickJS `js_string_trim` and
    /// `js_sub_string` representation rules. Full-range and empty results do
    /// not enter or consume the partial-substring reservation path; a partial
    /// wide range narrows when every retained UTF-16 code unit fits Latin-1.
    pub fn trim_whitespace(&self, trim_start: bool, trim_end: bool) -> Result<Self, JsStringError> {
        let source = self.linearize();
        let mut start = 0;
        let mut end = source.len();
        if trim_start {
            while start < end
                && source
                    .code_unit_at(start)
                    .is_some_and(is_ecmascript_whitespace)
            {
                start += 1;
            }
        }
        if trim_end {
            while end > start
                && source
                    .code_unit_at(end - 1)
                    .is_some_and(is_ecmascript_whitespace)
            {
                end -= 1;
            }
        }
        if start == 0 && end == source.len() {
            return Ok(source);
        }
        if start == end {
            // `js_new_string8_len(..., 0)` returns the canonical narrow empty
            // atom without performing the substring allocation.
            return Ok(Self::from_static(""));
        }

        fn reserve_trim_buffer<T>(capacity: usize) -> Result<Vec<T>, JsStringError> {
            #[cfg(test)]
            if FAIL_NEXT_TRIM_RESERVATION.with(|armed| armed.replace(false)) {
                return Err(JsStringError::OutOfMemory);
            }
            let mut selected = Vec::new();
            selected
                .try_reserve_exact(capacity)
                .map_err(|_| JsStringError::OutOfMemory)?;
            Ok(selected)
        }

        let selected_len = end - start;
        match source.0.data() {
            StringData::Latin1(units) => {
                let mut selected = reserve_trim_buffer(selected_len)?;
                selected.extend_from_slice(&units[start..end]);
                Ok(Self(StringRepr::allocate(StringData::Latin1(selected))))
            }
            StringData::Utf16(units) => {
                let range = &units[start..end];
                if range.iter().all(|unit| *unit <= u16::from(u8::MAX)) {
                    let mut selected = reserve_trim_buffer(selected_len)?;
                    selected.extend(range.iter().map(|unit| *unit as u8));
                    Ok(Self(StringRepr::allocate(StringData::Latin1(selected))))
                } else {
                    let mut selected = reserve_trim_buffer(selected_len)?;
                    selected.extend_from_slice(range);
                    Ok(Self(StringRepr::allocate(StringData::Utf16(selected))))
                }
            }
            StringData::Rope(_) => unreachable!("linearized String remained a rope"),
        }
    }

    /// Concatenate with QuickJS's short-flat/rope thresholds, bounded depth,
    /// Fibonacci rebalance and 30-bit length cap.
    ///
    /// # Errors
    /// Returns [`JsStringError::TooLong`] when the result would exceed
    /// [`Self::MAX_LEN`] UTF-16 code units.
    pub fn try_concat(&self, other: &Self) -> Result<Self, JsStringError> {
        Self::checked_length(self.len(), other.len())?;

        if other.is_flat() {
            if other.is_empty() {
                return Ok(self.clone());
            }
            if self.is_empty() && self.is_flat() {
                return Ok(other.clone());
            }
            if other.len() <= Self::ROPE_SHORT_LEN {
                if self.is_flat() {
                    if self.len() <= Self::ROPE_SHORT2_LEN {
                        return Ok(Self::flat_concat(self, other));
                    }
                    return Self::new_rope(self.clone(), other.clone());
                }
                if let StringData::Rope(rope) = self.0.data() {
                    let (left, right) = Self::rope_children(rope);
                    if right.is_flat() && right.len() <= Self::ROPE_SHORT_LEN {
                        let tail = Self::flat_concat(&right, other);
                        return Self::new_rope(left, tail);
                    }
                }
            }
        } else if self.is_flat() {
            if self.is_empty() {
                return Ok(other.clone());
            }
            if let StringData::Rope(rope) = other.0.data() {
                let (left, right) = Self::rope_children(rope);
                if left.is_flat() && left.len() <= Self::ROPE_SHORT_LEN {
                    let head = Self::flat_concat(self, &left);
                    return Self::new_rope(head, right);
                }
            }
        }
        Self::new_rope(self.clone(), other.clone())
    }

    /// Append to an exclusively owned compatible flat buffer. Shared strings
    /// and ropes retain the ordinary concatenation representation rules. The
    /// same short-flat thresholds apply even to an exclusively owned buffer.
    ///
    /// # Errors
    /// Rejects lengths above `MAX_LEN` and reports reservation failure.
    pub fn concat_owned(mut self, other: &Self) -> Result<Self, JsStringError> {
        Self::checked_length(self.len(), other.len())?;
        if other.is_empty() && other.is_flat() {
            return Ok(self);
        }
        if self.is_empty() && self.is_flat() {
            return Ok(other.clone());
        }
        if self.try_concat_in_place(other)? {
            return Ok(self);
        }
        self.try_concat(other)
    }

    /// Append only if this flat buffer has one strong owner and no weak owners.
    /// Reservation precedes every mutation: a failed allocation leaves the
    /// original local binding intact. A declined append likewise changes nothing.
    pub(crate) fn try_concat_in_place(&mut self, other: &Self) -> Result<bool, JsStringError> {
        fn reserve<T>(buffer: &mut Vec<T>, additional: usize) -> Result<(), JsStringError> {
            #[cfg(test)]
            if FAIL_NEXT_CONCAT_RESERVATION.with(|armed| armed.replace(false)) {
                return Err(JsStringError::OutOfMemory);
            }
            buffer
                .try_reserve(additional)
                .map_err(|_| JsStringError::OutOfMemory)
        }
        Self::checked_length(self.len(), other.len())?;
        // QuickJS exposes rope/flat distinctions in host value printing. Keep
        // its flat-concat admission even when we own spare buffer capacity.
        if self.len() > Self::ROPE_SHORT2_LEN || other.len() > Self::ROPE_SHORT_LEN {
            return Ok(false);
        }

        if let Some(repr) = Rc::get_mut(&mut self.0) {
            // Only this exclusive mutation can change code units. Shared rope
            // linearization preserves content and therefore preserves its hash.
            repr.content_hash.set(None);
            match (&mut repr.data, other.0.data()) {
                (StringData::Latin1(left), StringData::Latin1(right)) => {
                    reserve(left, right.len())?;
                    left.extend_from_slice(right);
                    return Ok(true);
                }
                (StringData::Utf16(left), StringData::Utf16(right)) => {
                    reserve(left, right.len())?;
                    left.extend_from_slice(right);
                    return Ok(true);
                }
                (StringData::Utf16(left), StringData::Latin1(right)) => {
                    reserve(left, right.len())?;
                    left.extend(right.iter().copied().map(u16::from));
                    return Ok(true);
                }
                _ => {}
            }
        }
        Ok(false)
    }

    /// Encode the byte slice exposed by QuickJS `JS_ToCStringLen2` when its
    /// `cesu8` flag is false.
    ///
    /// Valid surrogate pairs become standard four-byte UTF-8. Unpaired
    /// surrogates are deliberately retained as three-byte WTF-8 sequences.
    /// Embedded U+0000 is returned as an ordinary zero byte; the terminating C
    /// NUL owned by QuickJS is not part of its reported length and is therefore
    /// not included in this safe Rust byte vector. As in QuickJS, converting a
    /// rope caches its flat representation on the shared String identity.
    ///
    /// # Errors
    /// Returns [`TryReserveError`] if the output buffer cannot be reserved.
    pub fn try_to_wtf8_bytes(&self) -> Result<Vec<u8>, TryReserveError> {
        self.try_to_quickjs_utf8_bytes(false)
    }

    /// Append the WTF-8 bytes exposed by `JS_ToCStringLen2(..., false)` to an
    /// existing buffer while preserving the same rope-linearization side
    /// effect as [`Self::try_to_wtf8_bytes`].
    pub fn try_append_wtf8_bytes(&self, output: &mut Vec<u8>) -> Result<(), TryReserveError> {
        self.try_append_quickjs_utf8_bytes(output, false)
    }

    /// Encode the byte slice exposed by QuickJS `JS_ToCStringLen2` when its
    /// `cesu8` flag is true.
    ///
    /// Every UTF-16 code unit is encoded independently, so a valid surrogate
    /// pair occupies two three-byte CESU-8 sequences. Embedded U+0000 is
    /// retained and no trailing C NUL is included in the returned vector. Rope
    /// conversion has the same shared flat-cache transition as the WTF-8 path.
    ///
    /// # Errors
    /// Returns [`TryReserveError`] if the output buffer cannot be reserved.
    pub fn try_to_cesu8_bytes(&self) -> Result<Vec<u8>, TryReserveError> {
        self.try_to_quickjs_utf8_bytes(true)
    }

    fn try_to_quickjs_utf8_bytes(&self, cesu8: bool) -> Result<Vec<u8>, TryReserveError> {
        let mut output = Vec::new();
        self.try_append_quickjs_utf8_bytes(&mut output, cesu8)?;
        Ok(output)
    }

    fn try_append_quickjs_utf8_bytes(
        &self,
        output: &mut Vec<u8>,
        cesu8: bool,
    ) -> Result<(), TryReserveError> {
        // JS_ToCStringLen2 first rewrites a rope to its cached flat form. The
        // root keeps its rope identity, but later diagnostics observe one
        // logical leaf instead of the former tree boundary.
        let value = self.linearize();
        let encoded_len = quickjs_utf8_length(value.utf16_units(), cesu8);
        let initial_len = output.len();
        output.try_reserve_exact(encoded_len)?;
        output.extend(QuickJsUtf8Bytes::new(&value, cesu8));
        debug_assert_eq!(output.len(), initial_len + encoded_len);
        Ok(())
    }

    pub fn wtf8_bytes(&self) -> impl Iterator<Item = u8> {
        QuickJsUtf8Bytes::new(self, false)
    }

    pub fn push_c_string_to(&self, output: &mut NativeErrorMessage) {
        output.push_c_string_bytes(self.wtf8_bytes());
    }

    /// Reproduce `JS_AtomGetStr(..., char buf[64], 64)` as consumed by a C
    /// `%s` native-error argument. Narrow all-ASCII atoms bypass the scratch
    /// buffer; every other text atom is encoded one UTF-16 code unit at a time
    /// and stops before starting a unit once 58 bytes have already been written.
    pub fn push_atom_get_str_to(&self, output: &mut NativeErrorMessage) {
        let atom = self.linearize();
        if !atom.is_wide() && atom.utf16_units().all(|unit| unit < 0x80) {
            atom.push_c_string_to(output);
            return;
        }

        const ATOM_BUFFER_SIZE: usize = 64;
        const UTF8_CHAR_LEN_MAX: usize = 6;
        let mut scratch = [0_u8; ATOM_BUFFER_SIZE];
        let mut len = 0;
        for unit in atom.utf16_units() {
            if len >= ATOM_BUFFER_SIZE - UTF8_CHAR_LEN_MAX {
                break;
            }
            let mut encoded = [0_u8; 4];
            let encoded_len = encode_quickjs_utf8(&mut encoded, u32::from(unit));
            scratch[len..len + encoded_len].copy_from_slice(&encoded[..encoded_len]);
            len += encoded_len;
        }
        output.push_c_string_bytes(scratch[..len].iter().copied());
    }

    /// Lossy conversion is suitable for terminal diagnostics. It must not be
    /// used for language-level string comparison or indexing.
    #[must_use]
    pub fn to_utf8_lossy(&self) -> String {
        match self.0.data() {
            StringData::Latin1(units) if units.is_ascii() => {
                String::from_utf8(units.clone()).expect("ASCII is UTF-8")
            }
            StringData::Latin1(units) => units.iter().map(|&unit| char::from(unit)).collect(),
            StringData::Utf16(units) => String::from_utf16_lossy(units),
            StringData::Rope(_) => char::decode_utf16(self.utf16_units())
                .map(|result| result.unwrap_or(char::REPLACEMENT_CHARACTER))
                .collect(),
        }
    }
}

pub fn decode_quickjs_utf8(value: &[u8]) -> Option<(u32, usize)> {
    let first = *value.first()?;
    if first < 0x80 {
        return Some((u32::from(first), 1));
    }

    let (continuation_count, mask, minimum): (usize, u8, u32) = match first {
        0x00..=0x7f => unreachable!("ASCII byte escaped the fast path"),
        0xc0..=0xdf => (1, 0x1f, 0x80),
        0xe0..=0xef => (2, 0x0f, 0x800),
        0xf0..=0xf7 => (3, 0x07, 0x1_0000),
        0xf8..=0xfb => (4, 0x03, 0x20_0000),
        0xfc..=0xfd => (5, 0x01, 0x400_0000),
        0x80..=0xbf | 0xfe..=0xff => return None,
    };
    if value.len() <= continuation_count {
        return None;
    }

    let mut code_point = u32::from(first & mask);
    for &byte in &value[1..=continuation_count] {
        if !(0x80..=0xbf).contains(&byte) {
            return None;
        }
        code_point = (code_point << 6) | u32::from(byte & 0x3f);
    }
    (code_point >= minimum).then_some((code_point, continuation_count + 1))
}

fn skip_quickjs_invalid_utf8(value: &[u8], mut index: usize) -> usize {
    while value
        .get(index)
        .is_some_and(|byte| (0x80..=0xbf).contains(byte))
    {
        index += 1;
    }
    if index < value.len() {
        index += 1;
        while value
            .get(index)
            .is_some_and(|byte| (0x80..=0xbf).contains(byte))
        {
            index += 1;
        }
    }
    index
}

fn quickjs_utf8_length(units: impl Iterator<Item = u16>, cesu8: bool) -> usize {
    let mut units = units.peekable();
    let mut length = 0;
    while let Some(unit) = units.next() {
        if !cesu8
            && (0xd800..=0xdbff).contains(&unit)
            && units
                .peek()
                .is_some_and(|next| (0xdc00..=0xdfff).contains(next))
        {
            units.next();
            length += 4;
        } else {
            length += match unit {
                0x0000..=0x007f => 1,
                0x0080..=0x07ff => 2,
                0x0800..=0xffff => 3,
            };
        }
    }
    length
}

fn encode_quickjs_utf8(output: &mut [u8; 4], code_point: u32) -> usize {
    match code_point {
        0x0000..=0x007f => {
            output[0] = code_point as u8;
            1
        }
        0x0080..=0x07ff => {
            output[0] = (0xc0 | (code_point >> 6)) as u8;
            output[1] = (0x80 | (code_point & 0x3f)) as u8;
            2
        }
        0x0800..=0xffff => {
            output[0] = (0xe0 | (code_point >> 12)) as u8;
            output[1] = (0x80 | ((code_point >> 6) & 0x3f)) as u8;
            output[2] = (0x80 | (code_point & 0x3f)) as u8;
            3
        }
        0x1_0000..=0x10_ffff => {
            output[0] = (0xf0 | (code_point >> 18)) as u8;
            output[1] = (0x80 | ((code_point >> 12) & 0x3f)) as u8;
            output[2] = (0x80 | ((code_point >> 6) & 0x3f)) as u8;
            output[3] = (0x80 | (code_point & 0x3f)) as u8;
            4
        }
        _ => unreachable!("QuickJS byte encoder received an invalid code point"),
    }
}

impl PartialEq for JsString {
    fn eq(&self, other: &Self) -> bool {
        if Rc::ptr_eq(&self.0, &other.0) {
            return true;
        }
        if self.len() != other.len() {
            return false;
        }
        // Flat strings need neither owned traversal handles nor a rope stack.
        // Width is a representation choice, so mixed-width equality compares
        // exact code units rather than treating Latin-1 as UTF-8.
        match (self.0.data(), other.0.data()) {
            (StringData::Latin1(left), StringData::Latin1(right)) => left == right,
            (StringData::Utf16(left), StringData::Utf16(right)) => left == right,
            (StringData::Latin1(narrow), StringData::Utf16(wide))
            | (StringData::Utf16(wide), StringData::Latin1(narrow)) => narrow
                .iter()
                .copied()
                .map(u16::from)
                .eq(wide.iter().copied()),
            _ => self.utf16_units().eq(other.utf16_units()),
        }
    }
}

impl Eq for JsString {}

impl Hash for JsString {
    fn hash<H: Hasher>(&self, state: &mut H) {
        state.write_u32(self.content_hash());
    }
}

impl fmt::Debug for JsString {
    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
        formatter
            .debug_tuple("JsString")
            .field(&self.to_utf8_lossy())
            .finish()
    }
}

impl fmt::Display for JsString {
    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
        formatter.write_str(&self.to_utf8_lossy())
    }
}

/// A constant-pool value of the front end. Numbers follow `QuickJS`'s split
/// between immediate 32-bit integers and IEEE-754 doubles.
#[derive(Clone, Debug)]
pub enum PrimitiveValue {
    Int(i32),
    Float(f64),
    BigInt(JsBigInt),
    String(JsString),
}

impl PrimitiveValue {
    #[must_use]
    pub fn number(value: f64) -> Self {
        if value == f64::from(value as i32) && !(value == 0.0 && value.is_sign_negative()) {
            Self::Int(value as i32)
        } else {
            Self::Float(value)
        }
    }

    #[must_use]
    pub const fn as_number(&self) -> Option<f64> {
        match self {
            Self::Int(value) => Some(*value as f64),
            Self::Float(value) => Some(*value),
            _ => None,
        }
    }

    /// Apply ECMAScript `ToString` to the value kinds implemented by the
    /// runtime kernel.
    ///
    /// # Errors
    /// Symbol conversion throws, an extended-limit BigInt can fail the pinned
    /// QuickJS decimal-conversion allocation guard, and object conversion must
    /// be routed through a context so `ToPrimitive` can execute user code.
    pub fn to_js_string(&self) -> Result<JsString, Error> {
        let text = match self {
            Self::Int(value) => value.to_string(),
            Self::Float(value) => number_to_string(*value),
            Self::BigInt(value) => {
                if value.exceeds_allocation_limit() {
                    return Err(Error::new(
                        ErrorKind::Range,
                        "BigInt is too large to allocate",
                    ));
                }
                value.to_string()
            }
            Self::String(value) => return Ok(value.linearize()),
        };
        Ok(JsString::try_from_utf8(&text)?)
    }

    #[must_use]
    #[expect(clippy::float_cmp)]
    pub fn strict_equal(&self, other: &Self) -> bool {
        match (self, other) {
            (Self::String(left), Self::String(right)) => left == right,
            (Self::BigInt(left), Self::BigInt(right)) => left == right,
            (Self::Int(left), Self::Int(right)) => left == right,
            (left, right) => match (left.as_number(), right.as_number()) {
                (Some(left), Some(right)) => left == right,
                _ => false,
            },
        }
    }
}

/// `Number::toString(value, 10)`, as the core prints it.
#[must_use]
pub fn number_to_string(value: f64) -> String {
    oxide_core::vm::radix::to_string_radix(value, 10)
}

pub(super) const fn is_ecmascript_whitespace(unit: u16) -> bool {
    matches!(
        unit,
        0x0009..=0x000d
            | 0x0020
            | 0x00a0
            | 0x1680
            | 0x2000..=0x200a
            | 0x2028
            | 0x2029
            | 0x202f
            | 0x205f
            | 0x3000
            | 0xfeff
    )
}

impl PartialEq for PrimitiveValue {
    fn eq(&self, other: &Self) -> bool {
        self.strict_equal(other)
    }
}

#[cfg(test)]
mod tests {
    use std::cell::Cell;
    use std::collections::hash_map::DefaultHasher;

    use std::hash::{Hash, Hasher};
    use std::rc::Rc;

    use super::{
        JsString, JsStringError, PrimitiveValue, StringData, StringRepr, number_to_string,
    };
    use crate::engine::api::error::{Error, ErrorKind};

    #[test]
    fn cached_content_hash_survives_linearization_and_invalidates_on_append() {
        let mut flat = JsString::from_static("abc");
        assert_eq!(
            flat.content_hash(),
            JsString::from_static("abc").content_hash()
        );
        assert!(flat.0.content_hash.get().is_some());
        assert!(
            flat.try_concat_in_place(&JsString::from_static("def"))
                .unwrap()
        );
        assert_eq!(
            flat.content_hash(),
            JsString::from_static("abcdef").content_hash()
        );
        let left = JsString::from_owned_latin1(vec![b'a'; 1024]);
        let right = JsString::from_owned_utf16(vec![0xd800; 1024]);
        let rope = left.try_concat(&right).unwrap();
        let hash = rope.content_hash();
        let flat = rope.linearize();
        assert_eq!(flat.content_hash(), hash);
        assert_eq!(rope.content_hash(), hash);
        assert!(rope.same_representation(&rope.clone()));
    }

    #[test]
    fn utf16_nth_skips_leaf_and_rope_ranges() {
        let left = JsString::from_owned_latin1(vec![b'a'; 1024]);
        let right = JsString::from_owned_utf16(vec![0x1234; 1024]);
        let joined = left.try_concat(&right).unwrap();
        assert!(matches!(joined.0.data(), StringData::Rope(_)));
        for source in [&left, &joined] {
            let expected = source.utf16_units().collect::<Vec<_>>();
            for skip in 0..=expected.len() + 1 {
                let mut iter = source.utf16_units();
                assert_eq!(iter.nth(skip), expected.get(skip).copied());
                assert_eq!(
                    iter.collect::<Vec<_>>(),
                    expected.get(skip.saturating_add(1)..).unwrap_or(&[])
                );
            }
        }
    }

    #[test]
    fn owned_concat_preserves_aliases_width_identity_and_rope_content() {
        let empty = JsString::from_static("");
        let narrow = JsString::from_owned_latin1(b"abc".to_vec());
        assert!(Rc::ptr_eq(&empty.try_concat(&narrow).unwrap().0, &narrow.0));
        assert!(Rc::ptr_eq(&narrow.try_concat(&empty).unwrap().0, &narrow.0));
        let identity = Rc::as_ptr(&narrow.0);
        let grown = narrow.concat_owned(&JsString::from_static("d")).unwrap();
        assert_eq!(Rc::as_ptr(&grown.0), identity);
        assert_eq!(grown.to_string(), "abcd");
        let alias = grown.clone();
        let copied = grown.concat_owned(&JsString::from_static("e")).unwrap();
        assert_eq!(alias.to_string(), "abcd");
        assert_eq!(copied.to_string(), "abcde");
        assert!(!Rc::ptr_eq(&alias.0, &copied.0));
        let wide = JsString::from_owned_utf16(vec![0xd800]);
        let identity = Rc::as_ptr(&wide.0);
        let wide = wide.concat_owned(&JsString::from_static("x")).unwrap();
        assert_eq!(Rc::as_ptr(&wide.0), identity);
        assert_eq!(wide.utf16_units().collect::<Vec<_>>(), vec![0xd800, 120]);
        let wide = wide
            .concat_owned(&JsString::from_owned_utf16(vec![0xdc00]))
            .unwrap();
        assert_eq!(Rc::as_ptr(&wide.0), identity);
        let widened = JsString::from_static("a").concat_owned(&wide).unwrap();
        assert_eq!(
            widened.utf16_units().collect::<Vec<_>>(),
            vec![97, 0xd800, 120, 0xdc00]
        );
        let weak_owner = JsString::from_owned_latin1(b"weak".to_vec());
        let weak = Rc::downgrade(&weak_owner.0);
        let identity = Rc::as_ptr(&weak_owner.0);
        let weak_result = weak_owner
            .concat_owned(&JsString::from_static("!"))
            .unwrap();
        assert_ne!(Rc::as_ptr(&weak_result.0), identity);
        assert!(weak.upgrade().is_none());
        let suffix = JsString::from_static("xy");
        let mut owned = JsString::from_owned_latin1(b"start".to_vec());
        let identity = Rc::as_ptr(&owned.0);
        let mut reference = owned.clone();
        // Release the temporary alias before beginning owned append.
        reference = reference.try_concat(&suffix).unwrap();
        owned = owned.concat_owned(&suffix).unwrap();
        for _ in 0..100 {
            owned = owned.concat_owned(&suffix).unwrap();
            reference = reference.try_concat(&suffix).unwrap();
        }
        assert_eq!(Rc::as_ptr(&owned.0), identity);
        assert!(owned.is_flat());
        for _ in 0..8900 {
            owned = owned.concat_owned(&suffix).unwrap();
            reference = reference.try_concat(&suffix).unwrap();
        }
        assert_eq!(owned, reference);
        assert_eq!(content_hash(&owned), content_hash(&reference));
        assert!(!owned.is_flat());
        assert!(!reference.is_flat());
        let rope = reference.clone();
        let appended = reference.concat_owned(&suffix).unwrap();
        assert_eq!(appended, rope.try_concat(&suffix).unwrap());
    }

    #[test]
    fn owned_concat_preserves_observable_flat_rope_thresholds() {
        for (left_len, right_len, expected_in_place) in [
            (600, 600, false),
            (8192, 512, true),
            (8193, 512, false),
            (8192, 513, false),
        ] {
            let mut left = JsString::from_owned_latin1(vec![b'a'; left_len]);
            let right = JsString::from_owned_latin1(vec![b'b'; right_len]);
            let identity = Rc::as_ptr(&left.0);
            assert_eq!(left.try_concat_in_place(&right).unwrap(), expected_in_place);
            assert_eq!(Rc::as_ptr(&left.0), identity);
            if expected_in_place {
                assert!(left.is_flat());
                assert_eq!(left.len(), left_len + right_len);
            } else {
                assert_eq!(left.len(), left_len);
                let result = left.concat_owned(&right).unwrap();
                assert!(!result.is_flat());
                assert_eq!(result.len(), left_len + right_len);
            }
        }
    }

    fn content_hash(value: &JsString) -> u64 {
        let mut state = DefaultHasher::new();
        value.hash(&mut state);
        state.finish()
    }

    #[test]
    fn tagged_atom_decimal_strings_are_narrow_and_fresh() {
        for (value, expected) in [(0, "0"), (42, "42"), (2_147_483_647, "2147483647")] {
            let left = JsString::from_fresh_decimal_u32(value);
            let right = JsString::from_fresh_decimal_u32(value);
            assert_eq!(left, JsString::from_static(expected));
            assert_eq!(right, left);
            assert!(!left.same_representation(&right));
            assert!(matches!(left.0.data(), StringData::Latin1(_)));
        }
    }

    #[test]
    fn checked_string_construction_rejects_limits_and_hostile_hints_without_overpolling() {
        struct PanicOnPoll;
        impl Iterator for PanicOnPoll {
            type Item = u16;

            fn next(&mut self) -> Option<Self::Item> {
                panic!("oversized lower bound should reject before polling")
            }

            fn size_hint(&self) -> (usize, Option<usize>) {
                (usize::MAX, None)
            }
        }

        struct LateOverflow {
            polls: Rc<Cell<usize>>,
        }
        impl Iterator for LateOverflow {
            type Item = u16;

            fn next(&mut self) -> Option<Self::Item> {
                let poll = self.polls.get();
                self.polls.set(poll + 1);
                match poll {
                    0..=3 => Some(u16::from(b'a') + u16::try_from(poll).unwrap()),
                    _ => panic!("constructor polled after observing the overflowing unit"),
                }
            }

            fn size_hint(&self) -> (usize, Option<usize>) {
                (0, Some(0))
            }
        }

        assert_eq!(JsString::MAX_LEN, 0x3fff_ffff);
        assert_eq!(
            JsString::checked_length(JsString::MAX_LEN - 1, 1),
            Ok(JsString::MAX_LEN)
        );
        assert_eq!(
            JsString::checked_length(JsString::MAX_LEN, 1),
            Err(JsStringError::TooLong)
        );
        assert_eq!(
            JsString::checked_length(usize::MAX, 1),
            Err(JsStringError::TooLong)
        );
        assert_eq!(
            JsString::try_from_utf16_with_limit(PanicOnPoll, 3),
            Err(JsStringError::TooLong)
        );

        struct UpperHint<I>(I);
        impl<I: Iterator<Item = u16>> Iterator for UpperHint<I> {
            type Item = u16;

            fn next(&mut self) -> Option<Self::Item> {
                self.0.next()
            }

            fn size_hint(&self) -> (usize, Option<usize>) {
                (0, Some(usize::MAX))
            }
        }
        let accepted =
            JsString::try_from_utf16_with_limit(UpperHint([0x61, 0x100].into_iter()), 2).unwrap();
        assert_eq!(accepted.utf16_units().collect::<Vec<_>>(), [0x61, 0x100]);

        let polls = Rc::new(Cell::new(0));
        assert_eq!(
            JsString::try_from_utf16_with_limit(
                LateOverflow {
                    polls: polls.clone(),
                },
                3,
            ),
            Err(JsStringError::TooLong)
        );
        assert_eq!(polls.get(), 4);
        assert!(JsString::try_from_utf16_with_limit([], 0).is_ok());
        assert_eq!(
            JsString::try_from_utf16_with_limit([0x61], 0),
            Err(JsStringError::TooLong)
        );
        assert!(JsString::try_from_utf16_with_limit("😀".encode_utf16(), 2).is_ok());
        assert_eq!(
            JsString::try_from_utf16_with_limit("😀".encode_utf16(), 1),
            Err(JsStringError::TooLong)
        );
    }

    #[test]
    fn quickjs_byte_constructor_preserves_wtf8_and_invalid_skip_rules() {
        let cases = [
            (vec![], vec![]),
            (vec![0x00, 0x41], vec![0x0000, 0x0041]),
            (vec![0xc3, 0xa9], vec![0x00e9]),
            (vec![0xc4, 0x80], vec![0x0100]),
            (vec![0xed, 0xa0, 0x80], vec![0xd800]),
            (vec![0xed, 0xb0, 0x80], vec![0xdc00]),
            (vec![0xf0, 0x9f, 0x98, 0x80], vec![0xd83d, 0xde00]),
            (vec![0x80, 0x41], vec![0xfffd]),
            (vec![0xff, 0x41], vec![0xfffd, 0x0041]),
            (vec![0x80, 0x80, 0x41, 0x80, 0x42], vec![0xfffd, 0x0042]),
            (vec![0x80, 0xc2, 0xa2], vec![0xfffd]),
            (vec![0xe2, 0x28, 0xa1], vec![0xfffd, 0x0028, 0xfffd]),
            (vec![0xe2, 0x82], vec![0xfffd]),
            (vec![0xc0, 0x80, 0x41], vec![0xfffd, 0x0041]),
            (vec![0xf4, 0x90, 0x80, 0x80, 0x41], vec![0xfffd, 0x0041]),
            (vec![0xfe, 0x80, 0x41], vec![0xfffd, 0x0041]),
            (vec![0xf8, 0x88, 0x80, 0x80, 0x80], vec![0xfffd]),
        ];
        for (bytes, expected) in cases {
            let actual = JsString::try_from_bytes(&bytes).unwrap();
            assert_eq!(
                actual.utf16_units().collect::<Vec<_>>(),
                expected,
                "{bytes:02x?}"
            );
        }

        assert!(JsString::try_from_bytes_with_limit(&[0xf0, 0x9f, 0x98, 0x80], 2).is_ok());
        assert_eq!(
            JsString::try_from_bytes_with_limit(&[0xf0, 0x9f, 0x98, 0x80], 1),
            Err(JsStringError::TooLong)
        );
        assert!(JsString::try_from_bytes_with_limit(&[0x80, 0x41], 1).is_ok());
        assert_eq!(
            JsString::try_from_bytes_with_limit(&[0xff, 0x41], 1),
            Err(JsStringError::TooLong)
        );
        assert_eq!(
            JsString::try_from_bytes_with_limit(b"ab", 1),
            Err(JsStringError::TooLong)
        );
    }

    #[test]
    fn quickjs_byte_exports_distinguish_wtf8_from_cesu8_and_roundtrip_utf16() {
        let value = JsString::try_from_utf16([
            0x0041, 0x0000, 0x00e9, 0x0800, 0xd800, 0x0042, 0xdc00, 0xd83d, 0xde00,
        ])
        .unwrap();
        assert_eq!(
            value.try_to_wtf8_bytes().unwrap(),
            [
                0x41, 0x00, 0xc3, 0xa9, 0xe0, 0xa0, 0x80, 0xed, 0xa0, 0x80, 0x42, 0xed, 0xb0, 0x80,
                0xf0, 0x9f, 0x98, 0x80,
            ]
        );
        assert_eq!(
            value.try_to_cesu8_bytes().unwrap(),
            [
                0x41, 0x00, 0xc3, 0xa9, 0xe0, 0xa0, 0x80, 0xed, 0xa0, 0x80, 0x42, 0xed, 0xb0, 0x80,
                0xed, 0xa0, 0xbd, 0xed, 0xb8, 0x80,
            ]
        );

        let all_units = JsString::try_from_utf16(0_u16..=u16::MAX).unwrap();
        for bytes in [
            all_units.try_to_wtf8_bytes().unwrap(),
            all_units.try_to_cesu8_bytes().unwrap(),
        ] {
            assert_eq!(JsString::try_from_bytes(&bytes).unwrap(), all_units);
        }

        let high = JsString::try_from_utf16([0xd83d]).unwrap();
        let low_and_tail = JsString::try_from_utf16(
            [0xde00]
                .into_iter()
                .chain(std::iter::repeat_n(u16::from(b'b'), 512)),
        )
        .unwrap();
        let across_leaf = JsString::try_from_utf8(&"a".repeat(8193))
            .unwrap()
            .try_concat(&high)
            .unwrap()
            .try_concat(&low_and_tail)
            .unwrap();
        assert!(!across_leaf.is_flat());
        let mut leaf_lengths = Vec::new();
        across_leaf.for_each_flat_leaf(|leaf| leaf_lengths.push(leaf.len()));
        assert_eq!(leaf_lengths, [8193, 1, 513]);
        let flat = JsString::try_from_utf16(
            std::iter::repeat_n(u16::from(b'a'), 8193)
                .chain([0xd83d, 0xde00])
                .chain(std::iter::repeat_n(u16::from(b'b'), 512)),
        )
        .unwrap();
        let wtf8 = across_leaf.try_to_wtf8_bytes().unwrap();
        assert_eq!(&wtf8[8193..8197], &[0xf0, 0x9f, 0x98, 0x80]);
        assert_eq!(wtf8, flat.try_to_wtf8_bytes().unwrap());
        leaf_lengths.clear();
        across_leaf.for_each_flat_leaf(|leaf| leaf_lengths.push(leaf.len()));
        assert_eq!(leaf_lengths, [across_leaf.len()]);
        let cesu8 = across_leaf.try_to_cesu8_bytes().unwrap();
        assert_eq!(&cesu8[8193..8199], &[0xed, 0xa0, 0xbd, 0xed, 0xb8, 0x80]);
        assert_eq!(cesu8, flat.try_to_cesu8_bytes().unwrap());
    }

    #[test]
    fn string_length_counts_utf16_code_units() {
        let text = JsString::from_static("a🚀");
        assert_eq!(text.len(), 3);
        assert_eq!(
            text.utf16_units().collect::<Vec<_>>(),
            vec![0x61, 0xd83d, 0xde80]
        );
    }

    #[test]
    fn strings_preserve_lone_surrogates() {
        let text = JsString::try_from_utf16([0xd800, 0x61]).unwrap();
        assert_eq!(text.utf16_units().collect::<Vec<_>>(), vec![0xd800, 0x61]);
        assert_eq!(text.to_utf8_lossy(), "�a");
    }

    #[test]
    fn utf16_index_code_point_and_well_formed_helpers_preserve_quickjs_rules() {
        let text = JsString::try_from_utf16([0x41, 0xd83d, 0xde80, 0xd800, 0x42, 0xdc00]).unwrap();
        assert_eq!(text.code_unit_at(0), Some(0x41));
        assert_eq!(text.code_unit_at(6), None);
        assert_eq!(text.code_point_at(1), Some(0x1f680));
        assert_eq!(text.code_point_at(2), Some(0xde80));
        assert_eq!(text.code_point_at(3), Some(0xd800));
        assert!(!text.is_well_formed());
        assert_eq!(
            text.to_well_formed().utf16_units().collect::<Vec<_>>(),
            vec![0x41, 0xd83d, 0xde80, 0xfffd, 0x42, 0xfffd]
        );

        let well_formed = JsString::try_from_utf16([0xd83d, 0xde80]).unwrap();
        assert!(well_formed.is_well_formed());
        assert_eq!(well_formed.to_well_formed(), well_formed);
    }

    #[test]
    fn sub_string_linearizes_ranges_and_preserves_quickjs_width_rules() {
        let latin1 = JsString::from_static("abcdef");
        let latin1_full = latin1.sub_string(0, latin1.len());
        assert!(latin1_full.same_representation(&latin1));
        let latin1_range = latin1.sub_string(1, 4);
        assert_eq!(latin1_range, JsString::from_static("bcd"));
        assert!(matches!(latin1_range.0.data(), StringData::Latin1(_)));

        let forced_wide = JsString(StringRepr::allocate(StringData::Utf16(
            [u16::from(b'a'), u16::from(b'b'), 0xd800, u16::from(b'c')]
                .into_iter()
                .collect::<Vec<_>>(),
        )));
        let narrowed = forced_wide.sub_string(0, 2);
        assert!(matches!(narrowed.0.data(), StringData::Latin1(_)));
        assert_eq!(
            narrowed.utf16_units().collect::<Vec<_>>(),
            [u16::from(b'a'), u16::from(b'b')]
        );
        let still_wide = forced_wide.sub_string(1, 3);
        assert!(matches!(still_wide.0.data(), StringData::Utf16(_)));
        assert_eq!(
            still_wide.utf16_units().collect::<Vec<_>>(),
            [u16::from(b'b'), 0xd800]
        );
        let empty = forced_wide.sub_string(2, 2);
        assert!(matches!(empty.0.data(), StringData::Latin1(units) if units.is_empty()));

        let left = JsString::try_from_utf8(&"x".repeat(8_193)).unwrap();
        let right = JsString::try_from_utf16([0xd83d, 0xde00, u16::from(b'z')]).unwrap();
        let rope = left.try_concat(&right).unwrap();
        assert!(!rope.is_flat());
        let full = rope.sub_string(0, rope.len());
        assert!(full.is_flat());
        assert_eq!(full, rope);
        let cached_flat = rope.linearize();
        assert!(full.same_representation(&cached_flat));
        let crossed = rope.sub_string(8_193, 8_196);
        assert!(crossed.is_flat());
        assert_eq!(
            crossed.utf16_units().collect::<Vec<_>>(),
            [0xd83d, 0xde00, u16::from(b'z')]
        );
    }

    #[test]
    fn repeat_builds_one_flat_string_with_quickjs_width_and_limit_rules() {
        let latin1 = JsString::from_static("ab");
        let repeated = latin1.repeat_with_limit(3, JsString::MAX_LEN).unwrap();
        assert_eq!(repeated, JsString::from_static("ababab"));
        assert!(repeated.is_flat());
        assert!(matches!(repeated.0.data(), StringData::Latin1(_)));
        let once = latin1.repeat_with_limit(1, 1).unwrap();
        assert!(once.same_representation(&latin1));

        let wide = JsString(StringRepr::allocate(StringData::Utf16(
            [u16::from(b'A'), 0xd800].into_iter().collect(),
        )));
        let repeated = wide.repeat_with_limit(3, JsString::MAX_LEN).unwrap();
        assert_eq!(
            repeated.utf16_units().collect::<Vec<_>>(),
            [0x41, 0xd800, 0x41, 0xd800, 0x41, 0xd800],
        );
        assert!(matches!(repeated.0.data(), StringData::Utf16(_)));
        let zero = wide.repeat_with_limit(0, JsString::MAX_LEN).unwrap();
        assert!(matches!(zero.0.data(), StringData::Latin1(units) if units.is_empty()));

        let left = JsString::try_from_utf8(&"x".repeat(8_193)).unwrap();
        let rope = left.try_concat(&JsString::from_static("yz")).unwrap();
        assert!(!rope.is_flat());
        let once = rope.repeat_with_limit(1, JsString::MAX_LEN).unwrap();
        assert!(once.is_flat());
        assert!(once.same_representation(&rope.linearize()));
        let twice = rope.repeat_with_limit(2, JsString::MAX_LEN).unwrap();
        assert!(twice.is_flat());
        assert_eq!(twice.len(), rope.len() * 2);
        assert_eq!(twice.code_unit_at(rope.len()), Some(u16::from(b'x')));

        let empty = JsString::from_static("");
        let empty_repeat = empty.repeat_with_limit(2_147_483_647, 0).unwrap();
        assert!(empty_repeat.same_representation(&empty));
        assert_eq!(latin1.repeat_with_limit(3, 5), Err(JsStringError::TooLong));

        super::fail_next_repeat_reservation_for_test();
        let forced_fast = latin1.repeat_with_limit(1, 1).unwrap();
        assert!(forced_fast.same_representation(&latin1));
        assert_eq!(
            latin1.repeat_with_limit(2, JsString::MAX_LEN),
            Err(JsStringError::OutOfMemory),
        );
        assert_eq!(
            latin1.repeat_with_limit(2, JsString::MAX_LEN).unwrap(),
            JsString::from_static("abab"),
            "the fail-next reservation hook was not consumed exactly once",
        );
    }

    #[test]
    fn pad_uses_quickjs_order_width_limit_and_recoverable_reservations() {
        let source = JsString::from_static("ab");
        let filler = JsString::from_static("xy");
        assert_eq!(
            source
                .pad_with_limit(7, Some(&filler), true, JsString::MAX_LEN)
                .unwrap(),
            JsString::from_static("abxyxyx"),
        );
        assert_eq!(
            source
                .pad_with_limit(7, Some(&filler), false, JsString::MAX_LEN)
                .unwrap(),
            JsString::from_static("xyxyxab"),
        );
        assert_eq!(
            source
                .pad_with_limit(4, None, true, JsString::MAX_LEN)
                .unwrap(),
            JsString::from_static("ab  "),
        );

        let forced_wide_filler = JsString(StringRepr::allocate(StringData::Utf16(
            [u16::from(b'z'), 0x100].into_iter().collect(),
        )));
        let narrow = source
            .pad_with_limit(3, Some(&forced_wide_filler), false, JsString::MAX_LEN)
            .unwrap();
        assert_eq!(narrow, JsString::from_static("zab"));
        assert!(matches!(narrow.0.data(), StringData::Latin1(_)));
        let wide = source
            .pad_with_limit(4, Some(&forced_wide_filler), false, JsString::MAX_LEN)
            .unwrap();
        assert_eq!(
            wide.utf16_units().collect::<Vec<_>>(),
            [u16::from(b'z'), 0x100, u16::from(b'a'), u16::from(b'b')],
        );
        assert!(matches!(wide.0.data(), StringData::Utf16(_)));

        let early = source
            .pad_with_limit(2, Some(&filler), true, 0)
            .expect("source-length early return must precede the cap");
        assert!(early.same_representation(&source));
        let empty = JsString::from_static("");
        let empty_filler = source
            .pad_with_limit(100, Some(&empty), false, 0)
            .expect("empty-filler early return must precede the cap");
        assert!(empty_filler.same_representation(&source));
        assert_eq!(
            source.pad_with_limit(3, Some(&filler), true, 2),
            Err(JsStringError::TooLong),
        );

        super::fail_next_pad_reservation_for_test();
        let forced_fast = source.pad_with_limit(2, Some(&filler), true, 0).unwrap();
        assert!(forced_fast.same_representation(&source));
        let forced_empty = source.pad_with_limit(100, Some(&empty), false, 0).unwrap();
        assert!(forced_empty.same_representation(&source));
        assert_eq!(
            source.pad_with_limit(3, Some(&filler), true, 2),
            Err(JsStringError::TooLong),
            "length rejection must precede and preserve the reservation hook",
        );
        assert_eq!(
            source.pad_with_limit(3, Some(&filler), true, JsString::MAX_LEN),
            Err(JsStringError::OutOfMemory),
        );
        assert_eq!(
            source
                .pad_with_limit(3, Some(&filler), true, JsString::MAX_LEN)
                .unwrap(),
            JsString::from_static("abx"),
            "the fail-next pad reservation hook was not consumed exactly once",
        );
    }

    #[test]
    fn trim_uses_quickjs_whitespace_width_identity_and_reservation_rules() {
        let whitespace = [
            0x0009, 0x000a, 0x000b, 0x000c, 0x000d, 0x0020, 0x00a0, 0x1680, 0x2000, 0x2001, 0x2002,
            0x2003, 0x2004, 0x2005, 0x2006, 0x2007, 0x2008, 0x2009, 0x200a, 0x2028, 0x2029, 0x202f,
            0x205f, 0x3000, 0xfeff,
        ];
        for unit in whitespace {
            let source = JsString::try_from_utf16([unit, u16::from(b'x'), unit]).unwrap();
            assert_eq!(
                source.trim_whitespace(true, true).unwrap(),
                JsString::from_static("x"),
                "U+{unit:04X} was not trimmed",
            );
        }
        for unit in [0x0000, 0x0085, 0x180e, 0x200b] {
            let source = JsString::try_from_utf16([unit, u16::from(b'x'), unit]).unwrap();
            assert!(
                source
                    .trim_whitespace(true, true)
                    .unwrap()
                    .same_representation(&source),
                "U+{unit:04X} was incorrectly trimmed",
            );
        }

        let source = JsString::from_static("  value  ");
        assert_eq!(
            source.trim_whitespace(true, false).unwrap(),
            JsString::from_static("value  "),
        );
        assert_eq!(
            source.trim_whitespace(false, true).unwrap(),
            JsString::from_static("  value"),
        );
        assert_eq!(
            source.trim_whitespace(true, true).unwrap(),
            JsString::from_static("value"),
        );

        let forced_wide_latin1 = JsString(StringRepr::allocate(StringData::Utf16(
            [0x20, u16::from(b'a'), u16::from(b'b'), 0x20]
                .into_iter()
                .collect(),
        )));
        let narrowed = forced_wide_latin1.trim_whitespace(true, true).unwrap();
        assert_eq!(narrowed, JsString::from_static("ab"));
        assert!(matches!(narrowed.0.data(), StringData::Latin1(_)));
        let forced_wide = JsString::try_from_utf16([0x20, 0x100, 0x20]).unwrap();
        let still_wide = forced_wide.trim_whitespace(true, true).unwrap();
        assert_eq!(still_wide.utf16_units().collect::<Vec<_>>(), [0x100]);
        assert!(matches!(still_wide.0.data(), StringData::Utf16(_)));

        let empty = JsString::from_static(" \u{feff}\u{2029}")
            .trim_whitespace(true, true)
            .unwrap();
        assert!(matches!(empty.0.data(), StringData::Latin1(units) if units.is_empty()));

        let rope = JsString::try_from_utf8(&" ".repeat(8_193))
            .unwrap()
            .try_concat(&JsString::from_static("x "))
            .unwrap();
        assert!(!rope.is_flat());
        assert_eq!(
            rope.trim_whitespace(true, true).unwrap(),
            JsString::from_static("x"),
        );

        super::fail_next_trim_reservation_for_test();
        let unchanged = JsString::from_static("value");
        assert!(
            unchanged
                .trim_whitespace(true, true)
                .unwrap()
                .same_representation(&unchanged),
            "full-range return must not consume the reservation hook",
        );
        let all_space = JsString::from_static("  ");
        assert!(all_space.trim_whitespace(true, true).unwrap().is_empty());
        assert_eq!(
            source.trim_whitespace(true, true),
            Err(JsStringError::OutOfMemory),
        );
        assert_eq!(
            source.trim_whitespace(true, true).unwrap(),
            JsString::from_static("value"),
            "the fail-next trim reservation hook was not consumed exactly once",
        );
    }

    #[test]
    fn rope_thresholds_fringe_merges_and_content_identity_match_quickjs() {
        let flat_8192 = JsString::try_from_utf8(&"a".repeat(8192)).unwrap();
        let short_512 = JsString::try_from_utf8(&"b".repeat(512)).unwrap();
        let merged_flat = flat_8192.try_concat(&short_512).unwrap();
        assert!(merged_flat.is_flat());
        assert_eq!(merged_flat.len(), 8704);

        let flat_8193 = JsString::try_from_utf8(&"a".repeat(8193)).unwrap();
        let rope = flat_8193.try_concat(&short_512).unwrap();
        assert!(!rope.is_flat());
        assert_eq!(rope.depth(), 1);
        assert_eq!(rope.code_unit_at(8192), Some(u16::from(b'a')));
        assert_eq!(rope.code_unit_at(8193), Some(u16::from(b'b')));

        let expected = JsString::try_from_utf8(&("a".repeat(8193) + &"b".repeat(512))).unwrap();
        assert_eq!(rope, expected);
        assert_eq!(content_hash(&rope), content_hash(&expected));
        let mut units = rope.utf16_units();
        assert_eq!(units.len(), rope.len());
        assert_eq!(units.next(), Some(u16::from(b'a')));
        assert_eq!(units.len(), rope.len() - 1);

        let one = JsString::from_static("c");
        let tail_merged = rope.try_concat(&one).unwrap();
        assert_eq!(tail_merged.depth(), 1);
        let super::StringData::Rope(tail) = tail_merged.0.data() else {
            panic!("short right fringe did not remain a rope");
        };
        let (_, tail_right) = JsString::rope_children(tail);
        assert_eq!(tail_right.len(), 513);
        assert!(tail_right.is_flat());

        let right_513 = JsString::try_from_utf8(&"z".repeat(513)).unwrap();
        let threshold_rope = flat_8192.try_concat(&right_513).unwrap();
        assert!(!threshold_rope.is_flat());
        let no_tail_merge = threshold_rope.try_concat(&one).unwrap();
        assert_eq!(no_tail_merge.depth(), 2);
        let super::StringData::Rope(no_tail_merge_rope) = no_tail_merge.0.data() else {
            panic!("513-unit right fringe did not remain nested");
        };
        let (unmerged_left, _) = JsString::rope_children(no_tail_merge_rope);
        assert!(Rc::ptr_eq(&unmerged_left.0, &threshold_rope.0));

        let left_513_rope = right_513.try_concat(&flat_8192).unwrap();
        let no_head_merge = one.try_concat(&left_513_rope).unwrap();
        assert_eq!(no_head_merge.depth(), 2);
        let super::StringData::Rope(no_head_merge_rope) = no_head_merge.0.data() else {
            panic!("513-unit left fringe did not remain nested");
        };
        let (_, unmerged_right) = JsString::rope_children(no_head_merge_rope);
        assert!(Rc::ptr_eq(&unmerged_right.0, &left_513_rope.0));

        let right_rope = short_512.try_concat(&right_513).unwrap();
        assert_eq!(right_rope.depth(), 1);
        let head_merged = one.try_concat(&right_rope).unwrap();
        assert_eq!(head_merged.depth(), 1);
        let super::StringData::Rope(head) = head_merged.0.data() else {
            panic!("short left fringe did not remain a rope");
        };
        let (head_left, _) = JsString::rope_children(head);
        assert_eq!(head_left.len(), 513);
        assert!(head_left.is_flat());

        let empty = JsString::from_static("");
        let large_flat = JsString::try_from_utf8(&"q".repeat(8193)).unwrap();
        let empty_plus_flat = empty.try_concat(&large_flat).unwrap();
        assert!(empty_plus_flat.is_flat());
        assert!(Rc::ptr_eq(&empty_plus_flat.0, &large_flat.0));
        let rope = large_flat.try_concat(&one).unwrap();
        let empty_plus_rope = empty.try_concat(&rope).unwrap();
        assert!(Rc::ptr_eq(&empty_plus_rope.0, &rope.0));
        let rope_plus_empty = rope.try_concat(&empty).unwrap();
        assert!(Rc::ptr_eq(&rope_plus_empty.0, &rope.0));

        let cached_flat = rope.linearize();
        assert!(cached_flat.is_flat());
        let cached_concat = rope.try_concat(&JsString::from_static("!")).unwrap();
        assert!(!cached_concat.is_flat());
        assert_eq!(
            cached_concat.code_unit_at(cached_concat.len() - 1),
            Some(0x21)
        );
    }

    #[test]
    fn rope_code_points_and_well_formed_scans_cross_leaf_boundaries() {
        let mut left = vec![u16::from(b'a'); 8192];
        left.push(0xd83d);
        let mut right = vec![0xde80];
        right.extend(std::iter::repeat_n(u16::from(b'b'), 512));
        let valid = JsString::try_from_utf16(left)
            .unwrap()
            .try_concat(&JsString::try_from_utf16(right).unwrap())
            .unwrap();
        assert!(!valid.is_flat());
        assert_eq!(valid.code_point_at(8192), Some(0x1f680));
        assert!(valid.is_well_formed());

        let invalid =
            JsString::try_from_utf16(std::iter::repeat_n(u16::from(b'a'), 8192).chain([0xd800]))
                .unwrap()
                .try_concat(
                    &JsString::try_from_utf16(
                        [u16::from(b'x')]
                            .into_iter()
                            .chain(std::iter::repeat_n(u16::from(b'b'), 512)),
                    )
                    .unwrap(),
                )
                .unwrap();
        assert!(!invalid.is_well_formed());
        let repaired = invalid.to_well_formed();
        assert_eq!(repaired.code_unit_at(8192), Some(0xfffd));
        assert_eq!(repaired.len(), invalid.len());
    }

    #[test]
    fn rope_rebalances_and_reaches_the_quickjs_length_guard_without_flattening() {
        let marker_chunk = |index: usize| {
            let marker = char::from(b'A' + u8::try_from(index % 26).unwrap());
            JsString::try_from_utf8(&marker.to_string().repeat(8193)).unwrap()
        };
        let mut deep = marker_chunk(0);
        for index in 1..101 {
            deep = deep.try_concat(&marker_chunk(index)).unwrap();
            assert!(deep.depth() <= JsString::ROPE_MAX_DEPTH);
        }
        for index in 0..101 {
            let marker = u16::from(b'A' + u8::try_from(index % 26).unwrap());
            assert_eq!(deep.code_unit_at(index * 8193), Some(marker));
            assert_eq!(deep.code_unit_at((index + 1) * 8193 - 1), Some(marker));
        }

        let mut prepended = marker_chunk(100);
        for index in (0..100).rev() {
            prepended = marker_chunk(index).try_concat(&prepended).unwrap();
            assert!(prepended.depth() <= JsString::ROPE_MAX_DEPTH);
        }
        for index in 0..101 {
            let marker = u16::from(b'A' + u8::try_from(index % 26).unwrap());
            assert_eq!(prepended.code_unit_at(index * 8193), Some(marker));
            assert_eq!(prepended.code_unit_at((index + 1) * 8193 - 1), Some(marker));
        }

        let before_hash = content_hash(&deep);
        let flat = deep.linearize();
        assert!(flat.is_flat());
        assert_eq!(deep, flat);
        assert_eq!(before_hash, content_hash(&deep));
        assert_eq!(before_hash, content_hash(&flat));

        let chunk = JsString::try_from_utf8(&"x".repeat(8193)).unwrap();
        let mut near_limit = chunk;
        for _ in 0..16 {
            near_limit = near_limit.try_concat(&near_limit).unwrap();
        }
        assert_eq!(near_limit.len(), 536_936_448);
        assert!(matches!(
            near_limit.try_concat(&near_limit),
            Err(JsStringError::TooLong)
        ));
        assert!(matches!(
            near_limit.clone().concat_owned(&near_limit),
            Err(JsStringError::TooLong)
        ));
        let error = Error::from(JsStringError::TooLong);
        assert_eq!(error.kind(), ErrorKind::JsInternal);
        assert_eq!(error.message(), "string too long");

        let mut powers = Vec::with_capacity(30);
        let mut power = JsString::from_static("x");
        powers.push(power.clone());
        for _ in 1..30 {
            power = power.try_concat(&power).unwrap();
            powers.push(power.clone());
        }
        let mut exact_max = JsString::from_static("");
        for power in powers.into_iter().rev() {
            exact_max = exact_max.try_concat(&power).unwrap();
        }
        assert_eq!(exact_max.len(), JsString::MAX_LEN);
        assert!(matches!(
            exact_max.try_concat(&JsString::from_static("x")),
            Err(JsStringError::TooLong)
        ));
        let exact_plus_empty = exact_max.try_concat(&JsString::from_static("")).unwrap();
        assert!(Rc::ptr_eq(&exact_plus_empty.0, &exact_max.0));
    }

    #[test]
    fn number_uses_int_fast_path_without_losing_negative_zero() {
        assert!(matches!(
            PrimitiveValue::number(42.0),
            PrimitiveValue::Int(42)
        ));
        assert!(
            matches!(PrimitiveValue::number(-0.0), PrimitiveValue::Float(value) if value.is_sign_negative())
        );
    }

    #[test]
    fn number_formatting_uses_ecmascript_thresholds() {
        assert_eq!(number_to_string(-0.0), "0");
        assert_eq!(number_to_string(f64::NAN), "NaN");
        assert_eq!(number_to_string(1e20), "100000000000000000000");
        assert_eq!(number_to_string(1e21), "1e+21");
    }
}