Expand description
Auxiliary library: helper functions for building Lua libraries.
C source: reference/lua-5.4.7/src/lauxlib.c (1127 lines, ~50 functions)
Target crate: lua-stdlib
This module provides the high-level luaL_* API layer that sits on top of
the raw lua_* C API. In Rust we translate each luaL_* function as a
free function receiving &mut LuaState rather than a method, matching the
structure of the other stdlib modules.
PORT NOTE: The C buffer system (luaL_Buffer) uses a small inline initial
buffer backed by a Lua-stack userdata box on overflow. In Rust we replace
this with a plain Vec<u8> (LuaBuffer), dropping all the C-internal
UBox / resizebox / boxgc / boxmt / newbox / buffonstack
machinery. The public interface remains compatible.
PORT NOTE: File-loading functions (load_filex) use the embedder-installed
GlobalState::file_loader_hook; concrete filesystem access belongs in
lua-cli or another host backend.
Structs§
- LuaBuffer
- Growable byte-buffer used by the auxiliary library for building strings.
- LuaReg
- A function-registration entry for
set_funcs. - LuaStream
- File-stream handle used by the IO library.
Constants§
- LUA_
ERRFILE - Extended error code: file-related I/O error from
load_filex. - LUA_
FILE_ HANDLE - Metatable name / file-handle key for the IO library.
- LUA_
GNAME - Name of the global environment table.
- LUA_
LOADED_ TABLE - Registry key for the table of loaded modules.
- LUA_
NOREF - Pseudo-reference meaning “no reference” (never created by
lua_ref). - LUA_
PRELOAD_ TABLE - Registry key for the table of preloaded loaders.
- LUA_
REFNIL - Pseudo-reference returned by
lua_refwhen the pushed value wasnil.
Functions§
- add_
char - Append a single byte to
buf. - add_
gsub - Perform global byte-string substitution: replace all occurrences of
patwithreplins, appending results intobuf. - add_
lstring - Append
stobuf. - add_
size - Append
szto the length counter (used after writing directly into the buffer). - add_
value - Pop the string at top of
state’s stack and append it tobuf. - arg_
error - Push an error for argument
argwith extra messageextramsg. Attempts to enrich the message with the calling function’s name. Always returnsErr. - buf_
init - Initialize
bufand associate it withstate. Pushes a placeholder light-userdata ontostateto anchor the buffer in C. In Rust the Vec is self-contained; we still push a placeholder for stack-slot compatibility with code that later callsadd_value/push_result. - buf_
init_ size - Initialize
buf, reserveszbytes, and return the writable region. - call_
meta - Call the metafield
eventofobjwithobjas argument, pushing one result. Returnstrueif the meta-method existed and was called. - check_
any - Assert that a value (not
none) is present atarg. - check_
integer - Return the integer at
argasi64; raise if not an integer-convertible number. - check_
lstring - Return the string at
argas bytes; raise a type error if not a string. - check_
number - Return the number at
argasf64; raise a type error if not a number. - check_
option - Check that
argis one of the strings inlstand return its index. IfdefisSomeit is used as default whenargis absent/nil. - check_
stack - Ensure the stack has at least
spaceextra slots; raise on failure. - check_
type - Assert that the value at
arghas Lua typet; raise type error otherwise. - check_
udata - Like
test_udatabut raises a type error if the check fails. - check_
version - Version-compatibility check: error if numeric type sizes or version mismatch.
- exec_
result - Push the result of a process-exit status onto the stack. Returns 3 values: success-bool-or-nil, exit-kind string, status code.
- file_
result - Push the result of a POSIX-style file operation onto the stack.
On success pushes
true; on failure pushesnil, errmsg, errno. Returns the number of pushed values. - get_
metafield - Push the metafield
eventofobjonto the stack and return its type. If there is no metafield, nothing is pushed andLuaType::Nilis returned. - get_
metatable - Push
registry[tname]and return its type. - get_
subtable - Ensure
state[idx][fname]is a table; push it. Returnstrueif the table already existed,falseif newly created. - gsub
- Build a string from
sby replacingpatwithrepl, push it on the stack, and return the bytes of the pushed string. - load_
buffer - Load a buffer as a Lua chunk (no mode argument).
- load_
bufferx - Load a buffer as a Lua chunk.
- load_
filex - Load a file as a Lua chunk. Returns
LUA_OKon success or an error code. - load_
string - Load a NUL-terminated byte-string as a Lua chunk.
- lua_
error - Format a runtime error with source location and raise it.
Always returns
Err. - lua_len
- Return the length of the value at
idxas ai64, raising an error if the length is not an integer. - lua_ref
- Store the value at the top of the stack in table
tand return a unique integer reference. If the value isnil, returnsLUA_REFNILwithout modifying the table. - lua_
unref - Release reference
reffrom tablet, adding it to the free list. - new_
metatable - Create a new metatable for type
tnameand register it in the registry. Returnstrue(and leaves new metatable on stack) if the table was created; returnsfalse(and leaves existing table on stack) if already existed. - new_
state - Create a new
LuaStatewith the default allocator, a panic handler, and warnings disabled. - opt_
integer - Return the integer at
arg; if absent/nil returndef. - opt_
lstring - Return the string at
arg; if absent/nil returndef. - opt_
number - Return the number at
arg; if absent/nil returndef. - prep_
buff_ size - Ensure at least
szfree bytes are available inbuf. - push_
result - Push the buffer contents as a Lua string onto
state’s stack. - push_
result_ size - Add
szbytes to the buffer count then callpush_result. - push_
where - Push a string describing the location of the call at
levelonto the stack. If no location is available, pushes an empty string. - requiref
- Simplified
require: open modulemodnameviaopenf, register it inpackage.loaded, and (ifglb) in the global table. Leaves the module on top of the stack. - set_
funcs - Register the functions in
linto the table at-(nup + 1), giving each closure thenupupvalues currently at the top of the stack. - set_
metatable - Set the metatable of the value at stack top to the one registered as
tname. - test_
udata - Check whether the value at
udis a full userdata with metatabletname. ReturnsSome(userdata)if yes,Noneotherwise. - to_
lua_ string - Convert the value at
idxto a byte-string representation (using__tostringif available) and push it onto the stack. - traceback
- Build a stack traceback string from thread
otherstarting atlevel. Ifmsgis non-None it is prepended on its own line. Leaves the result string on top ofstate. - type_
error_ arg - Push a type-mismatch error for argument
arg, statingtnamewas expected. Always returnsErr.