luars 0.26.2

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
use crate::lua_value::{LUA_VNIL, LuaInnerValue};
use crate::stdlib::basic::parse_number::parse_lua_number;
use crate::stdlib::debug::{objtypename, ordererror, typeerror};
use crate::{
    Instruction, LuaProto, LuaResult, LuaValue, OpCode,
    gc::TablePtr,
    lua_value::{lua_value_to_udvalue, udvalue_to_lua_value, udvalue_to_lua_value_with_token},
    lua_vm::{
        LuaError, LuaState, StkId, TmKind,
        call_info::{
            CallInfo,
            call_status::{
                CIST_C, CIST_PENDING_FINISH, CIST_RECST, CIST_XPCALL, CIST_YCALL, CIST_YPCALL,
            },
        },
        execute::{
            call::poscall,
            call_tm_res,
            concat::concat,
            metamethod::{self, call_tm_res_into},
        },
        lua_limits::{EXTRA_STACK, MAXTAGLOOP},
    },
};

/// Build hidden arguments for vararg functions
///
/// Initial stack:  func arg1 ... argn extra1 ...
///                 ^ ci->func                    ^ L->top
/// Final stack: func nil ... nil extra1 ... func arg1 ... argn
///                                          ^ ci->func
#[inline(never)]
pub fn buildhiddenargs(
    lua_state: &mut LuaState,
    chunk: &LuaProto,
    totalargs: usize,
    nfixparams: usize,
) -> LuaResult<usize> {
    let old_base = lua_state
        .current_frame()
        .expect("base lookup requires an active call frame")
        .base;
    let func_pos = if old_base > 0 { old_base - 1 } else { 0 };

    // The new function position is right after all the original arguments.
    // This way the extra (vararg) arguments are "hidden" between the old and new func positions.
    let new_func_pos = func_pos + totalargs + 1;
    let new_base = new_func_pos + 1;

    // Ensure enough stack space for new base + registers + EXTRA_STACK
    let new_needed_size = new_base + chunk.max_stack_size + EXTRA_STACK;
    if new_needed_size > lua_state.stack_len() {
        lua_state.grow_stack(new_needed_size)?;
    }

    let stack = lua_state.stack_mut();

    // Step 1: Copy function to new_func_pos
    // grow_stack above ensures stack is large enough for new_func_pos and new_base.
    stack[new_func_pos] = stack[func_pos];

    // Step 2: Copy fixed parameters to after new function position
    for i in 0..nfixparams {
        stack[new_base + i] = stack[func_pos + 1 + i];
        // Erase original parameter with nil (for GC)
        setnilvalue(&mut stack[func_pos + 1 + i]);
    }

    {
        let sp = lua_state.stack_mut().as_mut_ptr();
        let ci = lua_state
            .current_frame_mut()
            .expect("stack frame update requires an active call frame");
        ci.base = new_base;
        ci.base_stk = StkId::from_stack(sp, new_base);
        ci.top = (new_base + chunk.max_stack_size) as u32;
        ci.func_offset = (new_base - func_pos) as u32; // Distance from new_base to original func
    }

    // Update lua_state.top to match call_info.top
    let new_call_info_top = new_base + chunk.max_stack_size;
    lua_state.set_top(new_call_info_top)?;

    Ok(new_base)
}

/// ttisinteger_ref - 引用版本(保留兼容性)
#[inline]
pub fn ttisinteger(v: &LuaValue) -> bool {
    v.ttisinteger()
}

/// ttisfloat_ref - 引用版本(保留兼容性)
#[inline]
pub fn ttisfloat(v: &LuaValue) -> bool {
    v.ttisfloat()
}

/// ttisstring_ref - 引用版本(保留兼容性)
#[inline]
pub fn ttisstring(v: &LuaValue) -> bool {
    v.is_string()
}

// ============ 值访问宏 (对应 Lua 的 ivalue/fltvalue) ============

/// ivalue_ref - 引用版本(保留兼容性)
#[inline]
pub fn ivalue(v: &LuaValue) -> i64 {
    v.ivalue()
}

/// setnilvalue_ref - 引用版本(保留兼容性)
#[inline]
pub fn setnilvalue(v: &mut LuaValue) {
    // *v = LuaValue::nil();
    v.tt = LUA_VNIL;
    v.value = LuaInnerValue::NIL;
}

#[inline]
pub fn setobjs2s(l: &mut LuaState, a: usize, b: usize) {
    let stack = l.stack_mut();
    unsafe {
        *stack.get_unchecked_mut(a) = *stack.get_unchecked(b);
    }
}

/// tointegerns_ref - 引用版本(保留兼容性)
#[inline]
pub fn tointegerns(v: &LuaValue, out: &mut i64) -> bool {
    if v.ttisinteger() {
        *out = v.ivalue();
        true
    } else if v.ttisfloat() {
        // Try converting integral-valued floats (e.g. 5.0 -> 5)
        // Range check matches C Lua's lua_numbertointeger:
        //   f >= (i64::MIN as f64) && f < -(i64::MIN as f64)
        // Note: i64::MAX as f64 rounds UP to 2^63, so we must use strict <
        // with -(i64::MIN as f64) = 2^63 (exactly representable).
        let f = v.fltvalue();
        if f >= (i64::MIN as f64) && f < -(i64::MIN as f64) && f == (f as i64 as f64) {
            *out = f as i64;
            true
        } else {
            false
        }
    } else {
        false
    }
}

/// tonumberns - 引用版本
#[inline]
pub fn tonumberns(v: &LuaValue, out: &mut f64) -> bool {
    if v.ttisfloat() {
        *out = v.fltvalue();
        true
    } else if v.ttisinteger() {
        *out = v.ivalue() as f64;
        true
    } else {
        false
    }
}

/// Convert a LuaValue to integer using a rounding mode.
/// Port of C Lua 5.5's luaV_tointeger (lvm.c:157).
/// mode: 0 = exact only, 1 = floor, 2 = ceil
/// Handles integers, floats, and strings.
fn tointeger_mode(v: &LuaValue, mode: i32) -> Option<i64> {
    if v.ttisinteger() {
        return Some(v.ivalue());
    }
    // Get as float (including string conversion)
    let f = if v.ttisfloat() {
        v.fltvalue()
    } else if v.is_string() {
        let result = parse_lua_number(v.as_str().unwrap_or(""));
        if result.is_float() {
            result.fltvalue()
        } else if result.is_integer() {
            return Some(result.ivalue());
        } else {
            return None;
        }
    } else {
        return None;
    };
    // Convert float to integer using mode
    let rounded = match mode {
        1 => f.floor(),
        2 => f.ceil(),
        _ => f, // mode 0: exact
    };
    if rounded.is_nan() {
        return None;
    }
    if mode == 0 && rounded != f {
        return None;
    }
    // Check range: must fit in i64
    if rounded >= (i64::MIN as f64) && rounded < -(i64::MIN as f64) {
        Some(rounded as i64)
    } else {
        None
    }
}

/// Lookup value from object's metatable __index
/// Returns Ok(Some(value)) if found, Ok(None) if not found in table chain,
/// or Err if attempting to index a non-table value without __index metamethod.
///
/// Optimized hot path: inline fasttm check for __index to avoid function call overhead.
/// Matches Lua 5.5's luaV_finishget pattern.
fn finishget_inner(
    lua_state: &mut LuaState,
    obj: &LuaValue,
    key: &LuaValue,
    skip_first_raw_lookup: bool,
) -> LuaResult<Option<LuaValue>> {
    let mut t = *obj;
    let mut skip_raw_lookup = skip_first_raw_lookup;

    for _ in 0..MAXTAGLOOP {
        // Inline fasttm for __index on tables (hot path optimization)
        let tm = if let Some(table) = t.as_table_mut() {
            // Try raw_get first — handles key types the caller's fast paths didn't cover
            // (float→int normalization, long strings, etc.)
            if !skip_raw_lookup {
                if let Some(val) = table.raw_get(key) {
                    return Ok(Some(val));
                }
            } else {
                skip_raw_lookup = false;
            }

            match get_metamethod_from_meta_ptr(lua_state, table.meta_ptr(), TmKind::Index) {
                Some(v) => v,
                None => return Ok(None),
            }
        } else {
            // Non-table (string, userdata): check trait-based field access first
            if t.ttisfulluserdata()
                && let Some(ud) = t.as_userdata_mut()
            {
                let token = ud.sub_guard_token();
                let trait_obj = ud.get_trait()?;
                // Try trait-based get_field (key must be a string)
                if let Some(key_str) = key.as_str()
                    && let Some(udv) = trait_obj.get_field(key_str)
                {
                    let result = udvalue_to_lua_value_with_token(lua_state, udv, token)?;
                    return Ok(Some(result));
                }
            }
            // Fall back to general metamethod path
            match get_metamethod_event(lua_state, &t, TmKind::Index) {
                Some(tm) => tm,
                None => {
                    // No __index metamethod on non-table value → error
                    // Use typeerror for enhanced error message with varinfo
                    return Err(typeerror(lua_state, &t, "index"));
                }
            }
        };

        // If __index is a function, call it using call_tm_res
        if tm.is_function() {
            let result = call_tm_res(lua_state, tm, t, *key)?;
            return Ok(Some(result));
        }

        // __index is a table, try to access tm[key] directly
        t = tm;

        if let Some(table) = t.as_table() {
            // Use fast_geti for integer keys to avoid raw_get's float normalization
            let value = if key.ttisinteger() {
                table.impl_table.fast_geti(key.ivalue())
            } else if key.is_short_string() {
                table.impl_table.get_shortstr_fast(key)
            } else {
                table.raw_get(key)
            };
            if let Some(value) = value {
                return Ok(Some(value));
            }
            skip_raw_lookup = true;
        }

        // If not found, loop again to check if tm has __index
    }

    // Too many iterations - possible loop
    Err(lua_state.error("'__index' chain too long; possible loop".to_string()))
}

pub fn finishget(
    lua_state: &mut LuaState,
    obj: &LuaValue,
    key: &LuaValue,
) -> LuaResult<Option<LuaValue>> {
    finishget_inner(lua_state, obj, key, false)
}

/// Get a metamethod from a metatable value — implements Lua 5.5's fasttm/luaT_gettm pattern.
/// Uses bit-flag cache (u32, covering all 26 TmKind values) to skip hash lookups
/// when the metamethod is known absent.
#[inline]
fn get_metamethod_from_metatable(
    lua_state: &mut LuaState,
    metatable: LuaValue,
    tm_kind: TmKind,
) -> Option<LuaValue> {
    metatable
        .as_table_ptr()
        .and_then(|meta_ptr| get_metamethod_from_meta_ptr(lua_state, meta_ptr, tm_kind))
}

#[inline]
pub(crate) fn get_metamethod_from_meta_ptr(
    lua_state: &mut LuaState,
    meta_ptr: TablePtr,
    tm_kind: TmKind,
) -> Option<LuaValue> {
    if meta_ptr.is_null() {
        return None;
    }

    let mt = unsafe { &mut (*meta_ptr.as_mut_ptr()).data };
    let tm_idx = tm_kind as u8;
    if mt.no_tm(tm_idx) {
        return None;
    }

    let vm = lua_state.global_state_mut();
    let event_key = vm.const_strings.get_tm_value(tm_kind);
    let result = mt.impl_table.get_shortstr_fast(&event_key);

    if result.is_none() {
        mt.set_tm_absent(tm_idx);
    }

    result
}

/// Port of Lua 5.5's luaV_finishset from lvm.c:334
/// ```c
/// void luaV_finishset (lua_State *L, const TValue *t, TValue *key,
///                       TValue *val, int hres) {
///   int loop;  /* counter to avoid infinite loops */
///   for (loop = 0; loop < MAXTAGLOOP; loop++) {
///     const TValue *tm;  /* '__newindex' metamethod */
///     if (hres != HNOTATABLE) {  /* is 't' a table? */
///       Table *h = hvalue(t);  /* save 't' table */
///       tm = fasttm(L, h->metatable, TM_NEWINDEX);  /* get metamethod */
///       if (tm == NULL) {  /* no metamethod? */
///         sethvalue2s(L, L->top.p, h);  /* anchor 't' */
///         L->top.p++;  /* assume EXTRA_STACK */
///         luaH_finishset(L, h, key, val, hres);  /* set new value */
///         L->top.p--;
///         invalidateTMcache(h);
///         luaC_barrierback(L, obj2gco(h), val);
///         return;
///       }
///       /* else will try the metamethod */
///     }
///     else {  /* not a table; check metamethod */
///       tm = luaT_gettmbyobj(L, t, TM_NEWINDEX);
///       if (l_unlikely(notm(tm)))
///         luaG_typeerror(L, t, "index");
///     }
///     /* try the metamethod */
///     if (ttisfunction(tm)) {
///       luaT_callTM(L, tm, t, key, val);
///       return;
///     }
///     t = tm;  /* else repeat assignment over 'tm' */
///     luaV_fastset(t, key, val, hres, luaH_pset);
///     if (hres == HOK) {
///       luaV_finishfastset(L, t, val);
///       return;  /* done */
///     }
///     /* else 'return luaV_finishset(L, t, key, val, slot)' (loop) */
///   }
///   luaG_runerror(L, "'__newindex' chain too long; possible loop");
/// }
/// ```
pub(crate) fn finishset(
    lua_state: &mut LuaState,
    obj: &LuaValue,
    key: &LuaValue,
    value: LuaValue,
    skip_existing_check: bool,
) -> LuaResult<bool> {
    // Check for invalid keys (nil or NaN)
    if key.is_nil() {
        return Err(lua_state.error("table index is nil".to_string()));
    }
    if key.ttisfloat()
        && let Some(f) = key.as_float()
        && f.is_nan()
    {
        return Err(lua_state.error("table index is NaN".to_string()));
    }

    let mut t = *obj;
    let mut skip_existing = skip_existing_check;

    for _ in 0..MAXTAGLOOP {
        // Check if t is a table — use inline fasttm for __newindex
        if let Some(table) = t.as_table_mut() {
            let tm_val =
                get_metamethod_from_meta_ptr(lua_state, table.meta_ptr(), TmKind::NewIndex);

            if tm_val.is_none() {
                // No metamethod - set directly
                lua_state.raw_set(&t, *key, value);
                return Ok(true);
            }

            // Check if key already exists in the table.
            // If it does, do a raw set regardless of __newindex.
            if !skip_existing {
                if let Some(existing) = table.raw_get(key)
                    && !existing.is_nil()
                {
                    lua_state.raw_set(&t, *key, value);
                    return Ok(true);
                }
            } else {
                skip_existing = false;
            }

            // Key does not exist - call __newindex metamethod
            if let Some(tm) = tm_val {
                if tm.is_function() {
                    metamethod::call_tm(lua_state, tm, t, *key, value)?;
                    return Ok(true);
                }

                // Metamethod is a table - repeat assignment over 'tm'
                t = tm;
                continue;
            }
        } else {
            // Not a table — try trait-based set_field for userdata first
            if t.ttisfulluserdata()
                && let Some(ud) = t.as_userdata_mut()
                && let Some(key_str) = key.as_str()
            {
                let udv = lua_value_to_udvalue(&value);
                match ud.get_trait_mut() {
                    Ok(trait_obj) => match trait_obj.set_field(key_str, udv) {
                        Some(Ok(())) => return Ok(true),
                        Some(Err(msg)) => {
                            return Err(lua_state.error(msg));
                        }
                        None => {} // Fall through to metatable
                    },
                    Err(e) => return Err(e),
                }
            }
            // Get __newindex metamethod
            if let Some(tm) = get_metamethod_event(lua_state, &t, TmKind::NewIndex) {
                if tm.is_function() {
                    metamethod::call_tm(lua_state, tm, t, *key, value)?;

                    return Ok(true);
                }

                // Metamethod is a table
                t = tm;
                continue;
            }

            // No metamethod found for non-table
            return Err(typeerror(lua_state, &t, "index"));
        }
    }

    // Too many iterations - possible loop
    Err(lua_state.error("'__newindex' chain too long; possible loop".to_string()))
}

pub fn get_metamethod_event(
    lua_state: &mut LuaState,
    value: &LuaValue,
    tm_kind: TmKind,
) -> Option<LuaValue> {
    let mt = get_metatable(lua_state, value)?;
    get_metamethod_from_metatable(lua_state, mt, tm_kind)
}

/// Get binary operation metamethod from either of two values
/// Checks v1's metatable first, then v2's if not found
pub fn get_binop_metamethod(
    lua_state: &mut LuaState,
    v1: &LuaValue,
    v2: &LuaValue,
    tm_kind: TmKind,
) -> Option<LuaValue> {
    // Try v1's metatable first
    if let Some(mt) = get_metatable(lua_state, v1)
        && let Some(mm) = get_metamethod_from_metatable(lua_state, mt, tm_kind)
    {
        return Some(mm);
    }

    // Try v2's metatable
    if let Some(mt) = get_metatable(lua_state, v2)
        && let Some(mm) = get_metamethod_from_metatable(lua_state, mt, tm_kind)
    {
        return Some(mm);
    }

    None
}

/// Get metatable for any value type
pub fn get_metatable(lua_state: &mut LuaState, value: &LuaValue) -> Option<LuaValue> {
    if let Some(table) = value.as_table_mut() {
        return table.get_metatable();
    } else if let Some(ud) = value.as_userdata_mut() {
        return ud.get_metatable();
    }
    // Basic types: use global type metatable
    lua_state
        .global_state_mut()
        .get_basic_metatable(value.kind())
}

/// Finish a C frame left on the call stack after yield-resume.
/// This is the Rust equivalent of Lua 5.5's finishCcall.
#[cold]
#[inline(never)]
fn finish_c_frame(lua_state: &mut LuaState, ci: &CallInfo) -> LuaResult<()> {
    let pcall_func_pos = ci.base - ci.func_offset as usize;
    let nresults = ci.nresults();
    let call_status = ci.call_status;
    let has_recst = call_status & CIST_RECST != 0;
    let is_xpcall = call_status & CIST_XPCALL != 0;

    if call_status & CIST_YPCALL != 0 {
        if has_recst {
            // Save handler before it gets overwritten (only for xpcall)
            let handler = if is_xpcall {
                lua_state.stack_get(pcall_func_pos).unwrap_or_default()
            } else {
                LuaValue::nil()
            };

            // Error recovery completed (or continuing) after yield.
            // Retrieve the saved error value and try to close remaining TBC entries.
            let error_val = lua_state.take_error_object();
            lua_state.clear_error();
            let close_level = pcall_func_pos + 1; // body's base position

            // Try to close remaining TBC entries
            let close_result = lua_state.close_tbc_with_error(close_level, error_val);

            match close_result {
                Ok(()) => {
                    // All TBC entries closed. Set up (false, error) result.
                    let final_err = lua_state.take_error_object();
                    let result_err = if !final_err.is_nil() {
                        final_err
                    } else {
                        error_val
                    };
                    lua_state.clear_error();

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

                    lua_state.stack_set(pcall_func_pos, LuaValue::boolean(false))?;
                    lua_state.stack_set(pcall_func_pos + 1, result_err)?;
                    let n = 2;

                    // Pop pcall C frame
                    lua_state.pop_frame();

                    // Handle nresults adjustment
                    let final_n = if nresults == -1 { n } else { nresults as usize };
                    let new_top = pcall_func_pos + final_n;

                    if nresults >= 0 {
                        let wanted = nresults as usize;
                        for i in n..wanted {
                            lua_state.stack_set(pcall_func_pos + i, LuaValue::nil())?;
                        }
                    }

                    lua_state.set_top_raw(new_top);

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

                    Ok(())
                }
                Err(LuaError::Yield) => {
                    // Another TBC close method yielded. Save cascaded error and yield.
                    let had_cascaded = lua_state.has_error_object();
                    let cascaded = lua_state.take_error_object();
                    lua_state.set_error_object(if had_cascaded { cascaded } else { error_val });
                    Err(LuaError::Yield)
                }
                Err(e) => {
                    // TBC close threw — propagate as error
                    Err(e)
                }
            }
        } else {
            // pcall body completed successfully after yield.
            // Body's return values are at pcall_func_pos + 1 … top-1.
            // We need: [true, res1, res2, ...] starting at pcall_func_pos.
            let stack_top = lua_state.get_top();
            let body_results_start = pcall_func_pos + 1;
            let body_nres = stack_top.saturating_sub(body_results_start);

            // Place true at pcall_func_pos (body results already at +1)
            lua_state.stack_set(pcall_func_pos, LuaValue::boolean(true))?;

            let n = 1 + body_nres; // total results: true + body results

            // Pop pcall C frame
            lua_state.pop_frame();

            // Handle nresults adjustment (same as call_c_function post-processing)
            let final_n = if nresults == -1 { n } else { nresults as usize };
            let new_top = pcall_func_pos + final_n;

            if nresults >= 0 {
                let wanted = nresults as usize;
                // Pad with nil if needed
                for i in n..wanted {
                    lua_state.stack_set(pcall_func_pos + i, LuaValue::nil())?;
                }
            }

            lua_state.set_top_raw(new_top);

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

            Ok(())
        }
    } else if call_status & CIST_YCALL != 0 {
        // Unprotected call (e.g. dofile) completed after yield.
        // Move results from body_start to func_pos (no true/false prefix).
        let stack_top = lua_state.get_top();
        let body_results_start = pcall_func_pos + 1;
        let body_nres = stack_top.saturating_sub(body_results_start);

        // Move results down to func_pos
        for i in 0..body_nres {
            let val = lua_state
                .stack_get(body_results_start + i)
                .unwrap_or_default();
            lua_state.stack_set(pcall_func_pos + i, val)?;
        }

        let n = body_nres;

        lua_state.pop_frame();

        let final_n = if nresults == -1 { n } else { nresults as usize };
        let new_top = pcall_func_pos + final_n;

        if nresults >= 0 {
            let wanted = nresults as usize;
            for i in n..wanted {
                lua_state.stack_set(pcall_func_pos + i, LuaValue::nil())?;
            }
        }

        lua_state.set_top_raw(new_top);

        if lua_state.call_depth() > 0 {
            let ci_idx = lua_state.call_depth() - 1;
            if nresults == -1 {
                let ci_top = lua_state.get_call_info(ci_idx).top as usize;
                if ci_top < new_top {
                    lua_state.get_call_info_mut(ci_idx).top = new_top as u32;
                }
            } else {
                let frame_top = lua_state.get_call_info(ci_idx).top as usize;
                lua_state.set_top_raw(frame_top);
            }
        }

        Ok(())
    } else {
        // Generic C frame after yield — just pop it.
        // This shouldn't normally happen, but be safe.
        lua_state.pop_frame();
        Ok(())
    }
}

#[inline]
fn mark_pending_finish(ci: &mut CallInfo, aux: i32) {
    ci.set_pending_finish_get(aux);
    ci.call_status |= CIST_PENDING_FINISH;
}

/// Handle pending metamethod finish (cold path, extracted from main loop).
/// Returns true if a C frame was finished and execution should restart.
/// This is the equivalent of C Lua's luaV_finishOp.
#[cold]
#[inline(never)]
pub fn handle_pending_ops(lua_state: &mut LuaState, ci: &mut CallInfo) -> LuaResult<bool> {
    if ci.call_status & CIST_C != 0 {
        finish_c_frame(lua_state, ci)?;
        return Ok(true); // restart startfunc
    }
    // === luaV_finishOp equivalent ===
    // Extract needed CI fields upfront, then drop the borrow.
    let (saved_pc, base_tmp, pending_finish, _nresults, ci_chunk_ptr, ci_top) = {
        (
            ci.pc as usize,
            ci.base,
            ci.pending_finish_get(),
            ci.nresults(),
            ci.chunk_ptr,
            ci.top as usize,
        )
    };

    // Get the chunk to read the interrupted instruction
    if !ci_chunk_ptr.is_null() {
        let chunk = unsafe { &*ci_chunk_ptr };
        let code = &chunk.code;

        if saved_pc > 0 && saved_pc <= code.len() {
            let interrupted_instr = code[saved_pc - 1];
            let op = interrupted_instr.get_opcode();
            match op {
                OpCode::MmBin | OpCode::MmBinI | OpCode::MmBinK => {
                    // Arithmetic metamethod: result at stack[top-1],
                    // destination from the instruction at savedpc - 2
                    let top = lua_state.get_top();
                    if top > 0 && saved_pc >= 2 {
                        let arith_instr = code[saved_pc - 2];
                        let dest = if pending_finish >= 0 {
                            pending_finish as usize
                        } else {
                            base_tmp + arith_instr.get_a() as usize
                        };
                        let result = lua_state.stack_mut()[top - 1];
                        lua_state.stack_mut()[dest] = result;
                        lua_state.set_top_raw(top - 1);
                    }
                }
                OpCode::Unm
                | OpCode::BNot
                | OpCode::Len
                | OpCode::GetTabUp
                | OpCode::GetTable
                | OpCode::GetI
                | OpCode::GetField
                | OpCode::Self_ => {
                    // Unary/table get ops: result at stack[top-1],
                    // destination at base + A of the interrupted instruction
                    let top = lua_state.get_top();
                    if top > 0 {
                        let dest = if pending_finish >= 0 {
                            pending_finish as usize
                        } else {
                            base_tmp + interrupted_instr.get_a() as usize
                        };
                        let result = lua_state.stack_mut()[top - 1];
                        lua_state.stack_mut()[dest] = result;
                        lua_state.set_top_raw(top - 1);
                    }
                }
                OpCode::Lt
                | OpCode::Le
                | OpCode::LtI
                | OpCode::LeI
                | OpCode::GtI
                | OpCode::GeI
                | OpCode::Eq => {
                    // Comparison ops: truthiness of stack[top-1] is the result.
                    // Next instruction should be JMP.
                    // If result != k, skip the JMP.
                    let top = lua_state.get_top();
                    if top > 0 {
                        let res_val = lua_state.stack_mut()[top - 1];
                        let res = !res_val.is_nil() && !(res_val == LuaValue::boolean(false));
                        lua_state.set_top_raw(top - 1);
                        let k = interrupted_instr.get_k();
                        if res != k {
                            // Skip the JMP instruction
                            ci.pc += 1;
                        }
                    }
                }
                OpCode::Concat => {
                    // Port of C Lua 5.5's finishOp for OP_CONCAT (lvm.c:882-893)
                    // After yield in __concat metamethod, the result is at top-1.
                    // We must copy it to concat_top - 2 and continue if elements remain.
                    let top = lua_state.get_top();
                    if top > 0 {
                        let a = interrupted_instr.get_a() as usize;
                        let n = interrupted_instr.get_b() as usize;
                        let concat_top = base_tmp + a + n;
                        let result = lua_state.stack_mut()[top - 1];
                        lua_state.stack_mut()[concat_top - 2] = result;
                        let total = concat_top - 1 - (base_tmp + a);
                        if total > 1 {
                            lua_state.set_top_raw(concat_top - 1);
                            concat(lua_state, total)?;
                        }
                    }
                }
                _ => {
                    // CALL, TAILCALL, TFORCALL, SETTAB*, SETFIELD, SETI — no special action needed
                }
            }
        }
    }

    // Restore ci_top
    let current_top = lua_state.get_top();
    if current_top < ci_top {
        lua_state.set_top_raw(ci_top);
    }

    ci.set_pending_finish_get(-1);
    ci.call_status &= !CIST_PENDING_FINISH;
    Ok(false) // continue to hot path
}

pub fn objlen(
    l: &mut LuaState,
    ci: &mut CallInfo,
    dest_stk_id: StkId,
    value: LuaValue,
) -> LuaResult<()> {
    if let Some(bytes) = value.as_bytes() {
        let len = bytes.len();
        dest_stk_id.set_integer(len as i64);
        return Ok(());
    } else if value.ttistable() {
        if let Some(tm) = get_metamethod_event(l, &value, TmKind::Len) {
            return match call_tm_res_into(l, tm, value, value, dest_stk_id) {
                Ok(()) => Ok(()),
                Err(LuaError::Yield) => {
                    let aux = l.offset_of_stk_id(dest_stk_id);
                    mark_pending_finish(ci, aux);
                    Err(LuaError::Yield)
                }
                Err(e) => Err(e),
            };
        }

        let len = value.hvalue().len();
        dest_stk_id.set_integer(len as i64);
        return Ok(());
    } else {
        // Try trait-based __len for userdata first
        if value.ttisfulluserdata()
            && let Some(ud) = value.as_userdata_mut()
        {
            let trait_obj = ud.get_trait()?;
            if let Some(udv) = trait_obj.lua_len() {
                let result = udvalue_to_lua_value(l, udv)?;
                dest_stk_id.set_integer(result.as_integer().unwrap_or(0));
                return Ok(());
            }
        }
    }

    let tm = get_metamethod_event(l, &value, TmKind::Len);
    if let Some(tm) = tm {
        match call_tm_res_into(l, tm, value, value, dest_stk_id) {
            Ok(()) => {}
            Err(LuaError::Yield) => {
                let aux = l.offset_of_stk_id(dest_stk_id);
                mark_pending_finish(ci, aux);
                return Err(LuaError::Yield);
            }
            Err(e) => return Err(e),
        }
    } else {
        return Err(typeerror(l, &value, "get length of"));
    }
    Ok(())
}

/// Equality comparison - direct port of Lua 5.5's luaV_equalobj
/// Returns true if values are equal, false otherwise
/// Handles metamethods for tables and userdata
pub fn equalobj(lua_state: &mut LuaState, t1: LuaValue, t2: LuaValue) -> LuaResult<bool> {
    // Direct port of lvm.c:582 luaV_equalobj
    if t1 == t2 {
        return Ok(true);
    }

    if t1.tt() != t2.tt() {
        return Ok(false);
    }

    if t1.ttisfulluserdata() {
        // Userdata: first check identity
        if let (Some(u_ptr1), Some(u_ptr2)) = (t1.as_userdata_ptr(), t2.as_userdata_ptr())
            && u_ptr1 == u_ptr2
        {
            return Ok(true);
        }
        // Try trait-based lua_eq before metatable
        if let Some(ud1) = t1.as_userdata_mut()
            && let Some(ud2) = t2.as_userdata_mut()
            && let Some(result) = {
                let t1 = ud1.get_trait()?;
                let t2 = ud2.get_trait()?;
                t1.lua_eq(t2)
            }
        {
            return Ok(result);
        }
        // Different userdata - try __eq metamethod
        let tm = get_binop_metamethod(lua_state, &t1, &t2, TmKind::Eq);

        if let Some(metamethod) = tm {
            let result = call_tm_res(lua_state, metamethod, t1, t2)?;
            return Ok(!result.is_falsy());
        } else {
            return Ok(false);
        }
    }

    if t1.ttistable() {
        // Tables: first check identity
        if let (Some(t_ptr1), Some(t_ptr2)) = (t1.as_table_ptr(), t2.as_table_ptr())
            && t_ptr1 == t_ptr2
        {
            return Ok(true);
        }
        // Different tables - try __eq metamethod
        let tm = get_binop_metamethod(lua_state, &t1, &t2, TmKind::Eq);
        if let Some(metamethod) = tm {
            let result = call_tm_res(lua_state, metamethod, t1, t2)?;
            return Ok(!result.is_falsy());
        } else {
            return Ok(false);
        }
    }

    if t1.ttiscfunction() {
        // C functions: compare function pointers
        return Ok(unsafe { std::ptr::fn_addr_eq(t1.value.f, t2.value.f) });
    }

    // Lua functions, threads, etc.: compare GC pointers
    if let (Some(f_ptr1), Some(f_ptr2)) = (t1.as_function_ptr(), t2.as_function_ptr()) {
        return Ok(f_ptr1 == f_ptr2);
    }

    Ok(false)
}

pub fn forprep(lua_state: &mut LuaState, ra: StkId) -> LuaResult<bool> {
    let r_init = ra;
    let r_limit = ra.offset(1);
    let r_step = ra.offset(2);
    if r_init.is_integer() && r_step.is_integer() {
        // Integer loop (init and step are integers)
        let init = r_init.ivalue();
        let step = r_step.ivalue();

        if step == 0 {
            return Err(lua_state.error("'for' step is zero".to_string()));
        }
        // forlimit: convert limit to integer per C Lua 5.5 logic
        let (limit, should_skip) = for_limit(lua_state, r_limit, init, step)?;

        if should_skip {
            return Ok(true);
        }

        // Check if loop should be skipped based on direction
        if step > 0 {
            if init > limit {
                return Ok(true); // skip: init already past limit
            }
        } else if limit > init {
            return Ok(true); // skip: init already past limit (counting down)
        }

        {
            let count = if step > 0 {
                ((limit as u64).wrapping_sub(init as u64)) / (step as u64)
            } else {
                let step_abs = if step == i64::MIN {
                    i64::MAX as u64 + 1
                } else {
                    (-step) as u64
                };
                ((init as u64).wrapping_sub(limit as u64)) / step_abs
            };

            ra.change_i(count as i64);
            r_limit.set_integer(step);
            r_step.change_i(init);
        }
    } else {
        // Float loop — delegate to existing handler
        let mut init = 0.0;
        let mut limit = 0.0;
        let mut step = 0.0;

        if !for_tonumber(r_limit, &mut limit) {
            let t = objtypename(lua_state, r_limit.get_ref());
            return Err(lua_state.error(format!("bad 'for' limit (number expected, got {})", t)));
        }
        if !for_tonumber(r_step, &mut step) {
            let t = objtypename(lua_state, r_step.get_ref());
            return Err(lua_state.error(format!("bad 'for' step (number expected, got {})", t)));
        }
        if !for_tonumber(ra, &mut init) {
            let t = objtypename(lua_state, ra.get_ref());
            return Err(lua_state.error(format!(
                "bad 'for' initial value (number expected, got {})",
                t
            )));
        }

        if step == 0.0 {
            return Err(lua_state.error("'for' step is zero".to_string()));
        }

        let should_skip = if step > 0.0 {
            limit < init
        } else {
            init < limit
        };

        if should_skip {
            return Ok(true);
        } else {
            ra.set_float(limit);
            r_limit.set_float(step);
            r_step.set_float(init);
        }
    }

    Ok(false)
}

/// Port of C Lua 5.5's luaV_tonumber_ (lvm.c).
/// Full conversion: handles strings in addition to integers/floats.
/// Used by forprep where operands may be strings.
/// Marked cold to keep the arithmetic hot path (ptonumberns) compact.
fn for_tonumber(v: StkId, out: &mut f64) -> bool {
    if v.is_float() {
        *out = v.fltvalue();
        true
    } else if v.is_integer() {
        *out = v.ivalue() as f64;
        true
    } else if v.get_ref().is_string() {
        if let Some(s) = v.get_ref().as_str() {
            let result = parse_lua_number(s);
            if result.is_float() {
                *out = result.fltvalue();
                true
            } else if result.is_integer() {
                *out = result.ivalue() as f64;
                true
            } else {
                false
            }
        } else {
            false
        }
    } else {
        false
    }
}

fn for_limit(
    lua_state: &mut LuaState,
    r_limit: StkId,
    init: i64,
    step: i64,
) -> LuaResult<(i64, bool)> {
    // Port of C Lua 5.5's forlimit (lvm.c:181-198)
    // Try converting the limit to integer (with floor or ceil depending on step direction)
    let mode = if step < 0 { 2 } else { 1 }; // 1=floor, 2=ceil
    if let Some(lim) = tointeger_mode(r_limit.get_ref(), mode) {
        // Successfully converted to integer
        let skip = if step > 0 { init > lim } else { init < lim };
        Ok((lim, skip))
    } else {
        // Not coercible to integer. Try converting to float to check bounds.
        let mut flimit = 0.0;
        if !for_tonumber(r_limit, &mut flimit) {
            return Err(error_for_bad_limit(lua_state, r_limit.get_ref()));
        }
        // flim is a float out of integer bounds
        if 0.0 < flimit {
            // Limit is above max integer
            if step < 0 {
                return Ok((i64::MAX, true)); // skip
            }
            Ok((i64::MAX, false)) // truncate, caller checks init > limit
        } else {
            // Limit is below min integer
            if step > 0 {
                return Ok((i64::MIN, true)); // skip
            }
            Ok((i64::MIN, false)) // truncate, caller checks init < limit
        }
    }
}

#[cold]
#[inline(never)]
pub fn float_for_loop(ra: StkId) -> bool {
    let step = ra.offset(1).fltvalue();
    let limit = ra.fltvalue();
    let mut idx = ra.offset(2).fltvalue();
    idx += step;
    if (step > 0.0 && idx <= limit) || (step <= 0.0 && idx >= limit) {
        ra.offset(2).change_f(idx);
        return true;
    }

    false
}

#[cold]
#[inline(never)]
fn error_for_bad_limit(lua_state: &mut LuaState, limit_val: &LuaValue) -> LuaError {
    let t = objtypename(lua_state, limit_val);
    lua_state.error(format!("bad 'for' limit (number expected, got {})", t))
}

/// Cold error: attempt to divide by zero (IDIV)
#[cold]
#[inline(never)]
pub fn error_div_by_zero(lua_state: &mut LuaState) -> LuaError {
    lua_state.error("attempt to divide by zero".to_string())
}

/// Cold error: attempt to perform 'n%0' (MOD)
#[cold]
#[inline(never)]
pub fn error_mod_by_zero(lua_state: &mut LuaState) -> LuaError {
    lua_state.error("attempt to perform 'n%0'".to_string())
}

#[cold]
#[inline(never)]
pub fn error_global(lua_state: &mut LuaState, global_name: &str) -> LuaError {
    lua_state.error(format!("global '{}' already defined", global_name))
}

/// Cold path: comparison metamethod fallback for LtI/LeI/GtI/GeI/Lt/Le
/// Extraced from execute_loop to reduce main function size and improve register allocation.
#[cold]
#[inline(never)]
pub fn order_tm_fallback(
    lua_state: &mut LuaState,
    ci: &mut CallInfo,
    va: LuaValue,
    vb: LuaValue,
    tm: TmKind,
) -> LuaResult<bool> {
    use crate::lua_vm::execute::metamethod::try_comp_tm;
    match try_comp_tm(lua_state, va, vb, tm) {
        Ok(Some(result)) => Ok(result),
        Ok(None) => Err(ordererror(lua_state, &va, &vb)),
        Err(LuaError::Yield) => {
            mark_pending_finish(ci, -1);
            Err(LuaError::Yield)
        }
        Err(e) => Err(e),
    }
}

/// Cold path: binary metamethod fallback for MmBin/MmBinI/MmBinK
#[cold]
#[inline(never)]
#[allow(clippy::too_many_arguments)]
pub fn bin_tm_fallback(
    lua_state: &mut LuaState,
    ci: &mut CallInfo,
    ra: LuaValue,
    rb: LuaValue,
    result_reg: u32,
    a_reg: u32,
    b_reg: u32,
    tm: TmKind,
) -> LuaResult<()> {
    use crate::lua_vm::execute::metamethod::try_bin_tm;
    match try_bin_tm(lua_state, ra, rb, result_reg, a_reg, b_reg, tm) {
        Ok(_) => Ok(()),
        Err(LuaError::Yield) => {
            mark_pending_finish(ci, result_reg as i32);
            Err(LuaError::Yield)
        }
        Err(e) => Err(e),
    }
}

/// Cold path: unary metamethod fallback for Unm/BNot/Len
#[cold]
#[inline(never)]
pub fn unary_tm_fallback(
    lua_state: &mut LuaState,
    ci: &mut CallInfo,
    rb: LuaValue,
    result_reg: usize,
    tm: TmKind,
) -> LuaResult<()> {
    use crate::lua_vm::execute::metamethod::try_unary_tm;
    match try_unary_tm(lua_state, rb, result_reg, tm) {
        Ok(_) => Ok(()),
        Err(LuaError::Yield) => {
            mark_pending_finish(ci, result_reg as i32);
            Err(LuaError::Yield)
        }
        Err(e) => Err(e),
    }
}

/// finishget wrapper for GetTabUp/GetTable/GetI/GetField/Self_
/// Handles __index metamethod chain + yield propagation.
/// NOT #[cold]: __index is a common OOP operation.
#[inline(never)]
pub fn finishget_fallback(
    lua_state: &mut LuaState,
    ci: &mut CallInfo,
    obj: &LuaValue,
    key: &LuaValue,
    dest_stk_id: StkId,
) -> LuaResult<()> {
    match finishget_to_reg_known_miss(lua_state, obj, key, dest_stk_id) {
        Ok(()) => Ok(()),
        Err(LuaError::Yield) => {
            let aux = lua_state.offset_of_stk_id(dest_stk_id);
            mark_pending_finish(ci, aux);
            Err(LuaError::Yield)
        }
        Err(e) => Err(e),
    }
}

/// Fast path for OOP-style `SELF_` lookups where the miss is resolved by a
/// table-only `__index` chain and the key is a short string.
/// Returns true if a value was found and written directly to `dest_stk_id`.
#[inline(never)]
pub fn self_shortstr_index_chain_fast(
    lua_state: &mut LuaState,
    obj: &LuaValue,
    key: &LuaValue,
    dest_stk_id: StkId,
) -> bool {
    const TM_INDEX_BIT: u8 = TmKind::Index as u8;

    debug_assert!(key.is_short_string());

    let event_key = lua_state
        .global_state_mut()
        .const_strings
        .get_tm_value(TmKind::Index);
    let mut current = *obj;

    for _ in 0..MAXTAGLOOP {
        let Some(table) = current.as_table_mut() else {
            return false;
        };

        if let Some(value) = table.impl_table.get_shortstr_fast(key) {
            dest_stk_id.write(&value);
            return true;
        }

        let meta = table.meta_ptr();
        if meta.is_null() {
            return false;
        }

        let mt = unsafe { &mut (*meta.as_mut_ptr()).data };
        if mt.no_tm(TM_INDEX_BIT) {
            return false;
        }

        let Some(tm) = mt.impl_table.get_shortstr_fast(&event_key) else {
            mt.set_tm_absent(TM_INDEX_BIT);
            return false;
        };

        if tm.is_function() || !tm.is_table() {
            return false;
        }

        current = tm;
    }

    false
}

fn finishget_to_reg_inner(
    lua_state: &mut LuaState,
    obj: &LuaValue,
    key: &LuaValue,
    dest_stk_id: StkId,
    skip_first_raw_lookup: bool,
) -> LuaResult<()> {
    const TM_INDEX_BIT: u8 = TmKind::Index as u8;

    let mut t = *obj;
    let mut skip_raw_lookup = skip_first_raw_lookup;

    for _ in 0..MAXTAGLOOP {
        let tm = if let Some(table) = t.as_table_mut() {
            if !skip_raw_lookup {
                if let Some(val) = table.raw_get(key) {
                    dest_stk_id.write(&val);
                    return Ok(());
                }
            } else {
                skip_raw_lookup = false;
            }

            let meta = table.meta_ptr();
            if meta.is_null() {
                dest_stk_id.set_nil();
                return Ok(());
            }
            let mt = unsafe { &mut (*meta.as_mut_ptr()).data };
            if mt.no_tm(TM_INDEX_BIT) {
                dest_stk_id.set_nil();
                return Ok(());
            }
            let vm = lua_state.global_state_mut();
            let event_key = vm.const_strings.get_tm_value(TmKind::Index);
            match mt.impl_table.get_shortstr_fast(&event_key) {
                Some(v) => v,
                None => {
                    mt.set_tm_absent(TM_INDEX_BIT);
                    dest_stk_id.set_nil();
                    return Ok(());
                }
            }
        } else {
            if t.ttisfulluserdata()
                && let Some(ud) = t.as_userdata_mut()
            {
                let token = ud.sub_guard_token();
                let trait_obj = ud.get_trait()?;
                if let Some(key_str) = key.as_str()
                    && let Some(udv) = trait_obj.get_field(key_str)
                {
                    let result = udvalue_to_lua_value_with_token(lua_state, udv, token)?;
                    dest_stk_id.write(&result);
                    return Ok(());
                }
            }

            match get_metamethod_event(lua_state, &t, TmKind::Index) {
                Some(tm) => tm,
                None => {
                    return Err(typeerror(lua_state, &t, "index"));
                }
            }
        };

        if tm.is_function() {
            return call_tm_res_into(lua_state, tm, t, *key, dest_stk_id);
        }

        t = tm;
        if let Some(table) = t.as_table() {
            let value = if key.ttisinteger() {
                table.impl_table.fast_geti(key.ivalue())
            } else if key.is_short_string() {
                table.impl_table.get_shortstr_fast(key)
            } else {
                table.raw_get(key)
            };
            if let Some(value) = value {
                dest_stk_id.write(&value);
                return Ok(());
            }
            skip_raw_lookup = true;
        }
    }

    Err(lua_state.error("'__index' chain too long; possible loop".to_string()))
}

fn finishget_to_reg_known_miss(
    lua_state: &mut LuaState,
    obj: &LuaValue,
    key: &LuaValue,
    dest_stk_id: StkId,
) -> LuaResult<()> {
    finishget_to_reg_inner(lua_state, obj, key, dest_stk_id, true)
}

/// finishset wrapper for SetTabUp/SetTable/SetI/SetField
/// Handles __newindex metamethod chain + yield propagation.
/// NOT #[cold]: __newindex is a common OOP operation.
#[inline(never)]
pub fn finishset_fallback(
    lua_state: &mut LuaState,
    ci: &mut CallInfo,
    obj: &LuaValue,
    key: &LuaValue,
    value: LuaValue,
    known_miss: bool,
) -> LuaResult<()> {
    match finishset(lua_state, obj, key, value, known_miss) {
        Ok(_) => Ok(()),
        Err(LuaError::Yield) => {
            mark_pending_finish(ci, -2);
            Err(LuaError::Yield)
        }
        Err(e) => Err(e),
    }
}

/// Cold path: equality metamethod fallback for Eq
#[inline(never)]
pub fn eq_fallback(
    lua_state: &mut LuaState,
    ci: &mut CallInfo,
    ra: LuaValue,
    rb: LuaValue,
) -> LuaResult<bool> {
    match equalobj(lua_state, ra, rb) {
        Ok(eq) => Ok(eq),
        Err(LuaError::Yield) => {
            mark_pending_finish(ci, -1);
            Err(LuaError::Yield)
        }
        Err(e) => Err(e),
    }
}

/// Cold path: Return0 with active hooks — delegates to generic poscall
#[inline(never)]
pub fn return0_with_hook(
    lua_state: &mut LuaState,
    ci: &mut CallInfo,
    a_pos: usize,
    pc: usize,
) -> LuaResult<()> {
    lua_state.set_top_raw(a_pos);
    ci.save_pc(pc);
    poscall(lua_state, ci, 0, pc)
}

/// Cold path: Return1 with active hooks — delegates to generic poscall
#[inline(never)]
pub fn return1_with_hook(
    lua_state: &mut LuaState,
    ci: &mut CallInfo,
    a_pos: usize,
    pc: usize,
) -> LuaResult<()> {
    lua_state.set_top_raw(a_pos + 1);
    ci.save_pc(pc);
    poscall(lua_state, ci, 1, pc)
}

#[inline]
pub fn instr_at(code: &[Instruction], pc: usize) -> Instruction {
    unsafe { *code.get_unchecked(pc) }
}

#[inline]
pub fn k_val(constants: &[LuaValue], index: u32) -> &LuaValue {
    unsafe { constants.get_unchecked(index as usize) }
}

#[inline]
pub fn pk_val(constants: &[LuaValue], index: usize) -> StkId {
    StkId::from_const_ptr(unsafe { constants.get_unchecked(index) } as *const LuaValue)
}