lua-users home
lua-l archive

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


Thank you, Jerome. It was a copy/paste error.

  luaL_checkudata() is not an issue in my case. In my example, Func(1, {key={1, 2, 3,
> 4}, key1={p1, p2, p3}, key2={{5, 6, 7, 8}, p4, p5, p6}}, 2),
   
  At first it finds the first key key1, then goes to find out usedata p1, p2 and p3. The issue is it won't go on to find the next key, but instead try to find p4 or 1 in this case.
   
  
Jerome Vuarand <jerome.vuarand@ubisoft.com> wrote:
  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& numberlist, std::map> vector >& 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(lua_touserdata(L,
> -1)); std::vector 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.


       
---------------------------------
Looking for last minute shopping deals?  Find them fast with Yahoo! Search.