Skip to main content

FileOpenHook

Type Alias FileOpenHook 

Source
pub type FileOpenHook = fn(filename: &[u8], mode: &[u8]) -> Result<Box<dyn LuaFileHandle>>;
Expand description

Function-pointer signature for opening a file handle, installed on GlobalState::file_open_hook by the embedder.

std::fs is banned outside lua-cli, so lua-stdlib’s io library reaches the filesystem via this hook. None causes io.open and io.output(name) to return a “file system not available” error, which is appropriate for sandboxed embeddings.

mode is a Lua fopen-style mode string (e.g. b"r", b"w", b"a", b"r+", etc.). The hook must honour at least r, w, and a.

The error type is std::io::Error, not LuaError: io::Error is the only type in this boundary that can carry raw_os_error() end to end. lua-stdlib’s io.open/io.input/io.output read the errno straight off this error and render the message with strerror-shaped text, matching C-Lua’s luaL_fileresult/luaL_error(..., strerror(errno)). Converting to LuaError here (as earlier revisions did) discards raw_os_error(), which was issue #301 (errno 0 + a verbose Rust Display message reaching Lua).