bamts-native 0.1.0

Native linkage, FFI, and execution bridge for BamTS
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
//! The native runtime bridge: the typed helper-call algebra, the exact 32
//! `bamts_*` C-ABI helper exports, the panic- and nesting-safe thread-local
//! [`NativeOps`] dispatch seam, and the feature-gated JIT and AOT linkage
//! surfaces.
//!
//! # Where the unsafe lives
//!
//! Generated CLIF (JIT) and linked object code (AOT) own control flow but call
//! back into this crate for every value/heap/host operation. This module is the
//! *only* place raw pointers from that generated code are turned into safe Rust:
//!
//! * The exported `bamts_*` wrappers ([`bamts_load_constant`] …) receive raw
//!   `*mut ShadowFrame` / `*mut Completion` from CLIF, validate them into a safe
//!   [`NativeFrame`], and dispatch to the current [`NativeOps`]. They never
//!   unwind across the C boundary: any panic, missing dispatcher, or invalid
//!   frame is turned into a [`CompletionTag::FatalTrap`].
//! * [`JitEntry`] (feature `jit-entry`) wraps a finalized `cranelift-jit`
//!   entry-point pointer, bound to its `JITModule` lifetime.
//! * [`linked_program`] (feature `aot-image`) reads the generated external
//!   `bamts_program_descriptor` image.
//!
//! The helper table below (variant order, symbols, and operand types) is the
//! export contract shared verbatim with `bamts_codegen::Helper`
//! (`crates/bamts-codegen/src/lib.rs`, `enum Helper` / `symbol` /
//! `external_index` / `param_types`). Any drift breaks linkage, so a
//! self-consistent parity test pins it (`bamts-codegen` depends on this crate
//! under `host-jit`, so a direct comparison would be a dependency cycle).

use core::mem::{align_of, size_of};

use std::cell::Cell;
use std::fmt;
use std::panic::{AssertUnwindSafe, catch_unwind};

use crate::{Completion, CompletionTag, ShadowFrame, Value};

// -- The helper algebra ------------------------------------------------------

/// The number of runtime helpers, `0..HELPER_COUNT`.
pub const HELPER_COUNT: u32 = 32;

/// A runtime helper, identified by its stable ABI index. The variant order is
/// the canonical `external_index` order (0..31) and is byte-identical to
/// `bamts_codegen::Helper`; [`NativeHelper::symbol`] returns the exact linker
/// symbol generated code resolves against.
#[repr(u32)]
#[derive(Clone, Copy, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
pub enum NativeHelper {
    /// # Safety
    ///
    /// The caller must provide a live, uniquely owned `frame` whose nonempty handle
    /// range is disjoint from its header, and a live, aligned, writable `out` when
    /// this helper has one. Both remain valid and unaliased for the full call.
    ///
    /// `bamts_load_constant` — index 0.
    LoadConstant = 0,
    /// # Safety
    ///
    /// The caller must provide a live, uniquely owned `frame` whose nonempty handle
    /// range is disjoint from its header, and a live, aligned, writable `out` when
    /// this helper has one. Both remain valid and unaliased for the full call.
    ///
    /// `bamts_unary` — index 1.
    Unary = 1,
    /// # Safety
    ///
    /// The caller must provide a live, uniquely owned `frame` whose nonempty handle
    /// range is disjoint from its header, and a live, aligned, writable `out` when
    /// this helper has one. Both remain valid and unaliased for the full call.
    ///
    /// `bamts_binary` — index 2.
    Binary = 2,
    /// # Safety
    ///
    /// The caller must provide a live, uniquely owned `frame` whose nonempty handle
    /// range is disjoint from its header, and a live, aligned, writable `out` when
    /// this helper has one. Both remain valid and unaliased for the full call.
    ///
    /// `bamts_create_object` — index 3.
    CreateObject = 3,
    /// # Safety
    ///
    /// The caller must provide a live, uniquely owned `frame` whose nonempty handle
    /// range is disjoint from its header, and a live, aligned, writable `out` when
    /// this helper has one. Both remain valid and unaliased for the full call.
    ///
    /// `bamts_create_array` — index 4.
    CreateArray = 4,
    /// # Safety
    ///
    /// The caller must provide a live, uniquely owned `frame` whose nonempty handle
    /// range is disjoint from its header, and a live, aligned, writable `out` when
    /// this helper has one. Both remain valid and unaliased for the full call.
    ///
    /// `bamts_create_closure` — index 5.
    CreateClosure = 5,
    /// # Safety
    ///
    /// The caller must provide a live, uniquely owned `frame` whose nonempty handle
    /// range is disjoint from its header, and a live, aligned, writable `out` when
    /// this helper has one. Both remain valid and unaliased for the full call.
    ///
    /// `bamts_get_property` — index 6.
    GetProperty = 6,
    /// # Safety
    ///
    /// The caller must provide a live, uniquely owned `frame` whose nonempty handle
    /// range is disjoint from its header, and a live, aligned, writable `out` when
    /// this helper has one. Both remain valid and unaliased for the full call.
    ///
    /// `bamts_set_property` — index 7.
    SetProperty = 7,
    /// # Safety
    ///
    /// The caller must provide a live, uniquely owned `frame` whose nonempty handle
    /// range is disjoint from its header, and a live, aligned, writable `out` when
    /// this helper has one. Both remain valid and unaliased for the full call.
    ///
    /// `bamts_delete_property` — index 8.
    DeleteProperty = 8,
    /// # Safety
    ///
    /// The caller must provide a live, uniquely owned `frame` whose nonempty handle
    /// range is disjoint from its header, and a live, aligned, writable `out` when
    /// this helper has one. Both remain valid and unaliased for the full call.
    ///
    /// `bamts_call` — index 9.
    Call = 9,
    /// # Safety
    ///
    /// The caller must provide a live, uniquely owned `frame` whose nonempty handle
    /// range is disjoint from its header, and a live, aligned, writable `out` when
    /// this helper has one. Both remain valid and unaliased for the full call.
    ///
    /// `bamts_construct` — index 10.
    Construct = 10,
    /// # Safety
    ///
    /// The caller must provide a live, uniquely owned `frame` whose nonempty handle
    /// range is disjoint from its header, and a live, aligned, writable `out` when
    /// this helper has one. Both remain valid and unaliased for the full call.
    ///
    /// `bamts_import` — index 11.
    Import = 11,
    /// # Safety
    ///
    /// The caller must provide a live, uniquely owned `frame` whose nonempty handle
    /// range is disjoint from its header, and a live, aligned, writable `out` when
    /// this helper has one. Both remain valid and unaliased for the full call.
    ///
    /// `bamts_truthy` — index 12 (returns `0`/`1`, never writes `out`).
    Truthy = 12,
    /// # Safety
    ///
    /// The caller must provide a live, uniquely owned `frame` whose nonempty handle
    /// range is disjoint from its header, and a live, aligned, writable `out` when
    /// this helper has one. Both remain valid and unaliased for the full call.
    ///
    /// `bamts_resume_value` — index 13.
    ResumeValue = 13,
    /// # Safety
    ///
    /// The caller must provide a live, uniquely owned `frame` whose nonempty handle
    /// range is disjoint from its header, and a live, aligned, writable `out` when
    /// this helper has one. Both remain valid and unaliased for the full call.
    ///
    /// `bamts_define_accessor` — index 14.
    DefineAccessor = 14,
    /// # Safety
    ///
    /// The caller must provide a live, uniquely owned `frame` whose nonempty handle
    /// range is disjoint from its header, and a live, aligned, writable `out` when
    /// this helper has one. Both remain valid and unaliased for the full call.
    ///
    /// `bamts_load_global` — index 15.
    LoadGlobal = 15,
    /// # Safety
    ///
    /// The caller must provide a live, uniquely owned `frame` whose nonempty handle
    /// range is disjoint from its header, and a live, aligned, writable `out` when
    /// this helper has one. Both remain valid and unaliased for the full call.
    ///
    /// `bamts_store_global` — index 16.
    StoreGlobal = 16,
    /// # Safety
    ///
    /// The caller must provide a live, uniquely owned `frame` whose nonempty handle
    /// range is disjoint from its header, and a live, aligned, writable `out` when
    /// this helper has one. Both remain valid and unaliased for the full call.
    ///
    /// `bamts_typeof_global` — index 17.
    TypeOfGlobal = 17,
    /// # Safety
    ///
    /// The caller must provide a live, uniquely owned `frame` whose nonempty handle
    /// range is disjoint from its header, and a live, aligned, writable `out` when
    /// this helper has one. Both remain valid and unaliased for the full call.
    ///
    /// `bamts_load_this` — index 18.
    LoadThis = 18,
    /// # Safety
    ///
    /// The caller must provide a live, uniquely owned `frame` whose nonempty handle
    /// range is disjoint from its header, and a live, aligned, writable `out` when
    /// this helper has one. Both remain valid and unaliased for the full call.
    ///
    /// `bamts_load_arguments` — index 19.
    LoadArguments = 19,
    /// # Safety
    ///
    /// The caller must provide a live, uniquely owned `frame` whose nonempty handle
    /// range is disjoint from its header, and a live, aligned, writable `out` when
    /// this helper has one. Both remain valid and unaliased for the full call.
    ///
    /// `bamts_load_new_target` — index 20.
    LoadNewTarget = 20,
    /// # Safety
    ///
    /// The caller must provide a live, uniquely owned `frame` whose nonempty handle
    /// range is disjoint from its header, and a live, aligned, writable `out` when
    /// this helper has one. Both remain valid and unaliased for the full call.
    ///
    /// `bamts_array_push` — index 21.
    ArrayPush = 21,
    /// # Safety
    ///
    /// The caller must provide a live, uniquely owned `frame` whose nonempty handle
    /// range is disjoint from its header, and a live, aligned, writable `out` when
    /// this helper has one. Both remain valid and unaliased for the full call.
    ///
    /// `bamts_array_extend` — index 22.
    ArrayExtend = 22,
    /// # Safety
    ///
    /// The caller must provide a live, uniquely owned `frame` whose nonempty handle
    /// range is disjoint from its header, and a live, aligned, writable `out` when
    /// this helper has one. Both remain valid and unaliased for the full call.
    ///
    /// `bamts_object_spread` — index 23.
    ObjectSpread = 23,
    /// # Safety
    ///
    /// The caller must provide a live, uniquely owned `frame` whose nonempty handle
    /// range is disjoint from its header, and a live, aligned, writable `out` when
    /// this helper has one. Both remain valid and unaliased for the full call.
    ///
    /// `bamts_set_prototype` — index 24.
    SetPrototype = 24,
    /// # Safety
    ///
    /// The caller must provide a live, uniquely owned `frame` whose nonempty handle
    /// range is disjoint from its header, and a live, aligned, writable `out` when
    /// this helper has one. Both remain valid and unaliased for the full call.
    ///
    /// `bamts_create_private_name` — index 25.
    CreatePrivateName = 25,
    /// # Safety
    ///
    /// The caller must provide a live, uniquely owned `frame` whose nonempty handle
    /// range is disjoint from its header, and a live, aligned, writable `out` when
    /// this helper has one. Both remain valid and unaliased for the full call.
    ///
    /// `bamts_create_regexp` — index 26.
    CreateRegExp = 26,
    /// # Safety
    ///
    /// The caller must provide a live, uniquely owned `frame` whose nonempty handle
    /// range is disjoint from its header, and a live, aligned, writable `out` when
    /// this helper has one. Both remain valid and unaliased for the full call.
    ///
    /// `bamts_get_iterator` — index 27.
    GetIterator = 27,
    /// # Safety
    ///
    /// The caller must provide a live, uniquely owned `frame` whose nonempty handle
    /// range is disjoint from its header, and a live, aligned, writable `out` when
    /// this helper has one. Both remain valid and unaliased for the full call.
    ///
    /// `bamts_iterator_next` — index 28 (writes two registers directly).
    IteratorNext = 28,
    /// # Safety
    ///
    /// The caller must provide a live, uniquely owned `frame` whose nonempty handle
    /// range is disjoint from its header, and a live, aligned, writable `out` when
    /// this helper has one. Both remain valid and unaliased for the full call.
    ///
    /// `bamts_export` — index 29.
    Export = 29,
    /// # Safety
    ///
    /// The caller must provide a live, uniquely owned `frame` whose nonempty handle
    /// range is disjoint from its header, and a live, aligned, writable `out`.
    /// Both remain valid and unaliased for the full call.
    ///
    /// `bamts_consume_fuel` — index 30.
    ConsumeFuel = 30,
    /// `bamts_create_cell` — index 31.
    ///
    /// # Safety
    ///
    /// The caller must provide a live, uniquely owned `frame` whose nonempty
    /// handle range is disjoint from its header, and a live, aligned, writable
    /// `out`. Both remain valid and unaliased for the full call.
    CreateCell = 31,
}

impl NativeHelper {
    /// The C symbol generated code links against. Byte-identical to
    /// # Safety
    ///
    /// The caller must provide a live, uniquely owned `frame` whose nonempty handle
    /// range is disjoint from its header, and a live, aligned, writable `out` when
    /// this helper has one. Both remain valid and unaliased for the full call.
    ///
    /// `bamts_codegen::Helper::symbol`.
    #[must_use]
    pub const fn symbol(self) -> &'static str {
        match self {
            NativeHelper::LoadConstant => "bamts_load_constant",
            NativeHelper::Unary => "bamts_unary",
            NativeHelper::Binary => "bamts_binary",
            NativeHelper::CreateObject => "bamts_create_object",
            NativeHelper::CreateArray => "bamts_create_array",
            NativeHelper::CreateClosure => "bamts_create_closure",
            NativeHelper::GetProperty => "bamts_get_property",
            NativeHelper::SetProperty => "bamts_set_property",
            NativeHelper::DeleteProperty => "bamts_delete_property",
            NativeHelper::Call => "bamts_call",
            NativeHelper::Construct => "bamts_construct",
            NativeHelper::Import => "bamts_import",
            NativeHelper::Truthy => "bamts_truthy",
            NativeHelper::ResumeValue => "bamts_resume_value",
            NativeHelper::DefineAccessor => "bamts_define_accessor",
            NativeHelper::LoadGlobal => "bamts_load_global",
            NativeHelper::StoreGlobal => "bamts_store_global",
            NativeHelper::TypeOfGlobal => "bamts_typeof_global",
            NativeHelper::LoadThis => "bamts_load_this",
            NativeHelper::LoadArguments => "bamts_load_arguments",
            NativeHelper::LoadNewTarget => "bamts_load_new_target",
            NativeHelper::ArrayPush => "bamts_array_push",
            NativeHelper::ArrayExtend => "bamts_array_extend",
            NativeHelper::ObjectSpread => "bamts_object_spread",
            NativeHelper::SetPrototype => "bamts_set_prototype",
            NativeHelper::CreatePrivateName => "bamts_create_private_name",
            NativeHelper::CreateRegExp => "bamts_create_regexp",
            NativeHelper::GetIterator => "bamts_get_iterator",
            NativeHelper::IteratorNext => "bamts_iterator_next",
            NativeHelper::Export => "bamts_export",
            NativeHelper::ConsumeFuel => "bamts_consume_fuel",
            NativeHelper::CreateCell => "bamts_create_cell",
        }
    }

    /// The stable ABI index, `0..HELPER_COUNT`.
    #[inline]
    #[must_use]
    pub const fn as_u32(self) -> u32 {
        self as u32
    }

    /// Parses an ABI index, rejecting values outside `0..HELPER_COUNT`.
    #[must_use]
    pub const fn from_u32(index: u32) -> Option<NativeHelper> {
        match index {
            0 => Some(NativeHelper::LoadConstant),
            1 => Some(NativeHelper::Unary),
            2 => Some(NativeHelper::Binary),
            3 => Some(NativeHelper::CreateObject),
            4 => Some(NativeHelper::CreateArray),
            5 => Some(NativeHelper::CreateClosure),
            6 => Some(NativeHelper::GetProperty),
            7 => Some(NativeHelper::SetProperty),
            8 => Some(NativeHelper::DeleteProperty),
            9 => Some(NativeHelper::Call),
            10 => Some(NativeHelper::Construct),
            11 => Some(NativeHelper::Import),
            12 => Some(NativeHelper::Truthy),
            13 => Some(NativeHelper::ResumeValue),
            14 => Some(NativeHelper::DefineAccessor),
            15 => Some(NativeHelper::LoadGlobal),
            16 => Some(NativeHelper::StoreGlobal),
            17 => Some(NativeHelper::TypeOfGlobal),
            18 => Some(NativeHelper::LoadThis),
            19 => Some(NativeHelper::LoadArguments),
            20 => Some(NativeHelper::LoadNewTarget),
            21 => Some(NativeHelper::ArrayPush),
            22 => Some(NativeHelper::ArrayExtend),
            23 => Some(NativeHelper::ObjectSpread),
            24 => Some(NativeHelper::SetPrototype),
            25 => Some(NativeHelper::CreatePrivateName),
            26 => Some(NativeHelper::CreateRegExp),
            27 => Some(NativeHelper::GetIterator),
            28 => Some(NativeHelper::IteratorNext),
            29 => Some(NativeHelper::Export),
            30 => Some(NativeHelper::ConsumeFuel),
            31 => Some(NativeHelper::CreateCell),
            _ => None,
        }
    }
}

/// A typed helper invocation. Runtime `Value`s carry their [`Value`] type;
/// operator selectors, string-constant ids, function/register indices, and
/// protocol kinds are the raw ABI `u32` selectors codegen passes (never
/// # Safety
///
/// The caller must provide a live, uniquely owned `frame` whose nonempty handle
/// range is disjoint from its header, and a live, aligned, writable `out` when
/// this helper has one. Both remain valid and unaliased for the full call.
///
/// `bamts_bytecode` enums — this crate does not depend on the bytecode). The
/// implicit `frame` and completion `out` are supplied by [`NativeOps::dispatch`]
/// and the wrapper, not by the operands here.
#[derive(Clone, Copy, Debug, PartialEq)]
pub enum HelperCall {
    /// Materialize module constant `const_id`.
    LoadConstant { const_id: u32 },
    /// Apply unary operator selector `op` to `operand`.
    Unary { op: u32, operand: Value },
    /// Apply binary operator selector `op` to `left` and `right`.
    Binary { op: u32, left: Value, right: Value },
    /// A fresh empty object.
    CreateObject,
    /// A fresh empty array.
    CreateArray,
    /// A compiler-private array cell seeded with the uninitialized sentinel.
    CreateCell,
    /// A closure over `function_id` binding the `captures` array value.
    CreateClosure { function_id: u32, captures: Value },
    /// `object[key]`.
    GetProperty { object: Value, key: Value },
    /// `object[key] = value`.
    SetProperty {
        object: Value,
        key: Value,
        value: Value,
    },
    /// `delete object[key]`.
    DeleteProperty { object: Value, key: Value },
    /// Call `callee` with receiver `this_value` over the `arguments` array.
    Call {
        callee: Value,
        this_value: Value,
        arguments: Value,
    },
    /// Construct with `callee` over the `arguments` array.
    Construct { callee: Value, arguments: Value },
    /// Import the module named by string constant `specifier`.
    Import { specifier: u32 },
    /// ToBoolean on `value`. Routed to [`NativeOps::truthy`]; present here for a
    /// complete algebra but never delivered to [`NativeOps::dispatch`] in normal
    /// operation.
    Truthy { value: Value },
    /// The verified resumed value for the current frame.
    ResumeValue,
    /// Install a getter/setter (`kind` selector) under `key`.
    DefineAccessor {
        object: Value,
        key: Value,
        accessor: Value,
        kind: u32,
    },
    /// `globalThis[name]`.
    LoadGlobal { name: u32 },
    /// `globalThis[name] = value`.
    StoreGlobal { name: u32, value: Value },
    /// `typeof globalThis[name]`.
    TypeOfGlobal { name: u32 },
    /// The `this` binding.
    LoadThis,
    /// The `arguments` object.
    LoadArguments,
    /// `new.target`.
    LoadNewTarget,
    /// Append `value` to `array`.
    ArrayPush { array: Value, value: Value },
    /// Spread `iterable` onto the end of `array`.
    ArrayExtend { array: Value, iterable: Value },
    /// Copy own enumerable properties of `source` onto `target`.
    ObjectSpread { target: Value, source: Value },
    /// Set the `[[Prototype]]` of `object`.
    SetPrototype { object: Value, prototype: Value },
    /// A fresh private name described by string constant `description`.
    CreatePrivateName { description: u32 },
    /// A `RegExp` from string constants `pattern` and `flags`.
    CreateRegExp { pattern: u32, flags: u32 },
    /// Acquire an iterator over `src` using protocol `kind`.
    GetIterator { src: Value, kind: u32 },
    /// Advance `iterator`, writing the done flag into register `done_reg` and
    /// the produced value into register `value_reg` (both via the frame). On
    /// `Throw`, the thrown handle is the result value and neither register is
    /// written.
    IteratorNext {
        iterator: Value,
        done_reg: u32,
        value_reg: u32,
    },
    /// Export local value `src` under string constant `name`.
    Export { name: u32, src: Value },
    /// Consume `amount` units from the shared instruction budget.
    ConsumeFuel { amount: u32 },
}

impl HelperCall {
    /// The helper this call selects.
    #[must_use]
    pub const fn helper(&self) -> NativeHelper {
        match self {
            HelperCall::LoadConstant { .. } => NativeHelper::LoadConstant,
            HelperCall::Unary { .. } => NativeHelper::Unary,
            HelperCall::Binary { .. } => NativeHelper::Binary,
            HelperCall::CreateObject => NativeHelper::CreateObject,
            HelperCall::CreateArray => NativeHelper::CreateArray,
            HelperCall::CreateCell => NativeHelper::CreateCell,
            HelperCall::CreateClosure { .. } => NativeHelper::CreateClosure,
            HelperCall::GetProperty { .. } => NativeHelper::GetProperty,
            HelperCall::SetProperty { .. } => NativeHelper::SetProperty,
            HelperCall::DeleteProperty { .. } => NativeHelper::DeleteProperty,
            HelperCall::Call { .. } => NativeHelper::Call,
            HelperCall::Construct { .. } => NativeHelper::Construct,
            HelperCall::Import { .. } => NativeHelper::Import,
            HelperCall::Truthy { .. } => NativeHelper::Truthy,
            HelperCall::ResumeValue => NativeHelper::ResumeValue,
            HelperCall::DefineAccessor { .. } => NativeHelper::DefineAccessor,
            HelperCall::LoadGlobal { .. } => NativeHelper::LoadGlobal,
            HelperCall::StoreGlobal { .. } => NativeHelper::StoreGlobal,
            HelperCall::TypeOfGlobal { .. } => NativeHelper::TypeOfGlobal,
            HelperCall::LoadThis => NativeHelper::LoadThis,
            HelperCall::LoadArguments => NativeHelper::LoadArguments,
            HelperCall::LoadNewTarget => NativeHelper::LoadNewTarget,
            HelperCall::ArrayPush { .. } => NativeHelper::ArrayPush,
            HelperCall::ArrayExtend { .. } => NativeHelper::ArrayExtend,
            HelperCall::ObjectSpread { .. } => NativeHelper::ObjectSpread,
            HelperCall::SetPrototype { .. } => NativeHelper::SetPrototype,
            HelperCall::CreatePrivateName { .. } => NativeHelper::CreatePrivateName,
            HelperCall::CreateRegExp { .. } => NativeHelper::CreateRegExp,
            HelperCall::GetIterator { .. } => NativeHelper::GetIterator,
            HelperCall::IteratorNext { .. } => NativeHelper::IteratorNext,
            HelperCall::Export { .. } => NativeHelper::Export,
            HelperCall::ConsumeFuel { .. } => NativeHelper::ConsumeFuel,
        }
    }
}

/// The outcome of a completion helper: the ABI tag plus the completion value
/// its tag interprets (return value, thrown/yielded handle, or trap id).
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct HelperResult {
    /// The completion class.
    pub tag: CompletionTag,
    /// The completion value; meaning fixed by `tag`.
    pub value: Value,
}

impl HelperResult {
    /// A `Normal` completion carrying `value`.
    #[inline]
    #[must_use]
    pub const fn normal(value: Value) -> HelperResult {
        HelperResult {
            tag: CompletionTag::Normal,
            value,
        }
    }

    /// A `Throw` completion carrying the rooted error handle `value`.
    #[inline]
    #[must_use]
    pub const fn throw(value: Value) -> HelperResult {
        HelperResult {
            tag: CompletionTag::Throw,
            value,
        }
    }
}

// -- Trap record ids ---------------------------------------------------------

/// `out.value` id written when a wrapper runs with no installed [`NativeOps`].
pub const TRAP_MISSING_NATIVE_OPS: u32 = 0x1000;
/// `out.value` id written when a wrapper receives an invalid frame pointer.
pub const TRAP_INVALID_FRAME: u32 = 0x1001;
/// `out.value` id written when the dispatcher panics (caught at the boundary).
pub const TRAP_PANIC: u32 = 0x1002;
/// `out.value` id written when a helper receives an out-of-range register index.
pub const TRAP_INVALID_REGISTER: u32 = 0x1003;
/// `out.value` id written when a native entry returns an unrecognized completion tag.
pub const TRAP_INVALID_COMPLETION_TAG: u32 = 0x1004;

// -- The safe frame view -----------------------------------------------------

/// A validated safe view of a [`ShadowFrame`] and its register (`handle`)
/// array. Constructed only inside the `bamts_*` wrappers, which reject a null,
/// misaligned, or malformed frame before dispatching.
pub struct NativeFrame<'a> {
    frame: &'a mut ShadowFrame,
    handles: &'a mut [Value],
}

impl<'a> NativeFrame<'a> {
    /// Builds a safe view from an already-borrowed frame and register slice.
    /// Returns `None` unless the frame's `handle_len` and `handles` metadata
    /// describe exactly `handles`. This lets runtime-owned execution paths use
    /// the same checked view without raw pointers or unsafe code.
    #[must_use]
    pub fn new(frame: &'a mut ShadowFrame, handles: &'a mut [Value]) -> Option<NativeFrame<'a>> {
        let len = u16::try_from(handles.len()).ok()?;
        if frame.handle_len != len {
            return None;
        }
        if !handles.is_empty() && !core::ptr::eq(frame.handles, handles.as_mut_ptr()) {
            return None;
        }
        Some(NativeFrame { frame, handles })
    }

    /// Validates a raw frame pointer into a safe view.
    ///
    /// # Safety
    ///
    /// `frame`, when non-null, must point to a live, unaliased [`ShadowFrame`]
    /// whose `handles` field addresses exactly `handle_len` initialized
    /// [`Value`]s (or is unused when `handle_len == 0`). Generated native code
    /// upholds this: it owns the frame for the synchronous duration of the
    /// helper call, and the register array is a distinct allocation from the
    /// 32-byte header. Returns `None` for a null or misaligned pointer.
    #[must_use]
    pub unsafe fn from_raw(frame: *mut ShadowFrame) -> Option<NativeFrame<'a>> {
        if frame.is_null() || !frame.addr().is_multiple_of(align_of::<ShadowFrame>()) {
            return None;
        }
        // Read only Copy fields through raw pointers before forming any mutable
        // reference. This lets us reject a handle range that aliases the header.
        let len = unsafe { core::ptr::addr_of!((*frame).handle_len).read() as usize };
        let handles_ptr = unsafe { core::ptr::addr_of!((*frame).handles).read() };
        if len != 0 {
            if handles_ptr.is_null() || !handles_ptr.addr().is_multiple_of(align_of::<Value>()) {
                return None;
            }
            let header_start = frame.addr();
            let header_end = header_start.checked_add(size_of::<ShadowFrame>())?;
            let handles_start = handles_ptr.addr();
            let handles_end = handles_start.checked_add(len.checked_mul(size_of::<Value>())?)?;
            if handles_start < header_end && header_start < handles_end {
                return None;
            }
        }
        // SAFETY: the caller contract guarantees the validated raw frame is live
        // and unaliased; the address-range check above proves its handle storage
        // cannot overlap the header before these mutable references are formed.
        let header: &'a mut ShadowFrame = unsafe { &mut *frame };
        let handles: &'a mut [Value] = if len == 0 {
            &mut []
        } else {
            // SAFETY: checked above for alignment, range arithmetic, and
            // non-overlap; the caller contract guarantees initialized Values.
            unsafe { core::slice::from_raw_parts_mut(handles_ptr, len) }
        };
        Some(NativeFrame {
            frame: header,
            handles,
        })
    }

    /// The number of live registers (`ShadowFrame::handle_len`).
    #[inline]
    #[must_use]
    pub fn handle_len(&self) -> u32 {
        u32::from(self.frame.handle_len)
    }

    /// The dense module id of the executing function.
    #[inline]
    #[must_use]
    pub fn module_id(&self) -> u32 {
        self.frame.module_id
    }

    /// The current bytecode program counter / resume token.
    #[inline]
    #[must_use]
    pub fn pc(&self) -> u32 {
        self.frame.bytecode_pc
    }

    /// Stores a resume token into the frame (yield path).
    #[inline]
    pub fn set_resume(&mut self, token: u32) {
        self.frame.bytecode_pc = token;
    }

    /// The register array.
    #[inline]
    #[must_use]
    pub fn registers(&self) -> &[Value] {
        self.handles
    }

    /// The register array, mutably.
    #[inline]
    #[must_use]
    pub fn registers_mut(&mut self) -> &mut [Value] {
        self.handles
    }

    /// Register `index`. Panics if out of range; the wrapper turns the panic
    /// into a [`CompletionTag::FatalTrap`]. Use [`NativeFrame::try_register`] to
    /// branch instead.
    #[inline]
    #[must_use]
    pub fn register(&self, index: u32) -> Value {
        self.handles[index as usize]
    }

    /// Sets register `index`. Panics if out of range (see [`NativeFrame::register`]).
    #[inline]
    pub fn set_register(&mut self, index: u32, value: Value) {
        self.handles[index as usize] = value;
    }

    /// Register `index`, or `None` when out of range.
    #[inline]
    #[must_use]
    pub fn try_register(&self, index: u32) -> Option<Value> {
        self.handles.get(index as usize).copied()
    }

    /// Sets register `index`, returning `false` when out of range.
    #[inline]
    pub fn try_set_register(&mut self, index: u32, value: Value) -> bool {
        match self.handles.get_mut(index as usize) {
            Some(slot) => {
                *slot = value;
                true
            }
            None => false,
        }
    }

    /// The caller's frame, or null at the base of the stack.
    #[inline]
    #[must_use]
    pub fn previous(&self) -> *mut ShadowFrame {
        self.frame.previous
    }
}
/// Validates a raw frame pointer into a safe view.
///
/// # Safety
///
/// `frame` must be either null or a live, unaliased [`ShadowFrame`] pointer
/// owned by the caller for the synchronous duration of the call, with
/// `handle_len` initialized [`Value`]s when non-null. This is the same
/// contract as [`NativeFrame::from_raw`].
unsafe fn frame_from_raw<'a>(frame: *mut ShadowFrame) -> Option<NativeFrame<'a>> {
    // SAFETY: forwarded to `NativeFrame::from_raw`, which validates null and
    // alignment before dereferencing; the caller upholds the lifetime and
    // aliasing contract described above.
    unsafe { NativeFrame::from_raw(frame) }
}

// -- The dispatch seam -------------------------------------------------------

/// The runtime semantic engine the exported helpers dispatch into. Implemented
/// by `bamts_runtime`'s native engine; installed for the current thread with
/// [`with_native_ops`].
///
/// Methods take `&self` because dispatch is **re-entrant**: a `Call`,
/// `Construct`, or `CreateClosure` may re-enter native code that calls another
/// helper, which dispatches back into the same instance on the same thread.
/// Shared `&self` reborrows alias soundly, so the outer `dispatch` may resume
/// touching `self` after the nested call returns — e.g. to pop an activation
/// record or record a result. The engine therefore holds its mutable state
/// behind interior mutability (`Cell`/`RefCell`/`UnsafeCell`); the one
/// discipline is to never hold a `RefCell` borrow guard across a nested native
/// re-entry (the re-entrant borrow would panic). Nested execution always uses a
/// distinct child [`ShadowFrame`], so the outer `frame` borrow never aliases it.
pub trait NativeOps {
    /// The total ToBoolean coercion. Never throws.
    fn truthy(&self, frame: &mut NativeFrame<'_>, value: Value) -> bool;

    /// Executes one completion helper, writing any register side effects through
    /// `frame` and returning the completion.
    fn dispatch(&self, frame: &mut NativeFrame<'_>, call: HelperCall) -> HelperResult;
}

type ErasedOps = *const (dyn NativeOps + 'static);

thread_local! {
    /// The [`NativeOps`] active on this thread, or `None`. Holds an erased raw
    /// pointer valid only for the duration of the [`with_native_ops`] scope that
    /// installed it.
    static CURRENT_OPS: Cell<Option<ErasedOps>> = const { Cell::new(None) };
}

/// Installs `ops` as the current thread's dispatcher for the duration of
/// `body`, then restores the previous dispatcher.
///
/// Nesting-safe: an inner `with_native_ops` saves and restores the outer
/// dispatcher. Panic-safe: the previous dispatcher is restored even if `body`
/// unwinds (the restore runs in a guard's `Drop`).
pub fn with_native_ops<R>(ops: &mut dyn NativeOps, body: impl FnOnce() -> R) -> R {
    // `ops` is taken uniquely to guarantee the caller owns the engine while it
    // is installed, but the dispatcher is invoked through shared `&self`
    // reborrows (see `NativeOps`), so it is stored as a shared raw pointer;
    // re-entrant native calls then form further shared reborrows that alias
    // soundly.
    let ptr: *const dyn NativeOps = ops;
    // SAFETY: `ptr` and `erased` have the identical fat-pointer representation,
    // alignment, initialization, and provenance; the transmute changes only the
    // erased lifetime metadata, not the data or vtable addresses. `ptr` stays
    // valid for all of `body`, and the guard restores the previous slot before
    // return/unwind, so the erased pointer is never dereferenced outside that
    // lifetime. No bounds are involved.
    let erased: ErasedOps = unsafe { core::mem::transmute::<*const dyn NativeOps, ErasedOps>(ptr) };
    let previous = CURRENT_OPS.with(|slot| slot.replace(Some(erased)));
    let _guard = OpsGuard { previous };
    body()
}

/// Restores the previous [`CURRENT_OPS`] value on scope exit, including unwind.
struct OpsGuard {
    previous: Option<ErasedOps>,
}

impl Drop for OpsGuard {
    fn drop(&mut self) {
        CURRENT_OPS.with(|slot| slot.set(self.previous));
    }
}

/// Runs `f` with the current thread's dispatcher, or returns `None` if none is
/// installed.
fn with_current_ops<R>(f: impl FnOnce(&dyn NativeOps) -> R) -> Option<R> {
    let ptr = CURRENT_OPS.with(|slot| slot.get())?;
    // SAFETY: `ptr` was installed by an enclosing `with_native_ops` and remains
    // valid/aligned/initialized for that scope, preserving the trait object's
    // data/vtable provenance. It is dereferenced only as a shared `&dyn`; any
    // number of these may coexist across re-entrant helper calls, so aliasing is
    // sound. The helper returns before the installation scope ends.
    let ops: &dyn NativeOps = unsafe { &*ptr };
    Some(f(ops))
}

/// The internal outcome of a completion helper before it is written to `out`.
enum HelperOutcome {
    Done(HelperResult),
    Trap(u32),
}

/// The shared body of every completion `bamts_*` wrapper: validate the frame,
/// find the dispatcher, build the [`HelperCall`], write the completion, and
/// return the exact tag discriminant. Never unwinds; every failure becomes a
/// `FatalTrap`.
fn run_completion_helper(
    frame: *mut ShadowFrame,
    out: *mut Completion,
    build: impl FnOnce(&mut NativeFrame<'_>, &dyn NativeOps) -> HelperResult,
) -> u32 {
    // There is no writable completion slot on this path. Return the exact fatal
    // tag without dereferencing `out`; generated code treats the tag as control
    // flow and never reads `out.value` after a FatalTrap.
    if out.is_null() || !out.addr().is_multiple_of(align_of::<Completion>()) {
        return CompletionTag::FatalTrap.as_u32();
    }

    let outcome = catch_unwind(AssertUnwindSafe(|| {
        // SAFETY: the caller (generated native code) passes a frame it owns for
        // this synchronous call; `frame_from_raw` rejects null/misaligned pointers.
        let mut native_frame = match unsafe { frame_from_raw(frame) } {
            Some(view) => view,
            None => return HelperOutcome::Trap(TRAP_INVALID_FRAME),
        };
        match with_current_ops(|ops| build(&mut native_frame, ops)) {
            Some(result) => HelperOutcome::Done(result),
            None => HelperOutcome::Trap(TRAP_MISSING_NATIVE_OPS),
        }
    }));

    let (tag, value) = match outcome {
        Ok(HelperOutcome::Done(result)) => (result.tag, result.value),
        Ok(HelperOutcome::Trap(id)) => (CompletionTag::FatalTrap, Value::int32(id)),
        Err(_) => (CompletionTag::FatalTrap, Value::int32(TRAP_PANIC)),
    };

    // SAFETY: `out` is non-null and aligned (checked above); generated native
    // code passes a valid, writable Completion out-parameter it owns for this
    // synchronous call.
    unsafe { core::ptr::write(out, Completion::new(value)) };
    tag.as_u32()
}

/// Dispatches `call` through the current dispatcher inside a validated frame.
/// Helper for wrappers whose `HelperCall` needs no frame data to build.
#[inline]
fn dispatch_simple(frame: *mut ShadowFrame, out: *mut Completion, call: HelperCall) -> u32 {
    run_completion_helper(frame, out, |native_frame, ops| {
        ops.dispatch(native_frame, call)
    })
}

// -- The exact 30 exported C-ABI helpers -------------------------------------
//
// Parameter order and widths mirror `bamts_codegen::Helper::param_types`
// exactly: `frame` (pointer) first, `out` (pointer) last, runtime `Value`s as
// `u64`, and selectors/indices as `u32`. `bamts_truthy` is the sole exception:
// no `out`, returns `0`/`1`.

/// # Safety
///
/// The caller must provide a live, uniquely owned `frame` whose nonempty handle
/// range is disjoint from its header, and a live, aligned, writable `out` when
/// this helper has one. Both remain valid and unaliased for the full call.
///
/// `bamts_load_constant(frame, const_id, out)`.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn bamts_load_constant(
    frame: *mut ShadowFrame,
    const_id: u32,
    out: *mut Completion,
) -> u32 {
    dispatch_simple(frame, out, HelperCall::LoadConstant { const_id })
}

/// # Safety
///
/// The caller must provide a live, uniquely owned `frame` whose nonempty handle
/// range is disjoint from its header, and a live, aligned, writable `out` when
/// this helper has one. Both remain valid and unaliased for the full call.
///
/// `bamts_unary(frame, op, operand, out)`.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn bamts_unary(
    frame: *mut ShadowFrame,
    op: u32,
    operand: u64,
    out: *mut Completion,
) -> u32 {
    dispatch_simple(
        frame,
        out,
        HelperCall::Unary {
            op,
            operand: Value::from_bits(operand),
        },
    )
}

/// # Safety
///
/// The caller must provide a live, uniquely owned `frame` whose nonempty handle
/// range is disjoint from its header, and a live, aligned, writable `out` when
/// this helper has one. Both remain valid and unaliased for the full call.
///
/// `bamts_binary(frame, op, left, right, out)`.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn bamts_binary(
    frame: *mut ShadowFrame,
    op: u32,
    left: u64,
    right: u64,
    out: *mut Completion,
) -> u32 {
    dispatch_simple(
        frame,
        out,
        HelperCall::Binary {
            op,
            left: Value::from_bits(left),
            right: Value::from_bits(right),
        },
    )
}

/// # Safety
///
/// The caller must provide a live, uniquely owned `frame` whose nonempty handle
/// range is disjoint from its header, and a live, aligned, writable `out` when
/// this helper has one. Both remain valid and unaliased for the full call.
///
/// `bamts_create_object(frame, out)`.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn bamts_create_object(frame: *mut ShadowFrame, out: *mut Completion) -> u32 {
    dispatch_simple(frame, out, HelperCall::CreateObject)
}

/// # Safety
///
/// The caller must provide a live, uniquely owned `frame` whose nonempty handle
/// range is disjoint from its header, and a live, aligned, writable `out` when
/// this helper has one. Both remain valid and unaliased for the full call.
///
/// `bamts_create_array(frame, out)`.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn bamts_create_array(frame: *mut ShadowFrame, out: *mut Completion) -> u32 {
    dispatch_simple(frame, out, HelperCall::CreateArray)
}

/// # Safety
///
/// The caller must provide a live, uniquely owned `frame` whose nonempty handle
/// range is disjoint from its header, and a live, aligned, writable `out`.
/// Both remain valid and unaliased for the full call.
///
/// `bamts_create_cell(frame, out)`.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn bamts_create_cell(frame: *mut ShadowFrame, out: *mut Completion) -> u32 {
    dispatch_simple(frame, out, HelperCall::CreateCell)
}

/// # Safety
///
/// The caller must provide a live, uniquely owned `frame` whose nonempty handle
/// range is disjoint from its header, and a live, aligned, writable `out` when
/// this helper has one. Both remain valid and unaliased for the full call.
///
/// `bamts_create_closure(frame, function_id, captures, out)`.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn bamts_create_closure(
    frame: *mut ShadowFrame,
    function_id: u32,
    captures: u64,
    out: *mut Completion,
) -> u32 {
    dispatch_simple(
        frame,
        out,
        HelperCall::CreateClosure {
            function_id,
            captures: Value::from_bits(captures),
        },
    )
}

/// # Safety
///
/// The caller must provide a live, uniquely owned `frame` whose nonempty handle
/// range is disjoint from its header, and a live, aligned, writable `out` when
/// this helper has one. Both remain valid and unaliased for the full call.
///
/// `bamts_get_property(frame, object, key, out)`.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn bamts_get_property(
    frame: *mut ShadowFrame,
    object: u64,
    key: u64,
    out: *mut Completion,
) -> u32 {
    dispatch_simple(
        frame,
        out,
        HelperCall::GetProperty {
            object: Value::from_bits(object),
            key: Value::from_bits(key),
        },
    )
}

/// # Safety
///
/// The caller must provide a live, uniquely owned `frame` whose nonempty handle
/// range is disjoint from its header, and a live, aligned, writable `out` when
/// this helper has one. Both remain valid and unaliased for the full call.
///
/// `bamts_set_property(frame, object, key, value, out)`.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn bamts_set_property(
    frame: *mut ShadowFrame,
    object: u64,
    key: u64,
    value: u64,
    out: *mut Completion,
) -> u32 {
    dispatch_simple(
        frame,
        out,
        HelperCall::SetProperty {
            object: Value::from_bits(object),
            key: Value::from_bits(key),
            value: Value::from_bits(value),
        },
    )
}

/// # Safety
///
/// The caller must provide a live, uniquely owned `frame` whose nonempty handle
/// range is disjoint from its header, and a live, aligned, writable `out` when
/// this helper has one. Both remain valid and unaliased for the full call.
///
/// `bamts_delete_property(frame, object, key, out)`.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn bamts_delete_property(
    frame: *mut ShadowFrame,
    object: u64,
    key: u64,
    out: *mut Completion,
) -> u32 {
    dispatch_simple(
        frame,
        out,
        HelperCall::DeleteProperty {
            object: Value::from_bits(object),
            key: Value::from_bits(key),
        },
    )
}

/// # Safety
///
/// The caller must provide a live, uniquely owned `frame` whose nonempty handle
/// range is disjoint from its header, and a live, aligned, writable `out` when
/// this helper has one. Both remain valid and unaliased for the full call.
///
/// `bamts_call(frame, callee, this, arguments, out)`.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn bamts_call(
    frame: *mut ShadowFrame,
    callee: u64,
    this_value: u64,
    arguments: u64,
    out: *mut Completion,
) -> u32 {
    dispatch_simple(
        frame,
        out,
        HelperCall::Call {
            callee: Value::from_bits(callee),
            this_value: Value::from_bits(this_value),
            arguments: Value::from_bits(arguments),
        },
    )
}

/// # Safety
///
/// The caller must provide a live, uniquely owned `frame` whose nonempty handle
/// range is disjoint from its header, and a live, aligned, writable `out` when
/// this helper has one. Both remain valid and unaliased for the full call.
///
/// `bamts_construct(frame, callee, arguments, out)`.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn bamts_construct(
    frame: *mut ShadowFrame,
    callee: u64,
    arguments: u64,
    out: *mut Completion,
) -> u32 {
    dispatch_simple(
        frame,
        out,
        HelperCall::Construct {
            callee: Value::from_bits(callee),
            arguments: Value::from_bits(arguments),
        },
    )
}

/// # Safety
///
/// The caller must provide a live, uniquely owned `frame` whose nonempty handle
/// range is disjoint from its header, and a live, aligned, writable `out` when
/// this helper has one. Both remain valid and unaliased for the full call.
///
/// `bamts_import(frame, specifier, out)`.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn bamts_import(
    frame: *mut ShadowFrame,
    specifier: u32,
    out: *mut Completion,
) -> u32 {
    dispatch_simple(frame, out, HelperCall::Import { specifier })
}

/// Validates `frame` and runs the tagless truthy helper for `value`.
///
/// Returns `None` when the frame is invalid or no dispatcher is installed.
fn truthy_from_raw(frame: *mut ShadowFrame, value: u64) -> Option<bool> {
    // SAFETY: generated native code passes a frame it owns for this
    // synchronous call; `frame_from_raw` rejects null or misaligned pointers
    // before any dereference.
    let mut native_frame = unsafe { frame_from_raw(frame) }?;
    with_current_ops(|ops| ops.truthy(&mut native_frame, Value::from_bits(value)))
}

/// # Safety
///
/// The caller must provide a live, uniquely owned `frame` whose nonempty handle
/// range is disjoint from its header, and a live, aligned, writable `out` when
/// this helper has one. Both remain valid and unaliased for the full call.
///
/// `bamts_truthy(frame, value) -> u32`. Total; never writes `out` (there is
/// none) and never throws. Returns `0` on an invalid frame or missing
/// dispatcher, the only channel available to a tagless helper.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn bamts_truthy(frame: *mut ShadowFrame, value: u64) -> u32 {
    let outcome = catch_unwind(AssertUnwindSafe(|| truthy_from_raw(frame, value)));
    match outcome {
        Ok(Some(true)) => 1,
        _ => 0,
    }
}

/// # Safety
///
/// The caller must provide a live, uniquely owned `frame` whose nonempty handle
/// range is disjoint from its header, and a live, aligned, writable `out` when
/// this helper has one. Both remain valid and unaliased for the full call.
///
/// `bamts_resume_value(frame, out)`.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn bamts_resume_value(frame: *mut ShadowFrame, out: *mut Completion) -> u32 {
    dispatch_simple(frame, out, HelperCall::ResumeValue)
}

/// # Safety
///
/// The caller must provide a live, uniquely owned `frame` whose nonempty handle
/// range is disjoint from its header, and a live, aligned, writable `out`. Both
/// remain valid and unaliased for the full call.
///
/// `bamts_consume_fuel(frame, amount, out)`.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn bamts_consume_fuel(
    frame: *mut ShadowFrame,
    amount: u32,
    out: *mut Completion,
) -> u32 {
    dispatch_simple(frame, out, HelperCall::ConsumeFuel { amount })
}

/// # Safety
///
/// The caller must provide a live, uniquely owned `frame` whose nonempty handle
/// range is disjoint from its header, and a live, aligned, writable `out` when
/// this helper has one. Both remain valid and unaliased for the full call.
///
/// `bamts_define_accessor(frame, object, key, accessor, kind, out)`.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn bamts_define_accessor(
    frame: *mut ShadowFrame,
    object: u64,
    key: u64,
    accessor: u64,
    kind: u32,
    out: *mut Completion,
) -> u32 {
    dispatch_simple(
        frame,
        out,
        HelperCall::DefineAccessor {
            object: Value::from_bits(object),
            key: Value::from_bits(key),
            accessor: Value::from_bits(accessor),
            kind,
        },
    )
}

/// # Safety
///
/// The caller must provide a live, uniquely owned `frame` whose nonempty handle
/// range is disjoint from its header, and a live, aligned, writable `out` when
/// this helper has one. Both remain valid and unaliased for the full call.
///
/// `bamts_load_global(frame, name, out)`.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn bamts_load_global(
    frame: *mut ShadowFrame,
    name: u32,
    out: *mut Completion,
) -> u32 {
    dispatch_simple(frame, out, HelperCall::LoadGlobal { name })
}

/// # Safety
///
/// The caller must provide a live, uniquely owned `frame` whose nonempty handle
/// range is disjoint from its header, and a live, aligned, writable `out` when
/// this helper has one. Both remain valid and unaliased for the full call.
///
/// `bamts_store_global(frame, name, value, out)`.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn bamts_store_global(
    frame: *mut ShadowFrame,
    name: u32,
    value: u64,
    out: *mut Completion,
) -> u32 {
    dispatch_simple(
        frame,
        out,
        HelperCall::StoreGlobal {
            name,
            value: Value::from_bits(value),
        },
    )
}

/// # Safety
///
/// The caller must provide a live, uniquely owned `frame` whose nonempty handle
/// range is disjoint from its header, and a live, aligned, writable `out` when
/// this helper has one. Both remain valid and unaliased for the full call.
///
/// `bamts_typeof_global(frame, name, out)`.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn bamts_typeof_global(
    frame: *mut ShadowFrame,
    name: u32,
    out: *mut Completion,
) -> u32 {
    dispatch_simple(frame, out, HelperCall::TypeOfGlobal { name })
}

/// # Safety
///
/// The caller must provide a live, uniquely owned `frame` whose nonempty handle
/// range is disjoint from its header, and a live, aligned, writable `out` when
/// this helper has one. Both remain valid and unaliased for the full call.
///
/// `bamts_load_this(frame, out)`.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn bamts_load_this(frame: *mut ShadowFrame, out: *mut Completion) -> u32 {
    dispatch_simple(frame, out, HelperCall::LoadThis)
}

/// # Safety
///
/// The caller must provide a live, uniquely owned `frame` whose nonempty handle
/// range is disjoint from its header, and a live, aligned, writable `out` when
/// this helper has one. Both remain valid and unaliased for the full call.
///
/// `bamts_load_arguments(frame, out)`.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn bamts_load_arguments(
    frame: *mut ShadowFrame,
    out: *mut Completion,
) -> u32 {
    dispatch_simple(frame, out, HelperCall::LoadArguments)
}

/// # Safety
///
/// The caller must provide a live, uniquely owned `frame` whose nonempty handle
/// range is disjoint from its header, and a live, aligned, writable `out` when
/// this helper has one. Both remain valid and unaliased for the full call.
///
/// `bamts_load_new_target(frame, out)`.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn bamts_load_new_target(
    frame: *mut ShadowFrame,
    out: *mut Completion,
) -> u32 {
    dispatch_simple(frame, out, HelperCall::LoadNewTarget)
}

/// # Safety
///
/// The caller must provide a live, uniquely owned `frame` whose nonempty handle
/// range is disjoint from its header, and a live, aligned, writable `out` when
/// this helper has one. Both remain valid and unaliased for the full call.
///
/// `bamts_array_push(frame, array, value, out)`.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn bamts_array_push(
    frame: *mut ShadowFrame,
    array: u64,
    value: u64,
    out: *mut Completion,
) -> u32 {
    dispatch_simple(
        frame,
        out,
        HelperCall::ArrayPush {
            array: Value::from_bits(array),
            value: Value::from_bits(value),
        },
    )
}

/// # Safety
///
/// The caller must provide a live, uniquely owned `frame` whose nonempty handle
/// range is disjoint from its header, and a live, aligned, writable `out` when
/// this helper has one. Both remain valid and unaliased for the full call.
///
/// `bamts_array_extend(frame, array, iterable, out)`.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn bamts_array_extend(
    frame: *mut ShadowFrame,
    array: u64,
    iterable: u64,
    out: *mut Completion,
) -> u32 {
    dispatch_simple(
        frame,
        out,
        HelperCall::ArrayExtend {
            array: Value::from_bits(array),
            iterable: Value::from_bits(iterable),
        },
    )
}

/// # Safety
///
/// The caller must provide a live, uniquely owned `frame` whose nonempty handle
/// range is disjoint from its header, and a live, aligned, writable `out` when
/// this helper has one. Both remain valid and unaliased for the full call.
///
/// `bamts_object_spread(frame, target, source, out)`.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn bamts_object_spread(
    frame: *mut ShadowFrame,
    target: u64,
    source: u64,
    out: *mut Completion,
) -> u32 {
    dispatch_simple(
        frame,
        out,
        HelperCall::ObjectSpread {
            target: Value::from_bits(target),
            source: Value::from_bits(source),
        },
    )
}

/// # Safety
///
/// The caller must provide a live, uniquely owned `frame` whose nonempty handle
/// range is disjoint from its header, and a live, aligned, writable `out` when
/// this helper has one. Both remain valid and unaliased for the full call.
///
/// `bamts_set_prototype(frame, object, prototype, out)`.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn bamts_set_prototype(
    frame: *mut ShadowFrame,
    object: u64,
    prototype: u64,
    out: *mut Completion,
) -> u32 {
    dispatch_simple(
        frame,
        out,
        HelperCall::SetPrototype {
            object: Value::from_bits(object),
            prototype: Value::from_bits(prototype),
        },
    )
}

/// # Safety
///
/// The caller must provide a live, uniquely owned `frame` whose nonempty handle
/// range is disjoint from its header, and a live, aligned, writable `out` when
/// this helper has one. Both remain valid and unaliased for the full call.
///
/// `bamts_create_private_name(frame, description, out)`.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn bamts_create_private_name(
    frame: *mut ShadowFrame,
    description: u32,
    out: *mut Completion,
) -> u32 {
    dispatch_simple(frame, out, HelperCall::CreatePrivateName { description })
}

/// # Safety
///
/// The caller must provide a live, uniquely owned `frame` whose nonempty handle
/// range is disjoint from its header, and a live, aligned, writable `out` when
/// this helper has one. Both remain valid and unaliased for the full call.
///
/// `bamts_create_regexp(frame, pattern, flags, out)`.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn bamts_create_regexp(
    frame: *mut ShadowFrame,
    pattern: u32,
    flags: u32,
    out: *mut Completion,
) -> u32 {
    dispatch_simple(frame, out, HelperCall::CreateRegExp { pattern, flags })
}

/// # Safety
///
/// The caller must provide a live, uniquely owned `frame` whose nonempty handle
/// range is disjoint from its header, and a live, aligned, writable `out` when
/// this helper has one. Both remain valid and unaliased for the full call.
///
/// `bamts_get_iterator(frame, src, kind, out)`.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn bamts_get_iterator(
    frame: *mut ShadowFrame,
    src: u64,
    kind: u32,
    out: *mut Completion,
) -> u32 {
    dispatch_simple(
        frame,
        out,
        HelperCall::GetIterator {
            src: Value::from_bits(src),
            kind,
        },
    )
}

/// # Safety
///
/// The caller must provide a live, uniquely owned `frame` whose nonempty handle
/// range is disjoint from its header, and a live, aligned, writable `out` when
/// this helper has one. Both remain valid and unaliased for the full call.
///
/// `bamts_iterator_next(frame, iterator, done_reg, value_reg, out)`.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn bamts_iterator_next(
    frame: *mut ShadowFrame,
    iterator: u64,
    done_reg: u32,
    value_reg: u32,
    out: *mut Completion,
) -> u32 {
    run_completion_helper(frame, out, |native_frame, ops| {
        if done_reg >= native_frame.handle_len() || value_reg >= native_frame.handle_len() {
            return HelperResult {
                tag: CompletionTag::FatalTrap,
                value: Value::int32(TRAP_INVALID_REGISTER),
            };
        }
        ops.dispatch(
            native_frame,
            HelperCall::IteratorNext {
                iterator: Value::from_bits(iterator),
                done_reg,
                value_reg,
            },
        )
    })
}

/// # Safety
///
/// The caller must provide a live, uniquely owned `frame` whose nonempty handle
/// range is disjoint from its header, and a live, aligned, writable `out` when
/// this helper has one. Both remain valid and unaliased for the full call.
///
/// `bamts_export(frame, name, src, out)`.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn bamts_export(
    frame: *mut ShadowFrame,
    name: u32,
    src: u64,
    out: *mut Completion,
) -> u32 {
    dispatch_simple(
        frame,
        out,
        HelperCall::Export {
            name,
            src: Value::from_bits(src),
        },
    )
}

// Compile-time signature parity with `bamts_codegen::Helper::param_types`. Each
// `const _` binds a `bamts_*` export to its exact ABI signature (every runtime
// `Value` is a 64-bit scalar, so `u64`; selectors/indices are `u32`). Any drift
// in codegen's helper parameter order, count, or width stops this crate from
// compiling, so the JIT/AOT linker can never silently mismatch a helper.
const _: unsafe extern "C" fn(*mut ShadowFrame, u32, *mut Completion) -> u32 = bamts_load_constant; // 0
const _: unsafe extern "C" fn(*mut ShadowFrame, u32, u64, *mut Completion) -> u32 = bamts_unary; // 1
const _: unsafe extern "C" fn(*mut ShadowFrame, u32, u64, u64, *mut Completion) -> u32 =
    bamts_binary; // 2
const _: unsafe extern "C" fn(*mut ShadowFrame, *mut Completion) -> u32 = bamts_create_object; // 3
const _: unsafe extern "C" fn(*mut ShadowFrame, *mut Completion) -> u32 = bamts_create_array; // 4
const _: unsafe extern "C" fn(*mut ShadowFrame, u32, u64, *mut Completion) -> u32 =
    bamts_create_closure; // 5
const _: unsafe extern "C" fn(*mut ShadowFrame, u64, u64, *mut Completion) -> u32 =
    bamts_get_property; // 6
const _: unsafe extern "C" fn(*mut ShadowFrame, u64, u64, u64, *mut Completion) -> u32 =
    bamts_set_property; // 7
const _: unsafe extern "C" fn(*mut ShadowFrame, u64, u64, *mut Completion) -> u32 =
    bamts_delete_property; // 8
const _: unsafe extern "C" fn(*mut ShadowFrame, u64, u64, u64, *mut Completion) -> u32 = bamts_call; // 9
const _: unsafe extern "C" fn(*mut ShadowFrame, u64, u64, *mut Completion) -> u32 = bamts_construct; // 10
const _: unsafe extern "C" fn(*mut ShadowFrame, u32, *mut Completion) -> u32 = bamts_import; // 11
const _: unsafe extern "C" fn(*mut ShadowFrame, u64) -> u32 = bamts_truthy; // 12 (no out)
const _: unsafe extern "C" fn(*mut ShadowFrame, *mut Completion) -> u32 = bamts_resume_value; // 13
const _: unsafe extern "C" fn(*mut ShadowFrame, u64, u64, u64, u32, *mut Completion) -> u32 =
    bamts_define_accessor; // 14
const _: unsafe extern "C" fn(*mut ShadowFrame, u32, *mut Completion) -> u32 = bamts_load_global; // 15
const _: unsafe extern "C" fn(*mut ShadowFrame, u32, u64, *mut Completion) -> u32 =
    bamts_store_global; // 16
const _: unsafe extern "C" fn(*mut ShadowFrame, u32, *mut Completion) -> u32 = bamts_typeof_global; // 17
const _: unsafe extern "C" fn(*mut ShadowFrame, *mut Completion) -> u32 = bamts_load_this; // 18
const _: unsafe extern "C" fn(*mut ShadowFrame, *mut Completion) -> u32 = bamts_load_arguments; // 19
const _: unsafe extern "C" fn(*mut ShadowFrame, *mut Completion) -> u32 = bamts_load_new_target; // 20
const _: unsafe extern "C" fn(*mut ShadowFrame, u64, u64, *mut Completion) -> u32 =
    bamts_array_push; // 21
const _: unsafe extern "C" fn(*mut ShadowFrame, u64, u64, *mut Completion) -> u32 =
    bamts_array_extend; // 22
const _: unsafe extern "C" fn(*mut ShadowFrame, u64, u64, *mut Completion) -> u32 =
    bamts_object_spread; // 23
const _: unsafe extern "C" fn(*mut ShadowFrame, u64, u64, *mut Completion) -> u32 =
    bamts_set_prototype; // 24
const _: unsafe extern "C" fn(*mut ShadowFrame, u32, *mut Completion) -> u32 =
    bamts_create_private_name; // 25
const _: unsafe extern "C" fn(*mut ShadowFrame, u32, u32, *mut Completion) -> u32 =
    bamts_create_regexp; // 26
const _: unsafe extern "C" fn(*mut ShadowFrame, u64, u32, *mut Completion) -> u32 =
    bamts_get_iterator; // 27
const _: unsafe extern "C" fn(*mut ShadowFrame, u64, u32, u32, *mut Completion) -> u32 =
    bamts_iterator_next; // 28
const _: unsafe extern "C" fn(*mut ShadowFrame, u32, u64, *mut Completion) -> u32 = bamts_export; // 29
const _: unsafe extern "C" fn(*mut ShadowFrame, u32, *mut Completion) -> u32 = bamts_consume_fuel; // 30

// -- Native entry invocation seam --------------------------------------------

/// A finalized native entry point: `extern "C" fn(frame, out) -> tag`.
pub type NativeEntryFn = unsafe extern "C" fn(*mut ShadowFrame, *mut Completion) -> u32;

/// A non-self-referential seam for invoking a compiled native entry by its
/// `(module_id, function_id)` identity. A JIT backend implements this over its
/// `JITModule`; a linked AOT image ([`LinkedProgram`]) implements it over its
/// unit table. The runtime engine stores `&dyn NativeEntryTable` and routes
/// nested `CreateClosure` re-entry through it.
pub trait NativeEntryTable {
    /// The exact canonical [`bamts_bytecode::Program::encode`] bytes compiled into these entries.
    ///
    /// Callers must compare these bytes with the supplied program before any native entry runs.
    fn program_bytes(&self) -> &[u8];

    /// Invokes the entry for `(module_id, function_id)`, returning its completion tag.
    fn invoke(
        &self,
        module_id: u32,
        function_id: u32,
        frame: &mut ShadowFrame,
        out: &mut Completion,
    ) -> Result<CompletionTag, AbiError>;
}

/// Calls a raw native entry with unique references, mapping the raw `u32` result
/// to a [`CompletionTag`] (an out-of-range tag becomes `FatalTrap`).
///
/// # Safety
///
/// `entry` must be a finalized native entry with the exact
/// `extern "C" fn(*mut ShadowFrame, *mut Completion) -> u32` ABI, and its code
/// must remain mapped for the duration of the call.
unsafe fn call_native_entry(
    entry: NativeEntryFn,
    frame: &mut ShadowFrame,
    out: &mut Completion,
) -> CompletionTag {
    // SAFETY: `entry` upholds the native-entry ABI (caller contract); `frame`
    // and `out` are unique valid references, so the raw pointers are valid and
    // unaliased for the synchronous call.
    let raw = unsafe { entry(frame as *mut ShadowFrame, out as *mut Completion) };
    match CompletionTag::from_u32(raw) {
        Some(tag) => tag,
        None => {
            *out = Completion::new(Value::int32(TRAP_INVALID_COMPLETION_TAG));
            CompletionTag::FatalTrap
        }
    }
}

// -- AOT image linkage (types + validator are always available) --------------

/// The little-endian image magic, `b"BMTSAOT1"`.
pub const AOT_MAGIC: u64 = u64::from_le_bytes(*b"BMTSAOT1");
/// The supported AOT image ABI version.
pub const AOT_ABI_VERSION: u32 = 3;

/// One compiled function in a linked AOT image.
#[repr(C)]
#[derive(Clone, Copy, Debug)]
pub struct UnitDescriptor {
    /// The bytecode function id this entry implements.
    pub function_id: u32,
    /// The bytecode module id containing `function_id`.
    pub module_id: u32,
    /// The finalized native entry.
    pub entry: NativeEntryFn,
}

/// The C-layout header of a linked AOT program image, exported by generated
/// code as the external symbol `bamts_program_descriptor`.
#[repr(C)]
#[derive(Clone, Copy, Debug)]
pub struct ProgramDescriptor {
    /// [`AOT_MAGIC`].
    pub magic: u64,
    /// [`AOT_ABI_VERSION`].
    pub abi_version: u32,
    /// Reserved flags; must be zero.
    pub flags: u32,
    /// The embedded verified bytecode image.
    pub bytecode: *const u8,
    /// The length of `bytecode` in bytes.
    pub bytecode_len: usize,
    /// The unit table.
    pub units: *const UnitDescriptor,
    /// The number of units.
    pub unit_count: usize,
    /// The entry function id (must appear in `units` with `entry_module`).
    pub entry_function: u32,
    /// The entry module id.
    pub entry_module: u32,
}

/// A validated, borrow-checked view of a linked AOT image. The bytecode is
/// carried opaquely (native code never decodes it); reconstruction/verification
/// is the runtime's job.
pub struct LinkedProgram<'a> {
    bytecode: &'a [u8],
    units: &'a [UnitDescriptor],
    entry_module: u32,
    entry_function: u32,
}

impl<'a> LinkedProgram<'a> {
    /// Validates a program descriptor into a borrowed linked view.
    ///
    /// Checks magic, ABI version, zeroed flags, non-null and non-empty bytecode
    /// and unit tables, overflow-safe slice extents, unit identities sorted and
    /// unique by `(module_id, function_id)`, and that the tuple entry is present.
    /// Layout is only defined on 64-bit targets; other widths yield
    /// [`AbiError::UnsupportedPointerWidth`].
    ///
    /// # Safety
    ///
    /// `descriptor`'s `bytecode`/`units` pointers must address `bytecode_len`
    /// bytes / `unit_count` [`UnitDescriptor`]s that remain valid and immutable
    /// for `'a`. The generated `bamts_program_descriptor` satisfies this for
    /// `'static`; a caller validating a synthetic descriptor guarantees it.
    pub unsafe fn from_descriptor(
        descriptor: &'a ProgramDescriptor,
    ) -> Result<LinkedProgram<'a>, AbiError> {
        if size_of::<usize>() != 8 {
            return Err(AbiError::UnsupportedPointerWidth {
                bits: usize::BITS as u16,
            });
        }
        if descriptor.magic != AOT_MAGIC {
            return Err(AbiError::BadMagic {
                found: descriptor.magic,
            });
        }
        if descriptor.abi_version != AOT_ABI_VERSION {
            return Err(AbiError::UnsupportedAbiVersion {
                found: descriptor.abi_version,
            });
        }
        if descriptor.flags != 0 {
            return Err(AbiError::NonZeroFlags {
                flags: descriptor.flags,
            });
        }

        if descriptor.bytecode_len == 0 {
            return Err(AbiError::EmptyBytecode);
        }
        if descriptor.bytecode.is_null() {
            return Err(AbiError::NullBytecode);
        }
        if descriptor.bytecode_len > isize::MAX as usize {
            return Err(AbiError::LengthOverflow);
        }

        if descriptor.unit_count == 0 {
            return Err(AbiError::EmptyUnits);
        }
        if descriptor.units.is_null() {
            return Err(AbiError::NullUnits);
        }
        let unit_bytes = descriptor
            .unit_count
            .checked_mul(size_of::<UnitDescriptor>())
            .ok_or(AbiError::LengthOverflow)?;
        if unit_bytes > isize::MAX as usize {
            return Err(AbiError::LengthOverflow);
        }

        // SAFETY: `bytecode` is non-null and its length is bounded by
        // `isize::MAX`; the caller contract guarantees the memory stays valid
        // and immutable for `'a`.
        let bytecode =
            unsafe { core::slice::from_raw_parts(descriptor.bytecode, descriptor.bytecode_len) };
        // SAFETY: `units` is non-null and `unit_count * size_of::<UnitDescriptor>()`
        // is bounded by `isize::MAX`; the caller contract guarantees the memory
        // stays valid and immutable for `'a`.
        let units = unsafe { core::slice::from_raw_parts(descriptor.units, descriptor.unit_count) };

        let mut entry_present = false;
        let mut previous = None;
        for unit in units {
            let identity = (unit.module_id, unit.function_id);
            if identity == (descriptor.entry_module, descriptor.entry_function) {
                entry_present = true;
            }
            if let Some((previous_module_id, previous_function_id)) = previous {
                match identity.cmp(&(previous_module_id, previous_function_id)) {
                    core::cmp::Ordering::Less => {
                        return Err(AbiError::UnsortedUnits {
                            previous_module_id,
                            previous_function_id,
                            module_id: unit.module_id,
                            function_id: unit.function_id,
                        });
                    }
                    core::cmp::Ordering::Equal => {
                        return Err(AbiError::DuplicateFunction {
                            module_id: unit.module_id,
                            function_id: unit.function_id,
                        });
                    }
                    core::cmp::Ordering::Greater => {}
                }
            }
            previous = Some(identity);
        }
        if !entry_present {
            return Err(AbiError::EntryFunctionMissing {
                module_id: descriptor.entry_module,
                function_id: descriptor.entry_function,
            });
        }

        Ok(LinkedProgram {
            bytecode,
            units,
            entry_module: descriptor.entry_module,
            entry_function: descriptor.entry_function,
        })
    }

    /// The embedded canonical [`bamts_bytecode::Program::encode`] bytes (opaque to native code).
    #[inline]
    #[must_use]
    pub fn bytecode(&self) -> &'a [u8] {
        self.bytecode
    }

    /// The validated unit table.
    #[inline]
    #[must_use]
    pub fn units(&self) -> &'a [UnitDescriptor] {
        self.units
    }

    /// The entry module id.
    #[inline]
    #[must_use]
    pub fn entry_module(&self) -> u32 {
        self.entry_module
    }

    /// The entry function id.
    #[inline]
    #[must_use]
    pub fn entry_function(&self) -> u32 {
        self.entry_function
    }

    /// The unit for `(module_id, function_id)`, if present.
    #[must_use]
    pub fn unit(&self, module_id: u32, function_id: u32) -> Option<&'a UnitDescriptor> {
        self.units
            .binary_search_by_key(&(module_id, function_id), |unit| {
                (unit.module_id, unit.function_id)
            })
            .ok()
            .map(|index| &self.units[index])
    }
}

impl NativeEntryTable for LinkedProgram<'_> {
    fn program_bytes(&self) -> &[u8] {
        self.bytecode
    }

    fn invoke(
        &self,
        module_id: u32,
        function_id: u32,
        frame: &mut ShadowFrame,
        out: &mut Completion,
    ) -> Result<CompletionTag, AbiError> {
        let unit = self
            .unit(module_id, function_id)
            .ok_or(AbiError::UnknownFunction {
                module_id,
                function_id,
            })?;
        // SAFETY: `unit.entry` is a finalized native entry installed by the AOT
        // image, upholding the native-entry ABI; its code stays mapped for the
        // program's lifetime.
        Ok(unsafe { call_native_entry(unit.entry, frame, out) })
    }
}

/// Compile-time AOT layout assertions (64-bit targets only, per the ABI).
#[cfg(target_pointer_width = "64")]
const _: () = {
    use core::mem::{align_of, offset_of, size_of};

    // UnitDescriptor: { u32, u32, fn ptr } => 16 bytes, 8-aligned.
    assert!(size_of::<UnitDescriptor>() == 16);
    assert!(align_of::<UnitDescriptor>() == 8);
    assert!(offset_of!(UnitDescriptor, function_id) == 0);
    assert!(offset_of!(UnitDescriptor, module_id) == 4);
    assert!(offset_of!(UnitDescriptor, entry) == 8);

    // ProgramDescriptor: 56 bytes, 8-aligned, fields at fixed offsets.
    assert!(size_of::<ProgramDescriptor>() == 56);
    assert!(align_of::<ProgramDescriptor>() == 8);
    assert!(offset_of!(ProgramDescriptor, magic) == 0);
    assert!(offset_of!(ProgramDescriptor, abi_version) == 8);
    assert!(offset_of!(ProgramDescriptor, flags) == 12);
    assert!(offset_of!(ProgramDescriptor, bytecode) == 16);
    assert!(offset_of!(ProgramDescriptor, bytecode_len) == 24);
    assert!(offset_of!(ProgramDescriptor, units) == 32);
    assert!(offset_of!(ProgramDescriptor, unit_count) == 40);
    assert!(offset_of!(ProgramDescriptor, entry_function) == 48);
    assert!(offset_of!(ProgramDescriptor, entry_module) == 52);
};

/// A typed AOT/entry-linkage failure.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum AbiError {
    /// The target pointer width is not 64-bit.
    UnsupportedPointerWidth {
        /// The offending width in bits.
        bits: u16,
    },
    /// The descriptor magic did not match [`AOT_MAGIC`].
    BadMagic {
        /// The observed magic.
        found: u64,
    },
    /// The descriptor ABI version is not [`AOT_ABI_VERSION`].
    UnsupportedAbiVersion {
        /// The observed version.
        found: u32,
    },
    /// A reserved `flags` field was non-zero.
    NonZeroFlags {
        /// The observed flags.
        flags: u32,
    },
    /// The bytecode pointer was null with a non-zero length.
    NullBytecode,
    /// The bytecode image was empty.
    EmptyBytecode,
    /// The unit pointer was null with a non-zero count.
    NullUnits,
    /// The unit table was empty.
    EmptyUnits,
    /// A slice extent overflowed `isize::MAX`.
    LengthOverflow,
    /// Unit identities are not sorted by `(module_id, function_id)`.
    UnsortedUnits {
        /// The preceding module id.
        previous_module_id: u32,
        /// The preceding function id.
        previous_function_id: u32,
        /// The out-of-order module id.
        module_id: u32,
        /// The out-of-order function id.
        function_id: u32,
    },
    /// Two units shared a `(module_id, function_id)` identity.
    DuplicateFunction {
        /// The duplicated module id.
        module_id: u32,
        /// The duplicated function id.
        function_id: u32,
    },
    /// The declared tuple entry was absent from the unit table.
    EntryFunctionMissing {
        /// The missing module id.
        module_id: u32,
        /// The missing function id.
        function_id: u32,
    },
    /// [`NativeEntryTable::invoke`] was asked for an unknown function identity.
    UnknownFunction {
        /// The requested module id.
        module_id: u32,
        /// The requested function id.
        function_id: u32,
    },
}

impl fmt::Display for AbiError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            AbiError::UnsupportedPointerWidth { bits } => {
                write!(f, "AOT image requires a 64-bit target, not {bits}-bit")
            }
            AbiError::BadMagic { found } => {
                write!(f, "AOT image magic {found:#018x} != {AOT_MAGIC:#018x}")
            }
            AbiError::UnsupportedAbiVersion { found } => {
                write!(f, "AOT image ABI version {found} != {AOT_ABI_VERSION}")
            }
            AbiError::NonZeroFlags { flags } => {
                write!(f, "AOT image flags {flags:#x} must be zero")
            }
            AbiError::NullBytecode => f.write_str("AOT image bytecode pointer is null"),
            AbiError::EmptyBytecode => f.write_str("AOT image bytecode is empty"),
            AbiError::NullUnits => f.write_str("AOT image unit pointer is null"),
            AbiError::EmptyUnits => f.write_str("AOT image unit table is empty"),
            AbiError::LengthOverflow => f.write_str("AOT image slice extent overflows isize::MAX"),
            AbiError::UnsortedUnits {
                previous_module_id,
                previous_function_id,
                module_id,
                function_id,
            } => write!(
                f,
                "AOT unit ({module_id}, {function_id}) follows ({previous_module_id}, {previous_function_id}) out of order"
            ),
            AbiError::DuplicateFunction {
                module_id,
                function_id,
            } => write!(
                f,
                "AOT image has duplicate native function ({module_id}, {function_id})"
            ),
            AbiError::EntryFunctionMissing {
                module_id,
                function_id,
            } => write!(
                f,
                "AOT image entry function ({module_id}, {function_id}) is absent"
            ),
            AbiError::UnknownFunction {
                module_id,
                function_id,
            } => write!(
                f,
                "no native entry for function ({module_id}, {function_id})"
            ),
        }
    }
}

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

/// Reads and validates the generated AOT image descriptor.
///
/// Feature-gated (`aot-image`) because it references the external
/// # Safety
///
/// The caller must provide a live, uniquely owned `frame` whose nonempty handle
/// range is disjoint from its header, and a live, aligned, writable `out` when
/// this helper has one. Both remain valid and unaliased for the full call.
///
/// `bamts_program_descriptor` symbol, which only exists in a fully linked AOT
/// binary. The descriptor types and [`LinkedProgram::from_descriptor`] validator
/// are always available for synthetic use and testing.
#[cfg(feature = "aot-image")]
pub fn linked_program() -> Result<LinkedProgram<'static>, AbiError> {
    unsafe extern "C" {
        static bamts_program_descriptor: ProgramDescriptor;
    }
    // SAFETY: `bamts_program_descriptor` is the generated 'static AOT image; its
    // pointer fields address 'static, immutable bytecode and unit tables, so
    // reading it and borrowing for 'static is sound.
    unsafe { LinkedProgram::from_descriptor(&bamts_program_descriptor) }
}

// -- JIT entry (feature `jit-entry`) -----------------------------------------

#[cfg(feature = "jit-entry")]
pub use jit::JitEntry;

#[cfg(feature = "jit-entry")]
mod jit {
    use core::marker::PhantomData;

    use cranelift_jit::JITModule;
    use cranelift_module::FuncId;

    use super::{Completion, CompletionTag, NativeEntryFn, ShadowFrame, call_native_entry};

    /// A finalized JIT entry point, bound to its owning `JITModule`'s lifetime so
    /// the code cannot be invoked after the module frees its memory.
    pub struct JitEntry<'m> {
        entry: NativeEntryFn,
        _module: PhantomData<&'m JITModule>,
    }

    impl<'m> JitEntry<'m> {
        /// Resolves the finalized entry for `func` in `module`.
        ///
        /// `func` must have been defined and finalized in `module` with the
        /// native-entry signature `(frame, out) -> tag` (every lowered function
        /// does). The returned entry borrows `module` for `'m`, so it cannot
        /// outlive the code it points at.
        #[must_use]
        pub fn new(module: &'m JITModule, func: FuncId) -> JitEntry<'m> {
            let ptr = module.get_finalized_function(func);
            // SAFETY: `ptr` is the finalized machine code for `func` — initialized
            // and non-null — living in the module's executable mapping
            // (provenance). Every lowered function is emitted with the
            // native-entry ABI `extern "C" fn(*mut ShadowFrame, *mut Completion)
            // -> u32`, so the thin code pointer and `NativeEntryFn` share size and
            // alignment and the reinterpretation is valid (no bounds involved).
            // The code stays mapped for `'m` because `JitEntry` borrows `module`.
            let entry: NativeEntryFn =
                unsafe { core::mem::transmute::<*const u8, NativeEntryFn>(ptr) };
            JitEntry {
                entry,
                _module: PhantomData,
            }
        }

        /// The raw finalized entry pointer.
        #[inline]
        #[must_use]
        pub fn entry_fn(&self) -> NativeEntryFn {
            self.entry
        }

        /// Invokes the entry, returning its completion tag.
        pub fn invoke(&self, frame: &mut ShadowFrame, out: &mut Completion) -> CompletionTag {
            // SAFETY: `entry` is a finalized native entry with the native-entry
            // ABI, and its module (`'m`) outlives `self`, so the code is mapped.
            unsafe { call_native_entry(self.entry, frame, out) }
        }
    }
}

#[cfg(test)]
mod tests {
    fn test_bamts_load_this(f: *mut ShadowFrame, o: *mut Completion) -> u32 {
        unsafe { super::bamts_load_this(f, o) }
    }
    fn test_bamts_call(f: *mut ShadowFrame, a: u64, t: u64, x: u64, o: *mut Completion) -> u32 {
        unsafe { super::bamts_call(f, a, t, x, o) }
    }
    fn test_bamts_binary(f: *mut ShadowFrame, op: u32, l: u64, r: u64, o: *mut Completion) -> u32 {
        unsafe { super::bamts_binary(f, op, l, r, o) }
    }
    fn test_bamts_truthy(f: *mut ShadowFrame, v: u64) -> u32 {
        unsafe { super::bamts_truthy(f, v) }
    }
    fn test_bamts_consume_fuel(f: *mut ShadowFrame, amount: u32, o: *mut Completion) -> u32 {
        unsafe { super::bamts_consume_fuel(f, amount, o) }
    }
    fn test_bamts_iterator_next(
        f: *mut ShadowFrame,
        i: u64,
        d: u32,
        v: u32,
        o: *mut Completion,
    ) -> u32 {
        unsafe { super::bamts_iterator_next(f, i, d, v, o) }
    }
    fn test_bamts_create_object(f: *mut ShadowFrame, o: *mut Completion) -> u32 {
        unsafe { super::bamts_create_object(f, o) }
    }
    fn test_bamts_load_global(f: *mut ShadowFrame, n: u32, o: *mut Completion) -> u32 {
        unsafe { super::bamts_load_global(f, n, o) }
    }

    use super::*;
    use crate::{Completion, CompletionTag, ShadowFrame, Value};
    use std::cell::Cell;
    use std::panic::{AssertUnwindSafe, catch_unwind};

    /// The codegen helper table, restated here as the parity fixture. A direct
    /// comparison to `bamts_codegen::Helper` is impossible (codegen depends on
    /// this crate under `host-jit`, so importing it would be a cycle), so this
    /// literal is the pinned contract; it must stay byte-identical to
    /// # Safety
    ///
    /// The caller must provide a live, uniquely owned `frame` whose nonempty handle
    /// range is disjoint from its header, and a live, aligned, writable `out` when
    /// this helper has one. Both remain valid and unaliased for the full call.
    ///
    /// `bamts_codegen::Helper::{external_index, symbol}`.
    const CODEGEN_HELPERS: [(u32, &str); 32] = [
        (0, "bamts_load_constant"),
        (1, "bamts_unary"),
        (2, "bamts_binary"),
        (3, "bamts_create_object"),
        (4, "bamts_create_array"),
        (5, "bamts_create_closure"),
        (6, "bamts_get_property"),
        (7, "bamts_set_property"),
        (8, "bamts_delete_property"),
        (9, "bamts_call"),
        (10, "bamts_construct"),
        (11, "bamts_import"),
        (12, "bamts_truthy"),
        (13, "bamts_resume_value"),
        (14, "bamts_define_accessor"),
        (15, "bamts_load_global"),
        (16, "bamts_store_global"),
        (17, "bamts_typeof_global"),
        (18, "bamts_load_this"),
        (19, "bamts_load_arguments"),
        (20, "bamts_load_new_target"),
        (21, "bamts_array_push"),
        (22, "bamts_array_extend"),
        (23, "bamts_object_spread"),
        (24, "bamts_set_prototype"),
        (25, "bamts_create_private_name"),
        (26, "bamts_create_regexp"),
        (27, "bamts_get_iterator"),
        (28, "bamts_iterator_next"),
        (29, "bamts_export"),
        (30, "bamts_consume_fuel"),
        (31, "bamts_create_cell"),
    ];

    /// A recording dispatcher: captures the last call and returns a fixed
    /// result. Uses interior mutability since [`NativeOps`] dispatches on `&self`.
    struct Recorder {
        last: Cell<Option<HelperCall>>,
        truthy_calls: Cell<u32>,
        truthy_answer: Cell<bool>,
        result: HelperResult,
    }

    impl Recorder {
        fn normal(value: Value) -> Recorder {
            Recorder {
                last: Cell::new(None),
                truthy_calls: Cell::new(0),
                truthy_answer: Cell::new(false),
                result: HelperResult::normal(value),
            }
        }
    }

    impl NativeOps for Recorder {
        fn truthy(&self, _frame: &mut NativeFrame<'_>, value: Value) -> bool {
            self.truthy_calls.set(self.truthy_calls.get() + 1);
            self.last.set(Some(HelperCall::Truthy { value }));
            self.truthy_answer.get()
        }

        fn dispatch(&self, frame: &mut NativeFrame<'_>, call: HelperCall) -> HelperResult {
            self.last.set(Some(call));
            if let HelperCall::IteratorNext {
                done_reg,
                value_reg,
                ..
            } = call
            {
                frame.set_register(done_reg, Value::TRUE);
                frame.set_register(value_reg, Value::int32(9));
            }
            self.result
        }
    }

    /// A dispatcher whose `dispatch` always panics, to exercise the boundary.
    struct Panicky;

    impl NativeOps for Panicky {
        fn truthy(&self, _frame: &mut NativeFrame<'_>, _value: Value) -> bool {
            panic!("truthy panic")
        }

        fn dispatch(&self, _frame: &mut NativeFrame<'_>, _call: HelperCall) -> HelperResult {
            panic!("dispatch panic")
        }
    }

    /// Runs `f`, catching an expected panic at the boundary. Expected panics
    /// still print one line each; that is normal test behavior and avoids the
    /// flakiness of swapping the process-global panic hook under parallel tests.
    fn quietly<R>(f: impl FnOnce() -> R + std::panic::UnwindSafe) -> std::thread::Result<R> {
        catch_unwind(f)
    }

    fn frame_with(regs: &mut [Value]) -> ShadowFrame {
        let len = u16::try_from(regs.len()).expect("register count fits u16");
        ShadowFrame::new(core::ptr::null_mut(), 0, 0, regs.as_mut_ptr(), len)
    }

    /// A dispatcher that re-enters itself once on the outer `Call`: it fires a
    /// nested `bamts_load_this` on a distinct child frame through the same TLS
    /// `&self`, then mutates its own state after the nested call returns. This is
    /// the exact single-instance reentry pattern that `&mut self` could not
    /// support soundly. State is behind `Cell` (interior mutability).
    struct Reentrant {
        depth: Cell<u32>,
        max_depth: Cell<u32>,
        post_nested_ran: Cell<bool>,
    }

    impl NativeOps for Reentrant {
        fn truthy(&self, _frame: &mut NativeFrame<'_>, _value: Value) -> bool {
            true
        }

        fn dispatch(&self, _frame: &mut NativeFrame<'_>, call: HelperCall) -> HelperResult {
            let depth = self.depth.get();
            self.max_depth.set(self.max_depth.get().max(depth));
            if depth == 0 && matches!(call, HelperCall::Call { .. }) {
                self.depth.set(1);
                // Re-enter native code on a DISTINCT child frame; this dispatches
                // back into the same `&self` via the TLS seam.
                let mut child_regs = [Value::UNINITIALIZED; 1];
                let mut child_frame = frame_with(&mut child_regs);
                let mut child_out = Completion::new(Value::UNDEFINED);
                let nested = test_bamts_load_this(&mut child_frame, &mut child_out);
                assert_eq!(nested, CompletionTag::Normal.as_u32());
                self.depth.set(0);
                // Post-nested-call state mutation — UB under `&mut self`, sound here.
                self.post_nested_ran.set(true);
            }
            HelperResult::normal(Value::int32(depth as i32 as u32))
        }
    }

    #[test]
    fn same_instance_reentry_mutates_state_after_nested_call() {
        let mut regs = [Value::UNINITIALIZED; 1];
        let mut frame = frame_with(&mut regs);
        let mut completion = Completion::new(Value::UNDEFINED);
        let mut ops = Reentrant {
            depth: Cell::new(0),
            max_depth: Cell::new(0),
            post_nested_ran: Cell::new(false),
        };
        let tag = with_native_ops(&mut ops, || {
            test_bamts_call(
                &mut frame,
                Value::UNDEFINED.to_bits(),
                Value::UNDEFINED.to_bits(),
                Value::UNDEFINED.to_bits(),
                &mut completion,
            )
        });
        assert_eq!(tag, CompletionTag::Normal.as_u32());
        // The nested dispatch re-entered the same instance (depth reached 1).
        assert_eq!(ops.max_depth.get(), 1);
        // And the outer dispatch resumed touching `self` after the nested return.
        assert!(ops.post_nested_ran.get());
    }

    #[test]
    fn helper_symbols_and_indices_match_codegen() {
        assert_eq!(HELPER_COUNT as usize, CODEGEN_HELPERS.len());
        for (index, symbol) in CODEGEN_HELPERS {
            let helper = NativeHelper::from_u32(index).expect("dense index");
            assert_eq!(helper.as_u32(), index, "index for {helper:?}");
            assert_eq!(helper.symbol(), symbol, "symbol for {helper:?}");
        }
        // Dense and total: no index past the table, and the inverse rejects it.
        assert_eq!(NativeHelper::from_u32(HELPER_COUNT), None);
    }

    #[test]
    fn helper_call_maps_to_its_helper() {
        assert_eq!(
            HelperCall::Binary {
                op: 0,
                left: Value::UNDEFINED,
                right: Value::UNDEFINED,
            }
            .helper(),
            NativeHelper::Binary
        );
        assert_eq!(HelperCall::ResumeValue.helper(), NativeHelper::ResumeValue);
        assert_eq!(
            HelperCall::Truthy { value: Value::TRUE }.helper(),
            NativeHelper::Truthy
        );
        assert_eq!(
            HelperCall::Export {
                name: 3,
                src: Value::NULL,
            }
            .helper(),
            NativeHelper::Export
        );
        assert_eq!(
            HelperCall::ConsumeFuel { amount: 1 }.helper(),
            NativeHelper::ConsumeFuel
        );
        assert_eq!(HelperCall::CreateCell.helper(), NativeHelper::CreateCell);
    }

    #[test]
    fn exported_wrapper_dispatches_and_writes_completion() {
        let mut regs = [Value::UNINITIALIZED; 2];
        let mut frame = frame_with(&mut regs);
        let mut completion = Completion::new(Value::UNDEFINED);
        let mut ops = Recorder::normal(Value::int32(42));
        let tag = with_native_ops(&mut ops, || {
            test_bamts_binary(
                &mut frame,
                2,
                Value::int32(3).to_bits(),
                Value::int32(4).to_bits(),
                &mut completion,
            )
        });
        assert_eq!(tag, CompletionTag::Normal.as_u32());
        assert_eq!(completion.value.as_int32(), Some(42));
        assert_eq!(
            ops.last.get(),
            Some(HelperCall::Binary {
                op: 2,
                left: Value::int32(3),
                right: Value::int32(4),
            })
        );
    }

    #[test]
    fn consume_fuel_wrapper_preserves_amount() {
        let mut regs = [Value::UNINITIALIZED; 1];
        let mut frame = frame_with(&mut regs);
        let mut completion = Completion::new(Value::UNDEFINED);
        let mut ops = Recorder::normal(Value::UNDEFINED);
        let tag = with_native_ops(&mut ops, || {
            test_bamts_consume_fuel(&mut frame, 7, &mut completion)
        });
        assert_eq!(tag, CompletionTag::Normal.as_u32());
        assert_eq!(ops.last.get(), Some(HelperCall::ConsumeFuel { amount: 7 }));
    }

    #[test]
    fn truthy_wrapper_routes_to_truthy_not_dispatch() {
        let mut regs = [Value::UNINITIALIZED; 1];
        let mut frame = frame_with(&mut regs);
        let mut ops = Recorder::normal(Value::UNDEFINED);
        ops.truthy_answer.set(true);
        let truthy = with_native_ops(&mut ops, || {
            test_bamts_truthy(&mut frame, Value::int32(1).to_bits())
        });
        assert_eq!(truthy, 1);
        assert_eq!(ops.truthy_calls.get(), 1);

        ops.truthy_answer.set(false);
        let falsy = with_native_ops(&mut ops, || {
            test_bamts_truthy(&mut frame, Value::int32(0).to_bits())
        });
        assert_eq!(falsy, 0);
        assert_eq!(ops.truthy_calls.get(), 2);
    }

    #[test]
    fn iterator_next_writes_both_registers() {
        let mut regs = [Value::UNINITIALIZED; 2];
        let mut frame = frame_with(&mut regs);
        let mut completion = Completion::new(Value::UNDEFINED);
        let mut ops = Recorder::normal(Value::UNDEFINED);
        let tag = with_native_ops(&mut ops, || {
            test_bamts_iterator_next(&mut frame, Value::NULL.to_bits(), 0, 1, &mut completion)
        });
        assert_eq!(tag, CompletionTag::Normal.as_u32());
        assert_eq!(regs[0], Value::TRUE);
        assert_eq!(regs[1], Value::int32(9));
    }

    #[test]
    fn missing_dispatcher_is_a_fatal_trap() {
        let mut regs = [Value::UNINITIALIZED; 1];
        let mut frame = frame_with(&mut regs);
        let mut completion = Completion::new(Value::UNDEFINED);
        // No `with_native_ops` scope: no dispatcher installed.
        let tag = test_bamts_create_object(&mut frame, &mut completion);
        assert_eq!(tag, CompletionTag::FatalTrap.as_u32());
        assert_eq!(completion.value.as_int32(), Some(TRAP_MISSING_NATIVE_OPS));
        // The tagless helper has only `0` to report the failure with.
        assert_eq!(test_bamts_truthy(&mut frame, Value::TRUE.to_bits()), 0);
    }

    #[test]
    fn invalid_frame_is_a_fatal_trap() {
        let mut completion = Completion::new(Value::UNDEFINED);
        let mut ops = Recorder::normal(Value::int32(1));
        let null = with_native_ops(&mut ops, || {
            test_bamts_create_object(core::ptr::null_mut(), &mut completion)
        });
        assert_eq!(null, CompletionTag::FatalTrap.as_u32());
        assert_eq!(completion.value.as_int32(), Some(TRAP_INVALID_FRAME));
        // A misaligned (non-null) pointer is rejected without a dereference.
        let misaligned = with_native_ops(&mut ops, || {
            test_bamts_create_object(
                core::ptr::null_mut::<ShadowFrame>().wrapping_byte_add(1),
                &mut completion,
            )
        });
        assert_eq!(misaligned, CompletionTag::FatalTrap.as_u32());
    }

    #[test]
    fn dispatcher_panic_is_caught_as_fatal_trap() {
        let mut regs = [Value::UNINITIALIZED; 1];
        let mut frame = frame_with(&mut regs);
        let mut completion = Completion::new(Value::UNDEFINED);
        let mut ops = Panicky;
        let tag = quietly(AssertUnwindSafe(|| {
            with_native_ops(&mut ops, || {
                test_bamts_create_object(&mut frame, &mut completion)
            })
        }))
        .expect("wrapper must not unwind across the boundary");
        assert_eq!(tag, CompletionTag::FatalTrap.as_u32());
        assert_eq!(completion.value.as_int32(), Some(TRAP_PANIC));
    }

    #[test]
    fn tls_nesting_restores_the_outer_dispatcher() {
        let mut regs = [Value::UNINITIALIZED; 1];
        let mut frame = frame_with(&mut regs);
        let mut completion = Completion::new(Value::UNDEFINED);
        let mut outer = Recorder::normal(Value::int32(1));
        let mut inner = Recorder::normal(Value::int32(2));

        with_native_ops(&mut outer, || {
            test_bamts_create_object(&mut frame, &mut completion);
            assert_eq!(completion.value.as_int32(), Some(1));
            with_native_ops(&mut inner, || {
                test_bamts_create_object(&mut frame, &mut completion);
                assert_eq!(completion.value.as_int32(), Some(2));
            });
            // The inner scope has ended: the outer dispatcher is restored.
            test_bamts_create_object(&mut frame, &mut completion);
            assert_eq!(completion.value.as_int32(), Some(1));
        });
        // Both scopes ended: no dispatcher, so a fatal trap.
        let tag = test_bamts_create_object(&mut frame, &mut completion);
        assert_eq!(tag, CompletionTag::FatalTrap.as_u32());
    }

    #[test]
    fn tls_is_restored_after_a_panicking_body() {
        let mut regs = [Value::UNINITIALIZED; 1];
        let mut frame = frame_with(&mut regs);
        let mut completion = Completion::new(Value::UNDEFINED);
        let mut ops = Recorder::normal(Value::int32(1));

        let result = quietly(AssertUnwindSafe(|| {
            with_native_ops(&mut ops, || panic!("body panic"));
        }));
        assert!(result.is_err());
        // The guard restored the previous (empty) dispatcher on unwind.
        let tag = test_bamts_create_object(&mut frame, &mut completion);
        assert_eq!(tag, CompletionTag::FatalTrap.as_u32());
    }

    #[test]
    fn native_frame_new_validates_metadata() {
        let mut regs = [Value::int32(1), Value::int32(2)];
        let base = regs.as_mut_ptr();
        let mut frame = ShadowFrame::new(core::ptr::null_mut(), 0, 3, base, 2);
        // Length mismatch against the frame header.
        {
            let mut short = [Value::int32(1)];
            assert!(NativeFrame::new(&mut frame, &mut short).is_none());
        }
        // Pointer mismatch against the frame header.
        {
            let mut other = [Value::int32(1), Value::int32(2)];
            assert!(NativeFrame::new(&mut frame, &mut other).is_none());
        }
        // Exact match.
        {
            let native = NativeFrame::new(&mut frame, &mut regs);
            assert!(native.is_some());
        }
    }

    #[test]
    fn native_frame_from_raw_validates_and_addresses_registers() {
        // SAFETY: from_raw rejects a null or misaligned pointer before any
        // dereference, so neither call accesses memory.
        assert!(unsafe { NativeFrame::from_raw(core::ptr::null_mut()) }.is_none());
        assert!(
            unsafe {
                NativeFrame::from_raw(core::ptr::null_mut::<ShadowFrame>().wrapping_byte_add(1))
            }
            .is_none()
        );

        let mut regs = [Value::int32(10), Value::int32(20)];
        let mut frame = ShadowFrame::new(core::ptr::null_mut(), 7, 11, regs.as_mut_ptr(), 2);
        {
            // SAFETY: `frame` is a live, unaliased local and its metadata points
            // at exactly the two initialized Values in `regs` for this scope.
            let mut native = unsafe { NativeFrame::from_raw(&mut frame) }.expect("valid frame");
            assert_eq!(native.handle_len(), 2);
            assert_eq!(native.module_id(), 11);
            assert_eq!(native.pc(), 7);
            assert_eq!(native.register(0), Value::int32(10));
            assert_eq!(native.try_register(5), None);
            native.set_register(1, Value::int32(99));
            assert!(native.try_set_register(1, Value::int32(99)));
            assert!(!native.try_set_register(5, Value::int32(0)));
            native.set_resume(3);
        }
        assert_eq!(frame.bytecode_pc, 3);
        assert_eq!(regs[1], Value::int32(99));
    }

    #[test]
    fn native_frame_from_raw_rejects_handles_overlapping_header() {
        let mut frame = ShadowFrame::new(core::ptr::null_mut(), 0, 0, core::ptr::null_mut(), 1);
        frame.handles = core::ptr::addr_of_mut!(frame).cast::<Value>();
        // SAFETY: `frame` is live and aligned; this test intentionally supplies
        // malformed metadata to verify it is rejected before aliasing references.
        assert!(unsafe { NativeFrame::from_raw(&mut frame) }.is_none());
    }

    unsafe extern "C" fn entry_returns_seven(
        _frame: *mut ShadowFrame,
        out: *mut Completion,
    ) -> u32 {
        // SAFETY: the test passes a valid, writable `Completion` out-parameter.
        unsafe { core::ptr::write(out, Completion::new(Value::int32(7))) };
        CompletionTag::Normal.as_u32()
    }

    unsafe extern "C" fn entry_returns_invalid_tag(
        _frame: *mut ShadowFrame,
        out: *mut Completion,
    ) -> u32 {
        // SAFETY: the test passes a valid, writable completion pointer.
        unsafe { core::ptr::write(out, Completion::new(Value::int32(99))) };
        u32::MAX
    }

    #[test]
    fn invalid_native_completion_tag_replaces_stale_output() {
        let mut regs: [Value; 0] = [];
        let mut frame = ShadowFrame::new(core::ptr::null_mut(), 0, 0, regs.as_mut_ptr(), 0);
        let mut out = Completion::new(Value::int32(123));
        // SAFETY: test entry has the native ABI and frame/output are live, unique.
        let tag = unsafe { call_native_entry(entry_returns_invalid_tag, &mut frame, &mut out) };
        assert_eq!(tag, CompletionTag::FatalTrap);
        assert_eq!(out.value.as_int32(), Some(TRAP_INVALID_COMPLETION_TAG));
    }

    fn unit(module_id: u32, function_id: u32) -> UnitDescriptor {
        UnitDescriptor {
            function_id,
            module_id,
            entry: entry_returns_seven,
        }
    }

    fn program(
        bytecode: &[u8],
        units: &[UnitDescriptor],
        entry_module: u32,
        entry_function: u32,
    ) -> ProgramDescriptor {
        ProgramDescriptor {
            magic: AOT_MAGIC,
            abi_version: AOT_ABI_VERSION,
            flags: 0,
            bytecode: bytecode.as_ptr(),
            bytecode_len: bytecode.len(),
            units: units.as_ptr(),
            unit_count: units.len(),
            entry_function,
            entry_module,
        }
    }

    /// Validates a locally-built descriptor after proving its raw pointer fields
    /// describe the supplied backing slices.
    fn linked_of<'a>(
        descriptor: &'a ProgramDescriptor,
        bytecode: &'a [u8],
        units: &'a [UnitDescriptor],
    ) -> Result<LinkedProgram<'a>, AbiError> {
        assert_eq!(descriptor.bytecode_len, bytecode.len());
        assert!(bytecode.is_empty() || core::ptr::eq(descriptor.bytecode, bytecode.as_ptr()));
        assert_eq!(descriptor.unit_count, units.len());
        assert!(units.is_empty() || core::ptr::eq(descriptor.units, units.as_ptr()));
        // SAFETY: the pointer/length pairs were checked against live immutable
        // backing slices above; both slices outlive the returned program view.
        unsafe { LinkedProgram::from_descriptor(descriptor) }
    }

    #[test]
    fn linked_program_validates_and_invokes_tuple_identities() {
        let bytecode = [1u8, 2, 3];
        let units = [unit(2, 4), unit(2, 5), unit(3, 5)];
        let descriptor = program(&bytecode, &units, 3, 5);
        let linked = linked_of(&descriptor, &bytecode, &units).expect("valid image");
        assert_eq!(linked.bytecode(), &[1, 2, 3]);
        assert_eq!(linked.program_bytes(), &[1, 2, 3]);
        assert_eq!(linked.units().len(), 3);
        assert_eq!(linked.entry_module(), 3);
        assert_eq!(linked.entry_function(), 5);
        assert!(linked.unit(2, 5).is_some());
        assert!(linked.unit(3, 5).is_some());
        assert!(linked.unit(3, 4).is_none());

        let mut regs: [Value; 0] = [];
        let mut frame = ShadowFrame::new(core::ptr::null_mut(), 0, 3, regs.as_mut_ptr(), 0);
        let mut completion = Completion::new(Value::UNDEFINED);
        let tag = linked
            .invoke(3, 5, &mut frame, &mut completion)
            .expect("entry present");
        assert_eq!(tag, CompletionTag::Normal);
        assert_eq!(completion.value.as_int32(), Some(7));
        assert_eq!(
            linked.invoke(4, 5, &mut frame, &mut completion).err(),
            Some(AbiError::UnknownFunction {
                module_id: 4,
                function_id: 5,
            })
        );
    }

    #[test]
    fn linked_program_rejects_malformed_descriptors() {
        let bytecode = [1u8, 2, 3];
        let units = [unit(2, 5)];

        let mut bad_magic = program(&bytecode, &units, 2, 5);
        bad_magic.magic = 0;
        assert_eq!(
            linked_of(&bad_magic, &bytecode, &units).err(),
            Some(AbiError::BadMagic { found: 0 })
        );

        let mut bad_version = program(&bytecode, &units, 2, 5);
        bad_version.abi_version = 1;
        assert_eq!(
            linked_of(&bad_version, &bytecode, &units).err(),
            Some(AbiError::UnsupportedAbiVersion { found: 1 })
        );

        let mut bad_flags = program(&bytecode, &units, 2, 5);
        bad_flags.flags = 1;
        assert_eq!(
            linked_of(&bad_flags, &bytecode, &units).err(),
            Some(AbiError::NonZeroFlags { flags: 1 })
        );

        let empty_bytecode = program(&[], &units, 2, 5);
        assert_eq!(
            linked_of(&empty_bytecode, &[], &units).err(),
            Some(AbiError::EmptyBytecode)
        );

        let empty_units = program(&bytecode, &[], 2, 5);
        assert_eq!(
            linked_of(&empty_units, &bytecode, &[]).err(),
            Some(AbiError::EmptyUnits)
        );
    }

    #[test]
    fn linked_program_rejects_unsorted_duplicate_and_missing_tuple_entries() {
        let bytecode = [1u8, 2, 3];

        let unsorted = [unit(2, 5), unit(1, 9)];
        let unsorted_descriptor = program(&bytecode, &unsorted, 2, 5);
        assert_eq!(
            linked_of(&unsorted_descriptor, &bytecode, &unsorted).err(),
            Some(AbiError::UnsortedUnits {
                previous_module_id: 2,
                previous_function_id: 5,
                module_id: 1,
                function_id: 9,
            })
        );

        let duplicate = [unit(2, 5), unit(2, 5)];
        let duplicate_descriptor = program(&bytecode, &duplicate, 2, 5);
        assert_eq!(
            linked_of(&duplicate_descriptor, &bytecode, &duplicate).err(),
            Some(AbiError::DuplicateFunction {
                module_id: 2,
                function_id: 5,
            })
        );

        let units = [unit(2, 5), unit(3, 5)];
        let missing_entry = program(&bytecode, &units, 4, 5);
        assert_eq!(
            linked_of(&missing_entry, &bytecode, &units).err(),
            Some(AbiError::EntryFunctionMissing {
                module_id: 4,
                function_id: 5,
            })
        );
    }

    #[test]
    fn null_out_is_a_fatal_trap_without_dereference() {
        let mut regs = [Value::UNINITIALIZED; 1];
        let mut frame = frame_with(&mut regs);
        let mut ops = Recorder::normal(Value::int32(1));
        // Null `out`: the wrapper returns the fatal tag and never writes a body.
        let tag = with_native_ops(&mut ops, || {
            test_bamts_load_global(&mut frame, 0, core::ptr::null_mut())
        });
        assert_eq!(tag, CompletionTag::FatalTrap.as_u32());
    }

    #[test]
    fn iterator_next_out_of_range_register_is_fatal_trap() {
        let mut regs = [Value::UNINITIALIZED; 1];
        let mut frame = frame_with(&mut regs);
        let mut completion = Completion::new(Value::UNDEFINED);
        let mut ops = Recorder::normal(Value::UNDEFINED);
        let tag = with_native_ops(&mut ops, || {
            test_bamts_iterator_next(&mut frame, Value::NULL.to_bits(), 99, 0, &mut completion)
        });
        assert_eq!(tag, CompletionTag::FatalTrap.as_u32());
        assert_eq!(completion.value.as_int32(), Some(TRAP_INVALID_REGISTER));
        // The dispatcher was never reached, so no register was mutated.
        assert_eq!(regs[0], Value::UNINITIALIZED);
    }
}