Skip to main content

lua_vm/
api.rs

1//
2// PORT NOTE: This is the Rust-native translation of lapi.c.
3// The C-API surface (lua_State *, int stack-index protocol) is replaced by
4// methods on LuaState.  `lua_lock` / `lua_unlock` are dropped (no-op in the
5// single-threaded default build).  `api_incr_top` is dropped; `state.push()`
6// already increments.  Stack pointers (StkId) become StackIdx (u32).
7
8#![allow(dead_code)]
9
10use std::convert::Infallible;
11#[allow(unused_imports)] use crate::prelude::*;
12
13use crate::state::{FinalizerObject, LuaState, LuaCFunction, LuaCallable, StackIdx, WeakTableEntry,
14    LuaValueExt, LuaTypeExt, StackIdxExt,
15    LuaTableRefExt, LuaUserDataRefExt};
16use lua_types::{
17    LuaValue, LuaType, LuaError, LuaString, LuaUserData, LuaClosure,
18    GcRef, LuaStatus,
19};
20use lua_types::value::LuaTable;
21
22pub const LUA_IDENT: &[u8] =
23    b"$LuaVersion: Lua 5.4.7  Copyright (C) 1994-2024 Lua.org, PUC-Rio $\
24      $LuaAuthors: R. Ierusalimschy, L. H. Figueiredo, W. Celes $";
25
26const LUA_REGISTRYINDEX: i32 = -(1_000_000) - 1000;
27
28const LUA_MULTRET: i32 = -1;
29
30const LUA_RIDX_GLOBALS: i64 = 2;
31
32const MAX_UPVAL: u8 = 255;
33
34#[inline]
35fn is_pseudo(idx: i32) -> bool {
36    idx <= LUA_REGISTRYINDEX
37}
38
39#[inline]
40fn is_upvalue(idx: i32) -> bool {
41    idx < LUA_REGISTRYINDEX
42}
43
44// PORT NOTE: In C, the only "invalid" TValue is the global nilvalue singleton
45// pointer returned by index2value when the index is out of range. In Rust we
46// cannot do pointer-equality on a singleton, so validity is decided by whether
47// the index resolves to a real stack/upvalue slot — see `is_valid_index`.
48#[inline]
49fn is_valid_index(state: &LuaState, idx: i32) -> bool {
50    if idx == 0 {
51        return false;
52    }
53    let ci = state.current_call_info();
54    if idx > 0 {
55        let slot = ci.func + idx;
56        slot.0 < state.top_idx().0
57    } else if !is_pseudo(idx) {
58        (-idx) as u32 <= state.top_idx().0.saturating_sub(ci.func.0 + 1)
59    } else if idx == LUA_REGISTRYINDEX {
60        true
61    } else {
62        let upval_n = (LUA_REGISTRYINDEX - idx) as usize;
63        let func_val = state.get_at(ci.func);
64        if let LuaValue::Function(LuaClosure::C(ref ccl)) = func_val {
65            let upvalues = ccl.upvalues.borrow();
66            upval_n >= 1 && upval_n <= upvalues.len()
67        } else {
68            false
69        }
70    }
71}
72
73// ── index helpers ─────────────────────────────────────────────────────────────
74
75// PORT NOTE: In Rust we cannot return a pointer; we return a cloned LuaValue.
76// Writers use a companion index_to_stack_idx() for actual stack slots.
77fn index_to_value(state: &LuaState, idx: i32) -> LuaValue {
78    let ci = state.current_call_info();
79    if idx > 0 {
80        let func_idx = ci.func;
81        let slot = func_idx + idx;
82        debug_assert!(
83            idx as u32 <= ci.top.saturating_sub(func_idx + 1),
84            "unacceptable index"
85        );
86        if slot.0 >= state.top_idx().0 {
87            LuaValue::Nil
88        } else {
89            state.get_at(slot)
90        }
91    } else if !is_pseudo(idx) {
92        // negative index
93        debug_assert!(
94            idx != 0,
95            "invalid index"
96        );
97        let top = state.top_idx();
98        let slot = (top.0 as i32 + idx) as u32;
99        state.get_at(slot)
100    } else if idx == LUA_REGISTRYINDEX {
101        state.registry_value()
102    } else {
103        // upvalues: idx = LUA_REGISTRYINDEX - idx  (idx < LUA_REGISTRYINDEX)
104        let upval_n = (LUA_REGISTRYINDEX - idx) as usize;
105        debug_assert!(upval_n <= MAX_UPVAL as usize + 1, "upvalue index too large");
106        let func_val = state.get_at(ci.func);
107        if let LuaValue::Function(LuaClosure::C(ref ccl)) = func_val {
108            // C closure upvalue
109            let upvalues = ccl.upvalues.borrow();
110            if upval_n >= 1 && upval_n <= upvalues.len() {
111                upvalues[upval_n - 1].clone()
112            } else {
113                LuaValue::Nil
114            }
115        } else {
116            LuaValue::Nil
117        }
118    }
119}
120
121// Returns a StackIdx for a valid (non-pseudo) actual stack slot.
122#[inline]
123fn index_to_stack_idx(state: &LuaState, idx: i32) -> StackIdx {
124    let ci = state.current_call_info();
125    if idx > 0 {
126        let slot = ci.func + idx;
127        debug_assert!(slot.0 < state.top_idx().0, "invalid index");
128        slot
129    } else {
130        debug_assert!(idx != 0 && !is_pseudo(idx), "invalid index");
131        StackIdx((state.top_idx().0 as i32 + idx) as u32)
132    }
133}
134
135// ── stack manipulation ────────────────────────────────────────────────────────
136
137pub fn check_stack(state: &mut LuaState, n: i32) -> bool {
138    debug_assert!(n >= 0, "negative 'n'");
139    let available = state.stack_available();
140    let res = if available > n as usize {
141        true
142    } else {
143        crate::do_::grow_stack(state, n, false).unwrap_or(false)
144    };
145    if res {
146        let needed_top = state.top_idx() + n as i32;
147        let ci_idx = state.current_ci_idx();
148        if state.get_ci(ci_idx).top.0 < needed_top.0 {
149            let live_top = state.top_idx();
150            state.get_ci_mut(ci_idx).top = needed_top;
151            state.clear_stack_range(live_top, needed_top);
152        }
153    }
154    res
155}
156
157/// Move the top `n` values from `from`'s stack onto `to`'s stack.
158///
159/// Both threads must share the same `GlobalState` (i.e. one is a
160/// coroutine the other created via `coroutine.create`). Calling with
161/// `from` == `to` is a no-op. Equivalent to:
162///
163/// ```text
164/// args = from.stack[top-n..top].clone();
165/// from.set_top(top - n);
166/// for v in args { to.push(v); }
167/// ```
168///
169///
170/// Phase E-3: implemented for the same-`GlobalState` case (the only one
171/// `lua-stdlib` uses today). `lua-vm` callers should prefer this helper
172/// over hand-rolling the snapshot/push dance.
173pub fn xmove(from: &mut LuaState, to: &mut LuaState, n: i32) {
174    if n <= 0 {
175        return;
176    }
177    if std::ptr::eq(from as *const LuaState, to as *const LuaState) {
178        return;
179    }
180    let abs_top = from.top_idx().0 as i32;
181    debug_assert!(abs_top >= n, "lua_xmove: from stack underflow");
182    let first_abs = abs_top - n;
183    let mut buf: Vec<lua_types::LuaValue> = Vec::with_capacity(n as usize);
184    for i in 0..n {
185        let idx = StackIdx((first_abs + i) as u32);
186        buf.push(from.get_at(idx));
187    }
188    from.set_top(StackIdx(first_abs as u32));
189    for v in buf {
190        to.push(v);
191    }
192}
193
194pub fn at_panic(
195    state: &mut LuaState,
196    panicf: Option<fn(&mut LuaState) -> Result<usize, LuaError>>,
197) -> Option<fn(&mut LuaState) -> Result<usize, LuaError>> {
198    let old = state.global_mut().panic;
199    state.global_mut().panic = panicf;
200    old
201}
202
203pub fn version(_state: &LuaState) -> f64 {
204    504.0
205}
206
207pub fn abs_index(state: &LuaState, idx: i32) -> i32 {
208    //          : cast_int(L->top.p - L->ci->func.p) + idx;
209    if idx > 0 || is_pseudo(idx) {
210        idx
211    } else {
212        let ci = state.current_call_info();
213        (state.top_idx().0 as i32 - ci.func.0 as i32) + idx
214    }
215}
216
217pub fn get_top(state: &LuaState) -> i32 {
218    let ci = state.current_call_info();
219    (state.top_idx().0 as i32) - (ci.func.0 as i32 + 1)
220}
221
222pub fn set_top(state: &mut LuaState, idx: i32) -> Result<(), LuaError> {
223    let func = state.current_call_info().func;
224    let ci_top = state.current_call_info().top;
225    if idx >= 0 {
226        debug_assert!(
227            idx as u32 <= ci_top.saturating_sub(func + 1),
228            "new top too large"
229        );
230        let new_top = func + 1 + idx as i32;
231        let old_top = state.top_idx();
232        if new_top.0 > old_top.0 {
233            for i in old_top.0..new_top.0 {
234                state.set_at(i, LuaValue::Nil);
235            }
236        }
237        // TODO(port): to-be-closed variable closing on stack shrink;
238        // luaF_close not yet translated. Skipping close logic for Phase A.
239        state.set_top_idx(new_top);
240    } else {
241        debug_assert!(
242            -(idx + 1) <= (state.top_idx().0 as i32 - (func.0 as i32 + 1)),
243            "invalid new top"
244        );
245        let new_top = (state.top_idx().0 as i32 + idx + 1) as u32;
246        // TODO(port): to-be-closed variable closing on stack shrink (same as above)
247        state.set_top_idx(new_top);
248    }
249    Ok(())
250}
251
252pub fn close_slot(state: &mut LuaState, idx: i32) -> Result<(), LuaError> {
253    let level = index_to_stack_idx(state, idx);
254    // TODO(port): tbc-list check and luaF_close not yet translated.
255    state.set_at(level, LuaValue::Nil);
256    Ok(())
257}
258
259#[inline]
260fn reverse_segment(state: &mut LuaState, from: StackIdx, to: StackIdx) {
261    let mut lo = from.0;
262    let mut hi = to.0;
263    while lo < hi {
264        let temp = state.get_at(StackIdx(lo));
265        let hi_val = state.get_at(StackIdx(hi));
266        state.set_at(StackIdx(lo), hi_val);
267        state.set_at(StackIdx(hi), temp);
268        lo += 1;
269        hi -= 1;
270    }
271}
272
273pub fn rotate(state: &mut LuaState, idx: i32, n: i32) {
274    let t = state.top_idx() - 1;
275    let p = index_to_stack_idx(state, idx);
276    debug_assert!((n.unsigned_abs() as i32) <= ((t.0 as i32) - (p.0 as i32) + 1), "invalid 'n'");
277    let m = if n >= 0 {
278        t - n
279    } else {
280        StackIdx((p.0 as i32 - n - 1) as u32)
281    };
282    reverse_segment(state, p, m);
283    reverse_segment(state, m + 1, t);
284    reverse_segment(state, p, t);
285}
286
287pub fn copy(state: &mut LuaState, fromidx: i32, toidx: i32) {
288    let fr = index_to_value(state, fromidx);
289    if is_upvalue(toidx) {
290        // Writing to a function upvalue pseudo-index
291        let upval_n = (LUA_REGISTRYINDEX - toidx) as usize;
292        let func_val = state.get_at(state.current_call_info().func);
293        if let LuaValue::Function(LuaClosure::C(ref ccl)) = func_val {
294            let wrote = {
295                let mut upvalues = ccl.upvalues.borrow_mut();
296                if upval_n >= 1 && upval_n <= upvalues.len() {
297                    upvalues[upval_n - 1] = fr.clone();
298                    true
299                } else {
300                    false
301                }
302            };
303            if wrote {
304                state.gc().barrier(ccl, &fr);
305            }
306        }
307    } else if toidx == LUA_REGISTRYINDEX {
308        // TODO(port): write to registry — needs GlobalState::set_registry(fr)
309    } else {
310        let to_slot = index_to_stack_idx(state, toidx);
311        state.set_at(to_slot, fr);
312    }
313}
314
315pub fn push_value(state: &mut LuaState, idx: i32) {
316    let v = index_to_value(state, idx);
317    state.push(v);
318}
319
320/// Inherent `push_copy` so the `LuaStateStubExt::push_copy` default
321/// `todo!()` no longer fires. Phase-A `state.push_copy(idx)` call-sites
322/// (base.rs, etc.) duplicate the value at `idx` onto the top of the stack —
323/// the same semantics as `lua_pushvalue`.
324impl LuaState {
325    pub fn push_copy(&mut self, idx: i32) -> Result<(), LuaError> {
326        push_value(self, idx);
327        Ok(())
328    }
329
330    pub fn push_value_at(&mut self, idx: i32) -> Result<(), LuaError> {
331        push_value(self, idx);
332        Ok(())
333    }
334
335    pub fn insert(&mut self, idx: i32) -> Result<(), LuaError> {
336        rotate(self, idx, 1);
337        Ok(())
338    }
339
340    /// Inherent `length_at` mirroring `luaL_len` from `lauxlib.c`: push the
341    /// value's length onto the stack (honouring `__len`), pop it as an
342    /// integer, and error if the result is not an integer. Defined on
343    /// `LuaState` so it overrides the `LuaStateStubExt::length_at` trait
344    /// default `todo!()`.
345    pub fn length_at(&mut self, idx: i32) -> Result<i64, LuaError> {
346        len(self, idx)?;
347        let l = match to_integer_x(self, -1) {
348            Some(n) => n,
349            None => {
350                return Err(LuaError::runtime(format_args!(
351                    "object length is not an integer"
352                )));
353            }
354        };
355        self.pop_n(1);
356        Ok(l)
357    }
358
359    /// Write `msg` bytes verbatim to standard output. Mirrors the C macro
360    /// `lua_writestring(s, l) = fwrite(s, 1, l, stdout)` from `lauxlib.h`,
361    /// used by `print` and friends. A failed write is propagated as a
362    /// `LuaError::runtime`; this matches C-Lua's behaviour where an I/O
363    /// error during `lua_writestring` would surface through the host's
364    /// error handling.
365    pub fn write_output(&mut self, msg: &[u8]) -> Result<(), LuaError> {
366        if let Some(write_fn) = self.global().stdout_hook {
367            write_fn(msg).map_err(|e| LuaError::runtime(format_args!("{}", e)))?;
368            return Ok(());
369        }
370
371        #[cfg(all(target_arch = "wasm32", target_os = "unknown"))]
372        {
373            let _ = msg;
374            Err(LuaError::runtime(format_args!(
375                "stdout not available in this host"
376            )))
377        }
378
379        #[cfg(not(all(target_arch = "wasm32", target_os = "unknown")))]
380        {
381            use std::io::Write;
382            let stdout = std::io::stdout();
383            let mut handle = stdout.lock();
384            handle
385                .write_all(msg)
386                .map_err(|e| LuaError::runtime(format_args!("{}", e)))?;
387            handle
388                .flush()
389                .map_err(|e| LuaError::runtime(format_args!("{}", e)))?;
390            Ok(())
391        }
392    }
393
394    /// Convert the value at `idx` to a display string, push the result onto
395    /// the stack, and return a copy of its bytes. Mirrors `luaL_tolstring`
396    /// from `lauxlib.c`. The default Lua formatting is used for primitives
397    /// (`"true"`/`"false"`/`"nil"`, `%I` integers, `%.14g` floats); for other
398    /// reference types the result is `"<typename>: 0x<hex pointer>"`.
399    ///
400    /// If the value has a `__tostring` metamethod, it is invoked first and its
401    /// (string) result is used in place of the default formatting (matching
402    pub fn to_display_string(&mut self, idx: i32) -> Result<Vec<u8>, LuaError> {
403        let abs = abs_index(self, idx);
404        let v = index_to_value(self, abs);
405        let mt: Option<GcRef<LuaTable>> = match &v {
406            LuaValue::Table(t) => t.metatable(),
407            LuaValue::UserData(u) => u.metatable(),
408            _ => self.global().mt[v.base_type() as usize].clone(),
409        };
410        if let Some(mt_ref) = mt {
411            let key = self.intern_str(b"__tostring")?;
412            let f = mt_ref.get_short_str(&key);
413            if !matches!(f, LuaValue::Nil) {
414                let func_idx = self.top_idx();
415                self.push(f);
416                self.push(v.clone());
417                if self.current_ci().is_lua_code() {
418                    self.do_call(func_idx, 1)?;
419                } else {
420                    self.do_call_no_yield(func_idx, 1)?;
421                }
422                let top = self.top_idx();
423                let result = self.get_at(StackIdx(top.0 - 1));
424                if let LuaValue::Str(s) = result {
425                    return Ok(s.as_bytes().to_vec());
426                }
427                return Err(LuaError::runtime(format_args!(
428                    "'__tostring' must return a string"
429                )));
430            }
431        }
432        let bytes: Vec<u8> = match &v {
433            LuaValue::Str(s) => {
434                let out = s.as_bytes().to_vec();
435                self.push(LuaValue::Str(s.clone()));
436                out
437            }
438            LuaValue::Int(_) | LuaValue::Float(_) => {
439                let s = crate::object::num_to_string(self, &v)?;
440                let out = s.as_bytes().to_vec();
441                self.push(LuaValue::Str(s));
442                out
443            }
444            LuaValue::Bool(b) => {
445                let lit: &[u8] = if *b { b"true" } else { b"false" };
446                let s = self.intern_str(lit)?;
447                self.push(LuaValue::Str(s));
448                lit.to_vec()
449            }
450            LuaValue::Nil => {
451                let s = self.intern_str(b"nil")?;
452                self.push(LuaValue::Str(s));
453                b"nil".to_vec()
454            }
455            _ => {
456                let kind = crate::tagmethods::obj_type_name(self, &v)?;
457                let ptr = to_pointer(self, abs).unwrap_or(0);
458                let mut buf = kind;
459                buf.extend_from_slice(b": 0x");
460                buf.extend_from_slice(format!("{:x}", ptr).as_bytes());
461                let s = self.intern_str(&buf)?;
462                self.push(LuaValue::Str(s));
463                buf
464            }
465        };
466        Ok(bytes)
467    }
468
469    /// (stack top minus the slot just after the frame's `func`).
470    ///
471    /// Receiver is `&mut self` to match the `LuaStateStubExt::top` trait
472    /// signature exactly; with a different receiver shape (`&self`), Rust's
473    /// method-resolution picks the trait default and the program panics on
474    /// `todo!("phase-b-reconcile: top")`.
475    pub fn top(&mut self) -> i32 {
476        get_top(self)
477    }
478
479    /// `LuaStateStubExt::get_top` trait method. Inherent method shadows the
480    /// trait default so the `todo!("phase-b-reconcile: get_top")` shim never
481    /// fires.
482    pub fn get_top(&mut self) -> i32 {
483        get_top(self)
484    }
485
486    /// stack index `idx`, or `LuaType::None` if `idx` falls outside the
487    /// active call frame. Inherent method shadows the
488    /// `LuaStateStubExt::type_at` trait default so the `todo!()` shim
489    /// never fires.
490    pub fn type_at(&mut self, idx: i32) -> LuaType {
491        lua_type_at(self, idx)
492    }
493
494    /// #N (value expected)` error if the slot at `arg` is `LUA_TNONE`
495    /// (i.e. beyond the active call frame's top). Otherwise a no-op.
496    ///
497    /// Inherent method on LuaState shadows the `LuaStateStubExt::check_arg_any`
498    /// trait default so the `todo!()` shim never fires.
499    pub fn check_arg_any(&mut self, arg: i32) -> Result<(), LuaError> {
500        if lua_type_at(self, arg) == LuaType::None {
501            return Err(LuaError::arg_error(arg, "value expected"));
502        }
503        Ok(())
504    }
505
506    /// at `arg` to a string via `lua_tolstring` (which coerces numbers to
507    /// their string form) and returns the bytes. Raises
508    /// `bad argument #N (string expected, got <type>)` if the value is not a
509    /// string and not number-coercible.
510    ///
511    /// Inherent method on LuaState shadows the `LuaStateStubExt::check_arg_string`
512    /// trait default so the `todo!()` shim never fires. Uses the free `to_lua_string`
513    /// helper here rather than `auxlib::check_lstring`, which routes through
514    /// `state.to_lua_string` / `state.type_name` — both still trait stubs.
515    pub fn check_arg_string(&mut self, arg: i32) -> Result<Vec<u8>, LuaError> {
516        match to_lua_string(self, arg)? {
517            Some(s) => Ok(s.as_bytes().to_vec()),
518            None => {
519                let got = index_to_value(self, arg);
520                let got_name = if lua_type_at(self, arg) == LuaType::None {
521                    b"no value".to_vec()
522                } else {
523                    crate::tagmethods::obj_type_name(self, &got)?
524                };
525                let extramsg = format!(
526                    "string expected, got {}",
527                    String::from_utf8_lossy(&got_name)
528                );
529                Err(crate::debug::arg_error_impl(self, arg, extramsg.as_bytes()))
530            }
531        }
532    }
533
534    /// `arg` to a `lua_Integer` (i64) via `lua_tointegerx` (which accepts
535    /// ints, floats with exact integer value, and string-form integers).
536    /// Raises `bad argument #N (number has no integer representation)` if
537    /// the value is a number but not representable as an integer, or
538    /// `bad argument #N (number expected, got <type>)` otherwise.
539    ///
540    /// Inherent method on LuaState shadows the `LuaStateStubExt::check_arg_integer`
541    /// trait default so the `todo!()` shim never fires. Uses the free
542    /// `to_integer_x` / `is_number` helpers in this file rather than
543    /// `auxlib::check_integer`, which routes through `state.to_integer_x`
544    /// and `state.type_name` — both still trait stubs.
545    pub fn check_arg_integer(&mut self, arg: i32) -> Result<i64, LuaError> {
546        match to_integer_x(self, arg) {
547            Some(d) => Ok(d),
548            None => {
549                if is_number(self, arg) {
550                    Err(crate::debug::arg_error_impl(
551                        self,
552                        arg,
553                        b"number has no integer representation",
554                    ))
555                } else {
556                    let got = index_to_value(self, arg);
557                    let got_name = if lua_type_at(self, arg) == LuaType::None {
558                        b"no value".to_vec()
559                    } else {
560                        crate::tagmethods::obj_type_name(self, &got)?
561                    };
562                    let extramsg = format!(
563                        "number expected, got {}",
564                        String::from_utf8_lossy(&got_name)
565                    );
566                    Err(crate::debug::arg_error_impl(self, arg, extramsg.as_bytes()))
567                }
568            }
569        }
570    }
571
572    /// `arg` to an `f64` via `lua_tonumberx` (which accepts ints, floats,
573    /// and number-shaped strings) and raises `bad argument #N (number
574    /// expected, got <type>)` if the value is not number-coercible.
575    ///
576    /// Inherent method on LuaState shadows the `LuaStateStubExt::check_number`
577    /// trait default so the `todo!()` shim never fires. Uses the free
578    /// `to_number_x` helper here rather than `auxlib::check_number`, which
579    /// routes through `state.to_number_x` and `state.type_name` — both still
580    /// trait stubs.
581    pub fn check_number(&mut self, arg: i32) -> Result<f64, LuaError> {
582        match to_number_x(self, arg) {
583            Some(d) => Ok(d),
584            None => {
585                let got = index_to_value(self, arg);
586                let got_name = if lua_type_at(self, arg) == LuaType::None {
587                    b"no value".to_vec()
588                } else {
589                    crate::tagmethods::obj_type_name(self, &got)?
590                };
591                let extramsg = format!(
592                    "number expected, got {}",
593                    String::from_utf8_lossy(&got_name)
594                );
595                Err(crate::debug::arg_error_impl(self, arg, extramsg.as_bytes()))
596            }
597        }
598    }
599
600    /// `arg` is absent (`LUA_TNONE`) or `nil`, return `def`; otherwise
601    /// convert it to an integer (with the same string-to-number coercion
602    /// `lua_tointegerx` applies) and raise on failure.
603    ///
604    /// Inherent method on LuaState shadows the `LuaStateStubExt::opt_arg_integer`
605    /// trait default so the `todo!()` shim never fires. Implemented with the
606    /// free-function helpers in this file rather than `auxlib::opt_integer`
607    /// because the latter routes through `state.is_none_or_nil` and
608    /// `state.to_integer_x`, which are themselves stubbed.
609    pub fn opt_arg_integer(&mut self, arg: i32, def: i64) -> Result<i64, LuaError> {
610        match lua_type_at(self, arg) {
611            LuaType::None | LuaType::Nil => Ok(def),
612            _ => match to_integer_x(self, arg) {
613                Some(d) => Ok(d),
614                None => {
615                    if is_number(self, arg) {
616                        Err(LuaError::arg_error(
617                            arg,
618                            "number has no integer representation",
619                        ))
620                    } else {
621                        let got = index_to_value(self, arg);
622                        Err(LuaError::type_arg_error(arg, "number", &got))
623                    }
624                }
625            },
626        }
627    }
628
629    /// `lua_pcallk` with no continuation. Defers to the existing `pcall_k`
630    /// free function, which routes through `protected_call_raw` and
631    /// surfaces any runtime / syntax error as `Err(LuaError::Runtime|Syntax)`.
632    ///
633    /// Inherent method on LuaState shadows the `LuaStateStubExt::protected_call`
634    /// trait default so the `todo!()` shim never fires.
635    pub fn protected_call(&mut self, nargs: i32, nresults: i32, msgh: i32) -> Result<(), LuaError> {
636        pcall_k(self, nargs, nresults, msgh, 0, None).map(|_| ())
637    }
638
639    /// protected call. When `k` is set and the thread is yieldable, an
640    /// inner yield propagates as `LuaError::Yield` and the continuation
641    /// fires on resume via `finishCcall` → `finishpcallk`.
642    pub fn protected_call_k(
643        &mut self,
644        nargs: i32,
645        nresults: i32,
646        msgh: i32,
647        ctx: isize,
648        k: Option<crate::state::LuaKFunction>,
649    ) -> Result<(), LuaError> {
650        pcall_k(self, nargs, nresults, msgh, ctx, k).map(|_| ())
651    }
652
653    pub fn push_string(&mut self, s: &[u8]) -> Result<(), LuaError> {
654        push_lstring(self, s)?;
655        Ok(())
656    }
657
658    pub fn push_c_closure(
659        &mut self,
660        f: fn(&mut LuaState) -> Result<usize, LuaError>,
661        n: i32,
662    ) -> Result<(), LuaError> {
663        push_cclosure(self, f, n)
664    }
665
666    pub fn raw_seti(&mut self, idx: i32, n: i64) -> Result<(), LuaError> {
667        raw_set_i(self, idx, n)
668    }
669
670    pub fn table_set_i(&mut self, idx: i32, n: i64) -> Result<(), LuaError> {
671        set_i(self, idx, n)
672    }
673
674    /// Get `t[n]` where `t` is a pre-resolved `LuaValue`, bypassing stack-index
675    /// resolution. Use this in tight loops that operate on the same table
676    /// repeatedly to avoid the `index_to_value` call per iteration.
677    pub fn table_get_i_value(&mut self, t: &LuaValue, n: i64) -> Result<LuaType, LuaError> {
678        get_i_value(self, t, n)
679    }
680
681    /// Set `t[n] = stack_top` (then pop) where `t` is a pre-resolved `LuaValue`,
682    /// bypassing stack-index resolution. Use this in tight loops that operate on
683    /// the same table repeatedly to avoid the `index_to_value` call per iteration.
684    pub fn table_set_i_value(&mut self, t: &LuaValue, n: i64) -> Result<(), LuaError> {
685        set_i_value(self, t, n)
686    }
687
688    pub fn create_table(&mut self, narr: i32, nrec: i32) -> Result<(), LuaError> {
689        create_table(self, narr, nrec)
690    }
691
692    /// Pop the value on top of the stack and store it in the registry under
693    /// the string `key`.
694    ///
695    pub fn registry_set(&mut self, key: &[u8]) -> Result<(), LuaError> {
696        set_field(self, LUA_REGISTRYINDEX, key)
697    }
698
699    /// Create a new metatable in the registry under key `tname`. Leaves the
700    /// new metatable on top of the stack and returns `true` when newly
701    /// created. If `registry[tname]` already exists, leaves it on top of the
702    /// stack and returns `false`.
703    ///
704    pub fn new_metatable(&mut self, tname: &[u8]) -> Result<bool, LuaError> {
705        if get_field(self, LUA_REGISTRYINDEX, tname)? != LuaType::Nil {
706            return Ok(false);
707        }
708        self.pop_n(1);
709        create_table(self, 0, 2)?;
710        push_lstring(self, tname)?;
711        set_field(self, -2, b"__name")?;
712        push_value(self, -1);
713        set_field(self, LUA_REGISTRYINDEX, tname)?;
714        Ok(true)
715    }
716
717    /// Create a new library table sized for `funcs` and register each entry as
718    /// a closure field on it. Leaves the table on the top of the stack.
719    ///
720    ///   `luaL_checkversion(L), luaL_newlibtable(L,l), luaL_setfuncs(L,l,0)`.
721    /// `luaL_checkversion` is a no-op here (no ABI-version mismatch is
722    /// possible inside the Rust port).
723    pub fn new_lib(
724        &mut self,
725        funcs: &[(&[u8], LuaCFunction)],
726    ) -> Result<(), LuaError> {
727        create_table(self, 0, funcs.len() as i32)?;
728        for (name, f) in funcs {
729            push_cclosure(self, *f, 0)?;
730            set_field(self, -2, name)?;
731        }
732        Ok(())
733    }
734
735    /// Create and populate a library table for `funcs`, leaving it on top of
736    /// the stack. The `_name` argument is informational and matches the
737    /// `luaL_register`-style call sites in the Phase-A stdlib; the actual
738    /// global binding for the library happens later via `luaL_requiref`.
739    ///
740    pub fn register_lib(
741        &mut self,
742        _name: &[u8],
743        funcs: &[(&[u8], LuaCFunction)],
744    ) -> Result<(), LuaError> {
745        self.new_lib(funcs)
746    }
747
748    /// Create a new empty table presized to hold every entry in `funcs`, and
749    /// leave it on top of the stack. No registration is performed — callers
750    /// typically follow up with `set_funcs` / `set_funcs_with_upvalues` to
751    /// populate the table.
752    ///
753    ///   `lua_createtable(L, 0, sizeof(l)/sizeof((l)[0]) - 1)`. The C macro's
754    /// `- 1` discounts the sentinel `{NULL, NULL}` entry; the Rust slice has
755    /// no sentinel, so we use `funcs.len()` directly.
756    pub fn new_lib_table(
757        &mut self,
758        funcs: &[(&[u8], LuaCFunction)],
759    ) -> Result<(), LuaError> {
760        create_table(self, 0, funcs.len() as i32)
761    }
762
763    /// Register each entry in `funcs` as a C closure on the table at index
764    /// `-(nup + 2)`, sharing the `nup` values currently on top of the stack
765    /// as upvalues. The upvalues are popped at the end.
766    ///
767    pub fn set_funcs_with_upvalues(
768        &mut self,
769        funcs: &[(&[u8], LuaCFunction)],
770        nup: i32,
771    ) -> Result<(), LuaError> {
772        check_stack(self, nup);
773        for (name, f) in funcs {
774            for _ in 0..nup {
775                push_value(self, -nup);
776            }
777            push_cclosure(self, *f, nup)?;
778            set_field(self, -(nup + 2), name)?;
779        }
780        self.pop_n(nup as usize);
781        Ok(())
782    }
783
784    pub fn set_metatable(&mut self, objindex: i32) -> Result<(), LuaError> {
785        set_metatable(self, objindex)?;
786        Ok(())
787    }
788
789    /// Fetch the metatable registered under `name` in the registry and assign
790    /// it as the metatable of the value currently on top of the stack. The
791    /// fetched metatable is popped after assignment, leaving the original top
792    /// value in place.
793    ///
794    pub fn set_metatable_by_name(&mut self, name: &[u8]) -> Result<(), LuaError> {
795        get_field(self, LUA_REGISTRYINDEX, name)?;
796        set_metatable(self, -2)?;
797        Ok(())
798    }
799
800    /// Ensure `registry[name]` is a table; push it onto the stack.
801    /// Returns `true` if the table already existed, `false` if newly created.
802    ///
803    pub fn get_subtable_registry(&mut self, name: &[u8]) -> Result<bool, LuaError> {
804        if get_field(self, LUA_REGISTRYINDEX, name)? == LuaType::Table {
805            return Ok(true);
806        }
807        self.pop_n(1);
808        let idx = abs_index(self, LUA_REGISTRYINDEX);
809        let new_tbl = self.new_table();
810        self.push(LuaValue::Table(new_tbl));
811        push_value(self, -1);
812        set_field(self, idx, name)?;
813        Ok(false)
814    }
815
816    /// Allocate a fresh full-userdata block of `size` bytes with `nuvalue`
817    /// nil-initialised user-value slots, push it on the stack, and return a
818    /// `GcRef` to it. The `_name` parameter is advisory — callers typically
819    /// follow up with `set_metatable_by_name(name)` to attach the registered
820    /// metatable.
821    ///
822    /// C-correspondent: `lua_newuserdatauv(L, size, nuvalue)` (no name
823    /// parameter on the C side; the Rust signature carries it for callers'
824    /// convenience).
825    pub fn new_userdata_typed(
826        &mut self,
827        _name: &[u8],
828        size: usize,
829        nuvalue: i32,
830    ) -> Result<GcRef<LuaUserData>, LuaError> {
831        debug_assert!(nuvalue >= 0 && nuvalue < u16::MAX as i32, "invalid value");
832        // TODO(D-1c-bridge): state.new_userdata is still todo!(); keep direct alloc
833        let u = GcRef::new(LuaUserData {
834            data: vec![0u8; size].into_boxed_slice(),
835            uv: std::cell::RefCell::new(vec![LuaValue::Nil; nuvalue as usize]),
836            metatable: std::cell::RefCell::new(None),
837            host_value: std::cell::RefCell::new(None),
838        });
839        u.account_buffer(u.buffer_bytes() as isize);
840        self.push(LuaValue::UserData(u.clone()));
841        self.gc().check_step();
842        Ok(u)
843    }
844}
845
846// ── access functions (stack → Rust) ──────────────────────────────────────────
847
848pub fn lua_type_at(state: &LuaState, idx: i32) -> LuaType {
849    if !is_valid_index(state, idx) {
850        return LuaType::None;
851    }
852    index_to_value(state, idx).base_type()
853}
854
855pub fn type_name(_state: &LuaState, t: LuaType) -> &'static [u8] {
856    t.type_name()
857}
858
859pub fn is_cfunction(state: &LuaState, idx: i32) -> bool {
860    let o = index_to_value(state, idx);
861    matches!(o, LuaValue::Function(LuaClosure::LightC(_)) | LuaValue::Function(LuaClosure::C(_)))
862}
863
864pub fn is_integer(state: &LuaState, idx: i32) -> bool {
865    let o = index_to_value(state, idx);
866    matches!(o, LuaValue::Int(_))
867}
868
869pub fn is_number(state: &LuaState, idx: i32) -> bool {
870    let o = index_to_value(state, idx);
871    o.to_number_with_strconv().is_some()
872}
873
874pub fn is_string(state: &LuaState, idx: i32) -> bool {
875    let o = index_to_value(state, idx);
876    matches!(o, LuaValue::Str(_) | LuaValue::Int(_) | LuaValue::Float(_))
877}
878
879pub fn is_userdata(state: &LuaState, idx: i32) -> bool {
880    let o = index_to_value(state, idx);
881    matches!(o, LuaValue::UserData(_) | LuaValue::LightUserData(_))
882}
883
884pub fn raw_equal(state: &LuaState, index1: i32, index2: i32) -> bool {
885    if !is_valid_index(state, index1) || !is_valid_index(state, index2) {
886        return false;
887    }
888    let o1 = index_to_value(state, index1);
889    let o2 = index_to_value(state, index2);
890    state.equal_obj(None, &o1, &o2)
891}
892
893// PORT NOTE: LUA_OPUNM / LUA_OPBNOT are unary; all others are binary.
894pub fn arith(state: &mut LuaState, op: i32) -> Result<(), LuaError> {
895    // TODO(port): LUA_OPUNM and LUA_OPBNOT constant values not yet defined in
896    // Rust; using raw i32 comparison for now.
897    const LUA_OPUNM: i32 = 12;
898    const LUA_OPBNOT: i32 = 14;
899    if op == LUA_OPUNM || op == LUA_OPBNOT {
900        // unary — duplicate top as fake second operand
901        let top_val = state.get_at(state.top_idx() - 1);
902        state.push(top_val);
903    }
904    let top = state.top_idx();
905    let a = state.get_at(top - 2);
906    let b = state.get_at(top - 1);
907    let result = state.arith_op(op, &a, &b)?;
908    state.set_at(top - 2, result);
909    state.pop();
910    Ok(())
911}
912
913pub fn compare(state: &mut LuaState, index1: i32, index2: i32, op: i32) -> Result<bool, LuaError> {
914    let valid = is_valid_index(state, index1) && is_valid_index(state, index2);
915    let o1 = index_to_value(state, index1);
916    let o2 = index_to_value(state, index2);
917    if valid {
918        match op {
919            0 => Ok(state.equal_obj_with_tm(&o1, &o2)?),
920            1 => state.less_than(&o1, &o2),
921            2 => state.less_equal(&o1, &o2),
922            _ => {
923                debug_assert!(false, "invalid option");
924                Ok(false)
925            }
926        }
927    } else {
928        Ok(false)
929    }
930}
931
932pub fn string_to_number(state: &mut LuaState, s: &[u8]) -> usize {
933    // TODO(port): luaO_str2num not yet translated; push result if successful.
934    match state.str_to_num(s) {
935        Some((val, consumed)) => {
936            state.push(val);
937            consumed
938        }
939        None => 0,
940    }
941}
942
943pub fn to_number_x(state: &LuaState, idx: i32) -> Option<f64> {
944    let o = index_to_value(state, idx);
945    o.to_number_with_strconv()
946}
947
948pub fn to_integer_x(state: &LuaState, idx: i32) -> Option<i64> {
949    let o = index_to_value(state, idx);
950    o.to_integer_with_strconv()
951}
952
953pub fn to_boolean(state: &LuaState, idx: i32) -> bool {
954    let o = index_to_value(state, idx);
955    !matches!(o, LuaValue::Nil | LuaValue::Bool(false))
956}
957
958// PORT NOTE: returns Option<GcRef<LuaString>> instead of raw C pointer+len.
959pub fn to_lua_string(
960    state: &mut LuaState,
961    idx: i32,
962) -> Result<Option<GcRef<LuaString>>, LuaError> {
963    let o = index_to_value(state, idx);
964    if let LuaValue::Str(s) = &o {
965        return Ok(Some(s.clone()));
966    }
967    if !matches!(o, LuaValue::Int(_) | LuaValue::Float(_)) {
968        return Ok(None);
969    }
970    state.obj_to_string(idx)?;
971    state.gc().check_step();
972    let updated = index_to_value(state, idx);
973    if let LuaValue::Str(s) = updated {
974        Ok(Some(s))
975    } else {
976        Ok(None)
977    }
978}
979
980pub fn raw_len(state: &LuaState, idx: i32) -> u64 {
981    let o = index_to_value(state, idx);
982    match &o {
983        LuaValue::Str(s) => s.len() as u64,
984        LuaValue::UserData(u) => u.len() as u64,
985        LuaValue::Table(t) => state.table_getn(t) as u64,
986        _ => 0,
987    }
988}
989
990pub fn to_cfunction(
991    state: &LuaState,
992    idx: i32,
993) -> Option<fn(&mut LuaState) -> Result<usize, LuaError>> {
994    let o = index_to_value(state, idx);
995    match o {
996        // TODO(phase-b): lua-types `LuaClosure::LightC` carries a placeholder
997        // `fn() -> i32` until it can reference `LuaState`. The real cast
998        // happens once lua-types absorbs the LuaState-aware signature.
999        LuaValue::Function(LuaClosure::LightC(_f)) => None,
1000        LuaValue::Function(LuaClosure::C(_ccl)) => None,
1001        _ => None,
1002    }
1003}
1004
1005#[inline]
1006fn to_userdata_ptr(o: &LuaValue) -> Option<*mut core::ffi::c_void> {
1007    match o {
1008        LuaValue::UserData(u) => {
1009            // TODO(port): getudatamem returns a pointer to the raw byte payload of Udata.
1010            // In Rust, LuaUserData carries a Box<[u8]>; we'd need to return a raw ptr.
1011            // This is only safe inside lua-gc; stubbing with None for Phase A.
1012            let _ = u;
1013            None
1014        }
1015        LuaValue::LightUserData(p) => Some(*p),
1016        _ => None,
1017    }
1018}
1019
1020pub fn to_userdata(state: &LuaState, idx: i32) -> Option<*mut core::ffi::c_void> {
1021    let o = index_to_value(state, idx);
1022    to_userdata_ptr(&o)
1023}
1024
1025pub fn to_thread(state: &LuaState, idx: i32) -> Option<GcRef<lua_types::value::LuaThread>> {
1026    // TODO(phase-b): lua-vm's rich LuaState is not the same type as
1027    // lua_types::value::LuaThread; the latter is a placeholder. Resolve in
1028    // Phase B by unifying thread types.
1029    let o = index_to_value(state, idx);
1030    if let LuaValue::Thread(t) = o {
1031        Some(t)
1032    } else {
1033        None
1034    }
1035}
1036
1037// PORT NOTE: returns a usize (opaque identity) rather than a raw void*.
1038// Raw pointers are only allowed in lua-gc / lua-coro.
1039pub fn to_pointer(state: &LuaState, idx: i32) -> Option<usize> {
1040    let o = index_to_value(state, idx);
1041    // TODO(port): returning a raw pointer here is not safe outside lua-gc.
1042    // Returning the GC identity as a usize for opaque pointer identity purposes.
1043    match &o {
1044        LuaValue::Function(LuaClosure::LightC(f)) => Some(*f as usize),
1045        LuaValue::LightUserData(p) => Some(*p as usize),
1046        LuaValue::Str(s) => Some(GcRef::identity(s)),
1047        LuaValue::Table(t) => Some(GcRef::identity(t)),
1048        LuaValue::Function(LuaClosure::Lua(f)) => Some(GcRef::identity(f)),
1049        LuaValue::Function(LuaClosure::C(f)) => Some(GcRef::identity(f)),
1050        LuaValue::UserData(u) => Some(GcRef::identity(u)),
1051        LuaValue::Thread(t) => Some(GcRef::identity(t)),
1052        _ => None,
1053    }
1054}
1055
1056// ── push functions (Rust → stack) ────────────────────────────────────────────
1057
1058pub fn push_nil(state: &mut LuaState) {
1059    state.push(LuaValue::Nil);
1060}
1061
1062pub fn push_number(state: &mut LuaState, n: f64) {
1063    state.push(LuaValue::Float(n));
1064}
1065
1066pub fn push_integer(state: &mut LuaState, n: i64) {
1067    state.push(LuaValue::Int(n));
1068}
1069
1070// PORT NOTE: returns the interned LuaString instead of a raw C pointer.
1071pub fn push_lstring(state: &mut LuaState, s: &[u8]) -> Result<GcRef<LuaString>, LuaError> {
1072    let ts = state.intern_str(s)?;
1073    state.push(LuaValue::Str(ts.clone()));
1074    state.gc().check_step();
1075    Ok(ts)
1076}
1077
1078pub fn push_string(state: &mut LuaState, s: Option<&[u8]>) -> Result<Option<GcRef<LuaString>>, LuaError> {
1079    match s {
1080        None => {
1081            state.push(LuaValue::Nil);
1082            state.gc().check_step();
1083            Ok(None)
1084        }
1085        Some(bytes) => {
1086            let ts = state.intern_str(bytes)?;
1087            state.push(LuaValue::Str(ts.clone()));
1088            state.gc().check_step();
1089            Ok(Some(ts))
1090        }
1091    }
1092}
1093
1094// PORT NOTE: va_list is not representable in safe Rust; callers pass a pre-formatted &[u8].
1095// TODO(port): lua_pushvfstring uses C varargs (va_list); no direct Rust equivalent.
1096// The Rust API uses state.push_fstring(format_args!(...)) instead.
1097pub fn push_vfstring(state: &mut LuaState, formatted: &[u8]) -> Result<GcRef<LuaString>, LuaError> {
1098    let ts = state.intern_str(formatted)?;
1099    state.push(LuaValue::Str(ts.clone()));
1100    state.gc().check_step();
1101    Ok(ts)
1102}
1103
1104// PORT NOTE: C varargs not used; callers use format_args! and push_fstring.
1105pub fn push_fstring(state: &mut LuaState, formatted: &[u8]) -> Result<GcRef<LuaString>, LuaError> {
1106    push_vfstring(state, formatted)
1107}
1108
1109pub fn push_cclosure(
1110    state: &mut LuaState,
1111    f: fn(&mut LuaState) -> Result<usize, LuaError>,
1112    n: i32,
1113) -> Result<(), LuaError> {
1114    //    if (n == 0) { setfvalue(s2v(L->top.p), fn); api_incr_top(L); }
1115    //    else { api_checknelems(L, n); api_check(L, n <= MAXUPVAL, ...);
1116    //           cl = luaF_newCclosure(L, n); cl->f = fn;
1117    //           L->top.p -= n;
1118    //           while (n--) setobj2n(L, &cl->upvalue[n], s2v(L->top.p + n));
1119    //           setclCvalue(L, s2v(L->top.p), cl); api_incr_top(L);
1120    //           luaC_checkGC(L); }
1121    //    lua_unlock(L);
1122    //
1123    // PORT NOTE: `LuaClosure::LightC` and `LuaCClosure` carry a `LuaCFnPtr`
1124    // (a `usize` index into `GlobalState.c_functions`) rather than the raw
1125    // function pointer, because lua-types cannot reference `LuaState`. We
1126    // register `f` in the per-state registry and store the resulting index.
1127    let idx: lua_types::closure::LuaCFnPtr = {
1128        let mut g = state.global_mut();
1129        if n == 0 {
1130            match g.c_functions.iter().position(|existing| {
1131                existing
1132                    .as_bare()
1133                    .is_some_and(|existing| std::ptr::fn_addr_eq(existing, f))
1134            }) {
1135                Some(i) => i,
1136                None => {
1137                    let i = g.c_functions.len();
1138                    g.c_functions.push(LuaCallable::bare(f));
1139                    i
1140                }
1141            }
1142        } else {
1143            let i = g.c_functions.len();
1144            g.c_functions.push(LuaCallable::bare(f));
1145            i
1146        }
1147    };
1148    if n == 0 {
1149        state.push(LuaValue::Function(LuaClosure::LightC(idx)));
1150    } else {
1151        debug_assert!(n > 0 && (n as u32) <= MAX_UPVAL as u32, "upvalue index too large");
1152        let n_usize = n as usize;
1153        let top = state.top_idx();
1154        debug_assert!((top.0 as usize) >= n_usize, "not enough elements on stack");
1155        let base = top.0 as usize - n_usize;
1156        let mut upvalues: Vec<LuaValue> = Vec::with_capacity(n_usize);
1157        for i in 0..n_usize {
1158            upvalues.push(state.get_at(crate::state::StackIdx((base + i) as u32)));
1159        }
1160        state.pop_n(n_usize);
1161        // TODO(D-1c-bridge): state.new_c_closure is still todo!(); keep direct alloc
1162        let cl = LuaClosure::C(GcRef::new(lua_types::closure::LuaCClosure {
1163            func: idx,
1164            upvalues: std::cell::RefCell::new(upvalues),
1165        }));
1166        if let LuaClosure::C(ccl) = &cl {
1167            ccl.account_buffer(ccl.buffer_bytes() as isize);
1168        }
1169        state.push(LuaValue::Function(cl));
1170        state.gc().check_step();
1171    }
1172    Ok(())
1173}
1174
1175pub fn push_boolean(state: &mut LuaState, b: bool) {
1176    state.push(LuaValue::Bool(b));
1177}
1178
1179pub fn push_light_userdata(state: &mut LuaState, p: *mut core::ffi::c_void) {
1180    state.push(LuaValue::LightUserData(p));
1181}
1182
1183// Returns true if pushed thread is the main thread.
1184pub fn push_thread(state: &mut LuaState) -> bool {
1185    let (value, is_main) = {
1186        let g = state.global();
1187        let id = g.current_thread_id;
1188        let v = g
1189            .thread_value_for(id)
1190            .expect("current_thread_id must always resolve to a registered thread");
1191        (v, id == g.main_thread_id)
1192    };
1193    state.push(LuaValue::Thread(value));
1194    is_main
1195}
1196
1197// ── get functions (Lua → stack) ───────────────────────────────────────────────
1198
1199fn aux_get_str(state: &mut LuaState, t: LuaValue, k: &[u8]) -> Result<LuaType, LuaError> {
1200    let str_val = {
1201        let ts = state.intern_str(k)?;
1202        LuaValue::Str(ts)
1203    };
1204    // TODO(port): luaV_fastget / luaV_finishget not yet translated; using
1205    // a simplified table_get that may miss metamethod chains.
1206    let result = state.table_get_with_tm(&t, &str_val)?;
1207    state.push(result);
1208    let top = state.top_idx();
1209    Ok(state.get_at(top - 1).base_type())
1210}
1211
1212fn get_global_table(state: &LuaState) -> LuaValue {
1213    // PORT NOTE (phase-b-reconcile): The lua-types LuaTable placeholder has
1214    // no storage, so we cannot fetch the globals table from the registry's
1215    // array slot. init_registry now stashes globals in a direct
1216    // GlobalState field; read it from there until the LuaTable placeholder
1217    // reconciles with lua-vm::table::LuaTable.
1218    state.global().globals.clone()
1219}
1220
1221pub fn get_global(state: &mut LuaState, name: &[u8]) -> Result<LuaType, LuaError> {
1222    let g = get_global_table(state);
1223    aux_get_str(state, g, name)
1224}
1225
1226pub fn get_table(state: &mut LuaState, idx: i32) -> Result<LuaType, LuaError> {
1227    let t = index_to_value(state, idx);
1228    let top = state.top_idx();
1229    let key = state.get_at(top - 1);
1230    let result = state.table_get_with_tm(&t, &key)?;
1231    state.set_at(top - 1, result);
1232    let val = state.get_at(top - 1);
1233    Ok(val.base_type())
1234}
1235
1236pub fn get_field(state: &mut LuaState, idx: i32, k: &[u8]) -> Result<LuaType, LuaError> {
1237    let t = index_to_value(state, idx);
1238    aux_get_str(state, t, k)
1239}
1240
1241pub fn get_i(state: &mut LuaState, idx: i32, n: i64) -> Result<LuaType, LuaError> {
1242    let t = index_to_value(state, idx);
1243    let key = LuaValue::Int(n);
1244    let result = state.table_get_with_tm(&t, &key)?;
1245    state.push(result);
1246    let top = state.top_idx();
1247    Ok(state.get_at(top - 1).base_type())
1248}
1249
1250/// Variant of `get_i` that accepts a pre-resolved table value instead of a
1251/// stack index. Callers that invoke `get_i` repeatedly on the same table
1252/// (e.g. the shift loops in `table.remove` / `table.insert`) should resolve
1253/// the table once and use this function to avoid calling `index_to_value`
1254/// on every iteration.
1255pub fn get_i_value(state: &mut LuaState, t: &LuaValue, n: i64) -> Result<LuaType, LuaError> {
1256    let key = LuaValue::Int(n);
1257    let result = state.table_get_with_tm(t, &key)?;
1258    state.push(result);
1259    let top = state.top_idx();
1260    Ok(state.get_at(top - 1).base_type())
1261}
1262
1263fn finish_raw_get(state: &mut LuaState, val: Option<LuaValue>) -> LuaType {
1264    let v = val.unwrap_or(LuaValue::Nil);
1265    state.push(v);
1266    let top = state.top_idx();
1267    state.get_at(top - 1).base_type()
1268}
1269
1270fn get_table_value(state: &LuaState, idx: i32) -> Option<GcRef<LuaTable>> {
1271    let t = index_to_value(state, idx);
1272    debug_assert!(matches!(t, LuaValue::Table(_)), "table expected");
1273    if let LuaValue::Table(tbl) = t {
1274        Some(tbl)
1275    } else {
1276        None
1277    }
1278}
1279
1280pub fn raw_get(state: &mut LuaState, idx: i32) -> LuaType {
1281    let t = get_table_value(state, idx);
1282    let top = state.top_idx();
1283    let key = state.get_at(top - 1);
1284    let val = t.as_ref().map(|tbl| tbl.get(&key));
1285    state.set_top_idx(top - 1);
1286    finish_raw_get(state, val)
1287}
1288
1289pub fn raw_get_i(state: &mut LuaState, idx: i32, n: i64) -> LuaType {
1290    let t = get_table_value(state, idx);
1291    let val = t.as_ref().map(|tbl| tbl.get_int(n));
1292    finish_raw_get(state, val)
1293}
1294
1295pub fn raw_get_p(state: &mut LuaState, idx: i32, p: *const core::ffi::c_void) -> LuaType {
1296    let t = get_table_value(state, idx);
1297    let key = LuaValue::LightUserData(p as *mut core::ffi::c_void);
1298    let val = t.as_ref().map(|tbl| tbl.get(&key));
1299    finish_raw_get(state, val)
1300}
1301
1302pub fn create_table(state: &mut LuaState, narray: i32, nrec: i32) -> Result<(), LuaError> {
1303    let t = state.new_table();
1304    if narray > 0 || nrec > 0 {
1305        t.resize(state, narray as usize, nrec as usize)?;
1306    }
1307    state.push(LuaValue::Table(t));
1308    state.gc().check_step();
1309    Ok(())
1310}
1311
1312pub fn get_metatable(state: &mut LuaState, objindex: i32) -> bool {
1313    let obj = index_to_value(state, objindex);
1314    let mt: Option<GcRef<LuaTable>> = match &obj {
1315        LuaValue::Table(t) => t.metatable(),
1316        LuaValue::UserData(u) => u.metatable(),
1317        other => {
1318            let idx = other.base_type() as usize;
1319            state.global().mt[idx].clone()
1320        }
1321    };
1322    if let Some(mt_table) = mt {
1323        state.push(LuaValue::Table(mt_table));
1324        true
1325    } else {
1326        false
1327    }
1328}
1329
1330pub fn get_i_uservalue(state: &mut LuaState, idx: i32, n: i32) -> LuaType {
1331    let o = index_to_value(state, idx);
1332    debug_assert!(matches!(o, LuaValue::UserData(_)), "full userdata expected");
1333    if let LuaValue::UserData(ref u) = o {
1334        let uv = u.uv.borrow();
1335        let uv_count = uv.len() as i32;
1336        if n <= 0 || n > uv_count {
1337            state.push(LuaValue::Nil);
1338            LuaType::None
1339        } else {
1340            let val = uv[(n - 1) as usize].clone();
1341            let t = val.base_type();
1342            state.push(val);
1343            t
1344        }
1345    } else {
1346        state.push(LuaValue::Nil);
1347        LuaType::None
1348    }
1349}
1350
1351// ── set functions (stack → Lua) ───────────────────────────────────────────────
1352
1353fn aux_set_str(state: &mut LuaState, t: LuaValue, k: &[u8]) -> Result<(), LuaError> {
1354    let str_val = {
1355        let ts = state.intern_str(k)?;
1356        LuaValue::Str(ts)
1357    };
1358    //       luaV_finishfastset(L, t, slot, s2v(L->top.p - 1)); L->top.p--;
1359    //    else { setsvalue2s L->top.p str; api_incr_top;
1360    //           luaV_finishset(L, t, s2v(L->top.p-1), s2v(L->top.p-2), slot);
1361    //           L->top.p -= 2; }
1362    let top = state.top_idx();
1363    let val = state.get_at(top - 1);
1364    state.table_set_with_tm(&t, str_val, val)?;
1365    state.pop();
1366    Ok(())
1367}
1368
1369pub fn set_global(state: &mut LuaState, name: &[u8]) -> Result<(), LuaError> {
1370    let g = get_global_table(state);
1371    aux_set_str(state, g, name)
1372}
1373
1374pub fn set_table(state: &mut LuaState, idx: i32) -> Result<(), LuaError> {
1375    let t = index_to_value(state, idx);
1376    let top = state.top_idx();
1377    let key = state.get_at(top - 2);
1378    let val = state.get_at(top - 1);
1379    state.table_set_with_tm(&t, key, val)?;
1380    state.set_top_idx(top - 2);
1381    Ok(())
1382}
1383
1384pub fn set_field(state: &mut LuaState, idx: i32, k: &[u8]) -> Result<(), LuaError> {
1385    let t = index_to_value(state, idx);
1386    aux_set_str(state, t, k)
1387}
1388
1389pub fn set_i(state: &mut LuaState, idx: i32, n: i64) -> Result<(), LuaError> {
1390    let t = index_to_value(state, idx);
1391    let top = state.top_idx();
1392    let val = state.get_at(top - 1);
1393    let key = LuaValue::Int(n);
1394    state.table_set_with_tm(&t, key, val)?;
1395    state.pop();
1396    Ok(())
1397}
1398
1399/// Variant of `set_i` that accepts a pre-resolved table value instead of a
1400/// stack index. Callers that invoke `set_i` repeatedly on the same table
1401/// (e.g. the shift loops in `table.remove` / `table.insert`) should resolve
1402/// the table once and use this function to avoid calling `index_to_value`
1403/// on every iteration.
1404pub fn set_i_value(state: &mut LuaState, t: &LuaValue, n: i64) -> Result<(), LuaError> {
1405    let top = state.top_idx();
1406    let val = state.get_at(top - 1);
1407    let key = LuaValue::Int(n);
1408    state.table_set_with_tm(t, key, val)?;
1409    state.pop();
1410    Ok(())
1411}
1412
1413fn aux_raw_set(state: &mut LuaState, idx: i32, key: LuaValue, n: u32) -> Result<(), LuaError> {
1414    let t = get_table_value(state, idx)
1415        .ok_or_else(|| LuaError::runtime(format_args!("table expected")))?;
1416    let top = state.top_idx();
1417    let val = state.get_at(top - 1);
1418    t.raw_set(state, key, val)?;
1419    t.invalidate_tm_cache();
1420    let top_val = state.get_at(top - 1);
1421    state.gc().barrier_back(&t, &top_val);
1422    state.set_top_idx(top - n as i32);
1423    Ok(())
1424}
1425
1426pub fn raw_set(state: &mut LuaState, idx: i32) -> Result<(), LuaError> {
1427    let top = state.top_idx();
1428    let key = state.get_at(top - 2);
1429    aux_raw_set(state, idx, key, 2)
1430}
1431
1432pub fn raw_set_p(state: &mut LuaState, idx: i32, p: *const core::ffi::c_void) -> Result<(), LuaError> {
1433    let key = LuaValue::LightUserData(p as *mut core::ffi::c_void);
1434    aux_raw_set(state, idx, key, 1)
1435}
1436
1437pub fn raw_set_i(state: &mut LuaState, idx: i32, n: i64) -> Result<(), LuaError> {
1438    let t = get_table_value(state, idx)
1439        .ok_or_else(|| LuaError::runtime(format_args!("table expected")))?;
1440    let top = state.top_idx();
1441    let val = state.get_at(top - 1);
1442    t.raw_set_int(state, n, val)?;
1443    let top_val = state.get_at(top - 1);
1444    state.gc().barrier_back(&t, &top_val);
1445    state.pop();
1446    Ok(())
1447}
1448
1449/// Returns true if `mt` (a metatable) holds a non-nil `__gc` entry.
1450///
1451/// PORT NOTE: Mirrors the body of C's `tofinalize` in `lgc.c` minus the bits
1452/// that consult per-object GC bits (irrelevant in Phase B's Rc world).
1453fn metatable_has_gc(state: &LuaState, mt: &GcRef<LuaTable>) -> bool {
1454    let name = state.global().tmname[crate::tagmethods::TagMethod::Gc as usize].clone();
1455    !matches!(mt.get_short_str(&name), LuaValue::Nil)
1456}
1457
1458fn register_finalizable_object(state: &mut LuaState, object: FinalizerObject) {
1459    let heap_ptr = object.heap_ptr();
1460    let mut g = state.global_mut();
1461    if g.finalizers.push_pending_unique(object) {
1462        if let Some(ptr) = heap_ptr {
1463            g.heap.move_allgc_to_finobj(ptr);
1464        }
1465    }
1466}
1467
1468/// Drain objects already promoted from `pending_finalizers` to
1469/// `to_be_finalized` by the heap mark phase.
1470pub fn run_pending_finalizers(state: &mut LuaState) {
1471    let _ = run_pending_finalizers_inner(state, false);
1472}
1473
1474const GC_FIN_MAX: usize = 10;
1475
1476/// `__gc` driver that mirrors C-Lua's `GCTM(L, propagateerrors)`.
1477///
1478/// `propagate` corresponds to C's `propagateerrors` argument. C calls
1479/// `GCTM(L, 1)` from `runafewfinalizers` (the explicit-collect / automatic
1480/// step paths) and `GCTM(L, 0)` from `callallpendingfinalizers` (the
1481/// `lua_close` path). When a finalizer errors and `propagate` is set, the
1482/// disposition is version-specific:
1483///
1484/// - 5.2 / 5.3: wrap the error object as `error in __gc metamethod (%s)`
1485///   (matching `GCTM`'s `LUA_ERRGCMM` branch) and re-throw, aborting the
1486///   drain. Returned here as `Err(LuaError)` for the caller to propagate.
1487/// - 5.4 / 5.5: `luaE_warnerror(L, "__gc")` — emit a warning and discard the
1488///   error. The warning is silent unless the program enabled warnings
1489///   (`warn("@on")`), which matches the reference default of swallowing
1490///   `__gc` errors with warnings off.
1491/// - 5.1: errors are silently swallowed (`luaC_callGCTM` ignores the status).
1492///
1493/// `propagate = false` (the close path) swallows the error on every version,
1494/// matching `callallpendingfinalizers`'s `GCTM(L, 0)`.
1495pub fn run_pending_finalizers_inner(
1496    state: &mut LuaState,
1497    propagate: bool,
1498) -> Result<(), LuaError> {
1499    run_pending_finalizers_limited(state, propagate, None).map(|_| ())
1500}
1501
1502pub(crate) fn run_some_pending_finalizers_inner(
1503    state: &mut LuaState,
1504    propagate: bool,
1505) -> Result<usize, LuaError> {
1506    run_pending_finalizers_limited(state, propagate, Some(GC_FIN_MAX))
1507}
1508
1509fn run_pending_finalizers_limited(
1510    state: &mut LuaState,
1511    propagate: bool,
1512    limit: Option<usize>,
1513) -> Result<usize, LuaError> {
1514    let version = state.global().lua_version;
1515    let mut processed = 0usize;
1516    loop {
1517        if limit.map_or(false, |limit| processed >= limit) {
1518            break;
1519        }
1520        // `to_be_finalized` is stored in tobefnz head order. Newly unreachable
1521        // objects are appended behind any older queued finalizers, matching
1522        // C-Lua's `finobj`/`tobefnz` ordering.
1523        if !state.global().finalizers.has_to_be_finalized() {
1524            break;
1525        }
1526        // The Phase-A pre-finalizer weak-value sweep (mirroring C-Lua's
1527        // `clearbyvalues(g, g->weak, NULL)` from `atomic()`) is no longer
1528        // needed: under D-2, weak-table sweeping runs inside the post-mark
1529        // hook of `Heap::full_collect_with_post_mark`, which uses
1530        // reachability instead of strong_count and therefore clears such
1531        // entries BEFORE this finalizer pass runs. The full "bug-in-5.1"
1532        // ordering (finalizer-visible state) still requires reachability-
1533        // based detection of which finalizable tables are about to die — a
1534        // gap tracked under D-2 ephemeron/finalizer follow-up.
1535        let object = {
1536            let mut g = state.global_mut();
1537            let object = g
1538                .finalizers
1539                .pop_to_be_finalized()
1540                .expect("to-be-finalized checked non-empty");
1541            if let Some(ptr) = object.heap_ptr() {
1542                g.heap.move_tobefnz_to_allgc(ptr);
1543            }
1544            object
1545        };
1546        processed += 1;
1547        let mt = object.metatable();
1548        let gc_fn = match mt {
1549            Some(ref m) => {
1550                let name = state.global().tmname[crate::tagmethods::TagMethod::Gc as usize].clone();
1551                m.get_short_str(&name)
1552            }
1553            None => LuaValue::Nil,
1554        };
1555        if !matches!(gc_fn, LuaValue::Function(_)) {
1556            continue;
1557        }
1558        let saved_top = state.top_idx();
1559        let ci_top = state.current_call_info().top;
1560        if saved_top.0 < ci_top.0 {
1561            state.clear_stack_range(saved_top, ci_top);
1562            state.set_top(ci_top);
1563        }
1564        state.push(gc_fn);
1565        state.push(object.as_lua_value());
1566        let func_idx = state.top_idx() - 2;
1567        let _heap_guard = {
1568            let g = state.global.borrow();
1569            lua_gc::HeapGuard::push(&g.heap)
1570        };
1571        let old_allowhook = state.allowhook;
1572        let old_gcstp = state.global_mut().stop_gc_internal();
1573        state.allowhook = false;
1574        let caller_ci = state.ci;
1575        let caller_status = state.get_ci(caller_ci).callstatus;
1576        state.get_ci_mut(caller_ci).callstatus = caller_status | crate::state::CIST_FIN;
1577        let status = crate::do_::pcall(
1578            state,
1579            |s| s.call_no_yield(func_idx, 0),
1580            func_idx,
1581            0,
1582        );
1583        // On error, `pcall` left the error object on top of the stack. Capture
1584        // it before `set_top` truncates so the version-specific disposition
1585        // below (propagate on 5.2/5.3, warn on 5.4/5.5) can use it.
1586        let finalizer_error: Option<LuaValue> =
1587            if status != LuaStatus::Ok {
1588                Some(state.get_at(state.top_idx() - 1).clone())
1589            } else {
1590                None
1591            };
1592        state.get_ci_mut(caller_ci).callstatus = caller_status;
1593        state.allowhook = old_allowhook;
1594        state.global_mut().set_gc_stop_flags(old_gcstp);
1595        state.set_top(saved_top);
1596
1597        if let Some(errobj) = finalizer_error {
1598            match version {
1599                lua_types::LuaVersion::V52 | lua_types::LuaVersion::V53 if propagate => {
1600                    // C `GCTM`: wrap a string error object as
1601                    // `error in __gc metamethod (%s)` and re-throw. A
1602                    // non-string object becomes `(no message)`. Aborts the
1603                    // drain (the remaining `to_be_finalized` entries are
1604                    // left for a later pass, matching the C longjmp out of
1605                    // `runafewfinalizers`).
1606                    let msg: Vec<u8> = match &errobj {
1607                        LuaValue::Str(s) => s.as_bytes().to_vec(),
1608                        _ => b"no message".to_vec(),
1609                    };
1610                    let mut wrapped = b"error in __gc metamethod (".to_vec();
1611                    wrapped.extend_from_slice(&msg);
1612                    wrapped.push(b')');
1613                    let interned = state.intern_str(&wrapped)?;
1614                    return Err(LuaError::from_value(LuaValue::Str(interned)));
1615                }
1616                lua_types::LuaVersion::V54 | lua_types::LuaVersion::V55 if propagate => {
1617                    // C `luaE_warnerror(L, "__gc")`: emit
1618                    // `error in __gc (<msg>)` through the warning system.
1619                    // Silent unless warnings are enabled.
1620                    let msg: Vec<u8> = match &errobj {
1621                        LuaValue::Str(s) => s.as_bytes().to_vec(),
1622                        _ => b"error object is not a string".to_vec(),
1623                    };
1624                    state.emit_warning(b"error in ", true);
1625                    state.emit_warning(b"__gc", true);
1626                    state.emit_warning(b" (", true);
1627                    state.emit_warning(&msg, true);
1628                    state.emit_warning(b")", false);
1629                }
1630                _ => {}
1631            }
1632        }
1633    }
1634    // Post-finalizer weak sweep is also obsolete: any weak entries newly
1635    // exposed by the finalizer pass will be cleared on the NEXT
1636    // `Heap::full_collect_with_post_mark`. We accept the one-cycle lag.
1637    Ok(processed)
1638}
1639
1640/// Run every still-pending `__gc` finalizer at state close.
1641///
1642/// Mirrors C-Lua's `luaC_freeallobjects` (`lgc.c`), which calls
1643/// `separatetobefnz(g, 1)` to move *all* remaining finalizable objects
1644/// (regardless of reachability) into the to-be-finalized list, then
1645/// `callallpendingfinalizers` to invoke each `__gc` before the objects are
1646/// freed. At `lua_close`, objects the program kept alive to program end —
1647/// e.g. a table held by a global — still have their finalizer run; that is
1648/// what emits messages like `>>> closing state <<<` from `gc.lua`.
1649///
1650/// The typed registry of finalizable objects is `pending_finalizers`. A single
1651/// snapshot of that list is promoted into `to_be_finalized` and drained by
1652/// [`run_pending_finalizers`]. We snapshot
1653/// once (matching C's single `separatetobefnz` call): a finalizer may
1654/// resurrect its object or register new finalizables via `setmetatable`, but
1655/// C does not re-finalize those at close (`gcstp = GCSTPCLS`), so neither do
1656/// we — the freshly-registered entries are left in `pending_finalizers` and
1657/// simply dropped with the state.
1658pub fn run_close_finalizers(state: &mut LuaState) {
1659    let pending: Vec<FinalizerObject> = state.global_mut().finalizers.take_pending();
1660    if pending.is_empty() {
1661        return;
1662    }
1663    let mut seen = std::collections::HashSet::<usize>::new();
1664    {
1665        let mut g = state.global_mut();
1666        for object in pending.into_iter().rev() {
1667            let heap_ptr = object.heap_ptr();
1668            if seen.insert(object.identity()) {
1669                g.finalizers.push_to_be_finalized(object);
1670                if let Some(ptr) = heap_ptr {
1671                    g.heap.move_finobj_to_tobefnz(ptr);
1672                }
1673            }
1674        }
1675    }
1676    run_pending_finalizers(state);
1677}
1678
1679/// Snapshot the currently-live weak tables from
1680/// `GlobalState.weak_tables_registry`, deduplicating by Rc pointer and
1681/// dropping any whose backing storage has been freed. Used by both the
1682/// pre-finalizer and post-finalizer sweeps in [`run_pending_finalizers`]
1683/// and by the explicit `collectgarbage("collect")` path.
1684fn collect_live_weak_tables(state: &mut LuaState) -> Vec<GcRef<lua_types::value::LuaTable>> {
1685    let mut g = state.global_mut();
1686    g.weak_tables_registry.live_snapshot()
1687}
1688
1689pub fn set_metatable(state: &mut LuaState, objindex: i32) -> Result<bool, LuaError> {
1690    let top = state.top_idx();
1691    let mt_val = state.get_at(top - 1);
1692    let mt: Option<GcRef<LuaTable>> = if matches!(mt_val, LuaValue::Nil) {
1693        None
1694    } else {
1695        debug_assert!(matches!(mt_val, LuaValue::Table(_)), "table expected");
1696        if let LuaValue::Table(t) = mt_val {
1697            Some(t)
1698        } else {
1699            None
1700        }
1701    };
1702
1703    let obj = index_to_value(state, objindex);
1704    match obj {
1705        LuaValue::Table(ref tbl) => {
1706            if mt.is_some() {
1707                state.gc().obj_barrier(tbl, mt.as_ref().unwrap());
1708            }
1709            tbl.set_metatable(mt.clone());
1710            if tbl.weak_mode() == 0 {
1711                state
1712                    .global_mut()
1713                    .weak_tables_registry
1714                    .remove_identity(tbl.identity());
1715            } else {
1716                state
1717                    .global_mut()
1718                    .weak_tables_registry
1719                    .push_unique(WeakTableEntry::new(tbl));
1720            }
1721            // Phase-B finalizer registration: if the new metatable carries
1722            // `__gc` and `obj` was not already registered, pin `obj` in the
1723            // pending-finalizers list so that `run_pending_finalizers` can
1724            // invoke the finalizer before the object is freed.
1725            //
1726            // Lua 5.1 has no `__gc` on tables — only userdata can be finalized.
1727            // Setting `__gc` on a table metatable is inert under V51 (no call,
1728            // no error). `__gc` on tables was added in 5.2, so only register
1729            // table finalizers off V51.
1730            let tables_finalizable =
1731                !matches!(state.global().lua_version, lua_types::LuaVersion::V51);
1732            if tables_finalizable {
1733                if let Some(ref mt_table) = mt {
1734                    if metatable_has_gc(state, mt_table) {
1735                        register_finalizable_object(state, FinalizerObject::Table(tbl.clone()));
1736                    }
1737                }
1738            }
1739        }
1740        LuaValue::UserData(ref ud) => {
1741            if let Some(ref mt_table) = mt {
1742                state.gc().obj_barrier(ud, mt_table);
1743                if metatable_has_gc(state, mt_table) {
1744                    register_finalizable_object(state, FinalizerObject::UserData(ud.clone()));
1745                }
1746            }
1747            ud.set_metatable(mt);
1748        }
1749        ref other => {
1750            let idx = other.base_type() as usize;
1751            state.global_mut().mt[idx] = mt;
1752        }
1753    }
1754    state.pop();
1755    Ok(true)
1756}
1757
1758pub fn set_i_uservalue(state: &mut LuaState, idx: i32, n: i32) -> Result<bool, LuaError> {
1759    let o = index_to_value(state, idx);
1760    debug_assert!(matches!(o, LuaValue::UserData(_)), "full userdata expected");
1761    let top = state.top_idx();
1762    let val = state.get_at(top - 1);
1763    let res = if let LuaValue::UserData(ref ud) = o {
1764        let mut uv = ud.uv.borrow_mut();
1765        let nuvalue = uv.len() as i32;
1766        if n < 1 || n > nuvalue {
1767            false
1768        } else {
1769            uv[(n - 1) as usize] = val.clone();
1770            drop(uv);
1771            state.gc().barrier_back(ud, &val);
1772            true
1773        }
1774    } else {
1775        false
1776    };
1777    state.pop();
1778    Ok(res)
1779}
1780
1781// ── load/call functions ───────────────────────────────────────────────────────
1782
1783//                            lua_KContext ctx, lua_KFunction k)
1784pub fn call_k(
1785    state: &mut LuaState,
1786    nargs: i32,
1787    nresults: i32,
1788    ctx: isize,
1789    k: Option<fn(&mut LuaState, i32, isize) -> Result<usize, LuaError>>,
1790) -> Result<(), LuaError> {
1791    let top = state.top_idx();
1792    let func_idx = top - (nargs + 1);
1793    //      L->ci->u.c.k = k; L->ci->u.c.ctx = ctx;
1794    //      luaD_call(L, func, nresults);
1795    //    } else {
1796    //      luaD_callnoyield(L, func, nresults);
1797    //    }
1798    if k.is_some() && state.is_yieldable() {
1799        let ci_idx = state.ci;
1800        {
1801            let ci = state.get_ci_mut(ci_idx);
1802            ci.set_u_c_k(k);
1803            ci.set_u_c_ctx(ctx);
1804        }
1805        state.call_at(func_idx, nresults)?;
1806    } else {
1807        state.call_no_yield(func_idx, nresults)?;
1808    }
1809    state.adjust_results(nresults);
1810    Ok(())
1811}
1812
1813//                            lua_KContext ctx, lua_KFunction k)
1814pub fn pcall_k(
1815    state: &mut LuaState,
1816    nargs: i32,
1817    nresults: i32,
1818    errfunc: i32,
1819    ctx: isize,
1820    k: Option<fn(&mut LuaState, i32, isize) -> Result<usize, LuaError>>,
1821) -> Result<LuaStatus, LuaError> {
1822    // Phase D-1c: activate the heap for the duration of this protected call.
1823    // GcRef::new (post D-1e) and any future allocator-aware code will route
1824    // through state.global.heap via with_current_heap(...). Stacked so nested
1825    // pcalls inside the same thread don't clobber each other.
1826    let _heap_guard = {
1827        let g = state.global.borrow();
1828        // The HeapGuard borrows &Heap; we let it live for the function scope.
1829        // The borrow of `g` is dropped immediately; the guard's NonNull
1830        // outlives it (the heap field is pinned inside GlobalState which
1831        // is Rc-managed and won't move).
1832        lua_gc::HeapGuard::push(&g.heap)
1833    };
1834    let err_handler_idx: isize = if errfunc == 0 {
1835        0
1836    } else {
1837        let o = index_to_stack_idx(state, errfunc);
1838        debug_assert!(
1839            matches!(state.get_at(o), LuaValue::Function(_)),
1840            "error handler must be a function"
1841        );
1842        o.0 as isize
1843    };
1844    let top = state.top_idx();
1845    let func_idx = top - (nargs + 1);
1846    if k.is_none() || !state.is_yieldable() {
1847        state.protected_call_raw(func_idx, nresults, StackIdx(err_handler_idx as u32))?;
1848        state.adjust_results(nresults);
1849        return Ok(LuaStatus::Ok);
1850    }
1851    // Yieldable continuation path: arrange for an interrupted call (yield or
1852    // recoverable error) to be resumable. The call is already protected by
1853    // `lua_resume`; real errors must propagate with CIST_YPCALL still set so
1854    // `precover` can run `finish_pcallk`.
1855    //
1856    let ci_idx = state.ci;
1857    let allow = state.allowhook;
1858    let saved_errfunc = state.errfunc;
1859    {
1860        let ci = state.get_ci_mut(ci_idx);
1861        ci.set_u_c_k(k);
1862        ci.set_u_c_ctx(ctx);
1863        ci.set_u2_funcidx(func_idx.0 as i32);
1864        ci.set_u_c_old_errfunc(saved_errfunc);
1865        ci.set_oah(allow);
1866        ci.callstatus |= crate::state::CIST_YPCALL;
1867    }
1868    state.errfunc = err_handler_idx;
1869    let call_result = crate::do_::call(state, func_idx, nresults);
1870    match call_result {
1871        Ok(()) => {
1872            //    L->errfunc = ci->u.c.old_errfunc;
1873            //    status = LUA_OK;
1874            state.get_ci_mut(ci_idx).callstatus &= !crate::state::CIST_YPCALL;
1875            state.errfunc = saved_errfunc;
1876            state.adjust_results(nresults);
1877            Ok(LuaStatus::Ok)
1878        }
1879        Err(crate::state::LuaError::Yield) => {
1880            // Yield must propagate up to lua_resume. The recovery prep stays
1881            // on `ci_idx` so that on resume, `finishCcall` will call
1882            // `finishpcallk` followed by the continuation `k`.
1883            Err(crate::state::LuaError::Yield)
1884        }
1885        Err(e) => {
1886            // Real errors take the same path as C longjmp: they unwind to
1887            // lua_resume's protected runner, which calls precover and then
1888            // finish_pcallk while this C frame still advertises CIST_YPCALL.
1889            Err(e)
1890        }
1891    }
1892}
1893
1894//                          const char *chunkname, const char *mode)
1895// PORT NOTE: lua_Reader (void* callback) is replaced by Box<dyn FnMut>; mode
1896// is &[u8].
1897pub fn load(
1898    state: &mut LuaState,
1899    reader: Box<dyn FnMut() -> Option<Vec<u8>>>,
1900    chunkname: Option<&[u8]>,
1901    mode: Option<&[u8]>,
1902) -> Result<LuaStatus, LuaError> {
1903    let name = chunkname.unwrap_or(b"?");
1904    let z = crate::zio::ZIO::new(reader);
1905    let status = state.protected_parser(z, name, mode);
1906    if status == LuaStatus::Ok {
1907        let top = state.top_idx();
1908        let func_val = state.get_at(top - 1);
1909        if let LuaValue::Function(LuaClosure::Lua(lcl)) = func_val {
1910            if !lcl.upvals.is_empty() {
1911                let gt = get_global_table(state);
1912                let uv = state.new_upval_closed(gt);
1913                lcl.set_upval(0, uv);
1914                state.gc().obj_barrier(&lcl, &uv);
1915            }
1916        }
1917    }
1918    Ok(status)
1919}
1920
1921pub fn dump(
1922    state: &LuaState,
1923    writer: &mut dyn FnMut(&[u8]) -> Result<(), LuaError>,
1924    strip: bool,
1925) -> Result<bool, LuaError> {
1926    let top = state.top_idx();
1927    let o = state.get_at(top - 1);
1928    if let LuaValue::Function(LuaClosure::Lua(ref lcl)) = o {
1929        crate::dump::dump(state, &lcl.proto, writer, strip)?;
1930        Ok(true)
1931    } else {
1932        Ok(false)
1933    }
1934}
1935
1936pub fn status(state: &LuaState) -> LuaStatus {
1937    LuaStatus::from_raw(state.status as i32)
1938}
1939
1940// ── garbage collection ────────────────────────────────────────────────────────
1941
1942/// GC operation codes (C: LUA_GC* constants)
1943#[repr(i32)]
1944#[derive(Debug, Clone, Copy, PartialEq, Eq)]
1945pub enum GcWhat {
1946    Stop = 0,
1947    Restart = 1,
1948    Collect = 2,
1949    Count = 3,
1950    CountB = 4,
1951    Step = 5,
1952    SetPause = 6,
1953    SetStepMul = 7,
1954    IsRunning = 9,
1955    Gen = 10,
1956    Inc = 11,
1957}
1958
1959// PORT NOTE: C varargs replaced by explicit GcArgs enum; callers supply parameters directly.
1960pub enum GcArgs {
1961    Stop,
1962    Restart,
1963    Collect,
1964    Count,
1965    CountB,
1966    Step { data: i32 },
1967    SetPause { value: i32 },
1968    SetStepMul { value: i32 },
1969    IsRunning,
1970    Gen { minormul: i32, majormul: i32 },
1971    Inc { pause: i32, stepmul: i32, stepsize: i32 },
1972    /// Lua 5.5 `collectgarbage("param", name [, value])`. `param` is the
1973    /// 0-based param index; `value < 0` means "read only".
1974    Param { param: usize, value: i64 },
1975}
1976
1977pub fn gc(state: &mut LuaState, args: GcArgs) -> i32 {
1978    // Lua 5.5 `collectgarbage("param", ...)` reads/writes a param and is not
1979    // gated by the finalizer (gc-stopped-internally) guard. Handled first so a
1980    // param read is never clobbered by the -1 sentinel.
1981    if let GcArgs::Param { param, value } = &args {
1982        return state.global_mut().gc55_param(*param, *value) as i32;
1983    }
1984    if state.global().is_gc_stopped_internally() {
1985        return -1;
1986    }
1987    match args {
1988        GcArgs::Stop => {
1989            state.global_mut().set_gc_stop_user();
1990        }
1991        GcArgs::Restart => {
1992            {
1993                let mut g = state.global_mut();
1994                crate::state::set_debt(&mut *g, 0);
1995            }
1996            state.global_mut().clear_gc_stop();
1997        }
1998        GcArgs::Collect => {
1999            if !state.allowhook {
2000                return 0;
2001            }
2002            // Under D-2, weak-table sweep happens INSIDE the heap's
2003            // post-mark hook (see GcHandle::full_collect), driven by
2004            // reachability rather than strong_count. The standalone weak
2005            // sweep that used to run here would now be a no-op against an
2006            // already-clean state and is removed.
2007            state.gc().full_collect();
2008            // Phase-B: drain pending __gc finalizers for tables whose user
2009            // refs have all been dropped. Kept for legacy compat; runs
2010            // after the heap's collect so weak entries have been cleared.
2011            // This is C-Lua's explicit-collect path (`runafewfinalizers` with
2012            // `propagateerrors = 1`): on 5.2/5.3 a finalizer error is wrapped
2013            // and re-thrown. `gc()` returns `i32`, so park the error in the
2014            // global for the `collectgarbage` built-in to re-raise.
2015            if let Err(e) = run_pending_finalizers_inner(state, true) {
2016                state.global_mut().gc_finalizer_error = Some(e.into_value());
2017            }
2018        }
2019        GcArgs::Count => {
2020            let g = state.global();
2021            let total = g.heap.bytes_used();
2022            return (total >> 10) as i32;
2023        }
2024        GcArgs::CountB => {
2025            let g = state.global();
2026            let total = g.heap.bytes_used();
2027            return (total & 0x3ff) as i32;
2028        }
2029        GcArgs::Step { data } => {
2030            let old_stp = {
2031                let mut g = state.global_mut();
2032                let old = g.gc_stop_flags();
2033                g.clear_gc_stop();
2034                old
2035            };
2036            // C-Lua converts `data` KiB of added debt into work units via
2037            // `stepmul`. We use a simpler mapping: the work-unit count is
2038            // `data * stepmul` after decoding the packed GC parameter, with a
2039            // floor of 1 unit. When
2040            // `data == 0` the call still performs one basic step (matching
2041            // C-Lua's `luaC_step(L)` after `setdebt(g, 0)`).
2042            let stepmul = (state.global().gc_stepmul_param() as isize | 1).max(1);
2043            let work_units = if data == 0 {
2044                stepmul
2045            } else {
2046                let raw = (data as isize).saturating_mul(stepmul);
2047                raw.max(1)
2048            };
2049            let debt_for_result = if data == 0 {
2050                let mut g = state.global_mut();
2051                crate::state::set_debt(&mut *g, 0);
2052                0
2053            } else {
2054                let debt = data as isize * 1024 + state.global().gc_debt();
2055                let mut g = state.global_mut();
2056                crate::state::set_debt(&mut *g, debt);
2057                debt
2058            };
2059            let gen_mode = state.global().is_gen_mode();
2060            let cycle_complete = if gen_mode {
2061                if data == 0 {
2062                    state.gc().generational_step_minor_only();
2063                } else {
2064                    state.gc().generational_step();
2065                }
2066                if state.global().finalizers.has_to_be_finalized() {
2067                    if let Err(e) = run_pending_finalizers_inner(state, true) {
2068                        state.global_mut().gc_finalizer_error = Some(e.into_value());
2069                    }
2070                }
2071                debt_for_result > 0 && state.global().gc_at_pause()
2072            } else if state.global().heap.gc_state() == lua_gc::GcState::CallFin
2073                && state.global().finalizers.has_to_be_finalized()
2074            {
2075                if let Err(e) = run_some_pending_finalizers_inner(state, true) {
2076                    state.global_mut().gc_finalizer_error = Some(e.into_value());
2077                }
2078                if state.global().finalizers.has_to_be_finalized() {
2079                    false
2080                } else {
2081                    state.global().heap.finish_callfin_phase()
2082                }
2083            } else {
2084                let completed = state.gc().incremental_step(work_units);
2085                if state.global().heap.gc_state() == lua_gc::GcState::CallFin
2086                    && state.global().finalizers.has_to_be_finalized()
2087                {
2088                    if let Err(e) = run_some_pending_finalizers_inner(state, true) {
2089                        state.global_mut().gc_finalizer_error = Some(e.into_value());
2090                    }
2091                    if state.global().finalizers.has_to_be_finalized() {
2092                        false
2093                    } else {
2094                        state.global().heap.finish_callfin_phase()
2095                    }
2096                } else {
2097                    completed
2098                }
2099            };
2100            state.global_mut().set_gc_stop_flags(old_stp);
2101            // Sync the global gcstate byte for `gc_at_pause()` callers.
2102            {
2103                let heap_state = state.global().heap.gc_state();
2104                let mut g = state.global_mut();
2105                g.gcstate = if heap_state.is_pause() { 0 } else { 1 };
2106            }
2107            return if cycle_complete { 1 } else { 0 };
2108        }
2109        GcArgs::SetPause { value } => {
2110            let old = state.global().gc_pause_param();
2111            state.global_mut().set_gc_pause_param(value);
2112            return old;
2113        }
2114        GcArgs::SetStepMul { value } => {
2115            let old = state.global().gc_stepmul_param();
2116            state.global_mut().set_gc_stepmul_param(value);
2117            return old;
2118        }
2119        GcArgs::IsRunning => {
2120            return state.global().gc_running() as i32;
2121        }
2122        GcArgs::Gen { minormul, majormul } => {
2123            let old_mode = if state.global().is_gen_mode() { 10i32 } else { 11i32 };
2124            if minormul != 0 {
2125                state.global_mut().genminormul = minormul as u8;
2126            }
2127            if majormul != 0 {
2128                state.global_mut().set_gc_genmajormul(majormul);
2129            }
2130            state.gc().change_mode(crate::state::GcKind::Generational);
2131            return old_mode;
2132        }
2133        GcArgs::Inc { pause, stepmul, stepsize } => {
2134            let old_mode = if state.global().is_gen_mode() { 10i32 } else { 11i32 };
2135            if pause != 0 {
2136                state.global_mut().set_gc_pause_param(pause);
2137            }
2138            if stepmul != 0 {
2139                state.global_mut().set_gc_stepmul_param(stepmul);
2140            }
2141            if stepsize != 0 {
2142                state.global_mut().gcstepsize = stepsize as u8;
2143            }
2144            state.gc().change_mode(crate::state::GcKind::Incremental);
2145            return old_mode;
2146        }
2147        GcArgs::Param { .. } => unreachable!("Param handled before the finalizer guard"),
2148    }
2149    0
2150}
2151
2152/// Configure the hosted-runtime GC mode after the standard libraries are open.
2153///
2154/// Upstream 5.4/5.5 initialize a fresh raw state in incremental mode, then the
2155/// standalone/runtime startup path restarts the collector and enters
2156/// generational mode. Keep `new_state()` semantics raw-state faithful and apply
2157/// the observable hosted default at this layer.
2158pub fn configure_startup_gc_mode(state: &mut LuaState) {
2159    if matches!(
2160        state.global().lua_version,
2161        lua_types::LuaVersion::V54 | lua_types::LuaVersion::V55
2162    ) {
2163        let _ = gc(state, GcArgs::Restart);
2164        let _ = gc(
2165            state,
2166            GcArgs::Gen {
2167                minormul: 0,
2168                majormul: 0,
2169            },
2170        );
2171    }
2172}
2173
2174// ── miscellaneous functions ───────────────────────────────────────────────────
2175
2176// PORT NOTE: returns Result<Infallible, _> — semantically "always Err". The
2177// translator originally wrote `Result<!, _>` but the `!` type in a return
2178// position is still nightly-only as of Rust 1.93; Infallible is the stable
2179// stand-in. Callsites just pattern-match on Err.
2180pub fn lua_error(state: &mut LuaState) -> Result<Infallible, LuaError> {
2181    //      luaM_error(L);  /* memory error */
2182    //    else
2183    //      luaG_errormsg(L);  /* regular error */
2184    let top = state.top_idx();
2185    let errobj = state.get_at(top - 1);
2186    let is_mem_err = if let LuaValue::Str(ref s) = errobj {
2187        let memerr = state.global().memerrmsg.clone();
2188        GcRef::ptr_eq(s, &memerr)
2189    } else {
2190        false
2191    };
2192    if is_mem_err {
2193        Err(LuaError::Memory)
2194    } else {
2195        Err(LuaError::from_value(errobj))
2196    }
2197}
2198
2199pub fn next(state: &mut LuaState, idx: i32) -> Result<bool, LuaError> {
2200    let t = get_table_value(state, idx)
2201        .ok_or_else(|| LuaError::runtime(format_args!("table expected")))?;
2202    let top = state.top_idx();
2203    let key = state.get_at(top - 1);
2204    match t.next(key)? {
2205        Some((next_key, next_val)) => {
2206            state.set_at(top - 1, next_key);
2207            state.push(next_val);
2208            Ok(true)
2209        }
2210        None => {
2211            state.set_top_idx(top - 1);
2212            Ok(false)
2213        }
2214    }
2215}
2216
2217pub fn to_close(state: &mut LuaState, idx: i32) -> Result<(), LuaError> {
2218    let _level = index_to_stack_idx(state, idx);
2219    // TODO(port): luaF_newtbcupval and to-be-closed variable infrastructure
2220    // not yet translated. Stubbing for Phase A.
2221    Ok(())
2222}
2223
2224pub fn concat(state: &mut LuaState, n: i32) -> Result<(), LuaError> {
2225    if n > 0 {
2226        state.concat(n)?;
2227    } else {
2228        let empty = state.intern_str(b"")?;
2229        state.push(LuaValue::Str(empty));
2230    }
2231    state.gc().check_step();
2232    Ok(())
2233}
2234
2235pub fn len(state: &mut LuaState, idx: i32) -> Result<(), LuaError> {
2236    let t = index_to_value(state, idx);
2237    let result = state.obj_len(&t)?;
2238    state.push(result);
2239    Ok(())
2240}
2241
2242// PORT NOTE: The custom allocator hook is not exposed in the Rust-native API.
2243// Rust's allocator handles all allocation.
2244// These are intentionally omitted.
2245
2246pub fn set_warn_f(
2247    state: &mut LuaState,
2248    f: Option<Box<dyn FnMut(&[u8], bool)>>,
2249) {
2250    // PORT NOTE: ud_warn userdata is folded into the closure per types.tsv.
2251    state.global_mut().warnf = f;
2252}
2253
2254pub fn warning(state: &mut LuaState, msg: &[u8], tocont: bool) {
2255    state.emit_warning(msg, tocont);
2256}
2257
2258pub fn new_userdata_uv(
2259    state: &mut LuaState,
2260    size: usize,
2261    nuvalue: i32,
2262) -> Result<GcRef<LuaUserData>, LuaError> {
2263    debug_assert!(nuvalue >= 0 && nuvalue < u16::MAX as i32, "invalid value");
2264    let u = state.new_userdata(size, nuvalue as usize)?;
2265    state.push(LuaValue::UserData(u.clone()));
2266    state.gc().check_step();
2267    Ok(u)
2268}
2269
2270// ── upvalue access ────────────────────────────────────────────────────────────
2271
2272// PORT NOTE: Returns (name, value) instead of mutating output pointers. The name
2273// is returned as an owned Vec<u8> because Lua upvalue names live in the proto's
2274// LuaString table (GC heap), not in static storage.
2275fn aux_upvalue(
2276    state: &LuaState,
2277    fi: &LuaValue,
2278    n: i32,
2279) -> Option<(Vec<u8>, LuaValue)> {
2280    match fi {
2281        LuaValue::Function(LuaClosure::C(ccl)) => {
2282            let upvalues = ccl.upvalues.borrow();
2283            let nupvalues = upvalues.len() as i32;
2284            if n < 1 || n > nupvalues {
2285                return None;
2286            }
2287            Some((Vec::new(), upvalues[(n - 1) as usize].clone()))
2288        }
2289        LuaValue::Function(LuaClosure::Lua(lcl)) => {
2290            let nupvalues = lcl.upvals.len() as i32;
2291            if n < 1 || n > nupvalues {
2292                return None;
2293            }
2294            let val = state.upvalue_get(lcl, (n - 1) as usize);
2295            // The proto records the static name of each upvalue (e.g. "_ENV"
2296            // for the main chunk's environment upvalue). Stripped chunks have
2297            // no upvalue-name debug info; Lua reports those as "(no name)".
2298            let name: Vec<u8> = lcl
2299                .proto
2300                .upvalues
2301                .get((n - 1) as usize)
2302                .and_then(|ud| ud.name.as_ref())
2303                .map(|s| s.as_bytes().to_vec())
2304                .unwrap_or_else(|| b"(no name)".to_vec());
2305            Some((name, val))
2306        }
2307        _ => None,
2308    }
2309}
2310
2311pub fn get_upvalue(state: &mut LuaState, funcindex: i32, n: i32) -> Option<Vec<u8>> {
2312    let fi = index_to_value(state, funcindex);
2313    if let Some((name, val)) = aux_upvalue(state, &fi, n) {
2314        state.push(val);
2315        Some(name)
2316    } else {
2317        None
2318    }
2319}
2320
2321pub fn setup_value(state: &mut LuaState, funcindex: i32, n: i32) -> Option<Vec<u8>> {
2322    let fi = index_to_value(state, funcindex);
2323    let (name, _) = aux_upvalue(state, &fi, n)?;
2324    let new_val = state.pop();
2325    match &fi {
2326        LuaValue::Function(LuaClosure::Lua(lcl)) => {
2327            state.upvalue_set(lcl, (n - 1) as usize, new_val).ok()?;
2328        }
2329        LuaValue::Function(LuaClosure::C(ccl)) => {
2330            let idx = (n - 1) as usize;
2331            {
2332                let mut upvalues = ccl.upvalues.borrow_mut();
2333                if idx >= upvalues.len() {
2334                    return None;
2335                }
2336                upvalues[idx] = new_val.clone();
2337            }
2338            state.gc().barrier(ccl, &new_val);
2339        }
2340        _ => return None,
2341    }
2342    Some(name)
2343}
2344
2345// PORT NOTE: returns an index into the upvals vec rather than a pointer-to-pointer.
2346// Returns None if n is out of range.
2347fn get_upval_ref_idx(state: &LuaState, fidx: i32, n: i32) -> Option<usize> {
2348    let fi = index_to_value(state, fidx);
2349    debug_assert!(matches!(fi, LuaValue::Function(LuaClosure::Lua(_))), "Lua function expected");
2350    if let LuaValue::Function(LuaClosure::Lua(ref lcl)) = fi {
2351        let sizeupvalues = lcl.upvals.len() as i32;
2352        if n >= 1 && n <= sizeupvalues {
2353            Some((n - 1) as usize)
2354        } else {
2355            None
2356        }
2357    } else {
2358        None
2359    }
2360}
2361
2362// PORT NOTE: Returns Option<usize> identity instead of raw void*.
2363pub fn upvalue_id(state: &LuaState, fidx: i32, n: i32) -> Option<usize> {
2364    let fi = index_to_value(state, fidx);
2365    match &fi {
2366        LuaValue::Function(LuaClosure::Lua(lcl)) => {
2367            let idx = get_upval_ref_idx(state, fidx, n)?;
2368            // Return the identity of the UpVal GcRef
2369            Some(GcRef::identity(&lcl.upval(idx)))
2370        }
2371        LuaValue::Function(LuaClosure::C(ccl)) => {
2372            let upvalues = ccl.upvalues.borrow();
2373            if n >= 1 && n <= upvalues.len() as i32 {
2374                // TODO(port): returning address of upvalue slot not possible without raw ptr.
2375                // Return a synthetic identity based on the closure's identity + n.
2376                Some(GcRef::identity(ccl) ^ (n as usize))
2377            } else {
2378                None
2379            }
2380        }
2381        LuaValue::Function(LuaClosure::LightC(_)) => None,
2382        _ => {
2383            debug_assert!(false, "function expected");
2384            None
2385        }
2386    }
2387}
2388
2389//                                               int fidx2, int n2)
2390pub fn upvalue_join(state: &mut LuaState, fidx1: i32, n1: i32, fidx2: i32, n2: i32) {
2391    let idx1 = match get_upval_ref_idx(state, fidx1, n1) {
2392        Some(i) => i,
2393        None => return,
2394    };
2395    let idx2 = match get_upval_ref_idx(state, fidx2, n2) {
2396        Some(i) => i,
2397        None => return,
2398    };
2399    let f1 = index_to_value(state, fidx1);
2400    let f2 = index_to_value(state, fidx2);
2401    if let (
2402        LuaValue::Function(LuaClosure::Lua(lcl1)),
2403        LuaValue::Function(LuaClosure::Lua(lcl2)),
2404    ) = (&f1, &f2)
2405    {
2406        let shared = lcl2.upval(idx2);
2407        lcl1.set_upval(idx1, shared);
2408        state.gc().obj_barrier(lcl1, &shared);
2409    }
2410}
2411
2412// ──────────────────────────────────────────────────────────────────────────
2413// PORT STATUS
2414//   source:        src/lapi.c  (1464 lines, ~47 functions)
2415//   target_crate:  lua-vm
2416//   confidence:    low
2417//   todos:         18
2418//   port_notes:    8
2419//   unsafe_blocks: 0   (must be 0 outside explicit unsafe-budget crates)
2420//   notes:         Heavy use of interior mutability TODOs (GcRef writes for
2421//                  metatables, upvalue writes, userdata uv writes). The
2422//                  index2value helper returns cloned LuaValue not a pointer,
2423//                  so write-back paths that C achieves with TValue* are
2424//                  stubbed. Stack pointer arithmetic faithfully translated to
2425//                  StackIdx (u32) arithmetic. va_list functions (pushvfstring,
2426//                  pushfstring) replaced by &[u8] forwarders. lua_gc varargs
2427//                  replaced by explicit GcArgs enum. Raw pointer returns
2428//                  (topointer, touserdata, upvalueid) return Option<usize>
2429//                  identity values; actual *mut void only legal in lua-gc.
2430//                  lua_pushthread stubbed (needs self_gcref()), lua_xmove
2431//                  stubbed (split-borrow), upvalue_join stubbed (GcRef write).
2432//                  Phase B must wire up: state.grow_stack, state.call_no_yield,
2433//                  state.protected_call_raw, state.adjust_results,
2434//                  state.table_get_with_tm, state.table_set_with_tm,
2435//                  state.arith_op, state.concat, state.obj_len,
2436//                  state.obj_to_string, state.str_to_num, state.table_getn,
2437//                  state.registry_value, state.registry_get,
2438//                  GcRef::identity, GcRef::ptr_eq, GlobalState GC accessors.
2439// ──────────────────────────────────────────────────────────────────────────