lua-users home
lua-l archive

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


I might instead use luaL_len() from the auxiliary lib, but perhaps it does the same thing -- as most of those luaL functions are macros or functions that perform a series of operations accomplished with the lua_ functions.

Afaik luaL_len() does not push anything on the stack, it just returns an int for the length of the object.  It probably calls lua_len() then pops that value and returns int (lua_tointeger()).

-peace-  :]


On Tue, Nov 19, 2013 at 3:26 PM, Marc Lepage <mlepage@antimeta.com> wrote:
In Lua a good way to check if a table t is empty is to check if next(t) == nil.

What is the best way to check if a table is empty using the C API? Is it basically the same technique?

    // table is on stack at index t
    lua_pushnil(L); // nil key
    if (lua_next(L, t))
    {
        // not empty
        lua_pop(L, 2);
    }
    else
    {
       // empty
    }

Or is there a better way?