lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


I would suggest looking at http://www.lua.org/pil/24.2.html (The Stack).  Most
of the lua C API operates on values on the stack.  I use
http://pgl.yoyo.org/luai/i/_
as a reference when I need to remember what the functions do.  Stack programming
takes a bit of getting used to, and some of the lua C api may do thinks you are
not expecting until you get a bit more comfortable with it.

To create a new global table named "tablename" in C you can do:

lua_newtable(L);                    //[{}]
lua_setglobal(L, "tablename");      //

If you want to create the table AND add a c function to it.

lua_newtable(L);                    //[{}]
lua_pushcfunction(L, someFunc);     //[somefunc, {}]
lua_setfield(L, -2, "someFuncName");//[{somefuncName=somefunc}]
lua_setglobal(L, "tablename");      //[]

if you need to access an existing table and add a c function:
lua_getglobal(L, "tablename");      //[{}]
lua_pushcfunction(L, someFunc);     //[somefunc, {}]
lua_setfield(L, -2, "someFuncName");//[{somefuncName=somefunc}]
lua_pop(L, 1);                      //[]

When I first started writing lua C code it helped me to put in comments what the
stack looks like after every command.