Type System & Tagged Values
PyLua is a dynamically typed language where values have types, not variables. Under the hood, every PyLua value is represented as a high-performance 16-byte Tagged Value (TValue).
1. The Fundamental Types
PyLua provides 8 basic Lua types along with native vector extensions:
| Type | type() Return |
Internal Tag | Description |
|---|---|---|---|
nil |
"nil" |
LUA_TNIL |
Represents non-existence or the absence of a useful value. Unassigned variables and missing table keys default to nil. |
boolean |
"boolean" |
LUA_TBOOLEAN |
Holds either true or false. In conditionals, only nil and false are falsey; everything else (including 0 and "") is truthy. |
number |
"number" |
LUA_TNUMBER |
Subtyped into 64-bit integer (LUA_VNUMINT) and 64-bit double float (LUA_VNUMFLT). Supports automatic integer coercion. |
string |
"string" |
LUA_TSTRING |
Immutable byte array. Short strings (≤ 40 bytes) are globally interned for \(O(1)\) equality comparisons; long strings use lazy hashing. |
table |
"table" |
LUA_TTABLE |
The sole data-structuring mechanism. Dual-memory layout with contiguous 1-indexed array part and robin-hood/open-addressing hash table. |
function |
"function" |
LUA_TFUNCTION |
First-class callable closures. Subtyped into C closures (CClosure) and PyLua bytecode closures (LClosure). |
userdata |
"userdata" |
LUA_TUSERDATA |
Raw C/C++ memory blocks managed by PyLua's garbage collector. Used for OS window handles, physics bodies, textures, and vectors. |
thread |
"thread" |
LUA_TTHREAD |
First-class coroutine execution contexts with independent stacks. Managed cooperatively via coroutine.resume(). |
2. Tagged Value Representation (`TValue`)
In include/lobject.h, every PyLua value is defined as a tagged union:
struct TValue {
Value value_; /* 8 bytes: double, int64, GCObject*, or void* */
lu_byte tt_; /* 1 byte: Basic type tag (0..8) & variant flags */
/* 7 bytes padding on 64-bit architectures = 16 bytes total */
};
typedef union Value {
GCObject *gc; /* Pointers to collected objects: String, Table, Closure, Userdata */
void *p; /* Light userdata raw pointer */
lua_CFunction f; /* Direct C function pointer */
lua_Integer i; /* 64-bit signed integer */
lua_Number n; /* 64-bit IEEE 754 floating point number */
} Value;
Because values are packed into 16 bytes, register-to-register copies (via opcode OP_MOVE) compile down to a pair of 64-bit CPU load and store instructions, achieving native execution speeds without pointer indirection for numbers, booleans, and nil.
3. Numbers: Integer vs Float Duality
PyLua automatically distinguishes between integers and floating-point numbers:
x = 100 -- Integer subtype (LUA_VNUMINT)
y = 3.14 -- Float subtype (LUA_VNUMFLT)
print(math.type(x)) -- Output: "integer"
print(math.type(y)) -- Output: "float"
-- Division behavior:
print(7 / 2) -- 3.5 (Float division)
print(7 // 2) -- 3 (Floor division preserves integer type)
Bitwise operations (&, |, ~, <<, >>) and table array indices strictly require integers. If a float has an exact integer representation (e.g. 4.0), PyLua automatically converts it into an integer without precision loss.
4. Tables: Dual-Memory Architecture
Tables in PyLua serve as arrays, hash maps, structs, sets, objects, and namespaces simultaneously. To achieve maximum performance and minimum cache misses, each table internally consists of:
- The Array Part: A contiguous C array storing keys from
1to \(N\). Element access viat[i]performs direct pointer arithmetic without hashing. - The Hash Part: An open-addressing hash table with collision node linking for arbitrary keys (strings, objects, negative numbers).
-- Dual table: contiguous array + keyed dictionary
player = {
10, 20, 30, -- Stored in the fast array part (keys 1, 2, 3)
name = "Aria", -- Stored in the hash part
health = 100,
isAlive = true
}
print(player[1]) -- Fast array access: 10
print(player.name) -- Hash lookup: "Aria"
5. Metatables & Object-Oriented Programming
Every table and userdata can have an associated metatable containing metamethods that intercept operations like addition, indexing, stringification, and garbage collection:
| Metamethod | Triggered Operation | Behavior Description |
|---|---|---|
__index |
t[k] or t.k |
Invoked when reading a key that does not exist in table t. Can be a fallback function or another table (prototype inheritance). |
__newindex |
t[k] = v |
Invoked when assigning a value to a non-existent key. |
__add, __sub, etc. |
a + b, a - b |
Overloads mathematical and bitwise operators. |
__tostring |
tostring(t) |
Custom string representation for logging and printing. |
__call |
t(...) |
Allows invoking a table as if it were a function. |
__gc |
Garbage collection | Destructor finalizer called before the object memory is reclaimed. |
Character = {}
Character.__index = Character
function Character.new(name, hp) {
instance = setmetatable({}, Character)
instance.name = name
instance.hp = hp
return instance
}
function Character:takeDamage(dmg) {
self.hp = self.hp - dmg
print(self.name .. " took " .. dmg .. " damage! HP: " .. self.hp)
}
hero = Character.new("Paladin", 150)
hero:takeDamage(25)