Skip to main content

lua_stdlib/
io_lib.rs

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