rustia 0.2.0

Rustia is a pure-Rust interpreter for embedding standalone Luau in games and tools. Edit scripts without a rebuild and let players write mods. Rustia grew out of a game by Slowchop Studios that needed to script character decisions and replay them on desktop and in a browser.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
//! <!-- project-copy: intro -->
//! Rustia is a pure-Rust interpreter for embedding standalone [Luau](https://luau.org/) in games and tools. Edit scripts without a rebuild and let players write mods.
//!
//! Rustia grew out of a game by [Slowchop Studios](https://slowchop.com/) that needed to script character decisions and replay them on desktop and in a browser.
//! <!-- /project-copy: intro -->
//!
//! Use [`RustiaBuilder::deterministic`] when the same script, seed, and inputs
//! need to produce the same decisions.
//!
//! # A first script
//!
//! <!-- project-copy: first-script -->
//! Start with a score of 21 and a Rust function called `double`. The Luau script calls `double(base_score)` and sends 42 back to Rust.
//!
//! In the Cargo project you [set up with Rustia](https://github.com/slowchop/rustia#try-it), replace `src/main.rs` with this code and run `cargo run`.
//!
//! ```rust
//! use rustia::Rustia;
//!
//! fn main() -> rustia::Result<()> {
//!     let mut vm = Rustia::builder().build()?;
//!     vm.insert_global("base_score", 21)?;
//!     vm.add_fn("double", |value: f64| value * 2.0)?;
//!
//!     let score: f64 = vm
//!         .load("return double(base_score)")
//!         .source_name("score.luau")
//!         .eval()?;
//!
//!     println!("The score is {score}");
//!     assert_eq!(score, 42.0);
//!     Ok(())
//! }
//! ```
//!
//! You should see `The score is 42`.
//!
//! - [`insert_global`](https://rustia.dev/docs/api/rustia/struct.Rustia.html#method.insert_global) gives Luau the starting score, and [`add_fn`](https://rustia.dev/docs/api/rustia/struct.Rustia.html#method.add_fn) lets it call the Rust function.
//! - [`source_name`](https://rustia.dev/docs/api/rustia/struct.Chunk.html#method.source_name) gives the script a label that appears in error messages.
//! - [`eval`](https://rustia.dev/docs/api/rustia/struct.Chunk.html#method.eval) gives you a value back in Rust. Use [`exec`](https://rustia.dev/docs/api/rustia/struct.Chunk.html#method.exec) when you only want to run the script.
//!
//! The example keeps the script in a string so it fits in one file. Read a `.luau` file at runtime if you want to edit it without rebuilding the Rust app.
//!
//! From a Rustia checkout, run [the example](https://github.com/slowchop/rustia/blob/main/crates/rustia/examples/first_script.rs) with `cargo run --locked -p rustia --example first_script`.
//! <!-- /project-copy: first-script -->
//!
//! # Find what you need
//!
//! | I want to… | Start here |
//! | --- | --- |
//! | Run a script or get a result back | [`Chunk::exec`] runs it. [`Chunk::eval`] returns a Rust value. |
//! | Pass data from Rust to Luau | [`Rustia::insert_global`] sets a named value. [`Rustia::insert_serde`] passes a struct or collection. |
//! | Call a Luau function from Rust | [`Chunk::install`] loads its module. [`Exports::function`] looks up the function for [`Function::call`]. |
//! | Load a module with `require` | Set a [`RustiaBuilder::module_resolver`]. See the [module resolver example]. |
//! | Change a script while the app runs | Keep the old functions until the edit installs successfully. See the [hot reload example]. |
//! | Show where a script failed | Give the script a name with [`Chunk::source_name`] and display [`Error::report`]. |
//! | Run Luau in a browser | Follow the [browser setup] or build the [bare WebAssembly example]. |
//!
//! Start with [`Rustia::builder`] for everyday scripting. Use [`RustiaBuilder::live`]
//! when a script needs the current time or a fresh random seed. To limit a script’s
//! work or memory, set [`RustiaBuilder::fuel`] or [`RustiaBuilder::memory_limit_bytes`].
//!
//! # Keeping state across script edits
//!
//! If you want something to survive a reload, like a character’s position,
//! keep it in Rust or pass the saved value to the new script. Local variables
//! in the new script start with their initial values.
//!
//! Use [`Chunk::install`] to try the replacement, then switch to the new exports
//! after it succeeds. If installation fails, Rustia rolls back guest changes and
//! the app can keep using the previous functions. See the [hot reload example].
//!
//! <!-- project-copy: sans-io -->
//! # Sans-I/O core
//!
//! Embed Luau without relying on built-in platform services. Your app can supply clocks and randomness, decide where `print` output goes, and choose how to load scripts.
//! <!-- /project-copy: sans-io -->
//!
//! # Cargo features
//!
//! - **`platform-adapters`** (default) provides system randomness and connects
//!   browser builds to JavaScript clocks, randomness, and console output.
//!   [`RustiaBuilder::standard()`] starts with repeatable inputs.
//!   [`RustiaBuilder::live()`] selects real clocks and a fresh seed. For Wasm
//!   without JavaScript imports, disable default features and supply your own
//!   clocks and seed provider if you use `live()`. See the [bare WebAssembly example].
//! - **`send`** makes an idle interpreter and its live handles [`Send`] so they
//!   can move together to another Rust thread. See [thread ownership] for
//!   host data bounds and the [transferable example] for a complete handoff.
//!
//! # More about embedding
//!
//! <details>
//! <summary>Read a Luau table from Rust</summary>
//!
//! If you only need the result, use [`Chunk::eval`] to copy it into Rust. For
//! example, ask for a `bool` or an `f64`. If the script returns a table such as
//! `{ name = "Ada", score = 42 }`, use [`Chunk::eval_serde`] to read it into a Rust
//! struct. These copies stay usable after the interpreter is gone. A Rust
//! `String` needs valid UTF-8. Copies of the same table are independent, and
//! tables with cycles can’t be copied into Rust.
//!
//! Use [`Chunk::eval_live`] if you need the original Luau value. For example,
//! a table could refer to itself or share another table with a different part
//! of the script. You can also use live values to read binary strings without
//! converting them to UTF-8. A live [`Value`], [`Table`], [`Function`], or [`Exports`]
//! belongs to one interpreter. After [`Rustia::reset_decision`] those handles
//! expire. See the [live values example] to try this with a table that refers to itself.
//!
//! </details>
//!
//! <details>
//! <summary>Give scripts access to Rust functions and objects</summary>
//!
//! Use [`Rustia::add_fn`] for a calculation such as `double`. Rustia trusts that
//! the function only uses its inputs. In the standard and live environments,
//! scripts can change a Rust object immediately. See the [mutable object example].
//!
//! To let a callback read something for just one frame, such as the current
//! target position, pass borrowed data in a [`HostContext`] with [`Rustia::invoke`].
//! For deterministic replay, a script can ask to move a character and Rust can
//! apply the move once the call succeeds. The [typed invocation example] and
//! [game object example] show both parts. See [`HostFunction`] for rules about
//! callback permissions and the data a closure captures.
//!
//! </details>
//!
//! <details>
//! <summary>Keep using the interpreter after a script fails</summary>
//!
//! | What you ran | What happens when an error reaches Rust |
//! | --- | --- |
//! | A script or function with [`Chunk::eval`], [`Chunk::exec`], or [`Function::call`] | Changes the script already made stay in the interpreter, even if converting the result to Rust failed. |
//! | A replacement module with [`Chunk::install`] | Rustia restores the previous script state. You can keep calling the old functions. |
//! | A call with [`Rustia::invoke`] or [`Rustia::invoke_into`] | Rust receives none of the changes the script queued for it. Changes made in Luau still survive, except those rolled back by [`Invocation::install`]. |
//! | A stateless decision with [`Rustia::invoke_entry_into`] | Rustia discards the queued changes and resets the script state. It also resets after success. Rust can reuse the [`CompiledEntry`] for the next call. See the [stateless decision example]. |
//!
//! A Luau `pcall` can catch a script error. If the outer invocation succeeds, it
//! can still deliver changes queued by earlier successful callbacks. Resetting
//! the interpreter clears its script state and releases the Rust objects it holds.
//! Files a callback wrote and changes it made to your application’s state persist.
//!
//! An ordinary script error lets you keep using the interpreter. If Rustia catches
//! a panic in a Rust callback, call [`Rustia::reset_decision`] before running more
//! Luau. For exhausted limits, [`VmError::kind`] identifies the resource.
//!
//! </details>
//!
//! For more, [browse the Rust examples].
//!
// Keep comparison rows together in Rustdoc's horizontally scrollable table.
#![doc = "<style>table:has(a[href='#luau-note-1']) :is(td, th) { white-space: nowrap; }</style>"]
#![doc = include_str!("../compare.md")]
//!
//! [hot reload example]: https://github.com/slowchop/rustia/blob/main/crates/rustia/examples/hot_reload.rs
//! [module resolver example]: https://github.com/slowchop/rustia/blob/main/crates/rustia/examples/module_resolution.rs
//! [thread ownership]: struct.Rustia.html#threads
//! [transferable example]: https://github.com/slowchop/rustia/blob/main/crates/rustia/examples/transferable.rs
//! [browser setup]: https://github.com/slowchop/rustia/blob/main/demos/web-live/README.md
//! [bare WebAssembly example]: https://github.com/slowchop/rustia/blob/main/demos/live-wasm/README.md
//! [live values example]: https://github.com/slowchop/rustia/blob/main/crates/rustia/examples/live_values.rs
//! [mutable object example]: https://github.com/slowchop/rustia/blob/main/crates/rustia/examples/mutable_objects.rs
//! [typed invocation example]: https://github.com/slowchop/rustia/blob/main/crates/rustia/examples/typed_invocation.rs
//! [game object example]: https://github.com/slowchop/rustia/blob/main/crates/rustia/examples/game_objects.rs
//! [stateless decision example]: https://github.com/slowchop/rustia/blob/main/crates/rustia/examples/hardened_decision.rs
//! [browse the Rust examples]: https://github.com/slowchop/rustia/tree/main/crates/rustia/examples

#[cfg(doctest)]
mod brand_contract;
mod chunk;
mod diagnostic;
mod diagnostic_data;
#[cfg(test)]
mod engine_boundary_tests;
mod error;
mod extension_api;
mod function;
mod host_function;
mod invocation;
mod live_value;
mod macros;
mod object;
mod object_context;
mod object_conversion;
mod object_method;
mod profile;
mod runtime_error;
mod serde_value;
mod settings;
mod source_error;
mod source_map;
mod value;

use invocation::TypedHostDispatcher;
use rustia_compiler::{compile_with_options, CompileOptions};
use rustia_vm::{HardenedLeafProgram, Program, Vm};
use std::panic::{catch_unwind, resume_unwind, AssertUnwindSafe};

#[cfg(test)]
extern crate self as rustia;

pub use chunk::Chunk;
pub use diagnostic::DiagnosticReason;
pub use diagnostic::{
    Diagnostic, DiagnosticFrame, DiagnosticLimits, DiagnosticSpan, ErrorReport, SourceSnapshot,
};
pub use error::{Error, ErrorKind};
pub use extension_api::{
    ModuleSource, RustiaBuilder, RustiaExecutionProfile, RustiaExtension, RustiaHostFuelPolicy,
    RustiaHostFunctionInfo, ServiceClock, ServiceEntropy, ServiceModuleResolver,
    RUSTIA_DEFAULT_HOST_FUNCTION_FUEL, RUSTIA_HOST_FUEL_ABI_VERSION, RUSTIA_HOST_FUEL_POLICY,
    RUSTIA_MIN_HOST_FUNCTION_FUEL,
};
pub use function::{
    Exports, FromHostArgument, Function, IntoHostFunction, IntoHostReturn, IntoHostValue,
};
pub use host_function::{HostFunction, IntoHostFunctionDefinition};
pub use invocation::{
    Effects, HostContext, HostModule, HostMut, HostSchema, Input, IntoEffectFunction,
    IntoExternalFunction, IntoInputFunction, IntoSlotReadFunction, IntoSlotWriteFunction,
    Invocation, InvocationChunk, InvocationLimits, InvocationOutcome, InvocationReceipt,
    InvocationUsage, SlotWrites, Slots,
};
pub use live_value::{Table, Value, ValueType};
pub use object::{NewObject, Object, ObjectType};
pub use object_context::{
    IntoEffectObjectMethod, IntoExternalMutObjectMethod, IntoExternalObjectMethod,
    IntoInputObjectMethod, IntoSlotReadObjectMethod, IntoSlotWriteObjectMethod,
};
pub use object_method::{IntoMutObjectMethod, IntoObjectIterator, IntoObjectMethod};
pub use profile::{GuestSurface, OpCapability, Profile};
pub use runtime_error::{VmError, VmErrorKind, VmErrorMessage};
/// Version of the decision-reset lifecycle contract.
pub const RUSTIA_DECISION_RESET_ABI_VERSION: u32 = rustia_vm::DECISION_RESET_ABI_VERSION;
/// Version of the hardened-leaf guest environment contract.
pub const RUSTIA_HARDENED_LEAF_GUEST_ABI_VERSION: u32 = rustia_vm::HARDENED_LEAF_GUEST_ABI_VERSION;
/// Version of installation rollback and publication behavior.
pub const RUSTIA_INSTALL_TRANSACTION_ABI_VERSION: u32 = rustia_vm::INSTALL_TRANSACTION_ABI_VERSION;
/// Version of module resolution, suspension, and cache behavior.
///
/// Replay protocols should record this value and their resolver's own content
/// identity. See [`ServiceModuleResolver`] for loading and failure rules.
pub const RUSTIA_MODULE_RESOLUTION_ABI_VERSION: u32 = rustia_vm::MODULE_RESOLUTION_ABI_VERSION;
pub use serde_value::SerdeValueError;
pub use settings::{ServiceDeterminism, Settings, VmMemoryStats};
pub use source_error::{CompileError, LexError, ParseError, SourceError, UnclosedBlock};
pub use source_map::SourceMap;
pub use value::{
    FromRustia, FromRustiaMulti, IntoRustia, IntoRustiaMulti, OwnedKey, OwnedTable, OwnedValue,
};

/// Owns one Luau interpreter and the Rust functions installed in it.
///
/// Create one with [`builder`](Self::builder), then use [`load`](Self::load) to
/// run scripts. Globals and script objects persist between calls until
/// [`reset_decision`](Self::reset_decision); an ordinary evaluation error does
/// not undo earlier script mutations.
///
/// ```
/// use rustia::Rustia;
///
/// let mut vm = Rustia::builder().build()?;
/// vm.insert_global("name", "Ada")?;
/// let greeting: String = vm.eval("return 'Hello, ' .. name")?;
/// assert_eq!(greeting, "Hello, Ada");
/// # Ok::<(), rustia::Error>(())
/// ```
///
/// # Threads
///
/// By default, an interpreter and its live handles are thread-affine. Create
/// each interpreter in the worker that will run it. Enabling the Cargo feature
/// `send` makes the interpreter and its live handles `Send` when their
/// typed host record and Rust payload types are also transferable. Move the
/// whole idle interpreter and its handles together; every execution operation
/// still needs exclusive `&mut` access, so this does not permit concurrent entry.
/// Transferable service closures require `Send`, entropy providers require
/// `Fn + Send + Sync`, and object payloads require `Send + Sync`. Host-owned
/// [`CompiledModule`] and [`CompiledEntry`] values can
/// always be reused across interpreters without sharing their guest state.
///
/// # Typed host contexts
///
/// The default `Rustia<()>` needs no per-call host context. For applications that
/// lend state to a script for one call, `Rustia<H>` stores a lifetime-independent
/// [`HostSchema`] and registered callbacks. Each execution borrows a fresh
/// [`HostContext<H>`] through
/// [`invoke`](Self::invoke) or [`invoke_into`](Self::invoke_into).
///
/// <details>
/// <summary>Why typed hosts require an invocation</summary>
///
/// Typed hosts cannot bypass that authority boundary with the context-free
/// execution helpers:
///
/// ```compile_fail
/// use rustia::{HostSchema, Rustia};
///
/// struct Host;
/// impl HostSchema for Host {
///     type Input = ();
///     type Slots = ();
///     type External = ();
///     type Effect = ();
///     type SlotWrite = ();
///     type Record = ();
/// }
///
/// fn bypass(mut rustia: Rustia<Host>) {
///     let _: i32 = rustia.eval("return 42").unwrap();
/// }
/// ```
///
/// </details>
pub struct Rustia<C: HostSchema = ()> {
    vm: Vm,
    journal: Vec<C::Record>,
}

impl<C> Rustia<C>
where
    C: HostSchema,
{
    #[cfg(test)]
    fn from_profile(profile: Profile) -> Self {
        Self {
            vm: Vm::new(profile.to_core()),
            journal: Vec::new(),
        }
    }

    fn from_settings(profile: Profile, settings: rustia_vm::VmSettings) -> Self {
        Self {
            vm: Vm::with_settings(profile.to_core(), settings),
            journal: Vec::new(),
        }
    }

    /// Reports live and reserved memory owned by the interpreter.
    ///
    /// These are logical accounting totals, not process memory usage. Captured
    /// Rust callback state is outside these totals; diagnostic source retention
    /// has a separate budget. See [`VmMemoryStats`] for the totals and optional
    /// instruction-boundary peaks.
    pub fn memory_stats(&mut self) -> VmMemoryStats {
        VmMemoryStats::from_core(self.vm.memory_stats())
    }

    /// Returns the bytes charged for retained diagnostic source snapshots.
    ///
    /// This host-side retention is separate from the script memory limit.
    pub fn diagnostic_source_bytes(&self) -> usize {
        self.vm.diagnostic_source_bytes()
    }

    /// Returns the selected language profile and guest surface.
    ///
    /// This descriptor does not include budgets, services, or host callbacks.
    pub fn execution_profile(&self) -> RustiaExecutionProfile {
        RustiaExecutionProfile::from_vm(self.vm.profile(), self.vm.settings().guest_surface)
    }

    /// Returns the interpreter's effective application settings.
    ///
    /// Use [`Settings::identity_bytes`] when binding configuration to a replay
    /// or package identity. Engine implementation controls remain private.
    pub fn settings(&self) -> Settings {
        Settings::from_core(Profile::from_core(self.vm.profile()), self.vm.settings())
    }

    #[cfg(test)]
    pub(crate) const fn vm_settings(&self) -> &rustia_vm::VmSettings {
        self.vm.settings()
    }

    /// Returns the generation used to validate live handles.
    ///
    /// It starts at zero and advances when [`reset_decision`](Self::reset_decision)
    /// succeeds.
    pub fn decision_epoch(&self) -> u64 {
        self.vm.decision_epoch()
    }

    /// Discards all guest state and begins a fresh decision epoch.
    ///
    /// VM configuration, services, extensions, and installed host functions
    /// persist. Globals inserted with [`insert_global`](Self::insert_global)
    /// and [`insert_serde`](Self::insert_serde) are decision inputs and must be
    /// rebound afterward. Every prior [`Value`], [`Table`], [`Function`], and
    /// [`Exports`] handle is invalidated, while host-owned [`CompiledModule`]
    /// and [`CompiledEntry`] values remain reusable. A hardened-leaf VM preserves its sealed
    /// immutable environment in place and discards every decision-local
    /// allocation after that seal.
    ///
    /// Replay protocols must record
    /// [`RUSTIA_DECISION_RESET_ABI_VERSION`].
    /// Reset requires an idle interpreter; attempting it during execution fails.
    pub fn reset_decision(&mut self) -> Result<()> {
        self.vm.reset_decision().map_err(Error::from_engine)?;
        self.journal.clear();
        Ok(())
    }

    /// Runs one typed, fuel-bounded host invocation and exposes its committed
    /// deterministic records from Rustia's reusable journal.
    ///
    /// Failure rolls back staged records, not ordinary guest heap/global
    /// mutation. Call [`reset_decision`](Self::reset_decision) when the host
    /// requires a fresh guest-state decision boundary.
    ///
    /// A panic in `operation` itself is ordinary caller-side Rust control
    /// flow: the private journal is dropped without publication and the panic
    /// continues unwinding; it is not translated into `HostPanicked` or VM
    /// poisoning. Panics from callbacks invoked by the VM are contained and
    /// poison the VM.
    pub fn invoke<'rustia, Return, Func>(
        &'rustia mut self,
        context: &mut dyn HostContext<C>,
        limits: InvocationLimits,
        operation: Func,
    ) -> Result<InvocationOutcome<'rustia, Return, C::Record>>
    where
        Func: for<'call> FnOnce(&mut Invocation<'call, C>) -> Result<Return>,
    {
        let (value, usage) = self.invoke_core(context, limits, operation)?;
        Ok(InvocationOutcome {
            value,
            records: &self.journal,
            usage,
        })
    }

    /// Runs one typed invocation and publishes its committed records into a
    /// caller-owned lane. Capacity is reserved before guest execution, so the
    /// final append cannot fail or allocate.
    pub fn invoke_into<Return, Func>(
        &mut self,
        context: &mut dyn HostContext<C>,
        limits: InvocationLimits,
        records: &mut Vec<C::Record>,
        operation: Func,
    ) -> Result<InvocationReceipt<Return>>
    where
        Func: for<'call> FnOnce(&mut Invocation<'call, C>) -> Result<Return>,
    {
        self.ensure_healthy()?;
        let reserve = usize::try_from(limits.max_records()).map_err(|_| {
            Error::from(VmError::new(VmErrorKind::HostResourceExhausted {
                resource: "effect-record count",
                limit: u64::from(limits.max_records()),
            }))
        })?;
        records
            .try_reserve(reserve)
            .map_err(|_| Error::from(VmError::new(VmErrorKind::OutOfMemory)))?;
        let (value, usage) = self.invoke_core(context, limits, operation)?;
        records.append(&mut self.journal);
        Ok(InvocationReceipt { value, usage })
    }

    /// Executes one precompiled stateless entry, publishes its committed
    /// records, and resets the decision epoch before returning.
    ///
    /// Reset runs after success, returned failure, and an unwinding panic from
    /// guest-value conversion or a contained host callback. The original panic
    /// resumes after reset. If reset itself fails after a normal return, any
    /// records published by the invocation are removed from the caller lane
    /// and the reset error wins. `panic = "abort"` builds cannot unwind and
    /// therefore cannot provide panic containment. This is the complete
    /// authoritative-leaf lifecycle used by worker lanes.
    pub fn invoke_entry_into<Return>(
        &mut self,
        context: &mut dyn HostContext<C>,
        limits: InvocationLimits,
        records: &mut Vec<C::Record>,
        entry: &CompiledEntry,
    ) -> Result<InvocationReceipt<Return>>
    where
        Return: FromRustia,
    {
        let records_checkpoint = records.len();
        let outcome = catch_unwind(AssertUnwindSafe(|| {
            self.invoke_into(context, limits, records, |invocation| {
                invocation.call_entry::<Return>(entry)
            })
        }));
        let reset = self.reset_decision();
        finish_entry_lifecycle(records, records_checkpoint, outcome, reset)
    }

    fn invoke_core<Return, Func>(
        &mut self,
        context: &mut dyn HostContext<C>,
        limits: InvocationLimits,
        operation: Func,
    ) -> Result<(Return, InvocationUsage)>
    where
        Func: for<'call> FnOnce(&mut Invocation<'call, C>) -> Result<Return>,
    {
        self.ensure_healthy()?;
        self.journal.clear();
        let reserve = usize::try_from(limits.max_records()).map_err(|_| {
            Error::from(VmError::new(VmErrorKind::HostResourceExhausted {
                resource: "effect-record count",
                limit: u64::from(limits.max_records()),
            }))
        })?;
        self.journal
            .try_reserve(reserve)
            .map_err(|_| Error::from(VmError::new(VmErrorKind::OutOfMemory)))?;

        let journal = std::mem::take(&mut self.journal);
        let dispatcher = TypedHostDispatcher::new(context, journal, limits);
        let mut invocation = Invocation::new(&mut self.vm, dispatcher, limits);
        let result = operation(&mut invocation);
        let commit_installation = result.is_ok() && invocation.can_commit_installation();
        let (dispatcher, fuel_used) = invocation.finish(commit_installation);
        let (journal, record_bytes, hard_error) = dispatcher.finish();
        self.journal = journal;
        if let Some(error) = hard_error {
            self.journal.clear();
            return Err(Error::from_engine(error));
        }
        match result {
            Ok(value) => {
                let record_count = u32::try_from(self.journal.len()).unwrap_or(u32::MAX);
                Ok((
                    value,
                    InvocationUsage {
                        fuel_used,
                        record_count,
                        record_bytes,
                    },
                ))
            }
            Err(error) => {
                self.journal.clear();
                Err(error)
            }
        }
    }

    fn ensure_healthy(&self) -> Result<()> {
        if self.vm.is_poisoned() {
            Err(Error::from(VmError::new(VmErrorKind::VmPoisoned)))
        } else {
            Ok(())
        }
    }

    /// Copies Rust data into a script global, replacing any existing value.
    ///
    /// Uses [`IntoRustia`] for conversion. The value is available to later scripts
    /// until replaced or [`reset_decision`](Self::reset_decision) clears it.
    /// Conversion errors and script memory-limit failures are returned as [`Error`].
    /// To insert a live handle without copying it, use [`Table::set_live`] on
    /// [`globals`](Self::globals).
    ///
    /// ```
    /// # use rustia::Rustia;
    /// # let mut vm = Rustia::builder().build()?;
    /// vm.insert_global("scores", vec![10, 20, 30])?;
    /// let first: i32 = vm.eval("return scores[1]")?;
    /// assert_eq!(first, 10);
    /// # Ok::<(), rustia::Error>(())
    /// ```
    pub fn insert_global<T>(&mut self, name: impl AsRef<str>, value: T) -> Result<()>
    where
        T: IntoRustia,
    {
        let value = value.into_rustia()?;
        let value = value::load_owned_value(&mut self.vm, &value)?;
        self.vm
            .set_global_value(name, value)
            .map_err(Error::from_engine)
    }

    /// Serializes Rust data into a script global, replacing any existing value.
    ///
    /// This copies the data; later script changes do not modify the Rust input.
    /// Serialization and script memory-limit failures are returned as [`Error`].
    /// Like [`insert_global`](Self::insert_global), the binding is cleared by reset.
    pub fn insert_serde<T>(&mut self, name: impl AsRef<str>, value: &T) -> Result<()>
    where
        T: serde::Serialize + ?Sized,
    {
        let value = OwnedValue::from_serialize(value)?;
        let value = value::load_owned_value(&mut self.vm, &value)?;
        self.vm
            .set_global_value(name, value)
            .map_err(Error::from_engine)
    }

    /// Returns a live handle to the interpreter's globals table.
    ///
    /// Reads and writes through [`Table`] preserve identity and are visible to
    /// subsequently executed guest code. Like ordinary global inputs, these
    /// mutations are decision-local and are discarded by
    /// [`reset_decision`](Self::reset_decision). Hardened leaves reject this
    /// mutable authority.
    pub fn globals(&mut self) -> Result<Table> {
        let value = self.vm.retain_globals().map_err(Error::from_engine)?;
        Ok(Table::new(Value::new(value, self.vm.decision_epoch())))
    }

    /// Creates an empty live table in this interpreter.
    ///
    /// The table and its root participate in the logical-live memory quota as
    /// one admission. Hardened leaves reject live table construction.
    ///
    /// ```
    /// # use rustia::Rustia;
    /// # let mut vm = Rustia::builder().build()?;
    /// let settings = vm.create_table()?;
    /// settings.set(&mut vm, "volume", 0.5)?;
    /// vm.globals()?.set_live(&mut vm, "settings", &settings.as_value())?;
    /// let volume: f64 = vm.eval("return settings.volume")?;
    /// assert_eq!(volume, 0.5);
    /// # Ok::<(), rustia::Error>(())
    /// ```
    pub fn create_table(&mut self) -> Result<Table> {
        let value = self
            .vm
            .create_retained_table()
            .map_err(Error::from_engine)?;
        Ok(Table::new(Value::new(value, self.vm.decision_epoch())))
    }

    /// Creates an immutable live Luau vector in this interpreter.
    ///
    /// The vector and its root participate in the logical-live memory quota as
    /// one admission. Components use Luau's canonical `f32` vector semantics.
    /// The returned handle belongs to this VM and decision epoch like every
    /// other live [`Value`].
    ///
    /// ```
    /// # use rustia::Rustia;
    /// # let mut vm = Rustia::builder().build()?;
    /// let direction = vm.create_vector(1.5, -2.0, 3.0)?;
    /// vm.globals()?.set_live(&mut vm, "direction", &direction)?;
    /// let x: f64 = vm.eval("return direction.x")?;
    /// assert_eq!(x, 1.5);
    /// # Ok::<(), rustia::Error>(())
    /// ```
    pub fn create_vector(&mut self, x: f32, y: f32, z: f32) -> Result<Value> {
        let value = self
            .vm
            .create_retained_vector(x, y, z)
            .map_err(Error::from_engine)?;
        Ok(Value::new(value, self.vm.decision_epoch()))
    }

    fn ensure_dynamic_source_loading_allowed(&self) -> Result<()> {
        if self.vm.settings().guest_surface.is_hardened_leaf() {
            return Err(Error::from(
                VmError::new(VmErrorKind::HardenedLeafViolation {
                    feature: "dynamic source loading",
                })
                .without_source_location(),
            ));
        }
        self.ensure_healthy()
    }

    /// Registers a context-free `Fn(A, B, ...) -> R` with an explicit fuel
    /// charge. The registration method fixes the capability to `Pure`;
    /// `Result<R>` creates a fallible host call.
    pub fn add_pure<Func, Args, Return>(
        &mut self,
        name: impl Into<String>,
        fuel_cost: u32,
        function: Func,
    ) -> Result<()>
    where
        Func: IntoHostFunction<Args, Return>,
    {
        self.register_function(name, HostFunction::pure(function).fuel_cost(fuel_cost))
    }

    /// Registers a deterministic input reader with closure shape
    /// `for<'a> Fn(Input<'a, C>, A, B, ...) -> R`.
    pub fn add_input<Func, Args, Return>(
        &mut self,
        name: impl Into<String>,
        fuel_cost: u32,
        function: Func,
    ) -> Result<()>
    where
        Func: IntoInputFunction<C, Args, Return>,
    {
        self.register_function(name, HostFunction::input(function).fuel_cost(fuel_cost))
    }

    /// Registers a deterministic slot reader with closure shape
    /// `for<'a> Fn(Slots<'a, C>, A, B, ...) -> R`.
    pub fn add_slot_read<Func, Args, Return>(
        &mut self,
        name: impl Into<String>,
        fuel_cost: u32,
        function: Func,
    ) -> Result<()>
    where
        Func: IntoSlotReadFunction<C, Args, Return>,
    {
        self.register_function(name, HostFunction::slot_read(function).fuel_cost(fuel_cost))
    }

    /// Registers a deterministic effect recorder with closure shape
    /// `for<'a> Fn(Effects<'a, C>, A, B, ...) -> R`.
    pub fn add_effect<Func, Args, Return>(
        &mut self,
        name: impl Into<String>,
        fuel_cost: u32,
        function: Func,
    ) -> Result<()>
    where
        Func: IntoEffectFunction<C, Args, Return>,
    {
        self.register_function(name, HostFunction::effect(function).fuel_cost(fuel_cost))
    }

    /// Registers a deterministic slot-write recorder with closure shape
    /// `for<'a> Fn(SlotWrites<'a, C>, A, B, ...) -> R`.
    pub fn add_slot_write<Func, Args, Return>(
        &mut self,
        name: impl Into<String>,
        fuel_cost: u32,
        function: Func,
    ) -> Result<()>
    where
        Func: IntoSlotWriteFunction<C, Args, Return>,
    {
        self.register_function(
            name,
            HostFunction::slot_write(function).fuel_cost(fuel_cost),
        )
    }

    /// Registers an external-state callback with closure shape
    /// `for<'a> Fn(HostMut<'a, C>, A, B, ...) -> R`.
    ///
    /// Deterministic profiles reject it before installation, and mutations
    /// through `HostMut` are outside Rustia's record rollback.
    pub fn add_external<Func, Args, Return>(
        &mut self,
        name: impl Into<String>,
        fuel_cost: u32,
        function: Func,
    ) -> Result<()>
    where
        Func: IntoExternalFunction<C, Args, Return>,
    {
        self.register_function(name, HostFunction::external(function).fuel_cost(fuel_cost))
    }

    /// Registers an explicitly nondeterministic `HostMut` callback.
    ///
    /// Deterministic profiles reject it before installation.
    pub fn add_nondeterministic<Func, Args, Return>(
        &mut self,
        name: impl Into<String>,
        fuel_cost: u32,
        function: Func,
    ) -> Result<()>
    where
        Func: IntoExternalFunction<C, Args, Return>,
    {
        self.register_function(
            name,
            HostFunction::nondeterministic(function).fuel_cost(fuel_cost),
        )
    }

    /// Installs a namespaced collection of capability-checked host functions.
    ///
    /// Installation is fail-closed: if validation or VM binding fails, no
    /// callable namespace or typed dispatcher bindings are retained.
    pub fn install_host_module(&mut self, module: HostModule<C>) -> Result<()> {
        module.install(&mut self.vm)
    }

    #[inline]
    pub(crate) fn vm_mut(&mut self) -> &mut Vm {
        &mut self.vm
    }

    #[inline]
    pub(crate) fn vm(&self) -> &Vm {
        &self.vm
    }
}

fn finish_entry_lifecycle<T, Record>(
    records: &mut Vec<Record>,
    records_checkpoint: usize,
    outcome: std::thread::Result<Result<T>>,
    reset: Result<()>,
) -> Result<T> {
    match (outcome, reset) {
        (Ok(outcome), Ok(())) => outcome,
        (Ok(_), Err(reset_error)) => {
            records.truncate(records_checkpoint);
            Err(reset_error)
        }
        (Err(payload), _) => {
            records.truncate(records_checkpoint);
            resume_unwind(payload)
        }
    }
}

impl Rustia<()> {
    /// Compiles a script once for later installation in one or more interpreters.
    ///
    /// Compilation does not run the script or change a VM. `source_name` labels
    /// diagnostics and identifies the caller to the module resolver for `require`.
    /// Invalid source returns a parse or compilation error. Call
    /// [`install`](Self::install) to run the compiled module and retain its exports.
    /// For PUC Lua 5.1 compatibility, use
    /// [`compile_module_with_profile`](Self::compile_module_with_profile).
    pub fn compile_module(source_name: impl Into<String>, source: &str) -> Result<CompiledModule> {
        Self::compile_module_bytes(source_name, source.as_bytes())
    }

    /// Compiles a reusable module with code generation appropriate for
    /// `profile`. Use this when the module will be installed into a VM whose
    /// profile is known ahead of time. `source_name` labels diagnostics and
    /// nested `require` resolver referrers.
    pub fn compile_module_with_profile(
        source_name: impl Into<String>,
        source: &str,
        profile: Profile,
    ) -> Result<CompiledModule> {
        Self::compile_module_bytes_with_profile(source_name, source.as_bytes(), profile)
    }

    /// Compiles bytes with the standard (source-order) table-constructor write
    /// order. `source_name` labels diagnostics and nested `require` resolver
    /// referrers. `Profile::Puc51Compat` hosts need PUC write order instead;
    /// compile through the instance `load` builder (which reads the VM profile)
    /// or use [`compile_module_bytes_with_profile`](Self::compile_module_bytes_with_profile).
    pub fn compile_module_bytes(
        source_name: impl Into<String>,
        source: &[u8],
    ) -> Result<CompiledModule> {
        Self::compile_module_bytes_with_constructor_order(
            source_name,
            source,
            false,
            DiagnosticLimits::default(),
        )
    }

    /// Byte-oriented form of [`compile_module_with_profile`](Self::compile_module_with_profile).
    pub fn compile_module_bytes_with_profile(
        source_name: impl Into<String>,
        source: &[u8],
        profile: Profile,
    ) -> Result<CompiledModule> {
        Self::compile_module_bytes_with_constructor_order(
            source_name,
            source,
            profile == Profile::Puc51Compat,
            DiagnosticLimits::default(),
        )
    }

    fn compile_module_bytes_with_constructor_order(
        source_name: impl Into<String>,
        source: &[u8],
        puc51_constructor_order: bool,
        diagnostic_limits: DiagnosticLimits,
    ) -> Result<CompiledModule> {
        let source_name = source_name.into();
        let chunk = rustia_parser::parse_source_bytes(source).map_err(|error| {
            Error::from_engine(error).with_source(&source_name, source, diagnostic_limits)
        })?;
        let options = CompileOptions {
            puc51_constructor_order,
            ..CompileOptions::default()
        }
        .with_source_bytes(source_name.clone(), source);
        let mut program = compile_with_options(&chunk, options).map_err(|error| {
            Error::from_engine(error).with_source(&source_name, source, diagnostic_limits)
        })?;
        program.diagnostic_source = rustia_vm::diagnostic::SourceSnapshot::copy_with_limit(
            source,
            diagnostic_limits.source_bytes,
        );
        Ok(CompiledModule {
            source_name,
            program,
        })
    }

    /// Compiles a chunk whose top-level body is invoked directly on each
    /// decision without installing exports or retaining bytecode in the VM.
    ///
    /// Stateless entries cannot contain nested function prototypes. Inputs and
    /// outputs should cross the typed host context, and the source should
    /// return its decision value directly.
    ///
    /// Compiles with the standard (source-order) table-constructor write
    /// order; see [`compile_module_bytes`](Self::compile_module_bytes).
    pub fn compile_entry(source_name: impl Into<String>, source: &str) -> Result<CompiledEntry> {
        let source_name = source_name.into();
        let chunk = rustia_parser::parse_source(source).map_err(Error::from_engine)?;
        let options = CompileOptions::default().with_source(source_name.clone(), source);
        let program = compile_with_options(&chunk, options).map_err(Error::from_engine)?;
        let program = HardenedLeafProgram::new(program).map_err(Error::from_engine)?;
        Ok(CompiledEntry {
            source_name,
            program,
        })
    }

    /// Returns the [`standard`](RustiaBuilder::standard) preset:
    /// reproducible-by-default, with a real stdout `print` sink on native
    /// targets.
    pub fn builder() -> RustiaBuilder {
        RustiaBuilder::standard()
    }

    /// Creates a service-free interpreter with the selected language profile.
    ///
    /// Unlike [`builder`](Self::builder), this constructor installs no stdout
    /// sink. Use the builder presets for normal application defaults and host
    /// clock, output, entropy, or module services.
    #[cfg(test)]
    pub(crate) fn new(profile: Profile) -> Self {
        Self::from_profile(profile)
    }

    /// Creates an interpreter with explicit low-level settings and services.
    ///
    /// Prefer [`builder`](Self::builder) for ordinary application configuration.
    #[cfg(test)]
    pub(crate) fn with_settings(profile: Profile, settings: rustia_vm::VmSettings) -> Self {
        Self::from_settings(profile, settings)
    }

    /// Creates a typed interpreter with explicit low-level settings and services.
    ///
    /// Prefer [`RustiaBuilder::build_for_host`] for application configuration.
    #[cfg(test)]
    pub(crate) fn for_host_with_settings<H>(
        profile: Profile,
        settings: rustia_vm::VmSettings,
    ) -> Rustia<H>
    where
        H: HostSchema,
    {
        Rustia::<H>::from_settings(profile, settings)
    }

    /// Prepares Luau source text or bytes to run in this interpreter.
    ///
    /// Building the chunk is inert: parsing, compilation, VM allocation, and
    /// execution begin only when a terminal operation is called.
    /// Use [`Chunk::source_name`] to label errors, then choose [`Chunk::eval`],
    /// [`Chunk::exec`], or [`Chunk::install`]. The source only needs to live until
    /// that operation finishes; returned values do not borrow it.
    ///
    /// ```
    /// # use rustia::Rustia;
    /// # let mut vm = Rustia::builder().build()?;
    /// let answer: i32 = vm
    ///     .load("return 6 * 7")
    ///     .source_name("answer.luau")
    ///     .eval()?;
    /// assert_eq!(answer, 42);
    /// # Ok::<(), rustia::Error>(())
    /// ```
    pub fn load<'rustia, 'source, S>(
        &'rustia mut self,
        source: &'source S,
    ) -> Chunk<'rustia, 'source>
    where
        S: AsRef<[u8]> + ?Sized,
    {
        Chunk::new(self, source.as_ref())
    }

    /// Evaluates source and converts its first result through [`FromRustia`].
    ///
    /// Shorthand for `load(source).eval()`. Use [`load`](Self::load) to give the
    /// source a name or select its environment. See [`Chunk::eval`] for conversion
    /// and failure behavior.
    pub fn eval<T>(&mut self, source: impl AsRef<[u8]>) -> Result<T>
    where
        T: FromRustia,
    {
        let source = source;
        self.load(&source).eval()
    }

    /// Evaluates source and deserializes its first result directly from the VM.
    ///
    /// Shorthand for `load(source).eval_serde()`; see [`Chunk::eval_serde`].
    pub fn eval_serde<T>(&mut self, source: impl AsRef<[u8]>) -> Result<T>
    where
        T: serde::de::DeserializeOwned,
    {
        let source = source;
        self.load(&source).eval_serde()
    }

    /// Executes source for side effects and discards its result.
    ///
    /// Shorthand for `load(source).exec()`; see [`Chunk::exec`].
    pub fn exec(&mut self, source: impl AsRef<[u8]>) -> Result<()> {
        let source = source;
        self.load(&source).exec()
    }

    /// Runs a compiled module in a fresh environment and retains its exports.
    ///
    /// The environment inherits VM globals. A failed installation restores prior
    /// guest state; external host callback and module-resolver effects cannot be
    /// undone. Previously installed exports remain usable until reset.
    /// See [`Chunk::install`] to compile and install source in one step.
    ///
    /// ```
    /// # use rustia::Rustia;
    /// let module = Rustia::compile_module(
    ///     "greeting.luau",
    ///     "return { greet = function(name) return 'Hello, ' .. name end }",
    /// )?;
    /// let mut vm = Rustia::builder().build()?;
    /// let exports = vm.install(&module)?;
    /// let greet = exports.function(&mut vm, "greet")?;
    /// let greeting: String = greet.call(&mut vm, ("Ada",))?;
    /// assert_eq!(greeting, "Hello, Ada");
    /// # Ok::<(), rustia::Error>(())
    /// ```
    pub fn install(&mut self, module: &CompiledModule) -> Result<Exports> {
        self.install_with_environment(module, None)
    }
}

impl<C> Rustia<C>
where
    C: HostSchema,
{
    fn install_with_environment(
        &mut self,
        module: &CompiledModule,
        environment: Option<rustia_vm::Value>,
    ) -> Result<Exports> {
        self.vm
            .begin_guest_transaction()
            .map_err(Error::from_engine)?;
        let outcome: Result<Exports> = (|| {
            let module_cohort = self.vm.create_module_cohort().map_err(Error::from_engine)?;
            let environment = match environment {
                Some(environment) => environment,
                None => self
                    .vm
                    .create_module_environment()
                    .map_err(Error::from_engine)?,
            };
            let value = self
                .vm
                .run_installation_with_environment(&module.program, environment, module_cohort)
                .map_err(Error::from_engine)?;
            let value = self.vm.retain_value(value).map_err(Error::from_engine)?;
            Ok(Exports::new(value, self.decision_epoch()))
        })();
        match outcome {
            Ok(exports) => {
                if let Err(error) = self.vm.commit_guest_transaction() {
                    self.vm.rollback_guest_transaction();
                    Err(Error::from_engine(error).installation_rolled_back())
                } else {
                    Ok(exports)
                }
            }
            Err(error) => {
                self.vm.rollback_guest_transaction();
                Err(error.installation_rolled_back())
            }
        }
    }
}

impl Rustia<()> {
    /// Registers a Rust function as a script global.
    ///
    /// Arguments and results are converted automatically. Return [`Result<T>`]
    /// from the callback to report a script-visible error. A call costs one fuel
    /// unit in addition to guest instructions; use [`add_fn_with`](Self::add_fn_with)
    /// to select another cost or declare captured-state effects.
    ///
    /// The callback must be `Fn + Send + Sync + 'static`. This helper declares
    /// it pure, so captured state must honour that promise. The registration
    /// survives [`reset_decision`](Self::reset_decision). For a live function
    /// value instead of a global binding, use [`create_function`](Self::create_function).
    ///
    /// ```
    /// # use rustia::Rustia;
    /// # let mut vm = Rustia::builder().build()?;
    /// vm.add_fn("divide", |a: f64, b: f64| -> rustia::Result<f64> {
    ///     if b == 0.0 {
    ///         return Err(rustia::Error::embed("division by zero"));
    ///     }
    ///     Ok(a / b)
    /// })?;
    /// let quotient: f64 = vm.eval("return divide(84, 2)")?;
    /// assert_eq!(quotient, 42.0);
    /// # Ok::<(), rustia::Error>(())
    /// ```
    pub fn add_fn<Func, Args, Return>(
        &mut self,
        name: impl Into<String>,
        function: Func,
    ) -> Result<()>
    where
        Func: IntoHostFunction<Args, Return>,
    {
        self.add_fn_with(name, 1, OpCapability::Pure, function)
    }

    /// Registers a pure callback; equivalent to [`add_fn`](Self::add_fn).
    pub fn add_pure_fn<Func, Args, Return>(
        &mut self,
        name: impl Into<String>,
        function: Func,
    ) -> Result<()>
    where
        Func: IntoHostFunction<Args, Return>,
    {
        self.add_fn_with(name, 1, OpCapability::Pure, function)
    }

    /// Registers a callback declared to read deterministic captured input.
    ///
    /// The callback receives ordinary arguments. For borrowed per-invocation
    /// input in a typed host, use [`add_input`](Self::add_input).
    pub fn add_input_fn<Func, Args, Return>(
        &mut self,
        name: impl Into<String>,
        function: Func,
    ) -> Result<()>
    where
        Func: IntoHostFunction<Args, Return>,
    {
        self.add_fn_with(name, 1, OpCapability::ReadsDeterministicInput, function)
    }

    /// Registers a callback declared to read captured slot state.
    ///
    /// For a borrowed [`Slots`] view in a typed host, use
    /// [`add_slot_read`](Self::add_slot_read).
    pub fn add_slot_read_fn<Func, Args, Return>(
        &mut self,
        name: impl Into<String>,
        function: Func,
    ) -> Result<()>
    where
        Func: IntoHostFunction<Args, Return>,
    {
        self.add_fn_with(name, 1, OpCapability::ReadsSlotState, function)
    }

    /// Registers a callback declared to produce slot-write effects.
    ///
    /// Captured Rust mutations are not rolled back. For a transactional
    /// [`SlotWrites`] journal, use [`add_slot_write`](Self::add_slot_write).
    pub fn add_slot_write_fn<Func, Args, Return>(
        &mut self,
        name: impl Into<String>,
        function: Func,
    ) -> Result<()>
    where
        Func: IntoHostFunction<Args, Return>,
    {
        self.add_fn_with(name, 1, OpCapability::WritesSlotEffect, function)
    }

    /// Registers a callback declared to produce deterministic effects.
    ///
    /// Captured Rust mutations are not rolled back. For a transactional
    /// [`Effects`] journal, use [`add_effect`](Self::add_effect).
    pub fn add_effect_fn<Func, Args, Return>(
        &mut self,
        name: impl Into<String>,
        function: Func,
    ) -> Result<()>
    where
        Func: IntoHostFunction<Args, Return>,
    {
        self.add_fn_with(name, 1, OpCapability::EmitsDeterministicEffect, function)
    }

    /// Registers a callback declared to access mutable external state.
    ///
    /// Deterministic profiles reject this capability. For a borrowed [`HostMut`]
    /// view in a typed host, use [`add_external`](Self::add_external).
    pub fn add_external_state_fn<Func, Args, Return>(
        &mut self,
        name: impl Into<String>,
        function: Func,
    ) -> Result<()>
    where
        Func: IntoHostFunction<Args, Return>,
    {
        self.add_fn_with(name, 1, OpCapability::ReadsExternalMutableState, function)
    }

    /// Registers a callback declared to have nondeterministic effects.
    ///
    /// Deterministic profiles reject this capability. Captured Rust effects are
    /// outside installation and invocation rollback.
    pub fn add_nondeterministic_fn<Func, Args, Return>(
        &mut self,
        name: impl Into<String>,
        function: Func,
    ) -> Result<()>
    where
        Func: IntoHostFunction<Args, Return>,
    {
        self.add_fn_with(name, 1, OpCapability::NondeterministicEffect, function)
    }

    /// Registers a callback with an explicit fuel cost and capability.
    ///
    /// A zero fuel cost or a capability forbidden by the selected profile fails
    /// before registration. The capability describes captured host behavior;
    /// it does not restrict what a Rust closure can do. See [`add_fn`](Self::add_fn)
    /// for the callback's conversion and lifetime rules.
    pub fn add_fn_with<Func, Args, Return>(
        &mut self,
        name: impl Into<String>,
        fuel_cost: u32,
        capability: OpCapability,
        function: Func,
    ) -> Result<()>
    where
        Func: IntoHostFunction<Args, Return>,
    {
        self.register_function(
            name,
            HostFunction::with_capability(capability, function).fuel_cost(fuel_cost),
        )
    }

    /// Installs a collection of host functions and modules atomically.
    ///
    /// Validation or binding failure leaves existing registrations unchanged.
    /// Use [`RustiaExtension`] to assemble a reusable application API.
    pub fn install_extension(&mut self, extension: RustiaExtension) -> Result<()> {
        extension.install(self)
    }
}

impl Default for Rustia {
    /// Creates the standard preset, matching [`Rustia::builder`].
    fn default() -> Self {
        Self::builder()
            .build()
            .expect("the standard preset is valid without caller-supplied configuration")
    }
}

/// A value returned by Rustia, or a script, conversion, or embedding [`Error`].
pub type Result<T> = std::result::Result<T, Error>;

/// A compiled script ready for [`Rustia::install`].
///
/// Create it with [`Rustia::compile_module`]. It owns its source metadata and
/// compiled code independently of any interpreter, and remains reusable after
/// reset. A clone shares constant and string storage; function and debug metadata
/// are currently copied.
#[derive(Clone)]
pub struct CompiledModule {
    source_name: String,
    program: Program,
}

impl CompiledModule {
    /// Returns the source label supplied when this module was compiled.
    pub fn source_name(&self) -> &str {
        &self.source_name
    }
}

/// Precompiled top-level decision body for allocation-light repeated
/// invocation through [`Invocation::call_entry`].
#[derive(Clone)]
pub struct CompiledEntry {
    source_name: String,
    program: HardenedLeafProgram,
}

impl CompiledEntry {
    /// Returns the source label supplied when this entry was compiled.
    pub fn source_name(&self) -> &str {
        &self.source_name
    }
}

/// A half-open byte range in the source, `start..end`.
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub struct SourceSpan {
    /// Byte offset of the first included byte.
    pub start: usize,
    /// Byte offset immediately after the range.
    pub end: usize,
}

impl SourceSpan {
    pub(crate) fn from_lexer(span: rustia_lexer::Span) -> Self {
        Self {
            start: span.start,
            end: span.end,
        }
    }
}

/// The source label and one-based line number attached to an error.
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub struct SourceLocation<'a> {
    /// Source label, such as `"rules.luau"`; it need not be a filesystem path.
    pub source: &'a str,
    /// One-based source line number.
    pub line: u32,
}

#[cfg(test)]
mod tests {
    use super::{
        finish_entry_lifecycle, Error, FromRustia, GuestSurface, IntoRustia, OpCapability,
        OwnedTable, OwnedValue, Profile, Result, Rustia, RustiaBuilder, RustiaExecutionProfile,
        RustiaExtension, RustiaHostFunctionInfo, ServiceClock, ServiceEntropy, SourceSpan,
        ValueType, VmErrorKind, RUSTIA_DECISION_RESET_ABI_VERSION,
        RUSTIA_DEFAULT_HOST_FUNCTION_FUEL, RUSTIA_HOST_FUEL_ABI_VERSION, RUSTIA_HOST_FUEL_POLICY,
        RUSTIA_MIN_HOST_FUNCTION_FUEL,
    };
    use rustia_compiler::{compile_with_options, CompileOptions};
    use rustia_parser::parse_source;
    use rustia_vm::{Vm, VmSettings};
    use serde::{Deserialize, Serialize};
    use std::collections::BTreeMap;
    use std::num::NonZeroU64;
    use std::sync::{Arc, Mutex};

    #[test]
    fn install_smoke() {
        let mut rustia = Rustia::new(Profile::DeterministicSim);
        let result = rustia.eval("local x: number = 6 * 7; return x").unwrap();
        assert_eq!(f64::from_rustia(result).unwrap(), 42.0);
    }

    #[test]
    fn live_values_preserve_identity_cycles_and_rich_payloads() {
        let mut rustia = Rustia::new(Profile::ReproducibleStandard);
        let root = rustia
            .load(
                r#"
local root = {}
local richKey = {}
local shared = { answer = 42 }
root.self = root
root.left = shared
root.right = shared
root.binary = string.char(0, 255, 65)
root.bytes = buffer.fromstring(string.char(1, 0, 254))
root.direction = vector(1.5, -2, 3)
root[richKey] = shared
return root
"#,
            )
            .eval_live()
            .unwrap();
        assert_eq!(root.kind(&rustia).unwrap(), ValueType::Table);
        let table = root.as_table(&rustia).unwrap().unwrap();

        let self_value = table.get(&mut rustia, "self").unwrap();
        assert!(root.raw_equal(&mut rustia, &self_value).unwrap());
        let left = table.get(&mut rustia, "left").unwrap();
        let right = table.get(&mut rustia, "right").unwrap();
        assert!(left.raw_equal(&mut rustia, &right).unwrap());
        assert_eq!(
            table
                .get(&mut rustia, "binary")
                .unwrap()
                .string_bytes(&mut rustia)
                .unwrap(),
            Some(vec![0, 255, 65])
        );
        assert_eq!(
            table
                .get(&mut rustia, "bytes")
                .unwrap()
                .buffer_bytes(&mut rustia)
                .unwrap(),
            Some(vec![1, 0, 254])
        );
        assert_eq!(
            table
                .get(&mut rustia, "direction")
                .unwrap()
                .vector_components(&mut rustia)
                .unwrap(),
            Some([1.5, -2.0, 3.0])
        );

        table.set(&mut rustia, "added", 99_i32).unwrap();
        assert_eq!(
            table
                .get(&mut rustia, "added")
                .unwrap()
                .as_number(&rustia)
                .unwrap(),
            Some(99.0)
        );
        let rich_entry = table
            .entries(&mut rustia)
            .unwrap()
            .into_iter()
            .find(|(key, _)| key.kind(&rustia).unwrap() == ValueType::Table)
            .expect("table identity key");
        assert!(left.raw_equal(&mut rustia, &rich_entry.1).unwrap());

        let exports = rustia
            .load("local live = { answer = 7 }; return { live = live }")
            .install()
            .unwrap();
        let exported = exports.get_live(&mut rustia, "live").unwrap();
        assert_eq!(
            exported
                .as_table(&rustia)
                .unwrap()
                .unwrap()
                .get(&mut rustia, "answer")
                .unwrap()
                .as_number(&rustia)
                .unwrap(),
            Some(7.0)
        );

        let _: f64 = rustia.eval("collectgarbage('collect'); return 0").unwrap();
        assert_eq!(
            left.as_table(&rustia)
                .unwrap()
                .unwrap()
                .get(&mut rustia, "answer")
                .unwrap()
                .as_number(&rustia)
                .unwrap(),
            Some(42.0),
            "a live handle remains a collector root"
        );
        assert!(
            root.to_owned(&mut rustia).is_err(),
            "cyclic snapshots stay explicit"
        );

        let mut other = Rustia::new(Profile::ReproducibleStandard);
        assert!(matches!(
            root.kind(&other)
                .unwrap_err()
                .as_vm_error()
                .map(|error| &error.kind),
            Some(VmErrorKind::RetainedValueFromDifferentVm)
        ));
        other.reset_decision().unwrap();
        rustia.reset_decision().unwrap();
        assert!(matches!(
            root.kind(&rustia)
                .unwrap_err()
                .as_vm_error()
                .map(|error| &error.kind),
            Some(VmErrorKind::StaleRetainedValue {
                retained_epoch: 0,
                current_epoch: 1
            })
        ));
    }

    #[test]
    fn constructed_tables_install_into_the_live_globals_graph() {
        let mut rustia = Rustia::new(Profile::ReproducibleStandard);
        let globals = rustia.globals().unwrap();
        let root = rustia.create_table().unwrap();
        root.set(&mut rustia, "answer", 42_i32).unwrap();
        root.set_live(&mut rustia, "self", &root.as_value())
            .unwrap();
        globals
            .set_live(&mut rustia, "host_graph", &root.as_value())
            .unwrap();

        let returned = rustia.load("return host_graph").eval_live().unwrap();
        assert!(root.as_value().raw_equal(&mut rustia, &returned).unwrap());
        let returned_table = returned.as_table(&rustia).unwrap().unwrap();
        let returned_self = returned_table.get(&mut rustia, "self").unwrap();
        assert!(returned.raw_equal(&mut rustia, &returned_self).unwrap());

        // Ordinary execution does not invalidate unrelated live handles.
        root.set(&mut rustia, "after_run", 7_i32).unwrap();
        let sees_mutation: bool = rustia
            .eval("return host_graph.answer == 42 and host_graph.after_run == 7")
            .unwrap();
        assert!(sees_mutation);

        let globals_again = rustia.globals().unwrap();
        assert!(globals
            .as_value()
            .raw_equal(&mut rustia, &globals_again.as_value())
            .unwrap());
    }

    #[test]
    fn live_table_constructors_are_available_in_every_standard_surface_profile() {
        for profile in [
            Profile::LuauCompat,
            Profile::ReproducibleStandard,
            Profile::DeterministicSim,
            Profile::Puc51Compat,
        ] {
            let mut rustia = Rustia::new(profile);
            let value = rustia.create_table().unwrap();
            value.set(&mut rustia, "profile_value", 9_i32).unwrap();
            rustia
                .globals()
                .unwrap()
                .set_live(&mut rustia, "host_value", &value.as_value())
                .unwrap();
            let observed: f64 = rustia.eval("return host_value.profile_value").unwrap();
            assert_eq!(observed, 9.0, "profile {profile:?}");
        }
    }

    #[test]
    fn set_live_rejects_foreign_tables_and_values_before_mutation() {
        let mut first = Rustia::new(Profile::ReproducibleStandard);
        let mut second = Rustia::new(Profile::ReproducibleStandard);
        let first_table = first.create_table().unwrap();
        let second_table = second.create_table().unwrap();

        let foreign_value = first_table
            .set_live(&mut first, "foreign", &second_table.as_value())
            .expect_err("a value from another VM must reject");
        assert!(matches!(
            foreign_value.as_vm_error().map(|error| &error.kind),
            Some(VmErrorKind::RetainedValueFromDifferentVm)
        ));
        assert!(first_table
            .get(&mut first, "foreign")
            .unwrap()
            .is_nil(&first)
            .unwrap());

        let foreign_table = second_table
            .set_live(&mut first, "foreign", &first_table.as_value())
            .expect_err("a table from another VM must reject");
        assert!(matches!(
            foreign_table.as_vm_error().map(|error| &error.kind),
            Some(VmErrorKind::RetainedValueFromDifferentVm)
        ));
    }

    #[test]
    fn decision_reset_stales_constructor_handles_and_discards_global_mutations() {
        let mut rustia = Rustia::new(Profile::ReproducibleStandard);
        let globals = rustia.globals().unwrap();
        let table = rustia.create_table().unwrap();
        globals
            .set_live(&mut rustia, "decision_local", &table.as_value())
            .unwrap();

        rustia.reset_decision().unwrap();
        assert!(matches!(
            globals
                .get(&mut rustia, "decision_local")
                .unwrap_err()
                .as_vm_error()
                .map(|error| &error.kind),
            Some(VmErrorKind::StaleRetainedValue {
                retained_epoch: 0,
                current_epoch: 1
            })
        ));
        assert!(matches!(
            table
                .set(&mut rustia, "value", 1_i32)
                .unwrap_err()
                .as_vm_error()
                .map(|error| &error.kind),
            Some(VmErrorKind::StaleRetainedValue {
                retained_epoch: 0,
                current_epoch: 1
            })
        ));
        let discarded: bool = rustia.eval("return decision_local == nil").unwrap();
        assert!(discarded);
    }

    #[test]
    fn hardened_leaves_reject_live_table_authority_without_allocating() {
        let mut rustia = RustiaBuilder::deterministic_leaf()
            .memory_limit_bytes(512 * 1024)
            .build_for_host::<()>()
            .unwrap();
        let before = rustia.memory_stats().live_bytes();

        let globals = rustia.globals().expect_err("hardened globals must reject");
        assert!(matches!(
            globals,
            Error { kind: crate::ErrorKind::Vm(ref error), .. }
                if matches!(
                    &error.kind,
                    VmErrorKind::HardenedLeafViolation {
                        feature: "mutable globals handles"
                    }
                )
        ));
        let table = rustia
            .create_table()
            .expect_err("hardened table construction must reject");
        assert!(matches!(
            table,
            Error { kind: crate::ErrorKind::Vm(ref error), .. }
                if matches!(
                    &error.kind,
                    VmErrorKind::HardenedLeafViolation {
                        feature: "live table construction"
                    }
                )
        ));
        assert_eq!(rustia.memory_stats().live_bytes(), before);
    }

    #[test]
    fn live_table_constructor_quota_failure_recovers_after_a_root_is_dropped() {
        let mut baseline = RustiaBuilder::standard().build().unwrap();
        let limit = baseline
            .memory_stats()
            .live_bytes()
            .saturating_add(4 * 1024);
        drop(baseline);
        let mut rustia = RustiaBuilder::standard()
            .memory_limit_bytes(limit)
            .build()
            .unwrap();
        let mut retained = Vec::new();
        let error = loop {
            match rustia.create_table() {
                Ok(table) => {
                    retained.push(table);
                    assert!(retained.len() < 10_000, "constructor quota did not bind");
                }
                Err(error) => break error,
            }
        };
        assert!(matches!(
            error,
            Error { kind: crate::ErrorKind::Vm(ref error), .. } if matches!(&error.kind, VmErrorKind::OutOfMemory)
        ));

        retained.pop().expect("at least one table fit before OOM");
        rustia
            .create_table()
            .expect("dropping one root must make one constructor admission recover");
    }

    #[test]
    fn live_functions_accept_and_return_live_values() {
        let mut rustia = Rustia::new(Profile::ReproducibleStandard);
        let function = rustia
            .load(
                "return function(value) local t = { value = value }; t.self = t; return value, t end",
            )
            .eval_live()
            .unwrap()
            .as_function(&rustia)
            .unwrap()
            .unwrap();
        let argument = rustia.load("return { answer = 42 }").eval_live().unwrap();
        let results = function
            .call_values(&mut rustia, std::slice::from_ref(&argument))
            .unwrap();
        assert_eq!(results.len(), 2);
        assert!(argument.raw_equal(&mut rustia, &results[0]).unwrap());
        let returned = results[1].as_table(&rustia).unwrap().unwrap();
        let returned_value = returned.get(&mut rustia, "value").unwrap();
        assert!(argument.raw_equal(&mut rustia, &returned_value).unwrap());
        let returned_self = returned.get(&mut rustia, "self").unwrap();
        assert!(returned
            .as_value()
            .raw_equal(&mut rustia, &returned_self)
            .unwrap());
    }

    #[test]
    fn entry_lifecycle_removes_new_records_when_reset_fails() {
        let mut records = vec![1, 2, 3];
        let result = finish_entry_lifecycle(
            &mut records,
            2,
            Ok(Ok(42)),
            Err(Error::embed("reset failed".to_owned())),
        );
        assert!(
            matches!(result, Err(Error { kind: crate::ErrorKind::Embed(ref message), .. }) if message == "reset failed")
        );
        assert_eq!(records, [1, 2]);
    }

    #[test]
    fn compiled_modules_can_be_cloned_across_vms() {
        let module = Rustia::compile_module(
            "shared.luau",
            "local greeting = 'compiled string shared across VMs'; return { greeting = greeting }",
        )
        .unwrap();
        let mut first = Rustia::new(Profile::ReproducibleStandard);
        let mut second = Rustia::new(Profile::ReproducibleStandard);

        first.install(&module).unwrap();
        second.install(&module).unwrap();
    }

    #[test]
    fn typed_run_converts_owned_results() {
        let mut rustia = Rustia::new(Profile::ReproducibleStandard);

        let answer: f64 = rustia.eval("return 40 + 2").unwrap();
        let numbers: Vec<i32> = rustia.eval("return { 1, 2, 3 }").unwrap();
        let missing: Option<String> = rustia.eval("return nil").unwrap();
        let named: BTreeMap<String, i32> = rustia.eval("return { scout = 5, brute = 9 }").unwrap();

        assert_eq!(answer, 42.0);
        assert_eq!(numbers, vec![1, 2, 3]);
        assert_eq!(missing, None);
        assert_eq!(named.get("scout"), Some(&5));
        assert_eq!(named.get("brute"), Some(&9));
    }

    #[test]
    fn typed_run_reports_conversion_errors() {
        let mut rustia = Rustia::new(Profile::ReproducibleStandard);
        let error = rustia.eval::<String>("return 42").unwrap_err().to_string();
        assert!(error.contains("expected string, got number"), "{error}");

        let array_error = rustia
            .eval::<BTreeMap<String, i32>>("return { 1, 2 }")
            .unwrap_err()
            .to_string();
        assert!(
            array_error.contains("expected string-keyed table, got table with array entries"),
            "{array_error}"
        );

        let key_error = rustia
            .eval::<BTreeMap<String, i32>>("return { [true] = 1 }")
            .unwrap_err()
            .to_string();
        assert!(
            key_error.contains("expected string-keyed table, got non-string key"),
            "{key_error}"
        );
    }

    #[test]
    fn numeric_table_keys_normalize_negative_zero_at_script_boundary() {
        let mut rustia = Rustia::new(Profile::ReproducibleStandard);
        let result = rustia
            .eval(
                "\
local t = {}
t[-0] = 11
t[0] = t[0] + 31
return t[-0] == 42 and t[0] == 42 and rawget(t, -0) == 42 and rawget(t, 0) == 42
",
            )
            .unwrap();
        assert!(bool::from_rustia(result).unwrap());
    }

    #[test]
    fn fuel_exhaustion_escapes_pcall_and_xpcall() {
        let cases = [
            "return pcall(function() while true do end end)",
            "return xpcall(function() while true do end end, function() return 'caught' end)",
        ];

        for source in cases {
            let settings = VmSettings {
                initial_fuel: Some(1_000),
                ..VmSettings::default()
            };
            let mut rustia = Rustia::with_settings(Profile::DeterministicSim, settings);
            let Err(Error {
                kind: crate::ErrorKind::Vm(error),
                ..
            }) = rustia.eval::<OwnedValue>(source)
            else {
                panic!("expected fuel exhaustion to escape protected call: {source}");
            };
            assert!(matches!(
                error.kind,
                VmErrorKind::FuelExhausted { limit: 1_000 }
            ));
        }
    }

    #[test]
    fn owned_values_snapshot_returned_tables() {
        let mut rustia = Rustia::new(Profile::ReproducibleStandard);
        let value: OwnedValue = rustia
            .eval(
                "\
return {
    label = 'spawn',
    count = 3,
    flags = { true, false },
}
",
            )
            .unwrap();
        let table = value.table().unwrap();
        assert_eq!(
            table.get_string("label"),
            Some(&OwnedValue::String("spawn".to_owned()))
        );
        assert_eq!(table.get_string("count"), Some(&OwnedValue::Number(3.0)));
        let flags = table.get_string("flags").unwrap().table().unwrap();
        assert_eq!(
            flags.array,
            vec![OwnedValue::Boolean(true), OwnedValue::Boolean(false)]
        );
    }

    #[test]
    fn owned_table_helpers_accept_typed_values_and_getters() {
        let labels = OwnedTable::string_entries([("kind", "scout"), ("role", "ai")]).unwrap();
        assert_eq!(
            labels.get_string_as::<String>("kind").unwrap().as_deref(),
            Some("scout")
        );
        assert_eq!(labels.get_string_as::<String>("missing").unwrap(), None);

        let stats = OwnedTable::string_entries([("hp", 5_i32), ("speed", 7_i32)]).unwrap();
        assert_eq!(stats.get_string_as::<i32>("hp").unwrap(), Some(5));

        let error = labels
            .get_string_as::<bool>("kind")
            .unwrap_err()
            .to_string();
        assert!(
            error.contains("expected boolean, got string"),
            "unexpected error: {error}"
        );

        let array = OwnedTable::array_values([1_i32, 2, 3]).unwrap();
        assert_eq!(
            array.array,
            vec![
                OwnedValue::Number(1.0),
                OwnedValue::Number(2.0),
                OwnedValue::Number(3.0)
            ]
        );
        assert!(array.entries.is_empty());
    }

    #[test]
    fn integer_embedding_rejects_silent_luau_number_rounding() {
        let exact = (1_u64 << 53).into_rustia().unwrap();
        assert_eq!(exact, OwnedValue::Number((1_u64 << 53) as f64));

        for error in [
            ((1_u64 << 53) + 1).into_rustia().unwrap_err(),
            u64::MAX.into_rustia().unwrap_err(),
            i64::MAX.into_rustia().unwrap_err(),
        ] {
            assert!(
                error.to_string().contains("cannot be represented exactly"),
                "unexpected error: {error}"
            );
        }

        assert!(u64::from_rustia(OwnedValue::Number(2.0_f64.powi(64))).is_err());
        assert!(i64::from_rustia(OwnedValue::Number(2.0_f64.powi(63))).is_err());

        let mut rustia = Rustia::new(Profile::ReproducibleStandard);
        assert!(rustia.insert_global("too_wide", u64::MAX).is_err());
    }

    #[test]
    fn serde_integer_conversion_is_exact_for_values_and_map_keys() {
        assert_eq!(
            OwnedValue::from_serialize(&(1_u64 << 53)).unwrap(),
            OwnedValue::Number((1_u64 << 53) as f64)
        );
        assert!(OwnedValue::from_serialize(&((1_u64 << 53) + 1)).is_err());
        assert!(OwnedValue::from_serialize(&u128::MAX).is_err());

        let exact_wide = 1_i128 << 100;
        let serialized = OwnedValue::from_serialize(&exact_wide).unwrap();
        assert_eq!(serialized.deserialize::<i128>().unwrap(), exact_wide);

        let rounded_key = BTreeMap::from([((1_u64 << 53) + 1, true)]);
        assert!(OwnedValue::from_serialize(&rounded_key).is_err());

        assert!(OwnedValue::Number(2.0_f64.powi(64))
            .deserialize::<u64>()
            .is_err());
        assert!(OwnedValue::Number(2.0_f64.powi(128))
            .deserialize::<u128>()
            .is_err());
    }

    #[test]
    fn owned_table_helpers_support_nested_typed_access() {
        let mut rustia = Rustia::new(Profile::ReproducibleStandard);
        let value: OwnedValue = rustia
            .eval(
                "\
return {
    config = {
        spawn = {
            kind = 'scout',
            stats = { 5, 7 },
        },
    },
}
",
            )
            .unwrap();

        let root = value.table().unwrap();
        let config = root.get_string_table("config").unwrap().unwrap();
        let spawn = config.get_string_table("spawn").unwrap().unwrap();
        assert_eq!(
            spawn.get_string_as::<String>("kind").unwrap().as_deref(),
            Some("scout")
        );
        assert!(spawn.get_string_table("missing").unwrap().is_none());

        let stats = spawn.get_string_table("stats").unwrap().unwrap();
        assert_eq!(stats.get_array(0), Some(&OwnedValue::Number(5.0)));
        assert_eq!(stats.get_array_as::<i32>(0).unwrap(), Some(5));
        assert_eq!(stats.get_array_as::<i32>(1).unwrap(), Some(7));
        assert_eq!(stats.get_array_as::<i32>(2).unwrap(), None);

        let string_error = spawn.get_string_table("kind").unwrap_err().to_string();
        assert!(
            string_error.contains("expected table, got string"),
            "unexpected error: {string_error}"
        );
        let array_error = stats.get_array_table(0).unwrap_err().to_string();
        assert!(
            array_error.contains("expected table, got number"),
            "unexpected error: {array_error}"
        );
    }

    #[test]
    fn owned_table_helpers_deserialize_nested_fields() {
        #[derive(Debug, Deserialize, PartialEq)]
        struct SpawnConfig<'a> {
            kind: &'a str,
            hp: u32,
            stats: Vec<u32>,
        }

        #[derive(Debug, Deserialize, PartialEq)]
        struct Enemy<'a> {
            kind: &'a str,
            hp: u32,
        }

        let mut rustia = Rustia::new(Profile::ReproducibleStandard);
        let value: OwnedValue = rustia
            .eval(
                "\
return {
    config = {
        kind = 'scout',
        hp = 5,
        stats = { 7, 9 },
    },
    enemies = {
        { kind = 'brute', hp = 9 },
        { kind = 'scout', hp = 5 },
    },
}
",
            )
            .unwrap();

        let root = value.table().unwrap();
        let config = root
            .get_string_deserialized::<SpawnConfig<'_>>("config")
            .unwrap()
            .unwrap();
        assert_eq!(
            config,
            SpawnConfig {
                kind: "scout",
                hp: 5,
                stats: vec![7, 9]
            }
        );
        assert!(root
            .get_string_deserialized::<SpawnConfig<'_>>("missing")
            .unwrap()
            .is_none());

        let enemies = root.get_string_table("enemies").unwrap().unwrap();
        assert_eq!(
            enemies.get_array_deserialized::<Enemy<'_>>(0).unwrap(),
            Some(Enemy {
                kind: "brute",
                hp: 9
            })
        );
        assert_eq!(
            enemies.get_array_deserialized::<Enemy<'_>>(1).unwrap(),
            Some(Enemy {
                kind: "scout",
                hp: 5
            })
        );
        assert!(enemies
            .get_array_deserialized::<Enemy<'_>>(2)
            .unwrap()
            .is_none());
    }

    #[test]
    fn insert_global_accepts_option_vec_and_owned_table() {
        let mut rustia = Rustia::new(Profile::ReproducibleStandard);
        let named_map = BTreeMap::from([("scout".to_owned(), 5_i32), ("brute".to_owned(), 9_i32)]);
        rustia.insert_global("maybe", Some(4_i32)).unwrap();
        rustia
            .insert_global("missing", Option::<i32>::None)
            .unwrap();
        rustia.insert_global("numbers", vec![1_i32, 2, 3]).unwrap();
        rustia.insert_global("named_map", named_map).unwrap();
        rustia
            .insert_global(
                "named",
                OwnedTable::string_entries([("kind", "scout")]).unwrap(),
            )
            .unwrap();

        let value = rustia
            .eval(
                "\
return maybe == 4
    and missing == nil
    and numbers[1] == 1
    and numbers[3] == 3
    and named_map.scout == 5
    and named_map.brute == 9
    and named.kind == 'scout'
",
            )
            .unwrap();
        assert!(bool::from_rustia(value).unwrap());
    }

    #[test]
    fn insert_global_and_typed_add_fn_are_callable() {
        let mut rustia = Rustia::new(Profile::DeterministicSim);
        rustia.insert_global("difficulty", 2_i32).unwrap();
        rustia
            .add_fn("clamp", |x: f64, lo: f64, hi: f64| x.clamp(lo, hi))
            .unwrap();

        let result = rustia.eval("return clamp(12 + difficulty, 0, 10)").unwrap();
        assert_eq!(f64::from_rustia(result).unwrap(), 10.0);
    }

    #[test]
    fn typed_host_functions_support_eight_arguments() {
        let mut rustia = Rustia::new(Profile::ReproducibleStandard);
        rustia
            .add_fn(
                "sum8",
                |a: f64, b: f64, c: f64, d: f64, e: f64, f: f64, g: f64, h: f64| {
                    a + b + c + d + e + f + g + h
                },
            )
            .unwrap();

        let result = rustia.eval("return sum8(1, 2, 3, 4, 5, 6, 7, 8)").unwrap();
        assert_eq!(f64::from_rustia(result).unwrap(), 36.0);
    }

    #[test]
    fn typed_host_functions_accept_and_return_owned_rust_shapes() {
        let mut rustia = Rustia::new(Profile::ReproducibleStandard);
        rustia
            .add_fn("sum_vec", |values: Vec<f64>| {
                values.into_iter().sum::<f64>()
            })
            .unwrap();
        rustia
            .add_fn("maybe_name", |enabled: bool| {
                enabled.then_some("scout".to_owned())
            })
            .unwrap();
        rustia.add_fn("make_vec", || vec![2.0, 4.0, 6.0]).unwrap();
        rustia
            .add_fn("sum_map", |values: BTreeMap<String, f64>| {
                values.values().sum::<f64>()
            })
            .unwrap();
        rustia
            .add_fn("make_map", || {
                BTreeMap::from([("scout".to_owned(), 5.0), ("brute".to_owned(), 9.0)])
            })
            .unwrap();
        rustia
            .add_fn("make_table", || {
                OwnedTable::string_entries([("kind", "scout")])
            })
            .unwrap();

        let value = rustia
            .eval(
                "\
return sum_vec({ 1, 2, 3 }) == 6
    and maybe_name(true) == 'scout'
    and maybe_name(false) == nil
    and make_vec()[2] == 4
    and sum_map({ scout = 5, brute = 9 }) == 14
    and make_map().brute == 9
    and make_table().kind == 'scout'
",
            )
            .unwrap();
        assert!(bool::from_rustia(value).unwrap());
    }

    #[test]
    fn facade_capability_helpers_register_allowed_deterministic_functions() {
        let mut rustia = Rustia::new(Profile::DeterministicSim);
        rustia.add_pure_fn("pure", || 5.0).unwrap();
        rustia.add_input_fn("input", || 2.0).unwrap();
        rustia.add_slot_read_fn("slot", || 3.0).unwrap();
        rustia
            .add_slot_write_fn("slot_write", |value: f64| value + 4.0)
            .unwrap();
        rustia
            .add_effect_fn("effect", |value: f64| value * 2.0)
            .unwrap();

        let result = rustia
            .eval("return pure() + input() + slot() + slot_write(1) + effect(3)")
            .unwrap();
        assert_eq!(f64::from_rustia(result).unwrap(), 21.0);
    }

    #[test]
    fn deterministic_facade_rejects_external_or_nondeterministic_helpers() {
        let mut external = Rustia::new(Profile::DeterministicSim);
        let Err(Error {
            kind: crate::ErrorKind::Vm(error),
            ..
        }) = external.add_external_state_fn("clock", || 1.0)
        else {
            panic!("expected deterministic profile to reject external-state host function");
        };
        assert!(matches!(error.kind, VmErrorKind::CapabilityRemoved { .. }));

        let mut nondeterministic = Rustia::new(Profile::DeterministicSim);
        let Err(Error {
            kind: crate::ErrorKind::Vm(error),
            ..
        }) = nondeterministic.add_nondeterministic_fn("random", || 1.0)
        else {
            panic!("expected deterministic profile to reject nondeterministic host function");
        };
        assert!(matches!(error.kind, VmErrorKind::CapabilityRemoved { .. }));
    }

    #[test]
    fn standard_facade_accepts_external_and_nondeterministic_helpers() {
        let mut rustia = Rustia::new(Profile::ReproducibleStandard);
        rustia.add_external_state_fn("clock", || 7.0).unwrap();
        rustia.add_nondeterministic_fn("random", || 11.0).unwrap();

        let result = rustia.eval("return clock() + random()").unwrap();
        assert_eq!(f64::from_rustia(result).unwrap(), 18.0);
    }

    #[test]
    fn typed_host_function_arg_errors_include_function_and_position() {
        let mut rustia = Rustia::new(Profile::ReproducibleStandard);
        rustia
            .add_fn("clamp", |x: f64, lo: f64, hi: f64| x.clamp(lo, hi))
            .unwrap();

        let wrong_type = rustia
            .eval::<OwnedValue>("return clamp('bad', 0, 10)")
            .unwrap_err();
        let wrong_type = wrong_type.to_string();
        assert!(
            wrong_type.contains("host function `clamp` argument 1: expected number, got string"),
            "{wrong_type}"
        );

        let missing_arg = rustia.eval::<OwnedValue>("return clamp(4, 0)").unwrap_err();
        let missing_arg = missing_arg.to_string();
        assert!(
            missing_arg.contains("host function `clamp` argument 3: expected number, got nil"),
            "{missing_arg}"
        );
    }

    #[test]
    fn typed_host_functions_can_return_rustia_result_errors() {
        let mut rustia = Rustia::new(Profile::ReproducibleStandard);
        rustia
            .add_fn("checked_div", |x: f64, y: f64| -> Result<f64> {
                if y == 0.0 {
                    return Err(Error::embed("division by zero".to_owned()));
                }
                Ok(x / y)
            })
            .unwrap();

        let value: f64 = rustia.eval("return checked_div(12, 3)").unwrap();
        assert_eq!(value, 4.0);

        let error = rustia
            .eval::<OwnedValue>("return checked_div(12, 0)")
            .unwrap_err()
            .to_string();
        assert!(
            error.contains("host function `checked_div` return value: division by zero"),
            "{error}"
        );
    }

    #[test]
    fn exports_facade_retains_script_functions() {
        let mut rustia = Rustia::new(Profile::ReproducibleStandard);
        let exports = rustia
            .load(
                "\
return {
    label = 'ai',
    decide = function(x)
        return x + 1
    end,
}
",
            )
            .install()
            .unwrap();
        assert_eq!(exports.get::<String>(&mut rustia, "label").unwrap(), "ai");

        let decide = exports.function(&mut rustia, "decide").unwrap();
        let result: f64 = decide.call(&mut rustia, (41.0,)).unwrap();
        assert_eq!(result, 42.0);
    }

    #[test]
    fn retained_script_functions_return_vec_and_option_shapes() {
        let mut rustia = Rustia::new(Profile::ReproducibleStandard);
        let exports = rustia
            .load(
                "\
return {
    values = function()
        return { 1, 2, 3 }
    end,
    maybe = function(enabled)
        if enabled then
            return 'scout'
        end
    end,
}
",
            )
            .install()
            .unwrap();

        let values = exports.function(&mut rustia, "values").unwrap();
        let values: Vec<f64> = values.call(&mut rustia, ()).unwrap();
        assert_eq!(values, vec![1.0, 2.0, 3.0]);

        let maybe = exports.function(&mut rustia, "maybe").unwrap();
        let found: Option<String> = maybe.call(&mut rustia, (true,)).unwrap();
        let missing: Option<String> = maybe.call(&mut rustia, (false,)).unwrap();
        assert_eq!(found.as_deref(), Some("scout"));
        assert_eq!(missing, None);
    }

    #[test]
    fn retained_script_functions_support_eight_arguments_and_results() {
        let mut rustia = Rustia::new(Profile::ReproducibleStandard);
        let exports = rustia
            .load(
                "\
return {
    rotate = function(a, b, c, d, e, f, g, h)
        return h, g, f, e, d, c, b, a
    end,
}
",
            )
            .install()
            .unwrap();

        let rotate = exports.function(&mut rustia, "rotate").unwrap();
        let result: (f64, f64, f64, f64, f64, f64, f64, f64) = rotate
            .call(&mut rustia, (1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0))
            .unwrap();
        assert_eq!(result, (8.0, 7.0, 6.0, 5.0, 4.0, 3.0, 2.0, 1.0));
    }

    #[test]
    fn builder_extension_installs_namespaced_deterministic_ops() {
        let extension = RustiaExtension::new("host")
            .fn_slot_read("bias", || 2.0)
            .fn_effect("emit", |value: f64| value + 1.0);
        let mut rustia = Rustia::builder()
            .profile(Profile::DeterministicSim)
            .extension(extension)
            .build()
            .unwrap();

        let result = rustia.eval("return host.bias() + host.emit(4)").unwrap();
        assert_eq!(f64::from_rustia(result).unwrap(), 7.0);
    }

    #[test]
    fn extension_reports_host_function_metadata() {
        let extension = RustiaExtension::new("host")
            .fn_pure("zero", || 1.0)
            .fn_pure("try_zero", || -> Result<f64> { Ok(1.0) })
            .fn_pure("split", |value: f64| (value, value > 0.0))
            .fn_with(
                "sum3",
                7,
                OpCapability::ReadsSlotState,
                |a: f64, b: f64, c: f64| a + b + c,
            );

        assert_eq!(extension.namespace(), Some("host"));
        assert_eq!(extension.fuel_policy(), RUSTIA_HOST_FUEL_POLICY);
        assert_eq!(
            extension.fuel_policy().abi_version,
            RUSTIA_HOST_FUEL_ABI_VERSION
        );
        assert_eq!(
            extension.fuel_policy().default_function_fuel,
            RUSTIA_DEFAULT_HOST_FUNCTION_FUEL
        );
        assert_eq!(
            extension.fuel_policy().minimum_function_fuel,
            RUSTIA_MIN_HOST_FUNCTION_FUEL
        );
        let functions = extension.functions().collect::<Vec<_>>();
        assert_eq!(
            functions,
            vec![
                RustiaHostFunctionInfo {
                    name: "zero",
                    fuel_abi_version: RUSTIA_HOST_FUEL_ABI_VERSION,
                    fuel_cost: 1,
                    capability: OpCapability::Pure,
                    parameter_count: 0,
                    parameter_types: &[],
                    return_types: &["f64"],
                    fallible: false,
                },
                RustiaHostFunctionInfo {
                    name: "try_zero",
                    fuel_abi_version: RUSTIA_HOST_FUEL_ABI_VERSION,
                    fuel_cost: 1,
                    capability: OpCapability::Pure,
                    parameter_count: 0,
                    parameter_types: &[],
                    return_types: &["f64"],
                    fallible: true,
                },
                RustiaHostFunctionInfo {
                    name: "split",
                    fuel_abi_version: RUSTIA_HOST_FUEL_ABI_VERSION,
                    fuel_cost: 1,
                    capability: OpCapability::Pure,
                    parameter_count: 1,
                    parameter_types: &["f64"],
                    return_types: &["f64", "bool"],
                    fallible: false,
                },
                RustiaHostFunctionInfo {
                    name: "sum3",
                    fuel_abi_version: RUSTIA_HOST_FUEL_ABI_VERSION,
                    fuel_cost: 7,
                    capability: OpCapability::ReadsSlotState,
                    parameter_count: 3,
                    parameter_types: &["f64", "f64", "f64"],
                    return_types: &["f64"],
                    fallible: false,
                },
            ]
        );
    }

    #[test]
    fn extension_rejects_zero_fuel_host_functions() {
        let extension = RustiaExtension::new("host").fn_with("free", 0, OpCapability::Pure, || 1.0);
        let result = Rustia::builder().extension(extension).build();
        let Err(Error {
            kind: crate::ErrorKind::Vm(error),
            ..
        }) = result
        else {
            panic!("expected zero-fuel host function to be rejected");
        };
        let VmErrorKind::InvalidProgram { reason } = error.kind else {
            panic!("expected invalid-program error, got {error:?}");
        };
        assert_eq!(reason, "host operation fuel cost must be non-zero");
    }

    #[test]
    fn deterministic_builder_rejects_nondeterministic_extension() {
        let extension = RustiaExtension::new("host").fn_nondeterministic("clock", || 1.0);
        let result = Rustia::builder()
            .profile(Profile::DeterministicSim)
            .extension(extension)
            .build();
        let Err(Error {
            kind: crate::ErrorKind::Vm(error),
            ..
        }) = result
        else {
            panic!("expected deterministic profile to reject nondeterministic extension");
        };
        assert!(matches!(error.kind, VmErrorKind::CapabilityRemoved { .. }));
    }

    #[test]
    fn deterministic_builder_rejects_external_state_extension() {
        let extension = RustiaExtension::new("host").fn_external_state("clock", || 1.0);
        let result = Rustia::builder()
            .profile(Profile::DeterministicSim)
            .extension(extension)
            .build();
        let Err(Error {
            kind: crate::ErrorKind::Vm(error),
            ..
        }) = result
        else {
            panic!("expected deterministic profile to reject external-state extension");
        };
        assert!(matches!(error.kind, VmErrorKind::CapabilityRemoved { .. }));
    }

    #[test]
    fn failed_replacement_install_keeps_old_function_callable() {
        let mut rustia = Rustia::new(Profile::ReproducibleStandard);
        let exports = rustia
            .load("return { decide = function() return 1 end }")
            .source_name("ai.luau")
            .install()
            .unwrap();
        let decide = exports.function(&mut rustia, "decide").unwrap();

        assert!(rustia
            .load("return {")
            .source_name("ai.luau")
            .install()
            .is_err());
        let result: f64 = decide.call(&mut rustia, ()).unwrap();
        assert_eq!(result, 1.0);
    }

    #[test]
    fn successful_replacement_install_keeps_both_versions_callable() {
        let mut rustia = Rustia::new(Profile::ReproducibleStandard);
        let first_exports = rustia
            .load("return { label = 'old', decide = function() return 1 end }")
            .source_name("ai.luau")
            .install()
            .unwrap();
        let first_decide = first_exports.function(&mut rustia, "decide").unwrap();

        let second_exports = rustia
            .load("return { label = 'new', decide = function() return 2 end }")
            .source_name("ai.luau")
            .install()
            .unwrap();
        let second_decide = second_exports.function(&mut rustia, "decide").unwrap();

        assert_eq!(
            first_exports.get::<String>(&mut rustia, "label").unwrap(),
            "old"
        );
        assert_eq!(
            second_exports.get::<String>(&mut rustia, "label").unwrap(),
            "new"
        );

        let old_result: f64 = first_decide.call(&mut rustia, ()).unwrap();
        let new_result: f64 = second_decide.call(&mut rustia, ()).unwrap();
        assert_eq!(old_result, 1.0);
        assert_eq!(new_result, 2.0);
    }

    #[test]
    fn retained_function_root_survives_after_exports_are_dropped() {
        let mut rustia = Rustia::new(Profile::ReproducibleStandard);
        let exports = rustia
            .load(
                "local state = { answer = 42 }; \
                 return { read = function() return state.answer end }",
            )
            .install()
            .unwrap();
        let read = exports.function(&mut rustia, "read").unwrap();
        drop(exports);

        let _: f64 = rustia.eval("collectgarbage('collect'); return 0").unwrap();
        let answer: f64 = read.call(&mut rustia, ()).unwrap();
        assert_eq!(answer, 42.0);
    }

    #[test]
    fn retained_function_root_survives_repeated_slot_reuse() {
        let mut rustia = Rustia::new(Profile::ReproducibleStandard);
        let exports = rustia
            .load(
                "local state = { answer = 42 }; \
                 return { read = function() return state.answer end }",
            )
            .install()
            .unwrap();
        let read = exports.function(&mut rustia, "read").unwrap();
        drop(exports);

        for _ in 0..32 {
            let _: f64 = rustia
                .eval(
                    "for i = 1, 256 do \
                         local state = { answer = i }; \
                         local function read() return state.answer end \
                         if read() ~= i then error('bad churn closure') end \
                     end \
                     collectgarbage('collect'); \
                     return 0",
                )
                .unwrap();
            let answer: f64 = read.call(&mut rustia, ()).unwrap();
            assert_eq!(answer, 42.0);
        }
    }

    #[test]
    fn retained_handles_reject_a_different_vm() {
        let mut first = Rustia::new(Profile::ReproducibleStandard);
        let exports = first
            .load("return { read = function() return 42 end }")
            .install()
            .unwrap();
        let read = exports.function(&mut first, "read").unwrap();
        let mut second = Rustia::new(Profile::ReproducibleStandard);

        let exports_error = exports.value(&mut second).unwrap_err();
        assert!(matches!(
            exports_error.as_vm_error().map(|error| &error.kind),
            Some(VmErrorKind::RetainedValueFromDifferentVm)
        ));
        let function_error = read.call::<_, f64>(&mut second, ()).unwrap_err();
        assert!(matches!(
            function_error.as_vm_error().map(|error| &error.kind),
            Some(VmErrorKind::RetainedValueFromDifferentVm)
        ));
    }

    #[test]
    fn named_compile_preserves_source_name_in_runtime_diagnostics() {
        let mut rustia = Rustia::new(Profile::ReproducibleStandard);
        let error = rustia
            .load("local t = {}; t[0 / 0] = 1")
            .source_name("bad_ai.luau")
            .install()
            .unwrap_err();
        assert!(error.to_string().contains("bad_ai.luau:1:"), "{error}");
    }

    #[test]
    fn chunk_builder_preserves_source_name_in_runtime_diagnostics() {
        let mut rustia = Rustia::new(Profile::ReproducibleStandard);

        let answer: f64 = rustia
            .load("return 40 + 2")
            .source_name("inline.luau")
            .eval()
            .unwrap();
        assert_eq!(answer, 42.0);

        let owned = rustia
            .load("return 'ok'")
            .source_name("owned.luau")
            .eval()
            .unwrap();
        assert_eq!(String::from_rustia(owned).unwrap(), "ok");

        let exports = rustia
            .load("return { answer = 42 }")
            .source_name("exports.luau")
            .install()
            .unwrap();
        assert_eq!(exports.get::<i32>(&mut rustia, "answer").unwrap(), 42);

        let error = rustia
            .load("local t = {}; t[0 / 0] = 1")
            .source_name("bad_inline.luau")
            .eval::<f64>()
            .unwrap_err();
        assert!(error.to_string().contains("bad_inline.luau:1:"), "{error}");
    }

    #[test]
    fn public_errors_expose_source_span_and_runtime_location() {
        let Err(error) = Rustia::compile_module("compile-diag.luau", "break") else {
            panic!("break outside loop should fail compilation");
        };
        assert_eq!(error.source_span(), Some(SourceSpan { start: 0, end: 5 }));
        assert_eq!(
            error.source_location(),
            Some(crate::SourceLocation {
                source: "compile-diag.luau",
                line: 1
            })
        );

        let mut rustia = Rustia::new(Profile::ReproducibleStandard);
        let Err(error) = rustia
            .load(
                "\
local t = {}
t[0 / 0] = 1
",
            )
            .source_name("runtime-diag.luau")
            .eval::<OwnedValue>()
        else {
            panic!("NaN table key should fail at runtime");
        };
        let location = error
            .source_location()
            .expect("runtime error should carry source location");
        assert_eq!(location.source, "runtime-diag.luau");
        assert_eq!(location.line, 2);
        assert!(error.source_span().is_some());
    }

    #[derive(Debug, PartialEq, Deserialize, Serialize)]
    struct ScenarioSpec {
        name: String,
        difficulty: u32,
        enemies: Vec<EnemySpec>,
    }

    #[derive(Debug, PartialEq, Deserialize, Serialize)]
    struct EnemySpec {
        kind: String,
        hp: u32,
    }

    #[test]
    fn serde_deserializes_exports_and_inserts_globals() {
        let mut rustia = Rustia::new(Profile::ReproducibleStandard);
        let exports = rustia
            .load(
                "\
return {
    name = 'arena',
    difficulty = 3,
    enemies = {
        { kind = 'scout', hp = 5 },
        { kind = 'brute', hp = 9 },
    },
}
",
            )
            .install()
            .unwrap();
        let spec: ScenarioSpec = exports.deserialize(&mut rustia).unwrap();
        assert_eq!(
            spec,
            ScenarioSpec {
                name: "arena".to_owned(),
                difficulty: 3,
                enemies: vec![
                    EnemySpec {
                        kind: "scout".to_owned(),
                        hp: 5
                    },
                    EnemySpec {
                        kind: "brute".to_owned(),
                        hp: 9
                    },
                ],
            }
        );
        let second_spec: ScenarioSpec = exports.deserialize(&mut rustia).unwrap();
        assert_eq!(second_spec, spec);

        rustia.insert_serde("config", &spec).unwrap();
        let value = rustia
            .eval("return config.enemies[2].kind .. ':' .. tostring(config.difficulty)")
            .unwrap();
        assert_eq!(String::from_rustia(value).unwrap(), "brute:3");
    }

    #[test]
    fn rustia_extension_macro_builds_capability_tagged_extension() {
        let extension = crate::rustia_extension!("host" {
            pure(fuel = 2) fn fixed_mul(a: i32, b: i32) -> i32 {
                a * b
            }

            effect fn emit(value: f64) -> f64 {
                value + 10.0
            }
        });
        let mut rustia = Rustia::builder()
            .profile(Profile::DeterministicSim)
            .extension(extension)
            .build()
            .unwrap();

        let value = rustia
            .eval("return host.fixed_mul(6, 7) + host.emit(1)")
            .unwrap();
        assert_eq!(f64::from_rustia(value).unwrap(), 53.0);
    }

    #[test]
    fn debug_traceback_for_errored_coroutine_reports_top_frame() {
        let source = "\
function halp(key, value)
    local t = {}
    t[key] = value
    return t
end

local co = coroutine.create(halp)
local ok, err = coroutine.resume(co, 0 / 0, 42)
return tostring(ok) .. \"|\" .. tostring(err) .. \"|\" .. debug.traceback(co)
";
        let chunk = parse_source(source).unwrap();
        let program = compile_with_options(
            &chunk,
            CompileOptions::default().with_source("debug.luau", source),
        )
        .unwrap();
        let mut vm = Vm::new(Profile::ReproducibleStandard.to_core());
        let result = vm.run(&program).unwrap();
        let traceback = vm.string_value(result).unwrap().unwrap();
        assert_eq!(
            traceback,
            "false|debug.luau:3: table index is NaN|debug.luau:3 function halp\n"
        );
    }

    #[test]
    fn debug_info_line_defined_precedes_active_line() {
        let source = "\
function testlinedefined()
    local line = debug.info(1, \"l\")
    local linedefined = debug.info(testlinedefined, \"l\")
    return tostring(line) .. \"|\" .. tostring(linedefined)
end

return testlinedefined()
";
        let chunk = parse_source(source).unwrap();
        let program = compile_with_options(
            &chunk,
            CompileOptions::default().with_source("debug.luau", source),
        )
        .unwrap();
        let mut vm = Vm::new(Profile::ReproducibleStandard.to_core());
        let result = vm.run(&program).unwrap();
        let lines = vm.string_value(result).unwrap().unwrap();
        assert_eq!(lines, "2|1");
    }

    #[test]
    fn conformance_int64_userdata_supports_metamethods_and_value_field() {
        let settings = VmSettings {
            enable_conformance_int64_userdata: true,
            ..VmSettings::default()
        };
        let mut rustia = Rustia::with_settings(Profile::ReproducibleStandard, settings);
        let result = rustia
            .eval(
                "\
local v = int64(42)
v.value = 4
return tostring(v + 2) .. '|' .. tostring(v == int64(4)) .. '|' .. tostring(int64(-5) // 2)
",
            )
            .unwrap();
        assert_eq!(String::from_rustia(result).unwrap(), "6|true|-3");
    }

    #[test]
    fn conformance_native_userdata_supports_vec2_and_vertex_surface() {
        let settings = VmSettings {
            enable_conformance_native_userdata: true,
            ..VmSettings::default()
        };
        let mut rustia = Rustia::with_settings(Profile::ReproducibleStandard, settings);
        let result = rustia
            .eval(
                "\
local s = vec2(5, 4)
local t = vec2(1, 2)
local u = vec2(10, 20)
local x = -u + s * t
local m = u:Min(s)
local v = vertex(vector.create(1, 2, 3), vector.create(0.5, 0, 0.5), vec2(0.25, 0.5))
return x.X == -5 and x.Y == -12 and s:Dot(t) == 13 and m.X == 5 and m.Y == 4 and v.uv.X == 0.25 and v.normal.Z == 0.5
",
            )
            .unwrap();
        assert!(bool::from_rustia(result).unwrap());
    }

    #[test]
    fn conformance_native_type_checks_validate_annotated_parameters() {
        let settings = VmSettings {
            enable_conformance_native_type_checks: true,
            ..VmSettings::default()
        };
        let mut rustia = Rustia::with_settings(Profile::ReproducibleStandard, settings);
        let result = rustia
            .eval(
                "\
local function add(a: number, b: number)
    assert(is_native())
    return a + b
end
local function checkbuffer(a: buffer?) return a == nil or type(a) == 'buffer' end
local ok = pcall(add, nil, 2)
return add(1, 3) == 4 and not ok and checkbuffer(nil) and checkbuffer(buffer.create(1))
",
            )
            .unwrap();
        assert!(bool::from_rustia(result).unwrap());
    }

    #[test]
    fn coroutine_resume_arguments_return_from_yield() {
        let mut rustia = Rustia::new(Profile::ReproducibleStandard);
        let result = rustia
            .eval(
                "\
local co
local seen
local function body(a, ...)
    local arg = {...}
    for i = 1, #arg do
        seen = { coroutine.yield(table.unpack(arg[i])) }
    end
    return table.unpack(a)
end
co = coroutine.create(body)
local s, a, b, c, d = coroutine.resume(co, {1, 2, 3}, {}, {1}, {'a', 'b', 'c'})
local ok1 = s and a == nil and coroutine.status(co) == 'suspended'
s, a, b, c, d = coroutine.resume(co, 1, 2, 3)
local ok2 = s and #seen == 3 and seen[1] == 1 and seen[2] == 2 and seen[3] == 3
    and a == 1 and b == nil
s, a, b, c, d = coroutine.resume(co, 'done')
local ok3 = s and #seen == 1 and seen[1] == 'done'
    and a == 'a' and b == 'b' and c == 'c' and d == nil
s, a, b, c, d = coroutine.resume(co)
local ok4 = s and a == 1 and b == 2 and c == 3 and d == nil
    and coroutine.status(co) == 'dead'
return ok1 and ok2 and ok3 and ok4
",
            )
            .unwrap();
        assert!(bool::from_rustia(result).unwrap());
    }

    #[test]
    fn coroutine_wrap_resumes_yield_call_results() {
        let mut rustia = Rustia::new(Profile::ReproducibleStandard);
        let result = rustia
            .eval(
                "\
local f = coroutine.wrap(function()
    local a, b = coroutine.yield('yielded')
    return a, b
end)
local first = f()
local second, third = f('resume', 42)
return first == 'yielded' and second == 'resume' and third == 42
",
            )
            .unwrap();
        assert!(bool::from_rustia(result).unwrap());
    }

    #[test]
    fn pcall_can_yield_and_resume_successfully() {
        let mut rustia = Rustia::new(Profile::ReproducibleStandard);
        let result = rustia
            .eval(
                "\
local co = coroutine.create(function()
    return pcall(function()
        coroutine.yield('pause')
        return 1, 2, 3
    end)
end)
local ok, value = coroutine.resume(co)
local first = ok and value == 'pause' and coroutine.status(co) == 'suspended'
local done, protected, a, b, c = coroutine.resume(co)
return first and done and protected and a == 1 and b == 2 and c == 3
",
            )
            .unwrap();
        assert!(bool::from_rustia(result).unwrap());
    }

    #[test]
    fn nested_pcall_preserves_protected_results_after_yield() {
        let mut rustia = Rustia::new(Profile::ReproducibleStandard);
        let result = rustia
            .eval(
                "\
local co = coroutine.create(function()
    return pcall(function()
        coroutine.yield(1)
        return pcall(function()
            coroutine.yield(2)
            return 3
        end)
    end)
end)
local ok, a = coroutine.resume(co)
local first = ok and a == 1
ok, a = coroutine.resume(co)
local second = ok and a == 2
local done, outer, inner, value = coroutine.resume(co)
return first and second and done and outer and inner and value == 3
",
            )
            .unwrap();
        assert!(bool::from_rustia(result).unwrap());
    }

    #[test]
    fn pcall_catches_error_after_yield() {
        let mut rustia = Rustia::new(Profile::ReproducibleStandard);
        let result = rustia
            .eval(
                "\
local co = coroutine.create(function()
    return pcall(function()
        coroutine.yield()
        error('foo')
    end)
end)
local ok = coroutine.resume(co)
local done, protected, err = coroutine.resume(co)
return ok and done and not protected and string.find(err, 'foo', 1, true) ~= nil
",
            )
            .unwrap();
        assert!(bool::from_rustia(result).unwrap());
    }

    #[test]
    fn deterministic_builder_captures_print_through_the_stdout_sink() {
        let captured = Arc::new(Mutex::new(String::new()));
        let sink = Arc::clone(&captured);
        let mut rustia = RustiaBuilder::deterministic()
            .stdout(move |text| sink.lock().unwrap().push_str(text))
            .build()
            .unwrap();

        rustia.exec("print('hello', 42)").unwrap();
        assert_eq!(captured.lock().unwrap().as_str(), "hello\t42\n");
    }

    #[test]
    fn execution_profiles_have_stable_unique_encodings() {
        let profiles = [
            (
                RustiaExecutionProfile::new(Profile::DeterministicSim, GuestSurface::HardenedLeaf),
                1,
                [1, 0, 0, 0, 3, 0, 0, 0, 2, 0, 0, 0],
            ),
            (
                RustiaExecutionProfile::new(Profile::LuauCompat, GuestSurface::Standard),
                2,
                [1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0],
            ),
            (
                RustiaExecutionProfile::new(Profile::ReproducibleStandard, GuestSurface::Standard),
                3,
                [1, 0, 0, 0, 2, 0, 0, 0, 1, 0, 0, 0],
            ),
            (
                RustiaExecutionProfile::new(Profile::DeterministicSim, GuestSurface::Standard),
                4,
                [1, 0, 0, 0, 3, 0, 0, 0, 1, 0, 0, 0],
            ),
            (
                RustiaExecutionProfile::new(Profile::LuauCompat, GuestSurface::HardenedLeaf),
                5,
                [1, 0, 0, 0, 1, 0, 0, 0, 2, 0, 0, 0],
            ),
            (
                RustiaExecutionProfile::new(
                    Profile::ReproducibleStandard,
                    GuestSurface::HardenedLeaf,
                ),
                6,
                [1, 0, 0, 0, 2, 0, 0, 0, 2, 0, 0, 0],
            ),
            (
                RustiaExecutionProfile::new(Profile::Puc51Compat, GuestSurface::Standard),
                7,
                [1, 0, 0, 0, 4, 0, 0, 0, 1, 0, 0, 0],
            ),
            (
                RustiaExecutionProfile::new(Profile::Puc51Compat, GuestSurface::HardenedLeaf),
                8,
                [1, 0, 0, 0, 4, 0, 0, 0, 2, 0, 0, 0],
            ),
        ];
        for (index, (profile, compatibility_id, identity_bytes)) in profiles.iter().enumerate() {
            assert_eq!(profile.compatibility_id(), *compatibility_id);
            assert_eq!(profile.identity_bytes(), *identity_bytes);
            for (other_profile, _, _) in &profiles[index + 1..] {
                assert_ne!(profile.compatibility_id(), other_profile.compatibility_id());
                assert_ne!(profile.identity_bytes(), other_profile.identity_bytes());
            }
        }
    }

    #[test]
    fn hardened_execution_profile_transfers_to_the_live_vm() {
        let expected = RustiaExecutionProfile::DETERMINISTIC_HARDENED_LEAF;

        let builder = RustiaBuilder::deterministic_leaf().memory_limit_bytes(512 * 1024);
        assert_eq!(builder.execution_profile(), expected);
        assert_eq!(builder.vm_settings().memory_limit_bytes, Some(512 * 1024));
        assert!(!builder.vm_settings().enable_memory_tracking);
        let rustia = builder.build_for_host::<()>().unwrap();
        assert_eq!(rustia.execution_profile(), expected);
        assert_eq!(rustia.vm_settings().memory_limit_bytes, Some(512 * 1024));
        assert!(!rustia.vm_settings().enable_memory_tracking);

        assert_eq!(
            RustiaBuilder::deterministic().execution_profile(),
            RustiaExecutionProfile::DETERMINISTIC_STANDARD
        );
        assert_ne!(
            RustiaExecutionProfile::DETERMINISTIC_STANDARD.identity_bytes(),
            expected.identity_bytes()
        );

        assert_eq!(
            RustiaBuilder::standard().execution_profile(),
            RustiaBuilder::live().execution_profile(),
            "services and dynamic-code settings are identity inputs owned by the embedder"
        );

        let normalized = Rustia::with_settings(
            Profile::ReproducibleStandard,
            VmSettings {
                guest_surface: GuestSurface::HardenedLeaf.to_core(),
                memory_limit_bytes: Some(512 * 1024),
                ..VmSettings::default()
            },
        );
        assert_eq!(
            normalized.execution_profile(),
            RustiaExecutionProfile::DETERMINISTIC_HARDENED_LEAF,
            "live attestation must report the VM-normalized profile, not requested metadata"
        );
    }

    #[test]
    fn deterministic_builder_admits_deterministic_clock_providers() {
        let mut rustia = RustiaBuilder::deterministic()
            .wall_clock(ServiceClock::fixed(1.5))
            .build()
            .unwrap();

        let time: f64 = rustia.eval("return os.time()").unwrap();
        assert_eq!(time, 1.5);
        let surface: bool = rustia
            .eval(
                "\
return type(os) == 'table'
    and type(os.time) == 'function'
    and type(os.clock) == 'function'
    and type(os.date) == 'function'
    and type(os.difftime) == 'function'
",
            )
            .unwrap();
        assert!(surface);
    }

    #[cfg(not(target_arch = "wasm32"))]
    #[test]
    fn deterministic_builder_rejects_system_service_clocks() {
        let result = RustiaBuilder::deterministic()
            .wall_clock(ServiceClock::system())
            .build();
        let Err(Error {
            kind: crate::ErrorKind::Vm(error),
            ..
        }) = result
        else {
            panic!("expected deterministic profile to reject a nondeterministic wall clock");
        };
        assert!(matches!(error.kind, VmErrorKind::CapabilityRemoved { .. }));

        let result = RustiaBuilder::deterministic()
            .monotonic_clock(ServiceClock::monotonic())
            .build();
        let Err(Error {
            kind: crate::ErrorKind::Vm(error),
            ..
        }) = result
        else {
            panic!("expected deterministic profile to reject a nondeterministic monotonic clock");
        };
        assert!(matches!(error.kind, VmErrorKind::CapabilityRemoved { .. }));

        let result = RustiaBuilder::deterministic()
            .entropy(ServiceEntropy::system())
            .build();
        let Err(Error {
            kind: crate::ErrorKind::Vm(error),
            ..
        }) = result
        else {
            panic!("expected deterministic profile to reject nondeterministic entropy");
        };
        assert!(matches!(error.kind, VmErrorKind::CapabilityRemoved { .. }));
    }

    #[test]
    fn fixed_entropy_is_reproducible_and_changes_the_initial_random_sequence() {
        let sequence = |seed| {
            let mut rustia = RustiaBuilder::standard()
                .entropy(ServiceEntropy::fixed(seed))
                .build()
                .unwrap();
            [
                rustia.eval::<f64>("return math.random()").unwrap(),
                rustia.eval::<f64>("return math.random()").unwrap(),
            ]
        };

        assert_eq!(sequence(42), sequence(42));
        assert_ne!(sequence(42), sequence(43));
        assert_eq!(sequence(0), {
            let mut rustia = RustiaBuilder::standard().build().unwrap();
            [
                rustia.eval::<f64>("return math.random()").unwrap(),
                rustia.eval::<f64>("return math.random()").unwrap(),
            ]
        });
    }

    #[test]
    fn standard_preset_keeps_the_pinned_epoch_and_logical_clock_by_default() {
        let mut rustia = Rustia::builder().build().unwrap();

        let time: f64 = rustia.eval("return os.time()").unwrap();
        assert_eq!(time, 1_700_000_000.0);
        let logical: bool = rustia
            .eval(
                "\
local first = os.clock()
local second = os.clock()
return first >= 0 and second >= first
",
            )
            .unwrap();
        assert!(logical);
    }

    #[test]
    fn decision_reset_invalidates_every_retained_facade_handle() {
        assert_eq!(RUSTIA_DECISION_RESET_ABI_VERSION, 1);
        let module = Rustia::compile_module(
            "decision.luau",
            "return { answer = 42, decide = function(x) return x + 1 end }",
        )
        .unwrap();
        let mut rustia = RustiaBuilder::deterministic().build().unwrap();
        let exports = rustia.install(&module).unwrap();
        let decide = exports.function(&mut rustia, "decide").unwrap();
        let exports_clone = exports.clone();
        let decide_clone = decide.clone();

        assert_eq!(exports.decision_epoch(), 0);
        assert_eq!(decide.decision_epoch(), 0);
        assert_eq!(decide.call::<_, f64>(&mut rustia, (4.0,)).unwrap(), 5.0);

        rustia.reset_decision().unwrap();
        assert_eq!(rustia.decision_epoch(), 1);
        for result in [
            exports.get::<f64>(&mut rustia, "answer").map(|_| ()),
            exports_clone.get::<f64>(&mut rustia, "answer").map(|_| ()),
            decide.call::<_, f64>(&mut rustia, ()).map(|_| ()),
            decide_clone.call::<_, f64>(&mut rustia, ()).map(|_| ()),
        ] {
            let error = result.expect_err("pre-reset handle must be stale");
            assert!(
                error.to_string().contains("decision epoch 0"),
                "unexpected stale-handle error: {error}"
            );
        }

        let fresh = rustia.install(&module).unwrap();
        let fresh_decide = fresh.function(&mut rustia, "decide").unwrap();
        assert_eq!(fresh.decision_epoch(), 1);
        assert_eq!(
            fresh_decide.call::<_, f64>(&mut rustia, (9.0,)).unwrap(),
            10.0
        );
    }

    #[test]
    fn decision_reset_discards_guest_state_and_restores_host_configuration_and_rng() {
        let settings = VmSettings {
            deterministic_gc_interval: NonZeroU64::new(8).unwrap(),
            enable_memory_tracking: true,
            ..VmSettings::default()
        };
        let extension = RustiaExtension::new("host").fn_pure("bias", || -> f64 { 7.0 });
        let mut rustia = RustiaBuilder::deterministic()
            .raw_settings(settings)
            .wall_clock(ServiceClock::fixed(1_234.0))
            .extension(extension)
            .build()
            .unwrap();
        rustia
            .add_pure_fn("double", |value: f64| value * 2.0)
            .unwrap();
        rustia.insert_global("input", 5.0).unwrap();

        let configured: bool = rustia
            .eval(
                "\
decision_global = (decision_global or 0) + 1
return input == 5 and decision_global == 1
    and host.bias() == 7 and double(3) == 6 and os.time() == 1234
",
            )
            .unwrap();
        assert!(configured);
        let first_random: Vec<f64> = rustia
            .eval("return { math.random(), math.random(), math.random() }")
            .unwrap();
        assert!(rustia.vm().deterministic_collection_count() > 0);
        let peak_before_reset = rustia.memory_stats().peak_live_bytes().unwrap();

        rustia.reset_decision().unwrap();
        assert_eq!(rustia.vm().deterministic_collection_count(), 0);
        assert_eq!(
            rustia.memory_stats().peak_live_bytes(),
            Some(peak_before_reset),
            "lifetime peak telemetry must survive a decision reset"
        );
        let clean_and_configured: bool = rustia
            .eval(
                "\
return input == nil and decision_global == nil
    and host.bias() == 7 and double(3) == 6 and os.time() == 1234
",
            )
            .unwrap();
        assert!(clean_and_configured);
        let second_random: Vec<f64> = rustia
            .eval("return { math.random(), math.random(), math.random() }")
            .unwrap();
        assert_eq!(first_random, second_random);
        assert!(rustia.vm().deterministic_collection_count() > 0);
    }

    #[test]
    fn decision_reset_reaches_a_many_decision_memory_plateau() {
        let module = Rustia::compile_module(
            "plateau.luau",
            "\
decision_global = {}
for i = 1, 64 do
    decision_global[i] = { value = i, label = 'temporary-' .. i }
end
local thread = coroutine.create(function()
    coroutine.yield(decision_global)
end)
coroutine.resume(thread)
return { count = #decision_global }
",
        )
        .unwrap();
        let mut rustia = RustiaBuilder::deterministic().build().unwrap();
        let mut plateau = None;

        for decision in 0..600 {
            let exports = rustia.install(&module).unwrap();
            assert_eq!(exports.get::<usize>(&mut rustia, "count").unwrap(), 64);
            rustia.reset_decision().unwrap();
            let stats = rustia.memory_stats();
            if decision == 49 {
                plateau = Some(stats);
            } else if decision > 49 {
                assert_eq!(
                    Some(stats),
                    plateau,
                    "VM memory changed after the warm plateau at decision {decision}"
                );
            }
        }
        assert_eq!(rustia.decision_epoch(), 600);
        let clean: bool = rustia.eval("return decision_global == nil").unwrap();
        assert!(clean);
    }

    #[cfg(not(target_arch = "wasm32"))]
    #[test]
    fn live_preset_installs_system_clocks_and_the_stock_loadstring_compiler() {
        let mut rustia = RustiaBuilder::live().build().unwrap();

        let time: f64 = rustia.eval("return os.time()").unwrap();
        assert!(
            time >= 1_700_000_000.0,
            "live() os.time() should read the real wall clock, got {time}"
        );
        let clock: f64 = rustia.eval("return os.clock()").unwrap();
        assert!(
            clock > 0.0,
            "live() os.clock() should read the real monotonic clock, got {clock}"
        );
        let answer: f64 = rustia.eval("return loadstring('return 40 + 2')()").unwrap();
        assert_eq!(answer, 42.0);
        // print goes to real process stdout via `print!`, which a test cannot read back from
        // inside the process; sink delivery is covered by the VM-level capture tests.
    }
}