lua-users home
lua-l archive

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


The following inconsistency report is not of major importance.
But I think it should be corrected in Lua 5.3.

In Lua API, the basic C int type is essentially used for stack indexes
(why not) and for boolean values (since C89 does not have a bool
type).
The functions lua_rawgeti, lua_rawseti and lua_createtable somewhat
create an exception, as they have a "n" argument of type int also :

LUA_API void  (lua_rawgeti) (lua_State *L, int idx, int n);
LUA_API void  (lua_rawseti) (lua_State *L, int idx, int n);
LUA_API void  (lua_createtable) (lua_State *L, int narr, int nrec);

These arguments should be of type size_t.
First because a negative value does not make any sense.
Second, because on 64-bit platforms, there is no reason to limit the
size of tables to 2147483647 elements (provided you have plenty of
memory...)
And third, to be coherent with lua_rawlen, which returns a size_t value.

Quite often, I ended up needing an extra cast because of this
inconsistency, otherwise the compiler issue a truncation warning on
64-platforms.
Typical cases are:

size_t len = lua_rawlen(L, idx)
for(size_t i=0;i<len;i++)
{
  lua_rawgeti(L, idx, (int)i+1); // extra cast here
  ...
  lua_pop(L, 1);
}

or alternatively :

int len = (int) lua_rawlen(L, idx);  // extra cast here
for(int i=0;i<len;i++)
{
  lua_push_something(L, somevalue);
  lua_rawseti(L, idx, i+1);
}