Skip to main content

Module io_lib

Module io_lib 

Source
Expand description

Standard I/O library — io.* functions and file:* methods.

C source: src/liolib.c (841 lines, ~35 functions).

PORT NOTE: Filesystem and process access is host-provided. Regular files use GlobalState::file_open_hook, io.popen uses GlobalState::popen_hook, and stdout/stderr use output hooks when installed. The native CLI provides hooks backed by std::fs, std::process, and std::io; sandboxed and WASM hosts can leave those capabilities absent.

PORT NOTE: Rust’s borrow checker prevents holding &mut dyn LuaFileOps (extracted from userdata) and &mut LuaState simultaneously. The affected functions (io_read, f_read, io_write, f_write, io_flush, f_flush, f_seek, f_setvbuf, get_io_file) are marked with TODO(port): borrow split. Phase B must restructure g_read/g_write to take a StackIdx rather than a raw &mut dyn LuaFileOps, and use RefCell inside LStream for interior mutability, or extract the file handle via a separate borrow scope.

Structs§

LStream
Lua file handle stored as the typed payload of a LuaUserData.

Enums§

BufMode
Buffering mode for file:setvbuf. C: {_IONBF, _IOFBF, _IOLBF}.
SeekWhence
Seek anchor for file:seek. C: {SEEK_SET, SEEK_CUR, SEEK_END}.
StdFileKind
Which standard stream to wrap in create_std_file.

Constants§

FILE_METAMETHODS
File-handle metamethods. C: static const luaL_Reg metameth[].
FILE_METHODS
file:* instance methods. C: static const luaL_Reg meth[].
IO_LIB
io.* module functions. C: static const luaL_Reg iolib[].
LUA_FILE_HANDLE
Name of the file-handle metatable in the Lua registry. C: LUA_FILEHANDLE.

Traits§

LuaFileOps
Capabilities required by the io library from an OS file handle.

Functions§

f_flush
file:flush(). C: f_flush.
f_lines
file:lines(...). C: f_lines.
f_read
file:read(...). C: f_read.
f_seek
file:seek([whence [, offset]]). C: f_seek.
f_setvbuf
file:setvbuf(mode [, size]). C: f_setvbuf.
f_write
file:write(...). C: f_write.
io_close
io.close([file]). C: io_close.
io_flush
io.flush(). C: io_flush.
io_input
io.input([file]). C: io_input.
io_lines
io.lines([filename, ...]). C: io_lines.
io_open
io.open(filename [, mode]). C: io_open.
io_output
io.output([file]). C: io_output.
io_popen
io.popen(filename [, mode]). C: io_popen.
io_read
io.read(...). C: io_read.
io_tmpfile
io.tmpfile(). C: io_tmpfile.
io_type
io.type(x) — return "file", "closed file", or false. C: io_type.
io_write
io.write(...). C: io_write.
luaopen_io
Open the io library and return 1 (the library table). C: luaopen_io.