Skip to main content

Module table

Module table 

Source
Expand description

Lua table implementation (array + hash hybrid).

Canonical port of reference/lua-5.4.7/src/ltable.c. Lives in lua-types because LuaValue::Table(GcRef<LuaTable>) is defined here and the table storage must be reachable without depending on lua-vm. The crate lua_unsafe = "forbid" lint is preserved.

§Interior mutability

GcRef<T> only yields &T on deref, so the mutable algorithms in C-Lua’s ltable.c (which write through Table *) must operate through a RefCell. The split is:

  • LuaTable — outer handle. All public methods take &self.
  • TableInner — storage + algorithms. All mutating methods are &mut TableInner and are reached via inner.borrow_mut().

The hash part uses Brent’s variation of chained scatter tables. The key invariant: if an element is not in its main position (the slot its hash maps to), the colliding element is in its own main position.

Structs§

LuaTable
A Lua table: hybrid array + hash map.
TableFlags
Bitfield for a LuaTable: lower bits record absent fast-access metamethods; bit 7 encodes whether alimit is the real array size.
TableInner
Hybrid array + hash storage backing a LuaTable.
TableNode
One node in a table’s hash part.

Enums§

TableSlotRef
Internal slot reference returned by the “get” family of functions.

Constants§

ARRAY_GROW_CAP
Soft cap on array growth in a single raw_set call.
MAXASIZE
Maximum size of the array part.
MAXHBITS
Largest k such that 2^k fits in a signed i32 minus one (hash part).
TOTAL_GROW_CAP
Soft cap on total entries (array + hash) used to emulate C-Lua’s malloc-NULL termination of unbounded for i = 1, math.huge do a[i] = ... end loops. C-Lua hits real malloc failure at multi-gigabyte allocations; we raise LuaError::Memory (which pcall catches as "not enough memory") once the table exceeds this size. Sized well above any realistic test workload (big.lua uses ~264K entries) while bounded enough that heavy.lua terminates within the harness timeout.