kataan 0.0.8

A high-performance JavaScript engine written in pure Rust. Library, C FFI, and CLI.
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
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
4027
4028
4029
4030
4031
4032
4033
4034
4035
4036
4037
4038
4039
4040
4041
4042
4043
4044
4045
4046
4047
4048
4049
4050
4051
4052
4053
4054
4055
4056
4057
4058
4059
4060
4061
4062
4063
4064
4065
4066
4067
4068
4069
4070
4071
4072
4073
4074
4075
4076
4077
4078
4079
4080
4081
4082
4083
4084
4085
4086
4087
4088
4089
4090
4091
4092
4093
4094
4095
4096
4097
4098
4099
4100
4101
4102
4103
4104
4105
4106
4107
4108
4109
4110
4111
4112
4113
4114
4115
4116
4117
4118
4119
4120
4121
4122
4123
4124
4125
4126
4127
4128
4129
4130
4131
4132
4133
4134
4135
4136
4137
4138
4139
4140
4141
4142
4143
4144
4145
4146
4147
4148
4149
4150
4151
4152
4153
4154
4155
4156
4157
4158
4159
4160
4161
4162
4163
4164
4165
4166
4167
4168
4169
4170
4171
4172
4173
4174
4175
4176
4177
4178
4179
4180
4181
4182
4183
4184
4185
4186
4187
4188
4189
4190
4191
4192
4193
4194
4195
4196
4197
4198
4199
4200
4201
4202
4203
4204
4205
4206
4207
4208
4209
4210
4211
4212
4213
4214
4215
4216
4217
4218
4219
4220
4221
4222
4223
4224
4225
4226
4227
4228
4229
4230
4231
4232
4233
4234
4235
4236
4237
4238
4239
4240
4241
4242
4243
4244
4245
4246
4247
4248
4249
4250
4251
4252
4253
4254
4255
4256
4257
4258
4259
4260
4261
4262
4263
4264
4265
4266
4267
4268
4269
4270
4271
4272
4273
4274
4275
4276
4277
4278
4279
4280
4281
4282
4283
4284
4285
4286
4287
4288
4289
4290
4291
4292
4293
4294
4295
4296
4297
4298
4299
4300
4301
4302
4303
4304
4305
4306
4307
4308
4309
4310
4311
4312
4313
4314
4315
4316
4317
4318
4319
4320
4321
4322
4323
4324
4325
4326
4327
4328
4329
4330
4331
4332
4333
4334
4335
4336
4337
4338
4339
4340
4341
4342
4343
4344
4345
4346
4347
4348
4349
4350
4351
4352
4353
4354
4355
4356
4357
4358
4359
4360
4361
4362
4363
4364
4365
4366
4367
4368
4369
4370
4371
4372
4373
4374
4375
4376
4377
4378
4379
4380
4381
4382
4383
4384
4385
4386
4387
4388
4389
4390
4391
4392
4393
4394
4395
4396
4397
4398
4399
4400
4401
4402
4403
4404
4405
4406
4407
4408
4409
4410
4411
4412
4413
4414
4415
4416
4417
4418
4419
4420
4421
4422
4423
4424
4425
4426
4427
4428
4429
4430
4431
4432
4433
4434
4435
4436
4437
4438
4439
4440
4441
4442
4443
4444
4445
4446
4447
4448
4449
4450
4451
4452
4453
4454
4455
4456
4457
4458
4459
4460
4461
4462
4463
4464
4465
4466
4467
4468
4469
4470
4471
4472
4473
4474
4475
4476
4477
4478
4479
4480
4481
4482
4483
4484
4485
4486
4487
4488
4489
4490
4491
4492
4493
4494
4495
4496
4497
4498
4499
4500
4501
4502
4503
4504
4505
4506
4507
4508
4509
4510
4511
4512
4513
4514
4515
4516
4517
4518
4519
4520
4521
4522
4523
4524
4525
4526
4527
4528
4529
4530
4531
4532
4533
4534
4535
4536
4537
4538
4539
4540
4541
4542
4543
4544
4545
4546
4547
4548
4549
4550
4551
4552
4553
4554
4555
4556
4557
4558
4559
4560
4561
4562
4563
4564
4565
4566
4567
4568
4569
4570
4571
4572
4573
4574
4575
4576
4577
4578
4579
4580
4581
4582
4583
4584
4585
4586
4587
4588
4589
4590
4591
4592
4593
4594
4595
4596
4597
4598
4599
4600
4601
4602
4603
4604
4605
4606
4607
4608
4609
4610
4611
4612
4613
4614
4615
4616
4617
4618
4619
4620
4621
4622
4623
4624
4625
4626
4627
4628
4629
4630
4631
4632
4633
4634
4635
4636
4637
4638
4639
4640
4641
4642
4643
4644
4645
4646
4647
4648
4649
4650
4651
4652
4653
4654
4655
4656
4657
4658
4659
4660
4661
4662
4663
4664
4665
4666
4667
4668
4669
4670
4671
4672
4673
4674
4675
4676
4677
4678
4679
4680
4681
4682
4683
4684
4685
4686
4687
4688
4689
4690
4691
4692
4693
4694
4695
4696
4697
4698
4699
4700
4701
4702
4703
4704
4705
4706
4707
4708
4709
4710
4711
4712
4713
4714
4715
4716
4717
4718
4719
4720
4721
4722
4723
4724
4725
4726
4727
4728
4729
4730
4731
4732
4733
4734
4735
4736
4737
4738
4739
4740
4741
4742
4743
4744
4745
4746
4747
4748
4749
4750
4751
4752
4753
4754
4755
4756
4757
4758
4759
4760
4761
4762
4763
4764
4765
4766
4767
4768
4769
4770
4771
4772
4773
4774
4775
4776
4777
4778
4779
4780
4781
4782
4783
4784
4785
4786
4787
4788
4789
4790
4791
4792
4793
4794
4795
4796
4797
4798
4799
4800
4801
4802
4803
4804
4805
4806
4807
4808
4809
4810
4811
4812
4813
4814
4815
4816
4817
4818
4819
4820
4821
4822
4823
4824
4825
4826
4827
4828
4829
4830
4831
4832
4833
4834
4835
4836
4837
4838
4839
4840
4841
4842
4843
4844
4845
4846
4847
4848
4849
4850
4851
4852
4853
4854
4855
4856
4857
4858
4859
4860
4861
4862
4863
4864
4865
4866
4867
4868
4869
4870
4871
4872
4873
4874
4875
4876
4877
4878
4879
4880
4881
4882
4883
4884
4885
4886
4887
4888
4889
4890
4891
4892
4893
4894
4895
4896
4897
4898
4899
4900
4901
4902
4903
4904
4905
4906
4907
4908
4909
4910
4911
4912
4913
4914
4915
4916
4917
4918
4919
4920
4921
4922
4923
4924
4925
4926
4927
4928
4929
4930
4931
4932
4933
4934
4935
4936
4937
4938
4939
4940
4941
4942
4943
4944
4945
4946
4947
4948
4949
4950
4951
4952
4953
4954
4955
4956
4957
4958
4959
4960
4961
4962
4963
4964
4965
4966
4967
4968
4969
4970
4971
4972
4973
4974
4975
4976
4977
4978
4979
4980
4981
4982
4983
4984
4985
4986
4987
4988
4989
4990
4991
4992
4993
4994
4995
4996
4997
4998
4999
5000
5001
5002
5003
5004
5005
5006
5007
5008
5009
5010
5011
5012
5013
5014
5015
5016
5017
5018
5019
5020
5021
5022
5023
5024
5025
5026
5027
5028
5029
5030
5031
5032
5033
5034
5035
5036
5037
5038
5039
5040
5041
5042
5043
5044
5045
5046
5047
5048
5049
5050
5051
5052
5053
5054
5055
5056
5057
5058
5059
5060
5061
5062
5063
5064
5065
5066
5067
5068
5069
5070
5071
5072
5073
5074
5075
5076
5077
5078
5079
5080
5081
5082
5083
5084
5085
5086
5087
5088
5089
5090
5091
5092
5093
5094
5095
5096
5097
5098
5099
5100
5101
5102
5103
5104
5105
5106
5107
5108
5109
5110
5111
5112
5113
5114
5115
5116
5117
5118
5119
5120
5121
5122
5123
5124
5125
5126
5127
5128
5129
5130
5131
5132
5133
5134
5135
5136
5137
5138
5139
5140
5141
5142
5143
5144
5145
5146
5147
5148
5149
5150
5151
5152
5153
5154
5155
5156
5157
5158
5159
5160
5161
5162
5163
5164
5165
5166
5167
5168
5169
5170
5171
5172
5173
5174
5175
5176
5177
5178
5179
5180
5181
5182
5183
5184
5185
5186
5187
5188
5189
5190
5191
5192
5193
5194
5195
5196
5197
5198
5199
5200
5201
5202
5203
5204
5205
5206
5207
5208
5209
5210
5211
5212
5213
5214
5215
5216
5217
5218
5219
5220
5221
5222
5223
5224
5225
5226
5227
5228
5229
5230
5231
5232
5233
5234
5235
5236
5237
5238
5239
5240
5241
5242
5243
5244
5245
5246
5247
5248
5249
5250
5251
5252
5253
5254
5255
5256
5257
5258
5259
5260
5261
5262
5263
5264
5265
5266
5267
5268
5269
5270
5271
5272
5273
5274
5275
5276
5277
5278
5279
5280
5281
5282
5283
5284
5285
5286
5287
5288
5289
5290
5291
5292
5293
5294
5295
5296
5297
5298
5299
5300
5301
5302
5303
5304
5305
5306
5307
5308
5309
5310
5311
5312
5313
5314
5315
5316
5317
5318
5319
5320
5321
5322
5323
5324
5325
5326
5327
5328
5329
5330
5331
5332
5333
5334
5335
5336
5337
5338
5339
5340
5341
5342
5343
5344
5345
5346
5347
5348
5349
5350
5351
5352
5353
5354
5355
5356
5357
5358
5359
5360
5361
5362
5363
5364
5365
5366
5367
5368
5369
5370
5371
5372
5373
5374
5375
5376
5377
5378
5379
5380
5381
5382
5383
5384
5385
5386
5387
5388
5389
5390
5391
5392
5393
5394
5395
5396
5397
5398
5399
5400
5401
5402
5403
5404
5405
5406
5407
5408
5409
5410
5411
5412
5413
5414
5415
5416
5417
5418
5419
5420
5421
5422
5423
5424
5425
5426
5427
5428
5429
5430
5431
5432
5433
5434
5435
5436
5437
5438
5439
5440
5441
5442
5443
5444
5445
5446
5447
5448
5449
5450
5451
5452
5453
5454
5455
5456
5457
5458
5459
5460
5461
5462
5463
5464
5465
5466
5467
5468
5469
5470
5471
5472
5473
5474
5475
5476
5477
5478
5479
5480
5481
5482
5483
5484
5485
5486
5487
5488
5489
5490
5491
5492
5493
5494
5495
5496
5497
5498
5499
5500
5501
5502
5503
5504
5505
5506
5507
5508
5509
5510
5511
5512
5513
5514
5515
5516
5517
5518
5519
5520
5521
5522
5523
5524
5525
5526
5527
5528
5529
5530
5531
5532
5533
5534
5535
5536
5537
5538
5539
5540
5541
5542
5543
5544
5545
5546
5547
5548
5549
5550
5551
5552
5553
5554
5555
5556
5557
5558
5559
5560
5561
5562
5563
5564
5565
5566
5567
5568
5569
5570
5571
5572
5573
5574
5575
5576
5577
5578
5579
5580
5581
5582
5583
5584
5585
5586
5587
5588
5589
5590
5591
5592
5593
5594
5595
5596
5597
5598
5599
5600
5601
5602
5603
5604
5605
5606
5607
5608
5609
5610
5611
5612
5613
5614
5615
5616
5617
5618
5619
5620
5621
5622
5623
5624
5625
5626
5627
5628
5629
5630
5631
5632
5633
5634
5635
5636
5637
5638
5639
5640
5641
5642
5643
5644
5645
5646
5647
5648
5649
5650
5651
5652
5653
5654
5655
5656
5657
5658
5659
5660
5661
5662
5663
5664
5665
5666
5667
5668
5669
5670
5671
5672
5673
5674
5675
5676
5677
5678
5679
5680
5681
5682
5683
5684
5685
5686
5687
5688
5689
5690
5691
5692
5693
5694
5695
5696
5697
5698
5699
5700
5701
5702
5703
5704
5705
5706
5707
5708
5709
5710
5711
5712
5713
5714
5715
5716
5717
5718
5719
5720
5721
5722
5723
5724
5725
5726
5727
5728
5729
5730
5731
5732
5733
5734
5735
5736
5737
5738
5739
5740
5741
5742
5743
5744
5745
5746
5747
5748
5749
5750
5751
5752
5753
5754
5755
5756
5757
5758
5759
5760
5761
5762
5763
5764
5765
5766
5767
5768
5769
5770
5771
5772
5773
5774
5775
5776
5777
5778
5779
5780
5781
5782
5783
5784
5785
5786
5787
5788
5789
5790
5791
5792
5793
5794
5795
5796
5797
5798
5799
5800
5801
5802
5803
5804
5805
5806
5807
5808
5809
5810
5811
5812
5813
5814
5815
5816
5817
5818
5819
5820
5821
5822
5823
5824
5825
5826
5827
5828
5829
5830
5831
5832
5833
5834
5835
5836
5837
5838
5839
5840
5841
5842
5843
5844
5845
5846
5847
5848
5849
5850
5851
5852
5853
5854
5855
5856
5857
5858
5859
5860
5861
5862
5863
5864
5865
5866
5867
5868
5869
5870
5871
5872
5873
5874
5875
5876
5877
5878
5879
5880
5881
5882
5883
5884
5885
5886
5887
5888
5889
5890
5891
5892
5893
5894
5895
5896
5897
5898
5899
5900
5901
5902
5903
5904
5905
5906
5907
5908
5909
5910
5911
5912
5913
5914
5915
5916
5917
5918
5919
5920
5921
5922
5923
5924
5925
5926
5927
5928
5929
5930
5931
5932
5933
5934
5935
5936
5937
5938
5939
5940
5941
5942
5943
5944
5945
5946
5947
5948
5949
5950
5951
5952
5953
5954
5955
5956
5957
5958
5959
5960
5961
5962
5963
5964
5965
5966
5967
5968
5969
5970
5971
5972
5973
5974
5975
5976
5977
5978
5979
5980
5981
5982
5983
5984
5985
5986
5987
5988
5989
5990
5991
5992
5993
5994
5995
5996
5997
5998
5999
6000
6001
6002
6003
6004
6005
6006
6007
6008
6009
6010
6011
6012
6013
6014
6015
6016
6017
6018
6019
6020
6021
6022
6023
6024
6025
6026
6027
6028
6029
6030
6031
6032
6033
6034
6035
6036
6037
6038
6039
6040
6041
6042
6043
6044
6045
6046
6047
6048
6049
6050
6051
6052
6053
6054
6055
6056
6057
6058
6059
6060
6061
6062
6063
6064
6065
6066
6067
6068
6069
6070
6071
6072
6073
6074
6075
6076
6077
6078
6079
6080
6081
6082
6083
6084
6085
6086
6087
6088
6089
6090
6091
6092
6093
6094
6095
6096
6097
6098
6099
6100
6101
6102
6103
6104
6105
6106
6107
6108
6109
6110
6111
6112
6113
6114
6115
6116
6117
6118
6119
6120
6121
6122
6123
6124
6125
6126
6127
6128
6129
6130
6131
6132
6133
6134
6135
6136
6137
6138
6139
6140
6141
6142
6143
6144
6145
6146
6147
6148
6149
6150
6151
6152
6153
6154
6155
6156
6157
6158
6159
6160
6161
6162
6163
6164
6165
6166
6167
6168
6169
6170
6171
6172
6173
6174
6175
6176
6177
6178
6179
6180
6181
6182
6183
6184
6185
6186
6187
6188
6189
6190
6191
6192
6193
6194
6195
6196
6197
6198
6199
6200
6201
6202
6203
6204
6205
6206
6207
6208
6209
6210
6211
6212
6213
6214
6215
6216
6217
6218
6219
6220
6221
6222
6223
6224
6225
6226
6227
6228
6229
6230
6231
6232
6233
6234
6235
6236
6237
6238
6239
6240
6241
6242
6243
6244
6245
6246
6247
6248
6249
6250
6251
6252
6253
6254
6255
6256
6257
6258
6259
6260
6261
6262
6263
6264
6265
6266
6267
6268
6269
6270
6271
6272
6273
6274
6275
6276
6277
6278
6279
6280
6281
6282
6283
6284
6285
6286
6287
6288
6289
6290
6291
6292
6293
6294
6295
6296
6297
6298
6299
6300
6301
6302
6303
6304
6305
6306
6307
6308
6309
6310
6311
6312
6313
6314
6315
6316
6317
6318
6319
6320
6321
6322
6323
6324
6325
6326
6327
6328
6329
6330
6331
6332
6333
6334
6335
6336
6337
6338
6339
6340
6341
6342
6343
6344
6345
6346
6347
6348
6349
6350
6351
6352
6353
6354
6355
6356
6357
6358
6359
6360
6361
6362
6363
6364
6365
6366
6367
6368
6369
6370
6371
6372
6373
6374
6375
6376
6377
6378
6379
6380
6381
6382
6383
6384
6385
6386
6387
6388
6389
6390
6391
6392
6393
6394
6395
6396
6397
6398
6399
6400
6401
6402
6403
6404
6405
6406
6407
6408
6409
6410
6411
6412
6413
6414
6415
6416
6417
6418
6419
6420
6421
6422
6423
6424
6425
6426
6427
6428
6429
6430
6431
6432
6433
6434
6435
6436
6437
6438
6439
6440
6441
6442
6443
6444
6445
6446
6447
6448
6449
6450
6451
6452
6453
6454
6455
6456
6457
6458
6459
6460
6461
6462
6463
6464
6465
6466
6467
6468
6469
6470
6471
6472
6473
6474
6475
6476
6477
6478
6479
6480
6481
6482
6483
6484
6485
6486
6487
6488
6489
6490
6491
6492
6493
6494
6495
6496
6497
6498
6499
6500
6501
6502
6503
6504
6505
6506
6507
6508
6509
6510
6511
6512
6513
6514
6515
6516
6517
6518
6519
6520
6521
6522
6523
6524
6525
6526
6527
6528
6529
6530
6531
6532
6533
6534
6535
6536
6537
6538
6539
6540
6541
6542
6543
6544
6545
6546
6547
6548
6549
6550
6551
6552
6553
6554
6555
6556
6557
6558
6559
6560
6561
6562
6563
6564
6565
6566
6567
6568
6569
6570
6571
6572
6573
6574
6575
6576
6577
6578
6579
6580
6581
6582
6583
6584
6585
6586
6587
6588
6589
6590
6591
6592
6593
6594
6595
6596
6597
6598
6599
6600
6601
6602
6603
6604
6605
6606
6607
6608
6609
6610
6611
6612
6613
6614
6615
6616
6617
6618
6619
6620
6621
6622
6623
6624
6625
6626
6627
6628
6629
6630
6631
6632
6633
6634
6635
6636
6637
6638
6639
6640
6641
6642
6643
6644
6645
6646
6647
6648
6649
6650
6651
6652
6653
6654
6655
6656
6657
6658
6659
6660
6661
6662
6663
6664
6665
6666
6667
6668
6669
6670
6671
6672
6673
6674
6675
6676
6677
6678
6679
6680
6681
6682
6683
6684
6685
6686
6687
6688
6689
6690
6691
6692
6693
6694
6695
6696
6697
6698
6699
6700
6701
6702
6703
6704
6705
6706
6707
6708
6709
6710
6711
6712
6713
6714
6715
6716
6717
6718
6719
6720
6721
6722
6723
6724
6725
6726
6727
6728
6729
6730
6731
6732
6733
6734
6735
6736
6737
6738
6739
6740
6741
6742
6743
6744
6745
6746
6747
6748
6749
6750
6751
6752
6753
6754
6755
6756
6757
6758
6759
6760
6761
6762
6763
6764
6765
6766
6767
6768
6769
6770
6771
6772
6773
6774
6775
6776
6777
6778
6779
6780
6781
6782
6783
6784
6785
6786
6787
6788
6789
6790
6791
6792
6793
6794
6795
6796
6797
6798
6799
6800
6801
6802
6803
6804
6805
6806
6807
6808
6809
6810
6811
6812
6813
6814
6815
6816
6817
6818
6819
6820
6821
6822
6823
6824
6825
6826
6827
6828
6829
6830
6831
6832
6833
6834
6835
6836
6837
6838
6839
6840
6841
6842
6843
6844
6845
6846
6847
6848
6849
6850
6851
6852
6853
6854
6855
6856
6857
6858
6859
6860
6861
6862
6863
6864
6865
6866
6867
6868
6869
6870
6871
6872
6873
6874
6875
6876
6877
6878
6879
6880
6881
6882
6883
6884
6885
6886
6887
6888
6889
6890
6891
6892
6893
6894
6895
6896
6897
6898
6899
6900
6901
6902
6903
6904
6905
6906
6907
6908
6909
6910
6911
6912
6913
6914
6915
6916
6917
6918
6919
6920
6921
6922
6923
6924
6925
6926
6927
6928
6929
6930
6931
6932
6933
6934
6935
6936
6937
6938
6939
6940
6941
6942
6943
6944
6945
6946
6947
6948
6949
6950
6951
6952
6953
6954
6955
6956
6957
6958
6959
6960
6961
6962
6963
6964
6965
6966
6967
6968
6969
6970
6971
6972
6973
6974
6975
6976
6977
6978
6979
6980
6981
6982
6983
6984
6985
6986
6987
6988
6989
6990
6991
6992
6993
6994
6995
6996
6997
6998
6999
7000
7001
7002
7003
7004
7005
7006
7007
7008
7009
7010
7011
7012
7013
7014
7015
7016
7017
7018
7019
7020
7021
7022
7023
7024
7025
7026
7027
7028
7029
7030
7031
7032
7033
7034
7035
7036
7037
7038
7039
7040
7041
7042
7043
7044
7045
7046
7047
7048
7049
7050
7051
7052
7053
7054
7055
7056
7057
7058
7059
7060
7061
7062
7063
7064
7065
7066
7067
7068
7069
7070
7071
7072
7073
7074
7075
7076
7077
7078
7079
7080
7081
7082
7083
7084
7085
7086
7087
7088
7089
7090
7091
7092
7093
7094
7095
7096
7097
7098
7099
7100
7101
7102
7103
7104
7105
7106
7107
7108
7109
7110
7111
7112
7113
7114
7115
7116
7117
7118
7119
7120
7121
7122
7123
7124
7125
7126
7127
7128
7129
7130
7131
7132
7133
7134
7135
7136
7137
7138
7139
7140
7141
7142
7143
7144
7145
7146
7147
7148
7149
7150
7151
7152
7153
7154
7155
7156
7157
7158
7159
7160
7161
7162
7163
7164
7165
7166
7167
7168
7169
7170
7171
7172
7173
7174
7175
7176
7177
7178
7179
7180
7181
7182
7183
7184
7185
7186
7187
7188
7189
7190
7191
7192
7193
7194
7195
7196
7197
7198
7199
7200
7201
7202
7203
7204
7205
7206
7207
7208
7209
7210
7211
7212
7213
7214
7215
7216
7217
7218
7219
7220
7221
7222
7223
7224
7225
7226
7227
7228
7229
7230
7231
7232
7233
7234
7235
7236
7237
7238
7239
7240
7241
7242
7243
7244
7245
7246
7247
7248
7249
7250
7251
7252
7253
7254
7255
7256
7257
7258
7259
7260
7261
7262
7263
7264
7265
7266
7267
7268
7269
7270
7271
7272
7273
7274
7275
7276
7277
7278
7279
7280
7281
7282
7283
7284
7285
7286
7287
7288
7289
7290
7291
7292
7293
7294
7295
7296
7297
7298
7299
7300
7301
7302
7303
7304
7305
7306
7307
7308
7309
7310
7311
7312
7313
7314
7315
7316
7317
7318
7319
7320
7321
7322
7323
7324
7325
7326
7327
7328
7329
7330
7331
7332
7333
7334
7335
7336
7337
7338
7339
7340
7341
7342
7343
7344
7345
7346
7347
7348
7349
7350
7351
7352
7353
7354
7355
7356
7357
7358
7359
7360
7361
7362
7363
7364
7365
7366
7367
7368
7369
7370
7371
7372
7373
7374
7375
7376
7377
7378
7379
7380
7381
7382
7383
7384
7385
7386
7387
7388
7389
7390
7391
7392
7393
7394
7395
7396
7397
7398
7399
7400
7401
7402
7403
7404
7405
7406
7407
7408
7409
7410
7411
7412
7413
7414
7415
7416
7417
7418
7419
7420
7421
7422
7423
7424
7425
7426
7427
7428
7429
7430
7431
7432
7433
7434
7435
7436
7437
7438
7439
7440
7441
7442
7443
7444
7445
7446
7447
7448
7449
7450
7451
7452
7453
7454
7455
7456
7457
7458
7459
7460
7461
7462
7463
7464
7465
7466
7467
7468
7469
7470
7471
7472
7473
7474
7475
7476
7477
7478
7479
7480
7481
7482
7483
7484
7485
7486
7487
7488
7489
7490
7491
7492
7493
7494
7495
7496
7497
7498
7499
7500
7501
7502
7503
7504
7505
7506
7507
7508
7509
7510
7511
7512
7513
7514
7515
7516
7517
7518
7519
7520
7521
7522
7523
7524
7525
7526
7527
7528
7529
7530
7531
7532
7533
7534
7535
7536
7537
7538
7539
7540
7541
7542
7543
7544
7545
7546
7547
7548
7549
7550
7551
7552
7553
7554
7555
7556
7557
7558
7559
7560
7561
7562
7563
7564
7565
7566
7567
7568
7569
7570
7571
7572
7573
7574
7575
7576
7577
7578
7579
7580
7581
7582
7583
7584
7585
7586
7587
7588
7589
7590
7591
7592
7593
7594
7595
7596
7597
7598
7599
7600
7601
7602
7603
7604
7605
7606
7607
7608
7609
7610
7611
7612
7613
7614
7615
7616
7617
7618
7619
7620
7621
7622
7623
7624
7625
7626
7627
7628
7629
7630
7631
7632
7633
7634
7635
7636
7637
7638
7639
7640
7641
7642
7643
7644
7645
7646
7647
7648
7649
7650
7651
7652
7653
7654
7655
7656
7657
7658
7659
7660
7661
7662
7663
7664
7665
7666
7667
7668
7669
7670
7671
7672
7673
7674
7675
7676
7677
7678
7679
7680
7681
7682
7683
7684
7685
7686
7687
7688
7689
7690
7691
7692
7693
7694
7695
7696
7697
7698
7699
7700
7701
7702
7703
7704
7705
7706
7707
7708
7709
7710
7711
7712
7713
7714
7715
7716
7717
7718
7719
7720
7721
7722
7723
7724
7725
7726
7727
7728
7729
7730
7731
7732
7733
7734
7735
7736
7737
7738
7739
7740
7741
7742
7743
7744
7745
7746
7747
7748
7749
7750
7751
7752
7753
7754
7755
7756
7757
7758
7759
7760
7761
7762
7763
7764
7765
7766
7767
7768
7769
7770
7771
7772
7773
7774
7775
7776
7777
7778
7779
7780
7781
7782
7783
7784
7785
7786
7787
7788
7789
7790
7791
7792
7793
7794
7795
7796
7797
7798
7799
7800
7801
7802
7803
7804
7805
7806
7807
7808
7809
7810
7811
7812
7813
7814
7815
7816
7817
7818
7819
7820
7821
7822
7823
7824
7825
7826
7827
7828
7829
7830
7831
7832
7833
7834
7835
7836
7837
7838
7839
7840
7841
7842
7843
7844
7845
7846
7847
7848
7849
7850
7851
7852
7853
7854
7855
7856
7857
7858
7859
7860
7861
7862
7863
7864
7865
7866
7867
7868
7869
7870
7871
7872
7873
7874
7875
7876
7877
7878
7879
7880
7881
7882
7883
7884
7885
7886
7887
7888
7889
7890
7891
7892
7893
7894
7895
7896
7897
7898
7899
7900
7901
7902
7903
7904
7905
7906
7907
7908
7909
7910
7911
7912
7913
7914
7915
7916
7917
7918
7919
7920
7921
7922
7923
7924
7925
7926
7927
7928
7929
7930
7931
7932
7933
7934
7935
7936
7937
7938
7939
7940
7941
7942
7943
7944
7945
7946
7947
7948
7949
7950
7951
7952
7953
7954
7955
7956
7957
7958
7959
7960
7961
7962
7963
7964
7965
7966
7967
7968
7969
7970
7971
7972
7973
7974
7975
7976
7977
7978
7979
7980
7981
7982
7983
7984
7985
7986
7987
7988
7989
7990
7991
7992
7993
7994
7995
7996
7997
7998
7999
8000
8001
8002
8003
8004
8005
8006
8007
8008
8009
8010
8011
8012
8013
8014
8015
8016
8017
8018
8019
8020
8021
8022
8023
8024
8025
8026
8027
8028
8029
8030
8031
8032
8033
8034
8035
8036
8037
8038
8039
8040
8041
8042
8043
8044
8045
8046
8047
8048
8049
8050
8051
8052
8053
8054
8055
8056
8057
8058
8059
8060
8061
8062
8063
8064
8065
8066
8067
8068
8069
8070
8071
8072
8073
8074
8075
8076
8077
8078
8079
8080
8081
8082
8083
8084
8085
8086
8087
8088
8089
8090
8091
8092
8093
8094
8095
8096
8097
8098
8099
8100
8101
8102
8103
8104
8105
8106
8107
8108
8109
8110
8111
8112
8113
8114
8115
8116
8117
8118
8119
8120
8121
8122
8123
8124
8125
8126
8127
8128
8129
8130
8131
8132
8133
8134
8135
8136
8137
8138
8139
8140
8141
8142
8143
8144
8145
8146
8147
8148
8149
8150
8151
8152
8153
8154
8155
8156
8157
8158
8159
8160
8161
8162
8163
8164
8165
8166
8167
8168
8169
8170
8171
8172
8173
8174
8175
8176
8177
8178
8179
8180
8181
8182
8183
8184
8185
8186
8187
8188
8189
8190
8191
8192
8193
8194
8195
8196
8197
8198
8199
8200
8201
8202
8203
8204
8205
8206
8207
8208
8209
8210
8211
8212
8213
8214
8215
8216
8217
8218
8219
8220
8221
8222
8223
8224
8225
8226
8227
8228
8229
8230
8231
8232
8233
8234
8235
8236
8237
8238
8239
8240
8241
8242
8243
8244
8245
8246
8247
8248
8249
8250
8251
8252
8253
8254
8255
8256
8257
8258
8259
8260
8261
8262
8263
8264
8265
8266
8267
8268
8269
8270
8271
8272
8273
8274
8275
8276
8277
8278
8279
8280
8281
8282
8283
8284
8285
8286
8287
8288
8289
8290
8291
8292
8293
8294
8295
8296
8297
8298
8299
8300
8301
8302
8303
8304
8305
8306
8307
8308
8309
8310
8311
8312
8313
8314
8315
8316
8317
8318
8319
8320
8321
8322
8323
8324
8325
8326
8327
8328
8329
8330
8331
8332
8333
8334
8335
8336
8337
8338
8339
8340
8341
8342
8343
8344
8345
8346
8347
8348
8349
8350
8351
8352
8353
8354
8355
8356
8357
8358
8359
8360
8361
8362
8363
8364
8365
8366
8367
8368
8369
8370
8371
8372
8373
8374
8375
8376
8377
8378
8379
8380
8381
8382
8383
8384
8385
8386
8387
8388
8389
8390
8391
8392
8393
8394
8395
8396
8397
8398
8399
8400
8401
8402
8403
8404
8405
8406
8407
8408
8409
8410
8411
8412
8413
8414
8415
8416
8417
8418
8419
8420
8421
8422
8423
8424
8425
8426
8427
8428
8429
8430
8431
8432
8433
8434
8435
8436
8437
8438
8439
8440
8441
8442
8443
8444
8445
8446
8447
8448
8449
8450
8451
8452
8453
8454
8455
8456
8457
8458
8459
8460
8461
8462
8463
8464
8465
8466
8467
8468
8469
8470
8471
8472
8473
8474
8475
8476
8477
8478
8479
8480
8481
8482
8483
8484
8485
8486
8487
8488
8489
8490
8491
8492
8493
8494
8495
8496
8497
8498
8499
8500
8501
8502
8503
8504
8505
8506
8507
8508
8509
8510
8511
8512
8513
8514
8515
8516
8517
8518
8519
8520
8521
8522
8523
8524
8525
8526
8527
8528
8529
8530
8531
8532
8533
8534
8535
8536
8537
8538
8539
8540
8541
8542
8543
8544
8545
8546
8547
8548
8549
8550
8551
8552
8553
8554
8555
8556
8557
8558
8559
8560
8561
8562
8563
8564
8565
8566
8567
8568
8569
8570
8571
8572
8573
8574
8575
8576
8577
8578
8579
8580
8581
8582
8583
8584
8585
8586
8587
8588
8589
8590
8591
8592
8593
8594
8595
8596
8597
8598
8599
8600
8601
8602
8603
8604
8605
8606
8607
8608
8609
8610
8611
8612
8613
8614
8615
8616
8617
8618
8619
8620
8621
8622
8623
8624
8625
8626
8627
8628
8629
8630
8631
8632
8633
8634
8635
8636
8637
8638
8639
8640
8641
8642
8643
8644
8645
8646
8647
8648
8649
8650
8651
8652
8653
8654
8655
8656
8657
8658
8659
8660
8661
8662
8663
8664
8665
8666
8667
8668
8669
8670
8671
8672
8673
8674
8675
8676
8677
8678
8679
8680
8681
8682
8683
8684
8685
8686
8687
8688
8689
8690
8691
8692
8693
8694
8695
8696
8697
8698
8699
8700
8701
8702
8703
8704
8705
8706
8707
8708
8709
8710
8711
8712
8713
8714
8715
8716
8717
8718
8719
8720
8721
8722
8723
8724
8725
8726
8727
8728
8729
8730
8731
8732
8733
8734
8735
8736
8737
8738
8739
8740
8741
8742
8743
8744
8745
8746
8747
8748
8749
8750
8751
8752
8753
8754
8755
8756
8757
8758
8759
8760
8761
8762
8763
8764
8765
8766
8767
8768
8769
8770
8771
8772
8773
8774
8775
8776
8777
8778
8779
8780
8781
8782
8783
8784
8785
8786
8787
8788
8789
8790
8791
8792
8793
8794
8795
8796
8797
8798
8799
8800
8801
8802
8803
8804
8805
8806
8807
8808
8809
8810
8811
8812
8813
8814
8815
8816
8817
8818
8819
8820
8821
8822
8823
8824
8825
8826
8827
8828
8829
8830
8831
8832
8833
8834
8835
8836
8837
8838
8839
8840
8841
8842
8843
8844
8845
8846
8847
8848
8849
8850
8851
8852
8853
8854
8855
8856
8857
8858
8859
8860
8861
8862
8863
8864
8865
8866
8867
8868
8869
8870
8871
8872
8873
8874
8875
8876
8877
8878
8879
8880
8881
8882
8883
8884
8885
8886
8887
8888
8889
8890
8891
8892
8893
8894
8895
8896
8897
8898
8899
8900
8901
8902
8903
8904
8905
8906
8907
8908
8909
8910
8911
8912
8913
8914
8915
8916
8917
8918
8919
8920
8921
8922
8923
8924
8925
8926
8927
8928
8929
8930
8931
8932
8933
8934
8935
8936
8937
8938
8939
8940
8941
8942
8943
8944
8945
8946
8947
8948
8949
8950
8951
8952
8953
8954
8955
8956
8957
8958
8959
8960
8961
8962
8963
8964
8965
8966
8967
8968
8969
use super::*;
#[cfg(not(feature = "std"))]
use crate::common::FloatExt;

/// The resolved form of `Intl.NumberFormat`'s `useGrouping` option: a boolean
/// `false`, or one of the string modes `"auto"`/`"always"`/`"min2"`.
enum UseGroupingResolved {
    Bool(bool),
    Str(&'static str),
}

/// The Unicode code point of digit `0` for a decimal numbering system whose ten
/// digits are consecutive (the common case), so digit `d` is `base + d`. Returns
/// `None` for `latn` (ASCII, unchanged) and for non-consecutive systems like
/// `hanidec`. Covers the numeric numbering systems the corpus exercises.
fn numbering_system_digit_base(nu: &str) -> Option<u32> {
    // Every CLDR numbering system with a *simple, consecutive* 10-digit mapping
    // (per Test262's `numberingSystemDigits`); `latn` maps to ASCII 0. The one
    // simple-mapped system whose digits are NOT consecutive — `hanidec` — is
    // handled separately in [`substitute_numbering_digits`].
    Some(match nu {
        "adlm" => 0x1E950,
        "ahom" => 0x11730,
        "arab" => 0x0660,
        "arabext" => 0x06F0,
        "bali" => 0x1B50,
        "beng" => 0x09E6,
        "bhks" => 0x11C50,
        "brah" => 0x11066,
        "cakm" => 0x11136,
        "cham" => 0xAA50,
        "deva" => 0x0966,
        "diak" => 0x11950,
        "fullwide" => 0xFF10,
        "gara" => 0x10D40,
        "gong" => 0x11DA0,
        "gonm" => 0x11D50,
        "gujr" => 0x0AE6,
        "gukh" => 0x16130,
        "guru" => 0x0A66,
        "hmng" => 0x16B50,
        "hmnp" => 0x1E140,
        "java" => 0xA9D0,
        "kali" => 0xA900,
        "kawi" => 0x11F50,
        "khmr" => 0x17E0,
        "knda" => 0x0CE6,
        "krai" => 0x16D70,
        "lana" => 0x1A80,
        "lanatham" => 0x1A90,
        "laoo" => 0x0ED0,
        "latn" => 0x0030,
        "lepc" => 0x1C40,
        "limb" => 0x1946,
        "mathbold" => 0x1D7CE,
        "mathdbl" => 0x1D7D8,
        "mathmono" => 0x1D7F6,
        "mathsanb" => 0x1D7EC,
        "mathsans" => 0x1D7E2,
        "mlym" => 0x0D66,
        "modi" => 0x11650,
        "mong" => 0x1810,
        "mroo" => 0x16A60,
        "mtei" => 0xABF0,
        "mymr" => 0x1040,
        "mymrepka" => 0x116DA,
        "mymrpao" => 0x116D0,
        "mymrshan" => 0x1090,
        "mymrtlng" => 0xA9F0,
        "nagm" => 0x1E4F0,
        "newa" => 0x11450,
        "nkoo" => 0x07C0,
        "olck" => 0x1C50,
        "onao" => 0x1E5F1,
        "orya" => 0x0B66,
        "osma" => 0x104A0,
        "outlined" => 0x1CCF0,
        "rohg" => 0x10D30,
        "saur" => 0xA8D0,
        "segment" => 0x1FBF0,
        "shrd" => 0x111D0,
        "sind" => 0x112F0,
        "sinh" => 0x0DE6,
        "sora" => 0x110F0,
        "sund" => 0x1BB0,
        "sunu" => 0x11BF0,
        "takr" => 0x116C0,
        "talu" => 0x19D0,
        "tamldec" => 0x0BE6,
        "telu" => 0x0C66,
        "thai" => 0x0E50,
        "tibt" => 0x0F20,
        "tirh" => 0x114D0,
        "tnsa" => 0x16AC0,
        "tols" => 0x11DE0,
        "vaii" => 0xA620,
        "wara" => 0x118E0,
        "wcho" => 0x1E2F0,
        _ => return None,
    })
}

/// Rewrites the ASCII digits of `s` into numbering system `nu` (the consecutive
/// mapping from [`numbering_system_digit_base`], or the special non-consecutive
/// `hanidec`); other systems and non-digit characters pass through unchanged.
fn substitute_numbering_digits(nu: &str, s: String) -> String {
    if nu == "hanidec" {
        const HANIDEC: [char; 10] = ['', '', '', '', '', '', '', '', '', ''];
        return s
            .chars()
            .map(|c| {
                if c.is_ascii_digit() {
                    HANIDEC[(c as u8 - b'0') as usize]
                } else {
                    c
                }
            })
            .collect();
    }
    match numbering_system_digit_base(nu) {
        Some(base) if base != 0x0030 => s
            .chars()
            .map(|c| {
                if c.is_ascii_digit() {
                    char::from_u32(base + (c as u32 - '0' as u32)).unwrap_or(c)
                } else {
                    c
                }
            })
            .collect(),
        _ => s,
    }
}

/// The numbering-system name for a native zero digit `c` (the reverse of
/// [`numbering_system_digit_base`]) — used to name the CLDR default numbering
/// system a locale resolves to, detected from the digit the `intl` crate emits.
#[cfg(feature = "intl")]
fn numbering_system_name_from_zero(c: char) -> Option<&'static str> {
    Some(match c as u32 {
        0x0030 => "latn",
        0x0660 => "arab",
        0x06F0 => "arabext",
        0x1B50 => "bali",
        0x09E6 => "beng",
        0x0966 => "deva",
        0xFF10 => "fullwide",
        0x0AE6 => "gujr",
        0x0A66 => "guru",
        0x17E0 => "khmr",
        0x0CE6 => "knda",
        0x0ED0 => "laoo",
        0x1946 => "limb",
        0x0D66 => "mlym",
        0x1810 => "mong",
        0x1040 => "mymr",
        0x0B66 => "orya",
        0x104A0 => "osma",
        0xA8D0 => "saur",
        0x1BB0 => "sund",
        0x19D0 => "talu",
        0x0BE6 => "tamldec",
        0x0C66 => "telu",
        0x0E50 => "thai",
        0x0F20 => "tibt",
        0xA620 => "vaii",
        _ => return None,
    })
}

/// Whether a decimal digit vector, rounded at index `cut`, rounds *up* under
/// `mode`. Mirrors `intl::number`'s internal `should_round_up`, but the caller
/// feeds the value's **shortest round-trip decimal** so the rounding boundary
/// matches ECMA-402 / ICU (which rounds the shortest decimal) rather than the
/// f64's exact binary expansion (`1.15` is really `1.1499…`, so the crate's own
/// path wrongly rounds it *down*).
#[cfg(feature = "intl")]
fn dec_round_up(
    digits: &[u8],
    cut: usize,
    mode: intl::number::RoundingMode,
    negative: bool,
) -> bool {
    use intl::number::RoundingMode::*;
    if cut >= digits.len() {
        return false;
    }
    let first = digits[cut];
    let rest_nonzero = digits[cut + 1..].iter().any(|&d| d != 0);
    let any = first != 0 || rest_nonzero;
    let gt_half = first > 5 || (first == 5 && rest_nonzero);
    let eq_half = first == 5 && !rest_nonzero;
    let kept_last_odd = cut > 0 && digits[cut - 1] % 2 == 1;
    match mode {
        Trunc => false,
        Expand => any,
        Ceil => any && !negative,
        Floor => any && negative,
        HalfExpand => gt_half || eq_half,
        HalfTrunc => gt_half,
        HalfEven => gt_half || (eq_half && kept_last_odd),
        HalfCeil => gt_half || (eq_half && !negative),
        HalfFloor => gt_half || (eq_half && negative),
    }
}

/// Add `delta` (< 10^18) at the units place of a big-endian decimal digit vector,
/// growing `point` (integer-digit count) on a leading carry.
#[cfg(feature = "intl")]
fn dec_add_units(digits: &mut alloc::vec::Vec<u8>, point: &mut usize, delta: u64) {
    let mut carry = delta;
    let mut i = digits.len();
    while carry > 0 && i > 0 {
        i -= 1;
        let sum = digits[i] as u64 + carry;
        digits[i] = (sum % 10) as u8;
        carry = sum / 10;
    }
    while carry > 0 {
        digits.insert(0, (carry % 10) as u8);
        carry /= 10;
        *point += 1;
    }
}

/// Subtract `delta` (≤ the represented value) at the units place of a big-endian
/// decimal digit vector.
#[cfg(feature = "intl")]
fn dec_sub_units(digits: &mut [u8], mut delta: u64) {
    let mut i = digits.len();
    while delta > 0 && i > 0 {
        i -= 1;
        let cur = digits[i] as i64 - (delta % 10) as i64;
        delta /= 10;
        if cur < 0 {
            digits[i] = (cur + 10) as u8;
            delta += 1;
        } else {
            digits[i] = cur as u8;
        }
    }
}

/// Round the **shortest round-trip decimal** of finite non-zero `n` to `keep_frac`
/// fraction digits (or, when `sig` is `Some`, to that many significant digits)
/// under `mode`, then snap to the nearest multiple of `increment` at the fraction
/// place. Returns a "clean" f64 whose exact binary expansion re-renders (through
/// `intl::number::format`) to the ECMA-402-correct digits, sidestepping the
/// crate's binary-expansion rounding boundary. See [`dec_round_up`].
#[cfg(feature = "intl")]
fn intl_decimal_round(
    n: f64,
    keep_frac: usize,
    sig: Option<usize>,
    increment: u32,
    mode: intl::number::RoundingMode,
) -> f64 {
    if !n.is_finite() || n == 0.0 {
        return n;
    }
    let negative = n.is_sign_negative();
    let abs = n.abs();
    let s = alloc::format!("{abs}");
    let (ip, fp) = s.split_once('.').unwrap_or((s.as_str(), ""));
    let mut digits: alloc::vec::Vec<u8> = ip.bytes().chain(fp.bytes()).map(|b| b - b'0').collect();
    let mut point = ip.len();
    if increment > 1 && sig.is_none() {
        // `roundingIncrement`: round `value × 10^keep_frac` to the nearest multiple
        // of `increment`, comparing the full discarded remainder against the
        // increment midpoint (no intermediate rounding to the fraction place, which
        // would double-round, e.g. 1.25 inc=2 → 1.2, not 1.4).
        let inc = increment as u64;
        // Pad so exactly `point + keep_frac` digits precede the cut.
        while digits.len() < point + keep_frac {
            digits.push(0);
        }
        let cut = point + keep_frac;
        // Remainder of the kept integer modulo the increment, and the quotient's
        // low bit (for ties-to-even on the multiple index).
        let mut rem_int: u64 = 0;
        let mut q_low: u64 = 0;
        for &d in &digits[..cut] {
            let cur = rem_int * 10 + d as u64;
            q_low = q_low.wrapping_mul(10).wrapping_add(cur / inc);
            rem_int = cur % inc;
        }
        // Fractional remainder below the cut, classified against 1/2.
        let first = digits.get(cut).copied().unwrap_or(0);
        let rest_nonzero = cut < digits.len() && digits[cut + 1..].iter().any(|&d| d != 0);
        let frac_pos = cut < digits.len() && (first != 0 || rest_nonzero);
        let frac_gt_half = first > 5 || (first == 5 && rest_nonzero);
        let frac_eq_half = first == 5 && !rest_nonzero;
        // Zero the fraction below the cut, keep the kept integer.
        digits.truncate(cut);
        let twice = rem_int * 2;
        let tie = {
            use intl::number::RoundingMode::*;
            match mode {
                Trunc | HalfTrunc => false,
                Floor => negative,
                Ceil => !negative,
                HalfFloor => negative,
                HalfCeil | HalfExpand | Expand => true,
                HalfEven => (q_low & 1) == 1,
            }
        };
        let up_snap = match twice.cmp(&inc) {
            core::cmp::Ordering::Greater => true,
            core::cmp::Ordering::Equal => {
                if frac_pos {
                    true
                } else {
                    tie
                }
            }
            core::cmp::Ordering::Less => {
                if inc - twice == 1 {
                    frac_gt_half || (frac_eq_half && tie)
                } else {
                    false
                }
            }
        };
        if up_snap {
            dec_add_units(&mut digits, &mut point, inc - rem_int);
        } else if rem_int > 0 {
            dec_sub_units(&mut digits, rem_int);
        }
    } else {
        let cut = if let Some(ms) = sig {
            match digits.iter().position(|&d| d != 0) {
                Some(fz) => (fz + ms).min(digits.len()),
                None => point,
            }
        } else {
            (point + keep_frac).min(digits.len())
        };
        let up = dec_round_up(&digits, cut, mode, negative);
        for d in digits.iter_mut().skip(cut).take(point.saturating_sub(cut)) {
            *d = 0;
        }
        digits.truncate(cut.max(point));
        if up {
            let mut i = cut;
            loop {
                if i == 0 {
                    digits.insert(0, 1);
                    point += 1;
                    break;
                }
                i -= 1;
                if digits[i] == 9 {
                    digits[i] = 0;
                } else {
                    digits[i] += 1;
                    break;
                }
            }
        }
    }
    let int_s: String = digits[..point]
        .iter()
        .map(|&d| (b'0' + d) as char)
        .collect();
    let frac_s: String = digits[point..]
        .iter()
        .map(|&d| (b'0' + d) as char)
        .collect();
    let mut out = String::new();
    if negative {
        out.push('-');
    }
    out.push_str(if int_s.is_empty() { "0" } else { &int_s });
    if !frac_s.is_empty() {
        out.push('.');
        out.push_str(&frac_s);
    }
    out.parse::<f64>().unwrap_or(n)
}

/// Whether a locale renders a negative `currencySign: "accounting"` amount with
/// parentheses (the CLDR root default, inherited by en/ja/ko/zh and most others)
/// rather than a leading minus (e.g. de-DE). Approximates the accounting pattern
/// the `intl` crate does not expose; the minus-sign locales are left to the crate.
#[cfg(feature = "intl")]
fn accounting_uses_parens(locale: &str) -> bool {
    let lang = locale
        .split(['-', '_'])
        .next()
        .unwrap_or(locale)
        .to_ascii_lowercase();
    // Locales known to keep a minus sign in their accounting pattern.
    !matches!(lang.as_str(), "de" | "nl" | "fi" | "hu" | "et")
}

/// `WeekdayToString` for `Intl.Locale`'s `firstDayOfWeek`: a weekday name
/// (`mon`…`sun`) or the 1–7 numeric form (1 = Monday) maps to the canonical name;
/// anything else is invalid.
fn weekday_to_string(s: &str) -> Option<String> {
    Some(String::from(match s {
        "1" => "mon",
        "2" => "tue",
        "3" => "wed",
        "4" => "thu",
        "5" => "fri",
        "6" => "sat",
        // Both 0 and 7 canonicalize to Sunday.
        "0" | "7" => "sun",
        // A non-numeric value is used verbatim if it is a valid `-u-fw` type
        // sequence (weekday names plus experimental calendars like `primidi`).
        other if is_unicode_type_value(other) => return Some(String::from(other)),
        _ => return None,
    }))
}

/// Split a BCP-47 locale into its base (with the requested `u`-extension keyword
/// removed) and the value of that keyword, if present. `key` is the two-letter
/// Unicode keyword (e.g. `"hc"`, `"nu"`, `"ca"`). Returns
/// `(locale_without_key, Some(value))` when the keyword is present, else
/// `(locale.to_owned(), None)`. Keys are exactly two chars; type values are
/// 3–8 chars, so the two are distinguishable within the `-u-` section.
fn split_u_keyword(locale: &str, key: &str) -> (String, Option<String>) {
    let segs: Vec<&str> = locale.split('-').collect();
    // Locate the `u` singleton extension (a length-1 segment equal to `u`).
    let mut ustart = None;
    for (i, s) in segs.iter().enumerate() {
        if s.len() == 1 && s.eq_ignore_ascii_case("u") {
            ustart = Some(i);
            break;
        }
    }
    let Some(ustart) = ustart else {
        return (String::from(locale), None);
    };
    // The `u` section runs until the next singleton (length-1) segment or end.
    let mut ext_end = ustart + 1;
    while ext_end < segs.len() && segs[ext_end].len() != 1 {
        ext_end += 1;
    }
    let is_key = |s: &str| s.len() == 2 && s.bytes().all(|b| b.is_ascii_alphanumeric());
    let mut found: Option<String> = None;
    let mut kept: Vec<&str> = Vec::new();
    let mut j = ustart + 1;
    while j < ext_end {
        let cur = segs[j];
        if is_key(cur) {
            // Gather this keyword's type values (3–8 char segments until the
            // next 2-char key or the section end).
            let mut k = j + 1;
            while k < ext_end && !is_key(segs[k]) {
                k += 1;
            }
            if cur.eq_ignore_ascii_case(key) {
                found = Some(segs[j + 1..k].join("-").to_ascii_lowercase());
            } else {
                kept.extend_from_slice(&segs[j..k]);
            }
            j = k;
        } else {
            // A leading attribute (no key) — preserve it.
            kept.push(cur);
            j += 1;
        }
    }
    if found.is_none() {
        return (String::from(locale), None);
    }
    let mut out: Vec<&str> = segs[..ustart].to_vec();
    if !kept.is_empty() {
        out.push("u");
        out.extend_from_slice(&kept);
    }
    out.extend_from_slice(&segs[ext_end..]);
    (out.join("-"), found)
}

/// Decide whether the dropped-tail `dropped` (the digits at and beyond the kept
/// precision) rounds the retained value up (away from zero), under `mode`.
#[cfg(feature = "intl")]
fn exact_round_up(dropped: &[u8], mode: intl::number::RoundingMode, neg: bool) -> bool {
    use intl::number::RoundingMode::*;
    if dropped.is_empty() {
        return false;
    }
    let first = dropped[0];
    let rest_nonzero = dropped[1..].iter().any(|&d| d != 0);
    let any_nonzero = first != 0 || rest_nonzero;
    match mode {
        Trunc => false,
        Expand => any_nonzero,
        Ceil => any_nonzero && !neg,
        Floor => any_nonzero && neg,
        HalfExpand => first >= 5,
        HalfTrunc => first > 5 || (first == 5 && rest_nonzero),
        HalfCeil => (first > 5 || (first == 5 && rest_nonzero)) || (first == 5 && !neg),
        HalfFloor => (first > 5 || (first == 5 && rest_nonzero)) || (first == 5 && neg),
        // Half-even (banker's) needs the last kept digit; approximate as halfExpand
        // (unused by the exact-decimal target tests).
        _ => first >= 5,
    }
}

/// Add one unit at the least-significant retained position of `int.frac`,
/// propagating the carry (grows `int` if it overflows).
#[cfg(feature = "intl")]
fn exact_increment(int: &mut alloc::vec::Vec<u8>, frac: &mut [u8]) {
    let mut carry = 1u8;
    for d in frac.iter_mut().rev() {
        let s = *d + carry;
        *d = s % 10;
        carry = s / 10;
        if carry == 0 {
            return;
        }
    }
    for d in int.iter_mut().rev() {
        let s = *d + carry;
        *d = s % 10;
        carry = s / 10;
        if carry == 0 {
            return;
        }
    }
    if carry > 0 {
        int.insert(0, carry);
    }
}

/// Map an ECMA-402 [`RoundingMode`](intl::number::RoundingMode) to the
/// `puremp::Decimal` [`Rounding`](puremp::decimal::Rounding) that reproduces it
/// exactly. `puremp` has no ties-toward-±∞ mode, so `halfCeil`/`halfFloor` are
/// resolved per the value's sign (`neg`) into ties-away-from / ties-toward zero —
/// exact because the sign fixes which of ±∞ the tie rounds toward.
#[cfg(feature = "intl")]
fn intl_mode_to_rounding(mode: intl::number::RoundingMode, neg: bool) -> puremp::decimal::Rounding {
    use intl::number::RoundingMode as M;
    use puremp::decimal::Rounding as R;
    match mode {
        M::Trunc => R::Down,
        M::Expand => R::Up,
        M::Floor => R::Floor,
        M::Ceil => R::Ceiling,
        M::HalfExpand => R::HalfUp,
        M::HalfTrunc => R::HalfDown,
        M::HalfEven => R::HalfEven,
        M::HalfCeil => {
            if neg {
                R::HalfDown
            } else {
                R::HalfUp
            }
        }
        M::HalfFloor => {
            if neg {
                R::HalfUp
            } else {
                R::HalfDown
            }
        }
    }
}

/// Exact ECMA-402 `ToRawPrecision` for a high-precision operand: rounds the exact
/// value `±int_part.frac_part` to `max_sig` significant digits (then pads trailing
/// zeros up to `min_sig`) using a `puremp::Decimal`, so the result is exact for
/// values beyond `f64` precision and every rounding mode — including half-even,
/// which the hand-rolled digit helpers only approximate. Returns the rounded
/// `(integer_digits, fraction_digits)` latn digit vectors for `assemble_decimal`.
#[cfg(feature = "intl")]
fn exact_significant_digits(
    neg: bool,
    int_part: &str,
    frac_part: &str,
    min_sig: Option<usize>,
    max_sig: Option<usize>,
    mode: intl::number::RoundingMode,
) -> Option<(alloc::vec::Vec<u8>, alloc::vec::Vec<u8>)> {
    use puremp::decimal::Decimal;
    // Reassemble the signed literal and parse it into an exact base-10 Decimal.
    let mut lit = String::new();
    if neg {
        lit.push('-');
    }
    lit.push_str(if int_part.is_empty() { "0" } else { int_part });
    if !frac_part.is_empty() {
        lit.push('.');
        lit.push_str(frac_part);
    }
    let d: Decimal = lit.parse().ok()?;
    let rounded = match max_sig {
        Some(m) => d.round_to_digits(m.max(1) as u32, intl_mode_to_rounding(mode, neg)),
        None => d,
    };
    // Expand the rounded Decimal to plain (non-scientific) int/frac digit runs.
    let s = alloc::format!("{}", rounded.abs());
    let (ip, fp) = s.split_once('.').unwrap_or((s.as_str(), ""));
    let mut int_digits: alloc::vec::Vec<u8> = ip.bytes().map(|b| b - b'0').collect();
    let mut frac_digits: alloc::vec::Vec<u8> = fp.bytes().map(|b| b - b'0').collect();
    if int_digits.is_empty() {
        int_digits.push(0);
    }
    // minimumSignificantDigits: pad trailing zeros so at least `min_sig` significant
    // digits (counting from the first nonzero, trailing zeros included) are present.
    if let Some(min_sig) = min_sig {
        let first = int_digits
            .iter()
            .chain(frac_digits.iter())
            .position(|&d| d != 0)
            .unwrap_or(int_digits.len());
        let sig_now = (int_digits.len() + frac_digits.len()).saturating_sub(first);
        if sig_now < min_sig {
            frac_digits.resize(frac_digits.len() + (min_sig - sig_now), 0);
        }
    }
    Some((int_digits, frac_digits))
}

/// Whether a compact-notation formatter carries no explicit digit options, so the
/// ECMA-402 default rounding (`roundingPriority: "morePrecision"` with
/// `maximumFractionDigits: 0` / `maximumSignificantDigits: 2`) applies and the
/// crate's plain one-fraction-digit mantissa must be re-rounded (see
/// [`compact_reround_parts`]). Any user-set fraction/significant digit count opts
/// out (the crate then honors the given precision).
#[cfg(feature = "intl")]
fn compact_wants_reround(opts: &intl::number::NumberFormatOptions) -> bool {
    matches!(opts.notation, intl::number::Notation::Compact)
        && opts.minimum_fraction_digits.is_none()
        && opts.maximum_fraction_digits.is_none()
        && opts.minimum_significant_digits.is_none()
        && opts.maximum_significant_digits.is_none()
}

/// Re-round the mantissa of a default compact-notation result to the ECMA-402
/// default precision. The crate renders the compact mantissa with a single
/// fraction digit (`987654321` → `987.7M`); the spec applies
/// `roundingPriority: "morePrecision"` over `maximumFractionDigits: 0` and
/// `maximumSignificantDigits: 2`, which for a mantissa whose most-significant
/// digit sits at decimal exponent `e` keeps `max(0, 1 - e)` fraction digits
/// (`987.65…M` → `988M`, `9.87…K` → `9.9K`, `0.159` → `0.16`). Operates on the
/// crate's tagged `(kind, value)` parts (latn digits, before numbering-system
/// substitution): rewrites the leading `integer`/`decimal`/`fraction` run in
/// place, leaving the sign and compact-suffix parts untouched. Grouped mantissae
/// (a `group` part right after the integer) are left alone — compact mantissae are
/// always < 1000 there, so no re-rounding is needed.
#[cfg(feature = "intl")]
fn compact_reround_parts(
    parts: &mut alloc::vec::Vec<(&'static str, String)>,
    mode: intl::number::RoundingMode,
) {
    let Some(int_idx) = parts.iter().position(|(k, _)| *k == "integer") else {
        return;
    };
    // Skip a grouped integer core (a `group` immediately after the integer): the
    // digits span several parts, but such a mantissa is >= 1000 with no fraction,
    // so `max(0, 1 - e)` is already 0 and nothing needs re-rounding.
    if parts.get(int_idx + 1).map(|(k, _)| *k) == Some("group") {
        return;
    }
    let neg = parts.iter().any(|(k, _)| *k == "minusSign");
    let int_str = parts[int_idx].1.clone();
    let (dec_idx, frac_idx) = if parts.get(int_idx + 1).map(|(k, _)| *k) == Some("decimal") {
        let f = if parts.get(int_idx + 2).map(|(k, _)| *k) == Some("fraction") {
            Some(int_idx + 2)
        } else {
            None
        };
        (Some(int_idx + 1), f)
    } else {
        (None, None)
    };
    let frac_str = frac_idx.map(|i| parts[i].1.clone()).unwrap_or_default();
    // Only re-round plain latn digits (kataan substitutes the numbering system
    // afterwards); bail on anything unexpected.
    if !int_str.bytes().all(|b| b.is_ascii_digit()) || !frac_str.bytes().all(|b| b.is_ascii_digit())
    {
        return;
    }
    // Decimal exponent `e` of the mantissa's most-significant digit.
    let int_stripped = int_str.trim_start_matches('0');
    let e: i32 = if !int_stripped.is_empty() {
        int_stripped.len() as i32 - 1
    } else {
        match frac_str.bytes().position(|b| b != b'0') {
            Some(p) => -(p as i32) - 1,
            None => 0,
        }
    };
    let keep = (1 - e).max(0) as usize;
    let mut int_d: alloc::vec::Vec<u8> = int_str.bytes().map(|b| b - b'0').collect();
    let mut frac_d: alloc::vec::Vec<u8> = frac_str.bytes().map(|b| b - b'0').collect();
    if frac_d.len() > keep {
        let up = exact_round_up(&frac_d[keep..], mode, neg);
        frac_d.truncate(keep);
        if up {
            exact_increment(&mut int_d, &mut frac_d);
        }
    }
    // Trim trailing fraction zeros (minimumFractionDigits defaults to 0).
    while frac_d.last() == Some(&0) {
        frac_d.pop();
    }
    while int_d.len() > 1 && int_d[0] == 0 {
        int_d.remove(0);
    }
    let new_int: String = int_d.iter().map(|d| (b'0' + d) as char).collect();
    let new_frac: String = frac_d.iter().map(|d| (b'0' + d) as char).collect();
    let dec_sep = dec_idx.map(|i| parts[i].1.clone());
    // Remove the old fraction then decimal (descending index keeps `int_idx` valid).
    if let Some(fi) = frac_idx {
        parts.remove(fi);
    }
    if let Some(di) = dec_idx {
        parts.remove(di);
    }
    parts[int_idx].1 = new_int;
    if !new_frac.is_empty() {
        let sep = dec_sep.unwrap_or_else(|| String::from("."));
        parts.insert(int_idx + 1, ("decimal", sep));
        parts.insert(int_idx + 2, ("fraction", new_frac));
    }
}

/// Split the separator whitespace off each `compact` part into its own `literal`
/// part, matching ECMA-402's parts shaping: a CLDR compact pattern like `"0 Mio."`
/// tags the space between the number and the suffix as a `literal` and only the
/// bare suffix as `compact` (`[integer "988"][literal "\u{a0}"][compact "Mio."]`).
/// The `intl` crate returns the leading/trailing whitespace fused into the
/// `compact` value; only the *outer* whitespace is peeled here (an internal space
/// in a multi-word suffix stays part of the `compact` token).
#[cfg(feature = "intl")]
fn split_compact_affix_parts(parts: &mut alloc::vec::Vec<(&'static str, String)>) {
    let needs = parts.iter().any(|(k, v)| {
        *k == "compact" && (v.starts_with(char::is_whitespace) || v.ends_with(char::is_whitespace))
    });
    if !needs {
        return;
    }
    let mut out: alloc::vec::Vec<(&'static str, String)> =
        alloc::vec::Vec::with_capacity(parts.len() + 2);
    for (k, v) in core::mem::take(parts) {
        if k != "compact" || v.trim().is_empty() {
            out.push((k, v));
            continue;
        }
        let lead_len = v.len() - v.trim_start().len();
        let core_end = v.trim_end().len();
        if lead_len > 0 {
            out.push(("literal", String::from(&v[..lead_len])));
        }
        out.push(("compact", String::from(&v[lead_len..core_end])));
        if core_end < v.len() {
            out.push(("literal", String::from(&v[core_end..])));
        }
    }
    *parts = out;
}

/// From a probe like `"1.1"` / `"-1.1"` (latn digits), split the leading prefix,
/// the decimal separator, and the trailing suffix.
#[cfg(feature = "intl")]
fn split_number_scaffold(probe: &str) -> (String, String, String) {
    let chars: alloc::vec::Vec<char> = probe.chars().collect();
    let mut i = 0;
    let mut prefix = String::new();
    while i < chars.len() && !chars[i].is_ascii_digit() {
        prefix.push(chars[i]);
        i += 1;
    }
    while i < chars.len() && chars[i].is_ascii_digit() {
        i += 1;
    }
    let mut sep = String::new();
    while i < chars.len() && !chars[i].is_ascii_digit() {
        sep.push(chars[i]);
        i += 1;
    }
    while i < chars.len() && chars[i].is_ascii_digit() {
        i += 1;
    }
    let suffix: String = chars[i..].iter().collect();
    (prefix, sep, suffix)
}

/// From a probe like `"1,111"` (latn), extract the first group separator that
/// appears between integer digit groups (`""` if the value isn't grouped).
#[cfg(feature = "intl")]
fn extract_group_sep(probe: &str) -> String {
    let chars: alloc::vec::Vec<char> = probe.chars().collect();
    let mut i = 0;
    while i < chars.len() && !chars[i].is_ascii_digit() {
        i += 1; // skip prefix
    }
    while i < chars.len() && chars[i].is_ascii_digit() {
        i += 1; // first integer group
    }
    let mut sep = String::new();
    while i < chars.len() && !chars[i].is_ascii_digit() {
        sep.push(chars[i]);
        i += 1;
    }
    sep
}

/// `ToRawFixed`: round the value `(int, frac)` to at most `max_frac` fraction
/// digits under `mode`. Returns the rounded `(int, frac)`.
#[cfg(feature = "intl")]
fn to_raw_fixed(
    neg: bool,
    mut int: alloc::vec::Vec<u8>,
    mut frac: alloc::vec::Vec<u8>,
    max_frac: usize,
    mode: intl::number::RoundingMode,
) -> (alloc::vec::Vec<u8>, alloc::vec::Vec<u8>) {
    if frac.len() > max_frac {
        let up = exact_round_up(&frac[max_frac..], mode, neg);
        frac.truncate(max_frac);
        if up {
            exact_increment(&mut int, &mut frac);
        }
    }
    (int, frac)
}

/// `ToRawPrecision`: round the value `(int, frac)` to at most `max_sig`
/// significant digits under `mode`. Returns the rounded `(int, frac)`.
#[cfg(feature = "intl")]
fn to_raw_precision(
    neg: bool,
    int: alloc::vec::Vec<u8>,
    frac: alloc::vec::Vec<u8>,
    max_sig: usize,
    mode: intl::number::RoundingMode,
) -> (alloc::vec::Vec<u8>, alloc::vec::Vec<u8>) {
    let mut digits: alloc::vec::Vec<u8> = int.iter().chain(frac.iter()).copied().collect();
    let mut point = int.len();
    let Some(first_sig) = digits.iter().position(|&d| d != 0) else {
        return (int, frac);
    };
    let last = first_sig + max_sig.max(1) - 1;
    if last + 1 < digits.len() {
        let up = exact_round_up(&digits[last + 1..], mode, neg);
        digits.truncate(last + 1);
        if up {
            let mut carry = 1u8;
            for d in digits.iter_mut().rev() {
                let s = *d + carry;
                *d = s % 10;
                carry = s / 10;
                if carry == 0 {
                    break;
                }
            }
            if carry > 0 {
                digits.insert(0, carry);
                point += 1;
            }
        }
    }
    while digits.len() < point {
        digits.push(0);
    }
    let int_out = digits[..point].to_vec();
    let frac_out = digits[point..].to_vec();
    (int_out, frac_out)
}

/// Group `int_str` (latn digits, no sign) into thousands with `sep`.
#[cfg(feature = "intl")]
fn group_thousands_sep(int_str: &str, sep: &str) -> String {
    if sep.is_empty() || int_str.len() <= 3 {
        return String::from(int_str);
    }
    let bytes = int_str.as_bytes();
    let mut out = String::new();
    let first = bytes.len() % 3;
    let first = if first == 0 { 3 } else { first };
    out.push_str(&int_str[..first]);
    let mut i = first;
    while i < bytes.len() {
        out.push_str(sep);
        out.push_str(&int_str[i..i + 3]);
        i += 3;
    }
    out
}

/// Whether `s` matches the UTS-35 `type` value production: one or more
/// `alphanum{3,8}` subtags joined by `-` (used for `ca`/`nu` option validation).
fn is_unicode_type_value(s: &str) -> bool {
    !s.is_empty()
        && s.split('-').all(|seg| {
            (3..=8).contains(&seg.len()) && seg.bytes().all(|b| b.is_ascii_alphanumeric())
        })
}

/// Whether `code` is a well-formed ISO-4217 currency code: exactly three ASCII
/// letters (case-insensitive).
fn is_well_formed_currency(code: &str) -> bool {
    code.len() == 3 && code.bytes().all(|b| b.is_ascii_alphabetic())
}

/// The ECMA-402 sanctioned single-unit identifiers (Table: "Single units
/// sanctioned for use in ECMAScript"). Shared by `Intl.supportedValuesOf("unit")`
/// and `IsWellFormedUnitIdentifier`.
pub(crate) const SANCTIONED_UNITS: &[&str] = &[
    "acre",
    "bit",
    "byte",
    "celsius",
    "centimeter",
    "day",
    "degree",
    "fahrenheit",
    "fluid-ounce",
    "foot",
    "gallon",
    "gigabit",
    "gigabyte",
    "gram",
    "hectare",
    "hour",
    "inch",
    "kilobit",
    "kilobyte",
    "kilogram",
    "kilometer",
    "liter",
    "megabit",
    "megabyte",
    "meter",
    "microsecond",
    "mile",
    "mile-scandinavian",
    "milliliter",
    "millimeter",
    "millisecond",
    "minute",
    "month",
    "nanosecond",
    "ounce",
    "percent",
    "petabyte",
    "pound",
    "second",
    "stone",
    "terabit",
    "terabyte",
    "week",
    "yard",
    "year",
];

/// `IsWellFormedUnitIdentifier(unit)`: a sanctioned single unit, or a
/// `<numerator>-per-<denominator>` compound of two sanctioned single units.
fn is_well_formed_unit(unit: &str) -> bool {
    let valid_single = |u: &str| SANCTIONED_UNITS.contains(&u);
    match unit.split_once("-per-") {
        Some((a, b)) => valid_single(a) && valid_single(b),
        None => valid_single(unit),
    }
}

/// Validates and canonicalizes a BCP-47 / UTS-35 `unicode_locale_id` (the
/// structural grammar, without CLDR alias/grandfathered replacement which the
/// `intl` crate doesn't expose). Returns the canonical tag, or `None` if the tag
/// is structurally invalid (the caller raises a `RangeError`).
///
/// Canonicalization performed: lowercase language; Titlecase script; UPPERCASE
/// region; lowercase variants **sorted** alphabetically; extensions ordered by
/// singleton (`x` private-use last); `-u-`/`-t-` keyword/field groups sorted by
/// key in ASCII order. Not performed (needs CLDR data the crate omits): legacy
/// language/region alias replacement, `-u-` type alias mapping (e.g. `yes`→`true`).
/// UTS-35 §3.3.1 "regular" grandfathered tag → canonical replacement, matched
/// on the lowercased language(-variant) base. This is the fixed set of regular
/// grandfathered tags that remain structurally valid Unicode BCP-47 locale ids
/// (their preferred replacement changes the primary language subtag).
fn grandfathered_canonical(base: &str) -> Option<&'static str> {
    Some(match base {
        "art-lojban" => "jbo",
        "cel-gaulish" => "xtg",
        "zh-guoyu" => "zh",
        "zh-hakka" => "hak",
        "zh-xiang" => "hsn",
        _ => return None,
    })
}

/// CLDR `bcp47` Unicode-extension type-value alias → canonical value, for the
/// `(key, value)` pairs ECMA-402 exercises. Deprecated type values (e.g.
/// `ca-islamicc`, `ks-primary`, `ms-imperial`) canonicalize to their preferred
/// forms. `value` is the full (hyphen-joined) type. Returns `None` when the pair
/// has no alias.
pub(crate) fn unicode_type_alias(key: &str, value: &str) -> Option<&'static str> {
    Some(match (key, value) {
        // -u-ca- (calendar)
        ("ca", "ethiopic-amete-alem") => "ethioaa",
        ("ca", "islamicc") => "islamic-civil",
        // -u-ks- (collation strength)
        ("ks", "primary") => "level1",
        ("ks", "tertiary") => "level3",
        // -u-ms- (measurement system)
        ("ms", "imperial") => "uksystem",
        // -u-tz- (time zone) and -u-rg-/-u-sd- (region override / subdivision):
        // the CLDR deprecation tables, generated into `intl_aliases`.
        ("tz", v) => return super::intl_aliases::lookup(super::intl_aliases::TIMEZONE, v),
        ("rg" | "sd", v) => {
            return super::intl_aliases::lookup(super::intl_aliases::SUBDIVISION, v);
        }
        _ => return None,
    })
}

/// The preferred form of a `-t-` *tvalue* (`m0-names` → `m0-prprname`), from
/// CLDR's `bcp47/transform.xml` deprecation aliases.
pub(crate) fn transform_value_alias(value: &str) -> Option<&'static str> {
    super::intl_aliases::lookup(super::intl_aliases::TRANSFORM_VALUE, value)
}

/// Extracts the `-u-ca-<calendar>` value from a BCP-47 locale tag, if present
/// (e.g. `"en-u-ca-iso8601"` → `Some("iso8601")`, `"ja-u-ca-islamic-civil"` →
/// `Some("islamic-civil")`). The calendar value may span several `-`-joined
/// subtags (each ≥ 3 chars); collection stops at the next 2-char extension key.
#[cfg(feature = "intl")]
pub(crate) fn locale_unicode_calendar(locale: &str) -> Option<String> {
    let lower = locale.to_ascii_lowercase();
    let subtags: Vec<&str> = lower.split('-').collect();
    // Find the "-u-" singleton extension, then the "ca" key inside it.
    let mut i = 0;
    while i < subtags.len() {
        if subtags[i] == "u" {
            let mut j = i + 1;
            while j < subtags.len() && subtags[j] != "u" {
                if subtags[j] == "ca" {
                    let mut parts = Vec::new();
                    let mut k = j + 1;
                    while k < subtags.len() && subtags[k].len() >= 3 {
                        parts.push(subtags[k]);
                        k += 1;
                    }
                    if parts.is_empty() {
                        return None;
                    }
                    let value = parts.join("-");
                    return Some(
                        unicode_type_alias("ca", &value)
                            .map(String::from)
                            .unwrap_or(value),
                    );
                }
                j += 1;
            }
        }
        i += 1;
    }
    None
}

/// Extracts the `-u-<key>-<value>` type value from a BCP-47 locale tag, if
/// present (e.g. `locale_unicode_keyword("en-u-nu-arab", "nu")` → `Some("arab")`).
/// The value may span several `-`-joined subtags (each ≥ 3 chars); collection
/// stops at the next 2-char extension key or the end of the `-u-` group.
pub(crate) fn locale_unicode_keyword(locale: &str, key: &str) -> Option<String> {
    let lower = locale.to_ascii_lowercase();
    let subtags: Vec<&str> = lower.split('-').collect();
    let mut i = 0;
    while i < subtags.len() {
        // A private-use singleton (`x`) starts private subtags; a `-u-` after it
        // is not a real extension (`de-x-u-co-phonebk` has no `co` keyword).
        if subtags[i] == "x" {
            break;
        }
        if subtags[i] == "u" {
            let mut j = i + 1;
            while j < subtags.len() && subtags[j].len() != 1 {
                if subtags[j] == key {
                    let mut parts = Vec::new();
                    let mut k = j + 1;
                    while k < subtags.len() && subtags[k].len() >= 3 {
                        parts.push(subtags[k]);
                        k += 1;
                    }
                    if parts.is_empty() {
                        return None;
                    }
                    return Some(parts.join("-"));
                }
                j += 1;
            }
        }
        i += 1;
    }
    None
}

/// Extracts a boolean Unicode `-u-<key>-` keyword (e.g. `kn`) from a locale tag:
/// a bare key or an explicit `true` → `Some(true)`, `false` → `Some(false)`, the
/// key being absent → `None`. Handles the canonical form where a `true` value is
/// dropped (`en-u-kn-true` → `en-u-kn`).
pub(crate) fn locale_unicode_bool_keyword(locale: &str, key: &str) -> Option<bool> {
    let lower = locale.to_ascii_lowercase();
    let subs: Vec<&str> = lower.split('-').collect();
    let mut i = 0;
    while i < subs.len() {
        if subs[i] == "x" {
            break;
        }
        if subs[i] == "u" {
            let mut j = i + 1;
            while j < subs.len() && subs[j].len() != 1 {
                if subs[j] == key {
                    return Some(match subs.get(j + 1) {
                        Some(v) if v.len() >= 3 => *v != "false",
                        _ => true,
                    });
                }
                j += 1;
            }
        }
        i += 1;
    }
    None
}

/// Whether `co` is a collation type the engine treats as a supported
/// `Intl.Collator` collation (the CLDR collation identifiers, excluding the
/// reserved `standard`/`search` values that are never valid `-u-co-` types).
pub(crate) fn is_supported_collation(co: &str) -> bool {
    matches!(
        co,
        "compat"
            | "dict"
            | "emoji"
            | "eor"
            | "phonebk"
            | "phonetic"
            | "pinyin"
            | "searchjl"
            | "stroke"
            | "trad"
            | "unihan"
            | "zhuyin"
    )
}

/// Zero-pads the minute/second fields of crate-produced `DateTimePart`s to two
/// digits when combined with another time field, and maps each to its ECMA-402
/// `(type, value)` part. CLDR/ICU has no single-digit `m`/`s` time format:
/// whenever minute or second appears alongside another time field it renders
/// 2-digit even for the `"numeric"` option (the crate widens by option only).
/// Shared by the `Date` and Temporal DateTimeFormat rendering paths.
#[cfg(feature = "intl")]
fn dtf_pad_time_parts(
    parts: alloc::vec::Vec<intl::datetime::DateTimePart>,
) -> Vec<(&'static str, String)> {
    use intl::datetime::DateTimePartType;
    let has_hour = parts.iter().any(|p| p.kind == DateTimePartType::Hour);
    let has_min = parts.iter().any(|p| p.kind == DateTimePartType::Minute);
    let has_sec = parts.iter().any(|p| p.kind == DateTimePartType::Second);
    parts
        .into_iter()
        .map(|p| {
            let mut v = p.value;
            let widen = match p.kind {
                DateTimePartType::Minute => has_hour || has_sec,
                DateTimePartType::Second => has_hour || has_min,
                _ => false,
            };
            if widen && v.len() == 1 && v.as_bytes()[0].is_ascii_digit() {
                v.insert(0, '0');
            }
            (p.kind.as_str(), v)
        })
        .collect()
}

/// The sexagenary (stem-branch) cyclic year name for the 1-based cyclic index
/// `cyclic1` (1..=60), e.g. `36` → `己亥`. The 60-name cycle is the ten heavenly
/// stems crossed with the twelve earthly branches; the Han characters are the
/// canonical (and CLDR `zh`) forms. Used for the `yearName` part of Chinese/Dangi
/// lunisolar dates (the crate's localized cyclic names are not public).
#[cfg(feature = "intl")]
fn sexagenary_year_name(cyclic1: i64) -> String {
    const STEMS: [&str; 10] = ["", "", "", "", "", "", "", "", "", ""];
    const BRANCHES: [&str; 12] = [
        "", "", "", "", "", "", "", "", "", "", "", "",
    ];
    let i = (cyclic1 - 1).rem_euclid(60);
    alloc::format!(
        "{}{}",
        STEMS[(i % 10) as usize],
        BRANCHES[(i % 12) as usize]
    )
}

/// The complete set of CLDR numbering-system identifiers (BCP-47 `nu` types).
/// Mirrors ECMA-402's `AvailableNumberingSystems`; used by
/// `Intl.supportedValuesOf("numberingSystem")` and to validate the
/// `numberingSystem` option against known systems in the Intl formatters.
pub(crate) const NUMBERING_SYSTEMS: &[&str] = &[
    "adlm", "ahom", "arab", "arabext", "armn", "armnlow", "bali", "beng", "bhks", "brah", "cakm",
    "cham", "cyrl", "deva", "diak", "ethi", "finance", "fullwide", "gara", "geor", "gong", "gonm",
    "grek", "greklow", "gujr", "gukh", "guru", "hanidays", "hanidec", "hans", "hansfin", "hant",
    "hantfin", "hebr", "hmng", "hmnp", "java", "jpan", "jpanfin", "jpanyear", "kali", "kawi",
    "khmr", "knda", "krai", "lana", "lanatham", "laoo", "latn", "lepc", "limb", "mathbold",
    "mathdbl", "mathmono", "mathsanb", "mathsans", "mlym", "modi", "mong", "mroo", "mtei", "mymr",
    "mymrepka", "mymrpao", "mymrshan", "mymrtlng", "nagm", "native", "newa", "nkoo", "olck",
    "onao", "orya", "osma", "outlined", "rohg", "roman", "romanlow", "saur", "segment", "shrd",
    "sind", "sinh", "sora", "sund", "sunu", "takr", "talu", "taml", "tamldec", "tnsa", "telu",
    "thai", "tirh", "tibt", "tols", "traditio", "vaii", "wara", "wcho",
];

/// Whether `nu` is a known CLDR numbering-system identifier.
pub(crate) fn is_known_numbering_system(nu: &str) -> bool {
    NUMBERING_SYSTEMS.contains(&nu)
}

/// `floor(log10(a))` for `a > 0`, computed by scaling so it is exact at powers
/// of ten (`log10(1e6)` can round to just under 6.0 in `f64`).
#[cfg(feature = "intl")]
fn decimal_magnitude(a: f64) -> i32 {
    if a <= 0.0 || !a.is_finite() {
        return 0;
    }
    let mut m = 0i32;
    let mut x = a;
    if x >= 1.0 {
        while x >= 10.0 {
            x /= 10.0;
            m += 1;
        }
    } else {
        while x < 1.0 {
            x *= 10.0;
            m -= 1;
        }
    }
    m
}

/// Builds the plural-operand string carrying the compact-decimal exponent for a
/// non-standard `notation`, in the crate's `<mantissa>c<exp>` form (e.g. `1.5e6`
/// in compact → `"1.5c6"`). Returns `None` for standard notation or when no
/// exponent applies (so the caller uses the plain decimal). `compact`/
/// `engineering` use exponents that are multiples of three; `scientific` uses
/// the full magnitude; compact suppresses exponents below 1000.
#[cfg(feature = "intl")]
fn plural_notation_operand_string(n: f64, notation: &str) -> Option<String> {
    let a = n.abs();
    if a == 0.0 || !a.is_finite() {
        return None;
    }
    let mag = decimal_magnitude(a);
    let e = match notation {
        "scientific" => mag,
        "engineering" => (mag as f64 / 3.0).floor() as i32 * 3,
        "compact" => {
            if mag < 3 {
                0
            } else {
                (mag as f64 / 3.0).floor() as i32 * 3
            }
        }
        _ => return None,
    };
    if e <= 0 {
        return None;
    }
    let mantissa = a / libm_pow10(e);
    Some(alloc::format!("{mantissa}c{e}"))
}

/// `10^e` for a small non-negative exponent, exact for the range used by
/// compact/scientific plural operands.
#[cfg(feature = "intl")]
fn libm_pow10(e: i32) -> f64 {
    let mut p = 1.0f64;
    for _ in 0..e {
        p *= 10.0;
    }
    p
}

/// The CLDR default numbering system for `locale` (e.g. `ar` → `arab`), derived
/// from the shape of its zero digit. Honors an explicit `-u-nu-` extension via
/// the crate. Falls back to `latn`.
pub(crate) fn default_numbering_for_locale_str(locale: &str) -> String {
    #[cfg(feature = "intl")]
    {
        let zero = intl::number::format_decimal_native(locale, 0.0);
        if let Some(c) = zero.chars().next()
            && let Some(name) = numbering_system_name_from_zero(c)
        {
            return String::from(name);
        }
    }
    let _ = locale;
    String::from("latn")
}

/// ResolveLocale for the Unicode `nu` key. Given the extension-free `base`, the
/// full requested `locale` (for its `-u-nu-` extension), and the validated
/// `option`, returns `(resolved_value, extension_addition)`. The addition is
/// non-empty (e.g. `"-nu-arab"`) only when the value is sourced from the
/// extension; an option-sourced override drops it. Values that are not known
/// numbering systems are ignored (falling back to the locale default).
pub(crate) fn resolve_nu_key(base: &str, locale: &str, option: Option<&str>) -> (String, String) {
    // The generic/algorithmic aliases (`native`, `traditio`, `finance`) are valid
    // `nu` type values but have no fixed digit mapping, so ECMA-402 does not treat
    // them as selectable numbering systems: a `-u-nu-native` (etc.) request is
    // ignored, falling back to the locale default.
    let selectable = |nu: &str| {
        is_known_numbering_system(nu) && !matches!(nu, "native" | "traditio" | "finance")
    };
    let mut value = default_numbering_for_locale_str(base);
    let mut addition = String::new();
    #[cfg(feature = "intl")]
    if let Some(ext) = locale_unicode_keyword(locale, "nu")
        && selectable(&ext)
    {
        value = ext.clone();
        addition = alloc::format!("-nu-{ext}");
    }
    let _ = locale;
    if let Some(opt) = option
        && selectable(opt)
        && opt != value
    {
        value = String::from(opt);
        addition = String::new();
    }
    (value, addition)
}

/// The calendar identifiers ECMA-402 `AvailableCalendars` reports (the CLDR
/// BCP-47 calendar types with fixed semantics). Used to validate a `calendar`
/// option / `-u-ca-` extension value: an unrecognized one is ignored.
pub(crate) const AVAILABLE_CALENDARS: [&str; 16] = [
    "buddhist",
    "chinese",
    "coptic",
    "dangi",
    "ethioaa",
    "ethiopic",
    "gregory",
    "hebrew",
    "indian",
    "islamic-civil",
    "islamic-tbla",
    "islamic-umalqura",
    "iso8601",
    "japanese",
    "persian",
    "roc",
];

/// Canonicalize a calendar identifier: lowercase it, apply the CLDR type alias
/// (`islamicc` → `islamic-civil`, `ethiopic-amete-alem` → `ethioaa`), then map the
/// deprecated `islamic`/`islamic-rgsa` to a concrete available calendar
/// (`islamic-civil`, per CreateDateTimeFormat step 9).
pub(crate) fn canonicalize_calendar(value: &str) -> String {
    let lower = value.to_ascii_lowercase();
    let canon = unicode_type_alias("ca", &lower)
        .map(String::from)
        .unwrap_or(lower);
    match canon.as_str() {
        "islamic" | "islamic-rgsa" => String::from("islamic-civil"),
        _ => canon,
    }
}

/// ResolveLocale for the Unicode `ca` (calendar) key, mirroring [`resolve_nu_key`].
/// Returns `(resolved_calendar, extension_addition)`. An unavailable calendar
/// (option or extension) is ignored; the addition is kept only when the value is
/// extension-sourced.
pub(crate) fn resolve_ca_key(_base: &str, locale: &str, option: Option<&str>) -> (String, String) {
    let mut value = String::from("gregory");
    let mut addition = String::new();
    #[cfg(feature = "intl")]
    if let Some(ext) = locale_unicode_calendar(locale) {
        let ext = canonicalize_calendar(&ext);
        if AVAILABLE_CALENDARS.contains(&ext.as_str()) {
            addition = alloc::format!("-ca-{ext}");
            value = ext;
        }
    }
    let _ = locale;
    if let Some(opt) = option {
        let opt = canonicalize_calendar(opt);
        if AVAILABLE_CALENDARS.contains(&opt.as_str()) && opt != value {
            value = opt;
            addition = String::new();
        }
    }
    (value, addition)
}

/// Removes the `-u-`/`-t-`/`-x-` singleton extensions (and everything after the
/// first singleton subtag) from a canonical BCP-47 tag, leaving just the
/// language/script/region/variant core — the base for ResolveLocale's
/// resolved-locale reconstruction.
pub(crate) fn strip_unicode_extension(locale: &str) -> String {
    let mut out: Vec<&str> = Vec::new();
    for sub in locale.split('-') {
        if sub.len() == 1 {
            break; // a singleton subtag (u/t/x/…) starts the extensions
        }
        out.push(sub);
    }
    out.join("-")
}

/// Reconstructs a resolved locale from its extension-free `base` and the list of
/// per-key Unicode-extension additions (each already rendered, e.g. `"-nu-arab"`,
/// `"-kn"`, `"-kf-upper"`). Keys are emitted in canonical (alphabetical) order.
/// An empty addition list yields the bare `base`.
pub(crate) fn build_resolved_locale(base: &str, additions: &[String]) -> String {
    let mut adds: Vec<&String> = additions.iter().filter(|a| !a.is_empty()).collect();
    if adds.is_empty() {
        return String::from(base);
    }
    adds.sort();
    let mut out = String::from(base);
    out.push_str("-u");
    for a in adds {
        out.push_str(a);
    }
    out
}

/// ECMA-402 `CanonicalizeUnicodeLocaleId(tag)`: validate `tag` as a structurally
/// valid language tag, then return its full canonical form.
///
/// Structure and case are canonicalized by the strict local grammar
/// ([`canonicalize_locale_id_structural`]) — which, unlike `intl`'s looser BCP-47
/// parser, rejects `_` separators, 4-alpha "languages", extlang subtags, and
/// duplicate variant/singleton subtags, sorts variant subtags, and applies the
/// `-u-`/`-t-` extension canonicalization (attribute/keyword sort, `true`/`yes`
/// elision only for the keys that alias it). On top of that, the deprecated
/// **base** subtags (language / script / region / variant, plus grandfathered
/// whole-tag forms) are substituted with their CLDR-canonical replacements via
/// `intl::locale::canonicalize` — the alias corpus (aliases.bin) newly shipped by
/// intl 0.5.1. Extension (`-u-`/`-t-`) subtags keep the local canonicalization,
/// which is more complete/correct here than intl 0.5.1 (which mis-drops `ka-yes`
/// and lacks `rg`/`sd` subdivision aliases). Returns `None` (→ **RangeError**)
/// when the tag is not structurally valid.
/// Canonicalizes a `-t-` extension's *tlang* — structural normalization plus the
/// CLDR base-subtag alias corpus, since a tlang is itself a language tag. Kept
/// separate from [`canonicalize_locale_id`] because a tlang carries no extensions
/// of its own, so there is no suffix to split off.
fn canonicalize_tlang(tlang: &str) -> Option<String> {
    let structural = canonicalize_locale_id_structural(tlang)?;
    #[cfg(feature = "intl")]
    {
        Some(
            intl::locale::canonicalize(&structural)
                .and_then(|t| canonicalize_locale_id_structural(&t))
                .unwrap_or(structural),
        )
    }
    #[cfg(not(feature = "intl"))]
    {
        Some(structural)
    }
}

pub(crate) fn canonicalize_locale_id(tag: &str) -> Option<String> {
    // Strict structural validation + local canonical form (rejects invalid tags,
    // e.g. `i-klingon`, → RangeError).
    let structural = canonicalize_locale_id_structural(tag)?;
    // Split off the extension/private-use suffix (the first length-1 subtag onward)
    // — its local canonicalization is authoritative; only the base takes intl's
    // alias substitution.
    let subs: Vec<&str> = structural.split('-').collect();
    let ext_at = subs.iter().position(|s| s.len() == 1).unwrap_or(subs.len());
    let base = subs[..ext_at].join("-");
    // Apply the CLDR base-subtag alias corpus, then re-run the strict canonicalizer
    // so any alias-introduced subtags are re-normalized (variant re-sort, casing).
    // The alias corpus is `intl` data; without that feature the structural
    // canonical form stands on its own (no alias substitution, still valid).
    #[cfg(feature = "intl")]
    let aliased_base = intl::locale::canonicalize(&base)
        .and_then(|b| canonicalize_locale_id_structural(&b))
        .unwrap_or(base);
    #[cfg(not(feature = "intl"))]
    let aliased_base = base;
    if ext_at == subs.len() {
        Some(aliased_base)
    } else {
        Some(alloc::format!(
            "{aliased_base}-{}",
            subs[ext_at..].join("-")
        ))
    }
}

fn canonicalize_locale_id_structural(tag: &str) -> Option<String> {
    // Structurally rejected outright: empty, non-ASCII, `_` separators, and the
    // empty subtags produced by leading/trailing/doubled `-`.
    if tag.is_empty() || !tag.is_ascii() || tag.contains('_') {
        return None;
    }
    // UTS-35 §3.3.1 "regular" grandfathered tags: the language(-variant) base is
    // replaced by its canonical form (the irregular `i-*`/`sgn-*`/`en-gb-oed`
    // grandfathered forms are structurally invalid and rejected by the grammar
    // below). Extensions, if any, are preserved; the substituted tag is then
    // canonicalized normally.
    {
        let lower = tag.to_ascii_lowercase();
        let subs: Vec<&str> = lower.split('-').collect();
        let base_end = subs.iter().position(|p| p.len() == 1).unwrap_or(subs.len());
        if let Some(repl) = grandfathered_canonical(&subs[..base_end].join("-")) {
            let mut rebuilt = String::from(repl);
            for p in &subs[base_end..] {
                rebuilt.push('-');
                rebuilt.push_str(p);
            }
            return canonicalize_locale_id_structural(&rebuilt);
        }
    }
    let parts: Vec<&str> = tag.split('-').collect();
    if parts.iter().any(|p| p.is_empty()) {
        return None;
    }
    let is_alpha = |s: &str| s.bytes().all(|b| b.is_ascii_alphabetic());
    let is_digit = |s: &str| s.bytes().all(|b| b.is_ascii_digit());
    let is_alnum = |s: &str| s.bytes().all(|b| b.is_ascii_alphanumeric());

    let mut idx = 0usize;
    let n = parts.len();

    // unicode_language_subtag = alpha{2,3} | alpha{5,8}  (NOT 4, NOT extlang).
    let lang = parts[idx];
    if !((2..=3).contains(&lang.len()) || (5..=8).contains(&lang.len())) || !is_alpha(lang) {
        return None;
    }
    let language = lang.to_ascii_lowercase();
    idx += 1;
    // No extlang subtags allowed in UTS-35: a 3-alpha subtag after a 2-3 alpha
    // language would be an extlang (BCP-47) — invalid here.
    if idx < n && is_alpha(parts[idx]) && parts[idx].len() == 3 {
        return None;
    }

    // unicode_script_subtag = alpha{4}.
    let mut script = None;
    if idx < n && parts[idx].len() == 4 && is_alpha(parts[idx]) {
        let s = parts[idx];
        let mut t = String::new();
        for (i, c) in s.chars().enumerate() {
            if i == 0 {
                t.push(c.to_ascii_uppercase());
            } else {
                t.push(c.to_ascii_lowercase());
            }
        }
        script = Some(t);
        idx += 1;
    }

    // unicode_region_subtag = alpha{2} | digit{3}.
    let mut region = None;
    if idx < n
        && ((parts[idx].len() == 2 && is_alpha(parts[idx]))
            || (parts[idx].len() == 3 && is_digit(parts[idx])))
    {
        region = Some(parts[idx].to_ascii_uppercase());
        idx += 1;
    }

    // unicode_variant_subtag = (alphanum{5,8} | digit alphanum{3}).
    let mut variants: Vec<String> = Vec::new();
    while idx < n {
        let s = parts[idx];
        let is_variant = ((5..=8).contains(&s.len()) && is_alnum(s))
            || (s.len() == 4 && s.as_bytes()[0].is_ascii_digit() && is_alnum(s));
        if !is_variant {
            break;
        }
        let v = s.to_ascii_lowercase();
        if variants.contains(&v) {
            return None; // duplicate variant
        }
        variants.push(v);
        idx += 1;
    }
    variants.sort();

    // Extensions and private use: singleton (alphanum) followed by subtags.
    // Each singleton may appear once; `u`/`t` have their own subtag grammars but
    // we validate generically and canonicalize key ordering for `u` and `t`.
    let mut extensions: Vec<(char, String)> = Vec::new();
    let mut seen_singletons: Vec<char> = Vec::new();
    while idx < n {
        let sing = parts[idx];
        if sing.len() != 1 || !sing.as_bytes()[0].is_ascii_alphanumeric() {
            return None; // expected a singleton here
        }
        let singleton = sing.as_bytes()[0].to_ascii_lowercase() as char;
        if seen_singletons.contains(&singleton) {
            return None; // duplicate singleton
        }
        seen_singletons.push(singleton);
        idx += 1;
        // Gather this singleton's subtags. Private use (`x`) consumes *all* the
        // remaining subtags, including length-1 ones (so `x-u-foo` is one private
        // sequence, not the start of a `u` extension).
        let mut subs: Vec<String> = Vec::new();
        let private = singleton == 'x';
        while idx < n && (private || parts[idx].len() != 1) {
            let st = parts[idx];
            // Private-use subtags: 1..=8 alphanum. Others: 2..=8 alphanum.
            let min = if private { 1 } else { 2 };
            if !((min..=8).contains(&st.len()) && is_alnum(st)) {
                return None;
            }
            subs.push(st.to_ascii_lowercase());
            idx += 1;
        }
        // A singleton with a length-1 subtag (private use excepted) is invalid,
        // and any singleton must have at least one subtag.
        if subs.is_empty() {
            return None;
        }
        let body = canonicalize_extension(singleton, &subs)?;
        extensions.push((singleton, body));
    }
    // UTS-35 forbids a `unicode_locale_id` consisting only of private use; a
    // valid id must have a real language (we already required one), so this is
    // satisfied. Extensions sort by singleton with `x` last.
    extensions.sort_by_key(|(s, _)| (*s == 'x', *s));

    let mut out = language;
    if let Some(s) = script {
        out.push('-');
        out.push_str(&s);
    }
    if let Some(r) = region {
        out.push('-');
        out.push_str(&r);
    }
    for v in &variants {
        out.push('-');
        out.push_str(v);
    }
    for (_, body) in &extensions {
        out.push('-');
        out.push_str(body);
    }
    Some(out)
}

/// Validates + canonicalizes the `code` argument to
/// `Intl.DisplayNames.prototype.of` against its instance `[[Type]]` (ECMA-402
/// `CanonicalizeDisplayNamesType`-adjacent `of` grammar). Returns the
/// canonical code, or `None` when `code` does not match the type's grammar (the
/// caller raises a `RangeError`). Unknown types pass through unchanged.
pub(crate) fn validate_display_code(ty: &str, code: &str) -> Option<String> {
    let is_alpha = |s: &str| !s.is_empty() && s.bytes().all(|b| b.is_ascii_alphabetic());
    let is_digit = |s: &str| !s.is_empty() && s.bytes().all(|b| b.is_ascii_digit());
    let is_alnum = |s: &str| !s.is_empty() && s.bytes().all(|b| b.is_ascii_alphanumeric());
    match ty {
        // unicode_language_id: a structurally valid tag with NO extension/private
        // singletons (a length-1 subtag such as `u` in `en-u-hebrew` is invalid).
        "language" => {
            if code.split('-').any(|p| p.len() == 1) {
                return None;
            }
            canonicalize_locale_id(code)
        }
        // unicode_region_subtag = alpha{2} | digit{3}.
        "region" => {
            if code.len() == 2 && is_alpha(code) {
                Some(code.to_ascii_uppercase())
            } else if code.len() == 3 && is_digit(code) {
                Some(String::from(code))
            } else {
                None
            }
        }
        // unicode_script_subtag = alpha{4} (title-cased).
        "script" => {
            if code.len() == 4 && is_alpha(code) {
                let mut t = String::new();
                for (i, c) in code.chars().enumerate() {
                    if i == 0 {
                        t.push(c.to_ascii_uppercase());
                    } else {
                        t.push(c.to_ascii_lowercase());
                    }
                }
                Some(t)
            } else {
                None
            }
        }
        // IsWellFormedCurrencyCode: 3 ASCII letters (upper-cased).
        "currency" => (code.len() == 3 && is_alpha(code)).then(|| code.to_ascii_uppercase()),
        // `type` nonterminal: alphanum{3,8} (-alphanum{3,8})* (lower-cased).
        "calendar" => {
            if code
                .split('-')
                .all(|p| (3..=8).contains(&p.len()) && is_alnum(p))
            {
                Some(code.to_ascii_lowercase())
            } else {
                None
            }
        }
        // A fixed enumeration (ES2023 Table of date-time field codes).
        "dateTimeField" => {
            const FIELDS: &[&str] = &[
                "era",
                "year",
                "quarter",
                "month",
                "weekOfYear",
                "weekday",
                "day",
                "dayPeriod",
                "hour",
                "minute",
                "second",
                "timeZoneName",
            ];
            FIELDS.contains(&code).then(|| String::from(code))
        }
        _ => Some(String::from(code)),
    }
}

/// Canonicalizes one extension's subtags into its `singleton-subtag-…` body.
/// For `u`/`t` the keyword/field groups are sorted by key (ASCII order); for
/// other singletons (and private use `x`) the order is preserved.
fn canonicalize_extension(singleton: char, subs: &[String]) -> Option<String> {
    if singleton == 'u' {
        // -u- = attribute* (key type*)* — attributes (length-2..8, but a *key*
        // is exactly length 2) come first, then keyword groups keyed by a
        // 2-char key. Group the trailing keyword sequences and sort by key.
        let mut attributes: Vec<String> = Vec::new();
        let mut i = 0;
        // A `key` is exactly 2 chars: alphanum then alpha (e.g. `ca`, `nu`, `0c`).
        // An `attribute`/`type` is 3..=8 alphanum. A 2-char subtag whose 2nd char
        // is a digit (e.g. `c0`, `00`) is neither — structurally invalid.
        let is_key = |s: &str| s.len() == 2 && s.as_bytes()[1].is_ascii_alphabetic();
        let is_attr_or_type = |s: &str| (3..=8).contains(&s.len());
        while i < subs.len() && !is_key(&subs[i]) {
            if !is_attr_or_type(&subs[i]) {
                return None;
            }
            attributes.push(subs[i].clone());
            i += 1;
        }
        attributes.sort();
        let mut keywords: Vec<(String, Vec<String>)> = Vec::new();
        while i < subs.len() {
            let key = subs[i].clone();
            i += 1;
            let mut vals: Vec<String> = Vec::new();
            while i < subs.len() && !is_key(&subs[i]) {
                if !is_attr_or_type(&subs[i]) {
                    return None;
                }
                vals.push(subs[i].clone());
                i += 1;
            }
            // CLDR bcp47 type-value aliases (calendar/collation-strength/
            // measurement): a deprecated value canonicalizes to its preferred
            // form (`ca-islamicc` → `ca-islamic-civil`, `ks-primary` → `ks-level1`,
            // `ms-imperial` → `ms-uksystem`, …).
            if let Some(canon) = unicode_type_alias(&key, &vals.join("-")) {
                vals = canon.split('-').map(String::from).collect();
            }
            // CLDR bcp47 type alias: for the boolean collation keys
            // kb/kc/kh/kk/kn the type "yes" is an alias of "true"
            // (`<type name="true" alias="yes"/>`). Other keys (ka/kf/kr/ks/kv)
            // have no such alias and keep "yes".
            if vals.len() == 1
                && vals[0] == "yes"
                && matches!(key.as_str(), "kb" | "kc" | "kh" | "kk" | "kn")
            {
                vals[0] = String::from("true");
            }
            // A `true` type value is elided in canonical form.
            if vals.len() == 1 && vals[0] == "true" {
                vals.clear();
            }
            // UTS-35 canonicalization: a repeated key keeps its *first* occurrence
            // and discards the rest (`da-u-ca-gregory-ca-buddhist` → `da-u-ca-gregory`).
            if !keywords.iter().any(|(k, _)| k == &key) {
                keywords.push((key, vals));
            }
        }
        keywords.sort_by(|a, b| a.0.cmp(&b.0));
        let mut body = String::from("u");
        for a in &attributes {
            body.push('-');
            body.push_str(a);
        }
        for (k, vals) in &keywords {
            body.push('-');
            body.push_str(k);
            for v in vals {
                body.push('-');
                body.push_str(v);
            }
        }
        return Some(body);
    }
    if singleton == 't' {
        // -t- = (tlang)? (tfield)*  where tfield = tkey tvalue+. A tkey is
        // exactly 2 chars with an alpha first and digit second (e.g. `m0`, `k0`).
        // The optional tlang (a language subtag) comes first.
        let is_tkey = |s: &str| {
            s.len() == 2
                && s.as_bytes()[0].is_ascii_alphabetic()
                && s.as_bytes()[1].is_ascii_digit()
        };
        let mut i = 0;
        let mut tlang: Vec<String> = Vec::new();
        while i < subs.len() && !is_tkey(&subs[i]) {
            tlang.push(subs[i].clone());
            i += 1;
        }
        // Canonicalize the tlang via the locale-id rules (lang/script/region/
        // variants). It is a *language tag*, so the CLDR alias corpus applies to
        // it exactly as it does to the base tag — `en-t-iw` canonicalizes to
        // `en-t-he`. Structural normalization alone left the deprecated subtag
        // in place.
        let tlang_canon = if tlang.is_empty() {
            None
        } else {
            Some(canonicalize_tlang(&tlang.join("-"))?)
        };
        let mut fields: Vec<(String, Vec<String>)> = Vec::new();
        while i < subs.len() {
            let key = subs[i].clone();
            i += 1;
            let mut vals: Vec<String> = Vec::new();
            while i < subs.len() && !is_tkey(&subs[i]) {
                vals.push(subs[i].clone());
                i += 1;
            }
            if vals.is_empty() {
                return None; // a tfield key must have a value
            }
            // CLDR bcp47 tvalue aliases (`m0-names` → `m0-prprname`).
            if let Some(canon) = transform_value_alias(&vals.join("-")) {
                vals = canon.split('-').map(String::from).collect();
            }
            fields.push((key, vals));
        }
        fields.sort_by(|a, b| a.0.cmp(&b.0));
        let mut body = String::from("t");
        if let Some(tl) = &tlang_canon {
            body.push('-');
            body.push_str(&tl.to_ascii_lowercase());
        }
        for (k, vals) in &fields {
            body.push('-');
            body.push_str(k);
            for v in vals {
                body.push('-');
                body.push_str(v);
            }
        }
        return Some(body);
    }
    // Other singletons / private use: subtags in given order, already lowercased.
    let mut body = String::new();
    body.push(singleton);
    for s in subs {
        body.push('-');
        body.push_str(s);
    }
    Some(body)
}

/// Per-`Intl` service metadata used to build its `.prototype` (the spec
/// `Symbol.toStringTag`, the hidden brand-marker key stamped on every instance,
/// and the data-method / accessor-getter names installed on the prototype).
struct IntlService {
    /// The constructor's native dispatch id (`N_INTL_NUMBER_FORMAT`, …).
    ctor_id: u16,
    /// The spec `[Symbol.toStringTag]` string (e.g. `"Intl.NumberFormat"`).
    tag: &'static str,
    /// The hidden internal-slot brand-marker property stamped on every instance and
    /// required by the prototype methods'/accessors' RequireInternalSlot check.
    marker: &'static str,
    /// First-class data-method names installed on the prototype (each a branded
    /// `N_INTL_PROTO_METHOD` wrapper delegating to the underlying method native).
    methods: &'static [&'static str],
    /// The single bound-function accessor on this service's prototype, if any:
    /// `(accessor-name, underlying-selector)`. NumberFormat/DateTimeFormat have
    /// `get format`; Collator has `get compare`. These return a per-instance bound
    /// function rather than being plain data methods (ECMA-402).
    bound_accessor: Option<(&'static str, &'static str)>,
}

/// The eight constructor-style `Intl` services that share the
/// formatter/options-object instance shape (NumberFormat … Segmenter). `Intl.Locale`
/// and `Intl.DurationFormat` have bespoke prototypes built separately.
const INTL_SERVICES: &[IntlService] = &[
    IntlService {
        ctor_id: N_INTL_NUMBER_FORMAT,
        tag: "Intl.NumberFormat",
        marker: "\u{0}brand_nf",
        methods: &[
            "resolvedOptions",
            "formatToParts",
            "formatRange",
            "formatRangeToParts",
        ],
        bound_accessor: Some(("format", "format")),
    },
    IntlService {
        ctor_id: N_INTL_DATETIME_FORMAT,
        tag: "Intl.DateTimeFormat",
        marker: "\u{0}brand_dtf",
        methods: &[
            "resolvedOptions",
            "formatToParts",
            "formatRange",
            "formatRangeToParts",
        ],
        bound_accessor: Some(("format", "format")),
    },
    IntlService {
        ctor_id: N_INTL_COLLATOR,
        tag: "Intl.Collator",
        marker: "\u{0}brand_col",
        methods: &["resolvedOptions"],
        bound_accessor: Some(("compare", "compare")),
    },
    IntlService {
        ctor_id: N_INTL_PLURAL_RULES,
        tag: "Intl.PluralRules",
        marker: "\u{0}brand_pr",
        methods: &["resolvedOptions", "select", "selectRange"],
        bound_accessor: None,
    },
    IntlService {
        ctor_id: N_INTL_LIST_FORMAT,
        tag: "Intl.ListFormat",
        marker: "\u{0}brand_lf",
        methods: &["resolvedOptions", "format", "formatToParts"],
        bound_accessor: None,
    },
    IntlService {
        ctor_id: N_INTL_REL_TIME,
        tag: "Intl.RelativeTimeFormat",
        marker: "\u{0}brand_rtf",
        methods: &["resolvedOptions", "format", "formatToParts"],
        bound_accessor: None,
    },
    IntlService {
        ctor_id: N_INTL_DISPLAY_NAMES,
        tag: "Intl.DisplayNames",
        marker: "\u{0}brand_dn",
        methods: &["resolvedOptions", "of"],
        bound_accessor: None,
    },
    IntlService {
        ctor_id: N_INTL_SEGMENTER,
        tag: "Intl.Segmenter",
        marker: "\u{0}brand_seg",
        methods: &["resolvedOptions", "segment"],
        bound_accessor: None,
    },
];

/// `Intl.Locale.prototype` accessor-getter names (each a `get`-only accessor
/// brand-checking the `[[InitializedLocale]]` slot).
const LOCALE_ACCESSORS: &[&str] = &[
    "baseName",
    "calendar",
    "caseFirst",
    "collation",
    "firstDayOfWeek",
    "hourCycle",
    "language",
    "numberingSystem",
    "numeric",
    "region",
    "script",
    "variants",
];

/// The arity (`length`) of a branded `Intl` prototype data method, given its
/// owning service ctor id and method name (RelativeTimeFormat's `format`/
/// `formatToParts` are length 2; everywhere else `format`-family methods are length 1).
fn intl_method_arity(ctor_id: u16, name: &str) -> u32 {
    match (ctor_id, name) {
        (_, "resolvedOptions") => 0,
        (N_INTL_REL_TIME, "format" | "formatToParts") => 2,
        (N_INTL_PLURAL_RULES, "selectRange") => 2,
        (_, "formatRange" | "formatRangeToParts") => 2,
        _ => 1,
    }
}

impl<'a> Interp<'a> {
    /// The underlying method native id for a branded `Intl` prototype data method.
    fn intl_underlying_native(name: &str) -> u16 {
        match name {
            "format" => N_INTL_FORMAT,
            "resolvedOptions" => N_INTL_RESOLVED_OPTIONS,
            "formatToParts" | "format_to_parts" => N_INTL_FORMAT_TO_PARTS,
            "formatRange" => N_INTL_FORMAT_RANGE,
            "formatRangeToParts" => N_INTL_FORMAT_RANGE_TO_PARTS,
            "compare" => N_INTL_COMPARE,
            "select" => N_INTL_PLURAL_SELECT,
            "selectRange" => N_INTL_PLURAL_SELECT_RANGE,
            "of" => N_INTL_DISPLAY_NAMES_OF,
            "segment" => N_INTL_SEGMENTER_SEGMENT,
            "list_format" => N_INTL_LIST_FORMAT_FORMAT,
            "rel_format" => N_INTL_REL_TIME_FORMAT,
            _ => 0,
        }
    }

    /// Builds (once) and links each `Intl` service constructor's `.prototype`,
    /// installing the branded methods, the `Symbol.toStringTag`, and the
    /// `constructor` back-link, and replacing the constructor's default
    /// `Function.prototype`-derived `.prototype` with a real `%XPrototype%`. Idempotent
    /// (the per-id cache short-circuits a second call). Invoked at realm setup.
    pub(crate) fn install_intl_prototypes(&mut self) {
        for svc in INTL_SERVICES {
            self.intl_service_prototype(svc);
        }
        self.intl_locale_prototype();
        self.intl_duration_prototype();
    }

    /// The `Intl` namespace object (where the service constructors live), if set up.
    fn intl_namespace(&mut self) -> Option<Handle> {
        self.current
            .get("Intl")
            .and_then(|v| v.as_handle())
            .map(Handle::from_raw)
    }

    /// The constructor function handle for the `Intl` service whose property name
    /// on the namespace is `ctor_name` (e.g. `"NumberFormat"`).
    pub(crate) fn intl_ctor_handle(&mut self, ctor_name: &str) -> Option<Handle> {
        let ns = self.intl_namespace()?;
        self.realm
            .get_property(ns, ctor_name)
            .and_then(|v| v.as_handle())
            .map(Handle::from_raw)
    }

    /// Builds and caches the `.prototype` object for one of the formatter-shaped
    /// `Intl` services, wiring it onto the constructor. Returns the prototype handle.
    fn intl_service_prototype(&mut self, svc: &IntlService) -> Option<Handle> {
        if let Some(p) = self.realm.intl_prototype(svc.ctor_id) {
            return Some(p);
        }
        // Map the ctor id back to its namespace property name.
        let ctor_name = match svc.ctor_id {
            N_INTL_NUMBER_FORMAT => "NumberFormat",
            N_INTL_DATETIME_FORMAT => "DateTimeFormat",
            N_INTL_COLLATOR => "Collator",
            N_INTL_PLURAL_RULES => "PluralRules",
            N_INTL_LIST_FORMAT => "ListFormat",
            N_INTL_REL_TIME => "RelativeTimeFormat",
            N_INTL_DISPLAY_NAMES => "DisplayNames",
            N_INTL_SEGMENTER => "Segmenter",
            _ => return None,
        };
        let ctor = self.intl_ctor_handle(ctor_name)?;
        let obj_proto = self.object_prototype();
        let proto = self.realm.new_object_with_proto(obj_proto);
        for &m in svc.methods {
            // ListFormat/RelativeTimeFormat have distinct `format`/`formatToParts`
            // method natives; pick the underlying selector accordingly.
            let selector = match (svc.ctor_id, m) {
                (N_INTL_LIST_FORMAT, "format") => "list_format",
                (N_INTL_REL_TIME, "format") => "rel_format",
                (N_INTL_LIST_FORMAT | N_INTL_REL_TIME, "formatToParts") => "format_to_parts",
                _ => m,
            };
            let arity = intl_method_arity(svc.ctor_id, m);
            let f = self.make_intl_proto_method(svc.marker, m, selector, arity);
            self.realm
                .set_property(proto, m, NanBox::handle(f.to_raw()));
            self.realm.mark_hidden(proto, m);
        }
        // The bound-function accessor (`get format` / `get compare`).
        if let Some((acc_name, selector)) = svc.bound_accessor {
            let label = alloc::format!("get {acc_name}");
            let marker_v = self.new_str(svc.marker);
            let sel_v = self.new_str(selector);
            let pair = self.realm.new_array(alloc::vec![marker_v, sel_v]);
            let getter = self.realm.new_bound_native(N_INTL_BOUND_GETTER, pair);
            self.install_fn_name_length(getter, &label, 0);
            self.realm.define_accessor(
                proto,
                acc_name,
                NanBox::handle(getter.to_raw()),
                NanBox::undefined(),
            );
            self.realm.mark_hidden(proto, acc_name);
        }
        // `prototype[Symbol.toStringTag]` — a `{ w:false, e:false, c:true }` string
        // data property (ECMA-402 11.3.x etc.).
        self.install_to_string_tag(proto, svc.tag);
        // `prototype.constructor` back-link (non-enumerable).
        self.realm
            .set_hidden_property(proto, "constructor", NanBox::handle(ctor.to_raw()));
        self.link_ctor_prototype(ctor, proto);
        self.realm.set_intl_prototype(svc.ctor_id, proto);
        Some(proto)
    }

    /// A branded prototype data method: an `N_INTL_PROTO_METHOD` bound native whose
    /// target is the 2-element `[markerKey, underlyingSelector]` array. The wrapper
    /// (in `call_with_this`) RequireInternalSlot-checks the call's `this` before
    /// delegating to the underlying method native.
    fn make_intl_proto_method(
        &mut self,
        marker: &str,
        name: &str,
        selector: &str,
        arity: u32,
    ) -> Handle {
        let marker_v = self.new_str(marker);
        let sel_v = self.new_str(selector);
        let pair = self.realm.new_array(alloc::vec![marker_v, sel_v]);
        let f = self.realm.new_bound_native(N_INTL_PROTO_METHOD, pair);
        self.install_fn_name_length(f, name, arity);
        f
    }

    /// Installs `proto` as the constructor's `prototype` data property, with the
    /// built-in `{ writable:false, enumerable:false, configurable:false }`
    /// attributes (every built-in constructor's `prototype`).
    fn link_ctor_prototype(&mut self, ctor: Handle, proto: Handle) {
        self.realm
            .set_property(ctor, "prototype", NanBox::handle(proto.to_raw()));
        self.realm.mark_hidden(ctor, "prototype");
        self.realm.set_readonly_property(ctor, "prototype");
        self.realm.set_non_configurable_property(ctor, "prototype");
    }

    /// Stamps a service's internal-slot brand marker on `obj` and links its
    /// `[[Prototype]]` to the (lazily built) service prototype — turning a bare
    /// options-bag object into a branded `Intl.X` instance.
    fn brand_intl_instance(&mut self, obj: Handle, ctor_id: u16) {
        let svc = INTL_SERVICES.iter().find(|s| s.ctor_id == ctor_id);
        if let Some(svc) = svc {
            self.realm
                .set_hidden_property(obj, svc.marker, NanBox::boolean(true));
            if let Some(proto) = self.intl_service_prototype(svc) {
                self.realm.set_object_proto(obj, Some(proto));
            }
        }
    }

    /// Brands `obj` with the service's internal-slot marker *without* touching its
    /// prototype (for a subclass `super()`, whose instance already links to the
    /// subclass prototype).
    fn set_intl_marker(&mut self, obj: Handle, ctor_id: u16) {
        if let Some(svc) = INTL_SERVICES.iter().find(|s| s.ctor_id == ctor_id) {
            self.realm
                .set_hidden_property(obj, svc.marker, NanBox::boolean(true));
        }
    }

    /// RequireInternalSlot for a branded `Intl` receiver: returns the receiver
    /// handle when `this` is an object carrying the hidden `marker`, else a
    /// TypeError. The prototype object itself (which has no marker) is rejected,
    /// matching the spec's per-method brand check.
    pub(crate) fn require_intl_slot(
        &mut self,
        this: NanBox,
        marker: &str,
        what: &str,
    ) -> Result<Handle, ExecError> {
        if let Some(h) = this.as_handle().map(Handle::from_raw)
            && self.realm.get_property(h, marker).is_some()
        {
            return Ok(h);
        }
        Err(self.type_error(&alloc::format!(
            "{what} called on an object that is not a valid {what} receiver"
        )))
    }

    /// Dispatches an `N_INTL_PROTO_METHOD` wrapper: `target` is the
    /// `[markerKey, underlyingSelector]` pair. Brand-checks `this`, then delegates to
    /// the underlying method native with `this` preserved.
    pub(crate) fn intl_proto_method_dispatch(
        &mut self,
        this: NanBox,
        target: Handle,
        args: &[NanBox],
    ) -> Result<NanBox, ExecError> {
        let pair = self
            .realm
            .array_elements(target)
            .map(<[_]>::to_vec)
            .unwrap_or_default();
        let marker = pair
            .first()
            .and_then(|v| v.as_handle())
            .map(Handle::from_raw)
            .and_then(|h| self.realm.string_value(h))
            .unwrap_or_default();
        let selector = pair
            .get(1)
            .and_then(|v| v.as_handle())
            .map(Handle::from_raw)
            .and_then(|h| self.realm.string_value(h))
            .unwrap_or_default();
        self.require_intl_slot(this, &marker, "Intl method")?;
        let id = Self::intl_underlying_native(&selector);
        // The underlying method native reads `self.this_val`; preserve the receiver.
        let saved = core::mem::replace(&mut self.this_val, this);
        let r = self.call_native(id, args);
        self.this_val = saved;
        r
    }

    /// Dispatches an `N_INTL_BOUND_GETTER` accessor (`get format`/`get compare`):
    /// `target` is `[markerKey, selector]`. Brand-checks `this`, then returns the
    /// per-instance bound function — created once and cached on the instance under a
    /// hidden `\0bound_<selector>` key so repeated reads return the same value
    /// (`nf.format === nf.format`, per ECMA-402).
    pub(crate) fn intl_bound_getter_dispatch(
        &mut self,
        this: NanBox,
        target: Handle,
    ) -> Result<NanBox, ExecError> {
        let pair = self
            .realm
            .array_elements(target)
            .map(<[_]>::to_vec)
            .unwrap_or_default();
        let marker = pair
            .first()
            .and_then(|v| v.as_handle())
            .map(Handle::from_raw)
            .and_then(|h| self.realm.string_value(h))
            .unwrap_or_default();
        let selector = pair
            .get(1)
            .and_then(|v| v.as_handle())
            .map(Handle::from_raw)
            .and_then(|h| self.realm.string_value(h))
            .unwrap_or_default();
        let inst = self.require_intl_slot(this, &marker, "Intl bound-function getter")?;
        let cache_key = alloc::format!("\u{0}bound_{selector}");
        if let Some(v) = self.realm.get_property(inst, &cache_key) {
            return Ok(v);
        }
        // Build the bound function: target = [instance, selector].
        let inst_v = NanBox::handle(inst.to_raw());
        let sel_v = self.new_str(&selector);
        let bpair = self.realm.new_array(alloc::vec![inst_v, sel_v]);
        let bound = self.realm.new_bound_native(N_INTL_BOUND_CALL, bpair);
        // The bound function's `name` is `""`; its `length` is 2 for `compare`
        // (a two-argument comparator) and 1 for `format` (sec-collator-compare /
        // sec-number-format-functions).
        let len = if selector == "compare" { 2 } else { 1 };
        self.install_fn_name_length(bound, "", len);
        let boundv = NanBox::handle(bound.to_raw());
        self.realm.set_hidden_property(inst, &cache_key, boundv);
        Ok(boundv)
    }

    /// Dispatches an `N_INTL_BOUND_CALL` (the function returned by `get format`/
    /// `get compare`): `target` is `[instance, selector]`. Formats/compares against
    /// the captured instance regardless of the call's `this` (a BoundFunction).
    pub(crate) fn intl_bound_call_dispatch(
        &mut self,
        target: Handle,
        args: &[NanBox],
    ) -> Result<NanBox, ExecError> {
        let pair = self
            .realm
            .array_elements(target)
            .map(<[_]>::to_vec)
            .unwrap_or_default();
        let inst = pair
            .first()
            .and_then(|v| v.as_handle())
            .map(Handle::from_raw);
        let selector = pair
            .get(1)
            .and_then(|v| v.as_handle())
            .map(Handle::from_raw)
            .and_then(|h| self.realm.string_value(h))
            .unwrap_or_default();
        let Some(inst) = inst else {
            return Ok(NanBox::undefined());
        };
        let arg0 = args.first().copied().unwrap_or(NanBox::undefined());
        match selector.as_str() {
            "compare" => {
                // Delegate to N_INTL_COMPARE with the captured collator as `this`.
                let saved = core::mem::replace(&mut self.this_val, NanBox::handle(inst.to_raw()));
                let r = self.call_native(N_INTL_COMPARE, args);
                self.this_val = saved;
                r
            }
            _ => {
                // `format`: format the single argument against the captured formatter.
                let s = self.intl_format_checked(inst, arg0)?;
                Ok(self.new_str(&s))
            }
        }
    }

    /// `format(value)` for a NumberFormat or DateTimeFormat instance. A
    /// DateTimeFormat coerces `value` via `ToNumber` + `TimeClip` (a non-finite /
    /// out-of-range date is a RangeError; `undefined` → now); a NumberFormat
    /// formats any numeric value (including `±Infinity`). Shared by the `format`
    /// method dispatch and the bound `get format` function.
    pub(crate) fn intl_format_checked(
        &mut self,
        inst: Handle,
        value: NanBox,
    ) -> Result<String, ExecError> {
        let is_datetime = self
            .realm
            .get_property(inst, "\u{0}intl")
            .map(|k| self.realm.to_display_string(k))
            .as_deref()
            == Some("datetime");
        if is_datetime {
            #[cfg(feature = "intl")]
            if let Some(s) = self.temporal_format_flat(inst, value, false)? {
                return Ok(s);
            }
            let ms = self.datetime_operand(value)?;
            Ok(self.format_intl_datetime(inst, ms))
        } else {
            // ECMA-402 `ToIntlMathematicalValue`: a string / BigInt argument with
            // more precision than an f64 preserves formats from its exact decimal
            // digits, bypassing the crate's f64 round-trip.
            #[cfg(feature = "intl")]
            if let Some(s) = self.try_exact_decimal_format(inst, value) {
                return Ok(s);
            }
            let n = self.coerce_intl_number(value)?;
            Ok(self.intl_format_value(inst, NanBox::number(n)))
        }
    }

    /// `Intl.NumberFormat` operand coercion (a reduced `ToIntlMathematicalValue`
    /// collapsed to an f64): a BigInt uses its numeric value; everything else is
    /// `ToPrimitive`(number) then `ToNumber` — so an object with a `valueOf`
    /// returning a numeric string formats as that number, and a `Symbol` throws.
    pub(crate) fn coerce_intl_number(&mut self, value: NanBox) -> Result<f64, ExecError> {
        if let Some(h) = value.as_handle().map(Handle::from_raw)
            && let Some(big) = self.realm.bigint_at(h)
        {
            return Ok(big.to_f64());
        }
        let prim = self.coerce_to_number(value)?;
        Ok(self.realm.to_number(prim))
    }

    /// Builds an `Intl.NumberFormat`/`DateTimeFormat` instance — an object that
    /// captures the relevant options behind a `\0intl` kind marker. Used for both
    /// `new Intl.X(...)` and the callable-without-`new` form.
    pub(crate) fn make_intl_formatter(
        &mut self,
        id: u16,
        args: &[NanBox],
    ) -> Result<NanBox, ExecError> {
        let obj = self.realm.new_object();
        // Link `[[Prototype]]` to `Intl.X.prototype` (a subclass/Reflect.construct
        // newTarget overrides it in the caller) and initialize the formatter state.
        self.brand_intl_instance(obj, id);
        self.init_intl_formatter_state(obj, id, args)?;
        Ok(NanBox::handle(obj.to_raw()))
    }

    /// Initializes an Intl formatter's internal slots on an *already-allocated*
    /// (and prototype-linked) `obj` — the shared body of `make_intl_formatter` and
    /// the `class S extends Intl.NumberFormat {}` `super()` path, so a subclass
    /// instance carries the `[[NumberFormat]]`/`[[DateTimeFormat]]` slots.
    pub(crate) fn init_intl_formatter_state(
        &mut self,
        obj: Handle,
        id: u16,
        args: &[NanBox],
    ) -> Result<(), ExecError> {
        let kind = if id == N_INTL_NUMBER_FORMAT {
            "number"
        } else {
            "datetime"
        };
        let marker = self.new_str(kind);
        self.realm.set_hidden_property(obj, "\u{0}intl", marker);
        // Brand the instance with the service's internal-slot marker (without
        // resetting the prototype, which a subclass `super()` has already set).
        self.set_intl_marker(obj, id);
        // CanonicalizeLocaleList(locales): the resolved locale is the first
        // requested tag (this engine serves any structurally valid locale), else
        // the default. A malformed tag raises a RangeError here.
        let requested =
            self.canonicalize_locale_list(args.first().copied().unwrap_or(NanBox::undefined()))?;
        let locale = requested
            .into_iter()
            .next()
            .unwrap_or_else(|| String::from("en-US"));
        let locv = self.new_str(&locale);
        self.realm.set_hidden_property(obj, "\u{0}locale", locv);
        // `options = ? CoerceOptionsToObject(options)`: `undefined` → no options;
        // `null` (→ ToObject) is a TypeError; any other primitive is wrapped
        // (exposing no own option keys).
        let opts_arg = args.get(1).copied().unwrap_or(NanBox::undefined());
        let opts = if matches!(opts_arg.unpack(), Unpacked::Undefined) {
            None
        } else if matches!(opts_arg.unpack(), Unpacked::Null) {
            return Err(self.type_error("Intl formatter options must not be null"));
        } else {
            self.coerce_to_object(opts_arg)
                .as_handle()
                .map(Handle::from_raw)
        };
        if id == N_INTL_NUMBER_FORMAT {
            self.init_number_format(obj, opts)?;
        } else {
            self.init_datetime_format(obj, opts)?;
        }
        Ok(())
    }

    /// `GetOption(options, prop, "string", values, default)` — reads `prop` via
    /// its getter, returns the default when `undefined`, else coerces to a string
    /// (a Symbol is a TypeError) and validates membership in `values` (a
    /// **RangeError** otherwise). `None` `default` with an absent option yields
    /// `None`.
    pub(crate) fn get_string_option(
        &mut self,
        opts: Option<Handle>,
        prop: &str,
        values: &[&str],
        default: Option<&str>,
    ) -> Result<Option<String>, ExecError> {
        let raw = match opts {
            Some(h) => self.read_member(h, prop)?,
            None => NanBox::undefined(),
        };
        if matches!(raw.unpack(), Unpacked::Undefined) {
            return Ok(default.map(String::from));
        }
        let s = self.coerce_to_string(raw)?;
        if !values.is_empty() && !values.iter().any(|v| *v == s) {
            let m = self.new_str(&alloc::format!("invalid value '{s}' for option {prop}"));
            return Err(ExecError::Throw(self.make_error(N_RANGE_ERROR, Some(m))));
        }
        Ok(Some(s))
    }

    /// `GetOption(options, prop, "boolean", …)` — reads `prop`; `undefined` →
    /// `default`, else `ToBoolean`.
    fn get_bool_option(
        &mut self,
        opts: Option<Handle>,
        prop: &str,
        default: Option<bool>,
    ) -> Result<Option<bool>, ExecError> {
        let raw = match opts {
            Some(h) => self.read_member(h, prop)?,
            None => NanBox::undefined(),
        };
        if matches!(raw.unpack(), Unpacked::Undefined) {
            return Ok(default);
        }
        Ok(Some(self.realm.truthy(raw)))
    }

    /// `GetNumberOption` / `DefaultNumberOption`: reads `prop`, coerces to a
    /// Number (throwing for a Symbol), and requires it to be a finite integer in
    /// `[min, max]` (else **RangeError**). `undefined` → `default`.
    fn get_int_option(
        &mut self,
        opts: Option<Handle>,
        prop: &str,
        min: f64,
        max: f64,
        default: Option<f64>,
    ) -> Result<Option<f64>, ExecError> {
        let raw = match opts {
            Some(h) => self.read_member(h, prop)?,
            None => NanBox::undefined(),
        };
        if matches!(raw.unpack(), Unpacked::Undefined) {
            return Ok(default);
        }
        let nv = self.coerce_to_number(raw)?;
        let n = self.realm.to_number(nv);
        if n.is_nan() || n < min || n > max {
            let m = self.new_str(&alloc::format!("value out of range for option {prop}"));
            return Err(ExecError::Throw(self.make_error(N_RANGE_ERROR, Some(m))));
        }
        Ok(Some(trunc_toward_zero(n)))
    }

    /// Stores a resolved option on the formatter (under its own key) when present.
    fn store_str(&mut self, obj: Handle, key: &str, val: &Option<String>) {
        if let Some(v) = val {
            let sv = self.new_str(v);
            self.realm.set_hidden_property(obj, key, sv);
        }
    }

    /// Initializes an `Intl.NumberFormat`, reading and validating options in spec
    /// order (`InitializeNumberFormat` → `SetNumberFormatUnitOptions` →
    /// `SetNumberFormatDigitOptions`). Stores resolved options on `obj`.
    fn init_number_format(&mut self, obj: Handle, opts: Option<Handle>) -> Result<(), ExecError> {
        // localeMatcher (validated, not otherwise used).
        let _ = self.get_string_option(
            opts,
            "localeMatcher",
            &["lookup", "best fit"],
            Some("best fit"),
        )?;
        let nu = self.get_string_option(opts, "numberingSystem", &[], None)?;
        if let Some(ns) = &nu {
            // numberingSystem must match `type` production (3-8 alnum, hyphen-joined).
            if !is_unicode_type_value(ns) {
                let m = self.new_str("invalid numberingSystem");
                return Err(ExecError::Throw(self.make_error(N_RANGE_ERROR, Some(m))));
            }
        }
        // ResolveLocale for the `nu` key: a supported `numberingSystem` option
        // wins, else the locale's `-u-nu-` extension (if it names a known system),
        // else the CLDR default. The resolved locale keeps only the relevant
        // extension (`-u-nu-` when it survives) and drops irrelevant ones (e.g.
        // `-u-cu-`), so `resolvedOptions().locale` reflects only what applied.
        let raw_locale = self
            .realm
            .get_property(obj, "\u{0}locale")
            .map(|v| self.realm.to_display_string(v))
            .unwrap_or_else(|| String::from("en-US"));
        let base = strip_unicode_extension(&raw_locale);
        let (resolved_nu, add) = resolve_nu_key(&base, &raw_locale, nu.as_deref());
        let resolved_locale = build_resolved_locale(&base, &[add]);
        let locv = self.new_str(&resolved_locale);
        self.realm.set_hidden_property(obj, "\u{0}locale", locv);
        self.store_str(obj, "numberingSystem", &Some(resolved_nu));

        // --- SetNumberFormatUnitOptions ---
        let style = self
            .get_string_option(
                opts,
                "style",
                &["decimal", "percent", "currency", "unit"],
                Some("decimal"),
            )?
            .unwrap();
        let currency = self.get_string_option(opts, "currency", &[], None)?;
        let currency_display = self.get_string_option(
            opts,
            "currencyDisplay",
            &["code", "symbol", "narrowSymbol", "name"],
            Some("symbol"),
        )?;
        let currency_sign = self.get_string_option(
            opts,
            "currencySign",
            &["standard", "accounting"],
            Some("standard"),
        )?;
        let unit = self.get_string_option(opts, "unit", &[], None)?;
        let unit_display = self.get_string_option(
            opts,
            "unitDisplay",
            &["short", "narrow", "long"],
            Some("short"),
        )?;
        // SetNumberFormatUnitOptions: a `currency` is required (TypeError) when
        // style is "currency"; whenever present it must be well-formed (RangeError)
        // regardless of style. Same shape for `unit`.
        match &currency {
            None if style == "currency" => {
                let m = self.new_str("currency code is required with currency style");
                return Err(ExecError::Throw(self.make_error(N_TYPE_ERROR, Some(m))));
            }
            Some(c) if !is_well_formed_currency(c) => {
                let m = self.new_str("invalid currency code");
                return Err(ExecError::Throw(self.make_error(N_RANGE_ERROR, Some(m))));
            }
            _ => {}
        }
        match &unit {
            None if style == "unit" => {
                let m = self.new_str("unit is required with unit style");
                return Err(ExecError::Throw(self.make_error(N_TYPE_ERROR, Some(m))));
            }
            Some(u) if !is_well_formed_unit(u) => {
                let m = self.new_str("invalid unit");
                return Err(ExecError::Throw(self.make_error(N_RANGE_ERROR, Some(m))));
            }
            _ => {}
        }
        let style_s = Some(style.clone());
        self.store_str(obj, "style", &style_s);
        // Canonical currency code is uppercased.
        if style == "currency" {
            let cc = currency.as_ref().map(|c| c.to_ascii_uppercase());
            self.store_str(obj, "currency", &cc);
            self.store_str(obj, "currencyDisplay", &currency_display);
            self.store_str(obj, "currencySign", &currency_sign);
        }
        if style == "unit" {
            self.store_str(obj, "unit", &unit);
            self.store_str(obj, "unitDisplay", &unit_display);
        }

        // notation (read before digit options per the spec order).
        let notation = self
            .get_string_option(
                opts,
                "notation",
                &["standard", "scientific", "engineering", "compact"],
                Some("standard"),
            )?
            .unwrap();

        // --- SetNumberFormatDigitOptions --- (shared with Intl.PluralRules).
        self.set_number_format_digit_options(obj, opts)?;

        // compactDisplay, useGrouping, signDisplay.
        let compact_display =
            self.get_string_option(opts, "compactDisplay", &["short", "long"], Some("short"))?;
        if notation == "compact" {
            self.store_str(obj, "compactDisplay", &compact_display);
        }
        // useGrouping: ECMA-402 accepts a boolean or "min2"/"auto"/"always". Read
        // it without enum validation, then normalize.
        let ug_raw = match opts {
            Some(h) => self.read_member(h, "useGrouping")?,
            None => NanBox::undefined(),
        };
        let use_grouping_val = self.normalize_use_grouping(ug_raw, &notation)?;
        let sign_display = self
            .get_string_option(
                opts,
                "signDisplay",
                &["auto", "never", "always", "exceptZero", "negative"],
                Some("auto"),
            )?
            .unwrap();

        // Store notation/sign options for resolvedOptions + formatting (the digit
        // options were stored by `set_number_format_digit_options`).
        self.store_str(obj, "notation", &Some(notation));
        self.store_str(obj, "signDisplay", &Some(sign_display));
        match use_grouping_val {
            UseGroupingResolved::Bool(b) => {
                self.realm
                    .set_hidden_property(obj, "useGrouping", NanBox::boolean(b));
            }
            UseGroupingResolved::Str(s) => {
                let sv = self.new_str(s);
                self.realm.set_hidden_property(obj, "useGrouping", sv);
            }
        }
        Ok(())
    }

    /// `SetNumberFormatDigitOptions`: reads, validates, and stores the shared
    /// number-format digit slots (`minimumIntegerDigits`, the fraction/significant
    /// digit pairs, `roundingIncrement`, `roundingMode`, `roundingPriority`,
    /// `trailingZeroDisplay`) on `obj` under their own hidden keys, in spec read
    /// order. Shared by `Intl.NumberFormat` and `Intl.PluralRules` so both honor the
    /// same option semantics and constructor option-read order. An invalid value is a
    /// RangeError; a Symbol is a TypeError (via the option getters).
    fn set_number_format_digit_options(
        &mut self,
        obj: Handle,
        opts: Option<Handle>,
    ) -> Result<(), ExecError> {
        let mnid = self
            .get_int_option(opts, "minimumIntegerDigits", 1.0, 21.0, Some(1.0))?
            .unwrap();
        let mnfd = self.get_int_option(opts, "minimumFractionDigits", 0.0, 100.0, None)?;
        let mxfd = self.get_int_option(opts, "maximumFractionDigits", 0.0, 100.0, None)?;
        let mnsd = self.get_int_option(opts, "minimumSignificantDigits", 1.0, 21.0, None)?;
        let mxsd = self.get_int_option(opts, "maximumSignificantDigits", 1.0, 21.0, None)?;
        // Cross-validation: an explicit minimum may not exceed an explicit maximum.
        if let (Some(a), Some(b)) = (mnfd, mxfd)
            && a > b
        {
            let m = self.new_str("minimumFractionDigits is greater than maximumFractionDigits");
            return Err(ExecError::Throw(self.make_error(N_RANGE_ERROR, Some(m))));
        }
        if let (Some(a), Some(b)) = (mnsd, mxsd)
            && a > b
        {
            let m =
                self.new_str("minimumSignificantDigits is greater than maximumSignificantDigits");
            return Err(ExecError::Throw(self.make_error(N_RANGE_ERROR, Some(m))));
        }
        // SetNumberFormatDigitOptions: when *either* significant-digit option is
        // present, the "significant digits" path is used and the other defaults
        // (min → 1, max → 21). Without this, a lone `minimumSignificantDigits` was
        // stored but the missing max meant the padding never applied
        // (`format(1)` with `{minimumSignificantDigits:3}` gave "1", not "1.00").
        let (mnsd, mxsd) = if mnsd.is_some() || mxsd.is_some() {
            (Some(mnsd.unwrap_or(1.0)), Some(mxsd.unwrap_or(21.0)))
        } else {
            (mnsd, mxsd)
        };
        // roundingIncrement ∈ a fixed allowed set.
        let rinc = self
            .get_int_option(opts, "roundingIncrement", 1.0, 5000.0, Some(1.0))?
            .unwrap();
        const ALLOWED_INC: [u32; 15] = [
            1, 2, 5, 10, 20, 25, 50, 100, 200, 250, 500, 1000, 2000, 2500, 5000,
        ];
        if !ALLOWED_INC.contains(&(rinc as u32)) {
            let m = self.new_str("invalid roundingIncrement");
            return Err(ExecError::Throw(self.make_error(N_RANGE_ERROR, Some(m))));
        }
        let rounding_mode = self
            .get_string_option(
                opts,
                "roundingMode",
                &[
                    "ceil",
                    "floor",
                    "expand",
                    "trunc",
                    "halfCeil",
                    "halfFloor",
                    "halfExpand",
                    "halfTrunc",
                    "halfEven",
                ],
                Some("halfExpand"),
            )?
            .unwrap();
        let rounding_priority = self
            .get_string_option(
                opts,
                "roundingPriority",
                &["auto", "morePrecision", "lessPrecision"],
                Some("auto"),
            )?
            .unwrap();
        // SetNumberFormatDigitOptions: a `roundingIncrement` other than 1 is only
        // valid with the *fractionDigits* rounding type — else a TypeError — and
        // additionally requires maximumFractionDigits == minimumFractionDigits
        // (else a RangeError).
        if rinc != 1.0 {
            let rounding_type = if rounding_priority == "morePrecision" {
                "morePrecision"
            } else if rounding_priority == "lessPrecision" {
                "lessPrecision"
            } else if mnsd.is_some() {
                "significantDigits"
            } else {
                "fractionDigits"
            };
            if rounding_type != "fractionDigits" {
                return Err(self.type_error(
                    "roundingIncrement other than 1 requires the fractionDigits rounding type",
                ));
            }
            if let (Some(a), Some(b)) = (mnfd, mxfd)
                && a != b
            {
                let m = self.new_str("roundingIncrement requires equal min/max fraction digits");
                return Err(ExecError::Throw(self.make_error(N_RANGE_ERROR, Some(m))));
            }
        }
        let tzd = self
            .get_string_option(
                opts,
                "trailingZeroDisplay",
                &["auto", "stripIfInteger"],
                Some("auto"),
            )?
            .unwrap();

        self.realm
            .set_hidden_property(obj, "minimumIntegerDigits", NanBox::number(mnid));
        if let Some(v) = mnfd {
            self.realm
                .set_hidden_property(obj, "minimumFractionDigits", NanBox::number(v));
        }
        if let Some(v) = mxfd {
            self.realm
                .set_hidden_property(obj, "maximumFractionDigits", NanBox::number(v));
        }
        if let Some(v) = mnsd {
            self.realm
                .set_hidden_property(obj, "minimumSignificantDigits", NanBox::number(v));
        }
        if let Some(v) = mxsd {
            self.realm
                .set_hidden_property(obj, "maximumSignificantDigits", NanBox::number(v));
        }
        self.realm
            .set_hidden_property(obj, "roundingIncrement", NanBox::number(rinc));
        self.store_str(obj, "roundingMode", &Some(rounding_mode));
        self.store_str(obj, "roundingPriority", &Some(rounding_priority));
        self.store_str(obj, "trailingZeroDisplay", &Some(tzd));
        Ok(())
    }

    /// `GetBooleanOrStringNumberFormatOption` for `useGrouping`: `undefined` →
    /// the default (`"min2"` when `notation` is `"compact"`, else `"auto"`);
    /// `true` → `"always"`; a falsy value → `false`; the strings `"true"`/`"false"`
    /// → the default; one of `"min2"`/`"auto"`/`"always"` → that string; any other
    /// string (or non-string, via ToString) → **RangeError**.
    fn normalize_use_grouping(
        &mut self,
        raw: NanBox,
        notation: &str,
    ) -> Result<UseGroupingResolved, ExecError> {
        let fallback = if notation == "compact" {
            "min2"
        } else {
            "auto"
        };
        if matches!(raw.unpack(), Unpacked::Undefined) {
            return Ok(UseGroupingResolved::Str(fallback));
        }
        if matches!(raw.unpack(), Unpacked::Bool(true)) {
            return Ok(UseGroupingResolved::Str("always"));
        }
        if !self.realm.truthy(raw) {
            return Ok(UseGroupingResolved::Bool(false));
        }
        let s = self.coerce_to_string(raw)?;
        match s.as_str() {
            "true" | "false" => Ok(UseGroupingResolved::Str(fallback)),
            "min2" => Ok(UseGroupingResolved::Str("min2")),
            "auto" => Ok(UseGroupingResolved::Str("auto")),
            "always" => Ok(UseGroupingResolved::Str("always")),
            _ => {
                let m = self.new_str(&alloc::format!("invalid useGrouping value '{s}'"));
                Err(ExecError::Throw(self.make_error(N_RANGE_ERROR, Some(m))))
            }
        }
    }

    /// Initializes an `Intl.DateTimeFormat`, reading and validating options in
    /// spec order. Stores resolved options on `obj`.
    fn init_datetime_format(&mut self, obj: Handle, opts: Option<Handle>) -> Result<(), ExecError> {
        let _ = self.get_string_option(
            opts,
            "localeMatcher",
            &["lookup", "best fit"],
            Some("best fit"),
        )?;
        let ca = self.get_string_option(opts, "calendar", &[], None)?;
        if let Some(c) = &ca
            && !is_unicode_type_value(c)
        {
            let m = self.new_str("invalid calendar");
            return Err(ExecError::Throw(self.make_error(N_RANGE_ERROR, Some(m))));
        }
        let nu = self.get_string_option(opts, "numberingSystem", &[], None)?;
        if let Some(n) = &nu
            && !is_unicode_type_value(n)
        {
            let m = self.new_str("invalid numberingSystem");
            return Err(ExecError::Throw(self.make_error(N_RANGE_ERROR, Some(m))));
        }
        // ResolveLocale for the relevant DateTimeFormat extension keys (`ca`, `nu`,
        // `hc`): a supported option wins, else the locale's `-u-` extension value,
        // else the default. The resolved locale keeps only surviving relevant keys
        // (`-u-ca-`/`-u-nu-`/`-u-hc-`) and drops irrelevant ones (e.g. `-u-cu-`).
        // `hc` is retained here as-is; its option interplay is finalized in
        // `dtf_hour_resolution` at resolvedOptions time.
        let raw_locale = self
            .realm
            .get_property(obj, "\u{0}locale")
            .map(|v| self.realm.to_display_string(v))
            .unwrap_or_else(|| String::from("en-US"));
        let base = strip_unicode_extension(&raw_locale);
        let (resolved_ca, ca_add) = resolve_ca_key(&base, &raw_locale, ca.as_deref());
        let (resolved_nu, nu_add) = resolve_nu_key(&base, &raw_locale, nu.as_deref());
        let hc_ext = split_u_keyword(&raw_locale, "hc")
            .1
            .filter(|v| matches!(v.as_str(), "h11" | "h12" | "h23" | "h24"));
        let hc_add = hc_ext
            .as_deref()
            .map(|v| alloc::format!("-hc-{v}"))
            .unwrap_or_default();
        let resolved_locale = build_resolved_locale(&base, &[ca_add, hc_add, nu_add]);
        let locv = self.new_str(&resolved_locale);
        self.realm.set_hidden_property(obj, "\u{0}locale", locv);
        self.store_str(obj, "calendar", &Some(resolved_ca));
        self.store_str(obj, "numberingSystem", &Some(resolved_nu));
        // hour12 (boolean) and hourCycle (enum).
        let hour12 = self.get_bool_option(opts, "hour12", None)?;
        if let Some(b) = hour12 {
            self.realm
                .set_hidden_property(obj, "hour12", NanBox::boolean(b));
        }
        let hc = self.get_string_option(opts, "hourCycle", &["h11", "h12", "h23", "h24"], None)?;
        self.store_str(obj, "hourCycle", &hc);
        // `timeZone`: ECMA-402 CreateDateTimeFormat steps 28–30. Undefined defaults
        // to the (system) UTC zone; otherwise the string is either an offset
        // identifier (`IsTimeZoneOffsetString`, canonicalized to `±HH:MM`) or a
        // named IANA identifier (ASCII-case-insensitively matched via the embedded
        // tz database and stored as its correctly-cased [[Identifier]] — links are
        // NOT canonicalized to their primary). Anything else is a RangeError.
        let tz = match self.get_string_option(opts, "timeZone", &[], None)? {
            Some(s) => self.dtf_resolve_time_zone(&s)?,
            None => String::from("UTC"),
        };
        self.store_str(obj, "timeZone", &Some(tz));
        // weekday/era/year/month/day/hour/minute/second/… component options.
        let nv = ["numeric", "2-digit"];
        let nm = ["long", "short", "narrow"];
        let weekday = self.get_string_option(opts, "weekday", &nm, None)?;
        self.store_str(obj, "weekday", &weekday);
        let era = self.get_string_option(opts, "era", &nm, None)?;
        self.store_str(obj, "era", &era);
        let year = self.get_string_option(opts, "year", &nv, None)?;
        self.store_str(obj, "year", &year);
        let month = self.get_string_option(
            opts,
            "month",
            &["numeric", "2-digit", "long", "short", "narrow"],
            None,
        )?;
        self.store_str(obj, "month", &month);
        let day = self.get_string_option(opts, "day", &nv, None)?;
        self.store_str(obj, "day", &day);
        let day_period = self.get_string_option(opts, "dayPeriod", &nm, None)?;
        self.store_str(obj, "dayPeriod", &day_period);
        let hour = self.get_string_option(opts, "hour", &nv, None)?;
        self.store_str(obj, "hour", &hour);
        let minute = self.get_string_option(opts, "minute", &nv, None)?;
        self.store_str(obj, "minute", &minute);
        let second = self.get_string_option(opts, "second", &nv, None)?;
        self.store_str(obj, "second", &second);
        let fsd = self.get_int_option(opts, "fractionalSecondDigits", 1.0, 3.0, None)?;
        if let Some(v) = fsd {
            self.realm
                .set_hidden_property(obj, "fractionalSecondDigits", NanBox::number(v));
        }
        let tzn = self.get_string_option(
            opts,
            "timeZoneName",
            &[
                "long",
                "short",
                "shortOffset",
                "longOffset",
                "shortGeneric",
                "longGeneric",
            ],
            None,
        )?;
        self.store_str(obj, "timeZoneName", &tzn);
        // formatMatcher (validated, unused).
        let _ = self.get_string_option(
            opts,
            "formatMatcher",
            &["basic", "best fit"],
            Some("best fit"),
        )?;
        let date_style = self.get_string_option(
            opts,
            "dateStyle",
            &["full", "long", "medium", "short"],
            None,
        )?;
        self.store_str(obj, "dateStyle", &date_style);
        let time_style = self.get_string_option(
            opts,
            "timeStyle",
            &["full", "long", "medium", "short"],
            None,
        )?;
        self.store_str(obj, "timeStyle", &time_style);
        // dateStyle/timeStyle are mutually exclusive with explicit component fields.
        if (date_style.is_some() || time_style.is_some())
            && (weekday.is_some()
                || era.is_some()
                || year.is_some()
                || month.is_some()
                || day.is_some()
                || hour.is_some()
                || minute.is_some()
                || second.is_some()
                || day_period.is_some()
                || fsd.is_some()
                || tzn.is_some())
        {
            let m = self.new_str("dateStyle/timeStyle may not be combined with component options");
            return Err(ExecError::Throw(self.make_error(N_TYPE_ERROR, Some(m))));
        }
        // If no component field and no dateStyle/timeStyle were requested, the
        // default format is a numeric date: year/month/day = "numeric".
        let any_field = weekday.is_some()
            || era.is_some()
            || year.is_some()
            || month.is_some()
            || day.is_some()
            || day_period.is_some()
            || hour.is_some()
            || minute.is_some()
            || second.is_some()
            || fsd.is_some()
            || tzn.is_some();
        if !any_field && date_style.is_none() && time_style.is_none() {
            let numeric = Some(String::from("numeric"));
            self.store_str(obj, "year", &numeric);
            self.store_str(obj, "month", &numeric);
            self.store_str(obj, "day", &numeric);
            // Mark that year/month/day are the implicit default (not user-requested),
            // so the ECMA-402 Temporal formatting protocol (which operates on the
            // *raw* component options) treats them as absent.
            self.realm
                .set_hidden_property(obj, "\u{0}dtf_default_date", NanBox::boolean(true));
        }
        Ok(())
    }

    /// ECMA-402 CreateDateTimeFormat time-zone resolution (steps 29–30). Accepts a
    /// UTC-offset identifier (`IsTimeZoneOffsetString`, minute precision only —
    /// `±HH`, `±HHMM`, `±HH:MM` — normalized to `±HH:MM`, and `-00` → `+00:00`), or
    /// a named IANA identifier matched ASCII-case-insensitively against the embedded
    /// tz database and returned as its correctly-cased [[Identifier]] (links such as
    /// `Asia/Calcutta` are preserved, NOT canonicalized to their primary). An empty
    /// string, a U+2212 sign, a malformed offset, or an unknown name is a RangeError.
    /// Reuses the Temporal time-zone helpers (same tz database as `ZonedDateTime`).
    fn dtf_resolve_time_zone(&mut self, s: &str) -> Result<String, ExecError> {
        use super::temporal_zoneddatetime::{parse_offset_id, resolve_named};
        if !s.is_empty() {
            if let Some((_, canon)) = parse_offset_id(s) {
                return Ok(canon);
            }
            if let Some(name) = resolve_named(s) {
                return Ok(name);
            }
        }
        let m = self.new_str(&alloc::format!("invalid time zone: {s}"));
        Err(ExecError::Throw(self.make_error(N_RANGE_ERROR, Some(m))))
    }

    /// The offset (in milliseconds east of UTC) that DateTimeFormat instance
    /// `handle`'s resolved `timeZone` applies at the exact instant `epoch_ms`.
    /// Returns 0 for the UTC default (and for any zone the tz database cannot
    /// resolve). Named zones consult the embedded IANA data (DST-aware); offset
    /// identifiers apply their fixed offset. Shared by the number/`Date` and the
    /// Temporal (Instant/ZonedDateTime) formatting paths.
    #[cfg(feature = "intl")]
    fn dtf_zone_offset_ms(&self, handle: Handle, epoch_ms: i64) -> i64 {
        let Some(tz) = self
            .realm
            .get_property(handle, "timeZone")
            .filter(|v| !matches!(v.unpack(), Unpacked::Undefined))
            .map(|v| self.realm.to_display_string(v))
        else {
            return 0;
        };
        if tz == "UTC" || tz.is_empty() {
            return 0;
        }
        let epoch_ns = i128::from(epoch_ms) * 1_000_000;
        (super::temporal_zoneddatetime::tz_offset_at(&tz, epoch_ns) / 1_000_000) as i64
    }

    /// Applies DateTimeFormat `handle`'s time zone to a Temporal value's epoch
    /// milliseconds: an Instant/ZonedDateTime is an exact instant, so its wall-clock
    /// rendering is shifted into the resolved zone (and the zone offset recorded for
    /// `timeZoneName`); the wall-clock Plain* types carry no instant and are rendered
    /// as-is (the zone is ignored, per the spec's `isPlain` handling). Returns the
    /// (possibly shifted) epoch milliseconds to decompose.
    #[cfg(feature = "intl")]
    fn dtf_apply_temporal_zone(
        &self,
        handle: Handle,
        ms: f64,
        kind: crate::temporal_iso::TemporalKind,
        o: &mut intl::datetime::DateTimeFormatOptions,
    ) -> f64 {
        use crate::temporal_iso::TemporalKind;
        if matches!(kind, TemporalKind::Instant | TemporalKind::ZonedDateTime) {
            let off = self.dtf_zone_offset_ms(handle, ms as i64);
            if o.time_zone_name.is_some() {
                o.tz_offset_minutes = Some((off / 60_000) as i32);
            }
            ms + off as f64
        } else {
            ms
        }
    }

    /// The locale's default 12-hour cycle (`"h11"`/`"h12"`) and its default clock
    /// (whether it is a 12-hour locale). `base_locale` has no `-u-hc-` keyword.
    /// Uses the `intl` crate's CLDR time patterns to decide 12- vs 24-hour; the
    /// non-intl build falls back to the en-family heuristic.
    fn locale_hour_defaults(&self, base_locale: &str) -> (&'static str, bool) {
        // CLDR's 12-hour cycle is `h11` (0–11) for Japanese, `h12` (1–12) else.
        let primary = base_locale.split('-').next().unwrap_or("");
        let hc12 = if primary.eq_ignore_ascii_case("ja") {
            "h11"
        } else {
            "h12"
        };
        #[cfg(feature = "intl")]
        {
            use intl::datetime::{DateTime, DateTimeFormatOptions, Numeric2Digit};
            let dt = DateTime {
                year: 2020,
                month: 1,
                day: 1,
                hour: 13,
                minute: 0,
                second: 0,
                millisecond: 0,
            };
            let mut o = DateTimeFormatOptions::default();
            o.hour = Some(Numeric2Digit::Numeric);
            if let Ok(parts) = intl::datetime::format_to_parts(base_locale, &dt, &o) {
                // A 12-hour locale renders 13:00 with a dayPeriod part (or an hour
                // value != "13").
                let is_12h = parts.iter().any(|p| {
                    matches!(p.kind, intl::datetime::DateTimePartType::DayPeriod)
                        || (matches!(p.kind, intl::datetime::DateTimePartType::Hour)
                            && p.value != "13")
                });
                return (hc12, is_12h);
            }
        }
        let is_12h = primary.eq_ignore_ascii_case("en");
        (hc12, is_12h)
    }

    /// ECMA-402 hour-cycle resolution for a `DateTimeFormat` instance: returns the
    /// resolved locale (its `-u-hc-` keyword kept only when it survives the option
    /// interplay), the resolved `[[HourCycle]]` (`None` when no hour field), and
    /// `[[HourCycle]]`-derived `hour12` (`None` when no hour field).
    fn dtf_hour_resolution(&self, handle: Handle) -> (String, Option<String>, Option<bool>) {
        let raw_locale = self
            .realm
            .get_property(handle, "\u{0}locale")
            .map(|v| self.realm.to_display_string(v))
            .unwrap_or_else(|| String::from("en-US"));
        let (base_locale, hc_ext) = split_u_keyword(&raw_locale, "hc");
        let hc_ext = hc_ext.filter(|v| matches!(v.as_str(), "h11" | "h12" | "h23" | "h24"));
        let get_str = |k: &str| -> Option<String> {
            self.realm
                .get_property(handle, k)
                .filter(|v| !matches!(v.unpack(), Unpacked::Undefined))
                .map(|v| self.realm.to_display_string(v))
        };
        let hc_opt = get_str("hourCycle");
        let hour12 = self
            .realm
            .get_property(handle, "hour12")
            .and_then(|v| v.as_boolean());
        // [[Hour]] is set iff an hour component or a timeStyle was requested.
        let hour_present = get_str("hour").is_some() || get_str("timeStyle").is_some();

        // Resolved locale: the `-u-hc-` keyword is kept only when it is present
        // and neither hour12 nor a *differing* hourCycle option overrides it.
        let keep_ext = hc_ext.is_some()
            && hour12.is_none()
            && match &hc_opt {
                Some(opt) => Some(opt.as_str()) == hc_ext.as_deref(),
                None => true,
            };
        let resolved_locale = if keep_ext {
            raw_locale.clone()
        } else {
            base_locale.clone()
        };

        if !hour_present {
            return (resolved_locale, None, None);
        }
        let (hc12, default_is_12h) = self.locale_hour_defaults(&base_locale);
        let resolved_hc = match hour12 {
            Some(true) => String::from(hc12),
            Some(false) => String::from("h23"),
            None => hc_opt
                .or(hc_ext)
                .unwrap_or_else(|| String::from(if default_is_12h { hc12 } else { "h23" })),
        };
        let h12 = matches!(resolved_hc.as_str(), "h11" | "h12");
        (resolved_locale, Some(resolved_hc), Some(h12))
    }

    /// `Intl.NumberFormat`/`DateTimeFormat` `resolvedOptions()` — a fresh object
    /// reporting the resolved configuration, in spec property order. `fmt` is the
    /// formatter instance (`None` → a default decimal NumberFormat shape).
    pub(crate) fn intl_resolved_options(&mut self, fmt: Option<Handle>) -> NanBox {
        let out = self.realm.new_object();
        let kind = fmt
            .and_then(|h| self.realm.get_property(h, "\u{0}intl"))
            .map(|v| self.realm.to_display_string(v))
            .unwrap_or_else(|| String::from("number"));
        let get_str = |this: &Self, key: &str| -> Option<String> {
            fmt.and_then(|h| this.realm.get_property(h, key))
                .filter(|v| !matches!(v.unpack(), Unpacked::Undefined))
                .map(|v| this.realm.to_display_string(v))
        };
        let get_num = |this: &Self, key: &str| -> Option<f64> {
            fmt.and_then(|h| this.realm.get_property(h, key))
                .filter(|v| !matches!(v.unpack(), Unpacked::Undefined))
                .map(|v| this.realm.to_number(v))
        };
        let locale = get_str(self, "\u{0}locale").unwrap_or_else(|| String::from("en-US"));
        let lv = self.new_str(&locale);
        self.realm.set_property(out, "locale", lv);

        if kind == "segmenter" {
            // `Intl.Segmenter.prototype.resolvedOptions()` — `{ locale, granularity }`.
            let gran = get_str(self, "granularity").unwrap_or_else(|| String::from("grapheme"));
            let gv = self.new_str(&gran);
            self.realm.set_property(out, "granularity", gv);
        } else if kind == "collator" {
            // `Intl.Collator.prototype.resolvedOptions()` — `{ locale, usage,
            // sensitivity, ignorePunctuation, collation, numeric?, caseFirst? }`
            // in that order (Table: "Resolved Options of Collator instances").
            let usage = get_str(self, "usage").unwrap_or_else(|| String::from("sort"));
            let uv = self.new_str(&usage);
            self.realm.set_property(out, "usage", uv);
            let sensitivity =
                get_str(self, "sensitivity").unwrap_or_else(|| String::from("variant"));
            let sv = self.new_str(&sensitivity);
            self.realm.set_property(out, "sensitivity", sv);
            let ip = fmt
                .and_then(|h| self.realm.get_property(h, "ignorePunctuation"))
                .is_some_and(|v| self.realm.truthy(v));
            self.realm
                .set_property(out, "ignorePunctuation", NanBox::boolean(ip));
            let collation = get_str(self, "collation").unwrap_or_else(|| String::from("default"));
            let cv = self.new_str(&collation);
            self.realm.set_property(out, "collation", cv);
            // `numeric`/`caseFirst` are reported only when the instance resolved
            // them (they are optional keys of the resolved-options table).
            if let Some(n) = fmt.and_then(|h| self.realm.get_property(h, "numeric")) {
                let nv = NanBox::boolean(self.realm.truthy(n));
                self.realm.set_property(out, "numeric", nv);
            }
            if let Some(cf) = get_str(self, "caseFirst") {
                let cfv = self.new_str(&cf);
                self.realm.set_property(out, "caseFirst", cfv);
            }
        } else if kind == "display" {
            // `Intl.DisplayNames.prototype.resolvedOptions()` — `{ locale, style,
            // type, fallback[, languageDisplay] }` (Table: "Resolved Options of
            // DisplayNames instances").
            let style = get_str(self, "style").unwrap_or_else(|| String::from("long"));
            let stv = self.new_str(&style);
            self.realm.set_property(out, "style", stv);
            if let Some(t) = get_str(self, "type") {
                let tv = self.new_str(&t);
                self.realm.set_property(out, "type", tv);
            }
            let fallback = get_str(self, "fallback").unwrap_or_else(|| String::from("code"));
            let fv = self.new_str(&fallback);
            self.realm.set_property(out, "fallback", fv);
            if get_str(self, "type").as_deref() == Some("language") {
                let ld =
                    get_str(self, "languageDisplay").unwrap_or_else(|| String::from("dialect"));
                let ldv = self.new_str(&ld);
                self.realm.set_property(out, "languageDisplay", ldv);
            }
        } else if kind == "list" {
            // `Intl.ListFormat.prototype.resolvedOptions()` — `{ locale, type, style }`
            // in that order (Table: "Resolved Options of ListFormat instances").
            let lt = get_str(self, "type").unwrap_or_else(|| String::from("conjunction"));
            let ltv = self.new_str(&lt);
            self.realm.set_property(out, "type", ltv);
            let style = get_str(self, "style").unwrap_or_else(|| String::from("long"));
            let stv = self.new_str(&style);
            self.realm.set_property(out, "style", stv);
        } else if kind == "rtf" {
            // `Intl.RelativeTimeFormat.prototype.resolvedOptions()` —
            // `{ locale, style, numeric, numberingSystem }` in that order
            // (Table: "Resolved Options of RelativeTimeFormat instances"). The
            // resolved numbering system for the supported locales is always `latn`.
            let style = get_str(self, "style").unwrap_or_else(|| String::from("long"));
            let stv = self.new_str(&style);
            self.realm.set_property(out, "style", stv);
            let numeric = get_str(self, "numeric").unwrap_or_else(|| String::from("always"));
            let nv = self.new_str(&numeric);
            self.realm.set_property(out, "numeric", nv);
            let ns = get_str(self, "numberingSystem").unwrap_or_else(|| String::from("latn"));
            let nsv = self.new_str(&ns);
            self.realm.set_property(out, "numberingSystem", nsv);
        } else if kind == "plural" {
            // `Intl.PluralRules.prototype.resolvedOptions()` — the key order is
            // `{ locale, type, notation, minimumIntegerDigits, (fraction|significant
            // digit pair), pluralCategories, roundingIncrement, roundingMode,
            // roundingPriority, trailingZeroDisplay }`.
            let pr_type = get_str(self, "type").unwrap_or_else(|| String::from("cardinal"));
            let tv = self.new_str(&pr_type);
            self.realm.set_property(out, "type", tv);
            let notation = get_str(self, "notation").unwrap_or_else(|| String::from("standard"));
            let nv = self.new_str(&notation);
            self.realm.set_property(out, "notation", nv);
            if notation == "compact" {
                let cd = get_str(self, "compactDisplay").unwrap_or_else(|| String::from("short"));
                let cdv = self.new_str(&cd);
                self.realm.set_property(out, "compactDisplay", cdv);
            }
            // Digit options, resolved as in SetNumberFormatDigitOptions (decimal
            // defaults: minimumFractionDigits 0, maximumFractionDigits 3).
            let mnid = get_num(self, "minimumIntegerDigits").unwrap_or(1.0);
            self.realm
                .set_property(out, "minimumIntegerDigits", NanBox::number(mnid));
            let mnsd = get_num(self, "minimumSignificantDigits");
            let mxsd = get_num(self, "maximumSignificantDigits");
            let mnfd_opt = get_num(self, "minimumFractionDigits");
            let mxfd_opt = get_num(self, "maximumFractionDigits");
            let priority =
                get_str(self, "roundingPriority").unwrap_or_else(|| String::from("auto"));
            let has_sig = mnsd.is_some() || mxsd.is_some();
            let report_frac = |this: &mut Self, out: Handle| {
                let mnfd = mnfd_opt.unwrap_or(0.0);
                let mxfd = mxfd_opt.unwrap_or_else(|| 3.0_f64.max(mnfd));
                this.realm
                    .set_property(out, "minimumFractionDigits", NanBox::number(mnfd));
                this.realm
                    .set_property(out, "maximumFractionDigits", NanBox::number(mxfd));
            };
            let report_sig = |this: &mut Self, out: Handle| {
                let mnsd = mnsd.unwrap_or(1.0);
                let mxsd = mxsd.unwrap_or(21.0);
                this.realm
                    .set_property(out, "minimumSignificantDigits", NanBox::number(mnsd));
                this.realm
                    .set_property(out, "maximumSignificantDigits", NanBox::number(mxsd));
            };
            if priority == "morePrecision" || priority == "lessPrecision" {
                report_frac(self, out);
                report_sig(self, out);
            } else if has_sig {
                report_sig(self, out);
            } else {
                report_frac(self, out);
            }
            // pluralCategories: a fresh sorted array on every call.
            let ordinal = pr_type == "ordinal";
            let cats = self.plural_categories(&locale, ordinal);
            let cat_vals: Vec<NanBox> = cats.iter().map(|c| self.new_str(c)).collect();
            let arr = self.realm.new_array(cat_vals);
            self.realm
                .set_property(out, "pluralCategories", NanBox::handle(arr.to_raw()));
            // Rounding options.
            let rinc = get_num(self, "roundingIncrement").unwrap_or(1.0);
            self.realm
                .set_property(out, "roundingIncrement", NanBox::number(rinc));
            let rm = get_str(self, "roundingMode").unwrap_or_else(|| String::from("halfExpand"));
            let rmv = self.new_str(&rm);
            self.realm.set_property(out, "roundingMode", rmv);
            let rp = self.new_str(&priority);
            self.realm.set_property(out, "roundingPriority", rp);
            let tzd = get_str(self, "trailingZeroDisplay").unwrap_or_else(|| String::from("auto"));
            let tzv = self.new_str(&tzd);
            self.realm.set_property(out, "trailingZeroDisplay", tzv);
        } else if kind == "number" {
            let ns = get_str(self, "numberingSystem").unwrap_or_else(|| String::from("latn"));
            let nsv = self.new_str(&ns);
            self.realm.set_property(out, "numberingSystem", nsv);
            let style = get_str(self, "style").unwrap_or_else(|| String::from("decimal"));
            let sv = self.new_str(&style);
            self.realm.set_property(out, "style", sv);
            if style == "currency" {
                if let Some(c) = get_str(self, "currency") {
                    let cv = self.new_str(&c);
                    self.realm.set_property(out, "currency", cv);
                }
                let cd = get_str(self, "currencyDisplay").unwrap_or_else(|| String::from("symbol"));
                let cdv = self.new_str(&cd);
                self.realm.set_property(out, "currencyDisplay", cdv);
                let cs = get_str(self, "currencySign").unwrap_or_else(|| String::from("standard"));
                let csv = self.new_str(&cs);
                self.realm.set_property(out, "currencySign", csv);
            }
            if style == "unit" {
                if let Some(u) = get_str(self, "unit") {
                    let uv = self.new_str(&u);
                    self.realm.set_property(out, "unit", uv);
                }
                let ud = get_str(self, "unitDisplay").unwrap_or_else(|| String::from("short"));
                let udv = self.new_str(&ud);
                self.realm.set_property(out, "unitDisplay", udv);
            }
            // Digit options. Resolve per SetNumberFormatDigitOptions: significant
            // digits (if requested) are reported; otherwise fraction digits with
            // style-derived defaults. roundingPriority "auto" with neither set
            // reports fraction digits only.
            let mnid = get_num(self, "minimumIntegerDigits").unwrap_or(1.0);
            self.realm
                .set_property(out, "minimumIntegerDigits", NanBox::number(mnid));
            let mnsd = get_num(self, "minimumSignificantDigits");
            let mxsd = get_num(self, "maximumSignificantDigits");
            let mnfd_opt = get_num(self, "minimumFractionDigits");
            let mxfd_opt = get_num(self, "maximumFractionDigits");
            let priority =
                get_str(self, "roundingPriority").unwrap_or_else(|| String::from("auto"));
            let has_sig = mnsd.is_some() || mxsd.is_some();
            // SetNumberFormatDigitOptions defaults: the currency-specific fraction
            // digits apply only in "standard" notation; compact defaults max to 0;
            // scientific/engineering (and standard non-currency) use 0..=3; percent 0.
            let notation = get_str(self, "notation").unwrap_or_else(|| String::from("standard"));
            let (def_min, def_max): (f64, f64) = match style.as_str() {
                "currency" if notation == "standard" => (2.0, 2.0),
                "percent" => (0.0, 0.0),
                _ if notation == "compact" => (0.0, 0.0),
                _ => (0.0, 3.0),
            };
            let report_frac = |this: &mut Self, out: Handle| {
                let mnfd = mnfd_opt.unwrap_or(def_min);
                let mxfd = mxfd_opt.unwrap_or_else(|| def_max.max(mnfd));
                this.realm
                    .set_property(out, "minimumFractionDigits", NanBox::number(mnfd));
                this.realm
                    .set_property(out, "maximumFractionDigits", NanBox::number(mxfd));
            };
            let report_sig = |this: &mut Self, out: Handle| {
                let mnsd = mnsd.unwrap_or(1.0);
                let mxsd = mxsd.unwrap_or(21.0);
                this.realm
                    .set_property(out, "minimumSignificantDigits", NanBox::number(mnsd));
                this.realm
                    .set_property(out, "maximumSignificantDigits", NanBox::number(mxsd));
            };
            if priority == "morePrecision" || priority == "lessPrecision" {
                // Both groups present.
                report_frac(self, out);
                report_sig(self, out);
            } else if has_sig {
                report_sig(self, out);
            } else {
                // Significant digits absent → fraction digits (with or without
                // explicit values, defaulted by style).
                report_frac(self, out);
            }

            let ug = fmt
                .and_then(|h| self.realm.get_property(h, "useGrouping"))
                .filter(|v| !matches!(v.unpack(), Unpacked::Undefined))
                .unwrap_or_else(|| self.new_str("auto"));
            self.realm.set_property(out, "useGrouping", ug);
            let notation = get_str(self, "notation").unwrap_or_else(|| String::from("standard"));
            let nv = self.new_str(&notation);
            self.realm.set_property(out, "notation", nv);
            if notation == "compact" {
                let cd = get_str(self, "compactDisplay").unwrap_or_else(|| String::from("short"));
                let cdv = self.new_str(&cd);
                self.realm.set_property(out, "compactDisplay", cdv);
            }
            let sd = get_str(self, "signDisplay").unwrap_or_else(|| String::from("auto"));
            let sdv = self.new_str(&sd);
            self.realm.set_property(out, "signDisplay", sdv);
            let rinc = get_num(self, "roundingIncrement").unwrap_or(1.0);
            self.realm
                .set_property(out, "roundingIncrement", NanBox::number(rinc));
            let rm = get_str(self, "roundingMode").unwrap_or_else(|| String::from("halfExpand"));
            let rmv = self.new_str(&rm);
            self.realm.set_property(out, "roundingMode", rmv);
            let rp = self.new_str(&priority);
            self.realm.set_property(out, "roundingPriority", rp);
            let tzd = get_str(self, "trailingZeroDisplay").unwrap_or_else(|| String::from("auto"));
            let tzv = self.new_str(&tzd);
            self.realm.set_property(out, "trailingZeroDisplay", tzv);
        } else {
            // DateTimeFormat resolvedOptions — property order per spec:
            // locale, calendar, numberingSystem, timeZone, hourCycle, hour12,
            // weekday, era, year, month, day, dayPeriod, hour, minute, second,
            // fractionalSecondDigits, timeZoneName, then dateStyle/timeStyle.
            let cal = get_str(self, "calendar").unwrap_or_else(|| String::from("gregory"));
            let cv = self.new_str(&cal);
            self.realm.set_property(out, "calendar", cv);
            let ns = get_str(self, "numberingSystem").unwrap_or_else(|| String::from("latn"));
            let nsv = self.new_str(&ns);
            self.realm.set_property(out, "numberingSystem", nsv);
            let tz = get_str(self, "timeZone").unwrap_or_else(|| String::from("UTC"));
            let tzv = self.new_str(&tz);
            self.realm.set_property(out, "timeZone", tzv);
            // Hour-cycle resolution (ECMA-402 CreateDateTimeFormat + the
            // resolvedOptions Table 6/7 rules): `[[HourCycle]]` and the derived
            // `hour12` are reported *only* when an hour field is present (no hour →
            // both undefined; e.g. a dateStyle-only or numeric-date formatter). The
            // resolved locale's `-u-hc-` keyword survives only when it is not
            // overridden by a differing option.
            if let Some(fmt) = fmt {
                let (resolved_locale, hour_cycle, hour12) = self.dtf_hour_resolution(fmt);
                let lv = self.new_str(&resolved_locale);
                self.realm.set_property(out, "locale", lv);
                if let Some(hc) = hour_cycle {
                    let v = self.new_str(&hc);
                    self.realm.set_property(out, "hourCycle", v);
                }
                if let Some(h12) = hour12 {
                    self.realm.set_property(out, "hour12", NanBox::boolean(h12));
                }
            }
            for key in [
                "weekday",
                "era",
                "year",
                "month",
                "day",
                "dayPeriod",
                "hour",
                "minute",
                "second",
            ] {
                if let Some(v) = get_str(self, key) {
                    let vv = self.new_str(&v);
                    self.realm.set_property(out, key, vv);
                }
            }
            // fractionalSecondDigits precedes timeZoneName in the spec order.
            if let Some(v) = get_num(self, "fractionalSecondDigits") {
                self.realm
                    .set_property(out, "fractionalSecondDigits", NanBox::number(v));
            }
            for key in ["timeZoneName", "dateStyle", "timeStyle"] {
                if let Some(v) = get_str(self, key) {
                    let vv = self.new_str(&v);
                    self.realm.set_property(out, key, vv);
                }
            }
        }
        NanBox::handle(out.to_raw())
    }

    /// Formats `value` per the `Intl.NumberFormat`/`DateTimeFormat` instance `handle`
    /// (a `\0intl`-marked object). Shared by `nf.format(x)` and the bound `nf.format`.
    /// `Number.prototype.toLocaleString(locale, options)` — with no options this is the
    /// grouped default; with an options object it honors `style` (decimal/percent/
    /// currency), `currency`, and `minimum`/`maximumFractionDigits` (en-US-ish, no real
    /// locale data; the rounding mode follows Rust's formatter, ~halfExpand).
    pub(crate) fn number_to_locale_string(&self, n: f64, opts: Option<NanBox>) -> String {
        let oh = match opts {
            Some(v) if !matches!(v.unpack(), Unpacked::Undefined | Unpacked::Null) => {
                match v.as_handle() {
                    Some(raw) => Handle::from_raw(raw),
                    None => return group_thousands(n),
                }
            }
            _ => return group_thousands(n),
        };
        // Non-finite values ignore the options (NaN/∞/-∞).
        if !n.is_finite() {
            return group_thousands(n);
        }
        let str_opt = |key: &str| -> Option<String> {
            self.realm
                .get_property(oh, key)
                .filter(|v| !matches!(v.unpack(), Unpacked::Undefined))
                .map(|v| self.realm.to_display_string(v))
        };
        let num_opt = |key: &str| -> Option<i32> {
            self.realm
                .get_property(oh, key)
                .filter(|v| !matches!(v.unpack(), Unpacked::Undefined))
                .map(|v| self.realm.to_number(v) as i32)
        };
        let style = str_opt("style").unwrap_or_else(|| String::from("decimal"));
        let (value, prefix, suffix, def_min, def_max) = match style.as_str() {
            "percent" => (n * 100.0, String::new(), String::from("%"), 0, 0),
            "currency" => {
                let sym = currency_symbol(&str_opt("currency").unwrap_or_default());
                (n, sym, String::new(), 2, 2)
            }
            _ => (n, String::new(), String::new(), 0, 3),
        };
        let min_frac = num_opt("minimumFractionDigits")
            .unwrap_or(def_min)
            .clamp(0, 100);
        let max_frac = num_opt("maximumFractionDigits")
            .unwrap_or(def_max.max(min_frac))
            .clamp(min_frac, 100);
        let neg = value.is_sign_negative() && value != 0.0;
        // Round to `max_frac` places, then trim trailing zeros down to `min_frac`.
        let formatted = alloc::format!("{:.*}", max_frac as usize, value.abs());
        let trimmed = if max_frac > min_frac && formatted.contains('.') {
            let dot = formatted.find('.').unwrap();
            let keep_min = dot + 1 + min_frac as usize;
            let mut end = formatted.len();
            while end > keep_min && formatted.as_bytes()[end - 1] == b'0' {
                end -= 1;
            }
            if end == dot + 1 {
                end = dot; // no fractional digits left → drop the '.'
            }
            String::from(&formatted[..end])
        } else {
            formatted
        };
        let grouped = group_thousands_str(&trimmed);
        let mut out = String::new();
        if neg {
            out.push('-');
        }
        out.push_str(&prefix);
        out.push_str(&grouped);
        out.push_str(&suffix);
        out
    }

    /// `Intl.DateTimeFormat` operand coercion for `format`/`formatToParts`:
    /// `undefined` → the current time; otherwise `? ToNumber(value)` (which runs
    /// a throwing `valueOf`), then `TimeClip`. A non-finite or out-of-range time
    /// value (`|x| > 8.64e15`) is a **RangeError** (sec-partitiondatetimepattern).
    pub(crate) fn datetime_operand(&mut self, value: NanBox) -> Result<f64, ExecError> {
        let x = if matches!(value.unpack(), Unpacked::Undefined) {
            now_ms()
        } else {
            let n = self.coerce_to_number(value)?;
            self.realm.to_number(n)
        };
        if !x.is_finite() || x.abs() > 8.64e15_f64 {
            let m = self.new_str("date value is not a finite time value");
            return Err(ExecError::Throw(self.make_error(N_RANGE_ERROR, Some(m))));
        }
        Ok(x)
    }

    /// The `[[NumberFormat]]`/`[[DateTimeFormat]]` instance for a `formatRange`
    /// call, plus its two coerced numeric endpoints. Per spec both `start`/`end`
    /// are **required** (`undefined` → TypeError) and must not be `NaN`
    /// (→ RangeError); a `start > end` is allowed. For a DateTimeFormat the
    /// endpoints are additionally `TimeClip`-validated (non-finite / out-of-range
    /// → RangeError). Returns `(instance, x, y)`.
    fn intl_range_operands(
        &mut self,
        this: NanBox,
        start: NanBox,
        end: NanBox,
    ) -> Result<(Handle, f64, f64), ExecError> {
        let Some(inst) = this
            .as_handle()
            .map(Handle::from_raw)
            .filter(|h| self.realm.get_property(*h, "\u{0}intl").is_some())
        else {
            return Err(self.type_error("formatRange called on an incompatible receiver"));
        };
        if matches!(start.unpack(), Unpacked::Undefined)
            || matches!(end.unpack(), Unpacked::Undefined)
        {
            return Err(self.type_error("formatRange requires two defined arguments"));
        }
        // `Intl.NumberFormat` uses ToIntlMathematicalValue, which accepts a BigInt
        // operand (via its numeric value); `Intl.DateTimeFormat` uses ToNumber,
        // which throws on a BigInt. Branch on the receiver kind.
        let is_number_format = self
            .realm
            .get_property(inst, "\u{0}intl")
            .map(|k| self.realm.to_display_string(k))
            .as_deref()
            == Some("number");
        let coerce_operand = |this: &mut Self, v: NanBox| -> Result<f64, ExecError> {
            if is_number_format
                && let Some(h) = v.as_handle().map(Handle::from_raw)
                && let Some(big) = this.realm.bigint_at(h)
            {
                return Ok(big.to_f64());
            }
            let n = this.coerce_to_number(v)?;
            Ok(this.realm.to_number(n))
        };
        let x = coerce_operand(self, start)?;
        let y = coerce_operand(self, end)?;
        if x.is_nan() || y.is_nan() {
            let m = self.new_str("formatRange arguments must not be NaN");
            return Err(ExecError::Throw(self.make_error(N_RANGE_ERROR, Some(m))));
        }
        let is_datetime = self
            .realm
            .get_property(inst, "\u{0}intl")
            .map(|k| self.realm.to_display_string(k))
            .as_deref()
            == Some("datetime");
        if is_datetime && (!x.is_finite() || x.abs() > 8.64e15_f64 || y.abs() > 8.64e15_f64) {
            let m = self.new_str("formatRange date value is not a finite time value");
            return Err(ExecError::Throw(self.make_error(N_RANGE_ERROR, Some(m))));
        }
        Ok((inst, x, y))
    }

    /// `Intl.NumberFormat/DateTimeFormat.prototype.formatRange(x, y)`: formats each
    /// endpoint and joins them with an en-dash range separator (a basic
    /// `FormatNumericRange`; `x === y` collapses to the single formatted value).
    pub(crate) fn intl_format_range(
        &mut self,
        this: NanBox,
        start: NanBox,
        end: NanBox,
    ) -> Result<String, ExecError> {
        let Some(inst) = this
            .as_handle()
            .map(Handle::from_raw)
            .filter(|h| self.realm.get_property(*h, "\u{0}intl").is_some())
        else {
            return Err(self.type_error("formatRange called on an incompatible receiver"));
        };
        // A DateTimeFormat range is rendered via the CLDR interval patterns (shared
        // fields collapsed, the locale interval separator); the flat string is the
        // concatenation of the tagged range parts.
        if self.intl_kind_is_datetime(inst) {
            let parts = self.dtf_range_dispatch(inst, start, end)?;
            return Ok(parts.iter().map(|(_, v, _)| v.as_str()).collect());
        }
        let (inst, x, y) = self.intl_range_operands(this, start, end)?;
        let fx = self.intl_format_value(inst, NanBox::number(x));
        if x == y {
            return Ok(fx);
        }
        let fy = self.intl_format_value(inst, NanBox::number(y));
        Ok(alloc::format!("{fx}\u{2013}{fy}"))
    }

    /// Whether the `\0intl`-branded instance `inst` is an `Intl.DateTimeFormat`.
    fn intl_kind_is_datetime(&self, inst: Handle) -> bool {
        self.realm
            .get_property(inst, "\u{0}intl")
            .map(|k| self.realm.to_display_string(k))
            .as_deref()
            == Some("datetime")
    }

    /// `formatRangeToParts(x, y)` → an array of `{ type, value, source }` parts.
    pub(crate) fn intl_format_range_to_parts(
        &mut self,
        this: NanBox,
        start: NanBox,
        end: NanBox,
    ) -> Result<NanBox, ExecError> {
        let Some(inst) = this
            .as_handle()
            .map(Handle::from_raw)
            .filter(|h| self.realm.get_property(*h, "\u{0}intl").is_some())
        else {
            return Err(self.type_error("formatRange called on an incompatible receiver"));
        };
        // `Intl.DateTimeFormat`: for a Gregorian, number-valued range, the `intl`
        // crate's `format_range_to_parts` applies the CLDR interval patterns (the
        // greatest-difference field, shared-field collapse, the locale interval
        // separator). Temporal endpoints and non-Gregorian calendars fall back to a
        // field-level approximation (each endpoint's `formatToParts`, tagged by
        // source), which still collapses byte-for-byte to `formatToParts` when the
        // endpoints are practically equal.
        if self.intl_kind_is_datetime(inst) {
            let tagged = self.dtf_range_dispatch(inst, start, end)?;
            return Ok(self.intl_build_source_parts(tagged));
        }
        // `Intl.NumberFormat`: format each endpoint as a single value joined by a
        // shared range separator (`x === y` collapses to the lone start value).
        let (inst, x, y) = self.intl_range_operands(this, start, end)?;
        let fx = self.intl_format_value(inst, NanBox::number(x));
        let mut parts: Vec<(&str, String, &str)> = alloc::vec![("literal", fx, "startRange")];
        if x != y {
            let fy = self.intl_format_value(inst, NanBox::number(y));
            parts.push(("literal", String::from("\u{2013}"), "shared"));
            parts.push(("literal", fy, "endRange"));
        }
        Ok(self.intl_build_source_parts(parts))
    }

    /// Resolves and validates a `DateTimeFormat` range's two endpoints, returning
    /// `(startIsTemporal, startNumber, endIsTemporal, endNumber)`. Both endpoints are
    /// required (`undefined` → TypeError) and must share the same Date/Temporal kind.
    ///
    /// Per `PartitionDateTimeRangePattern`, `ToDateTimeFormattable` runs on *both*
    /// endpoints (in order) — a Temporal object is kept as-is, anything else is
    /// `ToNumber`-coerced (running its `valueOf`) — **before** the same-kind check.
    /// A `NaN` from `ToNumber` does not throw here; the `TimeClip` RangeError is
    /// raised only later, when the number is actually formatted.
    fn dtf_resolve_range_operands(
        &mut self,
        start: NanBox,
        end: NanBox,
    ) -> Result<(bool, f64, bool, f64), ExecError> {
        if matches!(start.unpack(), Unpacked::Undefined)
            || matches!(end.unpack(), Unpacked::Undefined)
        {
            return Err(self.type_error("formatRange requires two defined arguments"));
        }
        #[cfg(feature = "intl")]
        let sx_temporal = self.is_temporal_value(start);
        #[cfg(not(feature = "intl"))]
        let sx_temporal = false;
        let sx_num = if sx_temporal {
            0.0
        } else {
            let n = self.coerce_to_number(start)?;
            self.realm.to_number(n)
        };
        #[cfg(feature = "intl")]
        let sy_temporal = self.is_temporal_value(end);
        #[cfg(not(feature = "intl"))]
        let sy_temporal = false;
        let sy_num = if sy_temporal {
            0.0
        } else {
            let n = self.coerce_to_number(end)?;
            self.realm.to_number(n)
        };
        // SameTemporalType: if either endpoint is a Temporal object, both must be
        // the same Temporal kind (a plain number counts as its own, non-Temporal
        // kind, so a Temporal-vs-number pair is always a mismatch).
        #[cfg(feature = "intl")]
        if (sx_temporal || sy_temporal) && self.range_type_tag(start) != self.range_type_tag(end) {
            return Err(
                self.type_error("formatRange arguments must be of the same Date/Temporal type")
            );
        }
        Ok((sx_temporal, sx_num, sy_temporal, sy_num))
    }

    /// The tagged `(type, value, source)` parts of a `DateTimeFormat` range.
    ///
    /// A Gregorian, number-valued range is rendered through the `intl` crate's
    /// `format_range_to_parts` (real CLDR interval patterns). Temporal endpoints and
    /// non-Gregorian calendars fall back to a field-level approximation: each
    /// endpoint's `formatToParts`, tagged `startRange`/`endRange` and joined by a
    /// shared interval separator, collapsing to all-`shared` when the endpoints are
    /// practically equal.
    fn dtf_range_dispatch(
        &mut self,
        inst: Handle,
        start: NanBox,
        end: NanBox,
    ) -> Result<Vec<(&'static str, String, &'static str)>, ExecError> {
        let (sx_temporal, sx_num, sy_temporal, sy_num) =
            self.dtf_resolve_range_operands(start, end)?;
        #[cfg(feature = "intl")]
        {
            let cal = self.dtf_resolved_calendar(inst);
            let gregorian = matches!(cal.as_str(), "gregory" | "gregorian" | "iso8601");
            if gregorian && !sx_temporal && !sy_temporal {
                for n in [sx_num, sy_num] {
                    if !n.is_finite() || n.abs() > 8.64e15_f64 {
                        let m = self.new_str("date value is not a finite time value");
                        return Err(ExecError::Throw(self.make_error(N_RANGE_ERROR, Some(m))));
                    }
                }
                // The collapse decision uses each endpoint's full field parts (so a
                // second-/fractional-second-only difference — which the crate's
                // greatest-difference search does not detect — still produces a
                // range). Practically-equal endpoints collapse to all-`shared`.
                let sp = self.datetime_parts(inst, sx_num);
                let ep = self.datetime_parts(inst, sy_num);
                if sp == ep {
                    return Ok(sp.into_iter().map(|(t, v)| (t, v, "shared")).collect());
                }
                // Endpoints differ: prefer the crate's CLDR interval patterns when it
                // resolves a genuine split; otherwise (it only collapsed because the
                // sole difference is below the minute) fall back to a field-level range
                // joined by the default CLDR interval separator.
                let crate_parts = self.dtf_range_crate_parts(inst, sx_num, sy_num);
                if crate_parts.iter().any(|(_, _, src)| *src != "shared") {
                    return Ok(crate_parts);
                }
                let mut v: Vec<(&'static str, String, &'static str)> =
                    Vec::with_capacity(sp.len() + ep.len() + 1);
                for (t, val) in sp {
                    v.push((t, val, "startRange"));
                }
                v.push((
                    "literal",
                    String::from("\u{2009}\u{2013}\u{2009}"),
                    "shared",
                ));
                for (t, val) in ep {
                    v.push((t, val, "endRange"));
                }
                return Ok(v);
            }
        }
        let sp = self.dtf_operand_parts(inst, start, sx_temporal, sx_num)?;
        let ep = self.dtf_operand_parts(inst, end, sy_temporal, sy_num)?;
        Ok(if sp == ep {
            sp.into_iter().map(|(t, v)| (t, v, "shared")).collect()
        } else {
            let mut v: Vec<(&'static str, String, &'static str)> =
                Vec::with_capacity(sp.len() + ep.len() + 1);
            for (t, val) in sp {
                v.push((t, val, "startRange"));
            }
            v.push(("literal", String::from("\u{2013}"), "shared"));
            for (t, val) in ep {
                v.push((t, val, "endRange"));
            }
            v
        })
    }

    /// The tagged range parts for a Gregorian, number-valued `DateTimeFormat` range,
    /// via the `intl` crate's CLDR interval patterns. Numeric part values are mapped
    /// through the instance's resolved numbering system.
    #[cfg(feature = "intl")]
    fn dtf_range_crate_parts(
        &mut self,
        inst: Handle,
        sx_num: f64,
        sy_num: f64,
    ) -> Vec<(&'static str, String, &'static str)> {
        use intl::datetime::{self, DateTimePartType};
        let (locale, dt1, o) = self.dtf_locale_dt_opts(inst, sx_num);
        let (_, dt2, _) = self.dtf_locale_dt_opts(inst, sy_num);
        let raw = match datetime::format_range_to_parts(&locale, &dt1, &dt2, &o) {
            Ok(parts) => parts,
            Err(_) => return Vec::new(),
        };
        // Match `dtf_pad_time_parts`: an abutting hour/second widens a single-digit
        // minute (and hour/minute widens second) to two digits, so a collapsed range
        // is byte-for-byte `format` (which pads the same way).
        let has_hour = raw.iter().any(|p| p.kind == DateTimePartType::Hour);
        let has_min = raw.iter().any(|p| p.kind == DateTimePartType::Minute);
        let has_sec = raw.iter().any(|p| p.kind == DateTimePartType::Second);
        raw.into_iter()
            .map(|p| {
                let widen = match p.kind {
                    DateTimePartType::Minute => has_hour || has_sec,
                    DateTimePartType::Second => has_hour || has_min,
                    _ => false,
                };
                let mut v = p.value;
                if widen && v.len() == 1 && v.as_bytes()[0].is_ascii_digit() {
                    v.insert(0, '0');
                }
                let value = self.apply_numbering_digits(inst, v);
                (p.kind.as_str(), value, p.source.as_str())
            })
            .collect()
    }

    /// The field-level parts of one already-`ToDateTimeFormattable`-resolved range
    /// endpoint: a Temporal object via the ECMA-402 Temporal protocol, otherwise the
    /// `TimeClip`-validated number `num` (non-finite / out-of-range → RangeError).
    #[cfg_attr(not(feature = "intl"), expect(unused_variables))]
    fn dtf_operand_parts(
        &mut self,
        inst: Handle,
        value: NanBox,
        is_temporal: bool,
        num: f64,
    ) -> Result<Vec<(&'static str, String)>, ExecError> {
        #[cfg(feature = "intl")]
        if is_temporal && let Some(p) = self.temporal_format_parts(inst, value, false)? {
            return Ok(p);
        }
        if !num.is_finite() || num.abs() > 8.64e15_f64 {
            let m = self.new_str("date value is not a finite time value");
            return Err(ExecError::Throw(self.make_error(N_RANGE_ERROR, Some(m))));
        }
        Ok(self.datetime_parts(inst, num))
    }

    /// Materializes a `[{ type, value, source }]` array from `(type, value, source)`
    /// triples.
    fn intl_build_source_parts(&mut self, parts: Vec<(&str, String, &str)>) -> NanBox {
        let mut elems = Vec::with_capacity(parts.len());
        for (ty, val, src) in parts {
            let o = self.realm.new_object();
            let tv = self.new_str(ty);
            let vv = self.new_str(&val);
            let sv = self.new_str(src);
            self.realm.set_property(o, "type", tv);
            self.realm.set_property(o, "value", vv);
            self.realm.set_property(o, "source", sv);
            elems.push(NanBox::handle(o.to_raw()));
        }
        NanBox::handle(self.realm.new_array(elems).to_raw())
    }

    pub(crate) fn intl_format_value(&mut self, handle: Handle, value: NanBox) -> String {
        let kind = self
            .realm
            .get_property(handle, "\u{0}intl")
            .map(|k| self.realm.to_display_string(k))
            .unwrap_or_default();
        if kind == "datetime" {
            let ms = match value.as_handle().map(Handle::from_raw) {
                Some(h) if self.realm.date_at(h).is_some() => self.realm.date_at(h).unwrap(),
                _ => self.realm.to_number(value),
            };
            self.format_intl_datetime(handle, ms)
        } else {
            let n = self.realm.to_number(value);
            self.intl_format_number(handle, n)
        }
    }

    /// Builds an `Intl.RelativeTimeFormat` instance (`InitializeRelativeTimeFormat`):
    /// canonicalizes the requested locales, then reads and validates the options in
    /// spec order (`localeMatcher`, `numberingSystem`, `style`, `numeric`), and brands
    /// the object with `\0intl="rtf"` plus its resolved `\0locale`/`numberingSystem`/
    /// `style`/`numeric`. Used only by the `new`-form constructor; a plain call (no
    /// `new`) throws a `TypeError` before reaching here.
    pub(crate) fn make_relative_time_format(
        &mut self,
        args: &[NanBox],
    ) -> Result<NanBox, ExecError> {
        let obj = self.realm.new_object();
        self.init_relative_time_format(obj, args)?;
        Ok(NanBox::handle(obj.to_raw()))
    }

    pub(crate) fn init_relative_time_format(
        &mut self,
        obj: Handle,
        args: &[NanBox],
    ) -> Result<(), ExecError> {
        let marker = self.new_str("rtf");
        self.realm.set_hidden_property(obj, "\u{0}intl", marker);
        // 1. CanonicalizeLocaleList(locales) — a malformed tag raises a RangeError.
        let requested =
            self.canonicalize_locale_list(args.first().copied().unwrap_or(NanBox::undefined()))?;
        let locale = requested
            .into_iter()
            .next()
            .unwrap_or_else(|| String::from("en-US"));
        let locv = self.new_str(&locale);
        self.realm.set_hidden_property(obj, "\u{0}locale", locv);
        // `InitializeRelativeTimeFormat` coerces options with `ToObject` (not
        // GetOptionsObject): `undefined` → an empty options bag; `null` → a TypeError;
        // any other primitive (boolean/string/number/symbol) → its wrapper object
        // (which has no own option keys, so all defaults apply).
        let opts_arg = args.get(1).copied().unwrap_or(NanBox::undefined());
        let opts = match opts_arg.unpack() {
            Unpacked::Undefined => None,
            Unpacked::Null => {
                return Err(self.type_error("Intl.RelativeTimeFormat options cannot be null"));
            }
            _ => self
                .coerce_to_object(opts_arg)
                .as_handle()
                .map(Handle::from_raw),
        };
        // Options are read in spec order: localeMatcher, numberingSystem, style, numeric.
        let _ = self.get_string_option(
            opts,
            "localeMatcher",
            &["lookup", "best fit"],
            Some("best fit"),
        )?;
        let nu = self.get_string_option(opts, "numberingSystem", &[], None)?;
        if let Some(ns) = &nu {
            // numberingSystem must match the UTS-35 `type` production (3-8 alnum,
            // hyphen-joined) — otherwise a RangeError.
            if !is_unicode_type_value(ns) {
                let m = self.new_str("invalid numberingSystem");
                return Err(ExecError::Throw(self.make_error(N_RANGE_ERROR, Some(m))));
            }
        }
        let style = self
            .get_string_option(opts, "style", &["long", "short", "narrow"], Some("long"))?
            .unwrap();
        let numeric = self
            .get_string_option(opts, "numeric", &["always", "auto"], Some("always"))?
            .unwrap();
        // ResolveLocale for the `nu` key: a known `numberingSystem` option wins,
        // else the locale's `-u-nu-` extension, else the CLDR default. The
        // resolved locale keeps `-u-nu-` only when the value came from the
        // extension (an option-sourced value drops the key from the tag).
        let base = strip_unicode_extension(&locale);
        let (resolved_nu, add) = resolve_nu_key(&base, &locale, nu.as_deref());
        let resolved_locale = build_resolved_locale(&base, &[add]);
        let locv = self.new_str(&resolved_locale);
        self.realm.set_hidden_property(obj, "\u{0}locale", locv);
        self.store_str(obj, "numberingSystem", &Some(resolved_nu));
        self.store_str(obj, "style", &Some(style));
        self.store_str(obj, "numeric", &Some(numeric));
        self.brand_intl_instance(obj, N_INTL_REL_TIME);
        Ok(())
    }

    /// The resolved `(numeric, style)` of an `Intl.RelativeTimeFormat` instance
    /// (defaults `"always"`/`"long"` when the slots are absent).
    pub(crate) fn rel_time_numeric_style(&mut self, fmt: Option<Handle>) -> (String, String) {
        let numeric = fmt
            .and_then(|h| self.realm.get_property(h, "numeric"))
            .filter(|v| !matches!(v.unpack(), Unpacked::Undefined))
            .map(|v| self.realm.to_display_string(v))
            .unwrap_or_else(|| String::from("always"));
        let style = fmt
            .and_then(|h| self.realm.get_property(h, "style"))
            .filter(|v| !matches!(v.unpack(), Unpacked::Undefined))
            .map(|v| self.realm.to_display_string(v))
            .unwrap_or_else(|| String::from("long"));
        (numeric, style)
    }

    /// `SingularRelativeTimeUnit(unit)`: `ToString`s `unit` (a Symbol throws a
    /// TypeError), accepts both the singular and plural forms of the eight relative
    /// time units, returning the singular stem; any other value is a RangeError.
    pub(crate) fn singular_relative_time_unit(
        &mut self,
        unit: NanBox,
    ) -> Result<String, ExecError> {
        let s = self.coerce_to_string(unit)?;
        let singular = match s.as_str() {
            "seconds" | "second" => "second",
            "minutes" | "minute" => "minute",
            "hours" | "hour" => "hour",
            "days" | "day" => "day",
            "weeks" | "week" => "week",
            "months" | "month" => "month",
            "quarters" | "quarter" => "quarter",
            "years" | "year" => "year",
            _ => {
                let m = self.new_str(&alloc::format!("invalid relative time unit '{s}'"));
                return Err(ExecError::Throw(self.make_error(N_RANGE_ERROR, Some(m))));
            }
        };
        Ok(String::from(singular))
    }

    /// Builds an `Intl.DisplayNames` instance: an object capturing `type` with a readable
    /// `of(code)` method.
    pub(crate) fn make_display_names(&mut self, args: &[NanBox]) -> Result<NanBox, ExecError> {
        let obj = self.realm.new_object();
        // CanonicalizeLocaleList(locales) first (a Symbol/bad element is a
        // TypeError/RangeError); the resolved locale is the first requested tag.
        let requested =
            self.canonicalize_locale_list(args.first().copied().unwrap_or(NanBox::undefined()))?;
        let locale = requested
            .into_iter()
            .next()
            .unwrap_or_else(|| String::from("en"));
        let locv = self.new_str(&locale);
        self.realm.set_hidden_property(obj, "\u{0}locale", locv);
        // `options = ? GetOptionsObject(options)` — a primitive other than
        // `undefined` is a TypeError; `undefined` yields an (empty) options object.
        let opts = match args.get(1).copied() {
            Some(v) if !matches!(v.unpack(), Unpacked::Undefined) => {
                if !self.is_object_value(v) {
                    return Err(self.type_error("Intl.DisplayNames: options must be an object"));
                }
                v.as_handle().map(Handle::from_raw)
            }
            _ => None,
        };
        // `localeMatcher` is read (and validated) first, before `type` (spec order);
        // it is otherwise unused.
        let _ = self.get_string_option(
            opts,
            "localeMatcher",
            &["lookup", "best fit"],
            Some("best fit"),
        )?;
        // Spec option-evaluation order: `style` is read (and validated) *before*
        // `type` (sec-Intl.DisplayNames steps 12–14), so an invalid `style` is a
        // RangeError even when the required `type` is absent.
        let style =
            self.get_string_option(opts, "style", &["narrow", "short", "long"], Some("long"))?;
        // `type` is a **required** option (its absence — including when `options`
        // is undefined — is a TypeError); it must be one of the valid types.
        let type_s = self.get_string_option(
            opts,
            "type",
            &[
                "language",
                "region",
                "script",
                "currency",
                "calendar",
                "dateTimeField",
            ],
            None,
        )?;
        let Some(type_s) = type_s else {
            return Err(self.type_error("Intl.DisplayNames: the `type` option is required"));
        };
        let tv = self.new_str(&type_s);
        self.realm.set_hidden_property(obj, "type", tv);
        // Mark the service kind so `resolvedOptions` reports the DisplayNames shape.
        let kindv = self.new_str("display");
        self.realm.set_hidden_property(obj, "\u{0}intl", kindv);
        self.store_str(obj, "style", &style);
        // `fallback` (default "code") and — for a language type —
        // `languageDisplay` (default "dialect"), validated + stored.
        let fallback = self.get_string_option(opts, "fallback", &["code", "none"], Some("code"))?;
        self.store_str(obj, "fallback", &fallback);
        if type_s == "language" {
            let ld = self.get_string_option(
                opts,
                "languageDisplay",
                &["dialect", "standard"],
                Some("dialect"),
            )?;
            self.store_str(obj, "languageDisplay", &ld);
        }
        self.brand_intl_instance(obj, N_INTL_DISPLAY_NAMES);
        Ok(NanBox::handle(obj.to_raw()))
    }

    /// Builds an `Intl.Collator` instance: an object capturing the locale and
    /// `sensitivity`/`numeric` options with a readable `compare` function (usable directly and
    /// as `arr.sort(collator.compare)`).
    pub(crate) fn make_collator(&mut self, args: &[NanBox]) -> Result<NanBox, ExecError> {
        let obj = self.realm.new_object();
        self.init_collator(obj, args)?;
        Ok(NanBox::handle(obj.to_raw()))
    }

    /// `InitializeCollator`: canonicalizes the requested locales, then reads and
    /// validates the options in spec order — `usage`, `localeMatcher`,
    /// `collation`, `numeric`, `caseFirst`, `sensitivity`, `ignorePunctuation` —
    /// storing the resolved values behind hidden slots for `resolvedOptions`.
    pub(crate) fn init_collator(&mut self, obj: Handle, args: &[NanBox]) -> Result<(), ExecError> {
        let marker = self.new_str("collator");
        self.realm.set_hidden_property(obj, "\u{0}intl", marker);
        // 1. CanonicalizeLocaleList(locales) — a malformed tag raises a RangeError.
        let locale = self
            .canonicalize_locale_list(args.first().copied().unwrap_or(NanBox::undefined()))?
            .into_iter()
            .next()
            .unwrap_or_else(|| String::from("en"));
        let locv = self.new_str(&locale);
        self.realm.set_hidden_property(obj, "\u{0}locale", locv);
        // GetOptionsObject: `undefined` → no options; a non-object (other than
        // undefined, e.g. `null`) is a TypeError; otherwise the object itself.
        let opts_arg = args.get(1).copied().unwrap_or(NanBox::undefined());
        let opts = if matches!(opts_arg.unpack(), Unpacked::Undefined) {
            None
        } else if self.is_object_value(opts_arg) {
            opts_arg.as_handle().map(Handle::from_raw)
        } else {
            return Err(self.type_error("Intl.Collator options must be an object"));
        };
        // Read order (observable via throwing getters): usage, localeMatcher,
        // collation, numeric, caseFirst, sensitivity, ignorePunctuation.
        let usage = self
            .get_string_option(opts, "usage", &["sort", "search"], Some("sort"))?
            .unwrap();
        let _ = self.get_string_option(
            opts,
            "localeMatcher",
            &["lookup", "best fit"],
            Some("best fit"),
        )?;
        // `collation`: an unvalidated string that must match the `type` grammar
        // (alphanum{3,8} groups) — a malformed value is a RangeError.
        let collation = self.get_string_option(opts, "collation", &[], None)?;
        if let Some(c) = &collation
            && !c
                .split('-')
                .all(|p| (3..=8).contains(&p.len()) && p.bytes().all(|b| b.is_ascii_alphanumeric()))
        {
            let m = self.new_str(&alloc::format!("invalid collation option: {c}"));
            return Err(ExecError::Throw(self.make_error(N_RANGE_ERROR, Some(m))));
        }
        let numeric = self.get_bool_option(opts, "numeric", None)?;
        let case_first =
            self.get_string_option(opts, "caseFirst", &["upper", "lower", "false"], None)?;
        let sensitivity = self
            .get_string_option(
                opts,
                "sensitivity",
                &["base", "accent", "case", "variant"],
                None,
            )?
            .unwrap_or_else(|| String::from("variant"));
        // `ignorePunctuation` default is locale-dependent (CLDR): true for Thai,
        // false elsewhere.
        let primary = locale.split(['-', '_']).next().unwrap_or("");
        let ip_default = primary.eq_ignore_ascii_case("th");
        let ignore_punct = self
            .get_bool_option(opts, "ignorePunctuation", Some(ip_default))?
            .unwrap_or(ip_default);
        // --- ResolveLocale over Collator's relevant extension keys (co, kn, kf).
        // Each key's value comes from the locale's `-u-` extension when supported,
        // overridden by a supported option; an option-sourced value drops the key
        // from the resolved locale, while an extension-sourced value keeps it.
        // Non-relevant `-u-` keys and unsupported values are dropped entirely.
        let base = strip_unicode_extension(&locale);
        let mut additions: Vec<String> = Vec::new();

        // co (collation): default "default".
        let mut co_value = String::from("default");
        if let Some(ext) = locale_unicode_keyword(&locale, "co")
            && is_supported_collation(&ext)
        {
            co_value = ext.clone();
            additions.push(alloc::format!("-co-{ext}"));
        }
        if let Some(opt) = &collation
            && is_supported_collation(opt)
            && *opt != co_value
        {
            co_value = opt.clone();
            additions.retain(|a| !a.starts_with("-co-"));
        }

        // kn (numeric): default false; reported only when set from ext or option.
        let ext_kn = locale_unicode_bool_keyword(&locale, "kn");
        let mut kn_value = ext_kn.unwrap_or(false);
        if let Some(b) = ext_kn {
            additions.push(if b {
                String::from("-kn")
            } else {
                String::from("-kn-false")
            });
        }
        if let Some(opt) = numeric
            && opt != kn_value
        {
            kn_value = opt;
            additions.retain(|a| a != "-kn" && a != "-kn-false");
        }
        let kn_reported = ext_kn.is_some() || numeric.is_some();

        // kf (caseFirst): reported only when set from ext or option.
        let mut kf_value: Option<String> = None;
        if let Some(ext) = locale_unicode_keyword(&locale, "kf")
            && matches!(ext.as_str(), "upper" | "lower" | "false")
        {
            kf_value = Some(ext.clone());
            additions.push(alloc::format!("-kf-{ext}"));
        }
        if let Some(opt) = &case_first
            && kf_value.as_deref() != Some(opt.as_str())
        {
            kf_value = Some(opt.clone());
            additions.retain(|a| !a.starts_with("-kf-"));
        }

        let resolved_locale = build_resolved_locale(&base, &additions);
        let rlv = self.new_str(&resolved_locale);
        self.realm.set_hidden_property(obj, "\u{0}locale", rlv);

        // Store the resolved slots for resolvedOptions.
        self.store_str(obj, "usage", &Some(usage));
        self.store_str(obj, "sensitivity", &Some(sensitivity));
        let ipv = NanBox::boolean(ignore_punct);
        self.realm
            .set_hidden_property(obj, "ignorePunctuation", ipv);
        self.store_str(obj, "collation", &Some(co_value));
        if kn_reported {
            self.realm
                .set_hidden_property(obj, "numeric", NanBox::boolean(kn_value));
        }
        if let Some(cf) = kf_value {
            self.store_str(obj, "caseFirst", &Some(cf));
        }
        self.brand_intl_instance(obj, N_INTL_COLLATOR);
        Ok(())
    }

    /// Compares `a` and `b` under an `Intl.Collator` instance's resolved slots via
    /// real UCA collation: `sensitivity` → strength, `numeric` → numeric ordering,
    /// `ignorePunctuation` → variable-weighting (Shifted vs NonIgnorable). Shared by
    /// `Intl.Collator.prototype.compare` and `String.prototype.localeCompare`.
    #[cfg(feature = "intl")]
    pub(crate) fn collator_ordering(
        &mut self,
        ch: Option<Handle>,
        a: &str,
        b: &str,
    ) -> core::cmp::Ordering {
        use intl::unicode::collate::{AlternateHandling, Collator, Strength};
        let strength = match ch
            .and_then(|h| self.realm.get_property(h, "sensitivity"))
            .map(|v| self.realm.to_display_string(v))
            .as_deref()
        {
            Some("base") => Strength::Primary,
            Some("accent") => Strength::Secondary,
            _ => Strength::Tertiary,
        };
        let numeric = matches!(
            ch.and_then(|h| self.realm.get_property(h, "numeric"))
                .map(|v| v.unpack()),
            Some(Unpacked::Bool(true))
        );
        let alternate = if matches!(
            ch.and_then(|h| self.realm.get_property(h, "ignorePunctuation"))
                .map(|v| v.unpack()),
            Some(Unpacked::Bool(true))
        ) {
            AlternateHandling::Shifted
        } else {
            AlternateHandling::NonIgnorable
        };
        Collator::new(alternate)
            .with_strength(strength)
            .with_numeric(numeric)
            .compare(a, b)
    }

    /// Builds an `Intl.ListFormat` instance (`InitializeListFormat`): canonicalizes
    /// the requested locales, reads and validates the `localeMatcher`/`type`/`style`
    /// options (in that order), and brands the object with `\0intl="list"` plus its
    /// resolved `\0locale`/`type`/`style`. Used only by the `new`-form constructor;
    /// a plain call (no `new`) throws a `TypeError` before reaching here.
    pub(crate) fn make_list_format(&mut self, args: &[NanBox]) -> Result<NanBox, ExecError> {
        let obj = self.realm.new_object();
        self.init_list_format(obj, args)?;
        Ok(NanBox::handle(obj.to_raw()))
    }

    pub(crate) fn init_list_format(
        &mut self,
        obj: Handle,
        args: &[NanBox],
    ) -> Result<(), ExecError> {
        let marker = self.new_str("list");
        self.realm.set_hidden_property(obj, "\u{0}intl", marker);
        // 1. CanonicalizeLocaleList(locales) — a malformed tag raises a RangeError.
        let requested =
            self.canonicalize_locale_list(args.first().copied().unwrap_or(NanBox::undefined()))?;
        let locale = requested
            .into_iter()
            .next()
            .unwrap_or_else(|| String::from("en-US"));
        let locv = self.new_str(&locale);
        self.realm.set_hidden_property(obj, "\u{0}locale", locv);
        // GetOptionsObject: `undefined` → no options; a non-object (other than
        // undefined) is a TypeError; otherwise the object itself.
        let opts_arg = args.get(1).copied().unwrap_or(NanBox::undefined());
        let opts = if matches!(opts_arg.unpack(), Unpacked::Undefined) {
            None
        } else if self.is_object_value(opts_arg) {
            opts_arg.as_handle().map(Handle::from_raw)
        } else {
            return Err(self.type_error("Intl.ListFormat options must be an object"));
        };
        // Options are read in spec order: localeMatcher, then type, then style.
        let _ = self.get_string_option(
            opts,
            "localeMatcher",
            &["lookup", "best fit"],
            Some("best fit"),
        )?;
        let list_type = self
            .get_string_option(
                opts,
                "type",
                &["conjunction", "disjunction", "unit"],
                Some("conjunction"),
            )?
            .unwrap();
        let style = self
            .get_string_option(opts, "style", &["long", "short", "narrow"], Some("long"))?
            .unwrap();
        self.store_str(obj, "type", &Some(list_type));
        self.store_str(obj, "style", &Some(style));
        self.brand_intl_instance(obj, N_INTL_LIST_FORMAT);
        Ok(())
    }

    /// `StringListFromIterable(iterable)`: `undefined` → an empty list; otherwise
    /// iterate, requiring every yielded value to be a primitive String (else a
    /// `TypeError`, after closing the iterator). Used by `format`/`formatToParts`.
    pub(crate) fn string_list_from_iterable(
        &mut self,
        iterable: NanBox,
    ) -> Result<Vec<String>, ExecError> {
        if matches!(iterable.unpack(), Unpacked::Undefined) {
            return Ok(Vec::new());
        }
        // Open the iterator once via the user `[Symbol.iterator]` when present, so
        // a custom iterable's `next`/`return` observe the exact spec call sequence
        // (and a non-string element closes it mid-stream). Built-in iterables
        // (arrays/strings/Maps/Sets/generators) have no readable iterator method;
        // they are drained eagerly and then validated element-by-element.
        if let Some(ih) = self.for_of_get_iterator(iterable)? {
            let mut out = Vec::new();
            loop {
                let next_fn = self.read_member(ih, "next")?;
                let res = self.call_with_this(next_fn, NanBox::handle(ih.to_raw()), &[])?;
                let Some(rh) = res.as_handle().map(Handle::from_raw) else {
                    return Err(self.type_error("iterator result is not an object"));
                };
                let done = self.read_member(rh, "done")?;
                if self.realm.truthy(done) {
                    break;
                }
                let value = self.read_member(rh, "value")?;
                match value.as_handle().map(Handle::from_raw) {
                    Some(vh) if self.realm.type_of(vh) == Some("string") => {
                        out.push(self.realm.string_value(vh).unwrap_or_default());
                    }
                    _ => {
                        // Non-String element: close the iterator, then throw.
                        let _ = self.iterator_close(ih);
                        return Err(
                            self.type_error("Intl.ListFormat: list elements must all be strings")
                        );
                    }
                }
                if out.len() > GEN_CAP {
                    return Err(self.type_error("iterator did not terminate"));
                }
            }
            return Ok(out);
        }
        // Built-in iterable (or a non-iterable, which `iterate_values` rejects with
        // a TypeError): drain, then require each element to be a String.
        let elems = self.iterate_values(iterable)?;
        let mut out = Vec::with_capacity(elems.len());
        for e in elems {
            match e.as_handle().map(Handle::from_raw) {
                Some(vh) if self.realm.type_of(vh) == Some("string") => {
                    out.push(self.realm.string_value(vh).unwrap_or_default());
                }
                _ => {
                    return Err(
                        self.type_error("Intl.ListFormat: list elements must all be strings")
                    );
                }
            }
        }
        Ok(out)
    }

    /// The resolved `(type, style)` of an `Intl.ListFormat` instance (defaults
    /// `"conjunction"`/`"long"` when the slots are absent).
    pub(crate) fn list_format_type_style(&self, fmt: Option<Handle>) -> (String, String) {
        let get = |key: &str, dflt: &str| -> String {
            fmt.and_then(|h| self.realm.get_property(h, key))
                .filter(|v| !matches!(v.unpack(), Unpacked::Undefined))
                .map(|v| self.realm.to_display_string(v))
                .unwrap_or_else(|| String::from(dflt))
        };
        (get("type", "conjunction"), get("style", "long"))
    }

    /// The `(literal-or-element, value)` parts of an `Intl.ListFormat` `format`/
    /// `formatToParts` over `items`, for the given `type` and `style`. The English
    /// CLDR patterns are hardcoded (the `intl` 0.4 crate exposes only and/or
    /// connectors, not the per-`type`/`style` list patterns, nor `unit`); these
    /// match the en-US output the conformance tests require.
    pub(crate) fn list_format_parts(
        &self,
        items: &[String],
        list_type: &str,
        style: &str,
    ) -> Vec<(&'static str, String)> {
        let n = items.len();
        let mut parts: Vec<(&'static str, String)> = Vec::new();
        if n == 0 {
            return parts;
        }
        // English CLDR `listPattern`s, keyed by (type, style). `pair` is the
        // two-element connector; `middle` joins non-final elements; `end` is the
        // final connector for three-or-more lists. (Reference-locale data; the
        // `intl` crate exposes only conjunction/disjunction *long*, so short /
        // narrow / unit patterns are encoded here.)
        let (pair, middle, end): (&str, &str, &str) = match (list_type, style) {
            ("disjunction", _) => (" or ", ", ", ", or "),
            ("unit", "narrow") => (" ", " ", " "),
            ("unit", _) => (", ", ", ", ", "),
            (_, "short") => (" & ", ", ", ", & "), // conjunction short
            (_, "narrow") => (", ", ", ", ", "),   // conjunction narrow
            _ => (" and ", ", ", ", and "),        // conjunction long (default)
        };
        for (i, it) in items.iter().enumerate() {
            if i > 0 {
                let lit = if n == 2 {
                    pair
                } else if i == n - 1 {
                    end
                } else {
                    middle
                };
                parts.push(("literal", String::from(lit)));
            }
            parts.push(("element", it.clone()));
        }
        parts
    }

    /// Builds an `Intl.PluralRules` instance (`InitializePluralRules`): canonicalizes
    /// the locale list, reads `localeMatcher`/`type`/`notation`/`compactDisplay` and
    /// the shared `SetNumberFormatDigitOptions` slots (in spec read order), and stores
    /// the resolved configuration behind hidden keys for `select`/`selectRange`/
    /// `resolvedOptions`. A non-object `options` (other than `undefined`) is a
    /// TypeError; an invalid option value is a RangeError.
    pub(crate) fn make_plural_rules(&mut self, args: &[NanBox]) -> Result<NanBox, ExecError> {
        let obj = self.realm.new_object();
        self.init_plural_rules(obj, args)?;
        Ok(NanBox::handle(obj.to_raw()))
    }

    /// Initializes an existing object as an `Intl.PluralRules` (used by both the
    /// `new`-form constructor and `super()` in a subclass — see `apply_native_super`).
    pub(crate) fn init_plural_rules(
        &mut self,
        obj: Handle,
        args: &[NanBox],
    ) -> Result<(), ExecError> {
        // Mark the instance kind so `resolvedOptions` reports the PluralRules shape.
        let kindv = self.new_str("plural");
        self.realm.set_hidden_property(obj, "\u{0}intl", kindv);
        self.brand_intl_instance(obj, N_INTL_PLURAL_RULES);
        // CanonicalizeLocaleList(locales): a malformed tag is a RangeError; the
        // resolved locale is the first requested tag, else the default.
        let requested =
            self.canonicalize_locale_list(args.first().copied().unwrap_or(NanBox::undefined()))?;
        let locale = requested
            .into_iter()
            .next()
            .unwrap_or_else(|| String::from("en"));
        let locv = self.new_str(&locale);
        self.realm.set_hidden_property(obj, "\u{0}locale", locv);
        // GetOptionsObject: undefined → no options; a non-object → TypeError.
        let opts_arg = args.get(1).copied().unwrap_or(NanBox::undefined());
        let opts = if matches!(opts_arg.unpack(), Unpacked::Undefined) {
            None
        } else if self.is_object_value(opts_arg) {
            opts_arg.as_handle().map(Handle::from_raw)
        } else {
            return Err(self.type_error("Intl.PluralRules options must be an object"));
        };
        // localeMatcher (validated, not otherwise used).
        let _ = self.get_string_option(
            opts,
            "localeMatcher",
            &["lookup", "best fit"],
            Some("best fit"),
        )?;
        // type: "cardinal" (default) or "ordinal".
        let pr_type = self
            .get_string_option(opts, "type", &["cardinal", "ordinal"], Some("cardinal"))?
            .unwrap();
        self.store_str(obj, "type", &Some(pr_type));
        // notation: "standard" (default) | "compact" | "scientific" | "engineering".
        let notation = self
            .get_string_option(
                opts,
                "notation",
                &["standard", "compact", "scientific", "engineering"],
                Some("standard"),
            )?
            .unwrap();
        // compactDisplay is read regardless, but only stored when notation is compact.
        let compact_display =
            self.get_string_option(opts, "compactDisplay", &["short", "long"], Some("short"))?;
        if notation == "compact" {
            self.store_str(obj, "compactDisplay", &compact_display);
        }
        self.store_str(obj, "notation", &Some(notation));
        // SetNumberFormatDigitOptions (shared with Intl.NumberFormat).
        self.set_number_format_digit_options(obj, opts)?;
        Ok(())
    }

    /// The plural category name (`"zero"`/`"one"`/`"two"`/`"few"`/`"many"`/`"other"`)
    /// of `n` per the receiver `Intl.PluralRules` instance's `[[Locale]]` and
    /// `[[Type]]`. A non-finite `n` is always `"other"`. With the `intl` feature this
    /// uses the crate's CLDR cardinal/ordinal rules; otherwise it falls back to the
    /// English rule (`1` → `"one"`, else `"other"`). Shared by `select`/`selectRange`.
    pub(crate) fn plural_select_category(&mut self, n: f64) -> &'static str {
        if !n.is_finite() {
            return "other";
        }
        #[cfg(feature = "intl")]
        {
            let fmt = self.this_val.as_handle().map(Handle::from_raw);
            let locale = fmt
                .and_then(|h| self.realm.get_property(h, "\u{0}locale"))
                .map(|v| self.realm.to_display_string(v))
                .unwrap_or_else(|| String::from("en"));
            let ordinal = fmt
                .and_then(|h| self.realm.get_property(h, "type"))
                .map(|v| self.realm.to_display_string(v))
                .as_deref()
                == Some("ordinal");
            let notation = fmt
                .and_then(|h| self.realm.get_property(h, "notation"))
                .map(|v| self.realm.to_display_string(v))
                .unwrap_or_else(|| String::from("standard"));
            // For compact/scientific/engineering notation the plural operands carry
            // a non-zero compact-decimal exponent `c`/`e` (e.g. 1.5e6 in compact →
            // mantissa 1.5, exponent 6), which some locales' `many` rules depend on
            // (fr: `e ∉ 0..5 → many`). Encode it via the crate's `<mantissa>c<exp>`
            // operand syntax; standard notation uses the plain decimal.
            let ops = match plural_notation_operand_string(n, &notation) {
                Some(s) => intl::plural::PluralOperands::parse(&s)
                    .unwrap_or_else(|| intl::plural::PluralOperands::from_int(n as i64)),
                None if n == (n as i64) as f64 => intl::plural::PluralOperands::from_int(n as i64),
                None => intl::plural::PluralOperands::parse(&alloc::format!("{n}"))
                    .unwrap_or_else(|| intl::plural::PluralOperands::from_int(n as i64)),
            };
            let cat = if ordinal {
                intl::plural::ordinal_category(&locale, &ops)
            } else {
                intl::plural::plural_category(&locale, &ops)
            };
            use intl::plural::PluralCategory::*;
            match cat {
                Zero => "zero",
                One => "one",
                Two => "two",
                Few => "few",
                Many => "many",
                Other => "other",
            }
        }
        #[cfg(not(feature = "intl"))]
        {
            if n == 1.0 { "one" } else { "other" }
        }
    }

    /// The sorted `pluralCategories` list for an `Intl.PluralRules` instance's
    /// locale+type: the distinct categories its cardinal (or ordinal) rules can
    /// produce, in CLDR order (`zero`, `one`, `two`, `few`, `many`, `other`). With
    /// the `intl` feature this probes the crate's rules over a representative sample
    /// of operands; otherwise it returns the English cardinal set `["one","other"]`.
    pub(crate) fn plural_categories(&mut self, locale: &str, ordinal: bool) -> Vec<&'static str> {
        const ORDER: [&str; 6] = ["zero", "one", "two", "few", "many", "other"];
        #[cfg(feature = "intl")]
        {
            use intl::plural::PluralCategory::*;
            let name = |c: intl::plural::PluralCategory| -> &'static str {
                match c {
                    Zero => "zero",
                    One => "one",
                    Two => "two",
                    Few => "few",
                    Many => "many",
                    Other => "other",
                }
            };
            let mut seen: Vec<&'static str> = Vec::new();
            // Probe a representative sample of operands: integers 0..=200 capture the
            // mod-10/mod-100 rule structure, plus a couple of fractional and large
            // compact-notation values that trigger the `many` category in some locales.
            let mut push = |this: &mut Self, s: &str| {
                if let Some(ops) = intl::plural::PluralOperands::parse(s) {
                    let cat = if ordinal {
                        intl::plural::ordinal_category(locale, &ops)
                    } else {
                        intl::plural::plural_category(locale, &ops)
                    };
                    let _ = this;
                    let nm = name(cat);
                    if !seen.contains(&nm) {
                        seen.push(nm);
                    }
                }
            };
            for i in 0..=200u32 {
                let s = alloc::format!("{i}");
                push(self, &s);
            }
            for s in ["0.0", "0.1", "1.5", "2.5", "1000000", "1000000.0"] {
                push(self, s);
            }
            // Return in canonical CLDR order.
            ORDER.iter().copied().filter(|c| seen.contains(c)).collect()
        }
        #[cfg(not(feature = "intl"))]
        {
            let _ = (locale, ordinal);
            let _ = ORDER;
            alloc::vec!["one", "other"]
        }
    }

    /// Builds an `Intl.Segmenter` instance: an object capturing `granularity` with a readable
    /// `segment(input)` method.
    pub(crate) fn make_segmenter(&mut self, args: &[NanBox]) -> Result<NanBox, ExecError> {
        let obj = self.realm.new_object();
        self.init_segmenter(obj, args)?;
        Ok(NanBox::handle(obj.to_raw()))
    }

    pub(crate) fn init_segmenter(&mut self, obj: Handle, args: &[NanBox]) -> Result<(), ExecError> {
        // 1. CanonicalizeLocaleList(locales) — a malformed tag / non-string,
        //    non-object element raises a RangeError/TypeError.
        let locale = self
            .canonicalize_locale_list(args.first().copied().unwrap_or(NanBox::undefined()))?
            .into_iter()
            .next()
            .unwrap_or_else(|| String::from("en"));
        let locv = self.new_str(&locale);
        self.realm.set_hidden_property(obj, "\u{0}locale", locv);
        // GetOptionsObject: `undefined` → no options; a non-object (other than
        // undefined, e.g. `null`) is a TypeError; otherwise the object itself.
        let opts_arg = args.get(1).copied().unwrap_or(NanBox::undefined());
        let opts = if matches!(opts_arg.unpack(), Unpacked::Undefined) {
            None
        } else if self.is_object_value(opts_arg) {
            opts_arg.as_handle().map(Handle::from_raw)
        } else {
            return Err(self.type_error("Intl.Segmenter options must be an object"));
        };
        // Options are read in spec order: localeMatcher, then granularity.
        let _ = self.get_string_option(
            opts,
            "localeMatcher",
            &["lookup", "best fit"],
            Some("best fit"),
        )?;
        // `granularity` (default "grapheme"), validated + stored so
        // `resolvedOptions` reports it (an invalid value is a RangeError).
        let gran = self
            .get_string_option(
                opts,
                "granularity",
                &["grapheme", "word", "sentence"],
                Some("grapheme"),
            )?
            .unwrap();
        let granv = self.new_str(&gran);
        self.realm.set_hidden_property(obj, "granularity", granv);
        // Mark the service kind so `resolvedOptions` reports the Segmenter shape.
        let kindv = self.new_str("segmenter");
        self.realm.set_hidden_property(obj, "\u{0}intl", kindv);
        self.brand_intl_instance(obj, N_INTL_SEGMENTER);
        Ok(())
    }

    /// Breaks a UTC millisecond timestamp into typed `(type, value)` parts per an
    /// `Intl.DateTimeFormat` instance's options, via the `intl` crate (CLDR, locale-aware).
    /// Used by both `format` and `formatToParts`.
    #[cfg(feature = "intl")]
    pub(crate) fn datetime_parts(
        &mut self,
        handle: Handle,
        ms: f64,
    ) -> Vec<(&'static str, String)> {
        use intl::datetime;
        let (locale, dt, o) = self.dtf_locale_dt_opts(handle, ms);
        // Chinese/Dangi lunisolar calendars: the crate's `format_to_parts` renders
        // only proleptic Gregorian, so derive the `relatedYear`/`yearName` (+ numeric
        // month/day) parts these calendars require ourselves.
        if let Some(p) = self.lunisolar_parts(handle, &locale, &dt, &o) {
            return p;
        }
        match datetime::format_to_parts(&locale, &dt, &o) {
            Ok(parts) => dtf_pad_time_parts(parts),
            Err(_) => Vec::new(),
        }
    }

    /// Builds the `intl` crate `(locale, DateTime, DateTimeFormatOptions)` for
    /// `handle` at instant `ms` — the shared front half of [`datetime_parts`] and
    /// the `formatRange` renderers. The `DateTime` is the instant shifted into the
    /// instance's resolved time zone; the options mirror the stored ECMA-402 fields.
    #[cfg(feature = "intl")]
    fn dtf_locale_dt_opts(
        &mut self,
        handle: Handle,
        ms: f64,
    ) -> (
        String,
        intl::datetime::DateTime,
        intl::datetime::DateTimeFormatOptions,
    ) {
        use intl::datetime::{
            DateStyle, DateTime, DateTimeFormatOptions, HourCycle, MonthStyle, NameStyle,
            Numeric2Digit, TimeZoneNameStyle,
        };
        let opt = |this: &mut Self, k: &str| -> Option<String> {
            this.realm
                .get_property(handle, k)
                .filter(|v| !matches!(v.unpack(), Unpacked::Undefined))
                .map(|v| this.realm.to_display_string(v))
        };
        let locale = opt(self, "\u{0}locale").unwrap_or_else(|| String::from("en"));
        // A `Date`/number is an exact instant; shift its wall clock into the
        // instance's resolved time zone (offset at that instant, DST-aware for named
        // zones) before decomposing. `zone_off_ms` also feeds `timeZoneName`.
        let zone_off_ms = self.dtf_zone_offset_ms(handle, ms as i64);
        let msi = ms as i64 + zone_off_ms;
        let day = msi.div_euclid(86_400_000);
        let tod = msi.rem_euclid(86_400_000);
        let (y, mo, d) = crate::realm::civil_from_days(day);
        let dt = DateTime {
            year: y as i32,
            month: mo as u8,
            day: d as u8,
            hour: (tod / 3_600_000) as u8,
            minute: ((tod / 60_000) % 60) as u8,
            second: ((tod / 1_000) % 60) as u8,
            millisecond: (tod % 1_000) as u16,
        };
        let name = |s: &str| match s {
            "long" => Some(NameStyle::Long),
            "short" => Some(NameStyle::Short),
            "narrow" => Some(NameStyle::Narrow),
            _ => None,
        };
        let n2 = |s: &str| match s {
            "numeric" => Some(Numeric2Digit::Numeric),
            "2-digit" => Some(Numeric2Digit::TwoDigit),
            _ => None,
        };
        let dstyle = |s: &str| match s {
            "full" => Some(DateStyle::Full),
            "long" => Some(DateStyle::Long),
            "medium" => Some(DateStyle::Medium),
            "short" => Some(DateStyle::Short),
            _ => None,
        };
        let mut o = DateTimeFormatOptions::default();
        // `dateStyle`/`timeStyle` are mutually exclusive with component fields (the crate
        // errors if both are set), matching ECMA-402.
        if opt(self, "dateStyle").is_some() || opt(self, "timeStyle").is_some() {
            o.date_style = opt(self, "dateStyle").as_deref().and_then(dstyle);
            o.time_style = opt(self, "timeStyle").as_deref().and_then(dstyle);
        } else {
            o.weekday = opt(self, "weekday").as_deref().and_then(name);
            o.era = opt(self, "era").as_deref().and_then(name);
            o.year = opt(self, "year").as_deref().and_then(n2);
            o.month = opt(self, "month").as_deref().and_then(|s| match s {
                "numeric" => Some(MonthStyle::Numeric),
                "2-digit" => Some(MonthStyle::TwoDigit),
                "long" => Some(MonthStyle::Long),
                "short" => Some(MonthStyle::Short),
                "narrow" => Some(MonthStyle::Narrow),
                _ => None,
            });
            o.day = opt(self, "day").as_deref().and_then(n2);
            o.hour = opt(self, "hour").as_deref().and_then(n2);
            o.minute = opt(self, "minute").as_deref().and_then(n2);
            o.second = opt(self, "second").as_deref().and_then(n2);
            o.day_period = opt(self, "dayPeriod").as_deref().and_then(name);
            o.fractional_second_digits =
                opt(self, "fractionalSecondDigits").and_then(|s| s.parse().ok());
            // ECMA-402 `ToDateTimeOptions` default: when *none* of the date/time
            // components (weekday/year/month/day/dayPeriod/hour/minute/second/
            // fractionalSecondDigits) is requested, fall back to a numeric date.
            // `era` and `timeZoneName` do NOT count (an era-only or timeZoneName-only
            // formatter still gets the default date), and a dayPeriod-only or
            // fractionalSecond-only formatter must NOT (it renders just that field).
            if o.weekday.is_none()
                && o.year.is_none()
                && o.month.is_none()
                && o.day.is_none()
                && o.day_period.is_none()
                && o.hour.is_none()
                && o.minute.is_none()
                && o.second.is_none()
                && o.fractional_second_digits.is_none()
            {
                o.year = Some(Numeric2Digit::Numeric);
                o.month = Some(MonthStyle::Numeric);
                o.day = Some(Numeric2Digit::Numeric);
            }
        }
        o.hour12 = self
            .realm
            .get_property(handle, "hour12")
            .and_then(|v| match v.unpack() {
                Unpacked::Bool(b) => Some(b),
                _ => None,
            });
        o.hour_cycle = opt(self, "hourCycle").as_deref().and_then(|s| match s {
            "h11" => Some(HourCycle::H11),
            "h12" => Some(HourCycle::H12),
            "h23" => Some(HourCycle::H23),
            "h24" => Some(HourCycle::H24),
            _ => None,
        });
        if let Some(tzn) = opt(self, "timeZoneName") {
            o.time_zone_name = match tzn.as_str() {
                "long" => Some(TimeZoneNameStyle::Long),
                "short" => Some(TimeZoneNameStyle::Short),
                "shortOffset" => Some(TimeZoneNameStyle::ShortOffset),
                "longOffset" => Some(TimeZoneNameStyle::LongOffset),
                "shortGeneric" => Some(TimeZoneNameStyle::ShortGeneric),
                "longGeneric" => Some(TimeZoneNameStyle::LongGeneric),
                _ => None,
            };
            o.tz_offset_minutes = Some((zone_off_ms / 60_000) as i32);
        }
        (locale, dt, o)
    }

    /// Chinese/Dangi lunisolar `formatToParts` output for `handle` (returns `None`
    /// unless the resolved calendar is `chinese`/`dangi` and the Gregorian date is
    /// within the crate's supported lunisolar range). Produces `relatedYear` (the
    /// Gregorian year the lunisolar year began), `yearName` (the sexagenary cyclic
    /// name), and numeric `month`/`day` parts; the localized lunisolar month names
    /// are not reachable through the `intl` crate's public API, so months render
    /// numerically. Any requested time components are appended via the crate's
    /// (calendar-independent) time formatter.
    #[cfg(feature = "intl")]
    fn lunisolar_parts(
        &self,
        handle: Handle,
        locale: &str,
        dt: &intl::datetime::DateTime,
        o: &intl::datetime::DateTimeFormatOptions,
    ) -> Option<Vec<(&'static str, String)>> {
        use intl::calendar;
        use intl::datetime::{self, DateTimeFormatOptions, MonthStyle, Numeric2Digit};
        let cal = self.dtf_resolved_calendar(handle);
        let (y, m, d) = (dt.year as i64, dt.month as i64, dt.day as i64);
        let (cyear, cmonth, cday, _leap) = match cal.as_str() {
            "chinese" => calendar::gregorian_to_chinese(y, m, d)?,
            "dangi" => calendar::gregorian_to_dangi(y, m, d)?,
            _ => return None,
        };
        // The related Gregorian year (the year the lunisolar year began) and the
        // 1-based sexagenary index, exactly as the crate's `format_chinese_date`
        // derives them.
        let related = if cal == "dangi" {
            calendar::dangi_to_gregorian(cyear, 1, 1, false)
        } else {
            calendar::chinese_to_gregorian(cyear, 1, 1, false)
        }
        .map_or(cyear, |g| g.0);
        let cyclic1 = (related - 4).rem_euclid(60) + 1;
        // zh renders the year/month/day with trailing `年`/`月`/`日` field markers;
        // other locales separate the numeric fields with plain literals.
        let zh = locale.starts_with("zh");
        let two = |v: i64| alloc::format!("{v:02}");
        let mut parts: Vec<(&'static str, String)> = Vec::new();
        if o.year.is_some() {
            parts.push(("relatedYear", alloc::format!("{related}")));
            parts.push(("yearName", sexagenary_year_name(cyclic1)));
            if zh {
                parts.push(("literal", String::from("")));
            }
        }
        if let Some(mstyle) = o.month {
            if !zh && !parts.is_empty() {
                parts.push(("literal", String::from(", ")));
            }
            parts.push((
                "month",
                match mstyle {
                    MonthStyle::TwoDigit => two(cmonth),
                    _ => alloc::format!("{cmonth}"),
                },
            ));
            if zh {
                parts.push(("literal", String::from("")));
            }
        }
        if let Some(dstyle) = o.day {
            if !zh && !parts.is_empty() {
                parts.push(("literal", String::from(" ")));
            }
            parts.push((
                "day",
                match dstyle {
                    Numeric2Digit::TwoDigit => two(cday),
                    _ => alloc::format!("{cday}"),
                },
            ));
            if zh {
                parts.push(("literal", String::from("")));
            }
        }
        // Time-of-day is calendar-independent: reuse the crate's time formatter for
        // any requested hour/minute/second/dayPeriod/fractionalSecond components.
        let has_time = o.hour.is_some()
            || o.minute.is_some()
            || o.second.is_some()
            || o.day_period.is_some()
            || o.fractional_second_digits.is_some();
        if has_time {
            let mut to = DateTimeFormatOptions::default();
            to.hour = o.hour;
            to.minute = o.minute;
            to.second = o.second;
            to.day_period = o.day_period;
            to.fractional_second_digits = o.fractional_second_digits;
            to.hour12 = o.hour12;
            to.hour_cycle = o.hour_cycle;
            to.time_zone_name = o.time_zone_name;
            to.tz_offset_minutes = o.tz_offset_minutes;
            if let Ok(tp) = datetime::format_to_parts(locale, dt, &to) {
                if !parts.is_empty() {
                    parts.push(("literal", String::from(", ")));
                }
                parts.extend(dtf_pad_time_parts(tp));
            }
        }
        Some(parts)
    }

    /// ECMA-402 `HandleDateTimeValue`: if `value` is a Temporal object, resolve it
    /// (WITHOUT calling `valueOf`) against `handle`'s `Intl.DateTimeFormat` options
    /// into the `(epoch-milliseconds, kind)` needed for formatting. A
    /// `Temporal.ZonedDateTime` throws a `TypeError`; a calendar mismatch throws a
    /// `RangeError`. Returns `Ok(None)` when `value` is not a Temporal object (the
    /// caller then falls back to the `ToNumber` / `Date` path).
    #[cfg(feature = "intl")]
    pub(crate) fn temporal_dtf_value(
        &mut self,
        handle: Handle,
        value: NanBox,
        zoned_ok: bool,
    ) -> Result<Option<(f64, crate::temporal_iso::TemporalKind)>, ExecError> {
        use crate::temporal_iso::{TemporalKind, iso_to_epoch_days};
        let Some(h) = value.as_handle().map(Handle::from_raw) else {
            return Ok(None);
        };
        let Some(d) = self.realm.temporal_at(h) else {
            return Ok(None);
        };
        // Resolved DateTimeFormat calendar: the explicit `calendar` option, else the
        // locale's `-u-ca-` extension, else "gregory".
        let dtf_cal = self
            .realm
            .get_property(handle, "calendar")
            .filter(|v| !matches!(v.unpack(), Unpacked::Undefined))
            .map(|v| self.realm.to_display_string(v))
            .or_else(|| {
                self.realm
                    .get_property(handle, "\u{0}locale")
                    .filter(|v| !matches!(v.unpack(), Unpacked::Undefined))
                    .map(|v| self.realm.to_display_string(v))
                    .and_then(|loc| locale_unicode_calendar(&loc))
            })
            .unwrap_or_else(|| String::from("gregory"));
        let cal = d.calendar.clone();
        // Calendar-compatibility checks (per-type).
        let cal_ok_iso = cal == dtf_cal || cal == "iso8601";
        let cal_ok_exact = cal == dtf_cal;
        let noon = 43_200_000_i64;
        let tod = |t: &crate::temporal_iso::IsoTime| -> i64 {
            i64::from(t.hour) * 3_600_000
                + i64::from(t.minute) * 60_000
                + i64::from(t.second) * 1_000
                + i64::from(t.millisecond)
        };
        let ms = match d.kind {
            // `Intl.DateTimeFormat.prototype.format`/`formatToParts`/`formatRange`
            // reject a `ZonedDateTime` (its own `toLocaleString` — `zoned_ok` —
            // formats it via its instant + time zone instead).
            TemporalKind::ZonedDateTime if !zoned_ok => {
                return Err(self.type_error(
                    "Temporal.ZonedDateTime is not supported by Intl.DateTimeFormat.prototype.format; \
                     use toLocaleString() or explicit options",
                ));
            }
            TemporalKind::ZonedDateTime => {
                // Format the ZonedDateTime's instant in its time zone (UTC-only
                // engine → the instant's wall clock). Its calendar must match.
                if !cal_ok_iso {
                    let m = self.new_str(
                        "Temporal object calendar is incompatible with this Intl.DateTimeFormat",
                    );
                    return Err(ExecError::Throw(self.make_error(N_RANGE_ERROR, Some(m))));
                }
                (d.epoch_ns / 1_000_000) as f64
            }
            TemporalKind::Duration => return Ok(None),
            TemporalKind::Instant => (d.epoch_ns / 1_000_000) as f64,
            TemporalKind::PlainDate | TemporalKind::PlainDateTime => {
                if !cal_ok_iso {
                    let m = self.new_str(
                        "Temporal object calendar is incompatible with this Intl.DateTimeFormat",
                    );
                    return Err(ExecError::Throw(self.make_error(N_RANGE_ERROR, Some(m))));
                }
                let days = iso_to_epoch_days(d.date);
                if d.kind == TemporalKind::PlainDate {
                    (days * 86_400_000 + noon) as f64
                } else {
                    (days * 86_400_000 + tod(&d.time)) as f64
                }
            }
            TemporalKind::PlainYearMonth | TemporalKind::PlainMonthDay => {
                if !cal_ok_exact {
                    let m = self.new_str(
                        "Temporal object calendar is incompatible with this Intl.DateTimeFormat",
                    );
                    return Err(ExecError::Throw(self.make_error(N_RANGE_ERROR, Some(m))));
                }
                (iso_to_epoch_days(d.date) * 86_400_000 + noon) as f64
            }
            TemporalKind::PlainTime => tod(&d.time) as f64,
        };
        Ok(Some((ms, d.kind)))
    }

    /// ECMA-402 `GetDateTimeFormat` specialized for a Temporal `kind`: builds the
    /// effective `intl` crate options for formatting a Temporal object, restricting
    /// `handle`'s DateTimeFormat options to the type's data model (per the spec's
    /// `required`/`defaults`/`inherit` rules). Returns `Ok(None)` when the format is
    /// *null* — meaning the requested options don't overlap the type's data model —
    /// which the caller turns into a `TypeError`.
    #[cfg(feature = "intl")]
    pub(crate) fn temporal_plain_options(
        &mut self,
        handle: Handle,
        kind: crate::temporal_iso::TemporalKind,
    ) -> Option<intl::datetime::DateTimeFormatOptions> {
        use crate::temporal_iso::TemporalKind;
        use intl::datetime::{
            DateStyle, DateTimeFormatOptions, HourCycle, MonthStyle, NameStyle, Numeric2Digit,
            TimeZoneNameStyle,
        };
        let opt = |this: &Self, k: &str| -> Option<String> {
            this.realm
                .get_property(handle, k)
                .filter(|v| !matches!(v.unpack(), Unpacked::Undefined))
                .map(|v| this.realm.to_display_string(v))
        };
        let defaulted = self
            .realm
            .get_property(handle, "\u{0}dtf_default_date")
            .and_then(|v| v.as_boolean())
            .unwrap_or(false);
        // Raw component options (the auto-filled numeric date is treated as absent).
        let weekday = opt(self, "weekday");
        let era = opt(self, "era");
        let (year, month, day) = if defaulted {
            (None, None, None)
        } else {
            (opt(self, "year"), opt(self, "month"), opt(self, "day"))
        };
        let day_period = opt(self, "dayPeriod");
        let hour = opt(self, "hour");
        let minute = opt(self, "minute");
        let second = opt(self, "second");
        let frac = self
            .realm
            .get_property(handle, "fractionalSecondDigits")
            .filter(|v| !matches!(v.unpack(), Unpacked::Undefined))
            .map(|v| self.realm.to_number(v) as u8);
        let tz_name = opt(self, "timeZoneName");
        let hour_cycle_s = opt(self, "hourCycle");
        let hour12 = self
            .realm
            .get_property(handle, "hour12")
            .and_then(|v| v.as_boolean());
        let date_style = opt(self, "dateStyle");
        let time_style = opt(self, "timeStyle");
        let ds = date_style.is_some();
        let ts = time_style.is_some();
        // Presence of the nine format components (dateStyle/timeStyle expanded).
        let p_weekday = weekday.is_some() || (ds && date_style.as_deref() == Some("full"));
        let p_year = year.is_some() || ds;
        let p_month = month.is_some() || ds;
        let p_day = day.is_some() || ds;
        let p_hour = hour.is_some() || ts;
        let p_minute = minute.is_some() || ts;
        let p_second = second.is_some() || (ts && time_style.as_deref() != Some("short"));
        let p_day_period = day_period.is_some();
        let p_frac = frac.is_some();
        let any_present = p_weekday
            || p_year
            || p_month
            || p_day
            || p_hour
            || p_minute
            || p_second
            || p_day_period
            || p_frac;

        // Style helpers.
        let name = |s: &str| match s {
            "long" => Some(NameStyle::Long),
            "short" => Some(NameStyle::Short),
            "narrow" => Some(NameStyle::Narrow),
            _ => None,
        };
        let n2 = |s: &str| match s {
            "numeric" => Some(Numeric2Digit::Numeric),
            "2-digit" => Some(Numeric2Digit::TwoDigit),
            _ => None,
        };
        let mstyle = |s: &str| match s {
            "numeric" => Some(MonthStyle::Numeric),
            "2-digit" => Some(MonthStyle::TwoDigit),
            "long" => Some(MonthStyle::Long),
            "short" => Some(MonthStyle::Short),
            "narrow" => Some(MonthStyle::Narrow),
            _ => None,
        };
        let hc = |s: &str| match s {
            "h11" => Some(HourCycle::H11),
            "h12" => Some(HourCycle::H12),
            "h23" => Some(HourCycle::H23),
            "h24" => Some(HourCycle::H24),
            _ => None,
        };
        // Native dateStyle → crate DateStyle (kept as-is for date-capable types).
        let dstyle = |s: &str| match s {
            "full" => Some(DateStyle::Full),
            "long" => Some(DateStyle::Long),
            "medium" => Some(DateStyle::Medium),
            "short" => Some(DateStyle::Short),
            _ => None,
        };
        // timeStyle downgraded to drop the time-zone name (plain types have no zone):
        // "full"/"long" carry a zone name, so clamp them to "medium".
        let tstyle_no_tz = |s: &str| match s {
            "short" => Some(DateStyle::Short),
            _ => Some(DateStyle::Medium),
        };
        // dateStyle expanded to (year, month) styles for year-month / month-day.
        let ds_year = |s: &str| match s {
            "short" => Numeric2Digit::TwoDigit,
            _ => Numeric2Digit::Numeric,
        };
        let ds_month = |s: &str| match s {
            "full" | "long" => MonthStyle::Long,
            "medium" => MonthStyle::Short,
            _ => MonthStyle::Numeric,
        };

        let mut o = DateTimeFormatOptions::default();
        match kind {
            TemporalKind::PlainDate => {
                let need_defaults = !(p_weekday || p_year || p_month || p_day);
                if need_defaults {
                    if any_present {
                        return None;
                    }
                    o.era = era.as_deref().and_then(name);
                    o.year = Some(Numeric2Digit::Numeric);
                    o.month = Some(MonthStyle::Numeric);
                    o.day = Some(Numeric2Digit::Numeric);
                } else if ds {
                    o.date_style = date_style.as_deref().and_then(dstyle);
                } else {
                    o.weekday = weekday.as_deref().and_then(name);
                    o.era = era.as_deref().and_then(name);
                    o.year = year.as_deref().and_then(n2);
                    o.month = month.as_deref().and_then(mstyle);
                    o.day = day.as_deref().and_then(n2);
                }
            }
            TemporalKind::PlainYearMonth => {
                let need_defaults = !(p_year || p_month);
                if need_defaults {
                    if any_present {
                        return None;
                    }
                    o.era = era.as_deref().and_then(name);
                    o.year = Some(Numeric2Digit::Numeric);
                    o.month = Some(MonthStyle::Numeric);
                } else if ds {
                    let s = date_style.as_deref().unwrap_or("short");
                    o.year = Some(ds_year(s));
                    o.month = Some(ds_month(s));
                } else {
                    o.era = era.as_deref().and_then(name);
                    o.year = year.as_deref().and_then(n2);
                    o.month = month.as_deref().and_then(mstyle);
                }
            }
            TemporalKind::PlainMonthDay => {
                let need_defaults = !(p_month || p_day);
                if need_defaults {
                    if any_present {
                        return None;
                    }
                    o.month = Some(MonthStyle::Numeric);
                    o.day = Some(Numeric2Digit::Numeric);
                } else if ds {
                    let s = date_style.as_deref().unwrap_or("short");
                    o.month = Some(ds_month(s));
                    o.day = Some(Numeric2Digit::Numeric);
                } else {
                    o.month = month.as_deref().and_then(mstyle);
                    o.day = day.as_deref().and_then(n2);
                }
            }
            TemporalKind::PlainTime => {
                let need_defaults = !(p_day_period || p_hour || p_minute || p_second || p_frac);
                if need_defaults {
                    if any_present {
                        return None;
                    }
                    o.hour_cycle = hour_cycle_s.as_deref().and_then(hc);
                    o.hour12 = hour12;
                    o.hour = Some(Numeric2Digit::Numeric);
                    o.minute = Some(Numeric2Digit::Numeric);
                    o.second = Some(Numeric2Digit::Numeric);
                } else {
                    o.hour_cycle = hour_cycle_s.as_deref().and_then(hc);
                    o.hour12 = hour12;
                    if ts {
                        o.time_style = time_style.as_deref().and_then(tstyle_no_tz);
                    } else {
                        o.hour = hour.as_deref().and_then(n2);
                        o.minute = minute.as_deref().and_then(n2);
                        o.second = second.as_deref().and_then(n2);
                        o.day_period = day_period.as_deref().and_then(name);
                        o.fractional_second_digits = frac;
                    }
                }
            }
            TemporalKind::PlainDateTime => {
                // required = ~any~ → never null (needDefaults ⇒ !anyPresent).
                o.hour_cycle = hour_cycle_s.as_deref().and_then(hc);
                o.hour12 = hour12;
                if !any_present {
                    o.era = era.as_deref().and_then(name);
                    o.year = Some(Numeric2Digit::Numeric);
                    o.month = Some(MonthStyle::Numeric);
                    o.day = Some(Numeric2Digit::Numeric);
                    o.hour = Some(Numeric2Digit::Numeric);
                    o.minute = Some(Numeric2Digit::Numeric);
                    o.second = Some(Numeric2Digit::Numeric);
                } else {
                    if ds {
                        o.date_style = date_style.as_deref().and_then(dstyle);
                    } else {
                        o.weekday = weekday.as_deref().and_then(name);
                        o.era = era.as_deref().and_then(name);
                        o.year = year.as_deref().and_then(n2);
                        o.month = month.as_deref().and_then(mstyle);
                        o.day = day.as_deref().and_then(n2);
                    }
                    if ts {
                        o.time_style = time_style.as_deref().and_then(tstyle_no_tz);
                    } else {
                        o.hour = hour.as_deref().and_then(n2);
                        o.minute = minute.as_deref().and_then(n2);
                        o.second = second.as_deref().and_then(n2);
                        o.day_period = day_period.as_deref().and_then(name);
                        o.fractional_second_digits = frac;
                    }
                }
            }
            TemporalKind::Instant | TemporalKind::ZonedDateTime => {
                // required = ~any~, defaults = ~all~, inherit = ~all~: keep every
                // requested option (including timeZoneName) and, absent any component,
                // fall back to a full numeric date *and* time. An Instant / ZonedDateTime
                // has absolute time, so the DateTimeFormat's time zone applies (engine
                // is UTC-only). A ZonedDateTime additionally defaults `timeZoneName` to
                // "short" when no component options were requested.
                o.hour_cycle = hour_cycle_s.as_deref().and_then(hc);
                o.hour12 = hour12;
                if !any_present {
                    o.era = era.as_deref().and_then(name);
                    o.year = Some(Numeric2Digit::Numeric);
                    o.month = Some(MonthStyle::Numeric);
                    o.day = Some(Numeric2Digit::Numeric);
                    o.hour = Some(Numeric2Digit::Numeric);
                    o.minute = Some(Numeric2Digit::Numeric);
                    o.second = Some(Numeric2Digit::Numeric);
                    if kind == TemporalKind::ZonedDateTime && tz_name.is_none() {
                        o.time_zone_name = Some(TimeZoneNameStyle::Short);
                        o.tz_offset_minutes = Some(0); // engine is UTC-only
                    }
                } else {
                    if ds {
                        o.date_style = date_style.as_deref().and_then(dstyle);
                    } else {
                        o.weekday = weekday.as_deref().and_then(name);
                        o.era = era.as_deref().and_then(name);
                        o.year = year.as_deref().and_then(n2);
                        o.month = month.as_deref().and_then(mstyle);
                        o.day = day.as_deref().and_then(n2);
                    }
                    if ts {
                        // Instant keeps the native time style (time-zone name and all).
                        o.time_style = time_style.as_deref().and_then(dstyle);
                    } else {
                        o.hour = hour.as_deref().and_then(n2);
                        o.minute = minute.as_deref().and_then(n2);
                        o.second = second.as_deref().and_then(n2);
                        o.day_period = day_period.as_deref().and_then(name);
                        o.fractional_second_digits = frac;
                    }
                }
                if let Some(tzn) = tz_name.as_deref() {
                    o.time_zone_name = match tzn {
                        "long" => Some(TimeZoneNameStyle::Long),
                        "short" => Some(TimeZoneNameStyle::Short),
                        "shortOffset" => Some(TimeZoneNameStyle::ShortOffset),
                        "longOffset" => Some(TimeZoneNameStyle::LongOffset),
                        "shortGeneric" => Some(TimeZoneNameStyle::ShortGeneric),
                        "longGeneric" => Some(TimeZoneNameStyle::LongGeneric),
                        _ => None,
                    };
                    o.tz_offset_minutes = Some(0); // engine is UTC-only
                }
                return Some(o);
            }
            // ZonedDateTime / Duration never reach here.
            _ => return None,
        }
        // Plain temporal types never render a time-zone name.
        let _ = tz_name;
        Some(o)
    }

    /// Renders `ms` through explicit `intl` crate options into `{type,value}` parts
    /// (the Temporal branch of `format`/`formatToParts`).
    #[cfg(feature = "intl")]
    pub(crate) fn temporal_datetime_parts(
        &self,
        handle: Handle,
        ms: f64,
        o: &intl::datetime::DateTimeFormatOptions,
    ) -> Vec<(&'static str, String)> {
        use intl::datetime::{self, DateTime};
        let locale = self
            .realm
            .get_property(handle, "\u{0}locale")
            .filter(|v| !matches!(v.unpack(), Unpacked::Undefined))
            .map(|v| self.realm.to_display_string(v))
            .unwrap_or_else(|| String::from("en"));
        let msi = ms as i64;
        let day = msi.div_euclid(86_400_000);
        let tod = msi.rem_euclid(86_400_000);
        let (y, mo, d) = crate::realm::civil_from_days(day);
        let dt = DateTime {
            year: y as i32,
            month: mo as u8,
            day: d as u8,
            hour: (tod / 3_600_000) as u8,
            minute: ((tod / 60_000) % 60) as u8,
            second: ((tod / 1_000) % 60) as u8,
            millisecond: (tod % 1_000) as u16,
        };
        // Non-Gregorian calendar rendering (Islamic / Persian): the `intl` crate can
        // only format these via a coarse `DateStyle` (no field-level parts), so this
        // path is taken only when the resolved calendar is one the crate ships CLDR
        // month/era names for AND a `dateStyle` is in effect. Everything else (all
        // Gregorian/ISO output, and field-component options) falls through to the
        // Gregorian `format_to_parts` below.
        if let Some(alt) = self.alt_calendar_date_parts(handle, &locale, &dt, o) {
            return alt;
        }
        match datetime::format_to_parts(&locale, &dt, o) {
            Ok(parts) => dtf_pad_time_parts(parts),
            Err(_) => Vec::new(),
        }
    }

    /// Renders the date portion of a Temporal object whose resolved `Intl`
    /// calendar is one the `intl` crate ships localized names for (Islamic and
    /// Persian). Returns `None` (Gregorian fallthrough) unless the DateTimeFormat
    /// `handle`'s resolved calendar is Islamic/Persian *and* the effective options
    /// carry a `dateStyle` (the only shape the crate's coarse calendar API can
    /// satisfy). When taken, the localized date is emitted as a single `month`
    /// part, optionally followed by the Gregorian-rendered time. `dt` is the
    /// engine's proleptic-Gregorian pivot for the value (date + time-of-day).
    #[cfg(feature = "intl")]
    fn alt_calendar_date_parts(
        &self,
        handle: Handle,
        locale: &str,
        dt: &intl::datetime::DateTime,
        o: &intl::datetime::DateTimeFormatOptions,
    ) -> Option<Vec<(&'static str, String)>> {
        use crate::temporal_iso::IsoDate;
        use intl::datetime;
        // The crate only renders a whole `dateStyle`; per-field month/era options on
        // a non-Gregorian calendar aren't expressible through its public API.
        let date_style = o.date_style?;
        let cal = self.dtf_resolved_calendar(handle);
        // Which crate formatter (if any) covers this calendar. All Islamic variants
        // share the same localized month/era names; only the day arithmetic (which
        // the engine has already done) differs between them.
        let is_islamic = cal.starts_with("islamic");
        let is_persian = cal == "persian";
        if !is_islamic && !is_persian {
            return None;
        }
        let iso = IsoDate {
            year: dt.year,
            month: dt.month,
            day: dt.day,
        };
        let f = crate::nbexec::temporal_calendar::iso_to_fields(&cal, iso);
        let (yy, mm, dd) = (f.year, f.month, f.day);
        let date = if is_islamic {
            datetime::format_islamic_date(locale, yy, mm, dd, date_style)
        } else {
            datetime::format_persian_date(locale, yy, mm, dd, date_style)
        };
        // Optional time portion (dateStyle + timeStyle). The crate has no
        // calendar-specific time rendering, so the Gregorian time formatter is used
        // — time-of-day is calendar-independent.
        let mut parts: Vec<(&'static str, String)> = alloc::vec![("month", date)];
        if let Some(ts) = o.time_style {
            parts.push(("literal", String::from(", ")));
            parts.push(("hour", datetime::format_time(locale, dt, ts)));
        }
        Some(parts)
    }

    /// The resolved `Intl.DateTimeFormat` calendar for `handle`: the explicit
    /// `calendar` option, else the locale's `-u-ca-` extension, else `"gregory"`.
    #[cfg(feature = "intl")]
    fn dtf_resolved_calendar(&self, handle: Handle) -> String {
        self.realm
            .get_property(handle, "calendar")
            .filter(|v| !matches!(v.unpack(), Unpacked::Undefined))
            .map(|v| self.realm.to_display_string(v))
            .or_else(|| {
                self.realm
                    .get_property(handle, "\u{0}locale")
                    .filter(|v| !matches!(v.unpack(), Unpacked::Undefined))
                    .map(|v| self.realm.to_display_string(v))
                    .and_then(|loc| locale_unicode_calendar(&loc))
            })
            .unwrap_or_else(|| String::from("gregory"))
    }

    /// The Temporal branch of `Intl.DateTimeFormat.prototype.format(value)`:
    /// if `value` is a Temporal object, formats it per the ECMA-402 protocol and
    /// returns `Ok(Some(string))`; otherwise `Ok(None)` (caller uses the number path).
    #[cfg(feature = "intl")]
    pub(crate) fn temporal_format_flat(
        &mut self,
        handle: Handle,
        value: NanBox,
        zoned_ok: bool,
    ) -> Result<Option<String>, ExecError> {
        let Some((ms, kind)) = self.temporal_dtf_value(handle, value, zoned_ok)? else {
            return Ok(None);
        };
        let Some(mut o) = self.temporal_plain_options(handle, kind) else {
            return Err(self.type_error(
                "the requested Intl.DateTimeFormat options are not compatible with this Temporal type",
            ));
        };
        let ms = self.dtf_apply_temporal_zone(handle, ms, kind, &mut o);
        let mut s = String::new();
        for (_, v) in self.temporal_datetime_parts(handle, ms, &o) {
            s.push_str(&v);
        }
        Ok(Some(self.apply_numbering_digits(handle, s)))
    }

    /// The Temporal branch of `formatToParts` — `Ok(Some(parts))` or `Ok(None)`.
    #[cfg(feature = "intl")]
    pub(crate) fn temporal_format_parts(
        &mut self,
        handle: Handle,
        value: NanBox,
        zoned_ok: bool,
    ) -> Result<Option<Vec<(&'static str, String)>>, ExecError> {
        let Some((ms, kind)) = self.temporal_dtf_value(handle, value, zoned_ok)? else {
            return Ok(None);
        };
        let Some(mut o) = self.temporal_plain_options(handle, kind) else {
            return Err(self.type_error(
                "the requested Intl.DateTimeFormat options are not compatible with this Temporal type",
            ));
        };
        let ms = self.dtf_apply_temporal_zone(handle, ms, kind, &mut o);
        Ok(Some(self.temporal_datetime_parts(handle, ms, &o)))
    }

    /// `Temporal.<Type>.prototype.toLocaleString(locales, options)`: constructs an
    /// `Intl.DateTimeFormat` from the arguments and formats the Temporal receiver
    /// through the ECMA-402 Temporal protocol (identical to `dtf.format(this)`).
    /// Used for the calendared/plain types (Duration and ZonedDateTime keep their
    /// own `toString`-based behavior).
    #[cfg(feature = "intl")]
    pub(crate) fn temporal_to_locale_string(
        &mut self,
        this: NanBox,
        args: &[NanBox],
    ) -> Result<NanBox, ExecError> {
        let locales = args.first().copied().unwrap_or(NanBox::undefined());
        let options = args.get(1).copied().unwrap_or(NanBox::undefined());
        let fmt_args = [locales, options];
        let inst = self.make_intl_formatter(N_INTL_DATETIME_FORMAT, &fmt_args)?;
        let Some(h) = inst.as_handle().map(Handle::from_raw) else {
            return Ok(self.new_str(""));
        };
        // `toLocaleString` accepts a `ZonedDateTime` (unlike `format`).
        let s = self
            .temporal_format_flat(h, this, true)?
            .unwrap_or_default();
        Ok(self.new_str(&s))
    }

    /// A range-endpoint "kind" tag: `0` for a Date/number, and a distinct value per
    /// Temporal type. Two `formatRange` endpoints must share the same tag.
    #[cfg(feature = "intl")]
    fn range_type_tag(&self, value: NanBox) -> u8 {
        use crate::temporal_iso::TemporalKind;
        if let Some(h) = value.as_handle().map(Handle::from_raw)
            && let Some(d) = self.realm.temporal_at(h)
        {
            return match d.kind {
                TemporalKind::PlainDate => 1,
                TemporalKind::PlainTime => 2,
                TemporalKind::PlainDateTime => 3,
                TemporalKind::Duration => 4,
                TemporalKind::Instant => 5,
                TemporalKind::PlainYearMonth => 6,
                TemporalKind::PlainMonthDay => 7,
                TemporalKind::ZonedDateTime => 8,
            };
        }
        0
    }

    /// Whether `value` carries a Temporal internal slot.
    #[cfg(feature = "intl")]
    fn is_temporal_value(&self, value: NanBox) -> bool {
        value
            .as_handle()
            .map(Handle::from_raw)
            .and_then(|h| self.realm.temporal_at(h))
            .is_some()
    }

    /// Hand-rolled en-US fallback for [`datetime_parts`](Self::datetime_parts) when the `intl`
    /// crate is unavailable: component options (`weekday`/`year`/`month`/`day`/`hour`/`minute`/
    /// `second`/`hour12`/`era`/`dateStyle`/`timeStyle`) with `literal` separators, UTC.
    #[cfg(not(feature = "intl"))]
    pub(crate) fn datetime_parts(
        &mut self,
        handle: Handle,
        ms: f64,
    ) -> Vec<(&'static str, String)> {
        let opt = |this: &mut Self, k: &str| -> Option<String> {
            this.realm
                .get_property(handle, k)
                .filter(|v| !matches!(v.unpack(), Unpacked::Undefined))
                .map(|v| this.realm.to_display_string(v))
        };
        let msi = ms as i64;
        let day = msi.div_euclid(86_400_000);
        let tod = msi.rem_euclid(86_400_000);
        let (y, mo, d) = crate::realm::civil_from_days(day);
        let (mo, d) = (i64::from(mo), i64::from(d));
        let wd_idx = (day + 4).rem_euclid(7) as usize; // 0 = Sunday
        let hour24 = tod / 3_600_000;
        let minute = (tod / 60_000) % 60;
        let second = (tod / 1_000) % 60;

        const MONTHS: [&str; 12] = [
            "January",
            "February",
            "March",
            "April",
            "May",
            "June",
            "July",
            "August",
            "September",
            "October",
            "November",
            "December",
        ];
        const WEEKDAYS: [&str; 7] = [
            "Sunday",
            "Monday",
            "Tuesday",
            "Wednesday",
            "Thursday",
            "Friday",
            "Saturday",
        ];
        let two = |v: i64| alloc::format!("{v:02}");
        let bare = |v: i64| alloc::format!("{v}");

        // Effective component options (after expanding dateStyle/timeStyle presets).
        let mut weekday = opt(self, "weekday");
        let mut year = opt(self, "year");
        let mut month = opt(self, "month");
        let mut day_o = opt(self, "day");
        let mut hour = opt(self, "hour");
        let mut minute_o = opt(self, "minute");
        let mut second_o = opt(self, "second");
        match opt(self, "dateStyle").as_deref() {
            Some("full") => {
                weekday = Some(String::from("long"));
                year = Some(String::from("numeric"));
                month = Some(String::from("long"));
                day_o = Some(String::from("numeric"));
            }
            Some("long") => {
                year = Some(String::from("numeric"));
                month = Some(String::from("long"));
                day_o = Some(String::from("numeric"));
            }
            Some("medium") => {
                year = Some(String::from("numeric"));
                month = Some(String::from("short"));
                day_o = Some(String::from("numeric"));
            }
            Some("short") => {
                year = Some(String::from("2-digit"));
                month = Some(String::from("numeric"));
                day_o = Some(String::from("numeric"));
            }
            _ => {}
        }
        match opt(self, "timeStyle").as_deref() {
            Some("full" | "long" | "medium") => {
                hour = Some(String::from("numeric"));
                minute_o = Some(String::from("2-digit"));
                second_o = Some(String::from("2-digit"));
            }
            Some("short") => {
                hour = Some(String::from("numeric"));
                minute_o = Some(String::from("2-digit"));
            }
            _ => {}
        }
        // With no options at all, the default is a numeric date.
        if weekday.is_none()
            && year.is_none()
            && month.is_none()
            && day_o.is_none()
            && hour.is_none()
            && minute_o.is_none()
            && second_o.is_none()
        {
            year = Some(String::from("numeric"));
            month = Some(String::from("numeric"));
            day_o = Some(String::from("numeric"));
        }

        let year_str = |style: &str| -> String {
            if style == "2-digit" {
                two(y.rem_euclid(100))
            } else {
                bare(y)
            }
        };
        let named_month = matches!(month.as_deref(), Some("long" | "short" | "narrow"));

        let lit = |s: &str| ("literal", String::from(s));

        // --- Date components (typed parts, en-US order) ---
        let mut date: Vec<(&'static str, String)> = Vec::new();
        if let Some(ws) = &weekday {
            let name = WEEKDAYS[wd_idx];
            date.push((
                "weekday",
                String::from(if ws == "long" { name } else { &name[..3] }),
            ));
        }
        if named_month {
            if !date.is_empty() {
                date.push(lit(", "));
            }
            if let Some(m) = &month {
                let name = MONTHS[(mo as usize).saturating_sub(1).min(11)];
                date.push((
                    "month",
                    String::from(if m == "long" { name } else { &name[..3] }),
                ));
            }
            if let Some(ds) = &day_o {
                date.push(lit(" "));
                date.push(("day", if ds == "2-digit" { two(d) } else { bare(d) }));
            }
            if let Some(ys) = &year {
                date.push(lit(if day_o.is_some() { ", " } else { " " }));
                date.push(("year", year_str(ys)));
            }
        } else {
            // A weekday-only request has no trailing separator; add ", " only before an
            // actual numeric date.
            if !date.is_empty() && (month.is_some() || day_o.is_some() || year.is_some()) {
                date.push(lit(", "));
            }
            let mut first = true;
            if let Some(m) = &month {
                date.push(("month", if m == "2-digit" { two(mo) } else { bare(mo) }));
                first = false;
            }
            if let Some(ds) = &day_o {
                if !first {
                    date.push(lit("/"));
                }
                date.push(("day", if ds == "2-digit" { two(d) } else { bare(d) }));
                first = false;
            }
            if let Some(ys) = &year {
                if !first {
                    date.push(lit("/"));
                }
                date.push(("year", year_str(ys)));
            }
        }
        if opt(self, "era").is_some() {
            if !date.is_empty() {
                date.push(lit(" "));
            }
            date.push(("era", String::from(if y > 0 { "AD" } else { "BC" })));
        }

        // --- Time components ---
        let mut time: Vec<(&'static str, String)> = Vec::new();
        if hour.is_some() || minute_o.is_some() || second_o.is_some() {
            // en-US defaults to 12-hour unless `hour12: false`.
            let h12 = !matches!(
                self.realm.get_property(handle, "hour12"),
                Some(v) if matches!(v.unpack(), Unpacked::Bool(false))
            );
            let h = if h12 {
                let m = hour24 % 12;
                if m == 0 { 12 } else { m }
            } else {
                hour24
            };
            time.push((
                "hour",
                if hour.as_deref() == Some("2-digit") {
                    two(h)
                } else {
                    bare(h)
                },
            ));
            if minute_o.is_some() {
                time.push(lit(":"));
                time.push(("minute", two(minute)));
            }
            if second_o.is_some() {
                time.push(lit(":"));
                time.push(("second", two(second)));
            }
            if h12 {
                // CLDR separates the time from AM/PM with U+202F (narrow no-break space).
                time.push(lit("\u{202f}"));
                time.push((
                    "dayPeriod",
                    String::from(if hour24 < 12 { "AM" } else { "PM" }),
                ));
            }
        }

        // --- Combine ---
        let mut parts = date;
        if !parts.is_empty() && !time.is_empty() {
            // CLDR's standard date-time connector is ", " (the crate uses it too).
            let _ = named_month;
            parts.push(lit(", "));
        }
        parts.extend(time);
        parts
    }

    /// `ToDateTimeOptions` for `Date.prototype.toLocale{,Date,Time}String`: builds
    /// a fresh options object from the user's `options`, adding the per-method
    /// component defaults (numeric year/month/day for a date-required call, and
    /// numeric hour/minute/second for a time-required call) when the user gave no
    /// matching component and no `dateStyle`/`timeStyle`.
    #[cfg(feature = "intl")]
    pub(crate) fn date_time_options(
        &mut self,
        user: NanBox,
        want_date: bool,
        want_time: bool,
    ) -> Result<Handle, ExecError> {
        let uh = user.as_handle().map(Handle::from_raw);
        let present = |this: &mut Self, keys: &[&str]| -> bool {
            uh.is_some_and(|h| {
                keys.iter().any(|k| {
                    this.realm
                        .get_property(h, k)
                        .is_some_and(|v| !matches!(v.unpack(), Unpacked::Undefined))
                })
            })
        };
        let has_date = present(self, &["weekday", "year", "month", "day"]);
        let has_time = present(
            self,
            &[
                "dayPeriod",
                "hour",
                "minute",
                "second",
                "fractionalSecondDigits",
            ],
        );
        let has_style = present(self, &["dateStyle", "timeStyle"]);
        let obj = self.realm.new_object();
        if let Some(h) = uh {
            for key in [
                "localeMatcher",
                "weekday",
                "era",
                "year",
                "month",
                "day",
                "dayPeriod",
                "hour",
                "minute",
                "second",
                "fractionalSecondDigits",
                "timeZoneName",
                "hour12",
                "hourCycle",
                "timeZone",
                "calendar",
                "numberingSystem",
                "dateStyle",
                "timeStyle",
                "formatMatcher",
            ] {
                if let Some(v) = self
                    .realm
                    .get_property(h, key)
                    .filter(|v| !matches!(v.unpack(), Unpacked::Undefined))
                {
                    self.realm.set_property(obj, key, v);
                }
            }
        }
        // ECMA-402 `ToDateTimeOptions` uses a *single* `needDefaults` flag over the
        // whole `required` set: any present component (date *or* time, per what the
        // method requires) suppresses *all* defaults. So `toLocaleString` with a lone
        // `{ year }` yields just the year — not year + a defaulted time.
        let need_defaults = !((want_date && has_date) || (want_time && has_time));
        if !has_style && need_defaults {
            let num = self.new_str("numeric");
            if want_date {
                for k in ["year", "month", "day"] {
                    self.realm.set_property(obj, k, num);
                }
            }
            if want_time {
                for k in ["hour", "minute", "second"] {
                    self.realm.set_property(obj, k, num);
                }
            }
        }
        Ok(obj)
    }

    /// The `Intl.DateTimeFormat` rendering of `ms` as a flat string (joins
    /// [`datetime_parts`](Self::datetime_parts)).
    pub(crate) fn format_intl_datetime(&mut self, handle: Handle, ms: f64) -> String {
        let mut s = String::new();
        for (_, v) in self.datetime_parts(handle, ms) {
            s.push_str(&v);
        }
        self.apply_numbering_digits(handle, s)
    }

    /// Rewrites the ASCII digits of `s` into the format object's resolved
    /// `numberingSystem` (see [`numbering_system_digit_base`]); non-digit code
    /// points (separators, letters) are untouched. Shared by NumberFormat and
    /// DateTimeFormat.
    pub(crate) fn apply_numbering_digits(&mut self, handle: Handle, s: String) -> String {
        let nu = self
            .realm
            .get_property(handle, "numberingSystem")
            .map(|v| self.realm.to_display_string(v))
            .unwrap_or_default();
        substitute_numbering_digits(&nu, s)
    }

    /// Builds the `intl` crate's `NumberFormatOptions` from an `Intl.NumberFormat` instance's
    /// stored JS options.
    #[cfg(feature = "intl")]
    pub(crate) fn number_format_options(
        &mut self,
        handle: Handle,
    ) -> intl::number::NumberFormatOptions {
        use intl::number::{
            CompactDisplay, CurrencyDisplay, Notation, NumberFormatOptions, NumberStyle,
            RoundingMode, SignDisplay, UnitDisplay, UseGrouping,
        };
        let opt_str = |this: &mut Self, k: &str| -> Option<String> {
            this.realm
                .get_property(handle, k)
                .filter(|v| !matches!(v.unpack(), Unpacked::Undefined))
                .map(|v| this.realm.to_display_string(v))
        };
        let opt_num = |this: &mut Self, k: &str| -> Option<u8> {
            this.realm
                .get_property(handle, k)
                .filter(|v| !matches!(v.unpack(), Unpacked::Undefined))
                .map(|v| this.realm.to_number(v) as u8)
        };
        // `NumberFormatOptions` is `#[non_exhaustive]` in intl 0.5, so it can't be
        // built with a struct literal — start from `default()` and set fields.
        let mut o = NumberFormatOptions::default();
        o.style = match opt_str(self, "style").as_deref() {
            Some("percent") => NumberStyle::Percent,
            Some("currency") => NumberStyle::Currency,
            Some("unit") => NumberStyle::Unit,
            _ => NumberStyle::Decimal,
        };
        o.notation = match opt_str(self, "notation").as_deref() {
            Some("scientific") => Notation::Scientific,
            Some("engineering") => Notation::Engineering,
            Some("compact") => Notation::Compact,
            _ => Notation::Standard,
        };
        o.compact_display = match opt_str(self, "compactDisplay").as_deref() {
            Some("long") => CompactDisplay::Long,
            _ => CompactDisplay::Short,
        };
        o.sign_display = match opt_str(self, "signDisplay").as_deref() {
            Some("always") => SignDisplay::Always,
            Some("exceptZero") => SignDisplay::ExceptZero,
            Some("negative") => SignDisplay::Negative,
            Some("never") => SignDisplay::Never,
            _ => SignDisplay::Auto,
        };
        o.currency_display = match opt_str(self, "currencyDisplay").as_deref() {
            Some("code") => CurrencyDisplay::Code,
            Some("name") => CurrencyDisplay::Name,
            Some("narrowSymbol") => CurrencyDisplay::NarrowSymbol,
            _ => CurrencyDisplay::Symbol,
        };
        o.unit_display = match opt_str(self, "unitDisplay").as_deref() {
            Some("long") => UnitDisplay::Long,
            Some("narrow") => UnitDisplay::Narrow,
            _ => UnitDisplay::Short,
        };
        // ECMA-402's default rounding is half-expand (1.25 → 1.3), not banker's rounding.
        o.rounding_mode = match opt_str(self, "roundingMode").as_deref() {
            Some("ceil") => RoundingMode::Ceil,
            Some("floor") => RoundingMode::Floor,
            Some("expand") => RoundingMode::Expand,
            Some("trunc") => RoundingMode::Trunc,
            Some("halfCeil") => RoundingMode::HalfCeil,
            Some("halfFloor") => RoundingMode::HalfFloor,
            Some("halfExpand") => RoundingMode::HalfExpand,
            Some("halfTrunc") => RoundingMode::HalfTrunc,
            Some("halfEven") => RoundingMode::HalfEven,
            _ => RoundingMode::HalfExpand,
        };
        // `useGrouping` is stored as a boolean (`false` → never, `true` → always) or
        // a string (`"min2"`/`"auto"`/`"always"`), per GetStringOrBooleanOption.
        match self
            .realm
            .get_property(handle, "useGrouping")
            .map(|v| v.unpack())
        {
            Some(Unpacked::Bool(false)) => o.use_grouping = UseGrouping::Never,
            Some(Unpacked::Bool(true)) => o.use_grouping = UseGrouping::Always,
            Some(_) => match opt_str(self, "useGrouping").as_deref() {
                Some("min2") => o.use_grouping = UseGrouping::Min2,
                Some("always") | Some("true") => o.use_grouping = UseGrouping::Always,
                Some("false") => o.use_grouping = UseGrouping::Never,
                // "auto" (and the absence of the option) keeps the locale default.
                _ => {}
            },
            None => {}
        }
        if let Some(mid) = opt_num(self, "minimumIntegerDigits") {
            o.minimum_integer_digits = mid;
        }
        o.minimum_fraction_digits = opt_num(self, "minimumFractionDigits");
        o.maximum_fraction_digits = opt_num(self, "maximumFractionDigits");
        o.minimum_significant_digits = opt_num(self, "minimumSignificantDigits");
        o.maximum_significant_digits = opt_num(self, "maximumSignificantDigits");
        // Scientific/engineering cap the mantissa at 3 fraction digits by default (1.235E5).
        if o.maximum_fraction_digits.is_none()
            && o.maximum_significant_digits.is_none()
            && matches!(o.notation, Notation::Scientific | Notation::Engineering)
        {
            o.maximum_fraction_digits = Some(3);
        }
        if let Some(c) = opt_str(self, "currency") {
            o.currency = Some(self.intern_static(&c));
        }
        if let Some(u) = opt_str(self, "unit") {
            o.unit = Some(self.intern_static(&u));
        }
        o
    }

    /// Whether an `Intl.NumberFormat` instance must use the hand-rolled path rather than the
    /// `intl` crate: `style: "unit"` isn't yet faithfully rendered by `intl::number::format`
    /// (narrow unit patterns are missing). `notation: "compact"` goes through the crate now,
    /// with its mantissa re-rounded by [`compact_reround_parts`] to the ECMA-402 default.
    #[cfg(feature = "intl")]
    pub(crate) fn number_uses_handrolled(&mut self, handle: Handle) -> bool {
        let get = |this: &mut Self, k: &str| -> Option<String> {
            this.realm
                .get_property(handle, k)
                .filter(|v| !matches!(v.unpack(), Unpacked::Undefined))
                .map(|v| this.realm.to_display_string(v))
        };
        get(self, "style").as_deref() == Some("unit")
    }

    /// Formats `n` per an `Intl.NumberFormat` instance. With the `intl` crate, all styles
    /// except those in [`number_uses_handrolled`](Self::number_uses_handrolled) go through
    /// `intl::number::format` (CLDR, locale-aware, full ECMA-402 options); the rest, and the
    /// no-`intl` build, use the hand-rolled en-US path below.
    pub(crate) fn intl_format_number(&mut self, handle: Handle, n: f64) -> String {
        // `roundingPriority` more/lessPrecision selects between the significant-
        // and fraction-digit results — an interplay the crate can't express.
        #[cfg(feature = "intl")]
        if let Some(s) = self.try_rounding_priority(handle, n) {
            return s;
        }
        let s = self.intl_format_number_inner(handle, n);
        self.apply_numbering_digits(handle, s)
    }

    /// Parse a string / BigInt `value` into an exact decimal
    /// `(negative, integer_digits, fraction_digits)` — but only when it carries
    /// more significant digits than an `f64` preserves (so the ordinary numeric
    /// path would silently round it). Returns `None` for values the crate's f64
    /// path renders exactly (≤ 15 significant digits) or that aren't a plain
    /// decimal literal (Infinity/NaN/exponent/etc.).
    #[cfg(feature = "intl")]
    fn extract_exact_decimal(&self, value: NanBox) -> Option<(bool, String, String)> {
        let h = value.as_handle().map(Handle::from_raw)?;
        let raw = if let Some(big) = self.realm.bigint_at(h) {
            alloc::format!("{big}")
        } else {
            self.realm.string_value(h)?
        };
        let s = raw.trim();
        let (neg, body) = match s.strip_prefix('-') {
            Some(r) => (true, r),
            None => (false, s.strip_prefix('+').unwrap_or(s)),
        };
        if body.is_empty() || body.matches('.').count() > 1 {
            return None;
        }
        if !body.bytes().all(|b| b.is_ascii_digit() || b == b'.') {
            return None;
        }
        let mut it = body.splitn(2, '.');
        let int_part = it.next().unwrap_or("");
        let frac_part = it.next().unwrap_or("");
        // Significant-digit span (first to last nonzero across int+frac).
        let all = alloc::format!("{int_part}{frac_part}");
        let first_nz = all.bytes().position(|b| b != b'0');
        let last_nz = all.bytes().rposition(|b| b != b'0');
        let sig = match (first_nz, last_nz) {
            (Some(a), Some(b)) => b - a + 1,
            _ => 0,
        };
        // A large-magnitude integer (its trailing zeros are significant) also
        // exceeds f64 integer precision beyond ~15 digits, even at low `sig`.
        let int_magnitude = int_part.trim_start_matches('0').len();
        if sig <= 15 && int_magnitude <= 15 {
            return None;
        }
        Some((neg, String::from(int_part), String::from(frac_part)))
    }

    /// Exact-decimal `Intl.NumberFormat.prototype.format` for a high-precision
    /// string / BigInt argument (ECMA-402 `ToIntlMathematicalValue` keeps the full
    /// value; the crate's `f64` path would round it). Handles only
    /// standard-notation plain-decimal formatting with fraction-digit options —
    /// everything else returns `None` to fall back to the numeric path.
    #[cfg(feature = "intl")]
    fn try_exact_decimal_format(&mut self, handle: Handle, value: NanBox) -> Option<String> {
        use intl::number::{Notation, NumberStyle, UseGrouping};
        let opts = self.number_format_options(handle);
        // Only standard-notation decimal style is shaped by `assemble_decimal`'s
        // probe; currency/percent/unit affixes and scientific/engineering/compact
        // mantissa shaping stay on the crate's f64 path.
        if opts.notation != Notation::Standard || !matches!(opts.style, NumberStyle::Decimal) {
            return None;
        }
        let increment = self
            .realm
            .get_property(handle, "roundingIncrement")
            .and_then(|v| v.as_number())
            .unwrap_or(1.0);
        if increment != 1.0 {
            // roundingIncrement rounds to a multiple of `increment` at the fraction
            // place; exact multiple-rounding on huge values isn't implemented here,
            // so leave it to the f64 path.
            return None;
        }
        let (neg, int_part, frac_part) = self.extract_exact_decimal(value)?;
        let min_int = (opts.minimum_integer_digits.max(1)) as usize;

        let (mut int_digits, frac_digits) = if opts.minimum_significant_digits.is_some()
            || opts.maximum_significant_digits.is_some()
        {
            // Significant-digit rounding (ECMA-402 roundingType `significantDigits`),
            // done exactly with a `puremp::Decimal`.
            exact_significant_digits(
                neg,
                &int_part,
                &frac_part,
                opts.minimum_significant_digits.map(|m| m as usize),
                opts.maximum_significant_digits.map(|m| m as usize),
                opts.rounding_mode,
            )?
        } else {
            let max_frac = opts.maximum_fraction_digits.unwrap_or(3) as usize;
            let min_frac = opts.minimum_fraction_digits.unwrap_or(0) as usize;
            let mut int_digits: alloc::vec::Vec<u8> = int_part.bytes().map(|b| b - b'0').collect();
            let mut frac_digits: alloc::vec::Vec<u8> =
                frac_part.bytes().map(|b| b - b'0').collect();
            // Round the fraction to maximumFractionDigits.
            if frac_digits.len() > max_frac {
                let up = exact_round_up(&frac_digits[max_frac..], opts.rounding_mode, neg);
                frac_digits.truncate(max_frac);
                if up {
                    exact_increment(&mut int_digits, &mut frac_digits);
                }
            }
            // Trim trailing fraction zeros down to minimumFractionDigits, then pad up.
            while frac_digits.len() > min_frac && frac_digits.last() == Some(&0) {
                frac_digits.pop();
            }
            while frac_digits.len() < min_frac {
                frac_digits.push(0);
            }
            (int_digits, frac_digits)
        };
        // Normalize the integer digits (strip leading zeros, pad to the minimum).
        while int_digits.len() > 1 && int_digits.first() == Some(&0) {
            int_digits.remove(0);
        }
        while int_digits.len() < min_int {
            int_digits.insert(0, 0);
        }
        let grouping = !matches!(opts.use_grouping, UseGrouping::Never);
        Some(self.assemble_decimal(handle, neg, &int_digits, &frac_digits, grouping))
    }

    /// Assemble a locale-scaffolded decimal string from latn `int`/`frac` digit
    /// vectors (probing the `intl` crate for the locale's affixes/separators),
    /// then substitute the resolved numbering-system digits. Shared by the
    /// exact-decimal and roundingPriority renderers.
    #[cfg(feature = "intl")]
    fn assemble_decimal(
        &mut self,
        handle: Handle,
        neg: bool,
        int: &[u8],
        frac: &[u8],
        grouping: bool,
    ) -> String {
        let int_str: String = int.iter().map(|d| (b'0' + d) as char).collect();
        let frac_str: String = frac.iter().map(|d| (b'0' + d) as char).collect();
        let probe = self.intl_format_number_inner(handle, if neg { -1.1 } else { 1.1 });
        let (prefix, dec_sep, suffix) = split_number_scaffold(&probe);
        let grouped = if grouping {
            let gp = self.intl_format_number_inner(handle, if neg { -1111.0 } else { 1111.0 });
            let group_sep = extract_group_sep(&gp);
            group_thousands_sep(&int_str, &group_sep)
        } else {
            int_str
        };
        let mut out = String::new();
        out.push_str(&prefix);
        out.push_str(&grouped);
        if !frac_str.is_empty() {
            out.push_str(&dec_sep);
            out.push_str(&frac_str);
        }
        out.push_str(&suffix);
        self.apply_numbering_digits(handle, out)
    }

    /// `roundingPriority: "morePrecision"` / `"lessPrecision"`: ECMA-402
    /// `FormatNumericToString` computes both `ToRawPrecision` (significant-digit)
    /// and `ToRawFixed` (fraction-digit) results and selects between them by their
    /// rounding magnitude. The `intl` crate can't express this interplay, so we
    /// render it here for standard-notation decimal formatting. Returns `None`
    /// (fall back to the crate) for any other configuration.
    #[cfg(feature = "intl")]
    fn try_rounding_priority(&mut self, handle: Handle, n: f64) -> Option<String> {
        use intl::number::{Notation, NumberStyle, UseGrouping};
        if !n.is_finite() || n == 0.0 {
            return None;
        }
        let priority = self
            .realm
            .get_property(handle, "roundingPriority")
            .map(|v| self.realm.to_display_string(v))
            .unwrap_or_default();
        if !matches!(priority.as_str(), "morePrecision" | "lessPrecision") {
            return None;
        }
        let opts = self.number_format_options(handle);
        if opts.notation != Notation::Standard || !matches!(opts.style, NumberStyle::Decimal) {
            return None;
        }
        let min_sig = opts.minimum_significant_digits.unwrap_or(1) as usize;
        let max_sig = opts.maximum_significant_digits.unwrap_or(21) as usize;
        let min_frac = opts.minimum_fraction_digits.unwrap_or(0) as usize;
        let max_frac = opts
            .maximum_fraction_digits
            .map(|m| m as usize)
            .unwrap_or_else(|| min_frac.max(3));

        // Shortest round-trip decimal of |n| (exponent form → fall back).
        let neg = n.is_sign_negative();
        let shortest = alloc::format!("{}", n.abs());
        if shortest.contains(['e', 'E', 'i', 'n']) {
            return None;
        }
        let mut it = shortest.splitn(2, '.');
        let int_part = it.next().unwrap_or("0");
        let frac_part = it.next().unwrap_or("");
        let int_v: alloc::vec::Vec<u8> = int_part.bytes().map(|b| b - b'0').collect();
        let frac_v: alloc::vec::Vec<u8> = frac_part.bytes().map(|b| b - b'0').collect();

        // sResult = ToRawPrecision; fResult = ToRawFixed.
        let (s_int, s_frac) = to_raw_precision(
            neg,
            int_v.clone(),
            frac_v.clone(),
            max_sig,
            opts.rounding_mode,
        );
        let (f_int, f_frac) = to_raw_fixed(neg, int_v, frac_v, max_frac, opts.rounding_mode);
        // ECMA-402 rounding magnitudes: ToRawPrecision rounds at
        // `e - maxSig + 1` (e = exponent of the most significant digit);
        // ToRawFixed rounds at `-maxFrac`. Lower (more negative) = more precise.
        let e = {
            let int_stripped = int_part.trim_start_matches('0');
            if !int_stripped.is_empty() {
                int_stripped.len() as i32 - 1
            } else {
                match frac_part.bytes().position(|b| b != b'0') {
                    Some(p) => -(p as i32) - 1,
                    None => 0,
                }
            }
        };
        let s_mag = e - max_sig as i32 + 1;
        let f_mag = -(max_frac as i32);

        let use_s = if priority == "morePrecision" {
            s_mag <= f_mag
        } else {
            s_mag > f_mag
        };

        // Build the selected result's digit vectors with its own minimum padding.
        let (int_d, frac_d) = if use_s {
            // ToRawPrecision: pad with trailing zeros up to minimumSignificantDigits.
            let (mut i, mut f) = (s_int, s_frac);
            let sig_now = {
                let first = i
                    .iter()
                    .chain(f.iter())
                    .position(|&d| d != 0)
                    .unwrap_or(i.len());
                (i.len() + f.len()).saturating_sub(first)
            };
            if sig_now < min_sig {
                f.resize(f.len() + (min_sig - sig_now), 0);
            }
            // Normalize leading zeros (keep at least one integer digit).
            while i.len() > 1 && i[0] == 0 {
                i.remove(0);
            }
            (i, f)
        } else {
            // ToRawFixed: trim trailing zeros down to minimumFractionDigits, then pad.
            let (i, mut f) = (f_int, f_frac);
            while f.len() > min_frac && f.last() == Some(&0) {
                f.pop();
            }
            if f.len() < min_frac {
                f.resize(min_frac, 0);
            }
            (i, f)
        };

        // minimumIntegerDigits padding.
        let min_int = opts.minimum_integer_digits.max(1) as usize;
        let mut int_d = int_d;
        while int_d.len() < min_int {
            int_d.insert(0, 0);
        }
        let grouping = !matches!(opts.use_grouping, UseGrouping::Never);
        Some(self.assemble_decimal(handle, neg, &int_d, &frac_d, grouping))
    }

    /// Pre-round `n` to the resolved precision using the ECMA-402 / ICU decimal
    /// rounding (see [`intl_decimal_round`]), returning a value the `intl` crate
    /// re-renders correctly. Applies to standard-notation decimal/currency only:
    /// percent (the crate scales by 100) and scientific/engineering/compact
    /// (mantissa rounding) are left untouched. Shared by `format`/`formatToParts`.
    #[cfg(feature = "intl")]
    fn number_precision_round(
        &mut self,
        handle: Handle,
        opts: &mut intl::number::NumberFormatOptions,
        n: f64,
    ) -> f64 {
        use intl::number::{Notation, NumberStyle};
        if !n.is_finite() || n == 0.0 || opts.notation != Notation::Standard {
            return n;
        }
        if matches!(opts.style, NumberStyle::Percent) {
            return n;
        }
        // `roundingPriority` other than the default `auto` combines the significant-
        // and fraction-digit results in a way the crate can't express; leave those
        // to the existing path rather than pre-rounding on the wrong precision.
        let priority = self
            .realm
            .get_property(handle, "roundingPriority")
            .map(|v| self.realm.to_display_string(v))
            .unwrap_or_default();
        if matches!(priority.as_str(), "morePrecision" | "lessPrecision")
            && opts.maximum_significant_digits.is_some()
            && opts.maximum_fraction_digits.is_some()
        {
            return n;
        }
        let increment = self
            .realm
            .get_property(handle, "roundingIncrement")
            .and_then(|v| v.as_number())
            .unwrap_or(1.0) as u32;
        let sig = opts.maximum_significant_digits.map(|s| s as usize);
        // Effective maximum fraction digits (the crate's pattern default when unset).
        let default_max = match opts.style {
            NumberStyle::Currency if opts.currency == Some("JPY") => 0,
            NumberStyle::Currency => 2,
            _ => 3,
        };
        let keep_frac = opts
            .maximum_fraction_digits
            .map(|m| m as usize)
            .unwrap_or(default_max);
        let rounded = intl_decimal_round(n, keep_frac, sig, increment, opts.rounding_mode);
        // We already applied the correct rounding to the shortest decimal; hand the
        // crate a mode that won't re-round the pre-rounded value's f64 noise (a
        // directional mode like `ceil`/`expand` would spuriously round `1.3` — really
        // `1.30000…0444` — up again). `halfExpand` leaves the clean value untouched.
        opts.rounding_mode = intl::number::RoundingMode::HalfExpand;
        rounded
    }

    fn intl_format_number_inner(&mut self, handle: Handle, n: f64) -> String {
        #[cfg(feature = "intl")]
        if !self.number_uses_handrolled(handle) {
            let locale = self
                .realm
                .get_property(handle, "\u{0}locale")
                .map(|v| self.realm.to_display_string(v))
                .unwrap_or_else(|| String::from("en"));
            let mut opts = self.number_format_options(handle);
            // Round the value's shortest round-trip decimal ourselves (ECMA-402 /
            // ICU rounding) and hand the crate a "clean" value — this fixes the
            // crate's binary-expansion rounding boundary (`1.15` → `1.2`, not `1.1`)
            // and applies `roundingIncrement` (which the crate has no option for).
            let n = self.number_precision_round(handle, &mut opts, n);
            // `trailingZeroDisplay: "stripIfInteger"`: an integer value drops its
            // forced trailing fraction (and significant) zeros. The intl crate has
            // no such option, so lower the minimum digit counts for this value.
            if n.is_finite() && n.fract() == 0.0 {
                let tzd = self
                    .realm
                    .get_property(handle, "trailingZeroDisplay")
                    .map(|v| self.realm.to_display_string(v))
                    .unwrap_or_default();
                if tzd == "stripIfInteger" {
                    opts.minimum_fraction_digits = Some(0);
                    if opts.minimum_significant_digits.is_some() {
                        opts.minimum_significant_digits = Some(1);
                    }
                }
            }
            // `intl` 0.5 panics formatting a true negative zero. Substitute the
            // smallest negative subnormal (which rounds to a displayed zero) so the
            // crate applies the locale's negative affix (e.g. ar's U+200E mark) and
            // the resolved `signDisplay` logic uniformly, instead of us hand-rolling
            // the sign and losing locale marks.
            let n = if n == 0.0 && n.is_sign_negative() {
                -f64::from_bits(1)
            } else {
                n
            };
            // Default compact notation: render via the tagged parts so the mantissa
            // can be re-rounded to the ECMA-402 default precision (the crate's plain
            // string path keeps a single fraction digit — `987654321` → `987.7M`
            // instead of `988M`). NaN/∞ carry no mantissa and fall through.
            if n.is_finite() && compact_wants_reround(&opts) {
                let mut o = opts;
                o.maximum_fraction_digits = Some(6);
                let mut parts: Vec<(&'static str, String)> =
                    intl::number::format_to_parts(&locale, n, &o)
                        .into_iter()
                        .map(|p| (p.kind.as_str(), p.value))
                        .collect();
                compact_reround_parts(&mut parts, opts.rounding_mode);
                // Return latn digits; the caller (`intl_format_number`) applies the
                // numbering system.
                return parts.into_iter().map(|(_, v)| v).collect();
            }
            let formatted = intl::number::format(&locale, n, &opts);
            // A small-magnitude negative that rounds to a displayed zero
            // (e.g. -0.0001 → "0", or -0.004 currency → "$0.00") carries no sign
            // under signDisplay "negative"/"never"/"exceptZero" — only "auto"/
            // "always" sign a zero. The intl crate keeps the input value's minus,
            // so strip it when every rendered digit is zero. (A literal -0 is
            // handled by the negative-zero branch above; -∞/NaN have no digits.)
            if formatted.starts_with('-') && n.is_finite() {
                let digits = formatted.bytes().filter(u8::is_ascii_digit);
                let mut any = false;
                let all_zero = digits.inspect(|_| any = true).all(|b| b == b'0');
                if any && all_zero {
                    let sd = self
                        .realm
                        .get_property(handle, "signDisplay")
                        .map(|v| self.realm.to_display_string(v))
                        .unwrap_or_default();
                    if matches!(sd.as_str(), "negative" | "never" | "exceptZero") {
                        return String::from(&formatted[1..]);
                    }
                }
            }
            // `signDisplay: "always"` signs a NaN as non-negative ("+NaN"); the
            // intl crate leaves NaN unsigned (it signs ±∞ but not NaN). Only
            // "always" signs NaN — "auto"/"never"/"exceptZero"/"negative" do not.
            if n.is_nan()
                && !formatted.starts_with(['+', '-'])
                && self
                    .realm
                    .get_property(handle, "signDisplay")
                    .map(|v| self.realm.to_display_string(v))
                    .as_deref()
                    == Some("always")
            {
                return alloc::format!("+{formatted}");
            }
            // `currencySign: "accounting"` renders a negative currency amount in the
            // locale's accounting pattern. That pattern is CLDR-locale data: most
            // locales inherit the root's parenthesized form (`($5.00)`), but some
            // (e.g. de-DE: `-987,00 $`) keep the minus. The intl crate has no
            // currencySign field, so approximate the parenthesizing locales here and
            // leave the minus-locales on the crate's output.
            if accounting_uses_parens(&locale)
                && formatted.starts_with('-')
                && self
                    .realm
                    .get_property(handle, "currencySign")
                    .map(|v| self.realm.to_display_string(v))
                    .as_deref()
                    == Some("accounting")
            {
                return alloc::format!("({})", &formatted[1..]);
            }
            return formatted;
        }
        let opt_str = |this: &mut Self, k: &str| -> Option<String> {
            this.realm
                .get_property(handle, k)
                .filter(|v| !matches!(v.unpack(), Unpacked::Undefined))
                .map(|v| this.realm.to_display_string(v))
        };
        let opt_num = |this: &mut Self, k: &str| -> Option<i32> {
            this.realm
                .get_property(handle, k)
                .filter(|v| !matches!(v.unpack(), Unpacked::Undefined))
                .map(|v| this.realm.to_number(v) as i32)
        };
        let style = opt_str(self, "style").unwrap_or_else(|| String::from("decimal"));
        let currency = opt_str(self, "currency");
        // Non-finite values render as the core glyph (∞ / NaN) with the sign and
        // currency/percent affixes, but no grouping or fraction digits.
        if !n.is_finite() {
            let mut out = String::new();
            if n.is_sign_negative() && !n.is_nan() {
                out.push('-');
            }
            if style == "currency" {
                out.push_str(&currency_symbol(currency.as_deref().unwrap_or("")));
            }
            out.push_str(if n.is_nan() { "NaN" } else { "" });
            if style == "percent" {
                out.push('%');
            }
            return out;
        }
        let use_grouping = !matches!(
            self.realm.get_property(handle, "useGrouping"),
            Some(v) if matches!(v.unpack(), Unpacked::Bool(false))
        );
        // Default fraction digits: currency = 2 (0 for JPY), else 0..=3.
        let is_jpy = currency.as_deref() == Some("JPY");
        let (def_min, def_max) = match style.as_str() {
            "currency" if is_jpy => (0, 0),
            "currency" => (2, 2),
            "percent" => (0, 0),
            _ => (0, 3),
        };
        let min = opt_num(self, "minimumFractionDigits")
            .unwrap_or(def_min)
            .clamp(0, 20);
        let max = opt_num(self, "maximumFractionDigits")
            .unwrap_or(def_max.max(min))
            .clamp(min, 20);
        let value = if style == "percent" { n * 100.0 } else { n };
        // Round `x` to `max` digits, trimming trailing zeros down to `min`.
        let fmt_digits = |x: f64| -> String {
            let mut s = alloc::format!("{:.*}", max as usize, x);
            if max > min && s.contains('.') {
                while s.ends_with('0')
                    && s.split_once('.').map_or(0, |(_, f)| f.len()) > min as usize
                {
                    s.pop();
                }
                if s.ends_with('.') {
                    s.pop();
                }
            }
            s
        };
        // `notation: "scientific" | "engineering"` renders `mantissa E exponent` (no
        // grouping); engineering pins the exponent to a multiple of 3.
        let notation = opt_str(self, "notation").unwrap_or_default();
        let (s, do_group) = if matches!(notation.as_str(), "scientific" | "engineering") {
            let neg = value < 0.0;
            let mag = value.abs();
            let mut exp = 0i32;
            let mut p = 1.0f64; // 10^exp
            if mag >= 1.0 {
                while mag >= p * 10.0 {
                    p *= 10.0;
                    exp += 1;
                }
            } else if mag > 0.0 {
                while mag < p {
                    p /= 10.0;
                    exp -= 1;
                }
            }
            if notation == "engineering" {
                // Drop the exponent to the nearest lower multiple of 3, scaling the mantissa
                // up to compensate (p = 10^exp must shrink as exp shrinks).
                let shift = exp.rem_euclid(3);
                exp -= shift;
                for _ in 0..shift {
                    p /= 10.0;
                }
            }
            let m = if mag == 0.0 { 0.0 } else { mag / p };
            let sign = if neg { "-" } else { "" };
            (alloc::format!("{sign}{}E{exp}", fmt_digits(m)), false)
        } else if notation == "compact" {
            // `notation: "compact"` (short): divide by the largest 10^(3k) scale and append
            // its suffix (K/M/B/T), showing one fraction digit only for a single-digit
            // mantissa (`1.2M`, but `123K` and `12K`).
            let neg = value < 0.0;
            let mag = value.abs();
            let (div, suffix) = if mag >= 1e12 {
                (1e12, "T")
            } else if mag >= 1e9 {
                (1e9, "B")
            } else if mag >= 1e6 {
                (1e6, "M")
            } else if mag >= 1e3 {
                (1e3, "K")
            } else {
                (1.0, "")
            };
            let m = mag / div;
            let cmax = if m < 10.0 { 1 } else { 0 };
            let mut ms = alloc::format!("{m:.*}", cmax as usize);
            if ms.contains('.') {
                while ms.ends_with('0') {
                    ms.pop();
                }
                if ms.ends_with('.') {
                    ms.pop();
                }
            }
            let sign = if neg { "-" } else { "" };
            (alloc::format!("{sign}{ms}{suffix}"), suffix.is_empty())
        } else {
            (fmt_digits(value), use_grouping)
        };
        // Group the integer part (skipped for scientific/engineering).
        let grouped = if do_group {
            let neg = s.starts_with('-');
            let body = s.trim_start_matches('-');
            let (ip, fp) = body
                .split_once('.')
                .map_or((body, None), |(i, f)| (i, Some(f)));
            let mut g = String::new();
            let len = ip.len();
            for (i, b) in ip.bytes().enumerate() {
                if i > 0 && (len - i) % 3 == 0 {
                    g.push(',');
                }
                g.push(b as char);
            }
            if let Some(f) = fp {
                g.push('.');
                g.push_str(f);
            }
            if neg { alloc::format!("-{g}") } else { g }
        } else {
            s
        };
        // Separate the sign from the magnitude so `signDisplay` and the style affixes
        // compose with the sign outermost (e.g. `-$5.00`, `+5%`).
        let neg = grouped.starts_with('-');
        let magnitude = grouped.trim_start_matches('-');
        let styled = match style.as_str() {
            "percent" => alloc::format!("{magnitude}%"),
            "currency" => {
                let sym = match currency.as_deref() {
                    Some("USD") => "$",
                    Some("EUR") => "",
                    Some("GBP") => "£",
                    Some("JPY" | "CNY") => "¥",
                    Some(other) => {
                        let other = String::from(other);
                        return alloc::format!(
                            "{}{other}\u{a0}{magnitude}",
                            if neg { "-" } else { "" }
                        );
                    }
                    None => "$",
                };
                alloc::format!("{sym}{magnitude}")
            }
            "unit" => {
                // `style: "unit"` appends the unit's short symbol (`5 km`); a
                // `unit-per-unit` compound joins the two with `/` (`5 km/h`).
                let unit = opt_str(self, "unit").unwrap_or_default();
                let sym = unit.split_once("-per-").map_or_else(
                    || String::from(unit_symbol(&unit)),
                    |(a, b)| alloc::format!("{}/{}", unit_symbol(a), unit_symbol(b)),
                );
                // Temperature/angle units attach with no space (`20°C`); others use a
                // (non-breaking) space (`5 km`).
                let sep = if matches!(unit.as_str(), "celsius" | "fahrenheit" | "degree") {
                    ""
                } else {
                    "\u{a0}"
                };
                alloc::format!("{magnitude}{sep}{sym}")
            }
            _ => String::from(magnitude),
        };
        // `currencySign: "accounting"` renders a negative currency amount in
        // parentheses (`($5.00)`) instead of with a minus sign.
        if neg
            && style == "currency"
            && opt_str(self, "currencySign").as_deref() == Some("accounting")
        {
            return alloc::format!("({styled})");
        }
        let is_zero = magnitude.bytes().all(|b| matches!(b, b'0' | b'.' | b','));
        let sign = match opt_str(self, "signDisplay").as_deref() {
            Some("never") => "",
            Some("always") => {
                if neg {
                    "-"
                } else {
                    "+"
                }
            }
            Some("exceptZero") if !is_zero => {
                if neg {
                    "-"
                } else {
                    "+"
                }
            }
            // "negative": a minus for a negative value but NOT a negative zero.
            Some("negative") if neg && !is_zero => "-",
            Some("negative") => "",
            // "auto" (default) and "exceptZero" on a zero: a sign only for negatives
            // (including a negative zero under "auto").
            _ if neg => "-",
            _ => "",
        };
        alloc::format!("{sign}{styled}")
    }

    /// ECMA-402 `CanonicalizeLocaleList(locales)`: coerces `locales` to a list of
    /// canonical locale tags (deduplicated, order preserved). `undefined` → empty
    /// list; a single string is treated as a one-element list; otherwise the
    /// argument is `ToObject`-ed and iterated by its `length`. Each element must be
    /// a String or Object (else **TypeError**), and each tag must be a structurally
    /// valid locale (else **RangeError**). A `Locale` instance contributes its
    /// already-canonical `[[Locale]]` tag.
    pub(crate) fn canonicalize_locale_list(
        &mut self,
        locales: NanBox,
    ) -> Result<Vec<String>, ExecError> {
        let mut seen: Vec<String> = Vec::new();
        if matches!(locales.unpack(), Unpacked::Undefined) {
            return Ok(seen);
        }
        // A bare string is a single-element list (no ToObject coercion of the
        // characters). A `Locale` object short-circuits below in the loop.
        let is_string = locales
            .as_handle()
            .map(Handle::from_raw)
            .is_some_and(|h| self.realm.is_string_handle(h));
        let push_tag =
            |this: &mut Self, tag: &str, seen: &mut Vec<String>| -> Result<(), ExecError> {
                match canonicalize_locale_id(tag) {
                    Some(c) => {
                        if !seen.contains(&c) {
                            seen.push(c);
                        }
                        Ok(())
                    }
                    None => {
                        let m = this.new_str(&alloc::format!(
                            "Incorrect locale information provided: {tag}"
                        ));
                        Err(ExecError::Throw(this.make_error(N_RANGE_ERROR, Some(m))))
                    }
                }
            };
        if is_string {
            let s = self.coerce_to_string(locales)?;
            push_tag(self, &s, &mut seen)?;
            return Ok(seen);
        }
        // A `Locale` object (has `[[InitializedLocale]]`) is likewise a single-
        // element list contributing its already-canonical `[[Locale]]` tag — read
        // directly (never via `toString`, which a subclass may override).
        if let Some(h) = locales.as_handle().map(Handle::from_raw)
            && self.realm.get_property(h, "\u{0}brand_loc").is_some()
            && let Some(loc) = self.realm.get_property(h, "\u{0}locale_tag")
        {
            seen.push(self.realm.to_display_string(loc));
            return Ok(seen);
        }
        // ToObject(null) is a TypeError (undefined was handled above).
        if matches!(locales.unpack(), Unpacked::Null) {
            let m = self.new_str("Cannot convert null to object");
            return Err(ExecError::Throw(self.make_error(N_TYPE_ERROR, Some(m))));
        }
        // ToObject, then iterate [0, len) reading each element (getter-aware, so
        // throwing getters and inherited indices behave per spec).
        let obj = self.coerce_to_object(locales);
        let Some(oh) = obj.as_handle().map(Handle::from_raw) else {
            return Ok(seen);
        };
        let len_v = self.read_member(oh, "length")?;
        // ToLength: a Symbol `length` is a TypeError (ToNumber throws).
        let len_v = self.coerce_to_number(len_v)?;
        let len_f = self.realm.to_number(len_v);
        let len = if len_f.is_nan() || len_f <= 0.0 {
            0u64
        } else {
            len_f.min(u32::MAX as f64 * 2.0) as u64
        };
        for i in 0..len {
            let key = alloc::format!("{i}");
            // HasProperty check: skip absent indices (sparse array-likes). Uses the
            // proxy-aware `[[HasProperty]]` so a `has` trap runs (and may throw).
            if !self.has_property_proxied(oh, &key)? {
                continue;
            }
            let el = self.read_member(oh, &key)?;
            // Element must be a String or Object (per spec `Type(kValue)`); any other
            // primitive — Symbol, Number, Boolean, BigInt — is a TypeError. (A boxed
            // String/primitive is an Object and is allowed, coerced via ToString.)
            let el_is_string = el
                .as_handle()
                .map(Handle::from_raw)
                .is_some_and(|h| self.realm.is_string_handle(h));
            let el_is_object = self.is_object_value(el) && !el_is_string;
            let ty = self.realm.type_of_value(el);
            let el_is_primitive_nonstring =
                !el_is_string && matches!(ty, "symbol" | "number" | "boolean" | "bigint");
            if el_is_primitive_nonstring || (!el_is_string && !el_is_object) {
                let m = self.new_str("locale list element is not a string or object");
                return Err(ExecError::Throw(self.make_error(N_TYPE_ERROR, Some(m))));
            }
            // A `Locale` instance contributes its canonical `[[Locale]]`.
            if let Some(h) = el.as_handle().map(Handle::from_raw)
                && let Some(loc) = self.realm.get_property(h, "\u{0}locale_tag")
            {
                let tag = self.realm.to_display_string(loc);
                if !seen.contains(&tag) {
                    seen.push(tag);
                }
                continue;
            }
            let s = self.coerce_to_string(el)?;
            push_tag(self, &s, &mut seen)?;
        }
        Ok(seen)
    }

    /// `Intl.getCanonicalLocales(locales)` — a fresh, mutable Array of the
    /// canonicalized tags.
    pub(crate) fn intl_get_canonical_locales(
        &mut self,
        locales: NanBox,
    ) -> Result<NanBox, ExecError> {
        let tags = self.canonicalize_locale_list(locales)?;
        let elems: Vec<NanBox> = tags.iter().map(|t| self.new_str(t)).collect();
        Ok(NanBox::handle(self.realm.new_array(elems).to_raw()))
    }

    /// `Intl.supportedValuesOf(key)` — a sorted, duplicate-free Array of the
    /// supported identifiers for `key` (`calendar`/`collation`/`currency`/
    /// `numberingSystem`/`timeZone`/`unit`). A `key` outside this set is a
    /// **RangeError**.
    pub(crate) fn intl_supported_values_of(&mut self, key: NanBox) -> Result<NanBox, ExecError> {
        let k = self.coerce_to_string(key)?;
        let values: &[&str] = match k.as_str() {
            // `AvailableCalendars`: the concrete calendar types (the deprecated
            // `islamic`/`islamic-rgsa` aliases are resolved away, not reported).
            "calendar" => &AVAILABLE_CALENDARS,
            "collation" => &[
                "compat", "dict", "emoji", "eor", "phonebk", "phonetic", "pinyin", "searchjl",
                "stroke", "trad", "unihan", "zhuyin",
            ],
            "currency" => &[
                "AED", "AFN", "ALL", "AMD", "ANG", "AOA", "ARS", "AUD", "AWG", "AZN", "BAM", "BBD",
                "BDT", "BGN", "BHD", "BIF", "BMD", "BND", "BOB", "BRL", "BSD", "BTN", "BWP", "BYN",
                "BZD", "CAD", "CDF", "CHF", "CLP", "CNY", "COP", "CRC", "CUP", "CVE", "CZK", "DJF",
                "DKK", "DOP", "DZD", "EGP", "ERN", "ETB", "EUR", "FJD", "FKP", "GBP", "GEL", "GHS",
                "GIP", "GMD", "GNF", "GTQ", "GYD", "HKD", "HNL", "HRK", "HTG", "HUF", "IDR", "ILS",
                "INR", "IQD", "IRR", "ISK", "JMD", "JOD", "JPY", "KES", "KGS", "KHR", "KMF", "KPW",
                "KRW", "KWD", "KYD", "KZT", "LAK", "LBP", "LKR", "LRD", "LSL", "LYD", "MAD", "MDL",
                "MGA", "MKD", "MMK", "MNT", "MOP", "MRU", "MUR", "MVR", "MWK", "MXN", "MYR", "MZN",
                "NAD", "NGN", "NIO", "NOK", "NPR", "NZD", "OMR", "PAB", "PEN", "PGK", "PHP", "PKR",
                "PLN", "PYG", "QAR", "RON", "RSD", "RUB", "RWF", "SAR", "SBD", "SCR", "SDG", "SEK",
                "SGD", "SHP", "SLE", "SOS", "SRD", "SSP", "STN", "SVC", "SYP", "SZL", "THB", "TJS",
                "TMT", "TND", "TOP", "TRY", "TTD", "TWD", "TZS", "UAH", "UGX", "USD", "UYU", "UZS",
                "VES", "VND", "VUV", "WST", "XAF", "XCD", "XOF", "XPF", "YER", "ZAR", "ZMW", "ZWL",
            ],
            "numberingSystem" => NUMBERING_SYSTEMS,
            "timeZone" => &["UTC"],
            "unit" => SANCTIONED_UNITS,
            _ => {
                let m = self.new_str(&alloc::format!("invalid key: {k}"));
                return Err(ExecError::Throw(self.make_error(N_RANGE_ERROR, Some(m))));
            }
        };
        let mut sorted: Vec<&str> = values.to_vec();
        // The generic/algorithmic numbering aliases (`native`/`traditio`/`finance`)
        // have no fixed digit mapping and are not selectable via the `nu` key, so
        // they are not reported by `supportedValuesOf` either.
        if k == "numberingSystem" {
            sorted.retain(|s| !matches!(*s, "native" | "traditio" | "finance"));
        }
        sorted.sort_unstable();
        sorted.dedup();
        let elems: Vec<NanBox> = sorted.iter().map(|s| self.new_str(s)).collect();
        Ok(NanBox::handle(self.realm.new_array(elems).to_raw()))
    }

    // --- Intl.Locale -------------------------------------------------------

    /// Builds (once) and links `Intl.Locale.prototype`: the `language`/`script`/…
    /// brand-checked `get` accessors, the `maximize`/`minimize`/`toString` methods,
    /// the `constructor` back-link, and `[Symbol.toStringTag] = "Intl.Locale"`.
    fn intl_locale_prototype(&mut self) -> Option<Handle> {
        if let Some(p) = self.realm.intl_prototype(N_INTL_LOCALE) {
            return Some(p);
        }
        let ctor = self.intl_ctor_handle("Locale")?;
        let obj_proto = self.object_prototype();
        let proto = self.realm.new_object_with_proto(obj_proto);
        for &name in LOCALE_ACCESSORS {
            let label = alloc::format!("get {name}");
            let target = self.new_str(name);
            let th = target.as_handle().map(Handle::from_raw).unwrap();
            let getter = self.realm.new_bound_native(N_INTL_LOCALE_ACCESSOR, th);
            self.install_fn_name_length(getter, &label, 0);
            self.realm.define_accessor(
                proto,
                name,
                NanBox::handle(getter.to_raw()),
                NanBox::undefined(),
            );
            self.realm.mark_hidden(proto, name);
        }
        for &m in &[
            "maximize",
            "minimize",
            "toString",
            "getCalendars",
            "getCollations",
            "getHourCycles",
            "getNumberingSystems",
            "getTimeZones",
            "getTextInfo",
            "getWeekInfo",
        ] {
            let target = self.new_str(m);
            let th = target.as_handle().map(Handle::from_raw).unwrap();
            let f = self.realm.new_bound_native(N_INTL_LOCALE_METHOD, th);
            self.install_fn_name_length(f, m, 0);
            self.realm
                .set_property(proto, m, NanBox::handle(f.to_raw()));
            self.realm.mark_hidden(proto, m);
        }
        self.install_to_string_tag(proto, "Intl.Locale");
        self.realm
            .set_hidden_property(proto, "constructor", NanBox::handle(ctor.to_raw()));
        self.link_ctor_prototype(ctor, proto);
        self.realm.set_intl_prototype(N_INTL_LOCALE, proto);
        Some(proto)
    }

    /// `new Intl.Locale(tag, options)` — parses and canonicalizes `tag` (a string or
    /// another `Locale`), applies the `language`/`script`/`region`/`calendar`/
    /// `collation`/`hourCycle`/`caseFirst`/`kn`/`numberingSystem` options onto the
    /// Unicode extension, and stores the resolved components behind hidden slots for
    /// the prototype accessors. Returns a branded `Intl.Locale` instance.
    pub(crate) fn make_locale(&mut self, args: &[NanBox]) -> Result<NanBox, ExecError> {
        let obj = self.realm.new_object();
        self.init_locale(obj, args)?;
        Ok(NanBox::handle(obj.to_raw()))
    }

    pub(crate) fn init_locale(&mut self, obj: Handle, args: &[NanBox]) -> Result<(), ExecError> {
        let tag_arg = args.first().copied().unwrap_or(NanBox::undefined());
        // Step 7: If Type(tag) is not String or Object, throw a TypeError. A
        // `Locale` argument contributes its `[[Locale]]` tag; a String/Object is
        // ToString-ed; everything else (undefined/null/boolean/number/symbol/
        // bigint) is a TypeError *before* any structural validation.
        let base_tag = if let Some(h) = tag_arg.as_handle().map(Handle::from_raw) {
            if let Some(t) = self.realm.get_property(h, "\u{0}locale_tag") {
                self.realm.to_display_string(t)
            } else if matches!(self.realm.type_of(h), Some("symbol") | Some("bigint")) {
                let m = self.new_str("Intl.Locale: tag must be a string or Locale");
                return Err(ExecError::Throw(self.make_error(N_TYPE_ERROR, Some(m))));
            } else {
                self.coerce_to_string(tag_arg)?
            }
        } else {
            let m = self.new_str("Intl.Locale: tag must be a string or Locale");
            return Err(ExecError::Throw(self.make_error(N_TYPE_ERROR, Some(m))));
        };
        let Some(canon) = canonicalize_locale_id(&base_tag) else {
            let m = self.new_str(&alloc::format!("invalid language tag: {base_tag}"));
            return Err(ExecError::Throw(self.make_error(N_RANGE_ERROR, Some(m))));
        };
        // Read the option overrides (each validated like the formatter options).
        // `ApplyOptionsToTag` does `ToObject(options)`: `undefined` yields no
        // overrides, `null` is a **TypeError**, and any other primitive boxes.
        let opts_arg = args.get(1).copied().unwrap_or(NanBox::undefined());
        let opts = match opts_arg.unpack() {
            Unpacked::Undefined => None,
            Unpacked::Null => {
                let m = self.new_str("Intl.Locale options must not be null");
                return Err(ExecError::Throw(self.make_error(N_TYPE_ERROR, Some(m))));
            }
            _ => self
                .coerce_to_object(opts_arg)
                .as_handle()
                .map(Handle::from_raw),
        };
        let language = self.get_string_option(opts, "language", &[], None)?;
        let script = self.get_string_option(opts, "script", &[], None)?;
        let region = self.get_string_option(opts, "region", &[], None)?;
        // `variants` is read after region, before the Unicode-extension keys (spec
        // option-evaluation order); applied to the tag's variant subtags.
        let variants = self.get_string_option(opts, "variants", &[], None)?;
        let calendar = self.get_string_option(opts, "calendar", &[], None)?;
        let collation = self.get_string_option(opts, "collation", &[], None)?;
        let hour_cycle =
            self.get_string_option(opts, "hourCycle", &["h11", "h12", "h23", "h24"], None)?;
        let case_first =
            self.get_string_option(opts, "caseFirst", &["upper", "lower", "false"], None)?;
        let numeric = self.get_bool_option(opts, "numeric", None)?;
        let numbering = self.get_string_option(opts, "numberingSystem", &[], None)?;
        // `firstDayOfWeek` (ES2024): a weekday name (mon…sun) or 1–7 (1=Monday),
        // resolved via WeekdayToString and applied as the `-u-fw` keyword.
        let first_day = self.get_string_option(opts, "firstDayOfWeek", &[], None)?;
        let first_day = if let Some(fd) = &first_day {
            let Some(w) = weekday_to_string(fd) else {
                let m = self.new_str("invalid firstDayOfWeek");
                return Err(ExecError::Throw(self.make_error(N_RANGE_ERROR, Some(m))));
            };
            Some(w)
        } else {
            None
        };
        // language/script/region subtags each have a fixed shape (UTS-35): a
        // language is alpha{2,3} or alpha{5,8}; a script is alpha{4}; a region is
        // alpha{2} or digit{3}. A malformed value is a RangeError.
        if let Some(l) = &language {
            let n = l.len();
            let ok = ((2..=3).contains(&n) || (5..=8).contains(&n))
                && l.bytes().all(|b| b.is_ascii_alphabetic());
            if !ok {
                let m = self.new_str("invalid language");
                return Err(ExecError::Throw(self.make_error(N_RANGE_ERROR, Some(m))));
            }
        }
        if let Some(s) = &script
            && !(s.len() == 4 && s.bytes().all(|b| b.is_ascii_alphabetic()))
        {
            let m = self.new_str("invalid script");
            return Err(ExecError::Throw(self.make_error(N_RANGE_ERROR, Some(m))));
        }
        if let Some(r) = &region {
            let alpha2 = r.len() == 2 && r.bytes().all(|b| b.is_ascii_alphabetic());
            let digit3 = r.len() == 3 && r.bytes().all(|b| b.is_ascii_digit());
            if !alpha2 && !digit3 {
                let m = self.new_str("invalid region");
                return Err(ExecError::Throw(self.make_error(N_RANGE_ERROR, Some(m))));
            }
        }
        // calendar/collation/numberingSystem must each match the UTS-35 `type`
        // value production (they take arbitrary keyword values, not a fixed list).
        for (val, name) in [
            (&calendar, "calendar"),
            (&collation, "collation"),
            (&numbering, "numberingSystem"),
        ] {
            if let Some(v) = val
                && !is_unicode_type_value(v)
            {
                let m = self.new_str(&alloc::format!("invalid {name}"));
                return Err(ExecError::Throw(self.make_error(N_RANGE_ERROR, Some(m))));
            }
        }

        let mut parsed = ParsedLocale::from_canonical(&canon);
        if let Some(l) = &language {
            parsed.language = l.to_ascii_lowercase();
        }
        if let Some(s) = &script {
            parsed.script = Some(titlecase_script(s));
        }
        if let Some(r) = &region {
            parsed.region = Some(r.to_ascii_uppercase());
        }
        if let Some(v) = &variants {
            // A `-`-joined sequence of `variant` subtags (each alnum{5,8}, or a
            // digit followed by alnum{3}); validated, lowercased, sorted, unique.
            let mut vs: Vec<String> = Vec::new();
            for sub in v.split('-') {
                let s = sub.to_ascii_lowercase();
                let alnum = s.bytes().all(|b| b.is_ascii_alphanumeric());
                let ok = ((5..=8).contains(&s.len()) && alnum)
                    || (s.len() == 4 && s.as_bytes()[0].is_ascii_digit() && alnum);
                if !ok || vs.contains(&s) {
                    let m = self.new_str("invalid variants");
                    return Err(ExecError::Throw(self.make_error(N_RANGE_ERROR, Some(m))));
                }
                vs.push(s);
            }
            vs.sort();
            parsed.variants = vs;
        }
        if let Some(c) = &calendar {
            // A deprecated calendar value passed via the `calendar` option is
            // canonicalized like the `-u-ca-` type (`islamicc` → `islamic-civil`).
            parsed.set_keyword("ca", unicode_type_alias("ca", c).unwrap_or(c));
        }
        if let Some(c) = &collation {
            parsed.set_keyword("co", c);
        }
        if let Some(h) = &hour_cycle {
            parsed.set_keyword("hc", h);
        }
        if let Some(k) = &case_first {
            parsed.set_keyword("kf", k);
        }
        if let Some(b) = numeric {
            parsed.set_keyword("kn", if b { "true" } else { "false" });
        }
        if let Some(n) = &numbering {
            parsed.set_keyword("nu", n);
        }
        if let Some(fw) = &first_day {
            parsed.set_keyword("fw", fw);
        }
        // Applying the options may turn the base into a regular grandfathered
        // form (`cel` + variant `gaulish` → `cel-gaulish` → `xtg`); re-canonicalize
        // and re-derive the components so the accessors reflect the replacement.
        if grandfathered_canonical(&parsed.base_name()).is_some()
            && let Some(canon) = canonicalize_locale_id(&parsed.to_tag())
        {
            parsed = ParsedLocale::from_canonical(&canon);
        }
        let final_tag = parsed.to_tag();
        let tagv = self.new_str(&final_tag);
        self.realm.set_hidden_property(obj, "\u{0}locale_tag", tagv);
        // Stash the resolved components so the accessors read them cheaply.
        let store = |this: &mut Self, key: &str, val: Option<&str>| {
            if let Some(v) = val {
                let sv = this.new_str(v);
                this.realm.set_hidden_property(obj, key, sv);
            }
        };
        store(self, "\u{0}loc_language", Some(&parsed.language));
        store(self, "\u{0}loc_script", parsed.script.as_deref());
        store(self, "\u{0}loc_region", parsed.region.as_deref());
        // `variants`: the `-`-joined variant subtags (already lowercased + sorted
        // by `from_canonical`), or `undefined` when the base name has none.
        if !parsed.variants.is_empty() {
            store(self, "\u{0}loc_variants", Some(&parsed.variants.join("-")));
        }
        store(self, "\u{0}loc_baseName", Some(&parsed.base_name()));
        store(self, "\u{0}loc_ca", parsed.keyword("ca"));
        store(self, "\u{0}loc_co", parsed.keyword("co"));
        store(self, "\u{0}loc_hc", parsed.keyword("hc"));
        store(self, "\u{0}loc_kf", parsed.keyword("kf"));
        store(self, "\u{0}loc_nu", parsed.keyword("nu"));
        store(self, "\u{0}loc_fw", parsed.keyword("fw"));
        // `numeric` is the boolean form of the `kn` keyword.
        let kn = parsed.keyword("kn");
        let numeric_val = matches!(kn, Some("true") | Some(""));
        self.realm
            .set_hidden_property(obj, "\u{0}loc_numeric", NanBox::boolean(numeric_val));
        // Brand + link to the prototype.
        self.realm
            .set_hidden_property(obj, "\u{0}brand_loc", NanBox::boolean(true));
        if let Some(proto) = self.intl_locale_prototype() {
            self.realm.set_object_proto(obj, Some(proto));
        }
        Ok(())
    }

    /// Dispatches an `Intl.Locale` `get` accessor: brand-checks `this`, then returns
    /// the stored component (a string, or `undefined`; `numeric` is a boolean).
    pub(crate) fn intl_locale_accessor_dispatch(
        &mut self,
        this: NanBox,
        name: &str,
    ) -> Result<NanBox, ExecError> {
        let h = self.require_intl_slot(this, "\u{0}brand_loc", "Intl.Locale.prototype getter")?;
        let read = |this: &Self, key: &str| -> NanBox {
            this.realm
                .get_property(h, key)
                .unwrap_or(NanBox::undefined())
        };
        Ok(match name {
            "language" => read(self, "\u{0}loc_language"),
            "script" => read(self, "\u{0}loc_script"),
            "region" => read(self, "\u{0}loc_region"),
            "variants" => read(self, "\u{0}loc_variants"),
            "baseName" => read(self, "\u{0}loc_baseName"),
            "calendar" => read(self, "\u{0}loc_ca"),
            "collation" => read(self, "\u{0}loc_co"),
            "hourCycle" => read(self, "\u{0}loc_hc"),
            "caseFirst" => read(self, "\u{0}loc_kf"),
            "numberingSystem" => read(self, "\u{0}loc_nu"),
            "numeric" => read(self, "\u{0}loc_numeric"),
            "firstDayOfWeek" => read(self, "\u{0}loc_fw"),
            _ => NanBox::undefined(),
        })
    }

    /// Dispatches an `Intl.Locale` method (`maximize`/`minimize`/`toString`):
    /// brand-checks `this`. `toString` returns the canonical tag; `maximize`/
    /// `minimize` return a fresh `Locale` with the same tag (no CLDR likely-subtags
    /// data, so the tag is returned unchanged — structurally valid).
    pub(crate) fn intl_locale_method_dispatch(
        &mut self,
        this: NanBox,
        name: &str,
    ) -> Result<NanBox, ExecError> {
        let h = self.require_intl_slot(this, "\u{0}brand_loc", "Intl.Locale.prototype method")?;
        let tag = self
            .realm
            .get_property(h, "\u{0}locale_tag")
            .map(|v| self.realm.to_display_string(v))
            .unwrap_or_default();
        // The Locale Info API: single-element defaults (real CLDR availability
        // data is §3.9). Each returns a fresh Array/object per call.
        let array_of = |this: &mut Self, items: &[&str]| {
            let vals: Vec<NanBox> = items.iter().map(|s| this.new_str(s)).collect();
            NanBox::handle(this.realm.new_array(vals).to_raw())
        };
        match name {
            "toString" => Ok(self.new_str(&tag)),
            "getCalendars" => Ok(array_of(self, &["gregory"])),
            "getCollations" => Ok(array_of(self, &["default"])),
            "getHourCycles" => Ok(array_of(self, &["h23"])),
            "getNumberingSystems" => Ok(array_of(self, &["latn"])),
            "getTimeZones" => {
                // `undefined` for a region-less locale; otherwise a (placeholder)
                // Array. Real per-region zone lists are §3.9 CLDR data.
                if ParsedLocale::from_canonical(&tag)
                    .region
                    .as_deref()
                    .unwrap_or("")
                    .is_empty()
                {
                    Ok(NanBox::undefined())
                } else {
                    Ok(array_of(self, &["UTC"]))
                }
            }
            "getTextInfo" => {
                let obj = self.realm.new_object();
                let pl = ParsedLocale::from_canonical(&tag);
                let rtl = matches!(
                    pl.script.as_deref().unwrap_or(""),
                    "Arab" | "Hebr" | "Syrc" | "Thaa" | "Nkoo" | "Rohg" | "Adlm"
                ) || matches!(
                    pl.language.as_str(),
                    "ar" | "he" | "fa" | "ur" | "ps" | "sd" | "ug" | "yi" | "dv" | "ku" | "ckb"
                );
                let dir = if rtl { "rtl" } else { "ltr" };
                let d = self.new_str(dir);
                self.realm.set_property(obj, "direction", d);
                Ok(NanBox::handle(obj.to_raw()))
            }
            "getWeekInfo" => {
                // Own keys are exactly { firstDay, weekend } (the proposal dropped
                // `minimalDays`). `firstDay` (1=Mon…7=Sun) comes from the locale's
                // `firstDayOfWeek` option when set, else defaults to Monday.
                let obj = self.realm.new_object();
                let fw = self
                    .realm
                    .get_property(h, "\u{0}loc_fw")
                    .map(|v| self.realm.to_display_string(v))
                    .unwrap_or_default();
                let first_day = match fw.as_str() {
                    "tue" => 2.0,
                    "wed" => 3.0,
                    "thu" => 4.0,
                    "fri" => 5.0,
                    "sat" => 6.0,
                    "sun" => 7.0,
                    _ => 1.0,
                };
                self.realm
                    .set_property(obj, "firstDay", NanBox::number(first_day));
                let weekend = self
                    .realm
                    .new_array(alloc::vec![NanBox::number(6.0), NanBox::number(7.0)]);
                self.realm
                    .set_property(obj, "weekend", NanBox::handle(weekend.to_raw()));
                Ok(NanBox::handle(obj.to_raw()))
            }
            // maximize/minimize: apply the CLDR likely-subtags data (Add/Remove
            // Likely Subtags, UTS-35) to the base name, preserving the `-u-`
            // keywords and other extensions. ECMA-402 `minimize` is defined as
            // "maximize, then remove", so it runs on the maximized locale.
            "maximize" | "minimize" => {
                #[allow(unused_mut)]
                let mut pl = ParsedLocale::from_canonical(&tag);
                #[cfg(feature = "intl")]
                {
                    let base = pl.base_name();
                    if let Ok(loc) = intl::locale::Locale::parse(&base) {
                        let result = if name == "maximize" {
                            loc.maximize()
                        } else {
                            loc.maximize().minimize()
                        };
                        pl.language = if result.language.is_empty() {
                            String::from("und")
                        } else {
                            result.language.clone()
                        };
                        pl.script = result.script.clone();
                        pl.region = result.region.clone();
                        pl.variants = result.variants.clone();
                    }
                }
                let new_tag = pl.to_tag();
                let tagv = self.new_str(&new_tag);
                self.make_locale(&[tagv])
            }
            // Any other method name: rebuild a Locale from the (unchanged) tag.
            _ => {
                let tagv = self.new_str(&tag);
                self.make_locale(&[tagv])
            }
        }
    }

    /// Splits the formatted output of an `Intl.NumberFormat` handle into typed
    /// `(kind, value)` parts, mirroring `nf.formatToParts(value)`. Shared by the
    /// `formatToParts` dispatch and by `Intl.DurationFormat`, which composes
    /// per-unit `NumberFormat`s. The `intl`-crate path yields CLDR parts; the
    /// hand-rolled path (unit style, or the no-`intl` build) re-derives parts from
    /// the formatted string's structure (sign / integer-groups / decimal / fraction).
    pub(crate) fn number_handle_parts(
        &mut self,
        handle: Handle,
        value: NanBox,
    ) -> Vec<(&'static str, String)> {
        let mut parts = self.number_handle_parts_inner(handle, value);
        // Apply the numbering system to each part's digits (see
        // `apply_numbering_digits`); non-digit parts (group/decimal/currency) are
        // untouched since they carry no ASCII digits.
        let nu = self
            .realm
            .get_property(handle, "numberingSystem")
            .map(|v| self.realm.to_display_string(v))
            .unwrap_or_default();
        if numbering_system_digit_base(&nu).is_some_and(|b| b != 0x0030) || nu == "hanidec" {
            for (_, v) in &mut parts {
                if v.chars().any(|c| c.is_ascii_digit()) {
                    *v = substitute_numbering_digits(&nu, core::mem::take(v));
                }
            }
        }
        parts
    }

    fn number_handle_parts_inner(
        &mut self,
        handle: Handle,
        value: NanBox,
    ) -> Vec<(&'static str, String)> {
        #[cfg(feature = "intl")]
        if !self.number_uses_handrolled(handle) {
            let n = self.realm.to_number(value);
            let locale = self
                .realm
                .get_property(handle, "\u{0}locale")
                .map(|v| self.realm.to_display_string(v))
                .unwrap_or_else(|| String::from("en"));
            let mut opts = self.number_format_options(handle);
            // Pre-round to the ECMA-402 / ICU decimal (matches the `format` path).
            let n = self.number_precision_round(handle, &mut opts, n);
            // `intl` 0.5 panics formatting a true negative zero; feed the smallest
            // negative subnormal (rounds to a displayed zero) so the crate emits the
            // proper minusSign / locale marks and applies `signDisplay` itself.
            let feed = if n == 0.0 && n.is_sign_negative() {
                -f64::from_bits(1)
            } else {
                n
            };
            // Default compact notation: request full mantissa precision from the
            // crate, then re-round to the ECMA-402 default (see the `format` path
            // and [`compact_reround_parts`]).
            let reround = feed.is_finite() && compact_wants_reround(&opts);
            if reround {
                opts.maximum_fraction_digits = Some(6);
            }
            let mut parts: Vec<(&'static str, String)> =
                intl::number::format_to_parts(&locale, feed, &opts)
                    .into_iter()
                    .map(|p| (p.kind.as_str(), p.value))
                    .collect();
            if reround {
                compact_reround_parts(&mut parts, opts.rounding_mode);
            }
            // Compact suffixes carry a separator space fused into the `compact`
            // token; peel it into a `literal` part per ECMA-402 (applies whether or
            // not the mantissa was re-rounded).
            if matches!(opts.notation, intl::number::Notation::Compact) {
                split_compact_affix_parts(&mut parts);
            }
            let sd = self
                .realm
                .get_property(handle, "signDisplay")
                .map(|v| self.realm.to_display_string(v))
                .unwrap_or_default();
            // The crate leaves NaN unsigned; `signDisplay: "always"` prefixes a
            // plusSign part ("+NaN"), matching the string `format` path.
            if n.is_nan()
                && sd == "always"
                && !parts
                    .iter()
                    .any(|(t, _)| *t == "minusSign" || *t == "plusSign")
            {
                parts.insert(0, ("plusSign", String::from("+")));
            }
            // The crate aliases `signDisplay: "negative"` to `"auto"`, so it wrongly
            // signs a value that rounds to a displayed zero; "negative"/"never"/
            // "exceptZero" must not sign zero. Drop the minusSign in that case.
            if matches!(sd.as_str(), "negative" | "never" | "exceptZero")
                && parts.iter().any(|(t, _)| *t == "minusSign")
                && !parts.iter().any(|(t, v)| {
                    matches!(*t, "integer" | "fraction") && v.chars().any(|c| c != '0')
                })
                && !parts.iter().any(|(t, _)| matches!(*t, "nan" | "infinity"))
            {
                parts.retain(|(t, _)| *t != "minusSign");
            }
            // `currencySign: "accounting"` parenthesizes a negative amount in the
            // parenthesizing locales (see `accounting_uses_parens`): the minusSign
            // part becomes a leading `literal "("` and a trailing `literal ")"`.
            let accounting = self
                .realm
                .get_property(handle, "currencySign")
                .map(|v| self.realm.to_display_string(v))
                .as_deref()
                == Some("accounting");
            if accounting
                && accounting_uses_parens(&locale)
                && parts.iter().any(|(t, _)| *t == "minusSign")
            {
                parts.retain(|(t, _)| *t != "minusSign");
                parts.insert(0, ("literal", String::from("(")));
                parts.push(("literal", String::from(")")));
            }
            return parts;
        }
        // Hand-rolled path: re-derive parts from the formatted string.
        let formatted = self.intl_format_value(handle, value);
        let style = self
            .realm
            .get_property(handle, "style")
            .map(|v| self.realm.to_display_string(v))
            .unwrap_or_else(|| String::from("decimal"));
        let currency_sym = if style == "currency" {
            let code = self
                .realm
                .get_property(handle, "currency")
                .map(|v| self.realm.to_display_string(v))
                .unwrap_or_default();
            currency_symbol(&code)
        } else {
            String::new()
        };
        let mut entries: Vec<(&'static str, String)> = Vec::new();
        let mut s = formatted.as_str();
        if let Some(rest) = s.strip_prefix('-') {
            entries.push(("minusSign", String::from("-")));
            s = rest;
        }
        if !currency_sym.is_empty() && s.starts_with(currency_sym.as_str()) {
            entries.push(("currency", currency_sym.clone()));
            s = &s[currency_sym.len()..];
        }
        let mut percent = false;
        if style == "percent" && s.ends_with('%') {
            percent = true;
            s = &s[..s.len() - '%'.len_utf8()];
        }
        // `unit`/`compact` append a suffix after the number: strip it here and emit
        // it as its own part(s) so the numeric decomposition below sees only digits
        // (`5 m` → integer "5" + literal " " + unit "m"; `1.2M` → …fraction + compact
        // "M"). The numeric prefix is the leading run of digits/group/decimal chars.
        let notation = self
            .realm
            .get_property(handle, "notation")
            .map(|v| self.realm.to_display_string(v))
            .unwrap_or_default();
        let mut suffix_parts: Vec<(&'static str, String)> = Vec::new();
        if style == "unit" || notation == "compact" {
            let num_end = s
                .find(|c: char| !(c.is_ascii_digit() || c == ',' || c == '.'))
                .unwrap_or(s.len());
            let suffix = &s[num_end..];
            if !suffix.is_empty() {
                let part_kind = if style == "unit" { "unit" } else { "compact" };
                // A leading separator (space/NBSP) is a `literal`; the rest is the
                // unit/compact token.
                let sep_end = suffix
                    .find(|c: char| !c.is_whitespace())
                    .unwrap_or(suffix.len());
                if sep_end > 0 {
                    suffix_parts.push(("literal", String::from(&suffix[..sep_end])));
                }
                if sep_end < suffix.len() {
                    suffix_parts.push((part_kind, String::from(&suffix[sep_end..])));
                }
            }
            s = &s[..num_end];
        }
        if s == "NaN" {
            entries.push(("nan", String::from("NaN")));
        } else if s == "" {
            entries.push(("infinity", String::from("")));
        } else {
            let (int_part, frac_part) = match s.split_once('.') {
                Some((i, f)) => (i, Some(f)),
                None => (s, None),
            };
            for (gi, grp) in int_part.split(',').enumerate() {
                if gi > 0 {
                    entries.push(("group", String::from(",")));
                }
                entries.push(("integer", String::from(grp)));
            }
            if let Some(f) = frac_part {
                entries.push(("decimal", String::from(".")));
                entries.push(("fraction", String::from(f)));
            }
        }
        if percent {
            entries.push(("percentSign", String::from("%")));
        }
        entries.extend(suffix_parts);
        entries
    }

    // --- Intl.DurationFormat ----------------------------------------------

    /// Builds (once) and links `Intl.DurationFormat.prototype` with branded
    /// `format`/`formatToParts`/`resolvedOptions` methods, the `constructor`
    /// back-link, and `[Symbol.toStringTag] = "Intl.DurationFormat"`.
    fn intl_duration_prototype(&mut self) -> Option<Handle> {
        if let Some(p) = self.realm.intl_prototype(N_INTL_DURATION_FORMAT) {
            return Some(p);
        }
        let ctor = self.intl_ctor_handle("DurationFormat")?;
        let obj_proto = self.object_prototype();
        let proto = self.realm.new_object_with_proto(obj_proto);
        for &(m, arity) in &[
            ("resolvedOptions", 0u32),
            ("format", 1),
            ("formatToParts", 1),
        ] {
            let target = self.new_str(m);
            let th = target.as_handle().map(Handle::from_raw).unwrap();
            let f = self.realm.new_bound_native(N_INTL_DURATION_METHOD, th);
            self.install_fn_name_length(f, m, arity);
            self.realm
                .set_property(proto, m, NanBox::handle(f.to_raw()));
            self.realm.mark_hidden(proto, m);
        }
        self.install_to_string_tag(proto, "Intl.DurationFormat");
        self.realm
            .set_hidden_property(proto, "constructor", NanBox::handle(ctor.to_raw()));
        self.link_ctor_prototype(ctor, proto);
        self.realm.set_intl_prototype(N_INTL_DURATION_FORMAT, proto);
        Some(proto)
    }

    /// `new Intl.DurationFormat(locales, options)` — `InitializeDurationFormat`:
    /// canonicalizes the locale, resolves `numberingSystem` (from the `-u-nu-`
    /// extension and/or option), reads the top-level `style` and each of the ten
    /// per-unit `<unit>`/`<unit>Display` options (via `GetDurationUnitOptions`, in
    /// spec order), and `fractionalDigits`. Stores all resolved slots behind hidden
    /// keys for `resolvedOptions`/`format`. A non-object `options` (other than
    /// `undefined`) is a TypeError; an invalid option value is a RangeError.
    pub(crate) fn make_duration_format(&mut self, args: &[NanBox]) -> Result<NanBox, ExecError> {
        let obj = self.realm.new_object();
        let requested =
            self.canonicalize_locale_list(args.first().copied().unwrap_or(NanBox::undefined()))?;
        // GetOptionsObject: undefined → no options; null/primitive → TypeError.
        let opts_arg = args.get(1).copied().unwrap_or(NanBox::undefined());
        let opts = if matches!(opts_arg.unpack(), Unpacked::Undefined) {
            None
        } else if self.is_object_value(opts_arg) {
            opts_arg.as_handle().map(Handle::from_raw)
        } else {
            return Err(self.type_error("Intl.DurationFormat options must be an object"));
        };
        // localeMatcher (validated, not otherwise used).
        let _ = self.get_string_option(
            opts,
            "localeMatcher",
            &["lookup", "best fit"],
            Some("best fit"),
        )?;
        // numberingSystem option (UTS-35 `type` validated) and the `-u-nu-` extension
        // resolve the data locale and the effective numbering system.
        let nu_opt = self.get_string_option(opts, "numberingSystem", &[], None)?;
        if let Some(ns) = &nu_opt
            && !is_unicode_type_value(ns)
        {
            let m = self.new_str("invalid numberingSystem");
            return Err(ExecError::Throw(self.make_error(N_RANGE_ERROR, Some(m))));
        }
        let (locale, numbering) =
            self.resolve_duration_locale(requested.first().map(String::as_str), nu_opt.as_deref());
        let locv = self.new_str(&locale);
        self.realm.set_hidden_property(obj, "\u{0}locale", locv);
        let nuv = self.new_str(&numbering);
        self.realm.set_hidden_property(obj, "numberingSystem", nuv);
        // style (default "short"); "digital" implies numeric h/m/s defaults.
        let style = self
            .get_string_option(
                opts,
                "style",
                &["long", "short", "narrow", "digital"],
                Some("short"),
            )?
            .unwrap();
        self.store_str(obj, "style", &Some(style.clone()));
        let digital = style == "digital";
        // Each row of the unit table: (unit, stylesList, digitalBase). prevStyle is
        // threaded through hours..microseconds (GetDurationUnitOptions step 6).
        const SUB: &[&str] = &["long", "short", "narrow"];
        const TIME: &[&str] = &["long", "short", "narrow", "numeric", "2-digit"];
        const FRAC: &[&str] = &["long", "short", "narrow", "numeric"];
        // (unit, stylesList, digitalDefault) per the unit table.
        let table: &[(&str, &[&str], &str)] = &[
            ("years", SUB, "short"),
            ("months", SUB, "short"),
            ("weeks", SUB, "short"),
            ("days", SUB, "short"),
            ("hours", TIME, "numeric"),
            ("minutes", TIME, "numeric"),
            ("seconds", TIME, "numeric"),
            ("milliseconds", FRAC, "numeric"),
            ("microseconds", FRAC, "numeric"),
            ("nanoseconds", FRAC, "numeric"),
        ];
        let mut prev_style: Option<String> = None;
        for (unit, styles, digital_base) in table {
            let (ust, udisp) = self.get_duration_unit_options(
                opts,
                unit,
                &style,
                styles,
                digital_base,
                prev_style.as_deref(),
                digital,
            )?;
            self.store_str(obj, unit, &Some(ust.clone()));
            let disp_key = alloc::format!("{unit}Display");
            self.store_str(obj, &disp_key, &Some(udisp));
            if matches!(
                *unit,
                "hours" | "minutes" | "seconds" | "milliseconds" | "microseconds"
            ) {
                prev_style = Some(ust);
            }
        }
        // fractionalDigits: GetNumberOption(0, 9, undefined).
        if let Some(fd) = self.get_int_option(opts, "fractionalDigits", 0.0, 9.0, None)? {
            self.realm
                .set_hidden_property(obj, "fractionalDigits", NanBox::number(fd));
        }
        self.realm
            .set_hidden_property(obj, "\u{0}brand_df", NanBox::boolean(true));
        if let Some(proto) = self.intl_duration_prototype() {
            self.realm.set_object_proto(obj, Some(proto));
        }
        Ok(NanBox::handle(obj.to_raw()))
    }

    /// Resolves the `Intl.DurationFormat` data locale and effective numbering system
    /// from the requested tag's `-u-nu-` extension and the `numberingSystem` option.
    /// The option (when a supported value) wins; otherwise a supported extension
    /// value is kept and reflected in the locale; an unsupported value falls back to
    /// the locale's default (`"latn"` for `en`). Returns `(dataLocale, numbering)`.
    fn resolve_duration_locale(
        &mut self,
        requested: Option<&str>,
        option: Option<&str>,
    ) -> (String, String) {
        let tag = requested.unwrap_or("en-US");
        // Split off any `-u-` extension; recover an `nu` keyword value.
        let parsed = ParsedLocale::from_canonical(tag);
        let ext_nu = parsed.keyword("nu").map(String::from);
        let default_nu = String::from("latn");
        let supported = |ns: &str| is_supported_numbering_system(ns);
        // The base tag without any -u- keywords (other extensions preserved).
        let mut base = parsed.base_name();
        for e in &parsed.other_ext {
            base.push('-');
            base.push_str(e);
        }
        match option {
            Some(opt) if supported(opt) => {
                // Option wins; reflect in the locale only if it equals the extension.
                if ext_nu.as_deref() == Some(opt) {
                    (alloc::format!("{base}-u-nu-{opt}"), String::from(opt))
                } else {
                    (base, String::from(opt))
                }
            }
            _ => {
                // No (usable) option: a supported extension value is used & reflected.
                match ext_nu {
                    Some(ns) if supported(&ns) => (alloc::format!("{base}-u-nu-{ns}"), ns),
                    _ => (base, default_nu),
                }
            }
        }
    }

    /// `GetDurationUnitOptions(unit, options, baseStyle, stylesList, digitalBase,
    /// prevStyle, twoDigitHours)` — reads the per-unit `<unit>` style and
    /// `<unit>Display` options, applying the spec defaults (digital base, the
    /// `prevStyle`-driven "numeric"/"2-digit" promotion) and the RangeError
    /// conditions. Returns `(style, display)`.
    #[allow(clippy::too_many_arguments)]
    fn get_duration_unit_options(
        &mut self,
        opts: Option<Handle>,
        unit: &str,
        base_style: &str,
        styles_list: &[&str],
        digital_base: &str,
        prev_style: Option<&str>,
        two_digit_hours: bool,
    ) -> Result<(String, String), ExecError> {
        let mut style = self.get_string_option(opts, unit, styles_list, None)?;
        let mut display_default = "always";
        if style.is_none() {
            if base_style == "digital" {
                if !matches!(unit, "hours" | "minutes" | "seconds") {
                    display_default = "auto";
                }
                style = Some(String::from(digital_base));
            } else {
                display_default = "auto";
                if matches!(prev_style, Some("numeric") | Some("2-digit")) {
                    style = Some(String::from("numeric"));
                } else {
                    style = Some(String::from(base_style));
                }
            }
        }
        let mut style = style.unwrap();
        let disp_key = alloc::format!("{unit}Display");
        let display = self
            .get_string_option(opts, &disp_key, &["auto", "always"], Some(display_default))?
            .unwrap();
        if matches!(prev_style, Some("numeric") | Some("2-digit")) {
            if style != "numeric" && style != "2-digit" {
                let m = self.new_str(&alloc::format!(
                    "invalid style '{style}' for {unit} following a numeric unit"
                ));
                return Err(ExecError::Throw(self.make_error(N_RANGE_ERROR, Some(m))));
            } else if matches!(unit, "minutes" | "seconds") {
                style = String::from("2-digit");
            }
        }
        // twoDigitHours: a digital formatter with 2-digit hours promotes "numeric".
        if unit == "hours" && two_digit_hours && style == "numeric" {
            // (Kept as "numeric" — the engine's digital renderer never pads hours,
            // matching the en reference where [[TwoDigitHours]] is false.)
        }
        Ok((style, display))
    }

    /// The ten duration units in table order.
    const DURATION_UNITS: [&'static str; 10] = [
        "years",
        "months",
        "weeks",
        "days",
        "hours",
        "minutes",
        "seconds",
        "milliseconds",
        "microseconds",
        "nanoseconds",
    ];

    /// Dispatches an `Intl.DurationFormat` prototype method: brand-checks `this`.
    pub(crate) fn intl_duration_method_dispatch(
        &mut self,
        this: NanBox,
        name: &str,
        args: &[NanBox],
    ) -> Result<NanBox, ExecError> {
        let h = self.require_intl_slot(
            this,
            "\u{0}brand_df",
            "Intl.DurationFormat.prototype method",
        )?;
        match name {
            "resolvedOptions" => self.duration_resolved_options(h),
            "formatToParts" => {
                let rec = self
                    .read_duration_record(args.first().copied().unwrap_or(NanBox::undefined()))?;
                let parts = self.partition_duration(h, &rec);
                let mut arr = Vec::with_capacity(parts.len());
                for (ty, val, unit) in parts {
                    let o = self.realm.new_object();
                    let tv = self.new_str(ty);
                    self.realm.set_property(o, "type", tv);
                    let vv = self.new_str(&val);
                    self.realm.set_property(o, "value", vv);
                    if let Some(u) = unit {
                        let uv = self.new_str(u);
                        self.realm.set_property(o, "unit", uv);
                    }
                    arr.push(NanBox::handle(o.to_raw()));
                }
                Ok(NanBox::handle(self.realm.new_array(arr).to_raw()))
            }
            // `format(duration)` — concatenate the partitioned parts' values.
            _ => {
                let rec = self
                    .read_duration_record(args.first().copied().unwrap_or(NanBox::undefined()))?;
                let parts = self.partition_duration(h, &rec);
                let s: String = parts.into_iter().map(|(_, v, _)| v).collect();
                Ok(self.new_str(&s))
            }
        }
    }

    /// `Intl.DurationFormat.prototype.resolvedOptions` — `{locale, numberingSystem,
    /// style, <unit>, <unit>Display …, fractionalDigits?}` in spec key order.
    fn duration_resolved_options(&mut self, h: Handle) -> Result<NanBox, ExecError> {
        let out = self.realm.new_object();
        let read = |this: &mut Self, key: &str, dflt: &str| -> String {
            this.realm
                .get_property(h, key)
                .filter(|v| !matches!(v.unpack(), Unpacked::Undefined))
                .map(|v| this.realm.to_display_string(v))
                .unwrap_or_else(|| String::from(dflt))
        };
        let locale = read(self, "\u{0}locale", "en-US");
        let lv = self.new_str(&locale);
        self.realm.set_property(out, "locale", lv);
        let nu = read(self, "numberingSystem", "latn");
        let nuv = self.new_str(&nu);
        self.realm.set_property(out, "numberingSystem", nuv);
        let style = read(self, "style", "short");
        let sv = self.new_str(&style);
        self.realm.set_property(out, "style", sv);
        for unit in Self::DURATION_UNITS {
            let ust = read(self, unit, "short");
            let uv = self.new_str(&ust);
            self.realm.set_property(out, unit, uv);
            let disp_key = alloc::format!("{unit}Display");
            let udisp = read(self, &disp_key, "auto");
            let dv = self.new_str(&udisp);
            self.realm.set_property(out, &disp_key, dv);
        }
        // fractionalDigits is reported only when it was explicitly set.
        if let Some(v) = self
            .realm
            .get_property(h, "fractionalDigits")
            .filter(|v| !matches!(v.unpack(), Unpacked::Undefined))
        {
            self.realm.set_property(out, "fractionalDigits", v);
        }
        Ok(NanBox::handle(out.to_raw()))
    }

    /// `ToDurationRecord(input)` — a String is parsed as an ISO-8601 duration
    /// (RangeError on a bad string); a non-object is a TypeError; otherwise each of
    /// the ten unit fields is read and `ToIntegerIfIntegral`-coerced (a non-integer
    /// or non-finite value is a RangeError), all fields absent is a TypeError, and
    /// the record is validated by `IsValidDuration` (RangeError otherwise). Returns
    /// the ten `f64` unit values in table order.
    fn read_duration_record(&mut self, input: NanBox) -> Result<[f64; 10], ExecError> {
        // A Temporal.Duration-like string is parsed; any other primitive (or a
        // String wrapper is an Object, handled below) follows the object path.
        if let Some(s) = input
            .as_handle()
            .map(Handle::from_raw)
            .and_then(|hh| self.realm.string_value(hh))
        {
            return self.parse_duration_string(&s);
        }
        if !self.is_object_value(input) {
            return Err(self.type_error("Intl.DurationFormat.format: argument must be an object"));
        }
        let oh = input.as_handle().map(Handle::from_raw).unwrap();
        // `ToDurationRecord` reads a `Temporal.Duration` from its internal slots,
        // NOT via the (user-observable, potentially tainted) prototype getters.
        if let Some(td) = self.realm.temporal_at(oh)
            && td.kind == crate::temporal_iso::TemporalKind::Duration
        {
            let d = &td.duration;
            return Ok([
                d.years as f64,
                d.months as f64,
                d.weeks as f64,
                d.days as f64,
                d.hours as f64,
                d.minutes as f64,
                d.seconds as f64,
                d.milliseconds as f64,
                d.microseconds as f64,
                d.nanoseconds as f64,
            ]);
        }
        let mut rec = [0.0f64; 10];
        let mut any = false;
        for (i, unit) in Self::DURATION_UNITS.iter().enumerate() {
            let v = self.read_member(oh, unit)?;
            if matches!(v.unpack(), Unpacked::Undefined) {
                continue;
            }
            any = true;
            // ToIntegerIfIntegral: ToNumber, then require an integral value.
            let nv = self.coerce_to_number(v)?;
            let n = self.realm.to_number(nv);
            if !n.is_finite() || trunc_toward_zero(n) != n {
                let m = self.new_str(&alloc::format!("duration field {unit} is not an integer"));
                return Err(ExecError::Throw(self.make_error(N_RANGE_ERROR, Some(m))));
            }
            rec[i] = n;
        }
        if !any {
            return Err(self.type_error("Intl.DurationFormat.format: no duration fields present"));
        }
        if !is_valid_duration(&rec) {
            let m = self.new_str("invalid Duration");
            return Err(ExecError::Throw(self.make_error(N_RANGE_ERROR, Some(m))));
        }
        Ok(rec)
    }

    /// Minimal ISO-8601 duration-string parser (`±PnYnMnWnDTnHnMnS`, fractional
    /// seconds). A malformed string is a RangeError. Sub-second fractions are split
    /// into milli/micro/nanoseconds.
    fn parse_duration_string(&mut self, s: &str) -> Result<[f64; 10], ExecError> {
        let bad = |this: &mut Self| -> ExecError {
            let m = this.new_str("invalid Duration string");
            ExecError::Throw(this.make_error(N_RANGE_ERROR, Some(m)))
        };
        let mut rec = [0.0f64; 10];
        let bytes = s.trim();
        let (sign, rest) = match bytes.strip_prefix('-') {
            Some(r) => (-1.0, r),
            None => (1.0, bytes.strip_prefix('+').unwrap_or(bytes)),
        };
        let rest = match rest.strip_prefix(['P', 'p']) {
            Some(r) => r,
            None => return Err(bad(self)),
        };
        // Split date / time portions on 'T'.
        let (date_part, time_part) = match rest.split_once(['T', 't']) {
            Some((d, t)) => (d, Some(t)),
            None => (rest, None),
        };
        let mut saw_any = false;
        // Returns (value, designator) tokens.
        let parse_section = |this: &mut Self,
                             sect: &str,
                             allowed: &[(char, usize)],
                             rec: &mut [f64; 10],
                             saw: &mut bool|
         -> Result<(), ExecError> {
            let mut chars = sect.chars().peekable();
            let mut last_idx: i32 = -1;
            while chars.peek().is_some() {
                let mut num = String::new();
                while let Some(&c) = chars.peek() {
                    if c.is_ascii_digit() || c == '.' || c == ',' {
                        num.push(if c == ',' { '.' } else { c });
                        chars.next();
                    } else {
                        break;
                    }
                }
                let Some(desig) = chars.next() else {
                    if num.is_empty() {
                        break;
                    }
                    return Err(bad(this));
                };
                if num.is_empty() {
                    return Err(bad(this));
                }
                let Some(&(_, slot)) = allowed.iter().find(|(d, _)| d.eq_ignore_ascii_case(&desig))
                else {
                    return Err(bad(this));
                };
                // Designators must appear in order.
                if (slot as i32) <= last_idx {
                    return Err(bad(this));
                }
                last_idx = slot as i32;
                *saw = true;
                // Only seconds may be fractional.
                if num.contains('.') && slot != 6 {
                    return Err(bad(this));
                }
                if slot == 6 {
                    // seconds with optional fraction → split into s/ms/us/ns.
                    let (int_s, frac) = num.split_once('.').unwrap_or((num.as_str(), ""));
                    rec[6] = int_s.parse::<f64>().map_err(|_| bad(this))?;
                    let mut f: String = frac.chars().take(9).collect();
                    while f.len() < 9 {
                        f.push('0');
                    }
                    rec[7] = f[0..3].parse::<f64>().unwrap_or(0.0);
                    rec[8] = f[3..6].parse::<f64>().unwrap_or(0.0);
                    rec[9] = f[6..9].parse::<f64>().unwrap_or(0.0);
                } else {
                    rec[slot] = num.parse::<f64>().map_err(|_| bad(this))?;
                }
            }
            Ok(())
        };
        // Date designators: Y(0) M(1) W(2) D(3).
        parse_section(
            self,
            date_part,
            &[('Y', 0), ('M', 1), ('W', 2), ('D', 3)],
            &mut rec,
            &mut saw_any,
        )?;
        if let Some(tp) = time_part {
            if tp.is_empty() {
                return Err(bad(self));
            }
            // Time designators: H(4) M(5) S(6).
            parse_section(
                self,
                tp,
                &[('H', 4), ('M', 5), ('S', 6)],
                &mut rec,
                &mut saw_any,
            )?;
        }
        if !saw_any {
            return Err(bad(self));
        }
        for v in &mut rec {
            *v *= sign;
        }
        if !is_valid_duration(&rec) {
            let m = self.new_str("invalid Duration");
            return Err(ExecError::Throw(self.make_error(N_RANGE_ERROR, Some(m))));
        }
        Ok(rec)
    }

    /// Resolves a duration formatter's per-unit `(style, display)` from its slots.
    fn duration_unit_resolved(&mut self, h: Handle, unit: &str) -> (String, String) {
        let style = self
            .realm
            .get_property(h, unit)
            .filter(|v| !matches!(v.unpack(), Unpacked::Undefined))
            .map(|v| self.realm.to_display_string(v))
            .unwrap_or_else(|| String::from("short"));
        let disp = self
            .realm
            .get_property(h, &alloc::format!("{unit}Display"))
            .filter(|v| !matches!(v.unpack(), Unpacked::Undefined))
            .map(|v| self.realm.to_display_string(v))
            .unwrap_or_else(|| String::from("auto"));
        (style, disp)
    }

    /// `PartitionDurationFormatPattern` (en, mirroring the test262 reference): formats
    /// each shown unit via a per-unit `Intl.NumberFormat` (`style:"unit"` or numeric/
    /// 2-digit), threads the time separator between numeric h:m:s, then joins the unit
    /// strings with an `Intl.ListFormat` (`type:"unit"`). Returns `(type, value, unit?)`
    /// parts. Composing the real `NumberFormat`/`ListFormat` keeps `format` and the
    /// reference output identical regardless of CLDR-data fidelity.
    fn partition_duration(
        &mut self,
        h: Handle,
        duration: &[f64; 10],
    ) -> Vec<(&'static str, String, Option<&'static str>)> {
        let style = self
            .realm
            .get_property(h, "style")
            .filter(|v| !matches!(v.unpack(), Unpacked::Undefined))
            .map(|v| self.realm.to_display_string(v))
            .unwrap_or_else(|| String::from("short"));
        let numbering = self
            .realm
            .get_property(h, "numberingSystem")
            .filter(|v| !matches!(v.unpack(), Unpacked::Undefined))
            .map(|v| self.realm.to_display_string(v))
            .unwrap_or_else(|| String::from("latn"));
        let fractional_digits = self
            .realm
            .get_property(h, "fractionalDigits")
            .filter(|v| !matches!(v.unpack(), Unpacked::Undefined))
            .map(|v| self.realm.to_number(v) as i32);
        let units = Self::DURATION_UNITS;
        // Resolve per-unit styles/displays up front.
        let mut ustyle = [const { String::new() }; 10];
        let mut udisp = [const { String::new() }; 10];
        for (i, u) in units.iter().enumerate() {
            let (s, d) = self.duration_unit_resolved(h, u);
            ustyle[i] = s;
            udisp[i] = d;
        }
        let time_separator = ":";
        // Each element of `result` is the ordered parts list for one shown unit.
        let mut result: Vec<Vec<(&'static str, String, Option<&'static str>)>> = Vec::new();
        let mut need_separator = false;
        let mut display_negative_sign = true;
        // Whether the duration is negative (drives the leading unit's sign). Per
        // DurationSign, a negative *zero* field counts as zero — `{years:-0}` has
        // sign 0 and formats identically to `{years:+0}` (no minus sign).
        let any_negative = duration.iter().any(|&v| v < 0.0);
        for idx in 0..units.len() {
            let unit = units[idx];
            let singular: &'static str = duration_singular(unit);
            // Normalize a negative-zero field to +0 (a genuinely-negative duration
            // re-applies the leading sign below); `{years:-0}` must format as "0".
            let mut value = duration[idx];
            if value == 0.0 {
                value = 0.0;
            }
            let style_u = ustyle[idx].clone();
            let display_u = udisp[idx].clone();
            // Combine numeric seconds/ms/us with their fractional remainder.
            let mut value_str: Option<String> = None;
            let mut done = false;
            let (mut nf_min_frac, mut nf_max_frac, mut nf_trunc) =
                (None::<i32>, None::<i32>, false);
            if matches!(unit, "seconds" | "milliseconds" | "microseconds") {
                let next_style = ustyle[idx + 1].as_str();
                if next_style == "numeric" {
                    let exp = match unit {
                        "seconds" => 9,
                        "milliseconds" => 6,
                        _ => 3,
                    };
                    value_str = Some(duration_to_fractional(duration, exp));
                    nf_max_frac = Some(fractional_digits.unwrap_or(9));
                    nf_min_frac = Some(fractional_digits.unwrap_or(0));
                    nf_trunc = true;
                    done = true;
                }
            }
            // Display zero numeric minutes when seconds will be displayed.
            let mut display_required = false;
            if unit == "minutes" && need_separator {
                display_required = udisp[6] == "always"
                    || duration[6] != 0.0
                    || duration[7] != 0.0
                    || duration[8] != 0.0
                    || duration[9] != 0.0;
            }
            let nonzero = value != 0.0
                || value_str
                    .as_deref()
                    .is_some_and(|s| s.bytes().any(|b| matches!(b, b'1'..=b'9')));
            if nonzero || display_u != "auto" || display_required {
                let mut sign_never = false;
                if display_negative_sign {
                    display_negative_sign = false;
                    if value == 0.0 && value_str.is_none() && any_negative {
                        value = -0.0;
                    }
                } else {
                    sign_never = true;
                }
                // Build the per-unit NumberFormat handle.
                let nf = self.realm.new_object();
                let marker = self.new_str("number");
                self.realm.set_hidden_property(nf, "\u{0}intl", marker);
                let loc = self.new_str("en");
                self.realm.set_hidden_property(nf, "\u{0}locale", loc);
                let nuv = self.new_str(&numbering);
                self.realm.set_hidden_property(nf, "numberingSystem", nuv);
                if sign_never {
                    let sd = self.new_str("never");
                    self.realm.set_hidden_property(nf, "signDisplay", sd);
                }
                if style_u == "2-digit" {
                    self.realm
                        .set_hidden_property(nf, "minimumIntegerDigits", NanBox::number(2.0));
                }
                if style_u != "numeric" && style_u != "2-digit" {
                    let st = self.new_str("unit");
                    self.realm.set_hidden_property(nf, "style", st);
                    let uu = self.new_str(singular);
                    self.realm.set_hidden_property(nf, "unit", uu);
                    let ud = self.new_str(&style_u);
                    self.realm.set_hidden_property(nf, "unitDisplay", ud);
                } else {
                    self.realm
                        .set_hidden_property(nf, "useGrouping", NanBox::boolean(false));
                }
                if let Some(mn) = nf_min_frac {
                    self.realm.set_hidden_property(
                        nf,
                        "minimumFractionDigits",
                        NanBox::number(mn as f64),
                    );
                }
                if let Some(mx) = nf_max_frac {
                    self.realm.set_hidden_property(
                        nf,
                        "maximumFractionDigits",
                        NanBox::number(mx as f64),
                    );
                }
                if nf_trunc {
                    let rm = self.new_str("trunc");
                    self.realm.set_hidden_property(nf, "roundingMode", rm);
                }
                // The value to format: the combined fractional string, or the number.
                let value_box = match &value_str {
                    Some(s) => self.new_str(s),
                    None => NanBox::number(value),
                };
                let number_parts = self.number_handle_parts(nf, value_box);
                let mut list: Vec<(&'static str, String, Option<&'static str>)> = if !need_separator
                {
                    Vec::new()
                } else {
                    // Append to the previous (numeric) unit's list, with a separator.
                    let mut prev = result.pop().unwrap();
                    prev.push(("literal", String::from(time_separator), None));
                    prev
                };
                for (ty, val) in number_parts {
                    list.push((ty, val, Some(singular)));
                }
                if !need_separator {
                    if style_u == "2-digit" || style_u == "numeric" {
                        need_separator = true;
                    }
                    result.push(list);
                } else {
                    result.push(list);
                }
            }
            if done {
                break;
            }
        }
        // List style: digital collapses to short.
        let list_style = if style == "digital" {
            String::from("short")
        } else {
            style
        };
        // Build the strings, then join via ListFormat "unit".
        let strings: Vec<String> = result
            .iter()
            .map(|parts| parts.iter().map(|(_, v, _)| v.as_str()).collect())
            .collect();
        let lf = self.realm.new_object();
        let lm = self.new_str("list");
        self.realm.set_hidden_property(lf, "\u{0}intl", lm);
        let llo = self.new_str("en");
        self.realm.set_hidden_property(lf, "\u{0}locale", llo);
        let lt = self.new_str("unit");
        self.realm.set_hidden_property(lf, "type", lt);
        let ls = self.new_str(&list_style);
        self.realm.set_hidden_property(lf, "style", ls);
        let list_parts = self.list_format_parts(&strings, "unit", &list_style);
        // Flatten: an "element" splices in the next unit's parts; a "literal" passes through.
        let mut flattened: Vec<(&'static str, String, Option<&'static str>)> = Vec::new();
        let mut iter = result.into_iter();
        for (ty, val) in list_parts {
            if ty == "element" {
                if let Some(parts) = iter.next() {
                    flattened.extend(parts);
                }
            } else {
                flattened.push((ty, val, None));
            }
        }
        let _ = lf;
        flattened
    }
}

/// A parsed `unicode_locale_id` split into core subtags plus its `-u-` keyword
/// list, used by `Intl.Locale` to apply option overrides and rebuild the tag.
struct ParsedLocale {
    language: String,
    script: Option<String>,
    region: Option<String>,
    variants: Vec<String>,
    /// `-u-` leading attributes (keyword-less subtags, e.g. the `attr` in
    /// `en-u-attr-co-phonebk`), sorted; emitted before the keywords.
    attributes: Vec<String>,
    /// `-u-` keyword `(key, value)` pairs (value may be empty for `kn`/`true`).
    keywords: Vec<(String, String)>,
    /// Any other extensions (`-t-`, `-x-`, …) preserved verbatim after the `-u-` block.
    other_ext: Vec<String>,
}

impl ParsedLocale {
    /// Splits a canonical tag (the output of [`canonicalize_locale_id`]) into its
    /// components and `-u-` keywords.
    fn from_canonical(canon: &str) -> Self {
        let mut language = String::new();
        let mut script = None;
        let mut region = None;
        let mut variants = Vec::new();
        let mut attributes: Vec<String> = Vec::new();
        let mut keywords = Vec::new();
        let mut other_ext = Vec::new();
        let parts: Vec<&str> = canon.split('-').collect();
        let mut i = 0;
        if i < parts.len() {
            language = parts[i].to_ascii_lowercase();
            i += 1;
        }
        if i < parts.len()
            && parts[i].len() == 4
            && parts[i].bytes().all(|b| b.is_ascii_alphabetic())
        {
            script = Some(titlecase_script(parts[i]));
            i += 1;
        }
        if i < parts.len()
            && ((parts[i].len() == 2 && parts[i].bytes().all(|b| b.is_ascii_alphabetic()))
                || (parts[i].len() == 3 && parts[i].bytes().all(|b| b.is_ascii_digit())))
        {
            region = Some(parts[i].to_ascii_uppercase());
            i += 1;
        }
        // Variants until a singleton.
        while i < parts.len() && parts[i].len() != 1 {
            variants.push(parts[i].to_ascii_lowercase());
            i += 1;
        }
        // Extensions.
        while i < parts.len() {
            let singleton = parts[i];
            if singleton == "u" {
                i += 1;
                // A `-u-` block alternates 2-char keys with their (3-8 char) values.
                // Leading attributes (3-8 char, no key) are rare; skip until the first
                // 2-char key, then read each key's run of non-key value subtags.
                while i < parts.len() && parts[i].len() != 1 {
                    if parts[i].len() != 2 {
                        // An attribute (no key) — a keyword-less subtag preserved
                        // before the keywords (`en-u-attr-co-phonebk`).
                        attributes.push(String::from(parts[i]));
                        i += 1;
                        continue;
                    }
                    let key = String::from(parts[i]);
                    i += 1;
                    let mut vals: Vec<String> = Vec::new();
                    while i < parts.len() && parts[i].len() != 1 && parts[i].len() != 2 {
                        vals.push(String::from(parts[i]));
                        i += 1;
                    }
                    keywords.push((key, vals.join("-")));
                }
            } else {
                // Preserve other extensions verbatim. Private use (`x`) consumes
                // *all* remaining subtags, including length-1 ones (so `x-u-foo`
                // is one private sequence, not the start of a `-u-` extension).
                let private = singleton == "x";
                let mut buf = alloc::vec![String::from(singleton)];
                i += 1;
                while i < parts.len() && (private || parts[i].len() != 1) {
                    buf.push(String::from(parts[i]));
                    i += 1;
                }
                other_ext.push(buf.join("-"));
            }
        }
        attributes.sort();
        ParsedLocale {
            language,
            script,
            region,
            variants,
            attributes,
            keywords,
            other_ext,
        }
    }

    /// The value of `-u-` keyword `key`, if present (empty string for a bare key).
    fn keyword(&self, key: &str) -> Option<&str> {
        self.keywords
            .iter()
            .find(|(k, _)| k == key)
            .map(|(_, v)| v.as_str())
    }

    /// Sets (or replaces) a `-u-` keyword. A `kf`/`kn` `"false"`/`"true"` is kept
    /// as-is; the accessors interpret it.
    fn set_keyword(&mut self, key: &str, val: &str) {
        let v = val.to_ascii_lowercase();
        if let Some(e) = self.keywords.iter_mut().find(|(k, _)| k == key) {
            e.1 = v;
        } else {
            self.keywords.push((String::from(key), v));
        }
    }

    /// The `baseName`: `language(-script)?(-region)?(-variants)?` (no extensions).
    fn base_name(&self) -> String {
        let mut out = self.language.clone();
        if let Some(s) = &self.script {
            out.push('-');
            out.push_str(s);
        }
        if let Some(r) = &self.region {
            out.push('-');
            out.push_str(r);
        }
        for v in &self.variants {
            out.push('-');
            out.push_str(v);
        }
        out
    }

    /// Rebuilds the full canonical tag: base name followed by every extension
    /// ordered by its singleton (`-a-`/`-t-`/`-u-`/… ascending, private use `-x-`
    /// last), with the `-u-` attributes and keywords each sorted.
    fn to_tag(&self) -> String {
        let mut out = self.base_name();
        // The `-u-` block, assembled from its (sorted) attributes and keywords.
        let mut kw = self.keywords.clone();
        kw.sort_by(|a, b| a.0.cmp(&b.0));
        let mut exts: Vec<String> = self.other_ext.clone();
        if !kw.is_empty() || !self.attributes.is_empty() {
            let mut u = String::from("u");
            for a in &self.attributes {
                u.push('-');
                u.push_str(a);
            }
            for (k, v) in &kw {
                u.push('-');
                u.push_str(k);
                if !v.is_empty() && v != "true" {
                    u.push('-');
                    u.push_str(v);
                }
            }
            exts.push(u);
        }
        // Extensions sort by their leading singleton, with private use (`x`) last.
        exts.sort_by_key(|e| {
            let s = e.as_bytes().first().copied().unwrap_or(b'~');
            (s == b'x', s)
        });
        for e in &exts {
            out.push('-');
            out.push_str(e);
        }
        out
    }
}

/// Whether `ns` is a numbering system this engine treats as supported (only `latn`
/// and `arab` have data; others fall back to the locale default for DurationFormat).
fn is_supported_numbering_system(ns: &str) -> bool {
    is_known_numbering_system(ns)
}

/// The singular `Intl.NumberFormat` unit name for a duration unit field
/// (`"hours"` → `"hour"`), i.e. the field name without its trailing `s`.
fn duration_singular(unit: &str) -> &'static str {
    match unit {
        "years" => "year",
        "months" => "month",
        "weeks" => "week",
        "days" => "day",
        "hours" => "hour",
        "minutes" => "minute",
        "seconds" => "second",
        "milliseconds" => "millisecond",
        "microseconds" => "microsecond",
        _ => "nanosecond",
    }
}

/// `IsValidDuration(record)` for the ten `f64` unit values: all-same-sign, finite,
/// `abs(years|months|weeks) < 2^32`, and `abs(normalizedSeconds) < 2^53` where the
/// seconds are accumulated exactly via `i128` nanoseconds to avoid `f64` rounding.
fn is_valid_duration(rec: &[f64; 10]) -> bool {
    let mut sign = 0i32;
    for &v in rec {
        if !v.is_finite() {
            return false;
        }
        let s = if v > 0.0 {
            1
        } else if v < 0.0 {
            -1
        } else {
            0
        };
        if s != 0 {
            if sign != 0 && sign != s {
                return false;
            }
            sign = s;
        }
    }
    // years/months/weeks bound.
    let two32 = 4_294_967_296.0f64; // 2^32
    if rec[0].abs() >= two32 || rec[1].abs() >= two32 || rec[2].abs() >= two32 {
        return false;
    }
    // normalizedSeconds = days*86400 + hours*3600 + minutes*60 + seconds
    //   + ms/1e3 + us/1e6 + ns/1e9, computed exactly in i128 nanoseconds.
    // Each value is integral and finite here. Guard against i128 overflow by
    // rejecting any single term that already exceeds the 2^53-seconds budget.
    let two53_ns = (1i128 << 53) * 1_000_000_000; // 2^53 seconds in ns
    let term_ns = |v: f64, scale_ns: i128| -> Option<i128> {
        if v.abs() >= 9.0e30 {
            return None; // far beyond any valid range; treat as overflow
        }
        (v as i128).checked_mul(scale_ns)
    };
    let mut total: i128 = 0;
    let scales: [(usize, i128); 7] = [
        (3, 86_400 * 1_000_000_000),
        (4, 3_600 * 1_000_000_000),
        (5, 60 * 1_000_000_000),
        (6, 1_000_000_000),
        (7, 1_000_000),
        (8, 1_000),
        (9, 1),
    ];
    for (i, scale) in scales {
        match term_ns(rec[i], scale).and_then(|t| total.checked_add(t)) {
            Some(t) => total = t,
            None => return false,
        }
    }
    total.abs() < two53_ns
}

/// `DurationToFractional` (test262 reference): the numeric value of the unit at
/// `exponent` (9=seconds, 6=milliseconds, 3=microseconds) combined with all smaller
/// sub-second fields, as an exact decimal string. Uses `i128` nanoseconds.
fn duration_to_fractional(duration: &[f64; 10], exponent: u32) -> String {
    let (seconds, milliseconds, microseconds, nanoseconds) =
        (duration[6], duration[7], duration[8], duration[9]);
    // Fast path: no smaller sub-seconds present → return the bare amount.
    match exponent {
        9 if milliseconds == 0.0 && microseconds == 0.0 && nanoseconds == 0.0 => {
            return format_integral(seconds);
        }
        6 if microseconds == 0.0 && nanoseconds == 0.0 => {
            return format_integral(milliseconds);
        }
        3 if nanoseconds == 0.0 => {
            return format_integral(microseconds);
        }
        _ => {}
    }
    let mut ns: i128 = nanoseconds as i128;
    if exponent >= 9 {
        ns += (seconds as i128) * 1_000_000_000;
    }
    if exponent >= 6 {
        ns += (milliseconds as i128) * 1_000_000;
    }
    if exponent >= 3 {
        ns += (microseconds as i128) * 1_000;
    }
    let e: i128 = 10i128.pow(exponent);
    let q = ns / e;
    let mut r = ns % e;
    if r < 0 {
        r = -r;
    }
    let mut rs = alloc::format!("{r}");
    while rs.len() < exponent as usize {
        rs.insert(0, '0');
    }
    alloc::format!("{q}.{rs}")
}

/// Formats an integral `f64` without a decimal point or exponent (for duration
/// values, which are validated integers).
fn format_integral(v: f64) -> String {
    alloc::format!("{}", v as i128)
}

/// Titlecases a 4-letter script subtag (`latn` → `Latn`).
fn titlecase_script(s: &str) -> String {
    let mut out = String::new();
    for (i, c) in s.chars().enumerate() {
        if i == 0 {
            out.push(c.to_ascii_uppercase());
        } else {
            out.push(c.to_ascii_lowercase());
        }
    }
    out
}

#[cfg(all(test, feature = "intl"))]
mod decimal_round_tests {
    use super::intl_decimal_round;
    use intl::number::RoundingMode;

    // Round a value and render it the way `intl::number::format` would for en-US
    // decimal (min/max fraction), so the assertions read like the ECMA-402 output.
    fn fmt(n: f64, keep_frac: usize, sig: Option<usize>, inc: u32, mode: RoundingMode) -> f64 {
        intl_decimal_round(n, keep_frac, sig, inc, mode)
    }

    #[test]
    fn shortest_decimal_boundary() {
        // 1.15 is really 1.1499… in f64; ECMA-402 rounds the *shortest* decimal up.
        assert_eq!(fmt(1.15, 3, Some(2), 1, RoundingMode::HalfExpand), 1.2);
        assert_eq!(fmt(1.15, 3, Some(2), 1, RoundingMode::HalfEven), 1.2);
        assert_eq!(fmt(1.15, 3, Some(2), 1, RoundingMode::HalfCeil), 1.2);
        assert_eq!(fmt(-1.15, 3, Some(2), 1, RoundingMode::HalfFloor), -1.2);
        // 123.445 minSig handled by crate; here max 5 sig → 123.45.
        assert_eq!(
            fmt(123.445, 3, Some(5), 1, RoundingMode::HalfExpand),
            123.45
        );
    }

    #[test]
    fn rounding_increment_direct() {
        // 1.25 inc=2 maxFrac=1 → nearest 0.2 → 1.2 (not double-rounded to 1.4).
        assert_eq!(fmt(1.25, 1, None, 2, RoundingMode::HalfExpand), 1.2);
        // 1.075 inc=5 maxFrac=2 → nearest 0.05, tie → away → 1.10.
        assert_eq!(fmt(1.0750, 2, None, 5, RoundingMode::HalfExpand), 1.1);
        // 1.15 inc=10 maxFrac=2 → nearest 0.10, tie → 1.20.
        assert_eq!(fmt(1.15, 2, None, 10, RoundingMode::HalfExpand), 1.2);
        // 1.20 inc=20 maxFrac=2 → nearest 0.20 → already a multiple → 1.20.
        assert_eq!(fmt(1.20, 2, None, 20, RoundingMode::HalfExpand), 1.2);
        // 1.5 inc=25 maxFrac=2 → multiples 1.25/1.50 → 1.50.
        assert_eq!(fmt(1.5000, 2, None, 25, RoundingMode::HalfExpand), 1.5);
        // 1.25 inc=250 maxFrac=3 → nearest 0.250 → 1.250.
        assert_eq!(fmt(1.2500, 3, None, 250, RoundingMode::HalfExpand), 1.25);
    }

    #[test]
    fn plain_fraction_rounding() {
        assert_eq!(fmt(1.005, 2, None, 1, RoundingMode::HalfExpand), 1.01);
        assert_eq!(fmt(2.5, 0, None, 1, RoundingMode::HalfEven), 2.0);
        assert_eq!(fmt(3.5, 0, None, 1, RoundingMode::HalfEven), 4.0);
        assert_eq!(fmt(0.0, 2, None, 1, RoundingMode::HalfExpand), 0.0);
    }
}

// ECMA-402 resolution helpers (`-u-` keyword extraction, calendar / numbering
// canonicalization) and the exact-decimal / roundingPriority digit routines,
// tested as pure functions.
#[cfg(test)]
mod resolution_helper_tests {
    use super::*;

    #[test]
    fn split_u_keyword_hc() {
        assert_eq!(
            split_u_keyword("en-US-u-hc-h23", "hc"),
            (String::from("en-US"), Some(String::from("h23")))
        );
        // Keeps sibling keywords when removing `hc`.
        assert_eq!(
            split_u_keyword("en-u-ca-gregory-hc-h11-nu-arab", "hc"),
            (
                String::from("en-u-ca-gregory-nu-arab"),
                Some(String::from("h11"))
            )
        );
        // Absent keyword → unchanged, None.
        assert_eq!(
            split_u_keyword("en-US", "hc"),
            (String::from("en-US"), None)
        );
        // Removing the only keyword drops the whole `-u-` section.
        assert_eq!(
            split_u_keyword("de-u-hc-h24", "hc"),
            (String::from("de"), Some(String::from("h24")))
        );
    }

    #[test]
    fn calendar_canonicalization() {
        assert_eq!(canonicalize_calendar("islamicc"), "islamic-civil");
        assert_eq!(canonicalize_calendar("ISO8601"), "iso8601");
        assert_eq!(canonicalize_calendar("ethiopic-amete-alem"), "ethioaa");
        // Deprecated `islamic`/`islamic-rgsa` fall back to a concrete calendar.
        assert_eq!(canonicalize_calendar("islamic"), "islamic-civil");
        assert_eq!(canonicalize_calendar("islamic-rgsa"), "islamic-civil");
        assert_eq!(canonicalize_calendar("gregory"), "gregory");
    }

    #[test]
    fn resolve_ca_option_and_extension() {
        // Invalid option ignored → the extension calendar is used and kept.
        assert_eq!(
            resolve_ca_key("en", "en-u-ca-iso8601", Some("invalid")),
            (String::from("iso8601"), String::from("-ca-iso8601"))
        );
        // Valid option differing from the extension → option wins, extension dropped.
        assert_eq!(
            resolve_ca_key("en", "en-u-ca-gregory", Some("iso8601")),
            (String::from("iso8601"), String::new())
        );
        // Both invalid → default gregory, no extension.
        assert_eq!(
            resolve_ca_key("en", "en-u-ca-invalid", Some("invalid2")),
            (String::from("gregory"), String::new())
        );
    }

    #[test]
    fn resolve_nu_rejects_generic_aliases() {
        // `native`/`traditio`/`finance` are not selectable → fall back to default.
        assert_eq!(
            resolve_nu_key("ja-JP", "ja-JP-u-nu-native", None),
            (String::from("latn"), String::new())
        );
        assert_eq!(
            resolve_nu_key("en", "en", Some("finance")),
            (String::from("latn"), String::new())
        );
        // A real system via the extension is kept.
        assert_eq!(
            resolve_nu_key("en", "en-u-nu-arab", None),
            (String::from("arab"), String::from("-nu-arab"))
        );
    }
}

#[cfg(all(test, feature = "intl"))]
mod exact_decimal_tests {
    use super::*;
    use intl::number::RoundingMode;

    fn digits(v: &[u8]) -> alloc::vec::Vec<u8> {
        v.to_vec()
    }

    #[test]
    fn raw_fixed_rounds_half_expand() {
        // 1.625 → 2 fraction digits, halfExpand → 1.63.
        let (i, f) = to_raw_fixed(
            false,
            digits(&[1]),
            digits(&[6, 2, 5]),
            2,
            RoundingMode::HalfExpand,
        );
        assert_eq!((i, f), (digits(&[1]), digits(&[6, 3])));
    }

    #[test]
    fn raw_precision_rounds_to_sig_digits() {
        // 1.234 → 3 significant digits → 1.23.
        let (i, f) = to_raw_precision(
            false,
            digits(&[1]),
            digits(&[2, 3, 4]),
            3,
            RoundingMode::HalfExpand,
        );
        assert_eq!((i, f), (digits(&[1]), digits(&[2, 3])));
    }

    #[test]
    fn round_up_modes() {
        assert!(exact_round_up(&[5], RoundingMode::HalfExpand, false));
        assert!(!exact_round_up(&[4, 9], RoundingMode::HalfExpand, false));
        assert!(!exact_round_up(&[5], RoundingMode::Trunc, false));
        assert!(exact_round_up(&[1], RoundingMode::Expand, false));
    }

    #[test]
    fn group_thousands() {
        assert_eq!(group_thousands_sep("100000", ","), "100,000");
        assert_eq!(
            group_thousands_sep("987654321987654321", ","),
            "987,654,321,987,654,321"
        );
        assert_eq!(group_thousands_sep("12", ","), "12");
    }
}

// Non-Gregorian (Islamic / Persian) `dateStyle` rendering: the engine computes the
// calendar fields via `temporal_calendar`, then the `intl` crate renders the
// localized month/era names. This exercises that wiring at the seam.
#[cfg(all(test, feature = "intl"))]
mod alt_calendar_format_tests {
    use crate::nbexec::temporal_calendar::iso_to_fields;
    use crate::temporal_iso::IsoDate;
    use intl::datetime::{DateStyle, format_islamic_date, format_persian_date};

    #[test]
    fn islamic_month_fields_and_names() {
        // 2024-03-26 (proleptic Gregorian) is 17 Ramadan 1445 in islamic-tbla.
        let iso = IsoDate {
            year: 2024,
            month: 3,
            day: 26,
        };
        let f = iso_to_fields("islamic-tbla", iso);
        assert_eq!(f.month, 9, "Ramadan is the 9th Islamic month");
        assert_eq!(f.year, 1445);
        // dateStyle:long spells the month out; dateStyle:short does not.
        let long = format_islamic_date("en", f.year, f.month, f.day, DateStyle::Long);
        assert!(long.contains("Ramadan"), "long includes month name: {long}");
        let short = format_islamic_date("en", f.year, f.month, f.day, DateStyle::Short);
        assert!(
            !short.contains("Ramadan"),
            "short uses a numeric month: {short}"
        );
    }

    #[test]
    fn persian_month_fields_and_names() {
        // 2024-03-26 is 7 Farvardin 1403 in the Persian (Solar Hijri) calendar.
        let iso = IsoDate {
            year: 2024,
            month: 3,
            day: 26,
        };
        let f = iso_to_fields("persian", iso);
        assert_eq!(f.month, 1, "Farvardin is the 1st Persian month");
        assert_eq!(f.year, 1403);
        let long = format_persian_date("en", f.year, f.month, f.day, DateStyle::Long);
        assert!(
            long.contains("Farvardin"),
            "long includes month name: {long}"
        );
        let short = format_persian_date("en", f.year, f.month, f.day, DateStyle::Short);
        assert!(
            !short.contains("Farvardin"),
            "short uses a numeric month: {short}"
        );
    }
}