tags:

views:

132

answers:

3

I am using the lua C-API to read in configuration data that is stored in a lua file.

I've got a nice little table in the file and I've written a query C-function that parses out a specific field in the table. (yay it works!)

It works by calling a few of these kinds of functions over and over:

... 
lua_getglobal (...); 
lua_pushinteger (...); 
lua_gettable (...); 
lua_pushstring (...); 
lua_gettable (...); 
lua_lua_getfield (...);
...

you get the idea.

After I am done querying my data like this, do I have to clean up the stack?

+1  A: 

It depends.

If your C function is called from Lua, the values you leave behind will be the values that your C function returns to Lua. If your C function is called by another C function that uses the Lua stack, then those values will still be on the stack, and you can do anything or nothing with them.

But if after you've called your C function you're done with Lua altogether, and from your question it sounds as though you are, then you don't have to clean up your Lua stack. Just close the Lua context, and it will clean up your stack for you.

David Seiler
+5  A: 
Norman Ramsey
+1  A: 

additionally, instead of using pushstring or pushinteger followed by a gettable, use lua_getfield and lua_rawgeti respectivly (the raw will not invoke metamethods though, incase you wanted that...)

daurnimator