[Date Prev][Date Next][Thread Prev][Thread Next]
- Subject: Re: reallycheckint(L,index)
- From: Dirk Laurie <dirk.laurie@...>
- Date: Wed, 29 May 2013 23:10:38 +0200
2013/5/29 Roberto Ierusalimschy <email@example.com>:
>> [...] 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,
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).
print(x) --> 1 2 3 4 5 6
x=60 -- no error
stdin:1: bad argument #2 to '__newindex' (index out of range)