lua_setfield popping the table from the stack?
[
Date Prev][
Date Next][
Thread Prev][
Thread Next]
[
Date Index]
[
Thread Index]
- Subject: lua_setfield popping the table from the stack?
- From: "Lua" <lua@...>
- Date: 2006年8月24日 18:44:13 -0400
Hi,
My goal is to export a set of constants under various tables. In the example below, I'm looking for something like this:
MyAppRootTable
Foo (Value = 5)
OpenTypes
Path (Value = eOTPath)
User (Value = eOTUser)
In order to accomplish this, I wrote "lua_RegisterValues()" as below. However, it seems as though lua_setfield() pops the table from the stack whereas the documentation says it only pops the value. Furthermore, it seems this only happens when I'm setting the field to a table. In other words, the following code works correctly, but I'm not sure why I need the line with the ***** next to it.
Thanks for any insight you may have,
James.
---
typedef struct tagExportedNames {
char* pName;
const tagExportedNames * pSubNames;
unsigned long Value;
} tExportedNames;
static const tExportedNames OpenTypeList[] =
{
{"Path",NULL,eOTPath},
{"User",NULL,eOTUser},
{NULL,NULL,0}
};
static const tExportedNames FCList[] =
{
{"Foo",NULL,5},
{"OpenTypes",OpenTypeList,0},
{NULL,NULL,0}
};
static void lua_RegisterValues(lua_State*L,const tExportedNames* pExportedNames)
{
while (pExportedNames->pName!=NULL) {
if (pExportedNames->pSubNames!=NULL) {
lua_newtable(L);
lua_pushvalue(L,-1); ----********
lua_setfield(L,-3,pExportedNames->pName);
lua_RegisterValues(L,pExportedNames->pSubNames);
lua_pop(L,1);
} else {
lua_pushinteger(L,pExportedNames->Value);
lua_setfield(L,-2,pExportedNames->pName);
}
pExportedNames++;
}
}