Skip to main content

lua_stdlib/
auxlib.rs

1//! Auxiliary library: helper functions for building Lua libraries.
2//!
3//! C source: `reference/lua-5.4.7/src/lauxlib.c` (1127 lines, ~50 functions)
4//! Target crate: `lua-stdlib`
5//!
6//! This module provides the high-level `luaL_*` API layer that sits on top of
7//! the raw `lua_*` C API. In Rust we translate each `luaL_*` function as a
8//! free function receiving `&mut LuaState` rather than a method, matching the
9//! structure of the other stdlib modules.
10//!
11//! PORT NOTE: The C buffer system (`luaL_Buffer`) uses a small inline initial
12//! buffer backed by a Lua-stack userdata box on overflow. In Rust we replace
13//! this with a plain `Vec<u8>` (`LuaBuffer`), dropping all the C-internal
14//! `UBox` / `resizebox` / `boxgc` / `boxmt` / `newbox` / `buffonstack`
15//! machinery. The public interface remains compatible.
16//!
17//! PORT NOTE: File-loading functions (`load_filex`) use the embedder-installed
18//! `GlobalState::file_loader_hook`; concrete filesystem access belongs in
19//! `lua-cli` or another host backend.
20
21use lua_types::{
22    error::LuaError,
23    value::LuaValue,
24    gc::GcRef,
25    string::LuaString,
26    userdata::LuaUserData,
27    LuaType,
28    LuaStatus,
29};
30use crate::state_stub::{LuaState, LuaStateStubExt as _, LuaDebug};
31
32// ── Constants ─────────────────────────────────────────────────────────────────
33
34/// Number of stack frames to show in the first part of a traceback.
35const LEVELS1: i32 = 10;
36
37/// Number of stack frames to show in the second part of a traceback.
38const LEVELS2: i32 = 11;
39
40/// Index (1-based) in the reference table that heads the free-list of recycled
41/// references. Placed after the last predefined registry key.
42const FREELIST_REF: i64 = 3; // LUA_RIDX_GLOBALS (2) + 1
43
44/// Pseudo-reference returned by `lua_ref` when the pushed value was `nil`.
45pub const LUA_REFNIL: i32 = -1;
46
47/// Pseudo-reference meaning "no reference" (never created by `lua_ref`).
48pub const LUA_NOREF: i32 = -2;
49
50/// Extended error code: file-related I/O error from `load_filex`.
51pub const LUA_ERRFILE: i32 = 6;
52
53/// Registry key for the table of loaded modules.
54pub const LUA_LOADED_TABLE: &[u8] = b"_LOADED";
55
56/// Registry key for the table of preloaded loaders.
57pub const LUA_PRELOAD_TABLE: &[u8] = b"_PRELOAD";
58
59/// Name of the global environment table.
60pub const LUA_GNAME: &[u8] = b"_G";
61
62/// Metatable name / file-handle key for the IO library.
63pub const LUA_FILE_HANDLE: &[u8] = b"FILE*";
64
65/// Pseudo-index for the Lua registry.
66const LUA_REGISTRYINDEX: i32 = -1_001_000;
67
68/// Minimum number of extra stack slots `lua_checkstack` guarantees per call.
69#[expect(dead_code, reason = "ported stdlib helper; not yet wired into the runtime")]
70const LUA_MINSTACK: i32 = 20;
71
72// ── Public types ──────────────────────────────────────────────────────────────
73
74/// A function-registration entry for `set_funcs`.
75///
76///
77/// In Rust, `name` is `&'static [u8]` (never `&str`). A `None` func is a
78/// placeholder that pushes `false` rather than a closure.
79pub struct LuaReg {
80    pub name: &'static [u8],
81    pub func: Option<fn(&mut LuaState) -> Result<usize, LuaError>>,
82}
83
84/// Growable byte-buffer used by the auxiliary library for building strings.
85///
86///
87/// The C version uses a small inline initial buffer with overflow managed via
88/// a Lua-stack userdata box. The Rust port collapses this to a plain `Vec<u8>`.
89/// All buffer mutating functions take `&mut LuaState` as a separate parameter.
90pub struct LuaBuffer {
91    pub data: Vec<u8>,
92}
93
94/// File-stream handle used by the IO library.
95///
96///
97/// `closef` in C is a `lua_CFunction`. In Rust we store an optional closer.
98// TODO(port): file I/O belongs in lua-stdlib/src/io_lib.rs; this definition
99// may move there. Keeping here to mirror the C header.
100pub struct LuaStream {
101    /// The underlying file handle. `None` for incompletely opened or closed streams.
102    // TODO(port): this legacy auxlib stream placeholder should converge with the
103    // host-provided LuaFileHandle abstraction used by io_lib.
104    pub f: Option<Box<dyn std::io::Read>>,
105    /// Optional close function (None for already-closed streams).
106    pub closef: Option<fn(&mut LuaState) -> Result<usize, LuaError>>,
107}
108
109// ── Traceback ─────────────────────────────────────────────────────────────────
110
111/// Search for `objidx` in the table at the top of the stack.
112/// `objidx` must be an absolute API stack index.
113/// Returns `true` (and leaves name string on top) when found.
114///
115fn find_field(
116    state: &mut LuaState,
117    objidx: i32,
118    level: i32,
119) -> Result<bool, LuaError> {
120    if level == 0 || state.type_at(-1) != LuaType::Table {
121        return Ok(false);
122    }
123    state.push(LuaValue::Nil);
124    while state.table_next(-2)? {
125        if state.type_at(-2) == LuaType::String {
126            if state.raw_equal(objidx, -1)? {
127                state.pop_n(1); // remove value (keep name)
128                return Ok(true);
129            } else if find_field(state, objidx, level - 1)? {
130                // stack: lib_name, lib_table, field_name (top)
131                state.push_string(b".")?; // place '.' between the two names
132                state.replace(-3)?; // in the slot occupied by table
133                state.concat(3)?; // lib_name.field_name
134                return Ok(true);
135            }
136        }
137        state.pop_n(1); // remove value
138    }
139    Ok(false)
140}
141
142/// Search all loaded modules for a global name for the function at `top+1`.
143/// Returns `true` and leaves name string on top (at `top+1`) if found.
144///
145fn push_global_func_name(
146    state: &mut LuaState,
147    ar: &mut LuaDebug,
148) -> Result<bool, LuaError> {
149    let top = state.top_count();
150    state.get_info(b"f", ar)?;
151    state.get_field(LUA_REGISTRYINDEX, LUA_LOADED_TABLE)?;
152    check_stack(state, 6, Some(b"not enough stack"))?;
153    if find_field(state, top + 1, 2)? {
154        if state.peek_bytes(-1).map_or(false, |n| n.starts_with(b"_G.")) {
155            let suffix = state.peek_bytes(-1)
156                .map(|n| n[3..].to_vec())
157                .unwrap_or_default();
158            state.push_bytes(&suffix)?;
159            state.remove(-2)?;
160        }
161        state.copy_value(-1, top + 1)?;
162        lua_vm::api::set_top(state, top + 1)?;
163        Ok(true)
164    } else {
165        lua_vm::api::set_top(state, top)?;
166        Ok(false)
167    }
168}
169
170fn push_global_func_name_from_target(
171    state: &mut LuaState,
172    target: &mut LuaState,
173    ar: &mut LuaDebug,
174) -> Result<bool, LuaError> {
175    let top = state.top_count();
176    target.get_info(b"f", ar)?;
177    let func = target.get_at(target.top_idx() - 1);
178    target.pop_n(1);
179    state.push(func);
180    state.get_field(LUA_REGISTRYINDEX, LUA_LOADED_TABLE)?;
181    check_stack(state, 6, Some(b"not enough stack"))?;
182    if find_field(state, top + 1, 2)? {
183        if state.peek_bytes(-1).map_or(false, |n| n.starts_with(b"_G.")) {
184            let suffix = state.peek_bytes(-1)
185                .map(|n| n[3..].to_vec())
186                .unwrap_or_default();
187            state.push_bytes(&suffix)?;
188            state.remove(-2)?;
189        }
190        state.copy_value(-1, top + 1)?;
191        lua_vm::api::set_top(state, top + 1)?;
192        Ok(true)
193    } else {
194        lua_vm::api::set_top(state, top)?;
195        Ok(false)
196    }
197}
198
199/// Push a human-readable name for the function described by `ar`.
200///
201fn push_func_name(
202    state: &mut LuaState,
203    ar: &mut LuaDebug,
204    global_lookup_target: Option<&mut LuaState>,
205) -> Result<(), LuaError> {
206    let found_global = match global_lookup_target {
207        Some(target) => push_global_func_name_from_target(state, target, ar)?,
208        None => push_global_func_name(state, ar)?,
209    };
210    if found_global {
211        let name = state.peek_bytes(-1).unwrap_or_else(|| b"?".to_vec());
212        state.push_fstring(format_args!("function '{}'", BStr(&name)))?;
213        state.remove(-2)?;
214    } else if !ar.namewhat.is_empty() {
215        let namewhat = ar.namewhat.clone();
216        let name = ar.name.clone().unwrap_or_else(|| b"?".to_vec());
217        state.push_fstring(format_args!("{} '{}'", BStr(&namewhat), BStr(&name)))?;
218    } else if ar.what == b'm' {
219        state.push_string(b"main chunk")?;
220    } else if ar.what != b'C' {
221        let src = ar.short_src.clone();
222        let line = ar.linedefined;
223        state.push_fstring(format_args!("function <{}:{}>", BStr(&src), line))?;
224    } else {
225        state.push_string(b"?")?;
226    }
227    Ok(())
228}
229
230/// Binary-search for the last valid stack level in `state`.
231///
232fn last_level(state: &mut LuaState) -> i32 {
233    let mut ar = LuaDebug::default();
234    let mut li: i32 = 1;
235    let mut le: i32 = 1;
236    while state.get_stack(le, &mut ar) {
237        li = le;
238        le *= 2;
239    }
240    // binary search
241    while li < le {
242        let m = (li + le) / 2;
243        if state.get_stack(m, &mut ar) {
244            li = m + 1;
245        } else {
246            le = m;
247        }
248    }
249    le - 1
250}
251
252/// Build a stack traceback string from thread `other` starting at `level`.
253/// If `msg` is non-None it is prepended on its own line.
254/// Leaves the result string on top of `state`.
255///
256/// When `other` is `None`, the traceback is built for `state` itself (the
257/// common single-thread case). Rust's borrow checker forbids passing the same
258/// `&mut LuaState` twice, so we use an `Option` to express the aliasing intent
259/// rather than a separate parameter.
260///
261pub fn traceback(
262    state: &mut LuaState,
263    mut other: Option<&mut LuaState>,
264    msg: Option<&[u8]>,
265    level: i32,
266) -> Result<(), LuaError> {
267    let mut b = LuaBuffer::new();
268    let mut ar = LuaDebug::default();
269    let last = match &mut other {
270        Some(o) => last_level(o),
271        None => last_level(state),
272    };
273    let mut limit2show: i32 = if last - level > LEVELS1 + LEVELS2 { LEVELS1 } else { -1 };
274    buf_init(state, &mut b);
275    if let Some(m) = msg {
276        add_lstring(&mut b, m);
277        add_char(&mut b, b'\n');
278    }
279    add_lstring(&mut b, b"stack traceback:");
280    let mut level = level;
281    loop {
282        let got = match &mut other {
283            Some(o) => o.get_stack(level, &mut ar),
284            None => state.get_stack(level, &mut ar),
285        };
286        if !got {
287            break;
288        }
289        level += 1;
290        if limit2show == 0 {
291            let n = last - level - LEVELS2 + 1;
292            state.push_fstring(format_args!("\n\t...\t(skipping {} levels)", n))?;
293            add_value(state, &mut b)?;
294            level += n;
295            limit2show = LEVELS2;
296        } else {
297            limit2show -= 1;
298            match &mut other {
299                Some(o) => o.get_info(b"Slnt", &mut ar)?,
300                None => state.get_info(b"Slnt", &mut ar)?,
301            }
302            if ar.currentline <= 0 {
303                let src = ar.short_src.clone();
304                state.push_fstring(format_args!("\n\t{}: in ", BStr(&src)))?;
305            } else {
306                let src = ar.short_src.clone();
307                let line = ar.currentline;
308                state.push_fstring(format_args!("\n\t{}:{}: in ", BStr(&src), line))?;
309            }
310            add_value(state, &mut b)?;
311            match &mut other {
312                Some(o) => push_func_name(state, &mut ar, Some(&mut **o))?,
313                None => push_func_name(state, &mut ar, None)?,
314            }
315            add_value(state, &mut b)?;
316            if ar.istailcall {
317                add_lstring(&mut b, b"\n\t(...tail calls...)");
318            }
319        }
320    }
321    push_result(state, &mut b)?;
322    Ok(())
323}
324
325// ── Error-report functions ─────────────────────────────────────────────────────
326
327/// Push an error for argument `arg` with extra message `extramsg`.
328/// Attempts to enrich the message with the calling function's name.
329/// Always returns `Err`.
330///
331pub fn arg_error(
332    state: &mut LuaState,
333    mut arg: i32,
334    extramsg: &[u8],
335) -> Result<usize, LuaError> {
336    let mut ar = LuaDebug::default();
337    if !state.get_stack(0, &mut ar) {
338        return Err(LuaError::runtime(format_args!(
339            "bad argument #{} ({})",
340            arg,
341            BStr(extramsg)
342        )));
343    }
344    state.get_info(b"n", &mut ar)?;
345    if ar.namewhat == b"method" {
346        arg -= 1; // do not count 'self'
347        if arg == 0 {
348            let name = ar.name.clone().unwrap_or_else(|| b"?".to_vec());
349            return Err(LuaError::runtime(format_args!(
350                "calling '{}' on bad self ({})",
351                BStr(&name),
352                BStr(extramsg)
353            )));
354        }
355    }
356    let fname = if ar.name.is_none() {
357        if push_global_func_name(state, &mut ar)? {
358            state.peek_bytes(-1).unwrap_or_else(|| b"?".to_vec())
359        } else {
360            b"?".to_vec()
361        }
362    } else {
363        ar.name.clone().unwrap_or_else(|| b"?".to_vec())
364    };
365    Err(LuaError::runtime(format_args!(
366        "bad argument #{} to '{}' ({})",
367        arg,
368        BStr(&fname),
369        BStr(extramsg)
370    )))
371}
372
373/// Push a type-mismatch error for argument `arg`, stating `tname` was expected.
374/// Always returns `Err`.
375///
376pub fn type_error_arg(
377    state: &mut LuaState,
378    arg: i32,
379    tname: &[u8],
380) -> Result<usize, LuaError> {
381    //      typearg = lua_tostring(L, -1);
382    //    else if (lua_type(L, arg) == LUA_TLIGHTUSERDATA)
383    //      typearg = "light userdata";
384    //    else
385    //      typearg = luaL_typename(L, arg);
386    let typearg: Vec<u8> = if get_metafield(state, arg, b"__name")? == LuaType::String {
387        let bytes = state.peek_bytes(-1).unwrap_or_else(|| b"?".to_vec());
388        state.pop_n(1);
389        bytes
390    } else if state.type_at(arg) == LuaType::LightUserData {
391        b"light userdata".to_vec()
392    } else {
393        state.type_name_at(arg).to_vec()
394    };
395    let msg_owned = format!(
396        "{} expected, got {}",
397        BStr(tname),
398        BStr(&typearg)
399    );
400    arg_error(state, arg, msg_owned.as_bytes())
401}
402
403/// Push a type-tag error for `arg`, using the Lua type name for `tag`.
404///
405fn tag_error(state: &mut LuaState, arg: i32, tag: LuaType) -> Result<(), LuaError> {
406    let name = state.type_name(tag);
407    type_error_arg(state, arg, name)?;
408    Ok(())
409}
410
411/// Push a string describing the location of the call at `level` onto the stack.
412/// If no location is available, pushes an empty string.
413///
414pub fn push_where(state: &mut LuaState, level: i32) -> Result<(), LuaError> {
415    let mut ar = LuaDebug::default();
416    if state.get_stack(level, &mut ar) {
417        state.get_info(b"Sl", &mut ar)?;
418        if ar.currentline > 0 {
419            let src = ar.short_src.clone();
420            let line = ar.currentline;
421            state.push_fstring(format_args!("{}:{}: ", BStr(&src), line))?;
422            return Ok(());
423        }
424    }
425    state.push_string(b"")?;
426    Ok(())
427}
428
429/// Format a runtime error with source location and raise it.
430/// Always returns `Err`.
431///
432///
433/// PORT NOTE: C uses varargs + `lua_pushvfstring`. Rust callers pass a
434/// pre-formatted `&[u8]` message; use `format_args!` at the call site.
435pub fn lua_error(state: &mut LuaState, msg: &[u8]) -> Result<usize, LuaError> {
436    push_where(state, 1)?;
437    let where_str = state.pop_bytes();
438    let full = [where_str.as_slice(), msg].concat();
439    Err(LuaError::runtime(format_args!("{}", BStr(&full))))
440}
441
442/// Push the result of a POSIX-style file operation onto the stack.
443/// On success pushes `true`; on failure pushes `nil, errmsg, errno`.
444/// Returns the number of pushed values.
445///
446pub fn file_result(
447    state: &mut LuaState,
448    stat: bool,
449    fname: Option<&[u8]>,
450) -> Result<usize, LuaError> {
451    if stat {
452        state.push(LuaValue::Bool(true));
453        Ok(1)
454    } else {
455        state.push(LuaValue::Nil);
456        // TODO(port): use std::io::Error::last_os_error() for errno-style message.
457        let errmsg = b"(errno unavailable in Rust port)".to_vec();
458        if let Some(name) = fname {
459            let full = [name, b": ".as_slice(), &errmsg].concat();
460            state.push_bytes(&full)?;
461        } else {
462            state.push_bytes(&errmsg)?;
463        }
464        // TODO(port): push actual errno integer once os-error helpers are available.
465        state.push(LuaValue::Int(0));
466        Ok(3)
467    }
468}
469
470/// Push the result of a process-exit status onto the stack.
471/// Returns 3 values: success-bool-or-nil, exit-kind string, status code.
472///
473// TODO(port): POSIX WIFEXITED / WIFSIGNALED inspection requires cfg(unix).
474pub fn exec_result(state: &mut LuaState, stat: i32) -> Result<usize, LuaError> {
475    if stat != 0 {
476        return file_result(state, false, None);
477    }
478    let what = b"exit".as_slice();
479    state.push(LuaValue::Bool(true));
480    state.push_bytes(what)?;
481    state.push(LuaValue::Int(stat as i64));
482    Ok(3)
483}
484
485// ── Userdata / metatable helpers ──────────────────────────────────────────────
486
487/// Create a new metatable for type `tname` and register it in the registry.
488/// Returns `true` (and leaves new metatable on stack) if the table was created;
489/// returns `false` (and leaves existing table on stack) if already existed.
490///
491pub fn new_metatable(state: &mut LuaState, tname: &[u8]) -> Result<bool, LuaError> {
492    if get_metatable(state, tname)? != LuaType::Nil {
493        return Ok(false); // leave previous value on top
494    }
495    state.pop_n(1);
496    state.create_table(0, 2)?;
497    state.push_bytes(tname)?;
498    state.set_field(-2, b"__name")?;
499    state.push_value(-1)?;
500    state.set_field(LUA_REGISTRYINDEX, tname)?;
501    Ok(true)
502}
503
504/// Set the metatable of the value at stack top to the one registered as `tname`.
505///
506pub fn set_metatable(state: &mut LuaState, tname: &[u8]) -> Result<(), LuaError> {
507    get_metatable(state, tname)?;
508    state.set_metatable(-2)?;
509    Ok(())
510}
511
512/// Check whether the value at `ud` is a full userdata with metatable `tname`.
513/// Returns `Some(userdata)` if yes, `None` otherwise.
514///
515pub fn test_udata(
516    state: &mut LuaState,
517    ud: i32,
518    tname: &[u8],
519) -> Result<Option<GcRef<LuaUserData>>, LuaError> {
520    let p = state.to_userdata(ud);
521    if let Some(p) = p {
522        if state.get_metatable(ud)? {
523            get_metatable(state, tname)?;
524            let eq = state.raw_equal(-1, -2)?;
525            state.pop_n(2); // remove both metatables
526            if eq {
527                return Ok(Some(p));
528            }
529        }
530    }
531    Ok(None)
532}
533
534/// Like `test_udata` but raises a type error if the check fails.
535///
536pub fn check_udata(
537    state: &mut LuaState,
538    ud: i32,
539    tname: &[u8],
540) -> Result<GcRef<LuaUserData>, LuaError> {
541    match test_udata(state, ud, tname)? {
542        Some(p) => Ok(p),
543        None => {
544            type_error_arg(state, ud, tname)?;
545            unreachable!()
546        }
547    }
548}
549
550// ── Argument-check functions ──────────────────────────────────────────────────
551
552/// Check that `arg` is one of the strings in `lst` and return its index.
553/// If `def` is `Some` it is used as default when `arg` is absent/nil.
554///
555pub fn check_option(
556    state: &mut LuaState,
557    arg: i32,
558    def: Option<&[u8]>,
559    lst: &[&[u8]],
560) -> Result<usize, LuaError> {
561    let name: Vec<u8> = match def {
562        Some(d) if state.is_none_or_nil(arg) => d.to_vec(),
563        _ => check_lstring(state, arg)?.as_bytes().to_vec(),
564    };
565    for (i, entry) in lst.iter().enumerate() {
566        if *entry == name.as_slice() {
567            return Ok(i);
568        }
569    }
570    Err(LuaError::runtime(format_args!(
571        "invalid option '{}'",
572        BStr(&name)
573    )))
574}
575
576/// Ensure the stack has at least `space` extra slots; raise on failure.
577///
578pub fn check_stack(
579    state: &mut LuaState,
580    space: i32,
581    msg: Option<&[u8]>,
582) -> Result<(), LuaError> {
583    if !state.check_stack_space(space) {
584        match msg {
585            Some(m) => {
586                return Err(LuaError::runtime(format_args!(
587                    "stack overflow ({})",
588                    BStr(m)
589                )));
590            }
591            None => {
592                return Err(LuaError::runtime(format_args!("stack overflow")));
593            }
594        }
595    }
596    Ok(())
597}
598
599/// Assert that the value at `arg` has Lua type `t`; raise type error otherwise.
600///
601pub fn check_type(state: &mut LuaState, arg: i32, t: LuaType) -> Result<(), LuaError> {
602    if state.type_at(arg) != t {
603        tag_error(state, arg, t)?;
604    }
605    Ok(())
606}
607
608/// Assert that a value (not `none`) is present at `arg`.
609///
610pub fn check_any(state: &mut LuaState, arg: i32) -> Result<(), LuaError> {
611    if state.type_at(arg) == LuaType::None {
612        return Err(LuaError::arg_error(arg, "value expected"));
613    }
614    Ok(())
615}
616
617/// Return the string at `arg` as bytes; raise a type error if not a string.
618///
619pub fn check_lstring(state: &mut LuaState, arg: i32) -> Result<GcRef<LuaString>, LuaError> {
620    match state.to_lua_string(arg) {
621        Some(s) => Ok(s),
622        None => {
623            tag_error(state, arg, LuaType::String)?;
624            unreachable!()
625        }
626    }
627}
628
629/// Return the string at `arg`; if absent/nil return `def`.
630///
631pub fn opt_lstring(
632    state: &mut LuaState,
633    arg: i32,
634    def: Option<&[u8]>,
635) -> Result<Option<Vec<u8>>, LuaError> {
636    if state.is_none_or_nil(arg) {
637        return Ok(def.map(|d| d.to_vec()));
638    }
639    let s = check_lstring(state, arg)?;
640    Ok(Some(s.as_bytes().to_vec()))
641}
642
643/// Return the number at `arg` as `f64`; raise a type error if not a number.
644///
645pub fn check_number(state: &mut LuaState, arg: i32) -> Result<f64, LuaError> {
646    match state.to_number_x(arg) {
647        Some(d) => Ok(d),
648        None => {
649            tag_error(state, arg, LuaType::Number)?;
650            unreachable!()
651        }
652    }
653}
654
655/// Return the number at `arg`; if absent/nil return `def`.
656///
657pub fn opt_number(state: &mut LuaState, arg: i32, def: f64) -> Result<f64, LuaError> {
658    if state.is_none_or_nil(arg) {
659        Ok(def)
660    } else {
661        check_number(state, arg)
662    }
663}
664
665/// Raise an error for a non-integer number argument.
666///
667///
668/// Always returns `Err`. The `Ok` arm uses `unreachable!()` to satisfy the
669/// return type; `!` (never) is nightly-only so we use `Result<usize, LuaError>`.
670fn int_error(state: &mut LuaState, arg: i32) -> Result<usize, LuaError> {
671    if state.is_number(arg) {
672        Err(LuaError::arg_error(
673            arg,
674            "number has no integer representation",
675        ))
676    } else {
677        tag_error(state, arg, LuaType::Number)?;
678        unreachable!("tag_error always returns Err")
679    }
680}
681
682/// Return the integer at `arg` as `i64`; raise if not an integer-convertible number.
683///
684pub fn check_integer(state: &mut LuaState, arg: i32) -> Result<i64, LuaError> {
685    match state.to_integer_x(arg) {
686        Some(d) => Ok(d),
687        None => {
688            int_error(state, arg)?;
689            unreachable!("int_error always returns Err")
690        }
691    }
692}
693
694/// Return the integer at `arg`; if absent/nil return `def`.
695///
696pub fn opt_integer(state: &mut LuaState, arg: i32, def: i64) -> Result<i64, LuaError> {
697    if state.is_none_or_nil(arg) {
698        Ok(def)
699    } else {
700        check_integer(state, arg)
701    }
702}
703
704// ── Buffer manipulation ────────────────────────────────────────────────────────
705
706impl LuaBuffer {
707    /// Create a new empty buffer.
708    ///
709    /// Rust uses `Vec::new()` which starts at zero capacity; capacity is managed by Vec.
710    pub fn new() -> Self {
711        LuaBuffer { data: Vec::new() }
712    }
713
714    /// Returns the number of bytes currently in the buffer.
715    pub fn len(&self) -> usize {
716        self.data.len()
717    }
718}
719
720impl Default for LuaBuffer {
721    fn default() -> Self {
722        LuaBuffer::new()
723    }
724}
725
726/// Initialize `buf` and associate it with `state`.
727/// Pushes a placeholder light-userdata onto `state` to anchor the buffer in C.
728/// In Rust the Vec is self-contained; we still push a placeholder for stack-slot
729/// compatibility with code that later calls `add_value` / `push_result`.
730///
731pub fn buf_init(state: &mut LuaState, buf: &mut LuaBuffer) {
732    // PORT NOTE: C pushes a light-userdata placeholder onto the stack to hold
733    // the buffer's position. We still push nil as a stack slot placeholder so
734    // that add_value / push_result see the same stack layout.
735    *buf = LuaBuffer::new();
736    // We push nil; Phase B can revisit if this matters for GC interaction.
737    let _ = state.push(LuaValue::Nil);
738}
739
740/// Initialize `buf`, reserve `sz` bytes, and return the writable region.
741///
742pub fn buf_init_size(
743    state: &mut LuaState,
744    buf: &mut LuaBuffer,
745    sz: usize,
746) -> Result<(), LuaError> {
747    buf_init(state, buf);
748    buf.data.reserve(sz);
749    Ok(())
750}
751
752/// Compute a new buffer capacity that accommodates `sz` more bytes,
753/// growing by ×1.5 or more.
754///
755fn new_buff_size(buf: &LuaBuffer, sz: usize) -> Result<usize, LuaError> {
756    if usize::MAX - sz < buf.len() {
757        return Err(LuaError::runtime(format_args!("buffer too large")));
758    }
759    let newsize = (buf.data.capacity() / 2) * 3; // ×1.5
760    if newsize < buf.len() + sz {
761        Ok(buf.len() + sz)
762    } else {
763        Ok(newsize)
764    }
765}
766
767/// Ensure at least `sz` free bytes are available in `buf`.
768///
769pub fn prep_buff_size(buf: &mut LuaBuffer, sz: usize) -> Result<(), LuaError> {
770    if buf.data.capacity() - buf.data.len() < sz {
771        let newcap = new_buff_size(buf, sz)?;
772        buf.data.reserve(newcap - buf.data.len());
773    }
774    Ok(())
775}
776
777/// Append `s` to `buf`.
778///
779pub fn add_lstring(buf: &mut LuaBuffer, s: &[u8]) {
780    if !s.is_empty() {
781        buf.data.extend_from_slice(s);
782    }
783}
784
785/// Append a single byte to `buf`.
786///
787pub fn add_char(buf: &mut LuaBuffer, c: u8) {
788    buf.data.push(c);
789}
790
791/// Append `sz` to the length counter (used after writing directly into the buffer).
792///
793pub fn add_size(_buf: &mut LuaBuffer, sz: usize) {
794    // PORT NOTE: In C this is a direct `n += sz` on the inline length field.
795    // With Vec, length is implicit; this is a no-op unless caller wrote past len.
796    // TODO(port): if direct-write into spare capacity is needed, switch to `unsafe`
797    // set_len or redesign; for Phase A this is a no-op.
798    let _ = sz;
799}
800
801/// Pop the string at top of `state`'s stack and append it to `buf`.
802///
803pub fn add_value(state: &mut LuaState, buf: &mut LuaBuffer) -> Result<(), LuaError> {
804    if let Some(bytes) = state.peek_bytes(-1) {
805        let owned = bytes.to_vec();
806        add_lstring(buf, &owned);
807    }
808    state.pop_n(1);
809    Ok(())
810}
811
812/// Push the buffer contents as a Lua string onto `state`'s stack.
813///
814pub fn push_result(state: &mut LuaState, buf: &mut LuaBuffer) -> Result<(), LuaError> {
815    state.push_bytes(&buf.data)?;
816    state.remove(-2)?;
817    Ok(())
818}
819
820/// Add `sz` bytes to the buffer count then call `push_result`.
821///
822pub fn push_result_size(
823    state: &mut LuaState,
824    buf: &mut LuaBuffer,
825    sz: usize,
826) -> Result<(), LuaError> {
827    add_size(buf, sz);
828    push_result(state, buf)
829}
830
831/// Perform global byte-string substitution: replace all occurrences of `pat`
832/// with `repl` in `s`, appending results into `buf`.
833///
834pub fn add_gsub(buf: &mut LuaBuffer, s: &[u8], pat: &[u8], repl: &[u8]) {
835    if pat.is_empty() {
836        add_lstring(buf, s);
837        return;
838    }
839    let mut remaining = s;
840    while let Some(pos) = find_bytes(remaining, pat) {
841        add_lstring(buf, &remaining[..pos]);
842        add_lstring(buf, repl);
843        remaining = &remaining[pos + pat.len()..];
844    }
845    add_lstring(buf, remaining);
846}
847
848/// Build a string from `s` by replacing `pat` with `repl`, push it on the stack,
849/// and return the bytes of the pushed string.
850///
851pub fn gsub<'a>(
852    state: &'a mut LuaState,
853    s: &[u8],
854    pat: &[u8],
855    repl: &[u8],
856) -> Result<Vec<u8>, LuaError> {
857    let mut b = LuaBuffer::new();
858    buf_init(state, &mut b);
859    add_gsub(&mut b, s, pat, repl);
860    push_result(state, &mut b)?;
861    Ok(state.peek_bytes(-1).unwrap_or_default())
862}
863
864/// Find `needle` in `haystack`, returning the byte offset or `None`.
865///
866/// Internal helper replacing C's `strstr`.
867fn find_bytes(haystack: &[u8], needle: &[u8]) -> Option<usize> {
868    if needle.is_empty() {
869        return Some(0);
870    }
871    haystack.windows(needle.len()).position(|w| w == needle)
872}
873
874// ── Reference system ──────────────────────────────────────────────────────────
875
876/// Store the value at the top of the stack in table `t` and return a unique
877/// integer reference. If the value is `nil`, returns `LUA_REFNIL` without
878/// modifying the table.
879///
880pub fn lua_ref(state: &mut LuaState, t: i32) -> Result<i32, LuaError> {
881    if state.type_at(-1) == LuaType::Nil {
882        state.pop_n(1);
883        return Ok(LUA_REFNIL);
884    }
885    let t = state.abs_index(t);
886    let ref_val: i32;
887    if state.raw_get_i(t, FREELIST_REF)? == LuaType::Nil {
888        ref_val = 0; // list is empty
889        state.push(LuaValue::Int(0));
890        state.raw_set_i(t, FREELIST_REF)?;
891    } else {
892        debug_assert!(state.type_at(-1) == LuaType::Number);
893        ref_val = state.to_integer_x(-1).unwrap_or(0) as i32;
894    }
895    state.pop_n(1); // remove element from stack
896    let next_ref: i32;
897    if ref_val != 0 {
898        state.raw_get_i(t, ref_val as i64)?;
899        state.raw_set_i(t, FREELIST_REF)?;
900        next_ref = ref_val;
901    } else {
902        next_ref = (state.raw_len(t) as i32) + 1;
903    }
904    state.raw_set_i(t, next_ref as i64)?;
905    Ok(next_ref)
906}
907
908/// Release reference `ref` from table `t`, adding it to the free list.
909///
910pub fn lua_unref(state: &mut LuaState, t: i32, r: i32) -> Result<(), LuaError> {
911    if r >= 0 {
912        let t = state.abs_index(t);
913        state.raw_get_i(t, FREELIST_REF)?;
914        debug_assert!(state.type_at(-1) == LuaType::Number);
915        state.raw_set_i(t, r as i64)?;
916        state.push(LuaValue::Int(r as i64));
917        state.raw_set_i(t, FREELIST_REF)?;
918    }
919    Ok(())
920}
921
922// ── Load functions ─────────────────────────────────────────────────────────────
923
924/// Internal chunk reader that returns a single buffer slice then signals EOF.
925///
926fn make_string_reader(data: Vec<u8>) -> impl FnMut() -> Option<Vec<u8>> {
927    let mut remaining = Some(data);
928    move || remaining.take()
929}
930
931/// Strip an optional UTF-8 BOM (EF BB BF) and any `#`-prefixed first line.
932///
933/// PORT NOTE: C reads byte-by-byte with `getc`/`feof` and lazily reopens the
934/// file in binary mode if it looks like a binary chunk. Here we ask the
935/// embedder-installed file loader hook for raw bytes, strip the BOM, and let
936/// `lua_vm::api::load` dispatch text vs. binary by the first byte. The "binary
937/// chunk" branch in `luaL_loadfilex` exists in C because text mode does newline
938/// translation; the host loader is expected to provide raw bytes.
939fn skip_bom_and_shebang(buf: &[u8]) -> Vec<u8> {
940    let s = if buf.starts_with(b"\xEF\xBB\xBF") { &buf[3..] } else { buf };
941    if s.first() == Some(&b'#') {
942        let nl = s.iter().position(|&b| b == b'\n').map(|p| p + 1).unwrap_or(s.len());
943        let rest = &s[nl..];
944        if rest.first() == Some(&0x1B) {
945            rest.to_vec()
946        } else {
947            let mut out = Vec::with_capacity(rest.len() + 1);
948            out.push(b'\n');
949            out.extend_from_slice(rest);
950            out
951        }
952    } else {
953        s.to_vec()
954    }
955}
956
957/// Load a file as a Lua chunk. Returns `LUA_OK` on success or an error code.
958///
959///
960/// PORT NOTE: PORTING.md §1 bans `std::fs` outside `lua-cli`, but C-Lua's
961/// `luaL_loadfilex` is part of the auxiliary library (`lauxlib.c`) and is
962/// reachable from the base library (`loadfile`/`dofile`). Phase A's stub
963/// raised an error here, which broke `loadfile(missing)` returning `nil, err`.
964/// The real C semantics push an error string onto the stack and return a
965/// non-zero status, which `load_aux` then converts to `(nil, errmsg)`.
966pub fn load_filex(
967    state: &mut LuaState,
968    filename: Option<&[u8]>,
969    mode: Option<&[u8]>,
970) -> Result<i32, LuaError> {
971    let _ = mode;
972    let fname = match filename {
973        Some(f) => f,
974        None => {
975            // TODO(port): stdin loading not yet supported in lua-stdlib; return
976            // an error string matching C's "cannot read stdin" shape.
977            state.push_string(b"cannot read stdin: no filename given")?;
978            return Ok(LUA_ERRFILE);
979        }
980    };
981    let raw = match state.global().file_loader_hook {
982        Some(load_fn) => load_fn(fname),
983        None => Err(LuaError::runtime(format_args!(
984            "no file_loader_hook registered"
985        ))),
986    };
987    let raw = match raw {
988        Ok(bytes) => bytes,
989        Err(e) => {
990            let detail = match &e {
991                LuaError::Runtime(LuaValue::Str(s)) => {
992                    String::from_utf8_lossy(s.as_bytes()).into_owned()
993                }
994                other => format!("{:?}", other),
995            };
996            state.push_fstring(format_args!(
997                "cannot open {}: {}",
998                BStr(fname),
999                detail
1000            ))?;
1001            return Ok(LUA_ERRFILE);
1002        }
1003    };
1004    let payload = skip_bom_and_shebang(&raw);
1005    let mut once = Some(payload);
1006    let boxed: Box<dyn FnMut() -> Option<Vec<u8>>> =
1007        Box::new(move || once.take());
1008    let mut chunkname = b"@".to_vec();
1009    chunkname.extend_from_slice(fname);
1010    let status = lua_vm::api::load(state, boxed, Some(&chunkname), mode)?;
1011    Ok(if status == LuaStatus::Ok { 0 } else { status as i32 })
1012}
1013
1014/// Load a buffer as a Lua chunk.
1015///
1016pub fn load_bufferx(
1017    state: &mut LuaState,
1018    buff: &[u8],
1019    name: &[u8],
1020    mode: Option<&[u8]>,
1021) -> Result<i32, LuaError> {
1022    // TODO(phase-b): state.load expects (chunk: &[u8], name, mode) in state_stub; the reader-based loader needs a load_with_reader API match.
1023    let _reader = make_string_reader(buff.to_vec());
1024    let ok = state.load(buff, name, mode)?;
1025    Ok(if ok { 0 } else { 1 })
1026}
1027
1028/// Load a buffer as a Lua chunk (no mode argument).
1029///
1030pub fn load_buffer(
1031    state: &mut LuaState,
1032    buff: &[u8],
1033    name: &[u8],
1034) -> Result<i32, LuaError> {
1035    load_bufferx(state, buff, name, None)
1036}
1037
1038/// Load a NUL-terminated byte-string as a Lua chunk.
1039///
1040pub fn load_string(state: &mut LuaState, s: &[u8]) -> Result<i32, LuaError> {
1041    load_buffer(state, s, s)
1042}
1043
1044// ── Meta-field and misc helpers ───────────────────────────────────────────────
1045
1046/// Push the metafield `event` of `obj` onto the stack and return its type.
1047/// If there is no metafield, nothing is pushed and `LuaType::Nil` is returned.
1048///
1049pub fn get_metafield(
1050    state: &mut LuaState,
1051    obj: i32,
1052    event: &[u8],
1053) -> Result<LuaType, LuaError> {
1054    if !state.get_metatable(obj)? {
1055        return Ok(LuaType::Nil);
1056    }
1057    state.push_bytes(event)?;
1058    let tt = state.raw_get(-2)?;
1059    if tt == LuaType::Nil {
1060        state.pop_n(2);
1061    } else {
1062        state.remove(-2)?;
1063    }
1064    Ok(tt)
1065}
1066
1067/// Call the metafield `event` of `obj` with `obj` as argument, pushing one result.
1068/// Returns `true` if the meta-method existed and was called.
1069///
1070pub fn call_meta(state: &mut LuaState, obj: i32, event: &[u8]) -> Result<bool, LuaError> {
1071    let obj = state.abs_index(obj);
1072    if get_metafield(state, obj, event)? == LuaType::Nil {
1073        return Ok(false);
1074    }
1075    state.push_value(obj)?;
1076    state.call(1, 1)?;
1077    Ok(true)
1078}
1079
1080/// Return the length of the value at `idx` as a `i64`, raising an error if
1081/// the length is not an integer.
1082///
1083pub fn lua_len(state: &mut LuaState, idx: i32) -> Result<i64, LuaError> {
1084    state.len_op(idx)?;
1085    let l = match state.to_integer_x(-1) {
1086        Some(n) => n,
1087        None => {
1088            return Err(LuaError::runtime(format_args!(
1089                "object length is not an integer"
1090            )));
1091        }
1092    };
1093    state.pop_n(1);
1094    Ok(l)
1095}
1096
1097/// Convert the value at `idx` to a byte-string representation (using `__tostring`
1098/// if available) and push it onto the stack.
1099///
1100pub fn to_lua_string(state: &mut LuaState, idx: i32) -> Result<Vec<u8>, LuaError> {
1101    let idx = state.abs_index(idx);
1102    if call_meta(state, idx, b"__tostring")? {
1103        if state.type_at(-1) != LuaType::String {
1104            return Err(LuaError::runtime(format_args!(
1105                "'__tostring' must return a string"
1106            )));
1107        }
1108    } else {
1109        match state.type_at(idx) {
1110            LuaType::Number => {
1111                if state.is_integer(idx) {
1112                    let i = state.to_integer_x(idx).unwrap_or(0);
1113                    state.push_fstring(format_args!("{}", i))?;
1114                } else {
1115                    let f = state.to_number_x(idx).unwrap_or(0.0);
1116                    state.push_fstring(format_args!("{:?}", f))?;
1117                }
1118            }
1119            LuaType::String => {
1120                state.push_value(idx)?;
1121            }
1122            LuaType::Boolean => {
1123                let b = state.to_boolean(idx);
1124                state.push_string(if b { b"true" } else { b"false" })?;
1125            }
1126            LuaType::Nil => {
1127                state.push_string(b"nil")?;
1128            }
1129            _ => {
1130                let tt = get_metafield(state, idx, b"__name")?;
1131                let kind: Vec<u8> = if tt == LuaType::String {
1132                    state.peek_bytes(-1).unwrap_or_else(|| b"?".to_vec())
1133                } else {
1134                    state.type_name_at(idx).to_vec()
1135                };
1136                // TODO(port): lua_topointer gives a pointer address; in Rust use
1137                // a hash or allocation address for a stable identifier.
1138                state.push_fstring(format_args!("{}: 0x?", BStr(&kind)))?;
1139                if tt != LuaType::Nil {
1140                    state.remove(-2)?;
1141                }
1142            }
1143        }
1144    }
1145    Ok(state.peek_bytes(-1).unwrap_or_default())
1146}
1147
1148/// Register the functions in `l` into the table at `-(nup + 1)`, giving each
1149/// closure the `nup` upvalues currently at the top of the stack.
1150///
1151pub fn set_funcs(
1152    state: &mut LuaState,
1153    l: &[LuaReg],
1154    nup: i32,
1155) -> Result<(), LuaError> {
1156    check_stack(state, nup, Some(b"too many upvalues"))?;
1157    for reg in l {
1158        match reg.func {
1159            None => {
1160                state.push(LuaValue::Bool(false));
1161            }
1162            Some(f) => {
1163                for _ in 0..nup {
1164                    state.push_value(-nup)?;
1165                }
1166                state.push_c_closure(f, nup)?;
1167            }
1168        }
1169        state.set_field(-(nup + 2), reg.name)?;
1170    }
1171    state.pop_n(nup as usize);
1172    Ok(())
1173}
1174
1175/// Ensure `state[idx][fname]` is a table; push it.
1176/// Returns `true` if the table already existed, `false` if newly created.
1177///
1178pub fn get_subtable(
1179    state: &mut LuaState,
1180    idx: i32,
1181    fname: &[u8],
1182) -> Result<bool, LuaError> {
1183    if state.get_field(idx, fname)? == LuaType::Table {
1184        return Ok(true);
1185    }
1186    state.pop_n(1);
1187    let idx = state.abs_index(idx);
1188    let new_tbl = state.new_table();
1189    state.push(LuaValue::Table(new_tbl));
1190    state.push_value(-1)?;
1191    state.set_field(idx, fname)?;
1192    Ok(false)
1193}
1194
1195/// Simplified `require`: open module `modname` via `openf`, register it in
1196/// `package.loaded`, and (if `glb`) in the global table.
1197/// Leaves the module on top of the stack.
1198///
1199pub fn requiref(
1200    state: &mut LuaState,
1201    modname: &[u8],
1202    openf: fn(&mut LuaState) -> Result<usize, LuaError>,
1203    glb: bool,
1204) -> Result<(), LuaError> {
1205    get_subtable(state, LUA_REGISTRYINDEX, LUA_LOADED_TABLE)?;
1206    state.get_field(-1, modname)?;
1207    if !state.to_boolean(-1) {
1208        state.pop_n(1);
1209        state.push_c_function(openf)?;
1210        state.push_bytes(modname)?;
1211        state.call(1, 1)?;
1212        state.push_value(-1)?;
1213        state.set_field(-3, modname)?;
1214    }
1215    state.remove(-2)?;
1216    if glb {
1217        state.push_value(-1)?;
1218        state.set_global(modname)?;
1219    }
1220    Ok(())
1221}
1222
1223// ── Helper for registry-based metatable lookup ─────────────────────────────────
1224
1225/// Push `registry[tname]` and return its type.
1226///
1227pub fn get_metatable(state: &mut LuaState, tname: &[u8]) -> Result<LuaType, LuaError> {
1228    state.get_field(LUA_REGISTRYINDEX, tname)
1229}
1230
1231// ── State creation and version check ─────────────────────────────────────────
1232
1233/// Create a new `LuaState` with the default allocator, a panic handler, and
1234/// warnings disabled.
1235///
1236pub fn new_state() -> Result<LuaState, LuaError> {
1237    // PORT NOTE: Rust's allocator is used implicitly; no l_alloc hook needed.
1238    // TODO(phase-b): LuaState::new() / set_panic_handler / set_warn_fn need a real LuaState constructor in lua-vm. Stub for Phase A.
1239    let _ = default_panic_handler;
1240    let _ = warn_off;
1241    todo!("phase-b: LuaState::new()")
1242}
1243
1244/// Default panic handler: print message to stderr and return to abort.
1245///
1246fn default_panic_handler(state: &mut LuaState) -> Result<usize, LuaError> {
1247    let msg = if state.type_at(-1) == LuaType::String {
1248        state.peek_bytes(-1).unwrap_or_else(|| b"?".to_vec())
1249    } else {
1250        b"error object is not a string".to_vec()
1251    };
1252    eprintln!("PANIC: unprotected error in call to Lua API ({})", BStr(&msg));
1253    Ok(0) // return to Lua to abort
1254}
1255
1256/// Warning function: warnings are off.
1257///
1258fn warn_off(state: &mut LuaState, message: &[u8], tocont: bool) -> Result<(), LuaError> {
1259    check_control(state, message, tocont)?;
1260    Ok(())
1261}
1262
1263/// Warning function: ready to start a new message.
1264///
1265fn warn_on(state: &mut LuaState, message: &[u8], tocont: bool) -> Result<(), LuaError> {
1266    if check_control(state, message, tocont)? {
1267        return Ok(());
1268    }
1269    eprint!("Lua warning: ");
1270    warn_cont(state, message, tocont)
1271}
1272
1273/// Warning function: continue writing a previous warning message.
1274///
1275fn warn_cont(_state: &mut LuaState, message: &[u8], tocont: bool) -> Result<(), LuaError> {
1276    eprint!("{}", BStr(message));
1277    // TODO(phase-b): set_warn_fn expects lua_CFunction in state_stub; warn_cont/warn_on take (msg, tocont). Wire after warn-fn API lands in lua-vm.
1278    if tocont {
1279        let _ = (warn_cont as fn(&mut LuaState, &[u8], bool) -> Result<(), LuaError>,);
1280    } else {
1281        eprintln!();
1282        let _ = (warn_on as fn(&mut LuaState, &[u8], bool) -> Result<(), LuaError>,);
1283    }
1284    Ok(())
1285}
1286
1287/// Handle a warning control message (e.g. `"@on"`, `"@off"`).
1288/// Returns `true` if the message was a recognised control message.
1289///
1290fn check_control(
1291    state: &mut LuaState,
1292    message: &[u8],
1293    tocont: bool,
1294) -> Result<bool, LuaError> {
1295    if tocont || message.first() != Some(&b'@') {
1296        return Ok(false);
1297    }
1298    let cmd = &message[1..];
1299    // TODO(phase-b): set_warn_fn expects lua_CFunction in state_stub; warn_off/warn_on take (msg, tocont). Wire after warn-fn API lands in lua-vm.
1300    let _ = state;
1301    if cmd == b"off" {
1302        let _ = warn_off as fn(&mut LuaState, &[u8], bool) -> Result<(), LuaError>;
1303    } else if cmd == b"on" {
1304        let _ = warn_on as fn(&mut LuaState, &[u8], bool) -> Result<(), LuaError>;
1305    }
1306    Ok(true)
1307}
1308
1309/// Version-compatibility check: error if numeric type sizes or version mismatch.
1310///
1311pub fn check_version(state: &mut LuaState, ver: f64, sz: usize) -> Result<(), LuaError> {
1312    const LUAL_NUMSIZES: usize = std::mem::size_of::<i64>() * 16 + std::mem::size_of::<f64>();
1313    if sz != LUAL_NUMSIZES {
1314        return Err(LuaError::runtime(format_args!(
1315            "core and library have incompatible numeric types"
1316        )));
1317    }
1318    let v = state.lua_version();
1319    if (v - ver).abs() > f64::EPSILON {
1320        return Err(LuaError::runtime(format_args!(
1321            "version mismatch: app. needs {}, Lua core provides {}",
1322            ver, v
1323        )));
1324    }
1325    Ok(())
1326}
1327
1328// ── Internal display helper ────────────────────────────────────────────────────
1329
1330/// Wrapper that implements `Display` for `&[u8]` as a lossy byte string.
1331/// Used to embed byte slices in `format_args!` without allocating a `String`.
1332///
1333/// PORT NOTE: not used for Lua string data; used only for error message
1334/// formatting inside `format_args!` literals.
1335struct BStr<'a>(&'a [u8]);
1336
1337impl<'a> std::fmt::Display for BStr<'a> {
1338    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
1339        for &b in self.0 {
1340            if b.is_ascii() {
1341                f.write_char(b as char)?;
1342            } else {
1343                write!(f, "\\x{:02x}", b)?;
1344            }
1345        }
1346        Ok(())
1347    }
1348}
1349
1350// Required for fmt::Display
1351use std::fmt::Write as _;
1352
1353// ── LuaDebug Default ─────────────────────────────────────────────────────────
1354
1355
1356// ──────────────────────────────────────────────────────────────────────────
1357// PORT STATUS
1358//   source:        src/lauxlib.c  (1127 lines, ~50 functions)
1359//   target_crate:  lua-stdlib
1360//   confidence:    medium
1361//   todos:         10
1362//   port_notes:    8
1363//   unsafe_blocks: 0
1364//   notes:         Buffer simplified from stack-based C UBox/box-on-Lua-stack to
1365//                  plain Vec<u8> (LuaBuffer); UBox/resizebox/boxgc/boxmt/newbox
1366//                  machinery dropped entirely — Rust Drop handles deallocation.
1367//                  load_filex reads via GlobalState::file_loader_hook and pushes
1368//                  an error string on open failure so loadfile/dofile return
1369//                  (nil, err) per C semantics (stdin loading still TODO).
1370//                  Warning system uses fn-ptr callbacks matching lua_WarnFunction
1371//                  type; warnfoff/warnfon/warnfcont translated faithfully.
1372//                  LuaState / LuaDebug / GcRef are Phase-A stubs; Phase B replaces
1373//                  with real imports from lua-vm / lua-types.
1374//                  add_size() is a no-op in Phase A (Vec tracks length implicitly);
1375//                  direct buffer writes via spare capacity need revisit in Phase B.
1376//                  int_error() return type changed from `!` to `Result<usize,_>` as
1377//                  the never type is nightly-only on stable Rust.
1378// ──────────────────────────────────────────────────────────────────────────