go-rs 0.1.2

Go on the fusevm bytecode VM — compiles to fusevm::Chunk, runs on a shared three-tier Cranelift JIT
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
//! Host builtins and the strict numeric hook for go-rs.
//!
//! fusevm runs the lowered chunk; this module supplies the runtime behavior the
//! bytecode can't express directly: the `fmt` print family (`Println`/`Print`/
//! `Printf`) and the Go builtins `println`/`print`, plus a [`numeric_hook`] that
//! gives `+` its string-concatenation overload and `<`/`==`/… their string
//! ordering, wraps overflowing integer arithmetic, and decides nil and mixed
//! `int`/`float64` identity. Values render with Go's `fmt` `%v` rules
//! ([`go_str`]).

use fusevm::{NumOp, Value, VM};
use std::cell::RefCell;
use std::collections::HashMap;

/// `fmt.Println` — space-separated operands, trailing newline, to stdout.
pub const GPRINTLN: u16 = 800;
/// `fmt.Print` — operands with Go's between-non-strings spacing, to stdout.
pub const GPRINT: u16 = 801;
/// `fmt.Printf` — format string + args, to stdout.
pub const GPRINTF: u16 = 802;
/// Go builtin `println` — space-separated, trailing newline, to stderr.
pub const GEPRINTLN: u16 = 803;
/// Go builtin `print` — no spacing, to stderr.
pub const GEPRINT: u16 = 804;
/// `__rust_compile("<base64>", line)` — compile an inline `rust {}` block.
pub const GFFI_COMPILE: u16 = 805;
/// FFI dispatch: call an exported inline-Rust symbol by name.
pub const GFFI_CALL: u16 = 806;
/// `--dap` per-statement line marker (only emitted by `compile_debug`).
pub const DBG_LINE: u16 = 807;
/// `make([]T, n)` / `make(map[K]V)` — allocate a zeroed slice or empty map.
pub const GMAKE: u16 = 810;
/// `[]T{...}` slice composite literal.
pub const GSLICE_LIT: u16 = 811;
/// `map[K]V{...}` map composite literal (stack: k0,v0,k1,v1,...).
pub const GMAP_LIT: u16 = 812;
/// `x[i]` read — slice index (bounds-checked) or map lookup.
pub const GINDEX_GET: u16 = 813;
/// `x[i] = v` write — slice index or map insert.
pub const GINDEX_SET: u16 = 814;
/// `len(x)` — slice/map/string length.
pub const GLEN: u16 = 815;
/// `cap(x)` — slice capacity (its length in this model).
pub const GCAP: u16 = 816;
/// `append(s, elems...)` — extend a slice, returning the (same) handle.
pub const GAPPEND: u16 = 817;
/// `delete(m, k)` — remove a map key.
pub const GDELETE: u16 = 818;
/// `T{...}` struct composite literal (stack: typeName, f0name,f0val, ...).
pub const GSTRUCT_NEW: u16 = 820;
/// `s.field` read on a struct.
pub const GFIELD_GET: u16 = 821;
/// `s.field = v` write on a struct.
pub const GFIELD_SET: u16 = 822;
/// Deep-copy a struct value (Go struct value semantics on assign/pass/return).
pub const GSTRUCT_COPY: u16 = 823;

/// Copy a struct value at a *bind* site — an assignment, an argument, a return,
/// a container store, a `range` binding. Unlike [`GSTRUCT_COPY`] it is the
/// identity on a handle marked `by_ref`, which is a Go **pointer**: Go copies a
/// struct value at every one of those sites and copies nothing at all through a
/// pointer, so `q := p` on a `*T` has to share what `p` points at.
pub const GSTRUCT_BIND: u16 = 857;

/// `os.writeFd(fd, s)` — the write behind `os.Stdout` / `os.Stderr`, which no
/// Go source in `goroot/` can name. Descriptor 2 is stderr; anything else is
/// stdout, because those are the only two files a synthesized `*os.File` is
/// ever built for.
pub const GWRITE_FD: u16 = 858;
/// The range keys of a value as a slice: `0..len` for a slice/string, the keys
/// for a map. Lets `for … range` iterate slices and maps uniformly.
pub const GRANGE_KEYS: u16 = 824;
/// The runtime type name of a struct value (drives interface method dispatch).
pub const GTYPEOF: u16 = 825;
/// Go 1.21 builtin `min(a, b, …)` — the smallest of the (ordered) arguments.
pub const GMIN: u16 = 826;
/// Go 1.21 builtin `max(a, b, …)` — the largest of the (ordered) arguments.
pub const GMAX: u16 = 827;
/// Build a closure: stack `[cap0, …, capN, lambda_id]`; pushes a closure value.
pub const GCLOSURE_NEW: u16 = 828;
/// Read a closure's captured value by index: stack `[closure, idx]`.
pub const GCLOSURE_GET: u16 = 829;
/// Read a closure's target subroutine name-index (for `Op::CallDynamic`).
pub const GCLOSURE_NAMEIDX: u16 = 819;
/// Push a new (empty) defer frame — one per invocation of a function that has
/// `defer` statements. (IDs 830–880 belong to the `stdlib` submodule.)
pub const GDEFER_ENTER: u16 = 881;
/// `[closure]` → push a deferred closure onto the current defer frame.
pub const GDEFER_PUSH: u16 = 882;
/// → `Int` count of deferred closures in the current frame (drives the drain).
pub const GDEFER_LEN: u16 = 883;
/// → pop and return the most-recently-deferred closure of the current frame.
pub const GDEFER_POP: u16 = 884;
/// Pop the (drained) defer frame.
pub const GDEFER_LEAVE: u16 = 885;
/// `[value]` → record a panic; execution unwinds to the current function's
/// defer drain (a deferred `recover()` may cancel it).
pub const GPANIC: u16 = 886;
/// → `Bool` whether a panic is currently propagating (drives unwind checks).
pub const GPANIC_ACTIVE: u16 = 887;
/// → the propagating panic value and clear it (nil if none) — Go's `recover()`.
pub const GRECOVER: u16 = 888;
/// If a panic is still propagating at program end, print it and exit non-zero.
pub const GPANIC_FINISH: u16 = 889;
/// Park the in-flight panic for the duration of one deferred call. Emitted by
/// the drain loop immediately before invoking a deferred closure.
///
/// Go runs a deferred function *normally* even while a panic is propagating: it
/// may call other functions, and only a `recover()` the deferred function makes
/// **itself** stops the panic. Leaving the panic flagged would make the unwind
/// check after each of those inner calls throw the deferred function out before
/// it reaches its `recover()`. Parking also records the call depth, which is
/// what makes the "directly" rule enforceable.
pub const GDEFER_PARK: u16 = 961;
/// Restore a parked panic after its deferred call returns, unless that call
/// recovered it (or panicked afresh, which supersedes).
pub const GDEFER_UNPARK: u16 = 962;

/// `[received, zero]` → `zero` when `received` is the drained-closed-channel
/// sentinel, else `received` itself. Every receive goes through this, so the
/// sentinel never escapes into a Go value.
pub const GCHAN_VAL: u16 = 963;
/// `[received]` → `Bool`: the `ok` of `v, ok := <-ch`. False exactly when the
/// receive found the channel closed and drained.
pub const GCHAN_OK: u16 = 964;
/// `[array, elemType]` → a copy of a fixed-size array value (Go array value
/// semantics on assign / pass / return / container read / container store /
/// channel send / `append` / `range`).
///
/// The element type is passed because an array and a slice are the same heap
/// object here, so only the written type says whether an element is itself a
/// value (copy) or a reference (share).
pub const GARRAY_COPY: u16 = 965;
/// `[container, item…]` → the same container with the items appended.
///
/// fusevm's `Op::CallBuiltin` carries its argument count in a `u8`, so one call
/// can take at most 255 stack values. A composite literal is not bounded by
/// that — `[]int{…}` with 256 elements is ordinary Go — so a literal longer
/// than one call can carry is built in chunks: the first chunk goes to
/// [`GSLICE_LIT`] / [`GMAP_LIT`] / [`GSTRUCT_NEW`] and each later chunk to this,
/// which dispatches on what it is handed (slice elements, `k,v` map pairs, or
/// `name,value` struct fields). Before this existed the count wrapped and the
/// literal silently lost every element past the first 255.
pub const GLIT_EXTEND: u16 = 966;
/// `[array, type]` → the same object, tagged as the fixed-size array type it
/// was written as, so `%T` and `%#v` can name it.
///
/// A `[N]T` and a `[]T` are the same `HostObj::Slice`, and the length is not
/// recoverable from the elements (`[3]int` and a 3-element `[]int` hold the
/// same thing), so the *written* type is stamped on the object where an array
/// value is born — a composite literal and a zero value. Every other array is a
/// copy of one of those, and [`GARRAY_COPY`] carries the tag across, so the tag
/// survives assignment, a parameter bind, a return and an `any` box alike.
pub const GARRAY_TAG: u16 = 967;
/// `[value, type]` → the value with every slice inside it stamped with its
/// *written* element type, for a `fmt` argument position only.
///
/// `fmt` reads `[]byte` as text under `%s`, `%q` and `%x` but distributes those
/// verbs over the elements of any other slice — `%q` of a `[]byte("ab")` is
/// `"ab"` and of a `[]int{97, 98}` is `['a' 'b']`. Nothing in the values tells
/// the two apart: both are integer elements. So the written type rides in from
/// the compiler, which walks it alongside the value and tags each slice node
/// (including the ones nested in an array, a slice or a map value). Like the
/// [`GF32_BOX`] / [`GU64_BOX`] width tags, the tagged value is a rebuilt copy
/// used for display only, so the program's own slice is untouched — and an
/// operand whose static type the compiler does not know is left untagged, where
/// the byte-slice guess from the element values stands in.
pub const GELEM_TAG: u16 = 968;
/// `[value, type]` → the value tagged with the defined type it was written as,
/// for a `fmt` argument position only.
///
/// `type Weekday int` declares a type distinct from `int` that is represented
/// exactly like one, so nothing at run time separates a `Weekday` from the `int`
/// holding the same number — but `%T` prints `main.Weekday` and `%#v` writes the
/// name too. The compiler knows the static type at the call site and tags the
/// operand here, the same way the `float32` and `uint64` width tags ride in.
/// Every verb but `%T` and `%#v` sees straight through it to the value.
pub const GNAMED_BOX: u16 = 969;
/// `[a, b, ne]` → Go's `==` (or `!=` when `ne` is 1) on two **interface**
/// operands: equal when the dynamic types match *and* the values do.
///
/// Go decides interface equality by dynamic type before value, so an `int` and a
/// `float64` are never equal however the numbers line up, and neither are two
/// struct types with identical fields. Nothing else in valid Go puts two
/// different types under one operator — arithmetic and ordered comparison on
/// mismatched types are compile errors, and an interface is unordered — so this
/// is the whole of the rule's reach.
///
/// It cannot be left to the native op or to [`numeric_hook`]. fusevm answers an
/// `Int`/`Float` pair natively by promoting the integer, so the pair never
/// reaches the frontend at all; and the pairs that *do* reach the hook land on
/// its rendered-string fallback, where `any(1) == any("1")` compares `"1"` with
/// `"1"` and says yes. So the compiler routes a comparison with an
/// interface-typed operand here instead of emitting `Op::NumEq`/`Op::StrEq`.
///
/// The dynamic type is `go_type_name` — the same function `%T` prints, which
/// already separates a struct type from another with the same fields, and a
/// typed nil (`HostObj::Nil`) from an untyped one. It does **not** separate two
/// integer widths: `int`, `int64` and `uint` are all `Value::Int` and all name
/// `int`, so `any(1) == any(int64(1))` is still wrong (BUGS.md).
pub const GIFACE_EQ: u16 = 970;
/// `[slice]` → the slice marked for expansion into a variadic builtin's operand
/// list, for `fmt.Printf(f, args...)` and its siblings.
///
/// A `fmt` builtin is compiled as `CallBuiltin(id, argc)` with `argc` fixed at
/// the call site, so a spread — whose length is only known at run time — cannot
/// be counted there. The compiler wraps the spread operand in this marker
/// instead and `pop_args` expands it back into the operand list, which is the
/// one place every `fmt` entry point already funnels through. Without it
/// `fmt.Sprintf(f, a...)` formats the *slice* as a single operand, so a wrapper
/// like `func log(f string, a ...any) { fmt.Printf(f, a...) }` silently prints
/// `[1 2]-%!d(MISSING)` where Go prints `1-2`.
pub const GSPREAD: u16 = 971;
/// `[value]` → a heap cell boxing `value`, for a variable captured by reference.
pub const GCELL_NEW: u16 = 890;
/// `[cell]` → read a boxed variable's current value.
pub const GCELL_GET: u16 = 891;
/// `[value, cell]` → store into a boxed variable (shared with its closures).
pub const GCELL_SET: u16 = 892;
/// `fmt.Sprintf(format, …)` — format and return the string (no output).
pub const GSPRINTF: u16 = 893;
/// `fmt.Sprint(…)` — concatenate operands (Go spacing) and return the string.
pub const GSPRINT: u16 = 894;
/// `fmt.Sprintln(…)` — like `Sprint` with spaces + trailing newline.
pub const GSPRINTLN: u16 = 895;
/// `s[low:high]` — a sub-slice/substring: stack `[recv, low, high]`.
pub const GSLICE_SUB: u16 = 896;
/// Enable recoverable runtime faults (emitted at program start when `recover` is
/// used).
pub const GSET_PANIC_MODE: u16 = 897;
/// Integer `a / b` with a divide-by-zero panic: stack `[a, b]`.
pub const GIDIV: u16 = 898;
/// Integer `a % b` with a divide-by-zero panic: stack `[a, b]`.
pub const GIMOD: u16 = 899;
/// A Go type conversion `T(v)`: stack `[value, typeNameConstIdx]` — the compiler
/// pushes the value then a constant naming the target type.
pub const GCONV: u16 = 900;
/// `[value]` → the runtime type name of a value (`int`/`string`/`bool`/`float64`,
/// a struct type name, `[]`/`map`/`func`, or `nil`) for type switches/assertions.
pub const GTYPETAG: u16 = 901;
/// `[value, "tag"]` → a single-result type assertion `x.(T)`: `value` if its type
/// matches, else a recoverable panic (`interface conversion`).
pub const GASSERT: u16 = 902;
/// `[iter, key]` → the loop value for `for key := range iter`. A string decodes
/// the rune (code point) starting at byte offset `key` (Go ranges strings by
/// rune); a slice/map indexes normally.
pub const GRANGE_VAL: u16 = 903;
/// `[dst, src]` → `copy(dst, src)`: copy `min(len(dst), len(src))` elements from
/// `src` (a slice, or a string for `copy([]byte, s)`) into `dst`, returning the
/// count. Writes through `dst`'s backing so a sub-slice destination aliases.
pub const GCOPY: u16 = 904;
/// `[map, key]` → the comma-ok map lookup `v, ok := m[key]` as a 2-element slice
/// `[value, present]`: the value (or zero) and whether the key was present.
pub const GMAP_GET2: u16 = 905;
/// `[base, xs]` → `append(base, xs...)`: a new slice of `base`'s elements
/// followed by every element of the spread slice `xs`.
pub const GAPPEND_SPREAD: u16 = 906;

/// `[a, b]` → `a / b` chosen at run time from the operand representations.
///
/// Go's `/` is integer division when both operands are integers and float
/// division otherwise, and the choice is made statically by the type checker.
/// go-rs's compiler infers a numeric category for most expressions, but some
/// forms (indexing a slice/map, a method result, an `interface{}` value) come
/// back `NumType::Unknown`; emitting the float `Op::Div` for those made
/// `xs[0] / 2` yield `3.5` where Go yields `3`. This builtin resolves the same
/// rule against the actual values, so an untyped-at-compile-time integer pair
/// still truncates toward zero (and panics on a zero divisor) like Go.
///
/// Numbered above the `stdlib`/`math` block (which runs to 921) — 907 collides
/// with `math.Sin`.
pub const GDYNDIV: u16 = 950;

/// `[value]` → the same handle, marked as a Go pointer (`&T{…}` / `new(T)`) so
/// `==` compares it by address rather than field by field.
pub const GPTR_MARK: u16 = 955;

/// `&x` on an existing variable — allocate a [`HostObj::Ptr`] addressing the
/// value's handle. A value that is not a heap object has no address to take, so
/// it passes through unchanged (see BUGS.md on `&` of a scalar).
pub const GPTR_TO: u16 = 859;

/// `*p = v` — overwrite the struct `p` points at, in place, so every other
/// pointer to it and the variable itself all see the new value.
pub const GDEREF_SET: u16 = 990;
/// `[typeName, "m1,m2,…"]` — record a concrete type's method set. Emitted once
/// per method-bearing type in the program prologue, and only when the program
/// tests a value against an interface's method set.
pub const GREG_METHODS: u16 = 952;
/// `[value, "m1,m2,…"]` → `Bool`: whether the value's dynamic type implements
/// every named method — Go's interface satisfaction, which is what a type
/// assertion or type-switch case against an interface type tests.
pub const GIFACE_OK: u16 = 953;
/// `[value, "m1,m2,…", "<interface display>"]` → the value when its dynamic type
/// implements the method set, else a recoverable `interface conversion` panic
/// naming the first missing method (Go's message).
pub const GASSERT_IFACE: u16 = 954;

/// `[a, b]` → IEEE float `a / b`: `±Inf` for a nonzero numerator over zero and
/// `NaN` for `0.0 / 0.0`, as Go's float division does.
///
/// fusevm's native `Op::Div` yields `Undef` for a zero divisor (it has no
/// float-specific division), so `1.0 / z` printed `<nil>`. The compiler still
/// emits `Op::Div` — which the JIT and AOT keep in registers — whenever the
/// divisor is a provably-nonzero constant, and falls back here otherwise.
pub const GFDIV: u16 = 951;

/// `[value]` → the same number tagged `float32` (`HostObj::F32`) for `fmt`, or
/// a fresh slice of tagged elements when the operand is a `[]float32`. Emitted
/// only at `fmt` argument positions.
pub const GF32_BOX: u16 = 957;

/// `[lhs, rhs, op]` → one arithmetic operation performed at 32-bit width, `op`
/// being an [`f32_op`] code.
///
/// Rounding an `f64` result to `f32` afterwards is a *different* computation:
/// the double rounding can land a ulp away. `float32(16777217) * float32(0.2)`
/// is `3.3554432e+06` rounded once and `3.3554434e+06` rounded twice. So a
/// `float32` operation is done in `f32` throughout, which is why it costs a
/// builtin rather than a native op.
pub const GF32_ARITH: u16 = 958;

/// The [`GF32_ARITH`] operator codes, shared with the compiler.
pub mod f32_op {
    pub const ADD: i64 = 0;
    pub const SUB: i64 = 1;
    pub const MUL: i64 = 2;
    pub const DIV: i64 = 3;
}

/// `[value]` → the same number tagged `uint64` (`HostObj::U64`) for `fmt`, so it
/// renders as an unsigned 64-bit integer. Emitted only at `fmt` argument
/// positions, and — like [`GF32_BOX`] — element-wise through a slice or map.
///
/// `uint64` / `uint` / `uintptr` share `Value::Int`'s 64-bit two's-complement
/// representation, so `+ - * << & | ^ &^` need nothing: the bit pattern is
/// already Go's answer. Only the operations that *read* the sign differ, and
/// display is one of them — `uint64(0) - 1` holds `-1i64` and must print
/// `18446744073709551615`.
pub const GU64_BOX: u16 = 959;

/// `[lhs, rhs, op]` → one operation performed at unsigned 64-bit width, `op`
/// being a [`u64_op`] code: the operations whose result depends on the sign bit
/// (`/`, `%`, `>>`, the four ordered comparisons, and the conversion to
/// `float64`).
pub const GU64_ARITH: u16 = 960;

/// The [`GU64_ARITH`] operator codes, shared with the compiler.
pub mod u64_op {
    pub const DIV: i64 = 0;
    pub const MOD: i64 = 1;
    pub const SHR: i64 = 2;
    pub const LT: i64 = 3;
    pub const LE: i64 = 4;
    pub const GT: i64 = 5;
    pub const GE: i64 = 6;
    /// Unary: `float64(u)` — the unsigned value widened, not the signed one.
    pub const TOFLOAT: i64 = 7;
}

/// `["[]T" | "map[K]V"]` → the typed nil that is that type's zero value
/// (`HostObj::Nil`). Emitted wherever the compiler needs a slice's or map's
/// zero value, so `fmt` can print `[]` / `map[]` rather than `<nil>`.
pub const GNIL_OF: u16 = 956;

/// Register every go-rs builtin on a VM. This is the single install choke point
/// later waves (slices, maps, `strings`/`strconv`, structs) grow into.
pub fn install(vm: &mut VM) {
    vm.register_builtin(GPRINTLN, b_println);
    vm.register_builtin(GPRINT, b_print);
    vm.register_builtin(GPRINTF, b_printf);
    vm.register_builtin(GEPRINTLN, b_eprintln);
    vm.register_builtin(GEPRINT, b_eprint);
    vm.register_builtin(GFFI_COMPILE, b_ffi_compile);
    vm.register_builtin(GFFI_CALL, b_ffi_call);
    vm.register_builtin(GMAKE, b_make);
    vm.register_builtin(GSLICE_LIT, b_slice_lit);
    vm.register_builtin(GMAP_LIT, b_map_lit);
    vm.register_builtin(GINDEX_GET, b_index_get);
    vm.register_builtin(GINDEX_SET, b_index_set);
    vm.register_builtin(GLEN, b_len);
    vm.register_builtin(GCAP, b_cap);
    vm.register_builtin(GAPPEND, b_append);
    vm.register_builtin(GDELETE, b_delete);
    vm.register_builtin(GSTRUCT_NEW, b_struct_new);
    vm.register_builtin(GFIELD_GET, b_field_get);
    vm.register_builtin(GFIELD_SET, b_field_set);
    vm.register_builtin(GSTRUCT_COPY, b_struct_copy);
    vm.register_builtin(GSTRUCT_BIND, b_struct_bind);
    vm.register_builtin(GWRITE_FD, b_write_fd);
    vm.register_builtin(GARRAY_COPY, b_array_copy);
    vm.register_builtin(GLIT_EXTEND, b_lit_extend);
    vm.register_builtin(GARRAY_TAG, b_array_tag);
    vm.register_builtin(GELEM_TAG, b_elem_tag);
    vm.register_builtin(GNAMED_BOX, b_named_box);
    vm.register_builtin(GSPREAD, b_spread);
    vm.register_builtin(GIFACE_EQ, b_iface_eq);
    vm.register_builtin(GRANGE_KEYS, b_range_keys);
    vm.register_builtin(GTYPEOF, b_typeof);
    vm.register_builtin(GMIN, b_min);
    vm.register_builtin(GMAX, b_max);
    vm.register_builtin(GCLOSURE_NEW, b_closure_new);
    vm.register_builtin(GCLOSURE_GET, b_closure_get);
    vm.register_builtin(GCLOSURE_NAMEIDX, b_closure_nameidx);
    vm.register_builtin(GDEFER_ENTER, b_defer_enter);
    vm.register_builtin(GDEFER_PUSH, b_defer_push);
    vm.register_builtin(GDEFER_LEN, b_defer_len);
    vm.register_builtin(GDEFER_POP, b_defer_pop);
    vm.register_builtin(GDEFER_LEAVE, b_defer_leave);
    vm.register_builtin(GDEFER_PARK, b_defer_park);
    vm.register_builtin(GDEFER_UNPARK, b_defer_unpark);
    vm.register_builtin(GCHAN_VAL, b_chan_val);
    vm.register_builtin(GCHAN_OK, b_chan_ok);
    vm.register_builtin(GPANIC, b_panic);
    vm.register_builtin(GPANIC_ACTIVE, b_panic_active);
    vm.register_builtin(GRECOVER, b_recover);
    vm.register_builtin(GPANIC_FINISH, b_panic_finish);
    vm.register_builtin(GCELL_NEW, b_cell_new);
    vm.register_builtin(GCELL_GET, b_cell_get);
    vm.register_builtin(GCELL_SET, b_cell_set);
    vm.register_builtin(GSPRINTF, b_sprintf);
    vm.register_builtin(GSPRINT, b_sprint);
    vm.register_builtin(GSPRINTLN, b_sprintln);
    vm.register_builtin(GSLICE_SUB, b_slice_sub);
    vm.register_builtin(GSET_PANIC_MODE, b_set_panic_mode);
    vm.register_builtin(GIDIV, b_idiv);
    vm.register_builtin(GIMOD, b_imod);
    vm.register_builtin(GCONV, b_conv);
    vm.register_builtin(GTYPETAG, b_typetag);
    vm.register_builtin(GASSERT, b_assert);
    vm.register_builtin(GRANGE_VAL, b_range_val);
    vm.register_builtin(GCOPY, b_copy);
    vm.register_builtin(GMAP_GET2, b_map_get2);
    vm.register_builtin(GAPPEND_SPREAD, b_append_spread);
    vm.register_builtin(GDYNDIV, b_dyndiv);
    vm.register_builtin(GFDIV, b_fdiv);
    vm.register_builtin(GPTR_MARK, b_ptr_mark);
    vm.register_builtin(GPTR_TO, b_ptr_to);
    vm.register_builtin(GDEREF_SET, b_deref_set);
    vm.register_builtin(GNIL_OF, b_nil_of);
    vm.register_builtin(GF32_BOX, b_f32_box);
    vm.register_builtin(GF32_ARITH, b_f32_arith);
    vm.register_builtin(GU64_BOX, b_u64_box);
    vm.register_builtin(GU64_ARITH, b_u64_arith);
    vm.register_builtin(GREG_METHODS, b_reg_methods);
    vm.register_builtin(GIFACE_OK, b_iface_ok);
    vm.register_builtin(GASSERT_IFACE, b_assert_iface);
    stdlib::install(vm);
}

thread_local! {
    /// Every concrete type's method set, keyed by the tag [`type_tag_of`]
    /// produces. Populated by [`GREG_METHODS`] in the program prologue; the
    /// interface tests read it to decide satisfaction.
    static METHOD_SETS: RefCell<std::collections::HashMap<String, Vec<String>>> =
        RefCell::new(std::collections::HashMap::new());
}

/// `[typeName, "m1,m2,…"]` — record a concrete type's method set.
fn b_reg_methods(vm: &mut VM, argc: u8) -> Value {
    let args = pop_args(vm, argc);
    let ty = args.first().map(go_str).unwrap_or_default();
    let methods: Vec<String> = args
        .get(1)
        .map(go_str)
        .unwrap_or_default()
        .split(',')
        .filter(|m| !m.is_empty())
        .map(str::to_string)
        .collect();
    METHOD_SETS.with(|m| m.borrow_mut().insert(ty, methods));
    Value::Undef
}

/// The first method of `want` that `v`'s dynamic type does not have, or `None`
/// when it implements them all. A nil value has no methods, so it satisfies only
/// the empty interface — as in Go, where a nil interface fails every assertion.
fn missing_method(v: &Value, want: &str) -> Option<String> {
    let tag = type_tag_of(v);
    METHOD_SETS.with(|m| {
        let m = m.borrow();
        let have = m.get(&tag);
        want.split(',')
            .filter(|w| !w.is_empty())
            .find(|w| !have.is_some_and(|h| h.iter().any(|x| x == w)))
            .map(str::to_string)
    })
}

/// `[value, "m1,m2,…"]` → whether the value's dynamic type implements them all.
fn b_iface_ok(vm: &mut VM, argc: u8) -> Value {
    let args = pop_args(vm, argc);
    let v = args.first().cloned().unwrap_or(Value::Undef);
    let want = args.get(1).map(go_str).unwrap_or_default();
    Value::bool(missing_method(&v, &want).is_none())
}

/// `[value, "m1,m2,…", display]` → the value, or an `interface conversion` panic.
fn b_assert_iface(vm: &mut VM, argc: u8) -> Value {
    let args = pop_args(vm, argc);
    let v = args.first().cloned().unwrap_or(Value::Undef);
    let want = args.get(1).map(go_str).unwrap_or_default();
    let display = args.get(2).map(go_str).unwrap_or_default();
    match missing_method(&v, &want) {
        None => v,
        Some(m) => {
            // Go's `TypeAssertionError` is not one of the `runtime error: `
            // messages — its text starts at `interface conversion`.
            let msg = if matches!(v, Value::Undef) {
                // A nil interface holds no dynamic type, so Go words it as the
                // absence of one rather than naming a missing method.
                format!("interface conversion: interface is nil, not {display}")
            } else {
                // `missing_method` answers with the `name/arity:results` key the
                // method table uses; Go's panic names only the method.
                let name = m.split('/').next().unwrap_or(&m);
                format!(
                    "interface conversion: {} is not {display}: missing method {name}",
                    go_type_name(&v)
                )
            };
            plain_panic(vm, msg);
            Value::Undef
        }
    }
}

/// `[value, "tag"]` → a single-result type assertion. Returns the value when its
/// runtime type matches; otherwise a recoverable panic. `tag` empty (an
/// interface type like `any`) always matches.
fn b_assert(vm: &mut VM, argc: u8) -> Value {
    let args = pop_args(vm, argc);
    let v = args.first().cloned().unwrap_or(Value::Undef);
    let want = args.get(1).map(go_str).unwrap_or_default();
    let got = type_tag_of(&v);
    if want.is_empty() || want == got {
        v
    } else {
        // Not a `runtime error: ` message: Go's `TypeAssertionError` text starts
        // at `interface conversion`.
        plain_panic(
            vm,
            format!("interface conversion: interface {{}} is {got}, not {want}"),
        );
        Value::Undef
    }
}

/// The runtime type tag of a value (shared by [`b_typetag`] and [`b_assert`]).
fn type_tag_of(v: &Value) -> String {
    match v {
        Value::Int(_) => "int".to_string(),
        Value::Float(_) => "float64".to_string(),
        Value::Str(_) => "string".to_string(),
        Value::Bool(_) => "bool".to_string(),
        Value::Obj(id) => HEAP.with(|h| match h.borrow().get(follow(*id) as usize) {
            Some(HostObj::Struct { type_name, .. }) => type_name.clone(),
            Some(HostObj::Slice { .. }) | Some(HostObj::SliceView { .. }) => "[]".to_string(),
            Some(HostObj::Map(_)) => "map".to_string(),
            // A typed nil keeps its type's tag, so a type switch on a nil slice
            // still picks the `[]T` case rather than the nil default.
            Some(HostObj::Nil { kind, .. }) => match kind {
                NilKind::Slice => "[]".to_string(),
                NilKind::Map => "map".to_string(),
            },
            Some(HostObj::Closure { .. }) => "func".to_string(),
            _ => "nil".to_string(),
        }),
        _ => "nil".to_string(),
    }
}

/// `[value]` → the runtime type name used by type switches and assertions.
fn b_typetag(vm: &mut VM, argc: u8) -> Value {
    let args = pop_args(vm, argc);
    Value::str(type_tag_of(args.first().unwrap_or(&Value::Undef)))
}

/// `[value, "type"]` → a Go type conversion `T(value)`. Integer types truncate
/// and wrap to their width; float types widen/narrow; `string(n)` is the UTF-8
/// encoding of code point `n`, and `string([]byte/[]rune)` joins the elements.
fn b_conv(vm: &mut VM, argc: u8) -> Value {
    let args = pop_args(vm, argc);
    let v = args.first().cloned().unwrap_or(Value::Undef);
    let ty = args.get(1).map(go_str).unwrap_or_default();
    match ty.as_str() {
        "int" | "int64" | "uint" | "uint64" | "uintptr" => Value::Int(to_int_wide(&v)),
        "int8" => Value::Int(to_int_wide(&v) as i8 as i64),
        "int16" => Value::Int(to_int_wide(&v) as i16 as i64),
        "int32" | "rune" => Value::Int(to_int_wide(&v) as i32 as i64),
        "uint8" | "byte" => Value::Int(to_int_wide(&v) as u8 as i64),
        "uint16" => Value::Int(to_int_wide(&v) as u16 as i64),
        "uint32" => Value::Int(to_int_wide(&v) as u32 as i64),
        "float32" => Value::Float(v.to_float() as f32 as f64),
        "float64" => Value::Float(v.to_float()),
        "bool" => Value::bool(v.is_truthy()),
        "string" => conv_string(&v),
        // `[]byte(s)` — the string's UTF-8 bytes as a slice of ints.
        "[]byte" => match &v {
            Value::Str(s) => {
                let elems = s.bytes().map(|b| Value::Int(b as i64)).collect();
                Value::Obj(heap_alloc(HostObj::slice(elems)))
            }
            _ => v,
        },
        // `[]rune(s)` — the string's Unicode code points as a slice of ints.
        "[]rune" => match &v {
            Value::Str(s) => {
                let elems = s.chars().map(|c| Value::Int(c as i64)).collect();
                Value::Obj(heap_alloc(HostObj::slice(elems)))
            }
            _ => v,
        },
        // An unknown/named type conversion is the identity (dynamic value model).
        _ => v,
    }
}

/// The integer value of `v` for a conversion (a float truncates toward zero).
fn to_int_wide(v: &Value) -> i64 {
    match v {
        Value::Float(f) => *f as i64,
        other => other.to_int(),
    }
}

/// `string(v)`: a code point becomes its UTF-8 char; a `[]byte`/`[]rune` slice
/// joins its elements; a string is unchanged.
fn conv_string(v: &Value) -> Value {
    match v {
        Value::Str(_) => v.clone(),
        Value::Int(n) => {
            let s = char::from_u32(*n as u32)
                .map(|c| c.to_string())
                .unwrap_or_else(|| "\u{FFFD}".to_string());
            Value::str(s)
        }
        Value::Obj(id) => {
            if let Some((_, _, len)) = slice_backing(*id) {
                let elems: Vec<i64> = (0..len)
                    .filter_map(|i| slice_get(*id, i))
                    .map(|e| e.to_int())
                    .collect();
                // go-rs erases the slice element type, so `string(slice)` must
                // disambiguate `[]byte` (UTF-8 bytes to decode) from `[]rune`
                // (code points to join). If every element is a byte and they form
                // a valid multibyte UTF-8 sequence, decode as bytes; otherwise
                // join as code points. This makes `string([]byte(s)) == s` for a
                // real string while a lone code point ≥ 128 (not valid standalone
                // UTF-8) still joins as a rune.
                let all_bytes = elems.iter().all(|&e| (0..=255).contains(&e));
                let has_high = elems.iter().any(|&e| e >= 128);
                if all_bytes && has_high {
                    let bytes: Vec<u8> = elems.iter().map(|&e| e as u8).collect();
                    if let Ok(s) = std::str::from_utf8(&bytes) {
                        return Value::str(s.to_string());
                    }
                }
                let s: String = elems
                    .iter()
                    .filter_map(|&e| char::from_u32(e as u32))
                    .collect();
                Value::str(s)
            } else {
                v.clone()
            }
        }
        _ => Value::str(go_str(v)),
    }
}

/// `[a, b]` → `a / b` (integer), panicking on divide-by-zero like Go.
fn b_idiv(vm: &mut VM, argc: u8) -> Value {
    let args = pop_args(vm, argc);
    let a = args.first().map(Value::to_int).unwrap_or(0);
    let b = args.get(1).map(Value::to_int).unwrap_or(0);
    if b == 0 {
        runtime_panic(vm, "integer divide by zero");
        return Value::Int(0);
    }
    Value::Int(a.wrapping_div(b))
}

/// `[a, b]` → `a / b`, picking Go's integer or float division from the operand
/// representations. Both integers ⇒ truncating integer division (panicking on a
/// zero divisor); anything else ⇒ float division, where `x / 0.0` is ±Inf.
fn b_dyndiv(vm: &mut VM, argc: u8) -> Value {
    let args = pop_args(vm, argc);
    let a = args.first().cloned().unwrap_or(Value::Undef);
    let b = args.get(1).cloned().unwrap_or(Value::Undef);
    match (&a, &b) {
        (Value::Int(x), Value::Int(y)) => {
            if *y == 0 {
                runtime_panic(vm, "integer divide by zero");
                return Value::Int(0);
            }
            Value::Int(x.wrapping_div(*y))
        }
        _ => Value::Float(a.to_float() / b.to_float()),
    }
}

/// `[a, b]` → IEEE float `a / b` (`±Inf` / `NaN` on a zero divisor).
fn b_fdiv(vm: &mut VM, argc: u8) -> Value {
    let args = pop_args(vm, argc);
    let a = args.first().map(Value::to_float).unwrap_or(0.0);
    let b = args.get(1).map(Value::to_float).unwrap_or(0.0);
    Value::Float(a / b)
}

/// `[a, b]` → `a % b` (integer), panicking on divide-by-zero like Go.
fn b_imod(vm: &mut VM, argc: u8) -> Value {
    let args = pop_args(vm, argc);
    let a = args.first().map(Value::to_int).unwrap_or(0);
    let b = args.get(1).map(Value::to_int).unwrap_or(0);
    if b == 0 {
        runtime_panic(vm, "integer divide by zero");
        return Value::Int(0);
    }
    Value::Int(a.wrapping_rem(b))
}

/// `s[low:high:max]` on a slice, or `s[low:high]` on a slice or string: stack
/// `[recv, low, high, max]`. Returns a view sharing the parent's backing array
/// (or a substring). A bound of `-1` means "omitted": `0` / `len` / `cap`.
fn b_slice_sub(vm: &mut VM, argc: u8) -> Value {
    let args = pop_args(vm, argc);
    let recv = args.first().cloned().unwrap_or(Value::Undef);
    let lo_raw = args.get(1).map(Value::to_int).unwrap_or(-1);
    let hi_raw = args.get(2).map(Value::to_int).unwrap_or(-1);
    let max_raw = args.get(3).map(Value::to_int).unwrap_or(-1);
    match recv {
        // A sub-slice shares the parent's backing array (so element writes are
        // visible both ways), matching Go — collapse a view-of-a-view to the
        // original backing.
        Value::Obj(id) => {
            let Some((backing, base, len)) = slice_backing(id) else {
                return Value::Undef;
            };
            // Go bounds a re-slice by capacity, not length: `s[:cap(s)]` is
            // legal and exposes the backing array's spare room, so an element an
            // append wrote past `len` is reachable through `s[0:len+1]`.
            let cap = slice_cap(id).unwrap_or(len) as i64;
            let len = len as i64;
            let lo = if lo_raw < 0 { 0 } else { lo_raw }.clamp(0, cap) as usize;
            let hi = if hi_raw < 0 { len } else { hi_raw }.clamp(0, cap) as usize;
            let hi = hi.max(lo);
            // The three-index form `s[lo:hi:max]` gives the result capacity
            // `max - lo`; omitted, it keeps the rest of the parent's capacity.
            let mx = if max_raw < 0 { cap } else { max_raw }.clamp(0, cap) as usize;
            let mx = mx.max(hi);
            Value::Obj(heap_alloc(HostObj::SliceView {
                backing,
                offset: base + lo,
                len: hi - lo,
                cap: mx - lo,
            }))
        }
        Value::Str(s) => {
            // Byte-indexed substring, matching Go's string slicing.
            let bytes = s.as_bytes();
            let len = bytes.len() as i64;
            let lo = if lo_raw < 0 { 0 } else { lo_raw }.clamp(0, len) as usize;
            let hi = if hi_raw < 0 { len } else { hi_raw }.clamp(0, len) as usize;
            let slice = bytes.get(lo..hi.max(lo)).unwrap_or(&[]);
            Value::str(String::from_utf8_lossy(slice).into_owned())
        }
        _ => Value::Undef,
    }
}

/// `fmt.Sprintf(format, …)` — the formatted string (no output).
fn b_sprintf(vm: &mut VM, argc: u8) -> Value {
    let args = pop_args(vm, argc);
    Value::str(sprintf(&args))
}

/// `fmt.Sprint(…)` — operands concatenated with Go's between-non-strings spacing.
fn b_sprint(vm: &mut VM, argc: u8) -> Value {
    let args = pop_args(vm, argc);
    Value::str(go_print_spacing(&args))
}

/// `fmt.Sprintln(…)` — operands space-separated with a trailing newline.
fn b_sprintln(vm: &mut VM, argc: u8) -> Value {
    let args = pop_args(vm, argc);
    let text: Vec<String> = args.iter().map(go_str).collect();
    Value::str(format!("{}\n", text.join(" ")))
}

/// `[value]` → a fresh heap cell boxing `value` (a by-reference-captured var).
fn b_cell_new(vm: &mut VM, argc: u8) -> Value {
    let args = pop_args(vm, argc);
    let v = args.into_iter().next().unwrap_or(Value::Undef);
    Value::Obj(heap_alloc(HostObj::Cell(v)))
}

/// `[cell]` → the boxed value.
fn b_cell_get(vm: &mut VM, argc: u8) -> Value {
    let args = pop_args(vm, argc);
    match args.first() {
        Some(Value::Obj(id)) => HEAP.with(|h| match h.borrow().get(*id as usize) {
            Some(HostObj::Cell(v)) => v.clone(),
            _ => Value::Undef,
        }),
        _ => Value::Undef,
    }
}

/// `[value, cell]` → store `value` into the shared cell (writes reach closures).
fn b_cell_set(vm: &mut VM, argc: u8) -> Value {
    let mut args = pop_args(vm, argc);
    // Stack order is [value, cell]; `cell` is the top (last) argument.
    let cell = args.pop().unwrap_or(Value::Undef);
    let value = args.pop().unwrap_or(Value::Undef);
    if let Value::Obj(id) = cell {
        HEAP.with(|h| {
            if let Some(HostObj::Cell(slot)) = h.borrow_mut().get_mut(id as usize) {
                *slot = value;
            }
        });
    }
    Value::Undef
}

/// `[value]` → begin a panic: store the value; unwinding is driven by the
/// compiler (jump to the defer drain, propagate past calls while active).
fn b_panic(vm: &mut VM, argc: u8) -> Value {
    let args = pop_args(vm, argc);
    let v = args.into_iter().next().unwrap_or(Value::Undef);
    PANIC.with(|p| *p.borrow_mut() = Some(v));
    Value::Undef
}

/// → whether a panic is propagating (an unwind check after each call).
fn b_panic_active(_vm: &mut VM, _argc: u8) -> Value {
    Value::bool(PANIC.with(|p| p.borrow().is_some()))
}

/// Go's `recover()`: return the propagating panic value and stop the panic, or
/// nil when nothing is panicking.
fn b_recover(vm: &mut VM, _argc: u8) -> Value {
    // Go: `recover()` is only effective when the function calling it was itself
    // invoked directly as a deferred call of a panicking frame. The drain loop
    // parks the panic at the draining frame's depth, so the deferred function's
    // own body runs exactly [`DEFERRED_BODY_DEPTH`] frames deeper; anything it
    // calls in turn is deeper still and gets nil.
    PARKED.with(|p| {
        let mut parked = p.borrow_mut();
        let Some(top) = parked.last_mut() else {
            return Value::Undef;
        };
        if top.recovered || vm.frames.len() != top.depth + DEFERRED_BODY_DEPTH {
            return Value::Undef;
        }
        match top.val.take() {
            Some(v) => {
                top.recovered = true;
                v
            }
            None => Value::Undef,
        }
    })
}

/// [`GDEFER_PARK`] — take the in-flight panic out of circulation for one
/// deferred call, remembering the depth its `recover()` must be called from.
fn b_defer_park(vm: &mut VM, _argc: u8) -> Value {
    let val = PANIC.with(|p| p.borrow_mut().take());
    PARKED.with(|p| {
        p.borrow_mut().push(Park {
            val,
            depth: vm.frames.len(),
            recovered: false,
        })
    });
    Value::Undef
}

/// The value a receive on a **closed and drained** channel yields, installed on
/// the scheduler with `Scheduler::with_recv_zero`.
///
/// `Scheduler::recv` returns this — and only this — for the one case Go reports
/// as `ok == false`, so it is the exact signal `v, ok := <-ch` and
/// `for v := range ch` need, decided atomically inside the scheduler rather than
/// by a racy "is it closed *and* empty?" check afterwards. It is a fresh heap
/// handle, and `Value::Obj` is identity-comparable, so no Go value can collide
/// with it — unlike the default `Value::Int(0)`, which a channel carrying a real
/// `0` produces all the time.
pub fn chan_closed_sentinel() -> Value {
    CHAN_CLOSED.with(|c| {
        c.borrow_mut()
            .get_or_insert_with(|| Value::Obj(heap_alloc(HostObj::ChanClosed)))
            .clone()
    })
}

/// Whether `v` is the drained-closed-channel sentinel.
fn is_chan_closed(v: &Value) -> bool {
    let Value::Obj(id) = v else { return false };
    HEAP.with(|h| matches!(h.borrow().get(*id as usize), Some(HostObj::ChanClosed)))
}

/// [`GCHAN_VAL`] — a receive's value: the element type's zero when the channel
/// was closed and drained, else what the channel delivered.
fn b_chan_val(vm: &mut VM, argc: u8) -> Value {
    let args = pop_args(vm, argc);
    let v = args.first().cloned().unwrap_or(Value::Undef);
    if is_chan_closed(&v) {
        return args.get(1).cloned().unwrap_or(Value::Undef);
    }
    v
}

/// [`GCHAN_OK`] — a receive's `ok`.
fn b_chan_ok(vm: &mut VM, argc: u8) -> Value {
    let args = pop_args(vm, argc);
    Value::bool(!args.first().map(is_chan_closed).unwrap_or(false))
}

/// [`GDEFER_UNPARK`] — the deferred call has returned. Resume the panic it was
/// running under unless that call recovered it, or unless the call raised a
/// panic of its own, which Go lets supersede the one it was deferred for.
fn b_defer_unpark(_vm: &mut VM, _argc: u8) -> Value {
    let Some(park) = PARKED.with(|p| p.borrow_mut().pop()) else {
        return Value::Undef;
    };
    if let Some(v) = park.val {
        PANIC.with(|p| {
            let mut slot = p.borrow_mut();
            if slot.is_none() {
                *slot = Some(v);
            }
        });
    }
    Value::Undef
}

/// At program end, a still-propagating panic is fatal: print it like Go's first
/// line (`panic: <value>`) on stderr and exit with status 2. (The goroutine
/// stack trace Go prints below that line is not reproduced.)
fn b_panic_finish(_vm: &mut VM, _argc: u8) -> Value {
    if let Some(v) = PANIC.with(|p| p.borrow_mut().take()) {
        eprintln!("panic: {}", go_str(&v));
        std::process::exit(2);
    }
    Value::Undef
}

/// Push a fresh defer frame at the start of a function that has `defer`s.
fn b_defer_enter(_vm: &mut VM, _argc: u8) -> Value {
    DEFERS.with(|d| d.borrow_mut().push(Vec::new()));
    Value::Undef
}

/// `[closure]` → record a deferred closure in the current frame (LIFO order).
fn b_defer_push(vm: &mut VM, argc: u8) -> Value {
    let args = pop_args(vm, argc);
    if let Some(c) = args.into_iter().next() {
        DEFERS.with(|d| {
            if let Some(frame) = d.borrow_mut().last_mut() {
                frame.push(c);
            }
        });
    }
    Value::Undef
}

/// → the number of deferred closures still to run in the current frame.
fn b_defer_len(_vm: &mut VM, _argc: u8) -> Value {
    Value::Int(DEFERS.with(|d| d.borrow().last().map(|f| f.len()).unwrap_or(0)) as i64)
}

/// → pop the most-recently-deferred closure of the current frame (LIFO).
fn b_defer_pop(_vm: &mut VM, _argc: u8) -> Value {
    DEFERS.with(|d| {
        d.borrow_mut()
            .last_mut()
            .and_then(|f| f.pop())
            .unwrap_or(Value::Undef)
    })
}

/// Drop the drained defer frame on function exit.
fn b_defer_leave(_vm: &mut VM, _argc: u8) -> Value {
    DEFERS.with(|d| {
        d.borrow_mut().pop();
    });
    Value::Undef
}

/// `[closure]` → the closure's target subroutine name-index (drives dynamic
/// dispatch of a function value via `Op::CallDynamic`).
fn b_closure_nameidx(vm: &mut VM, argc: u8) -> Value {
    let args = pop_args(vm, argc);
    match args.first() {
        Some(Value::Obj(id)) => HEAP.with(|h| {
            let h = h.borrow();
            match h.get(*id as usize) {
                Some(HostObj::Closure { name_idx, .. }) => Value::Int(*name_idx),
                _ => Value::Int(-1),
            }
        }),
        _ => Value::Int(-1),
    }
}

/// `[cap0, …, capN, name_idx]` → a closure value carrying its target subroutine
/// name-index and captured values (by value — Go captures by reference, a
/// documented gap).
fn b_closure_new(vm: &mut VM, argc: u8) -> Value {
    let mut args = pop_args(vm, argc);
    let name_idx = args.pop().map(|v| v.to_int()).unwrap_or(-1);
    Value::Obj(heap_alloc(HostObj::Closure {
        name_idx,
        captures: args,
    }))
}

/// `[closure, idx]` → the closure's captured value at `idx`.
fn b_closure_get(vm: &mut VM, argc: u8) -> Value {
    let args = pop_args(vm, argc);
    let idx = args.get(1).map(|v| v.to_int()).unwrap_or(0);
    match args.first() {
        Some(Value::Obj(id)) => HEAP.with(|h| {
            let h = h.borrow();
            match h.get(*id as usize) {
                Some(HostObj::Closure { captures, .. }) => usize::try_from(idx)
                    .ok()
                    .and_then(|i| captures.get(i))
                    .cloned()
                    .unwrap_or(Value::Undef),
                _ => Value::Undef,
            }
        }),
        _ => Value::Undef,
    }
}

/// `min(a, b, …)` — the smallest argument, preserving int vs float, compared
/// numerically (or lexicographically when all arguments are strings).
fn b_min(vm: &mut VM, argc: u8) -> Value {
    fold_extreme(vm, argc, true)
}

/// `max(a, b, …)` — the largest argument (see [`b_min`]).
fn b_max(vm: &mut VM, argc: u8) -> Value {
    fold_extreme(vm, argc, false)
}

fn fold_extreme(vm: &mut VM, argc: u8, want_min: bool) -> Value {
    let args = pop_args(vm, argc);
    let all_str = args.iter().all(|v| matches!(v, Value::Str(_)));
    args.into_iter()
        .reduce(|a, b| {
            let pick_b = if all_str {
                let (x, y) = (go_str(&a), go_str(&b));
                if want_min {
                    y < x
                } else {
                    y > x
                }
            } else if want_min {
                b.to_float() < a.to_float()
            } else {
                b.to_float() > a.to_float()
            };
            if pick_b {
                b
            } else {
                a
            }
        })
        .unwrap_or(Value::Undef)
}

/// The runtime type name of a struct value, or `""` for a non-struct. Used by
/// the compiler's interface method dispatch (a runtime type-switch).
fn b_typeof(vm: &mut VM, argc: u8) -> Value {
    let args = pop_args(vm, argc);
    match args.first() {
        Some(Value::Obj(id)) => HEAP.with(|h| {
            let h = h.borrow();
            // Through a pointer: dynamic dispatch switches on the *pointee*'s
            // type, which is the type whose method set the pointer satisfies.
            match h.get(follow(*id) as usize) {
                Some(HostObj::Struct { type_name, .. }) => Value::str(type_name.clone()),
                _ => Value::str(""),
            }
        }),
        _ => Value::str(""),
    }
}

/// The range keys of a value as a fresh slice: `0..len` for a slice or string,
/// the map's keys for a map. Drives `for … range` uniformly for both.
fn b_range_keys(vm: &mut VM, argc: u8) -> Value {
    let args = pop_args(vm, argc);
    let keys: Vec<Value> = match args.first() {
        // Go ranges a string by rune: the keys are the byte offsets where each
        // rune starts (its index in `range`), not every byte offset.
        Some(Value::Str(s)) => s
            .char_indices()
            .map(|(i, _)| Value::Int(i as i64))
            .collect(),
        Some(Value::Obj(id)) => {
            if let Some((_, _, len)) = slice_backing(*id) {
                (0..len as i64).map(Value::Int).collect()
            } else {
                HEAP.with(|h| match h.borrow().get(*id as usize) {
                    Some(HostObj::Map(m)) => m.iter().map(|(k, _)| k.clone()).collect(),
                    _ => Vec::new(),
                })
            }
        }
        // Go 1.22's range-over-int: `for i := range n` yields 0 … n-1. A
        // non-positive `n` yields nothing, which is why this is a range and not
        // an error. Without this arm an integer produced *no* keys, so the loop
        // silently ran zero times.
        Some(Value::Int(n)) => (0..*n).map(Value::Int).collect(),
        _ => Vec::new(),
    };
    Value::Obj(heap_alloc(HostObj::slice(keys)))
}

/// `[iter, key]` → the loop value for `for key := range iter`. A string yields
/// the rune (code point) starting at byte offset `key`; anything else indexes
/// by the key (slice element or map value).
fn b_range_val(vm: &mut VM, argc: u8) -> Value {
    let args = pop_args(vm, argc);
    let iter = args.first().cloned().unwrap_or(Value::Undef);
    let key = args.get(1).cloned().unwrap_or(Value::Undef);
    match &iter {
        Value::Str(s) => {
            let off = key.to_int().max(0) as usize;
            match s.get(off..).and_then(|rest| rest.chars().next()) {
                Some(c) => Value::Int(c as i64),
                None => Value::Int(0),
            }
        }
        // Range-over-int has only one loop variable — the integer itself — so
        // the "value" is the key. Go rejects a second variable there, which the
        // parser reports rather than inventing one.
        Value::Int(_) => key,
        _ => {
            // Slice/map: index normally.
            vm.push(iter);
            vm.push(key);
            b_index_get(vm, 2)
        }
    }
}

// ── host-owned heap for Go composite types ─────────────────────────────────
//
// `Value::Obj(id)` is an opaque handle into [`HEAP`]; slices and maps are Go
// reference types, so sharing a handle is exactly right. Structs are value
// types — the compiler emits a `GSTRUCT_COPY` on assignment / parameter bind /
// return so a struct handle is never aliased (Go copy semantics).

/// One object on the host-owned Go heap.
pub(crate) enum HostObj {
    /// A slice that owns its backing array. Go slices are reference types.
    ///
    /// `arr_ty` is `Some` when the object is a fixed-size **array** rather than
    /// a slice, and holds the type as written (`[3]int`, `[2][3]int`). The two
    /// are the same object here — a `[N]T` is a value and a `[]T` a reference,
    /// which the *static* type drives at every copy site — but `%T` and `%#v`
    /// read the run-time value, and nothing in the elements distinguishes a
    /// `[3]int` from a 3-element `[]int`. The tag is set where an array is born
    /// (a composite literal, a zero value) and carried by [`array_copy`], so it
    /// survives into an `any` where a `fmt`-position box could not.
    ///
    /// `elem_ty` is the written element type (`byte`, `int`, `string`, …) and is
    /// set only by [`b_elem_tag`], on the display copy of a `fmt` argument. It
    /// exists because `fmt` reads a `[]byte` as text under `%s`/`%q`/`%x` and
    /// distributes those verbs over the elements of every other slice, and the
    /// elements alone cannot tell a `[]byte` from a `[]int`. `None` means the
    /// compiler had no static type for the operand, and the formatter falls back
    /// to guessing from the element values.
    Slice {
        elems: Vec<Value>,
        arr_ty: Option<String>,
        elem_ty: Option<String>,
    },
    /// A sub-slice view `s[lo:hi]` sharing another slice's backing array at an
    /// offset, so element writes are visible through the parent (and vice versa).
    /// `backing` indexes a [`HostObj::Slice`]. `cap` is the view's own capacity:
    /// normally `backing.len() - offset`, but the three-index form `s[lo:hi:max]`
    /// records the smaller `max - lo` so an append past it reallocates instead of
    /// writing into backing the view no longer owns.
    SliceView {
        backing: u32,
        offset: usize,
        len: usize,
        cap: usize,
    },
    /// A map, insertion-ordered for stable iteration; keys compared by value.
    Map(GoMap),
    /// A pointer to another heap object: what `&x` on an existing *variable*
    /// evaluates to.
    ///
    /// `&T{…}` can mark its own handle `by_ref` because that handle is born a
    /// pointer and nothing else refers to it. `&x` cannot: the handle is `x`'s,
    /// and marking it would make `x` itself compare by identity where Go
    /// compares field by field. So the pointer becomes an object of its own,
    /// which leaves the pointee's identity exactly as it was — and gives two
    /// `&x` of the same variable the same `target` to compare, which is Go's
    /// rule for pointer equality.
    Ptr { target: u32 },
    /// A struct: its type name and ordered `(field, value)` pairs.
    ///
    /// `by_ref` marks a handle produced by `&T{…}` / `new(T)` — a Go *pointer*
    /// rather than a struct value. go-rs models both as the same heap handle, but
    /// `==` does not treat them alike: Go compares struct values field by field
    /// and pointers by address. A `by_ref` handle therefore compares by identity,
    /// which is what makes two `errors.New("x")` values distinct (and so what
    /// `errors.Is` walks a wrap chain looking for). A [`GSTRUCT_COPY`] of one
    /// clears the flag: the copy is a value, not the pointer.
    Struct {
        type_name: String,
        fields: Vec<(String, Value)>,
        by_ref: bool,
    },
    /// A closure: the name-index of its compiled `$lambda_N` subroutine (for
    /// dynamic dispatch when passed as a value) plus its captured values.
    Closure { name_idx: i64, captures: Vec<Value> },
    /// A one-slot mutable box for a variable captured by reference: the enclosing
    /// scope and every capturing closure share this handle, so writes propagate.
    Cell(Value),
    /// A `float32` at a `fmt` argument position. Go's `fmt` renders a float with
    /// the shortest decimal that round-trips **at the value's own width**, so a
    /// `float32` and the `float64` holding the same bits print differently
    /// (`0.33333334` vs `0.3333333333333333`). The value model has one float
    /// width, so the compiler boxes a statically-`float32` operand here on its
    /// way into `fmt` — and nowhere else, which keeps the box out of arithmetic.
    F32(f32),
    /// A `uint64` / `uint` / `uintptr` at a `fmt` argument position. The value
    /// model holds one integer width (`Value::Int`, an `i64`) whose bit pattern
    /// is already right for unsigned arithmetic; only *rendering* reads the sign
    /// bit, so the compiler boxes a statically-unsigned operand on its way into
    /// `fmt` — and nowhere else, which keeps the box out of arithmetic. `ty` is
    /// the written type so `%T` names it exactly.
    U64 { val: u64, ty: String },
    /// The unique marker a receive on a closed, drained channel yields — see
    /// [`chan_closed_sentinel`]. It is never stored in a Go variable: every
    /// receive site maps it to the element type's zero immediately.
    ChanClosed,
    /// The operands of a `f(xs...)` spread into a variadic builtin, waiting to be
    /// expanded back into the argument list — see [`GSPREAD`]. Like
    /// [`HostObj::ChanClosed`] it never reaches a Go variable: [`pop_args`]
    /// unwraps it on the way into the builtin that was handed it.
    Spread(Vec<Value>),
    /// The zero value of a slice or map type — Go's *typed* nil. It is not
    /// `Value::Undef`, because Go distinguishes a nil slice (`[]`, `len` 0,
    /// appendable) and a nil map (`map[]`, readable, not writable) from a nil
    /// interface (`<nil>`), and prints all three differently. `ty` is the written
    /// type, so `%T` and `%#v` name it exactly.
    ///
    /// It still compares equal to `nil`: [`numeric_hook`] answers `Eq`/`Ne`
    /// against [`Value::Undef`] for these handles, so `s == nil` stays true.
    Nil { kind: NilKind, ty: String },
    /// A value at a `fmt` argument position whose static type was a defined one
    /// (`type Weekday int`). A defined type is represented exactly like its
    /// base, so this box holds the base value untouched and adds only the name
    /// `%T` and `%#v` print. [`b_named_box`] applies it; the formatter unwraps
    /// it everywhere else, and it never reaches the program.
    Named { ty: String, inner: Value },
}

impl HostObj {
    /// A plain slice — the untagged [`HostObj::Slice`] every slice-producing
    /// path wants. An array is this plus a tag, which [`b_array_tag`] stamps on
    /// where the array is born and [`array_copy`] carries across.
    fn slice(elems: Vec<Value>) -> HostObj {
        HostObj::Slice {
            elems,
            arr_ty: None,
            elem_ty: None,
        }
    }
}

/// Which kind of typed nil a [`HostObj::Nil`] is — the two Go composite types
/// whose zero value is usable rather than a fault.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(crate) enum NilKind {
    Slice,
    Map,
}

thread_local! {
    /// One [`HostObj::Nil`] handle per written type. Nil slices and maps are
    /// immutable (an `append` reallocates, a map write panics), so every
    /// `var s []int` can share a handle; memoizing keeps a loop that declares one
    /// per iteration from growing the heap. Cleared by [`heap_reset`].
    static NILS: RefCell<std::collections::HashMap<String, Value>> =
        RefCell::new(std::collections::HashMap::new());
}

/// The typed nil for a slice or map type, shared per type name so `== nil` and
/// `%#v` see one object.
fn typed_nil(ty: &str) -> Value {
    let ty = ty.to_string();
    NILS.with(|n| {
        n.borrow_mut()
            .entry(ty.clone())
            .or_insert_with(|| {
                let kind = if ty.starts_with("map[") {
                    NilKind::Map
                } else {
                    NilKind::Slice
                };
                Value::Obj(heap_alloc(HostObj::Nil { kind, ty }))
            })
            .clone()
    })
}

/// `[typeName]` → the typed nil for that slice or map type.
fn b_nil_of(vm: &mut VM, argc: u8) -> Value {
    let args = pop_args(vm, argc);
    typed_nil(&args.first().map(go_str).unwrap_or_default())
}

/// [`GF32_BOX`] — tag the `float32`s in a `fmt` argument. Stack `[value, spec]`:
/// an empty `spec` tags the value itself, otherwise `spec` names the struct
/// fields to tag (`"x,y"`). Either way a slice or map operand is handled
/// element-wise, so `[]float32`, `map[string]float32` and `[]point` all work.
fn b_f32_box(vm: &mut VM, argc: u8) -> Value {
    let args = pop_args(vm, argc);
    let v = args.first().cloned().unwrap_or(Value::Undef);
    let spec = args.get(1).map(go_str).unwrap_or_default();
    box_for_fmt(&v, &spec, BoxTag::F32)
}

/// [`GU64_BOX`] — tag the unsigned 64-bit integers in a `fmt` argument. Stack
/// `[value, spec, ty]`, with `spec` read exactly as [`b_f32_box`] reads it.
fn b_u64_box(vm: &mut VM, argc: u8) -> Value {
    let args = pop_args(vm, argc);
    let v = args.first().cloned().unwrap_or(Value::Undef);
    let spec = args.get(1).map(go_str).unwrap_or_default();
    let ty = args
        .get(2)
        .map(go_str)
        .unwrap_or_else(|| "uint64".to_string());
    box_for_fmt(&v, &spec, BoxTag::U64(&ty))
}

/// Which width tag [`box_for_fmt`] applies to the leaves it reaches.
#[derive(Clone, Copy)]
enum BoxTag<'a> {
    F32,
    U64(&'a str),
}

/// Apply `tag` to one leaf value.
fn box_leaf(v: &Value, tag: BoxTag) -> Value {
    match tag {
        BoxTag::F32 => box_f32(v),
        BoxTag::U64(ty) => box_u64(v, ty),
    }
}

/// One value tagged as an unsigned 64-bit integer. A non-integer passes through
/// untouched, so the box is safe wherever the static type says `uint64`.
fn box_u64(v: &Value, ty: &str) -> Value {
    match v {
        Value::Int(n) => Value::Obj(heap_alloc(HostObj::U64 {
            val: *n as u64,
            ty: ty.to_string(),
        })),
        other => other.clone(),
    }
}

/// The `u64` a value carries when it is a [`HostObj::U64`] box.
fn unbox_u64(v: &Value) -> Option<u64> {
    let Value::Obj(id) = v else { return None };
    HEAP.with(|h| match h.borrow().get(*id as usize) {
        Some(HostObj::U64 { val, .. }) => Some(*val),
        _ => None,
    })
}

/// [`GU64_ARITH`] — one operation at unsigned 64-bit width. Both operands are
/// read as the `u64` their `i64` bit pattern denotes, so no caller has to have
/// boxed them.
fn b_u64_arith(vm: &mut VM, argc: u8) -> Value {
    let args = pop_args(vm, argc);
    let a = args.first().map(arg_int).unwrap_or(0) as u64;
    let b = args.get(1).map(arg_int).unwrap_or(0) as u64;
    match args.get(2).map(Value::to_int).unwrap_or(u64_op::DIV) {
        // Go panics on a zero divisor whatever the signedness.
        u64_op::DIV => match a.checked_div(b) {
            Some(q) => Value::Int(q as i64),
            None => {
                runtime_panic(vm, "integer divide by zero");
                Value::Int(0)
            }
        },
        u64_op::MOD => match a.checked_rem(b) {
            Some(r) => Value::Int(r as i64),
            None => {
                runtime_panic(vm, "integer divide by zero");
                Value::Int(0)
            }
        },
        // A shift count at or past the width yields 0 in Go, where Rust's `>>`
        // would panic on overflow.
        u64_op::SHR => Value::Int(if b >= 64 { 0 } else { (a >> b) as i64 }),
        u64_op::LT => Value::bool(a < b),
        u64_op::LE => Value::bool(a <= b),
        u64_op::GT => Value::bool(a > b),
        u64_op::GE => Value::bool(a >= b),
        // Unary — `b` is unused.
        _ => Value::Float(a as f64),
    }
}

/// Tag every `float32` a `fmt` argument holds, per [`b_f32_box`]'s `spec`.
/// Composites are rebuilt rather than mutated — the tag is a display detail and
/// must not be visible to the program that owns the original.
fn box_for_fmt(v: &Value, spec: &str, tag: BoxTag) -> Value {
    if let Some(es) = slice_elems(v) {
        let elems = es.iter().map(|e| box_for_fmt(e, spec, tag)).collect();
        // The rebuild keeps the `[N]T` tag, or `%T` on an array of `float32` /
        // `uint64` would fall back to naming it a slice.
        let arr_ty = match v {
            Value::Obj(id) => HEAP.with(|h| match h.borrow().get(*id as usize) {
                Some(HostObj::Slice { arr_ty, .. }) => arr_ty.clone(),
                _ => None,
            }),
            _ => None,
        };
        return Value::Obj(heap_alloc(HostObj::Slice {
            elems,
            arr_ty,
            elem_ty: None,
        }));
    }
    if let Some(pairs) = map_pairs(v) {
        let boxed = pairs
            .into_iter()
            .map(|(k, val)| (k, box_for_fmt(&val, spec, tag)))
            .collect();
        return Value::Obj(heap_alloc(HostObj::Map(GoMap::from_pairs(boxed))));
    }
    if spec.is_empty() {
        return box_leaf(v, tag);
    }
    let Value::Obj(id) = v else { return v.clone() };
    // Snapshot before boxing: `box_leaf` allocates, and the heap borrow is not
    // re-entrant.
    let snapshot = HEAP.with(|h| match h.borrow().get(*id as usize) {
        Some(HostObj::Struct {
            type_name,
            fields,
            by_ref,
        }) => Some((type_name.clone(), fields.clone(), *by_ref)),
        _ => None,
    });
    let Some((type_name, fields, by_ref)) = snapshot else {
        return v.clone();
    };
    let fields = fields
        .into_iter()
        .map(|(n, fv)| {
            let named = spec.split(',').any(|f| f == n);
            let fv = if named { box_leaf(&fv, tag) } else { fv };
            (n, fv)
        })
        .collect();
    Value::Obj(heap_alloc(HostObj::Struct {
        type_name,
        fields,
        by_ref,
    }))
}

/// A map value's `(key, value)` pairs, or `None` when it is not a map.
fn map_pairs(v: &Value) -> Option<Vec<(Value, Value)>> {
    let Value::Obj(id) = v else { return None };
    HEAP.with(|h| match h.borrow().get(*id as usize) {
        Some(HostObj::Map(m)) => Some(m.to_vec()),
        _ => None,
    })
}

/// One value tagged `float32`. A non-float (a nil, a string in an `any`) passes
/// through untouched, so the box is safe wherever the static type says `float32`.
fn box_f32(v: &Value) -> Value {
    match v {
        Value::Float(f) => Value::Obj(heap_alloc(HostObj::F32(*f as f32))),
        Value::Int(n) => Value::Obj(heap_alloc(HostObj::F32(*n as f32))),
        other => other.clone(),
    }
}

/// The `f32` a value carries when it is a [`HostObj::F32`] box.
fn unbox_f32(v: &Value) -> Option<f32> {
    let Value::Obj(id) = v else { return None };
    HEAP.with(|h| match h.borrow().get(*id as usize) {
        Some(HostObj::F32(f)) => Some(*f),
        _ => None,
    })
}

/// A `fmt` argument's numeric value, seeing through a [`HostObj::F32`] box.
fn arg_float(v: &Value) -> f64 {
    unbox_f32(v).map_or_else(|| v.to_float(), f64::from)
}

/// A `fmt` argument's integer value, seeing through a [`HostObj::F32`] or
/// [`HostObj::U64`] box. The `u64` is returned as its `i64` bit pattern, which
/// is what every unsigned operation consumes; only *rendering* re-reads it as
/// unsigned (see [`arg_uint`]).
fn arg_int(v: &Value) -> i64 {
    if let Some(u) = unbox_u64(v) {
        return u as i64;
    }
    unbox_f32(v).map_or_else(|| v.to_int(), |f| f as i64)
}

/// A `fmt` argument's value as an unsigned 64-bit integer when it is tagged one
/// — the only thing that makes `%d`/`%x`/`%o`/`%b` print the unsigned digits.
fn arg_uint(v: &Value) -> Option<u64> {
    unbox_u64(v)
}

/// [`GF32_ARITH`] — one arithmetic operation at 32-bit width.
fn b_f32_arith(vm: &mut VM, argc: u8) -> Value {
    let args = pop_args(vm, argc);
    let a = args.first().map(Value::to_float).unwrap_or(0.0) as f32;
    let b = args.get(1).map(Value::to_float).unwrap_or(0.0) as f32;
    let r = match args.get(2).map(Value::to_int).unwrap_or(f32_op::ADD) {
        f32_op::SUB => a - b,
        f32_op::MUL => a * b,
        f32_op::DIV => a / b,
        _ => a + b,
    };
    Value::Float(f64::from(r))
}

/// The kind of typed nil `v` is, if it is one.
fn nil_kind(v: &Value) -> Option<NilKind> {
    let Value::Obj(id) = v else { return None };
    HEAP.with(|h| match h.borrow().get(*id as usize) {
        Some(HostObj::Nil { kind, .. }) => Some(*kind),
        _ => None,
    })
}

/// How many VM frames deeper than the draining frame a deferred function's own
/// body runs. `defer f(a)` never calls `f` from the drain loop directly: the
/// compiler snapshots the callee and arguments into a synthesized zero-argument
/// closure (see `compile_defer`) and the drain loop calls *that*, which then
/// calls `f`. So the chain is always `draining frame → snapshot closure → f`,
/// two frames, whatever `f` is — a function literal, a named function, a
/// func-valued variable, or a method value. `recover()` is Go's "direct" one
/// exactly at this depth.
const DEFERRED_BODY_DEPTH: usize = 2;

/// A panic parked for the duration of one deferred call — see [`GDEFER_PARK`].
struct Park {
    /// The panic value, taken by the `recover()` that claims it.
    val: Option<Value>,
    /// The VM call depth of the frame that is draining defers. A `recover()` is
    /// Go's "direct" one exactly when it runs at `depth + 1`.
    depth: usize,
    /// Whether a `recover()` has already claimed this panic.
    recovered: bool,
}

thread_local! {
    /// The host-owned Go object heap. `Value::Obj(id)` indexes this slab; it
    /// grows per run and is cleared by [`heap_reset`] at the start of every
    /// program so handles never leak across runs.
    static HEAP: RefCell<Vec<HostObj>> = const { RefCell::new(Vec::new()) };

    /// A stack of defer frames, one per in-flight function invocation that has
    /// `defer` statements. Each frame holds its deferred closures in push order;
    /// the drain loop pops them LIFO before the function returns.
    static DEFERS: RefCell<Vec<Vec<Value>>> = const { RefCell::new(Vec::new()) };

    /// The value of an in-flight `panic`, or `None`. Set by `panic()`, cleared by
    /// `recover()`; the compiler unwinds through defer drains while it is `Some`.
    static PANIC: RefCell<Option<Value>> = const { RefCell::new(None) };

    /// One entry per deferred call currently running, innermost last. A drain
    /// loop parks the propagating panic here before each deferred call and
    /// restores it after, so `recover()` can be answered by frame depth.
    static PARKED: RefCell<Vec<Park>> = const { RefCell::new(Vec::new()) };

    /// The per-run drained-closed-channel sentinel handle, allocated on first
    /// use and dropped by [`heap_reset`] along with the heap slot it names.
    static CHAN_CLOSED: RefCell<Option<Value>> = const { RefCell::new(None) };

    /// Struct type name → the `(name, written type)` of each of its fields that
    /// holds a **value**: a field declared `T` where `T` is a struct type (not
    /// `*T`), or a fixed-size array `[N]T`. Written once per compile by
    /// [`set_struct_plan`], read by [`b_struct_copy`] so a copy recurses exactly
    /// as far as Go's value semantics reach and no further: a `*T` field is a
    /// pointer and must stay aliased, and slices/maps/channels are reference
    /// types whose handle the copy shares.
    ///
    /// The field's type is carried, not just its name, because an array field's
    /// copy needs its element type — the heap cannot tell `[2][3]int` (copy the
    /// inner arrays) from `[2][]int` (share the inner slices).
    ///
    /// Keyed by field *name* rather than position: a keyed composite literal
    /// (`T{B: 1, A: 2}`) may build the field vector in written order, so a
    /// positional plan would recurse into the wrong field.
    static STRUCT_PLAN: RefCell<HashMap<String, Vec<(String, String)>>> =
        RefCell::new(HashMap::new());
}

/// Record which struct fields a value-copy must recurse into. Called by the
/// compiler, which is the only place the declared field types are known — the
/// runtime sees a `Value::Obj` handle with no type information beyond the
/// struct's own name.
pub fn set_struct_plan(plan: HashMap<String, Vec<(String, String)>>) {
    STRUCT_PLAN.with(|p| *p.borrow_mut() = plan);
}

/// Clear the object heap, defer stack, and panic state. Called at each run start.
pub fn heap_reset() {
    HEAP.with(|h| h.borrow_mut().clear());
    METHOD_SETS.with(|m| m.borrow_mut().clear());
    DEFERS.with(|d| d.borrow_mut().clear());
    PANIC.with(|p| *p.borrow_mut() = None);
    PARKED.with(|p| p.borrow_mut().clear());
    CHAN_CLOSED.with(|c| *c.borrow_mut() = None);
    PANIC_MODE.with(|m| *m.borrow_mut() = false);
    NILS.with(|n| n.borrow_mut().clear());
    stdlib::sentinels_reset();
}

/// Allocate `obj` on the heap and return its handle.
fn heap_alloc(obj: HostObj) -> u32 {
    HEAP.with(|h| {
        let mut h = h.borrow_mut();
        let id = h.len() as u32;
        h.push(obj);
        id
    })
}

/// Resolve a slice handle to `(backing slice id, offset, len)`. A plain
/// [`HostObj::Slice`] is its own backing at offset 0; a [`HostObj::SliceView`]
/// names the backing it shares. `None` if `id` is not a slice.
fn slice_backing(id: u32) -> Option<(u32, usize, usize)> {
    HEAP.with(|h| match h.borrow().get(id as usize) {
        Some(HostObj::Slice { elems: a, .. }) => Some((id, 0, a.len())),
        Some(HostObj::SliceView {
            backing,
            offset,
            len,
            ..
        }) => Some((*backing, *offset, *len)),
        // A nil slice is a zero-length slice for every read: `len`, `cap`,
        // `range`, `copy` and indexing all behave as Go's do.
        Some(HostObj::Nil {
            kind: NilKind::Slice,
            ..
        }) => Some((id, 0, 0)),
        _ => None,
    })
}

/// A slice handle's capacity — `len` for a slice that owns its backing, the
/// recorded `cap` for a view. `None` if `id` is not a slice.
fn slice_cap(id: u32) -> Option<usize> {
    HEAP.with(|h| match h.borrow().get(id as usize) {
        Some(HostObj::Slice { elems: a, .. }) => Some(a.len()),
        Some(HostObj::SliceView { cap, .. }) => Some(*cap),
        Some(HostObj::Nil {
            kind: NilKind::Slice,
            ..
        }) => Some(0),
        _ => None,
    })
}

/// Read element `i` of a slice-or-view (bounds-checked against its length).
fn slice_get(id: u32, i: usize) -> Option<Value> {
    let (backing, offset, len) = slice_backing(id)?;
    if i >= len {
        return None;
    }
    HEAP.with(|h| match h.borrow().get(backing as usize) {
        Some(HostObj::Slice { elems: a, .. }) => a.get(offset + i).cloned(),
        _ => None,
    })
}

/// Write element `i` of a slice-or-view (bounds-checked), through its backing so
/// a sub-slice write is visible to the parent. Returns whether it landed.
fn slice_set(id: u32, i: usize, v: Value) -> bool {
    let Some((backing, offset, len)) = slice_backing(id) else {
        return false;
    };
    if i >= len {
        return false;
    }
    HEAP.with(|h| {
        if let Some(HostObj::Slice { elems: a, .. }) = h.borrow_mut().get_mut(backing as usize) {
            if let Some(slot) = a.get_mut(offset + i) {
                *slot = v;
                return true;
            }
        }
        false
    })
}

/// `copy(dst, src)` — copy `min(len(dst), len(src))` elements into `dst`,
/// returning the count. `src` may be a slice or a string (`copy([]byte, s)`).
fn b_copy(vm: &mut VM, argc: u8) -> Value {
    let args = pop_args(vm, argc);
    let Some(Value::Obj(dst)) = args.first().cloned() else {
        return Value::Int(0);
    };
    let dst_len = slice_backing(dst).map(|(_, _, l)| l).unwrap_or(0);
    let src_vals: Vec<Value> = match args.get(1) {
        Some(Value::Obj(sid)) => {
            let slen = slice_backing(*sid).map(|(_, _, l)| l).unwrap_or(0);
            (0..slen).filter_map(|i| slice_get(*sid, i)).collect()
        }
        Some(Value::Str(s)) => s.bytes().map(|b| Value::Int(b as i64)).collect(),
        _ => return Value::Int(0),
    };
    let n = dst_len.min(src_vals.len());
    for (i, v) in src_vals.into_iter().take(n).enumerate() {
        slice_set(dst, i, v);
    }
    Value::Int(n as i64)
}

/// Whether two values are equal as Go map keys (comparable kinds only).
fn key_eq(a: &Value, b: &Value) -> bool {
    match (a, b) {
        (Value::Str(x), Value::Str(y)) => x == y,
        (Value::Bool(x), Value::Bool(y)) => x == y,
        // Struct (and array) keys compare by value, field-by-field — Go's rule
        // for comparable struct/array keys — not by heap identity.
        (Value::Obj(x), Value::Obj(y)) => {
            if x == y {
                return true;
            }
            HEAP.with(|h| {
                let h = h.borrow();
                match (h.get(*x as usize), h.get(*y as usize)) {
                    (
                        Some(HostObj::Struct {
                            type_name: tx,
                            fields: fx,
                            ..
                        }),
                        Some(HostObj::Struct {
                            type_name: ty,
                            fields: fy,
                            ..
                        }),
                    ) => {
                        tx == ty
                            && fx.len() == fy.len()
                            && fx.iter().zip(fy).all(|((_, va), (_, vb))| key_eq(va, vb))
                    }
                    // An *array* key compares elementwise, which is why
                    // `m[[2]int{1, 2}]` finds the entry a differently-allocated
                    // `[2]int{1, 2}` stored. Reaching a slice here is not
                    // possible in a program `go` accepts — a slice is not a
                    // comparable type and so cannot be a map key at all.
                    (
                        Some(HostObj::Slice { elems: ex, .. }),
                        Some(HostObj::Slice { elems: ey, .. }),
                    ) => ex.len() == ey.len() && ex.iter().zip(ey).all(|(a, b)| key_eq(a, b)),
                    _ => false,
                }
            })
        }
        // `nil` is a key of its own: Go's `map[any]V` compares the dynamic type
        // before the value, so a nil key equals another nil and nothing else.
        (Value::Undef, Value::Undef) => true,
        // A string, a bool and a number are three different Go types, so a key
        // of one kind is never equal to a key of another. Comparing them by
        // `to_float` — which reads a `"1"` as `1` and a `true` as `1` — merged
        // three distinct keys of a `map[any]V` into one.
        (Value::Str(_) | Value::Bool(_) | Value::Obj(_) | Value::Undef, _)
        | (_, Value::Str(_) | Value::Bool(_) | Value::Obj(_) | Value::Undef) => false,
        // An integer and a float are two more different Go types, so `m[1]` and
        // `m[1.0]` are two keys of a `map[any]V`. Comparing them by `to_float`
        // merged those as well. It is the *compiler* that makes this safe: an
        // untyped constant in a `map[float64]V` index is converted to the key
        // type before it is pushed (`Compiler::emit_map_key`), so a float key
        // never arrives here as a `Value::Int`.
        (Value::Int(_), Value::Float(_)) | (Value::Float(_), Value::Int(_)) => false,
        // Within a kind, the value decides — and a `NaN` equals nothing, itself
        // included, exactly as Go's map says.
        _ => a.to_float() == b.to_float(),
    }
}

/// A Go map key projected to something hashable, for the index a [`GoMap`]
/// keeps beside its ordered pairs.
///
/// The projection has to agree with [`key_eq`] exactly, so numbers go through
/// `to_float` — that is what `key_eq` compares them by — with `-0.0` normalised
/// to `0.0` so the two hash alike. A `NaN` has no projection, which is also
/// Go's answer: a `NaN` key equals nothing, not even itself, so every insert of
/// one adds another entry no lookup can ever reach.
#[derive(PartialEq, Eq, Hash, Clone)]
enum MapKey {
    Nil,
    Str(std::sync::Arc<String>),
    Bool(bool),
    Int(i64),
    /// A float's bits, so `1` and `1.0` stay two keys of a `map[any]V`.
    Float(u64),
    /// A struct key: its type name and its *field values'* projections in
    /// declaration order — the field names are not part of it, because
    /// [`key_eq`] does not compare them either.
    Struct(String, Vec<MapKey>),
    /// An array key: its elements' projections, in order. A distinct variant
    /// from [`MapKey::Struct`] because `key_eq` never equates the two.
    Array(Vec<MapKey>),
}

/// A key's hash projection, or `None` when it has none: a `NaN`, a struct or
/// array holding one, or a heap object that is neither a struct nor an array
/// (a nil, a closure — things `key_eq` answers `false` for against anything but
/// themselves).
///
/// The variants partition keys exactly the way [`key_eq`] does, so two keys
/// with projections are equal if and only if their projections are. That is the
/// whole contract: the index is authoritative for any key this answers `Some`
/// for, so a projection that separated two keys `key_eq` calls equal would lose
/// entries.
///
/// **Reads the heap** for a struct or array key, so it must be called with no
/// *mutable* heap borrow held — which is why every [`GoMap`] method takes the
/// projection rather than computing it.
fn map_key(v: &Value) -> Option<MapKey> {
    match v {
        Value::Undef => Some(MapKey::Nil),
        Value::Str(s) => Some(MapKey::Str(s.clone())),
        Value::Bool(b) => Some(MapKey::Bool(*b)),
        // A struct or array key is compared field by field / element by
        // element, so it projects the same way: recursively, into a shape that
        // hashes. Without this every such key fell back to the ordered scan and
        // made a map of them O(n²) to build and to read.
        // The recursion re-enters `HEAP` for a nested struct or array field.
        // Both borrows are shared, which `RefCell` allows any number of.
        Value::Obj(id) => HEAP.with(|h| {
            let h = h.borrow();
            match h.get(*id as usize) {
                Some(HostObj::Struct {
                    type_name, fields, ..
                }) => Some(MapKey::Struct(
                    type_name.clone(),
                    fields
                        .iter()
                        .map(|(_, v)| map_key(v))
                        .collect::<Option<Vec<_>>>()?,
                )),
                Some(HostObj::Slice { elems, .. }) => Some(MapKey::Array(
                    elems.iter().map(map_key).collect::<Option<Vec<_>>>()?,
                )),
                _ => None,
            }
        }),
        // Two variants, because `key_eq` never equates an integer with a float.
        // `-0.0` normalises to `0.0` so the two hash alike, as `==` says they
        // are; a `NaN` has no projection, which is also Go's answer — a `NaN`
        // key equals nothing, not even itself, so every insert of one adds an
        // entry no lookup can ever reach.
        Value::Int(n) => Some(MapKey::Int(*n)),
        other => {
            let f = other.to_float();
            (!f.is_nan()).then(|| MapKey::Float(if f == 0.0 { 0.0 } else { f }.to_bits()))
        }
    }
}

/// What the hash index can say about a key.
enum Probe {
    /// The index is authoritative: this is where the key sits, or it is absent.
    Answered(Option<usize>),
    /// The index cannot speak for this map or this key — scan in order.
    Scan,
}

/// A Go map: insertion-ordered `(key, value)` pairs plus a hash index over the
/// keys.
///
/// The order is load-bearing — it is what makes iteration stable, and `fmt`
/// sorts on top of it — so the index sits *beside* the pairs rather than
/// replacing them. Without one, every lookup, insert and `delete` was a linear
/// scan and building a map was O(n²).
///
/// The index is dropped, and every operation falls back to that scan, the
/// moment a key appears that a hash lookup could not answer: one with no
/// [`map_key`] projection, which is a struct or array key (compared
/// structurally, through the heap) or a `NaN`.
/// Pairs below which the ordered scan beats hashing, so no index is built. A
/// program that creates many small maps — a literal per loop iteration — would
/// otherwise pay a `HashMap` allocation for each one it never grows into.
const INDEX_THRESHOLD: usize = 8;

#[derive(Clone, Default)]
pub(crate) struct GoMap {
    pairs: Vec<(Value, Value)>,
    /// Each pair's key projected for the index, aligned with `pairs`.
    ///
    /// Kept rather than recomputed because projecting a struct or array key
    /// reads the heap, and [`GoMap::build_index`] runs from inside
    /// [`GoMap::put`], which its callers reach while holding a *mutable* heap
    /// borrow. Every projection the map ever needs is therefore taken once, by
    /// the caller, before that borrow.
    keys: Vec<Option<MapKey>>,
    /// `None` until the map outgrows [`INDEX_THRESHOLD`], and `None` for good
    /// once `unindexable` is set.
    index: Option<HashMap<MapKey, usize>>,
    /// Set by a key with no [`map_key`] projection. The scan is then the only
    /// thing that can find that key again, so the index is never built.
    unindexable: bool,
}

/// Read access to the ordered pairs. There is deliberately no `DerefMut`: every
/// write goes through a method that keeps the index in step.
impl std::ops::Deref for GoMap {
    type Target = [(Value, Value)];

    fn deref(&self) -> &Self::Target {
        &self.pairs
    }
}

impl GoMap {
    /// A map rebuilt from pairs already known to have distinct keys — a display
    /// copy of an existing one, not a fresh sequence of inserts.
    fn from_pairs(pairs: Vec<(Value, Value)>) -> Self {
        let mut m = GoMap::default();
        for (k, v) in pairs {
            let mk = map_key(&k);
            m.put(None, mk, k, v);
        }
        m
    }

    /// Where the key projecting to `mk` sits, when the index can answer without
    /// the ordered scan.
    fn probe(&self, mk: Option<&MapKey>) -> Probe {
        let (Some(index), Some(k)) = (self.index.as_ref(), mk) else {
            return Probe::Scan;
        };
        Probe::Answered(index.get(k).copied())
    }

    /// Where `key` sits, falling back to the ordered scan. Callable only with no
    /// heap borrow held: [`key_eq`] on a struct key reads the heap itself.
    fn find(&self, mk: Option<&MapKey>, key: &Value) -> Option<usize> {
        match self.probe(mk) {
            Probe::Answered(hit) => hit,
            Probe::Scan => self.pairs.iter().position(|(k, _)| key_eq(k, key)),
        }
    }

    /// Insert `key` — projected to `mk` by the caller — or overwrite the value
    /// at `at` when the caller has already located it. Splitting both the
    /// search and the projection out is what lets the heap borrow be taken
    /// *after* the two things that read the heap themselves.
    fn put(&mut self, at: Option<usize>, mk: Option<MapKey>, key: Value, val: Value) {
        if let Some(i) = at {
            self.pairs[i].1 = val;
            return;
        }
        let pos = self.pairs.len();
        match &mk {
            Some(k) => {
                if let Some(index) = self.index.as_mut() {
                    index.insert(k.clone(), pos);
                }
            }
            // A key the index cannot hold makes it unsound for the whole map:
            // the scan is the only thing that can find that key again.
            None => {
                self.unindexable = true;
                self.index = None;
            }
        }
        self.keys.push(mk);
        self.pairs.push((key, val));
        if self.index.is_none() && !self.unindexable && self.pairs.len() > INDEX_THRESHOLD {
            self.build_index();
        }
    }

    /// Index every pair from the projections already taken. Runs once, when the
    /// map outgrows the scan.
    fn build_index(&mut self) {
        let mut index = HashMap::with_capacity(self.pairs.len() * 2);
        for (i, mk) in self.keys.iter().enumerate() {
            let Some(mk) = mk else {
                self.unindexable = true;
                return;
            };
            index.insert(mk.clone(), i);
        }
        self.index = Some(index);
    }

    /// Drop the pair at `i`, keeping the order of the rest.
    fn remove(&mut self, i: usize) {
        self.pairs.remove(i);
        let removed = self.keys.remove(i);
        let Some(index) = self.index.as_mut() else {
            return;
        };
        if let Some(k) = removed {
            index.remove(&k);
        }
        // Everything after it moved down a slot.
        for pos in index.values_mut() {
            if *pos > i {
                *pos -= 1;
            }
        }
    }
}

/// The index of `key` in the map at heap `id`, comparing by value.
///
/// The hash index answers under the borrow, since projecting a key never reads
/// the heap. The scan it falls back to does — a struct key's [`key_eq`] reads
/// the heap itself — so the keys are snapshotted first and the borrow released.
fn map_find_index(id: u32, key: &Value) -> Option<usize> {
    // Projected first: a struct or array key reads the heap to project, and the
    // probe below holds a borrow of it.
    let mk = map_key(key);
    let probe = HEAP.with(|h| match h.borrow().get(id as usize) {
        Some(HostObj::Map(m)) => m.probe(mk.as_ref()),
        // Not a map at all; a nil map reads as an empty one.
        _ => Probe::Answered(None),
    });
    match probe {
        Probe::Answered(hit) => hit,
        Probe::Scan => {
            let keys: Vec<Value> = HEAP.with(|h| match h.borrow().get(id as usize) {
                Some(HostObj::Map(m)) => m.iter().map(|(k, _)| k.clone()).collect(),
                _ => Vec::new(),
            });
            keys.iter().position(|k| key_eq(k, key))
        }
    }
}

/// `make([]T, n)` (2 args: kind-tag, n) or `make(map[K]V)` (1 arg: kind-tag).
/// The kind tag is a string: "slice" or "map". A slice is zero-filled with the
/// element zero value passed as the last argument.
fn b_make(vm: &mut VM, argc: u8) -> Value {
    let args = pop_args(vm, argc);
    let kind = args.first().map(go_str).unwrap_or_default();
    match kind.as_str() {
        "map" => Value::Obj(heap_alloc(HostObj::Map(GoMap::default()))),
        _ => {
            let n = args.get(1).map(|v| v.to_int()).unwrap_or(0);
            let zero = args.get(2).cloned().unwrap_or(Value::Int(0));
            // `-1` is the compiler's marker for an omitted capacity.
            let c = args.get(3).map(|v| v.to_int()).unwrap_or(-1);
            if n < 0 {
                ffi_fault(vm, format!("go-rs: makeslice: len out of range ({n})"));
                return Value::Undef;
            }
            if c >= 0 && c < n {
                ffi_fault(vm, format!("go-rs: makeslice: cap out of range ({c})"));
                return Value::Undef;
            }
            let (len, cap) = (n as usize, if c < 0 { n as usize } else { c as usize });
            // Each element gets its *own* zero value. Cloning one `Value` would
            // share a struct (or array) zero's handle across every slot, so a
            // write to `s[0].f` would appear in every element. `value_copy` is
            // the identity on a scalar zero, so the scalar case is unchanged.
            let elem_ty = args.get(4).map(go_str).unwrap_or_default();
            let fill = |k: usize| -> Vec<Value> {
                (0..k).map(|_| value_copy(zero.clone(), &elem_ty)).collect()
            };
            if cap == len {
                return Value::Obj(heap_alloc(HostObj::slice(fill(len))));
            }
            // A `cap > len` slice is a view over a longer backing array — the
            // same shape Go's slice header has, so `cap` reports the spare room
            // and an append that fits writes into it instead of reallocating.
            let backing = heap_alloc(HostObj::slice(fill(cap)));
            Value::Obj(heap_alloc(HostObj::SliceView {
                backing,
                offset: 0,
                len,
                cap,
            }))
        }
    }
}

/// `[]T{a, b, …}` — build a slice from the popped element values.
fn b_slice_lit(vm: &mut VM, argc: u8) -> Value {
    let elems = pop_args(vm, argc);
    Value::Obj(heap_alloc(HostObj::slice(elems)))
}

/// Insert a flat `k0,v0,k1,v1,…` run into `pairs`, a duplicate key overwriting
/// in place so the map keeps its first-mention order — Go's literal rule, and
/// the same one whether the run is the whole literal or a later chunk of it.
fn map_insert_all(map: &mut GoMap, flat: Vec<Value>) {
    let mut it = flat.into_iter();
    while let (Some(k), Some(v)) = (it.next(), it.next()) {
        let mk = map_key(&k);
        let at = map.find(mk.as_ref(), &k);
        map.put(at, mk, k, v);
    }
}

/// `map[K]V{k0: v0, …}` — build a map from popped `k0,v0,k1,v1,…` pairs.
fn b_map_lit(vm: &mut VM, argc: u8) -> Value {
    let flat = pop_args(vm, argc);
    let mut m = GoMap::default();
    map_insert_all(&mut m, flat);
    Value::Obj(heap_alloc(HostObj::Map(m)))
}

/// [`GLIT_EXTEND`] — append a further chunk of a composite literal to the
/// container the earlier chunks built, and hand the container back.
///
/// The three literal builtins each take their items as call arguments, and
/// fusevm's arity byte stops one call at 255 stack values, so a longer literal
/// arrives here in pieces. What a piece means is read off the container: slice
/// elements, `k,v` map pairs, or `name,value` struct fields.
fn b_lit_extend(vm: &mut VM, argc: u8) -> Value {
    let mut args = pop_args(vm, argc);
    if args.is_empty() {
        return Value::Undef;
    }
    let container = args.remove(0);
    let Value::Obj(id) = container else {
        return container;
    };
    // Which composite this is, read and released before anything is written:
    // a map merge calls `key_eq` and a struct field name calls `go_str`, both
    // of which read the heap themselves, and the borrow is not re-entrant.
    enum Kind {
        Slice,
        Map,
        Struct,
        Other,
    }
    let id = follow(id);
    let kind = HEAP.with(|h| match h.borrow().get(id as usize) {
        Some(HostObj::Slice { .. }) => Kind::Slice,
        Some(HostObj::Map(_)) => Kind::Map,
        Some(HostObj::Struct { .. }) => Kind::Struct,
        _ => Kind::Other,
    });
    match kind {
        Kind::Slice => HEAP.with(|h| {
            if let Some(HostObj::Slice { elems, .. }) = h.borrow_mut().get_mut(id as usize) {
                elems.append(&mut args);
            }
        }),
        Kind::Map => {
            let mut pairs = HEAP.with(|h| match h.borrow_mut().get_mut(id as usize) {
                Some(HostObj::Map(p)) => std::mem::take(p),
                _ => GoMap::default(),
            });
            map_insert_all(&mut pairs, args);
            HEAP.with(|h| {
                if let Some(HostObj::Map(slot)) = h.borrow_mut().get_mut(id as usize) {
                    *slot = pairs;
                }
            });
        }
        Kind::Struct => {
            let mut more = Vec::with_capacity(args.len() / 2);
            let mut it = args.into_iter();
            while let (Some(name), Some(val)) = (it.next(), it.next()) {
                more.push((go_str(&name), val));
            }
            HEAP.with(|h| {
                if let Some(HostObj::Struct { fields, .. }) = h.borrow_mut().get_mut(id as usize) {
                    fields.append(&mut more);
                }
            });
        }
        Kind::Other => {}
    }
    container
}

/// [`GARRAY_TAG`] — stamp the written `[N]T` on an array object, so `%T` and
/// `%#v` name the array rather than guessing a slice from its elements.
fn b_array_tag(vm: &mut VM, argc: u8) -> Value {
    let args = pop_args(vm, argc);
    let ty = args.get(1).map(go_str).unwrap_or_default();
    let val = args.first().cloned().unwrap_or(Value::Undef);
    if let Value::Obj(id) = val {
        HEAP.with(|h| {
            if let Some(HostObj::Slice { arr_ty, .. }) = h.borrow_mut().get_mut(id as usize) {
                *arr_ty = Some(ty);
            }
        });
    }
    val
}

/// `x[i]` — slice index (bounds-checked) or map lookup (zero value if absent).
fn b_index_get(vm: &mut VM, argc: u8) -> Value {
    let args = pop_args(vm, argc);
    let recv = args.first().cloned().unwrap_or(Value::Undef);
    let key = args.get(1).cloned().unwrap_or(Value::Undef);
    let id = match recv {
        Value::Obj(id) => id,
        // Indexing a string yields the byte at that position (Go: byte value).
        Value::Str(ref s) => {
            let i = key.to_int();
            let len = s.len();
            return match usize::try_from(i).ok().and_then(|i| s.as_bytes().get(i)) {
                Some(b) => Value::Int(*b as i64),
                None => {
                    runtime_panic(vm, format!("index out of range [{i}] with length {len}"));
                    Value::Undef
                }
            };
        }
        _ => {
            ffi_fault(vm, "go-rs: invalid index of nil".to_string());
            return Value::Undef;
        }
    };
    // A slice or sub-slice view: index into its backing (bounds-checked).
    if let Some((_, _, len)) = slice_backing(id) {
        let i = key.to_int();
        return match usize::try_from(i).ok().filter(|&i| i < len) {
            Some(i) => slice_get(id, i).unwrap_or(Value::Undef),
            None => {
                runtime_panic(vm, format!("index out of range [{i}] with length {len}"));
                Value::Undef
            }
        };
    }
    // A nil map reads like an empty one (every key is absent, yielding the
    // value type's zero); only writing to it is a fault.
    let is_map = HEAP.with(|h| {
        matches!(
            h.borrow().get(id as usize),
            Some(HostObj::Map(_))
                | Some(HostObj::Nil {
                    kind: NilKind::Map,
                    ..
                })
        )
    });
    if !is_map {
        ffi_fault(vm, "go-rs: invalid index target".to_string());
        return Value::Undef;
    }
    match map_find_index(id, &key) {
        Some(i) => HEAP.with(|h| match h.borrow().get(id as usize) {
            Some(HostObj::Map(m)) => m[i].1.clone(),
            _ => Value::Undef,
        }),
        // Go returns the value type's zero for a missing key. The map does not
        // carry its value type, so the compiler passes that zero as a third
        // argument wherever the receiver is statically a map; without one, the
        // integer `0` stands, which is right for the numeric case.
        None => args.get(2).cloned().unwrap_or(Value::Int(0)),
    }
}

/// `append(base, xs...)` — a fresh slice of `base`'s elements then every element
/// of each spread slice argument. Collecting into a new backing matches Go's
/// observable result (a caller reassigns the return value).
fn b_append_spread(vm: &mut VM, argc: u8) -> Value {
    let args = pop_args(vm, argc);
    // Argument 0 is the compiler's answer to "is the element type a Go *value*
    // type, and if so which?", from the static type of the spread operand —
    // empty when it is a reference type. Deciding it here instead, by looking at
    // what the elements happen to be, would copy a `[]*T`'s pointers and share a
    // `[][2]int`'s arrays; Go does the opposite of both.
    let elem_ty = args.first().map(go_str).unwrap_or_default();
    let mut out: Vec<Value> = Vec::new();
    let mut extend_from = |v: &Value| {
        if let Value::Obj(id) = v {
            if let Some((_, _, len)) = slice_backing(*id) {
                for i in 0..len {
                    if let Some(e) = slice_get(*id, i) {
                        // `append(dst, src...)` copies each element of `src`, so a
                        // value element in the result is independent of `src`'s.
                        out.push(if elem_ty.is_empty() {
                            e
                        } else {
                            value_copy(e, &elem_ty)
                        });
                    }
                }
            }
        }
    };
    // Then the base slice (nil → empty), then the spread slices (normally
    // exactly one).
    for a in args.iter().skip(1) {
        extend_from(a);
    }
    Value::Obj(heap_alloc(HostObj::slice(out)))
}

/// `[map, key]` → `[value, present]` for the comma-ok map lookup `v, ok := m[k]`.
fn b_map_get2(vm: &mut VM, argc: u8) -> Value {
    let args = pop_args(vm, argc);
    let recv = args.first().cloned().unwrap_or(Value::Undef);
    let key = args.get(1).cloned().unwrap_or(Value::Undef);
    // The value a missing key yields — the value type's zero, which only the
    // compiler knows and passes as a third argument. See [`b_index_get`].
    let absent = args.get(2).cloned().unwrap_or(Value::Int(0));
    let (val, present) = match recv {
        Value::Obj(id) => match map_find_index(id, &key) {
            Some(i) => HEAP.with(|h| match h.borrow().get(id as usize) {
                Some(HostObj::Map(m)) => (m[i].1.clone(), true),
                _ => (absent.clone(), false),
            }),
            None => (absent, false),
        },
        _ => (absent, false),
    };
    Value::Obj(heap_alloc(HostObj::slice(vec![val, Value::bool(present)])))
}

/// `x[i] = v` — slice element write (bounds-checked) or map insert. Returns `v`.
fn b_index_set(vm: &mut VM, argc: u8) -> Value {
    let args = pop_args(vm, argc);
    let recv = args.first().cloned().unwrap_or(Value::Undef);
    let key = args.get(1).cloned().unwrap_or(Value::Undef);
    let val = args.get(2).cloned().unwrap_or(Value::Undef);
    let id = match recv {
        Value::Obj(id) => id,
        _ => {
            ffi_fault(vm, "go-rs: assignment to entry in nil".to_string());
            return Value::Undef;
        }
    };
    // A slice or sub-slice view: write through the backing array at `offset+i`.
    if let Some((backing, offset, len)) = slice_backing(id) {
        let i = key.to_int();
        let err = match usize::try_from(i).ok().filter(|&i| i < len) {
            Some(i) => HEAP.with(|h| {
                if let Some(HostObj::Slice { elems: a, .. }) =
                    h.borrow_mut().get_mut(backing as usize)
                {
                    a[offset + i] = val.clone();
                }
                None::<String>
            }),
            None => Some(format!("index out of range [{i}] with length {len}")),
        };
        return match err {
            None => val,
            Some(msg) => {
                runtime_panic(vm, msg);
                Value::Undef
            }
        };
    }
    // Go's nil map is readable but not writable.
    if nil_kind(&recv) == Some(NilKind::Map) {
        plain_panic(vm, "assignment to entry in nil map".to_string());
        return Value::Undef;
    }
    // Find an existing key (by value) without holding the borrow across key_eq,
    // then insert/overwrite under a fresh mutable borrow.
    let existing = map_find_index(id, &key);
    let mk = map_key(&key);
    let err = HEAP.with(|h| {
        let mut h = h.borrow_mut();
        match h.get_mut(id as usize) {
            Some(HostObj::Map(m)) => {
                m.put(existing, mk, key.clone(), val.clone());
                None
            }
            _ => Some("go-rs: invalid assignment target".to_string()),
        }
    });
    match err {
        None => val,
        Some(msg) => {
            // A Go runtime fault (index OOB) is recoverable; an internal type
            // error keeps its `go-rs:` prefix and aborts.
            if msg.starts_with("go-rs:") {
                ffi_fault(vm, msg);
            } else {
                runtime_panic(vm, msg);
            }
            Value::Undef
        }
    }
}

/// `len(x)` — slice/map element count or string byte length.
fn b_len(vm: &mut VM, argc: u8) -> Value {
    let args = pop_args(vm, argc);
    match args.first() {
        Some(Value::Str(s)) => Value::Int(s.len() as i64),
        Some(Value::Obj(id)) => {
            if let Some((_, _, len)) = slice_backing(*id) {
                return Value::Int(len as i64);
            }
            HEAP.with(|h| match h.borrow().get(*id as usize) {
                Some(HostObj::Map(m)) => Value::Int(m.len() as i64),
                _ => Value::Int(0),
            })
        }
        _ => Value::Int(0),
    }
}

/// `cap(x)` — a slice's capacity: normally the room left in its backing array
/// (a sub-slice can grow into it without reallocating, like Go), or the smaller
/// bound a three-index slice `s[lo:hi:max]` recorded.
fn b_cap(vm: &mut VM, argc: u8) -> Value {
    let args = pop_args(vm, argc);
    match args.first() {
        Some(Value::Str(s)) => Value::Int(s.len() as i64),
        Some(Value::Obj(id)) => Value::Int(slice_cap(*id).unwrap_or(0) as i64),
        _ => Value::Int(0),
    }
}

/// Go's `runtime.nextslicecap`: the capacity a slice grows to when an append
/// pushes its length to `new_len` past a capacity of `old_cap`.
///
/// Port of `runtime/slice.go`:
///
/// ```text
/// newcap := oldCap
/// doublecap := newcap + newcap
/// if newLen > doublecap { return newLen }
/// const threshold = 256
/// if oldCap < threshold { return doublecap }
/// for { newcap += (newcap + 3*threshold) >> 2; if newcap >= newLen { break } }
/// return newcap
/// ```
///
/// Go then rounds the byte size up to a malloc size class, which go-rs does not
/// model (it has no sized allocator), so `cap` can read low for an append that
/// grows by more than double into a non-size-class length.
fn next_slice_cap(new_len: usize, old_cap: usize) -> usize {
    let doublecap = old_cap.saturating_mul(2);
    if new_len > doublecap {
        return new_len;
    }
    const THRESHOLD: usize = 256;
    if old_cap < THRESHOLD {
        return doublecap;
    }
    let mut newcap = old_cap;
    loop {
        newcap += (newcap + 3 * THRESHOLD) >> 2;
        if newcap >= new_len {
            return newcap;
        }
    }
}

/// Grow `elems` into a fresh backing array sized by [`next_slice_cap`] and
/// return a view of the live prefix — the reallocating half of Go's
/// `growslice`. The spare room is zero-filled with the slice's own element
/// shape so later appends have somewhere to land.
fn grow_slice(elems: Vec<Value>, old_cap: usize) -> Value {
    let new_len = elems.len();
    let cap = next_slice_cap(new_len, old_cap);
    // Go zeroes the tail of the new array; go-rs has no element type here, so
    // the filler is the untyped zero. It is never readable through the returned
    // slice (indices past `len` are out of bounds) — only a later append or a
    // re-slice within `cap` can reach it, and both overwrite it first.
    let mut backing = elems;
    backing.resize(cap, Value::Int(0));
    let id = heap_alloc(HostObj::slice(backing));
    Value::Obj(heap_alloc(HostObj::SliceView {
        backing: id,
        offset: 0,
        len: new_len,
        cap,
    }))
}

/// `append(s, elems...)` — return a slice with `elems` appended, growing the
/// backing array per Go's `growslice` when the capacity is exhausted.
/// A nil slice (non-handle) is treated as empty, so `append(nil, x)` allocates.
fn b_append(vm: &mut VM, argc: u8) -> Value {
    let mut args = pop_args(vm, argc);
    if args.is_empty() {
        return Value::Undef;
    }
    let recv = args.remove(0);
    match recv {
        Value::Obj(id) => {
            // Appending to a sub-slice view reallocates into a fresh slice (its
            // own backing), so it never clobbers the parent's elements.
            let is_view = HEAP
                .with(|h| matches!(h.borrow().get(id as usize), Some(HostObj::SliceView { .. })));
            if is_view {
                let (backing, offset, len) = match slice_backing(id) {
                    Some(t) => t,
                    None => return Value::Obj(heap_alloc(HostObj::slice(args))),
                };
                let new_len = len + args.len();
                // Go semantics: if the view has spare capacity, the new elements
                // are written *in place* (clobbering the parent's data there);
                // exhausting the capacity forces a reallocation into a fresh,
                // independent slice. A three-index view's capacity stops short of
                // the backing's end, which is exactly what makes `s[a:b:b]`
                // guarantee the next append cannot clobber the parent.
                let cap = slice_cap(id).unwrap_or(0);
                if new_len <= cap {
                    HEAP.with(|h| {
                        if let Some(HostObj::Slice { elems: a, .. }) =
                            h.borrow_mut().get_mut(backing as usize)
                        {
                            for (k, v) in args.into_iter().enumerate() {
                                a[offset + len + k] = v;
                            }
                        }
                    });
                    return Value::Obj(heap_alloc(HostObj::SliceView {
                        backing,
                        offset,
                        len: new_len,
                        cap,
                    }));
                }
                let mut out = HEAP.with(|h| match h.borrow().get(backing as usize) {
                    Some(HostObj::Slice { elems: a, .. }) => a[offset..offset + len].to_vec(),
                    _ => Vec::new(),
                });
                out.extend(args);
                return grow_slice(out, cap);
            }
            // A plain slice is its own backing with `cap == len`, so every append
            // reallocates — as it does in Go, which is why `b := append(a, x)`
            // does not alias `a`. The new backing carries Go's growth headroom,
            // so the following appends land in place and `cap` doubles the way
            // Go's does.
            let existing = HEAP.with(|h| match h.borrow().get(id as usize) {
                Some(HostObj::Slice { elems: a, .. }) => Some(a.clone()),
                // `append` to a nil slice allocates, exactly as Go's does.
                Some(HostObj::Nil {
                    kind: NilKind::Slice,
                    ..
                }) => Some(Vec::new()),
                _ => None,
            });
            if let Some(mut out) = existing {
                let old_cap = out.len();
                out.extend(args);
                return grow_slice(out, old_cap);
            }
            {
                ffi_fault(
                    vm,
                    "go-rs: first argument to append must be a slice".to_string(),
                );
                Value::Undef
            }
        }
        _ => Value::Obj(heap_alloc(HostObj::slice(args))),
    }
}

/// `delete(m, k)` — remove key `k` from map `m` (no-op if absent).
fn b_delete(vm: &mut VM, argc: u8) -> Value {
    let args = pop_args(vm, argc);
    let recv = args.first().cloned().unwrap_or(Value::Undef);
    let key = args.get(1).cloned().unwrap_or(Value::Undef);
    if let Value::Obj(id) = recv {
        // Locate the key (by value) before taking the mutable borrow, so a
        // struct-key `key_eq` doesn't re-enter the heap.
        if let Some(i) = map_find_index(id, &key) {
            HEAP.with(|h| {
                if let Some(HostObj::Map(m)) = h.borrow_mut().get_mut(id as usize) {
                    m.remove(i);
                }
            });
        }
    }
    Value::Undef
}

/// `T{f0: v0, …}` — build a struct (stack: typeName, f0name, f0val, …).
fn b_struct_new(vm: &mut VM, argc: u8) -> Value {
    let args = pop_args(vm, argc);
    let type_name = args.first().map(go_str).unwrap_or_default();
    let mut fields = Vec::new();
    let mut it = args.into_iter().skip(1);
    while let (Some(name), Some(val)) = (it.next(), it.next()) {
        fields.push((go_str(&name), val));
    }
    Value::Obj(heap_alloc(HostObj::Struct {
        type_name,
        fields,
        by_ref: false,
    }))
}

/// A host-built error value: the same `&$errorString{s: …}` shape `fmt.Errorf`
/// builds and `errors.New` returns, so `fmt` renders it through the synthesized
/// `Error()` method and `err != nil` and `err == err` behave like any other Go
/// error. `strconv`'s `ErrSyntax` / `ErrRange` sentinels are these; the
/// `*strconv.NumError` wrapping them is [`stdlib::num_error`].
pub(crate) fn make_error(msg: String) -> Value {
    Value::Obj(heap_alloc(HostObj::Struct {
        type_name: "$errorString".to_string(),
        fields: vec![("s".to_string(), Value::str(msg))],
        by_ref: true,
    }))
}

/// `[value]` → the same handle, marked as a pointer. Emitted for `&T{…}` and
/// `new(T)`, whose results Go compares by address rather than field by field.
fn b_ptr_mark(vm: &mut VM, argc: u8) -> Value {
    let args = pop_args(vm, argc);
    let v = args.first().cloned().unwrap_or(Value::Undef);
    if let Value::Obj(id) = v {
        HEAP.with(|h| {
            if let Some(HostObj::Struct { by_ref, .. }) = h.borrow_mut().get_mut(id as usize) {
                *by_ref = true;
            }
        });
    }
    v
}

/// Resolve a handle through any [`HostObj::Ptr`] indirection to the object it
/// points at. Every reader that wants the *pointed-to* struct goes through this;
/// the two that must not are [`struct_bind`], which has to hand a pointer back
/// unchanged, and pointer equality, which compares the target rather than
/// reading it.
///
/// The bound terminates a `**T` chain and a cycle alike; nothing in go-rs builds
/// either, so it is a guard rather than a limit.
fn follow(id: u32) -> u32 {
    let mut cur = id;
    for _ in 0..16 {
        let next = HEAP.with(|h| match h.borrow().get(cur as usize) {
            Some(HostObj::Ptr { target }) => Some(*target),
            _ => None,
        });
        match next {
            Some(t) => cur = t,
            None => break,
        }
    }
    cur
}

/// The identity Go's `==` compares two pointers by: a [`HostObj::Ptr`]'s target
/// (so two `&x` of one variable are equal and `&x` / `&y` are not), or a
/// `by_ref` handle itself (so two `&T{…}` are distinct). `None` for anything
/// that is not a pointer.
fn ptr_identity(v: &Value) -> Option<u32> {
    let Value::Obj(id) = v else { return None };
    HEAP.with(|h| match h.borrow().get(*id as usize) {
        Some(HostObj::Ptr { target }) => Some(*target),
        Some(HostObj::Struct { by_ref: true, .. }) => Some(*id),
        _ => None,
    })
}

/// Whether `v` is a handle Go would compare by address (a `&T{…}` pointer).
fn is_ptr(v: &Value) -> bool {
    ptr_identity(v).is_some()
}

/// Go's `==` on two values go-rs holds as heap handles. A pointer compares by
/// address — here, by handle — so two separately allocated errors with the same
/// message are distinct; anything else falls back to the caller's structural
/// comparison. `None` when neither side is a pointer.
pub(crate) fn ptr_eq(a: &Value, b: &Value) -> Option<bool> {
    if !is_ptr(a) && !is_ptr(b) {
        return None;
    }
    Some(match (ptr_identity(a), ptr_identity(b)) {
        // Both are pointers: equal exactly when they address the same object.
        (Some(x), Some(y)) => x == y,
        // One is; a pointer never equals a non-pointer.
        _ => false,
    })
}

/// Go's `==` on two interface operands: dynamic type first, value second.
///
/// The type half is `go_type_name`, which is what `%T` prints — so a `pt` and
/// a `qt` with the same field are different types and unequal, and an interface
/// holding a nil *slice* is a `[]int` rather than a `<nil>` and so is not equal
/// to the untyped `nil` (Go's non-nil-interface-holding-nil rule falls out of
/// this rather than needing a case of its own).
///
/// The value half runs only once the types agree, so it never has to reconcile
/// two representations. A pointer compares by handle, the scalars by value, and
/// anything left — two structs of the same type, two strings — structurally,
/// which is the comparison the ordinary `==` path already performed. `Float` is
/// compared as `f64` rather than through the rendered string so that Go's
/// `NaN != NaN` survives.
///
/// Two interfaces holding a *slice*, *map* or *func* panic in Go ("comparing
/// uncomparable type []int"); go-rs answers structurally instead, which is
/// unchanged by this and not a case this function adds.
pub fn iface_eq(a: &Value, b: &Value) -> bool {
    if go_type_name(a) != go_type_name(b) {
        return false;
    }
    if let Some(same) = ptr_eq(a, b) {
        return same;
    }
    match (a, b) {
        (Value::Undef, Value::Undef) => true,
        (Value::Bool(x), Value::Bool(y)) => x == y,
        (Value::Int(x), Value::Int(y)) => x == y,
        (Value::Float(x), Value::Float(y)) => x == y,
        _ => go_str(a) == go_str(b),
    }
}

/// [`GIFACE_EQ`] — `[a, b, ne]` → the interface comparison, negated when `ne`.
fn b_iface_eq(vm: &mut VM, argc: u8) -> Value {
    let args = pop_args(vm, argc);
    let a = args.first().cloned().unwrap_or(Value::Undef);
    let b = args.get(1).cloned().unwrap_or(Value::Undef);
    let ne = args.get(2).map(|v| v.to_int() != 0).unwrap_or(false);
    Value::bool(iface_eq(&a, &b) != ne)
}

/// `s.field` read on a struct.
fn b_field_get(vm: &mut VM, argc: u8) -> Value {
    let args = pop_args(vm, argc);
    let recv = args.first().cloned().unwrap_or(Value::Undef);
    let name = args.get(1).map(go_str).unwrap_or_default();
    let id = match recv {
        Value::Obj(id) => id,
        _ => {
            runtime_panic(vm, "invalid memory address or nil pointer dereference");
            return Value::Undef;
        }
    };
    let id = follow(id);
    HEAP.with(|h| {
        let h = h.borrow();
        match h.get(id as usize) {
            Some(HostObj::Struct { fields, .. }) => {
                if let Some((_, v)) = fields.iter().find(|(f, _)| *f == name) {
                    return v.clone();
                }
                // Not a direct field — try an embedded one (Go field promotion).
                match embedded_field_owner(&h, id, &name) {
                    Some(owner) => match h.get(owner as usize) {
                        Some(HostObj::Struct { fields, .. }) => fields
                            .iter()
                            .find(|(f, _)| *f == name)
                            .map(|(_, v)| v.clone())
                            .unwrap_or(Value::Undef),
                        _ => Value::Undef,
                    },
                    None => Value::Undef,
                }
            }
            _ => {
                ffi_fault(vm, format!("go-rs: no field `{name}`"));
                Value::Undef
            }
        }
    })
}

/// Find the struct that declares `name` through `root`'s embedded fields, and
/// return its heap id. An embedded field is one whose field name equals the
/// type name of the struct it holds — exactly how the parser records
/// `struct { Base }`. The search is breadth-first so the shallowest depth wins,
/// matching Go's promotion rule.
fn embedded_field_owner(heap: &[HostObj], root: u32, name: &str) -> Option<u32> {
    let mut frontier = vec![root];
    // Embedding is acyclic in Go (a struct cannot embed itself by value), so
    // the walk terminates on the type graph's depth.
    for _ in 0..16 {
        let mut next = Vec::new();
        for id in frontier {
            let Some(HostObj::Struct { fields, .. }) = heap.get(id as usize) else {
                continue;
            };
            for (fname, fval) in fields {
                let Value::Obj(inner) = fval else { continue };
                let Some(HostObj::Struct {
                    type_name,
                    fields: inner_fields,
                    ..
                }) = heap.get(*inner as usize)
                else {
                    continue;
                };
                if fname != type_name {
                    continue; // a named field, not an embedded one
                }
                if inner_fields.iter().any(|(f, _)| f == name) {
                    return Some(*inner);
                }
                next.push(*inner);
            }
        }
        if next.is_empty() {
            return None;
        }
        frontier = next;
    }
    None
}

/// `s.field = v` write on a struct. Returns `v`.
fn b_field_set(vm: &mut VM, argc: u8) -> Value {
    let args = pop_args(vm, argc);
    let recv = args.first().cloned().unwrap_or(Value::Undef);
    let name = args.get(1).map(go_str).unwrap_or_default();
    let val = args.get(2).cloned().unwrap_or(Value::Undef);
    let id = match recv {
        Value::Obj(id) => id,
        _ => {
            runtime_panic(vm, "invalid memory address or nil pointer dereference");
            return Value::Undef;
        }
    };
    // Resolved before the mutable borrow: `follow` takes a shared one.
    let id = follow(id);
    let ok = HEAP.with(|h| {
        let mut h = h.borrow_mut();
        // A write to a promoted field lands on the embedded struct that owns
        // it, not as a new field on the outer one.
        let target = match h.get(id as usize) {
            Some(HostObj::Struct { fields, .. }) if !fields.iter().any(|(f, _)| *f == name) => {
                embedded_field_owner(&h, id, &name).unwrap_or(id)
            }
            _ => id,
        };
        match h.get_mut(target as usize) {
            Some(HostObj::Struct { fields, .. }) => {
                if let Some(slot) = fields.iter_mut().find(|(f, _)| *f == name) {
                    slot.1 = val.clone();
                } else {
                    fields.push((name.clone(), val.clone()));
                }
                true
            }
            _ => false,
        }
    });
    if ok {
        val
    } else {
        ffi_fault(vm, format!("go-rs: cannot assign field `{name}`"));
        Value::Undef
    }
}

/// Copy a struct value (Go value semantics). Non-struct values pass through
/// unchanged (slices/maps are reference types and must NOT be copied).
fn b_struct_copy(vm: &mut VM, argc: u8) -> Value {
    let args = pop_args(vm, argc);
    struct_copy(args.first().cloned().unwrap_or(Value::Undef))
}

fn b_write_fd(vm: &mut VM, argc: u8) -> Value {
    use std::io::Write as _;
    let args = pop_args(vm, argc);
    let text = args.get(1).map(go_str).unwrap_or_default();
    // Unbuffered, and flushed at once, so a write to `os.Stdout` interleaves
    // with `fmt.Println` in the order the program made them.
    match args.first().map(|v| v.to_int()) {
        Some(2) => {
            let mut e = std::io::stderr();
            let _ = e.write_all(text.as_bytes());
            let _ = e.flush();
        }
        _ => {
            let mut o = std::io::stdout();
            let _ = o.write_all(text.as_bytes());
            let _ = o.flush();
        }
    }
    Value::Undef
}

fn b_ptr_to(vm: &mut VM, argc: u8) -> Value {
    let args = pop_args(vm, argc);
    let v = args.first().cloned().unwrap_or(Value::Undef);
    match v {
        // A pointer to a heap object addresses that object. Two `&x` of the
        // same variable are two `Ptr`s with one target, which is what makes
        // them `==` while `&x` and `&y` are not.
        Value::Obj(id) => Value::Obj(heap_alloc(HostObj::Ptr { target: id })),
        // A scalar is not a heap object, so there is no address to take and the
        // value stands in for the pointer — the gap BUGS.md records.
        other => other,
    }
}

fn b_deref_set(vm: &mut VM, argc: u8) -> Value {
    let args = pop_args(vm, argc);
    let target = args.first().cloned().unwrap_or(Value::Undef);
    let val = args.get(1).cloned().unwrap_or(Value::Undef);
    let Value::Obj(id) = target else {
        // `&x` on a scalar yields the value, not an address, so there is nothing
        // to write back through. Naming that beats faulting as if the pointer
        // were nil, which is what the address it never took would look like.
        ffi_fault(vm, "go-rs: cannot assign through a pointer to a non-composite value (`&x` on a scalar has no address)".to_string(),);
        return Value::Undef;
    };
    // The *pointee* is overwritten rather than rebound, which is what makes the
    // write visible through every other pointer to it and through the variable
    // itself. The source is copied first: `*p = q` must not alias `q`'s fields.
    let id = follow(id);
    let fields = match struct_copy(val.clone()) {
        Value::Obj(src) => HEAP.with(|h| match h.borrow().get(follow(src) as usize) {
            Some(HostObj::Struct { fields, .. }) => Some(fields.clone()),
            _ => None,
        }),
        _ => None,
    };
    match fields {
        Some(f) => HEAP.with(|h| {
            if let Some(HostObj::Struct { fields, .. }) = h.borrow_mut().get_mut(id as usize) {
                *fields = f;
            }
        }),
        None => {
            ffi_fault(vm, "go-rs: cannot assign through a pointer to a non-composite value (`&x` on a scalar has no address)".to_string(),);
        }
    }
    val
}

fn b_struct_bind(vm: &mut VM, argc: u8) -> Value {
    let args = pop_args(vm, argc);
    struct_bind(args.first().cloned().unwrap_or(Value::Undef))
}

/// One value copy at a bind site: the identity on a pointer, a struct copy on
/// anything else.
///
/// `&T{…}` and `new(T)` mark their handle `by_ref` ([`HostObj::Struct`]) — it is
/// a Go pointer, and Go copies nothing through one. Copying it anyway is what
/// made `q := p`, `f(p)` and `[]*T{p}` each hand out a private struct whose
/// writes the original never saw.
///
/// The inverse — `*p`, which asks for the pointed-to *value* — keeps
/// [`struct_copy`], and so does a value-receiver method call. Those are the two
/// places a pointer is deliberately turned back into a value.
fn struct_bind(v: Value) -> Value {
    let Value::Obj(id) = v else { return v };
    let is_ptr = HEAP.with(|h| {
        matches!(
            h.borrow().get(id as usize),
            Some(HostObj::Struct { by_ref: true, .. }) | Some(HostObj::Ptr { .. })
        )
    });
    if is_ptr {
        return v;
    }
    struct_copy(v)
}

/// Copy a fixed-size array value (Go array value semantics), given its written
/// element type. Non-array values pass through unchanged.
fn b_array_copy(vm: &mut VM, argc: u8) -> Value {
    let args = pop_args(vm, argc);
    let elem_ty = args.get(1).map(go_str).unwrap_or_default();
    array_copy(args.first().cloned().unwrap_or(Value::Undef), &elem_ty)
}

/// One value copy for a value of written type `ty`: an array copy when `ty` is
/// `[N]T`, a struct copy otherwise (which is the identity on anything that is
/// not a struct, so a scalar or a reference passes straight through).
fn value_copy(v: Value, ty: &str) -> Value {
    match crate::ast::array_elem_ty(ty) {
        Some(elem) => array_copy(v, elem),
        // A bind, not a copy: a `[2]*T` array's elements are pointers, and Go
        // copies the array while sharing what each element points at.
        None => struct_bind(v),
    }
}

/// One fixed-size array copy, recursing into elements that are themselves
/// values.
///
/// Go copies an array elementwise: `b := a` on a `[2][3]int` gives two
/// independent 2×3 arrays, and on a `[2]pt` two independent structs — while a
/// `[2][]int`'s two slices stay shared, because a slice is a reference. Which of
/// those an element is cannot be read off the heap (an array and a slice are the
/// same [`HostObj::Slice`]), so `elem_ty` — the *written* element type — decides
/// it. A struct element needs no such hint: [`struct_copy`] recognises one at
/// run time and is the identity on everything else.
/// The copy also inherits the source's `[N]T` tag, so `%T` still names the
/// array after it has been assigned, passed, returned or boxed into an `any` —
/// every one of which goes through a copy.
fn array_copy(v: Value, elem_ty: &str) -> Value {
    let Value::Obj(id) = v else { return v };
    let Some((_, _, len)) = slice_backing(id) else {
        return v;
    };
    let elems: Vec<Value> = (0..len)
        .map(|i| value_copy(slice_get(id, i).unwrap_or(Value::Undef), elem_ty))
        .collect();
    let arr_ty = HEAP.with(|h| match h.borrow().get(id as usize) {
        Some(HostObj::Slice { arr_ty, .. }) => arr_ty.clone(),
        _ => None,
    });
    Value::Obj(heap_alloc(HostObj::Slice {
        elems,
        arr_ty,
        elem_ty: None,
    }))
}

/// One struct value copy, recursing into the fields [`STRUCT_PLAN`] records as
/// struct-valued.
///
/// Go copies a struct *transitively*: assigning `outer{inner{1}, 2}` copies the
/// embedded `inner` too, so a write through the copy is invisible to the
/// original at every depth. Cloning only the field vector would share the
/// nested struct's handle and let a write through the copy reach the original
/// (`y := x; y.I.N = 8` used to change `x.I.N`).
///
/// The recursion is bounded without a visited set: Go rejects a struct type
/// that contains itself by value ("invalid recursive type"), so the plan graph
/// over value fields is a DAG. A pointer field breaks the cycle and is shared,
/// which is what makes a self-referential `*T` node type copy correctly.
fn struct_copy(v: Value) -> Value {
    let Value::Obj(id) = v else { return v };
    let id = follow(id);
    let parts = HEAP.with(|h| match h.borrow().get(id as usize) {
        Some(HostObj::Struct {
            type_name, fields, ..
        }) => Some((type_name.clone(), fields.clone())),
        // slice/map/other: a reference type, share the handle.
        _ => None,
    });
    let Some((type_name, mut fields)) = parts else {
        return v;
    };
    let nested = STRUCT_PLAN.with(|p| p.borrow().get(&type_name).cloned().unwrap_or_default());
    for (name, fv) in fields.iter_mut() {
        if let Some((_, fty)) = nested.iter().find(|(n, _)| n == name) {
            *fv = value_copy(fv.clone(), fty);
        }
    }
    // The copy is a struct *value*, never the pointer it was taken from, so it
    // compares field-wise again.
    Value::Obj(heap_alloc(HostObj::Struct {
        type_name,
        fields,
        by_ref: false,
    }))
}

/// Install the builtins plus the debug line-marker used by `go --dap`. The
/// marker fires synchronously at each statement and delegates to the DAP server,
/// which pauses in place on a breakpoint or step target.
pub fn install_debug(vm: &mut VM) {
    install(vm);
    vm.register_builtin(DBG_LINE, b_dbg_line);
}

thread_local! {
    /// Set by an inline-Rust FFI fault (compile error, call error, or an
    /// unresolved export). A builtin cannot return a `Result`, so it stashes the
    /// message here and halts the VM; [`crate::run_str`] reads it after
    /// `VM::run` returns and surfaces it as a `go-rs:` error.
    static FFI_ERROR: RefCell<Option<String>> = const { RefCell::new(None) };

    /// Whether the running program uses `recover`, so a runtime fault (divide by
    /// zero, index out of range, nil dereference) should become a *recoverable*
    /// panic (set `PANIC` and let the compiler's unwind machinery run) rather than
    /// aborting the VM. Set once at program start by `GSET_PANIC_MODE`.
    static PANIC_MODE: RefCell<bool> = const { RefCell::new(false) };
}

/// Take and clear any pending FFI-fault message.
pub fn take_ffi_error() -> Option<String> {
    FFI_ERROR.with(|e| e.borrow_mut().take())
}

/// Record an FFI fault and halt the VM; the message surfaces after the run.
fn ffi_fault(vm: &mut VM, msg: impl Into<String>) {
    FFI_ERROR.with(|e| *e.borrow_mut() = Some(msg.into()));
    vm.request_halt();
}

/// A Go runtime fault. When the program uses `recover` (panic mode), record it as
/// a catchable panic whose value is `runtime error: <msg>` (what Go's `recover()`
/// yields) and let the compiler-emitted unwind checks handle it. Otherwise abort
/// the run with a terse `go-rs:` diagnostic, as before.
fn runtime_panic(vm: &mut VM, msg: impl Into<String>) {
    plain_panic(vm, format!("runtime error: {}", msg.into()));
}

/// A runtime fault whose message Go does *not* prefix with `runtime error: ` —
/// its `runtime.plainError` cases, such as writing to a nil map.
fn plain_panic(vm: &mut VM, full: String) {
    if PANIC_MODE.with(|m| *m.borrow()) {
        // Recoverable: record it and let the compiler's unwind checks run.
        PANIC.with(|p| *p.borrow_mut() = Some(Value::str(full)));
    } else {
        // Unrecovered: print like Go's first line and exit 2. Halt the VM too so
        // no further ops run before the process exits (stdout is flushed first).
        use std::io::Write as _;
        let _ = std::io::stdout().flush();
        eprintln!("panic: {full}");
        vm.request_halt();
        std::process::exit(2);
    }
}

/// `GSET_PANIC_MODE`: enable recoverable runtime faults (the program uses
/// `recover`). Emitted once at program start.
fn b_set_panic_mode(_vm: &mut VM, _argc: u8) -> Value {
    PANIC_MODE.with(|m| *m.borrow_mut() = true);
    Value::Undef
}

/// `__rust_compile("<base64>", line)` builtin: pop the base64-encoded
/// `rust { ... }` block body, compile it to a cdylib, and register its exports.
fn b_ffi_compile(vm: &mut VM, argc: u8) -> Value {
    // The compiler emits `(base64, line)`; the base64 body is the deepest arg.
    let args = pop_args(vm, argc);
    let b64 = args.first().map(go_str).unwrap_or_default();
    if let Err(e) = fusevm::ffi::compile_and_register(&b64) {
        ffi_fault(vm, format!("go-rs: rust {{}} block: {e}"));
    }
    Value::Undef
}

/// `name(args...)` FFI dispatch: pop the function name (top of stack) and its
/// `argc - 1` arguments, call the exported symbol via `fusevm::ffi`, and return
/// its result.
fn b_ffi_call(vm: &mut VM, argc: u8) -> Value {
    let name = vm
        .stack
        .pop()
        .map(|v| v.as_str_cow().into_owned())
        .unwrap_or_default();
    let n = argc.saturating_sub(1) as usize;
    let mut args = Vec::with_capacity(n);
    for _ in 0..n {
        args.push(vm.stack.pop().unwrap_or(Value::Undef));
    }
    args.reverse();
    match fusevm::ffi::try_call(&name, &args) {
        Some(Ok(v)) => v,
        Some(Err(e)) => {
            ffi_fault(vm, format!("go-rs: rust FFI call {name}: {e}"));
            Value::Undef
        }
        None => {
            ffi_fault(vm, format!("go-rs: undefined: {name}"));
            Value::Undef
        }
    }
}

/// The `DBG_LINE` marker builtin: hand control to the DAP server for this line,
/// then return nil (popped by the trailing `Op::Pop` the compiler emits).
fn b_dbg_line(vm: &mut VM, _argc: u8) -> Value {
    crate::dap::on_debug_line(vm);
    Value::Undef
}

/// Pop `argc` values off the VM stack, restoring source (left-to-right) order.
fn pop_args(vm: &mut VM, argc: u8) -> Vec<Value> {
    let mut v = Vec::with_capacity(argc as usize);
    for _ in 0..argc {
        v.push(vm.stack.pop().unwrap_or(Value::Undef));
    }
    v.reverse();
    expand_spread(v)
}

/// Expand a [`HostObj::Spread`] marker back into the operand list it stands for.
///
/// Only a `f(xs...)` into a variadic builtin ever produces one, so the common
/// path is a scan that finds nothing and hands the vector straight back.
fn expand_spread(args: Vec<Value>) -> Vec<Value> {
    let spread = |v: &Value| match v {
        Value::Obj(id) => HEAP.with(|h| match h.borrow().get(*id as usize) {
            Some(HostObj::Spread(xs)) => Some(xs.clone()),
            _ => None,
        }),
        _ => None,
    };
    if !args.iter().any(|v| spread(v).is_some()) {
        return args;
    }
    let mut out = Vec::with_capacity(args.len());
    for v in args {
        match spread(&v) {
            Some(xs) => out.extend(xs),
            None => out.push(v),
        }
    }
    out
}

/// `[slice]` → the slice marked for expansion into a variadic builtin's operands.
fn b_spread(vm: &mut VM, argc: u8) -> Value {
    let args = pop_args(vm, argc);
    let v = args.into_iter().next().unwrap_or(Value::Undef);
    // A nil slice spreads to nothing, which is what Go's `f()` passes.
    let elems = slice_elems(&v).unwrap_or_default();
    Value::Obj(heap_alloc(HostObj::Spread(elems)))
}

/// Render a value the way Go's `fmt` `%v` verb does. Composite values (slices,
/// maps, structs) are looked up on the heap and formatted with Go's bracket /
/// `map[…]` / `{…}` conventions.
pub fn go_str(v: &Value) -> String {
    go_str_mode(v, FmtMode::V)
}

/// Which of `fmt`'s three struct-rendering verbs is being applied. They differ
/// only in how composites and strings are printed, so one walker serves all
/// three.
#[derive(Clone, Copy, PartialEq, Eq)]
pub(crate) enum FmtMode {
    /// `%v` — `{1 a}`, bare strings.
    V,
    /// `%+v` — `{X:1 Y:a}`, field names added, strings still bare.
    PlusV,
    /// `%#v` — `main.P{X:1, Y:"a"}`, Go-syntax with types and quoted strings.
    SharpV,
}

pub(crate) fn go_str_mode(v: &Value, mode: FmtMode) -> String {
    match v {
        Value::Bool(b) => if *b { "true" } else { "false" }.to_string(),
        Value::Int(n) => n.to_string(),
        Value::Float(f) => format_float(*f),
        Value::Str(s) => {
            if mode == FmtMode::SharpV {
                go_quote(s.as_str())
            } else {
                s.as_str().to_string()
            }
        }
        // A nil operand renders as `<nil>` under all three verbs.
        Value::Undef => "<nil>".to_string(),
        Value::Obj(id) => obj_str_mode(*id, mode),
        other => other.as_str_cow().into_owned(),
    }
}

/// `%q` on a string: Go's `strconv.Quote` — double quotes, backslash escapes for
/// the C escapes and `\`/`"`, and `\xNN`/`\uNNNN` for other non-printables.
pub(crate) fn go_quote(s: &str) -> String {
    let mut out = String::with_capacity(s.len() + 2);
    out.push('"');
    for c in s.chars() {
        match c {
            '"' => out.push_str("\\\""),
            '\\' => out.push_str("\\\\"),
            '\n' => out.push_str("\\n"),
            '\t' => out.push_str("\\t"),
            '\r' => out.push_str("\\r"),
            '\u{7}' => out.push_str("\\a"),
            '\u{8}' => out.push_str("\\b"),
            '\u{c}' => out.push_str("\\f"),
            '\u{b}' => out.push_str("\\v"),
            c if !go_is_print(c) => out.push_str(&escape_rune(c as u32)),
            c => out.push(c),
        }
    }
    out.push('"');
    out
}

/// How `strconv.Quote` writes a rune it will not print literally: `\xNN` for a
/// C0 control or `DEL`, `\uNNNN` inside the basic plane and `\UNNNNNNNN` above
/// it. The named escapes (`\n`, `\t`, …) are matched before this is reached.
fn escape_rune(c: u32) -> String {
    match c {
        0..=0x1f | 0x7f => format!("\\x{c:02x}"),
        0..=0xffff => format!("\\u{c:04x}"),
        _ => format!("\\U{c:08x}"),
    }
}

/// Whether Go's `strconv` writes a rune literally inside a quoted string —
/// `unicode.IsPrint`, which is every letter, mark, number, punctuation and
/// symbol plus the ASCII space.
///
/// The three non-printable classes tested here are exact: `Cc` (the C0 and C1
/// controls) is [`char::is_control`], every separator but the ASCII space is
/// [`char::is_whitespace`], and the private-use areas are three fixed ranges.
/// The fourth, `Cn` (code points Unicode has not assigned), needs the category
/// tables Rust's standard library does not expose, so an unassigned rune still
/// prints literally where Go escapes it — see BUGS.md.
fn go_is_print(c: char) -> bool {
    if c.is_control() {
        return false;
    }
    if c == ' ' {
        return true;
    }
    if c.is_whitespace() {
        return false;
    }
    !matches!(c as u32, 0xe000..=0xf8ff | 0xf0000..=0xffffd | 0x100000..=0x10fffd)
}

/// `%q` on an integer: Go quotes it as a rune literal (`'A'`, `'\n'`, `'世'`).
pub(crate) fn go_quote_rune(n: i64) -> String {
    let Some(c) = u32::try_from(n).ok().and_then(char::from_u32) else {
        // Go renders an out-of-range code point as the replacement character.
        return "'\u{fffd}'".to_string();
    };
    let inner = go_quote(&c.to_string());
    // Reuse the string escaper, then swap the delimiters and fix `'`/`"`.
    let body = &inner[1..inner.len() - 1];
    let body = body.replace("\\\"", "\"").replace('\'', "\\'");
    format!("'{body}'")
}

/// `%T`: the value's Go type name. go-rs carries no static element type for a
/// slice or map, so those are described from the values actually present and an
/// empty one falls back to `interface {}`.
///
/// A fixed-size array is the exception: its written `[N]T` is stamped on the
/// object, because the length is not recoverable from the elements and a
/// `[3]int` would otherwise be indistinguishable from a 3-element `[]int`.
pub(crate) fn go_type_name(v: &Value) -> String {
    match v {
        Value::Bool(_) => "bool".to_string(),
        Value::Int(_) => "int".to_string(),
        Value::Float(_) => "float64".to_string(),
        Value::Str(_) => "string".to_string(),
        Value::Undef => "<nil>".to_string(),
        Value::Obj(id) => HEAP.with(|h| {
            let h = h.borrow();
            match h.get(follow(*id) as usize) {
                // A `fmt`-tagged slice names its written element type, which is
                // the only way `[]uint8` and `[]int32` are distinguishable from
                // `[]int` — their elements are all plain integers.
                Some(HostObj::Slice {
                    elems: a,
                    arr_ty: None,
                    elem_ty,
                }) => match elem_ty {
                    Some(t) => format!("[]{}", go_type_spelling(t)),
                    None => format!("[]{}", elem_type_name(a.first())),
                },
                Some(HostObj::Slice {
                    arr_ty: Some(ty), ..
                }) => ty.clone(),
                Some(HostObj::SliceView {
                    backing, offset, ..
                }) => {
                    let e = match h.get(*backing as usize) {
                        Some(HostObj::Slice { elems: a, .. }) => a.get(*offset).cloned(),
                        _ => None,
                    };
                    format!("[]{}", elem_type_name(e.as_ref()))
                }
                Some(HostObj::Map(m)) => format!(
                    "map[{}]{}",
                    elem_type_name(m.first().map(|(k, _)| k)),
                    elem_type_name(m.first().map(|(_, v)| v))
                ),
                // A user-declared struct is qualified by its package, and go-rs
                // only ever compiles `package main`.
                Some(HostObj::Struct { type_name, .. }) => format!("main.{type_name}"),
                Some(HostObj::Closure { .. }) => "func()".to_string(),
                Some(HostObj::Cell(v)) => go_type_name(v),
                // A defined type is named, not described: `main.Weekday`, never
                // the `int` it is represented as.
                Some(HostObj::Named { ty, .. }) => go_type_spelling(ty),
                // A typed nil records the type it was written as, so unlike a
                // populated slice or map it needs no guess from its contents.
                Some(HostObj::Nil { ty, .. }) => go_type_spelling(ty),
                Some(HostObj::F32(_)) => "float32".to_string(),
                Some(HostObj::U64 { ty, .. }) => ty.clone(),
                // Every receive site maps the sentinel away, so it is only
                // reachable if one was missed; name it after what it stands for.
                Some(HostObj::ChanClosed) => "<nil>".to_string(),
                // Unreachable: `follow` resolved any pointer above.
                Some(HostObj::Ptr { .. }) => "<nil>".to_string(),
                // A spread marker is expanded by `pop_args` before any builtin sees it;
                // naming it at all means one leaked, so name it after what it holds.
                Some(HostObj::Spread(xs)) => xs
                    .first()
                    .map(go_type_name)
                    .unwrap_or_else(|| "<nil>".to_string()),
                None => "<nil>".to_string(),
            }
        }),
        _ => "interface {}".to_string(),
    }
}

fn elem_type_name(v: Option<&Value>) -> String {
    match v {
        Some(v) => go_type_name(v),
        None => "interface {}".to_string(),
    }
}

/// How `%T` spells a written type.
///
/// `byte` and `rune` are Go aliases and `%T` prints the type they name, so a
/// `[]byte` is `[]uint8` and a `[]rune` is `[]int32`. Any identifier that is not
/// predeclared names a type declared in the program, which `%T` qualifies by its
/// package — always `main` here. Both are applied per identifier, so
/// `map[string]pt` renames only the element.
pub(crate) fn go_type_spelling(ty: &str) -> String {
    let mut out = String::with_capacity(ty.len());
    let mut word = String::new();
    let flush = |word: &mut String, out: &mut String| {
        match word.as_str() {
            "" => {}
            "byte" => out.push_str("uint8"),
            "rune" => out.push_str("int32"),
            w if is_predeclared_type(w) => out.push_str(w),
            w => {
                out.push_str("main.");
                out.push_str(w);
            }
        }
        word.clear();
    };
    for c in ty.chars() {
        if c.is_alphanumeric() || c == '_' {
            word.push(c);
        } else {
            flush(&mut word, &mut out);
            out.push(c);
        }
    }
    flush(&mut word, &mut out);
    out
}

/// Whether `w` is one of Go's predeclared type names (plus the keywords that
/// open a type literal), which `%T` prints unqualified. Everything else is
/// declared in the program and carries its package.
fn is_predeclared_type(w: &str) -> bool {
    matches!(
        w,
        "bool"
            | "string"
            | "int"
            | "int8"
            | "int16"
            | "int32"
            | "int64"
            | "uint"
            | "uint8"
            | "uint16"
            | "uint32"
            | "uint64"
            | "uintptr"
            | "float32"
            | "float64"
            | "complex64"
            | "complex128"
            | "error"
            | "any"
            | "interface"
            | "struct"
            | "map"
            | "chan"
            | "func"
    ) || w.chars().all(|c| c.is_ascii_digit())
}

/// The element type of a written container type — `[]T` and `[N]T` name `T`,
/// `map[K]V` names `V` — or `None` when the type is not a container. The key of
/// a `map[K]V` can itself carry brackets (`map[[2]int]V`), so the key ends at
/// the `]` closing the one `map[` opened, found by depth.
fn elem_ty_spelling(ty: &str) -> Option<String> {
    if let Some(rest) = ty.strip_prefix("[]") {
        return Some(rest.to_string());
    }
    if let Some(rest) = ty.strip_prefix("map[") {
        let mut depth = 0usize;
        for (i, c) in rest.char_indices() {
            match c {
                '[' => depth += 1,
                ']' if depth == 0 => return Some(rest[i + 1..].to_string()),
                ']' => depth -= 1,
                _ => {}
            }
        }
        return None;
    }
    // `[N]T` — a fixed-size array, whose length is digits between the brackets.
    let rest = ty.strip_prefix('[')?;
    let close = rest.find(']')?;
    rest[..close]
        .chars()
        .all(|c| c.is_ascii_digit())
        .then(|| rest[close + 1..].to_string())
}

/// The map key type of a written `map[K]V`, or `None` for any other type.
fn key_ty_spelling(ty: &str) -> Option<String> {
    let rest = ty.strip_prefix("map[")?;
    let mut depth = 0usize;
    for (i, c) in rest.char_indices() {
        match c {
            '[' => depth += 1,
            ']' if depth == 0 => return Some(rest[..i].to_string()),
            ']' => depth -= 1,
            _ => {}
        }
    }
    None
}

/// [`GNAMED_BOX`] — tag a `fmt` argument with the defined type it was written
/// as. Stack `[value, type]`.
fn b_named_box(vm: &mut VM, argc: u8) -> Value {
    let args = pop_args(vm, argc);
    let inner = args.first().cloned().unwrap_or(Value::Undef);
    let ty = args.get(1).map(go_str).unwrap_or_default();
    Value::Obj(heap_alloc(HostObj::Named { ty, inner }))
}

/// The value inside a [`HostObj::Named`] tag, or the value itself. Every part of
/// the formatter but `%T` and `%#v` reads through the tag: a defined type is its
/// base at run time, so it prints, distributes and coerces as the base does.
pub(crate) fn unname(v: &Value) -> Value {
    let Value::Obj(id) = v else { return v.clone() };
    HEAP.with(|h| match h.borrow().get(*id as usize) {
        Some(HostObj::Named { inner, .. }) => inner.clone(),
        _ => v.clone(),
    })
}

/// [`GELEM_TAG`] — stamp the written element type on every slice inside a `fmt`
/// argument. Stack `[value, type]`, where `type` is the operand's written type.
fn b_elem_tag(vm: &mut VM, argc: u8) -> Value {
    let args = pop_args(vm, argc);
    let v = args.first().cloned().unwrap_or(Value::Undef);
    let ty = args.get(1).map(go_str).unwrap_or_default();
    tag_elem_ty(&v, &ty)
}

/// Rebuild `v` with each slice node carrying the element type `ty` names at that
/// depth. Like the width boxes, the result is a display copy: the program's own
/// slice keeps no tag, so nothing it can observe changes.
fn tag_elem_ty(v: &Value, ty: &str) -> Value {
    let Some(elem) = elem_ty_spelling(ty) else {
        return v.clone();
    };
    // A nil slice or map is not a rebuildable composite: it prints as `[]` /
    // `map[]` but `%#v` names it `[]int(nil)`, which only the nil object knows.
    if nil_composite_kind(v).is_some() {
        return v.clone();
    }
    if let Some(pairs) = map_pairs(v) {
        let key = key_ty_spelling(ty).unwrap_or_default();
        let tagged = pairs
            .into_iter()
            .map(|(k, val)| (tag_elem_ty(&k, &key), tag_elem_ty(&val, &elem)))
            .collect();
        return Value::Obj(heap_alloc(HostObj::Map(GoMap::from_pairs(tagged))));
    }
    let Some(es) = slice_elems(v) else {
        return v.clone();
    };
    let elems: Vec<Value> = es.iter().map(|e| tag_elem_ty(e, &elem)).collect();
    // The rebuild keeps the `[N]T` tag, or `%T` on a tagged array would fall
    // back to naming it a slice.
    let arr_ty = match v {
        Value::Obj(id) => HEAP.with(|h| match h.borrow().get(*id as usize) {
            Some(HostObj::Slice { arr_ty, .. }) => arr_ty.clone(),
            _ => None,
        }),
        _ => None,
    };
    Value::Obj(heap_alloc(HostObj::Slice {
        elems,
        arr_ty,
        elem_ty: Some(elem),
    }))
}

/// The elements of a slice value (materialising a sub-slice view), or `None`
/// when the value is not a slice. Lets a verb distribute over a slice the way
/// `fmt` does for `%d`/`%x`.
pub(crate) fn slice_elems(v: &Value) -> Option<Vec<Value>> {
    let Value::Obj(id) = v else { return None };
    HEAP.with(|h| {
        let h = h.borrow();
        match h.get(*id as usize) {
            Some(HostObj::Slice { elems: a, .. }) => Some(a.clone()),
            Some(HostObj::SliceView {
                backing,
                offset,
                len,
                ..
            }) => match h.get(*backing as usize) {
                Some(HostObj::Slice { elems: a, .. }) => Some(a[*offset..*offset + *len].to_vec()),
                _ => Some(Vec::new()),
            },
            Some(HostObj::Nil {
                kind: NilKind::Slice,
                ..
            }) => Some(Vec::new()),
            _ => None,
        }
    })
}

/// A value's bytes for the text-oriented verbs (`%s`, `%q`): a string's UTF-8,
/// or a slice whose elements are all byte-valued integers — go-rs has no static
/// element type, so a `[]byte` is recognised by its contents. A slice holding
/// anything outside `0..=255` is not text and yields `None`.
pub(crate) fn bytes_of(v: &Value) -> Option<Vec<u8>> {
    match v {
        Value::Str(s) => Some(s.as_str().as_bytes().to_vec()),
        _ => {
            let es = slice_elems(v)?;
            es.iter()
                .map(|e| match e {
                    Value::Int(n) if (0..=255).contains(n) => Some(*n as u8),
                    _ => None,
                })
                .collect()
        }
    }
}

/// Format a heap object the way Go's `%v` does: `[e0 e1 …]` for a slice,
/// `map[k0:v0 …]` (keys sorted, as Go's fmt does) for a map, `{f0 f1 …}` for a
/// struct.
fn obj_str_mode(id: u32, mode: FmtMode) -> String {
    // `%#v` uses Go source syntax: a typed composite literal, `, `-separated,
    // with every element itself in Go syntax. `%v`/`%+v` use the space-separated
    // display forms, differing only in whether struct fields are named.
    let sharp = mode == FmtMode::SharpV;
    let sep = if sharp { ", " } else { " " };
    let elems = |vs: &[Value]| -> String {
        vs.iter()
            .map(|v| go_str_mode(v, mode))
            .collect::<Vec<_>>()
            .join(sep)
    };
    let id = follow(id);
    HEAP.with(|h| {
        let h = h.borrow();
        match h.get(id as usize) {
            // Unreachable: `follow` resolved any pointer above.
            Some(HostObj::Ptr { .. }) => String::new(),
            Some(HostObj::Slice {
                elems: a, elem_ty, ..
            }) => {
                if !sharp {
                    return format!("[{}]", elems(a));
                }
                // `%#v` of a byte slice writes Go source for one: the alias name
                // `[]byte` (where `%T` prints `[]uint8`) and hex element
                // literals.
                if elem_ty.as_deref().is_some_and(is_byte_elem) {
                    let bytes: Vec<String> =
                        a.iter().map(|e| format!("{:#04x}", e.to_int())).collect();
                    return format!("[]byte{{{}}}", bytes.join(", "));
                }
                format!("{}{{{}}}", go_type_name(&Value::Obj(id)), elems(a))
            }
            Some(HostObj::SliceView {
                backing,
                offset,
                len,
                ..
            }) => {
                let view: Vec<Value> = match h.get(*backing as usize) {
                    Some(HostObj::Slice { elems: a, .. }) => a[*offset..*offset + *len].to_vec(),
                    _ => Vec::new(),
                };
                if sharp {
                    format!("{}{{{}}}", go_type_name(&Value::Obj(id)), elems(&view))
                } else {
                    format!("[{}]", elems(&view))
                }
            }
            Some(HostObj::Map(m)) => {
                // `fmt` sorts map keys so map output is deterministic. Sorting
                // the rendered `k:v` strings is only right when the keys order
                // the same as their text, so sort on the key values themselves.
                let mut pairs: Vec<(String, String)> = m
                    .iter()
                    .map(|(k, v)| (go_str_mode(k, mode), go_str_mode(v, mode)))
                    .collect();
                pairs.sort_by(|a, b| map_key_cmp(&a.0, &b.0));
                let body = pairs
                    .into_iter()
                    .map(|(k, v)| format!("{k}:{v}"))
                    .collect::<Vec<_>>()
                    .join(sep);
                if sharp {
                    format!("{}{{{}}}", go_type_name(&Value::Obj(id)), body)
                } else {
                    format!("map[{body}]")
                }
            }
            // A `float32` prints its own width's shortest decimal.
            Some(HostObj::F32(f)) => format_float32(*f),
            // An unsigned 64-bit integer prints its unsigned digits.
            Some(HostObj::U64 { val, .. }) => val.to_string(),
            // Only reachable if a receive site failed to map the sentinel away;
            // it stands for "no value", which is what Go's nil prints as.
            Some(HostObj::ChanClosed) => "<nil>".to_string(),
            // `pop_args` expands a spread marker before any builtin sees it, so
            // reaching here means one leaked past a non-builtin path; render the
            // operands it stands for, space-separated as an argument list.
            Some(HostObj::Spread(xs)) => xs
                .iter()
                .map(|x| go_str_mode(x, mode))
                .collect::<Vec<_>>()
                .join(" "),
            // Go prints a nil slice as `[]` and a nil map as `map[]` — the same
            // as an empty one — and `%#v` as the type followed by `(nil)`.
            Some(HostObj::Nil { kind, ty }) => match (mode, kind) {
                (FmtMode::SharpV, _) => format!("{ty}(nil)"),
                (_, NilKind::Slice) => "[]".to_string(),
                (_, NilKind::Map) => "map[]".to_string(),
            },
            Some(HostObj::Struct {
                type_name, fields, ..
            }) => match mode {
                FmtMode::V => {
                    let parts: Vec<String> =
                        fields.iter().map(|(_, v)| go_str_mode(v, mode)).collect();
                    format!("{{{}}}", parts.join(" "))
                }
                FmtMode::PlusV => {
                    let parts: Vec<String> = fields
                        .iter()
                        .map(|(n, v)| format!("{n}:{}", go_str_mode(v, mode)))
                        .collect();
                    format!("{{{}}}", parts.join(" "))
                }
                FmtMode::SharpV => {
                    let parts: Vec<String> = fields
                        .iter()
                        .map(|(n, v)| format!("{n}:{}", go_str_mode(v, mode)))
                        .collect();
                    format!("main.{type_name}{{{}}}", parts.join(", "))
                }
            },
            // Go prints a function value as a hex pointer; a fixed marker suffices.
            Some(HostObj::Closure { .. }) => "<func>".to_string(),
            // A cell is an internal box; render its contents (a captured value).
            Some(HostObj::Cell(v)) => go_str_mode(v, mode),
            // A defined type prints as its base — except under `%#v`, where a
            // composite is written as a typed literal and the type is the
            // defined name (`main.mySlice{1, 2}`). A scalar's `%#v` carries no
            // type at all, so it needs nothing.
            Some(HostObj::Named { ty, inner }) => {
                let body = go_str_mode(inner, mode);
                let composite = slice_elems(inner).is_some() || map_pairs(inner).is_some();
                match body.find('{').filter(|_| sharp && composite) {
                    Some(i) => format!("{}{}", go_type_spelling(ty), &body[i..]),
                    None => body,
                }
            }
            None => "<nil>".to_string(),
        }
    })
}

/// Order two rendered map keys the way `fmt` orders the underlying values:
/// numerically when both parse as numbers, lexicographically otherwise. Plain
/// string sorting would put `10` before `9`.
fn map_key_cmp(a: &str, b: &str) -> std::cmp::Ordering {
    match (a.parse::<f64>(), b.parse::<f64>()) {
        (Ok(x), Ok(y)) => x.partial_cmp(&y).unwrap_or(std::cmp::Ordering::Equal),
        _ => a.cmp(b),
    }
}

/// Go prints floats via `strconv.FormatFloat(f, 'g', -1, 64)`: shortest exact
/// decimal, whole values without a fractional part (`3`, not `3.0`), and
/// `+Inf`/`-Inf`/`NaN` for the non-finite cases.
pub(crate) fn format_float(f: f64) -> String {
    if f.is_nan() {
        return "NaN".to_string();
    }
    if f.is_infinite() {
        return if f < 0.0 { "-Inf" } else { "+Inf" }.to_string();
    }
    // Go's `ftoa` shortest-`g` path picks the `%e` form when the decimal
    // exponent is `< -4` or `>= 6`, and the `%f` form otherwise (strconv/ftoa.go
    // sets `eprec = 6` for the shortest case). So `999999` prints as `999999`
    // but `1000000` prints as `1e+06`, and `0.0001` stays decimal while
    // `0.00001` becomes `1e-05`.
    let (mant, exp) = shortest_sci(f);
    if !(-4..6).contains(&exp) {
        format_e(&mant, exp, 'e')
    } else {
        // Inside the plain-decimal window Rust's `{}` is the same shortest
        // round-tripping decimal Go computes, and never switches to exponent
        // notation itself.
        format!("{f}")
    }
}

/// Go's `strconv.FormatFloat(f, 'g', -1, 32)` — the same rendering as
/// [`format_float`], but the shortest decimal is computed against **32-bit**
/// precision. That is the whole difference: the `f64` nearest `1/3` prints as
/// `0.3333333333333333` at 64 bits and `0.33333334` at 32.
pub(crate) fn format_float32(f: f32) -> String {
    if f.is_nan() {
        return "NaN".to_string();
    }
    if f.is_infinite() {
        return if f < 0.0 { "-Inf" } else { "+Inf" }.to_string();
    }
    let (mant, exp) = shortest_sci_32(f);
    if !(-4..6).contains(&exp) {
        format_e(&mant, exp, 'e')
    } else {
        plain_form(&mant, exp)
    }
}

/// A finite `f32`'s shortest round-tripping decimal mantissa and exponent.
///
/// Rust's `{:e}` picks the same *length* Go does — both emit the shortest
/// decimal that round-trips — but breaks a tie the other way, so the result is
/// passed through [`go_even_tie`].
fn shortest_sci_32(f: f32) -> (String, i32) {
    let s = format!("{f:e}");
    let (mant, exp) = s.split_once('e').unwrap_or((s.as_str(), "0"));
    (go_even_tie(mant, f), exp.parse().unwrap_or(0))
}

/// Go breaks a shortest-decimal tie towards the **even** last digit; Rust's
/// formatter breaks it away from zero. They can only differ when Rust's last
/// digit is odd, so only then is the exact expansion consulted — and a tie is
/// exactly "the digits past the shortest form are a single 5".
///
/// `float32(4025693.25)` is such a value: Go prints `4.0256932e+06`, Rust
/// `4.0256933e+06`.
fn go_even_tie(mant: &str, f: f32) -> String {
    let Some(last) = mant.chars().last() else {
        return mant.to_string();
    };
    if !matches!(last, '1' | '3' | '5' | '7' | '9') {
        return mant.to_string();
    }
    let digits: String = mant.chars().filter(char::is_ascii_digit).collect();
    let exact = exact_sig_digits(f);
    // A tie: one more exact digit than the shortest form, and it is a 5 over the
    // shortest form's *lower* neighbour (Rust rounded up to reach `digits`).
    let lower = format!("{}{}", &digits[..digits.len() - 1], step_down(last));
    if exact.len() != digits.len() + 1 || !exact.ends_with('5') || exact[..digits.len()] != lower {
        return mant.to_string();
    }
    let mut out: Vec<char> = mant.chars().collect();
    if let Some(c) = out.last_mut() {
        *c = step_down(last);
    }
    out.into_iter().collect()
}

/// The digit one below `d`. Only called on an odd digit, so it never borrows.
fn step_down(d: char) -> char {
    char::from(d as u8 - 1)
}

/// Every significant digit of a finite `f32`'s **exact** value, leading and
/// trailing zeros trimmed. A binary float's decimal expansion terminates, and
/// the longest an `f32`'s can be is the 149 fractional digits of the smallest
/// subnormal — so 160 places renders every one of them exactly.
fn exact_sig_digits(f: f32) -> String {
    let s = format!("{:.*}", 160, f64::from(f).abs());
    let digits: String = s.chars().filter(char::is_ascii_digit).collect();
    digits
        .trim_start_matches('0')
        .trim_end_matches('0')
        .to_string()
}

/// Assemble the plain (non-exponent) rendering of a mantissa and exponent —
/// `("1.5", 3)` is `1500`, `("1.5", -2)` is `0.015`.
fn plain_form(mant: &str, exp: i32) -> String {
    let (sign, m) = match mant.strip_prefix('-') {
        Some(rest) => ("-", rest),
        None => ("", mant),
    };
    let digits: String = m.chars().filter(char::is_ascii_digit).collect();
    let point = exp + 1; // digits before the decimal point
    if point <= 0 {
        format!(
            "{sign}0.{}{digits}",
            "0".repeat(point.unsigned_abs() as usize)
        )
    } else if point as usize >= digits.len() {
        format!(
            "{sign}{digits}{}",
            "0".repeat(point as usize - digits.len())
        )
    } else {
        format!(
            "{sign}{}.{}",
            &digits[..point as usize],
            &digits[point as usize..]
        )
    }
}

/// Split a finite `f64` into its shortest round-tripping decimal mantissa
/// (`"-1.2345"`, sign included) and decimal exponent, via Rust's `{:e}` — which
/// already emits the shortest such representation.
fn shortest_sci(f: f64) -> (String, i32) {
    let s = format!("{f:e}");
    match s.split_once('e') {
        Some((m, e)) => (m.to_string(), e.parse().unwrap_or(0)),
        None => (s, 0),
    }
}

/// Assemble Go's `%e` rendering from a mantissa and exponent: the exponent
/// always carries a sign and at least two digits (`1e+06`, `1.5e-07`,
/// `1e+100`).
fn format_e(mant: &str, exp: i32, e: char) -> String {
    let sign = if exp < 0 { '-' } else { '+' };
    let mag = exp.unsigned_abs();
    if mag < 10 {
        format!("{mant}{e}{sign}0{mag}")
    } else {
        format!("{mant}{e}{sign}{mag}")
    }
}

/// Go's `%e`/`%E` verb: `prec` digits after the mantissa's decimal point
/// (default 6), or the shortest round-tripping mantissa when `prec` is `None`
/// and the caller asked for `%v`-style shortest output.
fn format_float_e(f: f64, prec: Option<usize>, upper: bool) -> String {
    if f.is_nan() {
        return "NaN".to_string();
    }
    if f.is_infinite() {
        return if f < 0.0 { "-Inf" } else { "+Inf" }.to_string();
    }
    let e = if upper { 'E' } else { 'e' };
    match prec {
        None => {
            let (mant, exp) = shortest_sci(f);
            format_e(&mant, exp, e)
        }
        Some(p) => {
            // Rust's `{:.*e}` rounds the mantissa to `p` fractional digits with
            // the same semantics, so only the exponent spelling differs.
            let s = format!("{:.*e}", p, f);
            match s.split_once('e') {
                Some((m, ex)) => format_e(m, ex.parse().unwrap_or(0), e),
                None => s,
            }
        }
    }
}

/// Go's `%g`/`%G` verb. With no explicit precision this is the same shortest
/// representation `%v` uses; with a precision it means "`prec` significant
/// digits", and the `%e`-vs-`%f` choice compares the decimal exponent against
/// that precision instead of the shortest-case 6.
fn format_float_g(f: f64, prec: Option<usize>, upper: bool) -> String {
    if f.is_nan() {
        return "NaN".to_string();
    }
    if f.is_infinite() {
        return if f < 0.0 { "-Inf" } else { "+Inf" }.to_string();
    }
    let Some(p) = prec else {
        let s = format_float(f);
        return if upper { s.replace('e', "E") } else { s };
    };
    // Go treats `%.0g` as `%.1g` (at least one significant digit).
    let p = p.max(1);
    // Round to `p` significant digits first, then decide the form from the
    // rounded exponent — rounding can carry into a new decade (9.99 → 1e+01).
    let (mant, exp) = match format!("{:.*e}", p - 1, f).split_once('e') {
        Some((m, e)) => (m.to_string(), e.parse::<i32>().unwrap_or(0)),
        None => (format!("{f}"), 0),
    };
    let e = if upper { 'E' } else { 'e' };
    if exp < -4 || exp >= p as i32 {
        // `%g` strips a trailing zero run from the mantissa that `%e` keeps.
        format_e(trim_zeros(&mant), exp, e)
    } else {
        let decimals = (p as i32 - 1 - exp).max(0) as usize;
        trim_zeros(&format!("{:.*}", decimals, f)).to_string()
    }
}

/// Drop a trailing fractional zero run (and a bare trailing `.`) from a decimal
/// string, the way Go's `%g` does. Leaves integral strings untouched.
fn trim_zeros(s: &str) -> &str {
    if !s.contains('.') {
        return s;
    }
    s.trim_end_matches('0').trim_end_matches('.')
}

fn b_println(vm: &mut VM, argc: u8) -> Value {
    let args = pop_args(vm, argc);
    let text: Vec<String> = args.iter().map(go_str).collect();
    println!("{}", text.join(" "));
    Value::Undef
}

fn b_print(vm: &mut VM, argc: u8) -> Value {
    let args = pop_args(vm, argc);
    print!("{}", go_print_spacing(&args));
    Value::Undef
}

fn b_printf(vm: &mut VM, argc: u8) -> Value {
    let args = pop_args(vm, argc);
    print!("{}", sprintf(&args));
    Value::Undef
}

fn b_eprintln(vm: &mut VM, argc: u8) -> Value {
    let args = pop_args(vm, argc);
    let text: Vec<String> = args.iter().map(go_str).collect();
    eprintln!("{}", text.join(" "));
    Value::Undef
}

fn b_eprint(vm: &mut VM, argc: u8) -> Value {
    let args = pop_args(vm, argc);
    eprint!("{}", go_print_spacing(&args));
    Value::Undef
}

/// Go's `Print`/builtin-`print` spacing: a space is inserted between two
/// operands only when neither is a string.
fn go_print_spacing(args: &[Value]) -> String {
    let mut out = String::new();
    for (i, v) in args.iter().enumerate() {
        if i > 0 {
            let prev_str = matches!(args[i - 1], Value::Str(_));
            let cur_str = matches!(v, Value::Str(_));
            if !prev_str && !cur_str {
                out.push(' ');
            }
        }
        out.push_str(&go_str(v));
    }
    out
}

/// The flags, width and precision of one `%` verb, as written before it.
#[derive(Clone, Copy, Default)]
struct Spec {
    /// `-`: pad on the right instead of the left.
    left: bool,
    /// `0`: pad a numeric verb with leading zeros, after any sign.
    zero: bool,
    /// `+`: always print a sign.
    plus: bool,
    /// `#`: the alternate form (`0x` on hex, back-quotes on `%q`, Go syntax on `%v`).
    sharp: bool,
    /// ` `: leave a space where a non-negative number's sign would go (`% d` of
    /// `42` is ` 42`), and separate the bytes of a string or byte slice printed
    /// in hex (`% x` of `"abc"` is `61 62 63`). `+` outranks it.
    space: bool,
    width: Option<usize>,
    prec: Option<usize>,
}

/// The sign a numeric verb writes before its magnitude: `-` when the value is
/// negative, otherwise whichever of `+` and ` ` the flags asked for.
fn sign_of(neg: bool, spec: &Spec) -> &'static str {
    if neg {
        "-"
    } else if spec.plus {
        "+"
    } else if spec.space {
        " "
    } else {
        ""
    }
}

/// `fmt.Printf`: the first argument is the format string and each verb consumes
/// the next operand.
///
/// `%v` and `%T` describe an operand as a whole. Every other verb applies
/// *element-wise* to a composite operand — [`render_verb`] does that — so the
/// flags, width and precision parsed here belong to each element rather than to
/// the whole rendering: `%8q` of a `[]string{"a", "b"}` is `[     "a"      "b"]`.
fn sprintf(args: &[Value]) -> String {
    let fmt = args.first().map(go_str).unwrap_or_default();
    let operands: &[Value] = args.get(1..).unwrap_or(&[]);
    // An index rather than an iterator: the operands left over after the format
    // is exhausted are Go's `%!(EXTRA …)`, which needs to see the tail.
    let mut next = 0usize;
    let mut out = String::new();
    let chars: Vec<char> = fmt.chars().collect();
    let mut i = 0;
    // Whether any verb reached for an operand by explicit `[n]` index. Go stops
    // reporting `%!(EXTRA …)` once one did — with the cursor moved about, the
    // operands past it are no evidence that they went unused.
    let mut reordered = false;
    while i < chars.len() {
        if chars[i] != '%' {
            out.push(chars[i]);
            i += 1;
            continue;
        }
        i += 1;
        // Cleared by a malformed or out-of-range `[n]`, which makes the verb
        // itself Go's `%!verb(BADINDEX)`.
        let mut good_arg_num = true;
        // flags
        let mut spec = Spec::default();
        while i < chars.len() {
            match chars[i] {
                '-' => spec.left = true,
                '0' => spec.zero = true,
                '+' => spec.plus = true,
                '#' => spec.sharp = true,
                ' ' => spec.space = true,
                _ => break,
            }
            i += 1;
        }
        // An explicit `[n]` operand index, which Go looks for in three places:
        // before the width, before the precision, and before the verb.
        let mut after_index = arg_number(
            &chars,
            &mut i,
            &mut next,
            operands.len(),
            &mut reordered,
            &mut good_arg_num,
        );
        // Width: literal digits, or `*` taking it from an operand. A `*` that
        // finds no `int` there is Go's `%!(BADWIDTH)`, after which the verb runs
        // on with no width at all.
        if i < chars.len() && chars[i] == '*' {
            i += 1;
            after_index = false;
            match star_arg(operands, &mut next) {
                // A negative `*` width means the same as the `-` flag applied to
                // its magnitude.
                Some(n) if n < 0 => {
                    spec.left = true;
                    spec.width = Some(n.unsigned_abs() as usize);
                }
                Some(n) => spec.width = Some(n as usize),
                None => out.push_str("%!(BADWIDTH)"),
            }
        } else {
            let mut width = 0usize;
            let mut has_width = false;
            while i < chars.len() && chars[i].is_ascii_digit() {
                has_width = true;
                width = width * 10 + (chars[i] as usize - '0' as usize);
                i += 1;
            }
            spec.width = has_width.then_some(width);
            // `%[3]2d` — an index and then a width is Go's `BADINDEX`: the
            // bracket has to sit immediately before what it selects.
            if after_index && spec.width.is_some() {
                good_arg_num = false;
            }
        }
        // Precision, with the same `*` form and the same `%!(BADPREC)` recovery.
        if i < chars.len() && chars[i] == '.' {
            i += 1;
            // `%[3].2d` — likewise.
            if after_index {
                good_arg_num = false;
            }
            after_index = arg_number(
                &chars,
                &mut i,
                &mut next,
                operands.len(),
                &mut reordered,
                &mut good_arg_num,
            );
            if i < chars.len() && chars[i] == '*' {
                i += 1;
                after_index = false;
                match star_arg(operands, &mut next) {
                    Some(n) if n >= 0 => spec.prec = Some(n as usize),
                    _ => out.push_str("%!(BADPREC)"),
                }
            } else {
                let mut p = 0usize;
                while i < chars.len() && chars[i].is_ascii_digit() {
                    p = p * 10 + (chars[i] as usize - '0' as usize);
                    i += 1;
                }
                spec.prec = Some(p);
            }
        }
        if !after_index {
            arg_number(
                &chars,
                &mut i,
                &mut next,
                operands.len(),
                &mut reordered,
                &mut good_arg_num,
            );
        }
        // A `%` that never reached a verb character — the format ended inside
        // the spec, or on the `%` itself.
        if i >= chars.len() {
            out.push_str("%!(NOVERB)");
            break;
        }
        let verb = chars[i];
        i += 1;

        // `%%` is a literal and is the one form that consumes no operand.
        if verb == '%' {
            out.push('%');
            continue;
        }
        if !good_arg_num {
            out.push_str(&format!("%!{verb}(BADINDEX)"));
            continue;
        }
        let Some(v) = operands.get(next) else {
            out.push_str("%!");
            out.push(verb);
            out.push_str("(MISSING)");
            continue;
        };
        next += 1;

        match verb {
            // `%T` is the operand's type, not its value.
            'T' => out.push_str(&pad_text(&go_type_name(v), &spec)),
            // `%w` renders exactly as `%v`. It is only *meaningful* to
            // `fmt.Errorf`, which is where the wrapping happens — the compiler
            // lowers `Errorf` to this `Sprintf` and reads `%w` off the format
            // separately to pick the error type — so here it is the operand's
            // text. (Go's `Printf` rejects it as `%!w(…)`; go-rs has one format
            // path, and keeping `Errorf` correct is worth more than rejecting a
            // verb nobody writes outside it. See BUGS.md.)
            'v' | 'w' => out.push_str(&render_v(v, &spec)),
            _ if is_verb(verb) => out.push_str(&render_verb(v, verb, &spec, 0)),
            // Not a verb at all. Go names the offender and shows the operand
            // rendered as `%v` under the same flags: `%5.2z` of `3` is
            // `%!z(int=   03)`. A nil operand has no type to name and is the
            // bare `%!z(<nil>)`, which is [`bad_verb`]'s depth-0 form.
            _ if matches!(v, Value::Undef) => out.push_str(&format!("%!{verb}(<nil>)")),
            _ => out.push_str(&format!(
                "%!{verb}({}={})",
                go_type_name(v),
                render_v(v, &spec)
            )),
        }
    }
    // Operands the format never reached. A format that reordered its operands
    // does not report them: Go stops tracking which were used at the first `[n]`.
    if !reordered && next < operands.len() {
        let extra: Vec<String> = operands[next..]
            .iter()
            .map(|v| format!("{}={}", go_type_name(v), go_str(v)))
            .collect();
        out.push_str(&format!("%!(EXTRA {})", extra.join(", ")));
    }
    out
}

/// Go's `pp.argNumber`: read an explicit `[n]` operand index at `i`.
///
/// `n` is one-based and selects which operand the next verb formats, so
/// `%[2]d` prints the second. On success the cursor moves past the bracket and
/// `next` is repointed; the return value is Go's `afterIndex`, which the caller
/// uses to reject a width or precision written *after* the bracket.
///
/// A bracket that does not parse, or names an operand that is not there, leaves
/// `next` alone and clears `good` — Go's `%!verb(BADINDEX)` — but still steps
/// the cursor past what it consumed so the rest of the format keeps its shape.
fn arg_number(
    chars: &[char],
    i: &mut usize,
    next: &mut usize,
    num_args: usize,
    reordered: &mut bool,
    good: &mut bool,
) -> bool {
    if chars.get(*i) != Some(&'[') {
        return false;
    }
    *reordered = true;
    let (index, width, ok) = parse_arg_number(&chars[*i..]);
    *i += width;
    if ok && index < num_args {
        *next = index;
        return true;
    }
    *good = false;
    ok
}

/// Go's `parseArgNumber`: the zero-based operand index a leading `[n]` names,
/// how many characters it spans, and whether it parsed at all.
///
/// A bracket with anything but decimal digits inside — or no closing `]` —
/// yields `ok = false`, and the span is what the caller should skip: the whole
/// `[…]` when a bracket was closed, otherwise just the `[`.
fn parse_arg_number(chars: &[char]) -> (usize, usize, bool) {
    if chars.len() < 3 {
        return (0, 1, false);
    }
    let Some(close) = chars.iter().position(|&c| c == ']') else {
        return (0, 1, false);
    };
    let digits = &chars[1..close];
    if digits.is_empty() || !digits.iter().all(|c| c.is_ascii_digit()) {
        return (0, close + 1, false);
    }
    let mut n = 0usize;
    for c in digits {
        n = n * 10 + (*c as usize - '0' as usize);
    }
    // Arg numbers are one-indexed; `%[0]d` has no operand and is a bad index.
    match n.checked_sub(1) {
        Some(index) => (index, close + 1, true),
        None => (0, close + 1, false),
    }
}

/// Take the `int` operand a `*` width or precision reads, advancing the cursor
/// past it. `None` — an operand that is not an `int`, or none left — is Go's
/// `%!(BADWIDTH)` / `%!(BADPREC)`; the operand is still consumed, because Go has
/// already taken it off the list by the time it finds the wrong type.
fn star_arg(operands: &[Value], next: &mut usize) -> Option<i64> {
    let v = operands.get(*next)?;
    *next += 1;
    match unname(v) {
        Value::Int(n) => Some(n),
        _ => None,
    }
}

/// Render one operand under `%v`, with `+`/`#` selecting the `%+v` / `%#v`
/// forms.
///
/// An integer is the exception: `%v` of one is exactly `%d`, so a precision is
/// the digit count [`int_precision`] applies rather than the string truncation
/// below (`%.2v` of `-3` is `-03`), and the `0` flag zero-fills it. `%+v` and
/// `%#v` are left on the `%v` path because neither flag reaches a number —
/// `%+v` of `42` is `42`, not `+42`.
fn render_v(v: &Value, spec: &Spec) -> String {
    let numeric = spec.prec.is_some() || spec.zero || spec.space;
    if numeric && !spec.plus && !spec.sharp && is_int_operand(v) {
        return pad_number(&scalar_verb(v, 'd', spec), 'd', spec);
    }
    let mode = if spec.sharp {
        FmtMode::SharpV
    } else if spec.plus {
        FmtMode::PlusV
    } else {
        FmtMode::V
    };
    // Go's `printValue` walks a composite and applies the width and the `0` flag
    // at each *leaf*, so `%10v` of a `[]int{1, 2}` is `[         1          2]`
    // rather than the list padded as a whole. Only plain `%v` walks: `%#v` and
    // `%+v` name the composite as a whole, and with no width and no `0` flag the
    // walk is indistinguishable from the one-shot rendering below.
    if matches!(mode, FmtMode::V) && (spec.width.is_some() || spec.zero) {
        if let Some(walked) = distribute_v(v, spec) {
            return walked;
        }
    }
    let mut s = go_str_mode(v, mode);
    if let Some(p) = spec.prec {
        if s.chars().count() > p {
            s = s.chars().take(p).collect();
        }
    }
    if is_num_operand(v) {
        pad_number(&s, 'v', spec)
    } else {
        pad_text(&s, spec)
    }
}

/// `%v` of a composite with the spec applied to each element, or `None` when the
/// operand is not one (a scalar, where the caller's one-shot rendering is the
/// whole answer).
///
/// The shapes match [`render_verb`]'s, which does the same walk for every other
/// verb; a nil slice or map keeps its empty form unpadded, as Go's `printValue`
/// writes the brackets around no elements at all.
fn distribute_v(v: &Value, spec: &Spec) -> Option<String> {
    if let Some(es) = slice_elems(v) {
        let body: Vec<String> = es.iter().map(|e| render_v(e, spec)).collect();
        return Some(format!("[{}]", body.join(" ")));
    }
    if let Some(pairs) = map_pairs(v) {
        // Sorted on the plain `%v` spelling of the key, as `fmt` orders map
        // output by key value rather than by the padded text.
        let mut rows: Vec<(String, String)> = pairs
            .iter()
            .map(|(k, val)| {
                (
                    go_str(k),
                    format!("{}:{}", render_v(k, spec), render_v(val, spec)),
                )
            })
            .collect();
        rows.sort_by(|a, b| map_key_cmp(&a.0, &b.0));
        let body: Vec<String> = rows.into_iter().map(|(_, r)| r).collect();
        return Some(format!("map[{}]", body.join(" ")));
    }
    if let Some(fields) = struct_fields_of(v) {
        let body: Vec<String> = fields.iter().map(|(_, f)| render_v(f, spec)).collect();
        return Some(format!("{{{}}}", body.join(" ")));
    }
    match nil_composite_kind(v)? {
        NilKind::Slice => Some("[]".to_string()),
        NilKind::Map => Some("map[]".to_string()),
    }
}

/// Whether `%v` should render this operand the way `%d` would — a plain integer,
/// an unsigned-64 box, or a defined type over either.
fn is_int_operand(v: &Value) -> bool {
    arg_uint(v).is_some() || matches!(unname(v), Value::Int(_))
}

/// Whether `c` is a verb [`render_verb`] renders element-wise. `v` and `T` are
/// deliberately absent: they describe a composite as a whole.
fn is_verb(c: char) -> bool {
    matches!(
        c,
        't' | 'q'
            | 'f'
            | 'F'
            | 'e'
            | 'E'
            | 'g'
            | 'G'
            | 'd'
            | 'x'
            | 'X'
            | 'o'
            | 'b'
            | 'c'
            | 'U'
            | 's'
    )
}

/// Render one operand under `verb`, distributing over a composite.
///
/// Go applies a verb to each element of a slice, array, map or struct and wraps
/// the results the way `%v` would — `[e0 e1]`, `map[k0:v0]`, `{f0 f1}` — with
/// the verb's flags, width and precision applied to each element. The exception
/// is a byte slice under `%s`/`%q`/`%x`/`%X`, which is the text it holds and not
/// a list; that holds at every depth, so a `[][]byte` prints its rows as
/// strings.
///
/// `depth` is 0 only for the operand itself: a nil *inside* a composite prints
/// `<nil>` where a nil operand is the bad-verb form `%!q(<nil>)`.
fn render_verb(v: &Value, verb: char, spec: &Spec, depth: usize) -> String {
    // A defined type is its base to every verb here — only `%T` and `%#v`, which
    // do not come through this path, print the name.
    let unnamed = unname(v);
    let v = &unnamed;
    if matches!(verb, 's' | 'q' | 'x' | 'X') {
        if let Some(b) = slice_bytes(v) {
            let text = Value::str(String::from_utf8_lossy(&b).into_owned());
            return pad_text(&scalar_verb(&text, verb, spec), spec);
        }
    }
    if let Some(es) = slice_elems(v) {
        let body: Vec<String> = es
            .iter()
            .map(|e| render_verb(e, verb, spec, depth + 1))
            .collect();
        return format!("[{}]", body.join(" "));
    }
    if let Some(pairs) = map_pairs(v) {
        // `fmt` orders map output by the *key values*, then renders each key and
        // value with the verb — so the sort reads the plain `%v` spelling, which
        // a bad-verb wrapper around the rendered key would otherwise scramble.
        let mut rows: Vec<(String, String)> = pairs
            .iter()
            .map(|(k, val)| {
                (
                    go_str(k),
                    format!(
                        "{}:{}",
                        render_verb(k, verb, spec, depth + 1),
                        render_verb(val, verb, spec, depth + 1)
                    ),
                )
            })
            .collect();
        rows.sort_by(|a, b| map_key_cmp(&a.0, &b.0));
        let body: Vec<String> = rows.into_iter().map(|(_, r)| r).collect();
        return format!("map[{}]", body.join(" "));
    }
    if let Some(fields) = struct_fields_of(v) {
        let body: Vec<String> = fields
            .iter()
            .map(|(_, f)| render_verb(f, verb, spec, depth + 1))
            .collect();
        return format!("{{{}}}", body.join(" "));
    }
    // An empty composite still has the shape of one: a nil slice under a
    // non-string verb is `[]`, a nil map `map[]`.
    if let Some(kind) = nil_composite_kind(v) {
        return match kind {
            NilKind::Slice => "[]".to_string(),
            NilKind::Map => "map[]".to_string(),
        };
    }
    if let Some(bad) = bad_verb(v, verb, spec, depth) {
        return bad;
    }
    let body = scalar_verb(v, verb, spec);
    match verb {
        'd' | 'o' | 'b' | 'x' | 'X' | 'f' | 'F' | 'e' | 'E' | 'g' | 'G' => {
            pad_number(&body, verb, spec)
        }
        // `fmt.fmtUnicode` clears the `0` flag itself before padding, so `%U` is
        // the one verb that stays space-filled under it: `%010U` of `'A'` is
        // `    U+0041`.
        'U' => pad_text(
            &body,
            &Spec {
                zero: false,
                ..*spec
            },
        ),
        _ => pad_text(&body, spec),
    }
}

/// The bytes of a value `fmt` reads as text under `%s`/`%q`/`%x`: a `[]byte`.
///
/// The written element type decides it when the compiler knew one ([`GELEM_TAG`]
/// stamps it); otherwise the elements are guessed from, which is right for the
/// `[]byte` an untyped path produced and wrong for a `[]int` that happens to
/// hold small numbers. An empty untagged slice is *not* guessed to be bytes:
/// the guess is vacuously true there, and `[]` is the commoner answer.
fn slice_bytes(v: &Value) -> Option<Vec<u8>> {
    if let Value::Obj(id) = v {
        // A nil `[]byte` is the empty string, a nil `[]int` the empty list.
        let nil_ty = HEAP.with(|h| match h.borrow().get(*id as usize) {
            Some(HostObj::Nil { kind, ty }) => Some((*kind, ty.clone())),
            _ => None,
        });
        if let Some((kind, ty)) = nil_ty {
            let bytes =
                kind == NilKind::Slice && elem_ty_spelling(&ty).is_some_and(|e| is_byte_elem(&e));
            return bytes.then(Vec::new);
        }
    }
    let tagged = slice_elem_tag(v);
    if matches!(&tagged, Some(t) if !is_byte_elem(t)) {
        return None;
    }
    let es = slice_elems(v)?;
    if tagged.is_none() && es.is_empty() {
        return None;
    }
    es.iter()
        .map(|e| match e {
            Value::Int(n) if (0..=255).contains(n) => Some(*n as u8),
            _ => None,
        })
        .collect()
}

/// Whether a written element type spells one of Go's byte types.
fn is_byte_elem(t: &str) -> bool {
    matches!(t, "byte" | "uint8")
}

/// The element type [`GELEM_TAG`] stamped on a slice, if any.
fn slice_elem_tag(v: &Value) -> Option<String> {
    let Value::Obj(id) = v else { return None };
    HEAP.with(|h| match h.borrow().get(*id as usize) {
        Some(HostObj::Slice { elem_ty, .. }) => elem_ty.clone(),
        _ => None,
    })
}

/// Which kind of nil composite a value is, or `None` when it is not one.
fn nil_composite_kind(v: &Value) -> Option<NilKind> {
    let Value::Obj(id) = v else { return None };
    HEAP.with(|h| match h.borrow().get(*id as usize) {
        Some(HostObj::Nil { kind, .. }) => Some(*kind),
        _ => None,
    })
}

/// A struct's `(field, value)` pairs, or `None` when the value is not a struct.
fn struct_fields_of(v: &Value) -> Option<Vec<(String, Value)>> {
    let Value::Obj(id) = v else { return None };
    HEAP.with(|h| match h.borrow().get(follow(*id) as usize) {
        Some(HostObj::Struct { fields, .. }) => Some(fields.clone()),
        _ => None,
    })
}

/// Render one non-composite operand under `verb`, unpadded.
fn scalar_verb(v: &Value, verb: char, spec: &Spec) -> String {
    match verb {
        't' => go_str(v),
        // `%q` quotes a string with `strconv.Quote` and an integer as a rune
        // literal. `#` asks for a back-quoted string where one is possible.
        'q' => match v {
            Value::Int(n) => go_quote_rune(*n),
            _ => {
                let mut s = go_str(v);
                // Precision truncates the string before it is quoted, so `%.2q`
                // of `"alpha"` is `"al"` — the quotes are not part of the count.
                if let Some(p) = spec.prec {
                    if s.chars().count() > p {
                        s = s.chars().take(p).collect();
                    }
                }
                if spec.sharp && !s.contains('`') && !s.contains('\n') {
                    format!("`{s}`")
                } else {
                    go_quote(&s)
                }
            }
        },
        'f' | 'F' => {
            let x = arg_float(v);
            let s = if x.is_nan() {
                "NaN".to_string()
            } else if x.is_infinite() {
                if x < 0.0 { "-Inf" } else { "+Inf" }.to_string()
            } else {
                format!("{:.*}", spec.prec.unwrap_or(6), x)
            };
            signed(s, spec)
        }
        'e' | 'E' => signed(
            format_float_e(arg_float(v), Some(spec.prec.unwrap_or(6)), verb == 'E'),
            spec,
        ),
        // `%g` with no precision is the shortest round-tripping decimal —
        // computed at the operand's own width, so a `float32` box narrows it.
        'g' | 'G' => {
            let s = match (spec.prec, unbox_f32(v)) {
                (None, Some(f)) => {
                    let g = format_float32(f);
                    if verb == 'G' {
                        g.to_uppercase()
                    } else {
                        g
                    }
                }
                _ => format_float_g(arg_float(v), spec.prec, verb == 'G'),
            };
            signed(s, spec)
        }
        // A precision is a minimum digit count here, so the sign is split off
        // the magnitude and [`int_precision`] pads between them: `%+.3d` of `7`
        // is `+007`, never `007` with the sign lost or `+7` unpadded.
        'd' => {
            let (sign, digits) = match arg_uint(v) {
                Some(u) => (sign_of(false, spec), u.to_string()),
                None => {
                    let n = arg_int(v);
                    (sign_of(n < 0, spec), n.unsigned_abs().to_string())
                }
            };
            int_precision(sign, "", &digits, spec.prec)
        }
        // `%x`/`%X` hex-encode a string bytewise.
        'x' | 'X' if matches!(v, Value::Str(_)) => {
            let upper = verb == 'X';
            let bytes: Vec<String> = go_str(v)
                .bytes()
                .map(|b| {
                    if upper {
                        format!("{b:02X}")
                    } else {
                        format!("{b:02x}")
                    }
                })
                .collect();
            // The space flag separates the bytes here rather than standing in
            // for a sign: `% x` of `"abc"` is `61 62 63`.
            let body = bytes.join(if spec.space { " " } else { "" });
            if spec.sharp {
                format!("{}{body}", if upper { "0X" } else { "0x" })
            } else {
                body
            }
        }
        // The base-N verbs. Go prints a *signed* operand as a sign and the
        // magnitude — `%x` of `-9` is `-9`, not the two's-complement bit pattern
        // — and only an unsigned one reads all 64 bits. `#` writes the base
        // prefix after the sign: `-0x9`, `-0b1001`, and a leading `0` for octal
        // where the digits do not already start with one.
        'x' | 'X' | 'o' | 'b' => {
            let (neg, mag) = match arg_uint(v) {
                Some(u) => (false, u),
                None => {
                    let n = arg_int(v);
                    (n < 0, n.unsigned_abs())
                }
            };
            let mut digits = match verb {
                'x' => format!("{mag:x}"),
                'X' => format!("{mag:X}"),
                'o' => format!("{mag:o}"),
                _ => format!("{mag:b}"),
            };
            let prefix = match (spec.sharp, verb) {
                (false, _) => "",
                (true, 'x') => "0x",
                (true, 'X') => "0X",
                (true, 'b') => "0b",
                // Octal's alternate form is a leading `0`, which is a digit —
                // so unlike `0x`/`0b` it counts toward a precision: `%#.4o` of
                // `8` is `0010`, four characters in all, where `%#.4x` of `255`
                // is `0x00ff` with four *after* the prefix.
                (true, _) => {
                    if !digits.starts_with('0') {
                        digits.insert(0, '0');
                    }
                    ""
                }
            };
            int_precision(sign_of(neg, spec), prefix, &digits, spec.prec)
        }
        // A code point outside Unicode — a negative or too-large integer — is
        // the replacement character, as Go's rune conversion makes it.
        'c' => u32::try_from(arg_int(v))
            .ok()
            .and_then(char::from_u32)
            .unwrap_or('\u{fffd}')
            .to_string(),
        // `%U` is Go's Unicode format: `U+4E16`, at least four hex digits. It
        // reads the operand's bits unsigned, so a negative integer shows all 64
        // (`U+FFFFFFFFFFFFFFF7`) rather than clamping to zero.
        'U' => format!("U+{:04X}", v.to_int() as u64),
        // `%s` on a byte slice is handled by the caller; here it is the value's
        // own text, truncated by any precision.
        's' => {
            let mut s = match v {
                Value::Str(s) => s.as_str().to_string(),
                v => match bytes_of(v) {
                    Some(b) => String::from_utf8_lossy(&b).into_owned(),
                    None => go_str(v),
                },
            };
            if let Some(p) = spec.prec {
                if s.chars().count() > p {
                    s = s.chars().take(p).collect();
                }
            }
            s
        }
        _ => go_str(v),
    }
}

/// Go's bad-verb rendering — `%!q(bool=true)` — for the operand kinds a verb
/// does not accept, or `None` when the verb accepts this one.
///
/// Only the kinds go-rs's value model names the same way Go's static types do
/// are reported: a `Value::Str` is always a Go `string` and a `Value::Bool`
/// always a `bool`, but a `Value::Int` can be an untyped constant that Go widened
/// to a `float64` on the way into a `[]float64`, so `%f` of an integer stays a
/// coercion rather than becoming a (wrong) `%!f(int=…)`.
///
/// `%b` and `%x`/`%X` are absent from the float row on purpose: Go accepts a
/// float under both (`%x` of `1.5` is `0x1.8p+00`), so a float there is a
/// *valid* operand go-rs renders differently, not a bad verb.
///
/// The operand inside the parentheses carries the verb's own width and
/// precision — `%05d` of `"k"` is `%!d(string=0000k)` — so the bad form is
/// already padded and the caller must not pad it again.
fn bad_verb(v: &Value, verb: char, spec: &Spec, depth: usize) -> Option<String> {
    // A nil operand is the bad-verb form on its own; nested in a composite it is
    // just `<nil>`, which is what Go's `printValue` writes for an invalid entry.
    if matches!(v, Value::Undef) {
        return Some(if depth == 0 {
            format!("%!{verb}(<nil>)")
        } else {
            "<nil>".to_string()
        });
    }
    let bad = match verb {
        'd' | 'o' | 'c' | 'U' => matches!(v, Value::Str(_) | Value::Bool(_) | Value::Float(_)),
        'b' => matches!(v, Value::Str(_) | Value::Bool(_)),
        'x' | 'X' => matches!(v, Value::Bool(_)),
        'f' | 'F' | 'e' | 'E' | 'g' | 'G' => matches!(v, Value::Str(_) | Value::Bool(_)),
        'q' => matches!(v, Value::Bool(_) | Value::Float(_)),
        's' => matches!(v, Value::Bool(_) | Value::Int(_) | Value::Float(_)),
        't' => !matches!(v, Value::Bool(_)),
        _ => false,
    };
    if !bad {
        return None;
    }
    let mut text = go_str(v);
    if let Some(p) = spec.prec {
        if text.chars().count() > p {
            text = text.chars().take(p).collect();
        }
    }
    // Go's `badVerb` re-prints the operand with `%v` under the same flags, so
    // the fill follows the operand's own kind, not the verb that rejected it.
    let inner = if is_num_operand(v) {
        pad_number(&text, 'v', spec)
    } else {
        pad_text(&text, spec)
    };
    Some(format!("%!{verb}({}={})", go_type_name(v), inner))
}

/// Apply Go's precision rule for an *integer* verb: a minimum digit count, not
/// the truncation a precision means for a string.
///
/// `%.3d` of `-7` is `-007` and `%.4x` of `255` is `00ff` — the zeros go inside
/// the sign and inside a `#` base prefix (`%#.4x` is `0x00ff`), because both
/// belong to the number rather than to its digits. A value with more digits than
/// the precision is never shortened: `%.3d` of `12345` is `12345`.
///
/// Precision `0` is the one case that removes digits. Go writes a zero value as
/// the empty string there (`%.0d` of `0` is ``) and any other value in full
/// (`%.0d` of `5` is `5`), which is what C's `printf` does too.
fn int_precision(sign: &str, prefix: &str, digits: &str, prec: Option<usize>) -> String {
    let Some(p) = prec else {
        return format!("{sign}{prefix}{digits}");
    };
    if p == 0 && digits == "0" {
        return format!("{sign}{prefix}");
    }
    let zeros = p.saturating_sub(digits.chars().count());
    format!("{sign}{prefix}{}{digits}", "0".repeat(zeros))
}

/// Apply `+` — or the space that stands in for it — to an unsigned float
/// rendering.
fn signed(s: String, spec: &Spec) -> String {
    if s.starts_with('-') {
        return s;
    }
    format!("{}{s}", sign_of(false, spec))
}

/// How many fill characters `body` needs to reach the spec's width, or `None`
/// when it already fills it (Go's `f.wid - utf8.RuneCount(b)`, clamped).
fn pad_room(body: &str, spec: &Spec) -> Option<usize> {
    let width = spec.width?;
    let len = body.chars().count();
    (len < width).then(|| width - len)
}

/// Go's `fmt.padString` — the fill for a rendering that is *text*.
///
/// `writePadding` fills with `0` whenever the flag is set, not just on the
/// numeric verbs, so `%010q` of `"z"` is `0000000"z"` and `%010T` of an `int`
/// is `0000000int`. It goes in front of the whole body: text has no sign for it
/// to slot behind, and sniffing one would mangle a string that merely begins
/// `-` or `0x`.
///
/// Left-justifying always fills with spaces, because Go clears the `0` flag the
/// moment it reads a `-` (`doPrintf`: "Do not pad with zeros to the right").
fn pad_text(body: &str, spec: &Spec) -> String {
    let Some(fill) = pad_room(body, spec) else {
        return body.to_string();
    };
    if spec.left {
        return format!("{body}{}", " ".repeat(fill));
    }
    let fill_char = if spec.zero { "0" } else { " " };
    format!("{}{body}", fill_char.repeat(fill))
}

/// Go's `fmt.fmtInteger` / `fmt.fmtFloat` fill — for a rendering that is a
/// *number*, where the zero fill goes behind the sign rather than in front of
/// it.
fn pad_number(body: &str, verb: char, spec: &Spec) -> String {
    let Some(fill) = pad_room(body, spec) else {
        return body.to_string();
    };
    if spec.left {
        return format!("{body}{}", " ".repeat(fill));
    }
    // An explicit precision already fixed the digit count on an integer verb, so
    // Go drops the `0` flag there and pads the rest of the width with spaces:
    // `%05.2d` of `3` is `   03`, not `00003`. A float keeps both, because its
    // precision counts fraction digits rather than leading ones.
    let zero = spec.zero && !(spec.prec.is_some() && matches!(verb, 'd' | 'x' | 'X' | 'o' | 'b'));
    if !zero {
        return format!("{}{body}", " ".repeat(fill));
    }
    // The space the ` ` flag left in place of a sign is part of the sign for
    // this purpose, so the zeros go after it: `% 05d` of `42` is ` 0042`.
    let (sign, rest) = match body.strip_prefix(['-', '+', ' ']) {
        Some(d) => (&body[..1], d),
        None => ("", body),
    };
    // A `#` base prefix sits between the sign and the zeros and does not
    // count toward the width at all: `%#08x` of `-9` is `-0x0000009`, whose
    // seven digits plus the sign make the eight. (Octal's prefix is a `0`,
    // which the fill supplies on its own.)
    let (prefix, digits) = match rest.get(..2) {
        Some(p @ ("0x" | "0X" | "0b")) => (p, &rest[2..]),
        _ => ("", rest),
    };
    format!("{sign}{prefix}{}{digits}", "0".repeat(fill + prefix.len()))
}

/// Whether a `fmt` operand renders as a number, and so takes [`pad_number`]'s
/// sign-first zero fill rather than [`pad_text`]'s.
fn is_num_operand(v: &Value) -> bool {
    arg_uint(v).is_some() || matches!(unname(v), Value::Int(_) | Value::Float(_))
}

/// A minimal `strings` and `strconv` standard library. Each exported function
/// is a numbered builtin the compiler dispatches `strings.X` / `strconv.X` calls
/// to. Split/Fields return heap slices; Join reads one.
pub mod stdlib {
    use super::{go_str, heap_alloc, pop_args, HostObj, HEAP};
    use fusevm::{Value, VM};
    use std::cell::RefCell;

    // strings.*
    pub const TO_UPPER: u16 = 830;
    pub const TO_LOWER: u16 = 831;
    pub const CONTAINS: u16 = 832;
    pub const HAS_PREFIX: u16 = 833;
    pub const HAS_SUFFIX: u16 = 834;
    pub const TRIM_SPACE: u16 = 835;
    pub const SPLIT: u16 = 836;
    pub const JOIN: u16 = 837;
    pub const REPEAT: u16 = 838;
    pub const INDEX: u16 = 839;
    pub const REPLACE_ALL: u16 = 840;
    pub const FIELDS: u16 = 841;
    pub const COUNT: u16 = 842;
    pub const TRIM_PREFIX: u16 = 843;
    pub const TRIM_SUFFIX: u16 = 844;
    pub const TRIM: u16 = 845;
    pub const TITLE: u16 = 846;
    pub const EQUAL_FOLD: u16 = 847;
    pub const LAST_INDEX: u16 = 848;
    pub const REPLACE: u16 = 849;
    // strconv.*
    pub const ITOA: u16 = 850;
    pub const ATOI: u16 = 851;
    pub const PARSE_INT: u16 = 852;
    pub const PARSE_FLOAT: u16 = 853;
    pub const FORMAT_INT: u16 = 854;
    pub const QUOTE: u16 = 855;
    /// `strconv.ErrSyntax` / `strconv.ErrRange` — the sentinel by name.
    pub const STRCONV_ERR: u16 = 856;
    // math.*
    pub const ABS: u16 = 860;
    pub const SQRT: u16 = 861;
    pub const POW: u16 = 862;
    pub const FLOOR: u16 = 863;
    pub const CEIL: u16 = 864;
    pub const ROUND: u16 = 865;
    pub const TRUNC: u16 = 866;
    pub const MOD_F: u16 = 867;
    pub const HYPOT: u16 = 868;
    pub const MAX_F: u16 = 869;
    pub const MIN_F: u16 = 870;
    // math.* trigonometry / exponential / logarithm (added wave).
    pub const SIN: u16 = 907;
    pub const COS: u16 = 908;
    pub const TAN: u16 = 909;
    pub const ASIN: u16 = 910;
    pub const ACOS: u16 = 911;
    pub const ATAN: u16 = 912;
    pub const ATAN2: u16 = 913;
    pub const SINH: u16 = 914;
    pub const COSH: u16 = 915;
    pub const TANH: u16 = 916;
    pub const EXP: u16 = 917;
    pub const LOG: u16 = 918;
    pub const LOG2: u16 = 919;
    pub const LOG10: u16 = 920;
    pub const CBRT: u16 = 921;
    // strings.* / strconv.* (added wave).
    pub const CONTAINS_RUNE: u16 = 972;
    pub const CONTAINS_ANY: u16 = 973;
    pub const INDEX_BYTE: u16 = 974;
    pub const INDEX_RUNE: u16 = 975;
    pub const INDEX_ANY: u16 = 976;
    pub const LAST_INDEX_BYTE: u16 = 977;
    pub const SPLIT_N: u16 = 978;
    pub const TRIM_LEFT: u16 = 979;
    pub const TRIM_RIGHT: u16 = 980;
    pub const COMPARE: u16 = 981;
    pub const PARSE_BOOL: u16 = 982;
    pub const FORMAT_BOOL: u16 = 983;
    pub const FORMAT_FLOAT: u16 = 984;
    pub const QUOTE_RUNE: u16 = 985;
    // sort.*
    pub const SORT_INTS: u16 = 875;
    pub const SORT_STRINGS: u16 = 876;
    pub const SORT_FLOAT64S: u16 = 877;
    // os.*
    pub const GETENV: u16 = 880;

    /// Resolve `pkg.func` to a stdlib builtin id, or `None` if unknown.
    pub fn resolve(pkg: &str, func: &str) -> Option<u16> {
        Some(match (pkg, func) {
            ("strings", "ToUpper") => TO_UPPER,
            ("strings", "ToLower") => TO_LOWER,
            ("strings", "Contains") => CONTAINS,
            ("strings", "HasPrefix") => HAS_PREFIX,
            ("strings", "HasSuffix") => HAS_SUFFIX,
            ("strings", "TrimSpace") => TRIM_SPACE,
            ("strings", "Split") => SPLIT,
            ("strings", "Join") => JOIN,
            ("strings", "Repeat") => REPEAT,
            ("strings", "Index") => INDEX,
            ("strings", "Replace") => REPLACE,
            ("strings", "ReplaceAll") => REPLACE_ALL,
            ("strings", "Fields") => FIELDS,
            ("strings", "Count") => COUNT,
            ("strings", "TrimPrefix") => TRIM_PREFIX,
            ("strings", "TrimSuffix") => TRIM_SUFFIX,
            ("strings", "Trim") => TRIM,
            ("strings", "Title") => TITLE,
            ("strings", "EqualFold") => EQUAL_FOLD,
            ("strings", "LastIndex") => LAST_INDEX,
            ("strings", "ContainsRune") => CONTAINS_RUNE,
            ("strings", "ContainsAny") => CONTAINS_ANY,
            ("strings", "IndexByte") => INDEX_BYTE,
            ("strings", "IndexRune") => INDEX_RUNE,
            ("strings", "IndexAny") => INDEX_ANY,
            ("strings", "LastIndexByte") => LAST_INDEX_BYTE,
            ("strings", "SplitN") => SPLIT_N,
            ("strings", "TrimLeft") => TRIM_LEFT,
            ("strings", "TrimRight") => TRIM_RIGHT,
            ("strings", "Compare") => COMPARE,
            ("strconv", "ParseBool") => PARSE_BOOL,
            ("strconv", "FormatBool") => FORMAT_BOOL,
            ("strconv", "FormatFloat") => FORMAT_FLOAT,
            ("strconv", "QuoteRune") => QUOTE_RUNE,
            ("strconv", "Itoa") => ITOA,
            ("strconv", "Atoi") => ATOI,
            ("strconv", "ParseInt") => PARSE_INT,
            ("strconv", "ParseFloat") => PARSE_FLOAT,
            ("strconv", "FormatInt") => FORMAT_INT,
            ("strconv", "Quote") => QUOTE,
            ("math", "Abs") => ABS,
            ("math", "Sqrt") => SQRT,
            ("math", "Pow") => POW,
            ("math", "Floor") => FLOOR,
            ("math", "Ceil") => CEIL,
            ("math", "Round") => ROUND,
            ("math", "Trunc") => TRUNC,
            ("math", "Mod") => MOD_F,
            ("math", "Hypot") => HYPOT,
            ("math", "Max") => MAX_F,
            ("math", "Min") => MIN_F,
            ("math", "Sin") => SIN,
            ("math", "Cos") => COS,
            ("math", "Tan") => TAN,
            ("math", "Asin") => ASIN,
            ("math", "Acos") => ACOS,
            ("math", "Atan") => ATAN,
            ("math", "Atan2") => ATAN2,
            ("math", "Sinh") => SINH,
            ("math", "Cosh") => COSH,
            ("math", "Tanh") => TANH,
            ("math", "Exp") => EXP,
            ("math", "Log") => LOG,
            ("math", "Log2") => LOG2,
            ("math", "Log10") => LOG10,
            ("math", "Cbrt") => CBRT,
            ("sort", "Ints") => SORT_INTS,
            ("sort", "Strings") => SORT_STRINGS,
            ("sort", "Float64s") => SORT_FLOAT64S,
            ("os", "Getenv") => GETENV,
            _ => return None,
        })
    }

    /// Whether `pkg.func` returns Go's `(value, error)` pair rather than a bare
    /// value — so the compiler destructures it and rejects its use where one
    /// value is expected.
    pub fn returns_error(pkg: &str, func: &str) -> bool {
        matches!(
            (pkg, func),
            ("strconv", "Atoi")
                | ("strconv", "ParseInt")
                | ("strconv", "ParseFloat")
                | ("strconv", "ParseBool")
        )
    }

    /// Resolve a package constant `pkg.NAME` (e.g. `math.Pi`) to its value, or
    /// `None` if unknown. Used by the compiler for bare selector values.
    pub fn resolve_const(pkg: &str, name: &str) -> Option<Value> {
        Some(match (pkg, name) {
            ("math", "Pi") => Value::Float(std::f64::consts::PI),
            ("math", "E") => Value::Float(std::f64::consts::E),
            ("math", "Sqrt2") => Value::Float(std::f64::consts::SQRT_2),
            ("math", "MaxInt64") => Value::Int(i64::MAX),
            ("math", "MinInt64") => Value::Int(i64::MIN),
            ("math", "MaxInt") => Value::Int(i64::MAX),
            ("math", "MinInt") => Value::Int(i64::MIN),
            _ => return None,
        })
    }

    pub fn install(vm: &mut VM) {
        vm.register_builtin(TO_UPPER, |vm, a| s1(vm, a, |s| s.to_uppercase()));
        vm.register_builtin(TO_LOWER, |vm, a| s1(vm, a, |s| s.to_lowercase()));
        vm.register_builtin(TRIM_SPACE, |vm, a| s1(vm, a, |s| s.trim().to_string()));
        vm.register_builtin(CONTAINS, |vm, a| b2(vm, a, |s, p| s.contains(p)));
        vm.register_builtin(HAS_PREFIX, |vm, a| b2(vm, a, |s, p| s.starts_with(p)));
        vm.register_builtin(HAS_SUFFIX, |vm, a| b2(vm, a, |s, p| s.ends_with(p)));
        vm.register_builtin(INDEX, b_index);
        vm.register_builtin(REPEAT, b_repeat);
        vm.register_builtin(REPLACE, b_replace);
        vm.register_builtin(REPLACE_ALL, b_replace_all);
        vm.register_builtin(SPLIT, b_split);
        vm.register_builtin(FIELDS, b_fields);
        vm.register_builtin(JOIN, b_join);
        vm.register_builtin(ITOA, b_itoa);
        vm.register_builtin(ATOI, b_atoi);
        // extra strings.*
        vm.register_builtin(COUNT, b_count);
        vm.register_builtin(TRIM_PREFIX, |vm, a| {
            two_str(vm, a, |s, p| s.strip_prefix(p).unwrap_or(s).to_string())
        });
        vm.register_builtin(TRIM_SUFFIX, |vm, a| {
            two_str(vm, a, |s, p| s.strip_suffix(p).unwrap_or(s).to_string())
        });
        vm.register_builtin(TRIM, |vm, a| {
            two_str(vm, a, |s, cut| {
                s.trim_matches(|c| cut.contains(c)).to_string()
            })
        });
        vm.register_builtin(TITLE, |vm, a| s1(vm, a, title_case));
        vm.register_builtin(EQUAL_FOLD, |vm, a| {
            b2(vm, a, |s, t| s.eq_ignore_ascii_case(t))
        });
        vm.register_builtin(LAST_INDEX, b_last_index);
        // `strings.ContainsRune` / `IndexRune` take a `rune`, which reaches here
        // as the code point rather than a string.
        vm.register_builtin(CONTAINS_RUNE, |vm, a| {
            let args = pop_args(vm, a);
            let s = args.first().map(go_str).unwrap_or_default();
            Value::bool(rune_of(args.get(1)).is_some_and(|r| s.contains(r)))
        });
        vm.register_builtin(INDEX_RUNE, |vm, a| {
            let args = pop_args(vm, a);
            let s = args.first().map(go_str).unwrap_or_default();
            let at = rune_of(args.get(1)).and_then(|r| s.find(r));
            Value::Int(at.map(|b| b as i64).unwrap_or(-1))
        });
        vm.register_builtin(CONTAINS_ANY, |vm, a| {
            b2(vm, a, |s, chars| s.contains(|c| chars.contains(c)))
        });
        // `IndexByte` / `LastIndexByte` answer in *bytes*, over bytes: a byte of
        // a multi-byte rune is a legitimate match, which searching by `char`
        // would miss.
        vm.register_builtin(INDEX_BYTE, |vm, a| {
            let args = pop_args(vm, a);
            let s = args.first().map(go_str).unwrap_or_default();
            let b = args.get(1).map(|v| v.to_int()).unwrap_or(-1);
            let at = u8::try_from(b)
                .ok()
                .and_then(|b| s.bytes().position(|x| x == b));
            Value::Int(at.map(|i| i as i64).unwrap_or(-1))
        });
        vm.register_builtin(LAST_INDEX_BYTE, |vm, a| {
            let args = pop_args(vm, a);
            let s = args.first().map(go_str).unwrap_or_default();
            let b = args.get(1).map(|v| v.to_int()).unwrap_or(-1);
            let at = u8::try_from(b)
                .ok()
                .and_then(|b| s.bytes().rposition(|x| x == b));
            Value::Int(at.map(|i| i as i64).unwrap_or(-1))
        });
        vm.register_builtin(INDEX_ANY, |vm, a| {
            let args = pop_args(vm, a);
            let s = args.first().map(go_str).unwrap_or_default();
            let chars = args.get(1).map(go_str).unwrap_or_default();
            let at = s.find(|c| chars.contains(c));
            Value::Int(at.map(|b| b as i64).unwrap_or(-1))
        });
        vm.register_builtin(SPLIT_N, b_split_n);
        vm.register_builtin(TRIM_LEFT, |vm, a| {
            two_str(vm, a, |s, cut| {
                s.trim_start_matches(|c| cut.contains(c)).to_string()
            })
        });
        vm.register_builtin(TRIM_RIGHT, |vm, a| {
            two_str(vm, a, |s, cut| {
                s.trim_end_matches(|c| cut.contains(c)).to_string()
            })
        });
        vm.register_builtin(COMPARE, |vm, a| {
            let args = pop_args(vm, a);
            let s = args.first().map(go_str).unwrap_or_default();
            let t = args.get(1).map(go_str).unwrap_or_default();
            Value::Int(match s.cmp(&t) {
                std::cmp::Ordering::Less => -1,
                std::cmp::Ordering::Equal => 0,
                std::cmp::Ordering::Greater => 1,
            })
        });
        vm.register_builtin(PARSE_BOOL, b_parse_bool);
        vm.register_builtin(FORMAT_BOOL, |vm, a| {
            let args = pop_args(vm, a);
            let b = matches!(args.first(), Some(Value::Bool(true)));
            Value::str(if b { "true" } else { "false" })
        });
        vm.register_builtin(FORMAT_FLOAT, b_format_float);
        vm.register_builtin(QUOTE_RUNE, |vm, a| {
            let args = pop_args(vm, a);
            Value::str(super::go_quote_rune(
                args.first().map(|v| v.to_int()).unwrap_or(0),
            ))
        });
        // extra strconv.*
        vm.register_builtin(PARSE_INT, b_parse_int);
        vm.register_builtin(PARSE_FLOAT, b_parse_float);
        vm.register_builtin(FORMAT_INT, b_format_int);
        // `strconv.Quote` is the same double-quoted Go literal `%q` produces —
        // escapes and all, which wrapping the raw string in quotes was not.
        vm.register_builtin(QUOTE, |vm, a| s1(vm, a, super::go_quote));
        vm.register_builtin(STRCONV_ERR, b_strconv_err);
        // math.*
        vm.register_builtin(ABS, |vm, a| math1(vm, a, f64::abs));
        vm.register_builtin(SQRT, |vm, a| math1(vm, a, f64::sqrt));
        vm.register_builtin(FLOOR, |vm, a| math1(vm, a, f64::floor));
        vm.register_builtin(CEIL, |vm, a| math1(vm, a, f64::ceil));
        vm.register_builtin(ROUND, |vm, a| math1(vm, a, f64::round));
        vm.register_builtin(TRUNC, |vm, a| math1(vm, a, f64::trunc));
        vm.register_builtin(POW, |vm, a| math2(vm, a, f64::powf));
        vm.register_builtin(MOD_F, |vm, a| math2(vm, a, |x, y| x % y));
        vm.register_builtin(HYPOT, |vm, a| math2(vm, a, f64::hypot));
        vm.register_builtin(MAX_F, |vm, a| math2(vm, a, f64::max));
        vm.register_builtin(MIN_F, |vm, a| math2(vm, a, f64::min));
        vm.register_builtin(SIN, |vm, a| math1(vm, a, f64::sin));
        vm.register_builtin(COS, |vm, a| math1(vm, a, f64::cos));
        vm.register_builtin(TAN, |vm, a| math1(vm, a, f64::tan));
        vm.register_builtin(ASIN, |vm, a| math1(vm, a, f64::asin));
        vm.register_builtin(ACOS, |vm, a| math1(vm, a, f64::acos));
        vm.register_builtin(ATAN, |vm, a| math1(vm, a, f64::atan));
        vm.register_builtin(ATAN2, |vm, a| math2(vm, a, f64::atan2));
        vm.register_builtin(SINH, |vm, a| math1(vm, a, f64::sinh));
        vm.register_builtin(COSH, |vm, a| math1(vm, a, f64::cosh));
        vm.register_builtin(TANH, |vm, a| math1(vm, a, f64::tanh));
        vm.register_builtin(EXP, |vm, a| math1(vm, a, f64::exp));
        vm.register_builtin(LOG, |vm, a| math1(vm, a, f64::ln));
        vm.register_builtin(LOG2, |vm, a| math1(vm, a, f64::log2));
        vm.register_builtin(LOG10, |vm, a| math1(vm, a, f64::log10));
        vm.register_builtin(CBRT, |vm, a| math1(vm, a, f64::cbrt));
        // sort.*
        vm.register_builtin(SORT_INTS, |vm, a| {
            sort_slice(vm, a, |x, y| x.to_int().cmp(&y.to_int()))
        });
        vm.register_builtin(SORT_FLOAT64S, |vm, a| {
            sort_slice(vm, a, |x, y| {
                x.to_float()
                    .partial_cmp(&y.to_float())
                    .unwrap_or(std::cmp::Ordering::Equal)
            })
        });
        vm.register_builtin(SORT_STRINGS, |vm, a| {
            sort_slice(vm, a, |x, y| go_str(x).cmp(&go_str(y)))
        });
        // os.*
        vm.register_builtin(GETENV, |vm, a| {
            let args = pop_args(vm, a);
            let k = args.first().map(go_str).unwrap_or_default();
            Value::str(std::env::var(&k).unwrap_or_default())
        });
    }

    /// A two-string-arg → string builtin.
    fn two_str(vm: &mut VM, argc: u8, f: impl Fn(&str, &str) -> String) -> Value {
        let args = pop_args(vm, argc);
        let s = args.first().map(go_str).unwrap_or_default();
        let p = args.get(1).map(go_str).unwrap_or_default();
        Value::str(f(&s, &p))
    }

    /// A one-float-arg → float `math` builtin.
    fn math1(vm: &mut VM, argc: u8, f: impl Fn(f64) -> f64) -> Value {
        let args = pop_args(vm, argc);
        Value::Float(f(args.first().map(|v| v.to_float()).unwrap_or(0.0)))
    }

    /// A two-float-arg → float `math` builtin.
    fn math2(vm: &mut VM, argc: u8, f: impl Fn(f64, f64) -> f64) -> Value {
        let args = pop_args(vm, argc);
        let a = args.first().map(|v| v.to_float()).unwrap_or(0.0);
        let b = args.get(1).map(|v| v.to_float()).unwrap_or(0.0);
        Value::Float(f(a, b))
    }

    /// Sort a heap slice in place by `cmp`. Returns nil (sort.* are void).
    fn sort_slice(
        vm: &mut VM,
        argc: u8,
        cmp: impl Fn(&Value, &Value) -> std::cmp::Ordering,
    ) -> Value {
        let args = pop_args(vm, argc);
        if let Some(Value::Obj(id)) = args.first() {
            // Sort the backing in place over the (sub-)slice's element range, so
            // sorting a view (`sort.Ints(s[1:4])`) sorts through the parent.
            if let Some((backing, offset, len)) = super::slice_backing(*id) {
                HEAP.with(|h| {
                    if let Some(HostObj::Slice { elems: a, .. }) =
                        h.borrow_mut().get_mut(backing as usize)
                    {
                        a[offset..offset + len].sort_by(cmp);
                    }
                });
            }
        }
        Value::Undef
    }

    fn title_case(s: &str) -> String {
        s.split(' ')
            .map(|w| {
                let mut c = w.chars();
                match c.next() {
                    Some(f) => f.to_uppercase().chain(c).collect::<String>(),
                    None => String::new(),
                }
            })
            .collect::<Vec<_>>()
            .join(" ")
    }

    fn b_count(vm: &mut VM, argc: u8) -> Value {
        let args = pop_args(vm, argc);
        let s = args.first().map(go_str).unwrap_or_default();
        let sub = args.get(1).map(go_str).unwrap_or_default();
        Value::Int(if sub.is_empty() {
            s.chars().count() as i64 + 1
        } else {
            s.matches(&sub).count() as i64
        })
    }

    fn b_last_index(vm: &mut VM, argc: u8) -> Value {
        let args = pop_args(vm, argc);
        let s = args.first().map(go_str).unwrap_or_default();
        let sub = args.get(1).map(go_str).unwrap_or_default();
        Value::Int(s.rfind(&sub).map(|b| b as i64).unwrap_or(-1))
    }

    /// `strconv.ParseInt(s, base, bitSize) (int64, error)`.
    fn b_parse_int(vm: &mut VM, argc: u8) -> Value {
        let args = pop_args(vm, argc);
        let s = args.first().map(go_str).unwrap_or_default();
        let base = args.get(1).map(|v| v.to_int()).unwrap_or(10).max(2) as u32;
        parse_signed(&s, base, "ParseInt")
    }

    /// `strconv.ParseFloat(s, bitSize) (float64, error)`. An overflowing literal
    /// yields ±Inf *and* a range error, as Go's does; a written `Inf` does not.
    fn b_parse_float(vm: &mut VM, argc: u8) -> Value {
        let args = pop_args(vm, argc);
        let s = args.first().map(go_str).unwrap_or_default();
        match s.parse::<f64>() {
            Ok(f)
                if f.is_infinite()
                    && !s.trim_start_matches(['+', '-']).eq_ignore_ascii_case("inf") =>
            {
                parsed(Value::Float(f), Some(num_error("ParseFloat", &s, RANGE)))
            }
            Ok(f) => parsed(Value::Float(f), None),
            Err(_) => parsed(Value::Float(0.0), Some(num_error("ParseFloat", &s, SYNTAX))),
        }
    }

    fn b_format_int(vm: &mut VM, argc: u8) -> Value {
        let args = pop_args(vm, argc);
        let n = args.first().map(|v| v.to_int()).unwrap_or(0);
        let base = args.get(1).map(|v| v.to_int()).unwrap_or(10);
        Value::str(match base {
            2 => format!("{n:b}"),
            8 => format!("{n:o}"),
            16 => format!("{n:x}"),
            _ => n.to_string(),
        })
    }

    /// A one-string-arg → string builtin.
    fn s1(vm: &mut VM, argc: u8, f: impl Fn(&str) -> String) -> Value {
        let args = pop_args(vm, argc);
        Value::str(f(&args.first().map(go_str).unwrap_or_default()))
    }

    /// A two-string-arg → bool builtin.
    fn b2(vm: &mut VM, argc: u8, f: impl Fn(&str, &str) -> bool) -> Value {
        let args = pop_args(vm, argc);
        let s = args.first().map(go_str).unwrap_or_default();
        let p = args.get(1).map(go_str).unwrap_or_default();
        Value::bool(f(&s, &p))
    }

    fn b_index(vm: &mut VM, argc: u8) -> Value {
        let args = pop_args(vm, argc);
        let s = args.first().map(go_str).unwrap_or_default();
        let sub = args.get(1).map(go_str).unwrap_or_default();
        Value::Int(s.find(&sub).map(|b| b as i64).unwrap_or(-1))
    }

    fn b_repeat(vm: &mut VM, argc: u8) -> Value {
        let args = pop_args(vm, argc);
        let s = args.first().map(go_str).unwrap_or_default();
        let n = args.get(1).map(|v| v.to_int()).unwrap_or(0).max(0) as usize;
        Value::str(s.repeat(n))
    }

    fn b_replace(vm: &mut VM, argc: u8) -> Value {
        let args = pop_args(vm, argc);
        let s = args.first().map(go_str).unwrap_or_default();
        let old = args.get(1).map(go_str).unwrap_or_default();
        let new = args.get(2).map(go_str).unwrap_or_default();
        let n = args.get(3).map(|v| v.to_int()).unwrap_or(-1);
        if old.is_empty() {
            return Value::str(s);
        }
        Value::str(if n < 0 {
            s.replace(&old, &new)
        } else {
            s.replacen(&old, &new, n as usize)
        })
    }

    fn b_replace_all(vm: &mut VM, argc: u8) -> Value {
        let args = pop_args(vm, argc);
        let s = args.first().map(go_str).unwrap_or_default();
        let old = args.get(1).map(go_str).unwrap_or_default();
        let new = args.get(2).map(go_str).unwrap_or_default();
        Value::str(if old.is_empty() {
            s
        } else {
            s.replace(&old, &new)
        })
    }

    fn b_split(vm: &mut VM, argc: u8) -> Value {
        let args = pop_args(vm, argc);
        let s = args.first().map(go_str).unwrap_or_default();
        let sep = args.get(1).map(go_str).unwrap_or_default();
        let parts: Vec<Value> = if sep.is_empty() {
            s.chars().map(|c| Value::str(c.to_string())).collect()
        } else {
            s.split(&sep).map(Value::str).collect()
        };
        Value::Obj(heap_alloc(HostObj::slice(parts)))
    }

    /// `strings.SplitN(s, sep, n)` — at most `n` parts, the last holding the
    /// unsplit remainder. `n == 0` is a nil slice and `n < 0` is `Split`.
    fn b_split_n(vm: &mut VM, argc: u8) -> Value {
        let args = pop_args(vm, argc);
        let s = args.first().map(go_str).unwrap_or_default();
        let sep = args.get(1).map(go_str).unwrap_or_default();
        let n = args.get(2).map(|v| v.to_int()).unwrap_or(-1);
        if n == 0 {
            return super::typed_nil("[]string");
        }
        let parts: Vec<Value> = match (sep.is_empty(), usize::try_from(n).ok()) {
            // An empty separator splits into runes. `n` caps how many, and the
            // last one holds the rest of the string whole — Go's `explode`
            // writes `n-1` single runes and then the remainder.
            (true, Some(k)) => {
                let mut rest = s.as_str();
                let mut out = Vec::new();
                while out.len() + 1 < k && !rest.is_empty() {
                    let c = rest.chars().next().expect("non-empty");
                    out.push(Value::str(c.to_string()));
                    rest = &rest[c.len_utf8()..];
                }
                if k > 0 && !rest.is_empty() {
                    out.push(Value::str(rest.to_string()));
                }
                out
            }
            (true, None) => s.chars().map(|c| Value::str(c.to_string())).collect(),
            (false, Some(k)) => s.splitn(k, &sep).map(Value::str).collect(),
            (false, None) => s.split(&sep).map(Value::str).collect(),
        };
        Value::Obj(heap_alloc(HostObj::slice(parts)))
    }

    /// `strconv.ParseBool(s) (bool, error)` — Go accepts exactly these ten
    /// spellings and nothing else, not even `TRUE`/`False` outside the list.
    fn b_parse_bool(vm: &mut VM, argc: u8) -> Value {
        let args = pop_args(vm, argc);
        let s = args.first().map(go_str).unwrap_or_default();
        match s.as_str() {
            "1" | "t" | "T" | "TRUE" | "true" | "True" => parsed(Value::bool(true), None),
            "0" | "f" | "F" | "FALSE" | "false" | "False" => parsed(Value::bool(false), None),
            _ => parsed(Value::bool(false), Some(num_error("ParseBool", &s, SYNTAX))),
        }
    }

    /// A `rune` argument as a `char`. It reaches a builtin as the code point,
    /// which is how every other integer arrives.
    fn rune_of(v: Option<&Value>) -> Option<char> {
        char::from_u32(u32::try_from(v?.to_int()).ok()?)
    }

    /// `strconv.FormatFloat(f, 'f', -1, bits)`: the shortest decimal that parses
    /// back to `f` at that width, always in fixed notation. Rust's `Display` for
    /// a float is exactly that — shortest round-trip, and never an exponent, so
    /// `1e21` writes its 22 digits out as Go's `'f'` does.
    fn shortest_fixed(f: f64, narrowed: bool) -> String {
        if narrowed {
            format!("{}", f as f32)
        } else {
            format!("{f}")
        }
    }

    /// `strconv.FormatFloat(f, 'e', -1, 32)`: the `%e` form of a `float32`'s
    /// shortest round-trip decimal. Rust's `{:e}` for an `f32` produces exactly
    /// those digits — only the exponent spelling differs from Go's signed
    /// two-digit form, which [`super::format_e`] writes.
    fn shortest_sci32(f: f32, upper: bool) -> String {
        let s = format!("{f:e}");
        let e = if upper { 'E' } else { 'e' };
        match s.split_once('e') {
            Some((mant, exp)) => super::format_e(mant, exp.parse().unwrap_or(0), e),
            None => s,
        }
    }

    /// `strconv.FormatFloat(f, fmt, prec, bitSize)`.
    ///
    /// `prec == -1` is "the fewest digits that parse back to `f` exactly", which
    /// is what `%v` already computes — at the *width* `bitSize` names, so a
    /// `float32` gets the shorter `float32` answer.
    fn b_format_float(vm: &mut VM, argc: u8) -> Value {
        let args = pop_args(vm, argc);
        let f = args.first().map(|v| v.to_float()).unwrap_or(0.0);
        let verb =
            char::from_u32(args.get(1).map(|v| v.to_int()).unwrap_or(0) as u32).unwrap_or('\0');
        let prec = args.get(2).map(|v| v.to_int()).unwrap_or(-1);
        let bits = args.get(3).map(|v| v.to_int()).unwrap_or(64);
        let prec = usize::try_from(prec).ok();
        let narrowed = bits == 32;
        // Go reads the value *at* `bitSize` before it formats anything
        // (`genericFtoa` takes the `float32` bits), so a 32-bit request rounds
        // first and writes second — which shows in an explicit precision
        // (`%.10f` of `1.0/3` at 32 is `0.3333333433`) and in an overflow to
        // infinity (`1e300` at 32 is `+Inf`).
        let f = if narrowed { f as f32 as f64 } else { f };
        // An infinity or a NaN is the whole answer, before any verb: Go's
        // `ftoa` returns it from the exponent check with no formatting at all,
        // so even `G` writes `+Inf` rather than an upper-cased one.
        if f.is_nan() {
            return Value::str("NaN");
        }
        if f.is_infinite() {
            return Value::str(if f < 0.0 { "-Inf" } else { "+Inf" });
        }
        let out = match verb {
            'f' | 'F' => match prec {
                Some(p) => format!("{:.*}", p, f),
                None => shortest_fixed(f, narrowed),
            },
            'e' | 'E' => match (prec, narrowed) {
                (None, true) => shortest_sci32(f as f32, verb == 'E'),
                _ => super::format_float_e(f, prec, verb == 'E'),
            },
            'g' | 'G' => match (prec, narrowed) {
                (None, true) => {
                    let g = super::format_float32(f as f32);
                    if verb == 'G' {
                        g.to_uppercase()
                    } else {
                        g
                    }
                }
                _ => super::format_float_g(f, prec, verb == 'G'),
            },
            // Go also defines `b`, `x` and `X` (binary and hexadecimal
            // exponents). Faulting is the honest answer: the alternative is a
            // decimal string presented as a hex-float one.
            'b' | 'x' | 'X' => {
                super::ffi_fault(
                    vm,
                    format!("go-rs: strconv.FormatFloat: fmt '{verb}' is not implemented"),
                );
                return Value::Undef;
            }
            // Go's `ftoa` writes `%` and the byte for anything else.
            other => format!("%{other}"),
        };
        Value::str(out)
    }

    fn b_fields(vm: &mut VM, argc: u8) -> Value {
        let args = pop_args(vm, argc);
        let s = args.first().map(go_str).unwrap_or_default();
        let parts: Vec<Value> = s.split_whitespace().map(Value::str).collect();
        Value::Obj(heap_alloc(HostObj::slice(parts)))
    }

    fn b_join(vm: &mut VM, argc: u8) -> Value {
        let args = pop_args(vm, argc);
        let sep = args.get(1).map(go_str).unwrap_or_default();
        let joined = match args.first() {
            Some(Value::Obj(id)) => HEAP.with(|h| {
                let h = h.borrow();
                let elems: &[Value] = match h.get(*id as usize) {
                    Some(HostObj::Slice { elems: a, .. }) => a,
                    Some(HostObj::SliceView {
                        backing,
                        offset,
                        len,
                        ..
                    }) => match h.get(*backing as usize) {
                        Some(HostObj::Slice { elems: a, .. }) => &a[*offset..*offset + *len],
                        _ => &[],
                    },
                    _ => &[],
                };
                elems.iter().map(go_str).collect::<Vec<_>>().join(&sep)
            }),
            _ => String::new(),
        };
        Value::str(joined)
    }

    /// `strconv.Itoa(n)` — integer to decimal string.
    fn b_itoa(vm: &mut VM, argc: u8) -> Value {
        let args = pop_args(vm, argc);
        Value::str(args.first().map(|v| v.to_int()).unwrap_or(0).to_string())
    }

    /// A `(value, error)` result as the 2-element tuple the compiler
    /// destructures, with `err` nil on success.
    fn parsed(value: Value, err: Option<Value>) -> Value {
        let e = err.unwrap_or(Value::Undef);
        Value::Obj(heap_alloc(HostObj::slice(vec![value, e])))
    }

    thread_local! {
        /// The two `strconv` sentinel errors, allocated once per run so every
        /// `NumError.Err` and every `strconv.ErrSyntax` mention is the *same*
        /// pointer — which is what `errors.Is` compares. Cleared by
        /// [`super::heap_reset`] along with the heap the handles index.
        static SENTINELS: RefCell<std::collections::HashMap<&'static str, Value>> =
            RefCell::new(std::collections::HashMap::new());
    }

    pub(super) fn sentinels_reset() {
        SENTINELS.with(|s| s.borrow_mut().clear());
    }

    /// `strconv.ErrSyntax` / `strconv.ErrRange` — Go's `errors.New` sentinels,
    /// memoized so repeated mentions compare equal by pointer.
    fn sentinel(reason: &'static str) -> Value {
        SENTINELS.with(|s| {
            s.borrow_mut()
                .entry(reason)
                .or_insert_with(|| super::make_error(reason.to_string()))
                .clone()
        })
    }

    /// `strconv.ErrSyntax` / `strconv.ErrRange` read as a value (stack: name).
    fn b_strconv_err(vm: &mut VM, argc: u8) -> Value {
        let args = pop_args(vm, argc);
        match args.first().map(go_str).unwrap_or_default().as_str() {
            "ErrRange" => sentinel(RANGE),
            _ => sentinel(SYNTAX),
        }
    }

    /// The `*strconv.NumError` Go returns from a failed conversion: the function
    /// name, the input, and the `ErrSyntax`/`ErrRange` sentinel it wraps. Its
    /// `Error()` and `Unwrap()` are synthesized as Go source by `crate::pkg`, so
    /// the message text, `errors.Is` and `errors.As` all come off the real type.
    fn num_error(func: &str, num: &str, reason: &'static str) -> Value {
        Value::Obj(heap_alloc(HostObj::Struct {
            type_name: NUM_ERROR.to_string(),
            fields: vec![
                ("Func".to_string(), Value::str(func.to_string())),
                ("Num".to_string(), Value::str(num.to_string())),
                ("Err".to_string(), sentinel(reason)),
            ],
            by_ref: true,
        }))
    }

    /// The linked name of `strconv`'s `NumError` — `strconv` is a native package,
    /// so its one source-level type is synthesized under its qualified name.
    pub const NUM_ERROR: &str = "strconv.NumError";

    const SYNTAX: &str = "invalid syntax";
    const RANGE: &str = "value out of range";

    /// Parse a signed integer in `base` the way `strconv` does: no surrounding
    /// whitespace is allowed, and an out-of-range value saturates *and* reports a
    /// range error (Go returns the clamped value alongside it).
    fn parse_signed(s: &str, base: u32, func: &str) -> Value {
        match i64::from_str_radix(s, base) {
            Ok(n) => parsed(Value::Int(n), None),
            Err(e) => {
                let (v, reason) = match e.kind() {
                    std::num::IntErrorKind::PosOverflow => (i64::MAX, RANGE),
                    std::num::IntErrorKind::NegOverflow => (i64::MIN, RANGE),
                    _ => (0, SYNTAX),
                };
                parsed(Value::Int(v), Some(num_error(func, s, reason)))
            }
        }
    }

    /// `strconv.Atoi(s) (int, error)` — a base-10 signed integer.
    fn b_atoi(vm: &mut VM, argc: u8) -> Value {
        let args = pop_args(vm, argc);
        let s = args.first().map(go_str).unwrap_or_default();
        parse_signed(&s, 10, "Atoi")
    }
}

/// Both operands as `i64`, or `None` if either is not an integer.
fn int_pair(a: &Value, b: &Value) -> Option<(i64, i64)> {
    match (a, b) {
        (Value::Int(x), Value::Int(y)) => Some((*x, *y)),
        _ => None,
    }
}

/// One operand an `Int` and the other a `Float`. Go's type system never builds
/// this pair out of a concrete expression — it has no implicit numeric
/// conversion — so it only arrives from an interface comparison. See
/// [`numeric_hook`].
fn mixed_num_pair(a: &Value, b: &Value) -> bool {
    matches!(
        (a, b),
        (Value::Int(_), Value::Float(_)) | (Value::Float(_), Value::Int(_))
    )
}

/// Strict numeric hook. fusevm delegates an arithmetic or comparison op here
/// when it cannot answer it natively, which happens for four reasons:
///
/// 1. An operand is non-numeric — a string or a heap object. This is Go's `+`
///    string-concatenation overload and its string ordering; every other
///    arithmetic op on a string is a type error, reported rather than coerced
///    (Go rejects `"a" - 1`).
/// 2. An all-numeric *integer* pair whose native op overflowed. Go's integers
///    are fixed-width and wrap, so these are re-done with `wrapping_*`.
/// 3. An operand is nil (`Undef`) — the erased zero value of a generic type
///    parameter, or a typed nil being compared for identity.
/// 4. A mixed `Int`/`Float` pair whose integer is past 2^53, where promoting it
///    to `f64` would round to a neighbouring value.
///
/// So "all-numeric never reaches here" is false in both directions: cases 2 and
/// 4 are all-numeric, and case 1 is the only one involving a string.
pub fn numeric_hook(op: NumOp, a: &Value, b: &Value) -> Result<Value, String> {
    match op {
        // The zero value of an erased generic type parameter (`var total T`) is
        // nil (`Undef`); Go would use T's concrete zero. Treat nil as the
        // additive identity so a generic accumulator matches Go for whichever
        // concrete type is passed: nil+int→int, nil+float→float, nil+str→str.
        NumOp::Add if matches!(a, Value::Undef) => Ok(b.clone()),
        NumOp::Add if matches!(b, Value::Undef) => Ok(a.clone()),
        // fusevm also routes here when a native integer op overflows. Go's
        // integers are fixed-width and wrap on overflow (two's complement), so
        // two integer operands wrap instead of reaching the string branches
        // below — which used to turn `int64max + 1` into the *concatenation*
        // "92233720368547758071" rather than -9223372036854775808.
        NumOp::Add | NumOp::Sub | NumOp::Mul if int_pair(a, b).is_some() => {
            let (x, y) = int_pair(a, b).unwrap_or((0, 0));
            Ok(Value::Int(match op {
                NumOp::Add => x.wrapping_add(y),
                NumOp::Sub => x.wrapping_sub(y),
                _ => x.wrapping_mul(y),
            }))
        }
        NumOp::Neg if matches!(a, Value::Int(_)) => Ok(Value::Int(a.to_int().wrapping_neg())),
        // fusevm answers a mixed Int/Float pair natively by promoting the
        // integer to f64, but past 2^53 that promotion lands on a
        // *neighbouring* value (3^34 = 16_677_181_699_666_569 has the f64
        // image …568), so it hands the pair over rather than round it.
        //
        // Answering does not need the exact integer. Go has no implicit
        // numeric conversion, so a mixed pair is unrepresentable in a concrete
        // expression: with `var i int64; var f float64`, both `i + f` and
        // `i == f` are compile errors ("mismatched types int64 and float64").
        // The one construct in valid Go that puts an int beside a float64
        // under a single operator is comparing two interfaces —
        // `any(1) == any(1.0)` — and interface equality is decided by dynamic
        // type before value: different types are never equal, whatever the
        // numbers are. Go prints `false` there, and would print `false` just
        // the same for `any(int64(1e18)) == any(float64(1e18))`. The rounding
        // fusevm was worried about cannot change the answer.
        //
        // That comparison no longer arrives here: the compiler routes an `==`
        // with an interface operand to [`GIFACE_EQ`], because a *small* mixed
        // pair is answered inside fusevm by promoting the integer exactly and
        // so never reaches the frontend at all. This arm stays as the backstop
        // for a mixed pair that reaches an ordinary comparison some other way,
        // and it answers the same way [`iface_eq`] does.
        NumOp::Eq | NumOp::Ne if mixed_num_pair(a, b) => Ok(Value::bool(op == NumOp::Ne)),
        // Every other operator on a mixed pair is unreachable from valid Go:
        // arithmetic needs an explicit conversion, and interfaces are
        // unordered (`a < b` on two `any` is "operator < not defined on
        // interface"). There is nothing to promote, so report it the way Go's
        // type checker does instead of falling through to the string branches
        // below — which would concatenate `+` into "166771816996665690.5" and
        // order `<` lexicographically, both silently.
        NumOp::Add
        | NumOp::Sub
        | NumOp::Mul
        | NumOp::Div
        | NumOp::Mod
        | NumOp::Pow
        | NumOp::Lt
        | NumOp::Gt
        | NumOp::Le
        | NumOp::Ge
            if mixed_num_pair(a, b) =>
        {
            let (x, y) = if matches!(a, Value::Int(_)) {
                ("int", "float64")
            } else {
                ("float64", "int")
            };
            Err(format!(
                "go-rs: invalid operation: operator {op:?} not defined on mismatched types {x} and {y}"
            ))
        }
        NumOp::Add => Ok(Value::str(format!("{}{}", go_str(a), go_str(b)))),
        // A typed nil (a nil slice or map) equals `nil` and nothing else — Go
        // permits no other comparison for those types.
        NumOp::Eq | NumOp::Ne if nil_kind(a).is_some() || nil_kind(b).is_some() => {
            let same = matches!((a, b), (Value::Undef, _) | (_, Value::Undef))
                || matches!((a, b), (Value::Obj(x), Value::Obj(y)) if x == y);
            Ok(Value::bool(if op == NumOp::Eq { same } else { !same }))
        }
        // A pointer compares by address, a struct value field by field.
        NumOp::Eq | NumOp::Ne if ptr_eq(a, b).is_some() => {
            let same = ptr_eq(a, b).unwrap_or(false);
            Ok(Value::bool(if op == NumOp::Eq { same } else { !same }))
        }
        NumOp::Eq => Ok(Value::bool(go_str(a) == go_str(b))),
        NumOp::Ne => Ok(Value::bool(go_str(a) != go_str(b))),
        NumOp::Lt => Ok(Value::bool(go_str(a) < go_str(b))),
        NumOp::Gt => Ok(Value::bool(go_str(a) > go_str(b))),
        NumOp::Le => Ok(Value::bool(go_str(a) <= go_str(b))),
        NumOp::Ge => Ok(Value::bool(go_str(a) >= go_str(b))),
        NumOp::Sub | NumOp::Mul | NumOp::Div | NumOp::Mod | NumOp::Pow => Err(format!(
            "go-rs: invalid operation: operator {op:?} not defined on `{}`",
            go_str(a)
        )),
        NumOp::Neg => Err(format!(
            "go-rs: invalid operation: unary `-` not defined on `{}`",
            go_str(a)
        )),
    }
}