luars 0.17.0

A library for lua 5.5 runtime implementation in Rust
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
4027
4028
4029
4030
4031
4032
4033
4034
4035
4036
4037
4038
4039
4040
4041
4042
4043
4044
4045
4046
4047
4048
4049
4050
4051
4052
4053
4054
4055
4056
4057
4058
4059
4060
4061
4062
4063
4064
4065
4066
4067
4068
4069
4070
4071
4072
4073
4074
4075
4076
4077
4078
4079
4080
4081
4082
4083
4084
4085
4086
4087
4088
4089
4090
4091
4092
4093
4094
4095
4096
4097
4098
4099
4100
4101
4102
4103
4104
4105
4106
4107
4108
4109
4110
4111
4112
4113
4114
4115
4116
4117
4118
4119
4120
4121
4122
4123
4124
4125
4126
4127
4128
4129
4130
4131
4132
4133
4134
4135
4136
4137
4138
4139
4140
4141
4142
4143
4144
4145
4146
4147
4148
4149
4150
4151
4152
4153
4154
4155
4156
4157
4158
4159
4160
4161
4162
4163
4164
4165
4166
4167
4168
4169
4170
4171
4172
4173
4174
4175
4176
4177
4178
4179
4180
4181
4182
4183
4184
4185
4186
4187
4188
4189
4190
4191
4192
4193
4194
4195
4196
4197
4198
4199
4200
4201
4202
4203
4204
4205
4206
4207
4208
4209
4210
4211
4212
4213
4214
4215
4216
4217
4218
4219
4220
4221
4222
4223
4224
4225
4226
4227
4228
4229
4230
4231
4232
4233
4234
4235
4236
4237
4238
4239
4240
4241
4242
4243
4244
4245
4246
4247
4248
4249
4250
4251
4252
4253
4254
4255
4256
4257
4258
4259
4260
4261
4262
4263
4264
4265
4266
4267
4268
4269
4270
4271
4272
4273
4274
4275
4276
4277
4278
4279
4280
4281
4282
4283
4284
4285
4286
4287
4288
4289
4290
4291
4292
4293
4294
4295
4296
4297
4298
4299
4300
4301
4302
4303
4304
4305
4306
4307
4308
4309
4310
4311
4312
4313
4314
4315
4316
4317
4318
4319
4320
4321
4322
4323
4324
4325
4326
4327
4328
4329
4330
4331
4332
4333
4334
4335
4336
4337
4338
4339
4340
4341
4342
4343
4344
4345
4346
4347
4348
4349
4350
4351
4352
4353
4354
4355
4356
4357
4358
4359
4360
4361
4362
4363
4364
4365
4366
4367
4368
4369
4370
4371
4372
4373
4374
4375
4376
4377
4378
4379
4380
4381
4382
4383
4384
4385
4386
4387
4388
4389
4390
4391
4392
4393
4394
4395
4396
4397
4398
4399
4400
4401
4402
4403
4404
4405
4406
4407
4408
4409
4410
4411
4412
4413
4414
4415
4416
4417
4418
4419
4420
4421
4422
4423
4424
4425
4426
4427
4428
4429
4430
4431
4432
4433
4434
4435
4436
4437
4438
4439
4440
4441
4442
4443
4444
4445
4446
4447
4448
4449
4450
4451
4452
// Lua execution state (equivalent to lua_State in Lua C API)
// Represents a single thread/coroutine execution context
// Multiple LuaStates can share the same LuaVM (global_State)

use std::future::Future;
use std::pin::Pin;

use crate::lua_value::userdata_trait::UserDataTrait;
use crate::lua_value::{LuaUserdata, LuaValue, LuaValueKind, LuaValuePtr, UpvalueStore};
use crate::lua_vm::call_info::call_status::{self, CIST_C, CIST_RECST, CIST_XPCALL, CIST_YPCALL};
use crate::lua_vm::execute::call::{call_c_function, resolve_call_chain};
use crate::lua_vm::execute::{self, lua_execute};
use crate::lua_vm::lua_limits::{BASIC_STACK_SIZE, CSTACKERR, EXTRA_STACK, LUAI_MAXCSTACK};
use crate::lua_vm::safe_option::{LuaSafeState, SafeOption};
#[cfg(feature = "sandbox")]
use crate::lua_vm::sandbox::{SANDBOX_TIMEOUT_CHECK_INTERVAL, SandboxConfig, SandboxRuntimeLimits};
use crate::lua_vm::{CallInfo, LuaError, LuaResult, TmKind, get_metamethod_event};
#[cfg(feature = "sandbox")]
use crate::platform_time::unix_nanos;
use crate::{
    AsyncReturnValue, CreateResult, GcObjectPtr, LuaAnyRef, LuaFunctionRef, LuaRegistrable,
    LuaTableRef, LuaVM, ProtoPtr, StringPtr, TablePtr, ThreadPtr, UpvaluePtr,
};

/// Execution state for a Lua thread/coroutine
/// This is separate from LuaVM (global_State) to support multiple execution contexts
pub struct LuaState {
    vm: *mut LuaVM,

    thread: ThreadPtr,
    /// Data stack - stores all values (registers, temporaries, function arguments)
    /// Layout: [frame0_values...][frame1_values...][frame2_values...]
    /// Similar to Lua's TValue stack[] in lua_State
    /// IMPORTANT: This is the PHYSICAL stack, only grows, never shrinks
    pub(crate) stack: Vec<LuaValue>,

    /// Logical stack top - index of first free slot (Lua's L->top.p)
    /// This is the actual "top" that controls which stack slots are active
    /// Values above stack_top are considered "garbage" and can be reused
    pub(crate) stack_top: usize,

    /// Call stack - one CallInfo per active function call
    /// Grows dynamically on demand (like Lua 5.4's linked list approach)
    /// Similar to Lua's CallInfo *ci in lua_State
    #[allow(clippy::vec_box)]
    pub(crate) call_stack: Vec<Box<CallInfo>>,

    /// Current call depth (index into call_stack)
    /// This is the actual depth, NOT call_stack.len()
    /// Implements Lua's optimization: never shrink call_stack, only move this index
    call_depth: usize,

    /// Open upvalues - upvalues pointing to stack locations
    /// Sorted Vec (higher stack indices first) for efficient lookup and close traversal.
    /// Linear scan is faster than HashMap for typical 0-5 open upvalues due to
    /// no hashing overhead and better cache locality. Matches C Lua's sorted list design.
    open_upvalues_list: Vec<UpvaluePtr>,

    /// Error message storage (lightweight error handling)
    pub(crate) error_msg: String,

    /// Error object storage (preserves actual error value for pcall)
    pub(crate) error_object: LuaValue,

    /// Yield values storage (for coroutine yield)
    yield_values: Vec<LuaValue>,

    /// Whether hook re-entry is allowed. Set to false while a hook is running
    /// to prevent recursive hook invocations within the same thread.
    pub(crate) allow_hook: bool,

    /// Hook function (nil = no hook). Per-thread, set via debug.sethook.
    pub(crate) hook: LuaValue,

    /// Active hook mask (bitmask of LUA_MASKCALL/RET/LINE/COUNT). Per-thread.
    pub(crate) hook_mask: u8,

    /// Base value for count hook (reset interval). Per-thread.
    pub(crate) base_hook_count: i32,

    /// Current countdown for count hook (per-thread, counts instructions independently).
    pub(crate) hook_count: i32,

    /// Last instruction PC (0-based index) seen by the line hook (like C Lua's L->oldpc).
    /// Stored per-thread so the main execution loop doesn't waste a register on it.
    /// Used by hook_check_instruction to detect line changes via changedline logic.
    /// Also set by rethook/return paths to the caller's current PC.
    pub(crate) oldpc: u32,

    /// Transfer info for call/return hooks (like C Lua's L->transferinfo).
    /// ftransfer: 1-based index of first transferred value (relative to func position).
    /// ntransfer: number of values being transferred.
    /// Set before calling hooks, read by debug.getinfo with 'r' option.
    pub(crate) ftransfer: i32,
    pub(crate) ntransfer: i32,

    safe_state: LuaSafeState,

    is_main: bool,

    /// To-be-closed variable list - stack indices of variables marked with <close>
    /// Maintained in order: most recently added TBC variable is last
    /// When leaving a block (OpCode::Close), we iterate from the end and call __close
    /// on each TBC variable whose stack index >= the close level
    pub(crate) tbc_list: Vec<usize>,

    /// Whether this coroutine has yielded and is waiting to be resumed.
    /// Used to distinguish "running" from "yielded" when call_stack is non-empty.
    /// Set to true when yield is captured by resume, false when execution resumes.
    yielded: bool,

    /// Whether this coroutine is dead (finished with error).
    /// Unlike normal completion (stack cleared), dead-by-error coroutines
    /// retain their stack and call frames for debug.traceback inspection,
    /// matching C Lua's behavior.
    pub(crate) dead: bool,

    /// Whether close_tbc_with_error is currently running on this thread.
    /// Used to detect re-entrant coroutine.close() calls from __close handlers.
    pub(crate) is_closing: bool,

    /// Non-yieldable nesting depth (like C Lua's nny packed in nCcalls).
    /// Main thread starts at 1 (always non-yieldable).
    /// Coroutine threads start at 0 (yieldable).
    /// Incremented when entering a non-yieldable C call boundary (e.g., pcall method
    /// used by C stdlib functions like gsub that don't support continuations).
    /// `yieldable(L)` == `nny == 0`.
    pub(crate) nny: u32,

    /// Pending async future — set by async CFunction wrappers before yielding.
    /// When an async function is called from Lua, it creates the Future and stores
    /// it here, then yields with ASYNC_SENTINEL. The AsyncThread polls this future
    /// and resumes the coroutine when it completes.
    /// `Option<Pin<Box<...>>>` is null-pointer-optimized: zero overhead when None.
    pub(crate) pending_future:
        Option<Pin<Box<dyn Future<Output = LuaResult<Vec<AsyncReturnValue>>>>>>,

    #[cfg(feature = "sandbox")]
    pub(crate) sandbox_limits: Option<SandboxRuntimeLimits>,
}

impl LuaState {
    /// Create a new execution state
    pub fn new(
        call_stack_size: usize,
        vm: *mut LuaVM,
        is_main: bool,
        safe_option: SafeOption,
    ) -> Self {
        Self {
            vm,
            stack: Vec::with_capacity(BASIC_STACK_SIZE),
            thread: ThreadPtr::null(),
            stack_top: 0, // Start with empty stack (Lua's L->top.p = L->stack)
            call_stack: Vec::with_capacity(call_stack_size),
            call_depth: 0,
            open_upvalues_list: Vec::new(),
            error_msg: String::new(),
            error_object: LuaValue::nil(),
            yield_values: Vec::new(),
            allow_hook: true,
            hook: LuaValue::nil(),
            hook_mask: 0,
            base_hook_count: 0,
            hook_count: 0,
            oldpc: 0,
            ftransfer: 0,
            ntransfer: 0,
            safe_state: safe_option.into(),
            is_main,
            tbc_list: Vec::new(),
            yielded: false,
            dead: false,
            is_closing: false,
            nny: if is_main { 1 } else { 0 },
            pending_future: None,
            #[cfg(feature = "sandbox")]
            sandbox_limits: None,
        }
    }

    // please donot use this function directly unless you are very sure of what you are doing
    pub(crate) unsafe fn thread_ptr(&self) -> ThreadPtr {
        self.thread
    }

    pub(crate) unsafe fn set_thread_ptr(&mut self, thread: ThreadPtr) {
        self.thread = thread;
    }

    /// Remove a dead string from the intern map (called by GC during sweep)
    pub(crate) fn remove_dead_string(&mut self, str_ptr: StringPtr) {
        unsafe {
            (*self.vm).object_allocator.remove_str(str_ptr);
        }
    }

    /// Get current call frame (equivalent to Lua's L->ci)
    #[inline(always)]
    pub fn current_frame(&self) -> Option<&CallInfo> {
        if self.call_depth > 0 {
            Some(self.call_stack.get(self.call_depth - 1)?.as_ref())
        } else {
            None
        }
    }

    /// Get current frame's top without Option wrapping (for hot paths where we know a frame exists)
    #[inline(always)]
    pub(crate) fn current_frame_top_unchecked(&self) -> usize {
        debug_assert!(self.call_depth > 0);
        unsafe { self.call_stack.get_unchecked(self.call_depth - 1).top as usize }
    }

    /// Get mutable current call frame
    #[inline(always)]
    pub fn current_frame_mut(&mut self) -> Option<&mut CallInfo> {
        if self.call_depth > 0 {
            Some(self.call_stack.get_mut(self.call_depth - 1)?.as_mut())
        } else {
            None
        }
    }

    /// Get call stack depth
    #[inline(always)]
    pub fn call_depth(&self) -> usize {
        self.call_depth
    }

    /// Push a new call frame (equivalent to Lua's luaD_precall)
    /// OPTIMIZED: Reuses CallInfo slots, only allocates when needed
    ///
    /// PERFORMANCE CRITICAL: This function is called on every function invocation
    /// Optimizations:
    /// - Assumes func is callable (checked by caller)
    /// - Caches as_lua_function() result to avoid repeated enum matching
    /// - Uses batch nil filling instead of loops
    /// - Minimizes branches in hot path
    #[inline]
    pub(crate) fn push_frame(
        &mut self,
        func: &LuaValue,
        base: usize,
        nparams: usize,
        nresults: i32,
    ) -> LuaResult<()> {
        // Fast path: check Lua call-stack depth
        if self.call_depth >= self.safe_state.max_call_depth {
            // If max_call_depth was elevated for error handling, produce
            // ErrorInErrorHandling (like C Lua's stackerror).
            if self.safe_state.max_call_depth > self.safe_state.base_call_depth {
                return Err(LuaError::ErrorInErrorHandling);
            }
            return Err(self.error(format!(
                "stack overflow (Lua stack depth: {})",
                self.call_depth
            )));
        }

        // Cache lua_function extraction (avoid repeated enum matching)
        // This single call replaces multiple is_c_function/as_lua_function checks

        // Determine function type and extract metadata in one pass
        let (call_status, maxstacksize, numparams, nextraargs, chunk_raw, upvalue_raw) =
            if let Some(func_obj) = func.as_lua_function() {
                let chunk = func_obj.chunk();
                // Lua function with chunk
                let numparams = chunk.param_count;
                let nextraargs = if nparams > numparams {
                    (nparams - numparams) as i32
                } else {
                    0
                };
                let chunk_ptr: *const crate::lua_value::Chunk = chunk;
                let upvalue_ptr = func_obj.upvalues().as_ptr();
                (
                    call_status::with_nresults(call_status::CIST_LUA, nresults),
                    chunk.max_stack_size,
                    numparams,
                    nextraargs,
                    chunk_ptr,
                    upvalue_ptr,
                )
            } else if func.is_c_callable() {
                // Light C function
                (
                    call_status::with_nresults(CIST_C, nresults),
                    nparams,
                    nparams,
                    0,
                    std::ptr::null(),
                    std::ptr::null(),
                )
            } else {
                // Not callable - this should be prevented by caller
                debug_assert!(false, "push_frame called with non-callable value");
                return Err(self.error(format!("attempt to call a {} value", func.type_name())));
            };

        // Fill missing parameters with nil (optimized batch operation)
        if nparams < numparams {
            let start = base + nparams;
            let end = base + numparams;

            // Ensure stack capacity.
            // CRITICAL: Use self.resize() instead of self.stack.resize() so that
            // open upvalue raw pointers are fixed if the Vec reallocates.
            // Direct self.stack.resize() bypasses fix_open_upvalue_pointers(),
            // causing use-after-free when upvalues read/write via stale pointers.
            if self.stack.len() < end {
                self.resize(end)?;
            }
            // Batch fill missing parameter slots with nil
            self.stack[start..end].fill(LuaValue::nil());

            // Update stack_top if necessary
            if self.stack_top < end {
                self.stack_top = end;
            }
        }

        let frame_top = base + maxstacksize;

        // Ensure physical stack has EXTRA_STACK slots above frame_top
        // for metamethod arguments (matching Lua 5.5's EXTRA_STACK guarantee)
        let needed_physical = frame_top + EXTRA_STACK;
        if needed_physical > self.stack.len() {
            self.resize(needed_physical)?;
        }

        // Fast path: reuse existing CallInfo slot (most common case)
        if self.call_depth < self.call_stack.len() {
            let ci = unsafe { self.call_stack.get_unchecked_mut(self.call_depth) };
            **ci = CallInfo {
                base,
                func_offset: 1,
                top: frame_top as u32,
                pc: 0,
                call_status,
                nextraargs,
                chunk_ptr: chunk_raw,
                upvalue_ptrs: upvalue_raw,
                aux_i32: -1,
            };
        } else {
            // Slow path: allocate new CallInfo (first time reaching this depth)
            let ci = Box::new(CallInfo {
                base,
                func_offset: 1,
                top: frame_top as u32,
                pc: 0,
                call_status,
                nextraargs,
                chunk_ptr: chunk_raw,
                upvalue_ptrs: upvalue_raw,
                aux_i32: -1,
            });
            self.call_stack.push(ci);
        }

        self.call_depth += 1;

        // Match Lua 5.5's luaD_precall: L->top.p = ci->top.p
        // For Lua functions, set stack_top to frame_top so the GC's
        // traverse_thread scans the full frame extent. Without this,
        // stack_top stays at the CALL instruction's ra+b, which can be
        // BELOW caller-frame locals that are still live — causing the GC
        // to miss marking those objects.
        if call_status & CIST_C == 0 && frame_top > self.stack_top {
            self.stack_top = frame_top;
        }

        Ok(())
    }

    /// Push a Lua function call frame (specialized fast path).
    /// Caller MUST already know `func` is a Lua function and provide the chunk metadata.
    /// Skips the function-type dispatch entirely.
    #[inline(always)]
    pub(crate) fn try_push_lua_frame_exact(
        &mut self,
        base: usize,
        nresults: i32,
        max_stack_size: usize,
        chunk_ptr: *const crate::lua_value::Chunk,
        upvalue_ptrs: *const crate::gc::UpvaluePtr,
    ) -> LuaResult<bool> {
        if self.call_depth >= self.safe_state.max_call_depth {
            self.push_lua_frame_overflow()?;
            return Ok(false);
        }

        let frame_top = base + max_stack_size;
        if frame_top + EXTRA_STACK > self.stack.len() || self.call_depth >= self.call_stack.len() {
            return Ok(false);
        }

        let ci = unsafe { self.call_stack.get_unchecked_mut(self.call_depth) };
        **ci = CallInfo {
            base,
            func_offset: 1,
            top: frame_top as u32,
            pc: 0,
            call_status: call_status::with_nresults(call_status::CIST_LUA, nresults),
            nextraargs: 0,
            chunk_ptr,
            upvalue_ptrs,
            aux_i32: -1,
        };

        self.call_depth += 1;
        if self.stack_top < frame_top {
            self.stack_top = frame_top;
        }
        Ok(true)
    }

    #[inline(always)]
    pub(crate) fn push_lua_frame(
        &mut self,
        base: usize,
        nparams: usize,
        nresults: i32,
        param_count: usize,
        max_stack_size: usize,
        chunk_ptr: *const crate::lua_value::Chunk,
        upvalue_ptrs: *const crate::gc::UpvaluePtr,
    ) -> LuaResult<()> {
        // Check stack depth (cold — almost never triggers)
        if self.call_depth >= self.safe_state.max_call_depth {
            return self.push_lua_frame_overflow();
        }

        // Pre-compute common values
        let frame_top = base + max_stack_size;

        // Fast path for the common case: enough params (no nil filling needed),
        // stack already large enough, call_stack slot available for reuse.
        // Covers exact match AND extra args (common in metamethods like __len
        // which receives 2 args but declares 1 param).
        if nparams >= param_count
            && frame_top + EXTRA_STACK <= self.stack.len()
            && self.call_depth < self.call_stack.len()
        {
            let ci = unsafe { self.call_stack.get_unchecked_mut(self.call_depth) }.as_mut();
            *ci = CallInfo {
                base,
                func_offset: 1,
                top: frame_top as u32,
                pc: 0,
                call_status: call_status::with_nresults(call_status::CIST_LUA, nresults),
                nextraargs: (nparams - param_count) as i32,
                chunk_ptr,
                upvalue_ptrs,
                aux_i32: -1,
            };

            self.call_depth += 1;

            if self.stack_top < frame_top {
                self.stack_top = frame_top;
            }

            return Ok(());
        }

        // Slow path: handle extra args, nil filling, stack resize, new slot allocation
        self.push_lua_frame_slow(
            base,
            nparams,
            nresults,
            param_count,
            max_stack_size,
            frame_top,
            chunk_ptr,
            upvalue_ptrs,
        )
    }

    /// Stack overflow error for push_lua_frame (cold path)
    #[cold]
    #[inline(never)]
    fn push_lua_frame_overflow(&mut self) -> LuaResult<()> {
        // If max_call_depth was elevated for error handling, produce
        // ErrorInErrorHandling (like C Lua's stackerror).
        if self.safe_state.max_call_depth > self.safe_state.base_call_depth {
            return Err(LuaError::ErrorInErrorHandling);
        }
        Err(self.error(format!(
            "stack overflow (Lua stack depth: {})",
            self.call_depth
        )))
    }

    /// Slow path for push_lua_frame — handles nil filling, resize, new slot allocation
    #[cold]
    #[inline(never)]
    fn push_lua_frame_slow(
        &mut self,
        base: usize,
        nparams: usize,
        nresults: i32,
        param_count: usize,
        _max_stack_size: usize,
        frame_top: usize,
        chunk_ptr: *const crate::lua_value::Chunk,
        upvalue_ptrs: *const crate::gc::UpvaluePtr,
    ) -> LuaResult<()> {
        let nextraargs = if nparams > param_count {
            (nparams - param_count) as i32
        } else {
            0
        };

        // Fill missing parameters with nil
        if nparams < param_count {
            let start = base + nparams;
            let end = base + param_count;
            // CRITICAL: Use self.resize() instead of self.stack.resize() so that
            // open upvalue raw pointers are fixed if the Vec reallocates.
            if self.stack.len() < end {
                self.resize(end)?;
            }
            // Batch fill missing parameter slots with nil
            self.stack[start..end].fill(LuaValue::nil());
            if self.stack_top < end {
                self.stack_top = end;
            }
        }

        let needed_physical = frame_top + EXTRA_STACK;
        if needed_physical > self.stack.len() {
            self.resize(needed_physical)?;
        }

        // Reuse existing CallInfo slot or allocate new one
        if self.call_depth < self.call_stack.len() {
            let ci = unsafe { self.call_stack.get_unchecked_mut(self.call_depth) };
            **ci = CallInfo {
                base,
                func_offset: 1,
                top: frame_top as u32,
                pc: 0,
                call_status: call_status::with_nresults(call_status::CIST_LUA, nresults),
                nextraargs,
                chunk_ptr,
                upvalue_ptrs,
                aux_i32: -1,
            };
        } else {
            self.call_stack.push(Box::new(CallInfo {
                base,
                func_offset: 1,
                top: frame_top as u32,
                pc: 0,
                call_status: call_status::with_nresults(call_status::CIST_LUA, nresults),
                nextraargs,
                chunk_ptr,
                upvalue_ptrs,
                aux_i32: -1,
            }));
        }

        self.call_depth += 1;

        if self.stack_top < frame_top {
            self.stack_top = frame_top;
        }

        Ok(())
    }

    /// Push a C function call frame (specialized fast path).
    /// Caller MUST already know `func` is a C function / cclosure.
    /// Skips the function-type dispatch entirely; mirrors `push_lua_frame`.
    #[inline(always)]
    pub(crate) fn push_c_frame(
        &mut self,
        base: usize,
        nargs: usize,
        nresults: i32,
    ) -> LuaResult<()> {
        // Check Lua call-stack depth
        if self.call_depth >= self.safe_state.max_call_depth {
            return Err(self.error(format!(
                "stack overflow (Lua stack depth: {})",
                self.call_depth
            )));
        }

        // For C functions: maxstacksize = nargs, numparams = nargs (no nil filling needed)
        let frame_top = base + nargs;

        // Ensure physical stack has EXTRA_STACK slots above frame_top
        let needed_physical = frame_top + EXTRA_STACK;
        if needed_physical > self.stack.len() {
            self.resize(needed_physical)?;
        }

        // Reuse existing CallInfo slot or allocate
        if self.call_depth < self.call_stack.len() {
            let ci = unsafe { self.call_stack.get_unchecked_mut(self.call_depth) }.as_mut();
            *ci = CallInfo {
                base,
                chunk_ptr: std::ptr::null(),
                upvalue_ptrs: std::ptr::null(),
                func_offset: 1,
                top: frame_top as u32,
                pc: 0,
                call_status: call_status::with_nresults(CIST_C, nresults),
                nextraargs: 0,
                aux_i32: -1,
            };
        } else {
            self.call_stack.push(Box::new(CallInfo {
                base,
                chunk_ptr: std::ptr::null(),
                upvalue_ptrs: std::ptr::null(),
                func_offset: 1,
                top: frame_top as u32,
                pc: 0,
                call_status: call_status::with_nresults(CIST_C, nresults),
                nextraargs: 0,
                aux_i32: -1,
            }));
        }

        self.call_depth += 1;

        if self.stack_top < frame_top {
            self.stack_top = frame_top;
        }

        Ok(())
    }

    /// Pop call frame (equivalent to Lua's luaD_poscall)
    #[inline(always)]
    pub(crate) fn pop_frame(&mut self) {
        if self.call_depth > 0 {
            self.call_depth -= 1;
        }
    }

    /// Pop a C call frame (specialized fast path, skips call_status bit check).
    /// Caller MUST know the current frame is a C frame.
    #[inline(always)]
    pub(crate) fn pop_c_frame(&mut self) {
        debug_assert!(self.call_depth > 0);
        self.call_depth -= 1;
    }

    /// Get logical stack top (L->top.p in Lua source)
    /// This is the first free slot in the stack, NOT the length of physical stack
    #[inline(always)]
    pub fn get_top(&self) -> usize {
        self.stack_top
    }

    /// Port of lua_checkstack (lapi.c): check if the stack can grow by `n` slots.
    /// Returns true if the stack can accommodate `n` more elements.
    pub fn check_stack(&self, n: usize) -> bool {
        self.stack_top + n <= self.safe_state.max_stack_size
    }

    /// Ensure the physical stack has room for `additional` more values beyond stack_top.
    /// Checks both the logical limit (max_stack_size) and physical capacity (Vec length).
    /// After this call, `push_value_unchecked` can be safely used `additional` times.
    #[inline]
    pub fn ensure_stack_capacity(&mut self, additional: usize) -> LuaResult<()> {
        let needed = self.stack_top + additional;
        if needed > self.safe_state.max_stack_size {
            return Err(LuaError::StackOverflow);
        }
        if needed > self.stack.len() {
            self.resize(needed)?;
        }
        Ok(())
    }

    /// Get a raw slice view of current frame arguments on the stack.
    /// Returns args[0..n] where arg(1) = slice[0], arg(2) = slice[1], etc.
    #[inline]
    pub fn arg_slice(&self) -> &[LuaValue] {
        if self.call_depth == 0 {
            return &[];
        }
        let frame = &self.call_stack[self.call_depth - 1];
        let end = (frame.top as usize).min(self.stack.len());
        &self.stack[frame.base..end]
    }

    /// Set logical stack top (L->top.p = L->stack + new_top in Lua)
    /// This is an internal VM operation — just moves the pointer.
    /// GC safety for stale slots above top is handled by the GC atomic phase
    /// (traverse_thread clears dead stack slices), matching Lua 5.5's design.
    #[inline(always)]
    pub fn set_top(&mut self, new_top: usize) -> LuaResult<()> {
        // Ensure physical stack is large enough
        if new_top > self.stack.len() {
            self.resize(new_top)?;
        }
        self.stack_top = new_top;

        Ok(())
    }

    /// Set logical stack top without any checks (fastest path).
    /// Caller must ensure physical stack is already large enough.
    /// Equivalent to Lua 5.5's `L->top.p = L->stack + new_top`.
    #[inline(always)]
    pub fn set_top_raw(&mut self, new_top: usize) {
        self.stack_top = new_top;
    }

    /// Get stack value at absolute index
    #[inline(always)]
    pub fn stack_get(&self, index: usize) -> Option<LuaValue> {
        self.stack.get(index).copied()
    }

    /// Set stack value at absolute index
    #[inline(always)]
    pub fn stack_set(&mut self, index: usize, value: LuaValue) -> LuaResult<()> {
        if index >= self.safe_state.max_stack_size {
            self.error(format!(
                "stack overflow: attempted to set index {} exceeding maximum {}",
                index, self.safe_state.max_stack_size
            ));
            return Err(LuaError::StackOverflow);
        }
        if index >= self.stack.len() {
            self.resize(index + 1)?;
        }
        self.stack[index] = value;
        Ok(())
    }

    fn resize(&mut self, new_size: usize) -> LuaResult<()> {
        if new_size > self.safe_state.max_stack_size {
            self.error(format!(
                "stack overflow: attempted to resize to {} exceeding maximum {}",
                new_size, self.safe_state.max_stack_size
            ));
            return Err(LuaError::StackOverflow);
        }
        let capacity = self.stack.capacity();
        self.stack.resize(new_size, LuaValue::nil());
        if self.stack.capacity() > capacity {
            // If the vector had to reallocate, we need to update all open upvalue pointers
            self.fix_open_upvalue_pointers();
        }

        Ok(())
    }

    /// Fix all open upvalue cached pointers after a Vec reallocation.
    /// Must be called whenever the stack Vec's internal buffer moves
    /// (e.g., after Vec::push triggers a reallocation).
    pub fn fix_open_upvalue_pointers(&mut self) {
        for upval_ptr in &self.open_upvalues_list {
            let data = &mut upval_ptr.as_mut_ref().data;
            // All entries in open_upvalues_list must be open
            debug_assert!(data.is_open());
            let stack_index = data.get_stack_index();
            if stack_index < self.stack.len() {
                data.update_stack_ptr(
                    (&self.stack[stack_index]) as *const LuaValue as *mut LuaValue,
                );
            }
        }
    }

    /// Get register relative to current frame base
    #[inline(always)]
    pub fn reg_get(&self, reg: u8) -> Option<LuaValue> {
        if let Some(frame) = self.current_frame() {
            self.stack_get(frame.base + reg as usize)
        } else {
            None
        }
    }

    /// Set register relative to current frame base
    #[inline(always)]
    pub fn reg_set(&mut self, reg: u8, value: LuaValue) -> LuaResult<()> {
        if let Some(frame) = self.current_frame() {
            let index = frame.base + reg as usize;
            self.stack_set(index, value)?;
        }

        Ok(())
    }

    /// Get open upvalues list
    #[inline(always)]
    pub fn open_upvalues(&self) -> &[UpvaluePtr] {
        &self.open_upvalues_list
    }

    /// Get mutable open upvalues list
    #[inline(always)]
    pub fn open_upvalues_mut(&mut self) -> &mut Vec<UpvaluePtr> {
        &mut self.open_upvalues_list
    }

    /// Set error message (without traceback - will be added later by top-level handler)
    #[cold]
    #[inline(never)]
    pub fn error(&mut self, msg: String) -> LuaError {
        // Try to get current source location for the error
        let mut location = String::new();
        if let Some(ci) = self.current_frame()
            && ci.is_lua()
            && !ci.chunk_ptr.is_null()
        {
            let chunk = unsafe { &*ci.chunk_ptr };
            let source = match chunk.source_name.as_deref() {
                Some(raw) => crate::compiler::format_source(raw),
                None => "?".to_string(), // stripped debug info
            };
            let line = if ci.pc > 0 && (ci.pc as usize - 1) < chunk.line_info.len() {
                chunk.line_info[ci.pc as usize - 1] as usize
            } else {
                0
            };
            location = if line > 0 {
                format!("{}:{}: ", source, line)
            } else if chunk.line_info.is_empty() {
                // No line info at all (stripped) — use ?
                format!("{}:?: ", source)
            } else {
                format!("{}: ", source)
            };
        };

        self.error_msg = format!("{}{}", location, msg);
        LuaError::RuntimeError
    }

    /// Raise an error from a C function, finding the calling Lua frame for location.
    /// Mirrors C Lua's luaL_error behavior: uses luaL_where(L,1) to get
    /// location from the Lua frame that called the current C function.
    #[cold]
    #[inline(never)]
    pub fn error_from_c(&mut self, msg: String) -> LuaError {
        // Find the nearest Lua frame by traversing up from current frame
        let depth = self.call_depth;
        for level in 0..depth {
            let idx = depth - 1 - level;
            if let Some(ci) = self.get_frame(idx)
                && ci.is_lua()
                && !ci.chunk_ptr.is_null()
            {
                let chunk = unsafe { &*ci.chunk_ptr };
                let source = match chunk.source_name.as_deref() {
                    Some(raw) => crate::compiler::format_source(raw),
                    None => "?".to_string(),
                };
                let line = if ci.pc > 0 && (ci.pc as usize - 1) < chunk.line_info.len() {
                    chunk.line_info[ci.pc as usize - 1] as usize
                } else {
                    0
                };
                let location = if line > 0 {
                    format!("{}:{}: ", source, line)
                } else if chunk.line_info.is_empty() {
                    format!("{}:?: ", source)
                } else {
                    format!("{}: ", source)
                };
                self.error_msg = format!("{}{}", location, msg);
                return LuaError::RuntimeError;
            }
        }
        // Fallback: no Lua frame found
        self.error_msg = msg;
        LuaError::RuntimeError
    }

    /// Set error message with preserved error object (for pcall to return)
    #[cold]
    #[inline(never)]
    pub fn error_with_object(&mut self, msg: String, obj: LuaValue) -> LuaError {
        self.error_object = obj;
        self.error(msg)
    }

    /// Clear error state
    #[inline(always)]
    pub fn clear_error(&mut self) {
        self.error_msg.clear();
        self.error_object = LuaValue::nil();
        self.yield_values.clear();
    }

    /// Get the last error message (for debugging)
    pub fn last_error_msg(&self) -> &str {
        &self.error_msg
    }

    /// Generate a Lua-style stack traceback
    /// Similar to luaL_traceback in lauxlib.c
    pub fn generate_traceback(&self) -> String {
        let mut result = String::new();

        // Only iterate through valid frames (up to call_depth)
        // call_stack Vec may contain residual data beyond call_depth
        let valid_frames = &self.call_stack[..self.call_depth];

        // Iterate through call stack from newest to oldest
        // Start from level 0 (most recent frame, not counting the error frame itself)
        for (level, ci) in valid_frames.iter().rev().enumerate() {
            if level >= 20 {
                result.push_str("\t...\n");
                break;
            }

            // Get function info
            if ci.is_lua() {
                // Lua function - get source and line info

                if !ci.chunk_ptr.is_null() {
                    let chunk = unsafe { &*ci.chunk_ptr };
                    let source = chunk.source_name.as_deref().unwrap_or("[string]");

                    // Format source name (strip @ prefix if present)
                    let source_display = crate::compiler::format_source(source);

                    // Get current line number from PC
                    let line = if ci.pc > 0 && (ci.pc as usize - 1) < chunk.line_info.len() {
                        chunk.line_info[ci.pc as usize - 1] as usize
                    } else if !chunk.line_info.is_empty() {
                        chunk.line_info[0] as usize
                    } else {
                        0
                    };

                    // Determine function description
                    // Main chunk has linedefined == 0
                    let what = if chunk.linedefined == 0 {
                        "main chunk".to_string()
                    } else {
                        format!("function <{}:{}>", source_display, chunk.linedefined)
                    };

                    if line > 0 {
                        result.push_str(&format!("\t{}:{}: in {}\n", source_display, line, what));
                    } else {
                        result.push_str(&format!("\t{}: in {}\n", source_display, what));
                    }
                    continue;
                }

                result.push_str("\t[?]: in function\n");
            } else if ci.is_c() {
                // C function
                result.push_str("\t[C]: in function\n");
            }
        }

        result
    }

    /// Set yield values
    #[inline(always)]
    pub fn set_yield(&mut self, values: Vec<LuaValue>) {
        self.yield_values = values;
    }

    /// Take yield values
    #[inline(always)]
    pub fn take_yield(&mut self) -> Vec<LuaValue> {
        std::mem::take(&mut self.yield_values)
    }

    /// Store a pending async future (called by async function wrappers before yielding)
    #[inline(always)]
    pub fn set_pending_future(
        &mut self,
        future: Pin<
            Box<dyn Future<Output = LuaResult<Vec<crate::lua_vm::async_thread::AsyncReturnValue>>>>,
        >,
    ) {
        self.pending_future = Some(future);
    }

    /// Take the pending async future (called by AsyncThread after detecting async yield)
    #[inline(always)]
    pub fn take_pending_future(
        &mut self,
    ) -> Option<
        Pin<
            Box<dyn Future<Output = LuaResult<Vec<crate::lua_vm::async_thread::AsyncReturnValue>>>>,
        >,
    > {
        self.pending_future.take()
    }

    /// Close upvalues from a given stack index upwards
    /// This is called when exiting a function or block scope
    /// Close upvalues from a given stack index upwards
    /// This is called when exiting a function or block scope
    pub fn close_upvalues(&mut self, level: usize) {
        // Optimization: The list is sorted by stack index descending (higher indices first).
        // Upvalues to close (index >= level) are at the beginning of the list.
        // We scan to find the cutoff point.
        let mut count = 0;
        let len = self.open_upvalues_list.len();

        while count < len {
            let upval_ptr = self.open_upvalues_list[count];
            let data = &upval_ptr.as_ref().data;
            // All entries in open_upvalues_list should be open
            if !data.is_open() || data.get_stack_index() < level {
                break;
            }
            count += 1;
        }

        if count > 0 {
            // Process upvalues to close in-place, then drain.
            for i in 0..count {
                let upval_ptr = self.open_upvalues_list[i];
                let data = &upval_ptr.as_ref().data;
                if data.is_open() {
                    let stack_idx = data.get_stack_index();

                    // Capture value from stack
                    let value = self
                        .stack
                        .get(stack_idx)
                        .copied()
                        .unwrap_or(LuaValue::nil());

                    // Close the upvalue (move value to heap)
                    upval_ptr.as_mut_ref().data.close(value);
                    let gc_ptr = GcObjectPtr::from(upval_ptr);

                    if let Some(header) = gc_ptr.header_mut()
                        && !header.is_white()
                    {
                        header.make_black();
                        if let Some(value_gc_ptr) = value.as_gc_ptr() {
                            self.gc_barrier(upval_ptr, value_gc_ptr);
                        }
                    }
                }
            }
            // Batch remove from front of list
            self.open_upvalues_list.drain(0..count);
        }
    }

    /// Get the name of a local variable at the given stack index
    /// by looking at the current frame's locvars debug info
    fn get_local_var_name(&self, stack_index: usize) -> Option<String> {
        let ci = self.current_frame()?;
        if !ci.is_lua() {
            return None;
        }
        if ci.chunk_ptr.is_null() {
            return None;
        }
        let chunk = unsafe { &*ci.chunk_ptr };
        let reg = stack_index.checked_sub(ci.base)?;
        // Use ci.pc (next instruction) as the PC for lookup,
        // because at TBC instruction, the variable's startpc equals the TBC PC
        // and ci.pc has already been incremented past it
        let pc = ci.pc as usize;
        // Walk locvars to find which variable occupies register 'reg' at 'pc'
        let mut n = 0usize;
        for locvar in &chunk.locals {
            if (locvar.startpc as usize) > pc {
                break;
            }
            if pc < locvar.endpc as usize {
                if n == reg {
                    return Some(locvar.name.clone());
                }
                n += 1;
            }
        }
        None
    }

    /// Mark a stack slot as to-be-closed (TBC)
    /// Called by OpCode::Tbc
    /// If the value is nil or false, it doesn't need to be closed
    /// Otherwise, it must have a __close metamethod
    pub fn mark_tbc(&mut self, stack_index: usize) -> LuaResult<()> {
        let value = self
            .stack
            .get(stack_index)
            .copied()
            .unwrap_or(LuaValue::nil());

        // nil and false don't need to be closed
        if value.is_falsy() {
            return Ok(());
        }

        // Check that the value has a __close metamethod
        use crate::lua_vm::execute::TmKind;
        use crate::lua_vm::execute::get_metamethod_event;
        let has_close = get_metamethod_event(self, &value, TmKind::Close).is_some();

        if !has_close {
            // Try to get the variable name from locvars
            let var_name = self.get_local_var_name(stack_index);
            let msg = if let Some(name) = var_name {
                format!("variable '{}' got a non-closable value", name)
            } else {
                "variable got a non-closable value".to_string()
            };
            return Err(self.error(msg));
        }

        self.tbc_list.push(stack_index);
        Ok(())
    }

    /// Close all to-be-closed variables down to (and including) the given level
    /// This calls __close(obj) on each TBC variable in reverse order
    /// For normal block exit (LUA_OK status), only 1 argument is passed
    /// If a __close method throws, subsequent closes get the error as 2nd arg
    /// (cascading error behavior from Lua 5.5)
    /// If a __close method yields, we propagate the yield immediately.
    /// The current TBC was already popped from tbc_list, so on resume
    /// close_tbc can be called again to continue with remaining entries.
    pub fn close_tbc(&mut self, level: usize) -> LuaResult<()> {
        // Restore cascaded error from a previous yield (saved in error_object
        // before yielding when a prior __close had thrown).
        let mut current_error: Option<LuaValue> = {
            let saved = std::mem::take(&mut self.error_object);
            if saved.is_nil() { None } else { Some(saved) }
        };

        while let Some(&tbc_idx) = self.tbc_list.last() {
            if tbc_idx < level {
                break;
            }
            self.tbc_list.pop();

            let value = self.stack.get(tbc_idx).copied().unwrap_or(LuaValue::nil());

            // Skip nil/false (shouldn't be in the list, but be safe)
            if value.is_falsy() {
                continue;
            }

            let close_method = get_metamethod_event(self, &value, TmKind::Close);

            if let Some(close_fn) = close_method {
                let result = if let Some(ref err) = current_error {
                    // Root the error on the Lua stack before the call so that
                    // GC can see it (a Rust local is invisible to the collector).
                    // This mirrors C Lua's prepcallclosemth which places the
                    // error at uv+1 and sets L->top accordingly.
                    let err_slot = tbc_idx + 1;
                    let needed = err_slot + 1;
                    if needed > self.stack.len() {
                        self.grow_stack(needed + 3)?;
                    }
                    self.stack[err_slot] = *err;
                    self.set_top_raw(err_slot + 1);

                    // Previous __close threw — pass error as 2nd arg
                    self.call_close_method_with_error(&close_fn, &value, *err)
                } else {
                    // Normal close — 1 argument only
                    self.call_close_method_normal(&close_fn, &value)
                };

                match result {
                    Ok(()) => {}
                    Err(LuaError::Yield) => {
                        // Close method yielded — propagate yield immediately.
                        // The TBC entry was already popped, so on resume
                        // close_tbc can continue with remaining entries.
                        // If a previous __close threw, persist the error in
                        // error_object so it survives the yield/resume cycle.
                        if let Some(err) = current_error {
                            self.error_object = err;
                        }
                        return Err(LuaError::Yield);
                    }
                    Err(_) => {
                        // This __close threw an error — capture it as current error
                        let err_obj = std::mem::take(&mut self.error_object);
                        if !err_obj.is_nil() {
                            current_error = Some(err_obj);
                        } else {
                            let msg = self.error_msg.clone();
                            if let Ok(s) = self.create_string(&msg) {
                                current_error = Some(s);
                            }
                        }
                        self.clear_error();
                    }
                }
            } else {
                // No __close metamethod on a non-nil/non-false TBC value
                // This is an error (metamethod was removed after marking)
                let var_name = self.get_local_var_name(tbc_idx);
                let msg = if let Some(name) = var_name {
                    format!(
                        "attempt to close non-closable variable '{}' (no metamethod 'close')",
                        name
                    )
                } else {
                    "attempt to close variable (no metamethod 'close')".to_string()
                };
                if let Ok(s) = self.create_string(&msg) {
                    current_error = Some(s);
                }
            }
        }

        // If any __close threw, propagate the last error
        if let Some(err) = current_error {
            self.error_object = err;
            let msg = if let Some(s) = err.as_str() {
                s.to_string()
            } else {
                format!("{:?}", err)
            };
            return Err(self.error(msg));
        }

        Ok(())
    }

    /// Close all upvalues AND to-be-closed variables down to the given level
    /// This is the main "close" operation used by OpCode::Close and return handlers
    pub fn close_all(&mut self, level: usize) -> LuaResult<()> {
        // First close upvalues (captures values from stack)
        self.close_upvalues(level);
        // Then call __close on TBC variables (in reverse order)
        self.close_tbc(level)
    }

    /// Close all to-be-closed variables with error status
    /// Used when unwinding due to errors  
    /// Calls __close(obj, err) — 2 arguments, with cascading error handling
    /// Yield propagation works the same as close_tbc.
    pub fn close_tbc_with_error(&mut self, level: usize, err: LuaValue) -> LuaResult<()> {
        use crate::lua_vm::execute::TmKind;
        use crate::lua_vm::execute::get_metamethod_event;

        let was_closing = self.is_closing;
        self.is_closing = true;

        let mut current_error = err;
        let mut had_close_error = false;

        while let Some(&tbc_idx) = self.tbc_list.last() {
            if tbc_idx < level {
                break;
            }
            self.tbc_list.pop();

            let value = self.stack.get(tbc_idx).copied().unwrap_or(LuaValue::nil());

            if value.is_falsy() {
                continue;
            }

            let close_method = get_metamethod_event(self, &value, TmKind::Close);

            if let Some(close_fn) = close_method {
                // Match C Lua's prepcallclosemth: set L->top to just past the
                // TBC variable + error slot.  This ensures ALL stack positions
                // below (including pending TBC vars) are within the GC scan
                // range (0..stack_top).  Place error on the stack at tbc_idx+1
                // so it is also a GC root (not just a Rust local).
                let err_slot = tbc_idx + 1;
                let needed = err_slot + 1; // need at least tbc_idx + 2
                if needed > self.stack.len() {
                    self.grow_stack(needed + 3)?;
                }
                self.stack[err_slot] = current_error;
                self.set_top_raw(err_slot + 1);

                // Call __close(obj, err) with 2 arguments.
                // call_close_method_with_error will place the call starting at
                // get_top() == tbc_idx + 2, right after the error slot.
                let result = self.call_close_method_with_error(&close_fn, &value, current_error);

                match result {
                    Ok(()) => {}
                    Err(LuaError::Yield) => {
                        // Close method yielded — propagate yield
                        return Err(LuaError::Yield);
                    }
                    Err(_) => {
                        // This __close threw — capture as new current error
                        had_close_error = true;
                        let err_obj = std::mem::take(&mut self.error_object);
                        if !err_obj.is_nil() {
                            current_error = err_obj;
                        } else {
                            let msg = self.error_msg.clone();
                            if let Ok(s) = self.create_string(&msg) {
                                current_error = s;
                            }
                        }
                        self.clear_error();
                    }
                }
            } else {
                // No __close metamethod — treat as error
                had_close_error = true;
                let var_name = self.get_local_var_name(tbc_idx);
                let msg = if let Some(name) = var_name {
                    format!(
                        "attempt to close non-closable variable '{}' (no metamethod 'close')",
                        name
                    )
                } else {
                    "attempt to close variable (no metamethod 'close')".to_string()
                };
                if let Ok(s) = self.create_string(&msg) {
                    current_error = s;
                }
            }
        }

        // Store the final cascaded error in error_object so pcall/xpcall can retrieve it
        if had_close_error {
            self.error_object = current_error;
        }

        self.is_closing = was_closing;
        Ok(())
    }

    /// Call __close(obj) for normal block exit — 1 argument only
    /// Lua 5.5: normal close passes errobj=NULL, so callclosemethod only pushes self
    fn call_close_method_normal(&mut self, close_fn: &LuaValue, obj: &LuaValue) -> LuaResult<()> {
        use crate::lua_vm::execute::{call, lua_execute};

        let caller_depth = self.call_depth();

        // Use current top directly (like Lua 5.5's callclosemethod)
        // Do NOT restore ci->top here — during pcall cleanup, frames may already
        // be popped and ci->top would be wrong
        let func_pos = self.get_top();

        // Ensure stack has space
        if func_pos + 2 >= self.stack.len() {
            self.grow_stack(func_pos + 3)?;
        }

        {
            let stack = self.stack_mut();
            stack[func_pos] = *close_fn; // function
            stack[func_pos + 1] = *obj; // self (1st argument)
        }
        self.set_top_raw(func_pos + 2); // 2 values: function + 1 arg

        let result = if close_fn.is_c_callable() {
            call::call_c_function(self, func_pos, 1, 0)
        } else if close_fn.is_lua_function() {
            let new_base = func_pos + 1;
            self.push_frame(close_fn, new_base, 1, 0)?;
            self.inc_n_ccalls()?;
            let r = lua_execute(self, caller_depth);
            self.dec_n_ccalls();
            r
        } else {
            // Non-callable close method (e.g., a number)
            let type_name = close_fn.type_name();
            Err(self.error(format!(
                "attempt to call a {} value (metamethod 'close')",
                type_name
            )))
        };

        match &result {
            Err(LuaError::Yield) => {
                // Yield: do NOT pop frames — they stay for resume
            }
            Err(_) => {
                // Error: pop any frames pushed by the close method
                while self.call_depth() > caller_depth {
                    self.pop_frame();
                }
            }
            Ok(()) => {}
        }

        result
    }

    /// Call __close(obj, err) for error unwinding — 2 arguments
    fn call_close_method_with_error(
        &mut self,
        close_fn: &LuaValue,
        obj: &LuaValue,
        err: LuaValue,
    ) -> LuaResult<()> {
        use crate::lua_vm::execute::{call, lua_execute};

        let caller_depth = self.call_depth();

        // Like Lua 5.5's callclosemethod: use current top directly, don't restore ci->top
        // (after frame pops, ci->top may be lower than TBC variables on the stack)
        let func_pos = self.get_top();
        // Ensure stack has room for function + 2 args
        if func_pos + 3 > self.stack().len() {
            self.grow_stack(func_pos + 3)?;
        }
        {
            let stack = self.stack_mut();
            stack[func_pos] = *close_fn; // function
            stack[func_pos + 1] = *obj; // self (1st argument)
            stack[func_pos + 2] = err; // error (2nd argument)
        }
        self.set_top_raw(func_pos + 3); // 3 values: function + 2 args

        let result = if close_fn.is_c_callable() {
            call::call_c_function(self, func_pos, 2, 0)
        } else if close_fn.is_lua_function() {
            let new_base = func_pos + 1;
            self.push_frame(close_fn, new_base, 2, 0)?;
            self.inc_n_ccalls()?;
            let r = lua_execute(self, caller_depth);
            self.dec_n_ccalls();
            r
        } else {
            // Non-callable close method (e.g., a number)
            let type_name = close_fn.type_name();
            Err(self.error(format!(
                "attempt to call a {} value (metamethod 'close')",
                type_name
            )))
        };

        match &result {
            Err(LuaError::Yield) => {
                // Yield: do NOT pop frames — they stay for resume
            }
            Err(_) => {
                // Error: pop any frames pushed by the close method
                while self.call_depth() > caller_depth {
                    self.pop_frame();
                }
            }
            Ok(()) => {}
        }

        result
    }

    /// Find or create an open upvalue for the given stack index.
    /// Uses linear scan on sorted Vec — faster than HashMap for typical 0-5 open upvalues.
    pub fn find_or_create_upvalue(&mut self, stack_index: usize) -> LuaResult<UpvaluePtr> {
        // Single scan on sorted list (descending by stack index).
        // Finds existing upvalue or determines insert position in one pass.
        let mut insert_pos = self.open_upvalues_list.len();
        for (i, &upval_ptr) in self.open_upvalues_list.iter().enumerate() {
            let idx = upval_ptr.as_ref().data.get_stack_index();
            if idx == stack_index {
                return Ok(upval_ptr);
            }
            if idx < stack_index {
                insert_pos = i;
                break;
            }
        }

        // Not found, create a new one
        let upval_ptr = {
            let ptr = LuaValuePtr {
                ptr: unsafe { self.stack.as_mut_ptr().add(stack_index) },
            };
            let vm = self.vm_mut();
            vm.create_upvalue_open(stack_index, ptr)?
        };

        self.open_upvalues_list.insert(insert_pos, upval_ptr);

        Ok(upval_ptr)
    }

    /// Get stack reference (for GC tracing)
    #[inline(always)]
    pub fn stack(&self) -> &[LuaValue] {
        &self.stack
    }

    /// Get mutable pointer to stack for VM execution
    ///
    /// # Safety
    /// Caller must ensure stack is not reallocated during pointer usage
    #[inline(always)]
    pub(crate) fn stack_mut(&mut self) -> &mut [LuaValue] {
        &mut self.stack
    }

    /// Get stack length
    #[inline(always)]
    pub fn stack_len(&self) -> usize {
        self.stack.len()
    }

    /// Truncate stack to specified length
    /// Used after function calls to remove temporary values
    pub fn stack_truncate(&mut self) {
        let new_len = 0;
        if new_len < self.stack.len() {
            for upval_ptr in &self.open_upvalues_list {
                let upval = &mut upval_ptr.as_mut_ref().data;
                if upval.is_open() {
                    let stack_idx = upval.get_stack_index();
                    if stack_idx >= new_len {
                        // Invalidate upvalue pointing to truncated stack
                        upval.close(self.stack[stack_idx]);
                    }
                }
            }

            self.stack.truncate(new_len);
        }
    }

    /// Grow stack to accommodate more values
    /// Grow stack to accommodate needed size (similar to luaD_growstack in Lua)
    /// Stack can grow dynamically up to MAX_STACK_SIZE
    /// C functions can call this, which means Vec may reallocate
    #[inline(never)]
    pub fn grow_stack(&mut self, needed: usize) -> LuaResult<()> {
        if needed > self.safe_state.max_stack_size {
            self.error(format!(
                "stack overflow: attempted to grow stack to {} exceeding maximum {}",
                needed, self.safe_state.max_stack_size
            ));
            return Err(LuaError::StackOverflow);
        }
        if self.stack.len() < needed {
            self.resize(needed)?;
        }

        Ok(())
    }

    /// Get frame base by index
    #[inline(always)]
    pub fn get_frame_base(&self, frame_idx: usize) -> usize {
        unsafe { self.call_stack.get_unchecked(frame_idx).base }
    }

    /// Get frame PC by index
    #[inline(always)]
    pub fn get_frame_pc(&self, frame_idx: usize) -> u32 {
        // Only return PC if frame is within current call_depth (valid frames)
        if frame_idx < self.call_depth {
            self.call_stack.get(frame_idx).map(|f| f.pc).unwrap_or(0)
        } else {
            0
        }
    }

    /// Get frame function by index
    #[inline(always)]
    pub fn get_frame_func(&self, frame_idx: usize) -> Option<LuaValue> {
        // Only return frame if it's within current call_depth (valid frames)
        if frame_idx < self.call_depth {
            let ci = self.call_stack.get(frame_idx)?.as_ref();
            self.stack.get(ci.func_index()).copied()
        } else {
            None
        }
    }

    /// Get frame by index (for GC root collection)
    #[inline(always)]
    pub fn get_frame(&self, frame_idx: usize) -> Option<&CallInfo> {
        // Only return frame if it's within current call_depth (valid frames)
        if frame_idx < self.call_depth {
            Some(self.call_stack.get(frame_idx)?.as_ref())
        } else {
            None
        }
    }

    // ========================================================================
    // Debug info (port of lua_getinfo / auxgetinfo from ldebug.c)
    // ========================================================================

    /// Get debug info for a stack frame at the given level.
    /// Level 0 = most-recent frame (top of stack), level 1 = its caller, etc.
    /// `what` controls which fields are filled (same options as C Lua's lua_getinfo).
    /// Returns `None` if the level is out of range.
    pub fn get_info_by_level(
        &self,
        level: usize,
        what: &str,
    ) -> Option<crate::lua_vm::debug_info::DebugInfo> {
        let call_depth = self.call_depth();
        if level >= call_depth {
            return None;
        }
        let frame_idx = call_depth - 1 - level;
        let func = self.get_frame_func(frame_idx)?;
        let ci = self.get_frame(frame_idx);
        Some(self.fill_debug_info(&func, ci, Some(frame_idx), what))
    }

    /// Get debug info for a function value (not on the call stack).
    /// Only fields that don't require a stack frame are meaningful
    /// ('n' and 'l' will return empty/defaults).
    pub fn get_info_for_func(
        &self,
        func: &LuaValue,
        what: &str,
    ) -> crate::lua_vm::debug_info::DebugInfo {
        self.fill_debug_info(func, None, None, what)
    }

    /// Core implementation: fill a DebugInfo struct based on the 'what' options.
    /// Port of auxgetinfo from ldebug.c.
    fn fill_debug_info(
        &self,
        func: &LuaValue,
        ci: Option<&CallInfo>,
        frame_idx: Option<usize>,
        what: &str,
    ) -> crate::lua_vm::debug_info::DebugInfo {
        use crate::lua_vm::call_info::call_status;
        let mut info = crate::lua_vm::debug_info::DebugInfo::new();

        if let Some(lua_func) = func.as_lua_function() {
            let chunk = lua_func.chunk();

            for ch in what.chars() {
                match ch {
                    'S' => {
                        info.fill_source(
                            chunk.source_name.as_deref(),
                            chunk.linedefined as i32,
                            chunk.lastlinedefined as i32,
                        );
                    }
                    'l' => {
                        let currentline = if let Some(ci) = ci {
                            if ci.is_lua() {
                                let pc_idx = ci.pc.saturating_sub(1) as usize;
                                if !chunk.line_info.is_empty() && pc_idx < chunk.line_info.len() {
                                    chunk.line_info[pc_idx] as i32
                                } else {
                                    -1
                                }
                            } else {
                                -1
                            }
                        } else {
                            -1
                        };
                        info.fill_currentline(currentline);
                    }
                    'u' => {
                        info.fill_upvalues(
                            chunk.upvalue_count as u8,
                            chunk.param_count as u8,
                            chunk.is_vararg,
                        );
                    }
                    'n' => {
                        if let Some(fidx) = frame_idx {
                            if let Some((namewhat, name)) =
                                crate::stdlib::debug::pub_getfuncname(self, fidx)
                            {
                                info.fill_name(namewhat, &name);
                            } else {
                                info.fill_name_empty();
                            }
                        } else {
                            info.fill_name_empty();
                        }
                    }
                    't' => {
                        let (istailcall, extraargs) = if let Some(ci) = ci {
                            (ci.is_tail(), call_status::get_ccmt_count(ci.call_status))
                        } else {
                            (false, 0)
                        };
                        info.fill_tail(istailcall, extraargs);
                    }
                    'r' => {
                        // Transfer info — only meaningful inside hooks (CIST_HOOKED).
                        // Like C Lua: only return actual values when ci has CIST_HOOKED flag.
                        if let Some(ci) = ci {
                            if ci.call_status & crate::lua_vm::call_info::call_status::CIST_HOOKED
                                != 0
                            {
                                info.fill_transfer(self.ftransfer, self.ntransfer);
                            } else {
                                info.fill_transfer(0, 0);
                            }
                        } else {
                            info.fill_transfer(0, 0);
                        }
                    }
                    'L' => {
                        info.fill_activelines(&chunk.line_info, chunk.is_vararg);
                    }
                    'f' => {
                        info.fill_func(*func);
                    }
                    _ => {} // ignore unknown options
                }
            }
        } else if func.is_c_callable() {
            // C function
            for ch in what.chars() {
                match ch {
                    'S' => {
                        info.fill_source_c();
                    }
                    'l' => {
                        info.fill_currentline(-1);
                    }
                    'u' => {
                        // C functions: nups from the closure, params=0, isvararg=true
                        let nups = func
                            .as_cclosure()
                            .map(|c| c.upvalues().len() as u8)
                            .unwrap_or(0);
                        info.fill_upvalues_c(nups);
                    }
                    'n' => {
                        if let Some(fidx) = frame_idx {
                            if let Some((namewhat, name)) =
                                crate::stdlib::debug::pub_getfuncname(self, fidx)
                            {
                                info.fill_name(namewhat, &name);
                            } else {
                                info.fill_name_empty();
                            }
                        } else {
                            info.fill_name_empty();
                        }
                    }
                    't' => {
                        let (istailcall, extraargs) = if let Some(ci) = ci {
                            (ci.is_tail(), call_status::get_ccmt_count(ci.call_status))
                        } else {
                            (false, 0)
                        };
                        info.fill_tail(istailcall, extraargs);
                    }
                    'r' => {
                        if let Some(ci) = ci {
                            if ci.call_status & crate::lua_vm::call_info::call_status::CIST_HOOKED
                                != 0
                            {
                                info.fill_transfer(self.ftransfer, self.ntransfer);
                            } else {
                                info.fill_transfer(0, 0);
                            }
                        } else {
                            info.fill_transfer(0, 0);
                        }
                    }
                    'L' => {
                        info.fill_activelines_nil();
                    }
                    'f' => {
                        info.fill_func(*func);
                    }
                    _ => {}
                }
            }
        }

        info
    }

    /// Get all open upvalues (for GC root collection)
    #[inline(always)]
    pub fn get_open_upvalues(&self) -> &[UpvaluePtr] {
        &self.open_upvalues_list
    }

    #[inline(always)]
    pub(crate) fn vm_mut(&mut self) -> &mut LuaVM {
        unsafe { &mut *self.vm }
    }

    pub(crate) fn vm_ptr(&self) -> *mut LuaVM {
        self.vm
    }

    /// Lua 5.5-style ccall depth tracking: increment shared n_ccalls before
    /// a recursive `lua_execute` call.  Returns `Err("C stack overflow")` if
    /// the limit is reached.  The limit is checked against this thread's
    /// `safe_option.max_c_stack_depth` so that xpcall's EXTRA zone increase
    /// takes effect.
    #[inline(always)]
    pub(crate) fn inc_n_ccalls(&mut self) -> LuaResult<()> {
        let vm = unsafe { &mut *self.vm };
        vm.n_ccalls += 1;
        if vm.n_ccalls >= self.safe_state.max_c_stack_depth {
            vm.n_ccalls -= 1;
            if self.safe_state.max_c_stack_depth > self.safe_state.base_c_stack_depth {
                // In error handler extra zone — C Lua's stackerror behavior
                return Err(LuaError::ErrorInErrorHandling);
            }
            Err(self.error("C stack overflow".to_string()))
        } else {
            Ok(())
        }
    }

    /// Decrement shared n_ccalls after returning from a recursive `lua_execute`.
    #[inline(always)]
    pub(crate) fn dec_n_ccalls(&self) {
        unsafe {
            (*self.vm).n_ccalls -= 1;
        }
    }

    // ===== Call Frame Management =====

    /// Get current CallInfo by index (unchecked — caller must ensure idx < call_depth)
    #[inline(always)]
    pub fn get_call_info(&self, idx: usize) -> &CallInfo {
        debug_assert!(idx < self.call_stack.len());
        unsafe { self.call_stack.get_unchecked(idx) }
    }

    /// Get mutable CallInfo by index (unchecked — caller must ensure idx < call_depth)
    #[inline(always)]
    pub fn get_call_info_mut(&mut self, idx: usize) -> &mut CallInfo {
        debug_assert!(idx < self.call_stack.len());
        unsafe { self.call_stack.get_unchecked_mut(idx) }
    }

    pub(crate) unsafe fn get_call_info_ptr(&self, idx: usize) -> *const CallInfo {
        debug_assert!(idx < self.call_stack.len());
        unsafe { self.call_stack.get_unchecked(idx).as_ref() as *const CallInfo }
    }

    /// Pop the current call frame (Lua callers only — does NOT adjust VM n_ccalls)
    #[inline(always)]
    pub fn pop_call_frame(&mut self) {
        debug_assert!(self.call_depth > 0);
        self.call_depth -= 1;
    }

    /// Get return values from stack
    /// Returns values from stack_base to stack_base + count
    pub fn get_return_values(&self, stack_base: usize, count: usize) -> Vec<LuaValue> {
        let mut results = Vec::with_capacity(count);
        for i in 0..count {
            if let Some(val) = self.stack_get(stack_base + i) {
                results.push(val);
            } else {
                results.push(LuaValue::nil());
            }
        }
        results
    }

    /// Get all return values from stack starting at stack_base
    pub fn get_all_return_values(&self, stack_base: usize) -> Vec<LuaValue> {
        let top = self.get_top();
        let count = top.saturating_sub(stack_base);
        self.get_return_values(stack_base, count)
    }

    // ===== Function Argument Access =====

    /// Get all arguments for the current C function call
    /// Returns arguments starting from index 1 (index 0 is the function itself)
    pub fn get_args(&self) -> Vec<LuaValue> {
        if self.call_depth == 0 {
            return Vec::new();
        }

        let frame = &self.call_stack[self.call_depth - 1];
        let base = frame.base;
        let top = frame.top as usize;

        // Arguments are from base to top-1 (NOT base+1!)
        // In Lua, the function itself is NOT part of the frame's stack
        // The frame starts at the first argument
        let arg_count = top.saturating_sub(base);

        let mut args = Vec::with_capacity(arg_count);
        for i in 0..arg_count {
            if let Some(val) = self.stack_get(base + i) {
                args.push(val);
            } else {
                args.push(LuaValue::nil());
            }
        }
        args
    }

    /// Get a specific argument (1-based index, Lua convention)
    /// Returns None if index is out of bounds
    pub fn get_arg(&self, index: usize) -> Option<LuaValue> {
        if index == 0 || self.call_depth == 0 {
            return None;
        }

        let frame = &self.call_stack[self.call_depth - 1];
        let base = frame.base;
        let top = frame.top as usize;

        // Arguments are 1-based: arg 1 is at base, arg 2 is at base+1, etc.
        // (NOT base+1, because C function frame.base already points to first arg)
        let stack_index = base + index - 1;

        // Check if argument position is within the frame's range and the stack
        if stack_index < top && stack_index < self.stack.len() {
            // Return the value (including nil values)
            Some(self.stack[stack_index])
        } else {
            // Argument doesn't exist
            None
        }
    }

    /// Get a specific argument without bounds checking (1-based index).
    /// SAFETY: Caller MUST ensure index >= 1, call_depth > 0,
    /// and `base + index - 1 < stack.len()`.
    #[inline(always)]
    pub unsafe fn get_arg_unchecked(&self, index: usize) -> LuaValue {
        unsafe {
            let frame = self.call_stack.get_unchecked(self.call_depth - 1);
            *self.stack.get_unchecked(frame.base + index - 1)
        }
    }

    /// Get the number of arguments for the current function call
    pub fn arg_count(&self) -> usize {
        if self.call_depth == 0 {
            return 0;
        }

        let frame = &self.call_stack[self.call_depth - 1];
        let base = frame.base;
        let top = frame.top as usize;

        // Arguments are from base to top-1
        top.saturating_sub(base)
    }

    pub fn push_value(&mut self, value: LuaValue) -> LuaResult<()> {
        // Check stack limit (Lua's luaD_checkstack equivalent)
        if self.stack_top >= self.safe_state.max_stack_size {
            self.error(format!(
                "stack overflow: attempted to push value exceeding maximum {}",
                self.safe_state.max_stack_size
            ));
            return Err(LuaError::StackOverflow);
        }

        // Save current top before any borrows
        let current_top = self.stack_top;

        // Ensure physical stack is large enough (Lua's luaD_reallocstack equivalent)
        if current_top >= self.stack.len() {
            // 1.5 x growth strategy
            let mut new_size = current_top + current_top / 2;
            if new_size < current_top + 1 {
                new_size = current_top + 1;
            }
            if new_size > self.safe_state.max_stack_size {
                new_size = self.safe_state.max_stack_size;
            }
            self.resize(new_size)?;
        }

        // Write at logical top position (L->top.p->value = value)
        self.stack[current_top] = value;

        // Increment logical top (L->top.p++)
        // Do NOT modify frame.top (ci->top) — it's immutable after push_frame.
        self.stack_top = current_top + 1;

        Ok(())
    }

    /// Push a value to the stack without capacity/overflow checking.
    /// SAFETY: Caller MUST ensure physical stack has room (guaranteed by EXTRA_STACK
    /// after push_c_frame) and stack_top < max_stack_size.
    #[inline(always)]
    pub unsafe fn push_value_unchecked(&mut self, value: LuaValue) {
        unsafe {
            let top = self.stack_top;
            *self.stack.get_unchecked_mut(top) = value;
            self.stack_top = top + 1;
        }
    }

    // ===== Object Creation =====

    /// Create table
    #[inline(always)]
    pub fn create_table(&mut self, narr: usize, nrec: usize) -> CreateResult {
        self.vm_mut().create_table(narr, nrec)
    }

    /// Create function closure
    #[inline]
    pub fn create_function(&mut self, chunk: ProtoPtr, upvalues: UpvalueStore) -> CreateResult {
        self.vm_mut().create_function(chunk, upvalues)
    }

    pub fn create_upvalue_closed(&mut self, value: LuaValue) -> LuaResult<UpvaluePtr> {
        self.vm_mut().create_upvalue_closed(value)
    }

    pub fn create_upvalue_open(
        &mut self,
        stack_index: usize,
        stack_ptr: LuaValuePtr,
    ) -> LuaResult<UpvaluePtr> {
        self.vm_mut().create_upvalue_open(stack_index, stack_ptr)
    }

    /// Create/intern string (automatically handles short string interning)
    #[inline]
    pub fn create_string(&mut self, s: &str) -> CreateResult {
        self.vm_mut().create_string(s)
    }

    #[inline]
    pub fn create_string_owned(&mut self, s: String) -> CreateResult {
        self.vm_mut().create_string_owned(s)
    }

    #[inline]
    pub fn create_binary(&mut self, data: Vec<u8>) -> CreateResult {
        self.vm_mut().create_binary(data)
    }

    #[inline]
    pub fn create_bytes(&mut self, bytes: &[u8]) -> CreateResult {
        self.vm_mut().create_bytes(bytes)
    }

    /// Create userdata
    pub fn create_userdata(&mut self, data: LuaUserdata) -> CreateResult {
        self.vm_mut().create_userdata(data)
    }

    /// Create a GC-managed userdata that **borrows** an external Rust object.
    ///
    /// The object stays on the Rust side; Lua gets a full userdata with field access,
    /// method calls, and metamethods — all forwarded through a raw pointer.
    ///
    /// # Safety
    /// The referenced object **must** outlive all Lua accesses to this userdata.
    /// Typical safe patterns:
    /// - Set the global, run Lua code, then clear/overwrite the global before the
    ///   Rust object is dropped.
    /// - Use scoped execution: create → execute → drop the Lua state / global.
    ///
    /// # Example
    /// ```ignore
    /// let mut player = Player::new("Alice", 100);
    /// let ud = unsafe { state.create_userdata_ref(&mut player)? };
    /// state.set_global("player", ud)?;
    /// state.execute_string(r#"player:take_damage(10)"#)?;
    /// assert_eq!(player.hp, 90); // Lua mutations visible in Rust
    /// ```
    #[inline]
    pub unsafe fn create_userdata_ref<T: UserDataTrait>(
        &mut self,
        reference: &mut T,
    ) -> CreateResult {
        let ud = unsafe { LuaUserdata::from_ref(reference) };
        self.vm_mut().create_userdata(ud)
    }

    /// Create an RClosure from any `Fn(&mut LuaState) -> LuaResult<usize> + 'static`.
    /// Unlike `CFunction` (bare fn pointer), this can capture arbitrary Rust state.
    #[inline]
    pub fn create_closure<F>(&mut self, func: F) -> CreateResult
    where
        F: Fn(&mut LuaState) -> LuaResult<usize> + 'static,
    {
        self.vm_mut().create_closure(func)
    }

    /// Create an RClosure with upvalues.
    #[inline]
    pub fn create_closure_with_upvalues<F>(
        &mut self,
        func: F,
        upvalues: Vec<LuaValue>,
    ) -> CreateResult
    where
        F: Fn(&mut LuaState) -> LuaResult<usize> + 'static,
    {
        self.vm_mut().create_closure_with_upvalues(func, upvalues)
    }

    // ===== Global Access =====

    /// Get global variable
    pub fn get_global(&mut self, name: &str) -> LuaResult<Option<LuaValue>> {
        self.vm_mut().get_global(name)
    }

    /// Set global variable
    pub fn set_global(&mut self, name: &str, value: LuaValue) -> LuaResult<()> {
        self.vm_mut().set_global(name, value)
    }

    // ===== Convenience API =====

    /// Compile source code and return a callable function value with _ENV wired.
    ///
    /// See [`LuaVM::load`] for details.
    pub fn load(&mut self, source: &str) -> LuaResult<LuaValue> {
        self.vm_mut().load(source)
    }

    #[cfg(feature = "sandbox")]
    pub fn load_sandboxed(&mut self, source: &str, config: &SandboxConfig) -> LuaResult<LuaValue> {
        self.vm_mut().load_sandboxed(source, config)
    }

    /// Compile source code with a chunk name and return a callable function value.
    ///
    /// See [`LuaVM::load_with_name`] for details.
    pub fn load_with_name(&mut self, source: &str, chunk_name: &str) -> LuaResult<LuaValue> {
        self.vm_mut().load_with_name(source, chunk_name)
    }

    #[cfg(feature = "sandbox")]
    pub fn load_with_name_sandboxed(
        &mut self,
        source: &str,
        chunk_name: &str,
        config: &SandboxConfig,
    ) -> LuaResult<LuaValue> {
        self.vm_mut()
            .load_with_name_sandboxed(source, chunk_name, config)
    }

    /// Read a file, compile it, and execute it.
    ///
    /// See [`LuaVM::dofile`] for details.
    pub fn dofile(&mut self, path: &str) -> LuaResult<Vec<LuaValue>> {
        self.vm_mut().dofile(path)
    }

    /// Call a function value with arguments.
    ///
    /// Unlike the LuaVM version, this operates on the *current* coroutine state
    /// and is safe to call from within a CFunction.
    pub fn call_function(
        &mut self,
        func: LuaValue,
        args: Vec<LuaValue>,
    ) -> LuaResult<Vec<LuaValue>> {
        self.call(func, args)
    }

    /// Look up a global function by name and call it.
    ///
    /// Safe to call from within a CFunction — operates on the current state.
    pub fn call_global(&mut self, name: &str, args: Vec<LuaValue>) -> LuaResult<Vec<LuaValue>> {
        let func = self
            .get_global(name)?
            .ok_or_else(|| self.error(format!("global '{}' not found", name)))?;
        self.call(func, args)
    }

    /// Register a synchronous Rust closure as a Lua global function.
    ///
    /// See [`LuaVM::register_function`] for details.
    pub fn register_function<F>(&mut self, name: &str, f: F) -> LuaResult<()>
    where
        F: Fn(&mut LuaState) -> LuaResult<usize> + 'static,
    {
        self.vm_mut().register_function(name, f)
    }

    pub fn register_function_typed<F, Args, R>(&mut self, name: &str, f: F) -> LuaResult<()>
    where
        F: crate::lua_vm::LuaTypedCallback<Args, R>,
    {
        self.vm_mut().register_function_typed(name, f)
    }

    /// Register a typed async Rust closure as a Lua global function.
    ///
    /// See [`LuaVM::register_async_typed`] for details.
    pub fn register_async_typed<F, Args, R>(&mut self, name: &str, f: F) -> LuaResult<()>
    where
        F: crate::lua_vm::LuaTypedAsyncCallback<Args, R>,
    {
        self.vm_mut().register_async_typed(name, f)
    }

    // ===== Async Support =====

    /// Register an async function as a Lua global (convenience proxy for `LuaVM::register_async`).
    ///
    /// See [`LuaVM::register_async`] for details.
    pub fn register_async<F, Fut>(&mut self, name: &str, f: F) -> LuaResult<()>
    where
        F: Fn(Vec<LuaValue>) -> Fut + 'static,
        Fut: std::future::Future<
                Output = LuaResult<Vec<crate::lua_vm::async_thread::AsyncReturnValue>>,
            > + 'static,
    {
        self.vm_mut().register_async(name, f)
    }

    // ===== Execute =====

    /// Compile and execute a Lua source string, returning results.
    ///
    /// This is a convenience proxy for `LuaVM::execute_string`.
    ///
    /// # Example
    /// ```ignore
    /// let results = state.execute("return 1 + 2")?;
    /// assert_eq!(results[0].as_integer(), Some(3));
    /// ```
    pub fn execute(&mut self, source: &str) -> LuaResult<Vec<LuaValue>> {
        self.vm_mut().execute(source)
    }

    #[cfg(feature = "sandbox")]
    pub fn execute_sandboxed(
        &mut self,
        source: &str,
        config: &SandboxConfig,
    ) -> LuaResult<Vec<LuaValue>> {
        self.vm_mut().execute_sandboxed(source, config)
    }

    /// Execute a pre-compiled chunk, returning results.
    ///
    /// This is a convenience proxy for `LuaVM::execute`.
    pub fn execute_chunk(&mut self, chunk: ProtoPtr) -> LuaResult<Vec<LuaValue>> {
        self.vm_mut().execute_chunk(chunk)
    }

    // ===== Type Registration =====

    /// Register a UserData type as a Lua global table with its static methods.
    ///
    /// Creates a table (e.g. `Point`) and populates it with all associated
    /// functions defined in the type's `#[lua_methods]` block (functions
    /// without `self`, such as constructors).
    ///
    /// After registration, Lua code can call e.g. `Point.new(3, 4)`.
    ///
    /// # Usage
    /// ```ignore
    /// // In Rust:
    /// state.register_type("Point", Point::__lua_static_methods())?;
    ///
    /// // In Lua:
    /// local p = Point.new(3, 4)
    /// print(p.x, p.y)      -- 3.0  4.0
    /// print(p:distance())   -- 5.0
    /// ```
    pub fn register_type(
        &mut self,
        name: &str,
        static_methods: &[(&str, super::CFunction)],
    ) -> LuaResult<()> {
        let class_table = self.create_table(0, static_methods.len())?;

        for &(method_name, func) in static_methods {
            let key = self.create_string(method_name)?;
            let value = LuaValue::cfunction(func);
            self.raw_set(&class_table, key, value);
        }

        self.set_global(name, class_table)
    }

    /// Register a UserData type by its generic type parameter.
    ///
    /// Equivalent to `register_type(name, T::__lua_static_methods())` but more
    /// concise and type-safe. Uses the `LuaStaticMethodProvider` trait (auto-
    /// implemented by `#[lua_methods]`) to discover static methods.
    ///
    /// # Usage
    /// ```ignore
    /// // Instead of:
    /// state.register_type("Point", Point::__lua_static_methods())?;
    ///
    /// // Write:
    /// state.register_type_of::<Point>("Point")?;
    /// ```
    pub fn register_type_of<T: LuaRegistrable>(&mut self, name: &str) -> LuaResult<()> {
        self.register_type(name, T::lua_static_methods())
    }

    // ===== Table Operations =====

    /// Get value from table (raw, no metamethods)
    pub fn raw_get(&mut self, table: &LuaValue, key: &LuaValue) -> Option<LuaValue> {
        self.vm_mut().raw_get(table, key)
    }

    /// Get value from table with __index metamethod support
    pub fn table_get(&mut self, table: &LuaValue, key: &LuaValue) -> LuaResult<Option<LuaValue>> {
        // First try raw access
        if let Some(val) = self.vm_mut().raw_get(table, key) {
            return Ok(Some(val));
        }
        // If not found, try __index metamethod
        execute::helper::finishget(self, table, key)
    }

    /// Set value in table with metamethod support (__newindex)
    pub fn table_set(&mut self, table: &LuaValue, key: LuaValue, value: LuaValue) -> LuaResult<()> {
        execute::helper::finishset(self, table, &key, value)?;
        Ok(())
    }

    /// Compare two values using < operator with metamethod support (__lt)
    pub fn obj_lt(&mut self, a: &LuaValue, b: &LuaValue) -> LuaResult<bool> {
        // Integer-integer
        if let (Some(i1), Some(i2)) = (a.as_integer(), b.as_integer()) {
            return Ok(i1 < i2);
        }
        // Float-float
        if let (Some(f1), Some(f2)) = (a.as_float(), b.as_float()) {
            return Ok(f1 < f2);
        }
        // Mixed number
        if let (Some(n1), Some(n2)) = (a.as_number(), b.as_number()) {
            return Ok(n1 < n2);
        }
        // String (including binary): compare raw bytes
        if a.is_string() && b.is_string() {
            let ba = a.as_bytes();
            let bb = b.as_bytes();
            if let (Some(ba), Some(bb)) = (ba, bb) {
                return Ok(ba < bb);
            }
        }
        // Try __lt metamethod
        match execute::metamethod::try_comp_tm(self, *a, *b, execute::TmKind::Lt) {
            Ok(Some(result)) => Ok(result),
            Ok(None) => Err(crate::stdlib::debug::ordererror(self, a, b)),
            Err(e) => Err(e),
        }
    }

    /// Get object length with metamethod support (__len)
    /// Returns the length as i64, going through __len if available.
    pub fn obj_len(&mut self, obj: &LuaValue) -> LuaResult<i64> {
        if let Some(bytes) = obj.as_bytes() {
            return Ok(bytes.len() as i64);
        }
        if obj.ttistable() {
            if let Some(mm) = execute::get_metamethod_event(self, obj, execute::TmKind::Len) {
                let result = execute::call_tm_res(self, mm, *obj, *obj)?;
                return result
                    .as_integer()
                    .ok_or_else(|| self.error("object length is not an integer".to_string()));
            }

            return Ok(obj.as_table().unwrap().len() as i64);
        }
        if let Some(mm) = execute::get_metamethod_event(self, obj, execute::TmKind::Len) {
            let result = execute::call_tm_res(self, mm, *obj, *obj)?;
            return result
                .as_integer()
                .ok_or_else(|| self.error("object length is not an integer".to_string()));
        }
        Err(self.error(format!(
            "attempt to get length of a {} value",
            obj.type_name()
        )))
    }

    /// Set value in table
    pub fn raw_set(&mut self, table: &LuaValue, key: LuaValue, value: LuaValue) -> bool {
        self.vm_mut().raw_set(table, key, value)
    }

    pub fn raw_geti(&mut self, table: &LuaValue, index: i64) -> Option<LuaValue> {
        self.vm_mut().raw_geti(table, index)
    }

    pub fn raw_seti(&mut self, table: &LuaValue, index: i64, value: LuaValue) -> bool {
        self.vm_mut().raw_seti(table, index, value)
    }

    /// Get element from table by integer key with __index metamethod support.
    /// Like C Lua's lua_geti. Returns nil if not found.
    pub fn table_geti(&mut self, table: &LuaValue, key: i64) -> LuaResult<LuaValue> {
        let k = LuaValue::integer(key);
        Ok(self.table_get(table, &k)?.unwrap_or(LuaValue::nil()))
    }

    /// Set element in table by integer key with __newindex metamethod support.
    /// Like C Lua's lua_seti.
    pub fn table_seti(&mut self, table: &LuaValue, key: i64, value: LuaValue) -> LuaResult<()> {
        let k = LuaValue::integer(key);
        self.table_set(table, k, value)
    }

    pub fn get_error_msg(&mut self, e: LuaError) -> String {
        match e {
            LuaError::OutOfMemory => {
                format!("out of memory: {}", self.vm_mut().gc.get_error_message())
            }
            _ => {
                // Return just the error message without "Runtime Error: " prefix
                // to match Lua 5.5 behavior (pcall returns the raw error message)
                std::mem::take(&mut self.error_msg)
            }
        }
    }

    // ===== Unprotected Call =====

    /// Unprotected call - like C Lua's lua_call / lua_callk.
    /// Errors propagate as Err(LuaError) to the enclosing pcall boundary.
    /// Does NOT create an error recovery boundary, so __close handlers
    /// see the correct error chain without an extra pcall frame.
    pub fn call(&mut self, func: LuaValue, args: Vec<LuaValue>) -> LuaResult<Vec<LuaValue>> {
        let initial_depth = self.call_depth();
        // Use stack_top (logical top) instead of stack.len() (physical end).
        // The physical stack can be much larger than needed (e.g., after deep
        // recursion tests), so placing the function at stack.len() would waste
        // address space and risk hitting max_stack_size limits unnecessarily.
        let func_idx = self.stack_top;
        let arg_count = args.len();
        let needed = func_idx + 1 + arg_count;

        // Ensure physical stack has room for function + args
        if needed > self.stack.len() {
            self.resize(needed)?;
        }

        // Write function and args at stack_top position
        self.stack[func_idx] = func;
        for (i, arg) in args.into_iter().enumerate() {
            self.stack[func_idx + 1 + i] = arg;
        }
        self.stack_top = needed;

        // Resolve __call metamethod chain if needed
        let (actual_arg_count, ccmt_depth) = resolve_call_chain(self, func_idx, arg_count)?;

        let func_val = self
            .stack_get(func_idx)
            .ok_or_else(|| self.error("call: function not found".to_string()))?;

        if func_val.is_c_callable() {
            // C function - call directly via call_c_function (unprotected)
            call_c_function(self, func_idx, actual_arg_count, -1)?;
        } else {
            // Lua function - push frame and execute
            let base = func_idx + 1;
            self.push_frame(&func_val, base, actual_arg_count, -1)?;

            if ccmt_depth > 0 {
                let frame_idx = self.call_depth - 1;
                if let Some(frame) = self.call_stack.get_mut(frame_idx) {
                    frame.call_status = call_status::set_ccmt_count(frame.call_status, ccmt_depth);
                }
            }

            self.inc_n_ccalls()?;
            let r = lua_execute(self, initial_depth);
            self.dec_n_ccalls();
            r?; // Propagate errors without catching
        }

        // Collect results from func_idx to stack_top
        let mut results = Vec::new();
        for i in func_idx..self.stack_top {
            if let Some(val) = self.stack_get(i) {
                results.push(val);
            }
        }

        // Clean up: nil out used slots for GC safety, restore stack_top.
        // Don't truncate the physical stack — it may be needed by the caller's
        // frame (ci.top). The physical stack will be shrunk naturally by GC or
        // when the enclosing frame exits.
        {
            let clear_end = self.stack_top.min(self.stack.len());
            for i in func_idx..clear_end {
                self.stack[i] = LuaValue::nil();
            }
        }
        self.stack_top = func_idx;

        // Restore caller frame top if needed
        if self.call_depth() > 0 {
            let ci_idx = self.call_depth() - 1;
            let frame_top = self.get_call_info(ci_idx).top as usize;
            if self.stack_top < frame_top {
                self.stack_top = frame_top;
            }
        }

        Ok(results)
    }

    /// Fast comparison call for sort — avoids Vec allocations entirely.
    /// Calls `func(a, b)` and returns whether the result is truthy.
    ///
    /// This is a specialized hot-path for `table.sort` with a custom comparator.
    /// Instead of going through `call()` which allocates two `Vec<LuaValue>` per
    /// invocation (~10,000 comparisons per 1000-element sort = ~20,000 heap allocs),
    /// this writes func+args directly on the stack and reads the boolean result
    /// in-place.
    #[inline]
    pub(crate) fn call_compare(
        &mut self,
        func: LuaValue,
        a: LuaValue,
        b: LuaValue,
    ) -> LuaResult<bool> {
        let initial_depth = self.call_depth();
        let func_idx = self.stack_top;
        let needed = func_idx + 3;

        // Ensure stack has room for func + 2 args
        if needed > self.stack.len() {
            self.resize(needed)?;
        }

        // Place func, a, b directly on stack (zero allocation)
        self.stack[func_idx] = func;
        self.stack[func_idx + 1] = a;
        self.stack[func_idx + 2] = b;
        self.stack_top = needed;

        if func.is_lua_function() {
            // Lua function fast path — skip resolve_call_chain, skip Vec alloc
            let lua_func = unsafe { func.as_lua_function_unchecked() };
            let chunk = lua_func.chunk();
            let base = func_idx + 1;

            self.push_lua_frame(
                base,
                2, // nparams
                1, // nresults — we only need the boolean
                chunk.param_count,
                chunk.max_stack_size,
                chunk as *const _,
                lua_func.upvalues().as_ptr(),
            )?;

            self.inc_n_ccalls()?;
            let r = lua_execute(self, initial_depth);
            self.dec_n_ccalls();
            r?;
        } else if func.is_c_callable() {
            // C function path
            call_c_function(self, func_idx, 2, 1)?;
        } else {
            // Fallback for __call metamethods — rare in sort comparators
            let results = self.call(func, vec![a, b])?;
            return Ok(results.first().map(|v| v.is_truthy()).unwrap_or(false));
        }

        // Result is at stack[func_idx] (placed there by RETURN handler / call_c_function)
        let result = self.stack[func_idx].is_truthy();

        // Clean up: nil out the slot, restore stack_top
        self.stack[func_idx] = LuaValue::nil();
        self.stack_top = func_idx;

        // Restore caller frame top if needed
        if self.call_depth() > 0 {
            let ci_idx = self.call_depth() - 1;
            let frame_top = self.get_call_info(ci_idx).top as usize;
            if self.stack_top < frame_top {
                self.stack_top = frame_top;
            }
        }

        Ok(result)
    }

    // ===== Protected Call (pcall/xpcall) =====

    /// Protected call - execute function with error handling (pcall semantics)
    /// Returns (success, results) where:
    /// - success=true, results=return values
    /// - success=false, results=[error_message]
    /// Note: Yields are NOT caught by pcall - they propagate through
    pub fn pcall(
        &mut self,
        func: LuaValue,
        args: Vec<LuaValue>,
    ) -> LuaResult<(bool, Vec<LuaValue>)> {
        // This is equivalent to C Lua's lua_call → luaD_callnoyield:
        // the callback runs in a non-yieldable context.
        self.nny += 1;
        let result = self.pcall_inner(func, args);
        self.nny -= 1;
        result
    }

    /// Inner implementation of pcall (separated for nny scoping)
    fn pcall_inner(
        &mut self,
        func: LuaValue,
        args: Vec<LuaValue>,
    ) -> LuaResult<(bool, Vec<LuaValue>)> {
        // Save state for cleanup
        let initial_depth = self.call_depth();
        let saved_stack_top = self.stack_top;
        // Use stack_top (logical top) instead of stack.len() (physical end).
        // See call() for rationale.
        let func_idx = self.stack_top;
        let arg_count = args.len();
        let needed = func_idx + 1 + arg_count;

        // Ensure physical stack has room for function + args
        if needed > self.stack.len() {
            self.resize(needed)?;
        }

        // Write function and args at stack_top position
        self.stack[func_idx] = func;
        for (i, arg) in args.into_iter().enumerate() {
            self.stack[func_idx + 1 + i] = arg;
        }

        // Sync logical stack top
        self.stack_top = needed;

        // Resolve __call chain if needed

        let (actual_arg_count, ccmt_depth) = match resolve_call_chain(self, func_idx, arg_count) {
            Ok((count, depth)) => (count, depth),
            Err(e) => {
                let error_msg = self.get_error_msg(e);
                let err_str = self.create_string(&error_msg)?;
                self.stack_top = saved_stack_top;
                return Ok((false, vec![err_str]));
            }
        };

        // Get resolved function
        let func = self
            .stack_get(func_idx)
            .ok_or_else(|| self.error("pcall: function not found".to_string()))?;

        // Check if it's a C function
        let is_c_callable = func.is_c_callable();
        if is_c_callable {
            // Create frame for C function
            let base = func_idx + 1;
            if let Err(e) = self.push_frame(&func, base, actual_arg_count, -1) {
                self.stack_top = saved_stack_top;
                let error_msg = self.get_error_msg(e);
                let err_str = self.create_string(&error_msg)?;
                return Ok((false, vec![err_str]));
            }

            // Set ccmt count in call_status
            if ccmt_depth > 0 {
                let frame_idx = self.call_depth - 1;
                if let Some(frame) = self.call_stack.get_mut(frame_idx) {
                    frame.call_status = call_status::set_ccmt_count(frame.call_status, ccmt_depth);
                }
            }

            // Get the C function pointer
            let cfunc = if let Some(c_func) = func.as_cfunction() {
                c_func
            } else if let Some(closure) = func.as_cclosure() {
                closure.func()
            } else {
                unreachable!()
            };

            // Call C function
            let result = cfunc(self);

            // CloseThread bypasses all pcalls — don't pop this frame,
            // handle_resume_result will pop everything.
            if matches!(result, Err(LuaError::CloseThread)) {
                return Err(LuaError::CloseThread);
            }

            // Pop frame
            self.pop_frame();

            match result {
                Ok(nresults) => {
                    // Success - collect results from stack_top (where push_value writes)
                    let mut results = Vec::new();
                    let result_start = self.stack_top.saturating_sub(nresults);

                    for i in result_start..self.stack_top {
                        if let Some(val) = self.stack_get(i) {
                            results.push(val);
                        }
                    }

                    // Clean up stack
                    self.stack_top = saved_stack_top;

                    Ok((true, results))
                }
                Err(LuaError::Yield) => Err(LuaError::Yield),
                Err(LuaError::CloseThread) => Err(LuaError::CloseThread),
                Err(e) => {
                    let err_obj = std::mem::take(&mut self.error_object);
                    let result_err = if !err_obj.is_nil() {
                        err_obj
                    } else {
                        let error_msg = self.get_error_msg(e);
                        self.create_string(&error_msg)?
                    };
                    self.stack_top = saved_stack_top;
                    Ok((false, vec![result_err]))
                }
            }
        } else {
            // Lua function - use lua_execute
            let base = func_idx + 1;
            // pcall expects all return values
            if let Err(e) = self.push_frame(&func, base, actual_arg_count, -1) {
                self.stack_top = saved_stack_top;
                let error_msg = self.get_error_msg(e);
                let err_str = self.create_string(&error_msg)?;
                return Ok((false, vec![err_str]));
            }

            // Execute via lua_execute — only execute the new frame
            self.inc_n_ccalls()?;
            let result = execute::lua_execute(self, initial_depth);
            self.dec_n_ccalls();

            match result {
                Ok(()) => {
                    // Success - collect return values from stack
                    // Use stack_top (not stack.len()) because RETURN opcodes
                    // set stack_top to reflect actual return values
                    let mut results = Vec::new();
                    for i in func_idx..self.stack_top {
                        if let Some(val) = self.stack_get(i) {
                            results.push(val);
                        }
                    }

                    // Ensure call_depth is back to initial_depth
                    // (normally RETURN should have handled this, but double-check)
                    while self.call_depth() > initial_depth {
                        self.pop_frame();
                    }

                    // Clean up stack
                    self.stack_top = saved_stack_top;

                    Ok((true, results))
                }
                Err(LuaError::Yield) => Err(LuaError::Yield),
                Err(LuaError::CloseThread) => Err(LuaError::CloseThread),
                Err(e) => {
                    // Error occurred - clean up
                    // Lua 5.5 order: L->ci = old_ci first, then closeprotected
                    // This ensures debug.getinfo(2) inside __close sees pcall's caller

                    // Get error object BEFORE closing TBC (close may modify it)
                    let err_obj = std::mem::take(&mut self.error_object);
                    let error_msg_str = self.get_error_msg(e);

                    // Get frame_base before popping frames
                    let frame_base = if self.call_depth() > initial_depth {
                        self.call_stack.get(initial_depth).map(|f| f.base)
                    } else {
                        None
                    };

                    // Pop frames FIRST (like Lua 5.5: L->ci = old_ci)
                    while self.call_depth() > initial_depth {
                        self.pop_frame();
                    }

                    // Then close upvalues and TBC variables
                    if let Some(base) = frame_base {
                        self.close_upvalues(base);
                        // Pass error to TBC close methods
                        // close_tbc_with_error may update error_object if __close cascades
                        let _ = self.close_tbc_with_error(base, err_obj);
                    }

                    // Check if close_tbc_with_error updated error_object (from cascading __close errors)
                    let cascaded_err = std::mem::take(&mut self.error_object);
                    let result_err = if !cascaded_err.is_nil() {
                        cascaded_err
                    } else if !err_obj.is_nil() {
                        err_obj
                    } else {
                        self.create_string(&error_msg_str)?
                    };

                    // Clean up stack
                    self.stack_top = saved_stack_top;

                    Ok((false, vec![result_err]))
                }
            }
        }
    }

    /// Unprotected call with stack-based arguments that supports yields.
    /// Like pcall_stack_based but errors propagate instead of being caught.
    /// Uses CIST_YCALL flag so finish_c_frame can properly move results after yield.
    /// Returns result_count on success; results are left on stack starting at func_idx.
    pub fn call_stack_based(&mut self, func_idx: usize, arg_count: usize) -> LuaResult<usize> {
        let initial_depth = self.call_depth();

        let (actual_arg_count, _ccmt_depth) = resolve_call_chain(self, func_idx, arg_count)?;

        let func = self
            .stack_get(func_idx)
            .ok_or_else(|| self.error("call: function not found".to_string()))?;

        let result = if func.is_c_callable() {
            call_c_function(self, func_idx, actual_arg_count, -1).map(|_| ())
        } else {
            let base = func_idx + 1;
            self.push_frame(&func, base, actual_arg_count, -1)?;
            self.inc_n_ccalls()?;
            let r = lua_execute(self, initial_depth);
            self.dec_n_ccalls();
            r
        };

        match result {
            Ok(()) => {
                let stack_top = self.get_top();
                let result_count = stack_top.saturating_sub(func_idx);
                Ok(result_count)
            }
            Err(LuaError::Yield) => {
                // Mark this C frame with CIST_YCALL so finish_c_frame
                // knows to move results (without prepending true/false).
                if initial_depth > 0 {
                    use crate::lua_vm::call_info::call_status::CIST_YCALL;
                    let frame_idx = initial_depth - 1;
                    if frame_idx < self.call_depth {
                        let ci = self.get_call_info_mut(frame_idx);
                        ci.call_status |= CIST_YCALL;
                    }
                }
                Err(LuaError::Yield)
            }
            Err(e) => Err(e), // Propagate all other errors
        }
    }

    /// Protected call with stack-based arguments (zero-allocation fast path)
    /// Args are already on stack at [arg_base, arg_base+arg_count)
    /// Returns (success, result_count) where results are left on stack
    pub fn pcall_stack_based(
        &mut self,
        func_idx: usize,
        arg_count: usize,
    ) -> LuaResult<(bool, usize)> {
        // Save current call stack depth
        let initial_depth = self.call_depth();

        // Resolve __call metamethod chain if needed

        let (actual_arg_count, ccmt_depth) = match resolve_call_chain(self, func_idx, arg_count) {
            Ok((count, depth)) => (count, depth),
            Err(e) => {
                // __call resolution failed - return error
                let error_msg = self.get_error_msg(e);
                let err_str = self.create_string(&error_msg)?;
                self.stack_set(func_idx, err_str)?;
                self.set_top(func_idx + 1)?;
                return Ok((false, 1));
            }
        };

        // Now func_idx contains a real callable (after __call resolution)
        let func = self
            .stack_get(func_idx)
            .ok_or_else(|| self.error("pcall: function not found after resolution".to_string()))?;

        // Call the function using the internal call machinery
        let result = if func.is_c_callable() {
            // C function - call directly
            call_c_function(
                self,
                func_idx,
                actual_arg_count,
                -1, // MULTRET - want all results
            )
            .map(|_| ())
        } else {
            // Lua function - push frame and execute, expecting all return values
            let base = func_idx + 1;
            self.push_frame(&func, base, actual_arg_count, -1)?;

            // Set ccmt count in call_status for Lua functions too
            if ccmt_depth > 0 {
                use crate::lua_vm::call_info::call_status;
                let frame_idx = self.call_depth - 1;
                if let Some(frame) = self.call_stack.get_mut(frame_idx) {
                    frame.call_status = call_status::set_ccmt_count(frame.call_status, ccmt_depth);
                }
            }

            self.inc_n_ccalls()?;
            let r = lua_execute(self, initial_depth);
            self.dec_n_ccalls();
            r
        };

        match result {
            Ok(()) => {
                // Success - count results from func_idx to logical stack top
                let stack_top = self.get_top();
                let result_count = stack_top.saturating_sub(func_idx);
                Ok((true, result_count))
            }
            Err(LuaError::Yield) => {
                // Mark pcall's own C frame with CIST_YPCALL so that
                // finish_c_frame knows to wrap results with true on resume.
                // pcall's C frame is at initial_depth - 1.
                if initial_depth > 0 {
                    use crate::lua_vm::call_info::call_status::CIST_YPCALL;
                    let pcall_frame_idx = initial_depth - 1;
                    if pcall_frame_idx < self.call_depth {
                        let ci = self.get_call_info_mut(pcall_frame_idx);
                        ci.call_status |= CIST_YPCALL;
                    }
                }
                Err(LuaError::Yield)
            }
            Err(LuaError::CloseThread) => {
                // CloseThread bypasses all pcalls — propagate to resume
                Err(LuaError::CloseThread)
            }
            Err(LuaError::ErrorInErrorHandling) => {
                // Stack overflow in error handler zone - C Lua's stackerror
                let frame_base = if self.call_depth() > initial_depth {
                    self.call_stack.get(initial_depth).map(|f| f.base)
                } else {
                    None
                };
                while self.call_depth() > initial_depth {
                    self.pop_frame();
                }
                if let Some(base) = frame_base {
                    self.close_upvalues(base);
                    let _ = self.close_tbc_with_error(base, LuaValue::nil());
                }
                let err_msg = self.create_string("error in error handling")?;
                self.stack_set(func_idx, err_msg)?;
                self.set_top(func_idx + 1)?;
                Ok((false, 1))
            }
            Err(e) => {
                // Error - clean up and return error
                // Lua 5.5 order: pop frames first, then close TBC

                // Get error object BEFORE closing TBC
                let err_obj = std::mem::take(&mut self.error_object);
                let error_msg_str = self.get_error_msg(e);

                // Get frame_base before popping frames
                let frame_base = if self.call_depth() > initial_depth {
                    self.call_stack.get(initial_depth).map(|f| f.base)
                } else {
                    None
                };

                // Pop frames FIRST (like Lua 5.5: L->ci = old_ci)
                while self.call_depth() > initial_depth {
                    self.pop_frame();
                }

                // Then close upvalues and TBC variables
                if let Some(base) = frame_base {
                    self.close_upvalues(base);
                    let close_result = self.close_tbc_with_error(base, err_obj);
                    match close_result {
                        Ok(()) => {} // continue to set up error result below
                        Err(LuaError::Yield) => {
                            // TBC close yielded during error recovery.
                            // Save state: mark pcall's C frame with CIST_YPCALL + CIST_RECST
                            // so finish_c_frame will handle error result on resume.
                            let pcall_ci_idx = initial_depth - 1;
                            if pcall_ci_idx < self.call_depth {
                                let ci = self.get_call_info_mut(pcall_ci_idx);
                                ci.call_status |= CIST_YPCALL | CIST_RECST;
                            }
                            // Save error value (may have cascaded) for finish_c_frame
                            let cascaded = std::mem::take(&mut self.error_object);
                            self.error_object = if !cascaded.is_nil() {
                                cascaded
                            } else {
                                err_obj
                            };
                            return Err(LuaError::Yield);
                        }
                        Err(_e2) => {
                            // TBC close threw — use the new error
                            // Fall through to set up error result below
                        }
                    }
                }

                // Check if close_tbc_with_error updated error_object (cascading)
                let cascaded_err = std::mem::take(&mut self.error_object);
                let result_err = if !cascaded_err.is_nil() {
                    cascaded_err
                } else if !err_obj.is_nil() {
                    err_obj
                } else {
                    self.create_string(&error_msg_str)?
                };

                // Set error at func_idx and update stack top
                self.stack_set(func_idx, result_err)?;
                self.set_top(func_idx + 1)?;

                Ok((false, 1))
            }
        }
    }

    /// Protected call with error handler, stack-based (xpcall semantics).
    /// Like pcall_stack_based but calls the error handler at `handler_idx`
    /// BEFORE unwinding call frames, so debug.traceback can see the full stack.
    /// Returns (success, result_count) where results are left on stack at func_idx.
    pub fn xpcall_stack_based(
        &mut self,
        func_idx: usize,
        arg_count: usize,
        handler_idx: usize,
    ) -> LuaResult<(bool, usize)> {
        let initial_depth = self.call_depth();

        let (actual_arg_count, ccmt_depth) = match resolve_call_chain(self, func_idx, arg_count) {
            Ok((count, depth)) => (count, depth),
            Err(e) => {
                let error_msg = self.get_error_msg(e);
                let err_str = self.create_string(&error_msg)?;
                self.stack_set(func_idx, err_str)?;
                self.set_top(func_idx + 1)?;
                return Ok((false, 1));
            }
        };

        let func = self
            .stack_get(func_idx)
            .ok_or_else(|| self.error("xpcall: function not found after resolution".to_string()))?;

        let result = if func.is_c_callable() {
            call_c_function(self, func_idx, actual_arg_count, -1).map(|_| ())
        } else {
            let base = func_idx + 1;
            self.push_frame(&func, base, actual_arg_count, -1)?;
            if ccmt_depth > 0 {
                let frame_idx = self.call_depth - 1;
                if let Some(frame) = self.call_stack.get_mut(frame_idx) {
                    frame.call_status = call_status::set_ccmt_count(frame.call_status, ccmt_depth);
                }
            }
            self.inc_n_ccalls()?;
            let r = lua_execute(self, initial_depth);
            self.dec_n_ccalls();
            r
        };

        match result {
            Ok(()) => {
                let stack_top = self.get_top();
                let result_count = stack_top.saturating_sub(func_idx);
                Ok((true, result_count))
            }
            Err(LuaError::Yield) => {
                if initial_depth > 0 {
                    let pcall_frame_idx = initial_depth - 1;
                    if pcall_frame_idx < self.call_depth {
                        let ci = self.get_call_info_mut(pcall_frame_idx);
                        ci.call_status |= CIST_YPCALL;
                    }
                }
                Err(LuaError::Yield)
            }
            Err(LuaError::CloseThread) => Err(LuaError::CloseThread),
            Err(LuaError::ErrorInErrorHandling) => {
                // Stack overflow in error handler zone - skip handler entirely
                let frame_base = if self.call_depth() > initial_depth {
                    self.call_stack.get(initial_depth).map(|f| f.base)
                } else {
                    None
                };
                while self.call_depth() > initial_depth {
                    self.pop_frame();
                }
                if let Some(base) = frame_base {
                    self.close_upvalues(base);
                    let _ = self.close_tbc_with_error(base, LuaValue::nil());
                }
                let err_msg = self.create_string("error in error handling")?;
                self.stack_set(func_idx, err_msg)?;
                self.set_top(func_idx + 1)?;
                Ok((false, 1))
            }
            Err(e) => {
                // Get error object BEFORE any cleanup
                let err_obj = std::mem::take(&mut self.error_object);
                let error_msg_str = self.get_error_msg(e);

                let mut err_value = if !err_obj.is_nil() {
                    err_obj
                } else {
                    self.create_string(&error_msg_str)?
                };

                let frame_base = if self.call_depth() > initial_depth {
                    self.call_stack.get(initial_depth).map(|f| f.base)
                } else {
                    None
                };

                // Temporarily increase max_c_stack_depth and max_call_depth
                // for error handler, so it can run even after stack overflow.
                // This mirrors C Lua's approach of allowing extra headroom
                // during error handling.
                let saved_max_c_depth = self.safe_state.max_c_stack_depth;
                let saved_max_call_depth = self.safe_state.max_call_depth;
                self.safe_state.max_c_stack_depth = saved_max_c_depth + CSTACKERR;
                self.safe_state.max_call_depth = saved_max_call_depth + CSTACKERR;

                // Call error handler WITH ALL ERROR FRAMES STILL ON STACK.
                // C Lua's luaG_errormsg recursively calls the handler when the
                // handler itself errors. We implement this as a loop.
                // Track accumulated "virtual depth" to simulate C Lua's nCcalls
                // accumulation during recursive handler calls.
                let handler = self.stack_get(handler_idx).unwrap_or_default();

                let mut handler_failed = false;
                let mut transformed_error = LuaValue::nil();

                // Budget: how many retries before we hit the depth limit.
                // In C Lua, each recursive handler call adds ~1 to nCcalls
                // (truly recursive via luaG_errormsg).
                // At MAXCCALLS (200), "C stack overflow" fires.
                // At MAXCCALLS+CSTACKERR (230), hard "error in error handling" fires.
                //
                // Our handler loop does NOT actually recurse (each handler call
                // is a single lua_execute that returns), so we don't consume
                // real Rust stack depth across iterations.  Use LUAI_MAXCSTACK
                // (the C Lua standard 200) for the budget simulation, NOT the
                // runtime max_c_stack_depth which may be reduced (e.g. 25 in
                // debug builds for Rust stack safety).
                let current_n_ccalls = unsafe { (*self.vm).n_ccalls };
                let depth_budget = LUAI_MAXCSTACK.saturating_sub(current_n_ccalls);
                let hard_limit = depth_budget + CSTACKERR; // extra room for error handling
                let mut retry_count: usize = 0;

                loop {
                    retry_count += 1;

                    // Hard limit: too many retries even in error zone
                    if retry_count > hard_limit {
                        handler_failed = true;
                        break;
                    }

                    // Soft limit: generate "C stack overflow" error for handler
                    if retry_count > depth_budget {
                        let overflow_str = self.create_string("C stack overflow")?;
                        err_value = overflow_str;
                    }

                    let current_top = self.stack_top;
                    self.push_value(handler)?;
                    let handler_func_idx = current_top;
                    self.push_value(err_value)?;

                    let handler_depth = self.call_depth();

                    let handler_result =
                        if handler.is_cfunction() || handler.as_cclosure().is_some() {
                            call_c_function(self, handler_func_idx, 1, -1)
                        } else {
                            match self.push_frame(&handler, handler_func_idx + 1, 1, -1) {
                                Ok(()) => {
                                    self.inc_n_ccalls()?;
                                    let r = lua_execute(self, handler_depth);
                                    self.dec_n_ccalls();
                                    r
                                }
                                Err(handler_err) => Err(handler_err),
                            }
                        };

                    match handler_result {
                        Ok(_) => {
                            transformed_error =
                                self.stack_get(handler_func_idx).unwrap_or_default();
                            while self.call_depth() > handler_depth {
                                self.pop_frame();
                            }
                            break;
                        }
                        Err(LuaError::ErrorInErrorHandling) => {
                            handler_failed = true;
                            self.error_object = LuaValue::nil();
                            while self.call_depth() > handler_depth {
                                self.pop_frame();
                            }
                            self.set_top(current_top)?;
                            break;
                        }
                        Err(_handler_err) => {
                            // Handler failed with normal error — retry with new error
                            let new_err = std::mem::take(&mut self.error_object);
                            while self.call_depth() > handler_depth {
                                self.pop_frame();
                            }
                            self.set_top(current_top)?;
                            if new_err.is_nil() {
                                handler_failed = true;
                                break;
                            }
                            err_value = new_err;
                        }
                    }
                }

                // Restore max_c_stack_depth and max_call_depth
                self.safe_state.max_c_stack_depth = saved_max_c_depth;
                self.safe_state.max_call_depth = saved_max_call_depth;

                // NOW pop error frames (after handler has seen them)
                while self.call_depth() > initial_depth {
                    self.pop_frame();
                }

                // Close upvalues and TBC
                if let Some(base) = frame_base {
                    self.close_upvalues(base);
                    let close_result = self.close_tbc_with_error(base, err_obj);
                    match close_result {
                        Ok(()) => {}
                        Err(LuaError::Yield) => {
                            let pcall_ci_idx = initial_depth - 1;
                            if pcall_ci_idx < self.call_depth {
                                let ci = self.get_call_info_mut(pcall_ci_idx);
                                ci.call_status |= CIST_YPCALL | CIST_RECST;
                            }
                            let cascaded = std::mem::take(&mut self.error_object);
                            self.error_object = if !cascaded.is_nil() {
                                cascaded
                            } else {
                                err_value
                            };
                            return Err(LuaError::Yield);
                        }
                        Err(_e2) => {}
                    }
                }

                // Determine final error value
                let final_error = if handler_failed {
                    self.create_string("error in error handling")?
                } else {
                    transformed_error
                };

                self.stack_set(func_idx, final_error)?;
                self.set_top(func_idx + 1)?;

                Ok((false, 1))
            }
        }
    }

    /// Protected call with error handler (xpcall semantics)
    /// The error handler is called if an error occurs
    /// Returns (success, results)
    pub fn xpcall(
        &mut self,
        func: LuaValue,
        args: Vec<LuaValue>,
        err_handler: LuaValue,
    ) -> LuaResult<(bool, Vec<LuaValue>)> {
        // Save error handler and function on stack
        let handler_idx = self.stack_top;
        self.push_value(err_handler)?;

        let initial_depth = self.call_depth();
        let func_idx = self.stack_top;
        self.push_value(func)?;

        for arg in args {
            self.push_value(arg)?;
        }
        let arg_count = self.stack_top - func_idx - 1;

        // Resolve __call chain if needed

        let (actual_arg_count, ccmt_depth) = match resolve_call_chain(self, func_idx, arg_count) {
            Ok((count, depth)) => (count, depth),
            Err(e) => {
                self.set_top(handler_idx)?;
                let error_msg = self.get_error_msg(e);
                let err_str = self.create_string(&error_msg)?;
                return Ok((false, vec![err_str]));
            }
        };

        // Get resolved function
        let func = self
            .stack_get(func_idx)
            .ok_or_else(|| self.error("xpcall: function not found".to_string()))?;

        let is_c_callable = func.is_c_callable();

        // Execute the function
        let result = if is_c_callable {
            // C function — call directly (like pcall_inner's C path)
            let base = func_idx + 1;
            if let Err(e) = self.push_frame(&func, base, actual_arg_count, -1) {
                self.set_top(handler_idx)?;
                let error_msg = self.get_error_msg(e);
                let err_str = self.create_string(&error_msg)?;
                return Ok((false, vec![err_str]));
            }

            if ccmt_depth > 0 {
                use crate::lua_vm::call_info::call_status;
                let frame_idx = self.call_depth - 1;
                if let Some(frame) = self.call_stack.get_mut(frame_idx) {
                    frame.call_status = call_status::set_ccmt_count(frame.call_status, ccmt_depth);
                }
            }

            let cfunc = if let Some(c_func) = func.as_cfunction() {
                c_func
            } else if let Some(closure) = func.as_cclosure() {
                closure.func()
            } else {
                unreachable!()
            };

            let c_result = cfunc(self);

            // CloseThread bypasses everything
            if matches!(c_result, Err(LuaError::CloseThread)) {
                return Err(LuaError::CloseThread);
            }

            self.pop_frame();

            match c_result {
                Ok(nresults) => {
                    // Collect results
                    let result_start = self.stack_top.saturating_sub(nresults);
                    // Move results to func_idx
                    for i in 0..nresults {
                        let val = self.stack_get(result_start + i).unwrap_or_default();
                        self.stack_set(func_idx + i, val)?;
                    }
                    self.set_top(func_idx + nresults)?;
                    Ok(())
                }
                Err(LuaError::Yield) => Err(LuaError::Yield),
                Err(e) => Err(e),
            }
        } else {
            // Lua function — push frame and execute
            let base = func_idx + 1;
            if let Err(e) = self.push_frame(&func, base, actual_arg_count, -1) {
                self.set_top(handler_idx)?;
                let error_msg = self.get_error_msg(e);
                let err_str = self.create_string(&error_msg)?;
                return Ok((false, vec![err_str]));
            }

            if ccmt_depth > 0 {
                use crate::lua_vm::call_info::call_status;
                let frame_idx = self.call_depth - 1;
                if let Some(frame) = self.call_stack.get_mut(frame_idx) {
                    frame.call_status = call_status::set_ccmt_count(frame.call_status, ccmt_depth);
                }
            }

            self.inc_n_ccalls()?;
            let r = execute::lua_execute(self, initial_depth);
            self.dec_n_ccalls();
            r
        };

        match result {
            Ok(()) => {
                // Success - collect results
                // Execution (via RETURN) sets stack_top to end of results
                // Results start at func_idx (replacing func and args)
                let mut results = Vec::new();
                let top = self.stack_top;

                if top > func_idx {
                    for i in func_idx..top {
                        if let Some(val) = self.stack_get(i) {
                            results.push(val);
                        }
                    }
                }

                // Ensure call_depth is back to initial_depth
                // (normally RETURN should have handled this, but double-check)
                while self.call_depth() > initial_depth {
                    self.pop_frame();
                }

                self.set_top(handler_idx)?;
                Ok((true, results))
            }
            Err(LuaError::Yield) => Err(LuaError::Yield),
            Err(LuaError::CloseThread) => Err(LuaError::CloseThread),
            Err(LuaError::ErrorInErrorHandling) => {
                // Stack overflow in error handler zone - skip handler entirely
                let frame_base = if self.call_depth() > initial_depth {
                    self.call_stack.get(initial_depth).map(|f| f.base)
                } else {
                    None
                };
                while self.call_depth() > initial_depth {
                    self.pop_frame();
                }
                if let Some(base) = frame_base {
                    self.close_upvalues(base);
                    let _ = self.close_tbc_with_error(base, LuaValue::nil());
                }
                let results = vec![self.create_string("error in error handling")?];
                self.set_top(handler_idx)?;
                Ok((false, results))
            }
            Err(e) => {
                // so that debug.traceback can see the full call stack
                // (mirrors CLua's luaG_errormsg which calls handler before longjmp)

                // Get error object BEFORE any cleanup
                let err_obj = std::mem::take(&mut self.error_object);
                let error_msg_str = self.get_error_msg(e);

                // Prepare error value for the handler
                let mut err_value = if !err_obj.is_nil() {
                    err_obj
                } else {
                    self.create_string(&error_msg_str)?
                };

                // Get frame_base for later cleanup (upvalues/TBC)
                let frame_base = if self.call_depth() > initial_depth {
                    self.call_stack.get(initial_depth).map(|f| f.base)
                } else {
                    None
                };

                // Temporarily increase max_c_stack_depth for error handler
                // (like CLua's CSTACKERR — allows error handlers to run
                // even after stack overflow)
                let saved_max_c_depth = self.safe_state.max_c_stack_depth;
                self.safe_state.max_c_stack_depth = saved_max_c_depth + CSTACKERR;

                // Call error handler with error value.
                // C Lua's luaG_errormsg recursively calls the handler when the
                // handler itself errors. We implement this as a loop: if the
                // handler fails with a normal error, retry with the new error.
                let handler = self.stack_get(handler_idx).unwrap_or_default();

                let mut results = Vec::new();
                let mut handler_failed = false;

                loop {
                    let current_top = self.stack_top;
                    self.push_value(handler)?;
                    let handler_func_idx = current_top;
                    self.push_value(err_value)?;

                    let handler_depth = self.call_depth();

                    let handler_result =
                        if handler.is_cfunction() || handler.as_cclosure().is_some() {
                            execute::call::call_c_function(self, handler_func_idx, 1, -1)
                        } else {
                            match self.push_frame(&handler, handler_func_idx + 1, 1, -1) {
                                Ok(()) => {
                                    self.inc_n_ccalls()?;
                                    let r = execute::lua_execute(self, handler_depth);
                                    self.dec_n_ccalls();
                                    r
                                }
                                Err(handler_err) => Err(handler_err),
                            }
                        };

                    match handler_result {
                        Ok(()) => {
                            // Handler succeeded — collect results
                            let result_top = self.stack_top;
                            if result_top > handler_func_idx {
                                for i in handler_func_idx..result_top {
                                    if let Some(val) = self.stack_get(i) {
                                        results.push(val);
                                    }
                                }
                            }
                            // Clean up handler frames
                            while self.call_depth() > handler_depth {
                                self.pop_frame();
                            }
                            break;
                        }
                        Err(LuaError::ErrorInErrorHandling) => {
                            // Stack overflow in error handler zone — give up
                            handler_failed = true;
                            self.error_object = LuaValue::nil();
                            while self.call_depth() > handler_depth {
                                self.pop_frame();
                            }
                            self.set_top(current_top)?;
                            break;
                        }
                        Err(_handler_err) => {
                            // Handler failed with a normal error.
                            // C Lua retries: luaG_errormsg calls errfunc again.
                            // Get the new error value and retry.
                            let new_err = std::mem::take(&mut self.error_object);
                            // Clean up handler frames before retrying
                            while self.call_depth() > handler_depth {
                                self.pop_frame();
                            }
                            // Reset stack top to before handler push
                            self.set_top(current_top)?;
                            if new_err.is_nil() {
                                // No error object — can't retry, treat as failure
                                handler_failed = true;
                                break;
                            }
                            err_value = new_err;
                            // Loop continues — retry handler with new error value
                        }
                    }
                }

                // Restore max_c_stack_depth after error handler completes
                self.safe_state.max_c_stack_depth = saved_max_c_depth;

                // NOW pop the error frames (after handler has seen them)
                while self.call_depth() > initial_depth {
                    self.pop_frame();
                }

                // Close upvalues and TBC
                if let Some(base) = frame_base {
                    self.close_upvalues(base);
                    let _ = self.close_tbc_with_error(base, err_obj);
                }

                // Check for cascading error from TBC
                let cascaded_err = std::mem::take(&mut self.error_object);
                if !cascaded_err.is_nil() && results.is_empty() {
                    if let Some(s) = cascaded_err.as_str() {
                        results.push(self.create_string(s)?);
                    } else {
                        results.push(cascaded_err);
                    }
                }

                if results.is_empty() {
                    if handler_failed {
                        results.push(self.create_string("error in error handling")?);
                    } else {
                        results.push(self.create_string(&error_msg_str)?);
                    }
                }

                self.set_top(handler_idx)?;
                Ok((false, results))
            }
        }
    }

    // ===== Coroutine Support (resume/yield) =====

    /// Resume a coroutine (should be called on the thread's LuaState)
    /// Returns (finished, results) where:
    /// - finished=true: coroutine completed normally
    /// - finished=false: coroutine yielded
    pub fn resume(&mut self, args: Vec<LuaValue>) -> LuaResult<(bool, Vec<LuaValue>)> {
        // Check coroutine state:
        // - dead flag set → dead by error (cannot resume)
        // - call_depth > 0 && !yielded → running (cannot resume)
        // - call_depth > 0 && yielded → suspended after yield (can resume)
        // - call_depth == 0 && stack not empty → initial state (can resume)
        // - call_depth == 0 && stack empty → dead (cannot resume)
        if self.dead {
            // Clear stale error_object so that this error uses the fresh
            // error_msg set by self.error(), rather than a leftover
            // error_object from a previous failed resume.
            self.error_object = LuaValue::nil();
            return Err(self.error("cannot resume dead coroutine".to_string()));
        }
        if self.call_depth > 0 && !self.yielded {
            self.error_object = LuaValue::nil();
            return Err(self.error("cannot resume non-suspended coroutine".to_string()));
        }

        // Mark as running (not yielded)
        self.yielded = false;

        // Check if this is the first resume (no active frames)
        if self.call_depth == 0 {
            // Initial resume - need to set up the function
            // The function should be at stack[0] (set by create_thread)
            if self.stack.is_empty() {
                self.error_object = LuaValue::nil(); // clear stale error object
                return Err(self.error("cannot resume dead coroutine".to_string()));
            }

            let func = self.stack[0];

            // Push arguments
            let old_capacity = self.stack.capacity();
            for arg in args {
                self.stack.push(arg);
            }
            // Fix open upvalue pointers if Vec was reallocated
            if self.stack.capacity() != old_capacity {
                self.fix_open_upvalue_pointers();
            }

            // Create initial frame, expecting all return values
            let nargs = self.stack.len() - 1; // -1 for function itself
            let base = 1; // Arguments start at index 1 (function is at 0)
            self.push_frame(&func, base, nargs, -1)?;

            // Execute until yield or completion
            let result = if func.is_c_callable() {
                // Call C function directly
                execute::call::call_c_function(self, 0, nargs, -1)
            } else {
                // Execute Lua bytecode
                self.inc_n_ccalls()?;
                let r = execute::lua_execute(self, 0);
                self.dec_n_ccalls();
                r
            };

            self.handle_resume_result(result)
        } else {
            // Resuming after yield
            // The yield function's frame is still on the stack, we need to:
            // 1. Pop the yield frame
            // 2. Place resume arguments as yield's return values (with proper adjustment)
            // 3. Continue execution from the caller's frame
            // NOTE: Do NOT close upvalues or TBC variables on yield resume!
            // Yield is not an exit — variables are still alive.

            // Get the yield frame info before popping
            let (func_idx, nresults) = if let Some(frame) = self.current_frame() {
                // func_idx is base - func_offset (where the yield function was called)
                (frame.base - frame.func_offset as usize, frame.nresults())
            } else {
                return Err(self.error("cannot resume: no frame".to_string()));
            };

            // Pop the yield frame
            self.pop_frame();

            // Place resume arguments at func_idx as yield's return values.
            // This simulates the yield function returning normally.
            // Like C Lua's luaD_poscall, we must respect nresults from the
            // CALL instruction to avoid stale stack values leaking as extra
            // return values.
            let actual_nresults = args.len();
            for (i, arg) in args.into_iter().enumerate() {
                if nresults >= 0 && i >= nresults as usize {
                    break; // Don't place more than requested
                }
                self.stack_set(func_idx + i, arg)?;
            }

            // Adjust stack top based on expected nresults
            if nresults >= 0 {
                let wanted = nresults as usize;
                // Nil-pad if fewer results than expected
                for i in actual_nresults..wanted {
                    self.stack_set(func_idx + i, LuaValue::nil())?;
                }
                // Restore caller frame's top (matching call_c_function behavior)
                if self.call_depth() > 0 {
                    let ci_top = self.get_call_info(self.call_depth() - 1).top as usize;
                    self.set_top_raw(ci_top);
                } else {
                    self.set_top_raw(func_idx + wanted);
                }
            } else {
                // MULTRET: top = func_idx + actual results
                let new_top = func_idx + actual_nresults;
                self.set_top_raw(new_top);
            }

            // Execute until yield or completion
            self.inc_n_ccalls()?;
            let result = execute::lua_execute(self, 0);
            self.dec_n_ccalls();

            // Handle result with pcall error recovery (precover)
            self.handle_resume_result(result)
        }
    }

    /// Handle the result of lua_execute during resume.
    /// Implements Lua 5.5's precover: when an error occurs, search the
    /// call stack for a pcall frame (CIST_YPCALL), recover there, and
    /// continue execution.
    fn handle_resume_result(
        &mut self,
        initial_result: LuaResult<()>,
    ) -> LuaResult<(bool, Vec<LuaValue>)> {
        let mut result = initial_result;

        loop {
            match result {
                Ok(()) => {
                    // Coroutine completed — pop any remaining frames
                    // (e.g., the initial frame pushed by resume for C functions)
                    let results = self.get_all_return_values(0);
                    while self.call_depth() > 0 {
                        self.pop_frame();
                    }
                    self.stack.clear();
                    self.stack_top = 0;
                    return Ok((true, results));
                }
                Err(LuaError::Yield) => {
                    // Coroutine yielded — mark as yielded for resume detection
                    self.yielded = true;
                    let yield_vals = self.take_yield();
                    return Ok((false, yield_vals));
                }
                Err(LuaError::CloseThread) => {
                    // Self-close: coroutine.close() closed TBC vars/upvalues
                    // and threw CloseThread to bypass all pcalls.
                    // Pop all remaining frames and mark thread as dead.
                    while self.call_depth() > 0 {
                        self.pop_frame();
                    }
                    // Check if __close set an error
                    let err_obj = std::mem::take(&mut self.error_object);
                    self.stack.clear();
                    self.stack_top = 0;
                    if err_obj.is_nil() {
                        // Normal close — success with no return values
                        return Ok((true, vec![]));
                    } else {
                        // __close errored — coroutine dies with error.
                        // Store error back so coroutine_resume can retrieve it.
                        self.error_object = err_obj;
                        return Err(LuaError::RuntimeError);
                    }
                }
                Err(_e) => {
                    // Error — try to find a pcall frame to recover
                    let pcall_idx = self.find_pcall_recovery_frame();
                    if pcall_idx.is_none() {
                        // No recovery point — coroutine dies.
                        // Keep stack and call frames intact for debug.traceback
                        // (matching C Lua behavior: dead coroutines retain their
                        // call stack for inspection).
                        let err_obj = std::mem::take(&mut self.error_object);
                        let error_val = if !err_obj.is_nil() {
                            err_obj
                        } else {
                            LuaValue::nil()
                        };

                        // Close all upvalues and TBC variables from level 0
                        self.close_upvalues(0);
                        let _ = self.close_tbc_with_error(0, error_val);

                        // Restore error state: if close cascaded, error_object
                        // is already set by close_tbc_with_error. If not, restore
                        // the original error value and msg.
                        if self.error_object.is_nil() {
                            self.error_object = error_val;
                        } else {
                            // Cascaded error — update error_msg to match
                            self.error_msg = format!("{}", self.error_object);
                        }

                        // Mark coroutine as dead but keep stack for debug.traceback
                        self.dead = true;

                        return Err(_e);
                    }
                    let pcall_frame_idx = pcall_idx.unwrap();

                    // Get pcall's info before cleanup
                    let pcall_ci = self.get_call_info(pcall_frame_idx);
                    let pcall_func_pos = pcall_ci.base - pcall_ci.func_offset as usize;
                    let pcall_nresults = pcall_ci.nresults();
                    let close_level = pcall_ci.base; // close from body position
                    let is_xpcall = pcall_ci.call_status & CIST_XPCALL != 0;

                    // Save the xpcall handler before anything overwrites it
                    let xpcall_handler = if is_xpcall {
                        self.stack_get(pcall_func_pos).unwrap_or_default()
                    } else {
                        LuaValue::nil()
                    };

                    // Get error object
                    let err_obj = std::mem::take(&mut self.error_object);
                    let error_val = if !err_obj.is_nil() {
                        err_obj
                    } else {
                        let msg = self.error_msg.clone();
                        self.create_string(&msg).unwrap_or_default()
                    };
                    self.clear_error();

                    // Pop frames down to pcall (exclusive — keep pcall's frame temporarily)
                    while self.call_depth() > pcall_frame_idx + 1 {
                        self.pop_frame();
                    }

                    // Close upvalues
                    self.close_upvalues(close_level);

                    // Close TBC with error (may yield or throw again)
                    let close_result = self.close_tbc_with_error(close_level, error_val);

                    match close_result {
                        Ok(()) => {
                            // Get the final error (might be cascaded from TBC closes)
                            let final_err = std::mem::take(&mut self.error_object);
                            let result_err = if !final_err.is_nil() {
                                final_err
                            } else {
                                error_val
                            };
                            self.clear_error();

                            // If xpcall, call error handler to transform the error
                            let result_err = if is_xpcall {
                                self.nny += 1;
                                let handler_result = self.pcall(xpcall_handler, vec![result_err]);
                                self.nny -= 1;
                                match handler_result {
                                    Ok((true, results)) => {
                                        results.into_iter().next().unwrap_or(LuaValue::nil())
                                    }
                                    _ => self.create_string("error in error handling")?,
                                }
                            } else {
                                result_err
                            };

                            // Set up pcall error result: (false, error)
                            self.stack_set(pcall_func_pos, LuaValue::boolean(false))
                                .ok();
                            self.stack_set(pcall_func_pos + 1, result_err).ok();
                            let n = 2;

                            // Pop pcall frame
                            self.pop_frame();

                            // Handle nresults like call_c_function post-processing
                            let final_n = if pcall_nresults == -1 {
                                n
                            } else {
                                pcall_nresults as usize
                            };
                            let new_top = pcall_func_pos + final_n;
                            if pcall_nresults >= 0 {
                                let wanted = pcall_nresults as usize;
                                for i in n..wanted {
                                    self.stack_set(pcall_func_pos + i, LuaValue::nil()).ok();
                                }
                            }
                            self.set_top_raw(new_top);

                            // Restore caller frame top
                            if self.call_depth() > 0 {
                                let ci_idx = self.call_depth() - 1;
                                if pcall_nresults == -1 {
                                    let ci_top = self.get_call_info(ci_idx).top as usize;
                                    if ci_top < new_top {
                                        self.get_call_info_mut(ci_idx).top = new_top as u32;
                                    }
                                } else {
                                    let frame_top = self.get_call_info(ci_idx).top as usize;
                                    self.set_top_raw(frame_top);
                                }
                            }

                            // Continue execution
                            if let Err(e) = self.inc_n_ccalls() {
                                result = Err(e);
                            } else {
                                result = execute::lua_execute(self, 0);
                                self.dec_n_ccalls();
                            }
                            // Loop again to check for more errors/yields
                        }
                        Err(LuaError::Yield) => {
                            // TBC close yielded during error recovery.
                            // Save recovery state: mark pcall frame with CIST_RECST
                            // and store the error value in error_object.
                            // When the close method finishes, finish_c_frame will
                            // detect CIST_RECST and set up (false, error) result.
                            use crate::lua_vm::call_info::call_status::CIST_RECST;
                            if pcall_frame_idx < self.call_depth() {
                                let ci = self.get_call_info_mut(pcall_frame_idx);
                                ci.call_status |= CIST_RECST;
                            }
                            // Store the error value for finish_c_frame to retrieve later
                            self.error_object = error_val;

                            // Mark coroutine as yielded so resume works
                            self.yielded = true;

                            // Return yield values normally
                            let yield_vals = self.take_yield();
                            return Ok((false, yield_vals));
                        }
                        Err(_e2) => {
                            // TBC close threw again — try to recover with updated error
                            result = Err(_e2);
                            // Loop continues to find next pcall frame
                        }
                    }
                }
            }
        }
    }

    /// Find a pcall C frame (CIST_YPCALL) on the call stack for error recovery.
    /// Returns the frame index if found.
    fn find_pcall_recovery_frame(&self) -> Option<usize> {
        for i in (0..self.call_depth()).rev() {
            let ci = self.get_call_info(i);
            if ci.call_status & CIST_YPCALL != 0 {
                return Some(i);
            }
        }
        None
    }

    /// Yield from current coroutine
    /// This should be called by Lua code via coroutine.yield
    pub fn do_yield(&mut self, values: Vec<LuaValue>) -> LuaResult<()> {
        self.set_yield(values);
        Err(LuaError::Yield)
    }

    // ============ GC Barriers ============

    /// Forward GC barrier (luaC_barrier in Lua 5.5)
    /// Called when modifying an object to point to another object
    #[inline(always)]
    pub fn gc_barrier(&mut self, upvalue_ptr: UpvaluePtr, value_gc_ptr: GcObjectPtr) {
        let vm = unsafe { &mut *self.vm };
        let owner_ptr = GcObjectPtr::from(upvalue_ptr);
        vm.gc.barrier(self, owner_ptr, value_gc_ptr);
    }

    /// Backward GC barrier (luaC_barrierback in Lua 5.5)
    /// Called when modifying a BLACK object (typically table) with new values
    /// Instead of marking the value, re-gray the object for re-traversal
    #[inline(always)]
    pub fn gc_barrier_back(&mut self, gc_ptr: GcObjectPtr) {
        let vm = unsafe { &mut *self.vm };
        vm.gc.barrier_back(gc_ptr);
    }

    /// Track table memory change from a resize delta.
    #[inline]
    pub fn gc_track_table_resize(&mut self, table_ptr: TablePtr, delta: isize) {
        let vm = unsafe { &mut *self.vm };
        vm.gc.track_resize(table_ptr, delta);
    }

    #[inline]
    pub fn check_gc(&mut self) -> LuaResult<bool> {
        let vm = unsafe { &mut *self.vm };
        if vm.gc.gc_debt > 0 {
            return Ok(false);
        }

        // Run GC step with the current top. Do NOT raise top to ci_top.
        //
        // In C Lua 5.5, the checkGC macro works with whatever top is set by
        // the caller. Opcodes like OP_NEWTABLE temporarily LOWER top to ra+1
        // before checkGC, which intentionally excludes stale registers from
        // the GC's stack scan. Raising top to ci_top would scan stale
        // registers that may hold dead values, keeping them alive and
        // breaking weak table clearing.
        //
        // traverse_thread in the atomic phase clears slots [top..stack_last]
        // to nil, which handles any stale references above top.
        let work = vm.check_gc(self);

        Ok(work)
    }

    #[inline(always)]
    pub(crate) fn check_gc_in_loop(
        &mut self,
        ci: &mut CallInfo,
        pc: usize,
        c: usize,
        trap: &mut bool,
    ) {
        let vm = unsafe { &mut *self.vm };
        if vm.gc.gc_debt > 0 {
            return;
        }

        ci.save_pc(pc);
        self.set_top_raw(c);
        vm.check_gc(self);
        *trap = self.hook_mask != 0;
    }

    pub fn collect_garbage(&mut self) -> LuaResult<()> {
        let vm = unsafe { &mut *self.vm };
        vm.full_gc(self, false);
        Ok(())
    }

    #[cfg(feature = "sandbox")]
    #[inline(always)]
    pub(crate) fn has_active_instruction_watch(&self) -> bool {
        self.hook_mask != 0 || self.sandbox_limits.is_some()
    }

    #[cfg(feature = "sandbox")]
    pub(crate) fn check_sandbox_runtime_limits(&mut self) -> LuaResult<()> {
        let Some(mut limits) = self.sandbox_limits else {
            return Ok(());
        };

        if let Some(remaining) = limits.remaining_instructions {
            if remaining == 0 {
                return Err(self.error("sandbox instruction limit exceeded".to_string()));
            }
            limits.remaining_instructions = Some(remaining - 1);
        }

        if let Some(deadline_nanos) = limits.deadline_nanos {
            if limits.instructions_until_time_check == 0 {
                limits.instructions_until_time_check = SANDBOX_TIMEOUT_CHECK_INTERVAL;
                if unix_nanos() >= deadline_nanos {
                    return Err(self.error("sandbox timeout exceeded".to_string()));
                }
            } else {
                limits.instructions_until_time_check -= 1;
            }
        }

        self.sandbox_limits = Some(limits);
        Ok(())
    }

    #[cfg(feature = "sandbox")]
    pub(crate) fn with_sandbox_runtime_limits<T, F>(
        &mut self,
        limits: Option<SandboxRuntimeLimits>,
        f: F,
    ) -> LuaResult<T>
    where
        F: FnOnce(&mut LuaState) -> LuaResult<T>,
    {
        let saved_limits = self.sandbox_limits;
        let saved_memory_limit = unsafe { &mut *self.vm }.gc.temporary_memory_limit();

        self.sandbox_limits = limits;

        if let Some(memory_limit_bytes) = limits.and_then(|limit| limit.memory_limit_bytes) {
            unsafe { &mut *self.vm }
                .gc
                .set_temporary_memory_limit(memory_limit_bytes);
        }

        let result = f(self);

        self.sandbox_limits = saved_limits;
        unsafe { &mut *self.vm }
            .gc
            .restore_temporary_memory_limit(saved_memory_limit);

        result
    }

    // ===== Debug Hook Support =====

    /// Invoke the debug hook function for the given event.
    /// This is called from the execute loop at hook check points.
    ///
    /// Mirrors C Lua's luaD_hook: saves/restores stack_top, sets allow_hook
    /// to false during the hook call, increments nny (hooks are non-yieldable).
    ///
    /// # Arguments
    /// * `event` - One of LUA_HOOKCALL, LUA_HOOKRET, LUA_HOOKLINE, etc.
    /// * `line` - Current line number (meaningful for LUA_HOOKLINE, -1 otherwise)
    /// * `ftransfer` - 1-based index of first transferred value (0 for line/count hooks)
    /// * `ntransfer` - number of values being transferred (0 for line/count hooks)
    #[cold]
    pub fn run_hook(
        &mut self,
        event: i32,
        line: i32,
        ftransfer: i32,
        ntransfer: i32,
    ) -> LuaResult<()> {
        let hook = self.hook;
        if hook.is_nil() || !self.allow_hook {
            return Ok(());
        }

        let vm = unsafe { &mut *self.vm };

        // Get the cached event name string from ConstString (no allocation)
        let event_name = match event {
            super::LUA_HOOKCALL => vm.const_strings.str_hook_call,
            super::LUA_HOOKRET => vm.const_strings.str_hook_return,
            super::LUA_HOOKLINE => vm.const_strings.str_hook_line,
            super::LUA_HOOKCOUNT => vm.const_strings.str_hook_count,
            super::LUA_HOOKTAILCALL => vm.const_strings.str_hook_tail_call,
            _ => return Ok(()),
        };

        // Save and restore allow_hook (like C Lua's luaD_hook)
        let old_allow_hook = self.allow_hook;
        self.allow_hook = false;
        // Hooks are non-yieldable
        self.nny += 1;
        // Store transfer info for debug.getinfo 'r' option
        self.ftransfer = ftransfer;
        self.ntransfer = ntransfer;
        // Save stack_top (restored after hook returns, like C Lua's luaD_hook)
        let old_top = self.stack_top;

        // Protect the current frame's registers: ensure stack_top >= ci.top
        // so that hook function/args are placed ABOVE the current frame.
        // This matches C Lua's luaD_hook: "if (isLua(ci) && L->top < ci->top)
        //   L->top = ci->top; /* protect entire activation register */"
        let ci_idx = self.call_depth().wrapping_sub(1);
        if ci_idx < self.call_depth() {
            let ci_top = self.get_call_info(ci_idx).top as usize;
            if self.stack_top < ci_top {
                self.stack_top = ci_top;
            }
        }

        // Mark current frame as hooked (for debug.getinfo namewhat="hook")
        let ci_idx = self.call_depth().wrapping_sub(1);
        if ci_idx < self.call_depth() {
            let ci = self.get_call_info_mut(ci_idx);
            ci.call_status |= call_status::CIST_HOOKED;
        }

        // Build args: (event_name, line_or_nil)
        let line_val = if line >= 0 {
            LuaValue::integer(line as i64)
        } else {
            LuaValue::nil()
        };

        let result = self.call(hook, vec![event_name, line_val]);

        // Clear hooked flag
        if ci_idx < self.call_depth() {
            let ci = self.get_call_info_mut(ci_idx);
            ci.call_status &= !crate::lua_vm::call_info::call_status::CIST_HOOKED;
        }

        // Restore state
        self.stack_top = old_top;
        self.nny -= 1;
        self.allow_hook = old_allow_hook;

        result.map(|_| ())
    }

    pub fn to_string(&mut self, value: &LuaValue) -> LuaResult<String> {
        // Fast path: simple types without metamethods
        match value.kind() {
            LuaValueKind::Nil => return Ok("nil".to_string()),
            LuaValueKind::Boolean => {
                if let Some(b) = value.as_boolean() {
                    return Ok(if b {
                        "true".to_string()
                    } else {
                        "false".to_string()
                    });
                }
            }
            LuaValueKind::Integer => {
                if let Some(n) = value.as_integer() {
                    return Ok(n.to_string());
                }
            }
            LuaValueKind::Float => {
                if let Some(n) = value.as_number() {
                    return Ok(n.to_string());
                }
            }
            LuaValueKind::String => {
                if let Some(s) = value.as_str() {
                    return Ok(s.to_string());
                }
                if let Some(bytes) = value.as_bytes() {
                    return Ok(String::from_utf8_lossy(bytes).into_owned());
                }
            }
            LuaValueKind::Function | LuaValueKind::CFunction => {
                // Functions: use default representation (no metatable support for functions)
                return Ok(format!("{}", value));
            }
            _ => {
                // Check for trait-based __tostring on userdata
                if value.ttisfulluserdata()
                    && let Some(ud) = value.as_userdata_mut()
                    && let Some(s) = ud.get_trait().lua_tostring()
                {
                    return Ok(s);
                }
                // Check for __tostring metamethod
                if let Some(mm) = get_metamethod_event(self, value, TmKind::ToString) {
                    let result = execute::call_tm_res1(self, mm, *value)?;
                    if let Some(s) = result.as_str() {
                        return Ok(s.to_string());
                    }
                    return Err(self.error("'__tostring' must return a string".to_string()));
                }
            }
        }

        // Fallback: generic representation
        // Check for __name in metatable (luaT_objtypename)
        let type_prefix = crate::stdlib::debug::objtypename(self, value);
        if type_prefix != value.type_name() {
            // Use __name as prefix instead of built-in type name
            Ok(format!(
                "{}: 0x{:x}",
                type_prefix,
                value.raw_ptr_repr() as usize
            ))
        } else {
            Ok(format!("{}", value))
        }
    }

    pub fn is_main_thread(&self) -> bool {
        self.is_main
    }

    /// Check if this coroutine has yielded and is waiting to be resumed.
    pub fn is_yielded(&self) -> bool {
        self.yielded
    }

    // ========================================================================
    // Debugger API — public methods for external debugger integration
    // ========================================================================

    /// Set a Lua hook function with the given mask and count.
    /// This is the programmatic equivalent of `debug.sethook`.
    pub fn set_hook(&mut self, hook: LuaValue, mask: u8, count: i32) {
        self.hook = hook;
        self.hook_mask = mask;
        self.base_hook_count = count;
        self.hook_count = count;
    }

    /// Get the current hook mask (bitmask of LUA_MASKCALL/RET/LINE/COUNT).
    pub fn hook_mask(&self) -> u8 {
        self.hook_mask
    }

    /// Get the current hook function value.
    pub fn hook_func(&self) -> LuaValue {
        self.hook
    }

    /// Get the source name of the function at the given stack level (0 = current).
    /// This is a fast path that only reads the chunk's source_name without
    /// building a full DebugInfo. Returns `None` for C functions or invalid levels.
    pub fn get_source(&self, level: usize) -> Option<String> {
        let call_depth = self.call_depth();
        if level >= call_depth {
            return None;
        }
        let frame_idx = call_depth - 1 - level;
        let func = self.get_frame_func(frame_idx)?;
        let lua_func = func.as_lua_function()?;
        lua_func.chunk().source_name.clone()
    }

    /// Get a local variable name and value at the given stack level and index.
    /// `level` is 0-based (0 = current frame). `local_idx` is 1-based.
    /// Returns `None` if the level/index is out of range.
    pub fn get_local(&self, level: usize, local_idx: usize) -> Option<(String, LuaValue)> {
        let call_depth = self.call_depth();
        if level >= call_depth {
            return None;
        }
        let frame_idx = call_depth - 1 - level;
        let func = self.get_frame_func(frame_idx)?;
        let lua_func = func.as_lua_function()?;
        let chunk = lua_func.chunk();
        let ci = self.get_frame(frame_idx)?;
        let pc = if ci.pc > 0 { ci.pc as usize - 1 } else { 0 };
        let base = ci.base;

        // Find the n-th active local variable at this PC
        let mut active_count = 0usize;
        for locvar in &chunk.locals {
            if (locvar.startpc as usize) > pc {
                break;
            }
            if pc < locvar.endpc as usize {
                active_count += 1;
                if active_count == local_idx {
                    let reg = active_count - 1;
                    let value = self.stack_get(base + reg).unwrap_or_default();
                    return Some((locvar.name.to_string(), value));
                }
            }
        }
        None
    }

    /// Count the number of active local variables at the given stack level.
    pub fn local_count(&self, level: usize) -> usize {
        let call_depth = self.call_depth();
        if level >= call_depth {
            return 0;
        }
        let frame_idx = call_depth - 1 - level;
        let func = match self.get_frame_func(frame_idx) {
            Some(f) => f,
            None => return 0,
        };
        let lua_func = match func.as_lua_function() {
            Some(f) => f,
            None => return 0,
        };
        let chunk = lua_func.chunk();
        let ci = match self.get_frame(frame_idx) {
            Some(c) => c,
            None => return 0,
        };
        let pc = if ci.pc > 0 { ci.pc as usize - 1 } else { 0 };

        let mut count = 0usize;
        for locvar in &chunk.locals {
            if (locvar.startpc as usize) > pc {
                break;
            }
            if pc < locvar.endpc as usize {
                count += 1;
            }
        }
        count
    }

    /// Get an upvalue name and value for the function at the given stack level.
    /// `level` is 0-based. `up_idx` is 1-based.
    pub fn get_upvalue(&self, level: usize, up_idx: usize) -> Option<(String, LuaValue)> {
        let call_depth = self.call_depth();
        if level >= call_depth || up_idx == 0 {
            return None;
        }
        let frame_idx = call_depth - 1 - level;
        let func = self.get_frame_func(frame_idx)?;
        let lua_func = func.as_lua_function()?;
        let upvalues = lua_func.upvalues();
        let chunk = lua_func.chunk();
        let idx = up_idx - 1;
        if idx >= upvalues.len() || idx >= chunk.upvalue_descs.len() {
            return None;
        }
        let name = chunk.upvalue_descs[idx].name.to_string();
        let value = upvalues[idx].as_ref().data.get_value();
        Some((name, value))
    }

    /// Count the number of upvalues for the function at the given stack level.
    pub fn upvalue_count(&self, level: usize) -> usize {
        let call_depth = self.call_depth();
        if level >= call_depth {
            return 0;
        }
        let frame_idx = call_depth - 1 - level;
        let func = match self.get_frame_func(frame_idx) {
            Some(f) => f,
            None => return 0,
        };
        let lua_func = match func.as_lua_function() {
            Some(f) => f,
            None => return 0,
        };
        lua_func.upvalues().len()
    }

    pub fn to_any_ref(&mut self, value: LuaValue) -> LuaAnyRef {
        self.vm_mut().to_ref(value)
    }

    pub fn to_table_ref(&mut self, value: LuaValue) -> Option<LuaTableRef> {
        self.vm_mut().to_table_ref(value)
    }

    pub fn to_function_ref(&mut self, value: LuaValue) -> Option<LuaFunctionRef> {
        self.vm_mut().to_function_ref(value)
    }

    pub fn to_userdata_ref<T: 'static>(
        &mut self,
        value: LuaValue,
    ) -> Option<crate::UserDataRef<T>> {
        self.vm_mut().to_userdata_ref(value)
    }
}

impl Default for LuaState {
    fn default() -> Self {
        Self::new(1, std::ptr::null_mut(), false, SafeOption::default())
    }
}