[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: RE: How to pass table
- From: Dive Florida <divefloridaus@...>
- Date: Fri, 15 Feb 2008 14:15:24 -0800 (PST)
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.
Could you please tell me how to write a non-throwing function luaL_isudata? I'm thinking about using:
void luaL_checktype (lua_State *L, int narg, int t);
---------------------------------
Looking for last minute shopping deals? Find them fast with Yahoo! Search.