I’m new to Lua. I’m in the process of
embedding Lua within an application I wrote. The only question I have at
the moment is when I load a library the table is left on the stack, is it okay to
pop it off? All the example code I’ve seen never do this. Isn’t
the stack space limited?
> The only question I have at the moment is when I
> load a library the table is left on the stack, is it okay to pop it off?
The table is typically returned on the stack so that it may be named by
In the case of the standard libraries (base, table, etc.), the names are
hard coded using luaL_register(), but they still follow the convention
of leaving the table on the stack to be used by the caller if needed.
The use of 5 stack slots is not a big deal during the luaopen_XXX()
calls for the standard libraries.
For my own (5.0) code I use the code below, where 'lib' is a C array of
open/name pairs. It allows me to mix and match the new method with the
old (back when the library tables were NOT left on the stack).
for (; lib->open != NULL; lib++)
* If there's no name, just run the opener. Otherwise, push the
* name, check that there's something on the stack, then put it
* in the global table.