lua-users home
lua-l archive

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


noel frankinet wrote:
>   void DecodeTable(lua_State *L,Cdbview &dbview )
>   {
>        lua_pushnil(L);
>        while (lua_next(L,-2) != 0) {
>         if lua_istable(L,-1)
>             DecodeField(L,dbview);
>         lua_pop(L, 1); // <-- here (2)
>        }
>        lua_pop(L, 1);
>   }
>   void DecodeField(lua_State *L,Cdbview &dbview )
>   {
>        lua_pushnil(L);
>        string name;
>        string type;
>        int t(0);
>        while (lua_next(L, -2) != 0) {
>         switch(t)
>         {case 0 : name = luaL_checkstring(L, -1); break;
>          case 1 : type = luaL_checkstring(L, -1); break;
>          case 2 : break;// constraint -- TODO
>         }
>         t++;
>         lua_pop(L, 1);
>        }
>        if (t>1)
>        {
>         Cdbcolumn c(name);
>         c.type_name(type.c_str());
>         dbview.add(c);
>        }
>        lua_pop(L, 1); // <-- here (1)
>   }
> 
> 
> The stack gets corrupted in the DecodeField function, and I get I have
> lost track of the lua stack state.
> Could you help, please.

What are you poping at the end of DecodeField (see (1) above) ? When
lua_next returns 0 it leaves nothing on the stack, so that pop removes
the subtable from the stack. Then you remove the key of that subtable
(see (2) above), which probably confuses the outer lua_next.