Skip to main content

lua_vm/
debug.rs

1//! Debug interface — ported from `ldebug.c`.
2//!
3//! Provides the Lua debug API: stack inspection, source info, variable lookup,
4//! hook management, and runtime error formatting.
5//!
6//! # C source
7//! `reference/lua-5.4.7/src/ldebug.c` (962 lines, 30 functions)
8
9
10#[allow(unused_imports)] use crate::prelude::*;
11use crate::state::{
12    CallInfo, GcRef, LuaClosure, LuaClosureLua, LuaProto, LuaState, LuaTable, LuaValue,
13    CIST_FIN, CIST_HOOKED, CIST_HOOKYIELD, CIST_TAIL, CIST_TRAN,
14};
15use lua_types::{CallInfoIdx, StackIdx, LuaString};
16use lua_types::error::LuaError;
17use lua_types::opcode::Instruction;
18use crate::vm::InstructionExt;
19
20// TODO(port): the following are cross-crate imports that will resolve in Phase B:
21//   - LuaDebug  (lua_Debug struct; Phase E debug)
22//   - HookEvent (LUA_HOOKCALL / LUA_HOOKLINE / LUA_HOOKCOUNT constants)
23//   - LuaStatus (LUA_OK / LUA_YIELD / LUA_ERRRUN)
24//   - luaF_getlocalname — from crate::func
25//   - luaT_objtypename  — from crate::tagmethods
26//   - luaO_chunkid      — from crate::object
27//   - luaD_hookcall, luaD_hook, luaD_callnoyield — from crate::do_
28//   - luaH_setint       — from crate::table
29//   - luaV_tointegerns  — from crate::vm
30//   - OpCode, Instruction field accessors — from lua_code crate
31
32// ─── Constants from macros.tsv / ldebug.h ────────────────────────────────────
33
34// macros.tsv: ABSLINEINFO → const ABS_LINE_INFO: i8 = -0x80
35const ABS_LINE_INFO: i8 = -0x80_i8;
36
37// macros.tsv: MAXIWTHABS → const MAX_IWTH_ABS: i32 = 128
38const MAX_IWTH_ABS: i32 = 128;
39
40// TODO(port): import from lua_types or luaconf.h translation
41const LUA_IDSIZE: usize = 60;
42
43// TODO(port): import from HookEvent enum once defined
44const LUA_MASKLINE: u8 = 1 << 2;
45const LUA_MASKCOUNT: u8 = 1 << 3;
46
47const LUA_HOOKLINE: i32 = 2;
48const LUA_HOOKCOUNT: i32 = 3;
49
50// macros.tsv: LUA_ENV → const LUA_ENV: &[u8] = b"_ENV"
51const LUA_ENV: &[u8] = b"_ENV";
52
53// ─── Local error constructors (not yet in lua-types) ─────────────────────────
54
55/// Build a `LuaError::Runtime` from a raw byte-string message.
56///
57/// TODO(phase-b): expose as `LuaError::runtime_bytes` in lua-types once
58/// that crate has a `LuaString::from_bytes` constructor in its public API.
59fn runtime_bytes(msg: Vec<u8>) -> LuaError {
60    LuaError::Runtime(lua_types::LuaValue::Str(lua_types::GcRef::new(
61        lua_types::LuaString::from_bytes(msg),
62    )))
63}
64
65/// Prepend `[source]:line:` to `msg` when the current call frame is a Lua
66/// function. Mirrors what `luaG_addinfo` does for messages routed through
67/// `luaG_runerror`; the typed error constructors below build their own
68/// message and skip that path, so we add the same prefix here.
69/// Public wrapper for `prefixed_runtime` so other VM modules can re-prefix
70/// bare runtime errors raised from typed-arith helpers with the current call
71/// frame's `source:line:`.
72pub(crate) fn prefixed_runtime_pub(state: &LuaState, msg: Vec<u8>) -> LuaError {
73    prefixed_runtime(state, msg)
74}
75
76fn prefixed_runtime(state: &LuaState, msg: Vec<u8>) -> LuaError {
77    let ci_idx = state.current_ci_idx();
78    let ci = state.get_ci(ci_idx).clone();
79    if !ci.is_lua() {
80        return runtime_bytes(msg);
81    }
82    let proto = ci_lua_proto(&ci, state);
83    let src = proto.source_string();
84    let line = get_current_line(&ci, state);
85    let prefixed = add_info(
86        None,
87        &msg,
88        src.map(|s| &**s),
89        line,
90    );
91    runtime_bytes(prefixed)
92}
93
94pub fn c_api_runtime(state: &LuaState, msg: Vec<u8>) -> LuaError {
95    let ci_idx = state.current_ci_idx();
96    if let Some(parent_idx) = state.prev_ci(ci_idx) {
97        let parent_ci = state.get_ci(parent_idx).clone();
98        if parent_ci.is_lua() {
99            let proto = ci_lua_proto(&parent_ci, state);
100            let src = proto.source_string();
101            let line = get_current_line(&parent_ci, state);
102            let prefixed = add_info(None, &msg, src.map(|s| &**s), line);
103            return runtime_bytes(prefixed);
104        }
105    }
106    runtime_bytes(msg)
107}
108
109/// Walk a table's entries looking for `target` function (by identity).
110/// At `depth == 1`, also recurses one level into table-valued entries so that
111/// e.g. `_G.table.sort` can be found as `"table.sort"`.
112/// Returns the dotted path on success, `None` otherwise.
113/// Mirrors `ldblib.c:findfield` from reference C-Lua 5.4.
114///
115/// Not called from `arg_error_impl` (that path was removed to prevent stack
116/// overflow via re-entrant error generation). Reserved for a future
117/// `debug.findfield` Lua binding.
118#[allow(dead_code)]
119fn find_func_in_table(table: &LuaTable, target: &LuaValue, prefix: &[u8], depth: u8) -> Option<Vec<u8>> {
120    let mut key = LuaValue::Nil;
121    loop {
122        let (k, v) = match table.next_pair(&key) {
123            Some(pair) => pair,
124            None => break,
125        };
126        if !matches!(v, LuaValue::Nil) {
127            let key_bytes: Option<Vec<u8>> = match &k {
128                LuaValue::Str(s) => Some(s.as_bytes().to_vec()),
129                _ => None,
130            };
131            if let Some(kb) = key_bytes {
132                if &v == target {
133                    if prefix.is_empty() {
134                        return Some(kb);
135                    }
136                    let mut result = prefix.to_vec();
137                    result.push(b'.');
138                    result.extend_from_slice(&kb);
139                    return Some(result);
140                }
141                if depth > 0 {
142                    if let LuaValue::Table(sub) = &v {
143                        let new_prefix = if prefix.is_empty() {
144                            kb.clone()
145                        } else {
146                            let mut p = prefix.to_vec();
147                            p.push(b'.');
148                            p.extend_from_slice(&kb);
149                            p
150                        };
151                        if let Some(name) = find_func_in_table(&**sub, target, &new_prefix, depth - 1) {
152                            return Some(name);
153                        }
154                    }
155                }
156            }
157        }
158        key = k;
159    }
160    None
161}
162
163/// When `get_info` cannot resolve a function name (e.g. the function was called
164/// as a value from C code), walk `_G` to find its dotted path by identity.
165/// Returns `None` if not found; caller falls back to `"?"`.
166///
167/// Not called from `arg_error_impl` (that path was removed to prevent stack
168/// overflow via re-entrant error generation). Reserved for a future
169/// `debug.findfield` Lua binding.
170#[allow(dead_code)]
171fn find_func_name_in_globals(state: &LuaState, func_val: &LuaValue) -> Option<Vec<u8>> {
172    let globals = state.global().globals.clone();
173    if let LuaValue::Table(globals_table) = globals {
174        find_func_in_table(&*globals_table, func_val, b"", 1)
175    } else {
176        None
177    }
178}
179
180/// Mirrors C `pushglobalfuncname` (lauxlib.c): search `package.loaded` (the
181/// `_LOADED` registry entry) for `func_val` by identity.  Only descends one
182/// level into each loaded module, so `table.sort` is found as `"table.sort"`.
183///
184/// Uses only raw table lookups (`get_str_bytes`, `next_pair`) — no VM calls,
185/// no metamethods, no GC.  Safe to call from error-formatting paths.
186fn find_func_name_in_loaded(state: &LuaState, func_val: &LuaValue) -> Option<Vec<u8>> {
187    let registry = state.global().l_registry.clone();
188    let loaded = match registry {
189        LuaValue::Table(ref reg_table) => reg_table.get_str_bytes(b"_LOADED"),
190        _ => return None,
191    };
192    let loaded_table = match loaded {
193        LuaValue::Table(t) => t,
194        _ => return None,
195    };
196    find_func_in_table(&*loaded_table, func_val, b"", 1)
197}
198
199/// Equivalent of C `luaL_argerror`: build an arg-type error with function name
200/// (from debug info) and caller source location. Handles method calls by
201/// producing "calling 'f' on bad self ..." when arg==1 and namewhat=="method".
202pub fn arg_error_impl(state: &mut LuaState, mut arg: i32, extramsg: &[u8]) -> LuaError {
203    let mut ar = LuaDebug::default();
204    if !get_stack(state, 0, &mut ar) {
205        let msg = format!("bad argument #{} ({})", arg, String::from_utf8_lossy(extramsg));
206        return c_api_runtime(state, msg.into_bytes());
207    }
208    get_info(state, b"n", &mut ar);
209    if ar.namewhat.as_deref() == Some(b"method") {
210        arg -= 1;
211        if arg == 0 {
212            let name = ar.name.clone().unwrap_or_else(|| b"?".to_vec());
213            let msg = format!(
214                "calling '{}' on bad self ({})",
215                String::from_utf8_lossy(&name),
216                String::from_utf8_lossy(extramsg)
217            );
218            return c_api_runtime(state, msg.into_bytes());
219        }
220    }
221    let fname = ar.name.clone().or_else(|| {
222        let ci_idx = ar.i_ci?;
223        let func_slot = state.get_ci(ci_idx).func;
224        let func_val = state.get_at(func_slot).clone();
225        let found = find_func_name_in_loaded(state, &func_val)?;
226        if found.starts_with(b"_G.") {
227            Some(found[3..].to_vec())
228        } else {
229            Some(found)
230        }
231    }).unwrap_or_else(|| b"?".to_vec());
232    let msg = format!(
233        "bad argument #{} to '{}' ({})",
234        arg,
235        String::from_utf8_lossy(&fname),
236        String::from_utf8_lossy(extramsg)
237    );
238    c_api_runtime(state, msg.into_bytes())
239}
240
241// ─── Debug info structures ────────────────────────────────────────────────────
242
243/// Debug introspection record.
244///
245/// holds only the fields that `ldebug.c` writes/reads.
246///
247/// # Port note
248/// `name` and `namewhat` are optional byte strings because in C they can be
249/// NULL. `source` is owned here because we build it from Proto.source (a GcRef).
250/// `short_src` matches C layout as a fixed array.
251pub struct LuaDebug {
252    pub event: i32,
253    pub name: Option<Vec<u8>>,
254    pub namewhat: Option<&'static [u8]>,
255    pub what: Option<&'static [u8]>,
256    pub source: Option<Vec<u8>>,
257    pub srclen: usize,
258    pub currentline: i32,
259    pub linedefined: i32,
260    pub lastlinedefined: i32,
261    pub nups: u8,
262    pub nparams: u8,
263    pub isvararg: bool,
264    pub istailcall: bool,
265    pub ftransfer: u16,
266    pub ntransfer: u16,
267    pub short_src: [u8; LUA_IDSIZE],
268    // PORT NOTE: C stores a raw pointer; Rust stores an index into LuaState.call_stack.
269    pub i_ci: Option<CallInfoIdx>,
270}
271
272impl Default for LuaDebug {
273    fn default() -> Self {
274        LuaDebug {
275            event: 0,
276            name: None,
277            namewhat: None,
278            what: None,
279            source: None,
280            srclen: 0,
281            currentline: -1,
282            linedefined: -1,
283            lastlinedefined: -1,
284            nups: 0,
285            nparams: 0,
286            isvararg: false,
287            istailcall: false,
288            ftransfer: 0,
289            ntransfer: 0,
290            short_src: [0u8; LUA_IDSIZE],
291            i_ci: None,
292        }
293    }
294}
295
296// ─── File-local helper: is this a Lua (non-C) closure? ───────────────────────
297
298// macros.tsv: LUA_VLCL → LuaClosure::Lua(_)
299#[inline]
300fn is_lua_closure(cl: Option<&LuaClosure>) -> bool {
301    matches!(cl, Some(LuaClosure::Lua(_)))
302}
303
304// ─── Current-PC helpers ───────────────────────────────────────────────────────
305
306/// Returns the program counter (0-based instruction index) for the current
307/// instruction in call frame `ci`.
308///
309/// ```c
310/// lua_assert(isLua(ci));
311/// return pcRel(ci->u.l.savedpc, ci_func(ci)->p);
312/// ```
313///
314/// PORT NOTE: In C, `savedpc` is a pointer to the *next* instruction. `pcRel`
315/// subtracts the code base and then subtracts 1 more to get the *current*
316/// instruction. In Rust, `saved_pc()` stores the 0-based index of the next
317/// instruction, so the current instruction index is `saved_pc() - 1`.
318fn current_pc(ci: &CallInfo) -> i32 {
319    debug_assert!(ci.is_lua());
320    // macros.tsv: pcRel → (pc - proto.code_base()) as i32 - 1
321    // In Rust savedpc is a u32 offset into code[]; current = savedpc - 1
322    ci.saved_pc().saturating_sub(1) as i32
323}
324
325// ─── Line-info lookup ─────────────────────────────────────────────────────────
326
327/// Finds the "base line" entry in `f.abslineinfo` for instruction `pc`.
328///
329/// Sets `*basepc` to the pc of the base entry (or -1 if starting from the
330/// function's first line), and returns the line number at that base.
331///
332fn get_baseline(f: &LuaProto, pc: i32, basepc: &mut i32) -> i32 {
333    if f.abslineinfo.is_empty() || pc < f.abslineinfo[0].pc {
334        *basepc = -1;
335        return f.linedefined;
336    }
337    // macros.tsv: cast_uint(x) → x as u32
338    let mut i = (pc as u32 / MAX_IWTH_ABS as u32).saturating_sub(1) as usize;
339    debug_assert!(
340        i < f.abslineinfo.len() && f.abslineinfo[i].pc <= pc,
341        "getbaseline: estimate is not a lower bound"
342    );
343    while i + 1 < f.abslineinfo.len() && pc >= f.abslineinfo[i + 1].pc {
344        i += 1;
345    }
346    *basepc = f.abslineinfo[i].pc;
347    f.abslineinfo[i].line
348}
349
350/// Returns the source line number corresponding to instruction `pc` in proto `f`.
351/// Returns -1 if the proto has no debug line information.
352///
353pub(crate) fn get_func_line(f: &LuaProto, pc: i32) -> i32 {
354    if f.lineinfo.is_empty() {
355        return -1;
356    }
357    let mut basepc: i32 = 0;
358    let mut baseline = get_baseline(f, pc, &mut basepc);
359    // PORT NOTE: C uses post-increment `basepc++` in the condition; the body
360    // then uses the already-incremented value. Rewritten as pre-increment.
361    while basepc < pc {
362        basepc += 1;
363        debug_assert!(
364            f.lineinfo[basepc as usize] != ABS_LINE_INFO,
365            "get_func_line: hit ABSLINEINFO in incremental walk"
366        );
367        baseline += f.lineinfo[basepc as usize] as i32;
368    }
369    baseline
370}
371
372/// Returns the source line for the current instruction in call frame `ci`.
373///
374fn get_current_line(ci: &CallInfo, state: &LuaState) -> i32 {
375    let proto = ci_lua_proto(ci, state);
376    get_func_line(&proto, current_pc(ci))
377}
378
379// ─── Hook support ─────────────────────────────────────────────────────────────
380
381/// Sets the `trap` flag on every active Lua call frame so that the VM checks
382/// debug hooks before each instruction.
383///
384///
385/// PORT NOTE: In C this walks an intrusive doubly-linked list. In Rust,
386/// `LuaState.call_stack` is a `Vec<CallInfo>`, so we iterate the slice.
387fn set_traps(state: &mut LuaState) {
388    //      if (isLua(ci)) ci->u.l.trap = 1;
389    // TODO(port): call_stack iteration API not yet finalised; this will change
390    // when LuaState.call_stack is fully implemented.
391    for ci in state.call_stack_mut().iter_mut() {
392        if ci.is_lua() {
393            ci.set_trap(true);
394        }
395    }
396}
397
398/// Installs a debug hook on thread `state`.
399///
400pub fn set_hook(
401    state: &mut LuaState,
402    func: Option<Box<dyn FnMut(&mut LuaState, &LuaDebug)>>,
403    mask: i32,
404    count: i32,
405) {
406    let (func, mask) = if func.is_none() || mask == 0 {
407        (None, 0i32)
408    } else {
409        (func, mask)
410    };
411    state.set_hook(func);
412    state.set_base_hook_count(count);
413    // macros.tsv: resethookcount → state.reset_hook_count()
414    state.reset_hook_count();
415    // macros.tsv: cast_byte(x) → x as u8
416    state.set_hook_mask(mask as u8);
417    if mask != 0 {
418        set_traps(state);
419    }
420}
421
422/// Returns the current debug hook function, if any.
423///
424///
425/// TODO(port): In C this returns a `lua_Hook` function pointer. In Rust the hook
426/// is a `Box<dyn FnMut>` and cannot be returned by raw reference without
427/// restructuring; for now returns a bool indicating whether a hook is installed.
428pub fn get_hook_installed(state: &LuaState) -> bool {
429    state.hook().is_some()
430}
431
432/// Returns the current hook event mask.
433///
434pub fn get_hook_mask(state: &LuaState) -> i32 {
435    state.hook_mask() as i32
436}
437
438/// Returns the current hook call count.
439///
440pub fn get_hook_count(state: &LuaState) -> i32 {
441    state.base_hook_count()
442}
443
444// ─── Stack introspection ──────────────────────────────────────────────────────
445
446/// Fills `ar` with information about the call frame at depth `level`.
447/// Level 0 is the current running function, level 1 is the caller, etc.
448/// Returns `true` on success, `false` if the level is out of range.
449///
450pub fn get_stack(state: &LuaState, level: i32, ar: &mut LuaDebug) -> bool {
451    if level < 0 {
452        return false;
453    }
454    let mut remaining = level;
455    let mut ci_idx = state.current_ci_idx();
456    loop {
457        if remaining == 0 {
458            break;
459        }
460        match state.prev_ci(ci_idx) {
461            Some(prev) => {
462                ci_idx = prev;
463                remaining -= 1;
464            }
465            None => {
466                return false;
467            }
468        }
469    }
470    if !state.is_base_ci(ci_idx) {
471        ar.i_ci = Some(ci_idx);
472        true
473    } else {
474        false
475    }
476}
477
478// ─── Upvalue and local variable name lookup ───────────────────────────────────
479
480/// Returns the name of upvalue `uv` in proto `p` (as a byte slice), or `b"?"`.
481///
482fn upval_name(p: &LuaProto, uv: usize) -> &[u8] {
483    //    if (s == NULL) return "?"; else return getstr(s);
484    // macros.tsv: check_exp(c, e) → { debug_assert!(c); e }
485    debug_assert!(uv < p.upvalues.len(), "upval_name: index out of range");
486    // TODO(port): UpvalDesc.name is GcRef<LuaString>; calling .as_bytes() requires
487    // access to the interned string's data. Actual lifetime is tied to the GcRef.
488    p.upvalues[uv].name.as_ref().map_or(b"?" as &[u8], |s| s.as_bytes())
489}
490
491/// Finds the stack slot for vararg value number `n` (n is negative) in `ci`.
492/// Returns `Some(pos)` and the name `b"(vararg)"` if found, else `None`.
493///
494///
495/// PORT NOTE: C sets `*pos` as an out-parameter. Rust returns an Option of the
496/// stack index alongside the name.
497fn find_vararg(state: &LuaState, ci: &CallInfo, n: i32) -> Option<(StackIdx, &'static [u8])> {
498    let proto = ci_lua_proto(ci, state);
499    if proto.is_vararg {
500        let nextra = ci.nextra_args();
501        if n >= -(nextra as i32) {
502            // PORT NOTE: pointer arithmetic converted to index arithmetic.
503            // ci->func.p is the function slot; varargs are at func - nextra - 1 .. func - 1
504            let pos = ci.func - (nextra + n + 1);
505            return Some((pos, b"(vararg)" as &[u8]));
506        }
507    }
508    None
509}
510
511/// Finds the name and stack position for local variable `n` in call frame `ci`.
512///
513/// - If `n > 0`, looks up as a numbered local (1-based).
514/// - If `n < 0`, looks up as a vararg slot.
515/// - Returns `None` if no such variable exists.
516/// - If `pos` is `Some`, sets it to the variable's stack index.
517///
518///
519/// PORT NOTE: returns an owned `Vec<u8>` rather than `&[u8]`. The Lua-function
520/// case must call `get_local_name`, which returns a slice borrowed from a
521/// `GcRef<LuaProto>` that drops at function end — there is no caller lifetime
522/// the slice could be tied to. Cloning the name is cheap (a handful of bytes).
523pub(crate) fn find_local(
524    state: &LuaState,
525    ci_idx: CallInfoIdx,
526    n: i32,
527    pos: Option<&mut StackIdx>,
528) -> Option<Vec<u8>> {
529    let ci = state.get_ci(ci_idx);
530    let base = ci.func + 1;
531    let mut name: Option<Vec<u8>> = None;
532
533    if ci.is_lua() {
534        if n < 0 {
535            if let Some((vpos, vname)) = find_vararg(state, ci, n) {
536                if let Some(out_pos) = pos {
537                    *out_pos = vpos;
538                }
539                return Some(vname.to_vec());
540            }
541            return None;
542        } else {
543            let proto = ci_lua_proto(ci, state);
544            let pc = current_pc(ci);
545            name = crate::func::get_local_name(&proto, n, pc).map(|s| s.to_vec());
546        }
547    }
548
549    if name.is_none() {
550        let limit: u32 = if ci_idx == state.current_ci_idx() {
551            state.top_idx().0
552        } else {
553            ci.next
554                .map(|next| state.get_ci(next).func.0)
555                .unwrap_or_else(|| state.top_idx().0)
556        };
557        if n > 0 && limit.saturating_sub(base.0) >= n as u32 {
558            name = Some(if ci.is_lua() { b"(temporary)".to_vec() } else { b"(C temporary)".to_vec() });
559        } else {
560            return None;
561        }
562    }
563
564    if let Some(out_pos) = pos {
565        *out_pos = base + (n - 1);
566    }
567    name
568}
569
570/// Gets the name and value of local variable `n` in call frame `ar->i_ci`
571/// (or in the function at the top of the stack if `ar` is NULL).
572/// Pushes the value on the stack and returns its name, or returns `None`.
573///
574pub fn get_local(state: &mut LuaState, ar: Option<&LuaDebug>, n: i32) -> Option<Vec<u8>> {
575    if ar.is_none() {
576        // macros.tsv: isLfunction → matches!(o, LuaValue::Function(LuaClosure::Lua(_)))
577        let top_val = state.peek_top();
578        if !matches!(top_val, LuaValue::Function(LuaClosure::Lua(_))) {
579            return None;
580        }
581        // PORT NOTE: reshaped for borrowck — convert to owned Vec<u8> inside the
582        // block so `cl` (and the borrow through it) drop before we return.
583        let name_owned: Option<Vec<u8>> = {
584            let cl = match top_val {
585                LuaValue::Function(LuaClosure::Lua(ref cl)) => cl.clone(),
586                _ => unreachable!(),
587            };
588            // TODO(port): access proto from LuaClosureLua GcRef
589            get_local_name_from_closure(&cl, n, 0).map(|s| s.to_vec())
590        };
591        return name_owned;
592    }
593
594    let ar = ar.unwrap();
595    let ci_idx = ar.i_ci?;
596    let mut pos = StackIdx(0);
597    // PORT NOTE: reshaped for borrowck — clone name to an owned Vec<u8> so the
598    // immutable borrow of `state` ends before the mutable push below.
599    let name_owned: Option<Vec<u8>> = find_local(state, ci_idx, n, Some(&mut pos));
600
601    if name_owned.is_some() {
602        let val = state.get_at(pos).clone();
603        state.push(val);
604    }
605    name_owned
606}
607
608/// Sets local variable `n` in call frame `ar->i_ci` to the value on top of the
609/// stack. Pops the value and returns the variable name, or returns `None`.
610///
611pub fn set_local(state: &mut LuaState, ar: &LuaDebug, n: i32) -> Option<Vec<u8>> {
612    let ci_idx = ar.i_ci?;
613    let mut pos = StackIdx(0);
614    // PORT NOTE: reshaped for borrowck — clone name before mutably borrowing state.
615    let name_owned: Option<Vec<u8>> = find_local(state, ci_idx, n, Some(&mut pos));
616    if name_owned.is_some() {
617        let val = state.get_at(state.top_idx() - 1).clone();
618        state.set_at(pos, val);
619        state.pop_n(1);
620    }
621    name_owned
622}
623
624// ─── Function info helpers ────────────────────────────────────────────────────
625
626/// Fills the source/line fields of `ar` from closure `cl`.
627///
628fn func_info(ar: &mut LuaDebug, cl: Option<&LuaClosure>) {
629    if !is_lua_closure(cl) {
630        // macros.tsv: LL(x) → literal.len()
631        ar.source = Some(b"=[C]".to_vec());
632        ar.srclen = b"=[C]".len();
633        ar.linedefined = -1;
634        ar.lastlinedefined = -1;
635        ar.what = Some(b"C");
636    } else {
637        let lua_cl = match cl {
638            Some(LuaClosure::Lua(cl)) => cl,
639            _ => unreachable!(),
640        };
641        // TODO(port): access proto via GcRef<LuaProto>
642        let proto: &LuaProto = &lua_cl.proto;
643        // renders as "?". Stripped binary chunks commonly have no source.
644        if let Some(src) = proto.source_string() {
645            ar.source = Some(src.as_bytes().to_vec());
646            ar.srclen = src.as_bytes().len();
647        } else {
648            ar.source = Some(b"=?".to_vec());
649            ar.srclen = b"=?".len();
650        }
651        ar.linedefined = proto.linedefined;
652        ar.lastlinedefined = proto.lastlinedefined;
653        ar.what = Some(if ar.linedefined == 0 { b"main" } else { b"Lua" });
654    }
655    // TODO(port): luaO_chunkid lives in crate::object; call it once available
656    chunk_id(&mut ar.short_src, ar.source.as_deref().unwrap_or(b"?"), ar.srclen);
657}
658
659/// Returns the line number after advancing by one instruction from `currentline`.
660/// Handles the ABSLINEINFO sentinel by falling through to `get_func_line`.
661///
662fn next_line(p: &LuaProto, currentline: i32, pc: usize) -> i32 {
663    //    else return luaG_getfuncline(p, pc);
664    if p.lineinfo.get(pc).copied() != Some(ABS_LINE_INFO) {
665        currentline + p.lineinfo[pc] as i32
666    } else {
667        get_func_line(p, pc as i32)
668    }
669}
670
671/// Collects all source lines that are covered by instructions in closure `f`
672/// into a new table and pushes it on the stack (or pushes `nil` for C functions).
673///
674fn collect_valid_lines(state: &mut LuaState, cl: Option<&LuaClosure>) -> Result<(), LuaError> {
675    if !is_lua_closure(cl) {
676        // macros.tsv: setnilvalue → *o = LuaValue::Nil; api_incr_top → gone
677        state.push(LuaValue::Nil);
678        return Ok(());
679    }
680    let lua_cl = match cl {
681        Some(LuaClosure::Lua(cl)) => cl.clone(),
682        _ => unreachable!(),
683    };
684    // TODO(port): access proto via GcRef<LuaProto>
685    let proto: GcRef<LuaProto> = lua_cl.proto.clone();
686    let p: &LuaProto = &proto;
687
688    let mut currentline = p.linedefined;
689
690    // macros.tsv: luaH_new(L) → state.new_table()
691    let t = state.new_table();
692    // macros.tsv: sethvalue2s → state.set_at(o, LuaValue::Table(t.clone()))
693    state.push(LuaValue::Table(t.clone()));
694
695    if !p.lineinfo.is_empty() {
696        // macros.tsv: setbtvalue → *o = LuaValue::Bool(true)
697        let v = LuaValue::Bool(true);
698
699        let start_i = if !p.is_vararg {
700            0usize
701        } else {
702            // TODO(port): verify opcode — GET_OPCODE lives in lua_code crate
703            debug_assert!(
704                p.code.first().map(|i| i.is_vararg_prep()).unwrap_or(false),
705                "collect_valid_lines: first instruction of vararg should be OP_VARARGPREP"
706            );
707            currentline = next_line(p, currentline, 0);
708            1usize
709        };
710
711        // PORT NOTE: C iterates up to sizelineinfo (same as lineinfo.len() in Rust).
712        for i in start_i..p.lineinfo.len() {
713            currentline = next_line(p, currentline, i);
714            // TODO(port): luaH_setint lives in crate::table; stub call here
715            t.raw_set_int(state, currentline as i64, v.clone())?;
716        }
717    }
718    Ok(())
719}
720
721// ─── Function naming (symbolic execution) ────────────────────────────────────
722
723/// Tries to find a name for the function being called, based on the calling
724/// call frame `ci`. Returns `None` if the frame is tail-called or unavailable.
725///
726fn get_func_name<'a>(
727    state: &'a LuaState,
728    ci: Option<&CallInfo>,
729    name: &mut Option<Vec<u8>>,
730) -> Option<&'static [u8]> {
731    //      return funcnamefromcall(L, ci->previous, name);
732    //    else return NULL;
733    let ci = ci?;
734    if ci.callstatus & CIST_TAIL != 0 {
735        return None;
736    }
737    // TODO(port): ci->previous requires navigating call_stack by prev idx
738    // TODO(phase-b): get_prev_ci needs to accept &CallInfo or take the previous idx.
739    let prev_idx = ci.previous?;
740    let prev_ci = state.get_ci(prev_idx).clone();
741    funcname_from_call(state, &prev_ci, name)
742}
743
744/// Fills `ar` with the requested debug information about closure `f` / frame `ci`.
745///
746fn aux_get_info(
747    state: &LuaState,
748    what: &[u8],
749    ar: &mut LuaDebug,
750    cl: Option<&LuaClosure>,
751    ci: Option<&CallInfo>,
752) -> bool {
753    let mut status = true;
754    for &ch in what {
755        match ch {
756            b'S' => {
757                func_info(ar, cl);
758            }
759            b'l' => {
760                ar.currentline = match ci {
761                    Some(ci) if ci.is_lua() => get_current_line(ci, state),
762                    _ => -1,
763                };
764            }
765            b'u' => {
766                ar.nups = cl.map_or(0, |c| c.nupvalues() as u8);
767                match cl {
768                    Some(LuaClosure::Lua(lua_cl)) => {
769                        // TODO(port): access proto via GcRef<LuaProto>
770                        ar.isvararg = lua_cl.proto.is_vararg;
771                        ar.nparams = lua_cl.proto.numparams;
772                    }
773                    _ => {
774                        ar.isvararg = true;
775                        ar.nparams = 0;
776                    }
777                }
778            }
779            b't' => {
780                ar.istailcall = ci.map_or(false, |ci| ci.callstatus & CIST_TAIL != 0);
781            }
782            b'n' => {
783                let mut name: Option<Vec<u8>> = None;
784                ar.namewhat = get_func_name(state, ci, &mut name);
785                if ar.namewhat.is_none() {
786                    ar.namewhat = Some(b"");
787                    ar.name = None;
788                } else {
789                    ar.name = name;
790                }
791            }
792            //              else { ftransfer = ...; ntransfer = ...; }
793            b'r' => match ci {
794                Some(ci) if ci.callstatus & CIST_TRAN != 0 => {
795                    // TODO(port): ci->u2.transferinfo.ftransfer / ntransfer
796                    ar.ftransfer = ci.transfer_ftransfer();
797                    ar.ntransfer = ci.transfer_ntransfer();
798                }
799                _ => {
800                    ar.ftransfer = 0;
801                    ar.ntransfer = 0;
802                }
803            },
804            b'L' | b'f' => {}
805            _ => {
806                status = false;
807            }
808        }
809    }
810    status
811}
812
813/// Returns debug information about a function or active call frame.
814///
815pub fn get_info(state: &mut LuaState, what: &[u8], ar: &mut LuaDebug) -> bool {
816    let (cl, ci_idx, func_val, what) = if what.first() == Some(&b'>') {
817        let func_val = state.peek_at(state.top_idx() - 1).clone();
818        state.pop_n(1);
819        debug_assert!(
820            matches!(func_val, LuaValue::Function(_)),
821            "get_info: function expected"
822        );
823        let cl = match &func_val {
824            LuaValue::Function(LuaClosure::Lua(_) | LuaClosure::C(_)) => {
825                Some(match &func_val {
826                    LuaValue::Function(c) => c.clone(),
827                    _ => unreachable!(),
828                })
829            }
830            _ => None,
831        };
832        (cl, None, func_val, &what[1..])
833    } else {
834        let ci_idx = match ar.i_ci { Some(i) => i, None => return false };
835        let func_val = state.get_at(state.get_ci(ci_idx).func).clone();
836        debug_assert!(
837            matches!(func_val, LuaValue::Function(_)),
838            "get_info: non-function at ci->func"
839        );
840        let cl = match &func_val {
841            LuaValue::Function(LuaClosure::Lua(_) | LuaClosure::C(_)) => {
842                Some(match &func_val {
843                    LuaValue::Function(c) => c.clone(),
844                    _ => unreachable!(),
845                })
846            }
847            _ => None,
848        };
849        (cl, Some(ci_idx), func_val, what)
850    };
851
852    let ci = ci_idx.and_then(|idx| Some(state.get_ci(idx).clone()));
853    let status = aux_get_info(state, what, ar, cl.as_ref(), ci.as_ref());
854
855    if what.contains(&b'f') {
856        state.push(func_val);
857    }
858    if what.contains(&b'L') {
859        // TODO(port): propagate error from collect_valid_lines
860        let _ = collect_valid_lines(state, cl.as_ref());
861    }
862    status
863}
864
865// ─── Symbolic execution — finding which instruction set a register ────────────
866
867/// Filters a pc: if `pc` is inside a conditional branch (before `jmptarget`),
868/// returns -1 (unknown); otherwise returns `pc`.
869///
870#[inline]
871fn filter_pc(pc: i32, jmptarget: i32) -> i32 {
872    if pc < jmptarget { -1 } else { pc }
873}
874
875/// Finds the last instruction before `lastpc` that wrote to register `reg`.
876/// Returns the pc of that instruction, or -1 if not found.
877///
878fn find_set_reg(p: &LuaProto, lastpc: i32, reg: i32) -> i32 {
879    let mut setreg: i32 = -1;
880    let mut jmptarget: i32 = 0;
881
882    // macros.tsv: testMMMode(op) → (luaP_opmodes[op as usize] & (1 << 7)) != 0
883    // TODO(port): GET_OPCODE and opmode tests live in lua_code crate
884    let effective_lastpc = if p.code.get(lastpc as usize).map_or(false, |i| i.is_mm_mode()) {
885        lastpc - 1
886    } else {
887        lastpc
888    };
889
890    for pc in 0..effective_lastpc {
891        let instr = p.code[pc as usize];
892        let op = instr.opcode();
893        let a = instr.arg_a() as i32;
894
895        let change = match op {
896            OpCode::LoadNil => {
897                let b = instr.arg_b() as i32;
898                a <= reg && reg <= a + b
899            }
900            OpCode::TForCall => reg >= a + 2,
901            OpCode::Call | OpCode::TailCall => reg >= a,
902            OpCode::Jmp => {
903                let b = instr.arg_s_j();
904                let dest = pc + 1 + b;
905                if dest <= effective_lastpc && dest > jmptarget {
906                    jmptarget = dest;
907                }
908                false
909            }
910            _ => {
911                // macros.tsv: testAMode(op) → (luaP_opmodes[op as usize] & (1 << 3)) != 0
912                // TODO(port): opmode table lives in lua_code crate
913                instr.test_a_mode() && reg == a
914            }
915        };
916
917        if change {
918            setreg = filter_pc(pc, jmptarget);
919        }
920    }
921    setreg
922}
923
924/// Finds a "name" for the constant at `index` in proto `p`.
925/// Returns `Some("constant")` and sets `*name` to the string content,
926/// or returns `None` and sets `*name` to `"?"`.
927///
928fn kname<'a>(p: &'a LuaProto, index: usize, name: &mut &'a [u8]) -> Option<&'static [u8]> {
929    //    if (ttisstring(kvalue)) { *name = getstr(tsvalue(kvalue)); return "constant"; }
930    //    else { *name = "?"; return NULL; }
931    match p.k.get(index) {
932        Some(LuaValue::Str(s)) => {
933            // TODO(port): as_bytes() lifetime is tied to GcRef; revisit in Phase B
934            *name = s.as_bytes();
935            Some(b"constant")
936        }
937        _ => {
938            *name = b"?";
939            None
940        }
941    }
942}
943
944/// Tries to find a basic name for register `reg` in proto `p` at instruction `ppc`.
945/// Returns the "kind" of the name (e.g. "local", "upvalue", "constant"), or `None`.
946///
947fn basic_get_obj_name<'a>(
948    p: &'a LuaProto,
949    ppc: &mut i32,
950    reg: i32,
951    name: &mut &'a [u8],
952) -> Option<&'static [u8]> {
953    let pc = *ppc;
954    //    if (*name) return "local";
955    if let Some(local_name) = get_local_name(p, reg + 1, pc) {
956        *name = local_name;
957        return Some(b"local");
958    }
959
960    *ppc = find_set_reg(p, pc, reg);
961    let pc = *ppc;
962
963    if pc == -1 {
964        return None;
965    }
966
967    let instr = p.code[pc as usize];
968    let op = instr.opcode();
969    match op {
970        OpCode::Move => {
971            let b = instr.arg_b() as i32;
972            if b < instr.arg_a() as i32 {
973                return basic_get_obj_name(p, ppc, b, name);
974            }
975        }
976        OpCode::GetUpVal => {
977            *name = upval_name(p, instr.arg_b() as usize);
978            return Some(b"upvalue");
979        }
980        OpCode::LoadK => {
981            return kname(p, instr.arg_bx() as usize, name);
982        }
983        OpCode::LoadKx => {
984            let next = p.code[(pc + 1) as usize];
985            return kname(p, next.arg_ax() as usize, name);
986        }
987        _ => {}
988    }
989    None
990}
991
992/// Finds a name for a register-or-K instruction's `C` field (the key side).
993/// Stores a "constant name" if possible, otherwise `"?"`.
994///
995fn rname<'a>(p: &'a LuaProto, pc: i32, c: i32, name: &mut &'a [u8]) {
996    let mut pc = pc;
997    //    if (!(what && *what == 'c')) *name = "?";
998    let what = basic_get_obj_name(p, &mut pc, c, name);
999    if !matches!(what, Some(kind) if kind.first() == Some(&b'c')) {
1000        *name = b"?";
1001    }
1002}
1003
1004/// Finds the name for an RK-encoded `C` operand (either a constant or a register).
1005///
1006fn rkname<'a>(p: &'a LuaProto, pc: i32, instr: Instruction, name: &mut &'a [u8]) {
1007    let c = instr.arg_c() as i32;
1008    // macros.tsv: GETARG_k → i.arg_k() -> u32
1009    if instr.arg_k() != 0 {
1010        kname(p, c as usize, name);
1011    } else {
1012        rname(p, pc, c, name);
1013    }
1014}
1015
1016/// Determines whether the table indexed by instruction `i` is `_ENV`.
1017/// Returns `"global"` if so, `"field"` otherwise.
1018///
1019fn is_env<'a>(p: &'a LuaProto, pc: i32, instr: Instruction, isup: bool) -> &'static [u8] {
1020    let t = instr.arg_b() as usize;
1021    let mut name: &[u8] = b"?";
1022    if isup {
1023        name = upval_name(p, t);
1024    } else {
1025        let mut pc = pc;
1026        basic_get_obj_name(p, &mut pc, t as i32, &mut name);
1027    }
1028    if name == LUA_ENV { b"global" } else { b"field" }
1029}
1030
1031/// Extended version of `basic_get_obj_name` that also handles table accesses.
1032/// Returns the "kind" of name, or `None`.
1033///
1034fn get_obj_name<'a>(
1035    p: &'a LuaProto,
1036    lastpc: i32,
1037    reg: i32,
1038    name: &mut &'a [u8],
1039) -> Option<&'static [u8]> {
1040    let mut lastpc = lastpc;
1041    let kind = basic_get_obj_name(p, &mut lastpc, reg, name);
1042    if kind.is_some() {
1043        return kind;
1044    }
1045
1046    if lastpc == -1 {
1047        return None;
1048    }
1049
1050    let instr = p.code[lastpc as usize];
1051    let op = instr.opcode();
1052    match op {
1053        OpCode::GetTabUp => {
1054            let k = instr.arg_c() as usize;
1055            kname(p, k, name);
1056            Some(is_env(p, lastpc, instr, true))
1057        }
1058        OpCode::GetTable => {
1059            let k = instr.arg_c() as i32;
1060            rname(p, lastpc, k, name);
1061            Some(is_env(p, lastpc, instr, false))
1062        }
1063        OpCode::GetI => {
1064            *name = b"integer index";
1065            Some(b"field")
1066        }
1067        OpCode::GetField => {
1068            let k = instr.arg_c() as usize;
1069            kname(p, k, name);
1070            Some(is_env(p, lastpc, instr, false))
1071        }
1072        OpCode::Self_ => {
1073            rkname(p, lastpc, instr, name);
1074            Some(b"method")
1075        }
1076        _ => None,
1077    }
1078}
1079
1080// ─── Function naming ──────────────────────────────────────────────────────────
1081
1082/// Tries to derive a name for a function from the bytecode instruction that
1083/// called it. Returns the "kind" of call (e.g. "for iterator", "metamethod"),
1084/// or `None`.
1085///
1086fn funcname_from_code<'a>(
1087    state: &LuaState,
1088    p: &'a LuaProto,
1089    pc: i32,
1090    name: &mut Option<Vec<u8>>,
1091) -> Option<&'static [u8]> {
1092    let instr = p.code[pc as usize];
1093    let op = instr.opcode();
1094
1095    match op {
1096        OpCode::Call | OpCode::TailCall => {
1097            let mut name_bytes: &[u8] = b"?";
1098            let kind = get_obj_name(p, pc, instr.arg_a() as i32, &mut name_bytes);
1099            *name = Some(name_bytes.to_vec());
1100            kind
1101        }
1102        OpCode::TForCall => {
1103            *name = Some(b"for iterator".to_vec());
1104            Some(b"for iterator")
1105        }
1106        // Metamethod dispatch cases — look up tm name from GlobalState
1107        OpCode::Self_ | OpCode::GetTabUp | OpCode::GetTable | OpCode::GetI | OpCode::GetField => {
1108            get_tm_name(state, TagMethod::Index, name)
1109        }
1110        OpCode::SetTabUp | OpCode::SetTable | OpCode::SetI | OpCode::SetField => {
1111            get_tm_name(state, TagMethod::NewIndex, name)
1112        }
1113        OpCode::MmBin | OpCode::MmBinI | OpCode::MmBinK => {
1114            // macros.tsv: cast(TMS, x) → x as TagMethod
1115            // TODO(port): TagMethod::from_u8 needs to exist
1116            let tm_idx = instr.arg_c() as u8;
1117            let tm = TagMethod::from_u8(tm_idx);
1118            get_tm_name(state, tm, name)
1119        }
1120        OpCode::Unm => get_tm_name(state, TagMethod::Unm, name),
1121        OpCode::BNot => get_tm_name(state, TagMethod::BNot, name),
1122        OpCode::Len => get_tm_name(state, TagMethod::Len, name),
1123        OpCode::Concat => get_tm_name(state, TagMethod::Concat, name),
1124        OpCode::Eq => get_tm_name(state, TagMethod::Eq, name),
1125        OpCode::Lt | OpCode::LtI | OpCode::GtI => get_tm_name(state, TagMethod::Lt, name),
1126        OpCode::Le | OpCode::LeI | OpCode::GeI => get_tm_name(state, TagMethod::Le, name),
1127        OpCode::Close | OpCode::Return => get_tm_name(state, TagMethod::Close, name),
1128        _ => None,
1129    }
1130}
1131
1132/// Looks up the name for tag method `tm` from GlobalState and stores it in `*name`.
1133/// Returns `Some("metamethod")`.
1134///
1135/// PORT NOTE: `+2` skips the leading `__` prefix in C; here we strip it from
1136/// the byte slice.
1137fn get_tm_name(
1138    state: &LuaState,
1139    tm: TagMethod,
1140    name: &mut Option<Vec<u8>>,
1141) -> Option<&'static [u8]> {
1142    // macros.tsv: getshrstr(ts) → ts.as_bytes(); G → state.global()
1143    // PORT NOTE: reshaped for borrowck — tm_name returns Option<GcRef<LuaString>>;
1144    // materialise the bytes before stripping so there is no borrow of a temporary.
1145    let raw_bytes: Vec<u8> = state.global()
1146        .tm_name(tm)
1147        .map(|s| s.as_bytes().to_vec())
1148        .unwrap_or_default();
1149    let stripped = raw_bytes
1150        .strip_prefix(b"__")
1151        .unwrap_or(&raw_bytes)
1152        .to_vec();
1153    *name = Some(stripped);
1154    Some(b"metamethod")
1155}
1156
1157/// Tries to derive a name for a function from how it was called (`ci`).
1158///
1159fn funcname_from_call<'a>(
1160    state: &'a LuaState,
1161    ci: &CallInfo,
1162    name: &mut Option<Vec<u8>>,
1163) -> Option<&'static [u8]> {
1164    if ci.callstatus & CIST_HOOKED != 0 {
1165        *name = Some(b"?".to_vec());
1166        return Some(b"hook");
1167    }
1168    if ci.callstatus & CIST_FIN != 0 {
1169        *name = Some(b"__gc".to_vec());
1170        return Some(b"metamethod");
1171    }
1172    if ci.is_lua() {
1173        let proto = ci_lua_proto(ci, state);
1174        return funcname_from_code(state, &proto, current_pc(ci), name);
1175    }
1176    None
1177}
1178
1179// ─── Pointer-to-value tracking (varinfo for error messages) ──────────────────
1180
1181/// Checks whether value at stack index `val_idx` is in the call frame `ci`'s
1182/// register window, and if so returns the register index (0-based).
1183/// Returns -1 if not found.
1184///
1185///
1186/// PORT NOTE: In C this compares raw pointers. In Rust we compare StackIdx
1187/// values. The function signature changes: instead of a `*o` pointer we take
1188/// the StackIdx of the value directly.
1189fn in_stack(ci: &CallInfo, val_idx: StackIdx) -> i32 {
1190    let base = StackIdx(ci.func.0 + 1);
1191    // TODO(port): in C this is a pointer-identity check (`o == s2v(base+pos)`).
1192    // In Rust, `val_idx` IS a StackIdx; we just check whether it falls in range.
1193    let ci_top = ci.top;
1194    let mut pos = 0i32;
1195    let mut cur = base;
1196    while cur.0 < ci_top.0 {
1197        if cur == val_idx {
1198            return pos;
1199        }
1200        cur = StackIdx(cur.0 + 1);
1201        pos += 1;
1202    }
1203    -1
1204}
1205
1206/// Checks whether `val_idx` is the current value of one of the upvalues in the
1207/// Lua closure at `ci`. If so, sets `*name` and returns `Some("upvalue")`.
1208///
1209///
1210/// PORT NOTE: In C this compares `c->upvals[i]->v.p == o` (pointer identity on
1211/// open upvalues or the closed slot). In Rust, open upvalues hold a StackIdx; we
1212/// compare that against `val_idx`. Closed upvalues cannot be identified by stack
1213/// position, so they are not matched here.
1214fn get_upval_name<'a>(
1215    ci: &CallInfo,
1216    val_idx: StackIdx,
1217    name: &mut &'a [u8],
1218    state: &'a LuaState,
1219) -> Option<&'static [u8]> {
1220    let proto = ci_lua_proto(ci, state);
1221    // TODO(port): actual upvalue objects require ci.lua_closure() on the LuaState;
1222    // this is a best-effort translation
1223    let lua_cl = match state.get_at(ci.func) {
1224        LuaValue::Function(LuaClosure::Lua(cl)) => cl.clone(),
1225        _ => return None,
1226    };
1227    for (i, upval_slot) in lua_cl.upvals.iter().enumerate() {
1228        let upval = upval_slot.get();
1229        let state = upval.slot().clone();
1230        if let lua_types::UpValState::Open { idx, .. } = state {
1231            if idx == val_idx {
1232                // TODO(phase-b): the name needs to be tied to state's lifetime; using
1233                // a static fallback keeps the trait bounds satisfied for now.
1234                let _ = upval_name(&proto, i);
1235                *name = b"upvalue";
1236                return Some(b"upvalue");
1237            }
1238        }
1239    }
1240    None
1241}
1242
1243/// Builds a human-readable "variable info" string like ` (local 'x')` or
1244/// ` (upvalue 'y')` to append to error messages. Returns an empty `Vec<u8>`
1245/// if no information is available.
1246///
1247fn format_var_info(kind: Option<&[u8]>, name: Option<&[u8]>) -> Vec<u8> {
1248    match (kind, name) {
1249        (Some(k), Some(n)) => {
1250            let mut out = Vec::with_capacity(4 + k.len() + n.len());
1251            out.extend_from_slice(b" (");
1252            out.extend_from_slice(k);
1253            out.extend_from_slice(b" '");
1254            out.extend_from_slice(n);
1255            out.extend_from_slice(b"')");
1256            out
1257        }
1258        _ => Vec::new(),
1259    }
1260}
1261
1262/// Returns a description string for the value at `val_idx` in the current call
1263/// frame, e.g. `" (local 'x')"` or `" (upvalue 'y')"`. Used in error messages.
1264///
1265fn var_info(state: &LuaState, val_idx: StackIdx) -> Vec<u8> {
1266    let ci_idx = state.current_ci_idx();
1267    let ci = state.get_ci(ci_idx).clone();
1268    let mut kind: Option<&[u8]> = None;
1269    let mut name_owned: Vec<u8> = b"?".to_vec();
1270
1271    if ci.is_lua() {
1272        let mut up_name: &[u8] = b"?";
1273        kind = get_upval_name(&ci, val_idx, &mut up_name, state);
1274        if kind.is_some() {
1275            name_owned = up_name.to_vec();
1276        } else {
1277            let reg = in_stack(&ci, val_idx);
1278            if reg >= 0 {
1279                let proto = ci_lua_proto(&ci, state);
1280                let mut nref: &[u8] = b"?";
1281                let pc = current_pc(&ci);
1282                let k = get_obj_name(&proto, pc, reg, &mut nref);
1283                kind = k;
1284                if kind.is_some() {
1285                    name_owned = nref.to_vec();
1286                }
1287            }
1288        }
1289    }
1290    format_var_info(kind, if kind.is_some() { Some(&name_owned) } else { None })
1291}
1292
1293// ─── Error-raising functions ──────────────────────────────────────────────────
1294
1295/// Internal helper: raises a type error with the given `extra` info string.
1296///
1297fn typeerror_inner(
1298    state: &LuaState,
1299    val: &LuaValue,
1300    op: &[u8],
1301    extra: &[u8],
1302) -> LuaError {
1303    let t = state.obj_type_name(val);
1304    let mut msg = Vec::new();
1305    msg.extend_from_slice(b"attempt to ");
1306    msg.extend_from_slice(op);
1307    msg.extend_from_slice(b" a ");
1308    msg.extend_from_slice(&t);
1309    msg.extend_from_slice(b" value");
1310    msg.extend_from_slice(extra);
1311    prefixed_runtime(state, msg)
1312}
1313
1314/// Raises a type error for performing operation `op` on value `val`.
1315/// Includes variable-info context (e.g. "local 'x'") if available.
1316///
1317pub(crate) fn type_error(state: &LuaState, val: &LuaValue, val_idx: StackIdx, op: &[u8]) -> LuaError {
1318    let extra = var_info(state, val_idx);
1319    typeerror_inner(state, val, op, &extra)
1320}
1321
1322/// Variant of `type_error` for bytecode paths where the target isn't on the
1323/// active stack — OP_SETTABUP / OP_GETTABUP read directly from the closure's
1324/// upvalue cells, so `var_info`'s in-stack heuristic can't recover the name.
1325/// The caller passes a pre-formatted `(kind, name)` pair (e.g.
1326/// `(b"upvalue", b"a")`) used verbatim in the trailing `(kind 'name')`.
1327pub(crate) fn type_error_with_hint(
1328    state: &LuaState,
1329    val: &LuaValue,
1330    op: &[u8],
1331    kind: &[u8],
1332    name: &[u8],
1333) -> LuaError {
1334    let extra = format_var_info(Some(kind), Some(name));
1335    let t = obj_type_name_static(val);
1336    let mut msg = Vec::new();
1337    msg.extend_from_slice(b"attempt to ");
1338    msg.extend_from_slice(op);
1339    msg.extend_from_slice(b" a ");
1340    msg.extend_from_slice(t);
1341    msg.extend_from_slice(b" value");
1342    msg.extend_from_slice(&extra);
1343    prefixed_runtime(state, msg)
1344}
1345
1346/// Standalone type-name accessor that does not require `&LuaState`. Used by
1347/// `type_error_with_hint` since callers there cannot easily thread `state`.
1348fn obj_type_name_static(val: &LuaValue) -> &'static [u8] {
1349    match val {
1350        LuaValue::Nil => b"nil",
1351        LuaValue::Bool(_) => b"boolean",
1352        LuaValue::Int(_) | LuaValue::Float(_) => b"number",
1353        LuaValue::Str(_) => b"string",
1354        LuaValue::Table(_) => b"table",
1355        LuaValue::Function(_) => b"function",
1356        LuaValue::UserData(_) => b"userdata",
1357        LuaValue::LightUserData(_) => b"light userdata",
1358        LuaValue::Thread(_) => b"thread",
1359    }
1360}
1361
1362/// Raises a "call" type error for a non-callable `val`.
1363/// Prefers name from `funcnamefromcall`; falls back to `varinfo`.
1364///
1365pub(crate) fn call_error(state: &LuaState, val: &LuaValue, val_idx: StackIdx) -> LuaError {
1366    let ci_idx = state.current_ci_idx();
1367    let ci = state.get_ci(ci_idx).clone();
1368    let mut name: Option<Vec<u8>> = None;
1369    let kind = funcname_from_call(state, &ci, &mut name);
1370    let extra = if kind.is_some() {
1371        format_var_info(kind, name.as_deref())
1372    } else {
1373        var_info(state, val_idx)
1374    };
1375    typeerror_inner(state, val, b"call", &extra)
1376}
1377
1378/// Raises a "bad 'for' <what>" error.
1379///
1380pub(crate) fn for_error(state: &mut LuaState, val: &LuaValue, what: &[u8]) -> LuaError {
1381    let t = crate::tagmethods::obj_type_name(state, val)
1382        .unwrap_or_else(|_| crate::tagmethods::type_name(val.base_type()).to_vec());
1383    let mut msg = Vec::new();
1384    msg.extend_from_slice(b"bad 'for' ");
1385    msg.extend_from_slice(what);
1386    msg.extend_from_slice(b" (number expected, got ");
1387    msg.extend_from_slice(&t);
1388    msg.push(b')');
1389    prefixed_runtime(state, msg)
1390}
1391
1392/// Raises an arithmetic type error. If `p1` is not a number, blames `p1`;
1393/// otherwise blames `p2`.
1394///
1395pub(crate) fn op_int_error(
1396    state: &LuaState,
1397    p1: &LuaValue,
1398    p1_idx: StackIdx,
1399    p2: &LuaValue,
1400    p2_idx: StackIdx,
1401    msg: &[u8],
1402) -> LuaError {
1403    // macros.tsv: ttisnumber → matches!(o, LuaValue::Int(_) | LuaValue::Float(_))
1404    let (bad_val, bad_idx) = if !matches!(p1, LuaValue::Int(_) | LuaValue::Float(_)) {
1405        (p1, p1_idx)
1406    } else {
1407        (p2, p2_idx)
1408    };
1409    type_error(state, bad_val, bad_idx, msg)
1410}
1411
1412/// Raises an "no integer representation" error for float→int conversion failure.
1413///
1414///
1415/// Stack indices are optional: when an operand is from a constant table or
1416/// an immediate, no register backs it and `var_info` has nothing to report.
1417pub(crate) fn to_int_error(
1418    state: &LuaState,
1419    p1: &LuaValue,
1420    p1_idx: Option<StackIdx>,
1421    _p2: &LuaValue,
1422    p2_idx: Option<StackIdx>,
1423) -> LuaError {
1424    let bad_idx = if p1.to_integer_no_strconv().is_none() {
1425        p1_idx
1426    } else {
1427        p2_idx
1428    };
1429    let extra = match bad_idx {
1430        Some(idx) => var_info(state, idx),
1431        None => Vec::new(),
1432    };
1433    let mut msg = Vec::new();
1434    msg.extend_from_slice(b"number");
1435    msg.extend_from_slice(&extra);
1436    msg.extend_from_slice(b" has no integer representation");
1437    prefixed_runtime(state, msg)
1438}
1439
1440/// Raises an order-comparison type error for incompatible types.
1441///
1442pub(crate) fn order_error(state: &LuaState, p1: &LuaValue, p2: &LuaValue) -> LuaError {
1443    // TODO(port): obj_type_name lives in crate::tagmethods
1444    let t1 = state.obj_type_name(p1);
1445    let t2 = state.obj_type_name(p2);
1446    //    else                      luaG_runerror(L, "attempt to compare %s with %s", t1, t2);
1447    let msg = if t1 == t2 {
1448        let mut m = Vec::new();
1449        m.extend_from_slice(b"attempt to compare two ");
1450        m.extend_from_slice(&t1);
1451        m.extend_from_slice(b" values");
1452        m
1453    } else {
1454        let mut m = Vec::new();
1455        m.extend_from_slice(b"attempt to compare ");
1456        m.extend_from_slice(&t1);
1457        m.extend_from_slice(b" with ");
1458        m.extend_from_slice(&t2);
1459        m
1460    };
1461    prefixed_runtime(state, msg)
1462}
1463
1464/// Prepends `src:line: ` to `msg` (as a new Lua string on the stack) and
1465/// returns the formatted string.
1466///
1467///
1468/// The C signature takes `lua_State *L` because the result is pushed onto the
1469/// Lua stack via `luaO_pushfstring`. Our port returns `Vec<u8>` instead, so
1470/// the state parameter is unused — keep an optional reference for callers
1471/// that still pass one, but the function works without it.
1472pub(crate) fn add_info(
1473    _state: Option<&mut LuaState>,
1474    msg: &[u8],
1475    src: Option<&LuaString>,
1476    line: i32,
1477) -> Vec<u8> {
1478    //    else { buff[0] = '?'; buff[1] = '\0'; }
1479    let mut buff = [0u8; LUA_IDSIZE];
1480    if let Some(src) = src {
1481        // macros.tsv: getstr(ts) → ts.as_bytes(); tsslen(ts) → ts.len()
1482        // TODO(port): luaO_chunkid lives in crate::object
1483        chunk_id(&mut buff, src.as_bytes(), src.len());
1484    } else {
1485        buff[0] = b'?';
1486    }
1487    // PORT NOTE: Instead of pushing on the stack, we return the formatted Vec<u8>.
1488    // Callers that need the result on the stack should push it themselves.
1489    let src_part = buff.iter().position(|&b| b == 0).map_or(&buff[..], |n| &buff[..n]);
1490    let mut out = Vec::with_capacity(src_part.len() + 12 + msg.len());
1491    out.extend_from_slice(src_part);
1492    out.push(b':');
1493    // Write line number as decimal bytes
1494    let line_str = line.to_string();
1495    out.extend_from_slice(line_str.as_bytes());
1496    out.extend_from_slice(b": ");
1497    out.extend_from_slice(msg);
1498    out
1499}
1500
1501
1502// ─── Line change detection ────────────────────────────────────────────────────
1503
1504/// Checks whether instruction `newpc` is on a different source line than `oldpc`.
1505///
1506fn changed_line(p: &LuaProto, oldpc: i32, newpc: i32) -> bool {
1507    if p.lineinfo.is_empty() {
1508        return false;
1509    }
1510
1511    if newpc - oldpc < MAX_IWTH_ABS / 2 {
1512        let mut delta: i32 = 0;
1513        let mut pc = oldpc;
1514        loop {
1515            pc += 1;
1516            if pc as usize >= p.lineinfo.len() {
1517                break;
1518            }
1519            let lineinfo = p.lineinfo[pc as usize];
1520            if lineinfo == ABS_LINE_INFO {
1521                break;
1522            }
1523            delta += lineinfo as i32;
1524            if pc == newpc {
1525                return delta != 0;
1526            }
1527        }
1528    }
1529    get_func_line(p, oldpc) != get_func_line(p, newpc)
1530}
1531
1532// ─── Trace execution hooks ────────────────────────────────────────────────────
1533
1534/// Called at the start of a Lua function. Fires the call hook if appropriate.
1535/// Returns 1 to keep the trap on, 0 to turn it off.
1536///
1537pub(crate) fn trace_call(state: &mut LuaState) -> Result<i32, LuaError> {
1538    let ci_idx = state.current_ci_idx();
1539    let ci = state.get_ci(ci_idx).clone();
1540    state.get_ci_mut(ci_idx).set_trap(true);
1541    let proto = ci_lua_proto(&ci, state);
1542
1543    if ci.saved_pc() == 0 {
1544        if proto.is_vararg {
1545            return Ok(0);
1546        } else if ci.callstatus & CIST_HOOKYIELD == 0 {
1547            // TODO(port): luaD_hookcall lives in crate::do_
1548            state.hook_call(ci_idx)?;
1549        }
1550    }
1551    Ok(1)
1552}
1553
1554/// Called before each VM instruction when debugging is active.
1555/// Fires line and count hooks as appropriate.
1556/// Returns 1 to keep trap on, 0 to turn it off.
1557///
1558///
1559/// PORT NOTE: The C `pc` parameter is a pointer to the instruction array.
1560/// In Rust, `pc` is the 0-based index of the NEXT instruction (same semantic as
1561/// `savedpc`). After incrementing for reference (`pc++` in C), it equals
1562/// the next-instruction index.
1563pub(crate) fn trace_exec(state: &mut LuaState, pc: u32) -> Result<i32, LuaError> {
1564    let ci_idx = state.current_ci_idx();
1565    let ci = state.get_ci(ci_idx).clone();
1566
1567    let mask = state.hook_mask();
1568
1569    if !state.allowhook {
1570        return Ok(1);
1571    }
1572
1573    if mask & (LUA_MASKLINE | LUA_MASKCOUNT) == 0 {
1574        state.get_ci_mut(ci_idx).set_trap(false);
1575        return Ok(0);
1576    }
1577
1578    let next_pc = pc + 1;
1579    state.get_ci_mut(ci_idx).set_saved_pc(next_pc);
1580
1581    let counthook = if mask & LUA_MASKCOUNT != 0 {
1582        let hc = state.hook_count() - 1;
1583        state.set_hook_count(hc);
1584        hc == 0
1585    } else {
1586        false
1587    };
1588
1589    if counthook {
1590        state.reset_hook_count();
1591    } else if mask & LUA_MASKLINE == 0 {
1592        return Ok(1);
1593    }
1594
1595    if ci.callstatus & CIST_HOOKYIELD != 0 {
1596        state.get_ci_mut(ci_idx).callstatus &= !CIST_HOOKYIELD;
1597        return Ok(1);
1598    }
1599
1600    if state.ci_lua_closure(ci_idx).is_none() {
1601        return Ok(1);
1602    }
1603
1604    // macros.tsv: isIT(i) → i.is_in_top()
1605    // PORT NOTE: savedpc - 1 is the current instruction (now at index next_pc - 1 = pc).
1606    let cur_instr = state.get_proto_instr(ci_idx, pc as u32);
1607    if !cur_instr.is_in_top() {
1608        let ci_top = state.get_ci(ci_idx).top;
1609        state.set_top(ci_top);
1610    }
1611
1612    if counthook {
1613        // TODO(port): luaD_hook lives in crate::do_
1614        state.call_hook_event(LUA_HOOKCOUNT, -1)?;
1615    }
1616
1617    if mask & LUA_MASKLINE != 0 {
1618        let proto = ci_lua_proto(&ci, state);
1619        let oldpc = if state.old_pc() < proto.code.len() as u32 {
1620            state.old_pc() as i32
1621        } else {
1622            0
1623        };
1624        // current instruction is pc (0-based); pcRel gives current = next - 1
1625        let npci = next_pc as i32 - 1;
1626
1627        if npci <= oldpc || changed_line(&proto, oldpc, npci) {
1628            let newline = get_func_line(&proto, npci);
1629            // TODO(port): luaD_hook lives in crate::do_
1630            state.call_hook_event(LUA_HOOKLINE, newline)?;
1631        }
1632        state.set_old_pc(npci as u32);
1633    }
1634
1635    if state.status() == lua_types::status::LuaStatus::Yield {
1636        if counthook {
1637            state.set_hook_count(1);
1638        }
1639        state.get_ci_mut(ci_idx).callstatus |= CIST_HOOKYIELD;
1640        // error_sites.tsv: luaD_throw(L, LUA_YIELD) → return Err(LuaError::with_status(LuaStatus::Yield))
1641        return Err(LuaError::Yield);
1642    }
1643
1644    Ok(1)
1645}
1646
1647// ─── File-local helpers referenced above but not directly translated ──────────
1648
1649/// Gets the source line name (short, truncated) for error messages.
1650///
1651/// to the real impl in `crate::object`. Handles `=name`, `@filename`, and
1652/// `[string "..."]` formatting so error prefixes are concise rather than dumping
1653/// the entire source verbatim.
1654fn chunk_id(out: &mut [u8; LUA_IDSIZE], source: &[u8], _srclen: usize) {
1655    out.fill(0);
1656    let n = crate::object::chunk_id(&mut out[..], source);
1657    if n < out.len() {
1658        out[n] = 0;
1659    }
1660}
1661
1662/// Gets the local variable name for register `reg+1` at instruction `pc` in `p`.
1663/// Returns `None` if not found (variable is not live at `pc`).
1664///
1665fn get_local_name(p: &LuaProto, n: i32, pc: i32) -> Option<&[u8]> {
1666    crate::func::get_local_name(p, n, pc)
1667}
1668
1669/// Gets the n-th local name from a Lua closure (for non-active function query).
1670fn get_local_name_from_closure(cl: &LuaClosureLua, n: i32, pc: i32) -> Option<&[u8]> {
1671    get_local_name(&cl.proto, n, pc)
1672}
1673
1674/// Retrieves the LuaProto for the Lua closure at `ci.func` from the stack.
1675///
1676/// macros.tsv: ci_func → ci.lua_closure() returning &GcRef<LuaClosure::Lua>
1677///
1678/// PORT NOTE: The C version returns a raw pointer and is a macro. Here we
1679/// navigate through the LuaState stack. Returns a reference with the
1680/// lifetime of the proto inside the GcRef (Rc), which must remain valid.
1681///
1682/// TODO(port): This returns a cloned Rc's inner reference; Phase B must verify
1683/// lifetimes are correct once all types are wired.
1684/// PORT NOTE: reshaped for borrowck — returns `GcRef<LuaProto>` (Rc clone) instead
1685/// of `&'a LuaProto` to avoid returning a reference to a temporary `LuaValue`
1686/// produced by `get_at`. Callers deref through `GcRef<T>: Deref<Target=T>`.
1687fn ci_lua_proto(ci: &CallInfo, state: &LuaState) -> GcRef<LuaProto> {
1688    match state.get_at(ci.func) {
1689        LuaValue::Function(LuaClosure::Lua(cl)) => cl.proto.clone(),
1690        _ => panic!("ci_lua_proto: call frame does not hold a Lua closure"),
1691    }
1692}
1693
1694// ──────────────────────────────────────────────────────────────────────────────
1695// PORT STATUS
1696//   source:        src/ldebug.c  (962 lines, 30 functions)
1697//   target_crate:  lua-vm
1698//   confidence:    medium
1699//   todos:         44
1700//   port_notes:    15
1701//   unsafe_blocks: 0
1702//   notes:         Logic faithful to C; cross-crate imports (luaF_*, luaT_*,
1703//                  luaD_*, luaO_chunkid, opcode accessors) are stubbed with
1704//                  TODO(port) markers. LuaState accessor methods (call_stack_mut,
1705//                  get_ci, set_trap, saved_pc, hook_mask, etc.) are called as if
1706//                  defined in state.rs — Phase B must implement them. The
1707//                  pointer-identity comparisons in instack/getupvalname are
1708//                  translated to StackIdx comparisons (a structural change).
1709//                  `lua_gethook` returns a bool instead of a fn pointer because
1710//                  Box<dyn FnMut> cannot be returned by value without restructuring.
1711//                  rustc check: zero real syntax errors; all 67 diagnostics are
1712//                  expected name-resolution errors (E0432/E0433/E0425/E0282).
1713// ──────────────────────────────────────────────────────────────────────────────