# reallycheckint(L,index)

5 messages
Open this post in threaded view
|

## reallycheckint(L,index)

 I can't believe the following is the best way of not happily accepting pi=3 etc. Too many calls to genuine functions as distinct from macros. #define reallycheckint(L,index) (\   lua_pushinteger(L,luaL_checkint(L,index)),\   luaL_argcheck(L,lua_compare(L,index,-1,LUA_OPEQ),index,"expected integer"),\   lua_pop(L,1), lua_tointeger(L,index) ) But every time I try to improve on it I need to store something in a C variable.
Open this post in threaded view
|

## Re: reallycheckint(L,index)

 I'm trying to figure out what you're checking (it's late here!) .. a Lua number that has no fractional part (with all the issues that can cause) or a Lua number that can be stored within a C "int" without overflow? pi==3 would make life so much easier … --Tim On May 28, 2013, at 11:22 PM, Dirk Laurie <[hidden email]> wrote: > I can't believe the following is the best way of not happily > accepting pi=3 etc. Too many calls to genuine functions as > distinct from macros. > > #define reallycheckint(L,index) (\ >  lua_pushinteger(L,luaL_checkint(L,index)),\ >  luaL_argcheck(L,lua_compare(L,index,-1,LUA_OPEQ),index,"expected integer"),\ >  lua_pop(L,1), lua_tointeger(L,index) ) > > But every time I try to improve on it I need to store something > in a C variable. >
Open this post in threaded view
|

## Re: reallycheckint(L,index)

 2013/5/29 Tim Hill <[hidden email]>: > I'm trying to figure out what you're checking (it's late here!) .. > a Lua number that has no fractional part (with all the issues > that can cause) or a Lua number that can be stored within > a C "int" without overflow? I am writing an __index/__newindex pair that checks the "proper sequence" property: the numeric keys form the exact set 1..n. However, the table is allowed to have string keys (for example, `n` is one.) It was quite a shock to discover that luaL_checkint does not actually check that something is an integer. But I have in the meantime thought of an elegant solution to the problem. I create the table at the outset and fill it with NaN. Then if the metamethod is invoked with a numeric key, it can only mean the key is not in the table. That implies that either it is out of the allowable set or that nil was at some time assigned into the table, both of which are errors. And I still have the telltale NaN to warn me that a key has not had a value assigned to it.