Skip to main content

lua_types/
filehandle.rs

1//! Minimal file-handle abstraction shared between `lua-vm` (hook type) and
2//! `lua-stdlib` (io library).
3//!
4//! `std::fs` is banned in `lua-stdlib` by PORTING.md ยง1. The concrete
5//! implementation (backed by `std::fs::File`) lives in `lua-cli` and is
6//! installed on [`lua_vm::state::GlobalState`] via the `FileOpenHook` /
7//! `FileRemoveHook` / `FileRenameHook` function pointers. Those hooks return
8//! `std::io::Result` (not `LuaError`): only `std::io::Error` carries
9//! `raw_os_error()`, and `io.open`/`os.remove`/`os.rename` must report the real
10//! errno as their third return value the way C's `luaL_fileresult` does โ€” a
11//! `LuaError` return would drop it (#301). This trait is the shared seam that
12//! lets `lua-stdlib` program against file handles without importing `std::fs`.
13//!
14//! On the `wasm32-unknown-unknown` host boundary there is no `io::Error` to pass
15//! by value, so the `open_file` host import encodes the outcome in its `i32`
16//! return: `>= 0` is a live handle id, `-1` is failure with no errno available
17//! (mapped to a `raw_os_error`-less error โ†’ a 2-value `(nil, msg)` result), and
18//! `<= -2` encodes `errno = -(id + 1)` (mapped via
19//! `io::Error::from_raw_os_error`); the one-unit shift keeps `-1` as the
20//! exclusive no-errno sentinel so EPERM (errno 1) stays representable (#305).
21//! See `lua-wasm`'s `imported_file_open` and the JS host's `openFile`.
22//!
23//! ## Trait design
24//! The trait mirrors the subset of `LuaFileOps` (defined in `lua-stdlib`) that
25//! is required to run the built-in io library at the level needed for
26//! `attrib.lua`-class tests: sequential write, byte-by-byte read, flush, and
27//! seek. `LuaFileOps` in `lua-stdlib` extends this trait so that a single
28//! concrete type (the `FsFile` in `lua-cli`) satisfies both.
29
30use std::io::{self, SeekFrom};
31
32/// Capabilities required by the io library from an OS file handle.
33///
34/// Designed to be object-safe (`Box<dyn LuaFileHandle>`). Implementations
35/// backed by `std::fs::File` live in `lua-cli`; implementations for the
36/// standard streams live in `lua-stdlib/src/io_lib.rs`.
37pub trait LuaFileHandle: Send {
38    /// Read one byte from the handle; return it as `i32`, or `-1` on EOF/error.
39    fn read_byte(&mut self) -> i32;
40
41    /// Push back a previously-read byte so the next `read_byte` returns it.
42    fn unread_byte(&mut self, byte: i32);
43
44    /// Write a byte slice; return the number of bytes actually written.
45    fn write_bytes(&mut self, data: &[u8]) -> io::Result<usize>;
46
47    /// Flush any write buffers to the underlying OS handle.
48    fn flush(&mut self) -> io::Result<()>;
49
50    /// Seek within the file.
51    fn seek(&mut self, pos: SeekFrom) -> io::Result<u64>;
52
53    /// Return the current file position without moving it.
54    fn tell(&mut self) -> io::Result<u64>;
55
56    /// Clear the error/EOF flag on the handle.
57    fn clear_error(&mut self);
58
59    /// Return `true` if the handle has a pending error.
60    fn has_error(&self) -> bool;
61
62    /// Return the last pending OS error as `(errno, message)` when available.
63    ///
64    /// Some real Lua programs probe platform behavior through specific errno
65    /// values. LuaRocks' macOS directory detection, for example, expects
66    /// reading an opened directory to report `EISDIR` rather than look like EOF.
67    fn last_error_info(&self) -> Option<(i32, String)> {
68        None
69    }
70
71    /// Control write buffering. Mode values mirror `file:setvbuf` option order:
72    /// 0 = no buffering, 1 = full buffering, 2 = line buffering.
73    fn set_buf_mode(&mut self, _mode: i32, _size: usize) -> io::Result<()> {
74        Ok(())
75    }
76}