phplang 0.2.17

PHP as a fusevm frontend: lexer, parser, and compiler to fusevm::Chunk on a PhpHost heap — the first compiled standalone PHP runtime
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
4027
4028
4029
4030
4031
4032
4033
4034
4035
4036
4037
4038
4039
4040
4041
4042
4043
4044
4045
4046
4047
4048
4049
4050
4051
4052
4053
4054
4055
4056
4057
4058
4059
4060
4061
4062
4063
4064
4065
4066
4067
4068
4069
4070
4071
4072
4073
4074
4075
4076
4077
4078
4079
4080
4081
4082
4083
4084
4085
4086
4087
4088
4089
4090
4091
4092
4093
4094
4095
4096
4097
4098
4099
4100
4101
4102
4103
4104
4105
4106
4107
4108
4109
4110
4111
4112
4113
4114
4115
4116
4117
4118
4119
4120
4121
4122
4123
4124
4125
4126
4127
4128
4129
4130
4131
4132
4133
4134
4135
4136
4137
4138
4139
4140
4141
4142
4143
4144
4145
4146
4147
4148
4149
4150
4151
4152
4153
4154
4155
4156
4157
4158
4159
4160
4161
4162
4163
4164
4165
4166
4167
4168
4169
4170
4171
4172
4173
4174
4175
4176
4177
4178
4179
4180
4181
4182
4183
4184
4185
4186
4187
4188
4189
4190
4191
4192
4193
4194
4195
4196
4197
4198
4199
4200
4201
4202
4203
4204
4205
4206
4207
4208
4209
4210
4211
4212
4213
4214
4215
4216
4217
4218
4219
4220
4221
4222
4223
4224
4225
4226
4227
4228
4229
4230
4231
4232
4233
4234
4235
4236
4237
4238
4239
4240
4241
4242
4243
4244
4245
4246
4247
4248
4249
4250
4251
4252
4253
4254
4255
4256
4257
4258
4259
4260
4261
4262
4263
4264
4265
4266
4267
4268
4269
4270
4271
4272
4273
4274
4275
4276
4277
4278
4279
4280
4281
4282
4283
4284
4285
4286
4287
4288
4289
4290
4291
4292
4293
4294
4295
4296
4297
4298
4299
4300
4301
4302
4303
4304
4305
4306
4307
4308
4309
4310
4311
4312
4313
4314
4315
4316
4317
4318
4319
4320
4321
4322
4323
4324
4325
4326
4327
4328
4329
4330
4331
4332
4333
4334
4335
4336
4337
4338
4339
4340
4341
4342
4343
4344
4345
4346
4347
4348
4349
4350
4351
4352
4353
4354
4355
4356
4357
4358
4359
4360
4361
4362
4363
4364
4365
4366
4367
4368
4369
4370
4371
4372
4373
4374
4375
4376
4377
4378
4379
4380
4381
4382
4383
4384
4385
4386
4387
4388
4389
4390
4391
4392
4393
4394
4395
4396
4397
4398
4399
4400
4401
4402
4403
4404
4405
4406
4407
4408
4409
4410
4411
4412
4413
4414
4415
4416
4417
4418
4419
4420
4421
4422
4423
4424
4425
4426
4427
4428
4429
4430
4431
4432
4433
4434
4435
4436
4437
4438
4439
4440
4441
4442
4443
4444
4445
4446
4447
4448
4449
4450
4451
4452
4453
4454
4455
4456
4457
4458
4459
4460
4461
4462
4463
4464
4465
4466
4467
4468
4469
4470
4471
4472
4473
4474
4475
4476
4477
4478
4479
4480
4481
4482
4483
4484
4485
4486
4487
4488
4489
4490
4491
4492
4493
4494
4495
4496
4497
4498
4499
4500
4501
4502
4503
4504
4505
4506
4507
4508
4509
4510
4511
4512
4513
4514
4515
4516
4517
4518
4519
4520
4521
4522
4523
4524
4525
4526
4527
4528
4529
4530
4531
4532
4533
4534
4535
4536
4537
4538
4539
4540
4541
4542
4543
4544
4545
4546
4547
4548
4549
4550
4551
4552
4553
4554
4555
4556
4557
4558
4559
4560
4561
4562
4563
4564
4565
4566
4567
4568
4569
4570
4571
4572
4573
4574
4575
4576
4577
4578
4579
4580
4581
4582
4583
4584
4585
4586
4587
4588
4589
4590
4591
4592
4593
4594
4595
4596
4597
4598
4599
4600
4601
4602
4603
4604
4605
4606
4607
4608
4609
4610
4611
4612
4613
4614
4615
4616
4617
4618
4619
4620
4621
4622
4623
4624
4625
4626
4627
4628
4629
4630
4631
4632
4633
4634
4635
4636
4637
4638
4639
4640
4641
4642
4643
4644
4645
4646
4647
4648
4649
4650
4651
4652
4653
4654
4655
4656
4657
4658
4659
4660
4661
4662
4663
4664
4665
4666
4667
4668
4669
4670
4671
4672
4673
4674
4675
4676
4677
4678
4679
4680
4681
4682
4683
4684
4685
4686
4687
4688
4689
4690
4691
4692
4693
4694
4695
4696
4697
4698
4699
4700
4701
4702
4703
4704
4705
4706
4707
4708
4709
4710
4711
4712
4713
4714
4715
4716
4717
4718
4719
4720
4721
4722
4723
4724
4725
4726
4727
4728
4729
4730
4731
4732
4733
4734
4735
4736
4737
4738
4739
4740
4741
4742
4743
4744
4745
4746
4747
4748
4749
4750
4751
4752
4753
4754
4755
4756
4757
4758
4759
4760
4761
4762
4763
4764
4765
4766
4767
4768
4769
4770
4771
4772
4773
4774
4775
4776
4777
4778
4779
4780
4781
4782
4783
4784
4785
4786
4787
4788
4789
4790
4791
4792
4793
4794
4795
4796
4797
4798
4799
4800
4801
4802
4803
4804
4805
4806
4807
4808
4809
4810
4811
4812
4813
4814
4815
4816
4817
4818
4819
4820
4821
4822
4823
4824
4825
4826
4827
4828
4829
4830
4831
4832
4833
4834
4835
4836
4837
4838
4839
4840
4841
4842
4843
4844
4845
4846
4847
4848
4849
4850
4851
4852
4853
4854
4855
4856
4857
4858
4859
4860
4861
4862
4863
4864
4865
4866
4867
4868
4869
4870
4871
4872
4873
4874
4875
4876
4877
4878
4879
4880
4881
4882
4883
4884
4885
4886
4887
4888
4889
4890
4891
4892
4893
4894
4895
4896
4897
4898
4899
4900
4901
4902
4903
4904
4905
4906
4907
4908
4909
4910
4911
4912
4913
4914
4915
4916
4917
4918
4919
4920
4921
4922
4923
4924
4925
4926
4927
4928
4929
4930
4931
4932
4933
4934
4935
4936
4937
4938
4939
4940
4941
4942
4943
4944
4945
4946
4947
4948
4949
4950
4951
4952
4953
4954
4955
4956
4957
4958
4959
4960
4961
4962
4963
4964
4965
4966
4967
4968
4969
4970
4971
4972
4973
4974
4975
4976
4977
4978
4979
4980
4981
4982
4983
4984
4985
4986
4987
4988
4989
4990
4991
4992
4993
4994
4995
4996
4997
4998
4999
5000
5001
5002
5003
5004
5005
5006
5007
5008
5009
5010
5011
5012
5013
5014
5015
5016
5017
5018
5019
5020
5021
5022
5023
5024
5025
5026
5027
5028
5029
5030
5031
5032
5033
5034
5035
5036
5037
5038
5039
5040
5041
5042
5043
5044
5045
5046
5047
5048
5049
5050
5051
5052
5053
5054
5055
5056
5057
5058
5059
5060
5061
5062
5063
5064
5065
5066
5067
5068
5069
5070
5071
5072
5073
5074
5075
5076
5077
5078
5079
5080
5081
5082
5083
5084
5085
5086
5087
5088
5089
5090
5091
5092
5093
5094
5095
5096
5097
5098
5099
5100
5101
5102
5103
5104
5105
5106
5107
5108
5109
5110
5111
5112
5113
5114
5115
5116
5117
5118
5119
5120
5121
5122
5123
5124
5125
5126
5127
5128
5129
5130
5131
5132
5133
5134
5135
5136
5137
5138
5139
5140
5141
5142
5143
5144
5145
5146
5147
5148
5149
5150
5151
5152
5153
5154
5155
5156
5157
5158
5159
5160
5161
5162
5163
5164
5165
5166
5167
5168
5169
5170
5171
5172
5173
5174
5175
5176
5177
5178
5179
5180
5181
5182
5183
5184
5185
5186
5187
5188
5189
5190
5191
5192
5193
5194
5195
5196
5197
5198
5199
5200
5201
5202
5203
5204
5205
5206
5207
5208
5209
5210
5211
5212
5213
5214
5215
5216
5217
5218
5219
5220
5221
5222
5223
5224
5225
5226
5227
5228
5229
5230
5231
5232
5233
5234
5235
5236
5237
5238
5239
5240
5241
5242
5243
5244
5245
5246
5247
5248
5249
5250
5251
5252
5253
5254
5255
5256
5257
5258
5259
5260
5261
5262
5263
5264
5265
5266
5267
5268
5269
5270
5271
5272
5273
5274
5275
5276
5277
5278
5279
5280
5281
5282
5283
5284
5285
5286
5287
5288
5289
5290
5291
5292
5293
5294
5295
5296
5297
5298
5299
5300
5301
5302
5303
5304
5305
5306
5307
5308
5309
5310
5311
5312
5313
5314
5315
5316
5317
5318
5319
5320
5321
5322
5323
5324
5325
5326
5327
5328
5329
5330
5331
5332
5333
5334
5335
5336
5337
5338
5339
5340
5341
5342
5343
5344
5345
5346
5347
5348
5349
5350
5351
5352
5353
5354
5355
5356
5357
5358
5359
5360
5361
5362
5363
5364
5365
5366
5367
5368
5369
5370
5371
5372
5373
5374
5375
5376
5377
5378
5379
5380
5381
5382
5383
5384
5385
5386
5387
5388
5389
5390
5391
5392
5393
5394
5395
5396
5397
5398
5399
5400
5401
5402
5403
5404
5405
5406
5407
5408
5409
5410
5411
5412
5413
5414
5415
5416
5417
5418
5419
5420
5421
5422
5423
5424
5425
5426
5427
5428
5429
5430
5431
5432
5433
5434
5435
5436
5437
5438
5439
5440
5441
5442
5443
5444
5445
5446
5447
5448
5449
5450
5451
5452
5453
5454
5455
5456
5457
5458
5459
5460
5461
5462
5463
5464
5465
5466
5467
5468
5469
5470
5471
5472
5473
5474
5475
5476
5477
5478
5479
5480
5481
5482
5483
5484
5485
5486
5487
5488
5489
5490
5491
5492
5493
5494
5495
5496
5497
5498
5499
5500
5501
5502
5503
5504
5505
5506
5507
5508
5509
5510
5511
5512
5513
5514
5515
5516
5517
5518
5519
5520
5521
5522
5523
5524
5525
5526
5527
5528
5529
5530
5531
5532
5533
5534
5535
5536
5537
5538
5539
5540
5541
5542
5543
5544
5545
5546
5547
5548
5549
5550
5551
5552
5553
5554
5555
5556
5557
5558
5559
5560
5561
5562
5563
5564
5565
5566
5567
5568
5569
5570
5571
5572
5573
5574
5575
5576
5577
5578
5579
5580
5581
5582
5583
5584
5585
5586
5587
5588
5589
5590
5591
5592
5593
5594
5595
5596
5597
5598
5599
5600
5601
5602
5603
5604
5605
5606
5607
5608
5609
5610
5611
5612
5613
5614
5615
5616
5617
5618
5619
5620
5621
5622
5623
5624
5625
5626
5627
5628
5629
5630
5631
5632
5633
5634
5635
5636
5637
5638
5639
5640
5641
5642
5643
5644
5645
5646
5647
5648
5649
5650
5651
5652
5653
5654
5655
5656
5657
5658
5659
5660
5661
5662
5663
5664
5665
5666
5667
5668
5669
5670
5671
5672
5673
5674
5675
5676
5677
5678
5679
5680
5681
5682
5683
5684
5685
5686
5687
5688
5689
5690
5691
5692
5693
5694
5695
5696
5697
5698
5699
5700
5701
5702
5703
5704
5705
5706
5707
5708
5709
5710
5711
5712
5713
5714
5715
5716
5717
5718
5719
5720
5721
5722
5723
5724
5725
5726
5727
5728
5729
5730
5731
5732
5733
5734
5735
5736
5737
5738
5739
5740
5741
5742
5743
5744
5745
5746
5747
5748
5749
5750
5751
5752
5753
5754
5755
5756
5757
5758
5759
5760
5761
5762
5763
5764
5765
5766
5767
5768
5769
5770
5771
5772
5773
5774
5775
5776
5777
5778
5779
5780
5781
5782
5783
5784
5785
5786
5787
5788
5789
5790
5791
5792
5793
5794
5795
5796
5797
5798
5799
5800
5801
5802
5803
5804
5805
5806
5807
5808
5809
5810
5811
5812
5813
5814
5815
5816
5817
5818
5819
5820
5821
5822
5823
5824
5825
5826
5827
5828
5829
5830
5831
5832
5833
5834
5835
5836
5837
5838
5839
5840
5841
5842
5843
5844
5845
5846
5847
5848
5849
5850
5851
5852
5853
5854
5855
5856
5857
5858
5859
5860
5861
5862
5863
5864
5865
5866
5867
5868
5869
5870
5871
5872
5873
5874
5875
5876
5877
5878
5879
5880
5881
5882
5883
5884
5885
5886
5887
5888
5889
5890
5891
5892
5893
5894
5895
5896
5897
5898
5899
5900
5901
5902
5903
5904
5905
5906
5907
5908
5909
5910
5911
5912
5913
5914
5915
5916
5917
5918
5919
5920
5921
5922
5923
5924
5925
5926
5927
5928
5929
5930
5931
5932
5933
5934
5935
5936
5937
5938
5939
5940
5941
5942
5943
5944
5945
5946
5947
5948
5949
5950
5951
5952
5953
5954
5955
5956
5957
5958
5959
5960
5961
5962
5963
5964
5965
5966
5967
5968
5969
5970
5971
5972
5973
5974
5975
5976
5977
5978
5979
5980
5981
5982
5983
5984
5985
5986
5987
5988
5989
5990
5991
5992
5993
5994
5995
5996
5997
5998
5999
6000
6001
6002
6003
6004
6005
6006
6007
6008
6009
6010
6011
6012
6013
6014
6015
6016
6017
6018
6019
6020
6021
6022
6023
6024
6025
6026
6027
6028
6029
6030
6031
6032
6033
6034
6035
6036
6037
6038
6039
6040
6041
6042
6043
6044
6045
6046
6047
6048
6049
6050
6051
6052
6053
6054
6055
6056
6057
6058
6059
6060
6061
6062
6063
6064
6065
6066
6067
6068
6069
6070
6071
6072
6073
6074
6075
6076
6077
6078
6079
6080
6081
6082
6083
6084
6085
6086
6087
6088
6089
6090
6091
6092
6093
6094
6095
6096
6097
6098
6099
6100
6101
6102
6103
6104
6105
6106
6107
6108
6109
6110
6111
6112
6113
6114
6115
6116
6117
6118
6119
6120
6121
6122
6123
6124
6125
6126
6127
6128
6129
6130
6131
6132
6133
6134
6135
6136
6137
6138
6139
6140
6141
6142
6143
6144
6145
6146
6147
6148
6149
6150
6151
6152
6153
6154
6155
6156
6157
6158
6159
6160
6161
6162
6163
6164
6165
6166
6167
6168
6169
6170
6171
6172
6173
6174
6175
6176
6177
6178
6179
6180
6181
6182
6183
6184
6185
6186
6187
6188
6189
6190
6191
6192
6193
6194
6195
6196
6197
6198
6199
6200
6201
6202
6203
6204
6205
6206
6207
6208
6209
6210
6211
6212
6213
6214
6215
6216
6217
6218
6219
6220
6221
6222
6223
6224
6225
6226
6227
6228
6229
6230
6231
6232
6233
6234
6235
6236
6237
6238
6239
6240
6241
6242
6243
6244
6245
6246
6247
6248
6249
6250
6251
6252
6253
6254
6255
6256
6257
6258
6259
6260
6261
6262
6263
6264
6265
6266
6267
6268
6269
6270
6271
6272
6273
6274
6275
6276
6277
6278
6279
6280
6281
6282
6283
6284
6285
6286
6287
6288
6289
6290
6291
6292
6293
6294
6295
6296
6297
6298
6299
6300
6301
6302
6303
6304
6305
6306
6307
6308
6309
6310
6311
6312
6313
6314
6315
6316
6317
6318
6319
6320
6321
6322
6323
6324
6325
6326
6327
6328
6329
6330
6331
6332
6333
6334
6335
6336
6337
6338
6339
6340
6341
6342
6343
6344
6345
6346
6347
6348
6349
6350
6351
6352
6353
6354
6355
6356
6357
6358
6359
6360
6361
6362
6363
6364
6365
6366
6367
6368
6369
6370
6371
6372
6373
6374
6375
6376
6377
6378
6379
6380
6381
6382
6383
6384
6385
6386
6387
6388
6389
6390
6391
6392
6393
6394
6395
6396
6397
6398
6399
6400
6401
6402
6403
6404
6405
6406
6407
6408
6409
6410
6411
6412
6413
6414
6415
6416
6417
6418
6419
6420
6421
6422
6423
6424
6425
6426
6427
6428
6429
6430
6431
6432
6433
6434
6435
6436
6437
6438
6439
6440
6441
6442
6443
6444
6445
6446
6447
6448
6449
6450
6451
6452
6453
6454
6455
6456
6457
6458
6459
6460
6461
6462
6463
6464
6465
6466
6467
6468
6469
6470
6471
6472
6473
6474
6475
6476
6477
6478
6479
6480
6481
6482
6483
6484
6485
6486
6487
6488
6489
6490
6491
6492
6493
6494
6495
6496
6497
6498
6499
6500
6501
6502
6503
6504
6505
6506
6507
6508
6509
6510
6511
6512
6513
6514
6515
6516
6517
6518
6519
6520
6521
6522
6523
6524
6525
6526
6527
6528
6529
6530
6531
6532
6533
6534
6535
6536
6537
6538
6539
6540
6541
6542
6543
6544
6545
6546
6547
6548
6549
6550
6551
6552
6553
6554
6555
6556
6557
6558
6559
6560
6561
6562
6563
6564
6565
6566
6567
6568
6569
6570
6571
6572
6573
6574
6575
6576
6577
6578
6579
6580
6581
6582
6583
6584
6585
6586
6587
6588
6589
6590
6591
6592
6593
6594
6595
6596
6597
6598
6599
6600
6601
6602
6603
6604
6605
6606
6607
6608
6609
6610
6611
6612
6613
6614
6615
6616
6617
6618
6619
6620
6621
6622
6623
6624
6625
6626
6627
6628
6629
6630
6631
6632
6633
6634
6635
6636
6637
6638
6639
6640
6641
6642
6643
6644
6645
6646
6647
6648
6649
6650
6651
6652
6653
6654
6655
6656
6657
6658
6659
6660
6661
6662
6663
6664
6665
6666
6667
6668
6669
6670
6671
6672
6673
6674
6675
6676
6677
6678
6679
6680
6681
6682
6683
6684
6685
6686
6687
6688
6689
6690
6691
6692
6693
6694
6695
6696
6697
6698
6699
6700
6701
6702
6703
6704
6705
6706
6707
6708
6709
6710
6711
6712
6713
6714
6715
6716
6717
6718
6719
6720
6721
6722
6723
6724
6725
6726
6727
6728
6729
6730
6731
6732
6733
6734
6735
6736
6737
6738
6739
6740
6741
6742
6743
6744
6745
6746
6747
6748
6749
6750
6751
6752
6753
6754
6755
6756
6757
6758
6759
6760
6761
6762
6763
6764
6765
6766
6767
6768
6769
6770
6771
6772
6773
6774
6775
6776
6777
6778
6779
6780
6781
6782
6783
6784
6785
6786
6787
6788
6789
6790
6791
6792
6793
6794
6795
6796
6797
6798
6799
6800
6801
6802
6803
6804
6805
6806
6807
6808
6809
6810
6811
6812
6813
6814
6815
6816
6817
6818
6819
6820
6821
6822
6823
6824
6825
6826
6827
6828
6829
6830
6831
6832
6833
6834
6835
6836
6837
6838
6839
6840
6841
6842
6843
6844
6845
6846
6847
6848
6849
6850
6851
6852
6853
6854
6855
6856
6857
6858
6859
6860
6861
6862
6863
6864
6865
6866
6867
6868
6869
6870
6871
6872
6873
6874
6875
6876
6877
6878
6879
6880
6881
6882
6883
6884
6885
6886
6887
6888
6889
6890
6891
6892
6893
6894
6895
6896
6897
6898
6899
6900
6901
6902
6903
6904
6905
6906
6907
6908
6909
6910
6911
6912
6913
6914
6915
6916
6917
6918
6919
6920
6921
6922
6923
6924
6925
6926
6927
6928
6929
6930
6931
6932
6933
6934
6935
6936
6937
6938
6939
6940
6941
6942
6943
6944
6945
6946
6947
6948
6949
6950
6951
6952
6953
6954
6955
6956
6957
6958
6959
6960
6961
6962
6963
6964
6965
6966
6967
6968
6969
6970
6971
6972
6973
6974
6975
6976
6977
6978
6979
6980
6981
6982
6983
6984
6985
6986
6987
6988
6989
6990
6991
6992
6993
6994
6995
6996
6997
6998
6999
7000
7001
7002
7003
7004
7005
7006
7007
7008
7009
7010
7011
7012
7013
7014
7015
7016
7017
7018
7019
7020
7021
7022
7023
7024
7025
7026
7027
7028
7029
7030
7031
7032
7033
7034
7035
7036
7037
7038
7039
7040
7041
7042
7043
7044
7045
7046
7047
7048
7049
7050
7051
7052
7053
7054
7055
7056
7057
7058
7059
7060
7061
7062
7063
7064
7065
7066
7067
7068
7069
7070
7071
7072
7073
7074
7075
7076
7077
7078
7079
7080
7081
7082
7083
7084
7085
7086
7087
7088
7089
7090
7091
7092
7093
7094
7095
7096
7097
7098
7099
7100
7101
7102
7103
7104
7105
7106
7107
7108
7109
7110
7111
7112
7113
7114
7115
7116
7117
7118
7119
7120
7121
7122
7123
7124
7125
7126
7127
7128
7129
7130
7131
7132
7133
7134
7135
7136
7137
7138
7139
7140
7141
7142
7143
7144
7145
7146
7147
7148
7149
7150
7151
7152
7153
7154
7155
7156
7157
7158
7159
7160
7161
7162
7163
7164
7165
7166
7167
7168
7169
7170
7171
7172
7173
7174
7175
7176
7177
7178
7179
7180
7181
7182
7183
7184
7185
7186
7187
7188
7189
7190
7191
7192
7193
7194
7195
7196
7197
7198
7199
7200
7201
7202
7203
7204
7205
7206
7207
7208
7209
7210
7211
7212
7213
7214
7215
7216
7217
7218
7219
7220
7221
7222
7223
7224
7225
7226
7227
7228
7229
7230
7231
7232
7233
7234
7235
7236
7237
7238
7239
7240
7241
7242
7243
7244
7245
7246
7247
7248
7249
7250
7251
7252
7253
7254
7255
7256
7257
7258
7259
7260
7261
7262
7263
7264
7265
7266
7267
7268
7269
7270
7271
7272
7273
7274
7275
7276
7277
7278
7279
7280
7281
7282
7283
7284
7285
7286
7287
7288
7289
7290
7291
7292
7293
7294
7295
7296
7297
7298
7299
7300
7301
7302
7303
7304
7305
7306
7307
7308
7309
7310
7311
7312
7313
7314
7315
7316
7317
7318
7319
7320
7321
7322
7323
7324
7325
7326
7327
7328
7329
7330
7331
7332
7333
7334
7335
7336
7337
7338
7339
7340
7341
7342
7343
7344
7345
7346
7347
7348
7349
7350
7351
7352
7353
7354
7355
7356
7357
7358
7359
7360
7361
7362
7363
7364
7365
7366
7367
7368
7369
7370
7371
7372
7373
7374
7375
7376
7377
7378
7379
7380
7381
7382
7383
7384
7385
7386
7387
7388
7389
7390
7391
7392
7393
7394
7395
7396
7397
7398
7399
7400
7401
7402
7403
7404
7405
7406
7407
7408
7409
7410
7411
7412
7413
7414
7415
7416
7417
7418
7419
7420
7421
7422
7423
7424
7425
7426
7427
7428
7429
7430
7431
7432
7433
7434
7435
7436
7437
7438
7439
7440
7441
7442
7443
7444
7445
7446
7447
7448
7449
7450
7451
7452
7453
7454
7455
7456
7457
7458
7459
7460
7461
7462
7463
7464
7465
7466
7467
7468
7469
7470
7471
7472
7473
7474
7475
7476
7477
7478
7479
7480
7481
7482
7483
7484
7485
7486
7487
7488
7489
7490
7491
7492
7493
7494
7495
7496
7497
7498
7499
7500
7501
7502
7503
7504
7505
7506
7507
7508
7509
7510
7511
7512
7513
7514
7515
7516
7517
7518
7519
7520
7521
7522
7523
7524
7525
7526
7527
7528
7529
7530
7531
7532
7533
7534
7535
7536
7537
7538
7539
7540
7541
7542
7543
7544
7545
7546
7547
7548
7549
7550
7551
7552
7553
7554
7555
7556
7557
7558
7559
7560
7561
7562
7563
7564
7565
7566
7567
7568
7569
7570
7571
7572
7573
7574
7575
7576
7577
7578
7579
7580
7581
7582
7583
7584
7585
7586
7587
7588
7589
7590
7591
7592
7593
7594
7595
7596
7597
7598
7599
7600
7601
7602
7603
7604
7605
7606
7607
7608
7609
7610
7611
7612
7613
7614
7615
7616
7617
7618
7619
7620
7621
7622
7623
7624
7625
7626
7627
7628
7629
7630
7631
7632
7633
7634
7635
7636
7637
7638
7639
7640
7641
7642
7643
7644
7645
7646
7647
7648
7649
7650
7651
7652
7653
7654
7655
7656
7657
7658
7659
7660
7661
7662
7663
7664
7665
7666
7667
7668
7669
7670
7671
7672
7673
7674
7675
7676
7677
7678
7679
7680
7681
7682
7683
7684
7685
7686
7687
7688
7689
7690
7691
7692
7693
7694
7695
7696
7697
7698
7699
7700
7701
7702
7703
7704
7705
7706
7707
7708
7709
7710
7711
7712
7713
7714
7715
7716
7717
7718
7719
7720
7721
7722
7723
7724
7725
7726
7727
7728
7729
7730
7731
7732
7733
7734
7735
7736
7737
7738
7739
7740
7741
7742
7743
7744
7745
7746
7747
7748
7749
7750
7751
7752
7753
7754
7755
7756
7757
7758
7759
7760
7761
7762
7763
7764
7765
7766
7767
7768
7769
7770
7771
7772
7773
7774
7775
7776
7777
7778
7779
7780
7781
7782
7783
7784
7785
7786
7787
7788
7789
7790
7791
7792
7793
7794
7795
7796
7797
7798
7799
7800
7801
7802
7803
7804
7805
7806
7807
7808
7809
7810
7811
7812
7813
7814
7815
7816
7817
7818
7819
7820
7821
7822
7823
7824
7825
7826
7827
7828
7829
7830
7831
7832
7833
7834
7835
7836
7837
7838
7839
7840
7841
7842
7843
7844
7845
7846
7847
7848
7849
7850
7851
7852
7853
7854
7855
7856
7857
7858
7859
7860
7861
7862
7863
7864
7865
7866
7867
7868
7869
7870
7871
7872
7873
7874
7875
7876
7877
7878
7879
7880
7881
7882
7883
7884
7885
7886
7887
7888
7889
7890
7891
7892
7893
7894
7895
7896
7897
7898
7899
7900
7901
7902
7903
7904
7905
7906
7907
7908
7909
7910
7911
7912
7913
7914
7915
7916
7917
7918
7919
7920
7921
7922
7923
7924
7925
7926
7927
7928
7929
7930
7931
7932
7933
7934
7935
7936
7937
7938
7939
7940
7941
7942
7943
7944
7945
7946
7947
7948
7949
7950
7951
7952
7953
7954
7955
7956
7957
7958
7959
7960
7961
7962
7963
7964
7965
7966
7967
7968
7969
7970
7971
7972
7973
7974
7975
7976
7977
7978
7979
7980
7981
7982
7983
7984
7985
7986
7987
7988
7989
7990
7991
7992
7993
7994
7995
7996
7997
7998
7999
8000
8001
8002
8003
8004
8005
8006
8007
8008
8009
8010
8011
8012
8013
8014
8015
8016
8017
8018
8019
8020
8021
8022
8023
8024
8025
8026
8027
8028
8029
8030
8031
8032
8033
8034
8035
8036
8037
8038
8039
8040
8041
8042
8043
8044
8045
8046
8047
8048
8049
8050
8051
8052
8053
8054
8055
8056
8057
8058
8059
8060
8061
8062
8063
8064
8065
8066
8067
8068
8069
8070
8071
8072
8073
8074
8075
8076
8077
8078
8079
8080
8081
8082
8083
8084
8085
8086
8087
8088
8089
8090
8091
8092
8093
8094
8095
8096
8097
8098
8099
8100
8101
8102
8103
8104
8105
8106
8107
8108
8109
8110
8111
8112
8113
8114
8115
8116
8117
8118
8119
8120
8121
8122
8123
8124
8125
8126
8127
8128
8129
8130
8131
8132
8133
8134
8135
8136
8137
8138
8139
8140
8141
8142
8143
8144
8145
8146
8147
8148
8149
8150
8151
8152
8153
8154
8155
8156
8157
8158
8159
8160
8161
8162
8163
8164
8165
8166
8167
8168
8169
8170
8171
8172
8173
8174
8175
8176
8177
8178
8179
8180
8181
8182
8183
8184
8185
8186
8187
8188
8189
8190
8191
8192
8193
8194
8195
8196
8197
8198
8199
8200
8201
8202
8203
8204
8205
8206
8207
8208
8209
8210
8211
8212
8213
8214
8215
8216
8217
8218
8219
8220
8221
8222
8223
8224
8225
8226
8227
8228
8229
8230
8231
8232
8233
8234
8235
8236
8237
8238
8239
8240
8241
8242
8243
8244
8245
8246
8247
8248
8249
8250
8251
8252
8253
8254
8255
8256
8257
8258
8259
8260
8261
8262
8263
8264
8265
8266
8267
8268
8269
8270
8271
8272
8273
8274
8275
8276
8277
8278
8279
8280
8281
8282
8283
8284
8285
8286
8287
8288
8289
8290
8291
8292
8293
8294
8295
8296
8297
8298
8299
8300
8301
8302
8303
8304
8305
8306
8307
8308
8309
8310
8311
8312
8313
8314
8315
8316
8317
8318
8319
8320
8321
8322
8323
8324
8325
8326
8327
8328
8329
8330
8331
8332
8333
8334
8335
8336
8337
8338
8339
8340
8341
8342
8343
8344
8345
8346
8347
8348
8349
8350
8351
8352
8353
8354
8355
8356
8357
8358
8359
8360
8361
8362
8363
8364
8365
8366
8367
8368
8369
8370
8371
8372
8373
8374
8375
8376
8377
8378
8379
8380
8381
8382
8383
8384
8385
8386
8387
8388
8389
8390
8391
8392
8393
8394
8395
8396
8397
8398
8399
8400
8401
8402
8403
8404
8405
8406
8407
8408
8409
8410
8411
8412
8413
8414
8415
8416
8417
8418
8419
8420
8421
8422
8423
8424
8425
8426
8427
8428
8429
8430
8431
8432
8433
8434
8435
8436
8437
8438
8439
8440
8441
8442
8443
8444
8445
8446
8447
8448
8449
8450
8451
8452
8453
8454
8455
8456
8457
8458
8459
8460
8461
8462
8463
8464
8465
8466
8467
8468
8469
8470
8471
8472
8473
8474
8475
8476
8477
8478
8479
8480
8481
8482
8483
8484
8485
8486
8487
8488
8489
8490
8491
8492
8493
8494
8495
8496
8497
8498
8499
8500
8501
8502
8503
8504
8505
8506
8507
8508
8509
8510
8511
8512
8513
8514
8515
8516
8517
8518
8519
8520
8521
8522
8523
8524
8525
8526
8527
8528
8529
8530
8531
8532
8533
8534
8535
8536
8537
8538
8539
8540
8541
8542
8543
8544
8545
8546
8547
8548
8549
8550
8551
8552
8553
8554
8555
8556
8557
8558
8559
8560
8561
8562
8563
8564
8565
8566
8567
8568
8569
8570
8571
8572
8573
8574
8575
8576
8577
8578
8579
8580
8581
8582
8583
8584
8585
8586
8587
8588
8589
8590
8591
8592
8593
8594
8595
8596
8597
8598
8599
8600
8601
8602
8603
8604
8605
8606
8607
8608
8609
8610
8611
8612
8613
8614
8615
8616
8617
8618
8619
8620
8621
8622
8623
8624
8625
8626
8627
8628
8629
8630
8631
8632
8633
8634
8635
8636
8637
8638
8639
8640
8641
8642
8643
8644
8645
8646
8647
8648
8649
8650
8651
8652
8653
8654
8655
8656
8657
8658
8659
8660
8661
8662
8663
8664
8665
8666
8667
8668
8669
8670
8671
8672
8673
8674
8675
8676
8677
8678
8679
8680
8681
8682
8683
8684
8685
8686
8687
8688
8689
8690
8691
8692
8693
8694
8695
8696
8697
8698
8699
8700
8701
8702
8703
8704
8705
8706
8707
8708
8709
8710
8711
8712
8713
8714
8715
8716
8717
8718
8719
8720
8721
8722
8723
8724
8725
8726
8727
8728
8729
8730
8731
8732
8733
8734
8735
8736
8737
8738
8739
8740
8741
8742
8743
8744
8745
8746
8747
8748
8749
8750
8751
8752
8753
8754
8755
8756
8757
8758
8759
8760
8761
8762
8763
8764
8765
8766
8767
8768
8769
8770
8771
8772
8773
8774
8775
8776
8777
8778
8779
8780
8781
8782
8783
8784
8785
8786
8787
8788
8789
8790
8791
8792
8793
8794
8795
8796
8797
8798
8799
8800
8801
8802
8803
8804
8805
8806
8807
8808
8809
8810
8811
8812
8813
8814
8815
8816
8817
8818
8819
8820
8821
8822
8823
8824
8825
8826
8827
8828
8829
8830
8831
8832
8833
8834
8835
8836
8837
8838
8839
8840
8841
8842
8843
8844
8845
8846
8847
8848
8849
8850
8851
8852
8853
8854
8855
8856
8857
8858
8859
8860
8861
8862
8863
8864
8865
8866
8867
8868
8869
8870
8871
8872
8873
8874
8875
8876
8877
8878
8879
8880
8881
8882
8883
8884
8885
8886
8887
8888
8889
8890
8891
8892
8893
8894
8895
8896
8897
8898
8899
8900
8901
8902
8903
8904
8905
8906
8907
8908
8909
8910
8911
8912
8913
8914
8915
8916
8917
8918
8919
8920
8921
8922
8923
8924
8925
8926
8927
8928
8929
8930
8931
8932
8933
8934
8935
8936
8937
8938
8939
8940
8941
8942
8943
8944
8945
8946
8947
8948
8949
8950
8951
8952
8953
8954
8955
8956
8957
8958
8959
8960
8961
8962
8963
8964
8965
8966
8967
8968
8969
8970
8971
8972
8973
8974
8975
8976
8977
8978
8979
8980
8981
8982
8983
8984
8985
8986
8987
8988
8989
8990
8991
8992
8993
8994
8995
8996
8997
8998
8999
9000
9001
9002
9003
9004
9005
9006
9007
9008
9009
9010
9011
9012
9013
9014
9015
9016
9017
9018
9019
9020
9021
9022
9023
9024
9025
9026
9027
9028
9029
9030
9031
9032
9033
9034
9035
9036
9037
9038
9039
9040
9041
9042
9043
9044
9045
9046
9047
9048
9049
9050
9051
9052
9053
9054
9055
9056
9057
9058
9059
9060
9061
9062
9063
9064
9065
9066
9067
9068
9069
9070
9071
9072
9073
9074
9075
9076
9077
9078
9079
9080
9081
9082
9083
9084
9085
9086
9087
9088
9089
9090
9091
9092
9093
9094
9095
9096
9097
9098
9099
9100
9101
9102
9103
9104
9105
9106
9107
9108
9109
9110
9111
9112
9113
9114
9115
9116
9117
9118
9119
9120
9121
9122
9123
9124
9125
9126
9127
9128
9129
9130
9131
9132
9133
9134
9135
9136
9137
9138
9139
9140
9141
9142
9143
9144
9145
9146
9147
9148
9149
9150
9151
9152
9153
9154
9155
9156
9157
9158
9159
9160
9161
9162
9163
9164
9165
9166
9167
9168
9169
9170
9171
9172
9173
9174
9175
9176
9177
9178
9179
9180
9181
9182
9183
9184
9185
9186
9187
9188
9189
9190
9191
9192
9193
9194
9195
9196
9197
9198
9199
9200
9201
9202
9203
9204
9205
9206
9207
9208
9209
9210
9211
9212
9213
9214
9215
9216
9217
9218
9219
9220
9221
9222
9223
9224
9225
9226
9227
9228
9229
9230
9231
9232
9233
9234
9235
9236
9237
9238
9239
9240
9241
9242
9243
9244
9245
9246
9247
9248
9249
9250
9251
9252
9253
9254
9255
9256
9257
9258
9259
9260
9261
9262
9263
9264
9265
9266
9267
9268
9269
9270
9271
9272
9273
9274
9275
9276
9277
9278
9279
9280
9281
9282
9283
9284
9285
9286
9287
9288
9289
9290
9291
9292
9293
9294
9295
9296
9297
9298
9299
9300
9301
9302
9303
9304
9305
9306
9307
9308
9309
9310
9311
9312
9313
9314
9315
9316
9317
9318
9319
9320
9321
9322
9323
9324
9325
9326
9327
9328
9329
9330
9331
9332
9333
9334
9335
9336
9337
9338
9339
9340
9341
9342
9343
9344
9345
9346
9347
9348
9349
9350
9351
9352
9353
9354
9355
9356
9357
9358
9359
9360
9361
9362
9363
9364
9365
9366
9367
9368
9369
9370
9371
9372
9373
9374
9375
9376
9377
9378
9379
9380
9381
9382
9383
9384
9385
9386
9387
9388
9389
9390
9391
9392
9393
9394
9395
9396
9397
9398
9399
9400
9401
9402
9403
9404
9405
9406
9407
9408
9409
9410
9411
9412
9413
9414
9415
9416
9417
9418
9419
9420
9421
9422
9423
9424
9425
9426
9427
9428
9429
9430
9431
9432
9433
9434
9435
9436
9437
9438
9439
9440
9441
9442
9443
9444
9445
9446
9447
9448
9449
9450
9451
9452
9453
9454
9455
9456
9457
9458
9459
9460
9461
9462
9463
9464
9465
9466
9467
9468
9469
9470
9471
9472
9473
9474
9475
9476
9477
9478
9479
9480
9481
9482
9483
9484
9485
9486
9487
9488
9489
9490
9491
9492
9493
9494
9495
9496
9497
9498
9499
9500
9501
9502
9503
9504
9505
9506
9507
9508
9509
9510
9511
9512
9513
9514
9515
9516
9517
9518
9519
9520
9521
9522
9523
9524
9525
9526
9527
9528
9529
9530
9531
9532
9533
9534
9535
9536
9537
9538
9539
9540
9541
9542
9543
9544
9545
9546
9547
9548
9549
9550
9551
9552
9553
9554
9555
9556
9557
9558
9559
9560
9561
9562
9563
9564
9565
9566
9567
9568
9569
9570
9571
9572
9573
9574
9575
9576
9577
9578
9579
9580
9581
9582
9583
9584
9585
9586
9587
9588
9589
9590
9591
9592
9593
9594
9595
9596
9597
9598
9599
9600
9601
9602
9603
9604
9605
9606
9607
9608
9609
9610
9611
9612
9613
9614
9615
9616
9617
9618
9619
9620
9621
9622
9623
9624
9625
9626
9627
9628
9629
9630
9631
9632
9633
9634
9635
9636
9637
9638
9639
9640
9641
9642
9643
9644
9645
9646
9647
9648
9649
9650
9651
9652
9653
9654
9655
9656
9657
9658
9659
9660
9661
9662
9663
9664
9665
9666
9667
9668
9669
9670
9671
9672
9673
9674
9675
9676
9677
9678
9679
9680
9681
9682
9683
9684
9685
9686
9687
9688
9689
9690
9691
9692
9693
9694
9695
9696
9697
9698
9699
9700
9701
9702
9703
9704
9705
9706
9707
9708
9709
9710
9711
9712
9713
9714
9715
9716
9717
9718
9719
9720
9721
9722
9723
9724
9725
9726
9727
9728
9729
9730
9731
9732
9733
9734
9735
9736
9737
9738
9739
9740
9741
9742
9743
9744
9745
9746
9747
9748
9749
9750
9751
9752
9753
9754
9755
9756
9757
9758
9759
9760
9761
9762
9763
9764
9765
9766
9767
9768
9769
9770
9771
9772
9773
9774
9775
9776
9777
9778
9779
9780
9781
9782
9783
9784
9785
9786
9787
9788
9789
9790
9791
9792
9793
9794
9795
9796
9797
9798
9799
9800
9801
9802
9803
9804
9805
9806
9807
9808
9809
9810
9811
9812
9813
9814
9815
9816
9817
9818
9819
9820
9821
9822
9823
9824
9825
9826
9827
9828
9829
9830
9831
9832
9833
9834
9835
9836
9837
9838
9839
9840
9841
9842
9843
9844
9845
9846
9847
9848
9849
9850
9851
9852
9853
9854
9855
9856
9857
9858
9859
9860
9861
9862
9863
9864
9865
9866
9867
9868
9869
9870
9871
9872
9873
9874
9875
9876
9877
9878
9879
9880
9881
9882
9883
9884
9885
9886
9887
9888
9889
9890
9891
9892
9893
9894
9895
9896
9897
9898
9899
9900
9901
9902
9903
9904
9905
9906
9907
9908
9909
9910
9911
9912
9913
9914
9915
9916
9917
9918
9919
9920
9921
9922
9923
9924
9925
9926
9927
9928
9929
9930
9931
9932
9933
9934
9935
9936
9937
9938
9939
9940
9941
9942
9943
9944
9945
9946
9947
9948
9949
9950
9951
9952
9953
9954
9955
9956
9957
9958
9959
9960
9961
9962
9963
9964
9965
9966
9967
9968
9969
9970
9971
9972
9973
9974
9975
9976
9977
9978
9979
9980
9981
9982
9983
9984
9985
9986
9987
9988
9989
9990
9991
9992
9993
9994
9995
9996
9997
9998
9999
10000
10001
10002
10003
10004
10005
10006
10007
10008
10009
10010
10011
10012
10013
10014
10015
10016
10017
10018
10019
10020
10021
10022
10023
10024
10025
10026
10027
10028
10029
10030
10031
10032
10033
10034
10035
10036
10037
10038
10039
10040
10041
10042
10043
10044
10045
10046
10047
10048
10049
10050
10051
10052
10053
10054
10055
10056
10057
10058
10059
10060
10061
10062
10063
10064
10065
10066
10067
10068
10069
10070
10071
10072
10073
10074
10075
10076
10077
10078
10079
10080
10081
10082
10083
10084
10085
10086
10087
10088
10089
10090
10091
10092
10093
10094
10095
10096
10097
10098
10099
10100
10101
10102
10103
10104
10105
10106
10107
10108
10109
10110
10111
10112
10113
10114
10115
10116
10117
10118
10119
10120
10121
10122
10123
10124
10125
10126
10127
10128
10129
10130
10131
10132
10133
10134
10135
10136
10137
10138
10139
10140
10141
10142
10143
10144
10145
10146
10147
10148
10149
10150
10151
10152
10153
10154
10155
10156
10157
10158
10159
10160
10161
10162
10163
10164
10165
10166
10167
10168
10169
10170
10171
10172
10173
10174
10175
10176
10177
10178
10179
10180
10181
10182
10183
10184
10185
10186
10187
10188
10189
10190
10191
10192
10193
10194
10195
10196
10197
10198
10199
10200
10201
10202
10203
10204
10205
10206
10207
10208
10209
10210
10211
10212
10213
10214
10215
10216
10217
10218
10219
10220
10221
10222
10223
10224
10225
10226
10227
10228
10229
10230
10231
10232
10233
10234
10235
10236
10237
10238
10239
10240
10241
10242
10243
10244
10245
10246
10247
10248
10249
10250
10251
10252
10253
10254
10255
10256
10257
10258
10259
10260
10261
10262
10263
10264
10265
10266
10267
10268
10269
10270
10271
10272
10273
10274
10275
10276
10277
10278
10279
10280
10281
10282
10283
10284
10285
10286
10287
10288
10289
10290
10291
10292
10293
10294
10295
10296
10297
10298
10299
10300
10301
10302
10303
10304
10305
10306
10307
10308
10309
10310
10311
10312
10313
10314
10315
10316
10317
10318
10319
10320
10321
10322
10323
10324
10325
10326
10327
10328
10329
10330
10331
10332
10333
10334
10335
10336
10337
10338
10339
10340
10341
10342
10343
10344
10345
10346
10347
10348
10349
10350
10351
10352
10353
10354
10355
10356
10357
10358
10359
10360
10361
10362
10363
10364
10365
10366
10367
10368
10369
10370
10371
10372
10373
10374
10375
10376
10377
10378
10379
10380
10381
10382
10383
10384
10385
10386
10387
10388
10389
10390
10391
10392
10393
10394
10395
10396
10397
10398
10399
10400
10401
10402
10403
10404
10405
10406
10407
10408
10409
10410
10411
10412
10413
10414
10415
10416
10417
10418
10419
10420
10421
10422
10423
10424
10425
10426
10427
10428
10429
10430
10431
10432
10433
10434
10435
10436
10437
10438
10439
10440
10441
10442
10443
10444
10445
10446
10447
10448
10449
10450
10451
10452
10453
10454
10455
10456
10457
10458
10459
10460
10461
10462
10463
10464
10465
10466
10467
10468
10469
10470
10471
10472
10473
10474
10475
10476
10477
10478
10479
10480
10481
10482
10483
10484
10485
10486
10487
10488
10489
10490
10491
10492
10493
10494
10495
10496
10497
10498
10499
10500
10501
10502
10503
10504
10505
10506
10507
10508
10509
10510
10511
10512
10513
10514
10515
10516
10517
10518
10519
10520
10521
10522
10523
10524
10525
10526
10527
10528
10529
10530
10531
10532
10533
10534
10535
10536
10537
10538
10539
10540
10541
10542
10543
10544
10545
10546
10547
10548
10549
10550
10551
10552
10553
10554
10555
10556
10557
10558
10559
10560
10561
10562
10563
10564
10565
10566
10567
10568
10569
10570
10571
10572
10573
10574
10575
10576
10577
10578
10579
10580
10581
10582
10583
10584
10585
10586
10587
10588
10589
10590
10591
10592
10593
10594
10595
10596
10597
10598
10599
10600
10601
10602
10603
10604
10605
10606
10607
10608
10609
10610
10611
10612
10613
10614
10615
10616
10617
10618
10619
10620
10621
10622
10623
10624
10625
10626
10627
10628
10629
10630
10631
10632
10633
10634
10635
10636
10637
10638
10639
10640
10641
10642
10643
10644
10645
10646
10647
10648
10649
10650
10651
10652
10653
10654
10655
10656
10657
10658
10659
10660
10661
10662
10663
10664
10665
10666
10667
10668
10669
10670
10671
10672
10673
10674
10675
10676
10677
10678
10679
10680
10681
10682
10683
10684
10685
10686
10687
10688
10689
10690
10691
10692
10693
10694
10695
10696
10697
10698
10699
10700
10701
10702
10703
10704
10705
10706
10707
10708
10709
10710
10711
10712
10713
10714
10715
10716
10717
10718
10719
10720
10721
10722
10723
10724
10725
10726
10727
10728
10729
10730
10731
10732
10733
10734
10735
10736
10737
10738
10739
10740
10741
10742
10743
10744
10745
10746
10747
10748
10749
10750
10751
10752
10753
10754
10755
10756
10757
10758
10759
10760
10761
10762
10763
10764
10765
10766
10767
10768
10769
10770
10771
10772
10773
10774
10775
10776
10777
10778
10779
10780
10781
10782
10783
10784
10785
10786
10787
10788
10789
10790
10791
10792
10793
10794
10795
10796
10797
10798
10799
10800
10801
10802
10803
10804
10805
10806
10807
10808
10809
10810
10811
10812
10813
10814
10815
10816
10817
10818
10819
10820
10821
10822
10823
10824
10825
10826
10827
10828
10829
10830
10831
10832
10833
10834
10835
10836
10837
10838
10839
10840
10841
10842
10843
10844
10845
10846
10847
10848
10849
10850
10851
10852
10853
10854
10855
10856
10857
10858
10859
10860
10861
10862
10863
10864
10865
10866
10867
10868
10869
10870
10871
10872
10873
10874
10875
10876
10877
10878
10879
10880
10881
10882
10883
10884
10885
10886
10887
10888
10889
10890
10891
10892
10893
10894
10895
10896
10897
10898
10899
10900
10901
10902
10903
10904
10905
10906
10907
10908
10909
10910
10911
10912
10913
10914
10915
10916
10917
10918
10919
10920
10921
10922
10923
10924
10925
10926
10927
10928
10929
10930
10931
10932
10933
10934
10935
10936
10937
10938
10939
10940
10941
10942
10943
10944
10945
10946
10947
10948
10949
10950
10951
10952
10953
10954
10955
10956
10957
10958
10959
10960
10961
10962
10963
10964
10965
10966
10967
10968
10969
10970
10971
10972
10973
10974
10975
10976
10977
10978
10979
10980
10981
10982
10983
10984
10985
10986
10987
10988
10989
10990
10991
10992
10993
10994
10995
10996
10997
10998
10999
11000
11001
11002
11003
11004
11005
11006
11007
11008
11009
11010
11011
11012
11013
11014
11015
11016
11017
11018
11019
11020
11021
11022
11023
11024
11025
11026
11027
11028
11029
11030
11031
11032
11033
11034
11035
11036
11037
11038
11039
11040
11041
11042
11043
11044
11045
11046
11047
11048
11049
11050
11051
11052
11053
11054
11055
11056
11057
11058
11059
11060
11061
11062
11063
11064
11065
11066
11067
11068
11069
11070
11071
11072
11073
11074
11075
11076
11077
11078
11079
11080
11081
11082
11083
11084
11085
11086
11087
11088
11089
11090
11091
11092
11093
11094
11095
11096
11097
11098
11099
11100
11101
11102
11103
11104
11105
11106
11107
11108
11109
11110
11111
11112
11113
11114
11115
11116
11117
11118
11119
11120
11121
11122
11123
11124
11125
11126
11127
11128
11129
11130
11131
11132
11133
11134
11135
11136
11137
11138
11139
11140
11141
11142
11143
11144
11145
11146
11147
11148
11149
11150
11151
11152
11153
11154
11155
11156
11157
11158
11159
11160
11161
11162
11163
11164
11165
11166
11167
11168
11169
11170
11171
11172
11173
11174
11175
11176
11177
11178
11179
11180
11181
11182
11183
11184
11185
11186
11187
11188
11189
11190
11191
11192
11193
11194
11195
11196
11197
11198
11199
11200
11201
11202
11203
11204
11205
11206
11207
11208
11209
11210
11211
11212
11213
11214
11215
11216
11217
11218
11219
11220
11221
11222
11223
11224
11225
11226
11227
11228
11229
11230
11231
11232
11233
11234
11235
11236
11237
11238
11239
11240
11241
11242
11243
11244
11245
11246
11247
11248
11249
11250
11251
11252
11253
11254
11255
11256
11257
11258
11259
11260
11261
11262
11263
11264
11265
11266
11267
11268
11269
11270
11271
11272
11273
11274
11275
11276
11277
11278
11279
11280
11281
11282
11283
11284
11285
11286
11287
11288
11289
11290
11291
11292
11293
11294
11295
11296
11297
11298
11299
11300
11301
11302
11303
11304
11305
11306
11307
11308
11309
11310
11311
11312
11313
11314
11315
11316
11317
11318
11319
11320
11321
11322
11323
11324
11325
11326
11327
11328
11329
11330
11331
11332
11333
11334
11335
11336
11337
11338
11339
11340
11341
11342
11343
11344
11345
11346
11347
11348
11349
11350
11351
11352
11353
11354
11355
11356
11357
11358
11359
11360
11361
11362
11363
11364
11365
11366
11367
11368
11369
11370
11371
11372
11373
11374
11375
11376
11377
11378
11379
11380
11381
11382
11383
11384
11385
11386
11387
11388
11389
11390
11391
11392
11393
11394
11395
11396
11397
11398
11399
11400
11401
11402
11403
11404
11405
11406
11407
11408
11409
11410
11411
11412
11413
11414
11415
11416
11417
11418
11419
11420
11421
11422
11423
11424
11425
11426
11427
11428
11429
11430
11431
11432
11433
11434
11435
11436
11437
11438
11439
11440
11441
11442
11443
11444
11445
11446
11447
11448
11449
11450
11451
11452
11453
11454
11455
11456
11457
11458
11459
11460
11461
11462
11463
11464
11465
11466
11467
11468
11469
11470
11471
11472
11473
11474
11475
11476
11477
11478
11479
11480
11481
11482
11483
11484
11485
11486
11487
11488
11489
11490
11491
11492
11493
11494
11495
11496
11497
11498
11499
11500
11501
11502
11503
11504
11505
11506
11507
11508
11509
11510
11511
11512
11513
11514
11515
11516
11517
11518
11519
11520
11521
11522
11523
11524
11525
11526
11527
11528
11529
11530
11531
11532
11533
11534
11535
11536
11537
11538
11539
11540
11541
11542
11543
11544
11545
11546
11547
11548
11549
11550
11551
11552
11553
11554
11555
11556
11557
11558
11559
11560
11561
11562
11563
11564
11565
11566
11567
11568
11569
11570
11571
11572
11573
11574
11575
11576
11577
11578
11579
11580
11581
11582
11583
11584
11585
11586
11587
11588
11589
11590
11591
11592
11593
11594
11595
11596
11597
11598
11599
11600
11601
11602
11603
11604
11605
11606
11607
11608
11609
11610
11611
11612
11613
11614
11615
11616
11617
11618
11619
11620
11621
11622
11623
11624
11625
11626
11627
11628
11629
11630
11631
11632
11633
11634
11635
11636
11637
11638
11639
11640
11641
11642
11643
11644
11645
11646
11647
11648
11649
11650
11651
11652
11653
11654
11655
11656
11657
11658
11659
11660
11661
11662
11663
11664
11665
11666
11667
11668
11669
11670
11671
11672
11673
11674
11675
11676
11677
11678
11679
11680
11681
11682
11683
11684
11685
11686
11687
11688
11689
11690
11691
11692
11693
11694
11695
11696
11697
11698
11699
11700
11701
11702
11703
11704
11705
11706
11707
11708
11709
11710
11711
11712
11713
11714
11715
11716
11717
11718
11719
11720
11721
11722
11723
11724
11725
11726
11727
11728
11729
11730
11731
11732
11733
11734
11735
11736
11737
11738
11739
11740
11741
11742
11743
11744
11745
11746
11747
11748
11749
11750
11751
11752
11753
11754
11755
11756
11757
11758
11759
11760
11761
11762
11763
11764
11765
11766
11767
11768
11769
11770
11771
11772
11773
11774
11775
11776
11777
11778
11779
11780
11781
11782
11783
11784
11785
11786
11787
11788
11789
11790
11791
11792
11793
11794
11795
11796
11797
11798
11799
11800
11801
11802
11803
11804
11805
11806
11807
11808
11809
11810
11811
11812
11813
11814
11815
11816
11817
11818
11819
11820
11821
11822
11823
11824
11825
11826
11827
11828
11829
11830
11831
11832
11833
11834
11835
11836
11837
11838
11839
11840
11841
11842
11843
11844
11845
11846
11847
11848
11849
11850
11851
11852
11853
11854
11855
11856
11857
11858
11859
11860
11861
11862
11863
11864
11865
11866
11867
11868
11869
11870
11871
11872
11873
11874
11875
11876
11877
11878
11879
11880
11881
11882
11883
11884
11885
11886
11887
11888
11889
11890
11891
11892
11893
11894
11895
11896
11897
11898
11899
11900
11901
11902
11903
11904
11905
11906
11907
11908
11909
11910
11911
11912
11913
11914
11915
11916
11917
11918
11919
11920
11921
11922
11923
11924
11925
11926
11927
11928
11929
11930
11931
11932
11933
11934
11935
11936
11937
11938
11939
11940
11941
11942
11943
11944
11945
11946
11947
11948
11949
11950
11951
11952
11953
11954
11955
11956
11957
11958
11959
11960
11961
11962
11963
11964
11965
11966
11967
11968
11969
11970
11971
11972
11973
11974
11975
11976
11977
11978
11979
11980
11981
11982
11983
11984
11985
11986
11987
11988
11989
11990
11991
11992
11993
11994
11995
11996
11997
11998
11999
12000
12001
12002
12003
12004
12005
12006
12007
12008
12009
12010
12011
12012
12013
12014
12015
12016
12017
12018
12019
12020
12021
12022
12023
12024
12025
12026
12027
12028
12029
12030
12031
12032
12033
12034
12035
12036
12037
12038
12039
12040
12041
12042
12043
12044
12045
12046
12047
12048
12049
12050
12051
12052
12053
12054
12055
12056
12057
12058
12059
12060
12061
12062
12063
12064
12065
12066
12067
12068
12069
12070
12071
12072
12073
12074
12075
12076
12077
12078
12079
12080
12081
12082
12083
12084
12085
12086
12087
12088
12089
12090
12091
12092
12093
12094
12095
12096
12097
12098
12099
12100
12101
12102
12103
12104
12105
12106
12107
12108
12109
12110
12111
12112
12113
12114
12115
12116
12117
12118
12119
12120
12121
12122
12123
12124
12125
12126
12127
12128
12129
12130
12131
12132
12133
12134
12135
12136
12137
12138
12139
12140
12141
12142
12143
12144
12145
12146
12147
12148
12149
12150
12151
12152
12153
12154
12155
12156
12157
12158
12159
12160
12161
12162
12163
12164
12165
12166
12167
12168
12169
12170
12171
12172
12173
12174
12175
12176
12177
12178
12179
12180
12181
12182
12183
12184
12185
12186
12187
12188
12189
12190
12191
12192
12193
12194
12195
12196
12197
12198
12199
12200
12201
12202
12203
12204
12205
12206
12207
12208
12209
12210
12211
12212
12213
12214
12215
12216
12217
12218
12219
12220
12221
12222
12223
12224
12225
12226
12227
12228
12229
12230
12231
12232
12233
12234
12235
12236
12237
12238
12239
12240
12241
12242
12243
12244
12245
12246
12247
12248
12249
12250
12251
12252
12253
12254
12255
12256
12257
12258
12259
12260
12261
12262
12263
12264
12265
12266
12267
12268
12269
12270
12271
12272
12273
12274
12275
12276
12277
12278
12279
12280
12281
12282
12283
12284
12285
12286
12287
12288
12289
12290
12291
12292
12293
12294
12295
12296
12297
12298
12299
12300
12301
12302
12303
12304
12305
12306
12307
12308
12309
12310
12311
12312
12313
12314
12315
12316
12317
12318
12319
12320
12321
12322
12323
12324
12325
12326
12327
12328
12329
12330
12331
12332
12333
12334
12335
12336
12337
12338
12339
12340
12341
12342
12343
12344
12345
12346
12347
12348
12349
12350
12351
12352
12353
12354
12355
12356
12357
12358
12359
12360
12361
12362
12363
12364
12365
12366
12367
12368
12369
12370
12371
12372
12373
12374
12375
12376
12377
12378
12379
12380
12381
12382
12383
12384
12385
12386
12387
12388
12389
12390
12391
12392
12393
12394
12395
12396
12397
12398
12399
12400
12401
12402
12403
12404
12405
12406
12407
12408
12409
12410
12411
12412
12413
12414
12415
12416
12417
12418
12419
12420
12421
12422
12423
12424
12425
12426
12427
12428
12429
12430
12431
12432
12433
12434
12435
12436
12437
12438
12439
12440
12441
12442
12443
12444
12445
12446
12447
12448
12449
12450
12451
12452
12453
12454
12455
12456
12457
12458
12459
12460
12461
12462
12463
12464
12465
12466
12467
12468
12469
12470
12471
12472
12473
12474
12475
12476
12477
12478
12479
12480
12481
12482
12483
12484
12485
12486
12487
12488
12489
12490
12491
12492
12493
12494
12495
12496
12497
12498
12499
12500
12501
12502
12503
12504
12505
12506
12507
12508
12509
12510
12511
12512
12513
12514
12515
12516
12517
12518
12519
12520
12521
12522
12523
12524
12525
12526
12527
12528
12529
12530
12531
12532
12533
12534
12535
12536
12537
12538
12539
12540
12541
12542
12543
12544
12545
12546
12547
12548
12549
12550
12551
12552
12553
12554
12555
12556
12557
12558
12559
12560
12561
12562
12563
12564
12565
12566
12567
12568
12569
12570
12571
12572
12573
12574
12575
12576
12577
12578
12579
12580
12581
12582
12583
12584
12585
12586
12587
12588
12589
12590
12591
12592
12593
12594
12595
12596
12597
12598
12599
12600
12601
12602
12603
12604
12605
12606
12607
12608
12609
12610
12611
12612
12613
12614
12615
12616
12617
12618
12619
12620
12621
12622
12623
12624
12625
12626
12627
12628
12629
12630
12631
12632
12633
12634
12635
12636
12637
12638
12639
12640
12641
12642
12643
12644
12645
12646
12647
12648
12649
12650
12651
12652
12653
12654
12655
12656
12657
12658
12659
12660
12661
12662
12663
12664
12665
12666
12667
12668
12669
12670
12671
12672
12673
12674
12675
12676
12677
12678
12679
12680
12681
12682
12683
12684
12685
12686
12687
12688
12689
12690
12691
12692
12693
12694
12695
12696
12697
12698
12699
12700
12701
12702
12703
12704
12705
12706
12707
12708
12709
12710
12711
12712
12713
12714
12715
12716
12717
12718
12719
12720
12721
12722
12723
12724
12725
12726
12727
12728
12729
12730
12731
12732
12733
12734
12735
12736
12737
12738
12739
12740
12741
12742
12743
12744
12745
12746
12747
12748
12749
12750
12751
12752
12753
12754
12755
12756
12757
12758
12759
12760
12761
12762
12763
12764
12765
12766
12767
12768
12769
12770
12771
12772
12773
12774
12775
12776
12777
12778
12779
12780
12781
12782
12783
12784
12785
12786
12787
12788
12789
12790
12791
12792
12793
12794
12795
12796
12797
12798
12799
12800
12801
12802
12803
12804
12805
12806
12807
12808
12809
12810
12811
12812
12813
12814
12815
12816
12817
12818
12819
12820
12821
12822
12823
12824
12825
12826
12827
12828
12829
12830
12831
12832
12833
12834
12835
12836
12837
12838
12839
12840
12841
12842
12843
12844
12845
12846
12847
12848
12849
12850
12851
12852
12853
12854
12855
12856
12857
12858
12859
12860
12861
12862
12863
12864
12865
12866
12867
12868
12869
12870
12871
12872
12873
12874
12875
12876
12877
12878
12879
12880
12881
12882
12883
12884
12885
12886
12887
12888
12889
12890
12891
12892
12893
12894
12895
12896
12897
12898
12899
12900
12901
12902
12903
12904
12905
12906
12907
12908
12909
12910
12911
12912
12913
12914
12915
12916
12917
12918
12919
12920
12921
12922
12923
12924
12925
12926
12927
12928
12929
12930
12931
12932
12933
12934
12935
12936
12937
12938
12939
12940
12941
12942
12943
12944
12945
12946
12947
12948
12949
12950
12951
12952
12953
12954
12955
12956
12957
12958
12959
12960
12961
12962
12963
12964
12965
12966
12967
12968
12969
12970
12971
12972
12973
12974
12975
12976
12977
12978
12979
12980
12981
12982
12983
12984
12985
12986
12987
12988
12989
12990
12991
12992
12993
12994
12995
12996
12997
12998
12999
13000
13001
13002
13003
13004
13005
13006
13007
13008
13009
13010
13011
13012
13013
13014
13015
13016
13017
13018
13019
13020
13021
13022
13023
13024
13025
13026
13027
13028
13029
13030
13031
13032
13033
13034
13035
13036
13037
13038
13039
13040
13041
13042
13043
13044
13045
13046
13047
13048
13049
13050
13051
13052
13053
13054
13055
13056
13057
13058
13059
13060
13061
13062
13063
13064
13065
13066
13067
13068
13069
13070
13071
13072
13073
13074
13075
13076
13077
13078
13079
13080
13081
13082
13083
13084
13085
13086
13087
13088
13089
13090
13091
13092
13093
13094
13095
13096
13097
13098
13099
13100
13101
13102
13103
13104
13105
13106
13107
13108
13109
13110
13111
13112
13113
13114
13115
13116
13117
13118
13119
13120
13121
13122
13123
13124
13125
13126
13127
13128
13129
13130
13131
13132
13133
13134
13135
13136
13137
13138
13139
13140
13141
13142
13143
13144
13145
13146
13147
13148
13149
13150
13151
13152
13153
13154
13155
13156
13157
13158
13159
13160
13161
13162
13163
13164
13165
13166
13167
13168
13169
13170
13171
13172
13173
13174
13175
13176
13177
13178
13179
13180
13181
13182
13183
13184
13185
13186
13187
13188
13189
13190
13191
13192
13193
13194
13195
13196
13197
13198
13199
13200
13201
13202
13203
13204
13205
13206
13207
13208
13209
13210
13211
13212
13213
13214
13215
13216
13217
13218
13219
13220
13221
13222
13223
13224
13225
13226
13227
13228
13229
13230
13231
13232
13233
13234
13235
13236
13237
13238
13239
13240
13241
13242
13243
13244
13245
13246
13247
13248
13249
13250
13251
13252
13253
13254
13255
13256
13257
13258
13259
13260
13261
13262
13263
13264
13265
13266
13267
13268
13269
13270
13271
13272
13273
13274
13275
13276
13277
13278
13279
13280
13281
13282
13283
13284
13285
13286
13287
13288
13289
13290
13291
13292
13293
13294
13295
13296
13297
13298
13299
13300
13301
13302
13303
13304
13305
13306
13307
13308
13309
13310
13311
13312
13313
13314
13315
13316
13317
13318
13319
13320
13321
13322
13323
13324
13325
13326
13327
13328
13329
13330
13331
13332
13333
13334
13335
13336
13337
13338
13339
13340
13341
13342
13343
13344
13345
13346
13347
13348
13349
13350
13351
13352
13353
13354
13355
13356
13357
13358
13359
13360
13361
13362
13363
13364
13365
13366
13367
13368
13369
13370
13371
13372
13373
13374
13375
13376
13377
13378
13379
13380
13381
13382
13383
13384
13385
13386
13387
13388
13389
13390
13391
13392
13393
13394
13395
13396
13397
13398
13399
13400
13401
13402
13403
13404
13405
13406
13407
13408
13409
13410
13411
13412
13413
13414
13415
13416
13417
13418
13419
13420
13421
13422
13423
13424
13425
13426
13427
13428
13429
13430
13431
13432
13433
13434
13435
13436
13437
13438
13439
13440
13441
13442
13443
13444
13445
13446
13447
13448
13449
13450
13451
13452
13453
13454
13455
13456
13457
13458
13459
13460
13461
13462
13463
13464
13465
13466
13467
13468
13469
13470
13471
13472
13473
13474
13475
13476
13477
13478
13479
13480
13481
13482
13483
13484
13485
13486
13487
13488
13489
13490
13491
13492
13493
13494
13495
13496
13497
13498
13499
13500
13501
13502
13503
13504
13505
13506
13507
13508
13509
13510
13511
13512
13513
13514
13515
13516
13517
13518
13519
13520
13521
13522
13523
13524
13525
13526
13527
13528
13529
13530
13531
13532
13533
13534
13535
13536
13537
13538
13539
13540
13541
13542
13543
13544
13545
13546
13547
13548
13549
13550
13551
13552
13553
13554
13555
13556
13557
13558
13559
13560
13561
13562
13563
13564
13565
13566
13567
13568
13569
13570
13571
13572
13573
13574
13575
13576
13577
13578
13579
13580
13581
13582
13583
13584
13585
13586
13587
13588
13589
13590
13591
13592
13593
13594
13595
13596
13597
13598
13599
13600
13601
13602
13603
13604
13605
13606
13607
13608
13609
13610
13611
13612
13613
13614
13615
13616
13617
13618
13619
13620
13621
13622
13623
13624
13625
13626
13627
13628
13629
13630
13631
13632
13633
13634
13635
13636
13637
13638
13639
13640
13641
13642
13643
13644
13645
13646
13647
13648
13649
13650
13651
13652
13653
13654
13655
13656
13657
13658
13659
13660
13661
13662
13663
13664
13665
13666
13667
13668
13669
13670
13671
13672
13673
13674
13675
13676
13677
13678
13679
13680
13681
13682
13683
13684
13685
13686
13687
13688
13689
13690
13691
13692
13693
13694
13695
13696
13697
13698
13699
13700
13701
13702
13703
13704
13705
13706
13707
13708
13709
13710
13711
13712
13713
13714
13715
13716
13717
13718
13719
13720
13721
13722
13723
13724
13725
13726
13727
13728
13729
13730
13731
13732
13733
13734
13735
13736
13737
13738
13739
13740
13741
13742
13743
13744
13745
13746
13747
13748
13749
13750
13751
13752
13753
13754
13755
13756
13757
13758
13759
13760
13761
13762
13763
13764
13765
13766
13767
13768
13769
13770
13771
13772
13773
13774
13775
13776
13777
13778
13779
13780
13781
13782
13783
13784
13785
13786
13787
13788
13789
13790
13791
13792
13793
13794
13795
13796
13797
13798
13799
13800
13801
13802
13803
13804
13805
13806
13807
13808
13809
13810
13811
13812
13813
13814
13815
13816
13817
13818
13819
13820
13821
13822
13823
13824
13825
13826
13827
13828
13829
13830
13831
13832
13833
13834
13835
13836
13837
13838
13839
13840
13841
13842
13843
13844
13845
13846
13847
13848
13849
13850
13851
13852
13853
13854
13855
13856
13857
13858
13859
13860
13861
13862
13863
13864
13865
13866
13867
13868
13869
13870
13871
13872
13873
13874
13875
13876
13877
13878
13879
13880
13881
13882
13883
13884
13885
13886
13887
13888
13889
13890
13891
13892
13893
13894
13895
13896
13897
13898
13899
13900
13901
13902
13903
13904
13905
13906
13907
13908
13909
13910
13911
13912
13913
13914
13915
13916
13917
13918
13919
13920
13921
13922
13923
13924
13925
13926
13927
13928
13929
13930
13931
13932
13933
13934
13935
13936
13937
13938
13939
13940
13941
13942
13943
13944
13945
13946
13947
13948
13949
13950
13951
13952
13953
13954
13955
13956
13957
13958
13959
13960
13961
13962
13963
13964
13965
13966
13967
13968
13969
13970
13971
13972
13973
13974
13975
13976
13977
13978
13979
13980
13981
13982
13983
13984
13985
13986
13987
13988
13989
13990
13991
13992
13993
13994
13995
13996
13997
13998
13999
14000
14001
14002
14003
14004
14005
14006
14007
14008
14009
14010
14011
14012
14013
14014
14015
14016
14017
14018
14019
14020
14021
14022
14023
14024
14025
14026
14027
14028
14029
14030
14031
14032
14033
14034
14035
14036
14037
14038
14039
14040
14041
14042
14043
14044
14045
14046
14047
14048
14049
14050
14051
14052
14053
14054
14055
14056
14057
14058
14059
14060
14061
14062
14063
14064
14065
14066
14067
14068
14069
14070
14071
14072
14073
14074
14075
14076
14077
14078
14079
14080
14081
14082
14083
14084
14085
14086
14087
14088
14089
14090
14091
14092
14093
14094
14095
14096
14097
14098
14099
14100
14101
14102
14103
14104
14105
14106
14107
14108
14109
14110
14111
14112
14113
14114
14115
14116
14117
14118
14119
14120
14121
14122
14123
14124
14125
14126
14127
14128
14129
14130
14131
14132
14133
14134
14135
14136
14137
14138
14139
14140
14141
14142
14143
14144
14145
14146
14147
14148
14149
14150
14151
14152
14153
14154
14155
14156
14157
14158
14159
14160
14161
14162
14163
14164
14165
14166
14167
14168
14169
14170
14171
14172
14173
14174
14175
14176
14177
14178
14179
14180
14181
14182
14183
14184
14185
14186
14187
14188
14189
14190
14191
14192
14193
14194
14195
14196
14197
14198
14199
14200
14201
14202
14203
14204
14205
14206
14207
14208
14209
14210
14211
14212
14213
14214
14215
14216
14217
14218
14219
14220
14221
14222
14223
14224
14225
14226
14227
14228
14229
14230
14231
14232
14233
14234
14235
14236
14237
14238
14239
14240
14241
14242
14243
14244
14245
14246
14247
14248
14249
14250
14251
14252
14253
14254
14255
14256
14257
14258
14259
14260
14261
14262
14263
14264
14265
14266
14267
14268
14269
14270
14271
14272
14273
14274
14275
14276
14277
14278
14279
14280
14281
14282
14283
14284
14285
14286
14287
14288
14289
14290
14291
14292
14293
14294
14295
14296
14297
14298
14299
14300
14301
14302
14303
14304
14305
14306
14307
14308
14309
14310
14311
14312
14313
14314
14315
14316
14317
14318
14319
14320
14321
14322
14323
14324
14325
14326
14327
14328
14329
14330
14331
14332
14333
14334
14335
14336
14337
14338
14339
14340
14341
14342
14343
14344
14345
14346
14347
14348
14349
14350
14351
14352
14353
14354
14355
14356
14357
14358
14359
14360
14361
14362
14363
14364
14365
14366
14367
14368
14369
14370
14371
14372
14373
14374
14375
14376
14377
14378
14379
14380
14381
14382
14383
14384
14385
14386
14387
14388
14389
14390
14391
14392
14393
14394
14395
14396
14397
14398
14399
14400
14401
14402
14403
14404
14405
14406
14407
14408
14409
14410
14411
14412
14413
14414
14415
14416
14417
14418
14419
14420
14421
14422
14423
14424
14425
14426
14427
14428
14429
14430
14431
14432
14433
14434
14435
14436
14437
14438
14439
14440
14441
14442
14443
14444
14445
14446
14447
14448
14449
14450
14451
14452
14453
14454
14455
14456
14457
14458
14459
14460
14461
14462
14463
14464
14465
14466
14467
14468
14469
14470
14471
14472
14473
14474
14475
14476
14477
14478
14479
14480
14481
14482
14483
14484
14485
14486
14487
14488
14489
14490
14491
14492
14493
14494
14495
14496
14497
14498
14499
14500
14501
14502
14503
14504
14505
14506
14507
14508
14509
14510
14511
14512
14513
14514
14515
14516
14517
14518
14519
14520
14521
14522
14523
14524
14525
14526
14527
14528
14529
14530
14531
14532
14533
14534
14535
14536
14537
14538
14539
14540
14541
14542
14543
14544
14545
14546
14547
14548
14549
14550
14551
14552
14553
14554
14555
14556
14557
14558
14559
14560
14561
14562
14563
14564
14565
14566
14567
14568
14569
14570
14571
14572
14573
14574
14575
14576
14577
14578
14579
14580
14581
14582
14583
14584
14585
14586
14587
14588
14589
14590
14591
14592
14593
14594
14595
14596
14597
14598
14599
14600
14601
14602
14603
14604
14605
14606
14607
14608
14609
14610
14611
14612
14613
14614
14615
14616
14617
14618
14619
14620
14621
14622
14623
14624
14625
14626
14627
14628
14629
14630
14631
14632
14633
14634
14635
14636
14637
14638
14639
14640
14641
14642
14643
14644
14645
14646
14647
14648
14649
14650
14651
14652
14653
14654
14655
14656
14657
14658
14659
14660
14661
14662
14663
14664
14665
14666
14667
14668
14669
14670
14671
14672
14673
14674
14675
14676
14677
14678
14679
14680
14681
14682
14683
14684
14685
14686
14687
14688
14689
14690
14691
14692
14693
14694
14695
14696
14697
14698
14699
14700
14701
14702
14703
14704
14705
14706
14707
14708
14709
14710
14711
14712
14713
14714
14715
14716
14717
14718
14719
14720
14721
14722
14723
14724
14725
14726
14727
14728
14729
14730
14731
14732
14733
14734
14735
14736
14737
14738
14739
14740
14741
14742
14743
14744
14745
14746
14747
14748
14749
14750
14751
14752
14753
14754
14755
14756
14757
14758
14759
14760
14761
14762
14763
14764
14765
14766
14767
14768
14769
14770
14771
14772
14773
14774
14775
14776
14777
14778
14779
14780
14781
14782
14783
14784
14785
14786
14787
14788
14789
14790
14791
14792
14793
14794
14795
14796
14797
14798
14799
14800
14801
14802
14803
14804
14805
14806
14807
14808
14809
14810
14811
14812
14813
14814
14815
14816
14817
14818
14819
14820
14821
14822
14823
14824
14825
14826
14827
14828
14829
14830
14831
14832
14833
14834
14835
14836
14837
14838
14839
14840
14841
14842
14843
14844
14845
14846
14847
14848
14849
14850
14851
14852
14853
14854
14855
14856
14857
14858
14859
14860
14861
14862
14863
14864
14865
14866
14867
14868
14869
14870
14871
14872
14873
14874
14875
14876
14877
14878
14879
14880
14881
14882
14883
14884
14885
14886
14887
14888
14889
14890
14891
14892
14893
14894
14895
14896
14897
14898
14899
14900
14901
14902
14903
14904
14905
14906
14907
14908
14909
14910
14911
14912
14913
14914
14915
14916
14917
14918
14919
14920
14921
14922
14923
14924
14925
14926
14927
14928
14929
14930
14931
14932
14933
14934
14935
14936
14937
14938
14939
14940
14941
14942
14943
14944
14945
14946
14947
14948
14949
14950
14951
14952
14953
14954
14955
14956
14957
14958
14959
14960
14961
14962
14963
14964
14965
14966
14967
14968
14969
14970
14971
14972
14973
14974
14975
14976
14977
14978
14979
14980
14981
14982
14983
14984
14985
14986
14987
14988
14989
14990
14991
14992
14993
14994
14995
14996
14997
14998
14999
15000
15001
15002
15003
15004
15005
15006
15007
15008
15009
15010
15011
15012
15013
15014
15015
15016
15017
15018
15019
15020
15021
15022
15023
15024
15025
15026
15027
15028
15029
15030
15031
15032
15033
15034
15035
15036
15037
15038
15039
15040
15041
15042
15043
15044
15045
15046
15047
15048
15049
15050
15051
15052
15053
15054
15055
15056
15057
15058
15059
15060
15061
15062
15063
15064
15065
15066
15067
15068
15069
15070
15071
15072
15073
15074
15075
15076
15077
15078
15079
15080
15081
15082
15083
15084
15085
15086
15087
15088
15089
15090
15091
15092
15093
15094
15095
15096
15097
15098
15099
15100
15101
15102
15103
15104
15105
15106
15107
15108
15109
15110
15111
15112
15113
15114
15115
15116
15117
15118
15119
15120
15121
15122
15123
15124
15125
15126
15127
15128
15129
15130
15131
15132
15133
15134
15135
15136
15137
15138
15139
15140
15141
15142
15143
15144
15145
15146
15147
15148
15149
15150
15151
15152
15153
15154
15155
15156
15157
15158
15159
15160
15161
15162
15163
15164
15165
15166
15167
15168
15169
15170
15171
15172
15173
15174
15175
15176
15177
15178
15179
15180
15181
15182
15183
15184
15185
15186
15187
15188
15189
15190
15191
15192
15193
15194
15195
15196
15197
15198
15199
15200
15201
15202
15203
15204
15205
15206
15207
15208
15209
15210
15211
15212
15213
15214
15215
15216
15217
15218
15219
15220
15221
15222
15223
15224
15225
15226
15227
15228
15229
15230
15231
15232
15233
15234
15235
15236
15237
15238
15239
15240
15241
15242
15243
15244
15245
15246
15247
15248
15249
15250
15251
15252
15253
15254
15255
15256
15257
15258
15259
15260
15261
15262
15263
15264
15265
15266
15267
15268
15269
15270
15271
15272
15273
15274
15275
15276
15277
15278
15279
15280
15281
15282
15283
15284
15285
15286
15287
15288
15289
15290
15291
15292
15293
15294
15295
15296
15297
15298
15299
15300
15301
15302
15303
15304
15305
15306
15307
15308
15309
15310
15311
15312
15313
15314
15315
15316
15317
15318
15319
15320
15321
15322
15323
15324
15325
15326
15327
15328
15329
15330
15331
15332
15333
15334
15335
15336
15337
15338
15339
15340
15341
15342
15343
15344
15345
15346
15347
15348
15349
15350
15351
15352
15353
15354
15355
15356
15357
15358
15359
15360
15361
15362
15363
15364
15365
15366
15367
15368
15369
15370
15371
15372
15373
15374
15375
15376
15377
15378
15379
15380
15381
15382
15383
15384
15385
15386
15387
15388
15389
15390
15391
15392
15393
15394
15395
15396
15397
15398
15399
15400
15401
15402
15403
15404
15405
15406
15407
15408
15409
15410
15411
15412
15413
15414
15415
15416
15417
15418
15419
15420
15421
15422
15423
15424
15425
15426
15427
15428
15429
15430
15431
15432
15433
15434
15435
15436
15437
15438
15439
15440
15441
15442
15443
15444
15445
15446
15447
15448
15449
15450
15451
15452
15453
15454
15455
15456
15457
15458
15459
15460
15461
15462
15463
15464
15465
15466
15467
15468
15469
15470
15471
15472
15473
15474
15475
15476
15477
15478
15479
15480
15481
15482
15483
15484
15485
15486
15487
15488
15489
15490
15491
15492
15493
15494
15495
15496
15497
15498
15499
15500
15501
15502
15503
15504
15505
15506
15507
15508
15509
15510
15511
15512
15513
15514
15515
15516
15517
15518
15519
15520
15521
15522
15523
15524
15525
15526
15527
15528
15529
15530
15531
15532
15533
15534
15535
15536
15537
15538
15539
15540
15541
15542
15543
15544
15545
15546
15547
15548
15549
15550
15551
15552
15553
15554
15555
15556
15557
15558
15559
15560
15561
15562
15563
15564
15565
15566
15567
15568
15569
15570
15571
15572
15573
15574
15575
15576
15577
15578
15579
15580
15581
15582
15583
15584
15585
15586
15587
15588
15589
15590
15591
15592
15593
15594
15595
15596
15597
15598
15599
15600
15601
15602
15603
15604
15605
15606
15607
15608
15609
//! The PHP object heap and runtime, reached from fusevm through registered
//! builtins (`builtins::install`) and the strict numeric hook.
//!
//! Scalars (int/float/bool/string/null) ride through the VM as native
//! `fusevm::Value`s. Arrays are heap objects: a `Value::Obj(u32)` handle indexes
//! `PhpHost::objs`. All mutable runtime state — the current variable scope stack,
//! the user-function table, the output buffer, the pending error/return signal —
//! lives in a `thread_local!` `PhpHost`, so a fresh `VM` can be spun up per
//! function call (see `call_function`) while sharing one heap.

use crate::ast::{TypeHint, Visibility};
use crate::errlevel;
use fusevm::{Chunk, VMResult, Value};
use indexmap::IndexMap;
use rustc_hash::{FxHashMap, FxHashSet};
use std::cell::{Cell, RefCell};

/// Builtin ids emitted by the compiler and registered on every VM.
pub mod ops {
    pub const ECHO: u16 = 1; // [parts...] argc=n -> Undef (prints each)
    pub const GETVAR: u16 = 2; // [name] -> value
    pub const SETVAR: u16 = 3; // [name, value] -> value
    pub const CONCAT: u16 = 4; // [a, b] -> string
    pub const TRUTHY: u16 = 5; // [v] -> Bool (PHP truthiness)
    pub const CALL: u16 = 6; // [name, args...] argc=1+n -> return value
    pub const MKARRAY: u16 = 7; // [k,v,...] argc=2n -> array handle
    pub const INDEX_GET: u16 = 8; // [recv, key] -> value
    pub const INDEX_SET: u16 = 9; // [name, key, val] -> val ($var[key] = val)
    pub const ARR_APPEND: u16 = 10; // [name, val] -> val ($var[] = val)
    pub const DIV: u16 = 11; // [a, b] -> a / b
    pub const MOD: u16 = 12; // [a, b] -> a % b
    pub const POW: u16 = 13; // [a, b] -> a ** b
    pub const LOOSE_EQ: u16 = 14; // [a, b] -> Bool (==)
    pub const LOOSE_NE: u16 = 15; // [a, b] -> Bool (!=)
    pub const STRICT_EQ: u16 = 16; // [a, b] -> Bool (===)
    pub const STRICT_NE: u16 = 17; // [a, b] -> Bool (!==)
    pub const LT: u16 = 18; // [a, b] -> Bool (<)
    pub const GT: u16 = 19; // [a, b] -> Bool (>)
    pub const LE: u16 = 20; // [a, b] -> Bool (<=)
    pub const GE: u16 = 21; // [a, b] -> Bool (>=)
    pub const SIG_RETURN: u16 = 22; // [v] -> halt function, return v
    pub const INCDEC: u16 = 23; // [name, code] -> value (++/--)
    pub const ARRAYKEYS: u16 = 24; // [arr] -> array of keys
    pub const ARRAYLEN: u16 = 25; // [arr] -> Int element count
    pub const BITAND: u16 = 26; // [a, b] -> a & b
    pub const BITOR: u16 = 27; // [a, b] -> a | b
    pub const BITXOR: u16 = 28; // [a, b] -> a ^ b
    pub const SHL: u16 = 29; // [a, b] -> a << b
    pub const SHR: u16 = 30; // [a, b] -> a >> b
    pub const BITNOT: u16 = 31; // [a] -> ~a
    pub const SPACESHIP: u16 = 32; // [a, b] -> -1 | 0 | 1
    pub const DBG_LINE: u16 = 33; // [line] -> DAP statement marker (debug only)
    pub const ARR_MUT: u16 = 34; // [name, subop, args...] -> by-reference array mutator
    pub const GET_PATH: u16 = 35; // [name, k1..kN] -> value (read $a[k1]..[kN])
    pub const SET_PATH: u16 = 36; // [name, k1..kN, val] -> val ($a[k1]..[kN] = val, N>=1)
    pub const APPEND_PATH: u16 = 37; // [name, k1..kM, val] -> val ($a[k1]..[kM][] = val)
    pub const INCDEC_PATH: u16 = 38; // [name, k1..kN, code] -> value (++/-- on element)
    pub const CALL_SPREAD: u16 = 39; // [name, flag, val, ...] -> return value (arg unpacking)
    pub const MKCLOSURE: u16 = 40; // [def_name, k, v, ...] argc=1+2n -> closure handle
    pub const CALL_VALUE: u16 = 41; // [callee, args...] argc=1+n -> return value
    pub const NEW: u16 = 42; // [class, args...] argc=1+n -> object handle
    pub const PROP_GET: u16 = 43; // [recv, name] -> value ($o->p)
    pub const PROP_SET: u16 = 44; // [recv, name, val] -> val ($o->p = v)
    pub const MCALL: u16 = 45; // [recv, method, args...] argc=2+n -> return
    pub const SCALL: u16 = 46; // [class, method, args...] argc=2+n -> return
    pub const SCONST: u16 = 47; // [class, name] -> class constant value
    pub const PATH_APPEND_CHILD: u16 = 48; // [name, k1..kN] -> handle of a freshly appended child array
    pub const PROP_ENSURE_ARRAY: u16 = 49; // [recv, name] -> handle of the array held in $o->name (vivified)
    pub const PROP_INCDEC: u16 = 50; // [recv, name, code] -> value (++/-- on $o->p)
    pub const THROW: u16 = 51; // [exc] -> record pending exception, halt chunk
    pub const RUN_TRY: u16 = 52; // [try-def id] -> control status int (see run_try)
    pub const SIG_HALT: u16 = 53; // [] -> halt chunk (propagate an already-set signal)
    pub const SIG_BREAK: u16 = 54; // [] -> signal `break`, halt chunk
    pub const SIG_CONTINUE: u16 = 55; // [] -> signal `continue`, halt chunk
    pub const CONST_FETCH: u16 = 56; // [name] -> constant value (or the bare name)
    pub const UNSET_VAR: u16 = 57; // [name] -> Undef (remove the scope variable)
    pub const UNSET_PATH: u16 = 58; // [name, k1..kN] -> Undef (remove $a[k1]..[kN])
    pub const FOREACH_PREP: u16 = 59; // [v] -> an iterable array (object -> iterated)
    pub const INSTANCEOF: u16 = 60; // [obj, class-name] -> Bool
    pub const REF_BIND: u16 = 61; // [target, source] -> value ($t = &$s)
    pub const BYREF_OUT: u16 = 62; // [position] -> the last call's by-ref param value
    /// `[name, value]` — a source-level `$x = expr`. Same as [`SETVAR`] but the
    /// copied, everything else passes through. Emitted by the compiler on the
    /// right-hand side of a source-level assignment only, so a compiler
    /// temporary and a `&` binding still share the handle they are given.
    /// See `PhpHost::copy_on_assign`.
    pub const COPY: u16 = 81;
    /// `[name]` — the enclosing variable's reference cell as a `PhpObj::Ref`
    /// handle, promoting the variable to a cell if it was not one. What
    /// `use (&$v)` captures; binding one into a frame aliases the name to the
    /// cell rather than storing the handle.
    pub const REF_CELL: u16 = 82;
    pub const SPROP_GET: u16 = 63; // [class, name] -> static property value (Class::$p)
    pub const SPROP_SET: u16 = 64; // [class, name, val] -> val (Class::$p = v)
    pub const SPROP_INCDEC: u16 = 65; // [class, name, code] -> value (++/-- on Class::$p)
    pub const STATIC_BIND: u16 = 66; // [name, slot-key, init] -> Undef (bind `static $var`)
                                     // Named-argument call variants (PHP 8.0). Each argument is encoded as a
                                     // `(name, value)` pair on the stack — `name` is a `Str` for a named argument
                                     // or `Undef` for a positional one — so the host can rebind by parameter name.
    pub const CALL_NAMED: u16 = 67; // [fname, (n,v)...] argc=1+2k -> return value
    pub const MCALL_NAMED: u16 = 68; // [recv, method, (n,v)...] argc=2+2k -> return
    pub const SCALL_NAMED: u16 = 69; // [class, method, (n,v)...] argc=2+2k -> return
    pub const NEW_NAMED: u16 = 70; // [class, (n,v)...] argc=1+2k -> object handle
    pub const CALLVALUE_NAMED: u16 = 71; // [callee, (n,v)...] argc=1+2k -> return value
                                         // Generators (host-side stackful coroutines). YIELD/YIELD_KV/YIELD_FROM run
                                         // inside a generator body and suspend it; the GEN_* ops drive a Generator from
                                         // a `foreach` (lazily, preserving side-effect order).
    pub const YIELD: u16 = 72; // [value] -> the value the next send()/next() supplies
    pub const YIELD_KV: u16 = 73; // [value, key] -> same, with an explicit key
    pub const YIELD_FROM: u16 = 74; // [iterable] -> delegate; leaves the delegate's return
    pub const IS_GENERATOR: u16 = 75; // [v] -> Bool (v is a Generator handle)
    pub const GEN_REWIND: u16 = 76; // [gen] -> Undef (prime to the first yield)
    pub const GEN_VALID: u16 = 77; // [gen] -> Bool (not yet finished)
    pub const GEN_KEY: u16 = 78; // [gen] -> current key
    pub const GEN_CURRENT: u16 = 79; // [gen] -> current value
    pub const GEN_NEXT: u16 = 80; // [gen] -> Undef (resume to the next yield)

    pub const SIG_LEVEL: u16 = 83; // [] -> Int, level of the last break/continue

    // `&` bindings to a *container slot* (`$r = &$a['x']['y']`, `$a[] = &$x`,
    // `$o->p = &$x`). Split into an ACQUIRE half, which returns the reference
    // cell the right-hand side denotes — promoting the element/property into a
    // reference if it was a plain value — and a BIND half, which points the
    // left-hand side at that cell. Composing the two halves covers every
    // combination of variable / element / property on either side without an
    // opcode per pair. The slot travels between them as a plain `Int`; it is a
    // compiler-internal value that never reaches a PHP expression.
    pub const REF_SLOT_VAR: u16 = 84; // [name] -> Int slot of $name
    pub const REF_SLOT_ELEM: u16 = 85; // [name, k1..kN] -> Int slot of $name[k1]..[kN]
    pub const REF_SLOT_PROP: u16 = 86; // [recv, prop] -> Int slot of $recv->prop
    pub const REF_TO_VAR: u16 = 87; // [name, slot] -> Undef ($name = &<slot>)
    pub const REF_TO_ELEM: u16 = 88; // [name, k1..kN, slot] -> Undef
    pub const REF_TO_APPEND: u16 = 89; // [name, k1..kM, slot] -> Undef ($name[..][] = &…)
    pub const REF_TO_PROP: u16 = 90; // [recv, prop, slot] -> Undef
    /// `[slot]` — a `function &f()` body's `return <lvalue>`: publish the slot as
    /// the call's returned reference and leave its value, which is what a plain
    /// (non-`&`) call of the function sees.
    pub const RET_REF: u16 = 91;
    /// `[]` — the reference cell the last call returned, for `$r = &f()`. A call
    /// that did not return by reference yields a fresh detached cell holding the
    /// returned value, so the binding degrades to a private copy rather than
    /// aliasing something arbitrary.
    pub const REF_SLOT_RET: u16 = 92;

    // Diagnostic-free reads. PHP fetches a variable / element / property in
    // "isset mode" (`BP_VAR_IS`) inside `isset()`, `empty()` and the left operand
    // of `??`, where a missing one is the question being asked rather than a
    // mistake, and raises no diagnostic. These are the same reads as their loud
    // twins with the warning suppressed; the compiler picks between them
    // statically, so a function call nested in a key still warns normally.
    pub const GETVAR_Q: u16 = 93; // [name] -> value, no "Undefined variable"
    pub const INDEX_GET_Q: u16 = 94; // [recv, key] -> value, no "Undefined array key"
    pub const PROP_GET_Q: u16 = 95; // [recv, name] -> value, no "Undefined property"

    /// `[recv, key] -> value` — one element of a `list()`/`[…]` destructuring.
    ///
    /// Separate from [`INDEX_GET`] because PHP treats a non-array subject
    /// differently here: `null` yields null silently, a scalar warns
    /// `Cannot use <type> as array`, and an object THROWS. Only an array
    /// reaches the ordinary keyed read.
    pub const LIST_ELEM_GET: u16 = 105;

    // Late static binding. `static::` names the class the call was made *on*, not
    // the class the running method was declared in, so it can only be resolved at
    // run time — a method inherited by two subclasses sees a different
    // `static::` in each.
    /// `[fallback]` -> the running frame's late-static-binding class. The
    /// compiler supplies the enclosing class as the fallback, which is what the
    /// name means outside any call (and what `self::` always means).
    pub const LSB_CLASS: u16 = 96;
    /// `[]` -> Undef. Marks the next call as *forwarding*: `self::m()`,
    /// `parent::m()` and `static::m()` keep the caller's late-static-binding
    /// class, whereas naming a class explicitly (`Base::m()`) replaces it.
    pub const LSB_FORWARD: u16 = 97;

    /// `[recv, name]` -> `recv`, unchanged. Marks a property as being fetched
    /// FOR WRITING, which is when PHP decides whether the write creates a dynamic
    /// property. Emitted ahead of the read half of a compound assignment
    /// (`$o->p .= "x"`), because the reference engine raises the
    /// "Creation of dynamic property" deprecation before the "Undefined property"
    /// warning the read then produces — the slot exists by the time it is read.
    pub const PROP_TOUCH: u16 = 98;
    /// `[recv, name, val]` -> `val`. The write half of a read-modify-write, whose
    /// creation of a dynamic property was already announced by the [`PROP_TOUCH`]
    /// that opened it. Identical to [`PROP_SET`] but silent, so `$o->p .= "x"`
    /// deprecates once rather than twice.
    pub const PROP_SET_RW: u16 = 99;
    /// `[] -> null`. Open an `@expr` suppression region (see
    /// [`super::PhpHost::suppress_push`]). The pushed null is dropped by the
    /// `Pop` the compiler emits after it.
    ///
    /// `@` needs a RUN-TIME region, not just the quiet read opcodes: a warning
    /// raised inside a library function (`@preg_match('/[a', $s)`,
    /// `@range('ab', 'c')`) is raised from Rust with no opcode of its own to
    /// quieten, and the reference silences those too.
    pub const SUPPRESS_PUSH: u16 = 100;
    /// `[value] -> value`. Close the region [`SUPPRESS_PUSH`] opened, passing the
    /// operand's value through so `@` stays an expression.
    pub const SUPPRESS_POP: u16 = 101;
    /// `[recv, name] -> null`. `unset($o->p)`.
    pub const PROP_UNSET: u16 = 102;
    /// `[recv, name] -> bool`. `isset($o->p)` — see [`super::PropAccess`] and the
    /// `IssetOf` arm of the compiler for why this cannot be a value comparison.
    pub const PROP_ISSET: u16 = 103;
    /// `[recv, name] -> value`. The read inside `empty($o->p)`. [`PROP_GET_Q`]
    /// with one arm changed: a property no `__isset` vouched for reads as null
    /// instead of falling back to `__get`, so a class with `__get` and no
    /// `__isset` is `empty()` without `__get` being called at all.
    pub const PROP_GET_EMPTY: u16 = 104;
    /// `[recv, key] -> bool`. `isset($a[k])`. An array or string answers from its
    /// own contents, but an `ArrayAccess` object answers with `offsetExists` and
    /// NOTHING else — `offsetGet` is never called, so the answer cannot be
    /// recovered from a value the way it can for an array.
    pub const INDEX_ISSET: u16 = 106;

    /// `[message] -> !`. A declaration the compiler read but the reference would
    /// refuse to LINK — today, a `use Trait` whose adaptations do not resolve.
    ///
    /// It is an op rather than a compile error because PHP binds a
    /// trait-using class at run time: everything the script printed before the
    /// declaration is printed, and only then does the fatal land. Emitting it
    /// where the declaration stood reproduces that ordering exactly.
    pub const DECL_FATAL: u16 = 107;

    /// `[value] -> class-name`. The left of a `::` when it is an expression
    /// rather than a bareword (`$cls::K`, `$obj::m()`): an object resolves to
    /// its class, a string is already a class name, and anything else is an
    /// `Error` — PHP does not coerce here.
    pub const DYN_CLASS: u16 = 108;

    /// `[value] -> class-name`. `$expr::class`, which is NOT `DYN_CLASS`
    /// followed by a read: PHP answers only for an object and rejects even the
    /// class-name string that `DYN_CLASS` would happily accept.
    pub const DYN_CLASS_CONST: u16 = 109;

    /// `[object] -> object`. `clone $o`: a new instance of the same class with
    /// the properties copied (arrays by value, handles shared), then `__clone`
    /// if the class defines one.
    pub const CLONE: u16 = 110;

    /// `[name, value] -> Undef`. The `const NAME = expr;` declaration.
    ///
    /// An op rather than a compile-time table write because the declaration
    /// takes effect WHERE IT STANDS: a `defined()` earlier in the script must
    /// answer false, and a redefinition must warn at the point of the second
    /// declaration. Both need the statement to run in source order.
    pub const CONST_DECL: u16 = 111;

    /// `[prefix, suffix] -> string`. `{prefix}{__FILE__}{suffix}` — the running
    /// script's name, optionally wrapped. The affixes are how a closure declared
    /// at file scope gets its `{closure:<file>:<line>}` name.
    pub const MAGIC_FILE: u16 = 112;

    /// `[] -> string`. `__DIR__`.
    pub const MAGIC_DIR: u16 = 113;

    /// `[prefix, suffix] -> string`. `{prefix}{__CLASS__}{suffix}` where the parse
    /// could not name the class — inside a trait method (the USING class) or an
    /// anonymous class. The affixes build such a class's `__METHOD__`.
    pub const MAGIC_CLASS: u16 = 114;

    /// `[pos] -> Bool`. Whether the call that just returned took its parameter at
    /// `pos` by reference. Guards the write-back at a call site whose callee is
    /// not a compile-time fact — a method call, a static call, or `$f(…)`.
    pub const BYREF_LIVE: u16 = 115;

    // ── slot-addressed locals ────────────────────────────────────────────────
    // The compiler resolves a scope's variables to indices and emits these
    // instead of the by-name ops above, so a read is a vector index rather than
    // a superglobal-name test plus a string hash into the scope map. The slot
    // and the name address the SAME storage (`Vars`), so `extract()`, `$$name`,
    // `unset()`, a reference binding, an array path write and a by-reference
    // parameter's write-back all stay coherent with slot access — they simply
    // reach the slot the other way round.
    /// `[] -> value` — read slot N, warning `Undefined variable` if unbound.
    pub const GETSLOT: u16 = 116;
    /// `[] -> value` — read slot N with no diagnostic (isset/`empty` context).
    pub const GETSLOT_Q: u16 = 117;
    /// `[value] -> value` — write slot N, through the shared cell if it is a
    /// reference binding.
    pub const SETSLOT: u16 = 118;
    /// `[slot, code] -> value` — `++`/`--` on slot N. One op for the whole
    /// read-modify-write: the by-name form resolved the name twice per
    /// execution, once to read and once to store, which in a counted loop is
    /// two hashes an iteration for a variable the compiler had already placed.
    pub const INCDEC_SLOT: u16 = 119;
    /// `[arr, k,v, ...] argc=1+2n -> arr` — append more `key => value` pairs to
    /// an array already built by `MKARRAY`.
    ///
    /// `CallBuiltin`'s operand count is a `u8`, so one `MKARRAY` can carry at
    /// most 127 pairs. A longer literal is emitted as an `MKARRAY` followed by
    /// `MKARRAY_ADD` continuations rather than overflowing that count — which
    /// is what a 200-element literal used to do (`400 as u8` == 144, so the
    /// builtin popped 144 of its 400 operands and left the rest on the stack).
    pub const MKARRAY_ADD: u16 = 120;
    /// `[name, lhs, rhs] -> value` — a DIRECT two-argument `min()`/`max()`.
    ///
    /// The reference compiles that exact call shape to a different C function
    /// than every other way of reaching `min`, and the two do not agree once a
    /// NaN is involved (see `minmax_frameless`). Selecting
    /// between them is a compile-time fact there, so it is one here too: the
    /// compiler emits this op only for a literal two-argument call, and
    /// everything else — a dynamic name, `call_user_func`, a spread, a named
    /// argument, any other arity — keeps [`CALL`] and the ordinary function.
    ///
    /// The name still travels with it so a user-declared `min`/`max` is found
    /// first, exactly as [`CALL`] would.
    pub const MINMAX_FLF2: u16 = 121;
    /// `[kind, callee, argno, param] -> null` — the by-reference ARGUMENT check.
    ///
    /// A by-reference parameter needs somewhere to write back to, and not every
    /// expression can supply one. The reference sorts them into three groups and
    /// treats each differently, so the compiler classifies the argument and emits
    /// this op to carry the verdict into the run:
    ///
    /// * a variable, a subscript, a property or a static property is a real
    ///   location and no diagnostic is emitted at all — this op is not emitted;
    /// * a function/method call or a `new` produces a fresh temporary the engine
    ///   CAN bind, so `kind` 0 raises `Notice: Only variables should be passed by
    ///   reference` and the call proceeds against that temporary;
    /// * anything else — a literal, a ternary, an assignment, a cast, `clone`,
    ///   `@expr`, `?->`, `??` — has no location even in principle, so `kind` 1
    ///   throws `Error: {callee}(): Argument #{argno} (${param}) could not be
    ///   passed by reference` and the call never runs.
    ///
    /// `param` is empty for a VARIADIC by-reference position (`sscanf`), whose
    /// message names no parameter.
    ///
    /// The op sits between the argument it judges and the rest of the argument
    /// list, because that is where the reference performs the check: the failing
    /// argument is evaluated for its side effects first, and the arguments AFTER
    /// it are never evaluated at all.
    pub const BYREF_ARG_DIAG: u16 = 122;
    /// `[name] -> null` — `global $name;`.
    ///
    /// Binds the running frame's `name` as an ALIAS of the global variable of
    /// that name, which is what the reference does: `global $x` is defined as
    /// `$x = &$GLOBALS['x']`, so the two share one cell and each sees the
    /// other's writes. One op per name keeps `global $a, $b;` a plain sequence.
    pub const GLOBAL_BIND: u16 = 123;
    /// `[old, inc] -> new` — one step of `++`/`--` on a VALUE.
    ///
    /// The slot and by-name forms of `++` read the variable, step it and write
    /// it back in one builtin, because they are the only ones that know where
    /// the variable lives. A local promoted to a fusevm frame slot is read and
    /// written by the ops themselves, so all it needs from the host is the step
    /// — which is not arithmetic: `"Az"++` is `"Ba"` and `null--` is a no-op.
    pub const INCDEC_STEP: u16 = 124;
    /// `[subject] -> string` — the `UnhandledMatchError` message for a `match`
    /// that matched no arm.
    ///
    /// Not a plain concatenation of the subject: the reference renders the
    /// value the way a stack trace renders an argument (a string single-quoted,
    /// escaped and cut at 15 bytes; a float keeping its `.0`; `true`/`false`/
    /// `NULL` as words) and replaces anything that is not a scalar with `of
    /// type <type>`. Concatenating instead turned `null` into the empty string,
    /// `true` into `1`, `'hi'` into unquoted `hi`, and an array into `Array`
    /// behind an `Array to string conversion` warning the reference never
    /// raises.
    pub const MATCH_ERROR_MSG: u16 = 125;
    /// `[] -> string` — the class `self::` names inside a TRAIT method.
    ///
    /// A trait's methods are compiled once and copied into every class that
    /// uses them, so `self` cannot be baked while the body is lowered: it is
    /// the composing class, which is only known once a frame is running. Baking
    /// it made `self::class` answer the TRAIT's name from every class that used
    /// it, and `new self()` build an instance of the trait.
    pub const SELF_CLASS: u16 = 126;
    /// `[] -> string` — the class `parent::` names inside a TRAIT method: the
    /// parent of the composing class, for the same reason as
    /// [`SELF_CLASS`]. Baking it refused every trait that spelled
    /// `parent::` with `'parent' used in a class with no parent`, whatever the
    /// using class extended.
    pub const PARENT_CLASS: u16 = 127;
    /// `[recv, method]` -> `recv`. Raises `Call to a member function m() on
    /// <type>` when `recv` is not an object, WITHOUT touching the method table.
    /// Emitted before a method call's arguments, because the reference decides
    /// a receiver is uncallable before it evaluates any of them.
    pub const MCALL_RECV_CHECK: u16 = 128;
    /// `[recv, key] -> value` — [`INDEX_GET_Q`] with `empty()`'s wording for a
    /// bad offset TYPE. Everything else about the read is identical; the two
    /// contexts differ only in the sentence the reference ends the message with.
    pub const INDEX_GET_EMPTY: u16 = 129;
    /// `[name]` -> `name`. Raises `Call to undefined function f()` when no
    /// function of that name resolves. Emitted before a named call's arguments
    /// for the same reason as [`MCALL_RECV_CHECK`]: the reference decides the
    /// callee before it evaluates a single argument, so `undefined(f())` must
    /// not run `f`.
    pub const CALL_NAME_CHECK: u16 = 130;
    /// `[class]` -> `class`. Raises `Class "X" not found` for an undeclared
    /// class. Emitted before the arguments of `new X(...)` and `X::m(...)`,
    /// which both reject an unknown class before evaluating them.
    pub const CALL_CLASS_CHECK: u16 = 131;
    /// `[callee]` -> `callee`. Raises whatever `$callee(...)` was going to raise
    /// for a value that is not callable at all, before the arguments run.
    pub const CALLVALUE_CHECK: u16 = 132;
    /// `[class, method]` -> `class`. The whole of `C::m(…)`'s callee decision —
    /// the class exists, the method is declared, and it is reachable from here —
    /// run before the arguments. Subsumes [`CALL_CLASS_CHECK`] for the static
    /// call form, which is why that op is not also emitted there.
    pub const SCALL_CALLEE_CHECK: u16 = 133;
    /// `[callable, is_instance_form]` -> `callable`. The callee decision of
    /// PHP 8.1 first-class callable syntax, `callee(...)`, made where the syntax
    /// is WRITTEN. The reference refuses an undefined function, an undeclared
    /// class, an undefined or unreachable method and a non-object receiver at
    /// that point — the closure is never built — so none of it can wait for the
    /// call the closure may never receive.
    pub const FCC_CHECK: u16 = 134;
    /// `[class, name]` -> value. `Class::$p` read in `BP_VAR_IS` mode, for
    /// `isset`/`empty`/`??`: an undeclared or unreachable static property reads
    /// as null instead of throwing (`zend_fetch_static_property_address` with
    /// `BP_VAR_IS`). An unknown CLASS still throws, as it does in the reference.
    pub const SPROP_GET_Q: u16 = 135;
    /// `[path, kind]` -> value. `include`/`require` and their `_once` forms:
    /// load, compile and run a file in the current frame (`host::run_include`).
    pub const INCLUDE: u16 = 136;
    /// `[code]` -> value. `eval()`: compile and run a string in the current frame.
    pub const EVAL: u16 = 137;
    /// `[key, name, namespace]` -> null. A function declared anywhere but a
    /// file's top level (inside an `if`, a loop or another function's body):
    /// the reference declares it when the statement RUNS, so until then the
    /// name is undefined, and a second run is a redeclaration. The compiled
    /// definition waits in the function table under `key`, which no PHP name
    /// can spell, until this moves it to its real name.
    pub const DECLARE_FN: u16 = 138;
    /// `[key, name, namespace]` -> null. [`DECLARE_FN`] for a class, interface,
    /// trait or enum declared inside a block.
    pub const DECLARE_CLASS: u16 = 139;
    /// `[class]` -> null. The `final` rules a class declared where it stands
    /// must keep against what it inherits (see [`final_violation`](super::final_violation)), checked
    /// when the declaration RUNS: until then its parent may not be declared.
    /// A broken rule stops the program like [`DECL_FATAL`].
    pub const FINAL_CHECK: u16 = 140;
    /// `[value, mark] -> null`. A `foreach` released its subject (the loop
    /// ended, or a `return` left it). A suspended generator created while the
    /// subject was evaluated (its id is at or past `mark`, see [`GEN_MARK`])
    /// that no variable, element or property holds is destroyed: the `finally`
    /// blocks around the `yield` it is parked at run now, as they do when the
    /// reference frees it.
    pub const GEN_RELEASE: u16 = 141;
    /// `[] -> int`. The id the next generator will get. Taken before a
    /// `foreach` subject is evaluated, for [`GEN_RELEASE`].
    pub const GEN_MARK: u16 = 142;
    /// `[recv, name] -> value`. `$recv->name` fetched FOR WRITING, as the link
    /// of a longer write target (`$recv->name->p = v`). A missing property is
    /// created as null with no `Undefined property` warning — the write after
    /// it is what fails — and a receiver that cannot hold one is an `Error`
    /// (`Attempt to modify property "name" on null`).
    pub const PROP_FETCH_W: u16 = 143;
    /// `[recv-or-class, method, argno, kind] -> null`. [`BYREF_ARG_DIAG`] for a
    /// method or static call, whose callee is only known at run time: the
    /// method is resolved on the receiver (an object, or a class name for
    /// `C::m()`), and the diagnostic is raised only when its parameter at
    /// `argno` is by reference. `kind` is the compiler's verdict on the
    /// argument, as for `BYREF_ARG_DIAG`. A method that does not resolve —
    /// one `__call` answers — takes nothing by reference.
    pub const BYREF_ARG_DIAG_M: u16 = 144;
    /// `[class] -> object`. The first half of `new`: allocate the instance
    /// (property defaults included) BEFORE the constructor arguments are
    /// evaluated, as `ZEND_NEW` does — it decides the `#N` the object gets.
    pub const NEW_ALLOC: u16 = 145;
    /// `[object, args...]` argc=1+n `-> object`. The second half: run the
    /// constructor over the instance [`NEW_ALLOC`] made.
    pub const NEW_INIT: u16 = 146;
    /// `[object, (n,v)...]` argc=1+2k `-> object`. [`NEW_INIT`] with named
    /// arguments.
    pub const NEW_INIT_NAMED: u16 = 147;
    /// `[class, name] -> array`. `Class::$p` fetched to be written through
    /// (`Class::$p[] = v`, `Class::$p[k]++`): its array, vivified when the
    /// property holds none.
    pub const SPROP_ENSURE_ARRAY: u16 = 148;
    /// `[v] -> v`'s iterator. A `foreach` over an `IteratorAggregate` walks the
    /// iterator its `getIterator()` answers; anything else is its own subject.
    pub const FOREACH_ITER: u16 = 149;
    /// `[v] -> Bool`: whether a `foreach` walks `v` one step at a time — a
    /// generator, or an object implementing `Iterator` — rather than as an
    /// array of its elements.
    pub const IS_LAZY_ITER: u16 = 150;

    /// `[object, properties] -> object`. `clone($o, [...])` (PHP 8.5): the copy,
    /// then each entry of the array assigned to it from the calling scope.
    pub const CLONE_WITH: u16 = 151;

    /// `[] -> array`. `$GLOBALS` read as a whole: a copy of the global frame.
    pub const GLOBALS_ARRAY: u16 = 152;
    /// `[name] -> value`. `$GLOBALS[name]` read, with the `Undefined global
    /// variable` warning for a name that is not bound.
    pub const GLOBALS_GET: u16 = 153;
    /// `[name] -> value`. The same read with no diagnostic: the operand of
    /// `isset`, `empty` and `??`.
    pub const GLOBALS_GET_Q: u16 = 154;
    /// `[local, name] -> null`. Bind the running frame's variable `local` to the
    /// global `name` by reference, creating it — the first step of every write
    /// through `$GLOBALS[name]`.
    pub const GLOBALS_BIND: u16 = 155;
    /// `[name] -> null`. `unset($GLOBALS[name])`.
    pub const GLOBALS_UNSET: u16 = 156;
    /// `[name] -> bool`. `isset($GLOBALS[name])`.
    pub const GLOBALS_ISSET: u16 = 157;
    /// `[ns\name, site] -> name`. The function an UNQUALIFIED call written in
    /// a namespace reaches (`ZEND_INIT_NS_FCALL_BY_NAME`): the namespaced one
    /// when it exists, else the global one (with a leading `\`, see
    /// `Dispatch::CompiledInNamespace`), else the namespaced name, which the
    /// call then refuses as undefined. The first answer is kept for `site`.
    pub const NS_FN_NAME: u16 = 158;
    /// `[ns\NAME] -> value`. An unqualified constant written in a namespace:
    /// the namespaced constant, else the global one, else `Error: Undefined
    /// constant "ns\NAME"`.
    pub const CONST_FETCH_NS: u16 = 159;
    /// `[name, interface] -> null`. A class declaration's parent (or, with
    /// `interface` true, an interface it implements or extends) at the point
    /// the declaration runs: autoloaded if it is not declared yet, and
    /// `Error: Class "X" not found` / `Interface "X" not found` if it is
    /// still missing (`zend_do_link_class`).
    pub const CLASS_LINK_CHECK: u16 = 160;
    /// `[class, name] -> Int slot`. The reference cell `Class::$name` is
    /// stored in, made one if it is not already (`&Class::$p`).
    pub const REF_SLOT_SPROP: u16 = 161;
    /// `[class, name, slot] -> value`. Bind `Class::$name` to the cell at
    /// `slot` (`Class::$p = &$x`).
    pub const REF_TO_SPROP: u16 = 162;
    /// `[slot-key] -> bool`. Whether the `static` declared under `slot-key`
    /// is initialized already, so its initializer is skipped.
    pub const STATIC_HAS: u16 = 163;
    /// `[slot, value] -> string`. `$x .= value` for a frame-slot local: the
    /// append happens in the slot's own buffer when it is the sole owner.
    pub const CONCAT_ASSIGN_FSLOT: u16 = 164;
    /// `[slot, value] -> string`. The same for a host-slot variable.
    pub const CONCAT_ASSIGN_SLOT: u16 = 165;
    /// `[name, value] -> string`. The same for a variable reached by name.
    pub const CONCAT_ASSIGN_VAR: u16 = 166;
    /// `[subject, array] -> keys`. The keys a `foreach` over `subject` (already
    /// materialized as `array`) binds; see `host::foreach_keys`.
    pub const FOREACH_KEYS: u16 = 167;
    /// `[v] -> bool`. `PhpHost::is_plain_foreach_object`.
    pub const IS_PLAIN_OBJECT: u16 = 168;
    /// `[val, recv, name, container] -> val`. Store `container` back into
    /// `$recv->name` when it is a string: the end of `$o->p[k] = v` on a string
    /// property, whose offset write produced a new string rather than writing
    /// through a handle.
    pub const PROP_DIM_WRITEBACK: u16 = 169;
    /// `[val, class, name, container] -> val`. The same for `Class::$p[k] = v`.
    pub const SPROP_DIM_WRITEBACK: u16 = 170;
}

/// The capture name a `static` closure carries from its creation site.
///
/// `$` cannot begin a PHP variable name, so this can never collide with one the
/// program wrote, and it travels through the ordinary capture list rather than
/// needing an operand of its own.
pub const STATIC_CLOSURE_CAPTURE: &str = "@static-closure";

/// The key operand of an array-literal element that has NO key — `[1, 2]`
/// rather than `[0 => 1, 1 => 2]`.
///
/// It cannot be `Value::Undef`: that is how PHP `null` rides through the VM, so
/// `[null => "a"]` and `["a"]` would compile to the same operand sequence.
/// They did, and the first one produced the integer key `0` instead of `""`.
/// `Value::Status` carries no PHP value at all, so no expression can forge it.
pub const AUTO_INDEX: Value = Value::Status(i32::MIN);

/// The key operand marking a `...` SPREAD element in an array literal.
///
/// A spread contributes a whole array's worth of entries rather than one, so it
/// cannot ride as a `key => value` pair. Like [`AUTO_INDEX`] it is a
/// `Value::Status`, which carries no PHP value, so no expression can forge it —
/// and it is a DIFFERENT status, so `[...$a]` and `[$a]` stay distinguishable.
pub const SPREAD_KEY: Value = Value::Status(i32::MIN + 1);

/// `array_filter()` `$mode`: the callback is handed the KEY alone.
pub const ARRAY_FILTER_USE_KEY: i64 = 2;
/// `array_filter()` `$mode`: the callback is handed the VALUE and the key.
pub const ARRAY_FILTER_USE_BOTH: i64 = 1;

/// Whether a key operand is the [`AUTO_INDEX`] marker rather than a real key.
pub fn is_auto_index(v: &Value) -> bool {
    matches!(v, Value::Status(n) if *n == i32::MIN)
}

/// Whether a key operand is the [`SPREAD_KEY`] marker.
pub fn is_spread_key(v: &Value) -> bool {
    matches!(v, Value::Status(n) if *n == i32::MIN + 1)
}

/// The largest number of `key => value` pairs one `MKARRAY`/`MKARRAY_ADD` can
/// carry, bounded by `CallBuiltin`'s `u8` operand count (`MKARRAY_ADD` spends
/// one of the 255 on the array itself).
pub const MKARRAY_CHUNK_PAIRS: usize = 127;

/// Sub-ops for the by-reference array mutators lowered through `ops::ARR_MUT`
/// (`array_push`/`array_pop`/`array_shift`/`array_unshift`/`array_splice`). These
/// take the array by variable name so the host can rewrite the bound array (and
/// auto-vivify an unset variable) the way PHP's by-reference parameter does.
pub mod arrmut {
    pub const PUSH: i64 = 0;
    pub const POP: i64 = 1;
    pub const SHIFT: i64 = 2;
    pub const UNSHIFT: i64 = 3;
    pub const SPLICE: i64 = 4;
}

/// A compiled formal parameter: its name, an optional default-value chunk (run in
/// the callee scope when the caller omits the argument), and whether it collects
/// all trailing arguments into an array (`...$rest`).
#[derive(Debug, Clone)]
pub struct Param {
    pub name: String,
    /// The line the parameter is DECLARED on — where PHP reports a coercion
    /// `Deprecated` against, rather than the call site.
    pub line: u32,
    pub default: Option<Chunk>,
    /// The default as the reference prints it when it keeps the expression
    /// rather than a value (see `crate::ast_export`); `None` for one it folds.
    pub default_text: Option<String>,
    pub variadic: bool,
    /// `&$x` — a by-reference parameter (final value copied back to the caller).
    pub by_ref: bool,
    /// The declared type, carried from the source so the bind can check it.
    pub ty: Option<TypeHint>,
    /// Its `#[…]` attributes.
    pub attrs: Vec<AttrDef>,
}

/// A compiled `#[Name(args)]` attribute: its name, and a chunk that builds its
/// argument array (positional entries, then the named ones under their
/// names). Reflection runs the chunk when asked for the arguments.
#[derive(Debug, Clone)]
pub struct AttrDef {
    pub name: String,
    pub args: Chunk,
}

/// A compiled user function: its parameters plus the lowered body chunk.
#[derive(Debug, Clone)]
pub struct FuncDef {
    /// Declared `function &f()`.
    pub returns_ref: bool,
    /// Where it was written; `None` for an internal function.
    pub span: Option<DeclSpan>,
    pub params: Vec<Param>,
    pub chunk: Chunk,
    /// The frame's variables in the order the chunk numbers them. The call
    /// reserves these slots before binding anything, so slot `n` in the body and
    /// slot `n` in the frame are the same variable. Empty when the body was
    /// compiled to address its variables by name.
    pub locals: Vec<String>,
    /// True when the body contains a `yield`: calling it builds a suspended
    /// `Generator` (a host-side stackful coroutine) instead of running the body.
    pub is_generator: bool,
    /// The declared return type, checked on the way out of the call.
    pub ret: Option<TypeHint>,
    /// Set only for a closure body: the site of the literal that declared it,
    /// which is what names its stack frames. `None` for a named function or
    /// method, whose frame is named by the function itself.
    pub closure_site: Option<DeclSite>,
    /// A named function's name as DECLARED, which is how its frames and its
    /// argument errors name it however a call spells it: `function Foo()`
    /// called as `FOO()` is `Foo()` in a trace. `None` for methods and closures.
    pub declared: Option<String>,
    /// Its `#[\Deprecated]` attribute: calling it raises the deprecation first.
    pub deprecated: Option<crate::ast::Deprecation>,
    /// The body's `static` declarations: each slot key, and a chunk for a
    /// constant initializer (the value before the declaration first runs).
    /// Empty for a closure, whose objects carry their own.
    pub statics: Vec<(String, Option<Chunk>)>,
    /// Its doc comment, for `getDocComment()`.
    pub doc: Option<String>,
    /// Its `#[…]` attributes.
    pub attrs: Vec<AttrDef>,
}

/// Where a closure literal was WRITTEN, which is what PHP 8.4 names a closure
/// frame by: `{closure:<where>:<line>}`.
///
/// `<where>` is the enclosing declaration — `K::m()` for a method, `outer()` for
/// a function, the script path at the top level — and it nests, so a closure
/// inside a closure reads `{closure:{closure:f.php:2}:3}`. The path is only
/// known once the script is running, so the top level stays a marker until then.
/// `Closure::bind` changes none of it: the site is the literal's, not the call's.
#[derive(Debug, Clone, PartialEq, Default)]
pub enum DeclSite {
    /// Written at the top level of the script; PHP prints the file path. This is
    /// the default because it is where lowering starts.
    #[default]
    Script,
    /// Written inside a named function or method, spelled as PHP spells it in
    /// this position: `outer()`, `K::m()`.
    Named(String),
    /// Written inside another closure literal, itself declared at that site and
    /// on that line.
    Closure(Box<DeclSite>, u32),
}

impl DeclSite {
    /// The `<where>` half of a `{closure:<where>:<line>}` name, with `script`
    /// standing in for the top level.
    pub fn render(&self, script: &str) -> String {
        match self {
            DeclSite::Script => script.to_string(),
            DeclSite::Named(name) => name.clone(),
            DeclSite::Closure(site, line) => {
                format!("{{closure:{}:{line}}}", site.render(script))
            }
        }
    }
}

/// A closure unpacked for a call: its parameters, body chunk, captured bindings,
/// and any `Closure::bind` rebinding (`$this` object + private-access scope class).
pub struct ClosureCall {
    pub params: Vec<Param>,
    pub chunk: Chunk,
    pub captured: Vec<(String, Value)>,
    pub bound_this: Option<Value>,
    pub scope: Option<String>,
    /// The late-static-binding class (`zend_closure.called_scope`).
    pub called_scope: Option<String>,
    pub is_generator: bool,
    /// The declared return type, checked on the way out of the call.
    pub ret: Option<TypeHint>,
    /// The site of the closure literal, carried through so the frame it opens
    /// can be named the way PHP names it. See `DeclSite`.
    pub site: Option<DeclSite>,
}

/// Why a value cannot take an array write as it stands; see
/// [`PhpHost::write_container_issue`].
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum ContainerIssue {
    Scalar,
    FalseToArray,
    /// The container refuses the write: an `Error` with this message.
    Refused(String),
}

/// The operation a write path ends in. A string container accepts only a plain
/// offset assignment (`$s[1] = "x"`); every other operation on it is an
/// `Error` whose message names the operation, as `zend_execute.c` reports it.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum WriteTail {
    /// `$a[k] = v`.
    Set,
    /// `$a[] = v`, `$a[] = &$x`, and a mid-path `$a[][k]`.
    Append,
    /// `$a[k] op= v`.
    AssignOp,
    /// `$a[k]++`, `--$a[k]`.
    IncDec,
    /// `&$a[k]`, including a by-reference argument.
    Ref,
}

impl WriteTail {
    /// The `Error` a string container raises for this operation, or `None` when
    /// the string takes it (only a plain offset assignment).
    fn string_refusal(self) -> Option<&'static str> {
        match self {
            WriteTail::Set => None,
            WriteTail::Append => Some("[] operator not supported for strings"),
            WriteTail::AssignOp => Some("Cannot use assign-op operators with string offsets"),
            WriteTail::IncDec => Some("Cannot increment/decrement string offsets"),
            WriteTail::Ref => Some("Cannot create references to/from string offsets"),
        }
    }
}

/// A string met before the last container of a write path: its offset would
/// have to be written into as an array.
const STRING_OFFSET_AS_ARRAY: &str = "Cannot use string offset as an array";

/// A compiled class: its parent (for single-inheritance resolution), constant and
/// property-default initializers (each an expression chunk that leaves its value
/// on the stack), and its methods keyed by lowercase name. `self::`/`parent::`
/// were resolved to concrete class names at compile time.
#[derive(Debug, Clone)]
pub struct ClassDef {
    /// `readonly class`.
    pub is_readonly: bool,
    /// A backed enum's backing type (`int` / `string`).
    pub enum_backing: Option<String>,
    /// Each property default the reference keeps as an expression, exported
    /// (see `crate::ast_export`).
    pub default_texts: Vec<(String, String)>,
    /// Where it was written; `None` for an internal class.
    pub span: Option<DeclSpan>,
    /// The doc comments of the declaration and its members — see
    /// `ClassDecl::docs`.
    pub docs: Vec<(String, String)>,
    /// The attributes of the declaration and its properties, constants and
    /// enum cases, keyed as `docs` is (a method's are on its `FuncDef`).
    pub attrs: Vec<(String, Vec<AttrDef>)>,
    /// The `#[\Deprecated]` of each constant and enum case that carries one,
    /// keyed `const:NAME` / `case:NAME` as `attrs` is.
    pub member_deprecated: Vec<(String, crate::ast::Deprecation)>,
    /// The class name in its declared spelling. The map is keyed by the
    /// lowercased name (PHP class lookup is case-insensitive), so this is the
    /// only place the source casing survives for display — a stack trace names
    /// the class that DEFINED the method, which may have no live instance to
    /// read the spelling back from.
    pub name: String,
    pub parent: Option<String>,
    /// Implemented interfaces (lowercased) — for an `interface`, the interfaces it
    /// extends. Consulted by `class_is_a`/`instanceof`/`catch`.
    pub interfaces: Vec<String>,
    pub consts: Vec<(String, Chunk)>,
    /// Declared visibility of the `private`/`protected` constants declared in
    /// THIS class (a trait's are copied in by the class that uses it). A name
    /// absent here is `public`.
    pub const_vis: FxHashMap<String, Visibility>,
    pub prop_defaults: Vec<(String, Chunk)>,
    /// `static` property declarations (`public static $n = 0;`). Kept apart from
    /// `prop_defaults` so they are never copied into an instance; each initializer
    /// runs once, on first access, into the host's per-class static store.
    pub static_prop_defaults: Vec<(String, Chunk)>,
    pub methods: FxHashMap<String, FuncDef>,
    /// Declared visibility of properties declared in THIS class (instance and
    /// static), by property name. Consulted (walking the parent chain) to enforce
    /// `private`/`protected` on external access.
    pub prop_vis: FxHashMap<String, Visibility>,
    /// The write visibility of the asymmetric properties THIS class declares
    /// (`public private(set) int $x`), by property name. A property absent here
    /// is written under its read visibility.
    pub prop_set_vis: FxHashMap<String, Visibility>,
    /// Properties THIS class declares `readonly` (a promoted constructor
    /// parameter counts as a declaration). Looked up along the parent chain, so
    /// the class named in `Cannot modify readonly property C::$p` is the one
    /// that declared it, not the one the write went through.
    pub readonly_props: FxHashSet<String>,
    /// Typed instance properties THIS class declares with no default, by name,
    /// each with its type as `var_dump` spells it. They start UNINITIALIZED —
    /// absent from a new instance — and reading one before a write is an
    /// `Error`. A trait's are copied in by the class that uses it.
    pub uninit_props: FxHashMap<String, String>,
    /// Every typed property THIS class (or a trait it uses) declares, instance
    /// and static, by name — what a write to it is checked against.
    pub prop_types: FxHashMap<String, crate::ast::TypeHint>,
    /// Declared visibility of methods declared in THIS class), by lowercased name.
    pub method_vis: FxHashMap<String, Visibility>,
    /// Methods THIS class declares `static`, by lowercased name. Looked up along
    /// the parent chain by [`PhpHost::method_is_static`], which is what tells a
    /// `["C", "m"]` callable apart from a `[$obj, "m"]` one: the reference
    /// refuses `["C", "m"]` for a non-static `m` with
    /// `non-static method C::m() cannot be called statically`.
    pub static_methods: FxHashSet<String>,
    /// Whether this class is an `enum` (PHP 8.1).
    pub is_enum: bool,
    /// Whether the class is `abstract` or an `interface` — either way, `new` on it
    /// is a fatal error in PHP.
    pub is_abstract: bool,
    pub is_interface: bool,
    /// Whether this entry is a `trait`. Traits share the class table (their
    /// members are seeded into every composing class from it), but they are a
    /// different KIND to the reflection surface: `trait_exists` answers yes and
    /// `class_exists` answers no.
    pub is_trait: bool,
    /// Traits named by `use` in this declaration, in source order and declared
    /// spelling. Kept after the members have been merged, because `class_uses`
    /// reports which traits were composed and the merge itself erases that.
    pub uses: Vec<String>,
    /// Whether the class carries `#[AllowDynamicProperties]`, which opts it — and
    /// everything that extends it — out of the "Creation of dynamic property"
    /// deprecation. Checked by walking the parent chain, because the attribute is
    /// inherited.
    pub allow_dynamic_props: bool,
    /// `enum` cases, in source order: `(case name, optional backing-value chunk)`.
    /// Consulted to build the singleton case instances (`E::Case`, `E::cases()`,
    /// `E::from()`, `E::tryFrom()`).
    pub enum_cases: Vec<(String, Option<Chunk>)>,
    /// Declared `final`: no class may extend it.
    pub is_final: bool,
    /// Methods THIS class (or a trait it uses) declares `final`, by lowercased
    /// name. A `private` one is left out unless it is the constructor: the
    /// reference never checks an override of a private method, since a
    /// private method is not inherited.
    pub final_methods: FxHashSet<String>,
    /// Constants THIS class (or interface, or used trait) declares `final`.
    pub final_consts: FxHashSet<String>,
    /// Properties THIS class (or a used trait) declares `final`.
    pub final_props: FxHashSet<String>,
    /// Every method THIS class declares or takes from a trait, as
    /// `(declared spelling, line of its declaration)`, in declaration order.
    /// A broken `final` rule names the overriding method's spelling and is
    /// reported at its line, and the reference checks the parent's methods in
    /// the order the parent declared them.
    pub method_sites: Vec<(String, u32)>,
    /// Methods THIS class declares without a body: every method of an interface
    /// and the `abstract` ones of an abstract class. A trait's are not recorded.
    /// By lowercased name.
    pub abstract_methods: FxHashSet<String>,
    /// Every property THIS class declares, instance and static, in
    /// declaration order (a promoted one at its constructor's place), then the
    /// ones its traits bring — the order reflection lists them in.
    pub prop_order: Vec<String>,
}

/// One compiled `catch (T1 | T2 [$var]) { body }` clause of a `try`.
#[derive(Debug, Clone)]
pub struct CatchClause {
    pub classes: Vec<String>,
    pub var: Option<String>,
    pub chunk: Chunk,
}

/// A compiled `try`/`catch`/`finally` construct: each body is its own detached
/// chunk, run by the `run_try` orchestrator on the *current* scope (no new frame),
/// so variables are shared with the enclosing code.
#[derive(Debug, Clone)]
pub struct TryDef {
    pub try_chunk: Chunk,
    pub catches: Vec<CatchClause>,
    pub finally_chunk: Option<Chunk>,
}

/// A PHP array key — always an integer or a string (bool/float/null keys are
/// normalized to one of these on insert, as PHP does).
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub enum ArrayKey {
    Int(i64),
    Str(String),
}

impl ArrayKey {
    /// The key as a PHP `Value` (for `array_keys`/`foreach`).
    fn to_value(&self) -> Value {
        match self {
            ArrayKey::Int(n) => Value::int(*n),
            ArrayKey::Str(s) => Value::str(s.clone()),
        }
    }
}

/// `nNextFreeElement` of an array no integer key has touched. PHP 8.3 starts it
/// at `ZEND_LONG_MIN` rather than 0, so the first integer key stored — a
/// negative one included — decides the next: `[-3 => 'a']` appends at `-2`. An
/// append onto an untouched array still lands on 0.
pub const NO_INDEX: i64 = i64::MIN;

/// The key an append would use given `next_index`.
fn next_key(next_index: i64) -> i64 {
    if next_index == NO_INDEX {
        0
    } else {
        next_index
    }
}

/// Claim the append key and advance `next_index` past it (saturating at
/// `PHP_INT_MAX`).
fn take_next_index(next_index: &mut i64) -> i64 {
    let k = next_key(*next_index);
    *next_index = k.saturating_add(1);
    k
}

/// A heap object. Arrays and closures live here in the scaffold; user-defined
/// objects are a later wave.
#[derive(Debug, Clone)]
pub enum PhpObj {
    Array {
        entries: IndexMap<ArrayKey, Value>,
        /// The next integer key an append (`$a[] = ...`) will use, or [`NO_INDEX`]
        /// while no integer key has been stored.
        next_index: i64,
    },
    /// A first-class callable: its parameters, its lowered body chunk, and the
    /// `(name, value)` bindings captured (by value) at creation time. `bound_this`
    /// / `scope` are set by `Closure::bind`/`bindTo`/`call`: the bound `$this`
    /// object and the class name whose private/protected members the body may reach.
    Closure {
        params: Vec<Param>,
        /// Boxed to keep the `Closure` variant from dominating `PhpObj`'s size
        /// (a `Chunk` is ~300 bytes; boxing it shrinks the enum to a handle).
        chunk: Box<Chunk>,
        captured: Vec<(String, Value)>,
        bound_this: Option<Value>,
        scope: Option<String>,
        /// The late-static-binding class its frames run with — what `static::`
        /// and `get_called_class()` answer inside it (`zend_closure.called_scope`).
        /// Set where the closure is created or rebound, never at the call.
        called_scope: Option<String>,
        is_generator: bool,
        /// The declared return type, checked on the way out of a call.
        ret: Option<TypeHint>,
        /// Declared `static`, so no instance may ever be bound to it — at
        /// creation, and by `Closure::bind`/`bindTo` afterwards.
        is_static: bool,
        /// Where the literal was written, which names the frames it opens. It
        /// travels with the closure VALUE, so `Closure::bind` cannot move it.
        site: Option<DeclSite>,
        /// The literal's doc comment, for `getDocComment()`.
        doc: Option<String>,
        /// The literal's `#[…]` attributes.
        attrs: Vec<AttrDef>,
        /// Where the literal was written, for `getFileName()` and the lines.
        span: Option<DeclSpan>,
    },
    /// A live generator: an index into `PhpHost::generators`, where the suspended
    /// stackful coroutine and its execution context live. Built by calling a
    /// generator function; iterated by `foreach` or the `Generator` methods.
    Generator { id: u32 },
    /// A class instance: its class name and its properties. Referenced by a
    /// `Value::Obj(u32)` handle, so objects have PHP reference semantics (passing
    /// one around shares the same instance).
    Object {
        class: String,
        props: IndexMap<String, Value>,
    },
    /// A handle to a variable's reference cell, which is what `use (&$v)`
    /// captures. It is never a value a PHP script can hold: it is created at
    /// closure creation and consumed when the closure's frame is built, where
    /// it binds the frame's name to the cell instead of storing the handle.
    Ref { slot: usize },
    /// A stream resource (`fopen`, `tmpfile`, `STDIN`, …). A `Value::Obj`
    /// handle, so every copy of the value shares one cursor. Boxed to keep the
    /// variant from dominating `PhpObj`'s size.
    Resource(Box<crate::stdlib::fileres::Stream>),
}

/// A control-flow signal that unwinds out of a function body. `Throw` rides a
/// separate `pending_throw` field (not this enum) so an in-flight exception is
/// never confused with a `return`/`break`/`continue` and survives frame unwinding.
enum Signal {
    Return(Value),
    /// `break n` — the payload is how many loop levels are still to be left,
    /// so a `try` that sits inside fewer loops than that can decrement it and
    /// re-raise for the next frame out.
    Break(u32),
    /// `continue n` — see [`Signal::Break`].
    Continue(u32),
}

/// What one name in a scope is bound to.
#[derive(Default, Clone)]
enum Slot {
    /// Never written, or `unset()`. Distinct from a slot holding PHP `null`
    /// (`Value::Undef`), and the difference is what `isset()` and `compact()`
    /// answer on: `$a = null; compact('a')` yields `['a' => null]`, an unset
    /// `$a` yields `[]`.
    #[default]
    Unset,
    Val(Value),
    /// Bound as a reference (`$b = &$a`): the value lives in a shared
    /// `PhpHost::ref_cells` cell, so every alias observes the others' writes.
    Ref(usize),
}

/// A scope's variables: one slot per name, reachable either by name or by the
/// index the compiler resolved for it.
///
/// Both routes address the SAME slot. That is the point — it is what lets
/// `extract()`, `$$name`, `unset()` and reference bindings stay coherent with
/// compiled slot access instead of needing a second, shadow representation that
/// could drift from this one.
#[derive(Default)]
struct Vars {
    slots: Vec<Slot>,
    index: FxHashMap<String, u32>,
}

impl Vars {
    /// The slot `name` already occupies, if it has one. A slot survives
    /// `unset()` (it just goes back to `Unset`), so an index handed out once
    /// stays valid for the life of the frame.
    fn slot_of(&self, name: &str) -> Option<u32> {
        self.index.get(name).copied()
    }

    /// The slot for `name`, reserving one if this is the first mention.
    fn ensure_slot(&mut self, name: &str) -> u32 {
        if let Some(&i) = self.index.get(name) {
            return i;
        }
        let i = self.slots.len() as u32;
        self.slots.push(Slot::Unset);
        self.index.insert(name.to_string(), i);
        i
    }

    fn at(&self, i: u32) -> &Slot {
        self.slots.get(i as usize).unwrap_or(&Slot::Unset)
    }

    fn put(&mut self, i: u32, v: Slot) {
        // Grow rather than drop: a frame that was not seeded from a compiled
        // slot order still behaves self-consistently instead of silently losing
        // the write.
        if self.slots.len() <= i as usize {
            self.slots.resize(i as usize + 1, Slot::Unset);
        }
        self.slots[i as usize] = v;
    }

    fn get(&self, name: &str) -> &Slot {
        match self.slot_of(name) {
            Some(i) => self.at(i),
            None => &Slot::Unset,
        }
    }

    /// Every bound name and its slot, unbound ones skipped — what
    /// `get_defined_vars()`, `compact()` and the debugger's locals view walk.
    /// Renumber so `names[i]` occupies slot `i`, keeping whatever each name is
    /// already bound to and keeping every other binding as well.
    ///
    /// The global frame has its superglobals bound before the main chunk is
    /// loaded, so simply reserving in order would push the chunk's own names
    /// past them and every index it was compiled with would be wrong.
    fn renumber(&mut self, names: &[String]) {
        let mut slots: Vec<Slot> = Vec::with_capacity(names.len() + self.slots.len());
        let mut index: FxHashMap<String, u32> = FxHashMap::default();
        for n in names {
            let cur = self.get(n).clone();
            index.insert(n.clone(), slots.len() as u32);
            slots.push(cur);
        }
        for (k, &i) in self.index.iter() {
            if index.contains_key(k) {
                continue;
            }
            let v = self.slots.get(i as usize).cloned().unwrap_or(Slot::Unset);
            index.insert(k.clone(), slots.len() as u32);
            slots.push(v);
        }
        self.slots = slots;
        self.index = index;
    }

    fn iter(&self) -> impl Iterator<Item = (&String, &Slot)> {
        self.index
            .iter()
            .filter_map(|(k, &i)| match self.slots.get(i as usize) {
                Some(Slot::Unset) | None => None,
                Some(s) => Some((k, s)),
            })
    }
}

/// One variable scope (the global scope, or a function-call frame).
#[derive(Default)]
struct Scope {
    vars: Vars,
    /// The source line this frame is currently executing. Under `--dap` every
    /// statement updates it; on the normal path only the ops that can start a
    /// call or raise a throw do (see `builtins::mark_frame_line`), which is all
    /// an exception backtrace needs — the frame's line at the moment it called
    /// deeper is exactly the call-site line PHP prints for the frame above it.
    line: u32,
    /// The function name for a call frame, `None` for the global scope. Reported
    /// as the frame name in a DAP `stackTrace`.
    name: Option<String>,
    /// The late-static-binding class of this frame — the class the call named,
    /// which `static::` resolves to. `None` outside a method call.
    static_class: Option<String>,
    /// Set only for a closure frame: where the literal was written, which is
    /// what PHP 8.4 names the frame by. Kept apart from `name`, which encodes
    /// the private-access scope and is parsed back for visibility checks.
    closure_site: Option<DeclSite>,
    /// Set for a frame that stands in for a LIBRARY function rather than PHP
    /// source. A trace prints the call site of the frame ABOVE such a frame as
    /// `[internal function]`, because there is no PHP line to name.
    internal: bool,
    /// The file this frame's code was compiled from, when it is not the main
    /// script: a function declared in an `include`d file, or a closure written
    /// in `eval()`'d code. Taken from the body chunk's `source` at the call.
    /// `None` for the main script's code — and, for an `internal` frame, "the
    /// caller's", which is where its diagnostics point.
    file: Option<std::sync::Arc<str>>,
    /// A frame the reference has no counterpart for: an iterator method a
    /// `foreach` runs where the reference walks the object through its
    /// native iterator. A trace leaves it out, and what it calls is reported
    /// as called from where IT was called.
    transparent: bool,
}

/// The name of the variadic parameter of the closure a first-class callable
/// expression (`f(...)`) lowers to, which marks its frame as transparent.
pub const FCC_PARAM: &str = "__fcc_args";

/// One running `include`/`require`/`eval`: the code it loaded executes on the
/// frame at `depth` (an index into the scope stack), so a stack trace shows it
/// as a frame of its own between that one and whatever it calls.
/// One frame of a stack trace in the structured form `Exception::getTrace()` and
/// `debug_backtrace()` report: where it was called from, what was called, and
/// with what.
#[derive(Clone)]
struct TraceEntry {
    /// The calling file and line, `None` when internal code made the call.
    site: Option<(String, u32)>,
    function: String,
    /// The declared class name and the call type (`->` or `::`) of a method.
    class: Option<(String, &'static str)>,
    args: Vec<(Option<String>, Value)>,
    /// `$this` of a method frame, for `debug_backtrace()`'s `object` key.
    object: Option<Value>,
    /// An `include`/`require`/`eval` frame, whose `args` precede its `function`.
    include: bool,
}

struct IncludeFrame {
    depth: usize,
    /// The file the loaded code came from — for `eval`, the synthetic
    /// `<file>(<line>) : eval()'d code` name the reference gives it.
    file: std::sync::Arc<str>,
    /// The construct a trace names the frame after: `include`, `require_once`,
    /// `eval`, ….
    construct: &'static str,
    /// The file the construct was handed, `None` for `eval`.
    arg: Option<String>,
    /// The line of the construct in the code that reached it.
    call_line: u32,
}

/// Where a user declaration was written: its file and the lines of its
/// keyword and closing brace — what `getFileName()`, `getStartLine()` and
/// `getEndLine()` answer. Internal (prelude) declarations have none.
#[derive(Debug, Clone, PartialEq)]
pub struct DeclSpan {
    pub file: String,
    pub start: u32,
    pub end: u32,
}

/// Where a user function or class was declared — what a redeclaration quotes
/// as `(previously declared in <file>:<line>)`.
#[derive(Debug, Clone)]
pub struct DeclAt {
    pub file: String,
    pub line: u32,
    /// The namespace it was declared in. phplang resolves names flat, so a
    /// same-named declaration in ANOTHER namespace is not a redeclaration and
    /// is not diagnosed as one.
    pub namespace: String,
}

/// The PHP runtime state for one thread.
pub struct PhpHost {
    objs: Vec<PhpObj>,
    scopes: Vec<Scope>,
    functions: FxHashMap<String, FuncDef>,
    /// Declaration sites of the user functions, by lowercased name. A name
    /// with no entry and a definition is not the program's own.
    fn_sites: FxHashMap<String, DeclAt>,
    /// Declaration sites of the user classes, by lowercased name.
    class_sites: FxHashMap<String, DeclAt>,
    /// User-declared classes, keyed by lowercase class name.
    classes: FxHashMap<String, ClassDef>,
    /// The class table's keys in the order they entered it — the order
    /// `get_declared_classes` and its siblings list them in, as the
    /// reference's `EG(class_table)` keeps insertion order.
    class_order: Vec<String>,
    /// When `Some`, `echo` appends here instead of writing to stdout (used by
    /// `eval_capture` and the test harness).
    capture: Option<String>,
    /// When `Some`, the program's standard input: `STDIN`, `php://stdin` and
    /// `readline` read from here instead of the process's fd 0 (set by
    /// [`crate::embed::run_program`]).
    pub(crate) stdin_feed: Option<std::io::Cursor<Vec<u8>>>,
    /// The output-buffering state of `main/output.c` — see
    /// [`crate::stdlib::output`]. When a handler is on the stack, `echo` appends
    /// to the top one instead of the capture buffer / stdout.
    pub(crate) ob: crate::stdlib::output::OutputState,
    error: Option<String>,
    signal: Option<Signal>,
    /// The in-flight exception object, if a `throw` has fired and not yet been
    /// caught. Kept apart from `signal` so it survives function-frame unwinding
    /// and bubbles through nested VMs on its own.
    pending_throw: Option<Value>,
    /// What `__debugInfo` answered for each object handle in the value a
    /// `var_dump` / `print_r` is rendering, collected before the render starts
    /// (calling PHP code needs the host unborrowed). Empty outside a dump.
    pub(crate) debug_info: FxHashMap<u32, Value>,
    /// What `__serialize` answered for each object handle in the value a
    /// `serialize` is rendering, collected the same way. Empty outside one.
    pub(crate) serialize_info: FxHashMap<u32, Value>,
    /// The status an `exit`/`die` asked the process to end with, once one has
    /// run. Like [`PhpHost::pending_throw`] it unwinds every frame, but it is
    /// NOT catchable and does NOT run a `finally` — PHP ends the request where
    /// the `exit` stands. Kept in its own field precisely so `catch (Throwable)`
    /// cannot see it.
    pub(crate) pending_exit: Option<i32>,
    /// `set_exception_handler`'s stack: the last entry is the live handler,
    /// `Value::Undef` one that `set_exception_handler(null)` pushed.
    exception_handlers: Vec<Value>,
    /// Whether an uncaught exception may reach [`PhpHost::exception_handlers`]
    /// at all — not under `php -r` (see [`run_main`]).
    exception_handler_enabled: bool,
    /// `set_error_handler`'s stack of `(callback, error_levels)`; the last
    /// entry is live, a `Value::Undef` callback one `set_error_handler(null)`
    /// pushed.
    error_handlers: Vec<(Value, i64)>,
    /// A user error handler is running.
    in_error_handler: bool,
    /// Diagnostics the live handler takes, waiting for [`drain_error_handlers`]:
    /// `(severity, level, message, file, line)`.
    queued_errors: Vec<(String, i64, String, String, u32)>,
    /// `register_shutdown_function`'s queue: each callable with its arguments,
    /// run in order at request end (see [`request_shutdown`]).
    shutdown_fns: Vec<(Value, Vec<Value>)>,
    /// Set while a diagnostic of the request shutdown is written: the
    /// reference has no script position then and names the file `Unknown`.
    unknown_site: bool,
    /// Objects whose `__destruct` the request-end sweep has already run, so
    /// neither pass runs it twice.
    destructed: FxHashSet<u32>,
    /// Set by a fatal error that is not an uncaught exception: the reference
    /// marks every object destructed then, so no `__destruct` runs at request end.
    destructors_off: bool,
    /// The main chunk's globals in first-appearance order, and which of them
    /// live in fusevm frame slots — see [`crate::compiler::Program`].
    main_order: Vec<String>,
    main_promoted: Vec<String>,
    /// The main chunk's fusevm frame slots as the run left them, captured
    /// before its VM is recycled so request shutdown can still see the
    /// promoted globals.
    main_frame_slots: Vec<Value>,
    /// Level of the most recent `break`/`continue` signal — see
    /// [`last_break_level`].
    last_break_level: u32,
    /// Compiled `try`/`catch`/`finally` constructs, indexed by the id the
    /// compiler bakes into each `RUN_TRY` call.
    try_defs: Vec<TryDef>,
    /// Named constants (`PHP_EOL`, `M_PI`, user `define`s), keyed case-sensitively.
    /// Seeded with the standard predefined constants on every fresh host.
    constants: FxHashMap<String, Value>,
    /// The namespaced constants (`A\B\K`) by [`const_key`] — their namespace
    /// part lowercased — to the spelling they were defined under, which is the
    /// key in `constants`. A namespace is case-insensitive and a constant's own
    /// name is not, so `\a\b\K` reaches `A\B\K` and `A\B\k` does not.
    const_ns_keys: FxHashMap<String, String>,
    /// The `spl_autoload_register` chain, in calling order.
    autoloaders: Vec<Value>,
    /// The lowercased classes being autoloaded right now — a loader that asks
    /// for the class it is loading gets `false`, not a recursion
    /// (`EG(in_autoload)`).
    autoloading: FxHashSet<String>,
    /// `spl_autoload_extensions()`: what the default `spl_autoload` appends to
    /// a class name to find its file.
    pub autoload_extensions: String,
    /// The callee each unqualified namespaced call site settled on the first
    /// time it ran, keyed by the site's compiled key — the reference's
    /// per-opline runtime cache slot, which is why a namespaced function
    /// declared AFTER a site first ran does not take it over.
    pub ns_call_sites: FxHashMap<String, Value>,
    /// The next resource number to hand out. Numbers are never reused.
    next_resource_id: i64,
    /// The `include`/`require`/`eval` constructs currently running, innermost
    /// last. Each runs on the frame it was reached from rather than on one of
    /// its own, so it is recorded here instead of on the scope stack.
    includes: Vec<IncludeFrame>,
    /// Every file an `include`/`require` has loaded, resolved, in load order —
    /// `get_included_files()` and what the `_once` forms test against.
    included_files: Vec<String>,
    /// Where the next run-time compilation (`include`, `eval`) starts its
    /// per-compilation counters, so the names it mints never collide with those
    /// of the code already loaded.
    compile_counters: crate::compiler::Counters,
    /// Shared storage cells for reference bindings (`$b = &$a`). A scope's `refs`
    /// map points names at these slots.
    ref_cells: Vec<Value>,
    /// The most recent call's by-reference parameters' final values, indexed by
    /// parameter position — read by the caller's post-call write-back (`BYREF_OUT`).
    byref_out: Vec<Value>,
    /// Which positions of `byref_out` the last call actually took by reference.
    /// The call sites that cannot know their callee statically test this before
    /// writing anything back — see [`PhpHost::byref_out_live`].
    byref_live: Vec<bool>,
    /// Static-property storage, keyed by `"declaringclass::prop"` (lowercased
    /// class). One cell per declaring class is shared by every subclass and every
    /// instance, matching PHP's static-property semantics.
    static_props: FxHashMap<String, Value>,
    /// Persistent slots for function `static $var` locals, keyed by the
    /// compile-time-unique id the compiler bakes into each declaration. The value
    /// is an index into `ref_cells`, so a body's `$var` aliases the cell and its
    /// value survives across calls.
    static_slots: FxHashMap<String, usize>,
    /// The cells of closure `static`s whose (non-constant) initializer has not
    /// run yet: a closure object is created holding one per declaration.
    static_uninit: FxHashSet<usize>,
    /// Singleton `enum` case instances, keyed by `"enumlower::CaseName"`. Built
    /// once on first access so `E::Case === E::Case` holds by object identity.
    enum_case_cache: FxHashMap<String, Value>,
    /// What a diagnostic names as the source: the script path, or
    /// `"Command line code"` for `php -r`. Set once at startup.
    script_name: String,
    /// Whether this compilation unit declared `strict_types=1`.
    ///
    /// Upstream this is PER FILE and read from the CALLER's file, so a strict file
    /// calling a non-strict file's function still checks strictly. This engine has
    /// no `include`, so a run is exactly one file and the two readings coincide;
    /// the flag is therefore whole-program without being unfaithful.
    strict_types: bool,
    /// Nesting depth of `@expr` suppression regions; a warning raised while this
    /// is non-zero is discarded.
    suppress: usize,
    /// The late-static-binding class the next call's frame will take. Set from
    /// the called class name, or — for a forwarding call (`self::`, `parent::`,
    /// `static::`) — from the caller's own, and taken when the frame is pushed.
    pending_lsb: Option<String>,
    /// The closure-literal site the next pushed frame takes, set just before a
    /// closure body is entered. A one-shot like `pending_lsb`, because the frame
    /// is built inside `invoke_with_locals`, which every kind of call shares.
    pending_closure_site: Option<DeclSite>,
    /// Set while a `foreach` calls a method of an iterator the reference
    /// walks natively: the frame that call pushes is `transparent`.
    pending_transparent: bool,
    /// The reference cell the most recent `function &f()` return published, for
    /// the caller's `$r = &f()` to bind. Taken (not just read) by the binding so a
    /// stale slot can never be picked up by a later plain call.
    ret_ref_slot: Option<usize>,
    /// Live generators, indexed by the id a `PhpObj::Generator` handle carries.
    /// Each holds a suspended stackful coroutine plus its swapped-out execution
    /// context (its own call frame, in-flight signal/throw).
    generators: Vec<GenCell>,
    /// Set once a fatal error has been displayed in PHP's own shape (see
    /// [`PhpHost::fatal`]), so the CLI wrapper reports the exit status without
    /// printing the message a second time in its terse `php: …` form.
    fatal_reported: bool,
    /// The `error_reporting` mask: a diagnostic is displayed only when its `E_*`
    /// bit is set here. Seeded from `-d error_reporting=…` (or `E_ALL`), then
    /// writable at run time through `error_reporting()` / `ini_set`.
    error_reporting: i64,
    /// The last diagnostic raised, as `error_get_last()` reports it: `(level,
    /// message, file, line)`. Recorded whether or not it was displayed — `@`
    /// and `error_reporting` hide a diagnostic, they do not un-raise it.
    last_error: Option<(i64, String, String, u32)>,
    /// `ini_set`/`ini_get` string store for settings with no dedicated field, so a
    /// value written by one is read back by the other unchanged.
    ini: FxHashMap<String, String>,
    /// What `preg_last_error()` reports: the outcome of the last `preg_*` call
    /// that reached the regex compiler. Sticky — only another such call rewrites
    /// it, so `preg_last_error()` itself and `preg_quote()` leave it alone.
    preg_error: i64,
    /// `strtok`'s tokenizer position: the subject it was last handed and the byte
    /// offset the next call resumes from. Upstream this is the pair
    /// `BG(strtok_string)` / `BG(strtok_last)`; `None` is upstream's NULL
    /// `strtok_string`, which is both the never-started state and the state
    /// `strtok` resets to when it runs out of tokens.
    strtok_state: Option<(String, usize)>,
    /// The object whose `__clone()` is currently running, if any. A `readonly`
    /// property is writable exactly once, and PHP 8.3 reopens that one write
    /// inside `__clone` so a copy can be given a fresh identity — the only
    /// place a second write to an initialized readonly property is legal.
    cloning: Option<u32>,
    /// The copy a `clone($o, [...])` is currently initialising. Its readonly
    /// properties may be written once more, from the declaring scope, whether or
    /// not they already hold a value.
    cloning_with: Option<u32>,
    /// Per object, the readonly properties that have already taken their one
    /// write. Kept beside the objects rather than in them because "written" is
    /// not a property of the stored value — a readonly property holding null
    /// may be initialized or may never have been assigned, and only these two
    /// states tell the two refusal messages apart.
    readonly_init: FxHashMap<u32, FxHashSet<String>>,
    /// Typed properties `unset()` has removed, per object handle. An
    /// uninitialized typed property is read through `__get` only after an
    /// explicit `unset()`; before its first write it is an `Error` even when the
    /// class has `__get`, which is how lazy-initialization idioms tell the two
    /// apart.
    unset_typed: FxHashMap<u32, FxHashSet<String>>,
    /// Magic property accesses currently on the stack, as
    /// `(object handle, property, magic method)`. PHP does not re-enter a magic
    /// method for a property already being handled by it, which is what lets
    /// `__get($n) { return $this->$n ?? 'd'; }` terminate: the inner `$this->$n`
    /// finds no property, sees `__get` already in progress for that name, and
    /// takes the ordinary undefined-property path instead of recursing.
    magic_in_progress: Vec<(u32, String, &'static str)>,
    /// Classes whose constant or static-property INITIALIZER is running, innermost
    /// last. A constant expression is evaluated in the scope of the class that
    /// declares it, so `const R = self::P + 1;` may read a `private` `P` even when
    /// the first read of `R` comes from outside the class.
    init_class_scope: Vec<String>,
    /// Handles of objects a RENDERER allocated to stage a value (see
    /// [`PhpHost::new_transient_object`]). They are never reachable from PHP, so
    /// [`PhpHost::object_ordinal`] skips them — otherwise a `json_encode` would
    /// silently bump the `#n` that a later `var_dump` prints.
    transient_objs: FxHashSet<u32>,
}

/// What `$obj->m(...)` / `C::m(...)` should do, once the method table and the
/// visibility rules have both been consulted. Returned by
/// [`PhpHost::method_dispatch`], which every call opcode routes through so the
/// instance and static forms cannot disagree about when `__call` fires.
///
/// The order is the reference's, and the interesting arm is `Magic` for a
/// method that IS declared: PHP routes a call it cannot reach to `__call` just
/// as readily as one that does not exist, so a private method plus a `__call`
/// is handled by the magic method rather than reported as an access error.
pub enum MethodDispatch {
    /// Declared (somewhere up the chain) and reachable from here: call it.
    Direct,
    /// Undeclared, or declared but out of reach, and the class supplies the
    /// magic catch-all — call `__call`/`__callStatic` with the argument list.
    Magic,
    /// Declared, out of reach, and no magic catch-all. Carries the reference's
    /// `Call to <vis> method C::m() from <scope>` message.
    Denied(String),
    /// No such method anywhere up the chain and no magic catch-all.
    Undefined,
}

/// What `$obj->name` should do, once the object's property table and the
/// visibility rules have both been consulted. Returned by
/// [`PhpHost::prop_access`], which every property opcode routes through so the
/// four of them cannot disagree about when a magic method fires.
pub enum PropAccess {
    /// The property is present on the object and reachable from here: read,
    /// write or remove the slot directly.
    Direct,
    /// Not directly reachable, and the class defines the magic method for this
    /// operation — call it.
    Magic,
    /// Not reachable, no magic method, and the property IS declared: its
    /// visibility puts it out of reach. Carries the reference's message.
    Denied(String),
    /// Not reachable, no magic method, and no such property. What that means is
    /// the caller's decision: a read warns and yields null, a write creates the
    /// property, an unset does nothing, an `isset` is false.
    Absent,
}

/// Ini settings applied to every host the moment it is created — what `php -d
/// name=value` writes.
///
/// A process-wide cell rather than an argument to `PhpHost::new`: hosts are
/// created implicitly on first `with_host` and thrown away by every
/// `reset_host`, so there is no single call site that could pass them in, and a
/// value set once on a host would not survive the next reset.
static INITIAL_INI: std::sync::Mutex<Vec<(String, String)>> = std::sync::Mutex::new(Vec::new());

/// Record a `-d name=value` override for every host created from now on.
pub fn add_initial_ini(name: &str, value: &str) {
    let mut g = INITIAL_INI.lock().unwrap_or_else(|e| e.into_inner());
    g.retain(|(k, _)| k != name);
    g.push((name.to_string(), value.to_string()));
}

/// The ini settings this engine knows, with the values the reference CLI reports
/// for them when nothing overrides them.
///
/// Every entry here is an ENGINE default — compiled into the interpreter, not
/// read from a php.ini. The distinction is testable rather than a judgement
/// call: `php -n` starts with no ini file at all, so a setting whose value is
/// the same under `php -n` and `php` is one the engine supplies itself, and a
/// setting whose value differs between them came from the file. Only the former
/// are listed, which is why `memory_limit` (`128M`) and `date.timezone` (`UTC`)
/// belong here — both survive `php -n` unchanged — while `variables_order`,
/// `enable_dl` and `short_open_tag`, which do not, are deliberately absent.
///
/// Two further exclusions, for values that are engine defaults but not portable
/// ones: anything whose default is a build path (`extension_dir`,
/// `include_path`, `openssl.cafile`), which encodes the prefix the reference
/// happened to be built with, and settings belonging to optional extensions
/// (`mysqli.*`, `pgsql.*`, `tidy.*`), which a differently configured build does
/// not register at all. What remains is PHP core plus `date` and `pcre` — the
/// two extensions PHP 8 cannot be built without.
fn default_ini() -> FxHashMap<String, String> {
    [
        // The startup mask, also readable through `ini_get`. `-d` and
        // `error_reporting()` both rewrite this entry, so the two views agree.
        ("error_reporting", "30719"),
        // Error handling and output.
        ("precision", "14"),
        ("serialize_precision", "-1"),
        ("display_errors", "1"),
        ("display_startup_errors", "1"),
        ("log_errors", "1"),
        // Empty, not absent: with no php.ini the reference reports `string(0) ""`
        // for `error_log`, which is what sends the `log_errors` copy to stderr.
        ("error_log", ""),
        ("html_errors", "0"),
        ("docref_ext", ""),
        ("error_append_string", ""),
        ("error_prepend_string", ""),
        ("ignore_repeated_errors", "0"),
        ("ignore_repeated_source", "0"),
        ("report_memleaks", "1"),
        ("fatal_error_backtraces", "1"),
        ("output_buffering", "0"),
        ("implicit_flush", "1"),
        ("output_handler", ""),
        // Resource limits. `max_execution_time` and `max_input_time` are the CLI
        // SAPI's own hardcoded overrides, not the common defaults.
        ("memory_limit", "128M"),
        ("max_memory_limit", "-1"),
        ("max_execution_time", "0"),
        ("max_input_time", "-1"),
        ("max_input_nesting_level", "64"),
        ("max_input_vars", "1000"),
        ("post_max_size", "8M"),
        ("default_socket_timeout", "60"),
        ("hard_timeout", "2"),
        ("unserialize_max_depth", "4096"),
        ("unserialize_callback_func", ""),
        // Language and encoding.
        ("default_charset", "UTF-8"),
        ("default_mimetype", "text/html"),
        ("input_encoding", ""),
        ("internal_encoding", ""),
        ("output_encoding", ""),
        ("disable_functions", ""),
        ("expose_php", "1"),
        ("ignore_user_abort", "0"),
        ("register_argc_argv", "0"),
        ("auto_detect_line_endings", "0"),
        ("allow_url_fopen", "1"),
        ("arg_separator.input", "&"),
        ("arg_separator.output", "&"),
        ("user_agent", ""),
        ("from", ""),
        // Zend engine.
        ("zend.assertions", "1"),
        ("zend.enable_gc", "1"),
        ("zend.detect_unicode", "1"),
        ("zend.multibyte", "0"),
        ("zend.exception_ignore_args", "0"),
        ("zend.exception_string_param_max_len", "15"),
        ("zend.script_encoding", ""),
        // ext/date — always built.
        ("date.timezone", "UTC"),
        ("date.default_latitude", "31.7667"),
        ("date.default_longitude", "35.2333"),
        ("date.sunrise_zenith", "90.833333"),
        ("date.sunset_zenith", "90.833333"),
        // ext/pcre — always built.
        ("pcre.backtrack_limit", "1000000"),
        ("pcre.recursion_limit", "100000"),
        ("pcre.jit", "1"),
    ]
    .iter()
    .map(|(k, v)| (k.to_string(), v.to_string()))
    .collect()
}

/// Settings from [`default_ini`] that `ini_set` cannot change at run time —
/// PHP's `PHP_INI_PERDIR`/`PHP_INI_SYSTEM` entries, which only a php.ini or a
/// `-d` may set. `ini_set` reports `false` for these and changes nothing, while
/// `ini_get` still reads them.
///
/// Determined by running `ini_set($name, ini_get($name))` on the reference for
/// every name in `default_ini` and recording which ones answered `false` —
/// writing a setting its OWN current value can only fail because the write
/// itself is refused.
const INI_FIXED: &[&str] = &[
    "allow_url_fopen",
    "arg_separator.input",
    "disable_functions",
    "expose_php",
    "hard_timeout",
    "max_input_nesting_level",
    "max_input_time",
    "max_input_vars",
    "max_memory_limit",
    "output_buffering",
    "output_handler",
    "post_max_size",
    "register_argc_argv",
    "zend.multibyte",
    "zend.script_encoding",
];

/// The set of array/object handles currently being walked by a recursive
/// renderer, so a structure that contains itself is DETECTED rather than
/// followed forever.
///
/// This is the analogue of the reference's `GC_PROTECT_RECURSION`: the engine
/// flags a hashtable while it is being printed and, on re-entering a flagged
/// one, substitutes a marker. Without it every one of these walkers exhausts
/// the native stack and the process aborts, which no PHP program can catch.
#[derive(Default)]
pub struct Visiting(Vec<u32>);

impl Visiting {
    /// Mark `v` as being walked. `false` means it already was — the caller must
    /// emit its recursion marker instead of descending.
    pub fn enter(&mut self, v: &Value) -> bool {
        let Value::Obj(id) = v else { return true };
        if self.0.contains(id) {
            return false;
        }
        self.0.push(*id);
        true
    }

    /// Finish walking the value the matching [`Visiting::enter`] admitted.
    pub fn leave(&mut self) {
        self.0.pop();
    }
}

/// A string as a stack trace renders it: cut to 15 BYTES, then escaped.
///
/// Port of `smart_str_append_escaped_truncated(…, 15)` plus
/// `smart_str_append_escaped` (`Zend/zend_smart_str.c`). Every byte below 32,
/// the backslash itself, and every byte above 126 is escaped — the named ones
/// (`\n`, `\r`, `\t`, `\f`, `\v`, `\e`, `\\`) by their letter, the rest as
/// `\xHH` with UPPERCASE hex digits. A single quote is NOT escaped, even though
/// the result is wrapped in single quotes.
///
/// The truncation is by byte and happens BEFORE escaping, so a 15-byte cut can
/// land mid-character; the escaping then renders the orphaned bytes as `\xHH`,
/// which is why the reference never emits invalid UTF-8 here.
fn escape_trace_string(s: &str) -> String {
    smart_str_escaped(s, 15)
}

/// `smart_str_append_escaped_truncated(…, limit)`: the first `limit` bytes of
/// `s` escaped (see `escape_trace_string`), then `...` if it was cut.
pub fn smart_str_escaped(s: &str, limit: usize) -> String {
    const VK_ESCAPE: u8 = 0x1b;
    let bytes = &*crate::bytes::to_bytes(s);
    let cut = bytes.len().min(limit);
    let mut out = String::new();
    for &c in &bytes[..cut] {
        if c < 32 || c == b'\\' || c > 126 {
            out.push('\\');
            match c {
                b'\n' => out.push('n'),
                b'\r' => out.push('r'),
                b'\t' => out.push('t'),
                0x0c => out.push('f'),
                0x0b => out.push('v'),
                b'\\' => out.push('\\'),
                VK_ESCAPE => out.push('e'),
                _ => out.push_str(&format!("x{c:02X}")),
            }
        } else {
            out.push(c as char);
        }
    }
    if bytes.len() > limit {
        out.push_str("...");
    }
    out
}

/// The message PHP raises when `$a[] =` cannot pick a key because the array
/// already holds an element at `PHP_INT_MAX`. Thrown as a plain `Error`, so
/// user code can catch it; the reference does not name a function in it.
pub const NEXT_ELEMENT_OCCUPIED: &str =
    "Cannot add element to the array as the next element is already occupied";

// DIVERGENCE — per-setting VALUE validation is not modelled. The reference
// refuses a value a setting will not take, with a `Warning` and `false`:
// `ini_set('date.timezone', '-1')` keeps UTC, `ini_set('memory_limit', '20')`
// keeps 128M. Neither is reproducible here. The timezone check needs a zone
// database this build does not carry (see `stdlib::datetime`, which documents
// the same gap for `date_default_timezone_set`), and `memory_limit`'s refusal
// message quotes the process's CURRENT memory usage in bytes, which is not a
// reproducible number. A write of a value the reference would refuse is
// therefore accepted here rather than guessed at; see the `ini_set` corpus entry.

impl Default for PhpHost {
    fn default() -> Self {
        Self::new()
    }
}

impl PhpHost {
    pub fn new() -> Self {
        let mut h = PhpHost {
            objs: Vec::new(),
            // Start with the global scope already open.
            scopes: vec![Scope::default()],
            functions: FxHashMap::default(),
            fn_sites: FxHashMap::default(),
            class_sites: FxHashMap::default(),
            classes: FxHashMap::default(),
            class_order: Vec::new(),
            capture: None,
            stdin_feed: None,
            error: None,
            signal: None,
            pending_throw: None,
            debug_info: FxHashMap::default(),
            serialize_info: FxHashMap::default(),
            pending_exit: None,
            exception_handlers: Vec::new(),
            error_handlers: Vec::new(),
            in_error_handler: false,
            queued_errors: Vec::new(),
            exception_handler_enabled: true,
            shutdown_fns: Vec::new(),
            unknown_site: false,
            destructed: Default::default(),
            destructors_off: false,
            main_order: Vec::new(),
            main_promoted: Vec::new(),
            main_frame_slots: Vec::new(),
            last_break_level: 1,
            try_defs: Vec::new(),
            constants: predefined_constants(),
            const_ns_keys: FxHashMap::default(),
            autoloaders: Vec::new(),
            autoloading: FxHashSet::default(),
            autoload_extensions: ".inc,.php".to_string(),
            ns_call_sites: FxHashMap::default(),
            next_resource_id: 1,
            includes: Vec::new(),
            included_files: Vec::new(),
            compile_counters: crate::compiler::Counters::default(),
            ob: Default::default(),
            ref_cells: Vec::new(),
            byref_out: Vec::new(),
            byref_live: Vec::new(),
            static_props: FxHashMap::default(),
            static_slots: FxHashMap::default(),
            static_uninit: FxHashSet::default(),
            enum_case_cache: FxHashMap::default(),
            script_name: "Command line code".to_string(),
            suppress: 0,
            pending_lsb: None,
            pending_closure_site: None,
            pending_transparent: false,
            ret_ref_slot: None,
            generators: Vec::new(),
            fatal_reported: false,
            error_reporting: errlevel::E_ALL,
            last_error: None,
            ini: default_ini(),
            preg_error: 0,
            strtok_state: None,
            cloning: None,
            cloning_with: None,
            readonly_init: FxHashMap::default(),
            unset_typed: FxHashMap::default(),
            magic_in_progress: Vec::new(),
            init_class_scope: Vec::new(),
            transient_objs: FxHashSet::default(),
            strict_types: false,
        };
        h.init_superglobals();
        h.init_stdio();
        // `-d` overrides land before the program is read, so a compile-time
        // notice is already tested against the level they set.
        for (name, value) in INITIAL_INI.lock().unwrap_or_else(|e| e.into_inner()).iter() {
            if name == "error_reporting" {
                // The ini path — unlike `ini_set` — runs the constant-expression
                // scanner, so `-d 'error_reporting=E_ALL & ~E_NOTICE'` works. The
                // RESOLVED number is what `ini_get` reports back afterwards.
                h.set_error_reporting(crate::errlevel::parse_ini_level(value).unwrap_or(0));
            } else {
                // Unlike `ini_set`, `-d` may introduce a name the engine has no
                // default for — the reference CLI registers it either way.
                h.ini.insert(name.clone(), value.clone());
            }
        }
        h
    }

    /// The last call's by-reference parameter value at `pos` (for the caller's
    /// write-back); `Undef` if out of range.
    pub fn byref_out_get(&self, pos: usize) -> Value {
        self.byref_out.get(pos).cloned().unwrap_or(Value::Undef)
    }

    /// Whether the call that just returned had a by-reference parameter at `pos`.
    ///
    /// A call site that cannot know the callee statically — a method call, whose
    /// receiver's class is a run-time fact, or `$f(…)` on a variable holding any
    /// callable — asks this before writing anything back. Without it a write-back
    /// emitted "just in case" would store the previous call's leftovers, or a
    /// null, into a variable the callee never took by reference.
    pub fn byref_out_live(&self, pos: usize) -> bool {
        self.byref_live.get(pos).copied().unwrap_or(false)
    }

    /// Record which of a returning call's parameters were by-reference, and their
    /// final values. Positions the callee did not take by reference are cleared,
    /// so nothing survives from the call before.
    pub fn byref_out_set(&mut self, vals: Vec<Value>, live: Vec<bool>) {
        self.byref_out = vals;
        self.byref_live = live;
    }

    /// Publish one by-reference OUT value from a *builtin* — `preg_match`'s
    /// `$matches`, `parse_str`'s result array. A user function fills the whole
    /// vector from its frame when it returns; a builtin has no frame, so it
    /// names the position it is writing.
    pub fn byref_out_put(&mut self, pos: usize, v: Value) {
        if self.byref_out.len() <= pos {
            self.byref_out.resize(pos + 1, Value::Undef);
            self.byref_live.resize(pos + 1, false);
        }
        self.byref_out[pos] = v;
        self.byref_live[pos] = true;
    }

    /// Drop the previous call's OUT values, so a call that writes none cannot
    /// be read as having written the one before it.
    pub fn byref_out_clear(&mut self) {
        self.byref_out.clear();
        self.byref_live.clear();
    }

    /// Seed the superglobal arrays in the global scope: `$_ENV`/`$_SERVER` from
    /// the real process environment, empty `$_GET`/`$_POST`/… request arrays, and
    /// `$argv`/`$argc`. Visible from every scope via `is_superglobal` resolution.
    /// The CLI's standard streams: `STDIN`, `STDOUT` and `STDERR` are
    /// resources 1, 2 and 3. The reference's first stream a script opens is 5,
    /// so the counter resumes there.
    fn init_stdio(&mut self) {
        use crate::stdlib::fileres::{Stream, StreamKind};
        for (name, kind) in [
            ("STDIN", StreamKind::Stdin),
            ("STDOUT", StreamKind::Stdout),
            ("STDERR", StreamKind::Stderr),
        ] {
            let v = self.new_stream(Stream::stdio(kind));
            self.constants.insert(name.to_string(), v);
        }
        self.next_resource_id = 5;
    }

    fn init_superglobals(&mut self) {
        let env: Vec<(String, String)> = std::env::vars().collect();
        let mkenv = |h: &mut PhpHost| -> Value {
            let a = h.new_array();
            for (k, v) in &env {
                h.arr_set_key(&a, &Value::str(k.clone()), Value::str(v.clone()));
            }
            a
        };
        let e = mkenv(self);
        self.set_var("_ENV", e);
        let s = mkenv(self);
        // A few conventional `$_SERVER` entries on top of the environment.
        self.arr_set_key(&s, &Value::str("PHP_SELF"), Value::str(String::new()));
        self.arr_set_key(&s, &Value::str("SCRIPT_NAME"), Value::str(String::new()));
        // Empty rather than a stand-in name when there is no file, which is what
        // the reference reports for `php -r` and for a script on stdin.
        self.arr_set_key(
            &s,
            &Value::str("SCRIPT_FILENAME"),
            Value::str(String::new()),
        );
        let now = std::time::SystemTime::now()
            .duration_since(std::time::UNIX_EPOCH)
            .map_or(0, |d| d.as_secs() as i64);
        self.arr_set_key(&s, &Value::str("REQUEST_TIME"), Value::int(now));
        self.set_var("_SERVER", s);
        for name in [
            "_GET", "_POST", "_REQUEST", "_COOKIE", "_FILES", "_SESSION", "GLOBALS",
        ] {
            let empty = self.new_array();
            self.set_var(name, empty);
        }
        let argv = self.new_array();
        self.arr_push_auto(&argv, Value::str(String::new()));
        self.set_var("argv", argv);
        self.set_var("argc", Value::int(1));
    }

    /// Publish the command line into the superglobals the reference fills from
    /// it: `$argv`/`$argc`, their `$_SERVER` copies, and the three `$_SERVER`
    /// names that report the script.
    ///
    /// `script` is the `FILE` argument VERBATIM, not the resolved path
    /// [`PhpHost::script_name`] carries — the reference keeps the two apart, so
    /// `php sub/s.php` has `$argv[0] === "sub/s.php"` while `__FILE__` is the
    /// absolute path. Code with no file reports `Standard input code`, and it
    /// reports that for `php -r` too, where `__FILE__` says `Command line code`
    /// instead; the two names genuinely disagree there.
    ///
    /// `SCRIPT_FILENAME` is the one that does NOT fall back to a stand-in name:
    /// with no file (`file` is `None`) it stays the empty string.
    pub fn set_script_args(&mut self, file: Option<&str>, args: &[String]) {
        let script = file.unwrap_or("Standard input code").to_string();
        let argv = self.new_array();
        self.arr_push_auto(&argv, Value::str(script.clone()));
        for a in args {
            self.arr_push_auto(&argv, Value::str(a.clone()));
        }
        let argc = Value::int(args.len() as i64 + 1);
        self.set_var("argv", argv.clone());
        self.set_var("argc", argc.clone());
        let server = self.get_var("_SERVER");
        for (k, v) in [
            ("argv", argv),
            ("argc", argc),
            ("PHP_SELF", Value::str(script.clone())),
            ("SCRIPT_NAME", Value::str(script)),
            (
                "SCRIPT_FILENAME",
                Value::str(file.unwrap_or_default().to_string()),
            ),
        ] {
            self.arr_set_key(&server, &Value::str(k.to_string()), v);
        }
    }

    /// A snapshot of all defined constants as `(name, value)` pairs — for
    /// `get_defined_constants`.
    pub fn all_constants(&self) -> Vec<(String, Value)> {
        self.constants
            .iter()
            .map(|(k, v)| (k.clone(), v.clone()))
            .collect()
    }

    /// `get_declared_classes` / `_interfaces` / `_traits`
    /// (`get_declared_class_impl`): every declared name of one kind, in declared
    /// spelling and declaration order. The engine types with no `ClassDef` come
    /// first, standing for the reference's internal classes; the prelude's own
    /// helpers (a `__` prefix) are not PHP's and are left out. An enum is listed
    /// as a class.
    pub fn declared_names(&self, kind: TypeKind) -> Vec<String> {
        let want = |k: TypeKind| match kind {
            TypeKind::Class => matches!(k, TypeKind::Class | TypeKind::Enum),
            other => k == other,
        };
        let mut out: Vec<String> = BUILTIN_TYPES
            .iter()
            .filter(|(_, k, ..)| want(*k))
            .map(|(n, ..)| n.to_string())
            .collect();
        for key in &self.class_order {
            let Some(def) = self.classes.get(key) else {
                continue;
            };
            if def.name.starts_with("__") && def.name != "__PHP_Incomplete_Class" {
                continue;
            }
            if self.type_kind(key).is_some_and(want) {
                out.push(def.name.clone());
            }
        }
        out
    }

    /// Insert into the class table, recording a new key's place in
    /// [`PhpHost::declared_names`]' order.
    fn class_insert(&mut self, key: String, def: ClassDef) {
        if !self.classes.contains_key(&key) {
            self.class_order.push(key.clone());
        }
        self.classes.insert(key, def);
    }

    // ── constants ───────────────────────────────────────────────────────────

    /// `constant(name)` / a bare constant reference, or `None` when no constant
    /// of that name is defined.
    ///
    /// An undefined one is an `Error` in PHP 8, not the bare name as a string —
    /// that leniency was PHP 7's and was removed. Raising it needs a throw, which
    /// this cannot do, so both callers turn the `None` into one themselves.
    pub fn const_fetch(&self, name: &str) -> Option<Value> {
        // `zend_get_constant_str_impl`: a leading `\` names the global
        // namespace, and a name the table lacks may still be one of the three
        // special constants, matched case-insensitively (`zend_get_special_const`).
        let name = name.strip_prefix('\\').unwrap_or(name);
        self.constants
            .get(name)
            .or_else(|| {
                let written = self.const_ns_keys.get(&const_key(name))?;
                self.constants.get(written)
            })
            .cloned()
            .or_else(|| match name.to_ascii_lowercase().as_str() {
                "true" => Some(Value::bool(true)),
                "false" => Some(Value::bool(false)),
                "null" => Some(Value::Undef),
                _ => None,
            })
    }

    /// [`Self::const_fetch`] for a USE of the constant — a bareword or
    /// `constant()` — which raises the deprecation an internal constant
    /// registered `#[\Deprecated]` carries (`zend_deprecated_constant`, reached
    /// from `zend_get_class_constant_ex` and `zend_quick_get_constant`).
    /// `defined()` is not a use and stays silent, as in the reference.
    pub fn const_use(&mut self, name: &str) -> Option<Value> {
        let value = self.const_fetch(name)?;
        let name = name.strip_prefix('\\').unwrap_or(name);
        if let Some(suffix) = deprecated_constant_suffix(name) {
            self.deprecated(format!("Constant {name} is deprecated {suffix}"));
        }
        Some(value)
    }

    /// Whether a constant of this name is defined.
    pub fn const_defined(&self, name: &str) -> bool {
        self.const_fetch(name).is_some()
    }

    /// Defines a constant, returning `true` unless it was already defined — PHP
    /// does not redefine, and the FIRST value is the one that survives.
    ///
    /// A redefinition is not silent: it warns. Both spellings that reach here
    /// (`define()` and the `const` declaration) warn identically, which is why
    /// the warning lives at this single write point rather than at either call
    /// site.
    pub fn const_define(&mut self, name: &str, value: Value) -> bool {
        let key = const_key(name);
        if self.constants.contains_key(name) || self.const_ns_keys.contains_key(&key) {
            // The reference names the constant by its registered key, whose
            // namespace part is lowercased.
            self.warn(format!(
                "Constant {key} already defined, this will be an error in PHP 9"
            ));
            return false;
        }
        if key.contains('\\') {
            self.const_ns_keys.insert(key, name.to_string());
        }
        self.constants.insert(name.to_string(), value);
        true
    }

    // ── program loading ────────────────────────────────────────────────────

    /// Install a compiled program's user functions onto the host.
    pub fn load_program(&mut self, functions: Vec<(String, FuncDef)>) {
        for (name, def) in functions {
            self.functions.insert(name, def);
        }
    }

    /// Install a compiled program's classes onto the host.
    pub fn load_classes(&mut self, classes: Vec<(String, ClassDef)>) {
        for (name, def) in classes {
            self.class_insert(name, def);
        }
    }

    /// Record where a loaded program's functions and classes were declared:
    /// `(lowercased name, line, namespace)` each, all in `file`.
    pub fn record_decl_sites(
        &mut self,
        file: &str,
        fns: Vec<(String, u32, String)>,
        classes: Vec<(String, u32, String)>,
    ) {
        let at = |line, namespace| DeclAt {
            file: file.to_string(),
            line,
            namespace,
        };
        for (name, line, ns) in fns {
            self.fn_sites.insert(name, at(line, ns));
        }
        for (name, line, ns) in classes {
            self.class_sites.insert(name, at(line, ns));
        }
    }

    /// The body of the reference's `Cannot redeclare function …` for declaring
    /// `name` in namespace `ns`, or `None` when the name is free.
    ///
    /// A user function is quoted with the site it was declared at; a library
    /// function — or one of the PHP-written prelude's — has none to quote. A
    /// namespaced declaration never collides with a library function, which
    /// lives in the global namespace.
    pub fn fn_redeclare_msg(&self, name: &str, ns: &str) -> Option<String> {
        let lname = name.to_ascii_lowercase();
        match self.fn_sites.get(&lname) {
            Some(at) if at.namespace == ns => Some(format!(
                "Cannot redeclare function {name}() (previously declared in {}:{})",
                at.file, at.line
            )),
            Some(_) => None,
            None if ns.is_empty()
                && (self.functions.contains_key(&lname)
                    || crate::stdlib::callable::is_library_function(&lname)) =>
            {
                Some(format!("Cannot redeclare function {name}()"))
            }
            None => None,
        }
    }

    /// The body of the reference's `Cannot redeclare <kind> <Name> …` for
    /// declaring a class, interface, trait or enum called `name` in namespace
    /// `ns`, or `None` when the name is free. The message names the type that
    /// is ALREADY declared — its kind and its spelling — not the new one.
    pub fn class_redeclare_msg(&self, name: &str, ns: &str) -> Option<String> {
        let lname = name.to_ascii_lowercase();
        if let Some(at) = self.class_sites.get(&lname) {
            if at.namespace != ns {
                return None;
            }
            let def = self.classes.get(&lname)?;
            return Some(format!(
                "Cannot redeclare {} {} (previously declared in {}:{})",
                class_kind_word(def),
                def.name,
                at.file,
                at.line
            ));
        }
        if !ns.is_empty() {
            return None;
        }
        if let Some(def) = self.classes.get(&lname) {
            return Some(format!(
                "Cannot redeclare {} {}",
                class_kind_word(def),
                def.name
            ));
        }
        if let Some((spelled, kind)) = crate::prelude_type(&lname) {
            return Some(format!("Cannot redeclare {kind} {spelled}"));
        }
        builtin_type(&lname).map(|(spelled, kind, ..)| {
            format!("Cannot redeclare {} {spelled}", type_kind_word(*kind))
        })
    }

    /// Declare a function whose definition was compiled under `key` (see
    /// `ops::DECLARE_FN`) as `name`, at `line` of the running file. `Err` is the
    /// redeclaration the reference refuses.
    pub fn declare_fn_at(
        &mut self,
        key: &str,
        name: &str,
        ns: &str,
        line: u32,
    ) -> Result<(), String> {
        if let Some(msg) = self.fn_redeclare_msg(name, ns) {
            return Err(msg);
        }
        let lname = name.to_ascii_lowercase();
        if let Some(def) = self.functions.remove(key) {
            self.functions.insert(lname.clone(), def);
        }
        let file = self.current_file().to_string();
        self.fn_sites.insert(
            lname,
            DeclAt {
                file,
                line,
                namespace: ns.to_string(),
            },
        );
        Ok(())
    }

    /// The `final` rule the class `class` breaks against the classes it
    /// inherits from, if any — see [`final_violation`].
    pub fn final_check(&self, class: &str) -> Option<(String, Option<u32>)> {
        let def = self.classes.get(&class.to_ascii_lowercase())?;
        final_violation(def, |n| self.classes.get(&n.to_ascii_lowercase()))
    }

    /// [`Self::final_check`] for `def` while it is still being compiled: it and
    /// the types compiled alongside it are not in the class table yet, so
    /// `local` is asked first.
    pub fn final_check_with<'a>(
        &'a self,
        def: &ClassDef,
        local: impl Fn(&str) -> Option<&'a ClassDef>,
    ) -> Option<(String, Option<u32>)> {
        final_violation(def, |n| {
            local(n).or_else(|| self.classes.get(&n.to_ascii_lowercase()))
        })
    }

    /// [`Self::declare_fn_at`] for a class compiled under `key`.
    pub fn declare_class_at(&mut self, key: &str, ns: &str, line: u32) -> Result<(), String> {
        let Some(def) = self.classes.get(key) else {
            return Ok(());
        };
        let name = def.name.clone();
        if let Some(msg) = self.class_redeclare_msg(&name, ns) {
            return Err(msg);
        }
        let lname = name.to_ascii_lowercase();
        if let Some(def) = self.classes.remove(key) {
            self.class_order.retain(|k| k != key);
            self.class_insert(lname.clone(), def);
        }
        let file = self.current_file().to_string();
        self.class_sites.insert(
            lname,
            DeclAt {
                file,
                line,
                namespace: ns.to_string(),
            },
        );
        Ok(())
    }

    /// Install a program's compiled `try`/`catch`/`finally` table. The `RUN_TRY`
    /// ids the main chunk carries are offsets into this vector.
    pub fn load_try_defs(&mut self, defs: Vec<TryDef>) {
        self.try_defs = defs;
    }

    /// Whether the object class `class` is caught by `catch (target)`: the same
    /// class, a subclass (walking the parent chain), or `target == Throwable`
    /// (the interface both exception roots implement). Names are compared
    /// case-insensitively, matching PHP and the lowercased class table.
    pub fn catch_matches(&self, class: &str, target: &str) -> bool {
        let target = target.to_ascii_lowercase();
        if target == "throwable" {
            return self.class_is_a(class, "exception") || self.class_is_a(class, "error");
        }
        self.class_is_a(class, &target)
    }

    /// Whether `class` is `ancestor` or descends from it, walking the compiled
    /// class table's parent chain. `ancestor` must already be lowercased.
    /// `PhpHost::class_is_a` for callers outside this module (the call opcodes
    /// need it to decide whether a static call's `$this` belongs to the class).
    pub fn class_is_a_pub(&self, class: &str, ancestor: &str) -> bool {
        self.class_is_a(class, &ancestor.to_ascii_lowercase())
    }

    fn class_is_a(&self, class: &str, ancestor: &str) -> bool {
        // `Stringable` is implemented AUTOMATICALLY by any class with a
        // `__toString`, whether or not it names the interface (PHP 8.0). No other
        // interface behaves this way, so it is answered before the walk.
        if ancestor == "stringable" && self.class_has_method(class, "__tostring") {
            return true;
        }
        // Traverse the parent chain AND implemented/extended interfaces
        // (transitively). A visited set + bound guards against malformed cycles.
        let mut stack = vec![class.to_ascii_lowercase()];
        let mut seen: Vec<String> = Vec::new();
        while let Some(c) = stack.pop() {
            if c == ancestor {
                return true;
            }
            if seen.contains(&c) || seen.len() > 1000 {
                continue;
            }
            seen.push(c.clone());
            if let Some(d) = self.classes.get(&c) {
                if let Some(p) = &d.parent {
                    stack.push(p.to_ascii_lowercase());
                }
                for i in &d.interfaces {
                    stack.push(i.to_ascii_lowercase());
                }
            } else {
                // Not in the class table: it may still be one of the engine's
                // own types, whose ancestry lives in `BUILTIN_TYPES` — that is
                // what makes `$gen instanceof Traversable` true.
                stack.extend(
                    builtin_type_parents(&c)
                        .iter()
                        .map(|p| p.to_ascii_lowercase()),
                );
            }
        }
        false
    }

    // ── output capture ─────────────────────────────────────────────────────

    pub fn begin_capture(&mut self) {
        self.capture = Some(String::new());
    }

    pub fn end_capture(&mut self) -> String {
        self.capture.take().unwrap_or_default()
    }

    /// Serve `input` as the program's standard input in place of fd 0.
    pub fn set_stdin(&mut self, input: &str) {
        self.stdin_feed = Some(std::io::Cursor::new(
            crate::bytes::to_bytes(input).into_owned(),
        ));
    }

    /// Write to the process's stderr, first flushing what stdout holds. The
    /// reference's CLI writes each stdout chunk as it is produced, so a line
    /// still sitting in Rust's line buffer would otherwise land AFTER a later
    /// diagnostic or `fwrite(STDERR, …)` on a shared `2>&1` stream.
    pub fn write_err(bytes: &[u8]) {
        use std::io::Write;
        let _ = std::io::stdout().flush();
        let _ = std::io::stderr().write_all(bytes);
    }

    /// A buffered write — `php_output_write`: into the output-buffering stack
    /// when a handler is active (see `crate::stdlib::output`), else the
    /// capture buffer if active, else stdout.
    pub fn write_out(&mut self, s: &str) {
        let top = self.ob.handlers.len();
        self.ob_write_below(top, s);
    }

    /// `php_output_op(PHP_OUTPUT_HANDLER_WRITE)` entered at the handler BELOW
    /// stack index `n` — `n` is the stack length for an ordinary write, and a
    /// handler's own index when its output is passed down. A disabled handler
    /// passes the data on; the first enabled one stores it, and one whose
    /// `chunk_size` it fills is due to run its handler, which happens at the
    /// next builtin boundary (`output::drain_chunks`) because a user handler
    /// is PHP code and this runs inside the host borrow.
    pub(crate) fn ob_write_below(&mut self, n: usize, s: &str) {
        if s.is_empty() {
            return;
        }
        let running = self.ob.running.is_some();
        for i in (0..n).rev() {
            let h = &mut self.ob.handlers[i];
            if h.is_disabled() {
                continue;
            }
            if running {
                h.flags |= crate::stdlib::output::PRODUCED_OUTPUT;
            }
            h.append(s);
            if h.chunk_full() && !running {
                self.ob.chunk_due = Some(h.id);
                crate::stdlib::output::mark_chunk_due();
            }
            return;
        }
        self.write_unbuffered(s);
    }

    /// Past every output handler: the capture buffer, or stdout.
    fn write_unbuffered(&mut self, s: &str) {
        if let Some(buf) = &mut self.capture {
            buf.push_str(s);
        } else {
            use std::io::Write;
            let mut out = std::io::stdout();
            let _ = out.write_all(&crate::bytes::to_bytes(s));
        }
    }

    // ── errors ─────────────────────────────────────────────────────────────

    // ── diagnostics ────────────────────────────────────────────────────────

    /// Name this run's source for diagnostics: the script path, or
    /// `"Command line code"` for `php -r` (which is the default).
    pub fn set_script_name(&mut self, name: impl Into<String>) {
        self.script_name = name.into();
        // A script that is a FILE is the first of `get_included_files()`, and
        // an `include_once` of it is a repeat.
        self.included_files = if self.script_name.starts_with('/') {
            vec![self.script_name.clone()]
        } else {
            Vec::new()
        };
    }

    /// What a diagnostic names as the source — the script path, or
    /// `"Command line code"` for `php -r`.
    pub fn script_name(&self) -> &str {
        &self.script_name
    }

    /// The file the code at scope `i` belongs to, ignoring any include running
    /// on it: the frame's own file, the caller's for an internal frame, the
    /// main script's otherwise.
    fn scope_file(&self, i: usize) -> &str {
        let scope = &self.scopes[i];
        if scope.internal && i > 0 {
            return self.scope_file(i - 1);
        }
        scope.file.as_deref().unwrap_or(&self.script_name)
    }

    /// The file the code running now belongs to — what a diagnostic, an
    /// exception's `getFile()`, `__FILE__` and `__DIR__` name. The innermost
    /// `include`/`eval` on the current frame wins over the frame's own file.
    pub fn current_file(&self) -> &str {
        if self.unknown_site {
            return "Unknown";
        }
        let top = self.scopes.len() - 1;
        match self.includes.last() {
            Some(f) if f.depth == top => &f.file,
            _ => {
                // An internal frame pushed over an include inherits it too.
                let mut i = top;
                while self.scopes[i].internal && i > 0 {
                    i -= 1;
                }
                match self.includes.last() {
                    Some(f) if f.depth == i => &f.file,
                    _ => self.scope_file(top),
                }
            }
        }
    }

    /// The line the innermost frame is executing — where an exception created
    /// right now records itself as having been raised, and the call site the
    /// frame below it reports in a backtrace.
    pub fn cur_frame_line(&self) -> u32 {
        self.scopes.last().map(|s| s.line).unwrap_or(0)
    }

    /// Enter/leave an `@expr` suppression region.
    pub fn suppress_push(&mut self) {
        self.suppress += 1;
    }

    pub fn suppress_pop(&mut self) {
        self.suppress = self.suppress.saturating_sub(1);
    }

    /// The open `@expr` region count, for a caller that has to put it back.
    pub fn suppress_depth(&self) -> usize {
        self.suppress
    }

    /// Force the region count back to `depth` — see the call in
    /// [`run_try_orchestrator`], the one place an `@expr` can be abandoned
    /// part-way through.
    pub fn suppress_restore(&mut self, depth: usize) {
        self.suppress = depth;
    }

    /// Emit a PHP `Warning` on the output stream.
    ///
    /// With the CLI defaults (`display_errors=STDOUT`, `html_errors=Off`) the
    /// reference interpreter writes exactly
    /// `"\nWarning: {msg} in {file} on line {n}\n"` to stdout, interleaved with
    /// the script's own output — so it goes through `write_out` and lands in an
    /// output buffer or a capture just as `echo` does. The copy `log_errors`
    /// sends to stderr is not reproduced: nothing observes it.
    pub fn warn(&mut self, msg: impl std::fmt::Display) {
        self.diagnose("Warning", errlevel::E_WARNING, warn_line(), msg);
    }

    /// Record whether this run declared `strict_types=1`. Set once, from the
    /// parse, before the program runs.
    pub fn set_strict_types(&mut self, on: bool) {
        self.strict_types = on;
    }

    /// Whether this run declared `strict_types=1`.
    pub fn strict_types(&self) -> bool {
        self.strict_types
    }

    /// How a value's type reads in a `TypeError`: PHP names the four scalars and
    /// `null` in lower case, an array as `array`, and an object by its CLASS.
    ///
    /// This is NOT [`crate::stdlib::types::debug_type`], and the two disagree on
    /// exactly one type. A diagnostic names a boolean by its VALUE — `true given`
    /// / `false given` — while `get_debug_type()` answers `"bool"` for both. PHP
    /// keeps them in separate functions (`zend_zval_value_name` against
    /// `zend_zval_get_legacy_type`/`get_debug_type`) for that reason, so any
    /// message of the form `… , <type> given` has to come from HERE. Reaching for
    /// `debug_type` because it is the public one silently writes `bool given`,
    /// which no PHP has ever printed.
    pub(crate) fn type_name_for_error(&self, v: &Value) -> String {
        match v {
            Value::Undef => "null".to_string(),
            Value::Bool(b) => if *b { "true" } else { "false" }.to_string(),
            Value::Int(_) => "int".to_string(),
            Value::Float(_) => "float".to_string(),
            Value::Str(_) => "string".to_string(),
            Value::Obj(_) => match self.as_array(v) {
                Some(PhpObj::Array { .. }) => "array".to_string(),
                Some(PhpObj::Resource(_)) => "resource".to_string(),
                _ => self
                    .instance_class(v)
                    .map(|c| display_class(&c).to_string())
                    .unwrap_or_else(|| "object".to_string()),
            },
            _ => "mixed".to_string(),
        }
    }

    /// A `Warning` about a specific source line rather than the one the runtime
    /// last touched — used for the compile-time diagnostics, which are issued
    /// before any statement has run and so have no "current" line to borrow.
    pub fn warn_at(&mut self, msg: impl std::fmt::Display, line: u32) {
        self.diagnose("Warning", errlevel::E_WARNING, line, msg);
    }

    /// Emit a PHP `Deprecated` diagnostic. Same stream and shape as `warn`,
    /// which is the only thing that distinguishes the severities on output.
    pub fn deprecated(&mut self, msg: impl std::fmt::Display) {
        self.diagnose("Deprecated", errlevel::E_DEPRECATED, warn_line(), msg);
    }

    /// `(int) $v` / `intval($v)`: the explicit int cast, with the reference's
    /// diagnostic for a double no `int` can hold.
    ///
    /// PHP 8.1 added `The float %s is not representable as an int, cast
    /// occurred` for exactly the inputs [`dval_to_lval`] has to wrap — anything
    /// outside ±2^63, plus NAN and the infinities. A merely FRACTIONAL double
    /// is silent here; its `Implicit conversion … loses precision` deprecation
    /// belongs to the implicit contexts (array offsets, parameter binds), not
    /// to a cast the program asked for.
    pub fn to_int_cast(&mut self, v: &Value) -> i64 {
        // Only a FLOAT operand wraps. A numeric STRING written in float form
        // goes through `ZEND_STRTOL`, which saturates and says nothing:
        // `(int) 1e19` is -8446744073709551616 but `(int) "1e19"` is
        // `PHP_INT_MAX`.
        let Value::Float(f) = *v else {
            return self.to_number(v).to_int();
        };
        const TWO_63: f64 = 9223372036854775808.0;
        if !f.is_finite() || !(-TWO_63..TWO_63).contains(&f) {
            let shown = crate::stdlib::types::serialize_float(f);
            self.warn(format!(
                "The float {shown} is not representable as an int, cast occurred"
            ));
        }
        dval_to_lval(f)
    }

    /// A double narrowed to `int` by an operator that wraps it (`~`):
    /// `zend_dval_to_lval`, whose `not representable` warning covers NAN, the
    /// infinities and anything outside `i64`, then the `loses precision`
    /// deprecation for NAN and for a fraction in range.
    pub fn dval_to_lval_reported(&mut self, f: f64) -> i64 {
        const TWO_63: f64 = 9223372036854775808.0;
        let l = self.to_int_cast(&Value::Float(f));
        let in_range = (-TWO_63..TWO_63).contains(&f);
        if f.is_nan() || (in_range && f.fract() != 0.0) {
            let shown = crate::stdlib::types::serialize_float(f);
            self.deprecated(format!(
                "Implicit conversion from float {shown} to int loses precision"
            ));
        }
        l
    }

    /// The diagnostic PHP issues when a value is USED as an array offset.
    ///
    /// Every offset use reports — reads and writes, and `isset`/`empty`/`unset`
    /// /`array_key_exists` alike — because the report is about the conversion,
    /// not about whether the element was found. Only two operand shapes convert
    /// lossily: `null` (which becomes `""`, not `0`) and a double that is
    /// fractional or too large for an `int`.
    ///
    /// It lives beside the mutating offset entry points rather than inside
    /// `norm_key`, which also normalizes keys taken back OUT of an
    /// array during a rebuild — those are already `Int`/`Str` and fall through
    /// here, so a rebuild cannot re-report the float key the array was built
    /// with. A STRING receiver is left alone: its own diagnostic is the
    /// differently worded `String offset cast occurred`.
    pub fn diagnose_offset(&mut self, key: &Value) {
        const TWO_63: f64 = 9223372036854775808.0;
        match key {
            Value::Undef => self.deprecated(
                "Using null as an array offset is deprecated, use an empty string instead",
            ),
            Value::Float(f) => {
                let (f, shown) = (*f, crate::stdlib::types::serialize_float(*f));
                if !f.is_finite() || !(-TWO_63..TWO_63).contains(&f) {
                    self.warn(format!(
                        "The float {shown} is not representable as an int, cast occurred"
                    ));
                    // `zend_dval_to_lval_safe`: NAN casts to 0, which it is not
                    // compatible with, so the narrowing is reported as well.
                    if f.is_nan() {
                        self.deprecated(format!(
                            "Implicit conversion from float {shown} to int loses precision"
                        ));
                    }
                } else if f.fract() != 0.0 {
                    self.deprecated(format!(
                        "Implicit conversion from float {shown} to int loses precision"
                    ));
                }
            }
            Value::Obj(_) => {
                if let Some((id, _)) = self.resource_info(key) {
                    self.warn(format!(
                        "Resource ID#{id} used as offset, casting to integer ({id})"
                    ));
                }
            }
            _ => {}
        }
    }

    /// [`PhpHost::diagnose_offset`], but only for a receiver that really is an
    /// array — a string offset reports something else entirely.
    /// The name the reference gives an offset that cannot BE an offset — an
    /// array or an object — or `None` for every key it accepts.
    ///
    /// PHP has no coercion for these two: `$a[[1]]` and `$a[new C]` are a
    /// `TypeError`, not the `Array to string conversion` warning plus a lookup
    /// under the key `"Array"` that this engine used to perform.
    pub fn bad_offset_type(&self, key: &Value) -> Option<String> {
        if self.is_array(key) {
            return Some("array".to_string());
        }
        self.instance_class(key)
            .map(|c| display_class(&c).to_string())
    }

    pub fn diagnose_array_offset(&mut self, recv: &Value, key: &Value) {
        if matches!(self.as_array(recv), Some(PhpObj::Array { .. })) {
            self.diagnose_offset(key);
        }
    }

    /// [`PhpHost::diagnose_offset`] for a QUIET subscript — `isset($x[k])`,
    /// `empty($x[k])`, `$x[k] ?? $d`.
    ///
    /// A string receiver reports here too, and reports the ordinary narrowing
    /// deprecation rather than the `String offset cast occurred` warning its
    /// value-context read emits.
    pub fn diagnose_quiet_offset(&mut self, recv: &Value, key: &Value) {
        if matches!(recv, Value::Str(_)) {
            self.diagnose_offset(key);
            return;
        }
        self.diagnose_array_offset(recv, key);
    }

    /// Whether `arr` holds an element under `key`, by hash lookup on the
    /// NORMALIZED key — which is what `array_key_exists` and `isset` ask.
    ///
    /// `None` for a receiver that is not an array, leaving the caller to handle
    /// an object's properties. The `array_key_exists` builtin used to walk every
    /// pair comparing `to_str` renderings, which is O(n) per call and answers
    /// the wrong question: `array_key_exists(1.7, [1 => "x"])` is true, because
    /// 1.7 narrows to the int key 1, and the string forms `"1.7"` and `"1"`
    /// never match.
    pub fn array_has_key(&mut self, arr: &Value, key: &Value) -> Option<bool> {
        if !matches!(self.as_array(arr), Some(PhpObj::Array { .. })) {
            return None;
        }
        self.diagnose_offset(key);
        let k = self.norm_key(key);
        match self.as_array(arr) {
            Some(PhpObj::Array { entries, .. }) => Some(entries.contains_key(&k)),
            _ => None,
        }
    }

    /// The `<function>(): ` a `php_error_docref` diagnostic raised for the code
    /// running now carries: `f()` for its function, `Unknown` at the top level.
    pub(crate) fn active_function_name(&self) -> String {
        match self.scopes.iter().rev().find(|s| !s.internal) {
            Some(scope) if scope.name.is_some() => {
                format!("{}()", self.trace_frame_name(scope).replace("->", "::"))
            }
            // `php_verror` with no function: the bare word, no parentheses.
            _ => "Unknown".to_string(),
        }
    }

    /// A deprecation raised while the request shuts down: `PHP Request
    /// Shutdown: …` in `Unknown` on line 0.
    pub(crate) fn deprecated_at_shutdown(&mut self, msg: impl std::fmt::Display) {
        self.unknown_site = true;
        self.diagnose(
            "Deprecated",
            errlevel::E_DEPRECATED,
            0,
            format!("PHP Request Shutdown: {msg}"),
        );
        self.unknown_site = false;
    }

    /// Emit a PHP `Notice`.
    pub fn notice(&mut self, msg: impl std::fmt::Display) {
        self.diagnose("Notice", errlevel::E_NOTICE, warn_line(), msg);
    }

    /// The one place a non-fatal diagnostic is rendered, for every severity.
    ///
    /// Two independent gates silence it: `@expr` suppression (lexically scoped,
    /// leaves the level alone) and the `error_reporting` mask (a global the script
    /// or `-d` sets). A diagnostic whose `E_*` bit is clear is not merely hidden —
    /// it is never written, so it cannot land in an output buffer either.
    /// Once past those two gates a diagnostic is written TWICE, to two streams
    /// under two separate ini flags — `display_errors` puts the copy the program
    /// sees on stdout, `log_errors` puts a `PHP `-prefixed copy on stderr. Both
    /// default on, so the ordinary run emits both; `-d log_errors=0` leaves only
    /// the stdout copy and `-d display_errors=0` only the stderr one.
    /// The stderr copy is written FIRST: `php_error_cb` logs before it displays,
    /// which is what an interleaved `2>&1` capture shows.
    pub fn diagnose(&mut self, severity: &str, level: i64, line: u32, msg: impl std::fmt::Display) {
        // Code a PHP-written prelude method runs stands for an internal
        // function, whose diagnostics name the user's call site.
        let line = prelude_call_site_line(self).unwrap_or(line);
        // `set_error_handler`: a level the live handler takes is queued for it
        // instead of displayed — the handler is PHP code, which cannot run
        // inside this borrow, so it runs at the next builtin boundary (see
        // [`drain_error_handlers`]). `@` does not stop it; the handler sees the
        // narrowed `error_reporting()` instead. While a handler runs, its own
        // diagnostics take the default path, as the reference unsets the
        // handler for the duration of the call.
        if !self.in_error_handler && level & errlevel::HANDLEABLE != 0 {
            if let Some((handler, mask)) = self.error_handlers.last() {
                if !matches!(handler, Value::Undef) && mask & level != 0 {
                    let file = self.current_file().to_string();
                    self.queued_errors.push((
                        severity.to_string(),
                        level,
                        msg.to_string(),
                        file,
                        line,
                    ));
                    ERRORS_QUEUED.with(|q| q.set(true));
                    return;
                }
            }
        }
        self.diagnose_default(severity, level, line, msg);
    }

    /// The reference's own error display, with no user handler consulted —
    /// also what a handler that returns `false` falls back to.
    fn diagnose_default(
        &mut self,
        severity: &str,
        level: i64,
        line: u32,
        msg: impl std::fmt::Display,
    ) {
        self.last_error = Some((
            level,
            msg.to_string(),
            self.current_file().to_string(),
            line,
        ));
        if self.suppress > 0 || self.error_reporting & level == 0 {
            return;
        }
        let body = format!("{msg} in {} on line {line}", self.current_file());
        if self.ini_flag("log_errors") {
            Self::write_err(format!("PHP {severity}:  {body}\n").as_bytes());
        }
        if self.ini_flag("display_errors") {
            self.write_out(&format!("\n{severity}: {body}\n"));
        }
    }

    /// Whether an ini setting reads as ON. PHP's ini booleans are stored `"1"` /
    /// `"0"` but a php.ini may spell them `On`/`Off`/`true`/`false`/`yes`/`no`,
    /// and an unset name is off.
    fn ini_flag(&self, name: &str) -> bool {
        match self.ini.get(name).map(|s| s.trim()) {
            None | Some("") | Some("0") => false,
            Some(s) => {
                !(s.eq_ignore_ascii_case("off")
                    || s.eq_ignore_ascii_case("false")
                    || s.eq_ignore_ascii_case("no"))
            }
        }
    }

    /// The current `error_reporting` mask.
    pub fn error_reporting(&self) -> i64 {
        self.error_reporting
    }

    /// What `preg_last_error()` reports.
    pub fn preg_error(&self) -> i64 {
        self.preg_error
    }

    /// Record the outcome of a `preg_*` call that reached the regex compiler.
    /// Called on SUCCESS too, which is what clears a previous failure: in the
    /// reference a pattern that compiles resets the state even when the match
    /// then finds nothing.
    pub fn set_preg_error(&mut self, code: i64) {
        self.preg_error = code;
    }

    /// `strtok`'s saved subject and resume offset, or `None` before the first
    /// two-argument call and after the subject has been exhausted.
    pub fn strtok_state(&self) -> Option<(String, usize)> {
        self.strtok_state.clone()
    }

    /// Install a fresh `strtok` subject (two-argument form) or advance/clear the
    /// resume offset. `None` is the "tokenization finished" state, which is what
    /// makes a later one-argument call answer `false` rather than restart.
    pub fn set_strtok_state(&mut self, state: Option<(String, usize)>) {
        self.strtok_state = state;
    }

    /// `error_get_last()`: an array of the last diagnostic's `type`, `message`,
    /// `file` and `line`, or null when none was raised (or it was cleared).
    pub fn error_get_last(&mut self) -> Value {
        let Some((level, msg, file, line)) = self.last_error.clone() else {
            return Value::Undef;
        };
        let arr = self.new_array();
        for (k, v) in [
            ("type", Value::int(level)),
            ("message", Value::str(msg)),
            ("file", Value::str(file)),
            ("line", Value::int(line as i64)),
        ] {
            self.arr_set_key(&arr, &Value::str(k.to_string()), v);
        }
        arr
    }

    /// `error_clear_last()`.
    pub fn error_clear_last(&mut self) {
        self.last_error = None;
    }

    /// Set the `error_reporting` mask, returning the previous one — what
    /// `error_reporting($level)` reports back. The ini store is updated with the
    /// decimal spelling, so `ini_get('error_reporting')` follows the function.
    pub fn set_error_reporting(&mut self, level: i64) -> i64 {
        self.ini
            .insert("error_reporting".to_string(), level.to_string());
        std::mem::replace(&mut self.error_reporting, level)
    }

    /// Read an ini setting as `ini_get` returns it — always a STRING, even for a
    /// numeric setting. `None` means the name is not a setting this engine knows,
    /// which `ini_get` reports as `false`.
    ///
    /// `error_reporting` is stored here VERBATIM as well as parsed into the mask,
    /// because the two can disagree: `ini_set('error_reporting', '12abc')` leaves
    /// `ini_get` reporting `"12abc"` while the mask becomes 12.
    ///
    /// DIVERGENCE: only the settings `default_ini` lists are known — PHP core
    /// plus `date` and `pcre`. A name belonging to an optional extension the
    /// reference happened to be built with (`mysqli.default_host`), or one whose
    /// default is that build's install prefix (`extension_dir`), reports `false`
    /// rather than a value that would be wrong on another machine.
    pub fn ini_get(&self, name: &str) -> Option<String> {
        self.ini.get(name).cloned()
    }

    /// Write an ini setting, returning the previous value — or `None` for a name
    /// the engine does not know, which `ini_set` reports as `false` without
    /// storing anything (PHP does not let `ini_set` invent settings).
    ///
    /// `error_reporting` also updates the mask, by PHP's ordinary string-to-int
    /// coercion — NOT the constant-expression scanner. `E_ALL & ~E_NOTICE` is a
    /// php.ini spelling, understood on the `-d`/ini path only; handed to
    /// `ini_set` at run time it reads as the integer 0 and mutes everything,
    /// which is exactly what the reference does with it.
    ///
    /// Two more ways a write is refused, both of which report `false` and change
    /// nothing: a setting that is not runtime-changeable (see `INI_FIXED`), and
    /// a value the setting rejects (see `ini_value_rejected`).
    pub fn ini_set(&mut self, name: &str, value: &str) -> Option<String> {
        if INI_FIXED.contains(&name) {
            return None;
        }
        let level = (name == "error_reporting")
            .then(|| self.to_number(&Value::str(value.to_string())).to_int());
        let slot = self.ini.get_mut(name)?;
        let old = std::mem::replace(slot, value.to_string());
        if let Some(level) = level {
            self.error_reporting = level;
        }
        Some(old)
    }

    /// One argument as a stack trace renders it. PHP does not print values in
    /// full here: a string is single-quoted and cut to 15 characters with a
    /// literal `...` inside the quotes, and an array or object collapses to its
    /// kind, so a trace can never be arbitrarily long.
    fn trace_arg(&self, v: &Value) -> String {
        match v {
            Value::Undef => "NULL".to_string(),
            Value::Bool(true) => "true".to_string(),
            Value::Bool(false) => "false".to_string(),
            Value::Str(s) => format!("'{}'", escape_trace_string(s)),
            Value::Obj(_) if self.is_array(v) => "Array".to_string(),
            Value::Obj(_) if self.is_stream(v) => self.to_str(v),
            Value::Obj(_) => match self.instance_class(v) {
                Some(c) => format!("Object({c})"),
                None => "Object(stdClass)".to_string(),
            },
            // A float keeps a fractional part in a trace even when it has none:
            // the reference renders trace arguments with `zero_frac` set, so a
            // whole-valued float reads `1.0` and stays distinguishable from the
            // int `1`. `INF`/`NAN` are left alone.
            Value::Float(f) => {
                let s = self.to_str(v);
                if f.is_finite() && !s.contains('.') && !s.contains('E') {
                    format!("{s}.0")
                } else {
                    s
                }
            }
            other => self.to_str(other),
        }
    }

    /// The `UnhandledMatchError` message for a subject that matched no arm.
    ///
    /// A SCALAR (and `null`) is rendered exactly as a stack trace renders an
    /// argument — `'hi'`, `1.0`, `true`, `NULL` — which is the same
    /// `smart_str_append_scalar` the reference calls for both. Anything else is
    /// `of type <name>`: `array`, `resource`, or the class, since a container
    /// has no rendering short enough for a message.
    pub fn match_error_message(&self, v: &Value) -> String {
        let rendered = match v {
            Value::Undef | Value::Bool(_) | Value::Int(_) | Value::Float(_) | Value::Str(_) => {
                self.trace_arg(v)
            }
            _ if self.is_stream(v) => "of type resource".to_string(),
            _ => format!("of type {}", crate::stdlib::types::debug_type(self, v)),
        };
        format!("Unhandled match case {rendered}")
    }

    /// A frame's name as a trace prints it, in the three parts the structured
    /// trace keeps apart: the class (declared spelling) and the call type `->`
    /// or `::` for a method, and the function. Frames are recorded as
    /// `Class::method` with the class lowercased (that is the lookup key); the
    /// declared spelling comes back off the `ClassDef`, and the arrow form from
    /// whether the frame bound `$this`.
    fn trace_frame_parts(&self, scope: &Scope) -> (Option<(String, &'static str)>, String) {
        let Some(name) = &scope.name else {
            return (None, String::new());
        };
        let call_type = if !matches!(scope.vars.get("this"), Slot::Unset) {
            "->"
        } else {
            "::"
        };
        let declared = |class: &str| {
            self.classes
                .get(class)
                .map(|d| d.name.clone())
                .unwrap_or_else(|| class.to_string())
        };
        // A closure frame is named for where the LITERAL was written, not for
        // anything about the call: `{closure:K::m():4}`, `{closure:outer():16}`,
        // `{closure:/path/to/script.php:14}` at the top level. The bound-scope
        // prefix (`K->` / `K::`) is still the frame name's own, so the two are
        // assembled here rather than baked into one string — `name` is parsed
        // back for visibility checks and must stay `Scope::{closure}`.
        if let Some(site) = &scope.closure_site {
            let rendered = match site {
                DeclSite::Closure(inner, line) => {
                    format!(
                        "{{closure:{}:{line}}}",
                        inner.render(scope.file.as_deref().unwrap_or(&self.script_name))
                    )
                }
                // A closure body always carries a `Closure` site; anything else
                // is a frame that was handed one it should not have.
                other => other.render(scope.file.as_deref().unwrap_or(&self.script_name)),
            };
            return match name.split_once("::") {
                Some((class, _)) => (Some((declared(class), call_type)), rendered),
                None => (None, rendered),
            };
        }
        match name.split_once("::") {
            Some((class, method)) => (Some((declared(class), call_type)), method.to_string()),
            None => (None, name.clone()),
        }
    }

    /// [`trace_frame_parts`](Self::trace_frame_parts) joined: `f`, `A->m`, `A::m`.
    fn trace_frame_name(&self, scope: &Scope) -> String {
        match self.trace_frame_parts(scope) {
            (Some((class, ty)), function) => format!("{class}{ty}{function}"),
            (None, function) => function,
        }
    }

    /// PHP's `Stack trace:` body for the call stack as it stands right now —
    /// captured when an exception object is *created*, which is when PHP snapshots
    /// it, because by the time the throw reaches the top the frames are gone.
    ///
    /// Frame `#i` names the function that was entered and the file/line of the
    /// call that entered it, so its call site is the *caller's* current line. The
    /// global scope is not a frame: it closes the list as `#N {main}`.
    ///
    /// A frame entered from inside a library function (an `array_map` callback,
    /// say) has no PHP call site, so its `file(line)` half is the literal
    /// `[internal function]` — the library frame below it carries the real one.
    /// A closure frame is named the way PHP 8.4 names it — see
    /// `trace_frame_parts`.
    pub fn backtrace(&self) -> String {
        self.render_trace(&self.trace_entries())
    }

    /// The frames of [`PhpHost::backtrace`], innermost first, in the structured
    /// form `Exception::getTrace()` and `debug_backtrace()` report them.
    fn trace_entries(&self) -> Vec<TraceEntry> {
        // The frames outermost first, each with the file and line its code is
        // at: a scope, then any include running on it. A frame's line is where
        // it called deeper — an include's own line for a scope that is running
        // one, the scope's live line otherwise.
        struct Frame<'a> {
            entry: Option<TraceEntry>,
            file: &'a str,
            line: u32,
            internal: bool,
            // A method of a PHP-written prelude class, which stands in for
            // an internal one.
            prelude: bool,
            // A prelude frame that is no method of the class it stands in for: a
            // `__`-named private helper, a helper class, or a closure the
            // prelude wrote. The reference has no frame for any of them.
            helper: bool,
            // A frame the reference has none for at all — see
            // `Scope::transparent` — or a prelude `__call`, which forwards to an
            // inner iterator the way `spl_dual_it_get_method` does.
            transparent: bool,
        }
        let mut frames: Vec<Frame<'_>> = Vec::new();
        for (i, scope) in self.scopes.iter().enumerate() {
            let incs: Vec<&IncludeFrame> = self.includes.iter().filter(|f| f.depth == i).collect();
            let entry = (i > 0).then(|| {
                // A STRING key in a frame's argument array is a named argument
                // the callee could not place — the only way one survives to
                // here — and the reference renders it `name: value`.
                let names = self.frame_arg_names(i);
                let args = self
                    .frame_args(i)
                    .unwrap_or_default()
                    .into_iter()
                    .enumerate()
                    .map(|(j, v)| (names.get(j).cloned().flatten(), v))
                    .collect();
                let (class, function) = self.trace_frame_parts(scope);
                let object = match scope.vars.get("this") {
                    Slot::Unset => None,
                    slot => Some(self.read_slot(&slot.clone())),
                }
                .filter(|_| class.is_some());
                TraceEntry {
                    site: None,
                    function,
                    class,
                    args,
                    object,
                    include: false,
                }
            });
            frames.push(Frame {
                entry,
                file: self.scope_file(i),
                line: incs.first().map_or(scope.line, |f| f.call_line),
                internal: scope.internal,
                prelude: scope.name.as_deref().is_some_and(is_prelude_frame),
                helper: scope.name.as_deref().is_some_and(is_prelude_frame)
                    && (scope.closure_site.is_some()
                        || scope.name.as_deref().is_some_and(is_prelude_helper_name)),
                transparent: scope.transparent
                    || scope
                        .name
                        .as_deref()
                        .is_some_and(|n| is_prelude_frame(n) && n.ends_with("::__call")),
            });
            for (k, inc) in incs.iter().enumerate() {
                frames.push(Frame {
                    entry: Some(TraceEntry {
                        site: None,
                        function: inc.construct.to_string(),
                        class: None,
                        args: inc
                            .arg
                            .clone()
                            .map(|p| (None, Value::str(p)))
                            .into_iter()
                            .collect(),
                        object: None,
                        include: true,
                    }),
                    file: &inc.file,
                    line: incs.get(k + 1).map_or(scope.line, |f| f.call_line),
                    internal: false,
                    prelude: false,
                    helper: false,
                    transparent: false,
                });
            }
        }
        // A prelude method is internal code: what it calls on its own behalf
        // (its helpers, the library functions it uses) is invisible, and a
        // method it calls the way the reference's C calls one — a user
        // `compare()`, an `accept()` — was entered from `[internal function]`.
        // A frame is reported as called from the nearest VISIBLE frame above it.
        let visible: Vec<bool> = (0..frames.len())
            .map(|j| {
                j == 0
                    || !(frames[j].transparent
                        || frames[j - 1].prelude && (frames[j].helper || frames[j].internal))
            })
            .collect();
        let mut out = Vec::new();
        for j in (1..frames.len()).rev() {
            if !visible[j] {
                continue;
            }
            let mut k = j - 1;
            while !visible[k] {
                k -= 1;
            }
            let caller = &frames[k];
            let Some(mut entry) = frames[j].entry.clone() else {
                continue;
            };
            if !(caller.internal || caller.prelude) {
                entry.site = Some((caller.file.to_string(), caller.line));
            }
            out.push(entry);
        }
        out
    }

    /// One argument list as the reference's `_build_trace_args` renders it.
    fn trace_args_string(&self, args: &[(Option<String>, Value)]) -> String {
        args.iter()
            .map(|(name, v)| match name {
                Some(n) => format!("{n}: {}", self.trace_arg(v)),
                None => self.trace_arg(v),
            })
            .collect::<Vec<_>>()
            .join(", ")
    }

    /// The `getTraceAsString()` text of a frame list: `#N file(line): f(args)`,
    /// closed by `#N {main}`.
    fn render_trace(&self, entries: &[TraceEntry]) -> String {
        let mut out = String::new();
        for (n, e) in entries.iter().enumerate() {
            let site = match &e.site {
                Some((file, line)) => format!("{file}({line})"),
                None => "[internal function]".to_string(),
            };
            let name = match &e.class {
                Some((class, ty)) => format!("{class}{ty}{}", e.function),
                None => e.function.clone(),
            };
            let args = self.trace_args_string(&e.args);
            // `eval()` and `include('f')` are frames of their own; PHP prints
            // them as the construct and its argument, never the file's name.
            out.push_str(&format!("#{n} {site}: {name}({args})\n"));
        }
        out.push_str(&format!("#{} {{main}}", entries.len()));
        out
    }

    /// The structured frame list as the PHP array `getTrace()` returns:
    /// `file`, `line`, `function`, `class`, `object`, `type`, `args`, in the
    /// reference's key order. A frame entered from internal code has no `file`
    /// or `line`; an include has `args` before `function`.
    pub fn trace_array(&mut self, provide_object: bool, ignore_args: bool, limit: usize) -> Value {
        let mut entries = self.trace_entries();
        if limit > 0 {
            entries.truncate(limit);
        }
        let out = self.new_array();
        for e in entries {
            let frame = self.new_array();
            if let Some((file, line)) = &e.site {
                self.arr_set_key(&frame, &Value::str("file"), Value::str(file.clone()));
                self.arr_set_key(&frame, &Value::str("line"), Value::int(*line as i64));
            }
            let args_arr = (!ignore_args).then(|| {
                let a = self.new_array();
                for (name, v) in &e.args {
                    match name {
                        Some(n) => self.arr_set_key(&a, &Value::str(n.clone()), v.clone()),
                        None => self.arr_push_auto(&a, v.clone()),
                    }
                }
                a
            });
            if e.include {
                // `eval` takes no argument and carries no `args` key.
                if let Some(a) = args_arr.filter(|_| !e.args.is_empty()) {
                    self.arr_set_key(&frame, &Value::str("args"), a);
                }
                self.arr_set_key(
                    &frame,
                    &Value::str("function"),
                    Value::str(e.function.clone()),
                );
            } else {
                self.arr_set_key(
                    &frame,
                    &Value::str("function"),
                    Value::str(e.function.clone()),
                );
                if let Some((class, ty)) = &e.class {
                    self.arr_set_key(&frame, &Value::str("class"), Value::str(class.clone()));
                    if let (true, Some(obj)) = (provide_object, &e.object) {
                        self.arr_set_key(&frame, &Value::str("object"), obj.clone());
                    }
                    self.arr_set_key(&frame, &Value::str("type"), Value::str(*ty));
                }
                if let Some(a) = args_arr {
                    self.arr_set_key(&frame, &Value::str("args"), a);
                }
            }
            self.arr_push_auto(&out, frame);
        }
        out
    }

    /// The `getTraceAsString()` text of a trace array — what `Exception` keeps
    /// in its `trace` property. Frames the array does not describe well enough
    /// to render (a hand-built one) read as `[internal function]: ()`.
    pub fn render_trace_array(&self, trace: &Value) -> String {
        let key = |f: &Value, k: &str| self.index_get(f, &Value::str(k.to_string()));
        let entries: Vec<TraceEntry> = self
            .array_pairs(trace)
            .unwrap_or_default()
            .into_iter()
            .map(|(_, f)| {
                let file = key(&f, "file");
                let site = match file {
                    Value::Undef => None,
                    file => Some((self.to_str(&file), key(&f, "line").to_int() as u32)),
                };
                let class = match key(&f, "class") {
                    Value::Undef => None,
                    c => Some((
                        self.to_str(&c),
                        if self.to_str(&key(&f, "type")) == "::" {
                            "::"
                        } else {
                            "->"
                        },
                    )),
                };
                let args = match key(&f, "args") {
                    a if self.is_array(&a) => self
                        .array_pairs(&a)
                        .unwrap_or_default()
                        .into_iter()
                        .map(|(k, v)| match k {
                            Value::Str(s) => (Some(s.to_string()), v),
                            _ => (None, v),
                        })
                        .collect(),
                    _ => Vec::new(),
                };
                TraceEntry {
                    site,
                    function: self.to_str(&key(&f, "function")),
                    class,
                    args,
                    object: None,
                    include: false,
                }
            })
            .collect();
        self.render_trace(&entries)
    }

    /// Push a call frame for a LIBRARY function so [`PhpHost::backtrace`] names
    /// it, with `args` as the array the trace renders. The counterpart to
    /// [`PhpHost::pop_internal_frame`]; `throw_from_internal` does the same thing
    /// inline for an exception, and this is the form a diagnostic can use, where
    /// no exception object is built.
    pub fn push_internal_frame(&mut self, func: &str, line: u32, args: Value) {
        self.scopes.push(Scope {
            name: Some(func.to_string()),
            line,
            internal: true,
            ..Scope::default()
        });
        self.set_var("@args", args);
    }

    pub fn pop_internal_frame(&mut self) {
        self.scopes.pop();
    }

    /// Push the `clone(...)` frame of the PHP 8.5 function form, keeping the
    /// CALLER's class as the scope visibility is judged in — the frame itself
    /// belongs to no class. Returns whether a class scope was pushed, for
    /// [`PhpHost::leave_clone_frame`].
    pub fn enter_clone_frame(&mut self, line: u32, args: Value) -> bool {
        let ctx = self.current_class_ctx();
        self.push_internal_frame("clone", line, args);
        match ctx {
            Some(c) => {
                self.init_class_scope.push(c);
                true
            }
            None => false,
        }
    }

    pub fn leave_clone_frame(&mut self, scoped: bool) {
        if scoped {
            self.init_class_scope.pop();
        }
        self.pop_internal_frame();
    }

    /// Frame `idx`'s call arguments as the reference reports them, or `None` if
    /// that frame is not a call frame at all (the global scope has no `@args`).
    ///
    /// A position covered by a declared parameter is reported as that
    /// parameter's CURRENT value, which is why `invoke` stashes the parameter's
    /// NAME there rather than the value passed. Both readers need it and both
    /// must agree: `func_get_args()` answers `[99]` for `function f($a) { $a =
    /// 99; … }` called as `f(1)`, and the stack trace renders that frame as
    /// `f(99)` for the same reason. A frame pushed for a LIBRARY function has
    /// `@args` and no `@argnames`, so every position there falls through to the
    /// value.
    /// The NAME each of frame `idx`'s arguments was passed under, positionally
    /// aligned with [`frame_args`]. `None` for an ordinary positional argument,
    /// which is all of them except a name a variadic library function could not
    /// place — see `throw_from_internal_named`.
    ///
    /// [`frame_args`]: PhpHost::frame_args
    pub(crate) fn frame_arg_names(&self, idx: usize) -> Vec<Option<String>> {
        let args = self.get_var_in(idx, "@args");
        if !self.is_array(&args) {
            return Vec::new();
        }
        self.array_pairs(&args)
            .unwrap_or_default()
            .iter()
            .map(|(k, _)| match k {
                Value::Str(s) => Some(s.to_string()),
                _ => None,
            })
            .collect()
    }

    pub(crate) fn frame_args(&self, idx: usize) -> Option<Vec<Value>> {
        let args = self.get_var_in(idx, "@args");
        if !self.is_array(&args) {
            return None;
        }
        let names = self.get_var_in(idx, "@argnames");
        Some(
            self.array_pairs(&args)
                .unwrap_or_default()
                .iter()
                .enumerate()
                .map(
                    |(pos, (_, v))| match self.index_get(&names, &Value::int(pos as i64)) {
                        Value::Undef => v.clone(),
                        n => self.get_var_in(idx, &self.to_str(&n)),
                    },
                )
                .collect(),
        )
    }

    /// [`PhpHost::frame_args`] for the frame that is executing — what the
    /// `func_get_args` family reports on.
    pub(crate) fn current_frame_args(&self) -> Option<Vec<Value>> {
        self.frame_args(self.scopes.len().checked_sub(1)?)
    }

    /// Read a variable out of a specific frame — the backtrace needs each frame's
    /// own hidden `@args`, not the innermost one's.
    fn get_var_in(&self, idx: usize, name: &str) -> Value {
        self.scopes
            .get(idx)
            .map(|s| s.vars.get(name).clone())
            .map(|s| self.read_slot(&s))
            .unwrap_or(Value::Undef)
    }

    /// Report a fatal error the way the PHP CLI does: the *display* copy goes on
    /// the output stream (so it lands inside an open `ob_start` buffer and
    /// interleaves with the program's own output exactly as the reference does),
    /// and `log_errors` puts a `PHP `-prefixed copy on stderr.
    /// Both copies are gated the way [`PhpHost::diagnose`]'s are, and by the same
    /// three gates — a fatal is not exempt from any of them. `error_reporting`
    /// masks it by its own level (`E_ERROR` for a fatal, `E_PARSE` for a syntax
    /// error), so `php -d error_reporting=0` runs an uncaught exception to
    /// completion in silence; `-d display_errors=0` leaves it on stderr alone
    /// and `-d log_errors=0` on stdout alone. `fatal_reported` is set through
    /// every one of them, because the run failed whether or not anyone was told
    /// and the process still has to exit 255.
    pub fn fatal(&mut self, severity: &str, body: &str) {
        let level = if severity == "Parse error" {
            errlevel::E_PARSE
        } else {
            errlevel::E_ERROR
        };
        if self.error_reporting & level == 0 {
            self.fatal_reported = true;
            return;
        }
        if self.ini_flag("log_errors") {
            Self::write_err(format!("PHP {severity}:  {body}\n").as_bytes());
        }
        if self.ini_flag("display_errors") {
            self.write_out(&format!("\n{severity}: {body}\n"));
        }
        self.fatal_reported = true;
    }

    /// Whether a fatal has already been displayed in PHP's shape, so the CLI
    /// wrapper must not print it again.
    pub fn fatal_reported(&self) -> bool {
        self.fatal_reported
    }

    /// The type name PHP uses inside a diagnostic — the short spelling (`int`,
    /// `bool`), not `gettype`'s (`integer`, `boolean`).
    pub fn diag_type(&self, v: &Value) -> &'static str {
        match v {
            Value::Undef => "null",
            Value::Bool(_) => "bool",
            Value::Int(_) => "int",
            Value::Float(_) => "float",
            Value::Str(_) => "string",
            Value::Obj(_) => {
                if self.is_array(v) {
                    "array"
                } else {
                    "object"
                }
            }
            _ => "null",
        }
    }

    /// The type name `Trying to access array offset on …` uses. It differs from
    /// [`diag_type`] for booleans, which it spells as the literal `true`/`false`.
    fn diag_offset_type(&self, v: &Value) -> &'static str {
        match v {
            Value::Bool(true) => "true",
            Value::Bool(false) => "false",
            other => self.diag_type(other),
        }
    }

    /// An array key as a diagnostic renders it: a string key is quoted, an
    /// integer key is bare (`Undefined array key "k"` vs `Undefined array key 7`).
    pub fn diag_key(&self, key: &Value) -> String {
        match self.norm_key(key) {
            ArrayKey::Int(n) => n.to_string(),
            ArrayKey::Str(s) => format!("\"{s}\""),
        }
    }

    pub fn set_error(&mut self, msg: impl Into<String>) {
        if self.error.is_none() {
            self.error = Some(msg.into());
        }
    }

    pub fn take_error(&mut self) -> Option<String> {
        self.error.take()
    }

    // ── variables ──────────────────────────────────────────────────────────

    /// The index of the scope a variable name resolves in: the global frame for a
    /// superglobal, else the innermost frame.
    fn scope_idx(&self, name: &str) -> usize {
        if is_superglobal(name) {
            0
        } else {
            self.scopes.len().saturating_sub(1)
        }
    }

    pub fn get_var(&self, name: &str) -> Value {
        let idx = self.scope_idx(name);
        let Some(scope) = self.scopes.get(idx) else {
            return Value::Undef;
        };
        self.read_slot(scope.vars.get(name))
    }

    /// The value a slot holds: a reference reads through its shared cell, an
    /// unset slot reads as PHP `null`.
    fn read_slot(&self, s: &Slot) -> Value {
        match s {
            Slot::Val(v) => v.clone(),
            Slot::Ref(c) => self.ref_cells.get(*c).cloned().unwrap_or(Value::Undef),
            Slot::Unset => Value::Undef,
        }
    }

    /// Is `name` BOUND in the scope it resolves in?
    ///
    /// Distinct from a non-`Undef` `get_var`, and the difference is the whole
    /// point: PHP `null` is `Value::Undef` here (fusevm has no null), so an unset
    /// name and a name holding `null` read back identically. Only the binding
    /// itself separates them, which is what `compact()` and `isset()`-shaped
    /// checks need — `$a = null; compact('a')` yields `['a' => null]` with no
    /// diagnostic, while an unset `$a` yields `[]` and a warning.
    pub fn var_defined(&self, name: &str) -> bool {
        let idx = self.scope_idx(name);
        let Some(scope) = self.scopes.get(idx) else {
            return false;
        };
        !matches!(scope.vars.get(name), Slot::Unset)
    }

    /// Whether `name` has a slot in the current frame, bound or not: a compiled
    /// variable the frame mentions anywhere (the reference's CV, present in the
    /// symbol table as an `IS_UNDEF` indirect until first written), or one bound
    /// dynamically and since unset.
    pub fn var_has_slot(&self, name: &str) -> bool {
        let idx = self.scope_idx(name);
        self.scopes
            .get(idx)
            .is_some_and(|s| s.vars.slot_of(name).is_some())
    }

    pub fn set_var(&mut self, name: &str, val: Value) {
        let idx = self.scope_idx(name);
        let Some(scope) = self.scopes.get_mut(idx) else {
            return;
        };
        let i = scope.vars.ensure_slot(name);
        self.write_slot(idx, i, val);
    }

    /// Whether a `foreach` over `v` walks its property table: an object with
    /// neither `getIterator` nor the `Iterator` protocol.
    pub fn is_plain_foreach_object(&self, v: &Value) -> bool {
        !self.is_array(v)
            && !self.is_generator_val(v)
            && self.object_class(v).is_some_and(|c| {
                !self.class_has_method(&c, "getIterator")
                    && !(self.class_has_method(&c, "valid") && self.class_has_method(&c, "current"))
            })
    }

    /// The value bound in slot `i` of scope `scope_idx` — through its cell when
    /// the slot is a reference — for an in-place update; `None` when unbound.
    fn bound_value_mut(&mut self, scope_idx: usize, i: u32) -> Option<&mut Value> {
        let cell = match self.scopes.get(scope_idx)?.vars.slots.get(i as usize)? {
            Slot::Ref(c) => Some(*c),
            Slot::Val(_) => None,
            Slot::Unset => return None,
        };
        match cell {
            Some(c) => self.ref_cells.get_mut(c),
            None => match self.scopes[scope_idx].vars.slots.get_mut(i as usize)? {
                Slot::Val(v) => Some(v),
                _ => None,
            },
        }
    }

    /// `$var .= y` in place when `$var` (slot `i` of scope `scope_idx`) holds a
    /// string: `Arc::make_mut` writes into the buffer when the variable is its
    /// only owner and copies it otherwise, so value semantics hold either way.
    /// `None` when the variable is unbound or not a string — the caller's
    /// general path, with its diagnostics.
    fn append_bound(&mut self, scope_idx: usize, i: u32, y: &str) -> Option<Value> {
        match self.bound_value_mut(scope_idx, i)? {
            Value::Str(s) => {
                crate::bytes::append(std::sync::Arc::make_mut(s), y);
                Some(Value::Str(s.clone()))
            }
            _ => None,
        }
    }

    /// `PhpHost::append_bound` on slot `i` of the current frame.
    pub fn slot_append_str(&mut self, i: u32, y: &str) -> Option<Value> {
        let idx = self.scopes.len().checked_sub(1)?;
        self.append_bound(idx, i, y)
    }

    /// `PhpHost::append_bound` on the variable `name`.
    pub fn var_append_str(&mut self, name: &str, y: &str) -> Option<Value> {
        let idx = self.scope_idx(name);
        let i = self.scopes.get(idx)?.vars.slot_of(name)?;
        self.append_bound(idx, i, y)
    }

    /// Store into a slot: a reference writes through its shared cell so every
    /// alias sees it, anything else takes the value directly.
    fn write_slot(&mut self, scope_idx: usize, i: u32, val: Value) {
        let cell = match self.scopes.get(scope_idx).map(|s| s.vars.at(i)) {
            Some(Slot::Ref(c)) => Some(*c),
            _ => None,
        };
        match cell {
            Some(c) => {
                if let Some(slot) = self.ref_cells.get_mut(c) {
                    *slot = val;
                }
            }
            None => {
                if let Some(scope) = self.scopes.get_mut(scope_idx) {
                    scope.vars.put(i, Slot::Val(val));
                }
            }
        }
    }

    /// The current frame's bound variables, in the order they were first
    /// bound — what `get_defined_vars()` answers.
    ///
    /// Slot order IS binding order: the compiler numbers a scope's parameters
    /// first and then each local at its first mention, and a name reserved
    /// later only ever appends. Iterating the name index instead would answer
    /// in hash order, which PHP's output is not.
    ///
    /// An unset name is absent while a name bound to `null` is present — the
    /// distinction `Slot::Unset` exists for. `$this` and the compiler's own
    /// `@`-prefixed entries are not the user's variables and are left out.
    pub fn defined_vars(&self) -> Vec<(String, Value)> {
        let idx = self.scopes.len().saturating_sub(1);
        if idx == 0 {
            return self.global_vars_ordered();
        }
        self.frame_vars(idx)
    }

    /// The variables of frame `idx`, as [`PhpHost::defined_vars`] lists them.
    fn frame_vars(&self, idx: usize) -> Vec<(String, Value)> {
        let Some(scope) = self.scopes.get(idx) else {
            return Vec::new();
        };
        let mut by_slot: Vec<(u32, &String)> = scope
            .vars
            .index
            .iter()
            .map(|(name, &i)| (i, name))
            .collect();
        by_slot.sort_unstable_by_key(|(i, _)| *i);
        by_slot
            .into_iter()
            // `$GLOBALS` is a view of the global frame rather than a variable
            // in it, and PHP never lists it here.
            .filter(|(_, name)| {
                !name.starts_with('@') && !matches!(name.as_str(), "this" | "GLOBALS")
            })
            .filter_map(|(i, name)| match scope.vars.at(i) {
                Slot::Unset => None,
                slot => Some((name.clone(), self.read_slot(slot))),
            })
            .collect()
    }

    /// Read the current frame's slot `i`. The compiler resolved the name to this
    /// index, so there is no name to test against the superglobals or to hash.
    pub fn slot_get(&mut self, i: u32) -> Value {
        let s = match self.scopes.last() {
            Some(sc) => sc.vars.at(i).clone(),
            None => Slot::Unset,
        };
        if matches!(s, Slot::Unset) {
            // Only the diagnostic needs the name back, and only on the path that
            // is already an error, so the reverse lookup is not on the hot path.
            if let Some(n) = self.slot_name(i) {
                if !n.starts_with('@') {
                    self.warn(format_args!("Undefined variable ${n}"));
                }
            }
            return Value::Undef;
        }
        self.read_slot(&s)
    }

    /// Read slot `i` with no `Undefined variable` diagnostic — the isset/`empty`
    /// context, where an unbound name is a legitimate answer rather than a bug.
    pub fn slot_get_quiet(&self, i: u32) -> Value {
        match self.scopes.last() {
            Some(sc) => self.read_slot(sc.vars.at(i)),
            None => Value::Undef,
        }
    }

    /// Write the current frame's slot `i`, through the shared cell when the slot
    /// holds a reference binding.
    pub fn slot_set(&mut self, i: u32, val: Value) {
        let idx = self.scopes.len().saturating_sub(1);
        self.write_slot(idx, i, val);
    }

    /// The name a slot was resolved from, for a diagnostic. Linear in the
    /// frame's variable count and only reached when reporting an unbound read.
    fn slot_name(&self, i: u32) -> Option<String> {
        self.scopes
            .last()?
            .vars
            .index
            .iter()
            .find(|(_, &v)| v == i)
            .map(|(k, _)| k.clone())
    }

    /// Reserve the GLOBAL frame's slots in the order the main chunk numbered
    /// them. Separate from `seed_slots`, which seeds the innermost
    /// frame: the main chunk runs in the global frame whatever is on the stack.
    pub fn seed_global_slots(&mut self, names: &[String]) {
        if let Some(scope) = self.scopes.first_mut() {
            scope.vars.renumber(names);
        }
    }

    /// Reserve this frame's slots in the order the compiler numbered them, so
    /// index `n` in the chunk and index `n` in the frame are the same variable.
    /// Called once per call, before any binding.
    fn seed_slots(&mut self, names: &[String]) {
        if let Some(scope) = self.scopes.last_mut() {
            for n in names {
                scope.vars.ensure_slot(n);
            }
        }
    }

    /// `$target = &$source` — bind `target` as a reference to `source`, so both
    /// names share one storage cell (either's mutation is visible to the other).
    /// The reference cell `name` resolves to, promoting a plain variable into
    /// one first, wrapped in a [`PhpObj::Ref`] handle. What `use (&$v)` captures
    /// — see [`PhpHost::bind_ref_slot`] for the other half.
    pub fn ref_cell_of(&mut self, name: &str) -> Value {
        let slot = self.ref_slot_of(name);
        self.objs.push(PhpObj::Ref { slot });
        Value::Obj((self.objs.len() - 1) as u32)
    }

    /// Point `name` in the current scope at an existing reference cell, so it
    /// and whatever else shares the cell are one variable.
    pub fn bind_ref_slot(&mut self, name: &str, slot: usize) {
        let idx = self.scope_idx(name);
        if let Some(scope) = self.scopes.get_mut(idx) {
            let i = scope.vars.ensure_slot(name);
            scope.vars.put(i, Slot::Ref(slot));
        }
    }

    /// Allocate a reference cell that no variable owns yet, holding `val`, and
    /// return a [`PhpObj::Ref`] handle to it together with its slot.
    ///
    /// Builtins that hand an element to a user callback by reference
    /// (`array_walk`) use this: pass the handle as the argument, let the callee's
    /// by-ref parameter bind the slot, then read the slot back with
    /// [`PhpHost::ref_cell_value`] to see what the callback wrote.
    pub fn new_ref_cell(&mut self, val: Value) -> (Value, usize) {
        self.ref_cells.push(val);
        let slot = self.ref_cells.len() - 1;
        self.objs.push(PhpObj::Ref { slot });
        (Value::Obj((self.objs.len() - 1) as u32), slot)
    }

    /// Read the current contents of the reference cell at `slot`.
    pub fn ref_cell_value(&self, slot: usize) -> Value {
        self.ref_cells.get(slot).cloned().unwrap_or(Value::Undef)
    }

    /// The reference slot a value denotes, if it is a [`PhpObj::Ref`] handle.
    ///
    /// A stored `Ref` handle is how an array element or an object property that a
    /// `&` binding has made into a reference is represented: the container keeps
    /// the handle, every read derefs it, and every write goes through the cell, so
    /// the element and the alias are one storage location — PHP's `IS_REFERENCE`
    /// zval in the slot.
    pub fn ref_slot_of_value(&self, v: &Value) -> Option<usize> {
        match v {
            Value::Obj(h) => match self.objs.get(*h as usize) {
                Some(PhpObj::Ref { slot }) => Some(*slot),
                _ => None,
            },
            _ => None,
        }
    }

    /// Resolve a stored value: a `Ref` handle reads as its cell's contents,
    /// everything else as itself. Applied on the way out of every container read
    /// so a reference in a slot is invisible to code that only reads the value.
    fn deref(&self, v: Value) -> Value {
        match self.ref_slot_of_value(&v) {
            Some(slot) => self.ref_cells.get(slot).cloned().unwrap_or(Value::Undef),
            None => v,
        }
    }

    /// Write `val` into the reference cell at `slot`.
    fn ref_cell_set(&mut self, slot: usize, val: Value) {
        if let Some(cell) = self.ref_cells.get_mut(slot) {
            *cell = val;
        }
    }

    /// The reference cell `name` resolves to, creating one — and moving the
    /// variable's current value into it — if the variable is still a plain one.
    fn ref_slot_of(&mut self, name: &str) -> usize {
        let idx = self.scope_idx(name);
        if let Some(Slot::Ref(c)) = self.scopes.get(idx).map(|s| s.vars.get(name)) {
            return *c;
        }
        let cur = self
            .scopes
            .get(idx)
            .map(|s| s.vars.get(name).clone())
            .map(|s| self.read_slot(&s))
            .unwrap_or(Value::Undef);
        self.ref_cells.push(cur);
        let slot = self.ref_cells.len() - 1;
        if let Some(scope) = self.scopes.get_mut(idx) {
            let i = scope.vars.ensure_slot(name);
            scope.vars.put(i, Slot::Ref(slot));
        }
        slot
    }

    /// The reference cell for the array element `$name[k1]..[kN]`, promoting the
    /// element into a reference (and auto-vivifying the path, as PHP does for a
    /// `&` lvalue) if it is not one already. `keys` must be non-empty.
    ///
    /// This is the storage half of `$r = &$a[k]`: the element slot keeps a
    /// [`PhpObj::Ref`] handle to the returned cell, so a later write through
    /// either name lands in the one cell.
    pub fn elem_ref_slot(&mut self, name: &str, keys: &[Value]) -> usize {
        let Some((last, inter)) = keys.split_last() else {
            return self.ref_slot_of(name);
        };
        let arr = self.ensure_path_array(name, inter);
        self.arr_elem_ref_slot(&arr, last)
    }

    /// The reference cell for `arr[key]` (a handle), promoting the element into a
    /// reference if it is not one already.
    pub fn arr_elem_ref_slot(&mut self, arr: &Value, key: &Value) -> usize {
        let k = self.norm_key(key);
        if let Some(slot) = self.entry_ref_slot(arr, &k) {
            return slot;
        }
        // PHP vivifies a `&`-taken element to null when it does not exist yet.
        let cur = match self.as_array(arr) {
            Some(PhpObj::Array { entries, .. }) => entries.get(&k).cloned().unwrap_or(Value::Undef),
            _ => Value::Undef,
        };
        self.ref_cells.push(cur);
        let slot = self.ref_cells.len() - 1;
        self.objs.push(PhpObj::Ref { slot });
        let handle = Value::Obj((self.objs.len() - 1) as u32);
        if let Some(PhpObj::Array {
            entries,
            next_index,
        }) = self.as_array_mut(arr)
        {
            if let ArrayKey::Int(n) = k {
                if n >= *next_index {
                    *next_index = n.saturating_add(1);
                }
            }
            entries.insert(k, handle);
        }
        slot
    }

    /// The reference cell for `$obj->name`, promoting the property into a
    /// reference if it is not one already.
    pub fn prop_ref_slot_ensure(&mut self, recv: &Value, name: &str) -> usize {
        if let Some(slot) = self.prop_ref_slot(recv, name) {
            return slot;
        }
        let cur = match self.as_array(recv) {
            Some(PhpObj::Object { props, .. }) => props.get(name).cloned().unwrap_or(Value::Undef),
            _ => Value::Undef,
        };
        self.ref_cells.push(cur);
        let slot = self.ref_cells.len() - 1;
        self.objs.push(PhpObj::Ref { slot });
        let handle = Value::Obj((self.objs.len() - 1) as u32);
        self.obj_put_prop(recv, name, handle);
        slot
    }

    /// Store a [`PhpObj::Ref`] handle for `slot` into `$name[k1]..[kN]` — the
    /// `$a[k] = &$x` direction, where the *container slot* becomes the alias.
    /// An empty `keys` binds the plain variable instead.
    pub fn bind_elem_to_slot(&mut self, name: &str, keys: &[Value], slot: usize) {
        let Some((last, inter)) = keys.split_last() else {
            self.bind_ref_slot(name, slot);
            return;
        };
        let arr = self.ensure_path_array(name, inter);
        self.objs.push(PhpObj::Ref { slot });
        let handle = Value::Obj((self.objs.len() - 1) as u32);
        let k = self.norm_key(last);
        if let Some(PhpObj::Array {
            entries,
            next_index,
        }) = self.as_array_mut(&arr)
        {
            if let ArrayKey::Int(n) = k {
                if n >= *next_index {
                    *next_index = n.saturating_add(1);
                }
            }
            entries.insert(k, handle);
        }
    }

    /// `$name[k1]..[kM][] = &$x` — append a [`PhpObj::Ref`] handle for `slot`.
    ///
    /// Refuses with [`NEXT_ELEMENT_OCCUPIED`] on a saturated array, exactly as
    /// the by-value append does — binding a reference is still an append.
    pub fn append_elem_to_slot(
        &mut self,
        name: &str,
        keys: &[Value],
        slot: usize,
    ) -> Result<(), String> {
        let arr = self.ensure_path_array(name, keys);
        if self.append_slot_taken(&arr) {
            return Err(NEXT_ELEMENT_OCCUPIED.to_string());
        }
        self.objs.push(PhpObj::Ref { slot });
        let handle = Value::Obj((self.objs.len() - 1) as u32);
        if let Some(PhpObj::Array {
            entries,
            next_index,
        }) = self.as_array_mut(&arr)
        {
            let k = ArrayKey::Int(take_next_index(next_index));
            entries.insert(k, handle);
        }
        Ok(())
    }

    /// `$obj->p = &$x` — store a [`PhpObj::Ref`] handle for `slot` in a property.
    pub fn bind_prop_to_slot(&mut self, recv: &Value, name: &str, slot: usize) {
        self.objs.push(PhpObj::Ref { slot });
        let handle = Value::Obj((self.objs.len() - 1) as u32);
        self.obj_put_prop(recv, name, handle);
    }

    /// The running frame's late-static-binding class, or `fallback` (the
    /// enclosing class the compiler baked in) outside any method call.
    pub fn lsb_class(&self, fallback: &str) -> String {
        self.scopes
            .last()
            .and_then(|s| s.static_class.clone())
            .unwrap_or_else(|| fallback.to_string())
    }

    /// Mark the next call as forwarding: it inherits this frame's
    /// late-static-binding class instead of taking the class it names.
    pub fn lsb_forward(&mut self) {
        self.pending_lsb = self.scopes.last().and_then(|s| s.static_class.clone());
    }

    /// Set the late-static-binding class the next call's frame takes, unless a
    /// forwarding call has already claimed it.
    pub fn lsb_set_for_next_call(&mut self, class: &str) {
        if self.pending_lsb.is_none() {
            self.pending_lsb = Some(class.to_string());
        }
    }

    /// Take the late-static-binding class a pushed frame should carry.
    fn lsb_take(&mut self) -> Option<String> {
        self.pending_lsb.take()
    }

    /// Hand the next pushed frame the site of the closure literal being entered.
    pub fn closure_site_for_next_call(&mut self, site: Option<DeclSite>) {
        self.pending_closure_site = site;
    }

    /// Take it. A non-closure call leaves it `None`, which is what a named
    /// function's frame wants.
    fn closure_site_take(&mut self) -> Option<DeclSite> {
        self.pending_closure_site.take()
    }

    /// Publish `slot` as the reference the running `function &f()` returns.
    pub fn set_ret_ref_slot(&mut self, slot: usize) {
        self.ret_ref_slot = Some(slot);
    }

    /// Take the reference cell the last call published with `return` in a
    /// `function &f()`, or `None` when it returned by value. Unlike
    /// [`PhpHost::take_ret_ref_slot`] nothing is allocated for a by-value call.
    pub fn take_published_ret_ref(&mut self) -> Option<usize> {
        self.ret_ref_slot.take()
    }

    /// Whether the call that just returned published a by-reference cell,
    /// leaving it for the binding that follows to take.
    pub fn has_published_ret_ref(&self) -> bool {
        self.ret_ref_slot.is_some()
    }

    /// The array the reference cell at `slot` holds, vivifying one into the
    /// cell first when it holds `null` — the write half of
    /// `$o->p[] = v` through a `function &__get()`.
    pub fn ref_cell_ensure_array(&mut self, slot: usize) -> Value {
        let cur = self.ref_cell_value(slot);
        if !matches!(cur, Value::Undef) {
            return cur;
        }
        let arr = self.new_array();
        if let Some(cell) = self.ref_cells.get_mut(slot) {
            *cell = arr.clone();
        }
        arr
    }

    /// Take the reference cell the last call returned. A call that did not return
    /// by reference has none, so `fallback` is parked in a fresh detached cell —
    /// `$r = &f()` on a by-value function then aliases only its own copy.
    pub fn take_ret_ref_slot(&mut self, fallback: Value) -> usize {
        match self.ret_ref_slot.take() {
            Some(slot) => slot,
            None => {
                self.ref_cells.push(fallback);
                self.ref_cells.len() - 1
            }
        }
    }

    /// The global frame's variables in the order the reference lists them: the
    /// superglobals the CLI populates first, in its fixed order, then the script's
    /// own variables in binding order.
    fn global_vars_ordered(&self) -> Vec<(String, Value)> {
        const FIXED: [&str; 7] = [
            "argv", "argc", "_GET", "_POST", "_COOKIE", "_FILES", "_SERVER",
        ];
        let all = self.frame_vars(0);
        let mut ordered: Vec<(String, Value)> = Vec::with_capacity(all.len());
        for name in FIXED {
            if let Some(pair) = all.iter().find(|(n, _)| n == name) {
                ordered.push(pair.clone());
            }
        }
        ordered.extend(
            all.into_iter()
                .filter(|(n, _)| !FIXED.contains(&n.as_str()) && !is_superglobal(n)),
        );
        ordered
    }

    /// The contents of `$GLOBALS`: the global frame's variables as an array, the
    /// superglobals the CLI populates first in the reference's fixed order and
    /// the script's own variables after them in binding order.
    pub fn globals_array(&mut self) -> Value {
        let ordered = self.global_vars_ordered();
        let arr = self.new_array();
        for (name, value) in ordered {
            self.arr_set_key(&arr, &Value::str(name), value);
        }
        arr
    }

    /// The value of the global variable `name`, or `None` when it is not bound.
    pub fn global_value(&self, name: &str) -> Option<Value> {
        if name == "GLOBALS" {
            return None;
        }
        let scope = self.scopes.first()?;
        match scope.vars.get(name) {
            Slot::Unset => None,
            slot => Some(self.read_slot(slot)),
        }
    }

    /// Bind the running frame's `local` to the global variable `name` by reference,
    /// creating the global when it does not exist — the write half of
    /// `$GLOBALS['name']`.
    pub fn globals_bind(&mut self, local: &str, name: &str) {
        const GLOBAL_FRAME: usize = 0;
        let cur = self.scopes.len().saturating_sub(1);
        let slot = match self.scopes.get(GLOBAL_FRAME).map(|s| s.vars.get(name)) {
            Some(Slot::Ref(c)) => *c,
            _ => {
                let cur_val = self
                    .scopes
                    .get(GLOBAL_FRAME)
                    .map(|s| s.vars.get(name).clone())
                    .map(|s| self.read_slot(&s))
                    .unwrap_or(Value::Undef);
                self.ref_cells.push(cur_val);
                let slot = self.ref_cells.len() - 1;
                if let Some(scope) = self.scopes.get_mut(GLOBAL_FRAME) {
                    let i = scope.vars.ensure_slot(name);
                    scope.vars.put(i, Slot::Ref(slot));
                }
                slot
            }
        };
        if let Some(scope) = self.scopes.get_mut(cur) {
            let i = scope.vars.ensure_slot(local);
            scope.vars.put(i, Slot::Ref(slot));
        }
    }

    /// `unset($GLOBALS['name'])`: the global binding goes away; a local that
    /// aliased it keeps the shared cell.
    pub fn globals_unset(&mut self, name: &str) {
        if let Some(scope) = self.scopes.first_mut() {
            if let Some(i) = scope.vars.slot_of(name) {
                scope.vars.put(i, Slot::Unset);
            }
        }
    }

    /// `global $name;` — make the running frame's `name` an alias of the GLOBAL
    /// variable of that name.
    ///
    /// The reference defines this as `$name = &$GLOBALS['name']`, and it is a
    /// real reference on both ends: the global is promoted to a shared cell if
    /// it was a plain value, and the local is bound to that same cell. So a
    /// write through either side is seen by the other, and a global that did not
    /// exist yet is CREATED by the binding rather than read as null once.
    ///
    /// At global scope the two frames are the same frame, and the reference
    /// treats `global $x` there as a no-op rather than aliasing a name to
    /// itself.
    pub fn bind_global(&mut self, name: &str) {
        const GLOBAL_FRAME: usize = 0;
        let cur = self.scopes.len().saturating_sub(1);
        if cur == GLOBAL_FRAME {
            return;
        }
        let slot = match self.scopes.get(GLOBAL_FRAME).map(|s| s.vars.get(name)) {
            Some(Slot::Ref(c)) => *c,
            _ => {
                // Promote the global into a cell, carrying whatever it already
                // held so `global $x` does not clear an existing value.
                let cur_val = self
                    .scopes
                    .get(GLOBAL_FRAME)
                    .map(|s| s.vars.get(name).clone())
                    .map(|s| self.read_slot(&s))
                    .unwrap_or(Value::Undef);
                self.ref_cells.push(cur_val);
                let slot = self.ref_cells.len() - 1;
                if let Some(scope) = self.scopes.get_mut(GLOBAL_FRAME) {
                    let i = scope.vars.ensure_slot(name);
                    scope.vars.put(i, Slot::Ref(slot));
                }
                slot
            }
        };
        if let Some(scope) = self.scopes.get_mut(cur) {
            let i = scope.vars.ensure_slot(name);
            scope.vars.put(i, Slot::Ref(slot));
        }
    }

    pub fn ref_bind(&mut self, target: &str, source: &str) {
        let idx = self.scope_idx(source);
        // Resolve the source's cell, promoting a plain variable into one.
        let slot = self.ref_slot_of(source);
        let _ = idx;
        let tidx = self.scope_idx(target);
        if let Some(scope) = self.scopes.get_mut(tidx) {
            let i = scope.vars.ensure_slot(target);
            scope.vars.put(i, Slot::Ref(slot));
        }
    }

    /// `unset($name)` — remove the scope variable (and break any reference
    /// binding for that name, leaving the shared cell intact for other aliases).
    pub fn unset_var(&mut self, name: &str) {
        let idx = self.scope_idx(name);
        if let Some(scope) = self.scopes.get_mut(idx) {
            // The slot stays reserved so an index the compiler handed out is
            // still valid; only the binding goes away.
            if let Some(i) = scope.vars.slot_of(name) {
                scope.vars.put(i, Slot::Unset);
            }
        }
    }

    /// `unset($name[k1]..[kN])` — remove the deepest array element along the key
    /// path. The key is removed without renumbering the remaining keys (PHP
    /// leaves a hole), matching `unset` on an array element.
    pub fn unset_path(&mut self, name: &str, keys: &[Value]) {
        let Some((last, inter)) = keys.split_last() else {
            return;
        };
        // Navigate (read-only, no vivification) to the array holding `last`.
        let mut arr = self.get_var(name);
        for k in inter {
            arr = self.index_get(&arr, k);
        }
        self.diagnose_array_offset(&arr, last);
        let nk = self.norm_key(last);
        if let Some(PhpObj::Array { entries, .. }) = self.as_array_mut(&arr) {
            entries.shift_remove(&nk);
        }
    }

    // ── DAP debug introspection (used only under `--dap`) ────────────────────

    /// Number of active scopes (the debugger's step-depth reference).
    pub fn frame_depth(&self) -> usize {
        self.scopes.len()
    }

    /// Record the source line the innermost frame is executing.
    ///
    /// Two callers, one meaning. Under `--dap` the per-statement line hook drives
    /// it; on every path the ops that can enter a frame or raise a throw record
    /// the op's own line, which is what a backtrace reads back as the call site.
    /// Distinct from [`crate::host::set_warn_line`], which is the line of
    /// the op that is warning *now* and need not survive a nested call returning.
    pub fn set_cur_line(&mut self, line: u32) {
        if let Some(s) = self.scopes.last_mut() {
            s.line = line;
        }
    }

    /// The call stack as `(frame name, line)` pairs, innermost first — for the
    /// DAP `stackTrace`. The global scope is reported as `{main}`.
    pub fn dbg_stack(&self) -> Vec<(String, u32)> {
        self.scopes
            .iter()
            .rev()
            .map(|s| {
                let name = s.name.clone().unwrap_or_else(|| "{main}".to_string());
                (name, s.line)
            })
            .collect()
    }

    /// The innermost frame's locals as `(name, string-cast)` pairs — for DAP
    /// `variables`. Compiler temporaries (`@`-prefixed) are hidden, matching a
    /// debugger's default locals view.
    pub fn dbg_locals(&self) -> Vec<(String, String)> {
        let pairs: Vec<(String, Slot)> = self
            .scopes
            .last()
            .map(|s| {
                s.vars
                    .iter()
                    .filter(|(k, _)| !k.starts_with('@'))
                    .map(|(k, v)| (k.clone(), v.clone()))
                    .collect()
            })
            .unwrap_or_default();
        // A reference-bound name reads through its shared cell like any other.
        let vars: Vec<(String, Value)> = pairs
            .into_iter()
            .map(|(k, s)| {
                let v = self.read_slot(&s);
                (k, v)
            })
            .collect();
        vars.into_iter()
            .map(|(n, v)| (format!("${n}"), self.to_str(&v)))
            .collect()
    }

    // ── arrays ─────────────────────────────────────────────────────────────

    /// A fresh `stdClass` holding `props` in the given order, invisible to the
    /// object counter. `json_encode` stages the public-only view of an object
    /// here rather than mutating the real one; because PHP never allocated it,
    /// it must not shift the `#n` handles `var_dump` prints.
    pub fn new_transient_object(&mut self, props: Vec<(String, Value)>) -> Value {
        let v = self.new_object_bare("stdClass", props);
        if let Value::Obj(id) = v {
            self.transient_objs.insert(id);
        }
        v
    }

    /// An instance of `class` carrying exactly `props`, with NO constructor and NO
    /// property defaults. `unserialize` needs this: the reference restores the
    /// recorded state verbatim and never runs `__construct`.
    pub fn new_object_bare(&mut self, class: &str, props: Vec<(String, Value)>) -> Value {
        self.objs.push(PhpObj::Object {
            class: class.to_string(),
            props: props.into_iter().collect(),
        });
        Value::Obj((self.objs.len() - 1) as u32)
    }

    pub fn new_array(&mut self) -> Value {
        self.objs.push(PhpObj::Array {
            entries: IndexMap::new(),
            next_index: NO_INDEX,
        });
        Value::Obj((self.objs.len() - 1) as u32)
    }

    /// A PHP array is a **value**: assigning one, passing one to a function or
    /// returning one hands over a copy, so a write through the new name is
    /// invisible through the old one. Everything else — an object, a closure,
    /// a generator, a stream — is a handle and is passed through untouched,
    /// which is exactly PHP's own split.
    ///
    /// The copy is deep in the arrays and shallow everywhere else: an array
    /// nested in an array is itself a value and is copied with it, while an
    /// object stored in one is a handle and stays shared. PHP defers the same
    /// copy until a write (copy-on-write), which no program can observe — the
    /// difference is the cost of a copy that turns out to be unread, not the
    /// answer.
    pub fn copy_on_assign(&mut self, v: Value) -> Value {
        let Value::Obj(id) = v else { return v };
        let Some(PhpObj::Array {
            entries,
            next_index,
        }) = self.objs.get(id as usize)
        else {
            return v;
        };
        let next_index = *next_index;
        let pairs: Vec<(ArrayKey, Value)> = entries
            .iter()
            .map(|(k, v)| (k.clone(), v.clone()))
            .collect();
        let copied: IndexMap<ArrayKey, Value> = pairs
            .into_iter()
            .map(|(k, v)| (k, self.copy_on_assign(v)))
            .collect();
        self.objs.push(PhpObj::Array {
            entries: copied,
            next_index,
        });
        Value::Obj((self.objs.len() - 1) as u32)
    }

    /// Duplicate the object `v` refers to, giving a NEW handle over the same
    /// state — the storage half of `clone $o`, without the `__clone` hook.
    ///
    /// Each property goes through [`PhpHost::copy_on_assign`], which is exactly
    /// PHP's rule for what a clone shares: an array property is a value and is
    /// copied, an object property is a handle and stays shared with the
    /// original. A closure is duplicated whole (its captures were already
    /// copied when it was built).
    ///
    /// `None` for anything that is not a clonable object — an array, a
    /// generator, or a scalar — so the caller can raise the right error, which
    /// differs between the three.
    pub fn clone_obj(&mut self, v: &Value) -> Option<Value> {
        let Value::Obj(id) = v else { return None };
        match self.objs.get(*id as usize)? {
            PhpObj::Object { class, props } => {
                let class = class.clone();
                let pairs: Vec<(String, Value)> =
                    props.iter().map(|(k, v)| (k.clone(), v.clone())).collect();
                let props: IndexMap<String, Value> = pairs
                    .into_iter()
                    .map(|(k, v)| (k, self.copy_on_assign(v)))
                    .collect();
                self.objs.push(PhpObj::Object { class, props });
                let copy = (self.objs.len() - 1) as u32;
                // The copy inherits which readonly properties are already
                // written: it carries their values, so they are initialized.
                if let Some(init) = self.readonly_init.get(id).cloned() {
                    self.readonly_init.insert(copy, init);
                }
                Some(Value::Obj(copy))
            }
            dup @ PhpObj::Closure { .. } => {
                let mut dup = dup.clone();
                if let PhpObj::Closure { captured, .. } = &mut dup {
                    self.fork_closure_statics(captured);
                }
                self.objs.push(dup);
                Some(Value::Obj((self.objs.len() - 1) as u32))
            }
            _ => None,
        }
    }

    // ── closures ───────────────────────────────────────────────────────────

    /// Build a closure object from a compiler-registered function definition
    /// (`def_name`, a synthetic name in the function table) plus the values
    /// captured at creation time. Returns the new handle, or `Undef` if the
    /// definition is missing (should never happen for compiler-emitted names).
    pub fn make_closure(&mut self, def_name: &str, mut captured: Vec<(String, Value)>) -> Value {
        let Some(def) = self.functions.get(def_name).cloned() else {
            return Value::Undef;
        };
        // `static function (…)` arrives with the marker capture the compiler
        // added. It is not a variable, so it is taken back out before the rest
        // become the closure's bindings.
        let is_static = {
            let n = captured.len();
            captured.retain(|(k, _)| k != STATIC_CLOSURE_CAPTURE);
            captured.len() != n
        };
        // The body's `static`s: each closure object holds its own cells, bound
        // into its frame under the declaration's key. A `?` key is one whose
        // initializer has yet to run.
        for (k, v) in captured.iter_mut() {
            let uninit = k.starts_with("?@static#");
            if !uninit && !k.starts_with("@static#") {
                continue;
            }
            if uninit {
                k.remove(0);
            }
            self.ref_cells.push(std::mem::take(v));
            let slot = self.ref_cells.len() - 1;
            if uninit {
                self.static_uninit.insert(slot);
            }
            self.objs.push(PhpObj::Ref { slot });
            *v = Value::Obj((self.objs.len() - 1) as u32);
        }
        // A closure defined inside a method auto-binds the current `$this` and its
        // class scope (PHP semantics), so `$this` and private member access work
        // without an explicit `bindTo`. `None`/`None` at the top level or in a free
        // function keeps the previous behavior exactly.
        let bound_this = match self.get_var("this") {
            // A static closure is never bound to the instance it was written
            // inside, so `$this` is unset in its body and `Closure::bind`
            // refuses to supply one later.
            _ if is_static => None,
            t @ Value::Obj(_) => Some(t),
            _ => None,
        };
        let scope = self.current_class_ctx();
        // `ZEND_DECLARE_LAMBDA_FUNCTION`: the class of the enclosing `$this`
        // when there is one — even for a `static` closure, which drops only the
        // instance — otherwise the enclosing frame's own called scope.
        let called_scope = match self.get_var("this") {
            t @ Value::Obj(_) => self.object_class(&t),
            _ => self.scopes.last().and_then(|s| s.static_class.clone()),
        };
        self.objs.push(PhpObj::Closure {
            params: def.params,
            chunk: Box::new(def.chunk),
            captured,
            bound_this,
            scope,
            called_scope,
            is_generator: def.is_generator,
            ret: def.ret,
            is_static,
            site: def.closure_site,
            doc: def.doc,
            attrs: def.attrs,
            span: def.span,
        });
        Value::Obj((self.objs.len() - 1) as u32)
    }

    /// The closure held by `v` (a handle), cloned for a call: its parameters,
    /// body chunk, captured bindings, and any bound `$this`/scope. `None` if `v`
    /// is not a closure.
    fn closure_of(&self, v: &Value) -> Option<ClosureCall> {
        match self.as_array(v) {
            Some(PhpObj::Closure {
                params,
                chunk,
                captured,
                bound_this,
                scope,
                called_scope,
                is_generator,
                ret,
                site,
                ..
            }) => Some(ClosureCall {
                params: params.clone(),
                chunk: (**chunk).clone(),
                captured: captured.clone(),
                bound_this: bound_this.clone(),
                scope: scope.clone(),
                called_scope: called_scope.clone(),
                is_generator: *is_generator,
                ret: ret.clone(),
                site: site.clone(),
            }),
            _ => None,
        }
    }

    /// A new closure identical to `v` but with `$this` rebound to `this` and the
    /// private-access scope set to `scope` (both optional). Backs
    /// `Closure::bind`/`bindTo`/`call`.
    pub fn rebind_closure(
        &mut self,
        v: &Value,
        this: Option<Value>,
        scope: Option<String>,
    ) -> Option<Value> {
        // The rebound closure is the same literal: everything but the binding
        // carries over — its site (which names its frames), doc comment and
        // attributes — and its `static`s are copied.
        let mut dup = match self.as_array(v) {
            Some(c @ PhpObj::Closure { .. }) => c.clone(),
            _ => return None,
        };
        // `do_closure_bind`: the new object's class, or the new scope without
        // one — in its declared spelling, since a scope may arrive lowercased.
        let new_called = match &this {
            Some(t) => self.object_class(t),
            None => scope.as_ref().map(|s| {
                self.classes
                    .get(&s.to_ascii_lowercase())
                    .map_or_else(|| s.clone(), |d| d.name.clone())
            }),
        };
        if let PhpObj::Closure {
            captured,
            bound_this,
            scope: s,
            called_scope,
            ..
        } = &mut dup
        {
            self.fork_closure_statics(captured);
            *bound_this = this;
            *s = scope;
            *called_scope = new_called;
        }
        self.objs.push(dup);
        Some(Value::Obj((self.objs.len() - 1) as u32))
    }

    /// Whether `v` is a closure handle.
    /// `zend_get_callable_name_ex` for a closure: the `{closure:<where>:<line>}`
    /// name PHP 8.4 gives the literal, as `ob_list_handlers()` reports it.
    pub fn closure_callable_name(&self, v: &Value) -> Option<String> {
        let cc = self.closure_of(v)?;
        Some(
            cc.site
                .map_or_else(|| "{closure}".to_string(), |s| s.render(&self.script_name)),
        )
    }

    pub fn is_closure(&self, v: &Value) -> bool {
        matches!(self.as_array(v), Some(PhpObj::Closure { .. }))
    }

    /// The closure's current private-access scope class, if any (for `bindTo`'s
    /// default "keep the current scope" behavior).
    fn closure_scope(&self, v: &Value) -> Option<String> {
        match self.as_array(v) {
            Some(PhpObj::Closure { scope, .. }) => scope.clone(),
            _ => None,
        }
    }

    /// Whether the closure was declared `static` (`ZEND_ACC_STATIC`).
    /// Whether `v` is a closure built by first-class callable syntax
    /// (`strlen(...)`) — the reference's `ZEND_ACC_FAKE_CLOSURE`, which the
    /// lowering marks with its [`FCC_PARAM`] variadic.
    pub fn closure_is_fake(&self, v: &Value) -> bool {
        matches!(
            self.as_array(v),
            Some(PhpObj::Closure { params, .. }) if params.first().is_some_and(|p| p.name == FCC_PARAM)
        )
    }

    fn closure_is_static(&self, v: &Value) -> bool {
        matches!(
            self.as_array(v),
            Some(PhpObj::Closure {
                is_static: true,
                ..
            })
        )
    }

    /// Whether the declared class `lower` is one the reference ships
    /// (`ZEND_INTERNAL_CLASS`): an engine type, or a PHP-written prelude class
    /// standing in for one.
    fn is_internal_class(&self, lower: &str) -> bool {
        crate::prelude_type(lower).is_some() || !self.classes.contains_key(lower)
    }

    // ── objects / classes ──────────────────────────────────────────────────

    pub fn is_object(&self, v: &Value) -> bool {
        matches!(self.as_array(v), Some(PhpObj::Object { .. }))
    }

    /// The heap handle of any object/array/resource value — a stable per-instance
    /// id (`spl_object_id`). `None` for non-heap values.
    ///
    /// This is the same number `var_dump` prints as `#N`, as it is in PHP —
    /// both are the object's handle — so it is `object_ordinal`, not the raw
    /// heap index, which also counts arrays, closures and resources.
    pub fn object_id(&self, v: &Value) -> Option<i64> {
        match v {
            Value::Obj(_) if self.is_object_value(v) => Some(self.object_ordinal(v) as i64),
            Value::Obj(h) => Some(*h as i64),
            _ => None,
        }
    }

    /// The 1-based creation-order number PHP shows as `#N` in `var_dump`.
    ///
    /// PHP numbers objects only — class instances, closures and generators — so count those
    /// entries up to and including this handle rather than using the raw heap
    /// index (which also covers arrays and resources).
    ///
    /// KNOWN DIVERGENCE: PHP frees an object's handle when its refcount drops to
    /// zero and hands the number to the next allocation, so a program that
    /// discards an object sees a *lower* number afterwards than phplang, which
    /// allocates from an append-only arena and never frees. Closing this needs
    /// refcounted handles and a free list ordered the way PHP's allocator orders
    /// its own — substrate that does not exist here — so the numbers agree only
    /// while no object has become unreachable.
    pub fn object_ordinal(&self, v: &Value) -> usize {
        let Value::Obj(h) = v else { return 0 };
        self.objs
            .iter()
            .enumerate()
            .take(*h as usize + 1)
            // A closure or a generator is an object to the reference and takes
            // a number too.
            .filter(|(i, o)| {
                matches!(
                    o,
                    PhpObj::Object { .. } | PhpObj::Closure { .. } | PhpObj::Generator { .. }
                ) && !self.transient_objs.contains(&(*i as u32))
            })
            .count()
    }

    /// The class name of an object handle, or `None` if `v` is not an object.
    ///
    /// STRUCTURAL: only a class instance answers here. Every call site that asks
    /// "what does PHP think this value's class is" wants [`Self::instance_class`]
    /// instead — a closure and a generator are objects of class `Closure` and
    /// `Generator` to a PHP program, but neither is a `PhpObj::Object`.
    pub fn object_class(&self, v: &Value) -> Option<String> {
        match self.as_array(v) {
            Some(PhpObj::Object { class, .. }) => Some(class.clone()),
            _ => None,
        }
    }

    /// The class name PHP reports for `v`, or `None` when `v` is not an object at
    /// all (a scalar, an array, a resource).
    ///
    /// This is the reflection surface's view, and it is wider than
    /// [`Self::object_class`]: a closure is an instance of `Closure` and a
    /// generator an instance of `Generator`, so `get_class`, `::class`,
    /// `instanceof`, `is_a`, `method_exists`, `get_debug_type` and every
    /// diagnostic that names a value's type must go through here. Before this
    /// existed each of those either special-cased `is_closure` by hand or missed
    /// the case, which is why `get_class($gen)` was a `TypeError` reading
    /// "must be of type object, object given".
    pub fn instance_class(&self, v: &Value) -> Option<String> {
        match self.as_array(v) {
            Some(PhpObj::Object { class, .. }) => Some(class.clone()),
            Some(PhpObj::Closure { .. }) => Some("Closure".to_string()),
            Some(PhpObj::Generator { .. }) => Some("Generator".to_string()),
            _ => None,
        }
    }

    /// Whether `v` is an object to a PHP program — `is_object()`'s answer.
    /// True for class instances, closures and generators; false for arrays.
    pub fn is_object_value(&self, v: &Value) -> bool {
        matches!(
            self.as_array(v),
            Some(PhpObj::Object { .. } | PhpObj::Closure { .. } | PhpObj::Generator { .. })
        )
    }

    // ── reflection support (for the `reflection` stdlib module) ──────────────

    /// Whether a type of the given name is declared (case-insensitive) — a
    /// class, an interface, a trait or an enum. The kind is not distinguished
    /// here because most callers only need "is this name resolvable"; the
    /// reflection builtins ask [`Self::type_kind`] instead.
    pub fn class_exists(&self, name: &str) -> bool {
        let lower = unanchored(name).to_ascii_lowercase();
        self.classes.contains_key(&lower) || builtin_type_kind(&lower).is_some()
    }

    /// Which of PHP's four declaration kinds `name` is, or `None` if the name is
    /// not declared. `class_exists`/`interface_exists`/`trait_exists`/
    /// `enum_exists` each answer for exactly one kind, so they cannot share the
    /// "is it declared at all" test: an `interface I {}` makes `class_exists('I')`
    /// FALSE in PHP even though the name resolves everywhere else.
    pub fn type_kind(&self, name: &str) -> Option<TypeKind> {
        let lower = unanchored(name).to_ascii_lowercase();
        match self.classes.get(&lower) {
            Some(d) if d.is_trait => Some(TypeKind::Trait),
            Some(d) if d.is_interface => Some(TypeKind::Interface),
            Some(d) if d.is_enum => Some(TypeKind::Enum),
            Some(_) => Some(TypeKind::Class),
            None => builtin_type_kind(&lower),
        }
    }

    /// Every interface `class` implements, in declared spelling and in the order
    /// the reference lays out `ce->interfaces` (`zend_do_implement_interfaces`):
    /// the parent's list, then the interfaces the class names itself — with
    /// `Stringable` appended when it declares `__toString` — then each of those
    /// interfaces' own parents, last to first. For `class_implements`.
    pub fn class_interface_names(&self, class: &str) -> Vec<String> {
        self.interface_list(&class.to_ascii_lowercase(), 0)
    }

    fn interface_list(&self, c: &str, depth: usize) -> Vec<String> {
        let Some(def) = self.classes.get(c) else {
            // An engine type: its ancestry is the built-in table's, and every
            // ancestor there is an interface.
            return builtin_type_parents(c)
                .iter()
                .map(|p| (*p).to_string())
                .collect();
        };
        if depth > 64 {
            return Vec::new();
        }
        let mut out = match &def.parent {
            Some(p) => self.interface_list(&p.to_ascii_lowercase(), depth + 1),
            None => Vec::new(),
        };
        let mut declared: Vec<String> = def
            .interfaces
            .iter()
            .map(|i| i.to_ascii_lowercase())
            .collect();
        if def.methods.contains_key("__tostring") && !declared.iter().any(|d| d == "stringable") {
            declared.push("stringable".to_string());
        }
        // A class that implements nothing of its own lists what it inherits
        // in reverse — measured: `class Q extends B {}` over `B implements S,
        // U` answers `U, S`, and one more level of the same answers `S, U`.
        if declared.is_empty() && def.parent.is_some() {
            out.reverse();
        }
        let spell = |lower: &str| {
            self.classes
                .get(lower)
                .map(|d| d.name.clone())
                .unwrap_or_else(|| {
                    builtin_type(lower)
                        .map_or_else(|| lower.to_string(), |(n, ..)| (*n).to_string())
                })
        };
        let push = |out: &mut Vec<String>, name: String| {
            if !out.iter().any(|o| o.eq_ignore_ascii_case(&name)) {
                out.push(name);
            }
        };
        for d in &declared {
            push(&mut out, spell(d));
        }
        for d in &declared {
            for inherited in self.interface_list(d, depth + 1).into_iter().rev() {
                push(&mut out, inherited);
            }
        }
        // A class the reference ships lists its interfaces in the order ext/*
        // registered them, measured into `internal_interfaces`; one this build
        // implements beyond that list keeps its place after it.
        if crate::prelude_type(c).is_some() {
            use crate::internal_interfaces::INTERNAL_INTERFACES as TABLE;
            if let Ok(i) = TABLE.binary_search_by(|&(k, _)| k.cmp(c)) {
                let order = TABLE[i].1;
                out.sort_by_key(|n| {
                    order
                        .iter()
                        .position(|o| o.eq_ignore_ascii_case(n))
                        .unwrap_or(order.len())
                });
            }
        }
        out
    }

    /// The traits `class` composes with `use`, in source order and declared
    /// spelling. For `class_uses`, which reports only THIS class's `use` clauses
    /// (PHP does not walk the parent chain for it).
    pub fn class_trait_names(&self, class: &str) -> Vec<String> {
        self.classes
            .get(&class.to_ascii_lowercase())
            .map(|d| d.uses.clone())
            .unwrap_or_default()
    }

    /// The PHP fatal-error message if `class` cannot be instantiated with `new`
    /// (it is `abstract`, an `interface`, a `trait` or an `enum`), else `None`. The original casing is
    /// used in the message.
    fn class_instantiation_error(&self, class: &str) -> Option<String> {
        let def = self.classes.get(&class.to_ascii_lowercase())?;
        // Four kinds cannot be instantiated, and the reference names each by
        // its OWN keyword so a caller catching the `Error` can tell them apart
        let kind = if def.is_interface {
            "interface"
        } else if def.is_trait {
            "trait"
        } else if def.is_enum {
            "enum"
        } else if def.is_abstract {
            "abstract class"
        } else {
            return None;
        };
        Some(format!(
            "Cannot instantiate {kind} {}",
            display_class(class)
        ))
    }

    /// Whether a user function of the given name is defined (case-insensitive).
    pub fn function_defined(&self, name: &str) -> bool {
        let name = name.strip_prefix('\\').unwrap_or(name);
        self.functions.contains_key(&name.to_ascii_lowercase())
    }

    /// The declared type of property `name` as seen from `class`, with the class
    /// (display spelling) that declares it — the nearest along the parent chain.
    pub fn prop_type(&self, class: &str, name: &str) -> Option<(String, TypeHint)> {
        let mut cur = Some(class.to_ascii_lowercase());
        while let Some(c) = cur {
            let def = self.classes.get(&c)?;
            if let Some(ty) = def.prop_types.get(name) {
                return Some((def.name.clone(), ty.clone()));
            }
            if def.prop_vis.contains_key(name) {
                return None;
            }
            cur = def.parent.as_ref().map(|p| p.to_ascii_lowercase());
        }
        None
    }

    /// The declared parent-class name of `name`, or `None` (no parent / unknown).
    pub fn class_parent(&self, name: &str) -> Option<String> {
        self.classes
            .get(&name.to_ascii_lowercase())
            .and_then(|d| d.parent.clone())
    }

    /// Whether `class` is `target` or descends from it (case-insensitive);
    /// `target == "Throwable"` matches either exception root. The public form of
    /// the catch-matching walk, for `is_a`/`is_subclass_of`/`instanceof`.
    pub fn is_a_class(&self, class: &str, target: &str) -> bool {
        self.catch_matches(class, unanchored(target))
    }

    /// Method names visible on `class`, walking the parent chain (lowercased, as
    /// stored). For `get_class_methods`.
    /// `get_class_methods`: the methods `class` has, in its function table's
    /// order — its own and its traits', then each ancestor's that it does not
    /// override — keeping those the calling scope may call
    /// (`zend_check_method_accessible`). DIVERGENCE: a name is lowercased
    /// rather than spelled as declared — `tests/stdlib_reflection.rs` pins that
    /// (see BUGS.md). An enum's synthesized `cases`, `from`
    /// and `tryFrom` follow its own methods. The prelude's helpers, which the
    /// internal classes it stands in for do not have, are left out.
    pub fn class_methods_visible(&self, class: &str) -> Vec<String> {
        let scope = self.current_class_ctx().map(|s| s.to_ascii_lowercase());
        let mut seen: Vec<String> = Vec::new();
        let mut out = Vec::new();
        let mut cur = Some(class.to_ascii_lowercase());
        while let Some(c) = cur {
            let Some(def) = self.classes.get(&c) else {
                out.extend(builtin_type_methods(&c).iter().map(|m| m.to_string()));
                break;
            };
            let mut names: Vec<String> = def.method_sites.iter().map(|(n, _)| n.clone()).collect();
            if def.is_enum {
                names.push("cases".to_string());
                if def
                    .interfaces
                    .iter()
                    .any(|i| i.eq_ignore_ascii_case("BackedEnum"))
                {
                    names.push("from".to_string());
                    names.push("tryFrom".to_string());
                }
            }
            for name in names {
                let lname = name.to_ascii_lowercase();
                if seen.contains(&lname) {
                    continue;
                }
                seen.push(lname.clone());
                if crate::prelude_type(&c).is_some()
                    && (is_prelude_helper_name(&format!("{c}::{name}"))
                        || self.spl_array_forwarder(&c, &lname))
                {
                    continue;
                }
                let accessible = match def.method_vis.get(&lname) {
                    Some(Visibility::Private) => scope.as_deref() == Some(c.as_str()),
                    Some(Visibility::Protected) => scope
                        .as_deref()
                        .is_some_and(|s| self.class_is_a(s, &c) || self.class_is_a(&c, s)),
                    _ => true,
                };
                if accessible {
                    out.push(lname);
                }
            }
            cur = def.parent.as_ref().map(|p| p.to_ascii_lowercase());
        }
        out
    }

    pub fn class_method_names(&self, class: &str) -> Vec<String> {
        let mut out = Vec::new();
        let mut cur = Some(class.to_ascii_lowercase());
        while let Some(c) = cur {
            let Some(def) = self.classes.get(&c) else {
                // An engine type has no `ClassDef`; its methods are listed in
                // `BUILTIN_TYPES`, in PHP's own spelling.
                out.extend(builtin_type_methods(&c).iter().map(|m| m.to_string()));
                break;
            };
            for m in def.methods.keys() {
                if !out.contains(m) {
                    out.push(m.clone());
                }
            }
            cur = def.parent.as_ref().map(|p| p.to_ascii_lowercase());
        }
        out
    }

    /// Whether `class` (or an ancestor) defines `method` (case-insensitive).
    pub fn class_has_method(&self, class: &str, method: &str) -> bool {
        if self.method_declared(class, &method.to_ascii_lowercase()) {
            return true;
        }
        // An engine type (`Generator`, `Closure`, `Iterator`, …) has no
        // `ClassDef` for `resolve_method` to walk.
        builtin_type_methods(&class.to_ascii_lowercase())
            .iter()
            .any(|m| m.eq_ignore_ascii_case(method))
    }

    /// `ZEND_FUNCTION(property_exists)` (`Zend/zend_builtin_functions.c`) for
    /// an object or a class name.
    ///
    /// The class's `properties_info` answers first: any declaration along the
    /// chain — instance or static, a promoted constructor parameter included —
    /// except a `private` one an ANCESTOR declared. Failing that, an object is
    /// asked `has_property(ZEND_PROPERTY_EXISTS)`: the property is on the
    /// instance and reachable from the calling scope, which is how a dynamic
    /// property counts and how an ancestor's private one counts from inside
    /// that ancestor.
    pub fn property_exists(&self, target: &Value, name: &str) -> bool {
        let is_obj = self.is_object(target);
        let class = if is_obj {
            self.object_class(target).unwrap_or_default()
        } else {
            // `zend_lookup_class` drops one leading backslash.
            let written = self.to_str(target);
            written
                .strip_prefix('\\')
                .map_or_else(|| written.clone(), str::to_string)
        };
        let decl = self.resolve_prop_vis(&class, name);
        if let Some((declaring, vis)) = &decl {
            if *vis != Visibility::Private || declaring.eq_ignore_ascii_case(&class) {
                return true;
            }
        }
        is_obj
            && self.object_props(target).iter().any(|(n, _)| n == name)
            && decl.is_none_or(|(declaring, vis)| self.visibility_allows(vis, &declaring))
    }

    /// How `print_r` and `serialize` decorate a property name that is not public:
    /// `print_r` writes `[b:protected]` / `[c:Declaring:private]`, and `serialize`
    /// writes `\0*\0b` / `\0Declaring\0c`. Both need the DECLARING class in its
    /// source spelling, which is why this returns the name rather than a flag.
    ///
    /// `None` for a public or undeclared (dynamic) property — the two cases that
    /// print bare.
    pub fn prop_visibility(&self, class: &str, name: &str) -> Option<(String, Visibility)> {
        let (declaring, vis) = self.resolve_prop_vis(class, name)?;
        if matches!(vis, Visibility::Public) {
            return None;
        }
        let spelled = self
            .classes
            .get(&declaring)
            .map(|d| d.name.clone())
            .unwrap_or(declaring);
        Some((spelled, vis))
    }

    /// An `enum` case singleton's `(case name, backing value)`; the value is
    /// `None` for a pure enum. `None` if `v` is not an enum case at all.
    ///
    /// Every renderer needs this: an enum does not print like the object it is
    /// implemented as (`var_dump` writes `enum(Suit::Hearts)`, `json_encode`
    /// writes the backing value, `serialize` writes `E:9:"Suit:Hearts";`).
    pub fn enum_case_of(&self, v: &Value) -> Option<(String, Option<Value>)> {
        let Some(PhpObj::Object { class, props }) = self.as_array(v) else {
            return None;
        };
        if !self.is_enum_class(class) {
            return None;
        }
        let name = props.get("name").map(|n| self.to_str(n))?;
        Some((name, props.get("value").map(|x| self.deref(x.clone()))))
    }

    /// An object's `(name, value)` properties in insertion order. For
    /// `get_object_vars`; empty if `v` is not an object.
    pub fn object_props(&self, v: &Value) -> Vec<(String, Value)> {
        match self.as_array(v) {
            Some(PhpObj::Object { props, .. }) => props
                .iter()
                .map(|(k, v)| (k.clone(), self.deref(v.clone())))
                .collect(),
            _ => Vec::new(),
        }
    }

    /// An object's properties that are *visible from the current scope* — what
    /// `get_object_vars` returns. Called from outside the class, that is the
    /// public ones only; called from inside a method of the class, the private
    /// and protected ones as well.
    pub fn object_props_visible(&self, v: &Value) -> Vec<(String, Value)> {
        let Some(class) = self.object_class(v) else {
            return Vec::new();
        };
        self.object_props(v)
            .into_iter()
            .filter(|(name, _)| match self.resolve_prop_vis(&class, name) {
                Some((declaring, vis)) => self.visibility_allows(vis, &declaring),
                // A dynamic property was never declared, so it is public.
                None => true,
            })
            .collect()
    }

    /// An object's properties keyed the way `(array)` spells them: PHP mangles a
    /// non-public name so the cast cannot silently collide two properties of the
    /// same name from different visibilities. A private `$p` declared by `C`
    /// becomes `"\0C\0p"`, a protected one `"\0*\0p"`, and a public one stays as
    /// it is. The NUL bytes are why such a key cannot be reached with `$arr['p']`.
    pub fn object_props_mangled(&self, v: &Value) -> Vec<(String, Value)> {
        let class = self.object_class(v).unwrap_or_default();
        self.object_props(v)
            .into_iter()
            .map(|(name, val)| {
                let key = match self.declaring_class_of_prop(&class, &name) {
                    // The mangled name carries the declaring class *as written*.
                    Some((declaring, Visibility::Private)) => format!("\0{declaring}\0{name}"),
                    Some((_, Visibility::Protected)) => format!("\0*\0{name}"),
                    _ => name,
                };
                (key, val)
            })
            .collect()
    }

    /// [`resolve_prop_vis`] with the declaring class in its source spelling,
    /// which is the form `(array)`'s mangled key needs. Class definitions are
    /// keyed by lowercase name but the `parent` link keeps its original case, so
    /// the walk carries the declared name alongside the lookup key.
    fn declaring_class_of_prop(&self, class: &str, name: &str) -> Option<(String, Visibility)> {
        let mut cur = Some(class.to_string());
        while let Some(c) = cur {
            let def = self.classes.get(&c.to_ascii_lowercase())?;
            if let Some(v) = def.prop_vis.get(name) {
                return Some((c, *v));
            }
            cur = def.parent.clone();
        }
        None
    }

    /// The object's `(name, value, is_reference)` triples — the property form of
    /// `array_pairs_marked`, for `var_dump`'s `&` marker.
    pub fn object_props_marked(&self, v: &Value) -> Vec<(String, Value, bool)> {
        match self.as_array(v) {
            Some(PhpObj::Object { props, .. }) => props
                .iter()
                .map(|(k, v)| {
                    let is_ref = self.ref_slot_of_value(v).is_some();
                    (k.clone(), self.deref(v.clone()), is_ref)
                })
                .collect(),
            _ => Vec::new(),
        }
    }

    // ── stream resources ─────────────────────────────────────────────────────

    /// Allocate a stream resource, numbering it from the resource counter.
    /// `php://temp` takes two numbers — the reference opens an inner memory
    /// stream behind it — and the outer stream gets the first.
    pub fn new_stream(&mut self, mut s: crate::stdlib::fileres::Stream) -> Value {
        s.id = self.next_resource_id;
        self.next_resource_id += if s.kind == crate::stdlib::fileres::StreamKind::Temp {
            2
        } else {
            1
        };
        self.objs.push(PhpObj::Resource(Box::new(s)));
        Value::Obj((self.objs.len() - 1) as u32)
    }

    /// The stream behind `v`, open or closed.
    pub fn stream_any(&self, v: &Value) -> Option<&crate::stdlib::fileres::Stream> {
        match self.as_array(v) {
            Some(PhpObj::Resource(s)) => Some(s),
            _ => None,
        }
    }

    /// The OPEN stream behind `v`; a closed one answers `None`.
    pub fn stream(&self, v: &Value) -> Option<&crate::stdlib::fileres::Stream> {
        self.stream_any(v).filter(|s| !s.closed)
    }

    /// The open stream behind `v`, mutably.
    pub fn stream_mut(&mut self, v: &Value) -> Option<&mut crate::stdlib::fileres::Stream> {
        match self.as_array_mut(v) {
            Some(PhpObj::Resource(s)) if !s.closed => Some(s),
            _ => None,
        }
    }

    /// Write into a buffered stream. A `php://temp` stream that reaches its
    /// `maxmemory` moves to a temporary file in the reference, which opens one
    /// more resource: the number is consumed here so later ones line up.
    pub fn stream_write(&mut self, v: &Value, bytes: &[u8]) {
        let mut spilled = false;
        if let Some(s) = self.stream_mut(v) {
            if let Some(limit) = s.spill_at {
                if s.buf.len() + bytes.len() >= limit {
                    s.spill_at = None;
                    spilled = true;
                }
            }
            s.write_at_cursor(bytes);
        }
        if spilled {
            self.next_resource_id += 1;
        }
    }

    /// Spend a resource number without keeping a stream: what a whole-file
    /// function does when it opens, reads and closes one internally.
    pub fn consume_resource_id(&mut self) {
        self.next_resource_id += 1;
    }

    /// `(resource number, closed)` for a stream value.
    pub fn resource_info(&self, v: &Value) -> Option<(i64, bool)> {
        self.stream_any(v).map(|s| (s.id, s.closed))
    }

    /// Whether `v` is an open resource (`is_resource`).
    pub fn is_resource(&self, v: &Value) -> bool {
        self.stream(v).is_some()
    }

    /// A stream resource, open or closed: a closed one is still of type
    /// resource, just no longer usable.
    pub fn is_stream(&self, v: &Value) -> bool {
        self.stream_any(v).is_some()
    }

    /// Write to the process's standard output, past any output buffering —
    /// what `fwrite(STDOUT, …)` does. An embedding host's capture buffer still
    /// receives it, since that buffer IS its standard output.
    pub fn write_stdout_direct(&mut self, s: &str) {
        if let Some(buf) = &mut self.capture {
            buf.push_str(s);
        } else {
            use std::io::Write;
            let _ = std::io::stdout().write_all(&crate::bytes::to_bytes(s));
        }
    }

    /// `$obj->name` read (`Undef` if the object lacks the property).
    /// Whether the object `recv` carries a property called `name` (declared
    /// or dynamic, initialized or not), whatever its visibility.
    pub fn obj_has_prop(&self, recv: &Value, name: &str) -> bool {
        matches!(self.as_array(recv), Some(PhpObj::Object { props, .. }) if props.contains_key(name))
    }

    pub fn prop_get(&self, recv: &Value, name: &str) -> Value {
        match self.as_array(recv) {
            Some(PhpObj::Object { props, .. }) => {
                let raw = props.get(name).cloned().unwrap_or(Value::Undef);
                self.deref(raw)
            }
            _ => Value::Undef,
        }
    }

    /// `$obj->name = val` — mutates the shared instance behind the handle. A
    /// property a `&` binding has turned into a reference is written through its
    /// cell rather than replaced, so the alias observes the write.
    pub fn prop_set(&mut self, recv: &Value, name: &str, val: Value) {
        if let Some(slot) = self.prop_ref_slot(recv, name) {
            self.ref_cell_set(slot, val);
            return;
        }
        self.obj_put_prop(recv, name, val);
    }

    /// Why a property WRITE on `recv` is refused, or `None` when `recv` is an
    /// object that can take it. `verb` names the write the way the reference's
    /// message does: `assign`, `increment/decrement`, or `modify` for a fetch
    /// that goes on to write deeper (`$x->p[] = 1`, `$x->p->q = 1`).
    ///
    /// A closure and a generator are objects that refuse every dynamic property;
    /// anything else that is not an object names its type — a bool by its value,
    /// as `zend_zval_value_name` spells it.
    pub fn prop_write_refusal(&self, recv: &Value, name: &str, verb: &str) -> Option<String> {
        let ty = match recv {
            Value::Bool(true) => "true",
            Value::Bool(false) => "false",
            Value::Obj(_) => match self.as_array(recv) {
                // An enum case is immutable: its `name`/`value` are readonly
                // and it takes no dynamic property (`zend_enum_register_props`
                // declares them readonly; enums are NO_DYNAMIC_PROPERTIES).
                Some(PhpObj::Object { class, props }) if self.is_enum_class(class) => {
                    let shown = self.class_display_name(&class.to_ascii_lowercase());
                    return Some(if props.contains_key(name) {
                        // A write THROUGH the property (`$e->value[] = 1`) is
                        // refused in the indirect wording.
                        let how = if verb == "modify" {
                            "indirectly modify"
                        } else {
                            "modify"
                        };
                        format!("Cannot {how} readonly property {shown}::${name}")
                    } else {
                        format!("Cannot create dynamic property {shown}::${name}")
                    });
                }
                // `WeakMap` and `WeakReference` are `ZEND_ACC_NO_DYNAMIC_PROPERTIES`.
                Some(PhpObj::Object { class, .. })
                    if matches!(
                        class.to_ascii_lowercase().as_str(),
                        "weakmap" | "weakreference"
                    ) && self.resolve_prop_vis(class, name).is_none() =>
                {
                    return Some(format!(
                        "Cannot create dynamic property {}::${name}",
                        self.class_display_name(&class.to_ascii_lowercase())
                    ));
                }
                Some(PhpObj::Object { .. } | PhpObj::Ref { .. }) | None => return None,
                Some(PhpObj::Closure { .. }) => {
                    return Some(format!("Cannot create dynamic property Closure::${name}"));
                }
                Some(PhpObj::Generator { .. }) => {
                    return Some(format!("Cannot create dynamic property Generator::${name}"));
                }
                Some(PhpObj::Resource(_)) => "resource",
                Some(PhpObj::Array { .. }) => "array",
            },
            other => self.diag_type(other),
        };
        Some(format!("Attempt to {verb} property \"{name}\" on {ty}"))
    }

    /// `$obj->name = val` from PHP source, with the PHP 8.2 deprecation for
    /// creating a property the class never declared.
    ///
    /// Split from `prop_set` because the engine's own writes must NOT warn:
    /// `seed_throwable` stamps `file`/`line`/`trace` onto every Throwable, and an
    /// internal write is not the user creating a dynamic property. Only the
    /// opcodes that lower a source-level `->` assignment come through here.
    pub fn prop_set_checked(&mut self, recv: &Value, name: &str, val: Value) {
        self.warn_dynamic_prop(recv, name);
        self.prop_set(recv, name, val);
    }

    /// Raise `Creation of dynamic property C::$p is deprecated` if writing `name`
    /// on `recv` would create one.
    ///
    /// It creates one when the object does not already carry the property AND no
    /// class in its chain declares it. `stdClass` is exempt — it is PHP's
    /// property bag, and `(object)` casts and `json_decode` produce it — as is any
    /// class opted in with `#[AllowDynamicProperties]`, itself or inherited.
    pub fn warn_dynamic_prop(&mut self, recv: &Value, name: &str) {
        let Some(PhpObj::Object { class, props }) = self.as_array(recv) else {
            return;
        };
        if props.contains_key(name) || class.eq_ignore_ascii_case("stdClass") {
            return;
        }
        let class = class.clone();
        if self.prop_is_declared(&class, name) || self.allows_dynamic_props(&class) {
            return;
        }
        self.deprecated(format_args!(
            "Creation of dynamic property {class}::${name} is deprecated"
        ));
    }

    /// Whether `class` or any ancestor declares a property called `name`.
    /// A declaration with no initializer still counts, so `prop_vis` — which every
    /// declaration writes — is the authority, not the defaults list.
    pub(crate) fn prop_is_declared(&self, class: &str, name: &str) -> bool {
        self.class_chain(class).any(|d| {
            d.prop_vis.contains_key(name) || d.prop_defaults.iter().any(|(p, _)| p == name)
        })
    }

    /// Whether `class` or any ancestor carries `#[AllowDynamicProperties]`.
    fn allows_dynamic_props(&self, class: &str) -> bool {
        self.class_chain(class).any(|d| d.allow_dynamic_props)
    }

    /// The class and its ancestors, innermost first. Stops at an undeclared
    /// parent rather than looping, so a broken chain cannot hang a lookup.
    fn class_chain<'a>(&'a self, class: &str) -> impl Iterator<Item = &'a ClassDef> + 'a {
        let mut cur = Some(class.to_ascii_lowercase());
        std::iter::from_fn(move || {
            let def = self.classes.get(&cur.take()?)?;
            cur = def.parent.as_ref().map(|p| p.to_ascii_lowercase());
            Some(def)
        })
    }

    /// The reference slot `$obj->name` holds, if that property is a reference.
    fn prop_ref_slot(&self, recv: &Value, name: &str) -> Option<usize> {
        let Some(PhpObj::Object { props, .. }) = self.as_array(recv) else {
            return None;
        };
        self.ref_slot_of_value(props.get(name)?)
    }

    /// The container a dimension write into `$obj->name` continues on
    /// (`$this->items[] = x`, `$this->map[$k] = v`): the array handle it
    /// holds, an empty array stored there in place of an unset or `null`
    /// property, or — for `false` — after the auto-vivification deprecation.
    /// Anything else (an object, a string, a scalar) is handed back as it is,
    /// for the path write to dispatch to (`ArrayAccess`), write into (a string
    /// offset) or refuse.
    /// What a property fetched for a dimension write keeps holding: `Some`
    /// unless it is unset, `null` or `false`, which the write replaces with an
    /// empty array (`false` with a deprecation, raised here).
    pub fn dim_container_kept(&mut self, cur: &Value) -> Option<Value> {
        match cur {
            Value::Undef => None,
            Value::Bool(false) => {
                self.deprecated("Automatic conversion of false to array is deprecated");
                None
            }
            _ => Some(cur.clone()),
        }
    }

    pub fn prop_ensure_array(&mut self, recv: &Value, name: &str) -> Value {
        let cur = self.prop_get(recv, name);
        if let Some(kept) = self.dim_container_kept(&cur) {
            return kept;
        }
        let arr = self.new_array();
        self.prop_set(recv, name, arr.clone());
        arr
    }

    /// `$o->name[...] = v` on an ARRAY_AS_PROPS `ArrayObject`/`ArrayIterator`
    /// whose `offsetGet` is the prelude's: `spl_array_get_property_ptr_ptr`
    /// hands out the storage element itself (`spl_array_get_dimension_ptr`),
    /// vivifying a missing or `null` one to an array, so the write lands in the
    /// storage. A scalar element is handed back for the write to refuse. `None`
    /// when the reference would go through `offsetGet` instead.
    pub fn spl_array_dim_for_write(&mut self, recv: &Value, name: &str) -> Option<Value> {
        let class = self.instance_class(recv)?;
        let native_get = matches!(
            self.method_owner_key(&class, "offsetget").as_deref(),
            Some("arrayobject" | "arrayiterator")
        );
        if !self.spl_array_forwarder(&class, "__get") || !native_get {
            return None;
        }
        let storage = self.prop_get(recv, "storage");
        if !self.is_array(&storage) {
            return None;
        }
        let key = Value::str(name);
        let cur = self.index_get(&storage, &key);
        if !matches!(cur, Value::Undef) {
            return Some(cur);
        }
        let arr = self.new_array();
        self.arr_set_key(&storage, &key, arr.clone());
        Some(arr)
    }

    /// Whether `class` or an ancestor declares `method` (already lowercased),
    /// WITHOUT materializing the definition.
    ///
    /// [`PhpHost::resolve_method`] clones the whole [`FuncDef`] — including the
    /// method body's `Chunk`, which owns its ops, constants, names, line table
    /// and nested sub-chunks — so asking it a yes/no question copies an entire
    /// compiled method to throw it away. Every ordinary `$o->m()` asked twice:
    /// once through [`PhpHost::method_dispatch`] to decide whether the call is
    /// allowed, then again in [`call_method`] for the definition it actually
    /// runs. This answers the first question with a `contains_key` per class in
    /// the chain and no allocation, leaving one clone on the path.
    fn method_declared(&self, class: &str, method: &str) -> bool {
        let mut cur = Some(class.to_ascii_lowercase());
        while let Some(c) = cur {
            let Some(def) = self.classes.get(&c) else {
                return false;
            };
            if def.methods.contains_key(method) {
                return true;
            }
            cur = def.parent.as_ref().map(|p| p.to_ascii_lowercase());
        }
        false
    }

    /// The by-reference parameter at 1-based `argno` of method `method` on
    /// `recv` (an object, or a class name), as `(Class::method, parameter)`, or
    /// `None` when the method does not resolve or that parameter is by value.
    /// The class named is the one that DECLARES the method. A variadic tail is
    /// left alone.
    pub fn method_byref_param(
        &self,
        recv: &Value,
        method: &str,
        argno: usize,
    ) -> Option<(String, String)> {
        let class = match recv {
            Value::Obj(_) => self.object_class(recv)?,
            other => self.to_str(other),
        };
        let (decl, def) = self.resolve_method(&class, &method.to_ascii_lowercase())?;
        let p = def.params.get(argno.checked_sub(1)?)?;
        (p.by_ref && !p.variadic).then(|| {
            (
                format!("{}::{method}", self.class_display_name(&decl)),
                p.name.clone(),
            )
        })
    }

    /// The lowercased class declaring the method `obj` runs for `lname`.
    pub(crate) fn method_owner(&self, obj: &Value, lname: &str) -> Option<String> {
        let class = self.instance_class(obj)?;
        self.resolve_method(&class, &lname.to_ascii_lowercase())
            .map(|(c, _)| c)
    }

    /// Resolve a method by walking the class up its parent chain; returns the
    /// defining class name plus the method definition.
    fn resolve_method(&self, class: &str, method: &str) -> Option<(String, FuncDef)> {
        let mut cur = Some(class.to_ascii_lowercase());
        while let Some(c) = cur {
            let def = self.classes.get(&c)?;
            if let Some(m) = def.methods.get(method) {
                return Some((c, m.clone()));
            }
            cur = def.parent.as_ref().map(|p| p.to_ascii_lowercase());
        }
        None
    }

    /// The class whose method is currently executing (for visibility checks),
    /// derived from the innermost frame's `Class::method` name. `None` in the
    /// global scope, a free function, or a closure (frame names without `::`).
    fn current_class_ctx(&self) -> Option<String> {
        if let Some(c) = self.init_class_scope.last() {
            return Some(c.clone());
        }
        let name = self.scopes.last()?.name.as_ref()?;
        name.rsplit_once("::").map(|(cls, _)| cls.to_string())
    }

    /// `__CLASS__` for the frame that is running, used where the parse could not
    /// name the class: a trait method (whose `__CLASS__` is the class that USED
    /// the trait) and an anonymous class (whose name the compiler mints).
    ///
    /// The DECLARED spelling is returned, and the whole of it — an anonymous
    /// class keeps the NUL-separated unique tail, which is what `get_class`
    /// reports for it too, and `__CLASS__` and `get_class($this)` agree in the
    /// reference.
    pub fn magic_class(&self) -> String {
        let Some(cls) = self.current_class_ctx() else {
            return String::new();
        };
        self.classes.get(&cls).map_or(cls, |d| d.name.clone())
    }

    /// The parent of the class the running frame belongs to, in its declared
    /// spelling. Empty when there is no frame class or it extends nothing.
    pub fn magic_parent_class(&self) -> String {
        let Some(cls) = self.current_class_ctx() else {
            return String::new();
        };
        // Lowercased before the lookup: `current_class_ctx` can hand back the
        // DECLARED spelling (a closure bound with `->call()` does), and the
        // class map is keyed by the lowercased name.
        let Some(parent) = self
            .classes
            .get(&cls.to_ascii_lowercase())
            .and_then(|d| d.parent.clone())
        else {
            return String::new();
        };
        self.classes
            .get(&parent.to_ascii_lowercase())
            .map_or(parent, |d| d.name.clone())
    }

    /// `__DIR__` — the directory the script lives in.
    ///
    /// `php -r` code has no file, and the reference answers the working directory
    /// for it; the same goes for a script read from standard input. Both are
    /// recognised by [`PhpHost::script_name`] not being a path.
    pub fn magic_dir(&self) -> String {
        let name = self.current_file();
        if name.starts_with('/') {
            if let Some(parent) = std::path::Path::new(name).parent() {
                return parent.display().to_string();
            }
        }
        std::env::current_dir().map_or_else(|_| String::new(), |p| p.display().to_string())
    }

    /// The class (lowercased) that declared `name` `readonly`, walking up from
    /// `class`. `None` when the property is not readonly anywhere in the chain,
    /// which is the overwhelmingly common case and the only one that costs
    /// nothing on the write path.
    fn readonly_owner(&self, class: &str, name: &str) -> Option<String> {
        let mut cur = Some(class.to_ascii_lowercase());
        while let Some(c) = cur {
            let def = self.classes.get(&c)?;
            if def.readonly_props.contains(name) {
                return Some(c);
            }
            cur = def.parent.as_ref().map(|p| p.to_ascii_lowercase());
        }
        None
    }

    /// Why a write to `$recv->name` is refused, or `None` when it is allowed.
    ///
    /// A readonly property takes exactly one write, and PHP 8.4 gave the rule
    /// its `protected(set)` shape: the initializing write must come from the
    /// declaring class or a subclass, and every later write is refused from
    /// everywhere. `__clone` is the one documented reopening — a copy is
    /// allowed to be given a fresh identity — so a write there always passes.
    ///
    /// The two refusals have different messages, and which one applies turns on
    /// whether the property was ever initialized, not on its current value: a
    /// readonly property explicitly set to null is initialized.
    pub fn readonly_write_error(&self, recv: &Value, name: &str) -> Option<String> {
        let Some(PhpObj::Object { class, .. }) = self.as_array(recv) else {
            return None;
        };
        let owner = self.readonly_owner(class, name)?;
        if let (Value::Obj(id), Some(c)) = (recv, self.cloning) {
            if *id == c {
                return None;
            }
        }
        let display = self.class_display_name(&owner);
        let reinit = matches!((recv, self.cloning_with), (Value::Obj(id), Some(c)) if *id == c);
        if self.readonly_is_init(recv, name) && !reinit {
            return Some(format!(
                "Cannot modify readonly property {display}::${name}"
            ));
        }
        let scope = self.current_class_ctx();
        if scope
            .as_deref()
            .is_some_and(|s| self.class_is_a(&s.to_ascii_lowercase(), &owner))
        {
            return None;
        }
        let from = match &scope {
            Some(s) => format!("scope {}", self.class_display_name(&s.to_ascii_lowercase())),
            None => "global scope".to_string(),
        };
        Some(format!(
            "Cannot modify protected(set) readonly property {display}::${name} from {from}"
        ))
    }

    /// Why a write to `$recv->name` is refused by an asymmetric write
    /// visibility (`public private(set) int $x`), or `None` when the property has
    /// none or the calling scope may write it. `verb` is `modify`, `indirectly
    /// modify` or `unset`.
    pub fn set_visibility_error(&self, recv: &Value, name: &str, verb: &str) -> Option<String> {
        let Some(PhpObj::Object { class, .. }) = self.as_array(recv) else {
            return None;
        };
        let decl = self
            .class_chain(class)
            .find(|d| d.prop_vis.contains_key(name))?;
        self.set_visibility_refusal(decl, name, verb)
    }

    /// [`PhpHost::set_visibility_error`] for the class `decl` that declared the
    /// property, which is also where a static property's rule is read from.
    fn set_visibility_refusal(&self, decl: &ClassDef, name: &str, verb: &str) -> Option<String> {
        let set = *decl.prop_set_vis.get(name)?;
        let decl_lc = decl.name.to_ascii_lowercase();
        let scope = self.current_class_ctx().map(|s| s.to_ascii_lowercase());
        let allowed = match set {
            Visibility::Public => true,
            Visibility::Private => scope.as_deref() == Some(decl_lc.as_str()),
            Visibility::Protected => scope
                .as_deref()
                .is_some_and(|s| self.class_is_a(s, &decl_lc) || self.class_is_a(&decl_lc, s)),
        };
        if allowed {
            return None;
        }
        let word = match set {
            Visibility::Public => "public",
            Visibility::Protected => "protected",
            Visibility::Private => "private",
        };
        let from = match &scope {
            Some(s) => format!("scope {}", self.class_display_name(s)),
            None => "global scope".to_string(),
        };
        Some(format!(
            "Cannot {verb} {word}(set) property {}::${name} from {from}",
            decl.name
        ))
    }

    /// [`PhpHost::set_visibility_error`] for `Class::$name`.
    pub fn static_set_visibility_error(&self, class: &str, name: &str) -> Option<String> {
        let owner = self.resolve_static_owner(class, name)?;
        let decl = self.classes.get(&owner)?;
        self.set_visibility_refusal(decl, name, "modify")
    }

    /// Why `unset($recv->name)` is refused, or `None`. PHP allows unsetting a
    /// readonly property that was never initialized (it is still uninitialized
    /// afterwards) and refuses it once it holds a value.
    pub fn readonly_unset_error(&self, recv: &Value, name: &str) -> Option<String> {
        let Some(PhpObj::Object { class, props }) = self.as_array(recv) else {
            return None;
        };
        if self.is_enum_class(class) && props.contains_key(name) {
            let shown = self.class_display_name(&class.to_ascii_lowercase());
            return Some(format!("Cannot unset readonly property {shown}::${name}"));
        }
        let owner = self.readonly_owner(class, name)?;
        self.readonly_is_init(recv, name).then(|| {
            let display = self.class_display_name(&owner);
            format!("Cannot unset readonly property {display}::${name}")
        })
    }

    /// Why `$recv->name[…] = …` (or any other write *through* the property) is
    /// refused, or `None`. PHP will not hand out a modifiable reference to a
    /// readonly property at all, initialized or not, and says so in its own
    /// wording rather than the plain "cannot modify".
    pub fn readonly_indirect_error(&self, recv: &Value, name: &str) -> Option<String> {
        let Some(PhpObj::Object { class, .. }) = self.as_array(recv) else {
            return None;
        };
        if let (Value::Obj(id), Some(c)) = (recv, self.cloning) {
            if *id == c {
                return None;
            }
        }
        let owner = self.readonly_owner(class, name)?;
        // The initializing write inside the declaring scope goes through the
        // ordinary path; only a write to an already-set property is "indirect"
        // in the sense the message means.
        if !self.readonly_is_init(recv, name) {
            return None;
        }
        let display = self.class_display_name(&owner);
        Some(format!(
            "Cannot indirectly modify readonly property {display}::${name}"
        ))
    }

    /// Whether this object's readonly property `name` has taken its one write.
    ///
    /// Tracked explicitly rather than inferred from the stored value, because
    /// null is a perfectly good value for an initialized readonly property and
    /// is indistinguishable from "never written" in the property table.
    fn readonly_is_init(&self, recv: &Value, name: &str) -> bool {
        let Value::Obj(id) = recv else { return false };
        self.readonly_init
            .get(id)
            .is_some_and(|set| set.contains(name))
    }

    /// Record that `$recv->name` has taken its one readonly write. Called after
    /// the write, by the same opcode handlers that asked permission for it.
    pub fn readonly_note_init(&mut self, recv: &Value, name: &str) {
        let Value::Obj(id) = recv else { return };
        let Some(PhpObj::Object { class, .. }) = self.as_array(recv) else {
            return;
        };
        if self.readonly_owner(class, name).is_none() {
            return;
        }
        self.readonly_init
            .entry(*id)
            .or_default()
            .insert(name.to_string());
    }

    /// Resolve the declared visibility of property `name` on `class`, walking the
    /// parent chain; returns the declaring class (lowercased) and its visibility.
    /// `None` for a dynamic/undeclared property (treated as public).
    fn resolve_prop_vis(&self, class: &str, name: &str) -> Option<(String, Visibility)> {
        let mut cur = Some(class.to_ascii_lowercase());
        while let Some(c) = cur {
            let def = self.classes.get(&c)?;
            if let Some(v) = def.prop_vis.get(name) {
                return Some((c, *v));
            }
            cur = def.parent.as_ref().map(|p| p.to_ascii_lowercase());
        }
        None
    }

    /// Whether the current class context may access a member declared with
    /// `vis` in `declaring` (lowercased). Public: always. Private: only from the
    /// declaring class itself. Protected: from the declaring class or any class in
    /// the same inheritance line (ancestor or descendant).
    fn visibility_allows(&self, vis: Visibility, declaring: &str) -> bool {
        match vis {
            Visibility::Public => true,
            Visibility::Private => {
                matches!(self.current_class_ctx(), Some(c) if c.eq_ignore_ascii_case(declaring))
            }
            Visibility::Protected => match self.current_class_ctx() {
                Some(c) => self.class_is_a(&c, declaring) || self.class_is_a(declaring, &c),
                None => false,
            },
        }
    }

    /// Decide what `$obj->name` does for the operation whose magic method is
    /// `magic` (`__get`, `__set`, `__isset`, `__unset`).
    ///
    /// The order is the reference's, and the two halves are easy to get backwards:
    /// a magic method is consulted for a property that is not THERE just as much
    /// as for one that is out of reach, and it is consulted BEFORE any access
    /// error. So `unset($o->pub)` on a class with `__get` really does route later
    /// reads of that public property through `__get` — the slot is gone, and gone
    /// is indistinguishable from never-declared at this point.
    ///
    /// A magic method already running for this object and property is skipped,
    /// which is the recursion guard described on `magic_in_progress`.
    pub fn prop_access(&self, recv: &Value, name: &str, magic: &'static str) -> PropAccess {
        let Some(PhpObj::Object { class, props }) = self.as_array(recv) else {
            // Not an object: no visibility to enforce and no magic to call.
            return PropAccess::Absent;
        };
        let declared = self.resolve_prop_vis(class, name);
        // A property no class declares carries no visibility, so it is reachable.
        let reachable = match &declared {
            Some((declaring, vis)) => self.visibility_allows(*vis, declaring),
            None => true,
        };
        if reachable && props.contains_key(name) {
            return PropAccess::Direct;
        }
        let handle = match recv {
            Value::Obj(h) => *h,
            _ => return PropAccess::Absent,
        };
        let guarded = self
            .magic_in_progress
            .iter()
            .any(|(h, p, m)| *h == handle && p == name && *m == magic);
        if !guarded
            && self.class_has_method(class, magic)
            && !self.spl_array_props_off(recv, class, magic)
        {
            return PropAccess::Magic;
        }
        match declared {
            // Out of reach rather than absent — the reference names the
            // visibility and the class that declared it, not the class of the
            // object, which for an inherited private property is not the same.
            Some((declaring, vis)) if !reachable => {
                let vname = match vis {
                    Visibility::Private => "private",
                    Visibility::Protected => "protected",
                    Visibility::Public => unreachable!("public is always reachable"),
                };
                let class = self.class_display_name(&declaring);
                PropAccess::Denied(format!("Cannot access {vname} property {class}::${name}"))
            }
            _ => PropAccess::Absent,
        }
    }

    /// The flags an `ArrayObject` or `ArrayIterator` (or a subclass) carries,
    /// read from the prelude's static table keyed by `spl_object_id`; `None`
    /// for any other value.
    pub fn spl_array_flags(&self, v: &Value) -> Option<i64> {
        let class = self.instance_class(v)?;
        let (key, in_pair) = if self.class_is_a(&class, "arrayobject") {
            ("arrayobject::__state", true)
        } else if self.class_is_a(&class, "arrayiterator") {
            ("arrayiterator::__flags", false)
        } else {
            return None;
        };
        let id = Value::Int(self.object_id(v)?);
        let entry = self
            .get_static_stored(key)
            .and_then(|t| self.array_pairs(&t))
            .and_then(|pairs| pairs.into_iter().find(|(k, _)| *k == id))
            .map(|(_, e)| self.deref(e));
        let flags = match entry {
            Some(e) if in_pair => self
                .array_pairs(&e)
                .and_then(|p| p.into_iter().next())
                .map(|(_, f)| self.deref(f)),
            other => other,
        };
        Some(match flags {
            Some(Value::Int(n)) => n,
            _ => 0,
        })
    }

    /// Whether the magic `method` the class would run for a property is the
    /// `ArrayObject`/`ArrayIterator` prelude's ARRAY_AS_PROPS forwarder on an
    /// object whose flags do not ask for it: the reference's handlers then
    /// take the ordinary property path.
    fn spl_array_props_off(&self, recv: &Value, class: &str, method: &str) -> bool {
        self.spl_array_forwarder(class, method)
            && self.spl_array_flags(recv).is_some_and(|f| f & 2 == 0)
    }

    /// Whether `class` runs the `ArrayObject`/`ArrayIterator` prelude's
    /// ARRAY_AS_PROPS forwarder for the lowercased magic `method`. The
    /// reference does this in its object handlers, so reflection
    /// (`method_exists`, `get_class_methods`) does not see these methods.
    pub fn spl_array_forwarder(&self, class: &str, method: &str) -> bool {
        matches!(method, "__get" | "__set" | "__isset" | "__unset")
            && matches!(
                self.method_owner_key(class, method).as_deref(),
                Some("arrayobject" | "arrayiterator")
            )
    }

    /// The lowercased class that declares the lowercased `method` for
    /// `class`, walking the parent chain without cloning a definition.
    fn method_owner_key(&self, class: &str, method: &str) -> Option<String> {
        let mut cur = Some(class.to_ascii_lowercase());
        while let Some(c) = cur {
            let def = self.classes.get(&c)?;
            if def.methods.contains_key(method) {
                return Some(c);
            }
            cur = def.parent.as_ref().map(|p| p.to_ascii_lowercase());
        }
        None
    }

    /// Mark a magic property access as in progress; the matching
    /// [`magic_leave`](PhpHost::magic_leave) must follow it.
    pub fn magic_enter(&mut self, recv: &Value, name: &str, magic: &'static str) {
        if let Value::Obj(h) = recv {
            self.magic_in_progress.push((*h, name.to_string(), magic));
        }
    }

    pub fn magic_leave(&mut self) {
        self.magic_in_progress.pop();
    }

    /// The open magic-access count, for a caller that has to put it back after an
    /// unwind — the same problem, and the same fix, as `@expr` suppression.
    pub fn magic_depth(&self) -> usize {
        self.magic_in_progress.len()
    }

    pub fn magic_restore(&mut self, depth: usize) {
        self.magic_in_progress.truncate(depth);
    }

    /// A class name as declared, recovered from the lowercased key the class
    /// table is indexed by, so a diagnostic prints `MyClass` and not `myclass`.
    pub fn class_display_name(&self, lowered: &str) -> String {
        let name = self
            .classes
            .get(lowered)
            .map_or(lowered, |d| d.name.as_str());
        display_class(name).to_string()
    }

    /// Remove `$obj->name` from the object's property table — `unset($o->p)`.
    /// A property that is not there is not an error; the caller has already
    /// decided this is the right thing to do (see [`prop_access`]).
    ///
    /// [`prop_access`]: PhpHost::prop_access
    pub fn prop_remove(&mut self, recv: &Value, name: &str) {
        self.note_unset(recv, name);
        if let Some(PhpObj::Object { props, .. }) = self.as_array_mut(recv) {
            props.shift_remove(name);
        }
    }

    /// Enforce method visibility for `$obj->method()`. Same policy as properties;
    /// the message matches PHP's `Call to <vis> method C::m() from <scope>`.
    pub fn check_method_access(&self, class: &str, method: &str) -> Result<(), String> {
        let Some((declaring, vis)) = self.declaring_method_vis(class, method) else {
            return Ok(());
        };
        if self.visibility_allows(vis, &declaring) {
            return Ok(());
        }
        let vname = match vis {
            Visibility::Private => "private",
            Visibility::Protected => "protected",
            Visibility::Public => unreachable!(),
        };
        Err(format!(
            "Call to {vname} method {class}::{method}() from {}",
            self.scope_display()
        ))
    }

    /// The calling scope as a message names it: `global scope`, or `scope C`
    /// in the class's declared spelling.
    fn scope_display(&self) -> String {
        match self.current_class_ctx() {
            Some(c) => format!("scope {}", self.class_display_name(&c.to_ascii_lowercase())),
            None => "global scope".to_string(),
        }
    }

    /// Why `new class` is refused by its constructor's visibility, worded as
    /// `zend_std_get_constructor` words it (`Call to private K::__construct()
    /// from global scope` — no `method`, and the class is the one that DECLARED
    /// the constructor). `None` when there is no constructor or the calling
    /// scope may call it: a private one only from the declaring class, a
    /// protected one from anywhere on its inheritance line.
    pub fn constructor_access_error(&self, class: &str) -> Option<String> {
        let (declaring, vis) = self.declaring_method_vis(class, "__construct")?;
        if self.visibility_allows(vis, &declaring) {
            return None;
        }
        let vname = match vis {
            Visibility::Private => "private",
            Visibility::Protected => "protected",
            Visibility::Public => unreachable!(),
        };
        Some(format!(
            "Call to {vname} {}::__construct() from {}",
            self.class_display_name(&declaring),
            self.scope_display()
        ))
    }

    /// The class up `class`'s chain that DECLARED `method`, and the visibility it
    /// declared it with. `None` when no class on the chain declares it — which is
    /// how an inherited-but-undeclared method stays reachable.
    fn declaring_method_vis(&self, class: &str, method: &str) -> Option<(String, Visibility)> {
        let method_l = method.to_ascii_lowercase();
        let mut cur = Some(class.to_ascii_lowercase());
        while let Some(c) = cur {
            let def = self.classes.get(&c)?;
            if let Some(v) = def.method_vis.get(&method_l) {
                return Some((c, *v));
            }
            cur = def.parent.as_ref().map(|p| p.to_ascii_lowercase());
        }
        None
    }

    /// The visibility `class` (or an ancestor) declared `method` with, as the
    /// reference spells it in a message. `None` for a public or undeclared one.
    pub fn method_visibility_name(&self, class: &str, method: &str) -> Option<&'static str> {
        match self.declaring_method_vis(class, method) {
            Some((_, Visibility::Private)) => Some("private"),
            Some((_, Visibility::Protected)) => Some("protected"),
            _ => None,
        }
    }

    /// Whether `class` (or an ancestor) declared `method` `static`.
    pub fn method_is_static(&self, class: &str, method: &str) -> bool {
        let method_l = method.to_ascii_lowercase();
        let mut cur = Some(class.to_ascii_lowercase());
        while let Some(c) = cur {
            let Some(def) = self.classes.get(&c) else {
                return false;
            };
            if def.methods.contains_key(&method_l) {
                return def.static_methods.contains(&method_l);
            }
            cur = def.parent.as_ref().map(|p| p.to_ascii_lowercase());
        }
        false
    }

    /// The magic catch-all a call to `class::method` would fall back to, if any:
    /// `__call` for an instance call, `__callStatic` for a static one. A class
    /// with only `__call` does NOT answer a static call, and vice versa.
    fn magic_call_name(&self, class: &str, has_this: bool) -> Option<&'static str> {
        let magic = if has_this { "__call" } else { "__callstatic" };
        self.method_declared(class, magic).then_some(if has_this {
            "__call"
        } else {
            "__callStatic"
        })
    }

    /// Decide what `$obj->m(...)` (or `C::m(...)`) does, consulting the method
    /// table, the visibility rules and the magic catch-all in the reference's
    /// order. Every call opcode routes through this so the instance and static
    /// forms answer identically.
    pub fn method_dispatch(&self, class: &str, method: &str, has_this: bool) -> MethodDispatch {
        let method_l = method.to_ascii_lowercase();
        if !self.method_declared(class, &method_l) {
            return match self.magic_call_name(class, has_this) {
                Some(_) => MethodDispatch::Magic,
                None => MethodDispatch::Undefined,
            };
        }
        match self.check_method_access(class, method) {
            Ok(()) => MethodDispatch::Direct,
            // Out of reach, but a catch-all takes precedence over the access
            // error — the reference calls `__call` for a private method too.
            Err(msg) => match self.magic_call_name(class, has_this) {
                Some(_) => MethodDispatch::Magic,
                None => MethodDispatch::Denied(msg),
            },
        }
    }

    /// The ordered property-default initializer chunks for a class, parent props
    /// first and child declarations overriding by name, each with the class that
    /// declares it (its initializer runs in that class's scope). `None` if
    /// unknown class.
    /// The third element is whether the property starts UNINITIALIZED (typed,
    /// no default, as its final declaration along the chain says): its chunk is
    /// not run and the instance is created without it.
    #[allow(clippy::type_complexity)]
    fn class_prop_default_chunks(
        &self,
        class: &str,
    ) -> Option<Vec<(String, (String, Chunk, bool))>> {
        let cl = class.to_ascii_lowercase();
        if !self.classes.contains_key(&cl) {
            return None;
        }
        // Build the chain child → root, then apply root → child so a child's
        // redeclared default wins while parent props keep their leading position.
        let mut chain: Vec<(String, &ClassDef)> = Vec::new();
        let mut cur = Some(cl);
        while let Some(c) = cur {
            let Some(def) = self.classes.get(&c) else {
                break;
            };
            chain.push((c, def));
            cur = def.parent.as_ref().map(|p| p.to_ascii_lowercase());
        }
        let mut map: IndexMap<String, (String, Chunk, bool)> = IndexMap::new();
        for (c, def) in chain.into_iter().rev() {
            for (name, chunk) in &def.prop_defaults {
                let uninit = def.uninit_props.contains_key(name);
                map.insert(name.clone(), (c.clone(), chunk.clone(), uninit));
            }
        }
        Some(map.into_iter().collect())
    }

    /// The instance properties `class` declares, parent's first, in the slot
    /// order a new instance lays them out in. Empty for an unknown class
    /// (`stdClass`), whose every property is dynamic.
    pub(crate) fn declared_prop_order(&self, class: &str) -> Vec<String> {
        let mut chain: Vec<&ClassDef> = Vec::new();
        let mut cur = Some(class.to_ascii_lowercase());
        while let Some(c) = cur {
            let Some(def) = self.classes.get(&c) else {
                break;
            };
            chain.push(def);
            cur = def.parent.as_ref().map(|p| p.to_ascii_lowercase());
        }
        let mut order: IndexMap<String, ()> = IndexMap::new();
        for def in chain.into_iter().rev() {
            for (name, _) in &def.prop_defaults {
                order.insert(name.clone(), ());
            }
        }
        order.into_keys().collect()
    }

    /// `(declaring class, type)` when `name`, as the nearest declaration along
    /// `class`'s chain has it, is a typed property with no default — one that is
    /// uninitialized whenever the instance does not hold it.
    fn uninit_prop_decl(&self, class: &str, name: &str) -> Option<(String, String)> {
        let mut cur = Some(class.to_ascii_lowercase());
        while let Some(c) = cur {
            let def = self.classes.get(&c)?;
            if def.prop_defaults.iter().any(|(n, _)| n == name) {
                return def
                    .uninit_props
                    .get(name)
                    .map(|ty| (self.class_display_name(&c), ty.clone()));
            }
            cur = def.parent.as_ref().map(|p| p.to_ascii_lowercase());
        }
        None
    }

    /// The `Error` a read of `$recv->name` raises when the property is typed and
    /// still uninitialized (never written, or `unset()`), or `None`.
    pub fn uninit_read_error(&self, recv: &Value, name: &str) -> Option<String> {
        let Some(PhpObj::Object { class, props }) = self.as_array(recv) else {
            return None;
        };
        if props.contains_key(name) {
            return None;
        }
        let (declaring, _) = self.uninit_prop_decl(class, name)?;
        Some(format!(
            "Typed property {declaring}::${name} must not be accessed before initialization"
        ))
    }

    /// Record an `unset()` of `$recv->name`, whether or not it was set, when it
    /// is a typed property with no default — see `unset_typed`.
    pub fn note_unset(&mut self, recv: &Value, name: &str) {
        if let (Value::Obj(id), Some(PhpObj::Object { class, .. })) = (recv, self.as_array(recv)) {
            if self.uninit_prop_decl(class, name).is_some() {
                self.unset_typed
                    .entry(*id)
                    .or_default()
                    .insert(name.to_string());
            }
        }
    }

    /// [`Self::uninit_read_error`] for a class with `__get`: the magic method
    /// answers for an uninitialized typed property only once `unset()` has
    /// removed it, so a never-written one is still the `Error`.
    pub fn uninit_magic_blocked(&self, recv: &Value, name: &str) -> Option<String> {
        let Value::Obj(id) = recv else { return None };
        if self.unset_typed.get(id).is_some_and(|s| s.contains(name)) {
            return None;
        }
        self.uninit_read_error(recv, name)
    }

    /// The uninitialized typed properties of an object, for `var_dump`: each as
    /// `(position, name, type)`, where `position` is the index into the object's
    /// property list the entry prints BEFORE. Declared properties occupy fixed
    /// slots ahead of every dynamic one, so an uninitialized slot prints where
    /// its declaration puts it.
    pub fn uninit_slots(&self, recv: &Value) -> Vec<(usize, String, String)> {
        let Some(PhpObj::Object { class, props }) = self.as_array(recv) else {
            return Vec::new();
        };
        let mut out = Vec::new();
        let mut present = 0;
        for name in self.declared_prop_order(class) {
            if props.contains_key(&name) {
                present += 1;
            } else if let Some((_, ty)) = self.uninit_prop_decl(class, &name) {
                out.push((present, name, ty));
            }
        }
        out
    }

    /// Store a property that may not be in the object yet. A DECLARED property
    /// coming back after `unset()`, or taking its first write while
    /// uninitialized, goes back into its declared slot rather than after the
    /// dynamic ones — `foreach` and `var_dump` list it where the class declared
    /// it, as the reference does. Anything else is appended.
    fn obj_put_prop(&mut self, recv: &Value, name: &str, val: Value) {
        let at = match self.as_array(recv) {
            Some(PhpObj::Object { props, .. }) if props.contains_key(name) => None,
            Some(PhpObj::Object { class, props }) => {
                let order = self.declared_prop_order(class);
                order.iter().position(|n| n == name).map(|mine| {
                    props
                        .keys()
                        .filter(|k| order[..mine].iter().any(|d| d == *k))
                        .count()
                })
            }
            _ => return,
        };
        if let Some(PhpObj::Object { props, .. }) = self.as_array_mut(recv) {
            match at {
                Some(i) => {
                    props.shift_insert(i, name.to_string(), val);
                }
                None => {
                    props.insert(name.to_string(), val);
                }
            }
        }
    }

    /// Resolve `Class::$name` to its storage key and initializer chunk, walking
    /// the parent chain to the class that actually declares the static property.
    /// The key is `"declaringclass::name"` so a subclass shares the parent's cell.
    /// The class (lowercased) that declares static property `name`, seen from
    /// `class`.
    pub fn resolve_static_owner(&self, class: &str, name: &str) -> Option<String> {
        self.resolve_static_key(class, name)
            .and_then(|(key, _)| key.split_once("::").map(|(c, _)| c.to_string()))
    }

    fn resolve_static_key(&self, class: &str, name: &str) -> Option<(String, Chunk)> {
        let mut cur = Some(class.to_ascii_lowercase());
        while let Some(c) = cur {
            let def = self.classes.get(&c)?;
            if let Some((_, chunk)) = def.static_prop_defaults.iter().find(|(n, _)| n == name) {
                return Some((format!("{c}::{name}"), chunk.clone()));
            }
            cur = def.parent.as_ref().map(|p| p.to_ascii_lowercase());
        }
        None
    }

    /// The names of the properties `class` declares or inherits, in the order
    /// `zend_do_inheritance` leaves its property table: a class's own
    /// declarations first, then those it inherits and does not redeclare, in the
    /// parent's own order. `statics` picks the static table or the instance one.
    fn property_names_inheritance_order(&self, class: &str, statics: bool) -> Vec<String> {
        let mut out: Vec<String> = Vec::new();
        let mut cur = Some(class.to_ascii_lowercase());
        while let Some(c) = cur {
            let Some(def) = self.classes.get(&c) else {
                break;
            };
            let own = if statics {
                &def.static_prop_defaults
            } else {
                &def.prop_defaults
            };
            for (name, _) in own {
                if !out.contains(name) {
                    out.push(name.clone());
                }
            }
            cur = def.parent.as_ref().map(|p| p.to_ascii_lowercase());
        }
        out
    }

    /// The stored value of a static property, by storage key (`None` = not yet
    /// initialized). A property bound by reference reads through its cell.
    fn get_static_stored(&self, key: &str) -> Option<Value> {
        let v = self.static_props.get(key)?;
        Some(match self.ref_slot_of_value(v) {
            Some(slot) => self.ref_cells[slot].clone(),
            None => v.clone(),
        })
    }

    /// Store a static property's value by storage key, through its reference
    /// cell when it is bound to one.
    fn set_static_stored(&mut self, key: &str, val: Value) {
        match self
            .static_props
            .get(key)
            .and_then(|v| self.ref_slot_of_value(v))
        {
            Some(slot) => self.ref_cells[slot] = val,
            None => {
                self.static_props.insert(key.to_string(), val);
            }
        }
    }

    /// The reference cell an initialized static property is stored in, made
    /// one (holding its current value) if it is not already.
    fn static_ref_slot_ensure(&mut self, key: &str) -> usize {
        let cur = self.static_props.get(key).cloned().unwrap_or(Value::Undef);
        if let Some(slot) = self.ref_slot_of_value(&cur) {
            return slot;
        }
        self.ref_cells.push(cur);
        let slot = self.ref_cells.len() - 1;
        self.bind_static_to_slot(key, slot);
        slot
    }

    /// Make the static property at `key` an alias of the cell at `slot`.
    fn bind_static_to_slot(&mut self, key: &str, slot: usize) {
        self.objs.push(PhpObj::Ref { slot });
        let handle = Value::Obj((self.objs.len() - 1) as u32);
        self.static_props.insert(key.to_string(), handle);
    }

    /// Bind a function-local `$name` to its persistent `static` slot. On first
    /// encounter the cell is created with `init`; later calls reuse it, so the
    /// value survives across calls. The current scope's name is aliased to the
    /// cell (like a reference), so ordinary reads/writes of `$name` in the body
    /// hit the persistent storage.
    pub fn bind_static_local(&mut self, name: &str, slot_key: &str, init: Value) {
        let slot = match self.closure_static_slot(slot_key) {
            // A closure's own copy, which the closure object brought in.
            Some(s) => {
                if self.static_uninit.remove(&s) {
                    self.ref_cells[s] = init;
                }
                s
            }
            None => match self.static_slots.get(slot_key) {
                Some(&s) => s,
                None => {
                    self.ref_cells.push(init);
                    let s = self.ref_cells.len() - 1;
                    self.static_slots.insert(slot_key.to_string(), s);
                    s
                }
            },
        };
        let idx = self.scope_idx(name);
        if let Some(scope) = self.scopes.get_mut(idx) {
            let i = scope.vars.ensure_slot(name);
            scope.vars.put(i, Slot::Ref(slot));
        }
    }

    /// The cell of the running closure's own copy of the `static` keyed
    /// `slot_key`: the closure binds it into its frame under that key.
    fn closure_static_slot(&self, slot_key: &str) -> Option<usize> {
        let idx = self.scope_idx(slot_key);
        match self.scopes.get(idx).map(|s| s.vars.get(slot_key)) {
            Some(Slot::Ref(c)) => Some(*c),
            _ => None,
        }
    }

    /// Whether the `static` keyed `slot_key` has been initialized, so its
    /// initializer must not run again.
    pub fn static_initialized(&self, slot_key: &str) -> bool {
        match self.closure_static_slot(slot_key) {
            Some(s) => !self.static_uninit.contains(&s),
            None => self.static_slots.contains_key(slot_key),
        }
    }

    /// Give every `static` cell in a closure's bindings a fresh copy of its
    /// current value, for a closure duplicated from another (`clone`,
    /// `bindTo`): the reference copies the static variables with it.
    fn fork_closure_statics(&mut self, captured: &mut [(String, Value)]) {
        for (name, v) in captured.iter_mut() {
            if !name.starts_with("@static#") {
                continue;
            }
            let Some(slot) = self.ref_slot_of_value(v) else {
                continue;
            };
            self.ref_cells.push(self.ref_cells[slot].clone());
            let fresh = self.ref_cells.len() - 1;
            if self.static_uninit.contains(&slot) {
                self.static_uninit.insert(fresh);
            }
            self.objs.push(PhpObj::Ref { slot: fresh });
            *v = Value::Obj((self.objs.len() - 1) as u32);
        }
    }

    // ── enums (PHP 8.1) ──────────────────────────────────────────────────────

    /// Whether `class` is an `enum` (case-insensitive).
    /// A class name as it was DECLARED, from any spelling of it. Class names are
    /// case-insensitive and keyed lowercase, so a diagnostic that echoes back the
    /// caller's spelling reads wrong; the reference always prints the declaration's.
    /// An undeclared name is handed back unchanged.
    pub fn declared_class_name(&self, class: &str) -> String {
        self.classes
            .get(&class.to_ascii_lowercase())
            .map_or_else(|| class.to_string(), |d| d.name.clone())
    }

    pub fn is_enum_class(&self, class: &str) -> bool {
        self.classes
            .get(&class.to_ascii_lowercase())
            .map(|d| d.is_enum)
            .unwrap_or(false)
    }

    /// The declared case's optional backing-value chunk (`Some(chunk_opt)` when the
    /// enum declares a case of this exact name, else `None`).
    fn enum_case_chunk(&self, class: &str, case: &str) -> Option<Option<Chunk>> {
        let d = self.classes.get(&class.to_ascii_lowercase())?;
        d.enum_cases
            .iter()
            .find(|(n, _)| n == case)
            .map(|(_, c)| c.clone())
    }

    /// The enum's case names in declaration order.
    fn enum_case_names(&self, class: &str) -> Vec<String> {
        self.classes
            .get(&class.to_ascii_lowercase())
            .map(|d| d.enum_cases.iter().map(|(n, _)| n.clone()).collect())
            .unwrap_or_default()
    }

    fn enum_case_cached(&self, key: &str) -> Option<Value> {
        self.enum_case_cache.get(key).cloned()
    }

    fn enum_case_store(&mut self, key: &str, v: Value) {
        self.enum_case_cache.insert(key.to_string(), v);
    }

    /// Allocate an enum-case instance with its `name` (and, for a backed enum,
    /// `value`) properties. `class` keeps its source casing for `get_class`.
    fn new_enum_object(&mut self, class: &str, name: &str, value: Option<Value>) -> Value {
        let mut props: IndexMap<String, Value> = IndexMap::new();
        props.insert("name".to_string(), Value::str(name.to_string()));
        if let Some(v) = value {
            props.insert("value".to_string(), v);
        }
        self.objs.push(PhpObj::Object {
            class: class.to_string(),
            props,
        });
        Value::Obj((self.objs.len() - 1) as u32)
    }

    /// The initializer chunk for `Class::name`, walking the parent chain and
    /// then the interfaces.
    ///
    /// The interfaces are not optional. A `const` declared in an interface is
    /// inherited by every class that implements it and by every interface that
    /// extends it, so `interface I { const K = 5; } class C implements I {}`
    /// answers `C::K` — and `self::K` and `static::K` from inside `C` are the
    /// same lookup. Walking only `parent` reported `Undefined constant C::K`
    /// for all three.
    ///
    /// Classes first, interfaces after: a class constant shadows the interface
    /// one of the same name (PHP 8.1 permits the override), and searching the
    /// whole parent chain before any interface is what makes that hold at every
    /// depth. The visited set and bound guard a malformed cycle, as in
    /// [`Host::class_is_a`].
    /// The `Error` a `Class::NAME` read raises when the calling scope cannot
    /// reach a `private`/`protected` constant, worded as `zend_get_class_constant_ex`
    /// (Zend/zend_constants.c) words it: the visibility and the class NAMED at
    /// the access site. Only the class chain can declare a non-public constant
    /// (an interface constant is always public), so only it is walked.
    fn const_access_denied(&self, class: &str, name: &str) -> Option<String> {
        let mut cur = Some(class.to_ascii_lowercase());
        while let Some(c) = cur {
            let def = self.classes.get(&c)?;
            if def.consts.iter().any(|(n, _)| n == name) {
                let vis = *def.const_vis.get(name)?;
                if self.visibility_allows(vis, &c) {
                    return None;
                }
                let vname = match vis {
                    Visibility::Private => "private",
                    Visibility::Protected => "protected",
                    Visibility::Public => return None,
                };
                let shown = self.class_display_name(&class.to_ascii_lowercase());
                return Some(format!("Cannot access {vname} constant {shown}::{name}"));
            }
            cur = def.parent.as_ref().map(|p| p.to_ascii_lowercase());
        }
        None
    }

    fn resolve_const_chunk(&self, class: &str, name: &str) -> Option<(String, Chunk)> {
        let mut ifaces: Vec<String> = Vec::new();
        let mut cur = Some(class.to_ascii_lowercase());
        while let Some(c) = cur {
            let Some(def) = self.classes.get(&c) else {
                break;
            };
            if let Some((_, chunk)) = def.consts.iter().find(|(n, _)| n == name) {
                return Some((c, chunk.clone()));
            }
            ifaces.extend(def.interfaces.iter().map(|i| i.to_ascii_lowercase()));
            cur = def.parent.as_ref().map(|p| p.to_ascii_lowercase());
        }
        let mut seen: Vec<String> = Vec::new();
        while let Some(i) = ifaces.pop() {
            if seen.contains(&i) || seen.len() > 1000 {
                continue;
            }
            seen.push(i.clone());
            if let Some(def) = self.classes.get(&i) {
                if let Some((_, chunk)) = def.consts.iter().find(|(n, _)| n == name) {
                    return Some((i.clone(), chunk.clone()));
                }
                ifaces.extend(def.interfaces.iter().map(|x| x.to_ascii_lowercase()));
                if let Some(p) = &def.parent {
                    ifaces.push(p.to_ascii_lowercase());
                }
            }
        }
        None
    }

    fn as_array_mut(&mut self, v: &Value) -> Option<&mut PhpObj> {
        match v {
            Value::Obj(h) => self.objs.get_mut(*h as usize),
            _ => None,
        }
    }

    fn as_array(&self, v: &Value) -> Option<&PhpObj> {
        match v {
            Value::Obj(h) => self.objs.get(*h as usize),
            _ => None,
        }
    }

    /// Normalize a value used as an array key, matching PHP: integer-valued
    /// strings and floats become int keys, bools/null fold to int/"".
    fn norm_key(&self, key: &Value) -> ArrayKey {
        match key {
            Value::Int(n) => ArrayKey::Int(*n),
            Value::Bool(b) => ArrayKey::Int(*b as i64),
            // `as i64` saturates where PHP wraps, so `$a[1e19]` landed on
            // `PHP_INT_MAX` instead of -8446744073709551616.
            Value::Float(f) => ArrayKey::Int(dval_to_lval(*f)),
            Value::Undef => ArrayKey::Str(String::new()),
            Value::Str(s) => match canonical_int_key(s) {
                Some(n) => ArrayKey::Int(n),
                None => ArrayKey::Str(String::clone(s)),
            },
            Value::Obj(_) if self.is_stream(key) => {
                ArrayKey::Int(self.resource_info(key).map_or(0, |(id, _)| id))
            }
            Value::Obj(_) => ArrayKey::Str("Array".into()),
            _ => ArrayKey::Str(self.to_str(key)),
        }
    }

    /// The reference slot the entry `arr[k]` holds, if that element is a reference.
    fn entry_ref_slot(&self, arr: &Value, k: &ArrayKey) -> Option<usize> {
        let Some(PhpObj::Array { entries, .. }) = self.as_array(arr) else {
            return None;
        };
        self.ref_slot_of_value(entries.get(k)?)
    }

    /// The class of `recv` when it is an object implementing `ArrayAccess`, so
    /// `$o[k]` must route through `offsetGet`/`offsetSet`/`offsetExists`/
    /// `offsetUnset` instead of touching an array.
    ///
    /// Returned as a name rather than a bool because every caller immediately
    /// needs it to dispatch the method, and that dispatch runs PHP — it cannot
    /// happen while the host is borrowed.
    pub fn array_access_class(&self, recv: &Value) -> Option<String> {
        let class = self.object_class(recv)?;
        self.class_is_a_pub(&class, "ArrayAccess").then_some(class)
    }

    /// `$arr[key]` read. Also indexes strings (single-character substring).
    pub fn index_get(&self, recv: &Value, key: &Value) -> Value {
        if let Some(PhpObj::Array { entries, .. }) = self.as_array(recv) {
            let k = self.norm_key(key);
            let raw = entries.get(&k).cloned().unwrap_or(Value::Undef);
            return self.deref(raw);
        }
        if let Value::Str(s) = recv {
            // PHP string offsets are byte-indexed and accept negatives (`$s[-1]`
            // is the last byte). An out-of-range offset yields `Undef`, so
            // `isset($s[i])` is false there (a plain read still echoes as "").
            let bytes = &*crate::bytes::to_bytes(s);
            let len = bytes.len() as i64;
            let StrOffset::At(mut i, _) = classify_string_offset(key) else {
                return Value::Undef;
            };
            if i < 0 {
                i += len;
            }
            if i >= 0 && i < len {
                let b = i as usize;
                return Value::str(crate::bytes::from_bytes(&bytes[b..b + 1]));
            }
        }
        Value::Undef
    }

    /// `$arr[key]` read in a *value* context, where a miss is a mistake rather
    /// than a question. Same result as `index_get`, plus PHP's diagnostic:
    /// `Undefined array key K` for a missing element, `Uninitialized string
    /// offset N` past the end of a string, and `Trying to access array offset on
    /// <type>` when the receiver is not subscriptable at all.
    pub fn index_get_warn(&mut self, recv: &Value, key: &Value) -> Value {
        self.diagnose_array_offset(recv, key);
        if let Some(PhpObj::Array { entries, .. }) = self.as_array(recv) {
            let k = self.norm_key(key);
            match entries.get(&k).cloned() {
                Some(v) => return self.deref(v),
                None => {
                    let k = self.diag_key(key);
                    self.warn(format_args!("Undefined array key {k}"));
                    return Value::Undef;
                }
            }
        }
        if let Value::Str(s) = recv {
            let len = s.len() as i64;
            // A key that is no offset at all was rejected by the caller (see
            // `classify_string_offset`); reaching here it is always `At`.
            let StrOffset::At(off, cast) = classify_string_offset(key) else {
                return Value::Undef;
            };
            if cast {
                self.warn("String offset cast occurred");
            }
            let i = if off < 0 { off + len } else { off };
            if i >= 0 && i < len {
                return self.index_get(recv, key);
            }
            self.warn(format_args!("Uninitialized string offset {off}"));
            return Value::Undef;
        }
        // A closure/generator/resource handle is not an array either, but PHP
        // raises an Error for those rather than a warning; only the scalars and
        // null reach this warning.
        if !matches!(recv, Value::Obj(_)) {
            let t = self.diag_offset_type(recv);
            self.warn(format_args!("Trying to access array offset on {t}"));
        }
        Value::Undef
    }

    /// `$name` read in a value context — `get_var` plus `Undefined variable $x`
    /// when the name is not bound. Compiler temporaries (which are prefixed with
    /// `@`, outside the PHP identifier space) never warn: they are not the user's
    /// variables and are always written before they are read.
    pub fn get_var_warn(&mut self, name: &str) -> Value {
        let idx = self.scope_idx(name);
        if let Some(scope) = self.scopes.get(idx) {
            let s = scope.vars.get(name).clone();
            if !matches!(s, Slot::Unset) {
                return self.read_slot(&s);
            }
        }
        if !name.starts_with('@') {
            self.warn(format_args!("Undefined variable ${name}"));
        }
        Value::Undef
    }

    /// `$obj->name` read in a value context — `prop_get` plus PHP's diagnostic:
    /// `Undefined property: C::$p` when the instance has no such property, and
    /// `Attempt to read property "p" on <type>` when the receiver is not an
    /// object at all.
    pub fn prop_get_warn(&mut self, recv: &Value, name: &str) -> Value {
        match self.as_array(recv) {
            Some(PhpObj::Object { class, props }) => match props.get(name).cloned() {
                Some(v) => self.deref(v),
                None => {
                    let class = display_class(class).to_string();
                    self.warn(format_args!("Undefined property: {class}::${name}"));
                    Value::Undef
                }
            },
            // An array handle reads as a property-less value; PHP reports the
            // same "read property on array" as for any other non-object.
            _ => {
                let t = self.diag_type(recv);
                self.warn(format_args!("Attempt to read property \"{name}\" on {t}"));
                Value::Undef
            }
        }
    }

    /// `$var[key] = val` on the named scope variable, auto-vivifying an array.
    ///
    /// A variable already holding a STRING is not auto-vivified: the write edits
    /// that string in place. See `Self::string_offset_set` — before it existed
    /// every `$s[0] = "x"` silently replaced the string with a one-element array.
    pub fn index_set_var(&mut self, name: &str, key: &Value, val: Value) -> Result<(), String> {
        if let Value::Str(cur) = self.get_var(name) {
            let updated = self.string_offset_set(&cur, key, &val)?;
            if let Some(updated) = updated {
                self.set_var(name, Value::str(updated));
            }
            return Ok(());
        }
        let arr = self.ensure_array_var(name);
        self.arr_set_key(&arr, key, val);
        Ok(())
    }

    /// `$s[offset] = $v` where `$s` is a string. Returns the new string, or
    /// `None` when the write was refused with a warning and `$s` is unchanged.
    ///
    /// Ported from `zend_assign_to_string_offset`. The rules, each measured
    /// against `php 8.5.9`:
    ///
    /// | form | outcome |
    /// |---|---|
    /// | `$s="abc"; $s[1]="Z"` | `"aZc"` |
    /// | `$s="abc"; $s[5]="Z"` | `"abc  Z"` — the gap is padded with SPACES |
    /// | `$s="abc"; $s[-1]="Z"` | `"abZ"` — negative counts from the end |
    /// | `$s="abc"; $s[-10]="Z"` | Warning `Illegal string offset -10`, unchanged |
    /// | `$s="abc"; $s[1]="XY"` | Warning `Only the first byte…`, `"aXc"` |
    /// | `$s="abc"; $s[1]=""` | Error `Cannot assign an empty string to a string offset` |
    /// | `$s="abc"; $s["x"]="Z"` | TypeError `Cannot access offset of type string on string` |
    /// | `$s="abc"; $s[1.7]="Z"` | Warning `String offset cast occurred`, `"aZc"` |
    fn string_offset_set(
        &mut self,
        cur: &str,
        key: &Value,
        val: &Value,
    ) -> Result<Option<String>, String> {
        // Only an int-ish offset addresses a string. A non-numeric string key is
        // a TypeError; a float/bool/null is accepted with a cast warning.
        let off = match key {
            Value::Int(n) => *n,
            Value::Str(k) => match parse_php_number_full(k) {
                Some(Value::Int(n)) => n,
                _ => {
                    return Err(crate::builtins::throws_bare(
                        "TypeError",
                        format!(
                            "Cannot access offset of type {} on string",
                            self.type_name_for_error(key)
                        ),
                    ))
                }
            },
            Value::Float(_) | Value::Bool(_) | Value::Undef => {
                self.warn("String offset cast occurred");
                self.to_number(key).to_int()
            }
            other => {
                return Err(crate::builtins::throws_bare(
                    "TypeError",
                    format!(
                        "Cannot access offset of type {} on string",
                        self.type_name_for_error(other)
                    ),
                ))
            }
        };

        let replacement = self.to_str(val);
        if replacement.is_empty() {
            return Err(crate::builtins::throws_bare(
                "Error",
                "Cannot assign an empty string to a string offset",
            ));
        }
        if replacement.len() > 1 {
            self.warn("Only the first byte will be assigned to the string offset");
        }
        let byte = (&*crate::bytes::to_bytes(&replacement))[0];

        let len = cur.len() as i64;
        let idx = if off < 0 {
            let from_end = len + off;
            if from_end < 0 {
                self.warn(format_args!("Illegal string offset {off}"));
                return Ok(None);
            }
            from_end as usize
        } else {
            off as usize
        };

        let mut bytes = (*crate::bytes::to_bytes(cur)).to_vec();
        if idx >= bytes.len() {
            bytes.resize(idx + 1, b' ');
        }
        bytes[idx] = byte;
        Ok(Some(crate::bytes::from_bytes(&bytes)))
    }

    /// `$var[] = val` append on the named scope variable, auto-vivifying.
    ///
    /// `Err` carries [`NEXT_ELEMENT_OCCUPIED`] when the array already holds an
    /// element at `PHP_INT_MAX` — the reference refuses the write rather than
    /// picking some other key, and the refusal is a catchable `Error`.
    pub fn append_var(&mut self, name: &str, val: Value) -> Result<(), String> {
        let arr = self.ensure_array_var(name);
        if self.append_slot_taken(&arr) {
            return Err(NEXT_ELEMENT_OCCUPIED.to_string());
        }
        if let Some(PhpObj::Array {
            entries,
            next_index,
        }) = self.as_array_mut(&arr)
        {
            let k = ArrayKey::Int(take_next_index(next_index));
            entries.insert(k, val);
        }
        Ok(())
    }

    /// Return the array handle held by `$name`, creating an empty array (and
    /// storing it back) if the variable is unset or not an array.
    fn ensure_array_var(&mut self, name: &str) -> Value {
        let cur = self.get_var(name);
        if matches!(self.as_array(&cur), Some(PhpObj::Array { .. })) {
            return cur;
        }
        let arr = self.new_array();
        self.set_var(name, arr.clone());
        arr
    }

    /// Descend the array handle `arr` along `keys`, auto-vivifying an array at each
    /// step, and return the handle of the array the final segment lands in. A slot
    /// that is unset — or holds a non-array — is (re)created as an empty array,
    /// matching PHP's auto-vivification of nested lvalues.
    fn ensure_path_from(&mut self, mut arr: Value, keys: &[Value]) -> Value {
        for key in keys {
            let k = self.norm_key(key);
            let child = match self.as_array(&arr) {
                // A referenced element descends into whatever its cell holds, so
                // `$r = &$a['x']; $a['x']['y'] = 1;` writes through the alias.
                Some(PhpObj::Array { entries, .. }) => {
                    entries.get(&k).cloned().map(|v| self.deref(v))
                }
                _ => None,
            };
            arr = match child {
                Some(v) if self.is_array(&v) => v,
                _ => {
                    let new = self.new_array();
                    self.arr_set_key(&arr, key, new.clone());
                    new
                }
            };
        }
        arr
    }

    /// Like `ensure_path_from`, but rooted at the scope variable `$name`
    /// (auto-vivifying the variable itself into an array).
    fn ensure_path_array(&mut self, name: &str, keys: &[Value]) -> Value {
        let root = self.ensure_array_var(name);
        self.ensure_path_from(root, keys)
    }

    /// `$name[k1]..[kN] = val` — set the deepest element along a key path,
    /// auto-vivifying the intermediate arrays (`N >= 1`).
    ///
    /// A string as the last container takes the write as a string offset
    /// (`$a = ["s"]; $a[0][2] = "x"` leaves `"s x"`), with the errors and
    /// warnings of `Self::string_offset_set`. A string any earlier along the
    /// path was refused by [`Self::write_container_issue`] before this runs.
    pub fn index_set_path(&mut self, name: &str, keys: &[Value], val: Value) -> Result<(), String> {
        let Some((last, inter)) = keys.split_last() else {
            return Ok(());
        };
        if let Value::Str(cur) = self.index_get_path(name, inter) {
            if let Some(updated) = self.string_offset_set(&cur, last, &val)? {
                if inter.is_empty() {
                    self.set_var(name, Value::str(updated));
                } else {
                    self.index_set_path(name, inter, Value::str(updated))?;
                }
            }
            return Ok(());
        }
        let arr = self.ensure_path_array(name, inter);
        self.arr_set_key(&arr, last, val);
        Ok(())
    }

    /// `$name[k1]..[kM][] = val` — append into the array reached along `keys`,
    /// auto-vivifying (`M >= 0`, so `$a[] = v` is the empty-path case).
    pub fn append_path(&mut self, name: &str, keys: &[Value], val: Value) {
        let arr = self.ensure_path_array(name, keys);
        self.arr_push_auto(&arr, val);
    }

    /// Read `$name[k1]..[kN]` without mutating (for read-modify-write compound
    /// assignment and `++`/`--` on an element). Missing slots read as `Undef`.
    pub fn index_get_path(&self, name: &str, keys: &[Value]) -> Value {
        let mut cur = self.get_var(name);
        for key in keys {
            cur = self.index_get(&cur, key);
        }
        cur
    }

    /// `Cannot use object of type C as array`: an object that is not
    /// `ArrayAccess` (a closure and a generator included) used as an array.
    pub fn object_as_array_refusal(&self, obj: &Value) -> ContainerIssue {
        let class = self.instance_class(obj).unwrap_or_default();
        ContainerIssue::Refused(format!(
            "Cannot use object of type {} as array",
            display_class(&class)
        ))
    }

    /// The storage array of an `ArrayObject`/`ArrayIterator` whose `offsetGet`
    /// is the prelude's. The reference's `spl_array_get_dimension_ptr` hands a
    /// nested write (`$ao['k'][] = v`) the storage element itself, so the write
    /// lands in it; a user `offsetGet` returns a value instead. `None` for any
    /// other object.
    pub fn spl_native_storage(&self, recv: &Value) -> Option<Value> {
        let class = self.instance_class(recv)?;
        let native_get = matches!(
            self.method_owner_key(&class, "offsetget").as_deref(),
            Some("arrayobject" | "arrayiterator")
        );
        let storage = self.prop_get(recv, "storage");
        (native_get && self.is_array(&storage)).then_some(storage)
    }

    /// What stops `$name[k1]..[kN]` being used as a write target ending in
    /// `tail`: the first container along the path (the variable itself, then
    /// each `keys` prefix) that holds a scalar or a string. `Scalar` is an
    /// `Error` in the reference; `false` is still converted to an array, with a
    /// deprecation. A string is `Cannot use string offset as an array` anywhere
    /// but the last container, and there refuses every tail but `Set`. An
    /// unset/null slot and an array are left to the ordinary write path. An
    /// `ArrayAccess` object ends the check (it answers the dimension
    /// itself); any other object is `Cannot use object of type C as array`.
    pub fn write_container_issue(
        &self,
        name: &str,
        keys: &[Value],
        tail: WriteTail,
    ) -> Option<ContainerIssue> {
        let mut cur = self.get_var(name);
        for i in 0..=keys.len() {
            match cur {
                Value::Int(_) | Value::Float(_) | Value::Bool(true) => {
                    return Some(ContainerIssue::Scalar)
                }
                Value::Bool(false) => return Some(ContainerIssue::FalseToArray),
                Value::Str(_) if i < keys.len() => {
                    return Some(ContainerIssue::Refused(STRING_OFFSET_AS_ARRAY.into()))
                }
                Value::Str(_) => {
                    return tail
                        .string_refusal()
                        .map(|m| ContainerIssue::Refused(m.into()))
                }
                // `ArrayAccess` answers the dimension itself; the caller
                // dispatches to it.
                Value::Obj(_) if self.array_access_class(&cur).is_some() => return None,
                Value::Obj(_) if self.is_object_value(&cur) => {
                    return Some(self.object_as_array_refusal(&cur))
                }
                _ => {}
            }
            let Some(key) = keys.get(i) else { break };
            cur = self.index_get(&cur, key);
        }
        None
    }

    /// What stops `unset($name[k1]..[kN])`, ported from `ZEND_UNSET_DIM` and the
    /// `BP_VAR_UNSET` dimension fetch that leads to it. An int, float or `true`
    /// anywhere along the path is `Cannot unset offset in a non-array variable`;
    /// a string is `Cannot use string offset as an array` before the last
    /// container and `Cannot unset string offsets` as it; `false` is a
    /// deprecation after which nothing is removed. A missing slot, null, an
    /// array or an `ArrayAccess` object ends the check; any other object is
    /// `Cannot use object of type C as array` (`keys` is the full path, `N >= 1`).
    pub fn unset_container_issue(&self, name: &str, keys: &[Value]) -> Option<ContainerIssue> {
        let mut cur = self.get_var(name);
        let last = keys.len().saturating_sub(1);
        for (i, key) in keys.iter().enumerate() {
            match cur {
                Value::Int(_) | Value::Float(_) | Value::Bool(true) => {
                    return Some(ContainerIssue::Refused(
                        "Cannot unset offset in a non-array variable".into(),
                    ))
                }
                Value::Bool(false) => return Some(ContainerIssue::FalseToArray),
                Value::Str(_) if i < last => {
                    return Some(ContainerIssue::Refused(STRING_OFFSET_AS_ARRAY.into()))
                }
                Value::Str(_) => {
                    return Some(ContainerIssue::Refused(
                        "Cannot unset string offsets".into(),
                    ))
                }
                _ if self.is_array(&cur) => cur = self.index_get(&cur, key),
                Value::Obj(_)
                    if self.is_object_value(&cur) && self.array_access_class(&cur).is_none() =>
                {
                    return Some(self.object_as_array_refusal(&cur))
                }
                _ => return None,
            }
        }
        None
    }

    /// `index_get_path` for the read half of a read-modify-write — `$a[k] += 1`,
    /// `$a[k]++` — which diagnoses every step of the path.
    ///
    /// PHP fetches an RW path in write mode: an unset container is reported and
    /// then *auto-vivified* into an empty array, so the next segment reports its
    /// own missing key rather than "array offset on null". That is what makes
    /// `$a['p']['q'] += 5` on a fresh `$a` print `Undefined variable $a`,
    /// `Undefined array key "p"`, `Undefined array key "q"` — three misses, not a
    /// miss followed by two null-offset complaints, which is what a plain read of
    /// the same path prints.
    pub fn index_get_path_warn(&mut self, name: &str, keys: &[Value]) -> Value {
        let mut cur = self.get_var_warn(name);
        for key in keys {
            if matches!(cur, Value::Undef) {
                // The write-mode fetch put an empty array here; the key is still
                // missing from it.
                let k = self.diag_key(key);
                self.warn(format_args!("Undefined array key {k}"));
                continue;
            }
            cur = self.index_get_warn(&cur, key);
        }
        cur
    }

    /// Append a fresh empty array as a new element of `$name[k1]..[kN]` and return
    /// its handle — the pivot for a mid-path append (`$a[][k] = v`): the caller
    /// keeps writing through the returned child handle, so each `[]` in the chain
    /// appends exactly one new element the way PHP does.
    pub fn path_append_child(&mut self, name: &str, keys: &[Value]) -> Value {
        let arr = self.ensure_path_array(name, keys);
        let new = self.new_array();
        self.arr_push_auto(&arr, new.clone());
        new
    }

    /// Whether an append to `arr` has nowhere left to go — the next integer key
    /// is already taken, which only happens once `next_index` has saturated at
    /// `PHP_INT_MAX` because some element was written under that exact key.
    ///
    /// The reference detects this in `_zend_hash_index_add_or_update_i`: an
    /// append is an ADD at `nNextFreeElement`, and an ADD onto an existing key
    /// fails rather than overwriting. `false` for a non-array, which leaves the
    /// caller's own type handling in charge.
    pub fn append_slot_taken(&self, arr: &Value) -> bool {
        matches!(
            self.as_array(arr),
            Some(PhpObj::Array { entries, next_index })
                if entries.contains_key(&ArrayKey::Int(next_key(*next_index)))
        )
    }

    /// Append `v` under the next integer key of the array `arr` (a handle).
    ///
    /// Saturating: once `next_index` reaches `PHP_INT_MAX` this overwrites that
    /// key rather than overflowing. Callers reachable from `$a[] =` must consult
    /// [`PhpHost::append_slot_taken`] first and raise [`NEXT_ELEMENT_OCCUPIED`];
    /// the many stdlib callers that build a FRESH array cannot reach the
    /// saturated state and so do not check.
    pub fn arr_push_auto(&mut self, arr: &Value, v: Value) {
        if let Some(PhpObj::Array {
            entries,
            next_index,
        }) = self.as_array_mut(arr)
        {
            let k = ArrayKey::Int(take_next_index(next_index));
            entries.insert(k, v);
        }
    }

    /// Insert `v` under `key` in the array `arr` (a handle). An element that a `&`
    /// binding has turned into a reference is written *through* — the alias sees
    /// the new value and the slot stays a reference — instead of being replaced.
    pub fn arr_set_key(&mut self, arr: &Value, key: &Value, v: Value) {
        self.diagnose_array_offset(arr, key);
        let k = self.norm_key(key);
        if let Some(slot) = self.entry_ref_slot(arr, &k) {
            self.ref_cell_set(slot, v);
            return;
        }
        if let Some(PhpObj::Array {
            entries,
            next_index,
        }) = self.as_array_mut(arr)
        {
            if let ArrayKey::Int(n) = k {
                if n >= *next_index {
                    *next_index = n.saturating_add(1);
                }
            }
            entries.insert(k, v);
        }
    }

    /// Replace an array handle's entries with a re-indexed (`0..n`) value list,
    /// in place — the mutation is visible through every variable holding the same
    /// handle (`sort`/`rsort`). No-op if `arr` is not an array.
    /// Replace every entry of `arr` with `src`'s, keys included — how a
    /// by-reference OUT array is written through a handle the caller already
    /// held. A no-op unless both are arrays.
    pub fn arr_replace_all(&mut self, arr: &Value, src: &Value) {
        let Some(PhpObj::Array {
            entries,
            next_index,
        }) = self.as_array(src)
        else {
            return;
        };
        let (entries, next_index) = (entries.clone(), *next_index);
        if let Some(PhpObj::Array {
            entries: dst,
            next_index: dst_next,
        }) = self.as_array_mut(arr)
        {
            *dst = entries;
            *dst_next = next_index;
        }
    }

    pub fn arr_set_reindexed(&mut self, arr: &Value, vals: Vec<Value>) {
        if let Some(PhpObj::Array {
            entries,
            next_index,
        }) = self.as_array_mut(arr)
        {
            entries.clear();
            *next_index = 0;
            for v in vals {
                let k = ArrayKey::Int(*next_index);
                *next_index = next_index.saturating_add(1);
                entries.insert(k, v);
            }
        }
    }

    /// Replace an array handle's entries with an explicit ordered `(key, value)`
    /// list, preserving keys, in place (`asort`/`ksort`).
    pub fn arr_set_pairs(&mut self, arr: &Value, pairs: Vec<(Value, Value)>) {
        // Normalize keys under `&self` first, then take the `&mut self` borrow.
        let normed: Vec<(ArrayKey, Value)> = pairs
            .into_iter()
            .map(|(k, v)| (self.norm_key(&k), v))
            .collect();
        if let Some(PhpObj::Array {
            entries,
            next_index,
        }) = self.as_array_mut(arr)
        {
            entries.clear();
            *next_index = NO_INDEX;
            for (k, v) in normed {
                if let ArrayKey::Int(n) = k {
                    if n >= *next_index {
                        *next_index = n.saturating_add(1);
                    }
                }
                entries.insert(k, v);
            }
        }
    }

    /// Rebuild an array handle's entries from an ordered `(key, value)` list,
    /// renumbering integer keys `0..` while preserving string keys — the shared
    /// core of `array_shift`/`array_unshift`/`array_splice`. No-op if `arr` is not
    /// an array.
    fn arr_rebuild_reindexed(&mut self, arr: &Value, pairs: Vec<(Value, Value)>) {
        // Normalize keys under `&self` first, then take the `&mut self` borrow.
        let normed: Vec<(ArrayKey, Value)> = pairs
            .into_iter()
            .map(|(k, v)| (self.norm_key(&k), v))
            .collect();
        if let Some(PhpObj::Array {
            entries,
            next_index,
        }) = self.as_array_mut(arr)
        {
            entries.clear();
            *next_index = 0;
            for (k, v) in normed {
                match k {
                    // Integer keys are renumbered sequentially; string keys stay.
                    ArrayKey::Int(_) => {
                        let nk = ArrayKey::Int(*next_index);
                        *next_index = next_index.saturating_add(1);
                        entries.insert(nk, v);
                    }
                    ArrayKey::Str(_) => {
                        entries.insert(k, v);
                    }
                }
            }
        }
    }

    /// Remove and return the last element of `$var` (`array_pop`). Remaining keys
    /// are left untouched. PHP resets the next-append index only when the popped
    /// element held the top of the current append run (its key was `next_index-1`);
    /// a sparse/gapped array (e.g. `[5=>a, 10=>b]`) keeps its `next_index`.
    pub fn arr_pop_var(&mut self, name: &str) -> Value {
        let arr = self.get_var(name);
        if let Some(PhpObj::Array {
            entries,
            next_index,
        }) = self.as_array_mut(&arr)
        {
            let before = *next_index;
            let popped = entries.pop();
            if let Some((ArrayKey::Int(n), _)) = &popped {
                if before.checked_sub(1) == Some(*n) {
                    *next_index = *n;
                }
            }
            return popped.map(|(_, v)| v).unwrap_or(Value::Undef);
        }
        Value::Undef
    }

    /// Remove and return the first element of `$var` (`array_shift`), reindexing
    /// integer keys from `0` while preserving string keys. Returns null if the
    /// variable is unset/empty or not an array.
    pub fn arr_shift_var(&mut self, name: &str) -> Value {
        let arr = self.get_var(name);
        let Some(mut pairs) = self.array_pairs(&arr) else {
            return Value::Undef;
        };
        if pairs.is_empty() {
            return Value::Undef;
        }
        let (_, first) = pairs.remove(0);
        self.arr_rebuild_reindexed(&arr, pairs);
        first
    }

    /// Append each value to `$var` (`array_push`), auto-vivifying an array;
    /// returns the new element count.
    /// `Err` carries [`NEXT_ELEMENT_OCCUPIED`] once the array's next integer key
    /// is taken — `array_push` refuses the write for the same reason `$a[] =`
    /// does, and the refusal is a catchable `Error`.
    pub fn arr_push_var(&mut self, name: &str, vals: Vec<Value>) -> Result<Value, String> {
        let arr = self.ensure_array_var(name);
        for v in vals {
            if self.append_slot_taken(&arr) {
                return Err(NEXT_ELEMENT_OCCUPIED.to_string());
            }
            self.arr_push_auto(&arr, v);
        }
        Ok(Value::int(self.array_len(&arr)))
    }

    /// Prepend `vals` to `$var` (`array_unshift`) as a fresh `0`-based run,
    /// reindexing the existing integer keys after them (string keys preserved);
    /// returns the new element count.
    pub fn arr_unshift_var(&mut self, name: &str, vals: Vec<Value>) -> Value {
        let arr = self.ensure_array_var(name);
        let existing = self.array_pairs(&arr).unwrap_or_default();
        let mut combined: Vec<(Value, Value)> = Vec::with_capacity(vals.len() + existing.len());
        // A placeholder integer key marks each new value for renumbering; the
        // rebuild renumbers all integer keys, so the placeholder value is unused.
        for v in vals {
            combined.push((Value::int(0), v));
        }
        combined.extend(existing);
        self.arr_rebuild_reindexed(&arr, combined);
        Value::int(self.array_len(&arr))
    }

    /// `array_splice($var, offset, length?, replacement?)` — remove `length`
    /// elements at `offset` (negatives count from the end; omitted length runs to
    /// the end) and splice `replacement` (an array or a single value) in their
    /// place, reindexing integer keys. Returns a new array of the removed
    /// elements.
    pub fn arr_splice_var(&mut self, name: &str, args: &[Value]) -> Value {
        let arr = self.ensure_array_var(name);
        let pairs = self.array_pairs(&arr).unwrap_or_default();
        let n = pairs.len() as i64;
        let mut off = args.first().map(|v| v.to_int()).unwrap_or(0);
        if off < 0 {
            off = (n + off).max(0);
        }
        let off = off.min(n).max(0) as usize;
        let len = match args.get(1) {
            Some(v) if !matches!(v, Value::Undef) => {
                let l = v.to_int();
                if l < 0 {
                    (n - off as i64 + l).max(0) as usize
                } else {
                    (l as usize).min(pairs.len() - off)
                }
            }
            _ => pairs.len() - off,
        };
        let end = (off + len).min(pairs.len());
        // The replacement flattens to a value list (an array's keys are dropped).
        let replacement: Vec<Value> = match args.get(2) {
            Some(v) if self.is_array(v) => self
                .array_pairs(v)
                .unwrap_or_default()
                .into_iter()
                .map(|(_, x)| x)
                .collect(),
            Some(v) if !matches!(v, Value::Undef) => vec![v.clone()],
            _ => Vec::new(),
        };
        let removed: Vec<(Value, Value)> = pairs[off..end].to_vec();
        // Rebuild the source: kept prefix, replacement run, kept suffix.
        let mut rebuilt: Vec<(Value, Value)> = Vec::with_capacity(pairs.len());
        rebuilt.extend(pairs[..off].iter().cloned());
        for v in replacement {
            rebuilt.push((Value::int(0), v));
        }
        rebuilt.extend(pairs[end..].iter().cloned());
        self.arr_rebuild_reindexed(&arr, rebuilt);
        let out = self.new_array();
        self.arr_rebuild_reindexed(&out, removed);
        out
    }

    pub fn array_keys(&mut self, recv: &Value) -> Value {
        let keys: Vec<Value> = match self.as_array(recv) {
            Some(PhpObj::Array { entries, .. }) => entries.keys().map(|k| k.to_value()).collect(),
            _ => Vec::new(),
        };
        let arr = self.new_array();
        if let Some(PhpObj::Array {
            entries,
            next_index,
        }) = self.as_array_mut(&arr)
        {
            for (i, k) in keys.into_iter().enumerate() {
                entries.insert(ArrayKey::Int(i as i64), k);
                *next_index = (i + 1) as i64;
            }
        }
        arr
    }

    pub fn array_len(&self, recv: &Value) -> i64 {
        match self.as_array(recv) {
            Some(PhpObj::Array { entries, .. }) => entries.len() as i64,
            _ => 0,
        }
    }

    /// The array's `(key, value)` pairs, cloned (for `print_r`/`implode`/etc.).
    pub fn array_pairs(&self, recv: &Value) -> Option<Vec<(Value, Value)>> {
        match self.as_array(recv) {
            Some(PhpObj::Array { entries, .. }) => {
                let raw: Vec<(Value, Value)> = entries
                    .iter()
                    .map(|(k, v)| (k.to_value(), v.clone()))
                    .collect();
                Some(raw.into_iter().map(|(k, v)| (k, self.deref(v))).collect())
            }
            _ => None,
        }
    }

    /// The array's `(key, value, is_reference)` triples, values still raw. Only
    /// `var_dump`, which prints a `&` before a referenced element, needs to see
    /// which slots are references; every other reader wants `array_pairs`.
    pub fn array_pairs_marked(&self, recv: &Value) -> Option<Vec<(Value, Value, bool)>> {
        let Some(PhpObj::Array { entries, .. }) = self.as_array(recv) else {
            return None;
        };
        let raw: Vec<(Value, Value)> = entries
            .iter()
            .map(|(k, v)| (k.to_value(), v.clone()))
            .collect();
        Some(
            raw.into_iter()
                .map(|(k, v)| {
                    let is_ref = self.ref_slot_of_value(&v).is_some();
                    (k, self.deref(v), is_ref)
                })
                .collect(),
        )
    }

    pub fn is_array(&self, v: &Value) -> bool {
        matches!(self.as_array(v), Some(PhpObj::Array { .. }))
    }

    // ── value coercions (PHP semantics) ────────────────────────────────────

    /// PHP truthiness: `false`, `0`, `0.0`, `""`, `"0"`, `null`, and the empty
    /// array are falsy; everything else is truthy. A closure/object handle is
    /// always truthy (only the empty *array* is falsy among heap objects).
    pub fn is_truthy(&self, v: &Value) -> bool {
        match v {
            Value::Undef => false,
            Value::Bool(b) => *b,
            Value::Int(n) => *n != 0,
            Value::Float(f) => *f != 0.0,
            Value::Str(s) => !(s.is_empty() || s.as_str() == "0"),
            Value::Obj(_) => !self.is_array(v) || self.array_len(v) != 0,
            _ => true,
        }
    }

    /// PHP string cast (`(string)` / echo / interpolation).
    /// [`PhpHost::to_str`] for a conversion the PROGRAM asked for, with the two
    /// diagnostics such a conversion raises.
    ///
    /// Both are properties of the conversion, not of the value, so they belong
    /// at the one point every PHP-visible string coercion passes through rather
    /// than at each of its callers: `echo`, `.`, `"$x"`, `(string)`, `strval`,
    /// `implode` and `%s` all raise them, while `var_dump`, `json_encode`,
    /// `in_array` and a loose comparison against a string raise neither, because
    /// none of those converts anything.
    ///
    /// An array has no string form, so the reference substitutes the literal
    /// text `Array` and warns. A NaN has a string form — `"NAN"` — and warns
    /// anyway, because the text does not read back as a number. The infinities
    /// are the control that shows this is about NaN specifically and not about
    /// non-finite doubles: `(string) INF` is `"INF"` and says nothing.
    pub fn to_str_diag(&mut self, v: &Value) -> String {
        match v {
            Value::Obj(_) if self.is_array(v) => self.warn("Array to string conversion"),
            Value::Float(f) if f.is_nan() => {
                self.warn("unexpected NAN value was coerced to string");
            }
            _ => {}
        }
        self.to_str(v)
    }

    /// A value as a PHP string.
    ///
    /// The `Str` arm CLONES the buffer rather than going through `to_string()`.
    /// They are not the same call: `Value::Str` holds an `Arc<String>`, which
    /// `ToString` has no specialisation for, so `s.to_string()` ran the whole
    /// `core::fmt` machinery — `Display::fmt` → `Write::write_str` →
    /// `push_str` — for every string that reaches a concatenation, an
    /// interpolation, an array key or a library argument. A sampling profile of
    /// `$s .= "x$i,"` spent most of its VM time there.
    pub fn to_str(&self, v: &Value) -> String {
        match v {
            Value::Undef => String::new(),
            Value::Bool(b) => if *b { "1" } else { "" }.to_string(),
            Value::Int(n) => n.to_string(),
            Value::Float(f) => float_to_php_string(*f),
            Value::Str(s) => String::clone(s),
            Value::Obj(_) => match self.resource_info(v) {
                Some((id, _)) => format!("Resource id #{id}"),
                None => "Array".to_string(),
            },
            _ => String::new(),
        }
    }

    /// Step an int by `delta`, widening to float on overflow the way PHP does.
    /// `$x = PHP_INT_MAX; $x++;` leaves a float, not a wrapped negative — the
    /// engine has no integer overflow, so the type changes instead.
    fn int_step_impl(n: i64, delta: i64) -> Value {
        match n.checked_add(delta) {
            Some(v) => Value::int(v),
            None => Value::float(n as f64 + delta as f64),
        }
    }

    /// `++`/`--` applied to one value, with the diagnostics PHP raises for the
    /// operand types the operators do not actually change.
    ///
    /// The operators are not arithmetic on `$x + 1`: `null--` and `true++` leave
    /// the value alone, `""++` produces the *string* `"1"`, and `++` on a
    /// non-numeric string is Perl-style alphanumeric succession (`"Az"` → `"Ba"`,
    /// `"zz"` → `"aaa"`) rather than a numeric coercion. Only a numeric string
    /// goes through the number path.
    pub fn incdec_value(&mut self, old: &Value, inc: bool) -> Value {
        let delta = if inc { 1 } else { -1 };
        match old {
            // null++ is 1; null-- is a no-op PHP has announced it will change.
            Value::Undef => {
                if inc {
                    Value::int(1)
                } else {
                    self.warn(
                        "Decrement on type null has no effect, this will change in the next \
                         major version of PHP",
                    );
                    Value::Undef
                }
            }
            // Neither operator has ever affected a bool.
            Value::Bool(_) => {
                let word = if inc { "Increment" } else { "Decrement" };
                self.warn(format_args!(
                    "{word} on type bool has no effect, this will change in the next major \
                     version of PHP"
                ));
                old.clone()
            }
            // Past the int range the operators WIDEN rather than wrap: PHP has
            // no integer overflow, so `PHP_INT_MAX + 1` is a float and stepping
            // an int off either end produces one.
            Value::Int(n) => Self::int_step_impl(*n, delta),
            Value::Float(f) => Value::float(f + delta as f64),
            Value::Str(s) if is_numeric_string(s) => match parse_php_number(s) {
                Value::Float(f) => Value::float(f + delta as f64),
                Value::Int(n) => Self::int_step_impl(n, delta),
                other => other,
            },
            Value::Str(s) if s.is_empty() => {
                if inc {
                    self.deprecated(
                        "Increment on non-numeric string is deprecated, use str_increment() \
                         instead",
                    );
                    Value::str("1")
                } else {
                    self.deprecated("Decrement on empty string is deprecated as non-numeric");
                    Value::int(-1)
                }
            }
            Value::Str(s) => {
                if inc {
                    self.deprecated(
                        "Increment on non-numeric string is deprecated, use str_increment() \
                         instead",
                    );
                    Value::str(increment_alnum_string(s))
                } else {
                    self.deprecated(
                        "Decrement on non-numeric string has no effect and is deprecated",
                    );
                    old.clone()
                }
            }
            other => other.clone(),
        }
    }

    /// Coerce to an `Int`/`Float` `Value` for arithmetic and comparison.
    pub fn to_number(&self, v: &Value) -> Value {
        match v {
            Value::Int(_) | Value::Float(_) => v.clone(),
            Value::Bool(b) => Value::int(*b as i64),
            Value::Undef => Value::int(0),
            Value::Str(s) => parse_php_number(s),
            // A resource casts to its number.
            Value::Obj(_) if self.is_stream(v) => {
                Value::int(self.resource_info(v).map_or(0, |(id, _)| id))
            }
            // Empty array → 0, non-empty array → 1; a closure/object casts to 1.
            Value::Obj(_) => Value::int(if self.is_array(v) && self.array_len(v) == 0 {
                0
            } else {
                1
            }),
            _ => Value::int(0),
        }
    }

    /// PHP `gettype` name.
    pub fn type_name(&self, v: &Value) -> &'static str {
        match v {
            Value::Undef => "NULL",
            Value::Bool(_) => "boolean",
            Value::Int(_) => "integer",
            Value::Float(_) => "double",
            Value::Str(_) => "string",
            Value::Obj(_) => {
                if self.is_array(v) {
                    "array"
                } else if let Some((_, closed)) = self.resource_info(v) {
                    if closed {
                        "resource (closed)"
                    } else {
                        "resource"
                    }
                } else {
                    "object"
                }
            }
            _ => "unknown type",
        }
    }
}

/// The standard predefined constants seeded onto every fresh host. Covers the
/// core/version/OS constants, the `M_*` math constants, and the integer flag
/// constants the standard library accepts (sort/count/str_pad/array_filter/preg/
/// json/filter/mbstring/file/pathinfo/entities/error-level families), so a
/// program that writes `SORT_STRING` or `FILTER_VALIDATE_EMAIL` gets the real
/// integer rather than the bare name.
/// Whether a variable name (sans `$`) is a PHP superglobal — auto-global across
/// every scope, resolved against the global frame.
pub fn is_superglobal(name: &str) -> bool {
    // First-byte gate before the eleven comparisons. This is asked on every
    // by-name variable access — it was 3% of a `foreach` profile — and the
    // answer is almost always no, which one byte settles for every name that
    // does not begin like a superglobal.
    if !matches!(
        (*crate::bytes::to_bytes(name)).first(),
        Some(b'_' | b'G' | b'a')
    ) {
        return false;
    }
    matches!(
        name,
        "_SERVER"
            | "_GET"
            | "_POST"
            | "_REQUEST"
            | "_COOKIE"
            | "_FILES"
            | "_ENV"
            | "_SESSION"
            | "GLOBALS"
            | "argv"
            | "argc"
    )
}

/// The PHP version this engine answers to: the reference it is measured
/// against (`tests/data/parity_reference_version.txt`). `PHP_VERSION`,
/// `PHP_*_VERSION`, `PHP_VERSION_ID` and `phpversion()` all derive from it, so a
/// `version_compare(PHP_VERSION, "8.4", ">=")` takes the branch the reference does.
pub const PHP_VERSION: &str = "8.5.11";

/// `PHP_VERSION` split into its three numbers.
fn php_version_parts() -> (i64, i64, i64) {
    let mut it = PHP_VERSION.split('.').map(|p| p.parse().unwrap_or(0));
    let mut next = || it.next().unwrap_or(0);
    (next(), next(), next())
}

/// `PHP_BINARY`: `php_binary_init` (main/main.c) over the CLI SAPI's
/// `executable_location`, which is `argv[0]`. A name without a `/` is searched
/// for along `PATH` — the first `dir/name` that resolves, is executable and is a
/// regular file — and one with a `/` is resolved where it is; either way the
/// answer is the `realpath`. Nothing found is the empty string.
#[cfg(unix)]
fn php_binary() -> String {
    use std::ffi::CString;
    use std::os::unix::ffi::OsStrExt;
    use std::path::{Path, PathBuf};

    let executable = |p: &Path| {
        CString::new(p.as_os_str().as_bytes())
            .is_ok_and(|c| unsafe { libc::access(c.as_ptr(), libc::X_OK) } == 0)
    };
    let Some(argv0) = std::env::args_os().next() else {
        return String::new();
    };
    let found: Option<PathBuf> = if argv0.as_bytes().contains(&b'/') {
        std::fs::canonicalize(&argv0)
            .ok()
            .filter(|real| executable(real))
    } else {
        let path = std::env::var_os("PATH").unwrap_or_default();
        path.as_bytes()
            .split(|&b| b == b':')
            .filter(|dir| !dir.is_empty())
            .find_map(|dir| {
                let mut candidate = dir.to_vec();
                candidate.push(b'/');
                candidate.extend_from_slice(argv0.as_bytes());
                let real = std::fs::canonicalize(std::ffi::OsStr::from_bytes(&candidate)).ok()?;
                (executable(&real) && real.is_file()).then_some(real)
            })
    };
    found
        .map(|p| p.to_string_lossy().into_owned())
        .unwrap_or_default()
}

/// `PHP_BINARY` on Windows: `GetModuleFileName(0)`, the running executable.
#[cfg(not(unix))]
fn php_binary() -> String {
    std::env::current_exe()
        .map(|p| p.to_string_lossy().into_owned())
        .unwrap_or_default()
}

/// The deprecation suffix of each predefined constant the reference declares
/// `#[\Deprecated]` (its stubs' `since` and `message`, rendered as
/// `get_deprecation_suffix_from_attribute` renders them), or `None`. Only the
/// constants [`predefined_constants`] seeds are listed: the reference's other
/// deprecated ones (`MHASH_*`, `ASSERT_*`, `MT_RAND_PHP`, …) are not defined here.
fn deprecated_constant_suffix(name: &str) -> Option<&'static str> {
    Some(match name {
        "E_STRICT" => "since 8.4, the error level was removed",
        "DATE_RFC7231" => {
            "since 8.5, as this format ignores the associated timezone and always uses GMT"
        }
        "SUNFUNCS_RET_TIMESTAMP" | "SUNFUNCS_RET_STRING" | "SUNFUNCS_RET_DOUBLE" => {
            "since 8.4, as date_sunrise() and date_sunset() were deprecated in 8.1"
        }
        "FILTER_SANITIZE_STRING" | "FILTER_SANITIZE_STRIPPED" => {
            "since 8.1, use htmlspecialchars() instead"
        }
        _ => return None,
    })
}

/// Whether `name` is a constant the engine itself defines — one the reference
/// substitutes while compiling (`zend_try_ct_eval_const`).
pub fn is_predefined_constant(name: &str) -> bool {
    thread_local! {
        static NAMES: FxHashSet<String> = predefined_constants().into_keys().collect();
    }
    NAMES.with(|n| n.contains(name))
}

fn predefined_constants() -> FxHashMap<String, Value> {
    let mut m = FxHashMap::default();
    let mut si = |k: &str, v: i64| {
        m.insert(k.to_string(), Value::int(v));
    };
    // core / platform
    si("PHP_INT_MAX", i64::MAX);
    si("PHP_INT_MIN", i64::MIN);
    si("PHP_INT_SIZE", 8);
    si("PHP_FLOAT_DIG", 15);
    let (major, minor, release) = php_version_parts();
    si("PHP_MAJOR_VERSION", major);
    si("PHP_MINOR_VERSION", minor);
    si("PHP_RELEASE_VERSION", release);
    si("PHP_VERSION_ID", major * 10000 + minor * 100 + release);
    si("PHP_MAXPATHLEN", 1024);
    si("PHP_FD_SETSIZE", 1024);
    si("INI_USER", 1);
    si("INI_PERDIR", 2);
    si("INI_SYSTEM", 4);
    si("INI_ALL", 7);
    si("CONNECTION_ABORTED", 1);
    si("CONNECTION_NORMAL", 0);
    si("CONNECTION_TIMEOUT", 2);
    si("PHP_QUERY_RFC1738", 1);
    si("PHP_QUERY_RFC3986", 2);
    for (i, k) in [
        "UPLOAD_ERR_OK",
        "UPLOAD_ERR_INI_SIZE",
        "UPLOAD_ERR_FORM_SIZE",
        "UPLOAD_ERR_PARTIAL",
        "UPLOAD_ERR_NO_FILE",
    ]
    .into_iter()
    .enumerate()
    {
        si(k, i as i64);
    }
    si("UPLOAD_ERR_NO_TMP_DIR", 6);
    si("UPLOAD_ERR_CANT_WRITE", 7);
    si("UPLOAD_ERR_EXTENSION", 8);
    si("PHP_ROUND_HALF_UP", 1);
    si("PHP_ROUND_HALF_DOWN", 2);
    si("PHP_ROUND_HALF_EVEN", 3);
    si("PHP_ROUND_HALF_ODD", 4);
    // Error levels — the canonical values live in `crate::errlevel`, which the
    // `error_reporting` mask and the `-d error_reporting=…` parser share.
    si("E_ERROR", errlevel::E_ERROR);
    si("E_WARNING", errlevel::E_WARNING);
    si("E_PARSE", errlevel::E_PARSE);
    si("E_NOTICE", errlevel::E_NOTICE);
    si("E_CORE_ERROR", errlevel::E_CORE_ERROR);
    si("E_CORE_WARNING", errlevel::E_CORE_WARNING);
    si("E_COMPILE_ERROR", errlevel::E_COMPILE_ERROR);
    si("E_COMPILE_WARNING", errlevel::E_COMPILE_WARNING);
    si("E_STRICT", errlevel::E_STRICT);
    si("E_RECOVERABLE_ERROR", errlevel::E_RECOVERABLE_ERROR);
    si("E_DEPRECATED", errlevel::E_DEPRECATED);
    si("E_ALL", errlevel::E_ALL);
    si("E_USER_ERROR", errlevel::E_USER_ERROR);
    si("E_USER_WARNING", errlevel::E_USER_WARNING);
    si("E_USER_NOTICE", errlevel::E_USER_NOTICE);
    si("E_USER_DEPRECATED", errlevel::E_USER_DEPRECATED);
    // sort / count / str_pad / array_filter
    si("SORT_REGULAR", 0);
    si("SORT_NUMERIC", 1);
    si("SORT_STRING", 2);
    si("SORT_DESC", 3);
    si("SORT_ASC", 4);
    si("SORT_LOCALE_STRING", 5);
    si("SORT_NATURAL", 6);
    si("SORT_FLAG_CASE", 8);
    // output control (main/php_output.h)
    si("PHP_OUTPUT_HANDLER_START", 1);
    si("PHP_OUTPUT_HANDLER_WRITE", 0);
    si("PHP_OUTPUT_HANDLER_FLUSH", 4);
    si("PHP_OUTPUT_HANDLER_CLEAN", 2);
    si("PHP_OUTPUT_HANDLER_FINAL", 8);
    si("PHP_OUTPUT_HANDLER_CONT", 0);
    si("PHP_OUTPUT_HANDLER_END", 8);
    si("PHP_OUTPUT_HANDLER_CLEANABLE", 16);
    si("PHP_OUTPUT_HANDLER_FLUSHABLE", 32);
    si("PHP_OUTPUT_HANDLER_REMOVABLE", 64);
    si("PHP_OUTPUT_HANDLER_STDFLAGS", 112);
    si("PHP_OUTPUT_HANDLER_STARTED", 4096);
    si("PHP_OUTPUT_HANDLER_DISABLED", 8192);
    si("PHP_OUTPUT_HANDLER_PROCESSED", 16384);
    si("COUNT_NORMAL", 0);
    si("COUNT_RECURSIVE", 1);
    si("STR_PAD_RIGHT", 1);
    si("STR_PAD_LEFT", 0);
    si("STR_PAD_BOTH", 2);
    si("ARRAY_FILTER_USE_KEY", ARRAY_FILTER_USE_KEY);
    si("ARRAY_FILTER_USE_BOTH", ARRAY_FILTER_USE_BOTH);
    // extract() — the low values pick one mode, EXTR_REFS is an OR-able bit.
    si("EXTR_OVERWRITE", 0);
    si("EXTR_SKIP", 1);
    si("EXTR_PREFIX_SAME", 2);
    si("EXTR_PREFIX_ALL", 3);
    si("EXTR_PREFIX_INVALID", 4);
    si("EXTR_PREFIX_IF_EXISTS", 5);
    si("EXTR_IF_EXISTS", 6);
    si("EXTR_REFS", 256);
    // locale categories: the C library's own numbering, which differs by platform
    si("LC_CTYPE", i64::from(libc::LC_CTYPE));
    si("LC_NUMERIC", i64::from(libc::LC_NUMERIC));
    si("LC_TIME", i64::from(libc::LC_TIME));
    si("LC_COLLATE", i64::from(libc::LC_COLLATE));
    si("LC_MONETARY", i64::from(libc::LC_MONETARY));
    si("LC_MESSAGES", i64::from(libc::LC_MESSAGES));
    si("LC_ALL", i64::from(libc::LC_ALL));
    // preg
    si("PREG_PATTERN_ORDER", 1);
    si("PREG_SET_ORDER", 2);
    si("PREG_OFFSET_CAPTURE", 256);
    si("PREG_UNMATCHED_AS_NULL", 512);
    si("PREG_SPLIT_NO_EMPTY", 1);
    si("PREG_SPLIT_DELIM_CAPTURE", 2);
    si("PREG_SPLIT_OFFSET_CAPTURE", 4);
    si("PREG_GREP_INVERT", 1);
    // The `preg_last_error()` codes, whose names are what user code compares
    // against rather than the bare numbers.
    si("PREG_NO_ERROR", 0);
    si("PREG_INTERNAL_ERROR", 1);
    si("PREG_BACKTRACK_LIMIT_ERROR", 2);
    si("PREG_RECURSION_LIMIT_ERROR", 3);
    si("PREG_BAD_UTF8_ERROR", 4);
    si("PREG_BAD_UTF8_OFFSET_ERROR", 5);
    si("PREG_JIT_STACKLIMIT_ERROR", 6);
    // json
    si("JSON_HEX_TAG", 1);
    si("JSON_HEX_AMP", 2);
    si("JSON_HEX_APOS", 4);
    si("JSON_HEX_QUOT", 8);
    si("JSON_FORCE_OBJECT", 16);
    si("JSON_NUMERIC_CHECK", 32);
    si("JSON_UNESCAPED_SLASHES", 64);
    si("JSON_PRETTY_PRINT", 128);
    si("JSON_UNESCAPED_UNICODE", 256);
    si("JSON_PARTIAL_OUTPUT_ON_ERROR", 512);
    si("JSON_PRESERVE_ZERO_FRACTION", 1024);
    si("JSON_UNESCAPED_LINE_TERMINATORS", 2048);
    si("JSON_THROW_ON_ERROR", 4194304);
    si("JSON_OBJECT_AS_ARRAY", 1);
    si("JSON_BIGINT_AS_STRING", 2);
    si("JSON_ERROR_NONE", 0);
    si("JSON_ERROR_DEPTH", 1);
    si("JSON_ERROR_STATE_MISMATCH", 2);
    si("JSON_ERROR_CTRL_CHAR", 3);
    si("JSON_ERROR_SYNTAX", 4);
    si("JSON_ERROR_UTF8", 5);
    si("JSON_INVALID_UTF8_IGNORE", 1_048_576);
    si("JSON_INVALID_UTF8_SUBSTITUTE", 2_097_152);
    si("JSON_ERROR_RECURSION", 6);
    si("JSON_ERROR_INF_OR_NAN", 7);
    si("JSON_ERROR_UNSUPPORTED_TYPE", 8);
    si("JSON_ERROR_INVALID_PROPERTY_NAME", 9);
    si("JSON_ERROR_UTF16", 10);
    si("JSON_ERROR_NON_BACKED_ENUM", 11);
    // url — `parse_url()`'s `$component` selectors (ext/standard/url.h).
    si("PHP_URL_SCHEME", 0);
    si("PHP_URL_HOST", 1);
    si("PHP_URL_PORT", 2);
    si("PHP_URL_USER", 3);
    si("PHP_URL_PASS", 4);
    si("PHP_URL_PATH", 5);
    si("PHP_URL_QUERY", 6);
    si("PHP_URL_FRAGMENT", 7);
    // filter
    si("INPUT_GET", 1);
    si("INPUT_POST", 0);
    si("FILTER_DEFAULT", 516);
    si("FILTER_UNSAFE_RAW", 516);
    si("FILTER_VALIDATE_INT", 257);
    si("FILTER_VALIDATE_BOOLEAN", 258);
    si("FILTER_VALIDATE_BOOL", 258);
    si("FILTER_VALIDATE_FLOAT", 259);
    si("FILTER_VALIDATE_REGEXP", 272);
    si("FILTER_VALIDATE_DOMAIN", 277);
    si("FILTER_VALIDATE_URL", 273);
    si("FILTER_VALIDATE_EMAIL", 274);
    si("FILTER_VALIDATE_IP", 275);
    si("FILTER_VALIDATE_MAC", 276);
    si("FILTER_SANITIZE_STRING", 513);
    si("FILTER_SANITIZE_STRIPPED", 513);
    si("FILTER_SANITIZE_ENCODED", 514);
    si("FILTER_SANITIZE_SPECIAL_CHARS", 515);
    si("FILTER_SANITIZE_FULL_SPECIAL_CHARS", 522);
    si("FILTER_SANITIZE_EMAIL", 517);
    si("FILTER_SANITIZE_URL", 518);
    si("FILTER_SANITIZE_NUMBER_INT", 519);
    si("FILTER_SANITIZE_NUMBER_FLOAT", 520);
    si("FILTER_SANITIZE_ADD_SLASHES", 523);
    si("FILTER_FLAG_ALLOW_OCTAL", 1);
    si("FILTER_FLAG_ALLOW_HEX", 2);
    si("FILTER_FLAG_STRIP_LOW", 4);
    si("FILTER_FLAG_STRIP_HIGH", 8);
    si("FILTER_FLAG_ALLOW_FRACTION", 4096);
    si("FILTER_FLAG_ALLOW_THOUSAND", 8192);
    si("FILTER_FLAG_ALLOW_SCIENTIFIC", 16384);
    si("FILTER_FLAG_IPV4", 1048576);
    si("FILTER_FLAG_IPV6", 2097152);
    si("FILTER_FLAG_HOSTNAME", 1048576);
    si("FILTER_NULL_ON_FAILURE", 134217728);
    si("FILTER_REQUIRE_SCALAR", 33554432);
    si("FILTER_REQUIRE_ARRAY", 16777216);
    si("FILTER_FORCE_ARRAY", 67108864);
    // mbstring
    si("MB_CASE_UPPER", 0);
    si("MB_CASE_LOWER", 1);
    si("MB_CASE_TITLE", 2);
    // file / dir
    si("FILE_USE_INCLUDE_PATH", 1);
    si("FILE_APPEND", 8);
    si("FILE_IGNORE_NEW_LINES", 2);
    si("FILE_SKIP_EMPTY_LINES", 4);
    si("FILE_NO_DEFAULT_CONTEXT", 16);
    si("LOCK_SH", 1);
    si("LOCK_EX", 2);
    si("LOCK_UN", 3);
    si("LOCK_NB", 4);
    // fseek whence
    si("SEEK_SET", 0);
    si("SEEK_CUR", 1);
    si("SEEK_END", 2);
    si("SCANDIR_SORT_ASCENDING", 0);
    si("SCANDIR_SORT_DESCENDING", 1);
    si("SCANDIR_SORT_NONE", 2);
    // glob / fnmatch. The values live next to the matcher that reads them, in
    // `stdlib::fileio`, so a flag cannot be seeded with one bit and tested with
    // another — which is what had happened before these were seeded at all.
    si("GLOB_ERR", crate::stdlib::fileio::GLOB_ERR);
    si("GLOB_MARK", crate::stdlib::fileio::GLOB_MARK);
    si("GLOB_NOCHECK", crate::stdlib::fileio::GLOB_NOCHECK);
    si("GLOB_NOSORT", crate::stdlib::fileio::GLOB_NOSORT);
    si("GLOB_BRACE", crate::stdlib::fileio::GLOB_BRACE);
    si("GLOB_NOESCAPE", crate::stdlib::fileio::GLOB_NOESCAPE);
    si("GLOB_ONLYDIR", crate::stdlib::fileio::GLOB_ONLYDIR);
    si(
        "GLOB_AVAILABLE_FLAGS",
        crate::stdlib::fileio::GLOB_AVAILABLE_FLAGS,
    );
    si("FNM_NOESCAPE", crate::stdlib::fileio::FNM_NOESCAPE);
    si("FNM_PATHNAME", crate::stdlib::fileio::FNM_PATHNAME);
    si("FNM_PERIOD", crate::stdlib::fileio::FNM_PERIOD);
    si("FNM_CASEFOLD", crate::stdlib::fileio::FNM_CASEFOLD);
    // gmp_div_q / gmp_div_r / gmp_div_qr rounding modes.
    si("GMP_ROUND_ZERO", crate::stdlib::gmp::GMP_ROUND_ZERO);
    si("GMP_ROUND_PLUSINF", crate::stdlib::gmp::GMP_ROUND_PLUSINF);
    si("GMP_ROUND_MINUSINF", crate::stdlib::gmp::GMP_ROUND_MINUSINF);
    // hash_init flag: HMAC the fed data under the key argument.
    si("HASH_HMAC", crate::stdlib::hashext::HASH_HMAC);
    // debug_backtrace / debug_print_backtrace option bits.
    si("DEBUG_BACKTRACE_PROVIDE_OBJECT", 1);
    si("DEBUG_BACKTRACE_IGNORE_ARGS", 2);
    si("PATHINFO_DIRNAME", 1);
    si("PATHINFO_BASENAME", 2);
    si("PATHINFO_EXTENSION", 4);
    si("PATHINFO_FILENAME", 8);
    si("PATHINFO_ALL", 15);
    // array_change_key_case
    si("CASE_LOWER", 0);
    si("CASE_UPPER", 1);
    // html entities
    si("HTML_SPECIALCHARS", 0);
    si("HTML_ENTITIES", 1);
    si("ENT_NOQUOTES", 0);
    si("ENT_COMPAT", 2);
    si("ENT_QUOTES", 3);
    si("ENT_HTML401", 0);
    si("ENT_HTML5", 48);
    si("ENT_XML1", 16);
    si("ENT_XHTML", 32);
    si("ENT_SUBSTITUTE", 8);
    si("ENT_IGNORE", 4);
    si("SUNFUNCS_RET_TIMESTAMP", 0);
    si("SUNFUNCS_RET_STRING", 1);
    si("SUNFUNCS_RET_DOUBLE", 2);
    // string constants
    let mut ss = |k: &str, v: &str| {
        m.insert(k.to_string(), Value::str(v.to_string()));
    };
    ss("PHP_EOL", "\n");
    ss("ICONV_IMPL", "libiconv");
    ss("ICONV_VERSION", "1.11");
    // ext/date's format constants, the same strings as `DateTimeInterface::*`.
    for (k, v) in [
        ("DATE_ATOM", r"Y-m-d\TH:i:sP"),
        ("DATE_COOKIE", "l, d-M-Y H:i:s T"),
        ("DATE_ISO8601", r"Y-m-d\TH:i:sO"),
        ("DATE_ISO8601_EXPANDED", r"X-m-d\TH:i:sP"),
        ("DATE_RFC822", "D, d M y H:i:s O"),
        ("DATE_RFC850", "l, d-M-y H:i:s T"),
        ("DATE_RFC1036", "D, d M y H:i:s O"),
        ("DATE_RFC1123", "D, d M Y H:i:s O"),
        ("DATE_RFC7231", r"D, d M Y H:i:s \G\M\T"),
        ("DATE_RFC2822", "D, d M Y H:i:s O"),
        ("DATE_RFC3339", r"Y-m-d\TH:i:sP"),
        ("DATE_RFC3339_EXTENDED", r"Y-m-d\TH:i:s.vP"),
        ("DATE_RSS", "D, d M Y H:i:s O"),
        ("DATE_W3C", r"Y-m-d\TH:i:sP"),
    ] {
        ss(k, v);
    }
    ss("PHP_VERSION", PHP_VERSION);
    ss("PHP_EXTRA_VERSION", "");
    ss("PHP_SAPI", "cli");
    ss("PHP_BINARY", &php_binary());
    ss("PHP_SHLIB_SUFFIX", "so");
    ss(
        "PHP_OS",
        if cfg!(target_os = "macos") {
            "Darwin"
        } else if cfg!(target_os = "windows") {
            "WINNT"
        } else {
            "Linux"
        },
    );
    ss(
        "PHP_OS_FAMILY",
        if cfg!(target_os = "windows") {
            "Windows"
        } else if cfg!(target_os = "macos") {
            "Darwin"
        } else {
            "Linux"
        },
    );
    ss(
        "DIRECTORY_SEPARATOR",
        if cfg!(windows) { "\\" } else { "/" },
    );
    ss("PATH_SEPARATOR", if cfg!(windows) { ";" } else { ":" });
    // math constants
    let mut sf = |k: &str, v: f64| {
        m.insert(k.to_string(), Value::float(v));
    };
    sf("M_PI", std::f64::consts::PI);
    sf("M_E", std::f64::consts::E);
    sf("M_SQRT2", std::f64::consts::SQRT_2);
    sf("M_SQRT1_2", std::f64::consts::FRAC_1_SQRT_2);
    sf("M_SQRT3", 1.7320508075688772);
    sf("M_2_SQRTPI", std::f64::consts::FRAC_2_SQRT_PI);
    sf("M_PI_2", std::f64::consts::FRAC_PI_2);
    sf("M_PI_4", std::f64::consts::FRAC_PI_4);
    sf("M_1_PI", std::f64::consts::FRAC_1_PI);
    sf("M_2_PI", std::f64::consts::FRAC_2_PI);
    sf("M_LN2", std::f64::consts::LN_2);
    sf("M_LN10", std::f64::consts::LN_10);
    sf("M_LOG2E", std::f64::consts::LOG2_E);
    sf("M_LOG10E", std::f64::consts::LOG10_E);
    sf("M_EULER", 0.5772156649015329);
    sf("M_SQRTPI", 1.7724538509055159);
    sf("M_LNPI", 1.1447298858494002);
    sf("PHP_FLOAT_EPSILON", f64::EPSILON);
    sf("PHP_FLOAT_MAX", f64::MAX);
    sf("PHP_FLOAT_MIN", f64::MIN_POSITIVE);
    sf("INF", f64::INFINITY);
    sf("NAN", f64::NAN);
    // build facts, booleans in the reference
    for k in [
        "ZEND_THREAD_SAFE",
        "ZEND_DEBUG_BUILD",
        "PHP_DEBUG",
        "PHP_ZTS",
    ] {
        m.insert(k.to_string(), Value::bool(false));
    }
    m
}

// ── thread-local host access ──────────────────────────────────────────────

thread_local! {
    static HOST: RefCell<PhpHost> = RefCell::new(PhpHost::new());

    /// The source line a diagnostic raised right now belongs to. The builtin
    /// that is about to warn records it from the line table of the op it is
    /// executing, so the host never has to walk the VM.
    ///
    /// It lives OUTSIDE `HOST` because it is written far more often than it is
    /// read: every variable read, every array subscript, and every arithmetic
    /// operator stamps it so that a diagnostic it MIGHT raise names the right
    /// line, and hardly any of them raises one. Through `HOST` each of those
    /// stamps borrowed the entire host through a `RefCell`; this is a store.
    static WARN_LINE: Cell<u32> = const { Cell::new(0) };
}

/// Record the line a diagnostic raised next belongs to.
pub fn set_warn_line(line: u32) {
    WARN_LINE.with(|c| c.set(line));
}

/// The line [`set_warn_line`] last recorded.
pub fn warn_line() -> u32 {
    WARN_LINE.with(Cell::get)
}

/// Run `f` with mutable access to the current thread's `PhpHost`.
pub fn with_host<R>(f: impl FnOnce(&mut PhpHost) -> R) -> R {
    HOST.with(|h| f(&mut h.borrow_mut()))
}

/// Reset the host to a fresh state (new heap, new global scope).
pub fn reset_host() {
    CUR_GEN.with(|c| c.set(None));
    crate::stdlib::system::reset_lc_ctype();
    crate::stdlib::strings::reset_mblen_state();
    set_warn_line(0);
    HOST.with(|h| *h.borrow_mut() = PhpHost::new());
}

// ── generators (host-side stackful coroutines) ──────────────────────────────
//
// A generator function's body runs on its own native stack inside a
// `corosensei::Coroutine`. `run_chunk_on` (the fusevm VM run loop) executes *on
// that stack*, so a `yield` deep inside the body suspends the whole VM back to
// the resumer with one stack switch — no fusevm change. Same design the sibling
// node-js frontend uses. Single-threaded: the coroutine shares the thread-local
// `HOST`, and no `with_host` borrow is ever held across a `resume`/`suspend`.
//
// The coroutine yields `()` (the current key/value are stashed in the `GenCell`),
// is resumed with a `Value` (the `->send($x)` argument), and returns
// `Result<Value, String>` (the generator body's `return` value, or an error).
// corosensei's generics are `Coroutine<Input, Yield, Return>`: the body is resumed
// with `Input` (the `->send($x)` value), yields `Yield` (here `()` — the current
// key/value ride in the `GenCell`), and finally returns `Return`.
type GenCoro = corosensei::Coroutine<Value, (), Result<Value, String>>;
type GenYielder = corosensei::Yielder<Value, ()>;

thread_local! {
    /// Id of the generator whose body is currently executing (the one a `yield`
    /// suspends), or `None` at the root / inside a non-generator call.
    static CUR_GEN: std::cell::Cell<Option<u32>> = const { std::cell::Cell::new(None) };
}

/// A forced completion injected at a suspended `yield`: `->throw()`, or the
/// generator being destroyed while suspended, which unwinds the body as a
/// `return` would — `finally` blocks run, `catch` blocks do not.
enum GenInject {
    Throw(Value),
    Destroy,
}

/// The error a `yield` reports when its generator is being destroyed. The
/// `return` signal is already set; the dispatcher only has to halt the chunk.
pub const GEN_DESTROY: &str = "__generator_destroy__";

/// The volatile execution state swapped in/out at every generator resume/suspend
/// boundary, so a suspended generator's half-finished call frame and in-flight
/// signal/throw never leak into the resumer (and vice versa). The global scope
/// (`scopes[0]`) is *not* part of this — it stays shared, so superglobals and
/// `global $x` work identically inside a generator.
#[derive(Default)]
struct GenContext {
    /// The generator's own call frames (everything above the global scope).
    frames: Vec<Scope>,
    signal: Option<Signal>,
    pending_throw: Option<Value>,
    error: Option<String>,
}

/// One live generator.
struct GenCell {
    /// The generator function's name as `zend_generator_get_debug_info`
    /// reports it under `function`: `f`, `A::m` (always `::`), or a closure's
    /// `{closure:…}` name. Fixed when the generator is created.
    function: String,
    /// The suspended coroutine. `None` only while it is actively running (taken
    /// out across `resume` so the body can re-borrow the host freely).
    coro: Option<GenCoro>,
    /// Raw pointer to the coroutine body's `Yielder`, published on entry. Valid
    /// for the whole body lifetime; only read from inside that body (its stack is
    /// live), on the same thread.
    yielder: *const GenYielder,
    ctx: GenContext,
    /// The most recently yielded key and value (read by `current()`/`key()`).
    cur_key: Value,
    cur_val: Value,
    /// The next auto-increment integer key (PHP numbers un-keyed yields 0,1,2…).
    auto_key: i64,
    /// The body's `return` value, once it has finished (`getReturn`).
    ret: Value,
    started: bool,
    /// `ZEND_GENERATOR_AT_FIRST_YIELD`: primed, and not resumed since — the
    /// only state `rewind()` accepts.
    at_first_yield: bool,
    done: bool,
    inject: Option<GenInject>,
    /// Being destroyed while suspended: a `yield` reached now (one in a
    /// `finally`) is an error rather than a suspension.
    destroying: bool,
}

impl PhpHost {
    pub fn is_generator_val(&self, v: &Value) -> bool {
        matches!(self.as_array(v), Some(PhpObj::Generator { .. }))
    }
    fn gen_id(&self, v: &Value) -> Option<u32> {
        match self.as_array(v) {
            Some(PhpObj::Generator { id }) => Some(*id),
            _ => None,
        }
    }
    /// Swap the volatile execution context in one shot, returning the previous one:
    /// installs a generator's frames/signal/throw on resume, pulls them back on
    /// suspend. The global scope (index 0) is left untouched, so superglobals and
    /// `global $x` stay shared.
    fn install_gen_ctx(&mut self, mut c: GenContext) -> GenContext {
        // Detach the caller's frames (everything above the global scope) and
        // install the generator's own frames in their place.
        let caller_frames = self.scopes.split_off(1);
        self.scopes.append(&mut c.frames); // drains c.frames into the active stack
        std::mem::swap(&mut self.signal, &mut c.signal);
        std::mem::swap(&mut self.pending_throw, &mut c.pending_throw);
        std::mem::swap(&mut self.error, &mut c.error);
        // `c` now carries the caller's saved signal/throw/error; hand it the
        // caller's frames so the reverse call restores them.
        c.frames = caller_frames;
        c
    }
}

/// Build a suspended generator over `body`, run in the already-bound call frame
/// `frame` (its parameters/captures set by the caller). Nothing runs until the
/// first resume.
fn make_generator(body: Chunk, frame: Scope) -> Value {
    let id = with_host(|h| {
        let id = h.generators.len() as u32;
        let function = match h.trace_frame_parts(&frame) {
            (Some((class, _)), function) => format!("{class}::{function}"),
            (None, function) => function,
        };
        h.generators.push(GenCell {
            function,
            coro: None,
            yielder: std::ptr::null(),
            ctx: GenContext {
                frames: vec![frame],
                ..GenContext::default()
            },
            cur_key: Value::Undef,
            cur_val: Value::Undef,
            auto_key: 0,
            ret: Value::Undef,
            started: false,
            at_first_yield: false,
            done: false,
            inject: None,
            destroying: false,
        });
        id
    });
    let coro = GenCoro::new(move |yielder: &GenYielder, _first: Value| {
        // Same thread → publish the yielder so `yield` (deep in the body's VM) can
        // reach it. Valid for the whole body.
        with_host(|h| h.generators[id as usize].yielder = yielder as *const _);
        let r = run_chunk_on(body);
        // A `return v;` in the body leaves a Return signal; capture it as the
        // generator's completion value (`getReturn`).
        let ret = with_host(|h| match h.signal.take() {
            Some(Signal::Return(v)) => v,
            _ => Value::Undef,
        });
        r.map(|_| ret)
    });
    with_host(|h| h.generators[id as usize].coro = Some(coro));
    with_host(|h| {
        h.objs.push(PhpObj::Generator { id });
        Value::Obj((h.objs.len() - 1) as u32)
    })
}

/// How a `yield` computes its key.
enum YieldKey {
    /// `yield $v` — take (and advance) the next auto-increment integer key.
    Auto,
    /// `yield $k => $v` — an explicit key (an int at/above the counter advances it,
    /// matching array-append semantics).
    Explicit(Value),
    /// `yield from` passthrough — re-emit the delegate's key without touching the
    /// outer generator's auto-key counter.
    Passthrough(Value),
}

/// Suspend the running generator, publishing `(key, val)` to the resumer. Returns
/// the value the next `->send($x)` supplies (`Undef` for `->next()`), or unwinds a
/// `->throw()` injected at this point.
fn gen_yield(key: YieldKey, val: Value) -> Result<Value, String> {
    let id = match CUR_GEN.with(|c| c.get()) {
        Some(id) => id,
        None => return Err("cannot yield outside a generator".to_string()),
    };
    if with_host(|h| h.generators[id as usize].destroying) {
        return Err(crate::builtins::throws_bare(
            "Error",
            "Cannot yield from finally in a force-closed generator",
        ));
    }
    let yp = with_host(|h| {
        let g = &mut h.generators[id as usize];
        let k = match key {
            YieldKey::Auto => {
                let k = g.auto_key;
                g.auto_key += 1;
                Value::int(k)
            }
            YieldKey::Explicit(Value::Int(i)) => {
                if i >= g.auto_key {
                    g.auto_key = i + 1;
                }
                Value::int(i)
            }
            YieldKey::Explicit(other) => other,
            YieldKey::Passthrough(k) => k,
        };
        g.cur_key = k;
        g.cur_val = val;
        g.yielder
    });
    // SAFETY: same-thread coroutine; the yielder lives for the whole body and we
    // only reach here from inside that live body.
    let yielder = unsafe { &*yp };
    let sent = yielder.suspend(());
    match with_host(|h| h.generators[id as usize].inject.take()) {
        // Re-raise the injected exception at the suspension point so an enclosing
        // try/catch in the body handles it (or it unwinds the body).
        Some(GenInject::Throw(e)) => {
            set_pending_throw(e);
            return Err("__generator_throw__".to_string());
        }
        // Destroyed while parked here: leave the body as a `return;` would.
        Some(GenInject::Destroy) => {
            set_return(Value::Undef);
            return Err(GEN_DESTROY.to_string());
        }
        None => {}
    }
    Ok(sent)
}

/// `yield $v` — suspend with an auto-incremented integer key.
pub fn yield_value(val: Value) -> Result<Value, String> {
    gen_yield(YieldKey::Auto, val)
}

/// `yield $k => $v` — suspend with an explicit key.
pub fn yield_kv(key: Value, val: Value) -> Result<Value, String> {
    gen_yield(YieldKey::Explicit(key), val)
}

/// `yield from $src` — the public entry point (see `gen_yield_from`).
pub fn yield_from(src: Value) -> Result<Value, String> {
    gen_yield_from(src)
}

/// `yield from $src` — delegate to an array, a Generator, or any Traversable,
/// re-yielding each key/value from the enclosing generator. Evaluates to the
/// delegate's `return` value (null for an array or a returnless generator).
fn gen_yield_from(src: Value) -> Result<Value, String> {
    // A sub-generator is driven lazily (preserving side-effect order and passing
    // sent values through). Anything else is normalized to an array and replayed.
    if with_host(|h| h.is_generator_val(&src)) {
        gen_ensure_init(&src)?;
        loop {
            if !gen_valid(&src)? {
                break;
            }
            let k = with_host(|h| {
                h.generators[h.gen_id(&src).unwrap() as usize]
                    .cur_key
                    .clone()
            });
            let v = with_host(|h| {
                h.generators[h.gen_id(&src).unwrap() as usize]
                    .cur_val
                    .clone()
            });
            let sent = gen_yield(YieldKey::Passthrough(k), v)?;
            gen_send_raw(&src, sent)?;
        }
        return Ok(with_host(|h| {
            h.generators[h.gen_id(&src).unwrap() as usize].ret.clone()
        }));
    }
    let arr = foreach_prep(src)?;
    let pairs: Vec<(Value, Value)> = with_host(|h| match h.as_array(&arr) {
        Some(PhpObj::Array { entries, .. }) => entries
            .iter()
            .map(|(k, v)| (k.to_value(), v.clone()))
            .collect(),
        _ => Vec::new(),
    });
    for (k, v) in pairs {
        gen_yield(YieldKey::Passthrough(k), v)?;
    }
    Ok(Value::Undef)
}

/// Resume a generator until its next `yield` or its body returns. The coroutine is
/// taken out (so the body re-enters `with_host` freely) and the volatile context
/// is swapped so the caller's frames/signal survive the switch.
fn gen_resume(id: u32, send: Value) -> Result<(), String> {
    if with_host(|h| h.generators[id as usize].done) {
        return Ok(());
    }
    let mut coro = match with_host(|h| h.generators[id as usize].coro.take()) {
        Some(c) => c,
        None => return Err("cannot resume an already-running generator".to_string()),
    };
    with_host(|h| {
        let g = &mut h.generators[id as usize];
        if g.started {
            g.at_first_yield = false;
        }
        g.started = true;
    });
    let gen_ctx = with_host(|h| std::mem::take(&mut h.generators[id as usize].ctx));
    let caller_ctx = with_host(|h| h.install_gen_ctx(gen_ctx));
    let prev = CUR_GEN.with(|c| c.replace(Some(id)));

    let out = coro.resume(send); // no host borrow held; body drives its own VM

    CUR_GEN.with(|c| c.set(prev));
    // An uncaught exception in the body leaves a pending throw in the generator's
    // (currently installed) context. Take it out before the context is swapped
    // away so it can be re-raised in the resumer's context below.
    let body_throw = with_host(|h| h.pending_throw.take());
    let gen_ctx = with_host(|h| h.install_gen_ctx(caller_ctx));
    with_host(|h| {
        h.generators[id as usize].ctx = gen_ctx;
        h.generators[id as usize].coro = Some(coro);
    });

    let result = match out {
        corosensei::CoroutineResult::Yield(()) => Ok(()),
        corosensei::CoroutineResult::Return(r) => {
            with_host(|h| {
                let g = &mut h.generators[id as usize];
                g.done = true;
                g.cur_key = Value::Undef;
                g.cur_val = Value::Undef;
            });
            match r {
                Ok(v) => {
                    with_host(|h| h.generators[id as usize].ret = v);
                    Ok(())
                }
                Err(e) => Err(e),
            }
        }
    };
    // Re-raise an uncaught body exception in the resumer's context.
    if let Some(e) = body_throw {
        set_pending_throw(e);
        return Err("__generator_throw__".to_string());
    }
    result
}

/// `zend_generator_ensure_initialized`: prime an unstarted generator to its
/// first `yield` (implicit on the first `current`/`valid`/`key`/`next`). A
/// no-op once started.
pub fn gen_ensure_init(gen: &Value) -> Result<(), String> {
    let id = with_host(|h| h.gen_id(gen)).ok_or("not a generator")?;
    if !with_host(|h| h.generators[id as usize].started) {
        gen_resume(id, Value::Undef)?;
        with_host(|h| h.generators[id as usize].at_first_yield = true);
    }
    Ok(())
}

/// `->rewind()` (`zend_generator_rewind`): prime the generator, and refuse one
/// already resumed past its first `yield` with the reference's `Exception`.
pub fn gen_rewind(gen: &Value) -> Result<(), String> {
    gen_ensure_init(gen)?;
    if with_host(|h| h.pending_throw.is_some()) {
        return Ok(());
    }
    let id = with_host(|h| h.gen_id(gen)).ok_or("not a generator")?;
    if !with_host(|h| h.generators[id as usize].at_first_yield) {
        let exc = new_object(
            "Exception",
            vec![Value::str("Cannot rewind a generator that was already run")],
        )?;
        set_pending_throw(exc);
    }
    Ok(())
}

/// A `foreach` (or `iterator_to_array`) over a generator: `zend_generator_get_iterator`
/// refuses a finished one, then the walk rewinds it.
pub fn gen_foreach_rewind(gen: &Value) -> Result<(), String> {
    let id = with_host(|h| h.gen_id(gen)).ok_or("not a generator")?;
    if with_host(|h| h.generators[id as usize].done) {
        let exc = new_object(
            "Exception",
            vec![Value::str("Cannot traverse an already closed generator")],
        )?;
        set_pending_throw(exc);
        return Ok(());
    }
    gen_rewind(gen)
}

/// `->valid()` — whether the generator has not yet finished (priming it first).
pub fn gen_valid(gen: &Value) -> Result<bool, String> {
    gen_ensure_init(gen)?;
    let id = with_host(|h| h.gen_id(gen)).ok_or("not a generator")?;
    Ok(with_host(|h| !h.generators[id as usize].done))
}

/// `->current()` — the current yielded value (priming first). Null once finished.
pub fn gen_current(gen: &Value) -> Result<Value, String> {
    gen_ensure_init(gen)?;
    let id = with_host(|h| h.gen_id(gen)).ok_or("not a generator")?;
    Ok(with_host(|h| h.generators[id as usize].cur_val.clone()))
}

/// `->key()` — the current yielded key (priming first). Null once finished.
pub fn gen_key(gen: &Value) -> Result<Value, String> {
    gen_ensure_init(gen)?;
    let id = with_host(|h| h.gen_id(gen)).ok_or("not a generator")?;
    Ok(with_host(|h| h.generators[id as usize].cur_key.clone()))
}

/// `->next()` — advance to the next yield (priming first, then resuming with null).
pub fn gen_next(gen: &Value) -> Result<Value, String> {
    gen_ensure_init(gen)?;
    let id = with_host(|h| h.gen_id(gen)).ok_or("not a generator")?;
    if !with_host(|h| h.generators[id as usize].done) {
        gen_resume(id, Value::Undef)?;
    }
    Ok(Value::Undef)
}

/// Resume `gen` with `sent` without the unstarted auto-prime — the internal step
/// used to pass a sent value down through `yield from`.
fn gen_send_raw(gen: &Value, sent: Value) -> Result<(), String> {
    let id = with_host(|h| h.gen_id(gen)).ok_or("not a generator")?;
    if !with_host(|h| h.generators[id as usize].done) {
        gen_resume(id, sent)?;
    }
    Ok(())
}

/// `->send($v)` — resume the generator, making the paused `yield` evaluate to `$v`,
/// and return the next yielded value (null once finished). On an unstarted
/// generator PHP first primes to the first yield, then sends.
pub fn gen_send(gen: &Value, sent: Value) -> Result<Value, String> {
    let id = with_host(|h| h.gen_id(gen)).ok_or("not a generator")?;
    if !with_host(|h| h.generators[id as usize].started) {
        gen_resume(id, Value::Undef)?;
    }
    if !with_host(|h| h.generators[id as usize].done) {
        gen_resume(id, sent)?;
    }
    Ok(with_host(|h| h.generators[id as usize].cur_val.clone()))
}

/// `->throw($e)` — raise `$e` at the current suspension point (running an enclosing
/// try/catch/finally in the body), then return the next yielded value.
pub fn gen_throw(gen: &Value, e: Value) -> Result<Value, String> {
    let id = with_host(|h| h.gen_id(gen)).ok_or("not a generator")?;
    gen_ensure_init(gen)?;
    if with_host(|h| h.generators[id as usize].done) {
        // Throw into a finished generator: it propagates straight to the caller.
        set_pending_throw(e);
        return Ok(Value::Undef);
    }
    with_host(|h| h.generators[id as usize].inject = Some(GenInject::Throw(e)));
    gen_resume(id, Value::Undef)?;
    Ok(with_host(|h| h.generators[id as usize].cur_val.clone()))
}

/// Request shutdown: destroy every generator still suspended, in the order they
/// were created, so the `finally` blocks around each one's parked `yield` run —
/// after an `exit` and after an uncaught exception's fatal as well, which is
/// when the reference frees them.
///
/// The reference first frees the global variables that alone hold an object,
/// newest first, and only then the rest in creation order. Globals held in
/// fusevm frame slots are gone by now, so that first pass cannot be
/// reproduced: two generators parked in two globals finish in creation order
/// here. An `exit` status already set is parked across the destruction, or
/// each body would see the run unwinding and skip its `finally`; one that a
/// `finally` sets itself wins.
pub fn shutdown_generators() {
    let exit = with_host(|h| h.pending_exit.take());
    let count = with_host(|h| h.generators.len());
    for id in 0..count as u32 {
        if gen_destroy(id).is_err() || unwinding() {
            break;
        }
    }
    with_host(|h| {
        if h.pending_exit.is_none() {
            h.pending_exit = exit;
        }
    });
    crate::stdlib::output::end_all();
}

/// [`ops::GEN_MARK`]: the id the next generator created will get.
pub fn gen_mark() -> i64 {
    with_host(|h| h.generators.len() as i64)
}

/// Destroy a generator: one suspended at a `yield` is resumed with a forced
/// `return`, so the `finally` blocks enclosing that `yield` run (an exception
/// one throws reaches the destroyer). An unstarted or finished one has nothing
/// to run. Either way it is finished afterwards.
fn gen_destroy(id: u32) -> Result<(), String> {
    let live = with_host(|h| {
        let g = &h.generators[id as usize];
        g.started && !g.done && g.coro.is_some()
    });
    let r = if live {
        with_host(|h| {
            let g = &mut h.generators[id as usize];
            g.destroying = true;
            g.inject = Some(GenInject::Destroy);
        });
        gen_resume(id, Value::Undef)
    } else {
        Ok(())
    };
    with_host(|h| {
        let g = &mut h.generators[id as usize];
        g.done = true;
        g.cur_key = Value::Undef;
        g.cur_val = Value::Undef;
    });
    r
}

/// [`ops::GEN_RELEASE`]: destroy `v` if it is a suspended generator that
/// nothing reachable holds any more.
///
/// Only a generator created at or after `mark` qualifies: one the `foreach`
/// subject expression itself produced. A local of a frame that is still
/// running can live in a fusevm frame slot this scan cannot see, but no such
/// local can have received a generator its callee created and returned —
/// that is what makes the scan below sufficient for a fresh one, where it
/// would not be for `foreach ($g as …)`.
///
/// phplang keeps no reference counts, so "nothing holds it" is answered by
/// looking: every frame's variables (the running ones and those parked in
/// other generators), reference cells, static and constant values, and every
/// array element, property and closure capture on the heap. A handle found
/// anywhere keeps the generator alive — a stale one in a dead array included,
/// which errs toward NOT running a `finally` early.
pub fn gen_release(v: &Value, mark: i64) -> Result<(), String> {
    let Some((id, handle)) = with_host(|h| match v {
        Value::Obj(o) => h.gen_id(v).map(|id| (id, *o)),
        _ => None,
    }) else {
        return Ok(());
    };
    let pending = with_host(|h| {
        let g = &h.generators[id as usize];
        i64::from(id) >= mark && g.started && !g.done
    });
    if !pending || with_host(|h| h.handle_held(handle)) {
        return Ok(());
    }
    gen_destroy(id)
}

impl PhpHost {
    /// Whether any variable, reference cell, static, constant, array element,
    /// property or closure binding holds the object handle `handle`.
    fn handle_held(&self, handle: u32) -> bool {
        let is = |v: &Value| matches!(v, Value::Obj(o) if *o == handle);
        let frame_holds = |s: &Scope| {
            s.vars
                .slots
                .iter()
                .any(|slot| matches!(slot, Slot::Val(v) if is(v)))
        };
        self.scopes.iter().any(frame_holds)
            || self
                .generators
                .iter()
                .any(|g| g.ctx.frames.iter().any(frame_holds))
            || self.ref_cells.iter().any(is)
            || self.static_props.values().any(is)
            || self.constants.values().any(is)
            || self.objs.iter().any(|o| match o {
                PhpObj::Array { entries, .. } => entries.values().any(is),
                PhpObj::Object { props, .. } => props.values().any(is),
                PhpObj::Closure {
                    captured,
                    bound_this,
                    ..
                } => captured.iter().any(|(_, v)| is(v)) || bound_this.as_ref().is_some_and(is),
                _ => false,
            })
    }
}

/// `->getReturn()` — the value the body `return`ed.
///
/// Asking BEFORE the body has returned is an error rather than a null: the
/// reference cannot distinguish "returned nothing" from "has not returned yet"
/// by value, so it refuses the question instead of answering it wrongly. A
/// generator that ran to completion without a `return` answers null.
pub fn gen_get_return(gen: &Value) -> Result<Value, String> {
    let id = with_host(|h| h.gen_id(gen)).ok_or("not a generator")?;
    let (done, ret) = with_host(|h| {
        let g = &h.generators[id as usize];
        (g.done, g.ret.clone())
    });
    if !done {
        // Raised through the internal-throw path so the trace carries the
        // `Generator->getReturn()` frame the reference prints for it.
        return throw_from_internal(
            "Generator->getReturn",
            &[],
            "Exception",
            "Cannot get return value of a generator that hasn't returned",
        );
    }
    Ok(ret)
}

/// Dispatch a `$gen->method(...)` call on a Generator object.
pub fn call_generator_method(gen: &Value, method: &str, args: Vec<Value>) -> Result<Value, String> {
    let mut args = args;
    match method.to_ascii_lowercase().as_str() {
        "current" => gen_current(gen),
        "key" => gen_key(gen),
        "next" => gen_next(gen),
        "valid" => gen_valid(gen).map(Value::bool),
        "rewind" => gen_rewind(gen).map(|_| Value::Undef),
        "send" => gen_send(gen, args.into_iter().next().unwrap_or(Value::Undef)),
        "throw" => gen_throw(gen, args.into_iter().next().unwrap_or(Value::Undef)),
        "getreturn" => gen_get_return(gen),
        other => {
            let _ = args.pop();
            Err(crate::builtins::throws_bare(
                "Error",
                format!("Call to undefined method Generator::{other}()"),
            ))
        }
    }
}

// ── execution ─────────────────────────────────────────────────────────────

thread_local! {
    static DEBUG_MODE: std::cell::Cell<bool> = const { std::cell::Cell::new(false) };

    /// Recycled VMs for [`run_chunk_on`].
    ///
    /// Every PHP function call, method call, closure invocation, constant
    /// initializer and `eval` runs its own chunk on its own VM. Building one
    /// meant `VM::new` plus `builtins::install`, which is 120 `register_builtin`
    /// calls each growing the table — per CALL. `VM::reset` keeps the builtin
    /// table (and the allocations behind the stack, frames and globals), so a
    /// recycled VM starts with all of that already in place.
    ///
    /// A stack, not a single slot, because calls nest: a recursive PHP function
    /// holds its VM for the whole call, and a suspended generator holds its VM
    /// on the coroutine stack, so neither is in the pool to be handed out twice.
    static VM_POOL: RefCell<fusevm::VMPool> = RefCell::new(fusevm::VMPool::new());
}

/// Enable/disable DAP debug execution. When on, `run_chunk_on` installs the
/// extension-handler seam and skips the tracing JIT (which would compile hot
/// loops and step over the `DBG_LINE` markers the debugger relies on).
///
/// Pooled VMs carry both of those settings across a `reset`, so the pool is
/// dropped here: a VM built under one mode must never be handed to the other.
pub fn set_debug_mode(on: bool) {
    DEBUG_MODE.with(|d| d.set(on));
    VM_POOL.with(|p| *p.borrow_mut() = fusevm::VMPool::new());
}

/// Register every phplang builtin + the strict numeric hook on a VM, then run it.
fn run_chunk_on(chunk: Chunk) -> Result<Value, String> {
    run_chunk_capturing(chunk, false)
}

/// [`run_chunk_on`], keeping the base frame's fusevm slots on the host when
/// `capture` is set — the main chunk's promoted globals, which request shutdown
/// still has to see after the VM is recycled.
fn run_chunk_capturing(chunk: Chunk, capture: bool) -> Result<Value, String> {
    // The hook warns (`A non-numeric value encountered`), so it needs the line
    // of the operator that delegated to it. Native arithmetic ops run no
    // builtin, so nothing else sets the warn site — hand the hook this chunk's
    // line table and let it index the op position the VM reports. Capturing it
    // per VM rather than in a thread-local keeps it correct when a generator
    // suspends mid-chunk, and the clone is dwarfed by the `Chunk` clone every
    // call already pays.
    let lines: std::sync::Arc<[u32]> = chunk.lines.as_slice().into();
    // A recycled VM already has the builtin table; only a fresh one is
    // installed onto. `VM::reset` keeps the table, the tracing-JIT flag and the
    // extension handler, which is exactly the state `install` and the mode
    // branch below would rebuild.
    let (mut vm, fresh) = VM_POOL.with(|p| {
        let mut pool = p.borrow_mut();
        let fresh = pool.is_empty();
        (pool.acquire(chunk), fresh)
    });
    if fresh {
        crate::builtins::install(&mut vm);
        if DEBUG_MODE.with(|d| d.get()) {
            vm.set_extension_handler(Box::new(|vm, id, _| {
                crate::dap::on_ext(vm, id);
            }));
        } else {
            vm.enable_tracing_jit();
        }
    }
    // Re-set every run: the hook closes over THIS chunk's line table.
    vm.set_sited_numeric_hook(std::sync::Arc::new(move |call| {
        crate::builtins::numeric_hook_sited(call, &lines)
    }));
    let outcome = vm.run();
    let err = with_host(|h| h.take_error());
    let result = match (&err, &outcome) {
        (Some(_), _) => None,
        (None, VMResult::Ok(v)) => Some(Ok(v.clone())),
        (None, VMResult::Halted) => Some(Ok(vm.stack.last().cloned().unwrap_or(Value::Undef))),
        // A builtin raising a PHP exception halts its chunk cleanly, but the
        // numeric hook has no VM handle to do that with — its only way out is
        // an error return. When it leaves an exception pending, that exception
        // is the real signal, so report the stop as a clean halt: every caller
        // (`run_body`, `run_main`, the call dispatcher) already checks for a
        // pending throw on this path, and only that path routes it to `catch`.
        (None, VMResult::Error(_)) if unwinding() => Some(Ok(Value::Undef)),
        (None, VMResult::Error(e)) => Some(Err(e.clone())),
    };
    if capture {
        let slots = vm
            .frames
            .first()
            .map(|f| f.slots.clone())
            .unwrap_or_default();
        with_host(|h| h.main_frame_slots = slots);
    }
    // Recycled only after the last read of `vm`. A generator that suspended
    // never gets here — its VM stays on the coroutine stack, which is what
    // keeps the pool from handing the same VM out twice.
    VM_POOL.with(|p| p.borrow_mut().release(vm));
    match (err, result) {
        (Some(e), _) => Err(e),
        (None, Some(r)) => r,
        (None, None) => unreachable!("result is set whenever there is no host error"),
    }
}

/// Run the top-level program chunk.
pub fn run_main(chunk: Chunk) -> Result<Value, String> {
    let r = run_chunk_capturing(chunk, true);
    // A diagnostic raised by the program's last native op has had no builtin
    // boundary to reach its handler at yet.
    drain_error_handlers();
    // A top-level `return` just ends the program; clear any leftover signal.
    with_host(|h| h.signal.take());
    // An exception that reached the top uncaught is a fatal error, displayed in
    // the PHP CLI's shape. `write_out` puts it where the reference puts it — on
    // stdout, inside any open output buffer — and the returned string is the
    // stderr log copy, which the CLI wrapper reports without re-displaying.
    if let Some(mut exc) = with_host(|h| h.pending_throw.take()) {
        // `set_exception_handler`: the handler takes the exception instead of
        // the fatal, and the run then ends normally. One it throws itself is
        // what goes uncaught. `php -r` never calls it — the reference runs that
        // code through `zend_eval_string`, which reports the exception directly.
        if let Some(handler) = with_host(|h| h.user_exception_handler()) {
            with_host(|h| {
                if let Some(g) = h.scopes.first_mut() {
                    g.internal = true;
                }
            });
            let _ = call_value(handler, vec![exc.clone()]);
            with_host(|h| h.signal.take());
            match with_host(|h| h.pending_throw.take()) {
                Some(next) => exc = next,
                None => {
                    return Ok(Value::Undef);
                }
            }
        }
        return Err(report_uncaught(exc));
    }
    r
}

/// Port of `Exception::__toString` (`Zend/zend_exceptions.c`), shared by
/// `Exception` and `Error` through the prelude's `__phplang_throwable_string`.
///
/// Walks the `previous` chain from `this` outward-in, rendering each throwable
/// as `Class: message in file:line\nStack trace:\n<trace>` (no `: message`
/// when the message is empty) and appending what was rendered so far after
/// `\n\nNext `, so the innermost exception reads first. The trace is the
/// object's own `getTraceAsString()`, `#0 {main}\n` when that is empty. A
/// user function's argument-type refusal (exactly `TypeError` or
/// `ArgumentCountError`) is extended with ` and defined`, because the file and
/// line that follow are the declaration's.
pub fn throwable_string(this: &Value) -> Result<Value, String> {
    let mut out = String::new();
    let mut exc = this.clone();
    while let Some(class) = with_host(|h| {
        h.object_class(&exc)
            // Throwable: phplang roots it at the two prelude classes.
            .filter(|c| h.class_is_a_pub(c, "Exception") || h.class_is_a_pub(c, "Error"))
    }) {
        let (message, file, line) = with_host(|h| {
            (
                h.to_str(&h.prop_get(&exc, "message")),
                h.to_str(&h.prop_get(&exc, "file")),
                h.prop_get(&exc, "line").to_int(),
            )
        });
        let trace = call_method(&class, "getTraceAsString", Some(exc.clone()), Vec::new())?;
        let trace = match trace {
            Value::Str(s) if !s.is_empty() => s.to_string(),
            _ => "#0 {main}\n".to_string(),
        };
        let message = if (class.eq_ignore_ascii_case("TypeError")
            || class.eq_ignore_ascii_case("ArgumentCountError"))
            && message.contains(", called in ")
        {
            format!("{message} and defined")
        } else {
            message
        };
        let name = display_class(&class).to_string();
        let next = if out.is_empty() { "" } else { "\n\nNext " };
        out = if message.is_empty() {
            format!("{name} in {file}:{line}\nStack trace:\n{trace}{next}{out}")
        } else {
            format!("{name}: {message} in {file}:{line}\nStack trace:\n{trace}{next}{out}")
        };
        exc = with_host(|h| h.prop_get(&exc, "previous"));
    }
    Ok(Value::str(out))
}

/// Display an exception nothing caught as the reference's fatal, on stdout
/// (inside any open output buffer), and answer the stderr log copy, which the
/// CLI wrapper reports without re-displaying.
fn report_uncaught(exc: Value) -> String {
    {
        // An uncaught `ParseError` — a syntax error in `include`d or `eval()`'d
        // code — is reported the way a syntax error in the script itself is:
        // `Parse error: <message> in <file> on line <n>`, with no trace.
        let parse = with_host(|h| {
            h.object_class(&exc)
                .is_some_and(|c| c.eq_ignore_ascii_case("ParseError"))
                .then(|| {
                    format!(
                        "{} in {} on line {}",
                        h.to_str(&h.prop_get(&exc, "message")),
                        h.to_str(&h.prop_get(&exc, "file")),
                        h.prop_get(&exc, "line").to_int()
                    )
                })
        });
        if let Some(body) = parse {
            with_host(|h| h.fatal("Parse error", &body));
            return format!("Parse error:  {body}");
        }
        // `zend_exception_error`: the text is the exception's own `__toString()`
        // — a user override included — and an exception thrown BY that
        // `__toString` is what gets reported instead.
        let class = with_host(|h| h.object_class(&exc)).unwrap_or_else(|| "Exception".to_string());
        // The engine makes the call, so a trace names its site
        // `[internal function]`.
        with_host(|h| {
            if let Some(g) = h.scopes.first_mut() {
                g.internal = true;
            }
        });
        let r = call_method(&class, "__toString", Some(exc.clone()), Vec::new());
        if let Some(inner) = with_host(|h| h.pending_throw.take()) {
            return report_uncaught(inner);
        }
        let str = r.map(|v| with_host(|h| h.to_str(&v))).unwrap_or_default();
        let body = with_host(|h| {
            let file = h.to_str(&h.prop_get(&exc, "file"));
            let line = h.prop_get(&exc, "line").to_int();
            format!("Uncaught {str}\n  thrown in {file} on line {line}")
        });
        with_host(|h| h.fatal("Fatal error", &body));
        format!("Fatal error:  {body}")
    }
}

/// Invoke a user function (or fall through to the builtin library) by name.
/// Pushes a fresh scope, binds positional parameters, runs the body chunk, and
/// returns the `return` value (or null if the body fell off the end).
/// PHP's string conversion with `__toString` honoured.
///
/// [`PhpHost::to_str`] takes `&self` and so cannot run PHP code; an object that
/// defines `__toString` therefore has to be converted *outside* the host borrow,
/// which is what this free function is for. It must not be called from inside a
/// `with_host` closure — invoking the method re-enters the host and the
/// `RefCell` would be borrowed twice.
///
/// Every other value takes the ordinary cast, and the probe for the method is a
/// single class lookup, so the non-object path costs one `with_host` round trip
/// and nothing else.
pub fn to_str_ext(v: &Value) -> String {
    let class = with_host(|h| {
        h.object_class(v)
            .filter(|c| h.class_has_method(c, "__tostring"))
    });
    let Some(class) = class else {
        // An object with no `__toString` has no string form: the reference
        // throws `Error` (`zend_std_cast_object_tostring`) rather than
        // yielding `Array`.
        if let Some(class) = with_host(|h| h.instance_class(v)) {
            let msg = format!("Object of class {class} could not be converted to string");
            if let Ok(exc) = new_object("Error", vec![Value::str(msg)]) {
                set_pending_throw(exc);
            }
            return String::new();
        }
        return with_host(|h| h.to_str_diag(v));
    };
    match call_method(&class, "__toString", Some(v.clone()), Vec::new()) {
        Ok(r) => with_host(|h| h.to_str(&r)),
        // A throwing `__toString` leaves the exception pending for the caller's
        // dispatcher; the conversion itself yields the empty string.
        Err(_) => String::new(),
    }
}

pub fn call_function(name: &str, args: Vec<Value>) -> Result<Value, String> {
    call_function_dispatched(name, args, Dispatch::Compiled)
}

/// Whether a call was written where the COMPILER could see it, or reached the
/// function through a value at run time.
///
/// The distinction exists for the handful of names the reference compiles to an
/// opcode (see `compiles_to_an_opcode`). The compiler can only specialise a call
/// it can see: dispatched through a callback, a `$f(…)`, or `call_user_func`,
/// the same name is an ordinary internal call and its argument error carries a
/// frame like any other.
///
/// ```text
/// $ php -r 'try { strlen([1]); }                catch (Throwable $e) { echo $e->getTraceAsString(); }'
/// #0 {main}
/// $ php -r 'try { call_user_func("strlen", [1]); } catch (Throwable $e) { echo $e->getTraceAsString(); }'
/// #0 Command line code(1): strlen(Array)
/// #1 {main}
/// ```
#[derive(Clone, Copy, PartialEq)]
pub enum Dispatch {
    /// Written as a call in the source.
    Compiled,
    /// Reached through a value: `$f(…)`, a callback, `call_user_func`.
    Indirect,
    /// Written as an UNQUALIFIED call inside a namespace that reached the global
    /// function. The reference resolves those at run time
    /// (`ZEND_INIT_NS_FCALL_BY_NAME`) and so never specialises them into an
    /// opcode, but the call is still the source's, under its strict mode.
    /// The compiler marks one by loading its name with a leading `\`.
    CompiledInNamespace,
}

fn call_function_dispatched(name: &str, args: Vec<Value>, how: Dispatch) -> Result<Value, String> {
    let how = if how == Dispatch::Compiled && name.starts_with('\\') {
        Dispatch::CompiledInNamespace
    } else {
        how
    };
    // A function name held in a string may carry the global-namespace `\`
    // (`zend_init_dynamic_call_string`, `zend_is_callable_check_func`).
    let name = name.strip_prefix('\\').unwrap_or(name);
    // Inline Rust FFI: the `rust { ... }` desugar emits `__rust_compile(b64,
    // line)`; compile + register the block's exported functions.
    if name == "__rust_compile" {
        let b64 = args.first().map(|v| v.to_str()).unwrap_or_default();
        return fusevm::ffi::compile_and_register(&b64).map(|_| Value::Undef);
    }
    // A `rust { ... }` block's exported functions are callable by bareword.
    // User-defined PHP functions still win (resolved below); the registry is
    // consulted before the PHP standard library so an exported name is
    // reachable, and the membership check keeps this off the hot path.
    let def = with_host(|h| h.functions.get(&name.to_ascii_lowercase()).cloned());
    if def.is_none() && fusevm::ffi::is_registered(name) {
        if let Some(r) = fusevm::ffi::try_call(name, &args) {
            return r;
        }
    }
    if let Some(def) = def {
        return invoke_with_locals(
            def.declared.as_deref().unwrap_or(name),
            Signature {
                params: &def.params,
                ret: def.ret.as_ref(),
                deprecated: def.deprecated.as_ref(),
            },
            def.chunk,
            Vec::new(),
            args,
            Vec::new(),
            def.is_generator,
            &def.locals,
        );
    }
    // A library function names itself in lower case in every message and trace
    // frame, however the call spelled it: `STRLEN([])` reports `strlen()`.
    // A function that does not exist is reported as the call spelled it —
    // `NoPe()`, `A\nope()` — since there is no declaration to name it. The
    // engine's own `__` helpers dispatch without being listed, so they are
    // left to the library's own refusal.
    if !name.starts_with("__") && !crate::stdlib::callable::dispatches(name) {
        // Frameless, as the library's own refusal is: nothing was entered.
        let message = format!("Call to undefined function {name}()");
        set_pending_throw(new_object("Error", vec![Value::str(message)])?);
        return Ok(Value::Undef);
    }
    // A string a library function builds by joining pieces that carry raw
    // bytes is stored canonically (see `crate::bytes`).
    match call_library_throwing(&crate::argsig::lower_name(name), args, how) {
        Ok(Value::Str(s)) if crate::bytes::has_raw(&s) => {
            Ok(Value::str(crate::bytes::canonical(s.as_str().to_string())))
        }
        other => other,
    }
}

/// Call a standard-library function, turning a tagged argument error into the
/// PHP exception it stands for (see [`crate::builtins::throws`]).
///
/// This wrapper is the caller rather than part of `call_library` itself because
/// it needs the argument list AFTER the call, to render the trace frame —
/// `call_library` borrows it, so the owner has to be the one that reacts.
/// Raise a tagged library error through the ordinary internal-throw path, so it
/// carries the same trace frame a failure inside the function would.
fn throw_from_internal_typed(name: &str, args: &[Value], e: String) -> Result<Value, String> {
    match crate::builtins::untag_throw(&e) {
        Some((class, message)) => throw_from_internal(name, args, class, message),
        None => Err(e),
    }
}

/// Whether the reference COMPILES this call to an opcode instead of a call, so
/// the argument error it raises has no frame of its own and the trace starts at
/// the caller.
///
/// The specialisation is arity-exact: `strlen($s)` becomes `ZEND_STRLEN`, but
/// `strlen($s, 1)` is an ordinary call whose `ArgumentCountError` IS framed, so
/// the count is part of the test. Measured for each name at both arities:
///
/// ```text
/// $ php -r 'try { strlen([1]); }   catch (Throwable $e) { echo $e->getTraceAsString(); }'
/// #0 {main}
/// $ php -r 'try { count([1], "x"); } catch (Throwable $e) { echo $e->getTraceAsString(); }'
/// #0 Command line code(1): count(Array, 'x')
/// #1 {main}
/// ```
///
/// The rest of PHP's special-cased names (`chr`, `ord`, `defined`, `in_array`,
/// `array_slice`, `sprintf`, `intval`) specialise only for a literal argument,
/// so a runtime call to one is compiled normally and frames its error; all seven
/// were measured framed and are deliberately absent.
fn compiles_to_an_opcode(name: &str, argc: usize) -> bool {
    match name {
        "strlen" | "count" | "sizeof" | "get_class" => argc == 1,
        "array_key_exists" | "call_user_func_array" => argc == 2,
        // `zend_compile_func_cuf`: `ZEND_INIT_USER_CALL` with the callee
        // checked by the opcode.
        "call_user_func" => argc >= 1,
        _ => false,
    }
}

/// Raise a tagged library error from the CALLER's frame: the exception is built
/// and left pending without pushing a frame for the library function, which is
/// what the reference produces for a call it compiled to an opcode.
fn throw_frameless_typed(e: &str) -> Result<Value, String> {
    match crate::builtins::untag_throw(e) {
        Some((class, message)) => {
            let exc = new_object(class, vec![Value::str(message.to_string())])?;
            set_pending_throw(exc);
            Ok(Value::Undef)
        }
        None => Err(e.to_string()),
    }
}

/// Whether a library function runs a PHP callback, and so occupies a frame of its
/// own in a trace captured from inside that callback.
///
/// Every name here was measured against the reference by throwing out of the
/// callback and reading the trace back. `call_user_func` and
/// `call_user_func_array` are deliberately ABSENT: the reference invokes their
/// callee from the caller's own frame, so neither appears in the trace and the
/// callee reports the call site rather than `[internal function]`.
///
/// The list is the set of callback-taking functions this engine implements —
/// every `host::call_value` reached from `builtins::call_library` — so a new one
/// has to be added here along with its dispatch arm.
/// The library functions that run a PHP callback, named as a slice so the
/// invariant `call_library_throwing` leans on can be TESTED: none of these
/// declares a parameter the argument parser converts in the slot, so the frame
/// they push may record its arguments as written (see
/// `crate::argtypes::converts_in_place`, and the test beside it).
pub(crate) const CALLS_BACK: &[&str] = &[
    "array_map",
    "array_filter",
    "array_reduce",
    "array_walk",
    "array_walk_recursive",
    "array_find",
    "array_find_key",
    "array_any",
    "array_all",
    "array_udiff",
    "array_uintersect",
    "array_diff_ukey",
    "array_intersect_ukey",
    "array_diff_uassoc",
    "array_intersect_uassoc",
    "array_udiff_assoc",
    "array_uintersect_assoc",
    "array_udiff_uassoc",
    "array_uintersect_uassoc",
    "ob_flush",
    "ob_clean",
    "ob_end_flush",
    "ob_end_clean",
    "ob_get_flush",
    "ob_get_clean",
    "usort",
    "uasort",
    "uksort",
    "preg_replace_callback",
    "preg_replace_callback_array",
    "iterator_apply",
    "iterator_to_array",
    "iterator_count",
];

fn calls_back(name: &str) -> bool {
    CALLS_BACK.contains(&name)
}

fn call_library_throwing(name: &str, args: Vec<Value>, how: Dispatch) -> Result<Value, String> {
    // PHP 8 refuses an argument whose type the parameter does not accept, before
    // the function runs. The check is by DECLARED TYPE rather than per function
    // (see `crate::argtypes`), and it goes through the same error path as any
    // other library failure so the refusal carries the call's trace frame.
    // A call the reference compiles to an opcode raises its argument error from
    // the CALLER's frame, because no call was ever made (see
    // `compiles_to_an_opcode`).
    let opcode = how == Dispatch::Compiled && compiles_to_an_opcode(name, args.len());
    // The COUNT is checked before the types, and a `callable` parameter is
    // judged in its own position among them — see `crate::argsig::check_args`.
    static NOCHECK: std::sync::OnceLock<bool> = std::sync::OnceLock::new();
    let nocheck = *NOCHECK.get_or_init(|| std::env::var_os("PHPLANG_NOCHECK").is_some());
    let checked = if nocheck {
        Ok(())
    } else {
        crate::argsig::check_args(
            name,
            &args,
            how != Dispatch::Indirect && with_host(|h| h.strict_types()),
        )
    };
    if let Err(e) = checked {
        return if opcode {
            throw_frameless_typed(&e)
        } else {
            // The frame renders the arguments the parser had already CONVERTED
            // when it stopped (see `crate::argtypes::trace_args`). An arity
            // refusal is raised before any argument is parsed, so that one
            // reports them exactly as written: `strtolower(5, 6)`.
            let arity = crate::builtins::untag_throw(&e)
                .is_some_and(|(class, _)| class == "ArgumentCountError");
            let targs = if arity {
                std::borrow::Cow::Borrowed(&args[..])
            } else {
                crate::argtypes::trace_args(name, &args)
            };
            throw_from_internal_typed(name, &targs, e)
        };
    }
    // A library function that runs a PHP callback IS a frame in the reference's
    // trace, and the callback's own frame reports `[internal function]` as its
    // call site. Pushed only for the functions that can call back, so the frame
    // stack — and everything that resolves a variable through it — is untouched
    // for the rest of the library.
    let framed = calls_back(name);
    if framed {
        with_host(|h| {
            let line = h.cur_frame_line();
            let argsarr = h.new_array();
            for a in &args {
                h.arr_push_auto(&argsarr, a.clone());
            }
            h.push_internal_frame(name, line, argsarr);
        });
    }
    let out = crate::builtins::call_library(name, &args);
    if framed {
        with_host(PhpHost::pop_internal_frame);
    }
    match out {
        Err(e) => {
            // Every frame below renders the arguments as the parser left them.
            let targs = crate::argtypes::trace_args(name, &args);
            // A frameless throw is raised from the caller's own frame: no scope
            // is pushed, so the trace starts where the call was written.
            if let Some((class, message)) = crate::builtins::untag_bare_throw(&e) {
                let exc = new_object(class, vec![Value::str(message.to_string())])?;
                set_pending_throw(exc);
                return Ok(Value::Undef);
            }
            // An engine-level fatal, not a Throwable: displayed with the same
            // trace a throw would carry but never routed through `catch`, and
            // the program stops.
            if let Some(message) = crate::builtins::untag_fatal(&e) {
                return Err(fatal_from_internal(name, &targs, message));
            }
            if let Some((class, code, message)) = crate::builtins::untag_throw_code(&e) {
                return throw_from_internal_args(
                    name,
                    &targs,
                    class,
                    vec![Value::str(message.to_string()), Value::int(code)],
                );
            }
            if opcode {
                return throw_frameless_typed(&e);
            }
            match crate::builtins::untag_throw(&e) {
                Some((class, message)) => throw_from_internal(name, &targs, class, message),
                None => Err(e),
            }
        }
        ok => ok,
    }
}

/// `call_function` with PHP 8.0 named arguments. User functions bind by parameter
/// name; a builtin (no name metadata) falls back to positional binding with the
/// named values appended in call order.
pub fn call_function_named(
    name: &str,
    args: Vec<Value>,
    named: Vec<(String, Value)>,
) -> Result<Value, String> {
    // A function name held in a string may carry the global-namespace `\`.
    let name = name.strip_prefix('\\').unwrap_or(name);
    let def = with_host(|h| h.functions.get(&name.to_ascii_lowercase()).cloned());
    if let Some(def) = def {
        return invoke_with_locals(
            def.declared.as_deref().unwrap_or(name),
            Signature {
                params: &def.params,
                ret: def.ret.as_ref(),
                deprecated: def.deprecated.as_ref(),
            },
            def.chunk,
            Vec::new(),
            args,
            named,
            def.is_generator,
            &def.locals,
        );
    }
    let lname = crate::argsig::lower_name(name);
    let name = lname.as_ref();
    // A builtin binds by name too — the table in `crate::argsig` is what says
    // which parameter a name means, which slot it fills, and what belongs in the
    // slots a name jumped over. Only a name the table does not describe (an FFI
    // export, `__cast_array`) still falls back to appending positionally.
    if crate::argsig::sig_of(name).is_some() {
        return match crate::argsig::bind_named(name, args, named) {
            // The two the reference refuses at SEND time carry no frame, so
            // they need no argument list either.
            Err(e) => match crate::builtins::untag_bare_throw(&e) {
                Some((class, message)) => {
                    let exc = new_object(class, vec![Value::str(message.to_string())])?;
                    set_pending_throw(exc);
                    Ok(Value::Undef)
                }
                None => Err(e),
            },
            Ok(bound) => {
                // A name a variadic could not place is the LAST refusal, after
                // the types: `sprintf([], x: 1)` reports the array in #1.
                let mut shown = bound.shown;
                let refusal = bound.refusal.or_else(|| {
                    bound.unplaced_name.then(|| {
                        // The parameters parsed ahead of the variadic have been
                        // converted by then, and the trace shows them so:
                        // `sprintf(1, x: 1)` prints `sprintf('1', x: 1)`.
                        let head = crate::argsig::zpp_head_len(name).min(bound.args.len());
                        let conv = crate::argtypes::trace_args(name, &bound.args[..head]);
                        for (slot, v) in shown.iter_mut().zip(conv.iter()) {
                            slot.1 = v.clone();
                        }
                        crate::argsig::refuse_unplaced(name, &bound.args)
                    })
                });
                match refusal {
                    Some(e) => throw_from_internal_named(name, &shown, e),
                    None => call_function(name, bound.args),
                }
            }
        };
    }
    let mut all = args;
    all.extend(named.into_iter().map(|(_, v)| v));
    call_function(name, all)
}

/// Raise a tagged refusal with a trace frame whose arguments are the ones the
/// call BOUND: every slot it reached, a slot it jumped over rendered `NULL`, and
/// a name a variadic could not place rendered `name: value` — which is how the
/// reference prints them.
///
/// ```text
/// $ php -r 'function f(){ count(mode: "ab"); } f();'
/// #0 Command line code(1): count(NULL, 'ab')
/// ```
fn throw_from_internal_named(
    func: &str,
    shown: &[(Option<String>, Value)],
    e: String,
) -> Result<Value, String> {
    let Some((class, message)) = crate::builtins::untag_throw(&e) else {
        return Err(e);
    };
    let (class, message) = (class.to_string(), message.to_string());
    with_host(|h| {
        let line = h.cur_frame_line();
        h.scopes.push(Scope {
            name: Some(func.to_string()),
            line,
            internal: true,
            ..Scope::default()
        });
        let argsarr = h.new_array();
        for (name, v) in shown {
            match name {
                Some(n) => h.arr_set_key(&argsarr, &Value::str(n.clone()), v.clone()),
                None => h.arr_push_auto(&argsarr, v.clone()),
            }
        }
        h.set_var("@args", argsarr);
    });
    let exc = new_object(&class, vec![Value::str(message)])?;
    with_host(|h| {
        h.scopes.pop();
    });
    set_pending_throw(exc);
    Ok(Value::Undef)
}

/// Run a user-defined function or closure body: push a call frame named `frame`,
/// pre-bind `pre` (a closure's captured `(name, value)` pairs, empty for a plain
/// function), bind `args` to `params` (variadic collection, then default chunks
/// for omitted parameters), run `body`, and return the `return` value (or null if
/// the body fell off the end). Default chunks run OUTSIDE the binding `with_host`
/// closure because `run_chunk_on` itself borrows the thread-local host.
#[allow(clippy::too_many_arguments)]
/// Apply each declared parameter type to the argument bound to it, returning the
/// values the callee should actually see.
///
/// Runs BEFORE any binding, because a `TypeError` here means the call never
/// happens — no scope is pushed and no default is evaluated. A parameter with no
/// type, or one whose type is not an enforced scalar, passes its argument through
/// untouched, which is what every parameter did before types were carried at all.
/// A frame name as it should READ in a diagnostic.
///
/// `resolve_method` keys classes by their lowercased name and hands that back, so
/// a frame arrives as `c::m` where PHP writes `C::m`. The declared spelling is
/// recovered here rather than at the call sites, which use the frame for scope
/// lookups that must keep matching case-insensitively.
fn display_frame(frame: &str) -> String {
    match frame.split_once("::") {
        Some((cls, rest)) => {
            let shown = with_host(|h| h.class_display_name(&cls.to_ascii_lowercase()));
            format!("{shown}::{rest}")
        }
        None => frame.to_string(),
    }
}

/// `Ok(None)` means a `TypeError` was raised and is pending: the call must not
/// happen, and the caller returns null while the dispatcher unwinds it.
type ArgCheck = Result<Option<(Vec<Value>, Vec<(String, Value)>)>, String>;

/// The call-shape errors PHP raises before a body runs: a named argument that
/// names nothing, one that collides with a positional, and a required parameter
/// left unfilled.
///
/// Separate from [`check_arg_types`], which returns early when no parameter is
/// typed — these apply to every call, typed or not. `Ok(false)` means an error
/// is pending and the caller must not run the body.
fn check_call_shape(
    frame: &str,
    params: &[Param],
    args: &[Value],
    named: &[(String, Value)],
) -> Result<bool, String> {
    let called_with: Vec<Value> = args.to_vec();
    let has_variadic = params.iter().any(|p| p.variadic);
    for (n, _) in named {
        match params.iter().position(|p| !p.variadic && p.name == *n) {
            // Naming a parameter a positional argument already filled is an
            // error, not a silent overwrite — PHP will not let one call bind the
            // same parameter twice.
            Some(i) if i < args.len() => {
                throw_from_internal(
                    frame,
                    &called_with,
                    "Error",
                    &format!("Named parameter ${n} overwrites previous argument"),
                )?;
                return Ok(false);
            }
            Some(_) => {}
            // A variadic collects names it cannot place; without one there is
            // nowhere for the name to go and dropping it was silent.
            None if !has_variadic => {
                throw_from_internal(
                    frame,
                    &called_with,
                    "Error",
                    &format!("Unknown named parameter ${n}"),
                )?;
                return Ok(false);
            }
            None => {}
        }
    }
    // A method of a prelude class stands in for an INTERNAL one, whose count
    // check is `zend_wrong_parameters_count_error`: it refuses too many
    // arguments as well as too few, and words both as `expects`.
    let required = params
        .iter()
        .filter(|p| !p.variadic && p.default.is_none())
        .count();
    if named.is_empty() && is_prelude_frame(frame) {
        let declared = params.iter().filter(|p| !p.variadic).count();
        let passed = args.len();
        if passed < required || (!has_variadic && passed > declared) {
            let (bound, n) = if required == declared && !has_variadic {
                ("exactly", required)
            } else if passed < required {
                ("at least", required)
            } else {
                ("at most", declared)
            };
            let s = if n == 1 { "" } else { "s" };
            throw_from_callee(
                frame,
                &called_with,
                "ArgumentCountError",
                &format!(
                    "{}() expects {bound} {n} argument{s}, {passed} given",
                    callee_shown(frame)
                ),
            )?;
            return Ok(false);
        }
    }
    // A parameter is filled positionally, filled by name, variadic, or has a
    // default. Anything else leaves the call short.
    let unfilled = params.iter().enumerate().find(|(i, p)| {
        !p.variadic
            && p.default.is_none()
            && *i >= args.len()
            && !named.iter().any(|(n, _)| *n == p.name)
    });
    let Some((i, p)) = unfilled else {
        return Ok(true);
    };
    let shown = callee_shown(frame);
    // A call that left a HOLE is reported by the parameter it left empty, not by
    // a count: the reference has no shortfall to report when the arguments
    // reached past it.
    //
    // ```text
    // $ php -r 'function f($a,$b,$c){} f(1);'       -> Too few arguments to function f(), 1 passed …
    // $ php -r 'function f($a,$b,$c){} f(1, c: 3);' -> f(): Argument #2 ($b) not passed
    // ```
    // …and only for a slot the call reached PAST. One left short at the end is
    // an ordinary shortfall, however it was filled:
    //
    // ```text
    // $ php -r 'function f($a,$b,$c){} f(1, b: 2);' -> Too few arguments to function f(), 2 passed …
    // ```
    let reached = params
        .iter()
        .enumerate()
        .filter(|(_, p)| named.iter().any(|(n, _)| *n == p.name))
        .map(|(j, _)| j + 1)
        .chain(std::iter::once(args.len()))
        .max()
        .unwrap_or(0);
    // The frame shows the arguments where they landed: a named one in its
    // parameter's slot, and NULL in each slot left empty before it —
    // `f(1, c: 3)` is traced as `f(1, NULL, 3)`.
    let placed: Vec<Value> = (0..reached)
        .map(|j| match args.get(j) {
            Some(v) => v.clone(),
            None => named
                .iter()
                .find(|(n, _)| *n == params[j].name)
                .map_or(Value::Undef, |(_, v)| v.clone()),
        })
        .collect();
    if i < reached {
        let argno = i + 1;
        throw_at_param(
            frame,
            &placed,
            "ArgumentCountError",
            &format!("{shown}(): Argument #{argno} (${}) not passed", p.name),
            p.line,
        )?;
        return Ok(false);
    }
    // "exactly" when every DECLARED parameter is required, "at least" when the
    // function also takes optional ones. A variadic tail is not a parameter for
    // this count — `function f($a, ...$r)` called with none is `exactly 1`.
    let declared = params.iter().filter(|p| !p.variadic).count();
    let bound = if required == declared {
        "exactly"
    } else {
        "at least"
    };
    let passed = args.len() + named.len();
    let (file, line) = with_host(|h| (h.current_file().to_string(), h.cur_frame_line()));
    throw_at_param(
        frame,
        &placed,
        "ArgumentCountError",
        &format!(
            "Too few arguments to function {shown}(), {passed} passed in {file} \
             on line {line} and {bound} {required} expected"
        ),
        p.line,
    )?;
    Ok(false)
}

fn check_arg_types(
    frame: &str,
    params: &[Param],
    args: Vec<Value>,
    named: Vec<(String, Value)>,
) -> ArgCheck {
    if params.iter().all(|p| p.ty.is_none()) {
        return Ok(Some((args, named)));
    }
    // The trace frame prints the arguments AS CALLED, so it is built from the
    // originals rather than from whatever survived coercion.
    let scope = TypeScope::of_frame(frame, None);
    let called_with = args.clone();
    let mut out = Vec::with_capacity(args.len());
    for (i, a) in args.into_iter().enumerate() {
        // Past the last declared parameter every remaining argument belongs to the
        // variadic one, and so is checked against ITS type.
        let p = params
            .get(i)
            .or_else(|| params.last().filter(|p| p.variadic));
        match coerce_arg(p, a, &scope)? {
            Ok(v) => out.push(v),
            Err(given) => {
                arg_type_error(frame, &called_with, p, i + 1, &given)?;
                return Ok(None);
            }
        }
    }
    let mut nout = Vec::with_capacity(named.len());
    for (n, v) in named {
        let pos = params.iter().position(|p| !p.variadic && p.name == n);
        let p = pos.map(|i| &params[i]);
        match coerce_arg(p, v, &scope)? {
            Ok(v) => nout.push((n, v)),
            Err(given) => {
                arg_type_error(frame, &called_with, p, pos.map_or(0, |i| i + 1), &given)?;
                return Ok(None);
            }
        }
    }
    Ok(Some((out, nout)))
}

/// One argument against one parameter's declared type. The inner `Result` is the
/// TYPE verdict — `Err(name)` carrying the type the value actually had — while the
/// outer one is reserved for a host failure.
#[allow(clippy::type_complexity)]
fn coerce_arg(
    p: Option<&Param>,
    v: Value,
    scope: &TypeScope,
) -> Result<Result<Value, String>, String> {
    let Some(p) = p else { return Ok(Ok(v)) };
    let Some(ty) = &p.ty else { return Ok(Ok(v)) };
    let ty = ty.clone();
    // A by-reference parameter is checked and coerced like any other, and the
    // converted value is written back THROUGH the reference before the body runs:
    // `function f(int &$x)` called with a `$v` holding `"5"` leaves `$v` as
    // `int(5)` even if the body never touches `$x`. An argument that arrives as a
    // reference cell is therefore rewritten in place, so the caller's variable and
    // the parameter still name one storage location.
    if p.by_ref {
        let line = p.line;
        let (slot, cur) = with_host(|h| {
            let slot = h.ref_slot_of_value(&v);
            let cur = match slot {
                Some(s) => h.ref_cell_value(s),
                None => v.clone(),
            };
            (slot, cur)
        });
        let saved = warn_line();
        set_warn_line(line);
        let r = verify_type(cur, &ty, scope);
        set_warn_line(saved);
        return Ok(with_host(|h| {
            r.map(|converted| match slot {
                Some(s) => {
                    h.ref_cell_set(s, converted);
                    // The HANDLE is what the binder needs: it aliases the
                    // parameter to the cell just rewritten.
                    v
                }
                // No cell: the caller writes the parameter's final value back
                // itself when the call returns, so converting the value is all
                // this side has to do.
                None => converted,
            })
        }));
    }
    // A lossy implicit conversion is reported against the PARAMETER's declaration,
    // not against the call — `f(int $x)` on line 2 called from line 9 names line 2
    // — so the diagnostic line is moved for the duration of the check and put back
    // after it, leaving the caller's line intact for anything the body warns about.
    let saved = warn_line();
    set_warn_line(p.line);
    let r = verify_type(v, &ty, scope);
    set_warn_line(saved);
    Ok(r)
}

/// Throw a refusal of a user function's arguments. The reference raises it
/// from the callee's receive for that parameter, so the exception's `line` is
/// where the parameter is DECLARED while its trace still shows the call:
///
/// ```text
/// $ php -r 'function f(
/// $a)
/// {}
/// f();'   -> Too few arguments to function f(), 0 passed in … on line 4 … in Command line code:2
/// ```
fn throw_at_param(
    frame: &str,
    called_with: &[Value],
    class: &str,
    msg: &str,
    decl_line: u32,
) -> Result<Value, String> {
    let r = throw_from_callee(frame, called_with, class, msg);
    // A method of a PHP-written prelude class stands in for an internal one,
    // which has no declaration to point at: the caller's line stays.
    if is_prelude_frame(frame) {
        return r;
    }
    with_host(|h| {
        if let Some(exc) = h.pending_throw.clone() {
            h.prop_set(&exc, "line", Value::int(decl_line as i64));
        }
    });
    r
}

/// Whether `frame` is a method of a PHP-written prelude class, which stands in
/// for an internal one.
fn is_prelude_frame(frame: &str) -> bool {
    frame
        .split_once("::")
        .is_some_and(|(c, _)| crate::prelude_type(&c.to_ascii_lowercase()).is_some())
}

/// Raise the `TypeError` for an argument that did not satisfy its declared type,
/// from a frame naming the callee — so the trace reads `#0 file(line): f('abc')`
/// exactly as it does when the reference rejects the same call.
fn arg_type_error(
    frame: &str,
    called_with: &[Value],
    p: Option<&Param>,
    pos: usize,
    given: &str,
) -> Result<Value, String> {
    let (name, ty) = match p {
        // A variadic parameter has no name in the message: PHP writes
        // `Argument #2 must be of type int`, with no `($xs)` after the number.
        Some(p) if p.variadic => (String::new(), p.ty.clone()),
        Some(p) => (format!(" (${})", p.name), p.ty.clone()),
        None => (String::new(), None),
    };
    let rendered = ty
        .map(|t| render_declared(&t, frame, None))
        .unwrap_or_default();
    let (file, line) = with_host(|h| (h.current_file().to_string(), h.cur_frame_line()));
    let shown = callee_shown(frame);
    // An internal function names no call site: it has no declaration either.
    let site = if is_prelude_frame(frame) {
        String::new()
    } else {
        format!(", called in {file} on line {line}")
    };
    let msg = &format!(
        "{shown}(): Argument #{pos}{name} must be of type {rendered}, {given} given{site}"
    );
    match p {
        Some(p) => throw_at_param(frame, called_with, "TypeError", msg, p.line),
        None => throw_from_callee(frame, called_with, "TypeError", msg),
    }
}

/// Apply a declared return type to the value a body produced. Like the argument
/// check, a failure raises a pending `TypeError` and yields null.
fn check_ret_type(
    frame: &str,
    ret: Option<&TypeHint>,
    called_with: &[Value],
    v: Value,
    static_class: Option<String>,
) -> Result<Value, String> {
    let Some(ty) = ret else { return Ok(v) };
    // `void` and `never` are refused while compiling; there is nothing left
    // to check here.
    if ty
        .parts
        .iter()
        .any(|p| p.eq_ignore_ascii_case("void") || p.eq_ignore_ascii_case("never"))
    {
        return Ok(v);
    }
    let ty = ty.clone();
    let scope = TypeScope::of_frame(frame, static_class);
    let scope_static = scope
        .static_class
        .clone()
        .or_else(|| scope.self_class.clone());
    match verify_type(v, &ty, &scope) {
        Ok(nv) => Ok(nv),
        // A return diagnostic names no call site — the `return` IS the site — but
        // the trace still enters the function the value was returned from.
        Err(given) => throw_from_callee(
            frame,
            called_with,
            "TypeError",
            &format!(
                "{}(): Return value must be of type {}, {given} returned",
                callee_shown(frame),
                render_declared(&ty, frame, scope_static.as_deref())
            ),
        ),
    }
}

/// A callee's declared signature: the parameters to bind on the way in and the
/// return type to check on the way out. The two travel together because every
/// caller of [`invoke`] has both and `invoke` reads them at opposite ends of the
/// same call.
struct Signature<'a> {
    params: &'a [Param],
    ret: Option<&'a TypeHint>,
    /// The callee's `#[\Deprecated]` attribute.
    deprecated: Option<&'a crate::ast::Deprecation>,
}

fn invoke(
    frame: &str,
    sig: Signature<'_>,
    body: Chunk,
    pre: Vec<(String, Value)>,
    args: Vec<Value>,
    named: Vec<(String, Value)>,
    is_generator: bool,
) -> Result<Value, String> {
    invoke_with_locals(frame, sig, body, pre, args, named, is_generator, &[])
}

/// [`invoke`] plus the callee's slot order: the frame reserves those slots, in
/// that order, before anything is bound, so an index the body was compiled with
/// names the same variable at runtime. Binding still goes through `set_var`,
/// which finds the reserved slot by name.
#[allow(clippy::too_many_arguments)]
fn invoke_with_locals(
    frame: &str,
    sig: Signature<'_>,
    body: Chunk,
    pre: Vec<(String, Value)>,
    args: Vec<Value>,
    named: Vec<(String, Value)>,
    is_generator: bool,
    locals: &[String],
) -> Result<Value, String> {
    let Signature {
        params,
        ret,
        deprecated,
    } = sig;
    // `ZEND_ACC_DEPRECATED`: the call raises the deprecation before the callee
    // binds anything, from the caller's line — and stops if a handler threw.
    if let Some(d) = deprecated {
        raise_deprecated_call(frame, d);
        // A handler runs NOW, before the callee is entered, as the reference's
        // `zend_deprecated_function` calls it.
        drain_error_handlers();
        if with_host(|h| h.pending_throw.is_some()) {
            return Ok(Value::Undef);
        }
    }
    // What a refusal raised for this call puts in its frame: the bound `$this`
    // (`A->m`) and a closure's site (`{closure:f.php:3}`).
    let ctx = CalleeCtx {
        this: pre
            .iter()
            .find(|(k, _)| k == "this")
            .map(|(_, v)| v.clone()),
        site: with_host(|h| h.pending_closure_site.clone()),
    };
    let checked = with_callee(&ctx, || check_call_shape(frame, params, &args, &named));
    if !checked? {
        // The call was rejected before it began; the dispatcher unwinds the
        // pending error.
        return Ok(Value::Undef);
    }
    let typed = with_callee(&ctx, || check_arg_types(frame, params, args, named));
    let Some((args, named)) = typed? else {
        // The call was rejected before it began: a `TypeError` is pending and the
        // dispatcher will unwind it.
        return Ok(Value::Undef);
    };
    // The trace of a return-type failure prints the arguments the call was made
    // with, so they are kept only when there is such a type to fail.
    let ret_args: Vec<Value> = match ret {
        Some(_) => args.clone(),
        None => Vec::new(),
    };
    // Which parameter positions a positional or named argument (or a null-fill for
    // a no-default omitted param) already bound — so the default pass below runs
    // only the chunks that are actually needed. Computed inside the binding closure.
    // Code compiled from another file names that file; the main script's
    // chunks carry no source, and their frames fall back to the script name.
    let file = (!body.source.is_empty()).then(|| std::sync::Arc::from(body.source.as_str()));
    let bound = with_host(|h| {
        let scope = Scope {
            name: Some(frame.to_string()),
            static_class: h.lsb_take(),
            closure_site: h.closure_site_take(),
            // The closure `f(...)` synthesizes forwards to its callee and is
            // invisible in a trace: the reference calls the target directly.
            transparent: std::mem::take(&mut h.pending_transparent)
                || params.first().is_some_and(|p| p.name == FCC_PARAM),
            ..Scope::default()
        };
        let scope = Scope { file, ..scope };
        h.scopes.push(scope);
        h.seed_slots(locals);
        // Captured bindings first, then parameters (a parameter of the same name
        // as a capture shadows it, as PHP does).
        for (k, v) in pre {
            // A `use (&$v)` capture arrives as a handle to the enclosing
            // variable's cell: bind the name to that cell rather than storing
            // the handle, and the two are one variable from here on.
            match v {
                Value::Obj(id) => match h.objs.get(id as usize) {
                    Some(PhpObj::Ref { slot }) => {
                        let slot = *slot;
                        h.bind_ref_slot(&k, slot);
                    }
                    _ => h.set_var(&k, v),
                },
                _ => h.set_var(&k, v),
            }
        }
        let mut bound = vec![false; params.len()];
        // Positional binding (a variadic `...$rest` collects the rest positionally).
        let mut ai = 0;
        for (i, p) in params.iter().enumerate() {
            if p.variadic {
                let arr = h.new_array();
                while ai < args.len() {
                    h.arr_push_auto(&arr, args[ai].clone());
                    ai += 1;
                }
                h.set_var(&p.name, arr);
                bound[i] = true;
            } else if ai < args.len() {
                // A by-value parameter takes a copy of an array argument; a
                // by-reference one must see the caller's array itself.
                match (p.by_ref, &args[ai]) {
                    // A caller that supplied an explicit reference cell (a
                    // builtin passing an array element to a callback, say) binds
                    // the parameter to that cell, so scalar writes are visible to
                    // the caller too — not just in-place array mutation.
                    (true, Value::Obj(id))
                        if matches!(h.objs.get(*id as usize), Some(PhpObj::Ref { .. })) =>
                    {
                        let Some(PhpObj::Ref { slot }) = h.objs.get(*id as usize) else {
                            unreachable!("guarded by the match arm above")
                        };
                        let slot = *slot;
                        h.bind_ref_slot(&p.name, slot);
                    }
                    (true, a) => h.set_var(&p.name, a.clone()),
                    (false, a) => {
                        // A by-value parameter never sees a reference cell: read
                        // through it so a builtin that offers an element by
                        // reference still works with a plain `function ($v)`.
                        let a = match a {
                            Value::Obj(id) => match h.objs.get(*id as usize) {
                                Some(PhpObj::Ref { slot }) => {
                                    let slot = *slot;
                                    h.ref_cell_value(slot)
                                }
                                _ => a.clone(),
                            },
                            _ => a.clone(),
                        };
                        let copied = h.copy_on_assign(a);
                        h.set_var(&p.name, copied);
                    }
                }
                bound[i] = true;
                ai += 1;
            }
        }
        // Named binding: match each `name: value` to the parameter of that name; an
        // unmatched name lands in the variadic parameter's array under a string key
        // (as PHP collects extra named args), or is dropped if there is no variadic.
        for (n, v) in &named {
            if let Some(i) = params.iter().position(|p| !p.variadic && p.name == *n) {
                let v = if params[i].by_ref {
                    v.clone()
                } else {
                    h.copy_on_assign(v.clone())
                };
                h.set_var(&params[i].name, v);
                bound[i] = true;
            } else if let Some(vi) = params.iter().position(|p| p.variadic) {
                let arr = h.get_var(&params[vi].name);
                h.arr_set_key(&arr, &Value::str(n.clone()), v.clone());
            }
        }
        // A parameter with neither an argument nor a default reads as null.
        for (i, p) in params.iter().enumerate() {
            if !bound[i] && !p.variadic && p.default.is_none() {
                h.set_var(&p.name, Value::Undef);
                bound[i] = true;
            }
        }
        // The call's argument list as `func_get_args()` reports it, in the hidden
        // `@args`/`@argnames` pair. Since PHP 7 the reference reports a declared
        // parameter's CURRENT value, not the value the caller passed — `function
        // f($a) { $a = 99; return func_get_args(); }` called as `f(1)` answers
        // `[99]`. So a position covered by a declared parameter stashes that
        // parameter's NAME and the read goes through the variable; only an
        // argument PAST the declared parameters is stored by value, which is why
        // mutating a variadic's array does not change what is reported.
        //
        // The count is the number of arguments the call supplied. A named
        // argument counts at its PARAMETER's position, so `f(9, c: 7)` on
        // `f($a=1,$b=2,$c=3)` reports three — the skipped `$b` reported at its
        // default, exactly as reading the parameter gives.
        let n_decl = params.iter().filter(|p| !p.variadic).count();
        let mut argc = args.len();
        for (n, _) in &named {
            if let Some(i) = params.iter().position(|p| !p.variadic && p.name == *n) {
                argc = argc.max(i + 1);
            }
        }
        let argsarr = h.new_array();
        let namesarr = h.new_array();
        for i in 0..argc {
            // A position covered by a declared parameter records the NAME and no
            // value; one past them records the value and no name.
            let declared = params.get(i).filter(|_| i < n_decl);
            let (name, value) = match declared {
                Some(p) => (Value::str(p.name.clone()), Value::Undef),
                None => (Value::Undef, args.get(i).cloned().unwrap_or(Value::Undef)),
            };
            h.arr_push_auto(&namesarr, name);
            h.arr_push_auto(&argsarr, value);
        }
        h.set_var("@args", argsarr);
        h.set_var("@argnames", namesarr);
        bound
    });
    // Evaluate defaults for the still-unbound parameters, left to right, so a
    // default may reference an earlier parameter already bound in this frame.
    for (i, p) in params.iter().enumerate() {
        if p.variadic || bound[i] {
            continue;
        }
        if let Some(chunk) = &p.default {
            match run_chunk_on(chunk.clone()) {
                Ok(v) => with_host(|h| h.set_var(&p.name, v)),
                Err(e) => {
                    with_host(|h| {
                        h.scopes.pop();
                    });
                    return Err(e);
                }
            }
        }
    }
    // A generator function does not run its body on call: it hands the fully-bound
    // call frame to a suspended coroutine and returns a Generator handle. The frame
    // is pulled off the active stack and lives in the generator until it finishes.
    if is_generator {
        let frame_scope = with_host(|h| h.scopes.pop().expect("generator call frame"));
        return Ok(make_generator(body, frame_scope));
    }
    let r = run_chunk_on(body);
    let sig = with_host(|h| {
        // Capture by-reference parameters' final values (read from the still-open
        // callee frame) so the caller can write them back after the call. Recorded
        // for EVERY call, so a callee with no by-reference parameter clears what
        // the previous one left rather than letting a caller read it back.
        let vals = params
            .iter()
            .map(|p| {
                if p.by_ref {
                    h.get_var(&p.name)
                } else {
                    Value::Undef
                }
            })
            .collect();
        h.byref_out_set(vals, params.iter().map(|p| p.by_ref).collect());
        let static_class = h.scopes.pop().and_then(|s| s.static_class);
        (h.signal.take(), static_class)
    });
    let (sig, static_class) = sig;
    // A pending exception (set by `throw`, kept in its own field) survives the
    // scope pop and takes precedence — the caller's dispatcher checks
    // `has_pending_throw` and re-halts to keep it bubbling.
    match sig {
        Some(Signal::Return(v)) => with_callee(&ctx, || {
            check_ret_type(frame, ret, &ret_args, v, static_class)
        }),
        // A `break`/`continue` that escapes a function body has no loop to
        // target; PHP treats it as falling off the end (null result). Falling off
        // the end is NOT checked against the return type: a `void` function does
        // it by design, and a typed one that reaches the end without returning is
        // a separate diagnostic this engine does not raise.
        Some(Signal::Break(_)) | Some(Signal::Continue(_)) | None => r.map(|_| Value::Undef),
    }
}

/// Run a closure body: bind its captures (a bound `$this` overriding any captured
/// one) and set the frame's class scope so private/protected access matches the
/// bound scope, then dispatch (as a generator, if its body yields).
fn invoke_closure(
    cc: ClosureCall,
    args: Vec<Value>,
    named: Vec<(String, Value)>,
) -> Result<Value, String> {
    // Encode the private-access scope in the frame name (`Scope::{closure}`), which
    // `current_class_ctx` reads back for visibility checks.
    let frame = match &cc.scope {
        Some(s) => format!("{s}::{{closure}}"),
        None => "{closure}".to_string(),
    };
    let mut pre = cc.captured;
    if let Some(t) = cc.bound_this {
        pre.retain(|(k, _)| k != "this");
        pre.push(("this".to_string(), t));
    }
    // The frame is built inside `invoke`, so the site is handed over the same
    // way the late-static-binding class is.
    with_host(|h| h.closure_site_for_next_call(cc.site));
    // `zend_closure_get_closure`: the frame runs with the closure's own called
    // scope, whatever forwarding the call site would otherwise have done.
    with_host(|h| h.pending_lsb = cc.called_scope);
    invoke(
        &frame,
        Signature {
            params: &cc.params,
            ret: cc.ret.as_ref(),
            deprecated: None,
        },
        cc.chunk,
        pre,
        args,
        named,
        cc.is_generator,
    )
}

/// The `Error` PHP raises for a value used as a callable that is not one.
///
/// Three distinct messages, and the reference distinguishes them: an array is
/// judged on its LENGTH (a callable array is exactly `[target, method]`), an
/// object that is not invokable names its class, and every scalar names its
/// type. All are catchable `Error`s — they used to surface as the scaffold-level
/// `php: value is not callable`, which no `try` block could see.
fn not_callable(v: &Value) -> String {
    let msg = with_host(|h| {
        if h.is_array(v) {
            "Array callback must have exactly two elements".to_string()
        } else if h.is_object(v) {
            let class = h.object_class(v).unwrap_or_else(|| "stdClass".to_string());
            format!("Object of type {class} is not callable")
        } else {
            // NOT `type_name_for_error`: that spells a bool as the literal
            // `true`/`false` (which is what an argument TypeError wants). This
            // message names the TYPE, so a bool is `bool`.
            let t = match v {
                Value::Bool(_) => "bool".to_string(),
                other => h.type_name_for_error(other),
            };
            format!("Value of type {t} is not callable")
        }
    });
    crate::builtins::throws_bare("Error", msg)
}

/// A callable value that is not a closure, resolved to the method it names.
///
/// PHP accepts four such forms and this is the ONE place that decodes them, so
/// every entry point agrees: `$f(…)`, `usort`, `array_map`,
/// `preg_replace_callback` and `Closure::fromCallable` all route through
/// [`call_value`]. Splitting the knowledge — which is what happened before, with
/// only `call_user_func` able to read the array and `"C::m"` forms — meant
/// `usort($a, [$obj, "cmp"])` raised "Array callback must have exactly two
/// elements" on an array that HAD two elements, and `array_map` silently
/// returned its input unmapped.
///
/// Returns `None` when `callee` is a plain function-name string (the caller
/// dispatches it by name) or is not callable at all.
pub(crate) fn callable_method(callee: &Value) -> Option<(String, String, Option<Value>)> {
    // Array callable — exactly `[target, method]`. An object target is an
    // instance call, a class-name string a static one.
    if with_host(|h| h.is_array(callee)) {
        let pairs = with_host(|h| h.array_pairs(callee)).unwrap_or_default();
        if pairs.len() != 2 {
            return None;
        }
        let target = pairs[0].1.clone();
        let method = with_host(|h| h.to_str(&pairs[1].1));
        return match with_host(|h| h.object_class(&target)) {
            Some(class) => Some((class, method, Some(target))),
            None => Some((
                anchored_class(&with_host(|h| h.to_str(&target))),
                method,
                None,
            )),
        };
    }
    // A non-array object is callable exactly when its class defines `__invoke`.
    if with_host(|h| h.is_object(callee)) {
        let class = with_host(|h| h.object_class(callee))?;
        if with_host(|h| h.class_has_method(&class, "__invoke")) {
            return Some((class, "__invoke".to_string(), Some(callee.clone())));
        }
        return None;
    }
    // `"Class::method"` — a static call spelled as one string.
    if let Value::Str(s) = callee {
        if let Some((class, method)) = s.as_str().split_once("::") {
            return Some((anchored_class(class), method.to_string(), None));
        }
    }
    None
}

/// Why `callee` cannot be called AT ALL, or `None` if the call gets to start.
///
/// This is [`call_value`]'s own decision tree, stopping where that one begins to
/// dispatch: a closure and anything [`callable_method`] resolves are let through
/// untouched, because whether the method they name exists is a question for the
/// method table, not for this. What is left is exactly `call_value`'s two
/// terminal arms — an unresolvable name string and [`not_callable`] — so this can
/// only ever refuse a call that was going to be refused a moment later.
///
/// It exists so the refusal can happen BEFORE the arguments are evaluated, which
/// is where the reference puts it: `$n = [1]; $n(f())` prints nothing before the
/// `Error`, because PHP never gets as far as `f()`.
pub(crate) fn callvalue_refusal(callee: &Value) -> Option<String> {
    if with_host(|h| h.closure_of(callee)).is_some() {
        return None;
    }
    // A callable naming a method answers to the method table, and it answers the
    // same way [`call_value`] will a moment later — so the refusal for
    // `$n = "Nope::m"` or `$n = [$obj, "nope"]` is available here, before the
    // arguments, which is where the reference raises it.
    if let Some((class, method, this)) = callable_method(callee) {
        return callable_method_refusal(&class, &method, &this);
    }
    match callee {
        Value::Str(s) => (!crate::stdlib::callable::function_resolves(s)).then(|| {
            crate::builtins::throws_bare("Error", format!("Call to undefined function {s}()"))
        }),
        other => Some(not_callable(other)),
    }
}

/// Why a method reached through a *value* — `$f = "C::m"`, `$f = [$obj, "m"]`,
/// the same shapes handed to `call_user_func` and `array_map` — cannot be
/// called, as a CATCHABLE `Error`, or `None` when the call gets to start.
///
/// `$o->m()` and `C::m()` are screened by their own opcodes, which throw a real
/// `Error` a `catch` block can see. The value form reached [`call_method`]
/// directly, whose miss is a bare `Err` — the scaffold surfaces that as
/// `php: Call to undefined method C::m()` on stderr and stops the program, so
/// `try { $f(1); } catch (Error $e)` never ran its handler. The three arms below
/// are the reference's, in its order: an undeclared class is reported as the
/// missing CLASS (`$n = "Nope::m"; $n(1)` says `Class "Nope" not found`, never
/// naming the method), and only then is the method table consulted.
fn callable_method_refusal(class: &str, method: &str, this: &Option<Value>) -> Option<String> {
    // Synthesized callees with no class table to consult, exactly as
    // `static_method_plan` lets them through: `Closure::bind`/`fromCallable`,
    // and the enum helpers. An engine object's methods (`Generator::next`) are
    // reached by `builtin_type_methods`, not by a `ClassDef`, so a class the
    // table does not know but that DOES answer this method is left alone.
    if class.eq_ignore_ascii_case("Closure") {
        return None;
    }
    if !with_host(|h| h.class_exists(class)) {
        if with_host(|h| h.class_has_method(class, method)) {
            return None;
        }
        return Some(crate::builtins::throws_bare(
            "Error",
            format!("Class \"{}\" not found", display_class(class)),
        ));
    }
    if with_host(|h| h.is_enum_class(class))
        && matches!(
            method.to_ascii_lowercase().as_str(),
            "cases" | "from" | "tryfrom"
        )
    {
        return None;
    }
    // `__call` answers an instance callable; `__callStatic` a `"C::m"` one. The
    // same test `static_method_plan` makes, so the two forms cannot disagree.
    let has_this = this
        .as_ref()
        .and_then(|t| with_host(|h| h.object_class(t)))
        .is_some_and(|c| with_host(|h| h.class_is_a_pub(&c, class)));
    match with_host(|h| h.method_dispatch(class, method, has_this)) {
        MethodDispatch::Direct | MethodDispatch::Magic => None,
        MethodDispatch::Denied(msg) => Some(crate::builtins::throws_bare("Error", msg)),
        MethodDispatch::Undefined => Some(crate::builtins::throws_bare(
            "Error",
            format!(
                "Call to undefined method {}::{method}()",
                display_class(class)
            ),
        )),
    }
}

/// Why `callee(...)` — PHP 8.1 first-class callable syntax — cannot build a
/// closure, as a catchable `Error`, or `None` when it can.
///
/// The reference settles the callee where the syntax is written: `$o->nope(...)`
/// raises `Call to undefined method C::nope()` on the spot, even though the
/// closure is never invoked. Every refusal is worded exactly as the CALL would
/// have been worded, which is why this delegates to `callvalue_refusal` rather
/// than inventing a second set of messages.
///
/// `instance` says the syntax was spelled with `->`. It cannot be recovered from
/// the value: both `$s->m(...)` and `$s::m(...)` on `$s = "C"` arrive as the
/// array `["C", "m"]`, and only the first is refused — a non-object receiver is
/// reported by its TYPE, as `$s->m()` itself would report it.
pub fn fcc_refusal(callable: &Value, instance: bool) -> Option<String> {
    if instance {
        let pairs = with_host(|h| h.array_pairs(callable)).unwrap_or_default();
        if let [(_, recv), (_, name)] = pairs.as_slice() {
            if !with_host(|h| h.is_object_value(recv)) {
                let method = with_host(|h| h.to_str(name));
                let ty = with_host(|h| crate::builtins::receiver_type_name(h, recv));
                return Some(crate::builtins::throws_bare(
                    "Error",
                    format!("Call to a member function {method}() on {ty}"),
                ));
            }
        }
    }
    callvalue_refusal(callable)
}

// ── autoloading ─────────────────────────────────────────────────────────────

impl PhpHost {
    /// Whether two autoloader callables are the same one, as
    /// `spl_autoload_register` decides a duplicate: the same closure or
    /// object, or the same function or method named the same way.
    fn same_callable(&self, a: &Value, b: &Value) -> bool {
        match (a, b) {
            (Value::Str(x), Value::Str(y)) => unanchored(x).eq_ignore_ascii_case(unanchored(y)),
            (Value::Obj(x), Value::Obj(y)) if x == y => true,
            _ if self.is_array(a) && self.is_array(b) => {
                let (pa, pb) = (
                    self.array_pairs(a).unwrap_or_default(),
                    self.array_pairs(b).unwrap_or_default(),
                );
                pa.len() == 2
                    && pb.len() == 2
                    && self
                        .to_str(&pa[1].1)
                        .eq_ignore_ascii_case(&self.to_str(&pb[1].1))
                    && match (&pa[0].1, &pb[0].1) {
                        (Value::Obj(x), Value::Obj(y)) if self.is_object(&pa[0].1) => x == y,
                        (x, y) => unanchored(&self.to_str(x))
                            .eq_ignore_ascii_case(unanchored(&self.to_str(y))),
                    }
            }
            _ => false,
        }
    }

    /// `spl_autoload_register(, prepend: )`. A callable
    /// already in the chain is not added twice.
    pub fn autoload_register(&mut self, callback: Value, prepend: bool) {
        if self
            .autoloaders
            .iter()
            .any(|l| self.same_callable(l, &callback))
        {
            return;
        }
        if prepend {
            self.autoloaders.insert(0, callback);
        } else {
            self.autoloaders.push(callback);
        }
    }

    /// `spl_autoload_unregister()`: whether it was in the chain.
    pub fn autoload_unregister(&mut self, callback: &Value) -> bool {
        match self
            .autoloaders
            .iter()
            .position(|l| self.same_callable(l, callback))
        {
            Some(i) => {
                self.autoloaders.remove(i);
                true
            }
            None => false,
        }
    }

    /// `spl_autoload_functions()`: the chain, in calling order.
    pub fn autoload_functions(&self) -> Vec<Value> {
        self.autoloaders.clone()
    }
}

/// Whether `name` could name a class at all (`zend_is_valid_class_name`): only
/// such a name is handed to an autoloader.
fn is_valid_class_name(name: &str) -> bool {
    !name.is_empty()
        && name
            .bytes()
            .all(|c| c.is_ascii_alphanumeric() || c == b'_' || c == b'\\' || c >= 0x80)
}

/// Make `class` exist if an autoloader can (`zend_lookup_class` →
/// `spl_perform_autoload`): each registered loader is called with the name,
/// leading `\` dropped, until the class, interface, trait or enum is
/// declared. Answers whether it exists afterwards. A loader that throws ends
/// the chain with the exception pending, and a loader that asks for the class
/// it is loading is answered `false`.
pub fn autoload(class: &str) -> Result<bool, String> {
    let name = unanchored(class);
    let lname = name.to_ascii_lowercase();
    let loaders = with_host(|h| {
        if h.class_exists(name)
            || h.autoloaders.is_empty()
            || h.autoloading.contains(&lname)
            || !is_valid_class_name(name)
        {
            None
        } else {
            h.autoloading.insert(lname.clone());
            Some(h.autoloaders.clone())
        }
    });
    let Some(loaders) = loaders else {
        return Ok(with_host(|h| h.class_exists(name)));
    };
    let mut out = Ok(());
    for loader in loaders {
        if let Err(e) = call_value(loader, vec![Value::str(name.to_string())]) {
            out = Err(e);
            break;
        }
        if unwinding() || with_host(|h| h.class_exists(name)) {
            break;
        }
    }
    with_host(|h| h.autoloading.remove(&lname));
    out.map(|()| with_host(|h| h.class_exists(name)))
}

/// Give the autoloaders their chance at `class` before a lookup that would
/// refuse it. `Some` is what the caller returns instead of going on: the run
/// is unwinding with a loader's exception. `self`, `parent` and `static` name
/// the running scope and are never autoloaded.
fn autoload_first(class: &str) -> Result<Option<Value>, String> {
    if ["self", "parent", "static"]
        .iter()
        .any(|r| class.eq_ignore_ascii_case(r))
    {
        return Ok(None);
    }
    class_known(class)?;
    Ok(unwinding().then_some(Value::Undef))
}

/// `(name, by_ref, has_default, variadic)` of one parameter, as a closure's
/// debug info needs it.
type DebugParam = (String, bool, bool, bool);

/// One parameter as a closure's debug info lists it: `&$name`, and whether
/// it is required.
fn debug_param_rows(params: &[DebugParam]) -> Vec<(String, &'static str)> {
    // `required_num_args`: everything up to the last parameter that has no
    // default and is not variadic, whatever defaults stand before it.
    let required = params
        .iter()
        .rposition(|(_, _, has_default, variadic)| !has_default && !variadic)
        .map_or(0, |i| i + 1);
    params
        .iter()
        .enumerate()
        .map(|(i, (name, by_ref, ..))| {
            let amp = if *by_ref { "&" } else { "" };
            let req = if i < required {
                "<required>"
            } else {
                "<optional>"
            };
            (format!("{amp}${name}"), req)
        })
        .collect()
}

/// `zend_closure_get_debug_info`: the array `var_dump` and `print_r` show for
/// a closure — `name`, `file` and `line` for a literal (`function` naming the
/// callee for a first-class callable), the `static` captured variables, the
/// bound `this`, and each `parameter` as `<required>`/`<optional>`. `None`
/// for anything but a closure.
/// `zend_generator_get_debug_info`: the one-entry array `var_dump` and
/// `print_r` show for a generator, `["function" => name]`. `None` for anything
/// but a generator.
pub fn generator_debug_info(v: &Value) -> Option<Value> {
    with_host(|h| {
        let id = h.gen_id(v)?;
        let function = Value::str(h.generators[id as usize].function.clone());
        let out = h.new_array();
        h.arr_set_key(&out, &Value::str("function"), function);
        Some(out)
    })
}

pub fn closure_debug_info(v: &Value) -> Option<Value> {
    struct Real {
        name: String,
        file: String,
        line: i64,
        statics: Vec<(String, Value)>,
    }
    let (real, fake_callee, this, params) = with_host(|h| {
        let Some(PhpObj::Closure {
            params,
            chunk,
            captured,
            bound_this,
            site,
            ..
        }) = h.as_array(v)
        else {
            return None;
        };
        let fake = params.first().is_some_and(|p| p.name == FCC_PARAM)
            || captured.iter().any(|(n, _)| n == "@fcc");
        let callee = fake
            .then(|| captured.iter().find(|(n, _)| n.starts_with("@fcc")))
            .flatten()
            .map(|(_, c)| h.deref(c.clone()));
        let real = (!fake).then(|| Real {
            name: site
                .as_ref()
                .map_or_else(|| "{closure}".to_string(), |s| s.render(&h.script_name)),
            file: if chunk.source.is_empty() {
                h.script_name.clone()
            } else {
                chunk.source.clone()
            },
            line: match site {
                Some(DeclSite::Closure(_, line)) => i64::from(*line),
                _ => 0,
            },
            // The captured variables, then the body's own `static`s, which
            // travel under their declaration key (`@static#N:name`).
            statics: captured
                .iter()
                .filter_map(|(n, v)| match n.strip_prefix("@static#") {
                    Some(key) => {
                        let name = key.split_once(':').map_or(key, |(_, name)| name);
                        Some((name.to_string(), h.deref(v.clone())))
                    }
                    None if n.starts_with(['@', '$']) || n == "this" => None,
                    None => Some((n.clone(), v.clone())),
                })
                .collect(),
        });
        let params: Vec<DebugParam> = params
            .iter()
            .map(|p| (p.name.clone(), p.by_ref, p.default.is_some(), p.variadic))
            .collect();
        Some((real, callee, bound_this.clone(), params))
    })?;
    // A first-class callable names the function it forwards to, and reports
    // that function's parameters rather than its own variadic shell.
    let (function, this, params, fn_statics) = match fake_callee {
        Some(callee) => {
            let (function, bound, params, statics) = fcc_target(&callee);
            (Some(function), bound.or(this), params, statics)
        }
        None => (None, this, params, Vec::new()),
    };
    let rows = debug_param_rows(&params);
    Some(with_host(|h| {
        let out = h.new_array();
        let set = |h: &mut PhpHost, k: &str, val: Value| h.arr_set_key(&out, &Value::str(k), val);
        match (&real, function) {
            (Some(r), _) => {
                set(h, "name", Value::str(r.name.clone()));
                set(h, "file", Value::str(r.file.clone()));
                set(h, "line", Value::int(r.line));
                if !r.statics.is_empty() {
                    let st = h.new_array();
                    for (n, val) in &r.statics {
                        h.arr_set_key(&st, &Value::str(n.clone()), val.clone());
                    }
                    set(h, "static", st);
                }
            }
            (None, Some(f)) => {
                set(h, "function", Value::str(f));
                if !fn_statics.is_empty() {
                    let st = h.new_array();
                    for (n, val) in &fn_statics {
                        h.arr_set_key(&st, &Value::str(n.clone()), val.clone());
                    }
                    set(h, "static", st);
                }
            }
            (None, None) => {}
        }
        if let Some(t) = this {
            set(h, "this", t);
        }
        if !rows.is_empty() {
            let ps = h.new_array();
            for (k, req) in rows {
                h.arr_set_key(&ps, &Value::str(k), Value::str(req.to_string()));
            }
            set(h, "parameter", ps);
        }
        out
    }))
}

/// What a first-class callable forwards to, as its debug info reports it: the
/// callee's name (`strlen`, `K::m`), the object a method callable is bound
/// to, and the callee's parameters `(name, by_ref, has_default, variadic)`.
/// The `static` variables a function or method definition declares, by name:
/// the value each holds, or — before its declaration first ran — its constant
/// initializer, else null.
pub fn defined_statics(statics: &[(String, Option<Chunk>)]) -> Vec<(String, Value)> {
    statics
        .iter()
        .map(|(key, init)| {
            let name = key
                .split_once(':')
                .map_or(key.as_str(), |(_, n)| n)
                .to_string();
            let held = with_host(|h| h.static_slots.get(key).map(|&s| h.ref_cells[s].clone()));
            let v = match (held, init) {
                (Some(v), _) => v,
                (None, Some(chunk)) => run_chunk_on(chunk.clone()).unwrap_or(Value::Undef),
                (None, None) => Value::Undef,
            };
            (name, v)
        })
        .collect()
}

/// The `static` variables of the user function `name` (see
/// [`defined_statics`]), empty for an unknown or library one.
pub fn function_statics(name: &str) -> Vec<(String, Value)> {
    let lower = unanchored(name).to_ascii_lowercase();
    let defs = with_host(|h| h.functions.get(&lower).map(|d| d.statics.clone()));
    defs.map(|d| defined_statics(&d)).unwrap_or_default()
}

/// The doc comment `getDocComment()` reports: of a closure object
/// (`kind` "closure", `target` the closure), a function ("function", its
/// name), a method ("method", class, name), or a class or one of its members
/// ("class", "prop", "const", "case" — class, member name), the member found
/// on the class or the nearest ancestor (or interface) that declares it.
pub fn doc_comment(kind: &str, target: &Value, name: &str) -> Option<String> {
    with_host(|h| match kind {
        "closure" => match h.as_array(target) {
            Some(PhpObj::Closure { doc, .. }) => doc.clone(),
            _ => None,
        },
        "function" => {
            let lower = unanchored(&h.to_str(target)).to_ascii_lowercase();
            h.functions.get(&lower).and_then(|d| d.doc.clone())
        }
        "method" => h
            .resolve_method(&h.to_str(target), &name.to_ascii_lowercase())
            .and_then(|(_, d)| d.doc),
        _ => {
            let key = if kind == "class" {
                "class".to_string()
            } else {
                format!("{kind}:{name}")
            };
            let mut pending = vec![h.to_str(target).to_ascii_lowercase()];
            let mut seen = Vec::new();
            while let Some(c) = pending.pop() {
                if seen.contains(&c) {
                    continue;
                }
                let Some(def) = h.classes.get(&c) else {
                    continue;
                };
                if let Some((_, d)) = def.docs.iter().find(|(k, _)| *k == key) {
                    return Some(d.clone());
                }
                if kind == "class" {
                    return None;
                }
                pending.extend(def.interfaces.iter().rev().map(|i| i.to_ascii_lowercase()));
                // A member a trait brought in keeps the trait's doc comment.
                pending.extend(def.uses.iter().rev().map(|t| t.to_ascii_lowercase()));
                pending.extend(def.parent.iter().map(|p| p.to_ascii_lowercase()));
                seen.push(c);
            }
            None
        }
    })
}

/// Where a closure, function, method or class was written (see [`DeclSpan`]),
/// or `None` for an internal one.
pub fn decl_span(kind: &str, target: &Value, name: &str) -> Option<DeclSpan> {
    with_host(|h| match kind {
        "closure" => match h.as_array(target) {
            Some(PhpObj::Closure { span, .. }) => span.clone(),
            _ => None,
        },
        "function" => {
            let lower = unanchored(&h.to_str(target)).to_ascii_lowercase();
            h.functions.get(&lower).and_then(|d| d.span.clone())
        }
        "method" => h
            .resolve_method(&h.to_str(target), &name.to_ascii_lowercase())
            .and_then(|(_, d)| d.span),
        _ => h
            .classes
            .get(&unanchored(&h.to_str(target)).to_ascii_lowercase())
            .and_then(|d| d.span.clone()),
    })
}

/// `(deprecated, returns by reference, static)` for a user function, method
/// or closure, as `Reflection*::__toString`, `returnsReference()` and
/// `isStatic()` read them.
pub fn refl_fn_flags(kind: &str, target: &Value, name: &str) -> (bool, bool, bool) {
    with_host(|h| {
        if let Some(PhpObj::Closure { is_static, .. }) = h.as_array(target) {
            return (false, false, *is_static);
        }
        let def = match kind {
            "function" => {
                let lower = unanchored(&h.to_str(target)).to_ascii_lowercase();
                h.functions.get(&lower).cloned()
            }
            "method" => h
                .resolve_method(&h.to_str(target), &name.to_ascii_lowercase())
                .map(|(_, d)| d),
            _ => None,
        };
        def.map_or((false, false, false), |d| {
            (d.deprecated.is_some(), d.returns_ref, false)
        })
    })
}

/// The attributes of a declaration and the class scope their arguments are
/// evaluated in: a function ("function", its name), method ("method", class,
/// name), closure ("closure", the closure), or a class or one of its members
/// ("class", "prop", "const", "case" — class, member name; found on the class
/// or the nearest ancestor or interface declaring it).
pub fn attr_defs(kind: &str, target: &Value, name: &str) -> Option<(String, Vec<AttrDef>)> {
    with_host(|h| match kind {
        "closure" => match h.as_array(target) {
            Some(PhpObj::Closure { attrs, scope, .. }) => {
                Some((scope.clone().unwrap_or_default(), attrs.clone()))
            }
            _ => None,
        },
        "function" => {
            let lower = unanchored(&h.to_str(target)).to_ascii_lowercase();
            h.functions
                .get(&lower)
                .map(|d| (String::new(), d.attrs.clone()))
        }
        "method" => h
            .resolve_method(&h.to_str(target), &name.to_ascii_lowercase())
            .map(|(c, d)| (c, d.attrs)),
        _ => {
            let key = if kind == "class" {
                "class".to_string()
            } else {
                format!("{kind}:{name}")
            };
            let mut pending = vec![h.to_str(target).to_ascii_lowercase()];
            let mut seen = Vec::new();
            while let Some(c) = pending.pop() {
                if seen.contains(&c) {
                    continue;
                }
                let Some(def) = h.classes.get(&c) else {
                    continue;
                };
                // An enum case is reflected as a class constant.
                let alt = format!("case:{name}");
                let found = def
                    .attrs
                    .iter()
                    .find(|(k, _)| *k == key || (kind == "const" && *k == alt));
                if let Some((_, a)) = found {
                    return Some((def.name.clone(), a.clone()));
                }
                if kind == "class" {
                    return Some((def.name.clone(), Vec::new()));
                }
                pending.extend(def.interfaces.iter().rev().map(|i| i.to_ascii_lowercase()));
                pending.extend(def.parent.iter().map(|p| p.to_ascii_lowercase()));
                seen.push(c);
            }
            None
        }
    })
}

/// The `static` variables of method `class::method` (see [`defined_statics`]).
pub fn method_statics(class: &str, method: &str) -> Vec<(String, Value)> {
    let defs = with_host(|h| {
        h.resolve_method(class, &method.to_ascii_lowercase())
            .map(|(_, d)| d.statics)
    });
    defs.map(|d| defined_statics(&d)).unwrap_or_default()
}

/// What a first-class callable's dump reports of its target: the name it
/// forwards to, a bound `$this`, its parameters and its `static`s.
type FccTarget = (String, Option<Value>, Vec<DebugParam>, Vec<(String, Value)>);

fn fcc_target(callee: &Value) -> FccTarget {
    let user_params = |def: &FuncDef| -> Vec<DebugParam> {
        def.params
            .iter()
            .map(|p| (p.name.clone(), p.by_ref, p.default.is_some(), p.variadic))
            .collect()
    };
    if let Some((class, method, this)) = callable_method(callee) {
        let found = with_host(|h| {
            let declared = h.class_display_name(&class.to_ascii_lowercase());
            let def = h.resolve_method(&class, &method).map(|(_, d)| d);
            (declared, def)
        });
        let (declared, def) = found;
        let params = def.as_ref().map(user_params).unwrap_or_default();
        let statics = def.map(|d| defined_statics(&d.statics)).unwrap_or_default();
        return (format!("{declared}::{method}"), this, params, statics);
    }
    let name = with_host(|h| h.to_str(callee));
    let name = unanchored(&name).to_string();
    if let Some(def) = with_host(|h| h.functions.get(&name.to_ascii_lowercase()).cloned()) {
        let params = user_params(&def);
        let statics = defined_statics(&def.statics);
        return (def.declared.clone().unwrap_or(name), None, params, statics);
    }
    let params = crate::argsig::library_params(&name)
        .unwrap_or_default()
        .into_iter()
        .map(|p| (p.name, p.by_ref, p.def.is_some(), p.variadic))
        .collect();
    (name.to_ascii_lowercase(), None, params, Vec::new())
}

/// Whether `class` exists, autoloading it if it does not yet.
pub fn class_known(class: &str) -> Result<bool, String> {
    if with_host(|h| h.class_exists(class)) {
        return Ok(true);
    }
    autoload(class)
}

/// Invoke a callable *value*: a closure handle runs its captured-plus-bound body
/// in a fresh scope; an array / `"C::m"` / `__invoke` object resolves through
/// `callable_method`; a plain string is dispatched by name through
/// `call_function`. Used by `$f(...)` calls and callback builtins (`array_map`).
pub fn call_value(callee: Value, args: Vec<Value>) -> Result<Value, String> {
    if let Some(cc) = with_host(|h| h.closure_of(&callee)) {
        return invoke_closure(cc, args, Vec::new());
    }
    if let Some((class, method, this)) = callable_method(&callee) {
        if let Some(e) = callable_method_refusal(&class, &method, &this) {
            return Err(e);
        }
        return call_method(&class, &method, this, args);
    }
    match callee {
        Value::Str(s) => call_function_dispatched(&s, args, Dispatch::Indirect),
        other => Err(not_callable(&other)),
    }
}

/// `call_value` with PHP 8.0 named arguments (for `$closure(name: v)` / `$f(...)`).
pub fn call_value_named(
    callee: Value,
    args: Vec<Value>,
    named: Vec<(String, Value)>,
) -> Result<Value, String> {
    if let Some(cc) = with_host(|h| h.closure_of(&callee)) {
        return invoke_closure(cc, args, named);
    }
    if let Some((class, method, this)) = callable_method(&callee) {
        if let Some(e) = callable_method_refusal(&class, &method, &this) {
            return Err(e);
        }
        return call_method_named(&class, &method, this, args, named);
    }
    match callee {
        Value::Str(s) => call_function_named(&s, args, named),
        other => Err(not_callable(&other)),
    }
}

/// `Closure::bind($fn, $obj, $scope)` / `$fn->bindTo($obj, $scope)` — a copy of the
/// closure with `$this` rebound to `obj` and its private-access scope set. `scope`
/// may be a class-name string, an object (its class is used), or null/"static"
/// (keep the current scope). A null `obj` unbinds `$this` (a static closure).
///
/// Port of `do_closure_bind` and the non-fake-closure checks of
/// `zend_valid_closure_binding` (`Zend/zend_closures.c`). Every refusal is a
/// warning and a NULL result, never an exception.
pub fn closure_bind(closure: &Value, obj: Value, scope: Option<Value>) -> Result<Value, String> {
    let this = matches!(obj, Value::Obj(_)).then_some(obj.clone());
    let Some(mut scope_class) = with_host(|h| resolve_bind_scope(h, closure, scope)) else {
        return Ok(Value::Undef);
    };
    let refusal = with_host(|h| {
        if this.is_some() && h.closure_is_static(closure) {
            return Some(
                "Cannot bind an instance to a static closure, this will be an error in PHP 9"
                    .to_string(),
            );
        }
        let current = h.closure_scope(closure).map(|s| s.to_ascii_lowercase());
        let internal = scope_class.as_deref().filter(|s| {
            let lower = s.to_ascii_lowercase();
            Some(&lower) != current.as_ref() && h.is_internal_class(&lower)
        });
        internal.map(|s| {
            format!(
                "Cannot bind closure to scope of internal class {s}, this will be an error in PHP 9"
            )
        })
    });
    if let Some(msg) = refusal {
        with_host(|h| h.warn(msg));
        return Ok(Value::Undef);
    }
    // `zend_create_closure_ex`: an object bound without any scope gets the dummy
    // scope `Closure`, so `self::` names `Closure` and no private member opens.
    if scope_class.is_none() && this.is_some() {
        scope_class = Some("Closure".to_string());
    }
    match with_host(|h| h.rebind_closure(closure, this, scope_class)) {
        Some(v) => Ok(v),
        None => Err("Closure::bind expects a Closure".to_string()),
    }
}

/// Resolve the `$scope` argument of `bind`/`bindTo` the way `do_closure_bind`
/// does: an object names its class; the exact string `"static"` — also what an
/// omitted argument defaults to — keeps the closure's current scope; any other
/// string must name a class; and `null` is the unscoped closure. The outer
/// `None` is the refusal for a class that does not exist, after its warning.
fn resolve_bind_scope(
    h: &mut PhpHost,
    closure: &Value,
    scope: Option<Value>,
) -> Option<Option<String>> {
    let s = match scope {
        None => return Some(h.closure_scope(closure)),
        Some(Value::Undef) => return Some(None),
        Some(o @ Value::Obj(_)) if h.is_object(&o) => return Some(h.object_class(&o)),
        Some(other) => h.to_str(&other),
    };
    if s == "static" {
        return Some(h.closure_scope(closure));
    }
    if !h.class_exists(&s) {
        h.warn(format!("Class \"{s}\" not found"));
        return None;
    }
    let lower = s.to_ascii_lowercase();
    Some(Some(match h.classes.get(&lower) {
        Some(d) => d.name.clone(),
        None => builtin_type(&lower).map_or(s, |(n, ..)| n.to_string()),
    }))
}

/// `$fn->call($obj, ...$args)` — bind `$fn` to `$obj` with the scope set to
/// `$obj`'s class (so it can reach that class's private members), then call it.
pub fn closure_call(closure: &Value, obj: Value, args: Vec<Value>) -> Result<Value, String> {
    // `call` always scopes to the bound object's class — pass the object as the
    // scope so `resolve_bind_scope` derives its class.
    let bound = closure_bind(closure, obj.clone(), Some(obj))?;
    // A refused binding has already warned; `call` then answers null.
    if matches!(bound, Value::Undef) {
        return Ok(Value::Undef);
    }
    call_value(bound, args)
}

/// Dispatch a method call on a `Closure` object: `bindTo`, `call`, or `__invoke`.
pub fn call_closure_method(
    closure: &Value,
    method: &str,
    args: Vec<Value>,
) -> Result<Value, String> {
    let mut args = args;
    match method.to_ascii_lowercase().as_str() {
        "bindto" => {
            let obj = if args.is_empty() {
                Value::Undef
            } else {
                args.remove(0)
            };
            let scope = if args.is_empty() {
                None
            } else {
                Some(args.remove(0))
            };
            closure_bind(closure, obj, scope)
        }
        "call" => {
            let obj = if args.is_empty() {
                Value::Undef
            } else {
                args.remove(0)
            };
            closure_call(closure, obj, args)
        }
        "__invoke" => call_value(closure.clone(), args),
        other => Err(crate::builtins::throws_bare(
            "Error",
            format!("Call to undefined method Closure::{other}()"),
        )),
    }
}

/// The method names [`call_closure_method`] dispatches, lowercased.
///
/// Stated as data beside that `match` so [`engine_method_refusal`] — which has
/// to answer "does this name exist" BEFORE the arguments run, where the
/// dispatcher's own answer would come too late — cannot drift away from it.
const CLOSURE_METHODS: &[&str] = &["bindto", "call", "__invoke"];

/// [`CLOSURE_METHODS`] for [`call_generator_method`].
const GENERATOR_METHODS: &[&str] = &[
    "current",
    "key",
    "next",
    "valid",
    "rewind",
    "send",
    "throw",
    "getreturn",
];

/// Why `$closure->m(…)` or `$generator->m(…)` is refused, as a catchable
/// `Error`, or `None` when the call dispatches.
///
/// A `Closure` and a `Generator` are objects with no PHP class, so neither
/// `PhpHost::method_dispatch` nor the class method table can answer for them;
/// they are dispatched by name in [`call_closure_method`] and
/// [`call_generator_method`]. This is that same decision, available early enough
/// to stop `$c->nope(f())` before `f` runs — which is where the reference stops
/// it.
pub fn engine_method_refusal(recv: &Value, method: &str) -> Option<String> {
    let (class, methods) = if with_host(|h| h.is_closure(recv)) {
        ("Closure", CLOSURE_METHODS)
    } else if with_host(|h| h.is_generator_val(recv)) {
        ("Generator", GENERATOR_METHODS)
    } else {
        return None;
    };
    let lname = method.to_ascii_lowercase();
    if methods.contains(&lname.as_str()) {
        return None;
    }
    Some(crate::builtins::throws_bare(
        "Error",
        format!("Call to undefined method {class}::{method}()"),
    ))
}

// ── objects (new / method dispatch / constants) ─────────────────────────────

/// Record on a freshly allocated Throwable where it was raised.
///
/// PHP fixes `file`, `line` and the backtrace when the exception object is
/// *constructed*, not when it is thrown — `throw $e;` on a later line still
/// reports the `new` site — and that is also the only moment the frames are
/// still on the stack. `getFile`/`getLine`/`getTraceAsString` read the three
/// back, and the uncaught-exception fatal prints all of them.
fn seed_throwable(class: &str, obj: &Value) {
    with_host(|h| {
        let cl = class.to_ascii_lowercase();
        if !h.class_is_a(&cl, "exception") && !h.class_is_a(&cl, "error") {
            return;
        }
        // A `new` run by a PHP-written prelude method stands for a throw from
        // an internal one, which has no line of its own: the exception takes
        // the file and line of the first frame below that runs user code.
        let top = h.scopes.len() - 1;
        let mut i = top;
        while i > 0 && h.scopes[i].name.as_deref().is_some_and(is_prelude_frame) {
            i -= 1;
        }
        let (file, line) = if i == top {
            (h.current_file().to_string(), h.cur_frame_line() as i64)
        } else {
            (h.scope_file(i).to_string(), h.scopes[i].line as i64)
        };
        let trace = h.trace_array(false, false, 0);
        h.prop_set(obj, "file", Value::str(file));
        h.prop_set(obj, "line", Value::int(line));
        h.prop_set(obj, "trace", trace);
    });
}

/// Zero-based positions of the parameters an internal function declares
/// `#[\SensitiveParameter]`, which a backtrace must never print.
///
/// Only functions that can THROW need an entry, because an internal frame exists
/// only for the duration of [`throw_from_internal`]; a library function that
/// merely returns never reaches a trace. Verified against the reference by
/// throwing out of each and reading back `getTraceAsString`.
fn sensitive_params(func: &str) -> &'static [usize] {
    match func.to_ascii_lowercase().as_str() {
        "hash_hmac" | "hash_hmac_file" => &[2], // $key
        "hash_pbkdf2" => &[1],                  // $password
        _ => &[],
    }
}

/// Throw a PHP exception *from inside a library function*, with the internal
/// call as a real backtrace frame.
///
/// PHP renders such a throw with the library function itself at `#0`:
///
/// ```text
/// Fatal error: Uncaught ValueError: range(): Argument #3 ($step) … in file:1
/// Stack trace:
/// #0 file(1): range(9, 10, 2)
/// #1 {main}
/// ```
///
/// A library function has no PHP call frame — it is Rust — so one is pushed for
/// exactly as long as the exception object takes to construct, which is when PHP
/// snapshots `file`, `line` and the trace (`seed_throwable`). The frame carries
/// the arguments as its hidden `@args` so the trace prints them through the same
/// `trace_arg` rendering a user frame uses, and its line is the CALLER's, which
/// is the line PHP reports for a throw out of an internal function.
///
/// Returns `Ok(Undef)`: the exception is recorded as pending, and the `bubbled`
/// helper at every call site halts the chunk the moment it sees one, so the value
/// is never observed. Reporting it as `Err` instead would surface the scaffold's
/// terse `php: …` fatal and lose the catchability.
pub fn throw_from_internal(
    func: &str,
    args: &[Value],
    class: &str,
    message: &str,
) -> Result<Value, String> {
    throw_from_internal_args(func, args, class, vec![Value::str(message.to_string())])
}

/// Report an engine-level fatal raised inside a library function, with the trace
/// frame for the call the way the reference prints it. Returns the error string
/// the VM stops on; nothing is thrown, so no `catch` can see it — contrast
/// `throw_from_internal`, which builds a real Throwable.
pub fn fatal_from_internal(func: &str, args: &[Value], message: &str) -> String {
    let body = with_host(|h| {
        let line = h.cur_frame_line();
        h.scopes.push(Scope {
            name: Some(func.to_string()),
            line,
            internal: true,
            ..Scope::default()
        });
        let argsarr = h.new_array();
        for a in args {
            h.arr_push_auto(&argsarr, a.clone());
        }
        h.set_var("@args", argsarr);
        let trace = h.backtrace();
        h.scopes.pop();
        let body = format!(
            "{message} in {} on line {line}\nStack trace:\n{trace}",
            h.current_file()
        );
        h.fatal("Fatal error", &body);
        body
    });
    format!("Fatal error:  {body}")
}

/// [`throw_from_internal`] with the exception's full constructor argument list,
/// for the throws whose `getCode()` or `getPrevious()` is part of the contract.
pub fn throw_from_internal_args(
    func: &str,
    args: &[Value],
    class: &str,
    ctor_args: Vec<Value>,
) -> Result<Value, String> {
    let sensitive = sensitive_params(func);
    with_host(|h| {
        let line = h.cur_frame_line();
        h.scopes.push(Scope {
            name: Some(func.to_string()),
            line,
            internal: true,
            ..Scope::default()
        });
        let argsarr = h.new_array();
        for (i, a) in args.iter().enumerate() {
            if sensitive.contains(&i) {
                // A `#[\SensitiveParameter]` argument never appears in a trace:
                // the engine substitutes a `SensitiveParameterValue` wrapper so a
                // password or key cannot leak into an error log. The wrapper is
                // built as a real object of that class so `trace_arg` renders it
                // `Object(SensitiveParameterValue)` through the ordinary path.
                let obj = h.objs.len() as u32;
                h.objs.push(PhpObj::Object {
                    class: "SensitiveParameterValue".to_string(),
                    props: IndexMap::new(),
                });
                h.arr_push_auto(&argsarr, Value::Obj(obj));
            } else {
                h.arr_push_auto(&argsarr, a.clone());
            }
        }
        h.set_var("@args", argsarr);
    });
    // `new_object` runs PHP (the exception constructor), so it must not be called
    // inside the borrow above.
    let exc = new_object(class, ctor_args);
    with_host(|h| {
        h.scopes.pop();
    });
    set_pending_throw(exc?);
    Ok(Value::Undef)
}

/// `new Class(args)`: allocate an object seeded with its (inherited) property
/// defaults, then run its constructor. Property-default and constructor code run
/// on fresh VMs, so no host borrow is held across them.
pub fn new_object(class: &str, args: Vec<Value>) -> Result<Value, String> {
    let obj = alloc_object(class)?;
    construct_object(&obj, class, args, Vec::new())?;
    Ok(obj)
}

/// The first half of `new`: the instance, with its property defaults, before
/// a constructor argument is evaluated — `ZEND_NEW` allocates it there, which
/// is why `new A(new B)` makes `A` object #1 and `B` #2.
pub fn alloc_object(class: &str) -> Result<Value, String> {
    if let Some(v) = autoload_first(class)? {
        return Ok(v);
    }
    let cl = class.to_ascii_lowercase();
    // Both refusals are catchable `Error`s in the reference; a bare `Err` here
    // stopped the program with a scaffold message no `try` block could see.
    if let Some(e) = with_host(|h| h.class_instantiation_error(class)) {
        return Err(crate::builtins::throws_bare("Error", e));
    }
    let Some(defaults) = with_host(|h| h.class_prop_default_chunks(&cl)) else {
        return Err(crate::builtins::throws_bare(
            "Error",
            format!("Class \"{class}\" not found"),
        ));
    };
    // Evaluate each property default (a constant expression chunk).
    let mut props: IndexMap<String, Value> = IndexMap::new();
    for (name, (declaring, chunk, uninit)) in defaults {
        if uninit {
            continue;
        }
        let v = run_in_class_scope(&declaring, chunk)?;
        props.insert(name, v);
    }
    // Allocate the instance. The class name is stored with its original casing
    // for display (`get_class`, an uncaught-exception fatal); all lookups
    // (`resolve_method`, `catch_matches`, …) lowercase internally.
    let obj = with_host(|h| {
        h.objs.push(PhpObj::Object {
            class: class.to_string(),
            props,
        });
        Value::Obj((h.objs.len() - 1) as u32)
    });
    seed_throwable(class, &obj);
    Ok(obj)
}

/// `new` as the program wrote it: [`alloc_object`] after `ZEND_NEW`'s
/// constructor-visibility check, which the reference makes before a single
/// constructor argument is evaluated. The abstract/interface/enum refusal
/// comes first, as `object_init_ex` runs before `get_constructor`.
/// `unserialize` and the other paths that build an instance without its
/// constructor call [`alloc_object`] directly and skip the check.
pub fn new_alloc(class: &str) -> Result<Value, String> {
    let refused = with_host(|h| {
        h.class_instantiation_error(class)
            .or_else(|| h.constructor_access_error(class))
    });
    if let Some(e) = refused {
        return Err(crate::builtins::throws_bare("Error", e));
    }
    alloc_object(class)
}

/// The second half of `new`: run the constructor, if one exists anywhere in
/// the chain, over the allocated instance.
pub fn construct_object(
    obj: &Value,
    class: &str,
    args: Vec<Value>,
    named: Vec<(String, Value)>,
) -> Result<(), String> {
    let cl = class.to_ascii_lowercase();
    if with_host(|h| h.method_declared(&cl, "__construct")) {
        // `static::` inside the constructor is the instantiated class in its
        // declared spelling, not the lowercased lookup key.
        with_host(|h| h.lsb_set_for_next_call(class));
        if named.is_empty() {
            call_method(&cl, "__construct", Some(obj.clone()), args)?;
        } else {
            call_method_named(&cl, "__construct", Some(obj.clone()), args, named)?;
        }
    }
    Ok(())
}

/// `new Class(...)` with PHP 8.0 named constructor arguments.
pub fn new_object_named(
    class: &str,
    args: Vec<Value>,
    named: Vec<(String, Value)>,
) -> Result<Value, String> {
    let obj = new_alloc(class)?;
    construct_object(&obj, class, args, named)?;
    Ok(obj)
}

/// Invoke `Class::method` (resolved up the parent chain) with `this` bound to
/// `$this` when present. Reuses the shared `invoke` frame handling, so methods
/// honor default and variadic parameters.
pub fn call_method(
    class: &str,
    method: &str,
    this: Option<Value>,
    args: Vec<Value>,
) -> Result<Value, String> {
    let method_l = method.to_ascii_lowercase();
    // `Closure::bind($fn, $obj, $scope)` / `Closure::fromCallable($c)` — the only
    // static `Closure` methods, synthesized here (there is no `Closure` class).
    if class.eq_ignore_ascii_case("Closure") {
        return call_closure_static(&method_l, args);
    }
    // Enum static helpers `cases()`/`from()`/`tryFrom()` are synthesized, not
    // declared, so they are handled before the ordinary method resolution.
    if with_host(|h| h.is_enum_class(class)) {
        match method_l.as_str() {
            "cases" => return enum_cases_all(class),
            "from" => {
                return enum_from(
                    class,
                    args.into_iter().next().unwrap_or(Value::Undef),
                    false,
                )
            }
            "tryfrom" => {
                return enum_from(class, args.into_iter().next().unwrap_or(Value::Undef), true)
            }
            _ => {}
        }
    }
    let Some((def_class, def)) = with_host(|h| h.resolve_method(class, &method_l)) else {
        return call_magic_call(class, method, this, args);
    };
    let pre = match this {
        Some(t) => vec![("this".to_string(), t)],
        None => Vec::new(),
    };
    // `static::` inside the body resolves to the class the call NAMED, which is
    // not `def_class` — the method may have been inherited from an ancestor. A
    // forwarding call (`self::`, `parent::`, `static::`) has already claimed the
    // slot with the caller's own, and keeps it.
    with_host(|h| h.lsb_set_for_next_call(class));
    invoke_with_locals(
        &format!("{def_class}::{method}"),
        Signature {
            params: &def.params,
            ret: def.ret.as_ref(),
            deprecated: def.deprecated.as_ref(),
        },
        def.chunk,
        pre,
        args,
        Vec::new(),
        def.is_generator,
        &def.locals,
    )
}

/// The `__call` / `__callStatic` fallback: a call that resolved to no method (or
/// to one out of reach) is re-dispatched as `__call($name, $args)`, with the
/// argument list packed into a PHP array exactly as the reference does.
///
/// Every entry point for a method call funnels here, so a `call_user_func`, a
/// first-class callable and an ordinary `$o->m()` all reach the catch-all.
pub fn call_magic_call(
    class: &str,
    method: &str,
    this: Option<Value>,
    args: Vec<Value>,
) -> Result<Value, String> {
    let packed = with_host(|h| {
        let arr = h.new_array();
        for a in &args {
            let v = h.deref(a.clone());
            let v = h.copy_on_assign(v);
            h.arr_push_auto(&arr, v);
        }
        arr
    });
    call_magic_call_packed(class, method, this, packed)
}

/// [`call_magic_call`] for a call written with named arguments. PHP 8 hands
/// `__call` a single array whose positional arguments keep their integer keys and
/// whose named ones appear under their own parameter names.
pub fn call_magic_call_named(
    class: &str,
    method: &str,
    this: Option<Value>,
    args: Vec<Value>,
    named: Vec<(String, Value)>,
) -> Result<Value, String> {
    if named.is_empty() {
        return call_magic_call(class, method, this, args);
    }
    let packed = with_host(|h| {
        let arr = h.new_array();
        for a in &args {
            let v = h.deref(a.clone());
            let v = h.copy_on_assign(v);
            h.arr_push_auto(&arr, v);
        }
        for (k, v) in &named {
            let v = h.deref(v.clone());
            let v = h.copy_on_assign(v);
            h.arr_set_key(&arr, &Value::str(k.clone()), v);
        }
        arr
    });
    call_magic_call_packed(class, method, this, packed)
}

/// [`call_magic_call`] with the argument array already built — the named-argument
/// form needs to add string keys to it before the call.
fn call_magic_call_packed(
    class: &str,
    method: &str,
    this: Option<Value>,
    packed: Value,
) -> Result<Value, String> {
    let has_this = this.is_some();
    let Some(magic) = with_host(|h| h.magic_call_name(class, has_this)) else {
        return Err(format!(
            "Call to undefined method {}::{method}()",
            display_class(class)
        ));
    };
    let magic_args = vec![Value::str(method.to_string()), packed];
    let Some((def_class, def)) =
        with_host(|h| h.resolve_method(class, &magic.to_ascii_lowercase()))
    else {
        return Err(format!(
            "Call to undefined method {}::{method}()",
            display_class(class)
        ));
    };
    let pre = match this {
        Some(t) => vec![("this".to_string(), t)],
        None => Vec::new(),
    };
    with_host(|h| h.lsb_set_for_next_call(class));
    invoke(
        &format!("{def_class}::{magic}"),
        Signature {
            params: &def.params,
            ret: def.ret.as_ref(),
            deprecated: def.deprecated.as_ref(),
        },
        def.chunk,
        pre,
        magic_args,
        Vec::new(),
        def.is_generator,
    )
}

/// Static `Closure::` helpers: `bind($fn, $obj, $scope)` and
/// `fromCallable($callable)`.
fn call_closure_static(method_l: &str, mut args: Vec<Value>) -> Result<Value, String> {
    match method_l {
        "bind" => {
            let closure = if args.is_empty() {
                Value::Undef
            } else {
                args.remove(0)
            };
            let obj = if args.is_empty() {
                Value::Undef
            } else {
                args.remove(0)
            };
            let scope = if args.is_empty() {
                None
            } else {
                Some(args.remove(0))
            };
            closure_bind(&closure, obj, scope)
        }
        // `Closure::fromCallable($c)` — a real `Closure` over `$c`, which is
        // what makes `instanceof Closure` and `get_class()` answer the way the
        // reference does. It used to return its argument unchanged, so a string
        // callable stayed a string.
        "fromcallable" => {
            const FRAME: &str = "Closure::fromCallable";
            if args.len() != 1 {
                let msg = format!(
                    "Closure::fromCallable() expects exactly 1 argument, {} given",
                    args.len()
                );
                return throw_from_internal(FRAME, &args, "ArgumentCountError", &msg);
            }
            let callable = args.into_iter().next().unwrap_or(Value::Undef);
            // A `Closure` is already one; the reference hands the same object
            // back rather than wrapping it.
            if with_host(|h| h.is_closure(&callable)) {
                return Ok(callable);
            }
            // The refusal quotes the same reason a `callable` library parameter
            // does, under a heading of its own.
            if let Some(why) = crate::stdlib::callable::callable_reason(&callable) {
                let msg = format!("Failed to create closure from callable: {why}");
                return throw_from_internal(
                    FRAME,
                    std::slice::from_ref(&callable),
                    "TypeError",
                    &msg,
                );
            }
            Ok(with_host(|h| {
                h.make_closure(
                    crate::FROM_CALLABLE_FORWARDER,
                    // `@fcc` names the target the way a first-class callable
                    // does, which is how a dump reports the closure.
                    vec![
                        ("c".to_string(), callable.clone()),
                        ("@fcc".to_string(), callable),
                    ],
                )
            }))
        }
        other => Err(format!("call to undefined method Closure::{other}()")),
    }
}

/// `call_method` with PHP 8.0 named arguments.
pub fn call_method_named(
    class: &str,
    method: &str,
    this: Option<Value>,
    args: Vec<Value>,
    named: Vec<(String, Value)>,
) -> Result<Value, String> {
    let method_l = method.to_ascii_lowercase();
    let Some((def_class, def)) = with_host(|h| h.resolve_method(class, &method_l)) else {
        return call_magic_call_named(class, method, this, args, named);
    };
    let pre = match this {
        Some(t) => vec![("this".to_string(), t)],
        None => Vec::new(),
    };
    // `static::` inside the body resolves to the class the call NAMED, which is
    // not `def_class` — the method may have been inherited from an ancestor. A
    // forwarding call (`self::`, `parent::`, `static::`) has already claimed the
    // slot with the caller's own, and keeps it.
    with_host(|h| h.lsb_set_for_next_call(class));
    invoke_with_locals(
        &format!("{def_class}::{method}"),
        Signature {
            params: &def.params,
            ret: def.ret.as_ref(),
            deprecated: def.deprecated.as_ref(),
        },
        def.chunk,
        pre,
        args,
        named,
        def.is_generator,
        &def.locals,
    )
}

/// Run `body` with `obj` marked as the object being cloned, so the readonly
/// check knows to allow the one write PHP reopens inside `__clone`. Restores
/// the previous marker even when the hook throws, because a `__clone` that
/// throws must not leave the next write unguarded.
fn in_clone_hook<T>(obj: &Value, body: impl FnOnce() -> Result<T, String>) -> Result<T, String> {
    let Value::Obj(id) = obj else { return body() };
    let prev = with_host(|h| h.cloning.replace(*id));
    let r = body();
    with_host(|h| h.cloning = prev);
    r
}

/// Run `body` with `copy` marked as the object a `clone($o, [...])` is
/// initialising: its readonly properties may be written once more.
pub fn in_clone_with<T>(copy: &Value, body: impl FnOnce() -> T) -> T {
    let Value::Obj(id) = copy else { return body() };
    let prev = with_host(|h| h.cloning_with.replace(*id));
    let r = body();
    with_host(|h| h.cloning_with = prev);
    r
}

/// `clone $o` — duplicate the object, then run `__clone()` on the copy.
///
/// The hook runs on the NEW object with no arguments, and it may write
/// properties the outside world can no longer touch (a `readonly` one included,
/// which PHP allows precisely here so a clone can carry a fresh identity).
pub fn clone_object(v: Value) -> Result<Value, String> {
    // A Throwable is not clonable: the copy would share a trace with its source.
    // Nor are the SPL iterators whose handlers set `clone_obj = NULL`
    // (`spl_handlers_dual_it`, `spl_handlers_rec_it_it`), subclasses included.
    const UNCLONEABLE: [&str; 3] = [
        "iteratoriterator",
        "recursiveiteratoriterator",
        "weakreference",
    ];
    if with_host(|h| {
        h.object_class(&v).is_some_and(|c| {
            h.catch_matches(&c, "throwable") || UNCLONEABLE.iter().any(|u| h.class_is_a(&c, u))
        })
    }) {
        let class = with_host(|h| h.object_class(&v)).unwrap_or_default();
        return Err(crate::builtins::throws_bare(
            "Error",
            format!(
                "Trying to clone an uncloneable object of class {}",
                display_class(&class)
            ),
        ));
    }
    // An `enum` case is a SINGLETON: the reference refuses to duplicate one, by
    // the same message a generator gets, so `clone E::A` never produces a second
    // instance that `===` would tell apart from the case.
    if let Some(class) = with_host(|h| h.object_class(&v).filter(|c| h.is_enum_class(c))) {
        return Err(crate::builtins::throws_bare(
            "Error",
            format!(
                "Trying to clone an uncloneable object of class {}",
                display_class(&class)
            ),
        ));
    }
    let Some(copy) = with_host(|h| h.clone_obj(&v)) else {
        // Not clonable. A live generator holds a suspended stack that cannot be
        // duplicated and says so by name; everything else never was an object.
        if with_host(|h| h.gen_id(&v).is_some()) {
            return Err(crate::builtins::throws_bare(
                "Error",
                "Trying to clone an uncloneable object of class Generator",
            ));
        }
        // `type_name_for_error`, not `debug_type`: `clone true` is rejected with
        // `true given`, not `bool given`. See the note on that method.
        let t = with_host(|h| h.type_name_for_error(&v));
        return Err(crate::builtins::throws_bare(
            "TypeError",
            format!("clone(): Argument #1 ($object) must be of type object, {t} given"),
        ));
    };
    if let Some(class) = with_host(|h| h.object_class(&copy)) {
        // A prelude class whose state lives outside the instance (a static
        // table keyed by object id) copies it here, before any `__clone`.
        if with_host(|h| h.method_declared(&class.to_ascii_lowercase(), "__phplang_clone")) {
            call_method(
                &class.to_ascii_lowercase(),
                "__phplang_clone",
                Some(copy.clone()),
                vec![v.clone()],
            )?;
        }
        if with_host(|h| h.method_declared(&class.to_ascii_lowercase(), "__clone")) {
            in_clone_hook(&copy, || {
                call_method(
                    &class.to_ascii_lowercase(),
                    "__clone",
                    Some(copy.clone()),
                    Vec::new(),
                )
            })?;
        }
    }
    Ok(copy)
}

/// Run a constant or property initializer in the scope of the class that
/// declares it — see [`PhpHost::init_class_scope`].
fn run_in_class_scope(declaring: &str, chunk: Chunk) -> Result<Value, String> {
    with_host(|h| h.init_class_scope.push(declaring.to_string()));
    let r = run_chunk_on(chunk);
    with_host(|h| h.init_class_scope.pop());
    r
}

/// `Class::CONST` — evaluate the (inherited) constant initializer. On an `enum`, a
/// name that is not a real constant is resolved as an enum case singleton.
///
/// This is a USE of the constant (`K::C`, `constant("K::C")`), so a
/// `#[\Deprecated]` one reports itself; see [`class_const_silent`].
pub fn class_const(class: &str, name: &str) -> Result<Value, String> {
    class_const_with(class, name, ConstLookup::Use)
}

/// [`class_const`] without the deprecation: the `ZEND_FETCH_CLASS_SILENT`
/// lookups — `defined("K::C")` and `unserialize` resolving an enum case —
/// that inspect a constant rather than use it. Visibility still applies.
pub fn class_const_silent(class: &str, name: &str) -> Result<Value, String> {
    class_const_with(class, name, ConstLookup::Silent)
}

/// [`class_const_silent`] for reflection, which reads a constant of any
/// visibility (`ReflectionClass::getConstants()`, `ReflectionClassConstant::
/// getValue()` on a private constant).
pub fn class_const_reflect(class: &str, name: &str) -> Result<Value, String> {
    class_const_with(class, name, ConstLookup::Reflect)
}

/// Who is asking [`class_const_with`] for a constant.
#[derive(PartialEq)]
enum ConstLookup {
    Use,
    Silent,
    Reflect,
}

fn class_const_with(class: &str, name: &str, lookup: ConstLookup) -> Result<Value, String> {
    let report = lookup == ConstLookup::Use;
    if let Some(v) = autoload_first(class)? {
        return Ok(v);
    }
    if let Some((declaring, chunk)) = with_host(|h| h.resolve_const_chunk(class, name)) {
        if let Some(msg) = with_host(|h| h.const_access_denied(class, name))
            .filter(|_| lookup != ConstLookup::Reflect)
        {
            return Err(crate::builtins::throws_bare("Error", msg));
        }
        if report && raise_deprecated_member(&declaring, &format!("const:{name}")) {
            return Ok(Value::Undef);
        }
        return run_in_class_scope(&declaring, chunk);
    }
    // A deprecated case warns when it is NAMED (`E::A`), not when `cases()`,
    // `from()` or `tryFrom()` hand it out.
    let lower = class.to_ascii_lowercase();
    if report
        && with_host(|h| h.is_enum_class(&lower) && h.enum_case_chunk(&lower, name).is_some())
        && raise_deprecated_member(&lower, &format!("case:{name}"))
    {
        return Ok(Value::Undef);
    }
    if let Some(r) = enum_case(class, name) {
        return r;
    }
    // Both of these are catchable `Error`s in PHP, and which one it is depends
    // on whether the class exists at all — a distinction `$cls::K` makes very
    // visible, since the name only shows up at run time.
    let (declared, display) = with_host(|h| {
        (
            h.class_exists(class),
            h.class_display_name(&class.to_ascii_lowercase()),
        )
    });
    if !declared {
        return Err(crate::builtins::throws_bare(
            "Error",
            format!("Class \"{}\" not found", display_class(class)),
        ));
    }
    Err(crate::builtins::throws_bare(
        "Error",
        format!("Undefined constant {display}::{name}"),
    ))
}

/// Resolve `Enum::Case` to its singleton instance (built once, then cached so
/// `Enum::Case === Enum::Case` holds by object identity). `None` when `class` is
/// not an enum or has no case of that name — the caller then falls through to the
/// normal class-constant lookup / error.
pub fn enum_case(class: &str, case: &str) -> Option<Result<Value, String>> {
    let lower = class.to_ascii_lowercase();
    if !with_host(|h| h.is_enum_class(&lower)) {
        return None;
    }
    let chunk_opt = with_host(|h| h.enum_case_chunk(&lower, case))?;
    let key = format!("{lower}::{case}");
    if let Some(v) = with_host(|h| h.enum_case_cached(&key)) {
        return Some(Ok(v));
    }
    let value = match chunk_opt {
        Some(chunk) => match run_chunk_on(chunk) {
            Ok(v) => Some(v),
            Err(e) => return Some(Err(e)),
        },
        None => None,
    };
    let obj = with_host(|h| h.new_enum_object(class, case, value));
    with_host(|h| h.enum_case_store(&key, obj.clone()));
    Some(Ok(obj))
}

/// `Enum::cases()` — an array of every case singleton, in declaration order.
pub fn enum_cases_all(class: &str) -> Result<Value, String> {
    let names = with_host(|h| h.enum_case_names(class));
    let arr = with_host(|h| h.new_array());
    for name in names {
        match enum_case(class, &name) {
            Some(Ok(v)) => with_host(|h| h.arr_push_auto(&arr, v)),
            Some(Err(e)) => return Err(e),
            None => {}
        }
    }
    Ok(arr)
}

/// `Enum::from(value)` / `Enum::tryFrom(value)` — the case whose backing `value`
/// matches. `tryFrom` yields null on no match; `from` is a hard error (PHP throws
/// a catchable `ValueError`; the scaffold surfaces it as a fatal).
pub fn enum_from(class: &str, needle: Value, is_try: bool) -> Result<Value, String> {
    let names = with_host(|h| h.enum_case_names(class));
    let spelled = with_host(|h| h.declared_class_name(class));
    let fname = if is_try { "tryFrom" } else { "from" };
    // The BACKING TYPE, read off the first case. It decides three things the
    // needle alone cannot: which values are accepted at all, what they are
    // coerced to, and how the failure message renders them. A pure enum has no
    // backing value and no `from` at all.
    let first = match names.first() {
        Some(n) => match enum_case(class, n) {
            Some(Ok(v)) => v,
            Some(Err(e)) => return Err(e),
            None => Value::Undef,
        },
        None => Value::Undef,
    };
    let want_int = match with_host(|h| h.prop_get(&first, "value")) {
        Value::Int(_) => true,
        Value::Str(_) => false,
        _ => {
            return Err(crate::builtins::throws_bare(
                "Error",
                format!("Call to undefined method {spelled}::{fname}()"),
            ))
        }
    };
    // Weak-mode parameter coercion, which the reference performs BEFORE looking
    // for a matching case: `E::from(1.5)` on an int-backed enum finds the case
    // for `1` (after deprecating the narrowing), and `E::from("z")` on one is a
    // TypeError rather than a "not a valid backing value" ValueError.
    let coerced = match enum_backing_coerce(&needle, want_int, &spelled, fname) {
        Coerced::Value(v) => v,
        // The argument was refused: `throw_from_internal` has already recorded
        // the exception (it returns `Ok` with a PENDING throw, not `Err`), so
        // its own result is what has to travel back.
        Coerced::Refused(r) => return r,
    };
    let needle_s = with_host(|h| h.to_str(&coerced));
    for name in names {
        let case = match enum_case(class, &name) {
            Some(Ok(v)) => v,
            Some(Err(e)) => return Err(e),
            None => continue,
        };
        let val = with_host(|h| h.prop_get(&case, "value"));
        if with_host(|h| h.to_str(&val)) == needle_s {
            return Ok(case);
        }
    }
    if is_try {
        return Ok(Value::Undef);
    }
    // A catchable ValueError, not a scaffold fatal. The needle is rendered as
    // the BACKING type, which is what it was coerced to: an int bare, a string
    // quoted. Rendering the value as passed reported `E::from(9)` on a
    // string-backed enum as `9` where the reference says `"9"`.
    let shown = if want_int {
        needle_s.clone()
    } else {
        format!("\"{needle_s}\"")
    };
    // `from` is a real call in the reference's trace — `#0 file(1): E::from(99)`
    // — and both halves of the name are the DECLARED spelling however the call
    // was written, so `MYENUM::FROM(99)` still reports `MyEnum::from(99)`.
    throw_from_internal(
        &format!("{spelled}::from"),
        &[needle],
        "ValueError",
        &format!("{shown} is not a valid backing value for enum {spelled}"),
    )
}

/// Coerce `Enum::from`'s argument to the enum's backing type under PHP's
/// weak-mode rules for its `string|int $value` parameter.
///
/// The union prefers `int`, so a float, a bool and `null` all narrow to an int
/// first and only then become a string for a string-backed enum: `E::from(1.5)`
/// is the case for `"1"` there, not for `"1.5"`. Each narrowing carries the
/// reference's own diagnostic.
fn enum_backing_coerce(v: &Value, want_int: bool, spelled: &str, fname: &str) -> Coerced {
    // The reference's TypeError carries a FRAME for the call — `#0 file(1):
    // E::from('z')` — exactly as its ValueError does, so it goes through the
    // same internal-throw path rather than a bare throw.
    let type_error = |v: &Value| -> Coerced {
        let what = with_host(|h| h.type_name_for_error(v));
        let want = if want_int { "int" } else { "string|int" };
        let msg = format!(
            "{spelled}::{fname}(): Argument #1 ($value) must be of type {want}, {what} given"
        );
        Coerced::Refused(throw_from_internal(
            &format!("{spelled}::{fname}"),
            std::slice::from_ref(v),
            "TypeError",
            &msg,
        ))
    };
    let as_int = |n: i64| -> Value {
        if want_int {
            Value::int(n)
        } else {
            Value::str(n.to_string())
        }
    };
    match v {
        // An object or an array satisfies neither member of the union.
        Value::Obj(_) => type_error(v),
        Value::Undef => {
            with_host(|h| {
                h.deprecated(format!(
                    "{spelled}::{fname}(): Passing null to parameter #1 ($value) of \
                     type string|int is deprecated"
                ))
            });
            Coerced::Value(as_int(0))
        }
        Value::Bool(b) => Coerced::Value(as_int(i64::from(*b))),
        Value::Int(n) => Coerced::Value(as_int(*n)),
        Value::Float(f) => {
            if f.fract() != 0.0 {
                let shown = crate::stdlib::types::serialize_float(*f);
                with_host(|h| {
                    h.deprecated(format!(
                        "Implicit conversion from float {shown} to int loses precision"
                    ))
                });
            }
            Coerced::Value(as_int(*f as i64))
        }
        // A string satisfies the union as itself for a string-backed enum. For
        // an int-backed one it has to be numeric — and a numeric one narrows in
        // silence unless the narrowing loses a fraction.
        Value::Str(s) => {
            if !want_int {
                return Coerced::Value(v.clone());
            }
            if !is_numeric_string(s) {
                return type_error(v);
            }
            let f = with_host(|h| h.to_number(v)).to_float();
            if f.fract() != 0.0 {
                with_host(|h| {
                    h.deprecated(format!(
                        "Implicit conversion from float-string \"{s}\" to int loses precision"
                    ))
                });
            }
            Coerced::Value(Value::int(f as i64))
        }
        other => type_error(other),
    }
}

/// The outcome of [`enum_backing_coerce`]: the coerced value, or the refusal
/// that has already been thrown.
enum Coerced {
    Value(Value),
    Refused(Result<Value, String>),
}

/// The `Error` a `Class::$name` access raises when the calling scope cannot
/// reach the static property, as `zend_std_get_static_property_with_info`
/// (Zend/zend_object_handlers.c) reports it through `zend_bad_property_access`:
/// the visibility and the class the access NAMED, so `B::$q` for a `protected`
/// declared on parent `A` says `B::$q`, unlike the instance-property message.
fn static_prop_denied(class: &str, name: &str) -> Option<String> {
    with_host(|h| {
        let (declaring, vis) = h.resolve_prop_vis(class, name)?;
        if h.visibility_allows(vis, &declaring) {
            return None;
        }
        let vname = match vis {
            Visibility::Private => "private",
            Visibility::Protected => "protected",
            Visibility::Public => return None,
        };
        let shown = h.class_display_name(&class.to_ascii_lowercase());
        Some(format!("Cannot access {vname} property {shown}::${name}"))
    })
}

/// `&Class::$prop`: the reference cell the static property is stored in, after
/// the same resolution, visibility check and first-access initialization as a
/// read.
pub fn static_prop_ref_slot(class: &str, name: &str) -> Result<usize, String> {
    static_prop_get(class, name)?;
    let key = with_host(|h| h.resolve_static_key(class, name).map(|(k, _)| k))
        .expect("resolved by the read above");
    Ok(with_host(|h| h.static_ref_slot_ensure(&key)))
}

/// `Class::$prop = &$x`: bind the static property to the cell at `slot`, and
/// answer the value it now has.
pub fn static_prop_bind(class: &str, name: &str, slot: usize) -> Result<Value, String> {
    static_prop_get(class, name)?;
    let key = with_host(|h| h.resolve_static_key(class, name).map(|(k, _)| k))
        .expect("resolved by the read above");
    Ok(with_host(|h| {
        h.bind_static_to_slot(&key, slot);
        h.ref_cell_value(slot)
    }))
}

/// `Class::$prop` read. On first access the (constant) initializer runs once and
/// is stored in the per-class static cell; subsequent reads return the cell.
pub fn static_prop_get(class: &str, name: &str) -> Result<Value, String> {
    if let Some(v) = autoload_first(class)? {
        return Ok(v);
    }
    let Some((key, chunk)) = with_host(|h| h.resolve_static_key(class, name)) else {
        return Err(crate::builtins::throws_bare(
            "Error",
            format!("Access to undeclared static property {class}::${name}"),
        ));
    };
    if let Some(msg) = static_prop_denied(class, name) {
        return Err(crate::builtins::throws_bare("Error", msg));
    }
    if let Some(v) = with_host(|h| h.get_static_stored(&key)) {
        return Ok(v);
    }
    let declaring = key
        .split_once("::")
        .map_or(key.as_str(), |(c, _)| c)
        .to_string();
    let v = run_in_class_scope(&declaring, chunk)?;
    with_host(|h| h.set_static_stored(&key, v.clone()));
    Ok(v)
}

/// `Class::$prop` read in isset mode: an undeclared or unreachable static
/// property is null rather than an `Error`. An unknown class is still the
/// ordinary `Class "X" not found`.
pub fn static_prop_get_quiet(class: &str, name: &str) -> Result<Value, String> {
    if let Some(v) = autoload_first(class)? {
        return Ok(v);
    }
    let declared = with_host(|h| h.resolve_static_key(class, name).is_some());
    if !declared {
        if !with_host(|h| h.class_exists(class)) {
            return Err(crate::builtins::throws_bare(
                "Error",
                format!("Class \"{}\" not found", display_class(class)),
            ));
        }
        return Ok(Value::Undef);
    }
    if static_prop_denied(class, name).is_some() {
        return Ok(Value::Undef);
    }
    static_prop_get(class, name)
}

/// `get_class_vars`: the default instance properties, then the static ones at
/// their current value, that the calling scope can see. A typed property with
/// no default reads as null here, as in the reference. `None` for an unknown
/// class.
pub fn class_vars(class: &str) -> Result<Option<Value>, String> {
    let cl = class.to_ascii_lowercase();
    let Some(defaults) = with_host(|h| h.class_prop_default_chunks(&cl)) else {
        return Ok(None);
    };
    let defaults: FxHashMap<String, _> = defaults.into_iter().collect();
    let arr = with_host(|h| h.new_array());
    if with_host(|h| h.is_enum_class(&cl)) {
        let backed = with_host(|h| {
            h.classes
                .get(&cl)
                .is_some_and(|d| d.enum_cases.iter().any(|(_, c)| c.is_some()))
        });
        with_host(|h| h.arr_set_key(&arr, &Value::str("name"), Value::Undef));
        if backed {
            with_host(|h| h.arr_set_key(&arr, &Value::str("value"), Value::Undef));
        }
    }
    for name in with_host(|h| h.property_names_inheritance_order(&cl, false)) {
        let visible = with_host(|h| {
            h.resolve_prop_vis(&cl, &name)
                .is_none_or(|(decl, vis)| h.visibility_allows(vis, &decl))
        });
        let Some((declaring, chunk, uninit)) = defaults.get(&name).cloned() else {
            continue;
        };
        if !visible {
            continue;
        }
        let v = if uninit {
            Value::Undef
        } else {
            run_in_class_scope(&declaring, chunk)?
        };
        with_host(|h| h.arr_set_key(&arr, &Value::str(name), v));
    }
    // Statics are reported at their DECLARED default, not their current value.
    for name in with_host(|h| h.property_names_inheritance_order(&cl, true)) {
        if static_prop_denied(class, &name).is_some() {
            continue;
        }
        let Some((key, chunk)) = with_host(|h| h.resolve_static_key(class, &name)) else {
            continue;
        };
        let declaring = key
            .split_once("::")
            .map_or(key.as_str(), |(c, _)| c)
            .to_string();
        let v = run_in_class_scope(&declaring, chunk)?;
        with_host(|h| h.arr_set_key(&arr, &Value::str(name), v));
    }
    Ok(Some(arr))
}

/// `Class::$prop = val` — write the per-class static cell, initializing lazily.
pub fn static_prop_set(class: &str, name: &str, val: Value) -> Result<Value, String> {
    if let Some(v) = autoload_first(class)? {
        return Ok(v);
    }
    let Some((key, _)) = with_host(|h| h.resolve_static_key(class, name)) else {
        return Err(crate::builtins::throws_bare(
            "Error",
            format!("Access to undeclared static property {class}::${name}"),
        ));
    };
    if let Some(msg) = static_prop_denied(class, name) {
        return Err(crate::builtins::throws_bare("Error", msg));
    }
    if let Some(msg) = with_host(|h| h.static_set_visibility_error(class, name)) {
        return Err(crate::builtins::throws_bare("Error", msg));
    }
    with_host(|h| h.set_static_stored(&key, val.clone()));
    Ok(val)
}

/// `Class::$prop` as an array to write through — `ops::SPROP_ENSURE_ARRAY`.
pub fn static_prop_ensure_array(class: &str, name: &str) -> Result<Value, String> {
    let cur = static_prop_get(class, name)?;
    if let Some(kept) = with_host(|h| h.dim_container_kept(&cur)) {
        return Ok(kept);
    }
    let arr = with_host(|h| h.new_array());
    static_prop_set(class, name, arr.clone())?;
    Ok(arr)
}

/// Whether `v` is something `...` may unpack: an array, a Generator, or an
/// object following the `Traversable` protocols — the same shapes
/// [`foreach_prep`] drives. A PLAIN object is NOT one: `foreach` walks its
/// public properties, but a spread rejects it, so the two part ways here.
pub fn is_unpackable(v: &Value) -> bool {
    if with_host(|h| h.is_array(v) || h.is_generator_val(v)) {
        return true;
    }
    let Some(class) = with_host(|h| h.object_class(v)) else {
        return false;
    };
    with_host(|h| {
        h.class_has_method(&class, "getIterator")
            || (h.class_has_method(&class, "valid") && h.class_has_method(&class, "current"))
    })
}

/// Normalize a `foreach` subject to an iterable array. Arrays pass through; an
/// object is iterated eagerly into a `(key, value)` array: `IteratorAggregate`
/// via `getIterator()`, the `Iterator` protocol (`rewind`/`valid`/`current`/
/// `key`/`next`), or — as a fallthrough — its public properties. Eager
/// materialization is fine for the finite iterators phplang supports; an infinite
/// iterator would not terminate (documented). A non-iterable yields an empty array.
///
/// Building an array MERGES repeated keys, which a Traversable is free to
/// produce (`yield from` restarts its inner generator's keys at 0). A caller
/// that must see every element — `iterator_to_array($it, false)`,
/// `iterator_count` — takes [`traversable_pairs`] instead.
/// Whether a `foreach` walks `v` one step at a time rather than as the array of
/// its elements: a generator, or an object implementing `Iterator` — whose
/// `rewind`/`valid`/`current`/`key`/`next` run interleaved with the body, as
/// `zend_fe_fetch_object_helper` drives them.
pub fn is_lazy_iter(v: &Value) -> bool {
    with_host(|h| {
        h.is_generator_val(v)
            || h.object_class(v)
                .is_some_and(|c| h.class_is_a_pub(&c, "Iterator"))
    })
}

/// [`ops::FOREACH_ITER`]: what a `foreach` over `v` walks. An
/// `IteratorAggregate` is replaced by the iterator its `getIterator()`
/// answers, through any number of aggregates, as `zend_user_it_get_new_iterator`
/// does — and an answer that is not `Traversable` is that function's
/// exception. Anything else is walked as it is.
pub fn foreach_iter(v: Value) -> Result<Value, String> {
    let mut v = v;
    loop {
        let Some(class) = with_host(|h| h.object_class(&v)) else {
            return Ok(v);
        };
        if !with_host(|h| h.class_is_a_pub(&class, "IteratorAggregate")) {
            return Ok(v);
        }
        let it = call_method(&class, "getIterator", Some(v.clone()), Vec::new())?;
        if with_host(|h| h.pending_throw.is_some()) {
            return Ok(Value::Undef);
        }
        let traversable = with_host(|h| {
            h.is_generator_val(&it)
                || h.object_class(&it)
                    .is_some_and(|c| h.class_is_a_pub(&c, "Traversable"))
        });
        if !traversable {
            let shown = with_host(|h| h.instance_class(&v)).unwrap_or(class);
            let exc = new_object(
                "Exception",
                vec![Value::str(format!(
                    "Objects returned by {shown}::getIterator() must be traversable or implement interface Iterator"
                ))],
            )?;
            set_pending_throw(exc);
            return Ok(Value::Undef);
        }
        v = it;
    }
}

/// One `Iterator` method of a `foreach` subject that is not a generator.
///
/// The reference walks most SPL iterators through a native `get_iterator`
/// rather than their methods, so a trace shows no frame for those steps —
/// until a subclass overrides one of the five methods, which switches the
/// class to `zend_user_it_get_iterator` (`zend_implement_iterator`). The
/// prelude methods standing in for such a native walk run in a transparent
/// frame.
pub fn iter_call(v: &Value, method: &str) -> Result<Value, String> {
    /// The classes `ext/spl` gives a `get_iterator` of their own.
    const NATIVE: &[&str] = &[
        "arrayiterator",
        "recursivearrayiterator",
        "recursiveiteratoriterator",
        "recursivetreeiterator",
        "spldoublylinkedlist",
        "splqueue",
        "splstack",
        "splheap",
        "splminheap",
        "splmaxheap",
        "splpriorityqueue",
    ];
    let class = with_host(|h| h.object_class(v)).ok_or("not an iterator")?;
    let native = with_host(|h| {
        ["rewind", "valid", "current", "key", "next"]
            .iter()
            .all(|m| {
                h.resolve_method(&class, m)
                    .is_some_and(|(owner, _)| NATIVE.contains(&owner.as_str()))
            })
    });
    with_host(|h| h.pending_transparent = native);
    let r = call_method(&class, method, Some(v.clone()), Vec::new());
    with_host(|h| h.pending_transparent = false);
    r
}

pub fn foreach_prep(v: Value) -> Result<Value, String> {
    if with_host(|h| h.is_array(&v)) {
        return Ok(v);
    }
    let arr = with_host(|h| h.new_array());
    let set = |key: Value, cur: Value| {
        with_host(|h| match key {
            Value::Undef => h.arr_push_auto(&arr, cur),
            k => h.arr_set_key(&arr, &k, cur),
        })
    };
    if let Some(inner) = traverse(&v, &mut |k, c| set(k, c))? {
        return foreach_prep(inner);
    }
    Ok(arr)
}

/// [`ops::FOREACH_KEYS`]: the key list a `foreach` walks. For a plain object
/// (no iterator protocol) it is the visible property names as the property
/// table keeps them — a numeric name stays the string `"5"` — where the
/// materialized array `arr` has normalized it to an integer key.
pub fn foreach_keys(subj: &Value, arr: &Value) -> Value {
    with_host(|h| {
        if !h.is_plain_foreach_object(subj) {
            return h.array_keys(arr);
        }
        let keys = h.new_array();
        for (k, _) in h.object_props_visible(subj) {
            h.arr_push_auto(&keys, prop_key(&k));
        }
        keys
    })
}

/// Every `(key, value)` a `foreach` over `v` would bind, in order and with
/// repeated keys kept. `Value::Undef` as a key means the source named none (an
/// `Iterator` without `key()`), which an array build turns into an append.
pub fn traversable_pairs(v: Value) -> Result<Vec<(Value, Value)>, String> {
    if with_host(|h| h.is_array(&v)) {
        return Ok(with_host(|h| h.array_pairs(&v).unwrap_or_default()));
    }
    let mut out = Vec::new();
    if let Some(inner) = traverse(&v, &mut |k, c| out.push((k, c)))? {
        return traversable_pairs(inner);
    }
    Ok(out)
}

/// Drive one non-array `foreach` subject, handing each `(key, value)` to `sink`.
///
/// `Ok(Some(inner))` means `v` is an `IteratorAggregate` and nothing was
/// emitted: the caller walks `inner`, which may itself be an array.
fn traverse(v: &Value, sink: &mut dyn FnMut(Value, Value)) -> Result<Option<Value>, String> {
    // A Generator is not a class instance — it has no entry in the class table —
    // so it is driven through its own protocol before the object paths below.
    // Materializing CONSUMES it, exactly as the reference's
    // `iterator_to_array($gen)` does.
    if with_host(|h| h.is_generator_val(v)) {
        gen_foreach_rewind(v)?;
        if with_host(|h| h.pending_throw.is_some()) {
            return Ok(None);
        }
        while gen_valid(v)? {
            let cur = gen_current(v)?;
            let key = gen_key(v)?;
            sink(key, cur);
            gen_next(v)?;
        }
        return Ok(None);
    }
    let Some(class) = with_host(|h| h.object_class(v)) else {
        return Ok(None);
    };
    // IteratorAggregate: `getIterator()` returns the real iterator (or a backing
    // array, for the SPL preludes) — the caller recurses on it.
    if with_host(|h| h.class_has_method(&class, "getIterator")) {
        return call_method(&class, "getIterator", Some(v.clone()), Vec::new()).map(Some);
    }
    // Iterator protocol.
    if with_host(|h| h.class_has_method(&class, "valid") && h.class_has_method(&class, "current")) {
        if with_host(|h| h.class_has_method(&class, "rewind")) {
            iter_call(v, "rewind")?;
        }
        let has_key = with_host(|h| h.class_has_method(&class, "key"));
        // Bound the walk so a broken `valid()` cannot hang the interpreter.
        for _ in 0..100_000_000u64 {
            let valid = iter_call(v, "valid")?;
            if !with_host(|h| h.is_truthy(&valid)) {
                break;
            }
            let cur = iter_call(v, "current")?;
            let key = if has_key {
                iter_call(v, "key")?
            } else {
                Value::Undef
            };
            sink(key, cur);
            iter_call(v, "next")?;
        }
        return Ok(None);
    }
    // Plain object: the properties the CALLING scope may see — public ones
    // from outside, private and protected ones too from inside the class —
    // which is the set `get_object_vars` answers with.
    for (k, val) in with_host(|h| h.object_props_visible(v)) {
        sink(prop_key(&k), val);
    }
    Ok(None)
}

/// `spl_iterator_apply`: rewind the subject's iterator, then while it is
/// valid run `step` and advance, stopping when `step` answers false. Unlike a
/// `foreach` it never asks for `current()` or `key()` — the callback reads the
/// iterator itself if it wants them. An `IteratorAggregate` is walked through
/// the iterator its `getIterator()` hands back.
pub fn iterator_apply_walk(
    v: Value,
    step: &mut dyn FnMut() -> Result<bool, String>,
) -> Result<(), String> {
    if with_host(|h| h.is_array(&v)) {
        for _ in 0..with_host(|h| h.array_len(&v)) {
            if !step()? {
                break;
            }
        }
        return Ok(());
    }
    if with_host(|h| h.is_generator_val(&v)) {
        gen_foreach_rewind(&v)?;
        while with_host(|h| h.pending_throw.is_none()) && gen_valid(&v)? {
            if !step()? {
                break;
            }
            gen_next(&v)?;
        }
        return Ok(());
    }
    let Some(class) = with_host(|h| h.object_class(&v)) else {
        return Ok(());
    };
    if with_host(|h| h.class_has_method(&class, "getIterator")) {
        let inner = call_method(&class, "getIterator", Some(v.clone()), Vec::new())?;
        return iterator_apply_walk(inner, step);
    }
    if with_host(|h| h.class_has_method(&class, "rewind")) {
        iter_call(&v, "rewind")?;
    }
    loop {
        let valid = iter_call(&v, "valid")?;
        if !with_host(|h| h.is_truthy(&valid)) || !step()? {
            break;
        }
        iter_call(&v, "next")?;
    }
    Ok(())
}

/// Whether the run already displayed a fatal in PHP's own shape, so the CLI
/// wrapper reports only the exit status instead of printing a second, terse copy.
pub fn fatal_reported() -> bool {
    with_host(|h| h.fatal_reported())
}

/// Record a pending `return` value for the enclosing function frame.
pub fn set_return(v: Value) {
    with_host(|h| h.signal = Some(Signal::Return(v)));
}

/// Record a `break`/`continue` control signal for the enclosing `try` body (the
/// orchestrator relays it to the loop the `try` sits inside).
pub fn set_break(level: u32) {
    with_host(|h| h.signal = Some(Signal::Break(level)));
}

pub fn set_continue(level: u32) {
    with_host(|h| h.signal = Some(Signal::Continue(level)));
}

/// The level of the `break`/`continue` that ended the most recent `try` body.
/// Read by the dispatch code the compiler emits after `RUN_TRY`, which needs to
/// know which enclosing loop the signal was aimed at.
pub fn last_break_level() -> u32 {
    with_host(|h| h.last_break_level)
}

/// Record a thrown exception object; the caller's dispatcher then unwinds.
pub fn set_pending_throw(v: Value) {
    with_host(|h| h.pending_throw = Some(v));
}

/// Record the status an `exit`/`die` ended the request with; every frame then
/// unwinds exactly as a throw does.
pub fn set_pending_exit(status: i32) {
    with_host(|h| h.pending_exit = Some(status));
}

/// The status `exit`/`die` ended the run with, if one ran. Read by the CLI
/// wrapper after the program returns, which is where the process status comes
/// from — `exit(3)` must leave the shell a 3, not a 0.
pub fn pending_exit() -> Option<i32> {
    with_host(|h| h.pending_exit)
}

/// Whether the run is unwinding: an exception is in flight, or an `exit`/`die`
/// has ended the request. Both stop every enclosing frame, so every call
/// dispatcher tests this rather than the throw alone — a dispatcher that only
/// looked for the throw would run the statement after an `exit` in a callee.
pub fn unwinding() -> bool {
    with_host(|h| h.pending_throw.is_some() || h.pending_exit.is_some())
}

/// The control status of running one `try`/`catch`/`finally` sub-body.
enum TryStatus {
    Normal,
    /// An `exit`/`die` ran inside the body. Unlike every other status this one
    /// is terminal: no `catch` may claim it and no `finally` runs after it,
    /// which is what the reference does — `try { exit(5); } finally { echo
    /// "fin"; }` prints nothing and exits 5.
    Exit,
    Return(Value),
    Throw(Value),
    Break,
    Continue,
    /// A non-catchable Rust-level fatal (e.g. an undefined function) — carried so
    /// `finally` still runs before it surfaces.
    Fatal(String),
}

/// Run one detached `try`/`catch`/`finally` body on the current scope, then read
/// back whichever control signal it raised (throw > return/break/continue >
/// normal). Signals are *taken* here so the next body runs clean — the value is
/// re-stashed only at the final propagation moment (see `run_try_orchestrator`).
fn run_body(chunk: Chunk) -> TryStatus {
    match run_chunk_on(chunk) {
        Err(e) => TryStatus::Fatal(e),
        Ok(_) => with_host(|h| {
            // The exit status is left in place rather than taken: it is not a
            // signal the orchestrator relays, it is the status the process ends
            // with, and the CLI wrapper reads it after the whole run.
            if h.pending_exit.is_some() {
                return TryStatus::Exit;
            }
            if let Some(exc) = h.pending_throw.take() {
                return TryStatus::Throw(exc);
            }
            match h.signal.take() {
                Some(Signal::Return(v)) => TryStatus::Return(v),
                Some(Signal::Break(n)) => {
                    h.last_break_level = n;
                    TryStatus::Break
                }
                Some(Signal::Continue(n)) => {
                    h.last_break_level = n;
                    TryStatus::Continue
                }
                None => TryStatus::Normal,
            }
        }),
    }
}

/// Orchestrate a `try`/`catch`/`finally` by id (baked into the `RUN_TRY` call).
/// Returns a status code the compiler branches on: `0` normal, `1` return,
/// `2` throw, `3` break, `4` continue. The propagated value is stashed into the
/// matching host signal just before returning, so the parent chunk consumes it
/// immediately. `finally` runs unconditionally and its own non-normal status
/// overrides whatever the `try`/`catch` produced. The whole thing lives on the
/// Rust stack, so nested `try`s recurse cleanly and no signal leaks across
/// unrelated constructs.
pub fn run_try_orchestrator(id: i64) -> Result<i64, String> {
    let Some(def) = with_host(|h| h.try_defs.get(id as usize).cloned()) else {
        return Err(format!("internal: no try-def #{id}"));
    };

    // An `@expr` that throws never reaches its `SUPPRESS_POP`, so the region it
    // opened would stay open and silence everything after the catch. Unwinding
    // out of the expression restores the depth, which is what the reference does
    // when it restores the error-reporting level on the way out.
    let suppress_depth = with_host(|h| h.suppress_depth());
    let mut status = run_body(def.try_chunk);
    with_host(|h| h.suppress_restore(suppress_depth));

    // An `exit` in the body ends the request there: no `catch` is consulted and
    // no `finally` runs. Reported as the same status code a throw uses, so the
    // parent chunk halts — but with nothing stashed for a `catch` to find.
    if matches!(status, TryStatus::Exit) {
        return Ok(2);
    }

    // A thrown exception: try each catch clause in order; the first whose union
    // of class names matches the thrown object's class wins.
    if let TryStatus::Throw(exc) = &status {
        if let Some(class) = with_host(|h| h.object_class(exc)) {
            let exc = exc.clone();
            for c in &def.catches {
                let matched = with_host(|h| c.classes.iter().any(|t| h.catch_matches(&class, t)));
                if matched {
                    if let Some(var) = &c.var {
                        with_host(|h| h.set_var(var, exc.clone()));
                    }
                    status = run_body(c.chunk.clone());
                    break;
                }
            }
        }
    }

    // `finally` always runs; a non-normal status from it replaces the pending one.
    if let Some(fin) = def.finally_chunk {
        match run_body(fin) {
            TryStatus::Normal => {}
            other => status = other,
        }
    }

    Ok(match status {
        TryStatus::Normal => 0,
        // An `exit` in a `catch` or `finally` body — same terminal handling as
        // one in the `try` body above, reached here because those two run after
        // the early return.
        TryStatus::Exit => 2,
        TryStatus::Return(v) => {
            with_host(|h| h.signal = Some(Signal::Return(v)));
            1
        }
        TryStatus::Throw(v) => {
            with_host(|h| h.pending_throw = Some(v));
            2
        }
        TryStatus::Break => 3,
        TryStatus::Continue => 4,
        TryStatus::Fatal(e) => return Err(e),
    })
}

// ── numeric formatting / parsing helpers ──────────────────────────────────

/// Format a float the way PHP's default `precision=14` echo does.
fn float_to_php_string(f: f64) -> String {
    if f.is_nan() {
        return "NAN".into();
    }
    if f.is_infinite() {
        return if f < 0.0 { "-INF" } else { "INF" }.into();
    }
    if f == 0.0 {
        // PHP prints negative zero as "-0".
        return if f.is_sign_negative() { "-0" } else { "0" }.into();
    }
    // PHP's default echo uses `zend_gcvt` with precision 14 (significant digits):
    // fixed notation in the normal range, scientific (`1.5E-10`, `1.0E+100`) once
    // the decimal exponent is < -4 or >= 15.
    php_gcvt(f, 14)
}

/// Format a positive-or-negative float the way PHP renders it — 14 significant
/// digits, `%G`-style choice of fixed vs scientific. Also used by `sprintf`'s
/// `%g` with a caller-supplied precision.
pub fn php_gcvt(f: f64, precision: usize) -> String {
    if f == 0.0 {
        return if f.is_sign_negative() {
            "-0".into()
        } else {
            "0".into()
        };
    }
    let neg = f < 0.0;
    let a = f.abs();
    let exp = a.log10().floor() as i32;
    // `%G` rule: scientific when the exponent is below -4 or reaches the
    // significant-digit precision (PHP's default precision is 14).
    let sci = exp < -4 || exp >= precision as i32;
    let body = if sci {
        // Mantissa with (precision-1) fractional digits, trailing zeros stripped
        // but a decimal point kept (`1.0E+100`); explicit exponent sign.
        let raw = format!("{:.*e}", precision.saturating_sub(1), a);
        let (mant, ex) = raw.split_once('e').unwrap_or((raw.as_str(), "0"));
        let mant = mant.trim_end_matches('0');
        // Keep exactly one fractional digit when the mantissa is integer-valued:
        // "1." → "1.0", "1" → "1.0"; leave "1.844674407371" untouched.
        let mant = if let Some(stripped) = mant.strip_suffix('.') {
            format!("{stripped}.0")
        } else if mant.contains('.') {
            mant.to_string()
        } else {
            format!("{mant}.0")
        };
        let exp_n: i32 = ex.parse().unwrap_or(0);
        format!(
            "{mant}E{}{}",
            if exp_n < 0 { "-" } else { "+" },
            exp_n.abs()
        )
    } else {
        let decimals = (precision as i32 - 1 - exp).max(0) as usize;
        let s = format!("{a:.decimals$}");
        if s.contains('.') {
            s.trim_end_matches('0').trim_end_matches('.').to_string()
        } else {
            s
        }
    };
    if neg {
        format!("-{body}")
    } else {
        body
    }
}

/// The whitespace PHP's numeric-string scanner skips. Deliberately not
/// `str::trim`, which also strips Unicode spaces: PHP reads `"\u{a0}5"` as
/// non-numeric, and trimming NBSP here would silently make it `5`.
const PHP_SPACE: &[char] = &[' ', '\t', '\n', '\r', '\x0b', '\x0c'];

/// Scan the leading numeric run of `s`, returning its value and how many bytes
/// of `s.trim_start_matches(PHP_SPACE)` it consumed, or `None` when the string
/// does not begin with a number at all.
///
/// This is the single source of truth behind all three questions PHP asks about
/// a string in a numeric context — is it numeric, does it merely *start*
/// numeric, and what is the number — so the three can never disagree.
///
/// Two rules are easy to get wrong and are what the byte scan is here for:
/// an exponent counts only when digits actually follow it, so `"5e"` reads as
/// `5` rather than failing; and `"5."` and `".5"` are both complete numbers
/// while a bare `"."` is not.
fn scan_php_number(s: &str) -> Option<(Value, usize, bool)> {
    let t = s.trim_start_matches(PHP_SPACE);
    let b = &*crate::bytes::to_bytes(t);
    let mut i = 0;
    if i < b.len() && (b[i] == b'+' || b[i] == b'-') {
        i += 1;
    }
    let digits_start = i;
    while i < b.len() && b[i].is_ascii_digit() {
        i += 1;
    }
    // `end` is the length of the longest complete number seen so far; 0 means
    // nothing valid has been scanned yet, which is how a bare sign or `.` fails.
    let mut end = if i > digits_start { i } else { 0 };
    let mut is_float = false;
    if i < b.len() && b[i] == b'.' {
        let frac_start = i + 1;
        let mut j = frac_start;
        while j < b.len() && b[j].is_ascii_digit() {
            j += 1;
        }
        // `".5"` needs the fraction digits; `"5."` is already complete without
        // them. A lone `"."` has neither and stays invalid.
        if j > frac_start || end != 0 {
            i = j;
            end = j;
            is_float = true;
        }
    }
    if end == 0 {
        return None;
    }
    if i < b.len() && (b[i] == b'e' || b[i] == b'E') {
        let mut j = i + 1;
        if j < b.len() && (b[j] == b'+' || b[j] == b'-') {
            j += 1;
        }
        let exp_digits = j;
        while j < b.len() && b[j].is_ascii_digit() {
            j += 1;
        }
        if j > exp_digits {
            end = j;
            is_float = true;
        }
    }
    let text = &t[..end];
    // An integer literal too wide for `i64` becomes a float, as PHP does.
    let value = match (is_float, text.parse::<i64>()) {
        (false, Ok(n)) => Value::int(n),
        _ => Value::float(text.parse::<f64>().ok()?),
    };
    Some((value, end, is_float))
}

/// Whether a string's numeric prefix is *written* in float form — it carries a
/// decimal point or an exponent.
///
/// This is not the same question as "did it parse to a float": an integer-format
/// string too wide for `i64` also parses to a float, and PHP treats the two
/// differently when narrowing back to int. `"1e20" % 2` deprecates the
/// narrowing, `"9223372036854775808" % 2` does not.
pub fn numeric_prefix_is_float(s: &str) -> bool {
    scan_php_number(s).is_some_and(|(_, _, is_float)| is_float)
}

/// Parse the leading numeric prefix of a string into an `Int`/`Float` `Value`,
/// as PHP does when a string is used in arithmetic (`"12abc" + 0 == 12`).
/// A string with no numeric prefix at all reads as `0`, matching `(int)"abc"`.
fn parse_php_number(s: &str) -> Value {
    scan_php_number(s).map_or(Value::int(0), |(v, _, _)| v)
}

/// Whether a string is a fully numeric PHP string (for loose comparison).
/// PHP's alphanumeric string succession, the `++` operator on a non-numeric
/// string (`"a"` → `"b"`, `"z"` → `"aa"`, `"Az"` → `"Ba"`, `"a9"` → `"b0"`).
///
/// Carry propagates right to left through `[a-zA-Z0-9]` only: the first
/// non-alphanumeric character stops it outright, leaving the rest of the string
/// untouched (`"a-z"` → `"a-a"`, `"a_"` unchanged). A carry that runs off the
/// front prepends a character of the same class as the one it left — `a`, `A` or
/// `1`.
fn increment_alnum_string(s: &str) -> String {
    let mut b = (*crate::bytes::to_bytes(s)).to_vec();
    let mut i = b.len();
    let mut carry = false;
    while i > 0 {
        i -= 1;
        match b[i] {
            b'z' => {
                b[i] = b'a';
                carry = true;
            }
            b'Z' => {
                b[i] = b'A';
                carry = true;
            }
            b'9' => {
                b[i] = b'0';
                carry = true;
            }
            c if c.is_ascii_alphanumeric() => {
                b[i] = c + 1;
                carry = false;
                break;
            }
            // A non-alphanumeric character ends the succession entirely.
            _ => {
                carry = false;
                break;
            }
        }
        if !carry {
            break;
        }
    }
    if carry {
        let lead = match b.first() {
            Some(b'0') => b'1',
            Some(b'A') => b'A',
            _ => b'a',
        };
        b.insert(0, lead);
    }
    crate::bytes::from_bytes(&b)
}

/// The `zend_long` an `int` parameter receives in coercive mode, without the
/// host borrow: a numeric string converts in full (`" 2"` and `"2.5"` are 2,
/// where `Value::to_int` would parse neither), a float truncates. The
/// parameter's refusals and deprecations are `crate::argtypes`'s, raised
/// before the function runs; a string that got past them unparsed reads its
/// leading number.
pub fn long_of(v: &Value) -> i64 {
    match v {
        Value::Str(s) => parse_php_number_full(s)
            .unwrap_or_else(|| parse_php_number(s))
            .to_int(),
        other => other.to_int(),
    }
}

pub fn is_numeric_string(s: &str) -> bool {
    parse_php_number_full(s).is_some()
}

/// The int key a STRING array key folds to, or `None` when it stays a string.
///
/// PHP folds a key that is written as a CANONICAL decimal integer — no sign
/// but `-`, no leading zeros, no `+`, and inside `i64` — and leaves every other
/// string alone. The shape is checked BEFORE parsing so the common non-numeric
/// key (`"name"`, `"k12"`) is rejected on its first byte, and the old
/// round-trip test `n.to_string() == **s`, which allocated a `String` for every
/// numeric-looking key just to compare it, is gone.
pub fn canonical_int_key(s: &str) -> Option<i64> {
    let b = &*crate::bytes::to_bytes(s);
    let digits = match b.first()? {
        b'-' => &b[1..],
        _ => b,
    };
    // No empty digit run, no leading zero (but "0" itself folds), and "-0" is
    // not canonical — PHP keeps it as the string key it was written as.
    if digits.is_empty() || !digits.iter().all(u8::is_ascii_digit) {
        return None;
    }
    if digits[0] == b'0' && (digits.len() > 1 || b[0] == b'-') {
        return None;
    }
    s.parse::<i64>().ok()
}

/// How PHP reads the subscript of a STRING receiver, `$s[k]`.
pub enum StrOffset {
    /// A byte offset. The flag is set when the key had to be CONVERTED to get
    /// it, which the reference reports as `String offset cast occurred` — a
    /// bool, a null, or ANY float, `$s[1.0]` included.
    At(i64, bool),
    /// No offset exists. A value-context read or a write throws `Cannot access
    /// offset of type <this> on string`; `isset()` and `??` answer quietly.
    Bad,
}

/// Classify a string subscript. See [`StrOffset`].
///
/// An integer key is used as-is. A STRING key is accepted only when it is
/// written as a plain integer — surrounding whitespace, a sign, and leading
/// zeros are all fine (`" 1"`, `"-1"`, `"01"`) — because that is what
/// `ZEND_HANDLE_NUMERIC_STRING` accepts. `"1.5"`, `"1e0"`, and `"x"` are not
/// offsets at all, and used to be read through `Value::to_int`, which answers
/// 0 for each of them and silently returned the FIRST byte.
pub fn classify_string_offset(key: &Value) -> StrOffset {
    match key {
        Value::Int(n) => StrOffset::At(*n, false),
        Value::Bool(b) => StrOffset::At(i64::from(*b), true),
        Value::Undef => StrOffset::At(0, true),
        Value::Float(f) => StrOffset::At(dval_to_lval(*f), true),
        Value::Str(s) => {
            let t = s.trim_matches(PHP_SPACE);
            let digits = t.strip_prefix(['+', '-']).unwrap_or(t);
            match (!digits.is_empty() && digits.bytes().all(|b| b.is_ascii_digit()))
                .then(|| t.parse::<i64>().ok())
                .flatten()
            {
                Some(n) => StrOffset::At(n, false),
                None => StrOffset::Bad,
            }
        }
        _ => StrOffset::Bad,
    }
}

/// `zend_dval_to_lval`: narrow a double to `int` the way PHP does.
///
/// Rust's `as i64` SATURATES, so `(int) 1e19` answered `PHP_INT_MAX`. PHP wraps
/// modulo 2^64 (`zend_dval_to_lval_slow`) and answers -8446744073709551616.
/// `fmod` is exact on IEEE doubles, so the wrap loses nothing the input did not
/// already lack. A non-finite double is 0.
pub fn dval_to_lval(f: f64) -> i64 {
    const TWO_63: f64 = 9223372036854775808.0; // 2^63
    const TWO_64: f64 = 18446744073709551616.0; // 2^64
    if !f.is_finite() {
        return 0;
    }
    if (-TWO_63..TWO_63).contains(&f) {
        return f as i64;
    }
    let wrapped = f.trunc().rem_euclid(TWO_64);
    if wrapped >= TWO_63 {
        (wrapped - TWO_64) as i64
    } else {
        wrapped as i64
    }
}

/// Parse a string that is *entirely* numeric (no trailing garbage), or `None`.
///
/// "Entirely" allows whitespace on both ends (`" 5 "` is numeric in PHP 8) but
/// nothing else. Routing through `scan_php_number` rather than Rust's own
/// float parser is what keeps `"INF"`/`"NAN"` non-numeric — Rust accepts those
/// spellings and PHP does not — while still admitting `"1e400"`, which is a
/// numeric string that happens to overflow to infinity.
pub fn parse_php_number_full(s: &str) -> Option<Value> {
    let t = s.trim_matches(PHP_SPACE);
    let (value, used, _) = scan_php_number(t)?;
    (used == t.len()).then_some(value)
}

/// How an operand reads to PHP 8's arithmetic operators.
///
/// PHP 8 split what PHP 7 did silently into three outcomes, and an operator has
/// to tell them apart *before* it computes anything. See [`classify_arith`].
pub enum ArithOperand {
    /// Converts silently: a number, a bool, null, or a fully numeric string.
    Numeric(Value),
    /// A leading-numeric string such as `"5g"`. The operator raises
    /// `A non-numeric value encountered` and then uses the prefix.
    Leading(Value),
    /// No numeric reading at all — `"g"`, `""`, an array, an object. The
    /// operator raises `TypeError` instead of producing a value.
    Unsupported,
}

/// Classify an operand for PHP 8's arithmetic operators.
///
/// The three-way split is the whole of the PHP 7 → 8 juggling change: `"g" + 9`
/// used to be `9` and is now a `TypeError`, while `"5g" + 1` used to be silent
/// and is now a warning that still yields `6`. Booleans and `null` were left
/// alone by that change and still convert without complaint.
/// Takes no host: every arm is decided by the operand ALONE. It used to take
/// one and ignore it, which cost the caller a thread-local lookup and a
/// `RefCell` borrow of the whole host per arithmetic operand — on the hot path
/// of every `%`, `intdiv`, and bitwise operator in a program.
pub fn classify_arith(v: &Value) -> ArithOperand {
    match v {
        Value::Int(_) | Value::Float(_) => ArithOperand::Numeric(v.clone()),
        Value::Bool(b) => ArithOperand::Numeric(Value::int(*b as i64)),
        Value::Undef => ArithOperand::Numeric(Value::int(0)),
        Value::Str(s) => match parse_php_number_full(s) {
            Some(n) => ArithOperand::Numeric(n),
            // A prefix exists but did not cover the string: warn and use it.
            None => match scan_php_number(s) {
                Some((n, _, _)) => ArithOperand::Leading(n),
                None => ArithOperand::Unsupported,
            },
        },
        // Arrays and objects have no arithmetic reading. `__toString` is not
        // consulted here: the reference reports `Closure + int`, not `string`.
        _ => ArithOperand::Unsupported,
    }
}

/// One of PHP's four type declaration kinds. `class_exists`, `interface_exists`,
/// `trait_exists` and `enum_exists` each answer for exactly one of them.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum TypeKind {
    Class,
    Interface,
    Trait,
    Enum,
}

/// The engine types PHP declares that this runtime implements without a
/// `ClassDef` — the two object kinds that are `PhpObj` variants rather than
/// class instances, and the interfaces the engine defines for user classes to
/// implement. Each entry is `(name, kind, transitive parents, method names) —
/// every name in PHP's own spelling, matched case-insensitively, because
/// `class_implements` reports the spelling and not the lookup key.
///
/// The parents are what `instanceof`/`is_a` walk when the name has no entry in
/// the class table: `$gen instanceof Traversable` is true, and nothing in the
/// class table says so.
///
/// Deliberately NOT here: engine classes this runtime does not implement
/// (`Fiber`). Answering `class_exists` yes for a name
/// that `new` cannot build trades one wrong answer for a worse one.
type BuiltinType = (
    &'static str,
    TypeKind,
    &'static [&'static str],
    &'static [&'static str],
);

const BUILTIN_TYPES: &[BuiltinType] = &[
    (
        "Closure",
        TypeKind::Class,
        &[],
        &["bind", "bindTo", "call", "fromCallable", "getCurrent"],
    ),
    (
        "Generator",
        TypeKind::Class,
        &["Iterator", "Traversable"],
        &[
            "rewind",
            "valid",
            "current",
            "key",
            "next",
            "send",
            "throw",
            "getReturn",
            "__debugInfo",
        ],
    ),
    ("Traversable", TypeKind::Interface, &[], &[]),
    (
        "Iterator",
        TypeKind::Interface,
        &["Traversable"],
        &["current", "next", "key", "valid", "rewind"],
    ),
    (
        "IteratorAggregate",
        TypeKind::Interface,
        &["Traversable"],
        &["getIterator"],
    ),
    ("Countable", TypeKind::Interface, &[], &["count"]),
    (
        "ArrayAccess",
        TypeKind::Interface,
        &[],
        &["offsetExists", "offsetGet", "offsetSet", "offsetUnset"],
    ),
    ("Stringable", TypeKind::Interface, &[], &["__toString"]),
    (
        "JsonSerializable",
        TypeKind::Interface,
        &[],
        &["jsonSerialize"],
    ),
    (
        "Throwable",
        TypeKind::Interface,
        &["Stringable"],
        &[
            "getMessage",
            "getCode",
            "getFile",
            "getLine",
            "getTrace",
            "getPrevious",
            "getTraceAsString",
            "__toString",
        ],
    ),
    ("UnitEnum", TypeKind::Interface, &[], &["cases"]),
    (
        "BackedEnum",
        TypeKind::Interface,
        &["UnitEnum"],
        &["from", "tryFrom", "cases"],
    ),
];

/// The entry for a built-in type name, matched case-insensitively.
fn builtin_type(name: &str) -> Option<&'static BuiltinType> {
    BUILTIN_TYPES
        .iter()
        .find(|(n, ..)| n.eq_ignore_ascii_case(name))
}

/// The kind of a built-in type name, or `None`.
fn builtin_type_kind(name: &str) -> Option<TypeKind> {
    builtin_type(name).map(|(_, k, ..)| *k)
}

/// The transitive ancestors of a built-in type name, in PHP's spelling.
fn builtin_type_parents(name: &str) -> &'static [&'static str] {
    builtin_type(name).map_or(&[][..], |(_, _, p, _)| *p)
}

/// The methods a built-in type declares, in PHP's spelling.
fn builtin_type_methods(name: &str) -> &'static [&'static str] {
    builtin_type(name).map_or(&[][..], |(.., m)| *m)
}

/// The key a constant is registered under (`zend_register_constant`): the
/// namespace part lowercased, the constant's own name as written.
pub fn const_key(name: &str) -> String {
    match name.rsplit_once('\\') {
        Some((ns, short)) => format!("{}\\{short}", ns.to_ascii_lowercase()),
        None => name.to_string(),
    }
}

/// A class name held in a string, without the one leading `\` that anchors
/// it to the global namespace — `zend_lookup_class_ex` accepts `'\A\P'` as
/// `A\P`. A name the source wrote never carries it: the parser resolves those.
pub fn unanchored(name: &str) -> &str {
    name.strip_prefix('\\').unwrap_or(name)
}

/// [`unanchored`] where the stripped name is a declared class, else the name
/// as written — for the lookups whose "not found" message quotes the string
/// with its `\` intact (`$c::m()`, a `"\C::m"` callback).
pub fn anchored_class(name: &str) -> String {
    let bare = unanchored(name);
    if bare.len() != name.len() && with_host(|h| h.class_exists(bare)) {
        bare.to_string()
    } else {
        name.to_string()
    }
}

/// A class name as a *message* shows it: everything up to the first NUL.
///
/// Only an anonymous class has one — its name is
/// `Base@anonymous\0<script>:<line>$<n>`, a single string carrying both a
/// readable head and the suffix that makes it unique. The reference passes that
/// name to its diagnostics and dumpers as a C string, so they print the head
/// alone, while `get_class`/`::class`/`var_export` hand back the whole thing.
/// A name with no NUL — every other class — is returned unchanged.
pub fn display_class(name: &str) -> &str {
    match name.split_once('\0') {
        Some((head, _)) => head,
        None => name,
    }
}

/// The first `final` rule the class `def` breaks against what it inherits, as
/// the reference reports it when it links the class: the message, plus the
/// line to report it at when that is not the declaration's own (an override
/// of a final method is reported at the overriding method). `find` resolves a
/// class or interface name, either case, to its definition.
///
/// The reference's order is kept: a final parent class first, then the
/// properties, the constants and the methods, each in the order the ancestor
/// declared them. Only the NEAREST ancestor declaring a member decides
/// whether it is final, which is also the class the message names.
pub fn final_violation<'a>(
    def: &ClassDef,
    find: impl Fn(&str) -> Option<&'a ClassDef>,
) -> Option<(String, Option<u32>)> {
    // The parent chain, nearest first. A cycle cannot link, but is not this
    // check's to report, so it only stops the walk.
    let mut chain: Vec<&ClassDef> = Vec::new();
    let mut next = def.parent.as_deref();
    while let Some(p) = next {
        let Some(d) = find(p) else { break };
        if chain.iter().any(|c| std::ptr::eq(*c, d)) {
            break;
        }
        chain.push(d);
        next = d.parent.as_deref();
    }
    if let Some(parent) = chain.first() {
        if parent.is_final {
            return Some((
                format!(
                    "Class {} cannot extend final class {}",
                    display_class(&def.name),
                    parent.name
                ),
                None,
            ));
        }
    }

    let mut seen: FxHashSet<&str> = FxHashSet::default();
    for anc in &chain {
        let names = anc.prop_defaults.iter().chain(&anc.static_prop_defaults);
        for (name, _) in names {
            if seen.insert(name)
                && anc.final_props.contains(name)
                && def.prop_vis.contains_key(name)
            {
                return Some((
                    format!("Cannot override final property {}::${name}", anc.name),
                    None,
                ));
            }
        }
    }

    // A redeclared asymmetric property may not narrow the write visibility its
    // ancestor granted, and may not introduce one the ancestor never had.
    let rank = |v: Visibility| match v {
        Visibility::Public => 0,
        Visibility::Protected => 1,
        Visibility::Private => 2,
    };
    let word = |v: Visibility| match v {
        Visibility::Public => "public",
        Visibility::Protected => "protected",
        Visibility::Private => "private",
    };
    for (name, vis) in &def.prop_vis {
        let Some(anc) = chain.iter().find(|a| a.prop_vis.contains_key(name)) else {
            continue;
        };
        let own_set = def.prop_set_vis.get(name).copied();
        match anc.prop_set_vis.get(name).copied() {
            Some(p) if rank(own_set.unwrap_or(*vis)) > rank(p) => {
                return Some((
                    format!(
                        "Set access level of {}::${name} must be {}(set) (as in class {}) or weaker",
                        display_class(&def.name),
                        word(p),
                        anc.name
                    ),
                    None,
                ));
            }
            None if own_set.is_some_and(|s| rank(s) > 0) => {
                return Some((
                    format!(
                        "Set access level of {}::${name} must be omitted (as in class {})",
                        display_class(&def.name),
                        anc.name
                    ),
                    None,
                ));
            }
            _ => {}
        }
    }

    // Constants come from interfaces as well: every interface the class or an
    // ancestor implements, and every interface those extend.
    let mut ifaces: Vec<&ClassDef> = Vec::new();
    let mut pending: Vec<&str> = def
        .interfaces
        .iter()
        .chain(chain.iter().flat_map(|c| &c.interfaces))
        .map(String::as_str)
        .collect();
    let mut i = 0;
    while i < pending.len() {
        if let Some(d) = find(pending[i]) {
            if !ifaces.iter().any(|x| std::ptr::eq(*x, d)) {
                ifaces.push(d);
                pending.extend(d.interfaces.iter().map(String::as_str));
            }
        }
        i += 1;
    }
    seen.clear();
    for anc in chain.iter().chain(&ifaces) {
        for (name, _) in &anc.consts {
            if seen.insert(name)
                && anc.final_consts.contains(name)
                && def.consts.iter().any(|(n, _)| n == name)
            {
                return Some((
                    format!(
                        "{}::{name} cannot override final constant {}::{name}",
                        display_class(&def.name),
                        anc.name
                    ),
                    None,
                ));
            }
        }
    }

    let mut seen: FxHashSet<String> = FxHashSet::default();
    for anc in &chain {
        for (spelled, _) in &anc.method_sites {
            let key = spelled.to_ascii_lowercase();
            if !seen.insert(key.clone()) || !anc.final_methods.contains(&key) {
                continue;
            }
            let own = def
                .method_sites
                .iter()
                .find(|(n, _)| n.eq_ignore_ascii_case(spelled));
            if let Some((own, line)) = own {
                return Some((
                    format!("Cannot override final method {}::{own}()", anc.name),
                    Some(*line),
                ));
            }
        }
    }
    throwable_violation(def, &find, &chain).or_else(|| abstract_violation(def, &find, &chain))
}

/// `zend_implement_throwable`: only `Exception` and `Error` (and what extends
/// them) may implement `Throwable`, directly or through an interface that
/// extends it.
fn throwable_violation<'a>(
    def: &ClassDef,
    find: &impl Fn(&str) -> Option<&'a ClassDef>,
    chain: &[&ClassDef],
) -> Option<(String, Option<u32>)> {
    if def.is_interface || def.is_trait {
        return None;
    }
    let roots = std::iter::once(def).chain(chain.iter().copied());
    if roots
        .clone()
        .any(|c| c.name.eq_ignore_ascii_case("exception") || c.name.eq_ignore_ascii_case("error"))
    {
        return None;
    }
    let mut pending: Vec<String> = roots
        .flat_map(|c| c.interfaces.iter().map(|i| i.to_ascii_lowercase()))
        .collect();
    let mut seen: FxHashSet<String> = FxHashSet::default();
    while let Some(iface) = pending.pop() {
        if iface == "throwable" {
            let name = display_class(&def.name);
            return Some((
                if def.is_enum {
                    format!("Enum {name} cannot implement interface Throwable")
                } else {
                    format!(
                        "Class {name} cannot implement interface Throwable, extend Exception or Error instead"
                    )
                },
                None,
            ));
        }
        if seen.insert(iface.clone()) {
            if let Some(d) = find(&iface) {
                pending.extend(d.interfaces.iter().map(|i| i.to_ascii_lowercase()));
            }
        }
    }
    None
}

/// `zend_verify_abstract_class`: the reference refuses to link a concrete class
/// that still has an abstract method — one it inherits from an abstract parent,
/// or one an interface it implements declares. `chain` is `def`'s parents,
/// nearest first. The names listed are the first three in the order the
/// reference reports them; any more are elided with `...`.
fn abstract_violation<'a>(
    def: &ClassDef,
    find: &impl Fn(&str) -> Option<&'a ClassDef>,
    chain: &[&ClassDef],
) -> Option<(String, Option<u32>)> {
    if def.is_abstract || def.is_interface || def.is_trait {
        return None;
    }
    // The nearest declaration of a name decides whether it is still abstract.
    let mut decided: FxHashSet<String> = FxHashSet::default();
    let mut missing: Vec<String> = Vec::new();
    for anc in std::iter::once(def).chain(chain.iter().copied()) {
        for (spelled, _) in &anc.method_sites {
            let key = spelled.to_ascii_lowercase();
            if decided.insert(key.clone()) && anc.abstract_methods.contains(&key) {
                missing.push(format!("{}::{spelled}", display_class(&anc.name)));
            }
        }
    }
    // Interface methods nothing above declared: the interfaces in the order the
    // class and then its ancestors name them, each followed by its own parents.
    let mut visited: Vec<String> = Vec::new();
    let mut pending: Vec<String> = std::iter::once(def)
        .chain(chain.iter().copied())
        .flat_map(|c| c.interfaces.iter().map(|i| i.to_ascii_lowercase()))
        .rev()
        .collect();
    while let Some(iface) = pending.pop() {
        if visited.contains(&iface) {
            continue;
        }
        visited.push(iface.clone());
        // Implemented by the engine itself.
        if matches!(iface.as_str(), "stringable" | "unitenum" | "backedenum") {
            continue;
        }
        if let Some(d) = find(&iface) {
            for (spelled, _) in &d.method_sites {
                if decided.insert(spelled.to_ascii_lowercase()) {
                    missing.push(format!("{}::{spelled}", display_class(&d.name)));
                }
            }
            pending.extend(d.interfaces.iter().rev().map(|i| i.to_ascii_lowercase()));
        } else if let Some((spelled, ..)) = builtin_type(&iface) {
            for m in builtin_type_methods(&iface) {
                if decided.insert(m.to_ascii_lowercase()) {
                    missing.push(format!("{spelled}::{m}"));
                }
            }
            pending.extend(
                builtin_type_parents(&iface)
                    .iter()
                    .rev()
                    .map(|p| p.to_ascii_lowercase()),
            );
        }
    }
    if missing.is_empty() {
        return None;
    }
    let n = missing.len();
    let plural = if n == 1 { "" } else { "s" };
    let mut listed = missing
        .iter()
        .take(3)
        .cloned()
        .collect::<Vec<_>>()
        .join(", ");
    if n > 3 {
        listed.push_str(", ...");
    }
    let name = display_class(&def.name);
    // An enum and an anonymous class can never be declared abstract, so the
    // reference drops the advice to do so.
    let msg = if def.is_enum {
        format!("Enum {name} must implement {n} abstract method{plural} ({listed})")
    } else if def.name.contains('\0') {
        format!("Class {name} must implement {n} abstract method{plural} ({listed})")
    } else {
        format!(
            "Class {name} contains {n} abstract method{plural} and must therefore be declared \
             abstract or implement the remaining method{plural} ({listed})"
        )
    };
    Some((msg, None))
}

/// The type name PHP prints in `Unsupported operand types: X op Y`.
///
/// These are the short spellings (`int`, `bool`, `null`), not the `gettype`
/// ones ([`PhpHost::type_name`] returns `integer`/`boolean`/`NULL`), and an
/// object contributes its class name rather than the word `object`.
pub fn arith_type_name(h: &PhpHost, v: &Value) -> String {
    match v {
        Value::Undef => "null".into(),
        Value::Bool(_) => "bool".into(),
        Value::Int(_) => "int".into(),
        Value::Float(_) => "float".into(),
        Value::Str(_) => "string".into(),
        Value::Obj(_) if h.is_array(v) => "array".into(),
        Value::Obj(_) if h.is_stream(v) => "resource".into(),
        Value::Obj(_) => h.instance_class(v).unwrap_or_else(|| "object".into()),
        _ => "mixed".into(),
    }
}

// ── include / require / eval ────────────────────────────────────────────────

/// A parameter as a call site needs it: `(name, by_ref, variadic)`.
pub type ParamShape = (String, bool, bool);

/// The `include_path` a failed `include` quotes. phplang searches the working
/// directory and then the including file's directory, and has no PEAR
/// directory to add.
pub const INCLUDE_PATH: &str = ".";

impl PhpHost {
    /// `(lowercased name, [(parameter, by_ref, variadic)])` for every loaded user
    /// function — what a run-time compilation needs to write a by-reference
    /// argument back at a call to a function it did not see declared.
    pub fn user_function_params(&self) -> Vec<(String, Vec<ParamShape>)> {
        self.functions
            .iter()
            .filter(|(_, def)| def.closure_site.is_none())
            .map(|(name, def)| {
                let params = def
                    .params
                    .iter()
                    .map(|p| (p.name.clone(), p.by_ref, p.variadic))
                    .collect();
                (name.clone(), params)
            })
            .collect()
    }

    /// `get_included_files()`: the main script (when it is a file) and every
    /// file loaded since, in load order.
    pub fn included_files(&self) -> &[String] {
        &self.included_files
    }

    /// Where the next run-time compilation continues numbering.
    pub fn set_compile_counters(&mut self, c: crate::compiler::Counters) {
        self.compile_counters = c;
    }

    /// Append a run-time compilation's `try` table; its ids were numbered to
    /// follow the ones already loaded.
    pub fn append_try_defs(&mut self, defs: Vec<TryDef>) {
        self.try_defs.extend(defs);
    }
}

/// Find the file an `include "name"` means: an absolute path as is; `./` and
/// `../` against the working directory; anything else in each `include_path`
/// directory, then in the directory of the file doing the including.
fn resolve_include(name: &str) -> std::path::PathBuf {
    let p = std::path::Path::new(name);
    if p.is_absolute() || name.starts_with("./") || name.starts_with("../") {
        return p.to_path_buf();
    }
    for dir in INCLUDE_PATH.split(':') {
        let candidate = std::path::Path::new(dir).join(name);
        if candidate.is_file() {
            return candidate;
        }
    }
    let here = with_host(|h| h.current_file().to_string());
    if let Some(dir) = std::path::Path::new(&here)
        .parent()
        .filter(|_| here.starts_with('/'))
    {
        let candidate = dir.join(name);
        if candidate.is_file() {
            return candidate;
        }
    }
    p.to_path_buf()
}

/// `spl_autoload($class, $file_extensions)`, the default autoloader: the
/// class name lowercased, `\` turned into `/`, each extension tried along the
/// include path, and the first file found included once
/// (`spl_autoload` in `ext/spl/php_spl.c`).
pub fn spl_autoload(class: &str, extensions: Option<&str>) -> Result<(), String> {
    let exts = match extensions {
        Some(e) => e.to_string(),
        None => with_host(|h| h.autoload_extensions.clone()),
    };
    let stem = unanchored(class).to_ascii_lowercase().replace('\\', "/");
    for ext in exts.split(',') {
        let file = format!("{stem}{ext}");
        if resolve_include(&file).is_file() {
            run_include(crate::ast::IncludeKind::IncludeOnce, &Value::str(file))?;
            return Ok(());
        }
    }
    Ok(())
}

/// `include`/`require`/`include_once`/`require_once`.
///
/// A file that cannot be read warns `Failed to open stream`; `include` then
/// warns `Failed opening … for inclusion` and yields `false`, while `require`
/// throws `Error: Failed opening required …`. A `_once` form yields `true` for a
/// file already loaded. Otherwise the file is compiled and run IN THE CURRENT
/// FRAME, and the construct yields its `return` value, or `1`.
pub fn run_include(kind: crate::ast::IncludeKind, path: &Value) -> Result<Value, String> {
    let name = to_str_ext(path);
    let construct = kind.name();
    if name.is_empty() {
        return Err(crate::builtins::throws_bare(
            "ValueError",
            "Path must not be empty",
        ));
    }
    let target = resolve_include(&name);
    let read = if target.is_dir() {
        // What the reference's `open(2)` + `read(2)` of a directory reports.
        Err(std::io::Error::from_raw_os_error(0))
    } else {
        std::fs::read(&target)
    };
    let src = match read {
        Ok(bytes) => crate::bytes::from_bytes(&bytes),
        Err(e) => {
            let shown = if target.is_dir() {
                std::fs::canonicalize(&target)
                    .map_or_else(|_| name.clone(), |p| p.display().to_string())
            } else {
                name.clone()
            };
            let reason = match e.raw_os_error() {
                Some(0) => "Undefined error: 0".to_string(),
                _ => {
                    let s = e.to_string();
                    s.find(" (os error")
                        .map_or(s.clone(), |i| s[..i].to_string())
                }
            };
            with_host(|h| {
                h.warn(format!(
                    "{construct}({shown}): Failed to open stream: {reason}"
                ))
            });
            if kind.required() {
                return Err(crate::builtins::throws_bare(
                    "Error",
                    format!("Failed opening required '{name}' (include_path='{INCLUDE_PATH}')"),
                ));
            }
            with_host(|h| {
                h.warn(format!(
                    "{construct}(): Failed opening '{name}' for inclusion \
                     (include_path='{INCLUDE_PATH}')"
                ))
            });
            return Ok(Value::bool(false));
        }
    };
    let real = std::fs::canonicalize(&target).map_or_else(
        |_| target.display().to_string(),
        |p| p.display().to_string(),
    );
    let seen = with_host(|h| h.included_files.contains(&real));
    if kind.once() && seen {
        return Ok(Value::bool(true));
    }
    with_host(|h| {
        // The stream the reference read the file through.
        h.consume_resource_id();
        if !seen {
            h.included_files.push(real.clone());
        }
    });
    run_loaded(&src, real.clone(), construct, Some(real), false)
}

/// `eval($code)`: compile and run `$code` in the current frame. The code is
/// named `<file>(<line>) : eval()'d code` wherever a file would be, and yields
/// its `return` value, or null.
pub fn run_eval(code: &Value) -> Result<Value, String> {
    let src = to_str_ext(code);
    let file = with_host(|h| {
        format!(
            "{}({}) : eval()'d code",
            h.current_file(),
            h.cur_frame_line()
        )
    });
    run_loaded(&format!("<?php {src}"), file, "eval", None, true)
}

/// Compile `src` as the file `file` and run it on the current frame.
///
/// The compile runs with the host's script name switched to `file`, so every
/// message the lexer and parser raise — and the `ParseError` a syntax error
/// becomes — names it. A syntax error is thrown as a catchable `ParseError`
/// carrying that file and line, as the reference does.
fn run_loaded(
    src: &str,
    file: String,
    construct: &'static str,
    arg: Option<String>,
    eval: bool,
) -> Result<Value, String> {
    let saved = with_host(|h| std::mem::replace(&mut h.script_name, file.clone()));
    let counters = with_host(|h| h.compile_counters);
    let compiled = crate::parser::parse_meta(src)
        .map_err(|e| (e.severity, e.message))
        .and_then(|(stmts, _meta)| {
            crate::compiler::compile_nested(&stmts, counters, &file).map_err(|m| {
                let m = match m.strip_prefix(crate::compiler::COMPILE_FATAL) {
                    Some(body) => body.to_string(),
                    None => m,
                };
                ("Fatal error", m)
            })
        });
    with_host(|h| h.script_name = saved);
    let prog = match compiled {
        Ok(p) => p,
        Err(("Parse error", msg)) => return throw_parse_error(&msg, &file),
        Err((severity, msg)) => {
            with_host(|h| h.fatal(severity, &msg));
            return Err(format!("{severity}:  {msg}"));
        }
    };
    let crate::compiler::Program {
        main,
        functions,
        classes,
        try_defs,
        diags,
        counters,
        fn_sites,
        class_sites,
        ..
    } = prog;
    let arc: std::sync::Arc<str> = std::sync::Arc::from(file.as_str());
    let call_line = with_host(|h| {
        h.set_compile_counters(counters);
        h.load_program(functions);
        h.load_classes(classes);
        h.append_try_defs(try_defs);
        h.record_decl_sites(&file, fn_sites, class_sites);
        let call_line = h.cur_frame_line();
        h.includes.push(IncludeFrame {
            depth: h.scopes.len() - 1,
            file: arc,
            construct,
            arg,
            call_line,
        });
        for d in &diags {
            h.diagnose(d.severity, d.level, d.line, &d.msg);
        }
        call_line
    });
    let r = run_chunk_on(main);
    with_host(|h| {
        h.includes.pop();
        if let Some(scope) = h.scopes.last_mut() {
            scope.line = call_line;
        }
    });
    r?;
    Ok(with_host(|h| match h.signal.take() {
        Some(Signal::Return(v)) => v,
        other => {
            h.signal = other;
            if eval {
                Value::Undef
            } else {
                Value::int(1)
            }
        }
    }))
}

/// Raise a syntax error in loaded code as the `ParseError` the reference
/// throws: the message without its ` in <file> on line <n>` tail, and the file
/// and line from that tail rather than from where the loading construct ran.
fn throw_parse_error(msg: &str, file: &str) -> Result<Value, String> {
    let tail = format!(" in {file} on line ");
    let (message, line) = match msg.rfind(&tail) {
        Some(i) => (
            msg[..i].to_string(),
            msg[i + tail.len()..].trim().parse::<i64>().unwrap_or(0),
        ),
        None => (msg.to_string(), 0),
    };
    let exc = new_object("ParseError", vec![Value::str(message)])?;
    with_host(|h| {
        h.prop_set(&exc, "file", Value::str(file.to_string()));
        h.prop_set(&exc, "line", Value::int(line));
    });
    set_pending_throw(exc);
    Ok(Value::Undef)
}

impl PhpHost {
    /// Every loaded `trait`, for a run-time compilation whose classes use one.
    pub fn trait_defs(&self) -> Vec<(String, ClassDef)> {
        self.classes
            .iter()
            .filter(|(_, d)| d.is_trait)
            .map(|(k, d)| (k.clone(), d.clone()))
            .collect()
    }
}

/// The word a diagnostic names a declared type's kind with.
fn class_kind_word(def: &ClassDef) -> &'static str {
    if def.is_trait {
        "trait"
    } else if def.is_interface {
        "interface"
    } else if def.is_enum {
        "enum"
    } else {
        "class"
    }
}

/// [`class_kind_word`] for a built-in type.
fn type_kind_word(kind: TypeKind) -> &'static str {
    match kind {
        TypeKind::Class => "class",
        TypeKind::Interface => "interface",
        TypeKind::Trait => "trait",
        TypeKind::Enum => "enum",
    }
}

// ── request shutdown ─────────────────────────────────────────────────────────

impl PhpHost {
    /// The live `set_exception_handler` callable, when one is set and this run
    /// is one the reference would call it for.
    fn user_exception_handler(&self) -> Option<Value> {
        if !self.exception_handler_enabled {
            return None;
        }
        self.exception_handlers
            .last()
            .filter(|v| !matches!(v, Value::Undef))
            .cloned()
    }

    /// `set_exception_handler($callback)`: push it, answering the one it replaces.
    pub fn set_exception_handler(&mut self, callback: Value) -> Value {
        let prev = self
            .exception_handlers
            .last()
            .cloned()
            .unwrap_or(Value::Undef);
        self.exception_handlers.push(callback);
        prev
    }

    /// `restore_exception_handler()`: drop back to the previous handler.
    pub fn restore_exception_handler(&mut self) {
        self.exception_handlers.pop();
    }

    /// `php -r` code never reaches a user exception handler.
    pub fn disable_exception_handler(&mut self) {
        self.exception_handler_enabled = false;
    }

    /// `register_shutdown_function($callback, ...$args)`.
    pub fn register_shutdown_fn(&mut self, callback: Value, args: Vec<Value>) {
        self.shutdown_fns.push((callback, args));
    }

    /// A fatal error that is not an uncaught exception ended the run.
    pub fn disable_destructors(&mut self) {
        self.destructors_off = true;
    }

    /// How many places other than the containers in `freed` hold `handle` —
    /// the reference count the request-end sweep needs, recovered by looking,
    /// since the heap keeps none. A stale handle in a dead temporary counts,
    /// which errs toward leaving an object to the creation-order pass.
    fn handle_refs(&self, handle: u32, freed: &FxHashSet<u32>) -> usize {
        let is = |v: &Value| matches!(v, Value::Obj(o) if *o == handle);
        let in_frame = |s: &Scope| {
            s.vars
                .slots
                .iter()
                .filter(|slot| matches!(slot, Slot::Val(v) if is(v)))
                .count()
        };
        let mut n = self.scopes.iter().map(in_frame).sum::<usize>()
            + self
                .generators
                .iter()
                .flat_map(|g| g.ctx.frames.iter())
                .map(in_frame)
                .sum::<usize>()
            + self.main_frame_slots.iter().filter(|v| is(v)).count()
            + self.ref_cells.iter().filter(|v| is(v)).count()
            + self.static_props.values().filter(|v| is(v)).count()
            + self.constants.values().filter(|v| is(v)).count();
        for (i, o) in self.objs.iter().enumerate() {
            if freed.contains(&(i as u32)) {
                continue;
            }
            n += match o {
                PhpObj::Array { entries, .. } => entries.values().filter(|v| is(v)).count(),
                PhpObj::Object { props, .. } => props.values().filter(|v| is(v)).count(),
                PhpObj::Closure {
                    captured,
                    bound_this,
                    ..
                } => {
                    captured.iter().filter(|(_, v)| is(v)).count()
                        + usize::from(bound_this.as_ref().is_some_and(is))
                }
                _ => 0,
            };
        }
        n
    }

    /// The handles `handle` itself holds: an array's elements, an object's
    /// properties, a closure's captures and bound `$this`.
    fn held_handles(&self, handle: u32) -> Vec<u32> {
        let vals: Vec<&Value> = match self.objs.get(handle as usize) {
            Some(PhpObj::Array { entries, .. }) => entries.values().collect(),
            Some(PhpObj::Object { props, .. }) => props.values().collect(),
            Some(PhpObj::Closure {
                captured,
                bound_this,
                ..
            }) => captured
                .iter()
                .map(|(_, v)| v)
                .chain(bound_this.iter())
                .collect(),
            _ => Vec::new(),
        };
        vals.into_iter()
            .filter_map(|v| match v {
                Value::Obj(o) => Some(*o),
                _ => None,
            })
            .collect()
    }

    /// The global variables in the order the reference's symbol table holds
    /// them, each with the object handle it holds directly (not through a
    /// reference), newest first.
    fn global_objects_newest_first(&self) -> Vec<(String, u32)> {
        let Some(g) = self.scopes.first() else {
            return Vec::new();
        };
        // The main chunk's names first, in the order it names them; then any
        // bound by name only (`extract`, `$$x`, an include), in binding order.
        let mut order: Vec<&String> = self.main_order.iter().collect();
        let mut rest: Vec<(u32, &String)> = g
            .vars
            .index
            .iter()
            .filter(|(k, _)| !self.main_order.contains(k))
            .map(|(k, &i)| (i, k))
            .collect();
        rest.sort_unstable();
        order.extend(rest.into_iter().map(|(_, k)| k));
        order
            .into_iter()
            .rev()
            .filter_map(|k| {
                let v = match self.main_promoted.iter().position(|p| p == k) {
                    Some(i) => self.main_frame_slots.get(i)?,
                    None => match g.vars.get(k) {
                        Slot::Val(v) => v,
                        _ => return None,
                    },
                };
                match v {
                    Value::Obj(o) if self.is_object_value(v) => Some((k.clone(), *o)),
                    _ => None,
                }
            })
            .collect()
    }

    /// Remove the global `name` from wherever it lives.
    fn drop_global(&mut self, name: &str) {
        match self.main_promoted.iter().position(|p| p == name) {
            Some(i) => {
                if let Some(v) = self.main_frame_slots.get_mut(i) {
                    *v = Value::Undef;
                }
            }
            None => {
                if let Some(g) = self.scopes.first_mut() {
                    if let Some(i) = g.vars.slot_of(name) {
                        g.vars.put(i, Slot::Unset);
                    }
                }
            }
        }
    }

    /// Record the main chunk's global layout (see `PhpHost::main_order`).
    pub fn set_main_layout(&mut self, order: Vec<String>, promoted: Vec<String>) {
        self.main_order = order;
        self.main_promoted = promoted;
        self.main_frame_slots.clear();
    }

    /// The class to run `__destruct` on for `handle`, if it still needs running.
    fn pending_destructor(&self, handle: u32) -> Option<String> {
        if self.destructed.contains(&handle) {
            return None;
        }
        match self.objs.get(handle as usize) {
            Some(PhpObj::Object { class, .. }) if self.class_has_method(class, "__destruct") => {
                Some(class.clone())
            }
            _ => None,
        }
    }
}

/// Request shutdown, in `php_request_shutdown`'s order: the
/// `register_shutdown_function` queue (one registered while it runs is run
/// too), then the destructors (see `call_destructors`). An exception either
/// lets escape is reported uncaught and ends the sweep, as a bailout would.
///
/// An `exit` status already set is parked across the sweep, or every callback
/// would see the run unwinding and return at once; one the sweep sets wins.
pub fn request_shutdown(run_ok_or_uncaught: bool) -> Result<(), String> {
    let exit = with_host(|h| h.pending_exit.take());
    // Nothing calls these from PHP source: a trace names their call site
    // `[internal function]`.
    with_host(|h| {
        if let Some(g) = h.scopes.first_mut() {
            g.internal = true;
        }
    });
    let r = run_shutdown_fns().and_then(|()| {
        if run_ok_or_uncaught && !with_host(|h| h.destructors_off) {
            call_destructors()
        } else {
            Ok(())
        }
    });
    with_host(|h| {
        if h.pending_exit.is_none() {
            h.pending_exit = exit;
        }
    });
    r
}

/// Report an exception a shutdown callback or destructor let escape, the way
/// the top level reports one.
fn shutdown_escape() -> Result<(), String> {
    match with_host(|h| h.pending_throw.take()) {
        Some(exc) => Err(report_uncaught(exc)),
        None => Ok(()),
    }
}

fn run_shutdown_fns() -> Result<(), String> {
    let mut i = 0;
    while let Some((callback, args)) = with_host(|h| h.shutdown_fns.get(i).cloned()) {
        i += 1;
        call_value(callback, args)?;
        with_host(|h| h.signal.take());
        shutdown_escape()?;
        if with_host(|h| h.pending_exit.is_some()) {
            break;
        }
    }
    Ok(())
}

/// `zend_call_destructors`, then `zend_objects_store_call_destructors`.
///
/// First the globals that alone hold an object are freed, newest first, until
/// a pass frees none; freeing one runs its `__destruct`, destroys a suspended
/// generator, and frees in turn whatever only it was holding. Every object
/// still alive after that is destructed in creation order — which, for a
/// generator, is the forced `return` that runs its `finally` blocks.
fn call_destructors() -> Result<(), String> {
    let mut freed: FxHashSet<u32> = FxHashSet::default();
    loop {
        let mut any = false;
        for (name, handle) in with_host(|h| h.global_objects_newest_first()) {
            if freed.contains(&handle) || with_host(|h| h.handle_refs(handle, &freed)) != 1 {
                continue;
            }
            with_host(|h| h.drop_global(&name));
            free_object(handle, &mut freed)?;
            any = true;
        }
        if !any {
            break;
        }
    }
    let count = with_host(|h| h.objs.len()) as u32;
    for handle in 0..count {
        if !freed.contains(&handle) {
            destruct_one(handle)?;
        }
    }
    Ok(())
}

/// Free `handle`: destruct it, then free every object it held that nothing
/// else (outside the freed set) still holds.
fn free_object(handle: u32, freed: &mut FxHashSet<u32>) -> Result<(), String> {
    if !freed.insert(handle) {
        return Ok(());
    }
    destruct_one(handle)?;
    for child in with_host(|h| h.held_handles(handle)) {
        if !freed.contains(&child) && with_host(|h| h.handle_refs(child, freed)) == 0 {
            free_object(child, freed)?;
        }
    }
    Ok(())
}

/// Run `handle`'s destructor once: `__destruct` for an instance, the forced
/// `return` for a suspended generator.
fn destruct_one(handle: u32) -> Result<(), String> {
    if let Some(id) = with_host(|h| match h.objs.get(handle as usize) {
        Some(PhpObj::Generator { id }) => Some(*id),
        _ => None,
    }) {
        gen_destroy(id)?;
        return shutdown_escape();
    }
    let Some(class) = with_host(|h| {
        let c = h.pending_destructor(handle);
        if c.is_some() {
            h.destructed.insert(handle);
        }
        c
    }) else {
        return Ok(());
    };
    call_method(&class, "__destruct", Some(Value::Obj(handle)), Vec::new())?;
    with_host(|h| h.signal.take());
    shutdown_escape()
}

// ── set_error_handler ────────────────────────────────────────────────────────

thread_local! {
    /// Whether [`PhpHost::queued_errors`] may be non-empty — read on every
    /// builtin call, so it is a flag beside the host rather than a borrow of it.
    static ERRORS_QUEUED: std::cell::Cell<bool> = const { std::cell::Cell::new(false) };
}

impl PhpHost {
    /// `set_error_handler($callback, $error_levels)`: push it, answering the
    /// callback it replaces.
    pub fn set_error_handler(&mut self, callback: Value, levels: i64) -> Value {
        let prev = self
            .error_handlers
            .last()
            .map(|(c, _)| c.clone())
            .unwrap_or(Value::Undef);
        self.error_handlers.push((callback, levels));
        prev
    }

    /// `restore_error_handler()`: drop back to the previous handler.
    pub fn restore_error_handler(&mut self) {
        self.error_handlers.pop();
    }

    /// `error_reporting()` as PHP code reads it: narrowed to the fatal levels
    /// inside an `@` region.
    pub fn error_reporting_visible(&self) -> i64 {
        if self.suppress > 0 {
            self.error_reporting & errlevel::SILENCED
        } else {
            self.error_reporting
        }
    }
}

/// Hand every queued diagnostic to the live error handler, as
/// `$handler($errno, $errstr, $errfile, $errline)`. A handler that returns
/// `false` gets the default display instead; one that throws stops the drain,
/// and whatever is still queued is dropped with the statement that raised it.
/// Answers whether the caller has to halt its chunk: the handler threw, exited
/// or failed.
///
/// Called at every builtin boundary (before and after the handler runs) and at
/// the end of the main chunk, which is the earliest point the host borrow that
/// raised the diagnostic has been released.
pub fn drain_error_handlers() -> bool {
    if !ERRORS_QUEUED.with(|q| q.get()) {
        return false;
    }
    loop {
        let next = with_host(|h| {
            if h.queued_errors.is_empty() {
                ERRORS_QUEUED.with(|q| q.set(false));
                return None;
            }
            let item = h.queued_errors.remove(0);
            let handler = h.error_handlers.last().map(|(c, _)| c.clone());
            Some((item, handler))
        });
        let Some(((severity, level, msg, file, line), handler)) = next else {
            return false;
        };
        let Some(handler) = handler.filter(|c| !matches!(c, Value::Undef)) else {
            with_host(|h| h.diagnose_default(&severity, level, line, &msg));
            continue;
        };
        let r = invoke_error_handler(handler, level, &msg, file, line);
        if unwinding() {
            with_host(|h| h.queued_errors.clear());
            ERRORS_QUEUED.with(|q| q.set(false));
            return true;
        }
        match r {
            Ok(Value::Bool(false)) => {
                with_host(|h| h.diagnose_default(&severity, level, line, &msg))
            }
            Ok(_) => {}
            Err(e) => {
                with_host(|h| {
                    h.queued_errors.clear();
                    h.set_error(e);
                });
                ERRORS_QUEUED.with(|q| q.set(false));
                return true;
            }
        }
    }
}

/// Call `handler` as the reference calls an error handler:
/// `($errno, $errstr, $errfile, $errline)`, from the line that raised the
/// diagnostic (the call site its trace frame names), with the handler itself
/// unset for the duration so its own diagnostics take the default path.
pub fn invoke_error_handler(
    handler: Value,
    level: i64,
    msg: &str,
    file: String,
    line: u32,
) -> Result<Value, String> {
    with_host(|h| {
        h.in_error_handler = true;
        if let Some(s) = h.scopes.last_mut() {
            s.line = line;
        }
    });
    let args = vec![
        Value::int(level),
        Value::str(msg.to_string()),
        Value::str(file),
        Value::int(i64::from(line)),
    ];
    let r = call_value(handler, args);
    with_host(|h| {
        h.in_error_handler = false;
        h.signal.take();
    });
    r
}

impl PhpHost {
    /// The live error handler, when it takes diagnostics of `level`.
    pub fn error_handler_for(&self, level: i64) -> Option<Value> {
        if self.in_error_handler {
            return None;
        }
        self.error_handlers
            .last()
            .filter(|(c, mask)| !matches!(c, Value::Undef) && mask & level != 0)
            .map(|(c, _)| c.clone())
    }
}

/// Answer a magic method — `__debugInfo`, `__serialize`/`__sleep` — for every
/// object reachable from `roots` whose class defines one of `methods`, keyed by
/// object handle. The first of `methods` the class has is the one called, so
/// `["__serialize", "__sleep"]` gives `__serialize` priority as the reference
/// does.
///
/// A renderer that consults such a method (`var_dump`, `print_r`,
/// `serialize`) runs inside a host borrow and cannot call PHP code, so the
/// reachable values are walked first, depth-first in render order, and each
/// object's answer is collected here. `answer(object, class, method, result)`
/// shapes what the method returned into the value the renderer will use, or
/// answers `None` having left an exception pending. The walk descends into an
/// answer rather than into the object's own properties, as the render will.
/// `None` when a method threw.
pub fn collect_object_hooks(
    roots: &[Value],
    methods: &[&str],
    answer: &dyn Fn(&Value, &str, &str, Value) -> Option<Value>,
) -> Option<FxHashMap<u32, Value>> {
    fn walk(
        v: &Value,
        methods: &[&str],
        answer: &dyn Fn(&Value, &str, &str, Value) -> Option<Value>,
        map: &mut FxHashMap<u32, Value>,
        seen: &mut FxHashSet<u32>,
    ) -> bool {
        let Value::Obj(id) = v else { return true };
        if !seen.insert(*id) {
            return true;
        }
        let hook = with_host(|h| {
            let class = h.object_class(v)?;
            let method = methods.iter().find(|m| h.class_has_method(&class, m))?;
            Some((class, *method))
        });
        // A closure or a generator has no properties to show; what the dumpers
        // print is its engine-built debug info, parked like a `__debugInfo`
        // answer.
        let closure_info = if hook.is_none() && methods.contains(&"__debugInfo") {
            closure_debug_info(v).or_else(|| generator_debug_info(v))
        } else {
            None
        };
        let target = match hook {
            None if closure_info.is_some() => {
                let arr = closure_info.unwrap_or(Value::Undef);
                map.insert(*id, arr.clone());
                arr
            }
            Some((class, method)) => {
                let r = call_method(&class, method, Some(v.clone()), Vec::new());
                if unwinding() {
                    return false;
                }
                let Some(arr) = answer(v, &class, method, r.unwrap_or(Value::Undef)) else {
                    return false;
                };
                map.insert(*id, arr.clone());
                arr
            }
            None => v.clone(),
        };
        let children: Vec<Value> = with_host(|h| {
            if h.is_object(&target) {
                h.object_props(&target)
                    .into_iter()
                    .map(|(_, x)| x)
                    .collect()
            } else {
                h.array_pairs(&target)
                    .unwrap_or_default()
                    .into_iter()
                    .map(|(_, x)| x)
                    .collect()
            }
        });
        children.iter().all(|c| walk(c, methods, answer, map, seen))
    }
    let mut map = FxHashMap::default();
    let mut seen = FxHashSet::default();
    roots
        .iter()
        .all(|r| walk(r, methods, answer, &mut map, &mut seen))
        .then_some(map)
}

/// `__phplang_try_call($fn)`: run the closure and answer `[true, $result]`,
/// or `[false, $exception]` with the exception it threw taken off the stack.
/// The prelude's stand-in for a `try`/`catch (Throwable)`, which prelude
/// source cannot hold — its try-defs are not loaded beside the program's.
pub fn try_call(f: Value) -> Result<Value, String> {
    let r = call_value(f, Vec::new());
    let thrown = with_host(|h| h.pending_throw.take());
    let pair = match thrown {
        Some(exc) => vec![Value::bool(false), exc],
        None => vec![Value::bool(true), r?],
    };
    Ok(with_host(|h| {
        let arr = h.new_array();
        for v in pair {
            h.arr_push_auto(&arr, v);
        }
        arr
    }))
}

/// The line of the innermost frame running USER code, when PHP-written
/// prelude methods sit above it: the call site an internal method's
/// diagnostics name. `None` when the running frame is user code itself.
fn prelude_call_site_line(h: &PhpHost) -> Option<u32> {
    let top = h.scopes.len() - 1;
    let mut i = top;
    while i > 0 && h.scopes[i].name.as_deref().is_some_and(is_prelude_frame) {
        i -= 1;
    }
    (i < top).then(|| h.scopes[i].line)
}

/// Port of `spl_offset_convert_to_ulong` (`ext/spl/spl_fixedarray.c`) for the
/// prelude's `SplFixedArray`: the integer an offset names. An int passes, a
/// canonical integer string converts, a bool is 0/1, and a float goes through
/// `zend_dval_to_lval_safe` — the cast's `not representable` warning, then the
/// `loses precision` deprecation for a fractional value that fits. Anything
/// else is `zend_illegal_container_offset`'s `TypeError`. Diagnostics name the
/// user's call site. A negative result is the caller's out-of-range case.
/// The first character of an INTEGER-keyed property's stored name.
///
/// A property table is a hash table, so it can hold an integer key as well as
/// a string one, and the two are different keys: the only thing that writes
/// one is `ArrayObject`/`ArrayIterator` appending to an object it wraps
/// (`zend_hash_next_index_insert` on the properties). `$o->{"0"}` does not
/// reach `0`, `var_dump` shows `[0]` rather than `["0"]`, and `foreach`,
/// `(array)` and `serialize` read an integer key. `U+10FF00` is an escape
/// character `crate::bytes` never produces (it encodes bytes 0x80..=0xFF as
/// `U+10FF80..`), so no string-named property can begin with it.
pub const INT_PROP_MARK: char = '\u{10FF00}';

/// The stored name of the integer-keyed property `n`.
pub fn int_prop_name(n: i64) -> String {
    format!("{INT_PROP_MARK}{n}")
}

/// The key a property's stored name stands for in an array or a `foreach`:
/// an integer for an integer-keyed one, else the name.
pub fn prop_key(name: &str) -> Value {
    match name
        .strip_prefix(INT_PROP_MARK)
        .and_then(|n| n.parse().ok())
    {
        Some(n) => Value::int(n),
        None => Value::str(name.to_string()),
    }
}

/// `ArrayObject::append` / `$ao[] = v` on a wrapped object: the value under the
/// next integer key its property table leaves free.
pub fn obj_append(obj: &Value, val: Value) {
    with_host(|h| {
        let next = h
            .object_props(obj)
            .iter()
            .filter_map(|(n, _)| match prop_key(n) {
                Value::Int(i) => Some(i + 1),
                _ => None,
            })
            .max()
            .unwrap_or(0);
        h.obj_put_prop(obj, &int_prop_name(next), val);
    });
}

/// The prelude's `ArrayObject` sort over a wrapped object: its property table
/// becomes `table` — an `(array)`-shaped array, so an integer key is an
/// integer-keyed property and a mangled name the property it mangles — in
/// exactly that order.
pub fn obj_replace_table(obj: &Value, table: &Value) {
    with_host(|h| {
        let pairs = h.array_pairs(table).unwrap_or_default();
        let props: IndexMap<String, Value> = pairs
            .into_iter()
            .map(|(k, v)| {
                let name = match k {
                    Value::Int(n) => int_prop_name(n),
                    k => {
                        let s = h.to_str(&k);
                        match s.strip_prefix('\0').and_then(|r| r.split_once('\0')) {
                            Some((_, name)) => name.to_string(),
                            None => s,
                        }
                    }
                };
                (name, v)
            })
            .collect();
        if let Some(PhpObj::Object { props: p, .. }) = h.as_array_mut(obj) {
            *p = props;
        }
    });
}

pub fn spl_offset(offset: &Value) -> Result<Value, String> {
    let v = with_host(|h| h.deref(offset.clone()));
    let n = match &v {
        Value::Int(n) => Some(*n),
        Value::Str(s) => canonical_int_key(s),
        Value::Bool(b) => Some(*b as i64),
        Value::Float(f) => {
            let f = *f;
            let saved = warn_line();
            if let Some(line) = with_host(|h| prelude_call_site_line(h)) {
                set_warn_line(line);
            }
            let l = with_host(|h| {
                let l = h.to_int_cast(&v);
                const TWO_63: f64 = 9223372036854775808.0;
                let fits = !(f >= TWO_63 || f < -TWO_63);
                if l as f64 != f && fits {
                    let shown = crate::stdlib::types::serialize_float(f);
                    h.deprecated(format!(
                        "Implicit conversion from float {shown} to int loses precision"
                    ));
                }
                l
            });
            set_warn_line(saved);
            Some(l)
        }
        _ => None,
    };
    match n {
        Some(n) => Ok(Value::int(n)),
        None => {
            let ty = with_host(|h| crate::stdlib::types::debug_type(h, &v));
            throw_as_internal_method(
                "TypeError",
                &format!("Cannot access offset of type {ty} on SplFixedArray"),
            )
        }
    }
}

/// Raise `class(message)` from a PHP-written prelude method as the reference
/// raises it from an internal one: the method's own frame is the top of the
/// trace (`#0 file(3): DateTime->__construct('x')`), and the exception's file
/// and line are its CALL SITE's — an internal method has no line of its own,
/// while the prelude method's frame is executing prelude source.
pub fn throw_as_internal_method(class: &str, message: &str) -> Result<Value, String> {
    let exc = new_object(class, vec![Value::str(message.to_string())])?;
    with_host(|h| {
        let n = h.scopes.len();
        if n >= 2 {
            let line = h.scopes[n - 2].line;
            let file = h.scope_file(n - 2).to_string();
            h.prop_set(&exc, "line", Value::int(line as i64));
            h.prop_set(&exc, "file", Value::str(file));
        }
    });
    set_pending_throw(exc);
    Ok(Value::Undef)
}

/// `__phplang_warn`: an E_WARNING raised by prelude code, at the line of the
/// innermost frame running user code — where the reference, whose method is
/// internal, reports it.
pub fn warn_from_prelude(message: &str) {
    let saved = warn_line();
    if let Some(line) = with_host(|h| prelude_call_site_line(h)) {
        set_warn_line(line);
    }
    with_host(|h| h.warn(message));
    set_warn_line(saved);
}

/// [`warn_from_prelude`] for an `E_DEPRECATED`.
pub fn deprecated_from_prelude(message: &str) {
    let saved = warn_line();
    if let Some(line) = with_host(|h| prelude_call_site_line(h)) {
        set_warn_line(line);
    }
    with_host(|h| h.deprecated(message));
    set_warn_line(saved);
}

/// Raise a warning from a PHP-written prelude method at its CALL SITE — see
/// [`throw_as_internal_method`].
pub fn warn_as_internal_method(message: &str) {
    let caller = with_host(|h| {
        let n = h.scopes.len();
        (n >= 2).then(|| h.scopes[n - 2].line)
    });
    let saved = warn_line();
    if let Some(line) = caller {
        set_warn_line(line);
    }
    with_host(|h| h.warn(message));
    set_warn_line(saved);
}

/// The classes a declared type's `self`, `parent` and `static` stand for.
#[derive(Debug, Clone, Default)]
pub struct TypeScope {
    pub self_class: Option<String>,
    pub parent: Option<String>,
    pub static_class: Option<String>,
}

impl TypeScope {
    /// The scope of the function a frame name (`Class::method`) belongs to.
    pub fn of_frame(frame: &str, static_class: Option<String>) -> TypeScope {
        let self_class = frame.split_once("::").map(|(c, _)| c.to_string());
        let parent = self_class
            .as_ref()
            .and_then(|c| with_host(|h| h.class_parent(c)));
        TypeScope {
            self_class,
            parent,
            static_class,
        }
    }
}

/// Whether `v` satisfies one alternative of a declared type as it stands —
/// the `zend_check_type_slow` half that converts nothing.
fn type_part_accepts(v: &Value, part: &str, scope: &TypeScope) -> bool {
    let lower = part.to_ascii_lowercase();
    if lower == "mixed" {
        return true;
    }
    let (is_array, obj_class, is_closure) = with_host(|h| {
        let arr = h.is_array(v);
        let cls = if arr { None } else { h.instance_class(v) };
        (arr, cls, h.is_closure(v))
    });
    let instance_of = |class: &str, target: &str| {
        with_host(|h| h.is_a_class(&class.to_ascii_lowercase(), &target.to_ascii_lowercase()))
    };
    match (v, lower.as_str()) {
        (Value::Undef, "null") => true,
        (Value::Bool(_), "bool") => true,
        (Value::Bool(true), "true") | (Value::Bool(false), "false") => true,
        (Value::Int(_), "int") | (Value::Float(_), "float") | (Value::Str(_), "string") => true,
        (Value::Str(_), "callable") => crate::stdlib::callable::callable_reason(v).is_none(),
        (Value::Obj(_), _) if is_array => match lower.as_str() {
            "array" | "iterable" => true,
            "callable" => crate::stdlib::callable::callable_reason(v).is_none(),
            _ => false,
        },
        (Value::Obj(_), _) => {
            let Some(class) = obj_class else { return false };
            match lower.as_str() {
                "object" => true,
                "iterable" => instance_of(&class, "Traversable"),
                "callable" => is_closure || crate::stdlib::callable::callable_reason(v).is_none(),
                "self" => scope
                    .self_class
                    .as_deref()
                    .is_some_and(|s| instance_of(&class, s)),
                "parent" => scope
                    .parent
                    .as_deref()
                    .is_some_and(|s| instance_of(&class, s)),
                "static" => scope
                    .static_class
                    .as_deref()
                    .or(scope.self_class.as_deref())
                    .is_some_and(|s| instance_of(&class, s)),
                "array" | "int" | "float" | "string" | "bool" | "null" | "false" | "true"
                | "void" | "never" => false,
                _ => part
                    .split('&')
                    .all(|t| instance_of(&class, t.trim_start_matches('\\'))),
            }
        }
        _ => false,
    }
}

/// `zend_verify_weak_scalar_type_hint`: the coercive conversions a union
/// tries, in its order — int, float, string, bool.
fn weak_scalar(h: &mut PhpHost, v: &Value, has: &dyn Fn(&str) -> bool) -> Option<Value> {
    fn fits(f: f64) -> bool {
        f.is_finite() && (-9223372036854775808.0..9223372036854775808.0).contains(&f)
    }
    if has("int") {
        if has("float") && matches!(v, Value::Str(_)) {
            if let Some(n) = parse_php_number_full(&h.to_str(v)) {
                return Some(n);
            }
        } else {
            // `zend_parse_arg_long_weak`.
            let long = match v {
                Value::Float(f) if f.is_nan() || !fits(*f) => None,
                Value::Float(f) => {
                    if f.fract() != 0.0 {
                        let shown = crate::stdlib::types::serialize_float(*f);
                        h.deprecated(format!(
                            "Implicit conversion from float {shown} to int loses precision"
                        ));
                    }
                    Some(*f as i64)
                }
                Value::Str(s) => match parse_php_number_full(s) {
                    Some(Value::Int(i)) => Some(i),
                    Some(n) => {
                        let f = n.to_float();
                        if !fits(f) {
                            None
                        } else {
                            if f.fract() != 0.0 {
                                h.deprecated(format!(
                                    "Implicit conversion from float-string \"{s}\" to int loses precision"
                                ));
                            }
                            Some(f as i64)
                        }
                    }
                    None => None,
                },
                Value::Bool(b) => Some(i64::from(*b)),
                _ => None,
            };
            if let Some(l) = long {
                return Some(Value::Int(l));
            }
        }
    }
    if has("float") {
        let d = match v {
            Value::Int(i) => Some(*i as f64),
            Value::Str(s) => parse_php_number_full(s).map(|n| n.to_float()),
            Value::Bool(b) => Some(f64::from(u8::from(*b))),
            _ => None,
        };
        if let Some(d) = d {
            return Some(Value::Float(d));
        }
    }
    // PHP 8.5's weak string and bool parses warn on a NaN: the value converts,
    // but `"NAN"`/`true` do not read back as the number it was.
    if has("string") && matches!(v, Value::Int(_) | Value::Float(_) | Value::Bool(_)) {
        return Some(Value::str(h.to_str_diag(v)));
    }
    if has("bool") && matches!(v, Value::Int(_) | Value::Float(_) | Value::Str(_)) {
        if matches!(v, Value::Float(f) if f.is_nan()) {
            h.warn("unexpected NAN value was coerced to bool");
        }
        return Some(Value::bool(h.is_truthy(v)));
    }
    None
}

/// `zend_check_type` + `zend_verify_scalar_type_hint`: `v` against a declared
/// type — classes (with `self`/`parent`/`static`), `array`, `callable`,
/// `iterable`, `object`, `mixed`, `true`/`false`/`null`, unions and
/// intersections. A value that does not satisfy it as it stands is converted
/// when the mode is coercive and a scalar alternative takes it, in the
/// reference's order; under `strict_types` only int widens to float. `Err`
/// carries the type the value had, for the `TypeError`.
pub fn verify_type(v: Value, ty: &TypeHint, scope: &TypeScope) -> Result<Value, String> {
    if ty.parts.iter().any(|p| type_part_accepts(&v, p, scope)) {
        return Ok(v);
    }
    let has = |want: &str| ty.parts.iter().any(|p| p.eq_ignore_ascii_case(want));
    let strict = with_host(|h| h.strict_types);
    if strict {
        if let (Value::Int(i), true) = (&v, has("float")) {
            return Ok(Value::Float(*i as f64));
        }
        return Err(with_host(|h| h.type_name_for_error(&v)));
    }
    // An object reaches a `string` alternative through `__toString`, the one
    // conversion `zend_parse_arg_str_weak` makes for something not scalar.
    if has("string") && with_host(|h| h.is_object(&v)) {
        let stringable = with_host(|h| {
            h.object_class(&v)
                .is_some_and(|c| h.class_has_method(&c.to_ascii_lowercase(), "__tostring"))
        });
        if stringable {
            return Ok(Value::str(to_str_ext(&v)));
        }
    }
    with_host(|h| match weak_scalar(h, &v, &has) {
        Some(converted) => Ok(converted),
        None => Err(h.type_name_for_error(&v)),
    })
}

/// A declared type as the reference prints it in a `TypeError` —
/// `zend_type_to_string`, with `self`/`parent` resolved against the frame's
/// class.
fn render_declared(ty: &TypeHint, frame: &str, static_class: Option<&str>) -> String {
    let (self_class, parent) = match frame.split_once("::") {
        Some((c, _)) => with_host(|h| {
            let shown = h.class_display_name(&c.to_ascii_lowercase());
            let parent = h
                .class_parent(c)
                .map(|p| h.class_display_name(&p.to_ascii_lowercase()));
            (shown, parent)
        }),
        None => ("self".to_string(), None),
    };
    // `static` is reported as the class it stood for in the call.
    let mut ty = ty.clone();
    if let Some(s) = static_class {
        let shown = with_host(|h| h.class_display_name(&s.to_ascii_lowercase()));
        for p in &mut ty.parts {
            if p.eq_ignore_ascii_case("static") {
                *p = shown.clone();
            }
        }
    }
    ty.declared(&self_class, parent.as_deref())
}

/// The callee a call-time refusal is raised for — see [`with_callee`].
#[derive(Clone)]
struct CalleeCtx {
    this: Option<Value>,
    site: Option<DeclSite>,
}

thread_local! {
    static CALLEE: RefCell<Option<CalleeCtx>> = const { RefCell::new(None) };
}

/// Run a call's argument or return check with its callee recorded, so a
/// refusal's frame reads `A->m(...)` / `{closure:f.php:3}(...)` as the
/// reference's does.
fn with_callee<R>(ctx: &CalleeCtx, f: impl FnOnce() -> R) -> R {
    let saved = CALLEE.with(|c| c.replace(Some(ctx.clone())));
    let r = f();
    CALLEE.with(|c| *c.borrow_mut() = saved);
    r
}

/// The callee's name as a refusal's message spells it: `A::m`, `f`, or
/// `{closure:f.php:3}` (class-prefixed with `::` for a closure in a class).
fn callee_shown(frame: &str) -> String {
    let site = CALLEE.with(|c| c.borrow().as_ref().and_then(|x| x.site.clone()));
    let Some(DeclSite::Closure(inner, line)) = site else {
        return display_frame(frame);
    };
    let script = with_host(|h| h.script_name().to_string());
    let rendered = format!("{{closure:{}:{line}}}", inner.render(&script));
    match frame.split_once("::") {
        Some((class, _)) => {
            let shown = with_host(|h| h.class_display_name(&class.to_ascii_lowercase()));
            format!("{shown}::{rendered}")
        }
        None => rendered,
    }
}

/// [`throw_from_internal`] for a refusal of a USER function's call: the pushed
/// frame carries the callee's `$this` and closure site.
fn throw_from_callee(
    func: &str,
    args: &[Value],
    class: &str,
    message: &str,
) -> Result<Value, String> {
    let ctx = CALLEE.with(|c| c.borrow().clone());
    let Some(ctx) = ctx else {
        return throw_from_internal(func, args, class, message);
    };
    with_host(|h| {
        let line = h.cur_frame_line();
        h.scopes.push(Scope {
            name: Some(func.to_string()),
            line,
            internal: true,
            closure_site: ctx.site.clone(),
            ..Scope::default()
        });
        let argsarr = h.new_array();
        for a in args {
            h.arr_push_auto(&argsarr, a.clone());
        }
        h.set_var("@args", argsarr);
        if let Some(this) = &ctx.this {
            h.set_var("this", this.clone());
        }
    });
    let exc = new_object(class, vec![Value::str(message.to_string())]);
    with_host(|h| {
        h.scopes.pop();
    });
    set_pending_throw(exc?);
    Ok(Value::Undef)
}

/// Whether a prelude frame named `frame` is one of the prelude's own helpers
/// rather than a method of the class it stands in for: a class whose name
/// starts with `__`, or a `__`-named method that is no PHP magic method. A
/// prelude `__call` is a helper too: it forwards to an inner iterator the way
/// `spl_dual_it_get_method` does, which the reference does with no frame.
fn is_prelude_helper_name(frame: &str) -> bool {
    const MAGIC: &[&str] = &[
        "__construct",
        "__destruct",
        "__callstatic",
        "__get",
        "__set",
        "__isset",
        "__unset",
        "__tostring",
        "__invoke",
        "__set_state",
        "__clone",
        "__debuginfo",
        "__serialize",
        "__unserialize",
        "__sleep",
        "__wakeup",
    ];
    let Some((class, method)) = frame.split_once("::") else {
        return false;
    };
    class.starts_with("__")
        || (method.starts_with("__") && !MAGIC.contains(&method.to_ascii_lowercase().as_str()))
}

/// `get_deprecation_suffix_from_attribute`: ` since <since>`, then
/// `, <message>`, each only when the attribute gave it.
fn deprecation_suffix(d: &crate::ast::Deprecation) -> String {
    let mut suffix = String::new();
    if let Some(since) = &d.since {
        suffix.push_str(&format!(" since {since}"));
    }
    if let Some(message) = &d.message {
        suffix.push_str(&format!(", {message}"));
    }
    suffix
}

/// `zend_deprecated_class_constant`: a use of a class constant or enum case
/// declared `#[\Deprecated]` raises `Constant K::C is deprecated` (`Enum case
/// E::A …`), naming the DECLARING class, before the value is produced. A user
/// class's is `E_USER_DEPRECATED`; a prelude class stands in for an internal
/// one, whose is `E_DEPRECATED`. The handler runs now; `true` when it threw.
fn raise_deprecated_member(declaring: &str, key: &str) -> bool {
    let Some((class, d)) = with_host(|h| {
        let def = h.classes.get(declaring)?;
        let (_, d) = def.member_deprecated.iter().find(|(k, _)| k == key)?;
        Some((h.class_display_name(declaring), d.clone()))
    }) else {
        return false;
    };
    let (kind, name) = match key.split_once(':') {
        Some(("case", name)) => ("Enum case", name),
        Some((_, name)) => ("Constant", name),
        None => ("Constant", key),
    };
    let msg = format!(
        "{kind} {class}::{name} is deprecated{}",
        deprecation_suffix(&d)
    );
    let level = if crate::prelude_type(declaring).is_some() {
        errlevel::E_DEPRECATED
    } else {
        errlevel::E_USER_DEPRECATED
    };
    with_host(|h| h.diagnose("Deprecated", level, warn_line(), msg));
    drain_error_handlers();
    with_host(|h| h.pending_throw.is_some())
}

/// `zend_deprecated_function`: `Method K::m() is deprecated` or `Function f()
/// is deprecated`, then ` since <since>` and `, <message>` from the attribute.
/// A user function's is `E_USER_DEPRECATED`; a prelude method stands in for an
/// internal one, whose is `E_DEPRECATED`.
fn raise_deprecated_call(frame: &str, d: &crate::ast::Deprecation) {
    let kind = if frame.contains("::") {
        "Method"
    } else {
        "Function"
    };
    let msg = format!(
        "{kind} {}() is deprecated{}",
        display_frame(frame),
        deprecation_suffix(d)
    );
    let level = if is_prelude_frame(frame) {
        errlevel::E_DEPRECATED
    } else {
        errlevel::E_USER_DEPRECATED
    };
    with_host(|h| h.diagnose("Deprecated", level, warn_line(), msg));
}

// ── reflection metadata ─────────────────────────────────────────────────────
//
// What the PHP-written `Reflection*` classes of the prelude ask the engine
// for, through `__phplang_reflect`. Names come back in declared spelling and
// in the order the reference's tables list them.

/// `ReflectionMethod::IS_*` / `ReflectionProperty::IS_*` modifier bits.
pub mod refl_flags {
    pub const PUBLIC: i64 = 1;
    pub const PROTECTED: i64 = 2;
    pub const PRIVATE: i64 = 4;
    pub const STATIC: i64 = 16;
    pub const FINAL: i64 = 32;
    pub const ABSTRACT: i64 = 64;
    pub const READONLY: i64 = 128;
    pub const PUBLIC_SET: i64 = 1024;
    pub const PROTECTED_SET: i64 = 2048;
    pub const PRIVATE_SET: i64 = 4096;
}

/// A class as reflection describes it.
pub struct ReflClass {
    pub name: String,
    pub is_readonly: bool,
    pub enum_backing: Option<String>,
    pub parent: Option<String>,
    pub interfaces: Vec<String>,
    pub traits: Vec<String>,
    pub is_interface: bool,
    pub is_trait: bool,
    pub is_enum: bool,
    pub is_abstract: bool,
    pub is_final: bool,
    /// Built into the engine (or its PHP-written prelude) rather than declared
    /// by the program.
    pub internal: bool,
    pub anonymous: bool,
}

/// A method or property as reflection describes it.
pub struct ReflMember {
    pub name: String,
    /// The declaring class, in declared spelling.
    pub class: String,
    pub modifiers: i64,
    /// The declared type (a method's return type), as reflection spells it.
    pub ty: Option<crate::ast::TypeHint>,
    pub has_default: bool,
}

fn vis_bits(v: Option<&Visibility>) -> i64 {
    match v {
        Some(Visibility::Private) => refl_flags::PRIVATE,
        Some(Visibility::Protected) => refl_flags::PROTECTED,
        _ => refl_flags::PUBLIC,
    }
}

impl PhpHost {
    /// The chain `class`, its parent, … as `(lowercased, def)`.
    fn refl_chain(&self, class: &str) -> Vec<(String, &ClassDef)> {
        let mut out = Vec::new();
        let mut cur = Some(unanchored(class).to_ascii_lowercase());
        while let Some(c) = cur {
            let Some(def) = self.classes.get(&c) else {
                break;
            };
            cur = def.parent.as_ref().map(|p| p.to_ascii_lowercase());
            out.push((c, def));
        }
        out
    }

    /// `new ReflectionClass($name)`'s view of a class, interface, trait or
    /// enum, or `None` when there is no such type.
    pub fn refl_class(&self, name: &str) -> Option<ReflClass> {
        let lname = unanchored(name).to_ascii_lowercase();
        if let Some(def) = self.classes.get(&lname) {
            return Some(ReflClass {
                name: def.name.clone(),
                is_readonly: def.is_readonly,
                enum_backing: def.enum_backing.clone(),
                parent: def
                    .parent
                    .as_ref()
                    .map(|p| self.class_display_name(&p.to_ascii_lowercase())),
                interfaces: self.class_interface_names(&lname),
                traits: def.uses.clone(),
                is_interface: def.is_interface,
                is_trait: def.is_trait,
                is_enum: def.is_enum,
                is_abstract: def.is_abstract || def.is_interface,
                is_final: def.is_final || def.is_enum,
                internal: crate::prelude_type(&lname).is_some(),
                anonymous: def.name.contains("@anonymous"),
            });
        }
        let (spelled, kind, parents, _) = builtin_type(&lname)?;
        Some(ReflClass {
            name: spelled.to_string(),
            is_readonly: false,
            enum_backing: None,
            parent: None,
            interfaces: parents.iter().map(|p| p.to_string()).collect(),
            traits: Vec::new(),
            is_interface: *kind == TypeKind::Interface,
            is_trait: false,
            is_enum: false,
            is_abstract: *kind == TypeKind::Interface,
            is_final: matches!(lname.as_str(), "closure" | "generator"),
            internal: true,
            anonymous: false,
        })
    }

    /// One method of `def` (declared in the class keyed `c`) as reflection
    /// describes it, by lowercased name.
    fn refl_method_of(&self, c: &str, def: &ClassDef, lname: &str) -> Option<ReflMember> {
        // An enum's `cases`/`from`/`tryFrom` are the engine's, public static.
        if def.is_enum && !def.methods.contains_key(lname) {
            let backed = def
                .interfaces
                .iter()
                .any(|i| i.eq_ignore_ascii_case("BackedEnum"));
            let spelled = match lname {
                "cases" => "cases",
                "from" if backed => "from",
                "tryfrom" if backed => "tryFrom",
                _ => return None,
            };
            let ret = match spelled {
                "cases" => vec!["array".to_string()],
                "from" => vec!["static".to_string()],
                _ => vec!["static".to_string(), "null".to_string()],
            };
            return Some(ReflMember {
                name: spelled.to_string(),
                class: self.declared_class_name(c),
                modifiers: refl_flags::PUBLIC | refl_flags::STATIC,
                ty: Some(crate::ast::TypeHint { parts: ret }),
                has_default: false,
            });
        }
        let f = def.methods.get(lname)?;
        let spelled = def
            .method_sites
            .iter()
            .find(|(n, _)| n.eq_ignore_ascii_case(lname))
            .map_or_else(|| lname.to_string(), |(n, _)| n.clone());
        let mut modifiers = vis_bits(def.method_vis.get(lname));
        if def.static_methods.contains(lname) {
            modifiers |= refl_flags::STATIC;
        }
        if def.final_methods.contains(lname) {
            modifiers |= refl_flags::FINAL;
        }
        if def.abstract_methods.contains(lname) {
            modifiers |= refl_flags::ABSTRACT;
        }
        Some(ReflMember {
            name: spelled,
            class: self.declared_class_name(c),
            modifiers,
            ty: f.ret.clone(),
            has_default: false,
        })
    }

    /// The methods `ReflectionClass::getMethods()` lists: the class's own (a
    /// trait's included) in declaration order, then each ancestor's that are
    /// not overridden — an ancestor's private ones are not inherited.
    pub fn refl_methods(&self, class: &str) -> Vec<ReflMember> {
        let mut seen: Vec<String> = Vec::new();
        let mut out = Vec::new();
        for (depth, (c, def)) in self.refl_method_chain(class).into_iter().enumerate() {
            let mut names: Vec<String> = def.method_sites.iter().map(|(n, _)| n.clone()).collect();
            if def.is_enum {
                names.extend(["cases", "from", "tryFrom"].map(String::from));
            }
            for name in &names {
                let lname = name.to_ascii_lowercase();
                if seen.contains(&lname)
                    || (crate::prelude_type(&c).is_some()
                        && is_prelude_helper_name(&format!("{c}::{name}")))
                {
                    continue;
                }
                let Some(m) = self.refl_method_of(&c, def, &lname) else {
                    continue;
                };
                if depth > 0 && m.modifiers & refl_flags::PRIVATE != 0 {
                    continue;
                }
                seen.push(lname);
                out.push(m);
            }
        }
        out
    }

    /// `new ReflectionMethod($class, $name)`: the nearest declaration along
    /// the chain, an ancestor's private one included.
    pub fn refl_method(&self, class: &str, name: &str) -> Option<ReflMember> {
        let lname = name.to_ascii_lowercase();
        self.refl_method_chain(class)
            .into_iter()
            .find_map(|(c, def)| self.refl_method_of(&c, def, &lname))
    }

    /// Where a method lookup walks: the class and its ancestors, then the
    /// interfaces, whose abstract methods an abstract class inherits.
    fn refl_method_chain(&self, class: &str) -> Vec<(String, &ClassDef)> {
        let mut out = self.refl_chain(class);
        for i in self.class_interface_names(&unanchored(class).to_ascii_lowercase()) {
            let li = i.to_ascii_lowercase();
            if let Some(def) = self.classes.get(&li) {
                if !out.iter().any(|(c, _)| *c == li) {
                    out.push((li, def));
                }
            }
        }
        out
    }

    /// The parameters of method `name` along `class`'s chain.
    pub fn refl_method_params(&self, class: &str, name: &str) -> Option<(String, Vec<Param>)> {
        let lname = name.to_ascii_lowercase();
        self.refl_chain(class).into_iter().find_map(|(c, def)| {
            def.methods
                .get(&lname)
                .map(|f| (self.class_display_name(&c), f.params.clone()))
                .or_else(|| {
                    // A backed enum's engine `from` / `tryFrom` take one
                    // `string|int $value`; its `cases` takes none.
                    let synthetic =
                        def.is_enum && matches!(lname.as_str(), "cases" | "from" | "tryfrom");
                    synthetic.then(|| {
                        let params = if lname == "cases" {
                            Vec::new()
                        } else {
                            vec![Param {
                                name: "value".to_string(),
                                line: 0,
                                default: None,
                                default_text: None,
                                variadic: false,
                                by_ref: false,
                                ty: Some(crate::ast::TypeHint {
                                    parts: vec!["string".to_string(), "int".to_string()],
                                }),
                                attrs: Vec::new(),
                            }]
                        };
                        (self.class_display_name(&c), params)
                    })
                })
        })
    }

    /// One property declared by `def` (the class keyed `c`).
    fn refl_prop_of(&self, c: &str, def: &ClassDef, name: &str) -> Option<ReflMember> {
        let is_static = def.static_prop_defaults.iter().any(|(n, _)| n == name);
        if !is_static && !def.prop_defaults.iter().any(|(n, _)| n == name) {
            return None;
        }
        let read = vis_bits(def.prop_vis.get(name));
        let mut modifiers = read;
        if is_static {
            modifiers |= refl_flags::STATIC;
        }
        // The write visibility, when it is narrower than the read one; a
        // public readonly property is implicitly `protected(set)`.
        let write = match def.prop_set_vis.get(name) {
            Some(v) => vis_bits(Some(v)),
            None if def.readonly_props.contains(name) && read == refl_flags::PUBLIC => {
                refl_flags::PROTECTED
            }
            None => read,
        };
        if write != read {
            modifiers |= match write {
                refl_flags::PRIVATE => refl_flags::PRIVATE_SET,
                refl_flags::PROTECTED => refl_flags::PROTECTED_SET,
                _ => refl_flags::PUBLIC_SET,
            };
        }
        if def.readonly_props.contains(name) {
            modifiers |= refl_flags::READONLY;
        }
        if def.final_props.contains(name) {
            modifiers |= refl_flags::FINAL;
        }
        let ty = def.prop_types.get(name).cloned();
        // An untyped property always has a default (`null` at the least); a
        // typed one only when it was given one.
        let has_default = ty.is_none() || !def.uninit_props.contains_key(name);
        Some(ReflMember {
            name: name.to_string(),
            class: self.declared_class_name(c),
            modifiers,
            ty,
            has_default,
        })
    }

    /// The properties `ReflectionClass::getProperties()` lists: the class's
    /// own in declaration order, then each ancestor's not redeclared — an
    /// ancestor's private ones are not inherited.
    /// An enum's engine-declared properties: `name`, and `value` for a backed
    /// enum — `public protected(set) readonly`, typed, with no default.
    fn enum_props(&self, c: &str, def: &ClassDef) -> Vec<ReflMember> {
        if !def.is_enum {
            return Vec::new();
        }
        let member = |name: &str, ty: &str| ReflMember {
            name: name.to_string(),
            class: self.declared_class_name(c),
            modifiers: refl_flags::PUBLIC | refl_flags::READONLY | refl_flags::PROTECTED_SET,
            ty: Some(crate::ast::TypeHint {
                parts: vec![ty.to_string()],
            }),
            has_default: false,
        };
        let mut out = vec![member("name", "string")];
        if let Some(b) = &def.enum_backing {
            out.push(member("value", &b.to_ascii_lowercase()));
        }
        out
    }

    pub fn refl_props(&self, class: &str) -> Vec<ReflMember> {
        let mut seen: Vec<String> = Vec::new();
        let mut out = Vec::new();
        for (depth, (c, def)) in self.refl_chain(class).into_iter().enumerate() {
            for name in &def.prop_order {
                if seen.contains(name) {
                    continue;
                }
                let Some(p) = self.refl_prop_of(&c, def, name) else {
                    continue;
                };
                if depth > 0 && p.modifiers & refl_flags::PRIVATE != 0 {
                    continue;
                }
                seen.push(name.clone());
                out.push(p);
            }
            if depth == 0 {
                for p in self.enum_props(&c, def) {
                    if !seen.contains(&p.name) {
                        seen.push(p.name.clone());
                        out.push(p);
                    }
                }
            }
        }
        out
    }

    /// `new ReflectionProperty($class, $name)`: the nearest declaration.
    pub fn refl_prop(&self, class: &str, name: &str) -> Option<ReflMember> {
        self.refl_chain(class)
            .into_iter()
            .enumerate()
            .find_map(|(depth, (c, def))| {
                self.refl_prop_of(&c, def, name)
                    .filter(|p| depth == 0 || p.modifiers & refl_flags::PRIVATE == 0)
                    .or_else(|| {
                        (depth == 0)
                            .then(|| {
                                self.enum_props(&c, def)
                                    .into_iter()
                                    .find(|p| p.name == name)
                            })
                            .flatten()
                    })
            })
    }

    /// The default-value chunk of property `name` and the class that
    /// declares it, for `ReflectionProperty::getDefaultValue()`.
    pub fn refl_prop_default(&self, class: &str, name: &str) -> Option<(String, Chunk)> {
        self.refl_chain(class).into_iter().find_map(|(c, def)| {
            def.prop_defaults
                .iter()
                .chain(def.static_prop_defaults.iter())
                .find(|(n, _)| n == name)
                .map(|(_, chunk)| (c.clone(), chunk.clone()))
        })
    }

    /// The exported default of property `name`, from the class that declares
    /// it (the one [`PhpHost::refl_prop_default`] reads), when the reference
    /// keeps it as an expression.
    pub fn refl_default_text(&self, class: &str, name: &str) -> Option<String> {
        self.refl_chain(class)
            .into_iter()
            .find(|(_, def)| {
                def.prop_defaults
                    .iter()
                    .chain(def.static_prop_defaults.iter())
                    .any(|(n, _)| n == name)
            })
            .and_then(|(_, def)| {
                def.default_texts
                    .iter()
                    .find(|(n, _)| n == name)
                    .map(|(_, t)| t.clone())
            })
    }

    /// The constant names `ReflectionClass::getConstants()` lists: the
    /// class's own, then each ancestor's and each interface's not redeclared.
    pub fn refl_const_names(&self, class: &str) -> Vec<String> {
        let mut out: Vec<String> = Vec::new();
        let mut visit: Vec<String> = vec![unanchored(class).to_ascii_lowercase()];
        let mut done: Vec<String> = Vec::new();
        while let Some(c) = (!visit.is_empty()).then(|| visit.remove(0)) {
            if done.contains(&c) {
                continue;
            }
            let Some(def) = self.classes.get(&c) else {
                continue;
            };
            done.push(c.clone());
            // An enum's cases are class constants too, listed first.
            let names = def
                .enum_cases
                .iter()
                .map(|(n, _)| n)
                .chain(def.consts.iter().map(|(n, _)| n));
            for n in names {
                if !out.contains(n) {
                    out.push(n.clone());
                }
            }
            if let Some(p) = &def.parent {
                visit.push(p.to_ascii_lowercase());
            }
            visit.extend(def.interfaces.iter().map(|i| i.to_ascii_lowercase()));
        }
        out
    }

    /// How constant `name` of `class` was declared, from the nearest class that
    /// declares it (the order [`PhpHost::refl_const_names`] visits):
    /// `(declaring class, visibility modifier, final, is an enum case)`.
    pub fn refl_const_info(&self, class: &str, name: &str) -> Option<(String, i64, bool, bool)> {
        let mut visit: Vec<String> = vec![unanchored(class).to_ascii_lowercase()];
        let mut done: Vec<String> = Vec::new();
        while let Some(c) = (!visit.is_empty()).then(|| visit.remove(0)) {
            if done.contains(&c) {
                continue;
            }
            let Some(def) = self.classes.get(&c) else {
                continue;
            };
            done.push(c.clone());
            let case = def.enum_cases.iter().any(|(n, _)| n == name);
            if case || def.consts.iter().any(|(n, _)| n == name) {
                let vis = match def.const_vis.get(name) {
                    Some(Visibility::Private) => 4,
                    Some(Visibility::Protected) => 2,
                    _ => 1,
                };
                return Some((def.name.clone(), vis, def.final_consts.contains(name), case));
            }
            if let Some(p) = &def.parent {
                visit.push(p.to_ascii_lowercase());
            }
            visit.extend(def.interfaces.iter().map(|i| i.to_ascii_lowercase()));
        }
        None
    }

    /// A user function's parameters, return type and declared name.
    pub fn refl_user_function(
        &self,
        name: &str,
    ) -> Option<(String, Vec<Param>, Option<crate::ast::TypeHint>)> {
        let def = self.functions.get(&unanchored(name).to_ascii_lowercase())?;
        Some((
            def.declared.clone().unwrap_or_else(|| name.to_string()),
            def.params.clone(),
            def.ret.clone(),
        ))
    }

    /// A closure's parameters, return type and `{closure:…}` name.
    pub fn refl_closure(
        &self,
        v: &Value,
    ) -> Option<(String, Vec<Param>, Option<crate::ast::TypeHint>)> {
        let Some(PhpObj::Closure { params, ret, .. }) = self.as_array(v) else {
            return None;
        };
        let name = self
            .closure_callable_name(v)
            .unwrap_or_else(|| "{closure}".to_string());
        Some((name, params.clone(), ret.clone()))
    }
}

/// Evaluate a reflected default (a parameter's or a property's) in the scope
/// of the class that declared it.
pub fn refl_eval_default(class: &str, chunk: Chunk) -> Result<Value, String> {
    run_in_class_scope(class, chunk)
}