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}. - Seek
Whence - Seek anchor for
file:seek. C:{SEEK_SET, SEEK_CUR, SEEK_END}. - StdFile
Kind - 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§
- LuaFile
Ops - 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", orfalse. C:io_type.- io_
write io.write(...). C:io_write.- luaopen_
io - Open the
iolibrary and return 1 (the library table). C:luaopen_io.