lua-users home
lua-l archive

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




On Wed, Aug 31, 2011 at 9:21 AM, Luiz Henrique de Figueiredo <lhf@tecgraf.puc-rio.br> wrote:
> But how do you validate, say, an index you give to a C function that
> queries the Lua stack?

Use lua_type. The manual says that it returns LUA_TNONE for a non-valid index.

Again, the ambiguity between acceptable and invalid: 

 "Returns the type of the value in the given acceptable index, or LUA_TNONE for a non-valid index (that is, an index to an "empty" stack position)." 

AFAICT, "non-valid" here, and in all Lua docs, refers to an index which satisfies lua_gettop() < index <= stackspace. If stackspace is 30, and lua_gettop() is 20, you have 10 "empty" slots left on Lua stack. The above says that lua_type(L, 21) will return LUA_TNONE, but lua_type(L, 31) has undefined behavior. 

Since there doesn't seem to be a way to check index <= stackspace, your own code has to call lua_type only if index <= lua_gettop(), and return LUA_TNONE (or whatever, as appropriate) if not. This seems to have bitten many Lua C API users, as it applies to all Lua C API, not just lua_type. 

Oliver