reallycheckint(L,index)

classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

reallycheckint(L,index)

Dirk Laurie-2
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.

Reply | Threaded
Open this post in threaded view
|

Re: reallycheckint(L,index)

Tim Hill
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.
>


Reply | Threaded
Open this post in threaded view
|

Re: reallycheckint(L,index)

Dirk Laurie-2
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.

Reply | Threaded
Open this post in threaded view
|

Re: reallycheckint(L,index)

Roberto Ierusalimschy
> [...] It was quite a shock
> to discover that luaL_checkint does not actually check that
> something is an integer.

Nothing checks whether something is an "integer", because Lua does
not have integers (yet ;). To check whether some value is a number,
you can use lua_type(L, index) == LUA_TNUMBER. To check whether that
number has an integer value, you can try

  floor(lua_tonumber(L, index)) == lua_tonumber(L, index)

(I am calling lua_tonumber twice to avoid using a variable.) It is
up to you to decide whether inf/-inf are really integers.

-- Roberto

Reply | Threaded
Open this post in threaded view
|

Re: reallycheckint(L,index)

Dirk Laurie-2
2013/5/29 Roberto Ierusalimschy <[hidden email]>:

>> [...] It was quite a shock
>> to discover that luaL_checkint does not actually check that
>> something is an integer.
>
> Nothing checks whether something is an "integer", because Lua does
> not have integers (yet ;). To check whether some value is a number,
> you can use lua_type(L, index) == LUA_TNUMBER. To check whether that
> number has an integer value, you can try
>
>   floor(lua_tonumber(L, index)) == lua_tonumber(L, index)

Well, what I really need to check is something much more restrictive:
whether an index into a "proper sequence" is in bounds. And that task
is beautifully simple in straight Lua.

1. Table is initialized with something other than nil (zero, NaN,
   whatever).
2. Metatable with __index, __newindex is assigned after that.
3. Any call that reaches __index or __newindex with a numeric key is
   by definition illegal (Lua "knows what to do" at the legal indices).

       x=Range(6)
       print(x) --> 1 2 3 4 5 6
       x[3]=60        -- no error
       x[math.pi]=180
    stdin:1: bad argument #2 to '__newindex' (index out of range)