lua-users home
lua-l archive

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


Dive Florida wrote:
> I intend to write a function that can take a table as input, but I
> afiled to pass the table parameter. Can someone help me? 
> 
> [...]
> 
> bool parse_pa( lua_State* L, int id, int i, const char *name,
>    std::list<int>& numberlist, std::map<std::string,
>         vector<OwnStruct*> >& pmap) { if (lua_istable(L, i)) {
>         lua_pushnil(L); while (lua_next(L, i)) {
>                  if (lua_isnumber(L, -1)) {
>                           int x = lua_tonumber(L, -1);
>                           numberlist.push_back(x);
>                  }
>                  } else if (luaL_checkudata(L, -1, MYOWNTYPE)) {
>                       p =
>                       reinterpret_cast<OwnStruct*>(lua_touserdata(L,
>                       -1)); std::vector<OwnStruct*> v; v.push_back(p);
>                       pmap[name] = v;
>             } else if (lua_istable(L, -1)) {
>                 if (!parse_pa(L, vid, i, name, numberlist, pmap)) {
>                     lua_pop(L, 2);
>                     return  false;
>                 }
>             } else {
>                  lua_pop(L, 2);
>                 r eturn  false;
>             }
>             lua_pop(L, 1);
>         }
>         return true;
>      }
>      return false;
> }
> 
> [...]
> 
> Can anyone help me to find out how to parse Func(1, {key={1, 2, 3,
> 4}, key1={p1, p2, p3}, key2={{5, 6, 7, 8}, p4, p5, p6}}, 2); ? 

Assuming the copy/paste accident in parse_pa was an if/elseif series
inside the while looking like:

if (lua_isnumber(L, -1)) {
    /* ... */
} else if (luaL_checkudata(L, -1, MYOWNTYPE)) {
    /* ... */
} else if (lua_istable(L, -1)) {
    /* ... */
} else {
    /* ... */
}

In this code, luaL_checkudata will throw an error if the value is not a
userdata of that type, so whatever the input, lua_istable(L, -1) check
is never called. In your failing input it appears that the failing case
is when there is a subtable in the object table, so that may be your
problem (but since your email is lacking crucial information I can't be
sure). Write a non-throwing function luaL_isudata and use it instead.