Skip to main content

lua_vm/
func.rs

1//! Auxiliary functions to manipulate prototypes and closures.
2//!
3//! Port of `reference/lua-5.4.7/src/lfunc.c` (295 lines, 16 functions).
4//! The companion header `lfunc.h` is merged here per PORTING.md §1.
5//!
6//! # Design notes
7//!
8//! The C implementation uses two intrusive linked lists managed through pointer
9//! fields embedded in stack slots and upvalue objects:
10//!
11//! - **`openupval`**: a singly-linked list of `UpVal`s sorted by stack level
12//!   (highest first), threaded through `UpVal.u.open.next / .previous`.
13//! - **`tbclist`**: a to-be-closed variable list encoded as `unsigned short` delta
14//!   offsets stored inside `StackValue.tbclist.delta`.
15//!
16//! Both are replaced in the Rust port:
17//! - `openupval` → `LuaState.openupval: Vec<GcRef<UpVal>>` (descending by StackIdx).
18//! - `tbclist`   → `LuaState.tbclist: Vec<StackIdx>` (back = most recent entry).
19//!
20//! The delta-encoding machinery (MAXDELTA, dummy nodes) is an artifact of the u16
21//! delta field and is entirely superseded by the `Vec<StackIdx>` model.
22
23// PORT NOTE: `LuaProto` is currently a stub in crate::state (from lstate.c's
24// partial port in state.rs). The full `LuaProto` definition belongs in
25// crate::object (lobject.c → object.rs). Fields referenced below will compile
26// once object.rs is written; see TODO(port) at each field site.
27
28// PORT NOTE: `GcRef<T> = Rc<T>` in Phase A–C provides no interior mutability.
29// `close_upval` and `init_upvals` must mutate `UpVal` and `LuaClosure` values
30// that are shared through `GcRef`. In Phase B, the design options are:
31//   (a) `GcRef<T> = Rc<RefCell<T>>` for mutable GC objects, or
32//   (b) a custom `GcCell<T>` wrapper with conditional interior mutability.
33// Both `close_upval` and `init_upvals` carry `TODO(port)` at the mutation sites.
34
35#[allow(unused_imports)]
36use crate::prelude::*;
37
38use crate::state::{GcRef, LuaState, LuaValue, UpVal};
39use lua_types::error::LuaError;
40pub use lua_types::{CallInfoIdx, StackIdx};
41
42// ── lfunc.h constants ─────────────────────────────────────────────────────────
43
44// macros.tsv: CLOSEKTOP → const CLOSE_K_TOP: i32 = -1
45/// Sentinel status meaning "close upvalues but preserve the stack top."
46/// Passed as `status` to `close` / `prep_call_close_mth`.
47pub(crate) const CLOSE_K_TOP: i32 = -1;
48
49// ── Closure allocation ────────────────────────────────────────────────────────
50
51/// Fills a Lua closure's upvalue slots with freshly-allocated closed upvalues,
52/// each holding `LuaValue::Nil`. Used when compiling closures that capture no
53/// live stack variables.
54///
55pub(crate) fn init_upvals(
56    state: &mut LuaState,
57    cl: &GcRef<lua_types::LuaLClosure>,
58) -> Result<(), LuaError> {
59    //      GCObject *o = luaC_newobj(L, LUA_VUPVAL, sizeof(UpVal));
60    //      UpVal *uv = gco2upv(o);
61    //      uv->v.p = &uv->u.value;  /* make it closed */
62    //      setnilvalue(uv->v.p);    /* *o = LuaValue::Nil */
63    //      cl->upvals[i] = uv;
64    //      luaC_objbarrier(L, cl, uv);
65    //  }
66    //
67    // In Rust: create UpVal::Closed(Nil) for each slot; GC barrier is no-op Phase A–C.
68
69    // TODO(port): GcRef<T> = Rc<T> has no interior mutability. Mutating
70    // `cl.upvals[i]` here requires either Rc<RefCell<LuaClosure>> or Rc::get_mut.
71    // The code below captures the intended logic; it will not compile until
72    // GcRef provides a borrow_mut() path (Phase B design decision).
73    let n = cl.upvals.len();
74    for i in 0..n {
75        let uv: GcRef<UpVal> = state.new_upval_closed(LuaValue::Nil);
76        // TODO(port): cl.borrow_mut().as_lua_mut().upvals[i] = Some(uv.clone());
77        // Requires interior mutability; see PORT NOTE at top of file.
78        let _ = (i, uv);
79    }
80    Ok(())
81}
82
83// ── Open-upvalue management ───────────────────────────────────────────────────
84
85/// Creates a new open upvalue for stack slot `level`, inserts it into
86/// `state.openupval` at `insert_pos`, and registers the thread in the
87/// global `twups` list if necessary.
88///
89fn new_open_upval(state: &mut LuaState, level: StackIdx, insert_pos: usize) -> GcRef<UpVal> {
90    //    UpVal *uv = gco2upv(o);
91    //    UpVal *next = *prev;
92    //    uv->v.p = s2v(level);   /* current value lives in the stack */
93    //    uv->u.open.next = next;
94    //    uv->u.open.previous = prev;
95    //    if (next) next->u.open.previous = &uv->u.open.next;
96    //    *prev = uv;
97    //
98    // In Rust: intrusive next/previous fields are gone; Vec insertion replaces
99    // the pointer-threading. The `prev` parameter (UpVal **) becomes `insert_pos`.
100    //
101    // The home thread of the upvalue is whichever thread is currently
102    // executing `find_upval` — it captures one of that thread's stack
103    // slots. Phase E-3 makes this id real so `upvalue_get`/`upvalue_set`
104    // can dispatch through `GlobalState::cross_thread_upvals` when a
105    // coroutine reads or writes an upvalue belonging to its parent.
106    let owner_tid = state.global().current_thread_id as usize;
107    let uv: GcRef<UpVal> = state.new_upval_open(owner_tid, level);
108    // PORT NOTE: Vec insert maintains descending StackIdx order (highest first),
109    // mirroring the C intrusive list where the head is always the topmost slot.
110    state.openupval.insert(insert_pos, uv.clone());
111    // macros.tsv: isintwups → state.in_twups()
112    // TODO(port): implement state.in_twups() and the twups insertion. The method needs to
113    // check whether this LuaState is already in global.twups. Requires either a flag on
114    // LuaState or a scan of global.twups. See also lstate.h discussion in state.rs.
115    if !state_in_twups(state) {
116        // TODO(port): state.global_mut().twups.push(gc_ref_to_this_thread(state));
117        // Deferred: obtaining a GcRef<LuaState> to self requires Arc/Rc self-reference
118        // which is an unsolved design problem for Phase E coroutines.
119    }
120    uv
121}
122
123/// Finds or creates an open upvalue for stack slot `level`.
124///
125/// Searches `state.openupval` (sorted descending by StackIdx) for an existing
126/// open upvalue at exactly `level`. If found, returns it. Otherwise, inserts a
127/// new one at the correct sorted position and returns it.
128///
129pub(crate) fn find_upval(state: &mut LuaState, level: StackIdx) -> GcRef<UpVal> {
130    debug_assert!(
131        state_in_twups(state) || state.openupval.is_empty(),
132        "thread must be in twups if it has open upvalues"
133    );
134    //    while ((p = *pp) != NULL && uplevel(p) >= level) {
135    //      lua_assert(!isdead(G(L), p));
136    //      if (uplevel(p) == level) return p;  /* found */
137    //      pp = &p->u.open.next;
138    //    }
139    //    return newupval(L, level, pp);
140    //
141    // The list is sorted descending. We scan from index 0 (highest) downward.
142    // When we find an entry with idx < level we've passed the insertion point.
143    let mut insert_pos = state.openupval.len(); // default: append at end
144    for (i, uv_ref) in state.openupval.iter().enumerate() {
145        // macros.tsv: uplevel → extract thread_stack_idx from the open payload
146        let uv_idx = match uv_ref.try_open_payload() {
147            Some((_thread_id, thread_stack_idx)) => thread_stack_idx,
148            None => {
149                debug_assert!(false, "closed upvalue found in openupval list");
150                continue;
151            }
152        };
153        if uv_idx.0 >= level.0 {
154            if uv_idx == level {
155                return uv_ref.clone();
156            }
157            // uv_idx.0 > level.0: this entry is higher on the stack; keep searching.
158        } else {
159            // uv_idx.0 < level.0: correct insertion point reached.
160            insert_pos = i;
161            break;
162        }
163    }
164    new_open_upval(state, level, insert_pos)
165}
166
167// ── Close-method call helpers ─────────────────────────────────────────────────
168
169/// Calls the `__close` metamethod on `obj` with error argument `err`.
170/// `yy` controls whether the call is yieldable (true) or non-yieldable (false).
171///
172/// This function assumes EXTRA_STACK free slots are available.
173///
174fn call_close_method(
175    state: &mut LuaState,
176    obj: LuaValue,
177    err: Option<LuaValue>,
178    yy: bool,
179) -> Result<(), LuaError> {
180    //    const TValue *tm = luaT_gettmbyobj(L, obj, TM_CLOSE);
181    //    setobj2s(L, top, tm);     /* push metamethod */
182    //    setobj2s(L, top + 1, obj); /* 1st arg: self */
183    //    setobj2s(L, top + 2, err); /* 2nd arg: error message */
184    //    L->top.p = top + 3;
185    //    if (yy) luaD_call(L, top, 0);
186    //    else    luaD_callnoyield(L, top, 0);
187    //
188    // In Rust: state.push() manages the top pointer; no pointer arithmetic needed.
189    // setobj2s → state.push(value.clone())
190    // macros.tsv: luaT_gettmbyobj → state.get_tm_by_obj(&obj, TagMethod::Close)
191    let tm = state.get_tm_by_obj(&obj, lua_types::tagmethod::TagMethod::Close);
192    let top = state.top;
193    state.push(tm);
194    state.push(obj);
195    if let Some(err) = err {
196        state.push(err);
197    }
198    // TODO(port): state.call(top, 0) / state.call_noyield(top, 0) —
199    // these methods live in do_.rs (ldo.c); cross-module call.
200    if yy {
201        state.lua_call(top, 0)?;
202    } else {
203        state.lua_callnoyield(top, 0)?;
204    }
205    Ok(())
206}
207
208/// Checks that the value at `level` has a `__close` metamethod, raising a
209/// runtime error if it does not.
210///
211fn check_close_mth(state: &mut LuaState, level: StackIdx) -> Result<(), LuaError> {
212    //    if (ttisnil(tm)) {
213    //      int idx = cast_int(level - L->ci->func.p);
214    //      const char *vname = luaG_findlocal(L, L->ci, idx, NULL);
215    //      if (vname == NULL) vname = "?";
216    //      luaG_runerror(L, "variable '%s' got a non-closable value", vname);
217    //    }
218    //
219    // macros.tsv: s2v(level) → state.stack_at(level) — returns &LuaValue
220    // macros.tsv: ttisnil(tm) → matches!(tm, LuaValue::Nil)
221    let val = state.get_stack_value(level).clone();
222    let tm = state.get_tm_by_obj(&val, lua_types::tagmethod::TagMethod::Close);
223    if matches!(tm, LuaValue::Nil) {
224        // macros.tsv: cast_int → x as i32
225        // CallInfo.func is the StackIdx of the function on the stack.
226        let func_idx = state.current_ci().func;
227        let idx = (level.0 as i32) - (func_idx.0 as i32);
228        let vname_owned: Vec<u8> = state
229            .debug_find_local(state.ci, idx)
230            .unwrap_or_else(|| b"?".to_vec());
231        // PORT NOTE: Lua variable names are ASCII identifiers; `escape_ascii`
232        // produces a Display-compatible wrapper for the byte slice.
233        return Err(LuaError::runtime(format_args!(
234            "variable '{}' got a non-closable value",
235            vname_owned.escape_ascii()
236        )));
237    }
238    Ok(())
239}
240
241/// Prepares and calls the closing method for the variable at `level`.
242///
243/// If `status == CLOSE_K_TOP`, the error argument passed to `__close` is nil.
244/// Otherwise, `set_error_obj` is called to materialise the error at `level + 1`
245/// before the close method is invoked.
246///
247fn prep_call_close_mth(
248    state: &mut LuaState,
249    level: StackIdx,
250    status: i32,
251    yy: bool,
252) -> Result<(), LuaError> {
253    //    TValue *errobj;
254    //    if (status == CLOSEKTOP)
255    //      errobj = &G(L)->nilvalue;  /* error object is nil */
256    //    else {  /* luaD_seterrorobj will set top to level+2 */
257    //      errobj = s2v(level + 1);
258    //      luaD_seterrorobj(L, status, level + 1);
259    //    }
260    //    callclosemethod(L, uv, errobj, yy);
261    //
262    // macros.tsv: s2v(level) → state.stack_at(level), returning &LuaValue
263    // Clone before any mutable operations to avoid borrow conflicts.
264    let uv = state.get_stack_value(level).clone();
265    let err = if state.global().lua_version == lua_types::LuaVersion::V55 {
266        if status == CLOSE_K_TOP || status == lua_types::LuaStatus::Ok as i32 {
267            None
268        } else {
269            state.set_error_obj(status, StackIdx(level.0 + 1))?;
270            Some(state.get_stack_value(StackIdx(level.0 + 1)).clone())
271        }
272    } else if status == CLOSE_K_TOP {
273        Some(LuaValue::Nil)
274    } else {
275        // TODO(port): state.set_error_obj(status, ...) lives in do_.rs (ldo.c).
276        state.set_error_obj(status, StackIdx(level.0 + 1))?;
277        Some(state.get_stack_value(StackIdx(level.0 + 1)).clone())
278    };
279    call_close_method(state, uv, err, yy)
280}
281
282// ── To-be-closed variable management ─────────────────────────────────────────
283
284/// Inserts the variable at `level` into the to-be-closed (`tbc`) list.
285///
286/// If the value is falsy (nil or false) it does not need closing and the
287/// function returns immediately. Otherwise it verifies that the value has a
288/// `__close` metamethod, then records it in `state.tbclist`.
289///
290pub(crate) fn new_tbc_upval(state: &mut LuaState, level: StackIdx) -> Result<(), LuaError> {
291    // In Rust: tbclist is Vec<StackIdx>, "current head" = last element.
292    debug_assert!(
293        state.tbclist.last().map_or(true, |&top| level.0 > top.0),
294        "new tbc entry must be above current tbclist head"
295    );
296    // macros.tsv: l_isfalse → matches!(o, LuaValue::Nil | LuaValue::Bool(false))
297    // Clone before borrow to avoid aliasing with later mutable calls.
298    let val = state.get_stack_value(level).clone();
299    if matches!(val, LuaValue::Nil | LuaValue::Bool(false)) {
300        return Ok(());
301    }
302    check_close_mth(state, level)?;
303    //   while (cast_uint(level - L->tbclist.p) > MAXDELTA) {
304    //     L->tbclist.p += MAXDELTA;
305    //     L->tbclist.p->tbclist.delta = 0;  /* dummy node */
306    //   }
307    //   level->tbclist.delta = cast(unsigned short, level - L->tbclist.p);
308    //   L->tbclist.p = level;
309    //
310    // PORT NOTE: The MAXDELTA / dummy-node mechanism is a C-only optimisation
311    // required because `StackValue.tbclist.delta` is a `u16` (max 65535). With
312    // `Vec<StackIdx>` the index fits a u32 and no dummy nodes are ever needed.
313    state.tbclist.push(level);
314    Ok(())
315}
316
317/// Closes all open upvalues whose stack index is ≥ `level`, transitioning each
318/// from `UpVal::Open { thread_id: _, idx: thread_stack_idx }` to `UpVal::Closed(value)` by copying
319/// the current stack value into the upvalue's own storage.
320///
321pub(crate) fn close_upval(state: &mut LuaState, level: StackIdx) {
322    //      TValue *slot = &uv->u.value;
323    //      lua_assert(uplevel(uv) < L->top.p);
324    //      luaF_unlinkupval(uv);
325    //      setobj(L, slot, uv->v.p);  /* copy stack value into upvalue */
326    //      uv->v.p = slot;            /* now the value lives here */
327    //      if (!iswhite(uv)) { nw2black(uv); luaC_barrier(L, uv, slot); }
328    //  }
329    //
330    // openupval is sorted descending; front element is the topmost open upvalue.
331    loop {
332        let uv = match state.openupval.first() {
333            Some(uv) => uv.clone(),
334            None => break,
335        };
336        let uv_idx = match uv.try_open_payload() {
337            Some((_thread_id, thread_stack_idx)) => thread_stack_idx,
338            None => {
339                // Cross-thread close/reset paths can leave a stale closed
340                // upvalue in this Vec-backed open list. The C intrusive list
341                // cannot represent that state; in Rust, unlink it and keep
342                // closing the remaining open entries.
343                state.openupval.remove(0);
344                continue;
345            }
346        };
347        if uv_idx.0 < level.0 {
348            break;
349        }
350        // PORT NOTE: C asserts `uplevel(uv) < L->top.p` because the C stack is a
351        // contiguous block where slots above top are undefined. The Rust stack is
352        // a `Vec<StackValue>` whose backing storage outlives any top movement, so
353        // reading `stack[uv_idx]` is always valid here even when `state.top` has
354        // been rolled back below the upvalue (which is exactly what happens on
355        // pcall error unwind, e.g. when `assert_fn` calls `set_top(L, 1)` before
356        // raising). Dropping the C-style assertion lets close_upval correctly
357        // close upvalues during error unwind regardless of top position.
358        state.openupval.remove(0);
359        let stack_val = state.get_stack_value(uv_idx).clone();
360        uv.close_with(stack_val);
361        // macros.tsv: iswhite → obj.is_white(); nw2black → obj.set_black()
362        //             luaC_barrier → state.gc().barrier(p, v) — no-op Phase A–C
363        // TODO(port): GC color methods (is_white, set_black) on GcRef<UpVal>;
364        // Phase D only. Omitted in Phase A–C.
365    }
366}
367
368/// Removes the most-recent entry from `state.tbclist`.
369///
370/// The C version must also skip over any delta==0 "dummy" nodes inserted to
371/// bridge gaps larger than MAXDELTA. In Rust no dummy nodes are ever inserted,
372/// so this is a straight `Vec::pop`.
373///
374fn pop_tbc_list(state: &mut LuaState) {
375    //    lua_assert(tbc->tbclist.delta > 0);  /* first element cannot be dummy */
376    //    tbc -= tbc->tbclist.delta;
377    //    while (tbc > L->stack.p && tbc->tbclist.delta == 0)
378    //      tbc -= MAXDELTA;  /* skip dummy nodes */
379    //    L->tbclist.p = tbc;
380    //
381    // PORT NOTE: Delta-encoding dropped (see new_tbc_upval). Just pop.
382    state.tbclist.pop();
383}
384
385/// Closes all upvalues and to-be-closed variables down to `level`, invoking
386/// `__close` metamethods as needed. Returns the (stable) `level` index.
387///
388/// `status` is passed to `prep_call_close_mth` to determine the error argument:
389/// `CLOSE_K_TOP` means nil; other statuses produce the appropriate error object.
390/// `yy` controls yieldability of the close-method calls.
391///
392pub(crate) fn close(
393    state: &mut LuaState,
394    level: StackIdx,
395    status: i32,
396    yy: bool,
397) -> Result<StackIdx, LuaError> {
398    // macros.tsv: savestack → idx (StackIdx is already stable across reallocs in Rust)
399    // PORT NOTE: savestack / restorestack are no-ops here. In C they save/restore a
400    // pointer as a byte-offset because the stack may reallocate during close-method
401    // calls. In Rust, StackIdx is an index into Vec and remains valid after any resize.
402
403    close_upval(state, level);
404    //      StkId tbc = L->tbclist.p;
405    //      poptbclist(L);
406    //      prepcallclosemth(L, tbc, status, yy);
407    //      level = restorestack(L, levelrel);
408    //    }
409    while state
410        .tbclist
411        .last()
412        .copied()
413        .map_or(false, |tbc| tbc.0 >= level.0)
414    {
415        let tbc = state
416            .tbclist
417            .last()
418            .copied()
419            .expect("tbclist non-empty (just checked)");
420        pop_tbc_list(state);
421        prep_call_close_mth(state, tbc, status, yy)?;
422    }
423    Ok(level)
424}
425
426// ── Debug helpers ─────────────────────────────────────────────────────────────
427
428/// Returns the byte-string name of the `local_number`-th local variable that is
429/// active at bytecode position `pc` in prototype `f`, or `None` if no such
430/// variable exists.
431///
432/// Variables are scanned in order. A variable is active when
433/// `startpc <= pc < endpc`. The first active variable is numbered 1.
434///
435pub(crate) fn get_local_name(
436    f: &crate::state::LuaProto,
437    local_number: i32,
438    pc: i32,
439) -> Option<&[u8]> {
440    //    for (i = 0; i < f->sizelocvars && f->locvars[i].startpc <= pc; i++) {
441    //      if (pc < f->locvars[i].endpc) {  /* is variable active? */
442    //        local_number--;
443    //        if (local_number == 0)
444    //          return getstr(f->locvars[i].varname);
445    //      }
446    //    }
447    //    return NULL;
448    //
449    // macros.tsv: getstr(ts) → ts.as_bytes()  returning &[u8]
450    //
451    // TODO(port): `f.locvars` does not exist on the current LuaProto stub in state.rs.
452    // This will compile once LuaProto gains its full set of fields from object.rs.
453    // The logic below faithfully translates the C loop.
454    let mut remaining = local_number;
455    // We break early once startpc > pc (variables are ordered by startpc).
456    for lv in f.locvars.iter() {
457        if lv.startpc > pc {
458            break;
459        }
460        if pc < lv.endpc {
461            remaining -= 1;
462            if remaining == 0 {
463                // macros.tsv: getstr → ts.as_bytes()
464                return Some(lv.varname.as_bytes());
465            }
466        }
467    }
468    None
469}
470
471// ── Private helpers (Rust-only) ───────────────────────────────────────────────
472
473/// Returns `true` if this thread is already registered in `global.twups`.
474///
475/// iff its twups pointer doesn't point back to itself).
476///
477/// PORT NOTE: In Phase A–D with coroutines stubbed there is effectively a
478/// single thread. The actual `GlobalState.twups` Vec management (insertion in
479/// `new_open_upval`) is deferred to Phase D/E and would require a GcRef-to-self.
480/// Until then we treat every thread as conceptually present in twups, which
481/// satisfies the invariant `state_in_twups || openupval.is_empty()` asserted by
482/// `find_upval`. The actual twups list does not yet drive any behaviour.
483fn state_in_twups(state: &LuaState) -> bool {
484    let _ = state;
485    true
486}
487
488// ── Trait stubs needed for compilation ───────────────────────────────────────
489
490/// Stub methods on `LuaState` assumed by this module.
491///
492/// These will be implemented in their home modules (do_.rs, debug.rs, tagmethods.rs)
493/// and removed from this file in Phase B.
494impl LuaState {
495    /// Returns the `LuaValue` at stack index `idx`.
496    ///
497    /// macros.tsv: `s2v → state.stack_at(idx)`.
498    pub(crate) fn get_stack_value(&self, idx: StackIdx) -> &LuaValue {
499        // TODO(port): bounds-check and return &self.stack[idx.0 as usize].val
500        &self.stack[idx.0 as usize].val
501    }
502
503    /// Returns the current CallInfo (active call frame).
504    ///
505    pub(crate) fn current_ci(&self) -> &crate::state::CallInfo {
506        // TODO(port): return &self.call_info[self.ci.0 as usize]
507        &self.call_info[self.ci.0 as usize]
508    }
509
510    /// Looks up the `__close` (or other) metamethod for a value.
511    ///
512    /// macros.tsv: `fasttm → state.fast_tm(et, e)`.
513    pub(crate) fn get_tm_by_obj(
514        &mut self,
515        val: &LuaValue,
516        tm: lua_types::tagmethod::TagMethod,
517    ) -> LuaValue {
518        let mt: Option<GcRef<lua_types::value::LuaTable>> = match val {
519            LuaValue::Table(t) => t.metatable(),
520            LuaValue::UserData(u) => u.metatable(),
521            other => {
522                let type_idx = other.base_type() as usize;
523                self.global().mt[type_idx].clone()
524            }
525        };
526        match mt {
527            Some(mt_ref) => {
528                let ename = self.global().tmname[tm as usize].clone();
529                mt_ref.get_short_str(&ename)
530            }
531            None => LuaValue::Nil,
532        }
533    }
534
535    /// Calls a Lua or C function (yieldable).
536    ///
537    pub(crate) fn lua_call(&mut self, top: StackIdx, nresults: i32) -> Result<(), LuaError> {
538        crate::do_::call(self, top, nresults)
539    }
540
541    /// Calls a Lua or C function (non-yieldable).
542    ///
543    pub(crate) fn lua_callnoyield(&mut self, top: StackIdx, nresults: i32) -> Result<(), LuaError> {
544        crate::do_::callnoyield(self, top, nresults)
545    }
546
547    /// Sets the error object at a given stack index for a given status code.
548    ///
549    pub(crate) fn set_error_obj(&mut self, status: i32, idx: StackIdx) -> Result<(), LuaError> {
550        let s = lua_types::status::LuaStatus::from_raw(status);
551        crate::do_::set_error_obj(self, s, idx);
552        Ok(())
553    }
554
555    /// Returns the local-variable name at frame position `n` for CallInfo `ci`.
556    ///
557    pub(crate) fn debug_find_local(&self, ci: CallInfoIdx, n: i32) -> Option<Vec<u8>> {
558        crate::debug::find_local(self, ci, n, None)
559    }
560}
561
562// ──────────────────────────────────────────────────────────────────────────
563// PORT STATUS
564//   source:        src/lfunc.c  (295 lines, 16 functions)
565//   target_crate:  lua-vm
566//   confidence:    medium
567//   todos:         36
568//   port_notes:    7
569//   unsafe_blocks: 0
570//   notes:         Logic is faithful. Two blockers for Phase B:
571//                  (1) GcRef<UpVal> needs interior mutability (Rc<RefCell<UpVal>>)
572//                      so close_upval and init_upvals can mutate in-place.
573//                  (2) LuaProto stub in state.rs must gain full field list from
574//                      object.rs before new_proto / get_local_name compile.
575//                  LuaClosureLua.proto needs Option<> wrapper for NULL init in
576//                  new_lua_closure. Stub methods on LuaState (get_tm_by_obj,
577//                  lua_call, set_error_obj, debug_find_local) must be removed
578//                  once their home modules are written (do_.rs, debug.rs,
579//                  tagmethods.rs). The 36 TODO(port) markers include both the
580//                  core design blockers and the stub-method placeholders; the
581//                  stub-method TODOs will auto-resolve as other modules land.
582// ──────────────────────────────────────────────────────────────────────────