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
//! phplang — PHP as a fusevm frontend.
//!
//! Pipeline: `lexer` → `parser` builds a PHP AST → `compiler` lowers it to a
//! `fusevm::Chunk` (plus a table of function sub-chunks) → fusevm executes it,
//! calling back into the `host` (through registered builtins and the strict
//! numeric hook) for every PHP-specific operation. There is no bespoke VM or
//! JIT here — execution and codegen live in fusevm.

pub mod argsig;
pub mod argtypes;
pub mod ast;
pub mod ast_export;
pub mod banner;
pub mod builtins;
pub mod bytes;
pub mod cli;
pub mod compiler;
pub mod corpus;
pub mod dap;
pub mod embed;
pub mod errlevel;
pub mod host;
pub mod intercepts;
pub mod internal_interfaces;
pub mod lexer;
pub mod lsp;
pub mod parser;
pub mod promote;
pub mod repl;
pub mod rust_ffi;
pub mod stdlib;
pub mod tiers;
pub mod timelib;

pub use embed::run_program;
pub use fusevm::Value;

/// Compile a PHP source string to a runnable program.
pub fn compile(src: &str) -> Result<compiler::Program, String> {
    compile_with_meta(src, false)
}

/// Compile with per-statement DAP line markers enabled (`php --dap`).
pub fn compile_debug(src: &str) -> Result<compiler::Program, String> {
    compile_with_meta(src, true)
}

/// The shared body of [`compile`] and [`compile_debug`].
///
/// The file-level facts the parse turns up have to be applied HERE rather than only
/// on the CLI path: `declare(strict_types=1)` changes how every later call binds its
/// arguments, so a program compiled through the library API would otherwise run in
/// coercive mode however it was written.
fn compile_with_meta(src: &str, debug: bool) -> Result<compiler::Program, String> {
    // A declaration is checked against the prelude's types, which are only
    // known once the prelude has compiled.
    prelude_defs();
    let (stmts, meta) = parser::parse_meta(src).map_err(|e| e.message)?;
    host::with_host(|h| {
        for (line, msg) in &meta.declare_warnings {
            h.warn_at(msg, *line);
        }
        h.set_strict_types(meta.strict_types);
    });
    compiler::compile(&stmts, debug)
}

/// The built-in exception class hierarchy, written in PHP so it flows through the
/// real class system: `throw`/`catch` resolve these as ordinary classes, and user
/// code can subclass them. `Exception` and `Error` are the two disjoint roots
/// (`catch (Throwable)` — handled in the host — matches either); the rest inherit
/// their `__construct`/`getMessage`/`getCode`/`getPrevious`/`__toString`.
///
/// `file`, `line` and `trace` are filled in by `host::seed_throwable` at
/// construction, exactly when the reference engine records them, so `getFile`,
/// `getLine` and `getTraceAsString` report the `new` site rather than the
/// `throw` site. `trace` is the structured frame array `getTrace()` returns;
/// `getTraceAsString()` renders it.
const EXCEPTION_PRELUDE: &str = r#"<?php
class Exception implements Throwable {
    protected $message = "";
    private $string = "";
    protected $code = 0;
    protected $file = "";
    protected $line = 0;
    private $trace = [];
    private $previous = null;
    public function __construct($message = "", $code = 0, $previous = null) {
        $this->message = $message;
        $this->code = $code;
        $this->previous = $previous;
    }
    final public function getMessage() { return $this->message; }
    final public function getCode() { return $this->code; }
    final public function getPrevious() { return $this->previous; }
    final public function getFile() { return $this->file; }
    final public function getLine() { return $this->line; }
    final public function getTrace() { return $this->trace; }
    final public function getTraceAsString() { return __phplang_trace_string($this->trace); }
    public function __toString() { return __phplang_throwable_string($this); }
}
class Error implements Throwable {
    protected $message = "";
    private $string = "";
    protected $code = 0;
    protected $file = "";
    protected $line = 0;
    private $trace = [];
    private $previous = null;
    public function __construct($message = "", $code = 0, $previous = null) {
        $this->message = $message;
        $this->code = $code;
        $this->previous = $previous;
    }
    final public function getMessage() { return $this->message; }
    final public function getCode() { return $this->code; }
    final public function getPrevious() { return $this->previous; }
    final public function getFile() { return $this->file; }
    final public function getLine() { return $this->line; }
    final public function getTrace() { return $this->trace; }
    final public function getTraceAsString() { return __phplang_trace_string($this->trace); }
    public function __toString() { return __phplang_throwable_string($this); }
}
class ErrorException extends Exception {
    protected $severity = 1;
    public function __construct($message = "", $code = 0, $severity = 1, $filename = null, $line = null, $previous = null) {
        parent::__construct($message, $code, $previous);
        $this->severity = $severity;
        // A filename replaces the recorded line too — with 0 when none is
        // given — while a line alone replaces only the line
        // (`ErrorException::__construct`, `Zend/zend_exceptions.c`).
        if ($filename !== null) {
            $this->file = $filename;
            $this->line = $line === null ? 0 : $line;
        } elseif ($line !== null) {
            $this->line = $line;
        }
    }
    final public function getSeverity() { return $this->severity; }
}
class RuntimeException extends Exception {}
class LogicException extends Exception {}
class InvalidArgumentException extends LogicException {}
class AssertionError extends Error {}
class ArithmeticError extends Error {}
class DivisionByZeroError extends ArithmeticError {}
class TypeError extends Error {}
class ArgumentCountError extends TypeError {}
class ValueError extends Error {}
class JsonException extends Exception {}
class CompileError extends Error {}
class ParseError extends CompileError {}
class UnhandledMatchError extends Error {}
class BadFunctionCallException extends LogicException {}
class BadMethodCallException extends BadFunctionCallException {}
class DomainException extends LogicException {}
class LengthException extends LogicException {}
class OutOfRangeException extends LogicException {}
class OutOfBoundsException extends RuntimeException {}
class OverflowException extends RuntimeException {}
class RangeException extends RuntimeException {}
class UnderflowException extends RuntimeException {}
class UnexpectedValueException extends RuntimeException {}
"#;

/// The date/time classes. Each method is a thin PHP shell over a
/// `__phplang_date_*` helper in `stdlib::datefn`, which ports the matching
/// `php_date.c` routine over `crate::timelib`; an object's state is the
/// properties the reference shows for it (see `stdlib::datefn`). Appended to
/// the prelude source (no `<?php` tag — it is concatenated after the
/// exceptions).
const DATETIME_PRELUDE: &str = r#"
class DateError extends Error {}
class DateObjectError extends DateError {}
class DateRangeError extends DateError {}
class DateException extends Exception {}
class DateInvalidTimeZoneException extends DateException {}
class DateInvalidOperationException extends DateException {}
class DateMalformedStringException extends DateException {}
class DateMalformedIntervalStringException extends DateException {}
class DateMalformedPeriodStringException extends DateException {}
interface DateTimeInterface {
    const ATOM = 'Y-m-d\TH:i:sP';
    const COOKIE = 'l, d-M-Y H:i:s T';
    const ISO8601 = 'Y-m-d\TH:i:sO';
    const ISO8601_EXPANDED = 'X-m-d\TH:i:sP';
    const RFC822 = 'D, d M y H:i:s O';
    const RFC850 = 'l, d-M-y H:i:s T';
    const RFC1036 = 'D, d M y H:i:s O';
    const RFC1123 = 'D, d M Y H:i:s O';
    #[\Deprecated(since: "8.5", message: "as this format ignores the associated timezone and always uses GMT")]
    const RFC7231 = 'D, d M Y H:i:s \G\M\T';
    const RFC2822 = 'D, d M Y H:i:s O';
    const RFC3339 = 'Y-m-d\TH:i:sP';
    const RFC3339_EXTENDED = 'Y-m-d\TH:i:s.vP';
    const RSS = 'D, d M Y H:i:s O';
    const W3C = 'Y-m-d\TH:i:sP';
    public function format(string $format): string;
    public function getTimezone(): DateTimeZone|false;
    public function getOffset(): int;
    public function getTimestamp(): int;
    public function getMicrosecond(): int;
    public function diff(DateTimeInterface $targetObject, bool $absolute = false): DateInterval;
}
class DateTime implements DateTimeInterface {
    public function __construct(string $datetime = "now", ?DateTimeZone $timezone = null) { __phplang_date_init($this, $datetime, $timezone); }
    public function format(string $format): string { return __phplang_date_format($this, $format); }
    public function modify(string $modifier): DateTime { return __phplang_date_modify($this, $modifier); }
    public function add(DateInterval $interval): DateTime { return __phplang_date_add($this, $interval); }
    public function sub(DateInterval $interval): DateTime { return __phplang_date_sub($this, $interval); }
    public function diff(DateTimeInterface $targetObject, bool $absolute = false): DateInterval { return __phplang_date_diff($this, $targetObject, $absolute); }
    public function getTimestamp(): int { return __phplang_date_timestamp($this); }
    public function getMicrosecond(): int { return __phplang_date_microsecond($this); }
    public function getOffset(): int { return __phplang_date_offset($this); }
    public function getTimezone(): DateTimeZone|false { return __phplang_date_timezone_get($this); }
    public function setTimezone(DateTimeZone $timezone): DateTime { return __phplang_date_timezone_set($this, $timezone); }
    public function setDate(int $year, int $month, int $day): DateTime { return __phplang_date_set($this, "date", $year, $month, $day, 0); }
    public function setISODate(int $year, int $week, int $dayOfWeek = 1): DateTime { return __phplang_date_set($this, "isodate", $year, $week, $dayOfWeek, 0); }
    public function setTime(int $hour, int $minute, int $second = 0, int $microsecond = 0): DateTime { return __phplang_date_set($this, "time", $hour, $minute, $second, $microsecond); }
    public function setTimestamp(int $timestamp): DateTime { return __phplang_date_set($this, "timestamp", $timestamp, 0, 0, 0); }
    public function setMicrosecond(int $microsecond): static { return __phplang_date_set($this, "microsecond", $microsecond, 0, 0, 0); }
    public static function createFromFormat(string $format, string $datetime, ?DateTimeZone $timezone = null): DateTime|false {
        $o = __phplang_date_new(static::class);
        return __phplang_date_init($o, $datetime, $timezone, $format) ? $o : false;
    }
    public static function createFromImmutable(DateTimeImmutable $object): static { return __phplang_date_copy(__phplang_date_new(static::class), $object); }
    public static function createFromInterface(DateTimeInterface $object): DateTime { return __phplang_date_copy(__phplang_date_new(static::class), $object); }
    public static function createFromTimestamp(int|float $timestamp): static { return __phplang_date_from_timestamp(__phplang_date_new(static::class), $timestamp); }
    public static function getLastErrors(): array|false { return __phplang_date_last_errors(); }
}
class DateTimeImmutable implements DateTimeInterface {
    public function __construct(string $datetime = "now", ?DateTimeZone $timezone = null) { __phplang_date_init($this, $datetime, $timezone); }
    public function format(string $format): string { return __phplang_date_format($this, $format); }
    public function modify(string $modifier): DateTimeImmutable { return __phplang_date_modify(clone $this, $modifier); }
    public function add(DateInterval $interval): DateTimeImmutable { return __phplang_date_add(clone $this, $interval); }
    public function sub(DateInterval $interval): DateTimeImmutable { return __phplang_date_sub(clone $this, $interval); }
    public function diff(DateTimeInterface $targetObject, bool $absolute = false): DateInterval { return __phplang_date_diff($this, $targetObject, $absolute); }
    public function getTimestamp(): int { return __phplang_date_timestamp($this); }
    public function getMicrosecond(): int { return __phplang_date_microsecond($this); }
    public function getOffset(): int { return __phplang_date_offset($this); }
    public function getTimezone(): DateTimeZone|false { return __phplang_date_timezone_get($this); }
    public function setTimezone(DateTimeZone $timezone): DateTimeImmutable { return __phplang_date_timezone_set(clone $this, $timezone); }
    public function setDate(int $year, int $month, int $day): DateTimeImmutable { return __phplang_date_set(clone $this, "date", $year, $month, $day, 0); }
    public function setISODate(int $year, int $week, int $dayOfWeek = 1): DateTimeImmutable { return __phplang_date_set(clone $this, "isodate", $year, $week, $dayOfWeek, 0); }
    public function setTime(int $hour, int $minute, int $second = 0, int $microsecond = 0): DateTimeImmutable { return __phplang_date_set(clone $this, "time", $hour, $minute, $second, $microsecond); }
    public function setTimestamp(int $timestamp): DateTimeImmutable { return __phplang_date_set(clone $this, "timestamp", $timestamp, 0, 0, 0); }
    public function setMicrosecond(int $microsecond): static { return __phplang_date_set(clone $this, "microsecond", $microsecond, 0, 0, 0); }
    public static function createFromFormat(string $format, string $datetime, ?DateTimeZone $timezone = null): DateTimeImmutable|false {
        $o = __phplang_date_new(static::class);
        return __phplang_date_init($o, $datetime, $timezone, $format) ? $o : false;
    }
    public static function createFromMutable(DateTime $object): static { return __phplang_date_copy(__phplang_date_new(static::class), $object); }
    public static function createFromInterface(DateTimeInterface $object): DateTimeImmutable { return __phplang_date_copy(__phplang_date_new(static::class), $object); }
    public static function createFromTimestamp(int|float $timestamp): static { return __phplang_date_from_timestamp(__phplang_date_new(static::class), $timestamp); }
    public static function getLastErrors(): array|false { return __phplang_date_last_errors(); }
}
class DateTimeZone {
    const AFRICA = 1;
    const AMERICA = 2;
    const ANTARCTICA = 4;
    const ARCTIC = 8;
    const ASIA = 16;
    const ATLANTIC = 32;
    const AUSTRALIA = 64;
    const EUROPE = 128;
    const INDIAN = 256;
    const PACIFIC = 512;
    const UTC = 1024;
    const ALL = 2047;
    const ALL_WITH_BC = 4095;
    const PER_COUNTRY = 4096;
    public function __construct(string $timezone) { __phplang_tz_init($this, $timezone); }
    public function getName(): string { return __phplang_tz_name($this); }
    public function getOffset(DateTimeInterface $datetime): int { return __phplang_tz_offset($this, $datetime); }
}
class DatePeriod implements IteratorAggregate {
    const EXCLUDE_START_DATE = 1;
    const INCLUDE_END_DATE = 2;
    public function __construct($start, $interval = null, $end = null, $options = null) { __phplang_period_init($this, func_get_args()); }
    public function getStartDate(): DateTimeInterface { return clone $this->start; }
    public function getEndDate(): ?DateTimeInterface { return $this->end === null ? null : clone $this->end; }
    public function getDateInterval(): DateInterval { return clone $this->interval; }
    public function getRecurrences(): ?int {
        $n = $this->recurrences - (int) $this->include_start_date - (int) $this->include_end_date;
        return $n === 0 ? null : $n;
    }
    public function getIterator(): Iterator { return new ArrayIterator(__phplang_period_list($this)); }
}
class DateInterval {
    public function __construct(string $duration) { __phplang_interval_init($this, $duration); }
    public function format(string $format): string { return __phplang_interval_format($this, $format); }
    public static function createFromDateString(string $datetime): DateInterval { return __phplang_interval_from_string($datetime); }
    public function __get($name) { return __phplang_interval_get($this, $name); }
}
"#;

/// `ext/hash`'s one class. The state `hash_init` … `hash_final`
/// (`stdlib::hashext`) keep in it is the private properties; `__debugInfo`
/// shows the `algo` the reference's `var_dump` shows. The constructor is
/// private as the reference's is: a context comes only from `hash_init` or
/// `hash_copy`.
const HASH_PRELUDE: &str = r#"
final class HashContext {
    private string $algo = "";
    private ?string $key = null;
    private ?int $seed = null;
    private ?string $secret = null;
    private string $data = "";
    private bool $finalized = false;
    private function __construct() {}
    public function __debugInfo(): array { return ["algo" => $this->algo]; }
}
"#;

/// `Zend/zend_weakrefs.c`'s `WeakReference` and `WeakMap`. Their state lives in
/// static tables keyed by object id, so an instance has no properties of its
/// own, as the reference's have none. phplang has no refcounts, so a referent
/// is never released early: `get()` keeps answering it and a map keeps its
/// entry after the program drops the key (BUGS.md).
const WEAKREF_PRELUDE: &str = r##"
final class WeakReference {
    // referent id => the WeakReference made for it; WeakReference id => referent
    private static $__made = [];
    private static $__referent = [];
    private static $__creating = false;
    public function __construct() {
        if (!self::$__creating) { throw new Error("Direct instantiation of WeakReference is not allowed, use WeakReference::create instead"); }
    }
    // `zend_weakref_find`: one WeakReference per referent.
    public static function create(object $object): WeakReference {
        $id = spl_object_id($object);
        if (isset(self::$__made[$id])) { return self::$__made[$id]; }
        self::$__creating = true;
        $ref = new WeakReference();
        self::$__creating = false;
        self::$__referent[spl_object_id($ref)] = $object;
        return self::$__made[$id] = $ref;
    }
    public function get(): ?object { return self::$__referent[spl_object_id($this)] ?? null; }
    public function __debugInfo(): array { return ["object" => $this->get()]; }
}
final class WeakMap implements ArrayAccess, Countable, IteratorAggregate {
    // map id => [key id => [key, value]]
    private static $__maps = [];
    private static function __key($object) {
        if (!is_object($object)) { throw new TypeError("WeakMap key must be an object"); }
        return spl_object_id($object);
    }
    public function offsetGet($object): mixed {
        $k = self::__key($object);
        $map = self::$__maps[spl_object_id($this)] ?? [];
        if (!array_key_exists($k, $map)) { throw new Error("Object " . get_class($object) . "#$k not contained in WeakMap"); }
        return $map[$k][1];
    }
    // A null offset is `$map[] = $v`, which `zend_weakmap_write_dimension` refuses.
    public function offsetSet($object, mixed $value): void {
        if ($object === null) { throw new Error("Cannot append to WeakMap"); }
        self::$__maps[spl_object_id($this)][self::__key($object)] = [$object, $value];
    }
    // `zend_weakmap_has_dimension`: present and not null.
    public function offsetExists($object): bool {
        return isset((self::$__maps[spl_object_id($this)] ?? [])[self::__key($object)][1]);
    }
    public function offsetUnset($object): void {
        unset(self::$__maps[spl_object_id($this)][self::__key($object)]);
    }
    public function count(): int { return count(self::$__maps[spl_object_id($this)] ?? []); }
    public function getIterator(): Iterator {
        foreach (self::$__maps[spl_object_id($this)] ?? [] as [$key, $value]) { yield $key => $value; }
    }
    private function __phplang_clone($orig) { self::$__maps[spl_object_id($this)] = self::$__maps[spl_object_id($orig)] ?? []; }
    public function __debugInfo(): array {
        $out = [];
        foreach (self::$__maps[spl_object_id($this)] ?? [] as [$key, $value]) { $out[] = ["key" => $key, "value" => $value]; }
        return $out;
    }
}
"##;

/// The Reflection API (`ext/reflection`), written in PHP over the engine's
/// `__phplang_reflect` metadata. Each object keeps only the public `name`
/// (and `class`) the reference shows; the rest of its state lives in static
/// tables keyed by object id, as `WeakReference`'s does. Access that crosses
/// visibility (`getValue`, `invoke`) runs in a closure bound to the declaring
/// class, which is the scope the reference grants it.
const REFLECTION_PRELUDE: &str = r##"
class ReflectionException extends Exception {}

class Reflection {
    public static function getModifierNames(int $modifiers): array {
        $names = [];
        if ($modifiers & 64) { $names[] = "abstract"; }
        if ($modifiers & 32) { $names[] = "final"; }
        if ($modifiers & 1) { $names[] = "public"; }
        elseif ($modifiers & 4) { $names[] = "private"; }
        elseif ($modifiers & 2) { $names[] = "protected"; }
        if ($modifiers & 1024) { $names[] = "public(set)"; }
        elseif ($modifiers & 2048) { $names[] = "protected(set)"; }
        elseif ($modifiers & 4096) { $names[] = "private(set)"; }
        if ($modifiers & 16) { $names[] = "static"; }
        if ($modifiers & (128 | 65536)) { $names[] = "readonly"; }
        return $names;
    }
}

abstract class ReflectionType implements Stringable {
    // object id => [name => as printed, parts => alternatives, null => allows null]
    protected static $__info = [];
    public static function __make(array $info): ReflectionType {
        $t = count($info["parts"]) == 1 ? new ReflectionNamedType() : new ReflectionUnionType();
        ReflectionType::$__info[spl_object_id($t)] = $info;
        return $t;
    }
    public function allowsNull(): bool { return ReflectionType::$__info[spl_object_id($this)]["null"]; }
    public function __toString(): string { return ReflectionType::$__info[spl_object_id($this)]["name"]; }
}

class ReflectionNamedType extends ReflectionType {
    public function getName(): string { return ReflectionType::$__info[spl_object_id($this)]["parts"][0]; }
    public function isBuiltin(): bool {
        return in_array(strtolower($this->getName()), ["int", "float", "string", "bool", "array", "mixed",
            "void", "null", "never", "false", "true", "callable", "iterable", "object", "static"], true);
    }
}

class ReflectionUnionType extends ReflectionType {
    public function getTypes(): array {
        $info = ReflectionType::$__info[spl_object_id($this)];
        $types = [];
        foreach ($info["parts"] as $p) { $types[] = ReflectionType::__make(["name" => $p, "parts" => [$p], "null" => $p === "null"]); }
        if ($info["null"]) { $types[] = ReflectionType::__make(["name" => "null", "parts" => ["null"], "null" => true]); }
        return $types;
    }
}

interface Reflector extends Stringable {}

abstract class ReflectionFunctionAbstract implements Reflector {
    public string $name = "";
    // object id => [kind, target, name] — what __phplang_reflect("params") reads
    protected static $__spec = [];
    public function __spec(): array { return ReflectionFunctionAbstract::$__spec[spl_object_id($this)]; }
    public function getName(): string { return $this->name; }
    public function getShortName(): string { $p = strrpos($this->name, "\\"); return $p === false ? $this->name : substr($this->name, $p + 1); }
    public function getNamespaceName(): string { $p = strrpos($this->name, "\\"); return $p === false ? "" : substr($this->name, 0, $p); }
    public function inNamespace(): bool { return str_contains($this->name, "\\"); }
    public function getParameters(): array {
        [$kind, $target, $fname] = $this->__spec();
        $out = [];
        foreach (__phplang_reflect("params", $kind, $target, $fname) as $i => $info) {
            $out[] = ReflectionParameter::__make($this, $info, $i);
        }
        return $out;
    }
    public function getNumberOfParameters(): int { [$k, $t, $n] = $this->__spec(); return count(__phplang_reflect("params", $k, $t, $n)); }
    public function getNumberOfRequiredParameters(): int {
        [$k, $t, $n] = $this->__spec();
        $req = 0;
        foreach (__phplang_reflect("params", $k, $t, $n) as $p) { if (!$p["optional"]) { $req++; } }
        return $req;
    }
    public function isVariadic(): bool {
        [$k, $t, $n] = $this->__spec();
        foreach (__phplang_reflect("params", $k, $t, $n) as $p) { if ($p["variadic"]) { return true; } }
        return false;
    }
    abstract protected function __returnInfo();
    public function hasReturnType(): bool { return $this->__returnInfo() !== null; }
    public function getReturnType(): ?ReflectionType { $t = $this->__returnInfo(); return $t === null ? null : ReflectionType::__make($t); }
    public function getDocComment(): string|false { [$k, $t, $n] = $this->__spec(); return __phplang_reflect("doc", $k, $t, $n); }
    public function isClosure(): bool { return false; }
    public function returnsReference(): bool { [$k, $t, $n] = $this->__spec(); return __phplang_reflect("fn_flags", $k, $t, $n)[1]; }
    public function isDeprecated(): bool { [$k, $t, $n] = $this->__spec(); return __phplang_reflect("fn_flags", $k, $t, $n)[0]; }
    // The class whose declaration this method's prototype is, as
    // do_inheritance_check_on_method sets it: the parent's prototype, else the
    // parent's method (a constructor only when that is abstract), and then an
    // interface's.
    public function __prototypeClass(): ?string {
        if (!($this instanceof ReflectionMethod)) { return null; }
        $proto = null;
        $parent = get_parent_class($this->class);
        if ($parent !== false && method_exists($parent, $this->name)) {
            $pm = new ReflectionMethod($parent, $this->name);
            if (!$pm->isPrivate()) {
                $root = $pm->__prototypeClass();
                $rootMethod = $root === null ? $pm : new ReflectionMethod($root, $this->name);
                if (!$this->isConstructor() || $rootMethod->isAbstract()) { $proto = $rootMethod->class; }
            }
        }
        foreach (class_implements($this->class) as $iface) {
            if (strcasecmp($iface, $this->class) != 0 && method_exists($iface, $this->name)) {
                $proto = ReflectionFunctionAbstract::__declaringInterface($iface, $this->name);
            }
        }
        return $proto;
    }
    // The interface among `$iface` and the interfaces it extends that
    // declares `$name` first, which is what an implementing method's
    // prototype names.
    public static function __declaringInterface(string $iface, string $name): string {
        foreach (class_implements($iface) as $p) {
            if (method_exists($p, $name)) { return ReflectionFunctionAbstract::__declaringInterface($p, $name); }
        }
        return $iface;
    }
    // The method `$name` in the function table of `$class`: its own or
    // inherited one, or an interface's it has not implemented. The
    // declaring class and whether it is private, or null.
    public static function __tableMethod(string $class, string $name): ?array {
        if (method_exists($class, $name)) {
            $m = new ReflectionMethod($class, $name);
            return [$m->class, $m->isPrivate()];
        }
        foreach (class_implements($class) as $iface) {
            if (method_exists($iface, $name)) { return [ReflectionFunctionAbstract::__declaringInterface($iface, $name), false]; }
        }
        return null;
    }
    // _function_string (ext/reflection/php_reflection.c). `$scope` is the
    // class the method was reflected through, or null.
    public function __fnString(?string $scope, string $indent): string {
        $method = $this instanceof ReflectionMethod;
        $internal = $this->isInternal() || $this->getFileName() === false;
        $s = "";
        $doc = $internal ? false : $this->getDocComment();
        if ($doc !== false) { $s .= $indent . $doc . "\n"; }
        $s .= $indent . ($this->isClosure() ? "Closure [ " : ($method ? "Method [ " : "Function [ "));
        $s .= $internal ? "<internal" : "<user";
        if ($this->isDeprecated()) { $s .= ", deprecated"; }
        if ($method && $scope !== null) {
            if (strcasecmp($this->class, $scope) != 0) {
                $s .= ", inherits " . $this->class;
            } else {
                $parent = get_parent_class($this->class);
                $over = $parent === false ? null : ReflectionFunctionAbstract::__tableMethod($parent, $this->name);
                if ($over !== null && strcasecmp($over[0], $this->class) != 0 && !$over[1]) { $s .= ", overwrites " . $over[0]; }
            }
        }
        $proto = $this->__prototypeClass();
        if ($proto !== null) { $s .= ", prototype " . $proto; }
        if ($method && $this->isConstructor()) { $s .= ", ctor"; }
        $s .= "> ";
        if ($method) {
            if ($this->isAbstract()) { $s .= "abstract "; }
            if ($this->isFinal()) { $s .= "final "; }
            if ($this->isStatic()) { $s .= "static "; }
            $s .= ($this->isPublic() ? "public " : ($this->isPrivate() ? "private " : "protected ")) . "method ";
        } else {
            if ($this->isStatic()) { $s .= "static "; }
            $s .= "function ";
        }
        if ($this->returnsReference()) { $s .= "&"; }
        $s .= $this->name . " ] {\n";
        if (!$internal) { $s .= $indent . "  @@ " . $this->getFileName() . " " . $this->getStartLine() . " - " . $this->getEndLine() . "\n"; }
        $pi = $indent . "  ";
        if ($this->isClosure()) {
            $vars = array_keys($this->getStaticVariables());
            if ($vars) {
                $s .= "\n" . $pi . "- Bound Variables [" . count($vars) . "] {\n";
                foreach ($vars as $i => $v) { $s .= $pi . "    Variable #" . $i . " [ \$" . $v . " ]\n"; }
                $s .= $pi . "}\n";
            }
        }
        $params = $this->getParameters();
        if ($internal || $params || $this->hasReturnType()) {
            $required = $this->__requiredCount();
            $s .= "\n" . $pi . "- Parameters [" . count($params) . "] {\n";
            foreach ($params as $i => $p) { $s .= $pi . "  " . $p->__paramString($i < $required) . "\n"; }
            $s .= $pi . "}\n";
        }
        if ($this->hasReturnType()) { $s .= "  " . $indent . "- Return [ " . $this->getReturnType() . " ]\n"; }
        return $s . $indent . "}\n";
    }
    // `required_num_args`: up to and including the last required parameter.
    public function __requiredCount(): int {
        $required = 0;
        foreach ($this->getParameters() as $i => $p) { if (!$p->isOptional() && !$p->isVariadic()) { $required = $i + 1; } }
        return $required;
    }
    public function getFileName(): string|false { [$k, $t, $n] = $this->__spec(); $s = __phplang_reflect("span", $k, $t, $n); return $s === false ? false : $s[0]; }
    public function getStartLine(): int|false { [$k, $t, $n] = $this->__spec(); $s = __phplang_reflect("span", $k, $t, $n); return $s === false ? false : $s[1]; }
    public function getEndLine(): int|false { [$k, $t, $n] = $this->__spec(); $s = __phplang_reflect("span", $k, $t, $n); return $s === false ? false : $s[2]; }
    public function isUserDefined(): bool { return !$this->isInternal(); }
    public function getStaticVariables(): array { [$k, $t, $n] = $this->__spec(); return $k === "method" ? __phplang_reflect("statics", $t, $n) : []; }
    public function getAttributes(?string $name = null, int $flags = 0): array {
        $spec = $this->__spec();
        return ReflectionAttribute::__list("ReflectionFunctionAbstract::getAttributes", $spec, -1, $spec[0] === "method" ? Attribute::TARGET_METHOD : Attribute::TARGET_FUNCTION, $name, $flags);
    }
}

class ReflectionFunction extends ReflectionFunctionAbstract {
    // object id => what __phplang_reflect("function") answered
    private static $__fn = [];
    public function __construct(Closure|string $function) {
        $info = __phplang_reflect("function", $function);
        if ($info === false) { throw new ReflectionException("Function " . $function . "() does not exist"); }
        $this->name = $info["name"];
        ReflectionFunction::$__fn[spl_object_id($this)] = [$info, $function];
        ReflectionFunctionAbstract::$__spec[spl_object_id($this)] = $info["closure"] ? ["closure", $function, ""] : ["function", $info["name"], ""];
    }
    protected function __returnInfo() { return ReflectionFunction::$__fn[spl_object_id($this)][0]["return"]; }
    public function isInternal(): bool { return ReflectionFunction::$__fn[spl_object_id($this)][0]["internal"]; }
    public function isClosure(): bool { return ReflectionFunction::$__fn[spl_object_id($this)][0]["closure"]; }
    public function isStatic(): bool { [$k, $t, $n] = $this->__spec(); return __phplang_reflect("fn_flags", $k, $t, $n)[2]; }
    public function invoke(mixed ...$args): mixed { return call_user_func_array(ReflectionFunction::$__fn[spl_object_id($this)][1], $args); }
    public function invokeArgs(array $args = []): mixed { return call_user_func_array(ReflectionFunction::$__fn[spl_object_id($this)][1], $args); }
    public function getClosure(): Closure { $f = ReflectionFunction::$__fn[spl_object_id($this)][1]; return $f instanceof Closure ? $f : Closure::fromCallable($f); }
    public function getStaticVariables(): array { [$k, $t] = $this->__spec(); return __phplang_reflect("statics", $t); }
    public function __toString(): string { return $this->__fnString(null, ""); }
}

class ReflectionMethod extends ReflectionFunctionAbstract {
    const IS_STATIC = 16;
    const IS_PUBLIC = 1;
    const IS_PROTECTED = 2;
    const IS_PRIVATE = 4;
    const IS_ABSTRACT = 64;
    const IS_FINAL = 32;
    public string $class = "";
    private static $__m = [];
    // object id => the class the method was reflected through
    private static $__scope = [];
    public function __construct(object|string $objectOrMethod, ?string $method = null) {
        if ($method === null) {
            __phplang_reflect("deprecated", "Calling ReflectionMethod::__construct() with 1 argument is deprecated, use ReflectionMethod::createFromMethodName() instead");
            if (!is_string($objectOrMethod) || !str_contains($objectOrMethod, "::")) {
                throw new ReflectionException("ReflectionMethod::__construct(): Argument #1 (\$objectOrMethod) must be a valid method name");
            }
            [$objectOrMethod, $method] = explode("::", $objectOrMethod, 2);
        }
        $class = is_object($objectOrMethod) ? get_class($objectOrMethod) : $objectOrMethod;
        $c = __phplang_reflect("class", $class);
        if ($c === false) { throw new ReflectionException("Class \"" . $class . "\" does not exist"); }
        $info = __phplang_reflect("method", $c["name"], $method);
        if ($info === null) { throw new ReflectionException("Method " . $c["name"] . "::" . $method . "() does not exist"); }
        $this->name = $info["name"];
        $this->class = $info["class"];
        ReflectionMethod::$__m[spl_object_id($this)] = $info;
        ReflectionMethod::$__scope[spl_object_id($this)] = $c["name"];
        ReflectionFunctionAbstract::$__spec[spl_object_id($this)] = ["method", $info["class"], $info["name"]];
    }
    public static function createFromMethodName(string $method): static {
        if (!str_contains($method, "::")) {
            throw new ReflectionException("ReflectionMethod::createFromMethodName(): Argument #1 (\$method) must be a valid method name");
        }
        [$class, $name] = explode("::", $method, 2);
        return new static($class, $name);
    }
    private function __info(): array { return ReflectionMethod::$__m[spl_object_id($this)]; }
    public function __toString(): string { return $this->__fnString(ReflectionMethod::$__scope[spl_object_id($this)], ""); }
    protected function __returnInfo() { return $this->__info()["type"]; }
    public function getModifiers(): int { return $this->__info()["modifiers"]; }
    public function isPublic(): bool { return ($this->getModifiers() & 1) != 0; }
    public function isProtected(): bool { return ($this->getModifiers() & 2) != 0; }
    public function isPrivate(): bool { return ($this->getModifiers() & 4) != 0; }
    public function isStatic(): bool { return ($this->getModifiers() & 16) != 0; }
    public function isFinal(): bool { return ($this->getModifiers() & 32) != 0; }
    public function isAbstract(): bool { return ($this->getModifiers() & 64) != 0; }
    public function isConstructor(): bool { return strcasecmp($this->name, "__construct") == 0; }
    public function isDestructor(): bool { return strcasecmp($this->name, "__destruct") == 0; }
    public function isInternal(): bool { return __phplang_reflect("class", $this->class)["internal"]; }
    public function getDeclaringClass(): ReflectionClass { return new ReflectionClass($this->class); }
    #[\Deprecated(message: "as it has no effect since PHP 8.1", since: "8.5")]
    public function setAccessible(bool $accessible): void {}
    public function invoke(?object $object = null, mixed ...$args): mixed { return $this->invokeArgs($object, $args); }
    public function invokeArgs(?object $object = null, array $args = []): mixed {
        $name = $this->name;
        if ($this->isStatic()) {
            return Closure::bind(function ($c, $n, $a) { return $c::$n(...$a); }, null, $this->class)($this->class, $name, $args);
        }
        return Closure::bind(function ($o, $n, $a) { return $o->$n(...$a); }, null, $this->class)($object, $name, $args);
    }
    public function getClosure(?object $object = null): Closure {
        $name = $this->name;
        if ($this->isStatic()) {
            return Closure::bind(function (...$a) use ($name) { return static::$name(...$a); }, null, $this->class);
        }
        return Closure::bind(function (...$a) use ($name) { return $this->$name(...$a); }, $object, $this->class);
    }
}

class ReflectionParameter implements Reflector {
    public string $name = "";
    // object id => [function, info, position]
    private static $__p = [];
    public static function __make(ReflectionFunctionAbstract $fn, array $info, int $position): ReflectionParameter {
        $p = new ReflectionParameter();
        $p->name = $info["name"];
        ReflectionParameter::$__p[spl_object_id($p)] = [$fn, $info, $position];
        return $p;
    }
    private function __info(): array { return ReflectionParameter::$__p[spl_object_id($this)][1]; }
    public function getName(): string { return $this->name; }
    public function getPosition(): int { return ReflectionParameter::$__p[spl_object_id($this)][2]; }
    public function isOptional(): bool { return $this->__info()["optional"]; }
    public function isVariadic(): bool { return $this->__info()["variadic"]; }
    public function isPassedByReference(): bool { return $this->__info()["byref"]; }
    public function canBePassedByValue(): bool { return !$this->__info()["byref"]; }
    public function hasType(): bool { return $this->__info()["type"] !== null; }
    public function getType(): ?ReflectionType { $t = $this->__info()["type"]; return $t === null ? null : ReflectionType::__make($t); }
    public function allowsNull(): bool { $t = $this->__info()["type"]; return $t === null || $t["null"]; }
    public function isDefaultValueAvailable(): bool { return $this->__info()["default"]; }
    public function getDefaultValue(): mixed {
        if (!$this->__info()["default"]) { throw new ReflectionException("Internal error: Failed to retrieve the default value"); }
        $fn = ReflectionParameter::$__p[spl_object_id($this)][0];
        [$k, $t, $n] = $fn->__spec();
        return __phplang_reflect("default", $k, $t, $n, $this->getPosition());
    }
    public function getDeclaringFunction(): ReflectionFunctionAbstract { return ReflectionParameter::$__p[spl_object_id($this)][0]; }
    public function getDeclaringClass(): ?ReflectionClass {
        $fn = ReflectionParameter::$__p[spl_object_id($this)][0];
        return $fn instanceof ReflectionMethod ? new ReflectionClass($fn->class) : null;
    }
    public function getAttributes(?string $name = null, int $flags = 0): array { $p = ReflectionParameter::$__p[spl_object_id($this)]; return ReflectionAttribute::__list("ReflectionParameter::getAttributes", $p[0]->__spec(), $p[2], Attribute::TARGET_PARAMETER, $name, $flags); }
    // _parameter_string (ext/reflection/php_reflection.c).
    public function __paramString(bool $required): string {
        $s = "Parameter #" . $this->getPosition() . " [ " . ($required ? "<required> " : "<optional> ");
        if ($this->hasType()) { $s .= $this->getType() . " "; }
        if ($this->isPassedByReference()) { $s .= "&"; }
        if ($this->isVariadic()) { $s .= "..."; }
        $s .= "\$" . $this->name;
        if (!$required && !$this->isVariadic()) {
            $fn = $this->getDeclaringFunction();
            [$k, $t, $n] = $fn->__spec();
            $d = __phplang_reflect("default_text", $k, $t, $n, $this->getPosition());
            if ($fn->isInternal()) { $s .= " = " . ($d ?? "<default>"); }
            elseif ($d !== null) { $s .= " = " . $d; }
        }
        return $s . " ]";
    }
    public function __toString(): string { return $this->__paramString($this->getPosition() < $this->getDeclaringFunction()->__requiredCount()); }
}

class ReflectionProperty implements Reflector {
    const IS_STATIC = 16;
    const IS_READONLY = 128;
    const IS_PUBLIC = 1;
    const IS_PROTECTED = 2;
    const IS_PRIVATE = 4;
    const IS_ABSTRACT = 64;
    const IS_FINAL = 32;
    public string $name = "";
    public string $class = "";
    private static $__p = [];
    public function __construct(object|string $class, string $property) {
        $cname = is_object($class) ? get_class($class) : $class;
        $c = __phplang_reflect("class", $cname);
        if ($c === false) { throw new ReflectionException("Class \"" . $cname . "\" does not exist"); }
        $info = __phplang_reflect("prop", $c["name"], $property);
        if ($info === null && is_object($class) && property_exists($class, $property)) {
            $info = ["name" => $property, "class" => $c["name"], "modifiers" => 1, "type" => null, "default" => false];
        }
        if ($info === null) { throw new ReflectionException("Property " . $c["name"] . "::\$" . $property . " does not exist"); }
        $this->name = $info["name"];
        $this->class = $info["class"];
        ReflectionProperty::$__p[spl_object_id($this)] = $info;
    }
    private function __info(): array { return ReflectionProperty::$__p[spl_object_id($this)]; }
    public function getName(): string { return $this->name; }
    public function getModifiers(): int { return $this->__info()["modifiers"]; }
    public function isPublic(): bool { return ($this->getModifiers() & 1) != 0; }
    public function isProtected(): bool { return ($this->getModifiers() & 2) != 0; }
    public function isPrivate(): bool { return ($this->getModifiers() & 4) != 0; }
    public function isStatic(): bool { return ($this->getModifiers() & 16) != 0; }
    public function isReadOnly(): bool { return ($this->getModifiers() & 128) != 0; }
    public function isFinal(): bool { return ($this->getModifiers() & 32) != 0; }
    public function isDefault(): bool { return true; }
    public function hasType(): bool { return $this->__info()["type"] !== null; }
    public function getType(): ?ReflectionType { $t = $this->__info()["type"]; return $t === null ? null : ReflectionType::__make($t); }
    public function hasDefaultValue(): bool { return $this->__info()["default"]; }
    public function getDefaultValue(): mixed { return __phplang_reflect("prop_default", $this->class, $this->name); }
    public function getDeclaringClass(): ReflectionClass { return new ReflectionClass($this->class); }
    public function getDocComment(): string|false { return __phplang_reflect("doc", "prop", $this->class, $this->name); }
    #[\Deprecated(message: "as it has no effect since PHP 8.1", since: "8.5")]
    public function setAccessible(bool $accessible): void {}
    public function getValue(?object $object = null): mixed {
        if ($this->isStatic()) {
            return Closure::bind(function ($c, $n) { return __phplang_reflect("static_get", $c, $n); }, null, $this->class)($this->class, $this->name);
        }
        return Closure::bind(function ($o, $n) { return $o->$n; }, null, $this->class)($object, $this->name);
    }
    public function setValue(mixed $objectOrValue = null, mixed $value = null): void {
        if ($this->isStatic()) {
            if (func_num_args() == 1) {
                __phplang_reflect("deprecated", "Calling ReflectionProperty::setValue() with a single argument is deprecated");
            }
            $v = func_num_args() == 1 ? $objectOrValue : $value;
            Closure::bind(function ($c, $n, $v) { __phplang_reflect("static_set", $c, $n, $v); }, null, $this->class)($this->class, $this->name, $v);
            return;
        }
        Closure::bind(function ($o, $n, $v) { $o->$n = $v; }, null, $this->class)($objectOrValue, $this->name, $value);
    }
    public function isInitialized(?object $object = null): bool {
        if ($this->isStatic()) { return true; }
        return Closure::bind(function ($o, $n) { return isset($o->$n) || array_key_exists($n, get_object_vars($o)); }, null, $this->class)($object, $this->name);
    }
    public function getAttributes(?string $name = null, int $flags = 0): array { return ReflectionAttribute::__list("ReflectionProperty::getAttributes", ["prop", $this->class, $this->name], -1, Attribute::TARGET_PROPERTY, $name, $flags); }
    // _property_string (ext/reflection/php_reflection.c).
    public function __propString(string $indent): string {
        $s = "";
        $doc = $this->getDocComment();
        if ($doc !== false) { $s .= $indent . $doc . "\n"; }
        $s .= $indent . "Property [ ";
        $m = $this->getModifiers();
        if ($m & 64) { $s .= "abstract "; }
        if ($m & 32) { $s .= "final "; }
        $s .= ($m & 1) ? "public " : (($m & 4) ? "private " : "protected ");
        if ($m & 4096) { $s .= "private(set) "; } elseif ($m & 2048) { $s .= "protected(set) "; }
        if ($m & 16) { $s .= "static "; }
        if ($m & 128) { $s .= "readonly "; }
        if ($this->hasType()) { $s .= $this->getType() . " "; }
        $s .= "\$" . $this->name;
        if ($this->hasDefaultValue()) { $s .= " = " . __phplang_reflect("prop_default_text", $this->class, $this->name); }
        return $s . " ]\n";
    }
    public function __toString(): string { return $this->__propString(""); }
}

class ReflectionClassConstant implements Reflector {
    const IS_PUBLIC = 1;
    const IS_PROTECTED = 2;
    const IS_PRIVATE = 4;
    const IS_FINAL = 32;
    public string $name = "";
    public string $class = "";
    public function __construct(object|string $class, string $constant) {
        $r = new ReflectionClass($class);
        if (!$r->hasConstant($constant)) { throw new ReflectionException("Constant " . $r->name . "::" . $constant . " does not exist"); }
        $this->name = $constant;
        $this->class = $r->name;
    }
    public function getName(): string { return $this->name; }
    public function getValue(): mixed { return __phplang_reflect("const_value", $this->class, $this->name); }
    public function getDocComment(): string|false { return __phplang_reflect("doc", "const", $this->class, $this->name) ?: __phplang_reflect("doc", "case", $this->class, $this->name); }
    public function getAttributes(?string $name = null, int $flags = 0): array { return ReflectionAttribute::__list("ReflectionClassConstant::getAttributes", ["const", $this->class, $this->name], -1, Attribute::TARGET_CLASS_CONSTANT, $name, $flags); }
    public function getDeclaringClass(): ReflectionClass { return new ReflectionClass($this->class); }
    // _class_const_string (ext/reflection/php_reflection.c).
    public function __constString(string $indent): string {
        [$cls, $mods, $case] = __phplang_reflect("const_info", $this->class, $this->name);
        $v = $this->getValue();
        $s = "";
        $doc = $this->getDocComment();
        if ($doc !== false) { $s .= $indent . $doc . "\n"; }
        $type = match (true) { $v === null => "null", is_bool($v) => "bool", is_int($v) => "int", is_float($v) => "float", is_string($v) => "string", is_array($v) => "array", default => get_class($v) };
        $vis = ($mods & 4) ? "private" : (($mods & 2) ? "protected" : "public");
        $s .= $indent . "Constant [ " . (($mods & 32) ? "final " : "") . $vis . " " . $type . " " . $this->name . " ] { ";
        $s .= is_array($v) ? "Array" : (is_object($v) ? "Object" : (is_float($v) ? __phplang_reflect("double14", $v) : (string) $v));
        return $s . " }\n";
    }
    // _enum_case_string (ext/reflection/php_reflection.c).
    public function __caseString(string $indent): string {
        $s = "";
        $doc = $this->getDocComment();
        if ($doc !== false) { $s .= $indent . $doc . "\n"; }
        $case = constant($this->class . "::" . $this->name);
        $s .= $indent . "Case " . $this->name;
        return $s . ($case instanceof BackedEnum ? " = " . $case->value : "") . "\n";
    }
    public function __toString(): string { return $this->__constString(""); }
}

class ReflectionClass implements Reflector {
    const IS_IMPLICIT_ABSTRACT = 16;
    const IS_EXPLICIT_ABSTRACT = 64;
    const IS_FINAL = 32;
    const IS_READONLY = 65536;
    public string $name = "";
    private static $__c = [];
    public function __construct(object|string $objectOrClass) {
        $info = __phplang_reflect("class", $objectOrClass);
        if ($info === false) { throw new ReflectionException("Class \"" . $objectOrClass . "\" does not exist"); }
        $this->name = $info["name"];
        $info["object"] = is_object($objectOrClass) ? $objectOrClass : null;
        ReflectionClass::$__c[spl_object_id($this)] = $info;
    }
    private function __info(): array { return ReflectionClass::$__c[spl_object_id($this)]; }
    public function getName(): string { return $this->name; }
    public function getShortName(): string { $p = strrpos($this->name, "\\"); return $p === false ? $this->name : substr($this->name, $p + 1); }
    public function getNamespaceName(): string { $p = strrpos($this->name, "\\"); return $p === false ? "" : substr($this->name, 0, $p); }
    public function inNamespace(): bool { return str_contains($this->name, "\\"); }
    public function isInterface(): bool { return $this->__info()["interface"]; }
    public function isTrait(): bool { return $this->__info()["trait"]; }
    public function isEnum(): bool { return $this->__info()["enum"]; }
    public function isAbstract(): bool { return $this->__info()["abstract"]; }
    public function isFinal(): bool { return $this->__info()["final"]; }
    public function isReadOnly(): bool { return $this->__info()["readonly"]; }
    public function isInternal(): bool { return $this->__info()["internal"]; }
    public function isUserDefined(): bool { return !$this->__info()["internal"]; }
    public function isAnonymous(): bool { return $this->__info()["anonymous"]; }
    public function isCloneable(): bool { return $this->isInstantiable(); }
    public function isInstantiable(): bool {
        $i = $this->__info();
        if ($i["interface"] || $i["trait"] || $i["enum"] || $i["abstract"]) { return false; }
        $ctor = __phplang_reflect("method", $this->name, "__construct");
        return $ctor === null || ($ctor["modifiers"] & 1) != 0;
    }
    public function isIterable(): bool { return $this->implementsInterface("Traversable") && $this->isInstantiable(); }
    public function isIterateable(): bool { return $this->isIterable(); }
    public function getModifiers(): int {
        $i = $this->__info();
        return ($i["abstract"] && !$i["interface"] ? 64 : 0) | ($i["final"] ? 32 : 0) | ($i["readonly"] ? 65536 : 0);
    }
    public function getParentClass(): ReflectionClass|false {
        $p = $this->__info()["parent"];
        return $p === null ? false : new ReflectionClass($p);
    }
    public function getInterfaceNames(): array { return $this->__info()["interfaces"]; }
    public function getInterfaces(): array {
        $out = [];
        foreach ($this->getInterfaceNames() as $i) { $out[$i] = new ReflectionClass($i); }
        return $out;
    }
    public function getTraitNames(): array { return $this->__info()["traits"]; }
    public function getTraits(): array {
        $out = [];
        foreach ($this->getTraitNames() as $t) { $out[$t] = new ReflectionClass($t); }
        return $out;
    }
    public function implementsInterface(ReflectionClass|string $interface): bool {
        $name = $interface instanceof ReflectionClass ? $interface->name : $interface;
        $r = __phplang_reflect("class", $name);
        if ($r === false) { throw new ReflectionException("Interface \"" . $name . "\" does not exist"); }
        if (!$r["interface"]) { throw new ReflectionException($r["name"] . " is not an interface"); }
        return is_a($this->name, $r["name"], true);
    }
    public function isSubclassOf(ReflectionClass|string $class): bool {
        $name = $class instanceof ReflectionClass ? $class->name : $class;
        $r = __phplang_reflect("class", $name);
        if ($r === false) { throw new ReflectionException("Class \"" . $name . "\" does not exist"); }
        return strcasecmp($r["name"], $this->name) != 0 && is_a($this->name, $r["name"], true);
    }
    public function isInstance(object $object): bool { return is_a($object, $this->name); }
    public function hasMethod(string $name): bool { return __phplang_reflect("method", $this->name, $name) !== null; }
    public function getMethod(string $name): ReflectionMethod { return new ReflectionMethod($this->name, $name); }
    public function getMethods(?int $filter = null): array {
        $out = [];
        foreach (__phplang_reflect("methods", $this->name) as $m) {
            if ($filter === null || ($m["modifiers"] & $filter)) { $out[] = new ReflectionMethod($this->name, $m["name"]); }
        }
        return $out;
    }
    public function getConstructor(): ?ReflectionMethod { return $this->hasMethod("__construct") ? new ReflectionMethod($this->name, "__construct") : null; }
    // Made from an object, its dynamic properties count too.
    public function hasProperty(string $name): bool {
        $o = $this->__info()["object"];
        return __phplang_reflect("prop", $this->name, $name) !== null || ($o !== null && property_exists($o, $name));
    }
    public function getProperty(string $name): ReflectionProperty { return new ReflectionProperty($this->name, $name); }
    public function getProperties(?int $filter = null): array {
        $out = [];
        foreach (__phplang_reflect("props", $this->name) as $p) {
            if ($filter === null || ($p["modifiers"] & $filter)) { $out[] = new ReflectionProperty($this->name, $p["name"]); }
        }
        return $out;
    }
    // The static properties first, then the instance ones.
    public function getDefaultProperties(): array {
        $out = [];
        foreach ([true, false] as $static) {
            foreach (__phplang_reflect("props", $this->name) as $p) {
                if ($p["default"] && (($p["modifiers"] & 16) != 0) === $static) {
                    $out[$p["name"]] = __phplang_reflect("prop_default", $this->name, $p["name"]);
                }
            }
        }
        return $out;
    }
    public function getStaticProperties(): array {
        $out = [];
        foreach ($this->getProperties(16) as $p) { $out[$p->name] = $p->getValue(); }
        return $out;
    }
    public function getStaticPropertyValue(string $name, mixed $default = null): mixed {
        if (!$this->hasProperty($name)) {
            if (func_num_args() > 1) { return $default; }
            throw new ReflectionException("Property " . $this->name . "::\$" . $name . " does not exist");
        }
        return $this->getProperty($name)->getValue();
    }
    public function setStaticPropertyValue(string $name, mixed $value): void { $this->getProperty($name)->setValue(null, $value); }
    public function hasConstant(string $name): bool { return array_key_exists($name, __phplang_reflect("consts", $this->name)); }
    public function getConstants(?int $filter = null): array { return __phplang_reflect("consts", $this->name); }
    public function getConstant(string $name): mixed {
        $all = __phplang_reflect("consts", $this->name);
        if (!array_key_exists($name, $all)) {
            __phplang_reflect("deprecated", "ReflectionClass::getConstant() for a non-existent constant is deprecated, use ReflectionClass::hasConstant() to check if the constant exists");
            return false;
        }
        return $all[$name];
    }
    public function getReflectionConstant(string $name): ReflectionClassConstant|false { return $this->hasConstant($name) ? new ReflectionClassConstant($this->name, $name) : false; }
    public function newInstance(mixed ...$args): object { $c = $this->name; return new $c(...$args); }
    public function newInstanceArgs(array $args = []): ?object { $c = $this->name; return new $c(...$args); }
    public function newInstanceWithoutConstructor(): object { return __phplang_reflect("instantiate", $this->name); }
    public function getDocComment(): string|false { return __phplang_reflect("doc", "class", $this->name, ""); }
    public function getFileName(): string|false { $s = __phplang_reflect("span", "class", $this->name, ""); return $s === false ? false : $s[0]; }
    public function getStartLine(): int|false { $s = __phplang_reflect("span", "class", $this->name, ""); return $s === false ? false : $s[1]; }
    public function getEndLine(): int|false { $s = __phplang_reflect("span", "class", $this->name, ""); return $s === false ? false : $s[2]; }
    public function getAttributes(?string $name = null, int $flags = 0): array { return ReflectionAttribute::__list("ReflectionClass::getAttributes", ["class", $this->name, ""], -1, Attribute::TARGET_CLASS, $name, $flags); }
    public function getExtensionName(): string|false { return false; }
    // _class_string (ext/reflection/php_reflection.c).
    public function __classString(string $indent): string {
        $i = $this->__info();
        $obj = $this instanceof ReflectionObject ? $i["object"] : null;
        $sub = $indent . "    ";
        $internal = $i["internal"];
        $s = "";
        $doc = $this->getDocComment();
        if ($doc !== false) { $s .= $indent . $doc . "\n"; }
        if ($obj !== null) { $s .= $indent . "Object of class [ "; }
        else { $s .= $indent . ($i["interface"] ? "Interface" : ($i["trait"] ? "Trait" : ($i["enum"] ? "Enum" : "Class"))) . " [ "; }
        $s .= $internal ? "<internal> " : "<user> ";
        if ($this->implementsInterface("Traversable") && !$i["interface"]) { $s .= "<iterateable> "; }
        if ($i["interface"]) { $s .= "interface "; }
        elseif ($i["trait"]) { $s .= "trait "; }
        elseif ($i["enum"]) { $s .= "enum "; }
        else {
            if ($i["abstract"]) { $s .= "abstract "; }
            if ($i["final"]) { $s .= "final "; }
            if ($i["readonly"]) { $s .= "readonly "; }
            $s .= "class ";
        }
        $s .= $this->name;
        if ($i["parent"] !== null) { $s .= " extends " . $i["parent"]; }
        if ($i["enum"] && $i["backing"] !== null) { $s .= ": " . strtolower($i["backing"]); }
        if ($i["interfaces"]) { $s .= ($i["interface"] ? " extends " : " implements ") . implode(", ", $i["interfaces"]); }
        $s .= " ] {\n";
        if (!$internal) { $s .= $indent . "  @@ " . $this->getFileName() . " " . $this->getStartLine() . "-" . $this->getEndLine() . "\n"; }
        $cases = ""; $consts = ""; $nCases = 0; $nConsts = 0;
        foreach (array_keys($this->getConstants()) as $name) {
            $c = new ReflectionClassConstant($this->name, $name);
            if (__phplang_reflect("const_info", $this->name, $name)[2]) { $cases .= $c->__caseString($sub); $nCases++; }
            else { $consts .= $c->__constString($sub); $nConsts++; }
        }
        if ($nCases) { $s .= "\n" . $indent . "  - Enum cases [" . $nCases . "] {\n" . $cases . $indent . "  }\n"; }
        $s .= "\n" . $indent . "  - Constants [" . $nConsts . "] {\n" . $consts . $indent . "  }\n";
        $props = array_filter($this->getProperties(), fn($p) => !$p->isPrivate() || strcasecmp($p->class, $this->name) == 0);
        $static = array_filter($props, fn($p) => $p->isStatic());
        $s .= "\n" . $indent . "  - Static properties [" . count($static) . "] {\n";
        foreach ($static as $p) { $s .= $p->__propString($sub); }
        $s .= $indent . "  }\n";
        $methods = array_filter($this->getMethods(), fn($m) => !$m->isPrivate() || strcasecmp($m->class, $this->name) == 0);
        $staticMethods = array_filter($methods, fn($m) => $m->isStatic());
        $s .= "\n" . $indent . "  - Static methods [" . count($staticMethods) . "] {";
        foreach ($staticMethods as $m) { $s .= "\n" . $m->__fnString($this->name, $sub); }
        if (!$staticMethods) { $s .= "\n"; }
        $s .= $indent . "  }\n";
        $instance = array_filter($props, fn($p) => !$p->isStatic());
        $s .= "\n" . $indent . "  - Properties [" . count($instance) . "] {\n";
        foreach ($instance as $p) { $s .= $p->__propString($sub); }
        $s .= $indent . "  }\n";
        if ($obj !== null) {
            $declared = array_map(fn($p) => $p->name, (new ReflectionClass($this->name))->getProperties());
            $dyn = "";
            $n = 0;
            foreach (get_object_vars($obj) as $name => $_) {
                if (!in_array((string) $name, $declared, true)) { $dyn .= $sub . "Property [ <dynamic> public \$" . $name . " ]\n"; $n++; }
            }
            $s .= "\n" . $indent . "  - Dynamic properties [" . $n . "] {\n" . $dyn . $indent . "  }\n";
        }
        $instanceMethods = array_filter($methods, fn($m) => !$m->isStatic());
        $s .= "\n" . $indent . "  - Methods [" . count($instanceMethods) . "] {";
        foreach ($instanceMethods as $m) { $s .= "\n" . $m->__fnString($this->name, $sub); }
        if (!$instanceMethods) { $s .= "\n"; }
        $s .= $indent . "  }\n";
        return $s . $indent . "}\n";
    }
    public function __toString(): string { return $this->__classString(""); }
}

class ReflectionObject extends ReflectionClass {
    public function __construct(object $object) { parent::__construct($object); }
}

class ReflectionEnum extends ReflectionClass {
    public function __construct(object|string $objectOrClass) {
        parent::__construct($objectOrClass);
        if (!$this->isEnum()) { throw new ReflectionException("Class \"" . $this->name . "\" is not an enum"); }
    }
    public function isBacked(): bool { return $this->implementsInterface("BackedEnum"); }
    public function getCases(): array {
        $c = $this->name;
        $kind = $this->isBacked() ? ReflectionEnumBackedCase::class : ReflectionEnumUnitCase::class;
        $out = [];
        foreach ($c::cases() as $case) { $out[] = new $kind($c, $case->name); }
        return $out;
    }
    public function hasCase(string $name): bool { foreach ($this->name::cases() as $case) { if ($case->name === $name) { return true; } } return false; }
    public function getCase(string $name): ReflectionEnumUnitCase {
        if (!$this->hasConstant($name)) { throw new ReflectionException("Case " . $this->name . "::" . $name . " does not exist"); }
        if (!$this->hasCase($name)) { throw new ReflectionException($this->name . "::" . $name . " is not a case"); }
        return $this->isBacked() ? new ReflectionEnumBackedCase($this->name, $name) : new ReflectionEnumUnitCase($this->name, $name);
    }
}

class ReflectionEnumUnitCase extends ReflectionClassConstant {
    public function __construct(object|string $class, string $constant) {
        parent::__construct($class, $constant);
        if (!(constant($this->class . "::" . $constant) instanceof UnitEnum)) { throw new ReflectionException("Constant " . $this->class . "::" . $constant . " is not a case"); }
    }
    public function getEnum(): ReflectionEnum { return new ReflectionEnum($this->class); }
    public function getValue(): UnitEnum { return constant($this->class . "::" . $this->name); }
}

class ReflectionEnumBackedCase extends ReflectionEnumUnitCase {
    public function __construct(object|string $class, string $constant) {
        parent::__construct($class, $constant);
        if (!($this->getValue() instanceof BackedEnum)) { throw new ReflectionException("Enum case " . $this->class . "::" . $constant . " is not a backed case"); }
    }
    public function getBackingValue(): int|string { return $this->getValue()->value; }
}
"##;

/// The SPL data-structure classes, written in PHP. They store elements in an
/// internal array property and mutate it through `$this`, which is the only way
/// a PHP array — a value, not a handle — can be mutated in place. Aliasing it to
/// a local first would copy it, and the write would be lost. Method-driven
/// (offsetGet/push/…), with `getIterator` returning the backing array.
const SPL_PRELUDE: &str = r#"
class stdClass {}
// `unserialize` restores an object of an unknown class into this placeholder,
// carrying the original name in `__PHP_Incomplete_Class_Name`.
class __PHP_Incomplete_Class {}
// SplDoublyLinkedList, SplQueue and SplStack: a port of ext/spl/spl_dllist.c.
// The list is a PHP list kept head first in `$dllist`, beside the mode bits a
// program set in `$flags`; `__debugInfo` reports both under the names, and in
// the order, the reference's debug view gives them. SplStack's LIFO bit and
// both subclasses' frozen-mode bit belong to the class, as the reference's
// `create_object` sets them. The traverse cursor — the index of the element it
// stands on, or null, and its position — lives in a static table keyed by
// object id, so it shows nowhere.
class SplDoublyLinkedList implements Iterator, Countable, ArrayAccess, Serializable {
    const IT_MODE_LIFO = 2;
    const IT_MODE_FIFO = 0;
    const IT_MODE_DELETE = 1;
    const IT_MODE_KEEP = 0;
    private $flags = 0;
    private $dllist = [];
    private static $__cursor = [];
    private function __flags() {
        if ($this instanceof SplStack) { return ($this->flags & 1) | 6; }
        if ($this instanceof SplQueue) { return ($this->flags & 1) | 4; }
        return $this->flags;
    }
    // `spl_ptr_llist_offset`: the list index of the element `$index` steps
    // from the head, or from the tail in LIFO mode.
    private function __at($index) {
        return ($this->__flags() & 2) ? count($this->dllist) - 1 - $index : $index;
    }
    private function __cur() { return self::$__cursor[spl_object_id($this)] ?? [null, 0]; }
    private function __setCur($ptr, $pos) { self::$__cursor[spl_object_id($this)] = [$ptr, $pos]; }
    // Keep the cursor on its element across an insertion (`$delta` 1) or a
    // removal (`$delta` -1) at list index `$at`; removing its element clears it.
    private function __moved($at, $delta) {
        [$ptr, $pos] = $this->__cur();
        if ($ptr === null) { return; }
        if ($delta < 0 && $ptr === $at) { $ptr = null; } elseif ($ptr >= $at) { $ptr += $delta; }
        $this->__setCur($ptr, $pos);
    }
    private function __outOfRange($method) {
        throw new OutOfRangeException("SplDoublyLinkedList::$method(): Argument #1 (\$index) is out of range");
    }
    public function add(int $index, mixed $value): void {
        $n = count($this->dllist);
        if ($index < 0 || $index > $n) { $this->__outOfRange("add"); }
        if ($index === $n) { $this->dllist[] = $value; return; }
        $at = $this->__at($index);
        array_splice($this->dllist, $at, 0, [$value]);
        $this->__moved($at, 1);
    }
    public function pop(): mixed {
        if (count($this->dllist) === 0) { throw new RuntimeException("Can't pop from an empty datastructure"); }
        $v = array_pop($this->dllist);
        $this->__moved(count($this->dllist), -1);
        return $v;
    }
    public function shift(): mixed {
        if (count($this->dllist) === 0) { throw new RuntimeException("Can't shift from an empty datastructure"); }
        $v = array_shift($this->dllist);
        $this->__moved(0, -1);
        return $v;
    }
    public function push(mixed $value): void { $this->dllist[] = $value; }
    public function unshift(mixed $value): void { array_unshift($this->dllist, $value); $this->__moved(0, 1); }
    public function top(): mixed {
        if (count($this->dllist) === 0) { throw new RuntimeException("Can't peek at an empty datastructure"); }
        return $this->dllist[count($this->dllist) - 1];
    }
    public function bottom(): mixed {
        if (count($this->dllist) === 0) { throw new RuntimeException("Can't peek at an empty datastructure"); }
        return $this->dllist[0];
    }
    public function count(): int { return count($this->dllist); }
    public function isEmpty(): bool { return count($this->dllist) === 0; }
    public function setIteratorMode(int $mode): int {
        $f = $this->__flags();
        if (($f & 4) && ($f & 2) !== ($mode & 2)) {
            throw new RuntimeException("Iterators' LIFO/FIFO modes for SplStack/SplQueue objects are frozen");
        }
        $this->flags = $mode & 3;
        return $this->__flags();
    }
    public function getIteratorMode(): int { return $this->__flags(); }
    public function offsetExists(int $index): bool { return $index >= 0 && $index < count($this->dllist); }
    public function offsetGet(int $index): mixed {
        if ($index < 0 || $index >= count($this->dllist)) { $this->__outOfRange("offsetGet"); }
        return $this->dllist[$this->__at($index)];
    }
    public function offsetSet(?int $index, mixed $value): void {
        if ($index === null) { $this->dllist[] = $value; return; }
        if ($index < 0 || $index >= count($this->dllist)) { $this->__outOfRange("offsetSet"); }
        $this->dllist[$this->__at($index)] = $value;
    }
    public function offsetUnset(int $index): void {
        if ($index < 0 || $index >= count($this->dllist)) { $this->__outOfRange("offsetUnset"); }
        $at = $this->__at($index);
        array_splice($this->dllist, $at, 1);
        $this->__moved($at, -1);
    }
    public function rewind(): void {
        $n = count($this->dllist);
        if ($this->__flags() & 2) {
            $this->__setCur($n > 0 ? $n - 1 : null, $n - 1);
        } else {
            $this->__setCur($n > 0 ? 0 : null, 0);
        }
    }
    public function valid(): bool { return $this->__cur()[0] !== null; }
    public function current(): mixed {
        $ptr = $this->__cur()[0];
        return $ptr === null ? null : $this->dllist[$ptr];
    }
    public function key(): int { return $this->__cur()[1]; }
    public function prev(): void { $this->__forward($this->__flags() ^ 2); }
    public function next(): void { $this->__forward($this->__flags()); }
    // `spl_dllist_it_helper_move_forward`: step toward the tail (FIFO) or the
    // head (LIFO), removing the element left behind in DELETE mode.
    private function __forward($flags) {
        [$ptr, $pos] = $this->__cur();
        if ($ptr === null) { return; }
        if ($flags & 2) {
            $ptr--;
            $pos--;
            if ($flags & 1) { array_pop($this->dllist); }
        } else {
            $ptr++;
            if ($flags & 1) { array_shift($this->dllist); $ptr--; } else { $pos++; }
        }
        $this->__setCur($ptr >= 0 && $ptr < count($this->dllist) ? $ptr : null, $pos);
    }
    // The properties a program gave the object, as `zend_std_get_properties`
    // holds them — everything but the list's own two slots.
    private function __members() {
        $m = (array) $this;
        unset($m["\0SplDoublyLinkedList\0flags"], $m["\0SplDoublyLinkedList\0dllist"]);
        return $m;
    }
    public function serialize(): string {
        $s = serialize($this->__flags());
        foreach ($this->dllist as $v) { $s .= ":" . serialize($v); }
        return $s;
    }
    public function unserialize(string $data): void {}
    public function __serialize(): array { return [$this->__flags(), $this->dllist, $this->__members()]; }
    public function __unserialize(array $data): void {
        if (!isset($data[0], $data[1], $data[2]) || !is_int($data[0]) || !is_array($data[1]) || !is_array($data[2])) {
            throw new UnexpectedValueException("Incomplete or ill-typed serialization data");
        }
        $this->flags = $data[0] & 3;
        foreach ($data[1] as $v) { $this->dllist[] = $v; }
        foreach ($data[2] as $k => $v) { $this->$k = $v; }
    }
    public function __debugInfo(): array {
        $m = $this->__members();
        $m["\0SplDoublyLinkedList\0flags"] = $this->__flags();
        $m["\0SplDoublyLinkedList\0dllist"] = $this->dllist;
        return $m;
    }
}
class SplQueue extends SplDoublyLinkedList {
    public function enqueue(mixed $value): void { $this->push($value); }
    public function dequeue(): mixed { return $this->shift(); }
}
class SplStack extends SplDoublyLinkedList {}
// SplFixedArray: a port of ext/spl/spl_fixedarray.c. The elements are a list
// in `$__elements`; `__debugInfo` shows them as the reference's
// `get_properties_for` does, as integer-keyed entries ahead of any property.
class SplFixedArray implements IteratorAggregate, ArrayAccess, Countable, JsonSerializable {
    private $__elements = [];
    public function __construct(int $size = 0) {
        if ($size < 0) {
            throw new ValueError('SplFixedArray::__construct(): Argument #1 ($size) must be greater than or equal to 0');
        }
        // A second __construct() call leaves a non-empty array alone.
        if (count($this->__elements) > 0) { return; }
        $this->__elements = $size > 0 ? array_fill(0, $size, null) : [];
    }
    // The element index `$index` names: `Index invalid or out of range` past
    // either end, and the offset conversion's TypeError for a non-integer.
    private function __index($index) {
        $i = __phplang_spl_offset($index);
        if ($i < 0 || $i >= count($this->__elements)) {
            throw new OutOfBoundsException("Index invalid or out of range");
        }
        return $i;
    }
    public function count(): int { return count($this->__elements); }
    public function toArray(): array { return $this->__elements; }
    public static function fromArray(array $array, bool $preserveKeys = true): SplFixedArray {
        $fa = new SplFixedArray();
        if (count($array) > 0 && $preserveKeys) {
            $max = 0;
            foreach ($array as $k => $v) {
                if (!is_int($k) || $k < 0) {
                    throw new InvalidArgumentException("array must contain only positive integer keys");
                }
                if ($k > $max) { $max = $k; }
            }
            $fa->__elements = array_fill(0, $max + 1, null);
            foreach ($array as $k => $v) { $fa->__elements[$k] = $v; }
        } elseif (count($array) > 0) {
            $fa->__elements = array_values($array);
        }
        return $fa;
    }
    public function getSize(): int { return count($this->__elements); }
    public function setSize(int $size): true {
        if ($size < 0) {
            throw new ValueError('SplFixedArray::setSize(): Argument #1 ($size) must be greater than or equal to 0');
        }
        $n = count($this->__elements);
        if ($size < $n) {
            $this->__elements = array_slice($this->__elements, 0, $size);
        } else {
            for ($i = $n; $i < $size; $i++) { $this->__elements[] = null; }
        }
        return true;
    }
    public function offsetExists($index): bool {
        $i = __phplang_spl_offset($index);
        return $i >= 0 && $i < count($this->__elements) && $this->__elements[$i] !== null;
    }
    public function offsetGet($index): mixed { return $this->__elements[$this->__index($index)]; }
    public function offsetSet($index, mixed $value): void { $this->__elements[$this->__index($index)] = $value; }
    public function offsetUnset($index): void { $this->__elements[$this->__index($index)] = null; }
    public function getIterator(): Iterator { return new ArrayIterator($this->__elements); }
    public function jsonSerialize(): array { return $this->__elements; }
    private function __members() {
        $m = (array) $this;
        unset($m["\0SplFixedArray\0__elements"]);
        return $m;
    }
    public function __serialize(): array {
        $out = $this->__elements;
        foreach ($this->__members() as $k => $v) { $out[$k] = $v; }
        return $out;
    }
    public function __unserialize(array $data): void {
        if (count($this->__elements) > 0) { return; }
        foreach ($data as $k => $v) {
            if (is_int($k)) { $this->__elements[] = $v; } else { $this->$k = $v; }
        }
    }
    public function __debugInfo(): array {
        $out = $this->__elements;
        foreach ($this->__members() as $k => $v) { $out[$k] = $v; }
        return $out;
    }
}
// ext/spl/spl_array.c's ArrayObject. The flags and the iterator class live in
// a static table keyed by object id, as ArrayIterator's flags do, so the
// instance shows only its storage. An object passed as the backing array is
// copied into it: the reference's write-through to that object is not
// modelled (BUGS.md).
class ArrayObject implements IteratorAggregate, ArrayAccess, Serializable, Countable {
    const STD_PROP_LIST = 1;
    const ARRAY_AS_PROPS = 2;
    private $storage = [];
    // id => [the flags a program set, the iterator class]
    private static $__state = [];
    public function __construct(array|object $array = [], int $flags = 0, string $iteratorClass = ArrayIterator::class) {
        $iteratorClass = self::__itclass($iteratorClass, 'ArrayObject::__construct(): Argument #3');
        if (func_num_args() == 0) { return; }
        $this->storage = self::__backing($array, 'ArrayObject::__construct()');
        self::$__state[spl_object_id($this)] = [$flags & ~0xFFFF0000, $iteratorClass];
    }
    // `spl_array_set_array`: an object backing is deprecated since 8.5. The
    // object itself is the storage — its properties are the elements, or,
    // for another ArrayObject / ArrayIterator, ITS storage — so a write
    // through either reaches both.
    private static function __backing($array, $fn) {
        if (is_array($array)) { return $array; }
        __phplang_deprecated("$fn: Using an object as a backing array for ArrayObject is deprecated, as it allows violating class constraints and invariants");
        return $array;
    }
    // Whether the storage is another ArrayObject / ArrayIterator, whose own
    // element methods the operations forward to.
    private function __shared() { return $this->storage instanceof ArrayObject || $this->storage instanceof ArrayIterator; }
    // The `C` parameter type: a class derived from ArrayIterator.
    private static function __itclass($class, $arg) {
        if (!class_exists($class) || !is_a($class, ArrayIterator::class, true)) {
            throw new TypeError("$arg (\$iteratorClass) must be a class name derived from ArrayIterator, $class given");
        }
        if (strcasecmp($class, ArrayIterator::class) === 0) { return ArrayIterator::class; }
        foreach (get_declared_classes() as $c) { if (strcasecmp($c, $class) === 0) { return $c; } }
        return $class;
    }
    private function __state() { return self::$__state[spl_object_id($this)] ?? [0, ArrayIterator::class]; }
    public function offsetGet(mixed $key): mixed {
        if (!is_object($this->storage)) { return $this->storage[$key]; }
        if ($this->__shared()) { return $this->storage->offsetGet($key); }
        return $this->storage->$key;
    }
    public function offsetSet(mixed $key, mixed $value): void {
        if (!is_object($this->storage)) {
            if ($key === null) { $this->storage[] = $value; } else { $this->storage[$key] = $value; }
            return;
        }
        if ($this->__shared()) { $this->storage->offsetSet($key, $value); return; }
        // An append takes the next INTEGER key of the property table; a named
        // element is the string-named property.
        if ($key === null) { __phplang_obj_append($this->storage, $value); return; }
        $this->storage->$key = $value;
    }
    public function offsetExists(mixed $key): bool {
        if (!is_object($this->storage)) { return isset($this->storage[$key]); }
        if ($this->__shared()) { return $this->storage->offsetExists($key); }
        return isset($this->storage->$key);
    }
    public function offsetUnset(mixed $key): void {
        if (!is_object($this->storage)) { unset($this->storage[$key]); return; }
        if ($this->__shared()) { $this->storage->offsetUnset($key); return; }
        unset($this->storage->$key);
    }
    // ARRAY_AS_PROPS: a property the object does not have is an element
    // (`spl_array_read_property` and its siblings). The host calls these only
    // while the flag is set.
    public function __get($name) { return $this->offsetGet($name); }
    public function __set($name, $value) { $this->offsetSet($name, $value); }
    public function __isset($name) { return $this->offsetExists($name) && $this->offsetGet($name) !== null; }
    public function __unset($name) { $this->offsetUnset($name); }
    public function append(mixed $value): void {
        if (!is_object($this->storage)) { $this->storage[] = $value; return; }
        if ($this->__shared()) { $this->storage->append($value); return; }
        throw new Error("Cannot append properties to objects, use ArrayObject::offsetSet() instead");
    }
    public function count(): int { return count($this->__elements(false)); }
    // An object's elements are its whole property table, as `(array)` reads
    // it; counting and iterating skip the non-public properties.
    public function getArrayCopy(): array { return $this->__elements(true); }
    public function __elements(bool $all) {
        if (!is_object($this->storage)) { return $this->storage; }
        if ($this->__shared()) { return $this->storage->__elements($all); }
        $t = (array) $this->storage;
        if (!$all) { foreach ($t as $k => $_) { if (is_string($k) && $k !== "" && $k[0] === "\0") { unset($t[$k]); } } }
        return $t;
    }
    public function exchangeArray(array|object $array): array { $old = $this->getArrayCopy(); $this->storage = self::__backing($array, 'ArrayObject::exchangeArray()'); return $old; }
    // `spl_array_object_new_ex` for the iterator: it starts with this object's flags.
    public function getIterator(): Iterator { [$flags, $class] = $this->__state(); return ArrayIterator::__over($this, $flags, $class); }
    public function getFlags(): int { return $this->__state()[0]; }
    public function setFlags(int $flags): void { self::$__state[spl_object_id($this)] = [$flags & ~0xFFFF0000, $this->__state()[1]]; }
    public function getIteratorClass(): string { return $this->__state()[1]; }
    public function setIteratorClass(string $iteratorClass): void { self::$__state[spl_object_id($this)] = [$this->__state()[0], self::__itclass($iteratorClass, 'ArrayObject::setIteratorClass(): Argument #1')]; }
    // `spl_array_object_new_ex` with `clone_orig`: the copy keeps the flags and the iterator class.
    private function __phplang_clone($orig) { self::$__state[spl_object_id($this)] = $orig->__state(); }
    public function serialize(): string { return __SplArraySerial::format($this->__state()[0], $this->storage, array_diff_key(get_object_vars($this), ['storage' => 1])); }
    public function unserialize(string $data): void {
        $parsed = __SplArraySerial::parse($data);
        if ($parsed === null) { return; }
        $flags = $parsed[0]; $storage = $parsed[1]; $members = $parsed[2];
        $this->storage = $storage;
        foreach ($members as $k => $v) { $this->$k = $v; }
        self::$__state[spl_object_id($this)] = [$flags, $this->__state()[1]];
    }
    public function __serialize(): array { [$flags, $class] = $this->__state(); return [$flags, $this->storage, array_diff_key(get_object_vars($this), ['storage' => 1]), $class === ArrayIterator::class ? null : $class]; }
    public function __unserialize(array $data): void { $this->storage = $data[1]; foreach ($data[2] ?? [] as $k => $v) { $this->$k = $v; } self::$__state[spl_object_id($this)] = [$data[0], $data[3] ?? ArrayIterator::class]; }
    public function asort(int $flags = SORT_REGULAR): bool { return $this->__sort("asort", $flags); }
    public function ksort(int $flags = SORT_REGULAR): bool { return $this->__sort("ksort", $flags); }
    public function uasort(callable $callback): bool { return $this->__sort("uasort", $callback); }
    public function uksort(callable $callback): bool { return $this->__sort("uksort", $callback); }
    public function natsort(): bool { return $this->__sort("natsort"); }
    public function natcasesort(): bool { return $this->__sort("natcasesort"); }
    // Sort the storage in place; an object backing has its properties put
    // back in the sorted order, a shared one is sorted by its owner.
    private function __sort($fn, ...$args) {
        if (!is_object($this->storage)) { return $fn($this->storage, ...$args); }
        if ($this->__shared()) { return $this->storage->$fn(...$args); }
        $table = (array) $this->storage;
        $r = $fn($table, ...$args);
        __phplang_obj_replace($this->storage, $table);
        return $r;
    }
}
interface SeekableIterator extends Iterator {
    public function seek(int $offset): void;
}
interface Serializable {
    public function serialize();
    public function unserialize(string $data);
}
interface SplObserver {
    public function update(SplSubject $subject): void;
}
interface SplSubject {
    public function attach(SplObserver $observer): void;
    public function detach(SplObserver $observer): void;
    public function notify(): void;
}
interface OuterIterator extends Iterator {
    public function getInnerIterator(): ?Iterator;
}
interface RecursiveIterator extends Iterator {
    public function hasChildren(): bool;
    public function getChildren(): ?RecursiveIterator;
}
#[Attribute(Attribute::TARGET_CLASS)]
final class Attribute {
    const TARGET_CLASS = 1;
    const TARGET_FUNCTION = 2;
    const TARGET_METHOD = 4;
    const TARGET_PROPERTY = 8;
    const TARGET_CLASS_CONSTANT = 16;
    const TARGET_PARAMETER = 32;
    const TARGET_CONSTANT = 64;
    const TARGET_ALL = 127;
    const IS_REPEATABLE = 128;
    public int $flags;
    public function __construct(int $flags = Attribute::TARGET_ALL) { $this->flags = $flags; }
}
#[Attribute(Attribute::TARGET_METHOD)]
final class ReturnTypeWillChange { public function __construct() {} }
#[Attribute(Attribute::TARGET_CLASS)]
final class AllowDynamicProperties { public function __construct() {} }
#[Attribute(Attribute::TARGET_PARAMETER)]
final class SensitiveParameter { public function __construct() {} }
#[Attribute(Attribute::TARGET_METHOD | Attribute::TARGET_PROPERTY)]
final class Override { public function __construct() {} }
#[Attribute(Attribute::TARGET_CLASS | Attribute::TARGET_FUNCTION | Attribute::TARGET_METHOD | Attribute::TARGET_CLASS_CONSTANT | Attribute::TARGET_CONSTANT)]
final class Deprecated {
    public readonly ?string $message;
    public readonly ?string $since;
    public function __construct(?string $message = null, ?string $since = null) { $this->message = $message; $this->since = $since; }
}
#[Attribute(Attribute::TARGET_FUNCTION | Attribute::TARGET_METHOD)]
final class NoDiscard {
    public readonly ?string $message;
    public function __construct(?string $message = null) { $this->message = $message; }
}
// `ReflectionAttribute`: one attribute of a declaration, read through
// `__phplang_reflect("attrs" / "attr_args")` on the declaration's
// `[kind, target, member]` spec (a parameter's position, else -1).
final class ReflectionAttribute implements Reflector {
    const IS_INSTANCEOF = 2;
    public string $name = "";
    // id => [kind, target, member, parameter position, index, target flag, repeated]
    private static $__a = [];
    // The `getAttributes($name, $flags)` of the declaration `$spec`, which
    // the reference names `$fn` in its argument errors.
    public static function __list(string $fn, array $spec, int $pos, int $target, ?string $name, int $flags): array {
        if ($flags & ~ReflectionAttribute::IS_INSTANCEOF) { throw new ValueError("$fn(): Argument #2 (\$flags) must be a valid attribute filter flag"); }
        if ($name !== null && ($flags & ReflectionAttribute::IS_INSTANCEOF) && !class_exists($name) && !interface_exists($name)) { throw new Error("Class \"$name\" not found"); }
        [$k, $t, $n] = $spec;
        $names = __phplang_reflect("attrs", $k, $t, $n, $pos);
        $counts = [];
        foreach ($names as $a) { $counts[strtolower($a)] = ($counts[strtolower($a)] ?? 0) + 1; }
        $out = [];
        foreach ($names as $i => $a) {
            if ($name !== null && ($flags & ReflectionAttribute::IS_INSTANCEOF ? !is_a($a, $name, true) : strcasecmp(ltrim($name, "\\"), $a) !== 0)) { continue; }
            $r = new ReflectionAttribute();
            $r->name = $a;
            ReflectionAttribute::$__a[spl_object_id($r)] = [$k, $t, $n, $pos, $i, $target, $counts[strtolower($a)] > 1];
            $out[] = $r;
        }
        return $out;
    }
    public function getName(): string { return $this->name; }
    public function __toString(): string {
        $s = "Attribute [ " . $this->name . " ]";
        $args = $this->getArguments();
        if (!$args) { return $s . "\n"; }
        $s .= " {\n  - Arguments [" . count($args) . "] {\n";
        $i = 0;
        foreach ($args as $k => $v) {
            $s .= "    Argument #" . $i++ . " [ " . (is_string($k) ? $k . " = " : "") . __phplang_reflect("format_value", $v) . " ]\n";
        }
        return $s . "  }\n}\n";
    }
    public function getTarget(): int { return ReflectionAttribute::$__a[spl_object_id($this)][5]; }
    public function isRepeated(): bool { return ReflectionAttribute::$__a[spl_object_id($this)][6]; }
    public function getArguments(): array {
        [$k, $t, $n, $p, $i] = ReflectionAttribute::$__a[spl_object_id($this)];
        return __phplang_reflect("attr_args", $k, $t, $n, $p, $i);
    }
    // `zend_get_attribute_object`: the class must exist and be an attribute
    // whose `#[Attribute]` flags admit this target, and repeat only when
    // `IS_REPEATABLE`.
    public function newInstance(): object {
        $c = $this->name;
        if (!class_exists($c)) { throw new Error("Attribute class \"$c\" not found"); }
        $meta = (new ReflectionClass($c))->getAttributes("Attribute");
        if (!$meta) { throw new Error("Attempting to use non-attribute class \"$c\" as attribute"); }
        $args = $meta[0]->getArguments();
        $allowed = $args[0] ?? $args["flags"] ?? Attribute::TARGET_ALL;
        $target = $this->getTarget();
        if (!($allowed & $target)) {
            $names = ["class", "function", "method", "property", "class constant", "parameter", "constant"];
            $shown = [];
            foreach ($names as $bit => $label) { if ($allowed & (1 << $bit)) { $shown[] = $label; } }
            throw new Error("Attribute \"$c\" cannot target " . $names[(int) log($target, 2)] . " (allowed targets: " . implode(", ", $shown) . ")");
        }
        if ($this->isRepeated() && !($allowed & Attribute::IS_REPEATABLE)) { throw new Error("Attribute \"$c\" must not be repeated"); }
        return new $c(...$this->getArguments());
    }
}
// An iterator over an array, positioned by a cursor kept outside the
// instance (a static table keyed by object id) so the instance shows only
// its storage, as the reference's does.
class ArrayIterator implements SeekableIterator, ArrayAccess, Serializable, Countable {
    const STD_PROP_LIST = 1;
    const ARRAY_AS_PROPS = 2;
    private $storage = [];
    private static $__cursor = [];
    // id => the flags a program set, `ar_flags & ~SPL_ARRAY_INT_MASK`.
    private static $__flags = [];
    // An object backing is the storage itself, as ArrayObject's: its property
    // table, or another ArrayObject / ArrayIterator's storage, which a write
    // through either reaches.
    public function __construct(array|object $array = [], int $flags = 0) {
        if (!is_array($array)) { __phplang_deprecated("ArrayIterator::__construct(): Using an object as a backing array for ArrayIterator is deprecated, as it allows violating class constraints and invariants"); }
        $this->storage = $array;
        self::$__flags[spl_object_id($this)] = $flags & ~0xFFFF0000;
    }
    private function __shared() { return $this->storage instanceof ArrayObject || $this->storage instanceof ArrayIterator; }
    // The elements as they stand: the array, or the backing object's table
    // — all of it for a copy, and for counting and iterating without the
    // non-public properties, which `spl_array_skip_protected` steps over.
    private function __table(bool $all = false) {
        if (!is_object($this->storage)) { return $this->storage; }
        if ($this->__shared()) { return $this->storage->__elements($all); }
        $t = (array) $this->storage;
        if (!$all) { foreach ($t as $k => $_) { if (is_string($k) && $k !== "" && $k[0] === "\0") { unset($t[$k]); } } }
        return $t;
    }
    public function __elements(bool $all) { return $this->__table($all); }
    // `ArrayObject::getIterator`: an iterator of `$class` sharing the
    // object's storage, made without running a constructor.
    public static function __over(ArrayObject $owner, int $flags, string $class) {
        $it = __phplang_reflect("instantiate", $class);
        $it->storage = $owner;
        self::$__flags[spl_object_id($it)] = $flags;
        return $it;
    }
    private function __at() {
        $c = self::$__cursor[spl_object_id($this)] ?? [0, null];
        if ($c[1] === null) { $c[1] = array_keys($this->__table()); self::$__cursor[spl_object_id($this)] = $c; }
        return $c;
    }
    private function __moved() { self::$__cursor[spl_object_id($this)][1] = null; }
    public function current(): mixed { [$i, $keys] = $this->__at(); return $i < count($keys) ? $this->__table()[$keys[$i]] : null; }
    public function key(): string|int|null { [$i, $keys] = $this->__at(); return $keys[$i] ?? null; }
    public function next(): void { [$i, $keys] = $this->__at(); self::$__cursor[spl_object_id($this)] = [$i + 1, $keys]; }
    public function rewind(): void { self::$__cursor[spl_object_id($this)] = [0, array_keys($this->__table())]; }
    public function valid(): bool { [$i, $keys] = $this->__at(); return $i < count($keys); }
    public function seek(int $offset): void {
        $keys = array_keys($this->__table());
        if ($offset < 0 || $offset >= count($keys)) { throw new OutOfBoundsException("Seek position $offset is out of range"); }
        self::$__cursor[spl_object_id($this)] = [$offset, $keys];
    }
    public function offsetGet(mixed $key): mixed {
        if (!is_object($this->storage)) { return $this->storage[$key]; }
        if ($this->__shared()) { return $this->storage->offsetGet($key); }
        return $this->storage->$key;
    }
    public function offsetSet(mixed $key, mixed $value): void {
        if (!is_object($this->storage)) {
            if ($key === null) { $this->storage[] = $value; } else { $this->storage[$key] = $value; }
        } elseif ($this->__shared()) {
            $this->storage->offsetSet($key, $value);
        } elseif ($key === null) {
            __phplang_obj_append($this->storage, $value);
        } else {
            $this->storage->$key = $value;
        }
        $this->__moved();
    }
    public function offsetExists(mixed $key): bool {
        if (!is_object($this->storage)) { return isset($this->storage[$key]); }
        if ($this->__shared()) { return $this->storage->offsetExists($key); }
        return isset($this->storage->$key);
    }
    public function offsetUnset(mixed $key): void {
        if (!is_object($this->storage)) { unset($this->storage[$key]); }
        elseif ($this->__shared()) { $this->storage->offsetUnset($key); }
        else { unset($this->storage->$key); }
        $this->__moved();
    }
    // ARRAY_AS_PROPS, as ArrayObject's.
    public function __get($name) { return $this->offsetGet($name); }
    public function __set($name, $value) { $this->offsetSet($name, $value); }
    public function __isset($name) { return $this->offsetExists($name) && $this->offsetGet($name) !== null; }
    public function __unset($name) { $this->offsetUnset($name); }
    public function append(mixed $value): void {
        if (is_object($this->storage) && !$this->__shared()) { throw new Error("Cannot append properties to objects, use ArrayIterator::offsetSet() instead"); }
        $this->offsetSet(null, $value);
    }
    public function count(): int { return count($this->__table()); }
    public function getArrayCopy(): array { return $this->__table(true); }
    public function getFlags(): int { return self::$__flags[spl_object_id($this)] ?? 0; }
    public function setFlags(int $flags): void { self::$__flags[spl_object_id($this)] = $flags & ~0xFFFF0000; }
    // `spl_array_object_new_ex` with `clone_orig`: the copy keeps the flags; its
    // position starts over.
    private function __phplang_clone($orig) { self::$__flags[spl_object_id($this)] = self::$__flags[spl_object_id($orig)] ?? 0; }
    public function serialize(): string { return __SplArraySerial::format(self::$__flags[spl_object_id($this)] ?? 0, $this->storage, array_diff_key(get_object_vars($this), ['storage' => 1])); }
    public function unserialize(string $data): void {
        $parsed = __SplArraySerial::parse($data);
        if ($parsed === null) { return; }
        $flags = $parsed[0]; $storage = $parsed[1]; $members = $parsed[2];
        $this->storage = $storage;
        foreach ($members as $k => $v) { $this->$k = $v; }
        self::$__flags[spl_object_id($this)] = $flags;
    }
    public function __serialize(): array { return [self::$__flags[spl_object_id($this)] ?? 0, $this->storage, array_diff_key(get_object_vars($this), ['storage' => 1]), null]; }
    public function __unserialize(array $data): void { $this->storage = $data[1]; foreach ($data[2] ?? [] as $k => $v) { $this->$k = $v; } self::$__flags[spl_object_id($this)] = $data[0]; }
    public function asort(int $flags = SORT_REGULAR): bool { return asort($this->storage, $flags); }
    public function ksort(int $flags = SORT_REGULAR): bool { return ksort($this->storage, $flags); }
    public function uasort(callable $callback): bool { return uasort($this->storage, $callback); }
    public function uksort(callable $callback): bool { return uksort($this->storage, $callback); }
    public function natsort(): bool { return natsort($this->storage); }
    public function natcasesort(): bool { return natcasesort($this->storage); }
}
// SplObjectStorage: a port of ext/spl/spl_observer.c. `$storage` is the
// reference's hash table in insertion order — a key per object, its handle
// unless a subclass overrides getHash(), whose string is the key then — of
// [object, info] pairs. `$pos` is the table's internal pointer (an offset into
// that order; the count is past the end) and `$index` the iteration index.
class SplObjectStorage implements Countable, SeekableIterator, Serializable, ArrayAccess {
    private $storage = [];
    private $pos = 0;
    private $index = 0;
    // `spl_object_storage_get_hash`
    private function __key($object) {
        if (__phplang_method_owner($this, 'gethash') === 'splobjectstorage') {
            return spl_object_id($object);
        }
        $h = $this->getHash($object);
        if (!is_string($h)) {
            $given = is_bool($h) ? ($h ? "true" : "false") : get_debug_type($h);
            throw new TypeError(get_class($this) . "::getHash(): Return value must be of type string, $given returned");
        }
        return $h;
    }
    private function __object($object, $method) {
        if (!is_object($object)) {
            throw new TypeError("SplObjectStorage::$method(): Argument #1 (\$object) must be of type object, " . get_debug_type($object) . " given");
        }
    }
    // `spl_object_storage_attach`: an object already stored keeps its place.
    private function __attach($object, $info) {
        $k = $this->__key($object);
        if (array_key_exists($k, $this->storage)) {
            $this->storage[$k][1] = $info;
        } else {
            $this->storage[$k] = [$object, $info];
        }
    }
    // `spl_object_storage_detach`, then the pointer reset its callers make.
    private function __detach($object) {
        unset($this->storage[$this->__key($object)]);
        $this->pos = 0;
        $this->index = 0;
    }
    private function __at() {
        $keys = array_keys($this->storage);
        return $this->pos < count($keys) ? $this->storage[$keys[$this->pos]] : null;
    }
    private function __members() {
        $m = (array) $this;
        unset($m["\0SplObjectStorage\0storage"], $m["\0SplObjectStorage\0pos"], $m["\0SplObjectStorage\0index"]);
        return $m;
    }
    // The reference marks attach(), detach() and contains() `#[\Deprecated]`
    // (since 8.5). Withheld here: tests/spl.rs pins their output without the
    // deprecation — see BUGS.md.
    public function attach(object $object, mixed $info = null): void { $this->__attach($object, $info); }
    public function detach(object $object): void { $this->__detach($object); }
    public function contains(object $object): bool { return array_key_exists($this->__key($object), $this->storage); }
    public function addAll(SplObjectStorage $storage): int {
        foreach ($storage->__pairs() as [$object, $info]) { $this->__attach($object, $info); }
        $this->index = 0;
        return count($this->storage);
    }
    public function removeAll(SplObjectStorage $storage): int {
        foreach ($storage->__pairs() as [$object]) { unset($this->storage[$this->__key($object)]); }
        $this->pos = 0;
        $this->index = 0;
        return count($this->storage);
    }
    public function removeAllExcept(SplObjectStorage $storage): int {
        foreach ($this->storage as $k => [$object]) {
            if (!$storage->offsetExists($object)) { unset($this->storage[$this->__key($object)]); }
        }
        $this->pos = 0;
        $this->index = 0;
        return count($this->storage);
    }
    private function __pairs() { return array_values($this->storage); }
    public function getInfo(): mixed { return $this->__at()[1] ?? null; }
    public function setInfo(mixed $info): void {
        $keys = array_keys($this->storage);
        if ($this->pos < count($keys)) { $this->storage[$keys[$this->pos]][1] = $info; }
    }
    public function count(int $mode = COUNT_NORMAL): int { return count($this->storage); }
    public function rewind(): void { $this->pos = 0; $this->index = 0; }
    public function valid(): bool { return $this->pos < count($this->storage); }
    public function key(): int { return $this->index; }
    public function current(): object {
        $e = $this->__at();
        if ($e === null) { throw new RuntimeException("Called current() on invalid iterator"); }
        return $e[0];
    }
    public function next(): void {
        if ($this->pos < count($this->storage)) { $this->pos++; }
        $this->index++;
    }
    public function seek(int $offset): void {
        $n = count($this->storage);
        if ($offset < 0 || $offset >= $n) { throw new OutOfBoundsException("Seek position $offset is out of range"); }
        $forward = function () use ($n) { if ($this->pos < $n) { $this->pos++; } $this->index++; };
        if ($offset == 0) {
            $this->pos = 0;
            $this->index = 0;
        } elseif ($offset > $this->index) {
            do { $forward(); } while ($offset > $this->index);
        } elseif ($offset < $this->index) {
            if ($this->index - $offset > $offset) {
                $this->pos = 0;
                $this->index = 0;
                do { $forward(); } while ($offset > $this->index);
            } else {
                do {
                    if ($this->pos < $n) { $this->pos = $this->pos == 0 ? $n : $this->pos - 1; }
                    $this->index--;
                } while ($offset < $this->index);
            }
        }
    }
    public function serialize(): string {
        $s = "x:i:" . count($this->storage) . ";";
        foreach ($this->storage as [$object, $info]) { $s .= serialize($object) . "," . serialize($info) . ";"; }
        return $s . "m:" . serialize($this->__members());
    }
    public function unserialize(string $data): void {}
    public function offsetExists($object): bool { $this->__object($object, 'offsetExists'); return array_key_exists($this->__key($object), $this->storage); }
    public function offsetGet($object): mixed {
        $this->__object($object, 'offsetGet');
        $k = $this->__key($object);
        if (!array_key_exists($k, $this->storage)) { throw new UnexpectedValueException("Object not found"); }
        return $this->storage[$k][1];
    }
    public function offsetSet($object, mixed $info = null): void { $this->__object($object, 'offsetSet'); $this->__attach($object, $info); }
    public function offsetUnset($object): void { $this->__object($object, 'offsetUnset'); $this->__detach($object); }
    public function getHash(object $object): string { return spl_object_hash($object); }
    public function __serialize(): array {
        $flat = [];
        foreach ($this->storage as [$object, $info]) { $flat[] = $object; $flat[] = $info; }
        return [$flat, $this->__members()];
    }
    public function __unserialize(array $data): void {
        if (!isset($data[0], $data[1]) || !is_array($data[0]) || !is_array($data[1])) {
            throw new UnexpectedValueException("Incomplete or ill-typed serialization data");
        }
        if (count($data[0]) % 2 != 0) { throw new UnexpectedValueException("Odd number of elements"); }
        $key = null;
        foreach ($data[0] as $v) {
            if ($key === null) { $key = [$v]; continue; }
            if (!is_object($key[0])) { throw new UnexpectedValueException("Non-object key"); }
            $this->__attach($key[0], $v);
            $key = null;
        }
        foreach ($data[1] as $k => $v) { $this->$k = $v; }
    }
    public function __debugInfo(): array {
        $m = $this->__members();
        $storage = [];
        foreach ($this->storage as [$object, $info]) { $storage[] = ["obj" => $object, "inf" => $info]; }
        $m["\0SplObjectStorage\0storage"] = $storage;
        return $m;
    }
}
// SplHeap, SplMinHeap, SplMaxHeap and SplPriorityQueue: a port of
// ext/spl/spl_heap.c. `$heap` is the binary heap array the reference sifts,
// with the reference's sift-up and delete-top walks, so elements that compare
// equal come out in the reference's order. The two class families share the
// heap mechanics through this trait; each says how two stored elements
// compare (`__elemCmp`). A `compare()` that throws leaves the operation to
// finish with every later comparison reading 0, then marks the heap corrupted
// and rethrows, as `spl_ptr_heap_insert`/`_delete_top` do.
// Each class declares the `$flags`, `$isCorrupted` and `$heap` the methods use.
trait __SplHeapOps {
    // id => [write-locked, exception a compare() threw during this operation]
    private static $__op = [];
    private function __state() { return self::$__op[spl_object_id($this)] ?? [false, null]; }
    private function __cmp($a, $b) {
        [$locked, $thrown] = $this->__state();
        if ($thrown !== null) { return 0; }
        [$ok, $r] = __phplang_try_call(fn() => $this->__elemCmp($a, $b));
        if ($ok) { return ((int) $r) <=> 0; }
        self::$__op[spl_object_id($this)] = [$locked, $r];
        return 0;
    }
    private function __begin() { self::$__op[spl_object_id($this)] = [true, null]; }
    private function __end() {
        $thrown = $this->__state()[1];
        unset(self::$__op[spl_object_id($this)]);
        if ($thrown !== null) {
            $this->isCorrupted = true;
            throw $thrown;
        }
    }
    // `spl_heap_consistency_validations`.
    private function __validate($write) {
        if ($this->isCorrupted) {
            throw new RuntimeException("Heap is corrupted, heap properties are no longer ensured.");
        }
        if ($write && $this->__state()[0]) {
            throw new RuntimeException("Heap cannot be changed when it is already being modified.");
        }
    }
    // `spl_ptr_heap_insert`: sift the new element up from the end.
    private function __insert($elem) {
        $this->__begin();
        $pos = count($this->heap);
        while ($pos > 0) {
            $parent = intdiv($pos - 1, 2);
            if ($this->__cmp($this->heap[$parent], $elem) >= 0) { break; }
            $this->heap[$pos] = $this->heap[$parent];
            $pos = $parent;
        }
        $this->heap[$pos] = $elem;
        $this->__end();
    }
    // `spl_ptr_heap_delete_top`: take the root, then sift the last element
    // down from it. Null for an empty heap.
    private function __deleteTop() {
        $n = count($this->heap);
        if ($n === 0) { return null; }
        $this->__begin();
        $top = $this->heap[0];
        $limit = intdiv($n - 1, 2);
        $count = $n - 1;
        $bottom = $this->heap[$count];
        for ($i = 0; $i < $limit; $i = $j) {
            $j = $i * 2 + 1;
            if ($j !== $count && $this->__cmp($this->heap[$j + 1], $this->heap[$j]) > 0) { $j++; }
            if ($this->__cmp($bottom, $this->heap[$j]) < 0) {
                $this->heap[$i] = $this->heap[$j];
            } else {
                break;
            }
        }
        array_pop($this->heap);
        if ($i !== $count) { $this->heap[$i] = $bottom; }
        $this->__end();
        return [$top];
    }
    public function count(): int { return count($this->heap); }
    public function isEmpty(): bool { return count($this->heap) === 0; }
    public function rewind(): void {}
    public function key(): int { return count($this->heap) - 1; }
    public function next(): void { $this->__validate(true); $this->__deleteTop(); }
    public function valid(): bool { return count($this->heap) !== 0; }
    public function recoverFromCorruption(): true { $this->isCorrupted = false; return true; }
    public function isCorrupted(): bool { return $this->isCorrupted; }
    private function __members() {
        $m = (array) $this;
        $self = self::class;
        unset($m["\0$self\0flags"], $m["\0$self\0isCorrupted"], $m["\0$self\0heap"]);
        return $m;
    }
    public function __debugInfo(): array {
        $m = $this->__members();
        $self = self::class;
        $m["\0$self\0flags"] = $this->flags;
        $m["\0$self\0isCorrupted"] = $this->isCorrupted;
        $m["\0$self\0heap"] = $this->heap;
        return $m;
    }
    public function __serialize(): array {
        $this->__validate(false);
        if ($this->__state()[0]) {
            throw new RuntimeException("Cannot serialize heap while it is being modified.");
        }
        return [$this->__members(), ["flags" => $this->flags, "heap_elements" => $this->heap]];
    }
    // `spl_heap_unserialize_internal_state`'s checks; `$elem` validates and
    // shapes one stored element, or answers null to refuse it.
    private function __restore(array $data, $flagsOk, $elem) {
        $bad = "Invalid serialization data for " . get_class($this) . " object";
        if (count($data) !== 2 || !isset($data[0]) || !is_array($data[0])) { throw new Exception($bad); }
        foreach ($data[0] as $k => $v) { $this->$k = $v; }
        $state = $data[1] ?? null;
        if (!is_array($state) || !is_int($state["flags"] ?? null) || !$flagsOk($state["flags"])
            || !is_array($state["heap_elements"] ?? null)) {
            throw new Exception($bad);
        }
        $this->flags = $state["flags"];
        foreach ($state["heap_elements"] as $v) {
            $e = $elem($v);
            if ($e === null) { throw new Exception($bad); }
            $this->__insert($e[0]);
        }
    }
}
abstract class SplHeap implements Iterator, Countable {
    use __SplHeapOps;
    private $flags = 0;
    private $isCorrupted = false;
    private $heap = [];
    abstract protected function compare(mixed $value1, mixed $value2): int;
    private function __elemCmp($a, $b) { return $this->compare($a, $b); }
    public function insert(mixed $value): true { $this->__validate(true); $this->__insert($value); return true; }
    public function extract(): mixed {
        $this->__validate(true);
        $top = $this->__deleteTop();
        if ($top === null) { throw new RuntimeException("Can't extract from an empty heap"); }
        return $top[0];
    }
    public function top(): mixed {
        $this->__validate(false);
        if (count($this->heap) === 0) { throw new RuntimeException("Can't peek at an empty heap"); }
        return $this->heap[0];
    }
    public function current(): mixed { return count($this->heap) === 0 ? null : $this->heap[0]; }
    public function __unserialize(array $data): void {
        $this->__validate(true);
        $this->__restore($data, fn($f) => $f === 0, fn($v) => [$v]);
        $this->__validate(false);
    }
}
class SplMinHeap extends SplHeap {
    protected function compare(mixed $value1, mixed $value2): int { return $value2 <=> $value1; }
}
class SplMaxHeap extends SplHeap {
    protected function compare(mixed $value1, mixed $value2): int { return $value1 <=> $value2; }
}
class SplPriorityQueue implements Iterator, Countable {
    use __SplHeapOps;
    private $flags = 1;
    private $isCorrupted = false;
    private $heap = [];
    const EXTR_BOTH = 3;
    const EXTR_PRIORITY = 2;
    const EXTR_DATA = 1;
    public function compare(mixed $priority1, mixed $priority2): int { return $priority1 <=> $priority2; }
    private function __elemCmp($a, $b) { return $this->compare($a["priority"], $b["priority"]); }
    // `spl_pqueue_extract_helper`.
    private function __shape($elem) {
        if (($this->flags & 3) === 3) { return $elem; }
        return ($this->flags & 1) ? $elem["data"] : $elem["priority"];
    }
    public function insert(mixed $value, mixed $priority): true {
        $this->__validate(true);
        $this->__insert(["data" => $value, "priority" => $priority]);
        return true;
    }
    public function setExtractFlags(int $flags): int {
        $flags &= 3;
        if (!$flags) { throw new RuntimeException("Must specify at least one extract flag"); }
        $this->flags = $flags;
        return $flags;
    }
    public function getExtractFlags(): int { return $this->flags; }
    public function top(): mixed {
        $this->__validate(false);
        if (count($this->heap) === 0) { throw new RuntimeException("Can't peek at an empty heap"); }
        return $this->__shape($this->heap[0]);
    }
    public function extract(): mixed {
        $this->__validate(true);
        $top = $this->__deleteTop();
        if ($top === null) { throw new RuntimeException("Can't extract from an empty heap"); }
        return $this->__shape($top[0]);
    }
    public function current(): mixed { return count($this->heap) === 0 ? null : $this->__shape($this->heap[0]); }
    public function __unserialize(array $data): void {
        $this->__restore(
            $data,
            fn($f) => ($f & 3) !== 0,
            fn($v) => is_array($v) && count($v) === 2 && array_key_exists("data", $v) && array_key_exists("priority", $v)
                ? [["data" => $v["data"], "priority" => $v["priority"]]] : null
        );
        $this->flags &= 3;
        $this->__validate(false);
    }
}
// The iterators of ext/spl/spl_iterators.c. Each instance's internal state —
// the reference's `spl_dual_it_object` / `spl_recursive_it_object` — lives in
// `__SplIt`, keyed by object id, so an instance shows no properties, as the
// reference's do not. `__SplIt`'s methods are the C file's static functions;
// the classes below are its PHP_METHODs. An inner iterator is driven through
// its Iterator methods, which is what the reference's `zend_user_iterator`
// does, including keeping the value `current()` answered until the iterator
// moves (`zend_user_it_get_current_data`).
// The `Serializable` form `spl_array_serialize` writes and
// `spl_array_unserialize` reads: `x:i:FLAGS;STORAGE;m:MEMBERS`, the storage
// and the members each in `serialize` notation.
final class __SplArraySerial {
    public static function format(int $flags, $storage, array $members): string {
        return "x:i:" . $flags . ";" . serialize($storage) . ";m:" . serialize($members);
    }
    // `[flags, storage, members]`, or null for an empty string (a no-op); a
    // malformed one is `UnexpectedValueException: Error at offset N of LEN
    // bytes`, N where the part that failed begins.
    public static function parse(string $data): ?array {
        if ($data === "") { return null; }
        $len = strlen($data);
        if (!preg_match('/\Ax:i:(\d+);/', $data, $m)) {
            throw new UnexpectedValueException("Error at offset 0 of $len bytes");
        }
        $start = strlen($m[0]);
        // The storage ends at the `;m:` whose prefix is one whole value.
        for ($at = strpos($data, ";m:", $start); $at !== false; $at = strpos($data, ";m:", $at + 1)) {
            $part = substr($data, $start, $at - $start);
            $storage = @unserialize($part);
            if (($storage !== false || $part === "b:0;") && serialize($storage) === $part) {
                $members = @unserialize(substr($data, $at + 3));
                if (!is_array($members)) {
                    throw new UnexpectedValueException("Error at offset $len of $len bytes");
                }
                return [(int) $m[1], $storage, $members];
            }
        }
        throw new UnexpectedValueException("Error at offset $start of $len bytes");
    }
}

final class __SplIt {
    private static $d = [];
    // RecursiveTreeIterator's prefix parts and postfix, set when the object is
    // created (`spl_RecursiveIteratorIterator_new_ex`), not by its constructor.
    private static $tree = [];
    private static $warning = null;

    // SPL_FETCH_AND_CHECK_DUAL_IT
    public static function id($o) {
        $id = spl_object_id($o);
        if (!isset(self::$d[$id])) {
            throw new Error("The object is in an invalid state as the parent constructor was not called");
        }
        return $id;
    }
    public static function get($o, $k) { return self::$d[self::id($o)][$k]; }
    public static function put($o, $k, $v) { self::$d[self::id($o)][$k] = $v; }

    // The `dit_type != DIT_Unknown` check at the top of `spl_dual_it_construct`.
    public static function once($o, $base) {
        if (isset(self::$d[spl_object_id($o)])) {
            throw new BadMethodCallException("$base::getIterator() must be called exactly once per instance");
        }
    }
    // The tail of `spl_dual_it_construct`: the inner object and the state.
    public static function construct($o, $inner, $extra = []) {
        self::$d[spl_object_id($o)] = $extra + [
            'inner' => $inner,
            'has' => false,
            'data' => null,
            'hasKey' => false,
            'key' => null,
            'pos' => 0,
        ];
    }
    // `spl_get_iterator_from_aggregate`.
    public static function fromAggregate($agg, $class) {
        $r = $agg->getIterator();
        if (!($r instanceof Traversable)) {
            throw new LogicException("$class::getIterator() must return an object that implements Traversable");
        }
        return $r;
    }
    // `zend_user_it_get_new_iterator`: an aggregate is iterated through the
    // iterator its getIterator() answers.
    public static function iteratorOf($t) {
        while (!($t instanceof Iterator)) {
            $t = self::fromAggregate($t, get_class($t));
        }
        return $t;
    }
    // IteratorIterator's arm of `spl_dual_it_construct`.
    public static function constructIteratorIterator($o, $it, $class) {
        self::once($o, 'IteratorIterator');
        if (!($it instanceof Iterator)) {
            $ce = get_class($it);
            if ($class !== null) {
                if (!class_exists($class) && !interface_exists($class) || !is_a($it, $class) || !is_subclass_of($class, 'Traversable') && strcasecmp($class, 'Traversable') !== 0) {
                    throw new LogicException("Class to downcast to not found or not base class or does not implement Traversable");
                }
                $ce = $class;
            }
            if ($it instanceof IteratorAggregate) {
                $it = self::fromAggregate($it, $ce);
            }
        }
        self::construct($o, $it, ['it' => self::iteratorOf($it)]);
    }

    // `spl_dual_it_free`
    public static function free($id) {
        self::$d[$id]['has'] = false;
        self::$d[$id]['data'] = null;
        self::$d[$id]['hasKey'] = false;
        self::$d[$id]['key'] = null;
        if (array_key_exists('zstr', self::$d[$id])) {
            self::$d[$id]['zstr'] = null;
            self::$d[$id]['children'] = null;
        }
    }
    // `spl_dual_it_rewind`
    public static function rewind($id) {
        self::free($id);
        self::$d[$id]['pos'] = 0;
        $it = self::$d[$id]['it'];
        if ($it !== null) { $it->rewind(); }
    }
    // `spl_dual_it_valid`
    public static function valid($id) {
        $it = self::$d[$id]['it'];
        return $it !== null && (bool) $it->valid();
    }
    // `spl_dual_it_fetch`
    public static function fetch($id, $checkMore) {
        self::free($id);
        if ($checkMore && !self::valid($id)) { return false; }
        $it = self::$d[$id]['it'];
        self::$d[$id]['data'] = $it->current();
        self::$d[$id]['has'] = true;
        self::$d[$id]['key'] = $it->key();
        self::$d[$id]['hasKey'] = true;
        return true;
    }
    // `spl_dual_it_next`
    public static function next($id, $doFree) {
        if ($doFree) {
            self::free($id);
        } elseif (self::$d[$id]['it'] === null) {
            throw new Error("The inner constructor wasn't initialized with an iterator instance");
        }
        self::$d[$id]['it']->next();
        self::$d[$id]['pos']++;
    }
    // `spl_filter_it_fetch`
    public static function filterFetch($o, $id) {
        while (self::fetch($id, true)) {
            if ($o->accept()) { return; }
            self::$d[$id]['it']->next();
        }
        self::free($id);
    }
    // `spl_dual_it_get_method`: a method the iterator does not have is looked
    // up on the inner iterator.
    public static function forward($o, $name, $args) {
        $inner = self::$d[spl_object_id($o)]['inner'] ?? null;
        if (is_object($inner) && (method_exists($inner, $name) || method_exists($inner, '__call'))) {
            return $inner->$name(...$args);
        }
        throw new Error("Call to undefined method " . get_class($o) . "::$name()");
    }

    // `spl_limit_it_seek`
    public static function limitSeek($o, $pos) {
        $id = self::id($o);
        $s = self::$d[$id];
        self::free($id);
        if ($pos < $s['offset']) {
            throw new OutOfBoundsException("Cannot seek to $pos which is below the offset {$s['offset']}");
        }
        if ($pos - $s['offset'] >= $s['count'] && $s['count'] != -1) {
            throw new OutOfBoundsException("Cannot seek to $pos which is behind offset {$s['offset']} plus count {$s['count']}");
        }
        if ($pos != $s['pos'] && $s['it'] instanceof SeekableIterator) {
            self::free($id);
            $s['it']->seek($pos);
            self::$d[$id]['pos'] = $pos;
            if (self::limitValid($id)) { self::fetch($id, false); }
        } else {
            if ($pos < $s['pos']) { self::rewind($id); }
            while ($pos > self::$d[$id]['pos'] && self::valid($id)) { self::next($id, true); }
            if (self::valid($id)) { self::fetch($id, true); }
        }
    }
    // `spl_limit_it_valid`
    public static function limitValid($id) {
        $s = self::$d[$id];
        if ($s['count'] != -1 && $s['pos'] - $s['offset'] >= $s['count']) { return false; }
        return self::valid($id);
    }

    // `spl_cit_check_flags`
    public static function citFlagsOk($flags) {
        $n = 0;
        foreach ([1, 2, 4, 8] as $bit) { if ($flags & $bit) { $n++; } }
        return $n <= 1;
    }
    public static function citFlagsError($fn, $arg) {
        throw new ValueError("$fn(): Argument #$arg (\$flags) must contain only one of CachingIterator::CALL_TOSTRING, CachingIterator::TOSTRING_USE_KEY, CachingIterator::TOSTRING_USE_CURRENT, or CachingIterator::TOSTRING_USE_INNER");
    }
    // `spl_caching_it_next`
    public static function cachingNext($o, $id) {
        if (!self::fetch($id, true)) {
            self::$d[$id]['flags'] &= ~0x10000;
            return;
        }
        self::$d[$id]['flags'] |= 0x10000;
        $flags = self::$d[$id]['flags'];
        if ($flags & 0x100) {
            $cache = self::$d[$id]['cache'];
            $cache[self::$d[$id]['key']] = self::$d[$id]['data'];
            self::$d[$id]['cache'] = $cache;
        }
        if (self::$d[$id]['recursive']) {
            $inner = self::$d[$id]['inner'];
            [$ok, $has] = __phplang_try_call(fn() => $inner->hasChildren());
            if (!$ok) {
                if (!($flags & 0x10)) { throw $has; }
            } elseif ($has) {
                [$ok, $kids] = __phplang_try_call(fn() => $inner->getChildren());
                if (!$ok) {
                    if (!($flags & 0x10)) { throw $kids; }
                } else {
                    [$ok, $child] = __phplang_try_call(fn() => new RecursiveCachingIterator($kids, $flags & 0xFFFF));
                    if ($ok) {
                        self::$d[$id]['children'] = $child;
                    } elseif (!($flags & 0x10)) {
                        throw $child;
                    }
                }
            }
        }
        if ($flags & (8 | 1)) {
            self::$d[$id]['zstr'] = ($flags & 8) ? (string) self::$d[$id]['inner'] : (string) self::$d[$id]['data'];
        }
        self::next($id, false);
    }
    // The `CIT_FULL_CACHE` check every cache accessor starts with.
    public static function fullCache($o) {
        $id = self::id($o);
        if (!(self::$d[$id]['flags'] & 0x100)) {
            throw new BadMethodCallException(get_class($o) . " does not use a full cache (see CachingIterator::__construct)");
        }
        return $id;
    }
    public static function cacheGet($o, $key) {
        $id = self::fullCache($o);
        $key = (string) $key;
        if (!array_key_exists($key, self::$d[$id]['cache'])) {
            __phplang_warn("Undefined array key \"$key\"");
            return null;
        }
        return self::$d[$id]['cache'][$key];
    }
    public static function cacheSet($o, $key, $value) {
        $id = self::fullCache($o);
        self::$d[$id]['cache'][(string) $key] = $value;
    }
    public static function cacheUnset($o, $key) {
        $id = self::fullCache($o);
        unset(self::$d[$id]['cache'][(string) $key]);
    }

    // `spl_append_it_next_iterator`
    public static function appendNextIterator($id) {
        self::free($id);
        self::$d[$id]['inner'] = null;
        self::$d[$id]['it'] = null;
        $list = self::$d[$id]['list'];
        if (!$list->valid()) { return false; }
        $it = $list->current();
        self::$d[$id]['inner'] = $it;
        self::$d[$id]['it'] = $it;
        self::rewind($id);
        return true;
    }
    // `spl_append_it_fetch`
    public static function appendFetch($id) {
        while (!self::valid($id)) {
            self::$d[$id]['list']->next();
            if (!self::appendNextIterator($id)) { return; }
        }
        self::fetch($id, false);
    }
    // AppendIterator::append
    public static function append($o, $it) {
        $id = self::id($o);
        $list = self::$d[$id]['list'];
        if ($list->valid() && !self::valid($id)) {
            $list->append($it);
            $list->next();
        } else {
            $list->append($it);
        }
        if (self::$d[$id]['it'] === null || !self::valid($id)) {
            if (!$list->valid()) { $list->rewind(); }
            do {
                self::appendNextIterator($id);
            } while (self::$d[$id]['inner'] !== $it);
            self::appendFetch($id);
        }
    }

    // A REGIT_MODE_* argument outside the five modes.
    public static function regexModeError($fn, $arg) {
        throw new ValueError("$fn(): Argument #$arg (\$mode) must be RegexIterator::MATCH, RegexIterator::GET_MATCH, RegexIterator::ALL_MATCHES, RegexIterator::SPLIT, or RegexIterator::REPLACE");
    }
    public static function catchWarning($no, $msg, $file = null, $line = null) {
        self::$warning = $msg;
        return true;
    }
    // `pcre_get_compiled_regex_cache` under EH_THROW: a pattern that does not
    // compile is an InvalidArgumentException carrying the warning, reworded
    // as the constructor's.
    public static function compileRegex($regex, $fn) {
        self::$warning = null;
        set_error_handler([self::class, 'catchWarning']);
        preg_match($regex, '');
        restore_error_handler();
        if (self::$warning !== null) {
            $msg = self::$warning;
            self::$warning = null;
            throw new InvalidArgumentException("$fn(): " . substr($msg, strlen('preg_match(): ')));
        }
    }
    // RegexIterator::accept
    public static function regexAccept($o) {
        $id = self::id($o);
        $s = self::$d[$id];
        if (!$s['has']) { return false; }
        if ($s['flags'] & 1) {
            $subject = (string) $s['key'];
        } else {
            if (is_array($s['data'])) { return false; }
            $subject = (string) $s['data'];
        }
        switch ($s['mode']) {
            case 1:
            case 2:
                self::$d[$id]['data'] = null;
                $count = $s['mode'] === 2
                    ? preg_match_all($s['regex'], $subject, $m, $s['pflags'])
                    : preg_match($s['regex'], $subject, $m, $s['pflags']);
                self::$d[$id]['data'] = $m;
                $r = $count > 0;
                break;
            case 3:
                self::$d[$id]['data'] = null;
                $parts = preg_split($s['regex'], $subject, -1, $s['pflags']);
                self::$d[$id]['data'] = $parts;
                $r = count($parts) > 1;
                break;
            case 4:
                $result = preg_replace($s['regex'], (string) $o->replacement, $subject, -1, $count);
                if ($result === null) { return false; }
                if ($s['flags'] & 1) {
                    self::$d[$id]['key'] = $result;
                } else {
                    self::$d[$id]['data'] = $result;
                }
                $r = $count > 0;
                break;
            default:
                $r = preg_match($s['regex'], $subject) === 1;
        }
        if ($s['flags'] & 2) { return !$r; }
        return $r;
    }

    // --- RecursiveIteratorIterator: `spl_recursive_it_object` ---
    // Each level is [iterator, state, value cached?, cached value].
    // States: RS_NEXT 0, RS_TEST 1, RS_SELF 2, RS_CHILD 3, RS_START 4.

    // `spl_recursive_it_it_construct`
    public static function riConstruct($o, $iterator, $mode, $flags) {
        if ($iterator instanceof IteratorAggregate) {
            $iterator = self::fromAggregate($iterator, get_class($iterator));
        }
        if (!($iterator instanceof RecursiveIterator)) {
            throw new InvalidArgumentException("An instance of RecursiveIterator or IteratorAggregate creating it is required");
        }
        // The hooks a subclass overrides; the base class's are not called.
        $hooks = [];
        foreach (['beginiteration', 'enditeration', 'callhaschildren', 'callgetchildren', 'beginchildren', 'endchildren', 'nextelement'] as $h) {
            $owner = __phplang_method_owner($o, $h);
            $hooks[$h] = $owner !== 'recursiveiteratoriterator' && $owner !== 'recursivetreeiterator';
        }
        self::$d[spl_object_id($o)] = [
            'its' => [[$iterator, 4, false, null]],
            'level' => 0,
            'mode' => $mode,
            'flags' => $flags,
            'max' => -1,
            'inIter' => false,
            'hooks' => $hooks,
        ];
    }
    // SPL_FETCH_SUB_ITERATOR
    public static function riId($o) { return self::id($o); }
    public static function riLevelIt($o) {
        $id = self::id($o);
        return self::$d[$id]['its'][self::$d[$id]['level']][0];
    }
    public static function riDepth($o) { return self::$d[spl_object_id($o)]['level'] ?? 0; }
    public static function riSubIterator($o, $level) {
        $id = spl_object_id($o);
        $cur = self::$d[$id]['level'] ?? 0;
        if ($level === null) {
            $level = $cur;
        } elseif ($level < 0 || $level > $cur) {
            return null;
        }
        self::id($o);
        return self::$d[$id]['its'][$level][0];
    }
    public static function riHasChildren($o) {
        $id = spl_object_id($o);
        if (!isset(self::$d[$id])) { return false; }
        return self::$d[$id]['its'][self::$d[$id]['level']][0]->hasChildren();
    }
    public static function riSetMax($o, $max) {
        if ($max < -1) {
            throw new ValueError("RecursiveIteratorIterator::setMaxDepth(): Argument #1 (\$maxDepth) must be greater than or equal to -1");
        }
        if ($max > 2147483647) { $max = 2147483647; }
        self::$d[self::id($o)]['max'] = $max;
    }
    public static function riGetMax($o) {
        $max = self::$d[spl_object_id($o)]['max'] ?? 0;
        return $max === -1 ? false : $max;
    }
    // `get_current_data` of the active sub-iterator, which keeps the value
    // until that iterator moves.
    public static function riCurrent($o) {
        $id = self::id($o);
        $l = self::$d[$id]['level'];
        if (!self::$d[$id]['its'][$l][2]) {
            self::$d[$id]['its'][$l][3] = self::$d[$id]['its'][$l][0]->current();
            self::$d[$id]['its'][$l][2] = true;
        }
        return self::$d[$id]['its'][$l][3];
    }
    // `spl_recursive_it_valid_ex`
    public static function riValid($o) {
        $id = spl_object_id($o);
        if (!isset(self::$d[$id])) { return false; }
        for ($l = self::$d[$id]['level']; $l >= 0; $l--) {
            if (self::$d[$id]['its'][$l][0]->valid()) { return true; }
        }
        if (self::$d[$id]['hooks']['enditeration'] && self::$d[$id]['inIter']) {
            [$ok, $e] = __phplang_try_call(fn() => $o->endIteration());
            self::$d[$id]['inIter'] = false;
            if (!$ok) { throw $e; }
        }
        self::$d[$id]['inIter'] = false;
        return false;
    }
    // `spl_recursive_it_rewind_ex`
    public static function riRewind($o) {
        $id = self::id($o);
        $thrown = null;
        while (self::$d[$id]['level'] > 0) {
            $l = self::$d[$id]['level'];
            unset(self::$d[$id]['its'][$l]);
            self::$d[$id]['level'] = $l - 1;
            if ($thrown === null) {
                [$ok, $e] = __phplang_try_call(fn() => $o->endChildren());
                if (!$ok) { $thrown = $e; }
            }
        }
        self::$d[$id]['its'][0][1] = 4;
        self::$d[$id]['its'][0][2] = false;
        $it = self::$d[$id]['its'][0][0];
        if ($thrown === null) {
            [$ok, $e] = __phplang_try_call(fn() => $it->rewind());
            if (!$ok) { $thrown = $e; }
        }
        if ($thrown === null && self::$d[$id]['hooks']['beginiteration'] && !self::$d[$id]['inIter']) {
            [$ok, $e] = __phplang_try_call(fn() => $o->beginIteration());
            if (!$ok) { $thrown = $e; }
        }
        self::$d[$id]['inIter'] = true;
        if ($thrown !== null) { throw $thrown; }
        self::riForward($o);
    }
    // `spl_recursive_it_move_forward_ex`
    public static function riForward($o) {
        $id = self::id($o);
        $catch = (self::$d[$id]['flags'] & 16) !== 0;
        $mode = self::$d[$id]['mode'];
        $hooks = self::$d[$id]['hooks'];
        while (true) {
            $l = self::$d[$id]['level'];
            $it = self::$d[$id]['its'][$l][0];
            $state = self::$d[$id]['its'][$l][1];
            if ($state === 0) {
                self::$d[$id]['its'][$l][2] = false;
                [$ok, $e] = __phplang_try_call(fn() => $it->next());
                if (!$ok && !$catch) { throw $e; }
                $state = 4;
            }
            if ($state === 4) {
                $valid = $it->valid();
                if (self::$d[$id]['level'] !== $l || (self::$d[$id]['its'][$l][0] ?? null) !== $it) {
                    return;
                }
                if ($valid) {
                    self::$d[$id]['its'][$l][1] = 1;
                    $state = 1;
                }
            }
            if ($state === 1) {
                [$ok, $has] = $hooks['callhaschildren']
                    ? __phplang_try_call(fn() => $o->callHasChildren())
                    : __phplang_try_call(fn() => $it->hasChildren());
                if (!$ok) {
                    if (!$catch) {
                        self::$d[$id]['its'][$l][1] = 0;
                        throw $has;
                    }
                    $has = false;
                }
                if ($has) {
                    $max = self::$d[$id]['max'];
                    if ($max === -1 || $max > $l) {
                        if ($mode === 0 || $mode === 2) {
                            self::$d[$id]['its'][$l][1] = 3;
                            continue;
                        }
                        if ($mode === 1) {
                            self::$d[$id]['its'][$l][1] = 2;
                            continue;
                        }
                    } elseif ($mode === 0) {
                        self::$d[$id]['its'][$l][1] = 0;
                        continue;
                    }
                }
                $ok = true;
                if ($hooks['nextelement']) {
                    [$ok, $e] = __phplang_try_call(fn() => $o->nextElement());
                }
                self::$d[$id]['its'][$l][1] = 0;
                if (!$ok && !$catch) { throw $e; }
                return;
            }
            if ($state === 2) {
                $ok = true;
                if ($hooks['nextelement'] && ($mode === 1 || $mode === 2)) {
                    [$ok, $e] = __phplang_try_call(fn() => $o->nextElement());
                }
                self::$d[$id]['its'][$l][1] = $mode === 1 ? 3 : 0;
                if (!$ok) { throw $e; }
                return;
            }
            if ($state === 3) {
                [$ok, $child] = $hooks['callgetchildren']
                    ? __phplang_try_call(fn() => $o->callGetChildren())
                    : __phplang_try_call(fn() => $it->getChildren());
                if (!$ok) {
                    if (!$catch) { throw $child; }
                    self::$d[$id]['its'][$l][1] = 0;
                    continue;
                }
                if (!($child instanceof RecursiveIterator)) {
                    throw new UnexpectedValueException("Objects returned by RecursiveIterator::getChildren() must implement RecursiveIterator");
                }
                self::$d[$id]['its'][$l][1] = $mode === 2 ? 2 : 0;
                self::$d[$id]['level'] = $l + 1;
                self::$d[$id]['its'][$l + 1] = [$child, 4, false, null];
                $child->rewind();
                if ($hooks['beginchildren']) {
                    [$ok, $e] = __phplang_try_call(fn() => $o->beginChildren());
                    if (!$ok && !$catch) { throw $e; }
                }
                continue;
            }
            // no more elements at this level
            if ($l > 0) {
                if ($hooks['endchildren']) {
                    [$ok, $e] = __phplang_try_call(fn() => $o->endChildren());
                    if (!$ok && !$catch) { throw $e; }
                }
                $cur = self::$d[$id]['level'];
                if ($cur > 0 && self::$d[$id]['its'][$cur][0] === $it) {
                    unset(self::$d[$id]['its'][$cur]);
                    self::$d[$id]['level'] = $cur - 1;
                }
            } else {
                return;
            }
        }
    }
    // `spl_recursive_it_get_method`
    public static function riForward_method($o, $name, $args) {
        $id = spl_object_id($o);
        if (!isset(self::$d[$id])) {
            throw new Error("The " . get_class($o) . " instance wasn't initialized properly");
        }
        $sub = self::$d[$id]['its'][self::$d[$id]['level']][0];
        if (method_exists($sub, $name) || method_exists($sub, '__call')) {
            return $sub->$name(...$args);
        }
        throw new Error("Call to undefined method " . get_class($o) . "::$name()");
    }

    // --- RecursiveTreeIterator ---
    public static function treeParts($o) {
        return self::$tree[spl_object_id($o)] ?? [["", "| ", "  ", "|-", "\\-", ""], ""];
    }
    public static function treeSetPart($o, $part, $value) {
        if ($part < 0 || $part > 5) {
            throw new ValueError("RecursiveTreeIterator::setPrefixPart(): Argument #1 (\$part) must be a RecursiveTreeIterator::PREFIX_* constant");
        }
        $t = self::treeParts($o);
        $t[0][$part] = $value;
        self::$tree[spl_object_id($o)] = $t;
    }
    public static function treeSetPostfix($o, $postfix) {
        $t = self::treeParts($o);
        $t[1] = $postfix;
        self::$tree[spl_object_id($o)] = $t;
    }
    // `spl_recursive_tree_iterator_get_prefix`
    public static function treePrefix($o) {
        $id = self::id($o);
        [$prefix] = self::treeParts($o);
        $str = $prefix[0];
        $level = self::$d[$id]['level'];
        for ($l = 0; $l < $level; $l++) {
            $str .= self::$d[$id]['its'][$l][0]->hasNext() === true ? $prefix[1] : $prefix[2];
        }
        $str .= self::$d[$id]['its'][$level][0]->hasNext() === true ? $prefix[3] : $prefix[4];
        return $str . $prefix[5];
    }
    // `spl_recursive_tree_iterator_get_entry`
    public static function treeEntry($o) {
        $data = self::riCurrent($o);
        return is_array($data) ? "Array" : (string) $data;
    }
}
class EmptyIterator implements Iterator {
    public function current(): never { throw new BadMethodCallException("Accessing the value of an EmptyIterator"); }
    public function next(): void {}
    public function key(): never { throw new BadMethodCallException("Accessing the key of an EmptyIterator"); }
    public function valid(): false { return false; }
    public function rewind(): void {}
}
class IteratorIterator implements OuterIterator {
    public function __construct(Traversable $iterator, ?string $class = null) { __SplIt::constructIteratorIterator($this, $iterator, $class); }
    public function getInnerIterator(): ?Iterator { return __SplIt::get($this, 'inner'); }
    public function rewind(): void { $id = __SplIt::id($this); __SplIt::rewind($id); __SplIt::fetch($id, true); }
    public function valid(): bool { return __SplIt::get($this, 'has'); }
    public function key(): mixed { return __SplIt::get($this, 'key'); }
    public function current(): mixed { return __SplIt::get($this, 'data'); }
    public function next(): void { $id = __SplIt::id($this); __SplIt::next($id, true); __SplIt::fetch($id, true); }
    public function __call($name, $args) { return __SplIt::forward($this, $name, $args); }
}
abstract class FilterIterator extends IteratorIterator {
    abstract public function accept(): bool;
    public function __construct(Iterator $iterator) { __SplIt::once($this, 'FilterIterator'); __SplIt::construct($this, $iterator, ['it' => $iterator]); }
    public function rewind(): void { $id = __SplIt::id($this); __SplIt::rewind($id); __SplIt::filterFetch($this, $id); }
    public function next(): void { $id = __SplIt::id($this); __SplIt::next($id, true); __SplIt::filterFetch($this, $id); }
}
class CallbackFilterIterator extends FilterIterator {
    public function __construct(Iterator $iterator, callable $callback) { __SplIt::once($this, 'CallbackFilterIterator'); __SplIt::construct($this, $iterator, ['it' => $iterator, 'callback' => $callback]); }
    public function accept(): bool {
        $id = __SplIt::id($this);
        if (!__SplIt::get($this, 'has') || !__SplIt::get($this, 'hasKey')) { return false; }
        $callback = __SplIt::get($this, 'callback');
        return $callback(__SplIt::get($this, 'data'), __SplIt::get($this, 'key'), __SplIt::get($this, 'inner'));
    }
}
abstract class RecursiveFilterIterator extends FilterIterator implements RecursiveIterator {
    public function __construct(RecursiveIterator $iterator) { __SplIt::once($this, 'RecursiveFilterIterator'); __SplIt::construct($this, $iterator, ['it' => $iterator]); }
    public function hasChildren(): bool { return __SplIt::get($this, 'inner')->hasChildren(); }
    public function getChildren(): ?RecursiveFilterIterator { return new static(__SplIt::get($this, 'inner')->getChildren()); }
}
class RecursiveCallbackFilterIterator extends CallbackFilterIterator implements RecursiveIterator {
    public function __construct(RecursiveIterator $iterator, callable $callback) { __SplIt::once($this, 'RecursiveCallbackFilterIterator'); __SplIt::construct($this, $iterator, ['it' => $iterator, 'callback' => $callback]); }
    public function hasChildren(): bool { return __SplIt::get($this, 'inner')->hasChildren(); }
    public function getChildren(): RecursiveCallbackFilterIterator {
        $children = __SplIt::get($this, 'inner')->getChildren();
        return new static($children, __SplIt::get($this, 'callback'));
    }
}
class ParentIterator extends RecursiveFilterIterator {
    public function __construct(RecursiveIterator $iterator) { __SplIt::once($this, 'ParentIterator'); __SplIt::construct($this, $iterator, ['it' => $iterator]); }
    public function accept(): bool { return __SplIt::get($this, 'inner')->hasChildren(); }
}
class LimitIterator extends IteratorIterator {
    public function __construct(Iterator $iterator, int $offset = 0, int $limit = -1) {
        __SplIt::once($this, 'LimitIterator');
        if ($offset < 0) { throw new ValueError("LimitIterator::__construct(): Argument #2 (\$offset) must be greater than or equal to 0"); }
        if ($limit < -1) { throw new ValueError("LimitIterator::__construct(): Argument #3 (\$limit) must be greater than or equal to -1"); }
        __SplIt::construct($this, $iterator, ['it' => $iterator, 'offset' => $offset, 'count' => $limit]);
    }
    public function rewind(): void { $id = __SplIt::id($this); __SplIt::rewind($id); __SplIt::limitSeek($this, __SplIt::get($this, 'offset')); }
    public function valid(): bool {
        $id = __SplIt::id($this);
        $count = __SplIt::get($this, 'count');
        return ($count == -1 || __SplIt::get($this, 'pos') - __SplIt::get($this, 'offset') < $count) && __SplIt::get($this, 'has');
    }
    public function next(): void {
        $id = __SplIt::id($this);
        __SplIt::next($id, true);
        $count = __SplIt::get($this, 'count');
        if ($count == -1 || __SplIt::get($this, 'pos') - __SplIt::get($this, 'offset') < $count) { __SplIt::fetch($id, true); }
    }
    public function seek(int $offset): int { __SplIt::id($this); __SplIt::limitSeek($this, $offset); return __SplIt::get($this, 'pos'); }
    public function getPosition(): int { return __SplIt::get($this, 'pos'); }
}
class CachingIterator extends IteratorIterator implements ArrayAccess, Countable, Stringable {
    const CALL_TOSTRING = 1;
    const CATCH_GET_CHILD = 16;
    const TOSTRING_USE_KEY = 2;
    const TOSTRING_USE_CURRENT = 4;
    const TOSTRING_USE_INNER = 8;
    const FULL_CACHE = 256;
    public function __construct(Iterator $iterator, int $flags = CachingIterator::CALL_TOSTRING) {
        __SplIt::once($this, 'CachingIterator');
        if (!__SplIt::citFlagsOk($flags)) { __SplIt::citFlagsError('CachingIterator::__construct', 2); }
        __SplIt::construct($this, $iterator, ['it' => $iterator, 'flags' => $flags & 0xFFFF, 'cache' => [], 'zstr' => null, 'children' => null, 'recursive' => false]);
    }
    public function rewind(): void { $id = __SplIt::id($this); __SplIt::rewind($id); __SplIt::put($this, 'cache', []); __SplIt::cachingNext($this, $id); }
    public function valid(): bool { return (__SplIt::get($this, 'flags') & 0x10000) !== 0; }
    public function next(): void { __SplIt::cachingNext($this, __SplIt::id($this)); }
    public function hasNext(): bool { return __SplIt::valid(__SplIt::id($this)); }
    public function __toString(): string {
        $flags = __SplIt::get($this, 'flags');
        if (!($flags & (1 | 2 | 4 | 8))) {
            throw new BadMethodCallException(get_class($this) . " does not fetch string value (see CachingIterator::__construct)");
        }
        if ($flags & 2) { return (string) __SplIt::get($this, 'key'); }
        if ($flags & 4) { return (string) __SplIt::get($this, 'data'); }
        return (string) __SplIt::get($this, 'zstr');
    }
    public function getFlags(): int { return __SplIt::get($this, 'flags'); }
    public function setFlags(int $flags): void {
        $old = __SplIt::get($this, 'flags');
        if (!__SplIt::citFlagsOk($flags)) { __SplIt::citFlagsError('CachingIterator::setFlags', 1); }
        if (($old & 1) && !($flags & 1)) { throw new InvalidArgumentException("Unsetting flag CALL_TO_STRING is not possible"); }
        if (($old & 8) && !($flags & 8)) { throw new InvalidArgumentException("Unsetting flag TOSTRING_USE_INNER is not possible"); }
        if (($flags & 0x100) && !($old & 0x100)) { __SplIt::put($this, 'cache', []); }
        __SplIt::put($this, 'flags', ($old & ~0xFFFF) | ($flags & 0xFFFF));
    }
    public function offsetGet($key): mixed { return __SplIt::cacheGet($this, $key); }
    public function offsetSet($key, mixed $value): void { __SplIt::cacheSet($this, $key, $value); }
    public function offsetUnset($key): void { __SplIt::cacheUnset($this, $key); }
    public function offsetExists($key): bool { $id = __SplIt::fullCache($this); return array_key_exists((string) $key, __SplIt::get($this, 'cache')); }
    public function getCache(): array { __SplIt::fullCache($this); return __SplIt::get($this, 'cache'); }
    public function count(): int { __SplIt::fullCache($this); return count(__SplIt::get($this, 'cache')); }
}
class RecursiveCachingIterator extends CachingIterator implements RecursiveIterator {
    public function __construct($iterator, int $flags = RecursiveCachingIterator::CALL_TOSTRING) {
        __SplIt::once($this, 'RecursiveCachingIterator');
        if (!($iterator instanceof RecursiveIterator)) {
            throw new TypeError("RecursiveCachingIterator::__construct(): Argument #1 (\$iterator) must be of type RecursiveIterator, " . get_debug_type($iterator) . " given");
        }
        if (!__SplIt::citFlagsOk($flags)) { __SplIt::citFlagsError('RecursiveCachingIterator::__construct', 2); }
        __SplIt::construct($this, $iterator, ['it' => $iterator, 'flags' => $flags & 0xFFFF, 'cache' => [], 'zstr' => null, 'children' => null, 'recursive' => true]);
    }
    public function hasChildren(): bool { return __SplIt::get($this, 'children') !== null; }
    public function getChildren(): ?RecursiveCachingIterator { return __SplIt::get($this, 'children'); }
}
class NoRewindIterator extends IteratorIterator {
    public function __construct(Iterator $iterator) { __SplIt::once($this, 'NoRewindIterator'); __SplIt::construct($this, $iterator, ['it' => $iterator, 'cached' => false]); }
    public function rewind(): void {}
    public function valid(): bool { return (bool) __SplIt::get($this, 'inner')->valid(); }
    public function key(): mixed { return __SplIt::get($this, 'inner')->key(); }
    public function current(): mixed {
        if (!__SplIt::get($this, 'cached')) {
            __SplIt::put($this, 'data', __SplIt::get($this, 'inner')->current());
            __SplIt::put($this, 'cached', true);
        }
        return __SplIt::get($this, 'data');
    }
    public function next(): void { __SplIt::put($this, 'cached', false); __SplIt::get($this, 'inner')->next(); }
}
class InfiniteIterator extends IteratorIterator {
    public function __construct(Iterator $iterator) { __SplIt::once($this, 'InfiniteIterator'); __SplIt::construct($this, $iterator, ['it' => $iterator]); }
    public function next(): void {
        $id = __SplIt::id($this);
        __SplIt::next($id, true);
        if (__SplIt::valid($id)) {
            __SplIt::fetch($id, false);
        } else {
            __SplIt::rewind($id);
            if (__SplIt::valid($id)) { __SplIt::fetch($id, false); }
        }
    }
}
class AppendIterator extends IteratorIterator {
    public function __construct() { __SplIt::once($this, 'AppendIterator'); __SplIt::construct($this, null, ['it' => null, 'list' => new ArrayIterator()]); }
    public function append(Iterator $iterator): void { __SplIt::append($this, $iterator); }
    public function rewind(): void {
        $id = __SplIt::id($this);
        __SplIt::get($this, 'list')->rewind();
        if (__SplIt::appendNextIterator($id)) { __SplIt::appendFetch($id); }
    }
    public function valid(): bool { return __SplIt::get($this, 'has'); }
    public function current(): mixed { $id = __SplIt::id($this); __SplIt::fetch($id, true); return __SplIt::get($this, 'data'); }
    public function next(): void {
        $id = __SplIt::id($this);
        if (__SplIt::valid($id)) { __SplIt::next($id, true); }
        __SplIt::appendFetch($id);
    }
    public function getIteratorIndex(): ?int {
        return __SplIt::get($this, 'list')->key();
    }
    public function getArrayIterator(): ArrayIterator { return __SplIt::get($this, 'list'); }
}
class RegexIterator extends FilterIterator {
    const USE_KEY = 1;
    const INVERT_MATCH = 2;
    const MATCH = 0;
    const GET_MATCH = 1;
    const ALL_MATCHES = 2;
    const SPLIT = 3;
    const REPLACE = 4;
    public ?string $replacement = null;
    public function __construct(Iterator $iterator, string $pattern, int $mode = RegexIterator::MATCH, int $flags = 0, int $pregFlags = 0) {
        __SplIt::once($this, 'RegexIterator');
        if ($mode < 0 || $mode >= 5) { __SplIt::regexModeError('RegexIterator::__construct', 3); }
        __SplIt::compileRegex($pattern, 'RegexIterator::__construct');
        __SplIt::construct($this, $iterator, ['it' => $iterator, 'regex' => $pattern, 'mode' => $mode, 'flags' => $flags, 'pflags' => $pregFlags]);
    }
    public function accept(): bool { return __SplIt::regexAccept($this); }
    public function getMode(): int { return __SplIt::get($this, 'mode'); }
    public function setMode(int $mode): void {
        if ($mode < 0 || $mode >= 5) { __SplIt::regexModeError('RegexIterator::setMode', 1); }
        __SplIt::put($this, 'mode', $mode);
    }
    public function getFlags(): int { return __SplIt::get($this, 'flags'); }
    public function setFlags(int $flags): void { __SplIt::put($this, 'flags', $flags); }
    public function getRegex(): string { return __SplIt::get($this, 'regex'); }
    public function getPregFlags(): int { return __SplIt::get($this, 'pflags'); }
    public function setPregFlags(int $pregFlags): void { __SplIt::put($this, 'pflags', $pregFlags); }
}
class RecursiveRegexIterator extends RegexIterator implements RecursiveIterator {
    public function __construct(RecursiveIterator $iterator, string $pattern, int $mode = RecursiveRegexIterator::MATCH, int $flags = 0, int $pregFlags = 0) {
        __SplIt::once($this, 'RecursiveRegexIterator');
        if ($mode < 0 || $mode >= 5) { __SplIt::regexModeError('RecursiveRegexIterator::__construct', 3); }
        __SplIt::compileRegex($pattern, 'RecursiveRegexIterator::__construct');
        __SplIt::construct($this, $iterator, ['it' => $iterator, 'regex' => $pattern, 'mode' => $mode, 'flags' => $flags, 'pflags' => $pregFlags]);
    }
    public function accept(): bool {
        if (!__SplIt::get($this, 'has')) { return false; }
        $data = __SplIt::get($this, 'data');
        if (is_array($data)) { return count($data) > 0; }
        return __SplIt::regexAccept($this);
    }
    public function hasChildren(): bool { return __SplIt::get($this, 'inner')->hasChildren(); }
    public function getChildren(): RecursiveRegexIterator {
        $children = __SplIt::get($this, 'inner')->getChildren();
        return new static($children, __SplIt::get($this, 'regex'), __SplIt::get($this, 'mode'), __SplIt::get($this, 'flags'), __SplIt::get($this, 'pflags'));
    }
}
class RecursiveIteratorIterator implements OuterIterator {
    const LEAVES_ONLY = 0;
    const SELF_FIRST = 1;
    const CHILD_FIRST = 2;
    const CATCH_GET_CHILD = 16;
    public function __construct(Traversable $iterator, int $mode = RecursiveIteratorIterator::LEAVES_ONLY, int $flags = 0) { __SplIt::riConstruct($this, $iterator, $mode, $flags); }
    public function rewind(): void { __SplIt::riRewind($this); }
    public function valid(): bool { return __SplIt::riValid($this); }
    public function key(): mixed { return __SplIt::riLevelIt($this)->key(); }
    public function current(): mixed { return __SplIt::riCurrent($this); }
    public function next(): void { __SplIt::riForward($this); }
    public function getDepth(): int { return __SplIt::riDepth($this); }
    public function getSubIterator(?int $level = null): ?RecursiveIterator { return __SplIt::riSubIterator($this, $level); }
    public function getInnerIterator(): RecursiveIterator { return __SplIt::riLevelIt($this); }
    public function beginIteration(): void {}
    public function endIteration(): void {}
    public function callHasChildren(): bool { return __SplIt::riHasChildren($this); }
    public function callGetChildren(): ?RecursiveIterator { return __SplIt::riLevelIt($this)->getChildren(); }
    public function beginChildren(): void {}
    public function endChildren(): void {}
    public function nextElement(): void {}
    public function setMaxDepth(int $maxDepth = -1): void { __SplIt::riSetMax($this, $maxDepth); }
    public function getMaxDepth(): int|false { return __SplIt::riGetMax($this); }
    public function __call($name, $args) { return __SplIt::riForward_method($this, $name, $args); }
}
class RecursiveTreeIterator extends RecursiveIteratorIterator {
    const BYPASS_CURRENT = 4;
    const BYPASS_KEY = 8;
    const PREFIX_LEFT = 0;
    const PREFIX_MID_HAS_NEXT = 1;
    const PREFIX_MID_LAST = 2;
    const PREFIX_END_HAS_NEXT = 3;
    const PREFIX_END_LAST = 4;
    const PREFIX_RIGHT = 5;
    public function __construct(RecursiveIterator|IteratorAggregate $iterator, int $flags = RecursiveTreeIterator::BYPASS_KEY, int $cachingIteratorFlags = CachingIterator::CATCH_GET_CHILD, int $mode = RecursiveTreeIterator::SELF_FIRST) {
        if ($iterator instanceof IteratorAggregate) { $iterator = __SplIt::fromAggregate($iterator, get_class($iterator)); }
        __SplIt::riConstruct($this, new RecursiveCachingIterator($iterator, $cachingIteratorFlags), $mode, $flags);
    }
    public function key(): mixed {
        $key = __SplIt::riLevelIt($this)->key();
        if (__SplIt::get($this, 'flags') & 8) { return $key; }
        $key = (string) $key;
        return __SplIt::treePrefix($this) . $key . __SplIt::treeParts($this)[1];
    }
    public function current(): mixed {
        __SplIt::id($this);
        if (__SplIt::get($this, 'flags') & 4) { return __SplIt::riCurrent($this); }
        $entry = __SplIt::treeEntry($this);
        return __SplIt::treePrefix($this) . $entry . __SplIt::treeParts($this)[1];
    }
    public function getPrefix(): string { return __SplIt::treePrefix($this); }
    public function setPostfix(string $postfix): void { __SplIt::treeSetPostfix($this, $postfix); }
    public function setPrefixPart(int $part, string $value): void { __SplIt::treeSetPart($this, $part, $value); }
    public function getEntry(): string { __SplIt::id($this); return __SplIt::treeEntry($this); }
    public function getPostfix(): string { __SplIt::id($this); return __SplIt::treeParts($this)[1]; }
}
// ext/spl/spl_array.c's RecursiveArrayIterator: an array or object element
// has children, which are iterated by an iterator of this same class.
class RecursiveArrayIterator extends ArrayIterator implements RecursiveIterator {
    const CHILD_ARRAYS_ONLY = 4;
    public function hasChildren(): bool {
        if (!parent::valid()) { return false; }
        $e = parent::current();
        return is_array($e) || (is_object($e) && (parent::getFlags() & self::CHILD_ARRAYS_ONLY) === 0);
    }
    public function getChildren(): ?RecursiveArrayIterator {
        if (!parent::valid()) { return null; }
        $e = parent::current();
        if (is_object($e)) {
            if (parent::getFlags() & self::CHILD_ARRAYS_ONLY) { return null; }
            if (is_a($e, get_class($this))) { return $e; }
        }
        return new static($e, parent::getFlags());
    }
}
"#;

/// The function-table key the `Closure` a `Closure::fromCallable($c)` builds
/// runs out of: a variadic forwarder holding the callable it was made from.
///
/// Spelled with a leading `@` so no PHP program can declare, call, or see a
/// function by this name — the same convention the compiler's own synthetic
/// closure definitions use — and with a name of its own rather than the
/// `@closureN` the snippet below compiles to, because a user program's Nth
/// closure would otherwise be merged over it.
pub const FROM_CALLABLE_FORWARDER: &str = "@__from_callable";

/// The body of that forwarder, as PHP: everything the closure is given goes to
/// the captured callable, positional and named alike — which is exactly what
/// `f(...)` already lowers to, so the two forms of "a Closure over a callable"
/// forward identically.
const FROM_CALLABLE_SRC: &str = r#"<?php $f = fn(...$args) => call_user_func_array($c, $args);"#;

/// `ext/random`'s exception, which `random_bytes` / `random_int` throw when the
/// OS CSPRNG fails (`stdlib::csprng`). It lives in a namespace, which the
/// unnamespaced prelude above cannot open, so it compiles from its own unit.
const RANDOM_PRELUDE: &str = r#"<?php
namespace Random;
class RandomException extends \Exception {}
"#;

/// A compiled program's installable definitions: `(functions, classes)`.
type PreludeDefs = (Vec<(String, host::FuncDef)>, Vec<(String, host::ClassDef)>);

/// The compiled prelude's functions and classes, built once and merged onto every
/// fresh host before the user program (so user declarations of the same name win).
fn prelude_defs() -> &'static PreludeDefs {
    use std::sync::OnceLock;
    static CACHE: OnceLock<PreludeDefs> = OnceLock::new();
    CACHE.get_or_init(|| {
        let src = format!(
            "{EXCEPTION_PRELUDE}{DATETIME_PRELUDE}{HASH_PRELUDE}{WEAKREF_PRELUDE}{SPL_PRELUDE}{REFLECTION_PRELUDE}"
        );
        // NOT `compile`: that applies the file-level facts of whatever it is given,
        // and this runs from `load_merged` — after the user program was compiled —
        // so going through it would reset the typing mode the user's own
        // `declare(strict_types=1)` had just set. The prelude declares nothing of
        // the sort, so it has no facts of its own to apply.
        let stmts = parser::parse(&src).expect("prelude parses");
        let mut prog = compiler::compile_prelude(&stmts).expect("prelude compiles");
        let random = parser::parse(RANDOM_PRELUDE).expect("random prelude parses");
        prog.classes.extend(
            compiler::compile_prelude(&random)
                .expect("random prelude compiles")
                .classes,
        );
        let mut functions = prog.functions;
        // The forwarder is compiled from its own snippet — the closure literal
        // is the only definition it produces — and re-keyed to a name of its
        // own before it joins the table.
        let snippet = parser::parse(FROM_CALLABLE_SRC).expect("forwarder parses");
        let fwd = compiler::compile(&snippet, false).expect("forwarder compiles");
        let (_, def) = fwd
            .functions
            .into_iter()
            .next()
            .expect("the forwarder snippet defines one closure");
        functions.push((FROM_CALLABLE_FORWARDER.to_string(), def));
        let types = prog
            .classes
            .iter()
            .map(|(lname, def)| {
                let kind = if def.is_trait {
                    "trait"
                } else if def.is_interface {
                    "interface"
                } else if def.is_enum {
                    "enum"
                } else {
                    "class"
                };
                (lname.clone(), (def.name.clone(), kind))
            })
            .collect();
        let _ = PRELUDE_TYPES.set(types);
        (functions, prog.classes)
    })
}

/// The prelude's types by lowercased name: `(declared spelling, kind word)`.
/// Filled once the prelude has compiled, so it is empty WHILE the prelude
/// compiles — which is what keeps the prelude from colliding with itself.
static PRELUDE_TYPES: std::sync::OnceLock<rustc_hash::FxHashMap<String, (String, &'static str)>> =
    std::sync::OnceLock::new();

/// A type the PHP-written prelude declares, as a redeclaration names it:
/// `(declared spelling, kind word)`. Built-in to a PHP program, so a user
/// declaration of the same name is refused as it would be for any other
/// built-in type.
pub(crate) fn prelude_type(lname: &str) -> Option<(String, &'static str)> {
    PRELUDE_TYPES.get()?.get(lname).cloned()
}

/// The classes the PHP-written prelude declares, keyed by lowercased name.
/// The prelude is merged onto the host only once the user program has
/// compiled, so a check made WHILE compiling (a class linking against
/// `Exception`) reads the definitions from here.
///
/// Compiles the prelude on first use, so it must not be reached while the
/// prelude itself is compiling, nor from inside `host::with_host`.
pub(crate) fn prelude_classes() -> &'static [(String, host::ClassDef)] {
    &prelude_defs().1
}

/// Merge an already-compiled program onto the current host (install the exception
/// prelude, then the program's user functions/classes/try-defs) and return the
/// main chunk for the caller to run.
pub fn load_merged(prog: compiler::Program) -> fusevm::Chunk {
    let compiler::Program {
        main,
        main_locals,
        main_order,
        main_promoted,
        functions,
        classes,
        try_defs,
        diags: _,
        counters,
        fn_sites,
        class_sites,
    } = prog;
    let (prelude_fns, prelude_classes) = prelude_defs();
    host::with_host(|h| {
        // Prelude first, then the user program — a user redeclaration wins.
        h.load_program(prelude_fns.clone());
        h.load_classes(prelude_classes.clone());
        h.load_program(functions);
        h.load_classes(classes);
        h.load_try_defs(try_defs);
        let file = h.script_name().to_string();
        h.record_decl_sites(&file, fn_sites, class_sites);
        // A later `include` or `eval` continues this program's numbering.
        h.set_compile_counters(counters);
        // Reserve the global frame's slots before the chunk that numbered them
        // runs. An `include`/`eval` later in the same frame keeps the by-name
        // path, which reaches these same slots.
        h.seed_global_slots(&main_locals);
        h.set_main_layout(main_order, main_promoted);
    });
    main
}

/// Run an already-compiled program on the current host.
///
/// Compile-time notices are flushed first, ahead of the program's own output —
/// the reference engine finishes reading the whole source before it executes a
/// line of it, so `echo "a"; $v = 1; echo "${v}";` prints the deprecation notice
/// BEFORE the `a`, not between the two statements.
///
/// The output buffers still open at the end are finalized (their handlers run
/// with `PHP_OUTPUT_HANDLER_FINAL`), as the end of a request does. A whole
/// program defers that to `run_request`'s shutdown sequence, after the
/// shutdown functions and destructors, whose output the buffers still take.
pub fn run_compiled(prog: compiler::Program) -> Result<Value, String> {
    let r = run_compiled_open(prog);
    crate::stdlib::output::end_all();
    r
}

/// [`run_compiled`] leaving the output buffers open.
fn run_compiled_open(mut prog: compiler::Program) -> Result<Value, String> {
    let diags = std::mem::take(&mut prog.diags);
    let main = load_merged(prog);
    if !diags.is_empty() {
        host::with_host(|h| {
            for d in &diags {
                h.diagnose(d.severity, d.level, d.line, &d.msg);
            }
        });
    }
    host::run_main(main)
}

/// Run a WHOLE program: [`run_compiled`] with the output buffers left open,
/// then the request shutdown that frees what the program left behind —
/// shutdown functions, destructors, suspended generators, and last the
/// output buffers (see [`host::request_shutdown`] and
/// [`host::shutdown_generators`]). The REPL runs each line through
/// [`run_compiled`] alone, since its variables outlive the line.
///
/// Shutdown functions run after ANY fatal the runtime displayed; destructors
/// only after a clean end, an `exit`, or an uncaught exception — a fatal
/// error proper marks every object destructed in the reference.
pub(crate) fn run_request(prog: compiler::Program) -> Result<Value, String> {
    let r = run_compiled_open(prog);
    let destructors = match &r {
        Ok(_) => true,
        Err(e) => e.starts_with("Fatal error:  Uncaught "),
    };
    let shut = if r.is_ok() || host::fatal_reported() {
        host::request_shutdown(destructors)
    } else {
        Ok(())
    };
    host::shutdown_generators();
    match (r, shut) {
        (Err(e), _) | (Ok(_), Err(e)) => Err(e),
        (Ok(v), Ok(())) => Ok(v),
    }
}

/// Parse, compile, load, and run a PHP source string on a fresh host; return the
/// value of the last top-level expression.
pub fn eval_str(src: &str) -> Result<Value, String> {
    host::reset_host();
    run_request(compile(src)?)
}

/// Compile `src`, displaying a syntax error the way the PHP CLI does: a
/// `Parse error: …` copy on stdout and a `PHP `-prefixed copy on stderr. The
/// error is still returned so the caller can take an exit status from it, with
/// [`host::fatal_reported`] marking that it has already been shown.
///
/// Only the *parser*'s failures are framed this way. A later compile failure has
/// no reference equivalent (it is scaffold-specific), so it falls through to the
/// terse `php: …` form.
fn compile_cli(src: &str) -> Result<compiler::Program, String> {
    if let Err(e) = parser::parse_meta(src) {
        // A `declare(strict_types=…)` violation is a `Fatal error`, not a
        // `Parse error`; both stop the run before it produces any output.
        host::with_host(|h| h.fatal(e.severity, &e.message));
        return Err(e.message);
    }
    compile(src).map_err(|e| match e.strip_prefix(compiler::COMPILE_FATAL) {
        // A compile-time `Fatal error` the compiler raised (a redeclaration):
        // shown in PHP's shape, exactly as the parser's are.
        Some(body) => {
            host::with_host(|h| h.fatal("Fatal error", body));
            body.to_string()
        }
        None => e,
    })
}

/// [`eval_str`] for the CLI: identical, except a syntax error is displayed in
/// PHP's shape rather than left to the caller to print tersely, and the trailing
/// command-line arguments reach the program as `$argv`.
pub fn eval_cli(src: &str, args: &[String]) -> Result<Value, String> {
    host::reset_host();
    // `php -r` code names itself `Command line code` in diagnostics and
    // `__FILE__`, but `Standard input code` in `$argv[0]`. The reference really
    // does disagree with itself here, so the two are set from different values.
    host::with_host(|h| {
        h.set_script_args(None, args);
        h.disable_exception_handler();
    });
    run_request(compile_cli(src)?)
}

/// [`eval_cli`] for a script read from standard input (`php < script.php`).
///
/// The SAME source names itself differently under the three CLI entry points, so
/// the name cannot be inferred from the text: `php -r` code is `Command line
/// code`, a named file is its resolved path, and stdin is `Standard input code`.
/// Every diagnostic quotes it, and so does `__FILE__`.
pub fn eval_stdin_cli(src: &str, args: &[String]) -> Result<Value, String> {
    host::reset_host();
    host::with_host(|h| {
        h.set_script_name("Standard input code");
        h.set_script_args(None, args);
    });
    run_request(compile_cli(src)?)
}

/// [`eval_file`] for the CLI — see [`eval_cli`].
pub fn eval_file_cli(path: &str, args: &[String]) -> Result<Value, String> {
    let src = std::fs::read(path)
        .map(|b| crate::bytes::from_bytes(&b))
        .map_err(|e| format!("cannot read {path}: {e}"))?;
    host::reset_host();
    set_script_name(path);
    // `$argv[0]` is the path AS WRITTEN on the command line, where the diagnostic
    // name just set is the resolved one — `php sub/s.php` reports both.
    host::with_host(|h| h.set_script_args(Some(path), args));
    run_request(compile_cli(&src)?)
}

/// Read and run a `.php` file on a fresh host.
pub fn eval_file(path: &str) -> Result<Value, String> {
    let src = std::fs::read(path)
        .map(|b| crate::bytes::from_bytes(&b))
        .map_err(|e| format!("cannot read {path}: {e}"))?;
    host::reset_host();
    set_script_name(path);
    run_request(compile(&src)?)
}

/// Name this run's source for diagnostics. PHP prints the script's *resolved*
/// path in `Warning: … in <file> on line N`, so a relative argument is expanded;
/// a fresh host defaults to `"Command line code"`, which is what `php -r` uses.
fn set_script_name(path: &str) {
    let resolved = std::fs::canonicalize(path)
        .map(|p| p.display().to_string())
        .unwrap_or_else(|_| path.to_string());
    host::with_host(|h| h.set_script_name(resolved));
}

/// Read and run a `.php` file under the DAP debugger (per-statement line markers,
/// tracing JIT disabled so the markers fire).
pub fn eval_file_debug(path: &str) -> Result<Value, String> {
    let src = std::fs::read(path)
        .map(|b| crate::bytes::from_bytes(&b))
        .map_err(|e| format!("cannot read {path}: {e}"))?;
    let prog = compile_debug(&src)?;
    host::reset_host();
    set_script_name(path);
    host::set_debug_mode(true);
    let r = run_compiled(prog);
    host::set_debug_mode(false);
    r
}

/// Evaluate `src` with `vars` bound and return the captured program output —
/// [`eval_capture`] for an embedder that has input to hand the program.
///
/// The variables are seeded *after* the host reset that starts every run, which
/// is the whole reason this exists: setting them beforehand through
/// `host::with_host` cannot work, because the reset wipes them. Each is bound as
/// an ordinary PHP variable, so `("stdin", "…")` reads as `$stdin`.
///
/// ```no_run
/// let out = phplang::eval_capture_with("<?php echo strtoupper($stdin);", &[("stdin", "hi")]);
/// assert_eq!(out.unwrap(), "HI");
/// ```
pub fn eval_capture_with(src: &str, vars: &[(&str, &str)]) -> Result<String, String> {
    host::reset_host();
    host::with_host(|h| {
        for (name, text) in vars {
            h.set_var(name, Value::Str(std::sync::Arc::new(text.to_string())));
        }
        h.begin_capture();
    });
    let r = run_compiled(compile(src)?);
    let out = host::with_host(|h| h.end_capture());
    r.map(|_| out)
}

/// Evaluate `src` and return the captured program output. The convenience entry
/// point for tests: installs an output buffer, runs, and returns what `echo`
/// wrote (PHP is output-oriented — its observable result is stdout, not a value).
pub fn eval_capture(src: &str) -> Result<String, String> {
    host::reset_host();
    host::with_host(|h| h.begin_capture());
    let r = run_compiled(compile(src)?);
    let out = host::with_host(|h| h.end_capture());
    r.map(|_| out)
}