Expand description
Global State — port of lstate.c (445 lines, 25 functions) + lstate.h (merged).
Manages per-thread (LuaState) and process-wide (GlobalState) Lua state:
creation, initialization, teardown, and coroutine lifecycle helpers.
The lstate.h header is merged into this module per PORTING.md §1.
§C source files
reference/lua-5.4.7/src/lstate.c(445 lines, 25 functions)reference/lua-5.4.7/src/lstate.h(408 lines; struct + macro definitions merged)
Structs§
- Call
Info - Saved state for a Lua or C call frame.
- Call
Info Extra - Payload of
CallInfo.u2. - Call
Info Idx - Index into the call-info stack.
- DynLib
Id - Opaque handle to a dynamically loaded library, allocated by a
DynLibLoadHookbackend and stored inpackage._CLIBS. - External
Root Key - Stable key for a value pinned in
ExternalRootSet. - External
Root Set - Values held alive by external Rust handles.
- GcHandle
- A short-lived handle returned by
state.gc()for GC operations. - GcRef
- A GC-managed pointer to a Lua collectable object. Newtype over
lua_gc::Gc<T>so callers preservegc.0-shape access while the backend swaps under them. - Global
State - Process-wide state shared by all Lua threads.
- LuaClosureC
- LuaClosure
Lua - LuaProto
- LuaState
- Per-thread Lua execution state.
- LuaString
- LuaTable
- A Lua table: hybrid array + hash map.
- LuaUser
Data - OsExecute
Result - Result returned by
OsExecuteHook, carrying the three values that C-Lua’sluaL_execresultpushes:(boolean|nil, "exit"|"signal", int). - Stack
Idx - Index into the Lua value stack. Never a pointer or borrow. Stack reallocates; only indices are stable across mutations.
- Stack
IdxConv - Internal: a thin wrapper used so stubbed methods can accept either
StackIdxoru32(Phase A code mixes both). Phase B will normalise. - Stack
Value - One slot on the Lua value stack. Wraps a
LuaValueand an optional to-be-closed delta (for thetbclistmechanism). - Thread
Registry Entry - One row of
GlobalState::threads. Pairs the per-threadLuaStatewith the canonicalGcRef<LuaThread>so everypush_threadfor the same id shares pointer-identity. Phase E-1 adds this; Phase E-2 extends it with interior-mutability bookkeeping whenresume/yieldneed to mutate the child thread while the parent holds a borrow. - UpVal
- A closure upvalue. Open upvalues point at a slot on a thread’s stack (referred to by index, since the stack reallocates). Closed upvalues own the value.
Enums§
- Call
Info Frame - Payload of
CallInfo.u. - Dynamic
Symbol - Resolved dynamic-library symbol.
- F2Imod
- Float-to-integer rounding mode (matches C-Lua’s F2Imod).
- GcKind
- Garbage collector operating mode.
- LuaCallable
- LuaClosure
- LuaError
- The Lua error type. Carries a
LuaValuepayload because Lua errors can be any value (typically a string). - LuaStatus
- Thread / call status codes.
- LuaValue
- The dynamically-typed Lua value. Replaces C’s
TValue. - OsExecute
Reason - Reason a shell command terminated, returned by
OsExecuteHook. - UpVal
State - Discriminator state for an upvalue: either still pointing at a thread’s stack slot, or owning the value after close.
Traits§
- Collectable
- Marker trait for GC-managed objects.
- LuaClosure
Ext LuaClosureaccessor —nupvalues()reports the upvalue count uniformly.- LuaL
Closure RefExt - LuaProto
Ext LuaProtosource bytes accessor.- LuaString
RefExt - LuaTable
RefExt GcRef<LuaTable>/GcRef<LuaUserData>field-access helpers. These methods are needed by api.rs and tagmethods.rs but the lua-types placeholders don’t yet expose them. TODO(phase-b): replace with real accessor methods on the canonical types in lua-types.- LuaType
Ext - Extension methods on
lua_types::LuaType. - LuaUser
Data RefExt - LuaValue
Ext - Extension methods on
LuaValue. TODO(phase-b): move these tolua_types::value(or wherever the canonical impl lives) once the type helpers stabilise. - Stack
IdxExt - StackIdx checked-arithmetic helpers. Returns the raw
u32because Phase A callers use the result in arithmetic comparisons against otheru32quantities (stack-distance offsets). - TmIndex
- Discriminant-to-index conversion for the two parallel
TagMethodenums.
Functions§
- close
- Close the Lua state and free all resources.
- close_
thread - Close a coroutine thread from the perspective of another thread.
- inc_
c_ stack - Increment the C-call depth counter, checking for overflow.
- new_
state - Create a new independent Lua state. Returns
Noneonly on OOM. - new_
thread - Create a new coroutine thread sharing the same GlobalState as the caller.
- reset_
thread - Reset a thread to its base state, closing all to-be-closed variables.
- reset_
thread_ api - Deprecated wrapper for
close_thread(L, NULL). - set_
c_ stack_ limit - Deprecated no-op that returns
LUAI_MAXCCALLS. - stack_
idx_ to_ i32 - Phase-A code casts
StackIdx as i32; provide aFromso it compiles. TODO(phase-b): expressions likestate.top_idx().0 as i32should becomestate.top_idx().raw() as i32. The non-primitive-cast error is silenced here by promoting the StackIdx through a free-function conversion.
Type Aliases§
- CpuClock
Hook - Function-pointer signature for retrieving program CPU time in seconds.
- DynLib
Load Hook - Function-pointer signature for loading a dynamic library, installed on
GlobalState::dynlib_load_hookby the embedder. - DynLib
Symbol Hook - Function-pointer signature for resolving a symbol in a previously loaded
dynamic library, installed on
GlobalState::dynlib_symbol_hook. - DynLib
Unload Hook - Function-pointer signature for unloading a dynamic library, installed on
GlobalState::dynlib_unload_hook. - Entropy
Hook - Function-pointer signature for host entropy used by default PRNG seeds and table-sort pivot randomisation. Hosts without entropy may leave it unset; the stdlib then uses deterministic fallback values instead of touching OS stubs.
- EnvHook
- Function-pointer signature for reading a host environment variable.
- File
Loader Hook - Function-pointer signature for reading a file’s full contents into memory,
installed on
GlobalState::file_loader_hookby the embedder. - File
Open Hook - Function-pointer signature for opening a file handle, installed on
GlobalState::file_open_hookby the embedder. - File
Remove Hook - Function-pointer signature for removing a file, installed on
GlobalState::file_remove_hookby the embedder. - File
Rename Hook - Function-pointer signature for renaming a file, installed on
GlobalState::file_rename_hookby the embedder. - Input
Hook - Function-pointer signature for reading bytes from a host-provided input
stream, installed on
GlobalState::stdin_hookby the embedder. - LuaC
FnPtr - Opaque registry index into
GlobalState.c_functions, where the reallua_CFunction(fn(&mut LuaState) -> Result<usize, LuaError>) is stored. Lua-types can’t referenceLuaStatewithout a circular dep, so we keep the closure variant type-erased here and resolve through the registry at call time. - LuaC
Function - A Lua-callable function pointer. C:
lua_CFunction. - LuaK
Function - Continuation function for yieldable C calls. C:
lua_KFunction. - LuaRust
Function - OsExecute
Hook - Function-pointer signature for executing a shell command, installed on
GlobalState::os_execute_hookby the embedder. - Output
Hook - Function-pointer signature for writing bytes to a host-provided output
stream, installed on
GlobalState::stdout_hookorGlobalState::stderr_hookby the embedder. - Parser
Hook - Function-pointer signature for the text-source parser, installed on
GlobalState::parser_hookby the embedder. - Popen
Hook - Function-pointer signature for spawning a child process with a connected
pipe, installed on
GlobalState::popen_hookby the embedder. - Temp
Name Hook - Function-pointer signature for generating a host temporary filename.
- Unix
Time Hook - Function-pointer signature for retrieving the current Unix time in seconds.