Skip to main content

lua_stdlib/
io_lib.rs

1//! Standard I/O library — `io.*` functions and `file:*` methods.
2//!
3//! C source: `reference/lua-5.4.7/src/liolib.c`.
4//!
5//! **Impurity is host-provided and load-bearing.** Filesystem and process
6//! access reach the host only through hooks: regular files via
7//! `GlobalState::file_open_hook`, `io.popen` via `GlobalState::popen_hook`,
8//! stdout/stderr via output hooks. The native CLI installs hooks backed by
9//! `std::fs`/`std::process`/`std::io`; sandboxed and `wasm32` hosts leave those
10//! capabilities absent, and the functions then return a clean `(nil, msg,
11//! errno)` failure tuple (or, for the standard streams, an `Unsupported` error)
12//! instead of touching ambient OS state. That hook plumbing and the `wasm32`
13//! cfg gates are deliberately kept intact.
14//!
15//! **The side-table indirection** is the one structural divergence from C: C
16//! stores the `LStream` inside the userdata payload, but `LStream` carries heap
17//! pointers (a `Box<dyn LuaFileHandle>` and a fn pointer) that cannot be safely
18//! reinterpreted from a raw byte buffer in safe Rust, so the stream lives in a
19//! thread-local `LSTREAM_REGISTRY` keyed by userdata identity. Each I/O step
20//! borrows the file through its `Rc<RefCell<LStream>>` briefly, releases the
21//! borrow, then touches `LuaState` — resolving C's single `LStream *` aliasing
22//! into two scoped borrows.
23//!
24//! Graduation: the deterministic format-parsing/validation and error-shaping
25//! surface (read formats, the `*`-prefix version seam, the closed-file error,
26//! `io.type`) is pinned by `tests/io_strengthen.rs` against the reference
27//! binaries; host-specific I/O *results* are not reproducible and stay
28//! oracle-checked only through the official `files.lua` suite. See
29//! `crates/lua-stdlib/GRADUATED.md`.
30
31use std::cell::RefCell;
32use std::collections::HashMap;
33use std::io::{self, SeekFrom};
34use std::rc::Rc;
35
36use crate::state_stub::{LuaState, LuaStateStubExt as _};
37use lua_types::{LuaError, LuaFileHandle, LuaType, LuaValue};
38use lua_vm::state::{InputHook, OutputHook};
39
40thread_local! {
41    /// Side-table mapping userdata identity (the `Rc` pointer address from
42    /// `GcRef::identity()`) to its associated `LStream` (see the module header
43    /// for why the stream lives here rather than inside the userdata payload).
44    /// Entries are inserted by `new_pre_file` and intentionally never removed —
45    /// a bounded leak per `PORTING.md` §2 #4.
46    static LSTREAM_REGISTRY: RefCell<HashMap<usize, Rc<RefCell<LStream>>>>
47        = RefCell::new(HashMap::new());
48}
49
50fn register_lstream(ud_id: usize, lstream: LStream) -> Rc<RefCell<LStream>> {
51    let cell = Rc::new(RefCell::new(lstream));
52    LSTREAM_REGISTRY.with(|reg| {
53        reg.borrow_mut().insert(ud_id, cell.clone());
54    });
55    cell
56}
57
58fn lookup_lstream(ud_id: usize) -> Option<Rc<RefCell<LStream>>> {
59    LSTREAM_REGISTRY.with(|reg| reg.borrow().get(&ud_id).cloned())
60}
61
62// ── Constants ────────────────────────────────────────────────────────────────
63
64/// Name of the file-handle metatable in the Lua registry. C: `LUA_FILEHANDLE`.
65pub const LUA_FILE_HANDLE: &[u8] = b"FILE*";
66
67/// Registry key for the default input file. C: `IO_INPUT` = `"_IO_input"`.
68const IO_INPUT_KEY: &[u8] = b"_IO_input";
69
70/// Registry key for the default output file. C: `IO_OUTPUT` = `"_IO_output"`.
71const IO_OUTPUT_KEY: &[u8] = b"_IO_output";
72
73/// Number of bytes in the `"_IO_"` prefix, used to strip it in error messages.
74const IO_PREFIX_LEN: usize = 4;
75
76/// Maximum number of format-arguments passed to `file:lines`. C: `MAXARGLINE`.
77const MAX_ARG_LINE: usize = 250;
78
79/// Maximum byte-length of a numeric literal read from a file. C: `L_MAXLENNUM`.
80const L_MAX_LEN_NUM: usize = 200;
81
82/// End-of-file sentinel returned by `LuaFileHandle::read_byte`. C: `EOF` == -1.
83const EOF_SENTINEL: i32 = -1;
84
85/// Bulk-read chunk size, mirroring C's `LUAL_BUFFERSIZE`.
86const LUAL_BUFFER_SIZE: usize = 8192;
87
88// ── Traits ───────────────────────────────────────────────────────────────────
89
90/// Capabilities required by the io library from an OS file handle.
91///
92/// This trait extends [`LuaFileHandle`] (defined in `lua-types`) with the
93/// additional `set_buf_mode` operation. Concrete implementations backed by
94/// `std::fs::File` live in `lua-cli`; standard-stream implementations live in
95/// this module. The split keeps `std::fs` out of `lua-stdlib` per PORTING.md §1.
96pub trait LuaFileOps: LuaFileHandle {
97    /// Control stream buffering. C: `setvbuf`.
98    fn set_buf_mode(&mut self, mode: BufMode, size: usize) -> io::Result<()>;
99}
100
101// ── Enums ────────────────────────────────────────────────────────────────────
102
103/// Seek anchor for `file:seek`. C: `{SEEK_SET, SEEK_CUR, SEEK_END}`.
104#[derive(Debug, Clone, Copy, PartialEq, Eq)]
105pub enum SeekWhence {
106    Set,
107    Cur,
108    End,
109}
110
111/// Buffering mode for `file:setvbuf`. C: `{_IONBF, _IOFBF, _IOLBF}`.
112#[derive(Debug, Clone, Copy, PartialEq, Eq)]
113pub enum BufMode {
114    No,
115    Full,
116    Line,
117}
118
119/// Which standard stream to wrap in `create_std_file`.
120pub enum StdFileKind {
121    Stdin,
122    Stdout,
123    Stderr,
124}
125
126// ── Structs ──────────────────────────────────────────────────────────────────
127
128/// A Lua file handle. C equivalent: `typedef luaL_Stream LStream`.
129///
130/// The instance lives in `LSTREAM_REGISTRY` (keyed by its userdata's identity),
131/// wrapped in `Rc<RefCell<…>>` so a brief file borrow and a `LuaState` borrow
132/// never overlap — the safe-Rust resolution of C's single `LStream *` pointer.
133pub struct LStream {
134    /// OS file handle. `None` = incompletely opened (the pre-file pattern).
135    /// Concrete implementations are installed via `GlobalState::file_open_hook`
136    /// (registered by `lua-cli`) to keep `std::fs` out of `lua-stdlib`.
137    pub file: Option<Box<dyn LuaFileHandle>>,
138    /// Close callback. `None` means the stream is closed. C: `p->closef == NULL`.
139    pub close_fn: Option<fn(&mut LuaState) -> Result<usize, LuaError>>,
140}
141
142impl LStream {
143    /// `isclosed(p)` in C: true when `closef` is NULL.
144    pub fn is_closed(&self) -> bool {
145        self.close_fn.is_none()
146    }
147}
148
149/// Standard stream handle for stdin/stdout/stderr.
150///
151/// Output goes through host hooks when installed. Native builds keep a direct
152/// stdio fallback for compatibility; bare `wasm32-unknown-unknown` reports
153/// unsupported instead of touching stubbed stdio.
154struct StdStreamHandle {
155    kind: StdFileKind,
156    input_hook: Option<InputHook>,
157    output_hook: Option<OutputHook>,
158    unread: Option<u8>,
159}
160
161impl LuaFileHandle for StdStreamHandle {
162    fn read_byte(&mut self) -> i32 {
163        if let Some(byte) = self.unread.take() {
164            return byte as i32;
165        }
166        match self.kind {
167            StdFileKind::Stdin => {
168                if let Some(read_fn) = self.input_hook {
169                    let mut buf = [0u8; 1];
170                    return match read_fn(&mut buf) {
171                        Ok(1) => buf[0] as i32,
172                        _ => EOF_SENTINEL,
173                    };
174                }
175
176                #[cfg(all(target_arch = "wasm32", target_os = "unknown"))]
177                {
178                    EOF_SENTINEL
179                }
180
181                #[cfg(not(all(target_arch = "wasm32", target_os = "unknown")))]
182                {
183                    use std::io::Read;
184                    let mut buf = [0u8; 1];
185                    match std::io::stdin().read(&mut buf) {
186                        Ok(1) => buf[0] as i32,
187                        _ => EOF_SENTINEL,
188                    }
189                }
190            }
191            _ => EOF_SENTINEL,
192        }
193    }
194    fn unread_byte(&mut self, byte: i32) {
195        if (0..=u8::MAX as i32).contains(&byte) {
196            self.unread = Some(byte as u8);
197        }
198    }
199    fn write_bytes(&mut self, data: &[u8]) -> io::Result<usize> {
200        if let Some(write_fn) = self.output_hook {
201            write_fn(data)?;
202            return Ok(data.len());
203        }
204
205        #[cfg(all(target_arch = "wasm32", target_os = "unknown"))]
206        {
207            let _ = data;
208            return Err(io::Error::new(
209                io::ErrorKind::Unsupported,
210                "standard output not available in this host",
211            ));
212        }
213
214        #[cfg(not(all(target_arch = "wasm32", target_os = "unknown")))]
215        {
216            use std::io::Write;
217            match self.kind {
218                StdFileKind::Stderr => {
219                    std::io::stderr().write_all(data)?;
220                    Ok(data.len())
221                }
222                _ => {
223                    std::io::stdout().write_all(data)?;
224                    Ok(data.len())
225                }
226            }
227        }
228    }
229    fn flush(&mut self) -> io::Result<()> {
230        if self.output_hook.is_some() {
231            return Ok(());
232        }
233
234        #[cfg(all(target_arch = "wasm32", target_os = "unknown"))]
235        {
236            return Err(io::Error::new(
237                io::ErrorKind::Unsupported,
238                "standard output not available in this host",
239            ));
240        }
241
242        #[cfg(not(all(target_arch = "wasm32", target_os = "unknown")))]
243        {
244            use std::io::Write;
245            match self.kind {
246                StdFileKind::Stderr => std::io::stderr().flush(),
247                _ => std::io::stdout().flush(),
248            }
249        }
250    }
251    fn seek(&mut self, _pos: SeekFrom) -> io::Result<u64> {
252        Err(io::Error::new(io::ErrorKind::Unsupported, "stdio seek"))
253    }
254    fn tell(&mut self) -> io::Result<u64> {
255        Err(io::Error::new(io::ErrorKind::Unsupported, "stdio tell"))
256    }
257    fn clear_error(&mut self) {}
258    fn has_error(&self) -> bool {
259        false
260    }
261}
262
263impl LuaFileOps for StdStreamHandle {
264    fn set_buf_mode(&mut self, _mode: BufMode, _size: usize) -> io::Result<()> {
265        Ok(())
266    }
267}
268
269impl StdStreamHandle {
270    fn new(
271        kind: StdFileKind,
272        input_hook: Option<InputHook>,
273        output_hook: Option<OutputHook>,
274    ) -> Self {
275        StdStreamHandle {
276            kind,
277            input_hook,
278            output_hook,
279            unread: None,
280        }
281    }
282}
283
284/// State machine for reading a numeric literal byte-by-byte from a file.
285struct ReadNumState {
286    /// Current look-ahead byte, or `EOF_SENTINEL`.
287    current: i32,
288    /// Number of bytes accumulated in `buf`.
289    count: usize,
290    /// Accumulated characters of the numeral (NUL-terminated on finalise).
291    buf: [u8; L_MAX_LEN_NUM + 1],
292}
293
294impl ReadNumState {
295    fn new(first_byte: i32) -> Self {
296        ReadNumState {
297            current: first_byte,
298            count: 0,
299            buf: [0u8; L_MAX_LEN_NUM + 1],
300        }
301    }
302
303    /// Save current char to `buf` and read the next byte from `file`.
304    /// Returns `false` if the buffer is full (numeral too long). C: `nextc`.
305    fn advance(&mut self, file: &mut dyn LuaFileHandle) -> bool {
306        if self.count >= L_MAX_LEN_NUM {
307            self.buf[0] = 0;
308            return false;
309        }
310        self.buf[self.count] = self.current as u8;
311        self.count += 1;
312        self.current = file.read_byte();
313        true
314    }
315
316    /// Accept current char if it equals either byte in `set`. C: `test2`.
317    fn try2(&mut self, file: &mut dyn LuaFileHandle, set: [u8; 2]) -> bool {
318        if self.current == set[0] as i32 || self.current == set[1] as i32 {
319            self.advance(file)
320        } else {
321            false
322        }
323    }
324
325    /// Consume a run of (hex)digits; return the count. C: `readdigits`.
326    fn read_digits(&mut self, file: &mut dyn LuaFileHandle, hex: bool) -> usize {
327        let mut count = 0usize;
328        loop {
329            let is_digit = if hex {
330                (self.current as u8).is_ascii_hexdigit()
331            } else {
332                (self.current as u8).is_ascii_digit()
333            };
334            if !is_digit || self.current == EOF_SENTINEL {
335                break;
336            }
337            if !self.advance(file) {
338                break;
339            }
340            count += 1;
341        }
342        count
343    }
344
345    /// Return the accumulated bytes (without the NUL terminator).
346    fn as_bytes(&self) -> &[u8] {
347        &self.buf[..self.count]
348    }
349}
350
351// ── Function registration tables ─────────────────────────────────────────────
352
353/// `io.*` module functions. C: `static const luaL_Reg iolib[]`.
354pub const IO_LIB: &[(&[u8], fn(&mut LuaState) -> Result<usize, LuaError>)] = &[
355    (b"close", io_close),
356    (b"flush", io_flush),
357    (b"input", io_input),
358    (b"lines", io_lines),
359    (b"open", io_open),
360    (b"output", io_output),
361    (b"popen", io_popen),
362    (b"read", io_read),
363    (b"tmpfile", io_tmpfile),
364    (b"type", io_type),
365    (b"write", io_write),
366];
367
368/// `file:*` instance methods. C: `static const luaL_Reg meth[]`.
369pub const FILE_METHODS: &[(&[u8], fn(&mut LuaState) -> Result<usize, LuaError>)] = &[
370    (b"read", f_read),
371    (b"write", f_write),
372    (b"lines", f_lines),
373    (b"flush", f_flush),
374    (b"seek", f_seek),
375    (b"close", f_close),
376    (b"setvbuf", f_setvbuf),
377];
378
379/// File-handle metamethods. C: `static const luaL_Reg metameth[]`.
380pub const FILE_METAMETHODS: &[(&[u8], fn(&mut LuaState) -> Result<usize, LuaError>)] = &[
381    (b"__gc", f_gc),
382    (b"__close", f_gc),
383    (b"__tostring", f_tostring),
384];
385
386// ── Helpers ──────────────────────────────────────────────────────────────────
387
388/// Validate an `fopen` mode string: must match `[rwa]\+?b*`. C: `l_checkmode`.
389///
390/// (*mode != '+' || ...) && strspn(mode, "b") == strlen(mode));`
391fn check_mode(mode: &[u8]) -> bool {
392    if mode.is_empty() {
393        return false;
394    }
395    let mut idx = 0usize;
396    if !matches!(mode[idx], b'r' | b'w' | b'a') {
397        return false;
398    }
399    idx += 1;
400    if idx < mode.len() && mode[idx] == b'+' {
401        idx += 1;
402    }
403    mode[idx..].iter().all(|&b| b == b'b')
404}
405
406/// Validate a `popen` mode string: only `"r"` or `"w"`. C: `l_checkmodep`.
407fn check_mode_popen(mode: &[u8]) -> bool {
408    matches!(mode, b"r" | b"w")
409}
410
411/// Push success (`true`) or failure (`fail`, msg, errno) per `luaL_fileresult`.
412///
413/// On success: `lua_pushboolean(L, 1); return 1`. On failure C runs
414/// `luaL_pushfail(L); lua_pushfstring(...); lua_pushinteger(errno); return 3`,
415/// and `luaL_pushfail` resolves to `lua_pushnil` on every supported version
416/// (5.1-5.5), so the first failure value is `nil`, never `false`. Tests that
417/// compare the failure handle to `nil` (e.g. `io.open(missing) == nil`) rely on
418/// this exact value.
419fn file_result(
420    state: &mut LuaState,
421    success: bool,
422    fname: Option<&[u8]>,
423    os_err: io::Error,
424) -> Result<usize, LuaError> {
425    if success {
426        state.push(LuaValue::Bool(true));
427        return Ok(1);
428    }
429    state.push(LuaValue::Nil);
430    let msg = os_err.to_string();
431    match fname {
432        Some(name) => {
433            let mut s = Vec::with_capacity(name.len() + 2 + msg.len());
434            s.extend_from_slice(name);
435            s.extend_from_slice(b": ");
436            s.extend_from_slice(msg.as_bytes());
437            state.push_string(&s)?;
438        }
439        None => {
440            state.push_string(msg.as_bytes())?;
441        }
442    }
443    let errno_code = os_err.raw_os_error().unwrap_or(0) as i64;
444    state.push(LuaValue::Int(errno_code));
445    Ok(3)
446}
447
448/// Push popen/system exit-status results per `luaL_execresult`: `true` on a
449/// zero status, else `(nil, "exit"|"signal", stat)`.
450///
451/// Deferred: `WIFEXITED`/`WTERMSIG` are not portable across all hosts, so this
452/// always reports a non-zero status as an `"exit"` code and never distinguishes
453/// a signal — faithful enough for the clients that probe an exit status.
454fn exec_result(state: &mut LuaState, stat: i32) -> Result<usize, LuaError> {
455    if stat == 0 {
456        state.push(LuaValue::Bool(true));
457        Ok(1)
458    } else {
459        state.push(LuaValue::Bool(false));
460        state.push_string(b"exit")?;
461        state.push(LuaValue::Int(stat as i64));
462        Ok(3)
463    }
464}
465
466/// Retrieve `LStream` from argument 1 via a userdata type-check.
467///
468/// Returns an `Rc<RefCell<LStream>>` from the side-table registry. The C port
469/// returns a raw `LStream *` pointing into the userdata payload; Rust uses a
470/// side table because `LStream` contains heap pointers that cannot be safely
471/// reinterpreted from a raw byte buffer in safe Rust.
472fn get_lstream(state: &mut LuaState) -> Result<Rc<RefCell<LStream>>, LuaError> {
473    let ud = state.check_arg_userdata(1, LUA_FILE_HANDLE)?;
474    lookup_lstream(ud.identity())
475        .ok_or_else(|| LuaError::runtime(format_args!("invalid file handle")))
476}
477
478/// Look up the `LStream` registered for the userdata sitting at upvalue `idx`.
479///
480/// `aux_lines` stores the file-handle userdata as upvalue 1 of `io_readline`;
481/// this helper performs the same registry round-trip that `get_lstream` does
482/// for argument 1, but reads the value from the closure's upvalue slot instead
483/// of the call stack.
484fn lstream_from_upvalue(state: &mut LuaState, idx: i32) -> Result<Rc<RefCell<LStream>>, LuaError> {
485    let v = state.value_at(crate::state_stub::upvalue_index(idx));
486    let ud_id = match v {
487        LuaValue::UserData(ud) => ud.identity(),
488        _ => {
489            return Err(LuaError::runtime(format_args!(
490                "invalid file handle in upvalue {}",
491                idx
492            )));
493        }
494    };
495    lookup_lstream(ud_id)
496        .ok_or_else(|| LuaError::runtime(format_args!("invalid file handle in upvalue {}", idx)))
497}
498
499/// Validate that argument 1 is an open file handle; error if closed.
500///
501/// The closed-file error is raised through `c_api_runtime` (the `luaL_error`
502/// analogue) so it carries the calling source-location prefix
503/// (`<source>:<line>:`), matching the reference `tofile` in `liolib.c`.
504fn tofile(state: &mut LuaState) -> Result<Rc<RefCell<LStream>>, LuaError> {
505    let p_rc = get_lstream(state)?;
506    let closed = {
507        let p = p_rc.borrow();
508        debug_assert!(p.is_closed() || p.file.is_some());
509        p.is_closed()
510    };
511    if closed {
512        return Err(lua_vm::debug::c_api_runtime(
513            state,
514            b"attempt to use a closed file".to_vec(),
515        ));
516    }
517    Ok(p_rc)
518}
519
520// ── File creation helpers ────────────────────────────────────────────────────
521
522/// Allocate a "closed" file-handle userdata and push it; set its metatable.
523/// Also registers an empty `LStream` in the side table keyed by the userdata
524/// identity, and returns the `Rc<RefCell<LStream>>` so the caller may finish
525/// initialising it (set `file`, set `close_fn`). C: `newprefile(L)`.
526fn new_pre_file(state: &mut LuaState) -> Result<Rc<RefCell<LStream>>, LuaError> {
527    let ud = state.new_userdata_typed(LUA_FILE_HANDLE, std::mem::size_of::<LStream>(), 0)?;
528    state.set_metatable_by_name(LUA_FILE_HANDLE)?;
529    let cell = register_lstream(
530        ud.identity(),
531        LStream {
532            file: None,
533            close_fn: None,
534        },
535    );
536    Ok(cell)
537}
538
539/// Allocate a new regular-file handle with `io_fclose` as the close function.
540fn new_file(state: &mut LuaState) -> Result<Rc<RefCell<LStream>>, LuaError> {
541    let cell = new_pre_file(state)?;
542    cell.borrow_mut().close_fn = Some(io_fclose);
543    Ok(cell)
544}
545
546/// Open `fname` and push its handle; raise a runtime error on failure.
547///
548/// The file system is reached via `GlobalState::file_open_hook` (registered by
549/// `lua-cli`) since `std::fs` is banned in `lua-stdlib` per PORTING.md §1.
550fn opencheck(state: &mut LuaState, fname: &[u8], mode: &[u8]) -> Result<(), LuaError> {
551    let hook = state.global().file_open_hook;
552    let fh = match hook {
553        Some(open_fn) => open_fn(fname, mode).map_err(|e| {
554            LuaError::runtime(format_args!(
555                "cannot open file '{}' ({})",
556                fname.escape_ascii(),
557                match e.message_bytes() {
558                    Some(b) => String::from_utf8_lossy(b).into_owned(),
559                    None => format!("{:?}", &e),
560                }
561            ))
562        })?,
563        None => {
564            return Err(LuaError::runtime(format_args!(
565                "cannot open file '{}' (no filesystem hook registered)",
566                fname.escape_ascii()
567            )));
568        }
569    };
570    let cell = new_file(state)?;
571    cell.borrow_mut().file = Some(fh);
572    Ok(())
573}
574
575// ── Close functions ──────────────────────────────────────────────────────────
576
577/// Close a regular file via `fclose`. C: `io_fclose`.
578///
579/// Dropping the `Box<dyn LuaFileHandle>` flushes through the host handle's own
580/// `Drop` (the CLI's writer flushes on drop). Deferred: surfacing a close-time
581/// I/O error as a `file_result` failure tuple — close currently always reports
582/// success.
583fn io_fclose(state: &mut LuaState) -> Result<usize, LuaError> {
584    let p_rc = get_lstream(state)?;
585    let _closed = p_rc.borrow_mut().file.take();
586    state.push(LuaValue::Bool(true));
587    Ok(1)
588}
589
590/// Close a popen process pipe. C: `io_pclose`.
591///
592/// Deferred: waiting on the child and forwarding its real exit status. Dropping
593/// the handle closes the pipe; the status reported here is a fixed success.
594fn io_pclose(state: &mut LuaState) -> Result<usize, LuaError> {
595    let p_rc = get_lstream(state)?;
596    let _closed = p_rc.borrow_mut().file.take();
597    exec_result(state, 0)
598}
599
600/// Refuse to close a standard-stream handle. C: `io_noclose`.
601///
602/// The close function is reinstalled before returning so the handle stays alive
603/// and remains closeable-but-inert on a later attempt, matching C's `io_noclose`.
604fn io_noclose(state: &mut LuaState) -> Result<usize, LuaError> {
605    let p_rc = get_lstream(state)?;
606    p_rc.borrow_mut().close_fn = Some(io_noclose);
607    state.push(LuaValue::Bool(false));
608    state.push_string(b"cannot close standard file")?;
609    Ok(2)
610}
611
612/// Invoke the stream's close function and mark it closed. C: `aux_close`.
613fn aux_close(state: &mut LuaState) -> Result<usize, LuaError> {
614    let p_rc = get_lstream(state)?;
615    let cf = p_rc.borrow_mut().close_fn.take().ok_or_else(|| {
616        LuaError::runtime(format_args!("attempt to close an already-closed file"))
617    })?;
618    cf(state)
619}
620
621// ── io.type ──────────────────────────────────────────────────────────────────
622
623/// `io.type(x)` — return `"file"`, `"closed file"`, or the fail value for a
624/// non-handle. C: `io_type`.
625///
626/// A non-handle pushes the `fail` value (`nil`) via the reference's
627/// `luaL_pushfail`, NOT `false`; `fail` is `nil` on every supported version.
628/// An unknown userdata still carrying the `FILE*` metatable but absent from the
629/// `LStream` side table is treated as closed (it cannot be an open stream).
630pub fn io_type(state: &mut LuaState) -> Result<usize, LuaError> {
631    state.check_arg_any(1)?;
632    let maybe_userdata = state.test_arg_userdata(1, LUA_FILE_HANDLE);
633    match maybe_userdata {
634        None => {
635            state.push(LuaValue::Nil);
636        }
637        Some(ud) => {
638            let is_closed = match lookup_lstream(ud.identity()) {
639                Some(rc) => rc.borrow().is_closed(),
640                None => true,
641            };
642            if is_closed {
643                state.push_string(b"closed file")?;
644            } else {
645                state.push_string(b"file")?;
646            }
647        }
648    }
649    Ok(1)
650}
651
652// ── __tostring metamethod ────────────────────────────────────────────────────
653
654/// `tostring(file)` metamethod. C: `f_tostring`.
655///
656/// An open handle renders `file (0x?)`. The reference prints the handle's real
657/// pointer address (`file (0x<addr>)`); that address is non-deterministic, so
658/// reproducing it is deferred and intentionally not pinned by the behavioral
659/// net. A closed handle renders `file (closed)`, matching the reference exactly.
660fn f_tostring(state: &mut LuaState) -> Result<usize, LuaError> {
661    let p_rc = get_lstream(state)?;
662    let closed = p_rc.borrow().is_closed();
663    if closed {
664        state.push_string(b"file (closed)")?;
665    } else {
666        state.push_string(b"file (0x?)")?;
667    }
668    Ok(1)
669}
670
671// ── close / gc ───────────────────────────────────────────────────────────────
672
673/// `file:close()`. C: `f_close`.
674fn f_close(state: &mut LuaState) -> Result<usize, LuaError> {
675    let _ = tofile(state)?; // validates stream is open before closing
676    aux_close(state)
677}
678
679/// `io.close([file])`. C: `io_close`.
680pub fn io_close(state: &mut LuaState) -> Result<usize, LuaError> {
681    // The pushed value naturally lands at position 1 (top advances by one from
682    // func+1 to func+2). The C source does NOT call lua_replace here; adding one
683    // would pop the value back out, since position 1 equals top-1 in this case.
684    if state.type_at(1) == LuaType::None {
685        state.registry_get(IO_OUTPUT_KEY)?;
686    }
687    f_close(state)
688}
689
690/// `__gc` / `__close` metamethod — silently close if still open. C: `f_gc`.
691fn f_gc(state: &mut LuaState) -> Result<usize, LuaError> {
692    let p_rc = get_lstream(state)?;
693    let needs_close = {
694        let p = p_rc.borrow();
695        !p.is_closed() && p.file.is_some()
696    };
697    if needs_close {
698        // ignore any error from aux_close during GC finalisation
699        let _ = aux_close(state);
700    }
701    Ok(0)
702}
703
704// ── io.open / io.popen / io.tmpfile ─────────────────────────────────────────
705
706/// `io.open(filename [, mode])`. C: `io_open`.
707///
708/// The file system is reached via `GlobalState::file_open_hook` (registered by
709/// `lua-cli`) since `std::fs` is banned in `lua-stdlib` per PORTING.md §1.
710pub fn io_open(state: &mut LuaState) -> Result<usize, LuaError> {
711    let filename: Vec<u8> = state.check_arg_string(1)?;
712    let mode: Vec<u8> = state.opt_arg_string(2, b"r")?;
713    if !check_mode(&mode) {
714        return Err(lua_vm::debug::arg_error_impl(state, 2, b"invalid mode"));
715    }
716    let hook = state.global().file_open_hook;
717    match hook {
718        Some(open_fn) => match open_fn(&filename, &mode) {
719            Ok(fh) => {
720                let cell = new_file(state)?;
721                cell.borrow_mut().file = Some(fh);
722                Ok(1)
723            }
724            Err(e) => {
725                let os_err = io::Error::new(
726                    io::ErrorKind::Other,
727                    match e.message_bytes() {
728                        Some(b) => String::from_utf8_lossy(b).into_owned(),
729                        None => format!("{:?}", &e),
730                    },
731                );
732                file_result(state, false, Some(&filename), os_err)
733            }
734        },
735        None => {
736            let os_err =
737                io::Error::new(io::ErrorKind::Unsupported, "no filesystem hook registered");
738            file_result(state, false, Some(&filename), os_err)
739        }
740    }
741}
742
743/// `io.popen(filename [, mode])`. C: `io_popen`.
744///
745/// `std::process::Command` is banned in `lua-stdlib`; the child process is
746/// spawned via `GlobalState::popen_hook`, which `lua-cli` installs. When the
747/// hook is absent (sandboxed embeddings), this returns a clean Lua failure
748/// shape (`nil, errmsg, errno`) rather than panicking, so clients such as
749/// LuaRocks that probe `io.popen` fall back gracefully instead of crashing
750/// the host.
751pub fn io_popen(state: &mut LuaState) -> Result<usize, LuaError> {
752    let filename: Vec<u8> = state.check_arg_string(1)?;
753    let mode: Vec<u8> = state.opt_arg_string(2, b"r")?;
754    if !check_mode_popen(&mode) {
755        return Err(lua_vm::debug::arg_error_impl(state, 2, b"invalid mode"));
756    }
757    let hook = state.global().popen_hook;
758    match hook {
759        Some(spawn_fn) => match spawn_fn(&filename, &mode) {
760            Ok(fh) => {
761                let cell = new_pre_file(state)?;
762                let mut p = cell.borrow_mut();
763                p.file = Some(fh);
764                p.close_fn = Some(io_pclose);
765                drop(p);
766                Ok(1)
767            }
768            Err(e) => {
769                let os_err = io::Error::new(
770                    io::ErrorKind::Other,
771                    match e.message_bytes() {
772                        Some(b) => String::from_utf8_lossy(b).into_owned(),
773                        None => format!("{:?}", &e),
774                    },
775                );
776                file_result(state, false, Some(&filename), os_err)
777            }
778        },
779        None => {
780            let os_err = io::Error::new(
781                io::ErrorKind::Unsupported,
782                "popen not enabled in this build",
783            );
784            file_result(state, false, Some(&filename), os_err)
785        }
786    }
787}
788
789fn native_temp_name() -> io::Result<Vec<u8>> {
790    #[cfg(all(target_arch = "wasm32", target_os = "unknown"))]
791    {
792        return Err(io::Error::new(
793            io::ErrorKind::Unsupported,
794            "temporary files not available in this host",
795        ));
796    }
797
798    #[cfg(not(all(target_arch = "wasm32", target_os = "unknown")))]
799    {
800        let mut path = std::env::temp_dir().to_string_lossy().as_bytes().to_vec();
801        if path.last().copied() != Some(b'/') && path.last().copied() != Some(b'\\') {
802            path.push(b'/');
803        }
804        let unique = format!(
805            "lua_tmpfile_{}_{}",
806            std::process::id(),
807            std::time::SystemTime::now()
808                .duration_since(std::time::UNIX_EPOCH)
809                .map(|d| d.as_nanos())
810                .unwrap_or(0)
811        );
812        path.extend_from_slice(unique.as_bytes());
813        Ok(path)
814    }
815}
816
817/// `io.tmpfile()`. C: `io_tmpfile`.
818pub fn io_tmpfile(state: &mut LuaState) -> Result<usize, LuaError> {
819    let hook = state.global().file_open_hook;
820    let Some(open_fn) = hook else {
821        let os_err = io::Error::new(io::ErrorKind::Unsupported, "no filesystem hook registered");
822        return file_result(state, false, None, os_err);
823    };
824
825    let temp_name_hook = state.global().temp_name_hook;
826    let path = match temp_name_hook {
827        Some(temp_fn) => match temp_fn() {
828            Ok(path) => path,
829            Err(e) => {
830                let msg = match e.message_bytes() {
831                    Some(b) => String::from_utf8_lossy(b).into_owned(),
832                    None => format!("{:?}", &e),
833                };
834                return file_result(
835                    state,
836                    false,
837                    None,
838                    io::Error::new(io::ErrorKind::Unsupported, msg),
839                );
840            }
841        },
842        None => match native_temp_name() {
843            Ok(path) => path,
844            Err(e) => return file_result(state, false, None, e),
845        },
846    };
847
848    match open_fn(&path, b"w+b") {
849        Ok(fh) => {
850            let cell = new_file(state)?;
851            cell.borrow_mut().file = Some(fh);
852            Ok(1)
853        }
854        Err(e) => {
855            let os_err = io::Error::new(
856                io::ErrorKind::Other,
857                match e.message_bytes() {
858                    Some(b) => String::from_utf8_lossy(b).into_owned(),
859                    None => format!("{:?}", &e),
860                },
861            );
862            file_result(state, false, None, os_err)
863        }
864    }
865}
866
867// ── io.input / io.output ─────────────────────────────────────────────────────
868
869/// Generic setter/getter for `io.input` and `io.output`. C: `g_iofile`.
870fn g_iofile(state: &mut LuaState, key: &[u8], mode: &[u8]) -> Result<usize, LuaError> {
871    if !matches!(state.type_at(1), LuaType::None | LuaType::Nil) {
872        if state.type_at(1) == LuaType::String {
873            let filename = state.check_arg_string(1)?;
874            opencheck(state, &filename, mode)?;
875        } else {
876            let _ = tofile(state)?;
877            state.push_value_at(1)?;
878        }
879        state.registry_set(key)?;
880    }
881    state.registry_get(key)?;
882    Ok(1)
883}
884
885/// `io.input([file])`. C: `io_input`.
886pub fn io_input(state: &mut LuaState) -> Result<usize, LuaError> {
887    g_iofile(state, IO_INPUT_KEY, b"r")
888}
889
890/// `io.output([file])`. C: `io_output`.
891pub fn io_output(state: &mut LuaState) -> Result<usize, LuaError> {
892    g_iofile(state, IO_OUTPUT_KEY, b"w")
893}
894
895// ── Read helpers ─────────────────────────────────────────────────────────────
896
897/// Read a numeric literal from `file` into an owned byte buffer.
898///
899/// The decimal point is always `.`: the reference reads the locale's
900/// `decimal_point`, but omnilua is locale-independent, so `.` is the single
901/// source of truth (the same simplification the rest of the number path makes).
902fn read_number_bytes(file: &mut dyn LuaFileHandle) -> Vec<u8> {
903    let first = loop {
904        let b = file.read_byte();
905        if b == EOF_SENTINEL || !(b as u8).is_ascii_whitespace() {
906            break b;
907        }
908    };
909
910    let mut rn = ReadNumState::new(first);
911
912    rn.try2(file, [b'-', b'+']);
913
914    let mut count: usize = 0;
915    let hex = if rn.try2(file, [b'0', b'0']) {
916        if rn.try2(file, [b'x', b'X']) {
917            true
918        } else {
919            count = 1;
920            false
921        }
922    } else {
923        false
924    };
925
926    count += rn.read_digits(file, hex);
927
928    let dec_point = b'.';
929    if rn.try2(file, [dec_point, b'.']) {
930        count += rn.read_digits(file, hex);
931    }
932
933    if count > 0 {
934        let exp_chars = if hex { [b'p', b'P'] } else { [b'e', b'E'] };
935        if rn.try2(file, exp_chars) {
936            rn.try2(file, [b'-', b'+']);
937            rn.read_digits(file, false);
938        }
939    }
940
941    file.unread_byte(rn.current);
942    rn.as_bytes().to_vec()
943}
944
945/// Peek for EOF: returns `true` if more input is available. C: `test_eof`
946/// (the file-only half — caller still pushes `""` regardless).
947fn test_eof(file: &mut dyn LuaFileHandle) -> bool {
948    let c = file.read_byte();
949    if c != EOF_SENTINEL {
950        file.unread_byte(c);
951    }
952    c != EOF_SENTINEL
953}
954
955/// Read one line from `file` into an owned buffer. Returns `(bytes, had_content)`.
956/// If `chop` is true the trailing `\n` is stripped. C: `read_line(L, f, chop)`.
957///
958/// The bytes are accumulated in `LUAL_BUFFER_SIZE`-sized passes and the outer
959/// loop continues while a pass fills without hitting a newline or EOF — the
960/// chunked structure mirrors C's `luaL_prepbuffer` loop, though a growable `Vec`
961/// stands in for the fixed stack buffer.
962fn read_line(file: &mut dyn LuaFileHandle, chop: bool) -> (Vec<u8>, bool) {
963    let mut buf: Vec<u8> = Vec::new();
964    let mut c: i32;
965
966    'outer: loop {
967        for _ in 0..LUAL_BUFFER_SIZE {
968            c = file.read_byte();
969            if c == EOF_SENTINEL || c == b'\n' as i32 {
970                break 'outer;
971            }
972            buf.push(c as u8);
973        }
974    }
975
976    if !chop && c == b'\n' as i32 {
977        buf.push(b'\n');
978    }
979
980    let had_content = c == b'\n' as i32 || !buf.is_empty();
981    (buf, had_content)
982}
983
984/// Read the entire file into an owned buffer. C: `read_all(L, f)` (file-only half).
985///
986/// Perf: C `fread`s in bulk; this reads one byte at a time via
987/// `LuaFileHandle::read_byte`. A future `read_chunk(&mut [u8])` on the trait
988/// would let the host fill a buffer directly.
989fn read_all(file: &mut dyn LuaFileHandle) -> Vec<u8> {
990    let mut buf: Vec<u8> = Vec::new();
991    loop {
992        let mut chunk_read = 0usize;
993        for _ in 0..LUAL_BUFFER_SIZE {
994            let b = file.read_byte();
995            if b == EOF_SENTINEL {
996                break;
997            }
998            buf.push(b as u8);
999            chunk_read += 1;
1000        }
1001        if chunk_read < LUAL_BUFFER_SIZE {
1002            break;
1003        }
1004    }
1005    buf
1006}
1007
1008/// Read at most `n` bytes from `file`. Returns `(bytes, had_content)`.
1009fn read_chars(file: &mut dyn LuaFileHandle, n: usize) -> (Vec<u8>, bool) {
1010    let mut buf = Vec::with_capacity(n);
1011    for _ in 0..n {
1012        let b = file.read_byte();
1013        if b == EOF_SENTINEL {
1014            break;
1015        }
1016        buf.push(b as u8);
1017    }
1018    let nr = buf.len();
1019    (buf, nr > 0)
1020}
1021
1022/// A validated `file:read`/`io.read` string format: the canonical option byte
1023/// (`b'n'`/`b'l'`/`b'L'`/`b'a'`) after the leading `*` has been resolved.
1024#[derive(Clone, Copy, PartialEq, Eq)]
1025enum ReadFormat {
1026    Number,
1027    Line,
1028    LineWithEol,
1029    All,
1030}
1031
1032/// Whether the leading `*` on a read format is REQUIRED (5.1/5.2) or OPTIONAL
1033/// (5.3+). The `*` was a mandatory marker in 5.1/5.2; 5.3 kept it accepted for
1034/// compatibility but made it optional (`if (*p == '*') p++;` in `liolib.c`'s
1035/// `g_read`). Single source of truth for that seam — verified empirically
1036/// against the 5.1.5/5.2.4 vs 5.3.6/5.4.7/5.5.0 reference binaries.
1037fn read_format_requires_star(version: lua_types::LuaVersion) -> bool {
1038    matches!(
1039        version,
1040        lua_types::LuaVersion::V51 | lua_types::LuaVersion::V52
1041    )
1042}
1043
1044/// Whether the `L` (line-with-end-of-line) format exists. `L` was added in 5.2;
1045/// 5.1 has only `n`/`l`/`a`, so `*L` there is an invalid format. Single source of
1046/// truth — verified against the 5.1.5 reference (`*L` → "invalid format") vs
1047/// 5.2.4+ (`*L` reads the line including its newline).
1048fn read_format_has_line_with_eol(version: lua_types::LuaVersion) -> bool {
1049    version != lua_types::LuaVersion::V51
1050}
1051
1052/// Resolve a read-format string against the version seam, returning the
1053/// canonical [`ReadFormat`] or the exact `extramsg` the reference passes to
1054/// `luaL_argerror`.
1055///
1056/// The two reference wordings encode the seam: a leading char that is not a
1057/// valid format marker yields `"invalid option"`, while a recognised marker
1058/// followed by an unknown option yields `"invalid format"`.
1059///   * 5.1/5.2: the `*` is the required marker. No `*` ⇒ `"invalid option"`.
1060///     After `*`, an unknown/absent option char ⇒ `"invalid format"`; on 5.1
1061///     the `L` option does not exist, so `*L` ⇒ `"invalid format"`.
1062///   * 5.3+: the `*` is optional. The option char is read directly (with or
1063///     without a leading `*`); an unknown one ⇒ `"invalid format"`.
1064fn resolve_read_format(
1065    version: lua_types::LuaVersion,
1066    fmt: &[u8],
1067) -> Result<ReadFormat, &'static [u8]> {
1068    let option = if read_format_requires_star(version) {
1069        if fmt.first() != Some(&b'*') {
1070            return Err(b"invalid option");
1071        }
1072        fmt.get(1).copied()
1073    } else if fmt.first() == Some(&b'*') {
1074        fmt.get(1).copied()
1075    } else {
1076        fmt.first().copied()
1077    };
1078    match option {
1079        Some(b'n') => Ok(ReadFormat::Number),
1080        Some(b'l') => Ok(ReadFormat::Line),
1081        Some(b'L') if read_format_has_line_with_eol(version) => Ok(ReadFormat::LineWithEol),
1082        Some(b'a') => Ok(ReadFormat::All),
1083        _ => Err(b"invalid format"),
1084    }
1085}
1086
1087/// Dispatch one or more read formats; push results. C: `g_read(L, f, first)`.
1088///
1089/// Takes an `Rc<RefCell<LStream>>` so each I/O step can borrow the file briefly,
1090/// release the borrow, then push the result to `state`. This is the "collect
1091/// then borrow" pattern that resolves the `&mut state` vs `&mut file` conflict.
1092fn g_read(
1093    state: &mut LuaState,
1094    p_rc: &Rc<RefCell<LStream>>,
1095    first: i32,
1096) -> Result<usize, LuaError> {
1097    //
1098    // In C, `getiofile` leaves the default stream on the stack, so subtracting
1099    // one skips that extra value. This Rust port resolves registry streams into
1100    // an Rc and pops the registry value before reaching `g_read`, so count the
1101    // read formats directly from `first`.
1102    let nargs = (state.top() - first + 1).max(0);
1103    let mut n = first;
1104    let mut success = true;
1105
1106    {
1107        let mut p = p_rc.borrow_mut();
1108        let fh = p.file.as_mut().expect("open stream has no file handle");
1109        fh.clear_error();
1110    }
1111
1112    if nargs == 0 {
1113        let (bytes, had) = {
1114            let mut p = p_rc.borrow_mut();
1115            let fh = p
1116                .file
1117                .as_deref_mut()
1118                .expect("open stream has no file handle");
1119            read_line(fh, true)
1120        };
1121        state.push_string(&bytes)?;
1122        success = had;
1123        n = first + 1;
1124    } else {
1125        state.ensure_stack((nargs as i32) + 20, "too many arguments")?;
1126        let mut remaining = nargs;
1127        while remaining > 0 && success {
1128            if state.type_at(n) == LuaType::Number {
1129                let l = state.check_arg_integer(n)? as usize;
1130                if l == 0 {
1131                    let not_eof = {
1132                        let mut p = p_rc.borrow_mut();
1133                        let fh = p
1134                            .file
1135                            .as_deref_mut()
1136                            .expect("open stream has no file handle");
1137                        test_eof(fh)
1138                    };
1139                    state.push_string(b"")?;
1140                    success = not_eof;
1141                } else {
1142                    let (bytes, had) = {
1143                        let mut p = p_rc.borrow_mut();
1144                        let fh = p
1145                            .file
1146                            .as_deref_mut()
1147                            .expect("open stream has no file handle");
1148                        read_chars(fh, l)
1149                    };
1150                    state.push_string(&bytes)?;
1151                    success = had;
1152                }
1153            } else {
1154                let s: Vec<u8> = state.check_arg_string(n)?;
1155                let version = state.global().lua_version;
1156                let format = match resolve_read_format(version, &s) {
1157                    Ok(format) => format,
1158                    Err(extramsg) => {
1159                        return Err(lua_vm::debug::arg_error_impl(state, n, extramsg));
1160                    }
1161                };
1162                match format {
1163                    ReadFormat::Number => {
1164                        let bytes = {
1165                            let mut p = p_rc.borrow_mut();
1166                            let fh = p
1167                                .file
1168                                .as_deref_mut()
1169                                .expect("open stream has no file handle");
1170                            read_number_bytes(fh)
1171                        };
1172                        let pushed = state.string_to_number_push(&bytes)?;
1173                        if pushed != 0 {
1174                            success = true;
1175                        } else {
1176                            state.push(LuaValue::Nil);
1177                            success = false;
1178                        }
1179                    }
1180                    ReadFormat::Line => {
1181                        let (bytes, had) = {
1182                            let mut p = p_rc.borrow_mut();
1183                            let fh = p
1184                                .file
1185                                .as_deref_mut()
1186                                .expect("open stream has no file handle");
1187                            read_line(fh, true)
1188                        };
1189                        state.push_string(&bytes)?;
1190                        success = had;
1191                    }
1192                    ReadFormat::LineWithEol => {
1193                        let (bytes, had) = {
1194                            let mut p = p_rc.borrow_mut();
1195                            let fh = p
1196                                .file
1197                                .as_deref_mut()
1198                                .expect("open stream has no file handle");
1199                            read_line(fh, false)
1200                        };
1201                        state.push_string(&bytes)?;
1202                        success = had;
1203                    }
1204                    ReadFormat::All => {
1205                        let bytes = {
1206                            let mut p = p_rc.borrow_mut();
1207                            let fh = p
1208                                .file
1209                                .as_deref_mut()
1210                                .expect("open stream has no file handle");
1211                            read_all(fh)
1212                        };
1213                        state.push_string(&bytes)?;
1214                        success = true;
1215                    }
1216                }
1217            }
1218            n += 1;
1219            remaining -= 1;
1220        }
1221    }
1222
1223    let has_err = {
1224        let p = p_rc.borrow();
1225        match p.file.as_deref() {
1226            Some(fh) => fh.has_error(),
1227            None => false,
1228        }
1229    };
1230    if has_err {
1231        let err = {
1232            let p = p_rc.borrow();
1233            match p.file.as_deref().and_then(|fh| fh.last_error_info()) {
1234                Some((code, _msg)) if code != 0 => io::Error::from_raw_os_error(code),
1235                Some((_code, msg)) => io::Error::new(io::ErrorKind::Other, msg),
1236                None => io::Error::new(io::ErrorKind::Other, "file read error"),
1237            }
1238        };
1239        return file_result(state, false, None, err);
1240    }
1241
1242    if !success {
1243        state.pop_n(1);
1244        state.push(LuaValue::Nil);
1245    }
1246
1247    Ok((n - first) as usize)
1248}
1249
1250/// Resolve the registry-default I/O file (IO_INPUT / IO_OUTPUT) into its
1251/// backing `Rc<RefCell<LStream>>`. Errors if the slot holds a closed handle
1252/// or a value that is not a registered file userdata.
1253///
1254fn get_io_file_rc(state: &mut LuaState, key: &[u8]) -> Result<Rc<RefCell<LStream>>, LuaError> {
1255    state.registry_get(key)?;
1256    let ud_id = state
1257        .test_arg_userdata(-1, LUA_FILE_HANDLE)
1258        .map(|ud| ud.identity());
1259    state.pop_n(1);
1260    let label = &key[IO_PREFIX_LEN..];
1261    let id = ud_id.ok_or_else(|| {
1262        LuaError::runtime(format_args!(
1263            "default {} file is invalid",
1264            label.escape_ascii()
1265        ))
1266    })?;
1267    let rc = lookup_lstream(id).ok_or_else(|| {
1268        LuaError::runtime(format_args!(
1269            "default {} file is invalid",
1270            label.escape_ascii()
1271        ))
1272    })?;
1273    if rc.borrow().is_closed() {
1274        return Err(LuaError::runtime(format_args!(
1275            "default {} file is closed",
1276            label.escape_ascii()
1277        )));
1278    }
1279    Ok(rc)
1280}
1281
1282/// `io.read(...)`. C: `io_read`.
1283pub fn io_read(state: &mut LuaState) -> Result<usize, LuaError> {
1284    let p_rc = get_io_file_rc(state, IO_INPUT_KEY)?;
1285    g_read(state, &p_rc, 1)
1286}
1287
1288/// `file:read(...)`. C: `f_read`.
1289pub fn f_read(state: &mut LuaState) -> Result<usize, LuaError> {
1290    let p_rc = tofile(state)?;
1291    g_read(state, &p_rc, 2)
1292}
1293
1294// ── Write helpers ────────────────────────────────────────────────────────────
1295
1296/// Render a numeric `LuaValue` to its `io.write` byte form.
1297///
1298/// Reference `g_write` writes numbers with `lua_tostring` — the same
1299/// `tostringbuff` path as `print`/`tostring` — so this routes through the
1300/// shared, version-aware [`lua_vm::object::num_to_string`] to keep
1301/// `io.write(1.0)` byte-identical to `print(1.0)` on every version: `%.14g` on
1302/// 5.1-5.4 (no `.0` suffix under the float-only 5.1/5.2), shortest-round-trip
1303/// on 5.5.
1304fn num_to_write_bytes(state: &mut LuaState, val: &LuaValue) -> Result<Vec<u8>, LuaError> {
1305    let s = lua_vm::object::num_to_string(state, val)?;
1306    Ok(s.as_bytes().to_vec())
1307}
1308
1309/// `io.write(...)`. C: `io_write`.
1310///
1311/// Writes all arguments to the current default output file (`IO_OUTPUT`). When
1312/// a file was set via `io.output(filename)`, writes go to that file; otherwise
1313/// they go to stdout via `state.write_output()`.
1314///
1315/// The borrow split (needing both `&mut LuaState` and `&mut dyn LuaFileHandle`)
1316/// is resolved by collecting all formatted strings first and then writing them
1317/// to the file handle obtained from the `LSTREAM_REGISTRY`.
1318pub fn io_write(state: &mut LuaState) -> Result<usize, LuaError> {
1319    // Step 1: collect all formatted byte strings before touching the file handle.
1320    let n = state.top();
1321    let mut chunks: Vec<Vec<u8>> = Vec::with_capacity(n as usize);
1322    for i in 1..=(n as i32) {
1323        if state.type_at(i) == LuaType::Number {
1324            let val = state.value_at(i);
1325            chunks.push(num_to_write_bytes(state, &val)?);
1326        } else {
1327            let bytes: Vec<u8> = state.check_arg_string(i)?;
1328            chunks.push(bytes);
1329        }
1330    }
1331
1332    // Step 2: resolve the current output file. C's `getiofile` errors when
1333    // the default output is closed; do not silently fall back to stdout.
1334    let p_rc = get_io_file_rc(state, IO_OUTPUT_KEY)?;
1335    {
1336        let mut p = p_rc.borrow_mut();
1337        let fh = p.file.as_mut().expect("open stream has no file handle");
1338        for chunk in &chunks {
1339            fh.write_bytes(chunk)
1340                .map_err(|e| LuaError::runtime(format_args!("io.write: {}", e)))?;
1341        }
1342    }
1343    state.registry_get(IO_OUTPUT_KEY)?;
1344    Ok(1)
1345}
1346
1347/// `file:write(...)`. C: `f_write`.
1348pub fn f_write(state: &mut LuaState) -> Result<usize, LuaError> {
1349    let p_rc = tofile(state)?;
1350
1351    // Step 1: collect args 2..=n as owned byte chunks before borrowing the file.
1352    let n = state.top();
1353    let mut chunks: Vec<Vec<u8>> = Vec::with_capacity(n.saturating_sub(1) as usize);
1354    for i in 2..=(n as i32) {
1355        if state.type_at(i) == LuaType::Number {
1356            let val = state.value_at(i);
1357            chunks.push(num_to_write_bytes(state, &val)?);
1358        } else {
1359            let bytes: Vec<u8> = state.check_arg_string(i)?;
1360            chunks.push(bytes);
1361        }
1362    }
1363
1364    // Step 2: write through the file with the LStream borrow scoped tightly.
1365    let result: io::Result<()> = {
1366        let mut p = p_rc.borrow_mut();
1367        let fh = p.file.as_mut().expect("open stream has no file handle");
1368        let mut r: io::Result<()> = Ok(());
1369        for chunk in &chunks {
1370            match fh.write_bytes(chunk) {
1371                Ok(written) if written == chunk.len() => {}
1372                Ok(_) => {
1373                    r = Err(io::Error::new(io::ErrorKind::Other, "short write"));
1374                    break;
1375                }
1376                Err(e) => {
1377                    r = Err(e);
1378                    break;
1379                }
1380            }
1381        }
1382        r
1383    };
1384
1385    // Step 3: on success return the file handle (arg 1); on failure use file_result.
1386    match result {
1387        Ok(()) => {
1388            state.push_value_at(1)?;
1389            Ok(1)
1390        }
1391        Err(e) => file_result(state, false, None, e),
1392    }
1393}
1394
1395// ── Seek / setvbuf / flush ───────────────────────────────────────────────────
1396
1397/// `file:seek([whence [, offset]])`. C: `f_seek`.
1398pub fn f_seek(state: &mut LuaState) -> Result<usize, LuaError> {
1399    static MODE_NAMES: &[&[u8]] = &[b"set", b"cur", b"end"];
1400
1401    let p_rc = tofile(state)?;
1402    let op = state.check_arg_option(2, Some(b"cur"), MODE_NAMES)?;
1403    let p3: i64 = state.opt_arg_integer(3, 0)?;
1404
1405    let seek_pos = match op {
1406        0 => SeekFrom::Start(p3 as u64),
1407        1 => SeekFrom::Current(p3),
1408        2 => SeekFrom::End(p3),
1409        _ => unreachable!(),
1410    };
1411
1412    let result = {
1413        let mut p = p_rc.borrow_mut();
1414        let fh = p.file.as_mut().expect("open stream has no file handle");
1415        fh.seek(seek_pos)
1416    };
1417    match result {
1418        Ok(pos) => {
1419            state.push(LuaValue::Int(pos as i64));
1420            Ok(1)
1421        }
1422        Err(e) => file_result(state, false, None, e),
1423    }
1424}
1425
1426/// `file:setvbuf(mode [, size])`. C: `f_setvbuf`.
1427pub fn f_setvbuf(state: &mut LuaState) -> Result<usize, LuaError> {
1428    static MODE_NAMES: &[&[u8]] = &[b"no", b"full", b"line"];
1429
1430    let p_rc = tofile(state)?;
1431    let op = state.check_arg_option(2, None, MODE_NAMES)?;
1432    let sz: i64 = state.opt_arg_integer(3, LUAL_BUFFER_SIZE as i64)?;
1433    let mode = match op {
1434        0 => BufMode::No,
1435        1 => BufMode::Full,
1436        2 => BufMode::Line,
1437        _ => unreachable!(),
1438    };
1439    let result = {
1440        let mut p = p_rc.borrow_mut();
1441        let fh = p.file.as_mut().expect("open stream has no file handle");
1442        let mode_index = match mode {
1443            BufMode::No => 0,
1444            BufMode::Full => 1,
1445            BufMode::Line => 2,
1446        };
1447        fh.set_buf_mode(mode_index, sz.max(0) as usize)
1448    };
1449    match result {
1450        Ok(()) => file_result(state, true, None, io::Error::last_os_error()),
1451        Err(e) => file_result(state, false, None, e),
1452    }
1453}
1454
1455/// `io.flush()`. C: `io_flush`.
1456pub fn io_flush(state: &mut LuaState) -> Result<usize, LuaError> {
1457    let ud_id: Option<usize> = {
1458        state.registry_get(IO_OUTPUT_KEY)?;
1459        let id = state
1460            .test_arg_userdata(-1, LUA_FILE_HANDLE)
1461            .map(|ud| ud.identity());
1462        state.pop_n(1);
1463        id
1464    };
1465    if let Some(id) = ud_id {
1466        if let Some(rc) = lookup_lstream(id) {
1467            let result = {
1468                let mut p = rc.borrow_mut();
1469                if p.is_closed() {
1470                    return Err(LuaError::runtime(format_args!(
1471                        "default output file is closed"
1472                    )));
1473                }
1474                let fh = p
1475                    .file
1476                    .as_deref_mut()
1477                    .expect("open stream has no file handle");
1478                fh.flush()
1479            };
1480            return match result {
1481                Ok(()) => {
1482                    state.push(LuaValue::Bool(true));
1483                    Ok(1)
1484                }
1485                Err(e) => file_result(state, false, None, e),
1486            };
1487        }
1488    }
1489    // No live default output file: behave like a successful no-op flush of stdout.
1490    state.push(LuaValue::Bool(true));
1491    Ok(1)
1492}
1493
1494/// `file:flush()`. C: `f_flush`.
1495pub fn f_flush(state: &mut LuaState) -> Result<usize, LuaError> {
1496    let p_rc = tofile(state)?;
1497    let result = {
1498        let mut p = p_rc.borrow_mut();
1499        let fh = p.file.as_mut().expect("open stream has no file handle");
1500        fh.flush()
1501    };
1502    match result {
1503        Ok(()) => {
1504            state.push(LuaValue::Bool(true));
1505            Ok(1)
1506        }
1507        Err(e) => file_result(state, false, None, e),
1508    }
1509}
1510
1511// ── Lines iterator ───────────────────────────────────────────────────────────
1512
1513/// Build the `io_readline` closure with its upvalues and push it.
1514///
1515/// Upvalue layout (C comment):
1516///   1) file handle (first stack value)
1517///   2) number of read-format arguments
1518///   3) toclose flag (bool)
1519///   4..n+3) format arguments
1520fn aux_lines(state: &mut LuaState, toclose: bool) -> Result<(), LuaError> {
1521    // `lua_gettop` is the stack count RELATIVE to the current frame, not the
1522    // absolute `top_idx`; using `state.top()` mirrors that.
1523    let n = state.top() - 1;
1524    if n > MAX_ARG_LINE as i32 {
1525        return Err(lua_vm::debug::arg_error_impl(
1526            state,
1527            MAX_ARG_LINE as i32 + 2,
1528            b"too many arguments",
1529        ));
1530    }
1531    state.push_value_at(1)?;
1532    state.push(LuaValue::Int(n as i64));
1533    state.push(LuaValue::Bool(toclose));
1534    state.rotate(2, 3)?;
1535    state.push_c_closure(io_readline, (3 + n) as i32)?;
1536    Ok(())
1537}
1538
1539/// `file:lines(...)`. C: `f_lines`.
1540pub fn f_lines(state: &mut LuaState) -> Result<usize, LuaError> {
1541    let _ = tofile(state)?; // validates file is open
1542    aux_lines(state, false)?;
1543    Ok(1)
1544}
1545
1546/// `io.lines([filename, ...])`. C: `io_lines`.
1547pub fn io_lines(state: &mut LuaState) -> Result<usize, LuaError> {
1548    if state.type_at(1) == LuaType::None {
1549        state.push(LuaValue::Nil);
1550    }
1551    let toclose = if state.type_at(1) == LuaType::Nil {
1552        state.registry_get(IO_INPUT_KEY)?;
1553        state.replace(1)?;
1554        let _ = tofile(state)?;
1555        false
1556    } else {
1557        let filename = state.check_arg_string(1)?;
1558        opencheck(state, &filename, b"r")?;
1559        state.replace(1)?;
1560        true
1561    };
1562
1563    aux_lines(state, toclose)?;
1564
1565    if toclose && state.global().lua_version.lines_returns_to_be_closed() {
1566        state.push(LuaValue::Nil); // state
1567        state.push(LuaValue::Nil); // control
1568        state.push_value_at(1)?; // file as to-be-closed variable (4th result)
1569        Ok(4)
1570    } else {
1571        Ok(1)
1572    }
1573}
1574
1575/// Iteration function created by `aux_lines`. C: `io_readline`.
1576///
1577/// Upvalue layout matches what `aux_lines` creates:
1578///   upvalue 1: file handle (userdata)
1579///   upvalue 2: n (number of read-format args)
1580///   upvalue 3: toclose flag
1581///   upvalue 4..n+3: format arguments
1582fn io_readline(state: &mut LuaState) -> Result<usize, LuaError> {
1583    let n = match state.value_at(crate::state_stub::upvalue_index(2)) {
1584        LuaValue::Int(i) => i as usize,
1585        _ => 0,
1586    };
1587
1588    let p_rc = lstream_from_upvalue(state, 1)?;
1589
1590    if p_rc.borrow().is_closed() {
1591        return Err(LuaError::runtime(format_args!("file is already closed")));
1592    }
1593
1594    lua_vm::api::set_top(state, 1)?;
1595    state.ensure_stack(n as i32, "too many arguments")?;
1596
1597    for i in 1..=n {
1598        let uv = state.value_at(crate::state_stub::upvalue_index(3 + i as i32));
1599        state.push(uv);
1600    }
1601
1602    let result_n: usize = g_read(state, &p_rc, 2)?;
1603
1604    debug_assert!(result_n > 0, "g_read should return at least one value");
1605
1606    let top = state.top_idx().get() as i32;
1607    let first_result_idx = top - result_n as i32;
1608    let first_truthy = !matches!(
1609        state.stack_at(first_result_idx),
1610        LuaValue::Nil | LuaValue::Bool(false)
1611    );
1612    if first_truthy {
1613        return Ok(result_n);
1614    }
1615
1616    if result_n > 1 {
1617        let err_val = state.stack_at(first_result_idx + 1).clone();
1618        return Err(LuaError::from_value(err_val));
1619    }
1620
1621    let toclose = !matches!(
1622        state.value_at(crate::state_stub::upvalue_index(3)),
1623        LuaValue::Nil | LuaValue::Bool(false)
1624    );
1625    if toclose {
1626        lua_vm::api::set_top(state, 0)?;
1627        state.push_upvalue(1)?;
1628        aux_close(state)?;
1629    }
1630
1631    Ok(0)
1632}
1633
1634// ── Module registration ──────────────────────────────────────────────────────
1635
1636/// Create the file-handle metatable in the registry. C: `createmeta(L)`.
1637fn create_meta(state: &mut LuaState) -> Result<(), LuaError> {
1638    state.new_metatable(LUA_FILE_HANDLE)?;
1639    state.set_funcs(FILE_METAMETHODS, 0)?;
1640    state.new_lib_table(FILE_METHODS)?;
1641    state.set_funcs(FILE_METHODS, 0)?;
1642    state.set_field(-2, b"__index")?;
1643    state.pop_n(1);
1644    Ok(())
1645}
1646
1647/// Register stdin, stdout, or stderr as a Lua file handle. C: `createstdfile`.
1648fn create_std_file(
1649    state: &mut LuaState,
1650    std_kind: StdFileKind,
1651    registry_key: Option<&[u8]>,
1652    field_name: &[u8],
1653) -> Result<(), LuaError> {
1654    let cell = new_pre_file(state)?;
1655    let output_hook = match std_kind {
1656        StdFileKind::Stdout => state.global().stdout_hook,
1657        StdFileKind::Stderr => state.global().stderr_hook,
1658        StdFileKind::Stdin => None,
1659    };
1660    let input_hook = match std_kind {
1661        StdFileKind::Stdin => state.global().stdin_hook,
1662        StdFileKind::Stdout | StdFileKind::Stderr => None,
1663    };
1664    {
1665        let mut p = cell.borrow_mut();
1666        p.file = Some(Box::new(StdStreamHandle::new(
1667            std_kind,
1668            input_hook,
1669            output_hook,
1670        )));
1671        p.close_fn = Some(io_noclose);
1672    }
1673    if let Some(key) = registry_key {
1674        state.push_value_at(-1)?;
1675        state.registry_set(key)?;
1676    }
1677    state.set_field(-2, field_name)?;
1678    Ok(())
1679}
1680
1681/// Open the `io` library and return 1 (the library table). C: `luaopen_io`.
1682pub fn luaopen_io(state: &mut LuaState) -> Result<usize, LuaError> {
1683    state.new_lib(IO_LIB)?;
1684    create_meta(state)?;
1685    create_std_file(state, StdFileKind::Stdin, Some(IO_INPUT_KEY), b"stdin")?;
1686    create_std_file(state, StdFileKind::Stdout, Some(IO_OUTPUT_KEY), b"stdout")?;
1687    create_std_file(state, StdFileKind::Stderr, None, b"stderr")?;
1688    Ok(1)
1689}