pub struct LuaState {Show 20 fields
pub status: u8,
pub allowhook: bool,
pub nci: u32,
pub top: StackIdx,
pub stack_last: StackIdx,
pub stack: Vec<StackValue>,
pub ci: CallInfoIdx,
pub call_info: Vec<CallInfo>,
pub openupval: Vec<GcRef<UpVal>>,
pub tbclist: Vec<StackIdx>,
pub hook: Option<Box<dyn FnMut(&mut LuaState, &LuaDebug)>>,
pub hookmask: u8,
pub basehookcount: i32,
pub hookcount: i32,
pub errfunc: isize,
pub n_ccalls: u32,
pub oldpc: u32,
pub marked: u8,
pub cached_thread_id: u64,
pub gc_check_needed: bool,
/* private fields */
}Expand description
Per-thread Lua execution state.
types.tsv: lua_State → LuaState
All stack-pointer fields in C (StkIdRel, StkId) become StackIdx (u32
index into stack: Vec<StackValue>). The C intrusive CallInfo linked list
becomes call_info: Vec<CallInfo> indexed by CallInfoIdx.
Fields§
§status: u8§allowhook: bool§nci: u32§top: StackIdx§stack_last: StackIdx§stack: Vec<StackValue>§ci: CallInfoIdx§call_info: Vec<CallInfo>§openupval: Vec<GcRef<UpVal>>§tbclist: Vec<StackIdx>§hook: Option<Box<dyn FnMut(&mut LuaState, &LuaDebug)>>§hookmask: u8§basehookcount: i32§hookcount: i32§errfunc: isize§n_ccalls: u32§oldpc: u32§marked: u8§cached_thread_id: u64Owner thread id for this LuaState, cached as a plain u64 so the
hot path of upvalue_get can compare against an open upvalue’s
thread_id without taking a RefCell::borrow on the shared
GlobalState.
Invariant: while this LuaState is the actively running thread,
GlobalState::current_thread_id == self.cached_thread_id. This is
maintained structurally by new_state/new_thread (which set
cached_thread_id to the thread’s own id once at construction)
combined with the coroutine resume protocol: coro_lib::resume
writes co_state.global.current_thread_id = co_id before the
coroutine runs, and restores parent_thread_id on yield/return.
Because each thread caches its own id (not the global’s id), the
invariant survives every context switch without an explicit refresh
at the resume site.
gc_check_needed: boolLocal GC gate.
Avoids borrowing GlobalState on every call edge when GC/finalizers
are not currently due.
Implementations§
Source§impl LuaState
Inherent push_copy so the LuaStateStubExt::push_copy default
todo!() no longer fires. Phase-A state.push_copy(idx) call-sites
(base.rs, etc.) duplicate the value at idx onto the top of the stack —
the same semantics as lua_pushvalue.
impl LuaState
Inherent push_copy so the LuaStateStubExt::push_copy default
todo!() no longer fires. Phase-A state.push_copy(idx) call-sites
(base.rs, etc.) duplicate the value at idx onto the top of the stack —
the same semantics as lua_pushvalue.
pub fn push_copy(&mut self, idx: i32) -> Result<(), LuaError>
pub fn push_value_at(&mut self, idx: i32) -> Result<(), LuaError>
pub fn insert(&mut self, idx: i32) -> Result<(), LuaError>
Sourcepub fn length_at(&mut self, idx: i32) -> Result<i64, LuaError>
pub fn length_at(&mut self, idx: i32) -> Result<i64, LuaError>
Inherent length_at mirroring luaL_len from lauxlib.c: push the
value’s length onto the stack (honouring __len), pop it as an
integer, and error if the result is not an integer. Defined on
LuaState so it overrides the LuaStateStubExt::length_at trait
default todo!().
Sourcepub fn write_output(&mut self, msg: &[u8]) -> Result<(), LuaError>
pub fn write_output(&mut self, msg: &[u8]) -> Result<(), LuaError>
Write msg bytes verbatim to standard output. Mirrors the C macro
lua_writestring(s, l) = fwrite(s, 1, l, stdout) from lauxlib.h,
used by print and friends. A failed write is propagated as a
LuaError::runtime; this matches C-Lua’s behaviour where an I/O
error during lua_writestring would surface through the host’s
error handling.
Sourcepub fn to_display_string(&mut self, idx: i32) -> Result<Vec<u8>, LuaError>
pub fn to_display_string(&mut self, idx: i32) -> Result<Vec<u8>, LuaError>
Convert the value at idx to a display string, push the result onto
the stack, and return a copy of its bytes. Mirrors luaL_tolstring
from lauxlib.c. The default Lua formatting is used for primitives
("true"/"false"/"nil", %I integers, %.14g floats); for other
reference types the result is "<typename>: 0x<hex pointer>".
If the value has a __tostring metamethod, it is invoked first and its
(string) result is used in place of the default formatting (matching
Sourcepub fn top(&mut self) -> i32
pub fn top(&mut self) -> i32
(stack top minus the slot just after the frame’s func).
Receiver is &mut self to match the LuaStateStubExt::top trait
signature exactly; with a different receiver shape (&self), Rust’s
method-resolution picks the trait default and the program panics on
todo!("phase-b-reconcile: top").
Sourcepub fn get_top(&mut self) -> i32
pub fn get_top(&mut self) -> i32
LuaStateStubExt::get_top trait method. Inherent method shadows the
trait default so the todo!("phase-b-reconcile: get_top") shim never
fires.
Sourcepub fn type_at(&mut self, idx: i32) -> LuaType
pub fn type_at(&mut self, idx: i32) -> LuaType
stack index idx, or LuaType::None if idx falls outside the
active call frame. Inherent method shadows the
LuaStateStubExt::type_at trait default so the todo!() shim
never fires.
Sourcepub fn check_arg_any(&mut self, arg: i32) -> Result<(), LuaError>
pub fn check_arg_any(&mut self, arg: i32) -> Result<(), LuaError>
#N (value expected)error if the slot atargisLUA_TNONE`
(i.e. beyond the active call frame’s top). Otherwise a no-op.
Inherent method on LuaState shadows the LuaStateStubExt::check_arg_any
trait default so the todo!() shim never fires.
Sourcepub fn check_arg_string(&mut self, arg: i32) -> Result<Vec<u8>, LuaError>
pub fn check_arg_string(&mut self, arg: i32) -> Result<Vec<u8>, LuaError>
at arg to a string via lua_tolstring (which coerces numbers to
their string form) and returns the bytes. Raises
bad argument #N (string expected, got <type>) if the value is not a
string and not number-coercible.
Inherent method on LuaState shadows the LuaStateStubExt::check_arg_string
trait default so the todo!() shim never fires. Uses the free to_lua_string
helper here rather than auxlib::check_lstring, which routes through
state.to_lua_string / state.type_name — both still trait stubs.
Sourcepub fn check_arg_integer(&mut self, arg: i32) -> Result<i64, LuaError>
pub fn check_arg_integer(&mut self, arg: i32) -> Result<i64, LuaError>
arg to a lua_Integer (i64) via lua_tointegerx (which accepts
ints, floats with exact integer value, and string-form integers).
Raises bad argument #N (number has no integer representation) if
the value is a number but not representable as an integer, or
bad argument #N (number expected, got <type>) otherwise.
Inherent method on LuaState shadows the LuaStateStubExt::check_arg_integer
trait default so the todo!() shim never fires. Uses the free
to_integer_x / is_number helpers in this file rather than
auxlib::check_integer, which routes through state.to_integer_x
and state.type_name — both still trait stubs.
Sourcepub fn check_number(&mut self, arg: i32) -> Result<f64, LuaError>
pub fn check_number(&mut self, arg: i32) -> Result<f64, LuaError>
arg to an f64 via lua_tonumberx (which accepts ints, floats,
and number-shaped strings) and raises bad argument #N (number expected, got <type>) if the value is not number-coercible.
Inherent method on LuaState shadows the LuaStateStubExt::check_number
trait default so the todo!() shim never fires. Uses the free
to_number_x helper here rather than auxlib::check_number, which
routes through state.to_number_x and state.type_name — both still
trait stubs.
Sourcepub fn opt_arg_integer(&mut self, arg: i32, def: i64) -> Result<i64, LuaError>
pub fn opt_arg_integer(&mut self, arg: i32, def: i64) -> Result<i64, LuaError>
arg is absent (LUA_TNONE) or nil, return def; otherwise
convert it to an integer (with the same string-to-number coercion
lua_tointegerx applies) and raise on failure.
Inherent method on LuaState shadows the LuaStateStubExt::opt_arg_integer
trait default so the todo!() shim never fires. Implemented with the
free-function helpers in this file rather than auxlib::opt_integer
because the latter routes through state.is_none_or_nil and
state.to_integer_x, which are themselves stubbed.
Sourcepub fn protected_call(
&mut self,
nargs: i32,
nresults: i32,
msgh: i32,
) -> Result<(), LuaError>
pub fn protected_call( &mut self, nargs: i32, nresults: i32, msgh: i32, ) -> Result<(), LuaError>
lua_pcallk with no continuation. Defers to the existing pcall_k
free function, which routes through protected_call_raw and
surfaces any runtime / syntax error as Err(LuaError::Runtime|Syntax).
Inherent method on LuaState shadows the LuaStateStubExt::protected_call
trait default so the todo!() shim never fires.
Sourcepub fn protected_call_k(
&mut self,
nargs: i32,
nresults: i32,
msgh: i32,
ctx: isize,
k: Option<fn(&mut LuaState, i32, isize) -> Result<usize, LuaError>>,
) -> Result<(), LuaError>
pub fn protected_call_k( &mut self, nargs: i32, nresults: i32, msgh: i32, ctx: isize, k: Option<fn(&mut LuaState, i32, isize) -> Result<usize, LuaError>>, ) -> Result<(), LuaError>
protected call. When k is set and the thread is yieldable, an
inner yield propagates as LuaError::Yield and the continuation
fires on resume via finishCcall → finishpcallk.
pub fn push_string(&mut self, s: &[u8]) -> Result<(), LuaError>
pub fn push_c_closure( &mut self, f: fn(&mut LuaState) -> Result<usize, LuaError>, n: i32, ) -> Result<(), LuaError>
pub fn raw_seti(&mut self, idx: i32, n: i64) -> Result<(), LuaError>
pub fn table_set_i(&mut self, idx: i32, n: i64) -> Result<(), LuaError>
Sourcepub fn table_get_i_value(
&mut self,
t: &LuaValue,
n: i64,
) -> Result<LuaType, LuaError>
pub fn table_get_i_value( &mut self, t: &LuaValue, n: i64, ) -> Result<LuaType, LuaError>
Get t[n] where t is a pre-resolved LuaValue, bypassing stack-index
resolution. Use this in tight loops that operate on the same table
repeatedly to avoid the index_to_value call per iteration.
Sourcepub fn table_set_i_value(
&mut self,
t: &LuaValue,
n: i64,
) -> Result<(), LuaError>
pub fn table_set_i_value( &mut self, t: &LuaValue, n: i64, ) -> Result<(), LuaError>
Set t[n] = stack_top (then pop) where t is a pre-resolved LuaValue,
bypassing stack-index resolution. Use this in tight loops that operate on
the same table repeatedly to avoid the index_to_value call per iteration.
pub fn create_table(&mut self, narr: i32, nrec: i32) -> Result<(), LuaError>
Sourcepub fn registry_set(&mut self, key: &[u8]) -> Result<(), LuaError>
pub fn registry_set(&mut self, key: &[u8]) -> Result<(), LuaError>
Pop the value on top of the stack and store it in the registry under
the string key.
Sourcepub fn new_metatable(&mut self, tname: &[u8]) -> Result<bool, LuaError>
pub fn new_metatable(&mut self, tname: &[u8]) -> Result<bool, LuaError>
Create a new metatable in the registry under key tname. Leaves the
new metatable on top of the stack and returns true when newly
created. If registry[tname] already exists, leaves it on top of the
stack and returns false.
Sourcepub fn new_lib(
&mut self,
funcs: &[(&[u8], fn(&mut LuaState) -> Result<usize, LuaError>)],
) -> Result<(), LuaError>
pub fn new_lib( &mut self, funcs: &[(&[u8], fn(&mut LuaState) -> Result<usize, LuaError>)], ) -> Result<(), LuaError>
Create a new library table sized for funcs and register each entry as
a closure field on it. Leaves the table on the top of the stack.
luaL_checkversion(L), luaL_newlibtable(L,l), luaL_setfuncs(L,l,0).
luaL_checkversion is a no-op here (no ABI-version mismatch is
possible inside the Rust port).
Sourcepub fn register_lib(
&mut self,
_name: &[u8],
funcs: &[(&[u8], fn(&mut LuaState) -> Result<usize, LuaError>)],
) -> Result<(), LuaError>
pub fn register_lib( &mut self, _name: &[u8], funcs: &[(&[u8], fn(&mut LuaState) -> Result<usize, LuaError>)], ) -> Result<(), LuaError>
Create and populate a library table for funcs, leaving it on top of
the stack. The _name argument is informational and matches the
luaL_register-style call sites in the Phase-A stdlib; the actual
global binding for the library happens later via luaL_requiref.
Sourcepub fn new_lib_table(
&mut self,
funcs: &[(&[u8], fn(&mut LuaState) -> Result<usize, LuaError>)],
) -> Result<(), LuaError>
pub fn new_lib_table( &mut self, funcs: &[(&[u8], fn(&mut LuaState) -> Result<usize, LuaError>)], ) -> Result<(), LuaError>
Create a new empty table presized to hold every entry in funcs, and
leave it on top of the stack. No registration is performed — callers
typically follow up with set_funcs / set_funcs_with_upvalues to
populate the table.
lua_createtable(L, 0, sizeof(l)/sizeof((l)[0]) - 1). The C macro’s
- 1 discounts the sentinel {NULL, NULL} entry; the Rust slice has
no sentinel, so we use funcs.len() directly.
Sourcepub fn set_funcs_with_upvalues(
&mut self,
funcs: &[(&[u8], fn(&mut LuaState) -> Result<usize, LuaError>)],
nup: i32,
) -> Result<(), LuaError>
pub fn set_funcs_with_upvalues( &mut self, funcs: &[(&[u8], fn(&mut LuaState) -> Result<usize, LuaError>)], nup: i32, ) -> Result<(), LuaError>
Register each entry in funcs as a C closure on the table at index
-(nup + 2), sharing the nup values currently on top of the stack
as upvalues. The upvalues are popped at the end.
pub fn set_metatable(&mut self, objindex: i32) -> Result<(), LuaError>
Sourcepub fn set_metatable_by_name(&mut self, name: &[u8]) -> Result<(), LuaError>
pub fn set_metatable_by_name(&mut self, name: &[u8]) -> Result<(), LuaError>
Fetch the metatable registered under name in the registry and assign
it as the metatable of the value currently on top of the stack. The
fetched metatable is popped after assignment, leaving the original top
value in place.
Sourcepub fn get_subtable_registry(&mut self, name: &[u8]) -> Result<bool, LuaError>
pub fn get_subtable_registry(&mut self, name: &[u8]) -> Result<bool, LuaError>
Ensure registry[name] is a table; push it onto the stack.
Returns true if the table already existed, false if newly created.
Sourcepub fn new_userdata_typed(
&mut self,
_name: &[u8],
size: usize,
nuvalue: i32,
) -> Result<GcRef<LuaUserData>, LuaError>
pub fn new_userdata_typed( &mut self, _name: &[u8], size: usize, nuvalue: i32, ) -> Result<GcRef<LuaUserData>, LuaError>
Allocate a fresh full-userdata block of size bytes with nuvalue
nil-initialised user-value slots, push it on the stack, and return a
GcRef to it. The _name parameter is advisory — callers typically
follow up with set_metatable_by_name(name) to attach the registered
metatable.
C-correspondent: lua_newuserdatauv(L, size, nuvalue) (no name
parameter on the C side; the Rust signature carries it for callers’
convenience).
impl LuaState
Stub methods on LuaState assumed by this module.
These will be implemented in their home modules (do_.rs, debug.rs, tagmethods.rs) and removed from this file in Phase B.
Source§impl LuaState
impl LuaState
Sourcepub fn global(&self) -> Ref<'_, GlobalState>
pub fn global(&self) -> Ref<'_, GlobalState>
Access the process-wide GlobalState immutably.
macros.tsv: G → state.global()
PORT NOTE: Returns std::cell::Ref<GlobalState> because GlobalState is held in
Rc<RefCell<...>>. Call sites that do state.global().field should work fine
via Deref. Callers must not hold the Ref across a global_mut() call.
Sourcepub fn global_mut(&self) -> RefMut<'_, GlobalState>
pub fn global_mut(&self) -> RefMut<'_, GlobalState>
Access the process-wide GlobalState mutably.
macros.tsv: G → state.global() (writes use state.global_mut())
Sourcepub fn global_rc(&self) -> Rc<RefCell<GlobalState>>
pub fn global_rc(&self) -> Rc<RefCell<GlobalState>>
Clone the Rc handle to the GlobalState for sharing with a new coroutine.
Used in new_thread to give the child thread access to the same GlobalState.
Sourcepub fn c_calls(&self) -> u32
pub fn c_calls(&self) -> u32
Return the current C-call recursion depth (lower 16 bits of n_ccalls).
macros.tsv: getCcalls → state.c_calls()
Sourcepub fn inc_nny(&mut self)
pub fn inc_nny(&mut self)
Increment the non-yieldable call count (upper 16 bits of n_ccalls).
macros.tsv: incnny → state.inc_nny()
Sourcepub fn dec_nny(&mut self)
pub fn dec_nny(&mut self)
Decrement the non-yieldable call count.
macros.tsv: decnny → state.dec_nny()
Sourcepub fn is_yieldable(&self) -> bool
pub fn is_yieldable(&self) -> bool
Returns true if the thread can yield (no non-yieldable frames on the stack).
macros.tsv: yieldable → state.is_yieldable()
Sourcepub fn reset_hook_count(&mut self)
pub fn reset_hook_count(&mut self)
Reset the hook countdown to the baseline.
macros.tsv: resethookcount → state.reset_hook_count()
Sourcepub fn stack_size(&self) -> usize
pub fn stack_size(&self) -> usize
Returns the current stack capacity (slots between base and stack_last).
macros.tsv: stacksize → state.stack_size()
Sourcepub fn push(&mut self, val: LuaValue)
pub fn push(&mut self, val: LuaValue)
Push a value onto the stack, incrementing top.
macros.tsv: api_incr_top → gone — state.push() already increments
Sourcepub fn stack_val(&self, idx: StackIdx) -> &LuaValue
pub fn stack_val(&self, idx: StackIdx) -> &LuaValue
Retrieve the value at the given stack index without removing it.
macros.tsv: s2v → state.stack_at(idx) → returns &LuaValue
Sourcepub fn set_stack_val(&mut self, idx: StackIdx, val: LuaValue)
pub fn set_stack_val(&mut self, idx: StackIdx, val: LuaValue)
Write a value to a specific stack slot.
Sourcepub fn gc(&mut self) -> GcHandle<'_>
pub fn gc(&mut self) -> GcHandle<'_>
Returns a no-op GC handle.
macros.tsv: luaC_checkGC → state.gc().check_step(), etc.
PORT NOTE: In Phases A–C the GC is Rc-based and all GC operations are
no-ops. Phase D replaces this with real GC logic in lua-gc.
Sourcepub fn external_root_value(&mut self, value: LuaValue) -> ExternalRootKey
pub fn external_root_value(&mut self, value: LuaValue) -> ExternalRootKey
Pin a Lua value in the external root set and return its stable key.
Sourcepub fn external_rooted_value(&self, key: ExternalRootKey) -> Option<LuaValue>
pub fn external_rooted_value(&self, key: ExternalRootKey) -> Option<LuaValue>
Read a value currently pinned by an external root key.
Sourcepub fn external_replace_root(
&mut self,
key: ExternalRootKey,
value: LuaValue,
) -> Option<LuaValue>
pub fn external_replace_root( &mut self, key: ExternalRootKey, value: LuaValue, ) -> Option<LuaValue>
Replace the value pinned by an external root key.
Sourcepub fn external_unroot_value(
&mut self,
key: ExternalRootKey,
) -> Option<LuaValue>
pub fn external_unroot_value( &mut self, key: ExternalRootKey, ) -> Option<LuaValue>
Remove an external root. Returns None for stale or already-removed keys.
Sourcepub fn try_external_unroot_value(
&mut self,
key: ExternalRootKey,
) -> Result<Option<LuaValue>, BorrowMutError>
pub fn try_external_unroot_value( &mut self, key: ExternalRootKey, ) -> Result<Option<LuaValue>, BorrowMutError>
Best-effort external root removal for destructors that may run while
the collector holds an immutable GlobalState borrow.
Sourcepub fn new_table(&mut self) -> GcRef<LuaTable>
pub fn new_table(&mut self) -> GcRef<LuaTable>
Create a new empty table and register it with the GC.
macros.tsv: lua_newtable → state.new_table()
Sourcepub fn new_table_with_sizes(
&mut self,
array_size: u32,
hash_size: u32,
) -> Result<GcRef<LuaTable>, LuaError>
pub fn new_table_with_sizes( &mut self, array_size: u32, hash_size: u32, ) -> Result<GcRef<LuaTable>, LuaError>
Create a fresh table with pre-sized array/hash parts.
mirrors the luaH_new + luaH_resize pair in one call so we don’t
pay an extra resize path for hot construction sites.
Sourcepub fn intern_str(&mut self, bytes: &[u8]) -> Result<GcRef<LuaString>, LuaError>
pub fn intern_str(&mut self, bytes: &[u8]) -> Result<GcRef<LuaString>, LuaError>
Intern a byte string in the global string pool.
In C, short strings (≤ LUAI_MAXSHORTLEN = 40 bytes) are interned globally
via luaS_newlstr, while long strings allocate a fresh TString each
call so distinct long strings keep distinct object identity (observable
via string.format("%p", s)). The parser separately deduplicates
long-string literals within a single chunk through luaX_newstring’s
ls->h anchor table.
macros.tsv: luaS_new → state.intern_str(s)
Source§impl LuaState
impl LuaState
pub fn get_at(&self, idx: impl Into<StackIdxConv>) -> LuaValue
pub fn set_at(&mut self, idx: impl Into<StackIdxConv>, v: LuaValue)
Sourcepub fn clear_stack_range(&mut self, start: StackIdx, end: StackIdx)
pub fn clear_stack_range(&mut self, start: StackIdx, end: StackIdx)
Clear stack slots in [start, end) without changing top.
Internal call setup reserves space up to ci.top; while GC tracing is
conservative over that range, the unused tail must not retain stale
collectable values from previous frames.
Sourcepub fn get_int_at(&self, idx: impl Into<StackIdxConv>) -> Option<i64>
pub fn get_int_at(&self, idx: impl Into<StackIdxConv>) -> Option<i64>
Hot-path accessor: returns Some(i) only when the stack slot at idx
holds a LuaValue::Int(i). Returns None for any other tag (including
out-of-bounds, which behaves as Nil).
ttisinteger predicate that gates the integer arithmetic fast path in
lvm.c’s op_arith_aux macro. Avoids the full LuaValue clone that
get_at performs — the operand is only needed for its i64 payload.
Sourcepub fn get_int_pair_at(
&self,
rb: impl Into<StackIdxConv>,
rc: impl Into<StackIdxConv>,
) -> Option<(i64, i64)>
pub fn get_int_pair_at( &self, rb: impl Into<StackIdxConv>, rc: impl Into<StackIdxConv>, ) -> Option<(i64, i64)>
Hot-path accessor: returns Some((a, b)) only when both stack slots
at rb and rc hold integers. Equivalent to two get_int_at calls
but is shaped so the arithmetic opcode dispatch arms can pattern-match
the common case with a single if let.
the op_arith_aux macro.
Sourcepub fn get_num_at(&self, idx: impl Into<StackIdxConv>) -> Option<f64>
pub fn get_num_at(&self, idx: impl Into<StackIdxConv>) -> Option<f64>
Hot-path accessor: returns Some(f) when the slot holds a Float(f)
or coerces an Int(i) to f64. Returns None for any other tag.
No LuaValue clone — only the primitive payload travels back.
Sourcepub fn get_float_at(&self, idx: impl Into<StackIdxConv>) -> Option<f64>
pub fn get_float_at(&self, idx: impl Into<StackIdxConv>) -> Option<f64>
Hot-path accessor: returns Some(f) only when the slot holds a
LuaValue::Float(f). Does NOT coerce integers; the integer branch is
the caller’s responsibility. Used by opcode arms that have already
ruled out the integer fast path.
Sourcepub fn get_num_pair_at(
&self,
rb: impl Into<StackIdxConv>,
rc: impl Into<StackIdxConv>,
) -> Option<(f64, f64)>
pub fn get_num_pair_at( &self, rb: impl Into<StackIdxConv>, rc: impl Into<StackIdxConv>, ) -> Option<(f64, f64)>
Hot-path accessor: pair version of get_num_at — returns Some((a,b))
when both slots coerce to f64 (Float or Int), None if either does
not. Used by the float fast path of the arith opcodes.
Sourcepub fn set_top(&mut self, idx: impl Into<StackIdxConv>)
pub fn set_top(&mut self, idx: impl Into<StackIdxConv>)
Set top to an absolute stack index. Grows the backing stack vector
(filling new slots with Nil) when idx is past stack.len(), but
never clobbers existing slots between the old top and the new top —
VM opcodes (Call, ForPrep, etc.) write registers via set_at and then
raise top to signal “these are now live”; nil-filling here would
erase the just-written values.
setnilvalue(s2v(L->top.p++))clear loop inlua_settop(lapi.c) is part of the public API path and lives inapi::set_topinstead. PORT NOTE: callers pass an absoluteStackIdx, not the relative idxof the publiclua_settop. The to-be-closed (tbclist`) close path
is Phase E and not handled here.
Sourcepub fn set_top_idx(&mut self, idx: impl Into<StackIdxConv>)
pub fn set_top_idx(&mut self, idx: impl Into<StackIdxConv>)
Primitive “set top index” — just writes self.top, no nil-fill.
PORT NOTE: callers (api.rs::set_top, raw_set, etc.) pre-nil-fill or
only shrink, so this routine intentionally does no clearing or resizing.
The to-be-closed (tbclist) close path is Phase E.
pub fn pop_n(&mut self, n: usize)
Sourcepub fn peek_at(&mut self, idx: impl Into<StackIdxConv>) -> LuaValue
pub fn peek_at(&mut self, idx: impl Into<StackIdxConv>) -> LuaValue
Returns the value at the given stack index without removing it.
Sourcepub fn peek_top(&mut self) -> LuaValue
pub fn peek_top(&mut self) -> LuaValue
Returns the value just below top (the topmost live slot) without
removing it.
Sourcepub fn peek_string_at_top(&mut self) -> GcRef<LuaString>
pub fn peek_string_at_top(&mut self) -> GcRef<LuaString>
Returns the topmost slot interpreted as a string. Panics if the slot
is not a LuaValue::Str. Callers (e.g. luaO_pushvfstring) guarantee
the value has been pushed as an interned string immediately prior.
Sourcepub fn stack_at(&mut self, idx: impl Into<StackIdxConv>) -> &mut LuaValue
pub fn stack_at(&mut self, idx: impl Into<StackIdxConv>) -> &mut LuaValue
Mutable reference to the value at the given stack slot.
Sourcepub fn stack_set_nil(&mut self, idx: impl Into<StackIdxConv>)
pub fn stack_set_nil(&mut self, idx: impl Into<StackIdxConv>)
Writes Nil to the given stack slot.
Sourcepub fn stack_resize(&mut self, size: usize) -> Result<(), LuaError>
pub fn stack_resize(&mut self, size: usize) -> Result<(), LuaError>
Resizes the underlying stack vector to size slots, padding new slots
with StackValue::default() (which is Nil). Returns Ok(()) on
success — Vec::resize_with in Rust does not have a fallible path the
way luaM_reallocvector does in C, so the Result is here for
signature parity with future fallible allocators.
newsize+EXTRA_STACK, StackValue)`.pub fn stack_available(&mut self) -> usize
pub fn check_stack(&mut self, n: i32) -> Result<(), LuaError>
Sourcepub fn grow_stack(&mut self, n: i32, raise_error: bool) -> Result<(), LuaError>
pub fn grow_stack(&mut self, n: i32, raise_error: bool) -> Result<(), LuaError>
Inherent method wrapper around the free function do_::grow_stack,
preserving the historical Result<(), LuaError> signature used by
check_stack and other VM call sites. The bool returned by the
underlying implementation distinguishes soft failure (when
raise_error is false) from success; that distinction is dropped here
because every current caller passes raise_error = true and only
cares about error propagation.
pub fn get_ci(&self, idx: CallInfoIdx) -> &CallInfo
pub fn get_ci_mut(&mut self, idx: CallInfoIdx) -> &mut CallInfo
pub fn current_call_info(&self) -> &CallInfo
pub fn current_call_info_mut(&mut self) -> &mut CallInfo
pub fn current_ci_idx(&self) -> CallInfoIdx
pub fn call_stack_mut(&mut self) -> &mut Vec<CallInfo>
pub fn next_ci(&mut self) -> Result<CallInfoIdx, LuaError>
pub fn prev_ci(&self, idx: CallInfoIdx) -> Option<CallInfoIdx>
pub fn get_prev_ci(&self, idx: CallInfoIdx) -> Option<&CallInfo>
pub fn is_base_ci(&self, idx: CallInfoIdx) -> bool
pub fn is_current_ci(&self, idx: CallInfoIdx) -> bool
pub fn ci_next_func(&self, idx: CallInfoIdx) -> StackIdx
pub fn ci_top(&self, idx: CallInfoIdx) -> StackIdx
pub fn ci_trap(&mut self, idx: CallInfoIdx) -> bool
pub fn ci_savedpc(&self, idx: CallInfoIdx) -> u32
pub fn set_ci_savedpc(&mut self, idx: CallInfoIdx, pc: u32)
pub fn set_ci_previous(&mut self, idx: CallInfoIdx)
pub fn ci_previous(&self, idx: CallInfoIdx) -> Option<CallInfoIdx>
pub fn ci_adjust_func(&mut self, idx: CallInfoIdx, delta: i32)
pub fn ci_base(&self, idx: CallInfoIdx) -> StackIdx
pub fn ci_is_fresh(&self, idx: CallInfoIdx) -> bool
pub fn ci_lua_closure(&self, idx: CallInfoIdx) -> Option<GcRef<LuaLClosure>>
pub fn ci_nextraargs(&self, idx: CallInfoIdx) -> i32
pub fn ci_nres(&self, idx: CallInfoIdx) -> i32
pub fn ci_nres_set(&mut self, idx: CallInfoIdx, n: i32)
pub fn ci_nresults(&self, idx: CallInfoIdx) -> i32
pub fn ci_prev_instruction(&self, idx: CallInfoIdx) -> Instruction
pub fn ci_prev2_instruction(&self, idx: CallInfoIdx) -> Instruction
pub fn ci_skip_next_instruction(&mut self, idx: CallInfoIdx)
pub fn ci_step_pc_back(&mut self, idx: CallInfoIdx)
pub fn get_ci_pcrel(&mut self, idx: CallInfoIdx) -> u32
pub fn get_ci_u2_funcidx(&mut self, idx: CallInfoIdx) -> i32
pub fn get_ci_u2_nres(&mut self, idx: CallInfoIdx) -> i32
pub fn get_ci_u2_nyield(&mut self, idx: CallInfoIdx) -> i32
pub fn get_ci_vararg_info(&mut self, idx: CallInfoIdx) -> (bool, i32, i32)
pub fn get_ci_lua_proto_numparams(&mut self, idx: CallInfoIdx) -> u8
pub fn set_ci_u2_nres(&mut self, idx: CallInfoIdx, n: i32)
pub fn set_ci_u2_nyield(&mut self, idx: CallInfoIdx, n: i32)
pub fn set_ci_transfer_info( &mut self, idx: CallInfoIdx, ftransfer: u16, ntransfer: u16, )
pub fn shrink_ci(&mut self)
pub fn check_c_stack(&mut self) -> Result<(), LuaError>
pub fn status(&mut self) -> LuaStatus
pub fn errfunc(&mut self) -> isize
pub fn old_pc(&mut self) -> u32
pub fn set_old_pc(&mut self, pc: u32)
pub fn set_oldpc(&mut self, pc: u32)
pub fn _hook_call_noargs(&mut self)
pub fn hook(&self) -> Option<&Box<dyn FnMut(&mut LuaState, &LuaDebug)>>
pub fn has_hook(&mut self) -> bool
pub fn hook_count(&mut self) -> i32
pub fn set_hook_count(&mut self, n: i32)
pub fn hook_mask(&self) -> u8
pub fn set_hook_mask(&mut self, m: u8)
pub fn base_hook_count(&self) -> i32
pub fn set_base_hook_count(&mut self, n: i32)
pub fn set_hook(&mut self, h: Option<Box<dyn FnMut(&mut LuaState, &LuaDebug)>>)
pub fn call_hook_event(&mut self, event: i32, line: i32) -> Result<(), LuaError>
pub fn registry_value(&self) -> LuaValue
pub fn registry_get(&self, key: usize) -> LuaValue
pub fn new_string(&mut self, bytes: &[u8]) -> Result<GcRef<LuaString>, LuaError>
Sourcepub fn new_proto(&mut self) -> GcRef<LuaProto>
pub fn new_proto(&mut self) -> GcRef<LuaProto>
Allocate a new Lua function prototype.
Caller mutates the returned proto in place (it’s behind GcRef, which is
Rc during Phase D-1; mutable access via Rc::get_mut only works while
no other GcRefs alias it — true at construction).
Sourcepub fn new_lclosure(
&mut self,
proto: GcRef<LuaProto>,
nupvals: usize,
) -> GcRef<LuaLClosure>
pub fn new_lclosure( &mut self, proto: GcRef<LuaProto>, nupvals: usize, ) -> GcRef<LuaLClosure>
Allocate a Lua-side closure (compiled function + upvalue slots).
Sourcepub fn new_upval_closed(&mut self, v: LuaValue) -> GcRef<UpVal>
pub fn new_upval_closed(&mut self, v: LuaValue) -> GcRef<UpVal>
Allocate a closed upvalue holding the given value.
Sourcepub fn new_upval_open(
&mut self,
thread_id: usize,
level: StackIdx,
) -> GcRef<UpVal>
pub fn new_upval_open( &mut self, thread_id: usize, level: StackIdx, ) -> GcRef<UpVal>
Allocate an open upvalue referring to a thread’s stack slot.
Sourcepub fn intern_or_create_str(
&mut self,
bytes: &[u8],
) -> Result<GcRef<LuaString>, LuaError>
pub fn intern_or_create_str( &mut self, bytes: &[u8], ) -> Result<GcRef<LuaString>, LuaError>
Mirrors luaS_newlstr: short strings are interned globally so equal
content shares a single TString; long strings (> LUAI_MAXSHORTLEN = 40)
always create a fresh TString without interning. This is what lets
string.format("%p", "long" .. "concat") differ from a same-content
literal — concat must produce a new object even when the literal already
lives in the lexer’s constant pool.
pub fn new_userdata( &mut self, _size: usize, _nuvalue: usize, ) -> Result<GcRef<LuaUserData>, LuaError>
pub fn new_c_closure( &mut self, _f: fn(&mut LuaState) -> Result<usize, LuaError>, _n: i32, ) -> Result<LuaClosure, LuaError>
pub fn push_closure( &mut self, proto_idx: usize, ci: CallInfoIdx, base: StackIdx, ra: StackIdx, ) -> Result<(), LuaError>
pub fn new_tbc_upval(&mut self, idx: StackIdx) -> Result<(), LuaError>
Sourcepub fn upvalue_get(&self, cl: &GcRef<LuaLClosure>, n: usize) -> LuaValue
pub fn upvalue_get(&self, cl: &GcRef<LuaLClosure>, n: usize) -> LuaValue
Read an open or closed upvalue.
Closed upvalues own their value and read trivially. Open upvalues point at a stack slot on the home thread that captured them.
Resolution order for an open upvalue whose home is not the current thread:
- If the home thread is registered in
GlobalState::threadsand itsRefCellis currently borrowable, read straight from its stack. This is the path used when the main thread reads a closure created inside a now-suspended coroutine, or when one coroutine reads an upvalue homed on a sibling suspended coroutine. - Otherwise fall back to
GlobalState::cross_thread_upvals. This is the path used while inside acoroutine.resume: the parent thread’sLuaStateis held by an outer&mutand is not reachable through anyRc<RefCell<_>>, soaux_resumesnapshots the parent’s open upvalues into the mirror across the resume boundary.
Sourcepub fn upvalue_set(
&mut self,
cl: &GcRef<LuaLClosure>,
n: usize,
val: LuaValue,
) -> Result<(), LuaError>
pub fn upvalue_set( &mut self, cl: &GcRef<LuaLClosure>, n: usize, val: LuaValue, ) -> Result<(), LuaError>
Write an open or closed upvalue.
Mirrors [upvalue_get]: open upvalues homed on the current thread
write through self.stack. For cross-thread open upvalues, the
home thread’s stack is written directly when its RefCell is
borrowable, otherwise the write lands in
GlobalState::cross_thread_upvals (the active-resume case where
the home thread is borrow-locked further up the call stack).
pub fn protected_call_raw( &mut self, func: StackIdx, nresults: i32, errfunc: StackIdx, ) -> Result<(), LuaError>
pub fn protected_parser( &mut self, z: ZIO, name: &[u8], mode: Option<&[u8]>, ) -> LuaStatus
pub fn do_call(&mut self, func: StackIdx, nresults: i32) -> Result<(), LuaError>
pub fn do_call_no_yield( &mut self, func: StackIdx, nresults: i32, ) -> Result<(), LuaError>
pub fn call_no_yield( &mut self, func: StackIdx, nresults: i32, ) -> Result<(), LuaError>
pub fn call_at(&mut self, func: StackIdx, nresults: i32) -> Result<(), LuaError>
pub fn precall( &mut self, func: StackIdx, nresults: i32, ) -> Result<Option<CallInfoIdx>, LuaError>
pub fn pretailcall( &mut self, ci: CallInfoIdx, func: StackIdx, narg1: i32, delta: i32, ) -> Result<i32, LuaError>
pub fn poscall<N>(&mut self, ci: CallInfoIdx, nres: N) -> Result<(), LuaError>
pub fn adjust_results(&mut self, nresults: i32)
pub fn adjust_varargs( &mut self, ci: CallInfoIdx, nfixparams: i32, cl: &GcRef<LuaLClosure>, ) -> Result<(), LuaError>
pub fn get_varargs( &mut self, ci: CallInfoIdx, ra: StackIdx, n: i32, ) -> Result<i32, LuaError>
pub fn close_upvals(&mut self, level: StackIdx) -> Result<(), LuaError>
pub fn close_upvals_status( &mut self, level: StackIdx, _status: i32, ) -> Result<(), LuaError>
pub fn close_upvals_from_base( &mut self, ci: CallInfoIdx, ) -> Result<(), LuaError>
pub fn arith_op( &mut self, op: i32, p1: &LuaValue, p2: &LuaValue, ) -> Result<LuaValue, LuaError>
pub fn concat(&mut self, n: i32) -> Result<(), LuaError>
pub fn less_than( &mut self, l: &LuaValue, r: &LuaValue, ) -> Result<bool, LuaError>
pub fn less_equal( &mut self, l: &LuaValue, r: &LuaValue, ) -> Result<bool, LuaError>
pub fn equal_obj( &self, _ctx: Option<&LuaValue>, l: &LuaValue, r: &LuaValue, ) -> bool
pub fn equal_obj_with_tm( &mut self, l: &LuaValue, r: &LuaValue, ) -> Result<bool, LuaError>
pub fn obj_len(&mut self, v: &LuaValue) -> Result<LuaValue, LuaError>
pub fn obj_to_string(&mut self, idx: i32) -> Result<GcRef<LuaString>, LuaError>
pub fn coerce_to_string( &mut self, idx: StackIdx, ) -> Result<GcRef<LuaString>, LuaError>
pub fn str_to_num(&mut self, s: &[u8]) -> Option<(LuaValue, usize)>
pub fn fast_get( &mut self, t: &LuaValue, k: &LuaValue, ) -> Result<Option<LuaValue>, LuaError>
pub fn fast_get_int( &mut self, t: &LuaValue, k: i64, ) -> Result<Option<LuaValue>, LuaError>
pub fn fast_get_short_str( &mut self, t: &LuaValue, k: &LuaValue, ) -> Result<Option<LuaValue>, LuaError>
pub fn fast_tm_table( &mut self, t: Option<&GcRef<LuaTable>>, tm: TagMethod, ) -> LuaValue
pub fn fast_tm_ud(&mut self, u: &GcRef<LuaUserData>, tm: TagMethod) -> LuaValue
pub fn table_get_with_tm( &mut self, t: &LuaValue, k: &LuaValue, ) -> Result<LuaValue, LuaError>
Sourcepub fn table_set_with_tm(
&mut self,
t: &LuaValue,
k: LuaValue,
v: LuaValue,
) -> Result<(), LuaError>
pub fn table_set_with_tm( &mut self, t: &LuaValue, k: LuaValue, v: LuaValue, ) -> Result<(), LuaError>
Set t[k] = v with __newindex metamethod awareness.
Fast path: when the table has no metatable, __newindex can never
fire, so the existence check via fast_get is pure waste —
try_raw_set handles both “key exists” and “key absent” cases via
a single lookup internally. Removing the fast_get halves the
lookups per set on the metamethod-free path (table.remove/insert
hot loops, most user code).
The GC backward barrier is invoked before the store (with &v)
instead of after; the barrier only inspects the value’s color, not
its location, so the order is semantically equivalent to upstream
C-Lua and lets us move v straight into table_raw_set without
the extra v.clone() that the post-store ordering forced.
pub fn table_raw_set( &mut self, t: &LuaValue, k: LuaValue, v: LuaValue, ) -> Result<(), LuaError>
pub fn table_array_set( &mut self, t: &LuaValue, idx: usize, v: LuaValue, ) -> Result<(), LuaError>
pub fn table_ensure_array( &mut self, t: &LuaValue, n: usize, ) -> Result<(), LuaError>
pub fn table_length(&mut self, t: &LuaValue) -> Result<i64, LuaError>
pub fn table_metatable(&mut self, v: &LuaValue) -> Option<GcRef<LuaTable>>
pub fn table_resize( &mut self, t: &GcRef<LuaTable>, na: usize, nh: usize, ) -> Result<(), LuaError>
pub fn table_getn(&self, t: &GcRef<LuaTable>) -> i64
pub fn try_bin_tm( &mut self, p1: &LuaValue, p1_idx: Option<StackIdx>, p2: &LuaValue, p2_idx: Option<StackIdx>, res: StackIdx, tm: TagMethod, ) -> Result<(), LuaError>
pub fn try_bin_i_tm( &mut self, p1: &LuaValue, p1_idx: Option<StackIdx>, imm: i64, flip: bool, res: StackIdx, tm: TagMethod, ) -> Result<(), LuaError>
pub fn try_bin_assoc_tm( &mut self, p1: &LuaValue, p1_idx: Option<StackIdx>, p2: &LuaValue, p2_idx: Option<StackIdx>, flip: bool, res: StackIdx, tm: TagMethod, ) -> Result<(), LuaError>
pub fn try_concat_tm( &mut self, _p1: &LuaValue, _p2: &LuaValue, ) -> Result<(), LuaError>
pub fn call_tm( &mut self, f: LuaValue, p1: &LuaValue, p2: &LuaValue, p3: &LuaValue, ) -> Result<(), LuaError>
pub fn call_tm_res( &mut self, f: LuaValue, p1: &LuaValue, p2: &LuaValue, res: StackIdx, ) -> Result<(), LuaError>
pub fn call_tm_res_bool( &mut self, f: LuaValue, p1: &LuaValue, p2: &LuaValue, ) -> Result<bool, LuaError>
pub fn call_order_tm( &mut self, p1: &LuaValue, p2: &LuaValue, tm: TagMethod, ) -> Result<bool, LuaError>
pub fn call_order_i_tm( &mut self, p1: &LuaValue, v2: i64, flip: bool, isfloat: bool, tm: TagMethod, ) -> Result<bool, LuaError>
pub fn proto_code(&self, cl: &GcRef<LuaLClosure>, pc: u32) -> Instruction
pub fn proto_const(&self, cl: &GcRef<LuaLClosure>, idx: usize) -> LuaValue
Sourcepub fn proto_const_int(
&self,
cl: &GcRef<LuaLClosure>,
idx: usize,
) -> Option<i64>
pub fn proto_const_int( &self, cl: &GcRef<LuaLClosure>, idx: usize, ) -> Option<i64>
Hot-path accessor: returns Some(i) only when the constant pool entry
at idx is an Int. Avoids the full LuaValue clone that
proto_const performs.
arithmetic opcode macros (op_arithK).
Sourcepub fn proto_const_num(
&self,
cl: &GcRef<LuaLClosure>,
idx: usize,
) -> Option<f64>
pub fn proto_const_num( &self, cl: &GcRef<LuaLClosure>, idx: usize, ) -> Option<f64>
Hot-path accessor: returns Some(f) for Float(f) or Int(i) (coerced)
constants. Avoids the full LuaValue clone. Used by the float fast
path of OP_ADDK/OP_SUBK/OP_MULK/OP_DIVK/OP_POWK.
pub fn get_proto_instr(&self, ci: CallInfoIdx, pc: u32) -> Instruction
Sourcepub fn trace_call(&mut self, _idx: CallInfoIdx) -> Result<bool, LuaError>
pub fn trace_call(&mut self, _idx: CallInfoIdx) -> Result<bool, LuaError>
flag as bool (C returns int 0/1).
The C function reads L->ci directly, so the _idx argument is unused;
the VM passes its locally tracked ci for symmetry with trace_exec.
Sourcepub fn trace_exec(
&mut self,
_idx: CallInfoIdx,
pc: u32,
) -> Result<bool, LuaError>
pub fn trace_exec( &mut self, _idx: CallInfoIdx, pc: u32, ) -> Result<bool, LuaError>
returning bool for the trap flag. _idx is unused for the same reason
as trace_call; pc is the 0-based index of the next instruction.
pub fn hook_call(&mut self, idx: CallInfoIdx) -> Result<(), LuaError>
pub fn gc_check_step(&mut self)
pub fn gc_cond_step(&mut self)
pub fn gc_barrier_back<T, U>(&mut self, _t: T, _v: U)
pub fn gc_barrier_upval<T, U, V>(&mut self, _cl: T, _uv: U, _v: V)
Sourcepub fn is_main_thread(&mut self) -> bool
pub fn is_main_thread(&mut self) -> bool
Phase E-1: compares GlobalState::current_thread_id against
main_thread_id. Coroutine resume (slice 02b) is what will swap
current_thread_id in and out; until then the running thread is
always the main thread and this returns true.