The Primary Table Functions
The five functions listed in Table 3-1 provide most of the functionality for working with tables. With the exception of lua_newtable()
, note that the first parameter after L
is consistently the index of the table on the stack. Tables have key/value pairs, so I’ll use the letters k
and v
to stand for key and value, respectively. For these pairs, the value is consistently either pushed onto or popped from the top of the stack.
C API function | Description | Stack use |
---|---|---|
lua_newtable(L) |
Create a new empty table and push it onto the top of the stack. | [–0, +1] |
lua_setfield(L, <index> , <key> ) |
Set t[k] = v , where t is the table at <index> , k is the string <key> , and the value v is whatever is on top of the stack. |
[–1, +0] |
lua_getfield(L, <index> , <key> ) |
Push the value t[k] onto the top of the stack, where t is the table at <index> and k is the string <key> . Similar to Lua, nil is pushed onto the stack if <key> is not a key of table t . |
[–0, +1] |
lua_settable(L, <index> ) |
Set t[k] = v , where t is the table at <index> , k is at stack position –2, and v is at stack position –1. |
[–2, +0] |
lua_gettable(L, <index> ) |
Pop the value k from the top of the stack and then push the value t[k] onto the top of the stack, where t is the table at <index> . Similar to Lua, nil is pushed onto the stack if k is not a key of table t . |
[–1, +1] |
The function lua_settable()
can do everything that lua_setfield()
does. The difference is that lua_setfield()
accepts a string as a direct parameter, whereas lua_settable()
accepts a stack index to the key, instead, which allows it to work with either string or nonstring keys. The lua_setfield()
function exists because it’s convenient to write shorter code in the common case that your key is a string. The relationship between lua_gettable()
and lua_getfield()
is analogous.
As in Lua, you can delete a table by removing all references to it. The lua_pop()
function described in Table 3-2 pops as many elements as you like from the top of the stack.
C API function | Description | Stack use |
---|---|---|
lua_pop(L, n) |
Remove (pop) n elements from the top of the stack. |
[–n, +0] |
If the table you want to delete is on the top of the stack, you can delete it via C like so:
lua_pop(L, 1); // Pop the table from the stack. (╯°□°)╯︵ ┻━┻
A (hopefully short) span of time might pass before the garbage collector actually frees the associated memory.
You can attach special functions to Lua tables that effectively override setting and getting of new keys. These functions, called metamethods, are attached by being assigned as values in a table’s metatable, and by having a key that’s a language-recognized special name, such as __index
. You’ll see metamethods in action in Chapter 4.
When you use a new key to perform a table lookup or set a value in a table with an __index()
(for lookups) or __newindex()
(for setting values) metamethod, that metamethod is called instead of Lua directly looking up or setting the value. These lookup/setting metamethods also work from C when using the C API functions that we just learned about. You can circumvent these metamethods by calling the functions lua_rawget()
and lua_rawset()
, or, specifically for integer keys, lua_rawgeti()
and law_rawseti()
. For the sake of brevity (the soul of wit), these last two functions are not covered in further detail in this book.