'numisinteger' (for table keys) replaced by 'luaV_tointeger' (old

'tointeger_aux'), which can do the same job.
This commit is contained in:
Roberto Ierusalimschy 2015-02-20 12:27:53 -02:00
parent 397ce11996
commit 81245b1ad5
3 changed files with 21 additions and 41 deletions

View File

@ -1,5 +1,5 @@
/* /*
** $Id: ltable.c,v 2.103 2015/02/16 13:15:00 roberto Exp roberto $ ** $Id: ltable.c,v 2.104 2015/02/20 14:05:01 roberto Exp roberto $
** Lua tables (hash) ** Lua tables (hash)
** See Copyright Notice in lua.h ** See Copyright Notice in lua.h
*/ */
@ -83,17 +83,6 @@ static const Node dummynode_ = {
}; };
/*
** Checks whether a float has a value representable as a lua_Integer
** (and does the conversion if so)
*/
static int numisinteger (lua_Number x, lua_Integer *p) {
if ((x) == l_floor(x)) /* integral value? */
return lua_numbertointeger(x, p); /* try as an integer */
else return 0;
}
/* /*
** Hash for floating-point numbers. ** Hash for floating-point numbers.
** The main computation should be just ** The main computation should be just
@ -455,14 +444,13 @@ TValue *luaH_newkey (lua_State *L, Table *t, const TValue *key) {
TValue aux; TValue aux;
if (ttisnil(key)) luaG_runerror(L, "table index is nil"); if (ttisnil(key)) luaG_runerror(L, "table index is nil");
else if (ttisfloat(key)) { else if (ttisfloat(key)) {
lua_Number n = fltvalue(key);
lua_Integer k; lua_Integer k;
if (luai_numisnan(n)) if (luaV_tointeger(key, &k, 0)) { /* index is int? */
luaG_runerror(L, "table index is NaN");
if (numisinteger(n, &k)) { /* index is int? */
setivalue(&aux, k); setivalue(&aux, k);
key = &aux; /* insert it as an integer */ key = &aux; /* insert it as an integer */
} }
else if (luai_numisnan(fltvalue(key)))
luaG_runerror(L, "table index is NaN");
} }
mp = mainposition(t, key); mp = mainposition(t, key);
if (!ttisnil(gval(mp)) || isdummy(mp)) { /* main position is taken? */ if (!ttisnil(gval(mp)) || isdummy(mp)) { /* main position is taken? */
@ -556,7 +544,7 @@ const TValue *luaH_get (Table *t, const TValue *key) {
case LUA_TNIL: return luaO_nilobject; case LUA_TNIL: return luaO_nilobject;
case LUA_TNUMFLT: { case LUA_TNUMFLT: {
lua_Integer k; lua_Integer k;
if (numisinteger(fltvalue(key), &k)) /* index is int? */ if (luaV_tointeger(key, &k, 0)) /* index is int? */
return luaH_getint(t, k); /* use specialized version */ return luaH_getint(t, k); /* use specialized version */
/* else go through */ /* else go through */
} }

24
lvm.c
View File

@ -1,5 +1,5 @@
/* /*
** $Id: lvm.c,v 2.233 2015/01/16 16:54:37 roberto Exp roberto $ ** $Id: lvm.c,v 2.234 2015/02/05 17:15:33 roberto Exp roberto $
** Lua virtual machine ** Lua virtual machine
** See Copyright Notice in lua.h ** See Copyright Notice in lua.h
*/ */
@ -31,16 +31,6 @@
#include "lvm.h" #include "lvm.h"
/*
** You can define LUA_FLOORN2I if you want to convert floats to integers
** by flooring them (instead of raising an error if they are not
** integral values)
*/
#if !defined(LUA_FLOORN2I)
#define LUA_FLOORN2I 0
#endif
/* limit for table tag-method chains (to avoid loops) */ /* limit for table tag-method chains (to avoid loops) */
#define MAXTAGLOOP 2000 #define MAXTAGLOOP 2000
@ -89,7 +79,7 @@ int luaV_tonumber_ (const TValue *obj, lua_Number *n) {
** mode == 1: takes the floor of the number ** mode == 1: takes the floor of the number
** mode == 2: takes the ceil of the number ** mode == 2: takes the ceil of the number
*/ */
static int tointeger_aux (const TValue *obj, lua_Integer *p, int mode) { int luaV_tointeger (const TValue *obj, lua_Integer *p, int mode) {
TValue v; TValue v;
again: again:
if (ttisfloat(obj)) { if (ttisfloat(obj)) {
@ -115,14 +105,6 @@ static int tointeger_aux (const TValue *obj, lua_Integer *p, int mode) {
} }
/*
** try to convert a value to an integer
*/
int luaV_tointeger_ (const TValue *obj, lua_Integer *p) {
return tointeger_aux(obj, p, LUA_FLOORN2I);
}
/* /*
** Try to convert a 'for' limit to an integer, preserving the ** Try to convert a 'for' limit to an integer, preserving the
** semantics of the loop. ** semantics of the loop.
@ -141,7 +123,7 @@ int luaV_tointeger_ (const TValue *obj, lua_Integer *p) {
static int forlimit (const TValue *obj, lua_Integer *p, lua_Integer step, static int forlimit (const TValue *obj, lua_Integer *p, lua_Integer step,
int *stopnow) { int *stopnow) {
*stopnow = 0; /* usually, let loops run */ *stopnow = 0; /* usually, let loops run */
if (!tointeger_aux(obj, p, (step < 0 ? 2 : 1))) { /* not fit in integer? */ if (!luaV_tointeger(obj, p, (step < 0 ? 2 : 1))) { /* not fit in integer? */
lua_Number n; /* try to convert to float */ lua_Number n; /* try to convert to float */
if (!tonumber(obj, &n)) /* cannot convert to float? */ if (!tonumber(obj, &n)) /* cannot convert to float? */
return 0; /* not a number */ return 0; /* not a number */

16
lvm.h
View File

@ -1,5 +1,5 @@
/* /*
** $Id: lvm.h,v 2.33 2014/07/30 14:42:44 roberto Exp roberto $ ** $Id: lvm.h,v 2.34 2014/08/01 17:24:02 roberto Exp roberto $
** Lua virtual machine ** Lua virtual machine
** See Copyright Notice in lua.h ** See Copyright Notice in lua.h
*/ */
@ -27,11 +27,21 @@
#endif #endif
/*
** You can define LUA_FLOORN2I if you want to convert floats to integers
** by flooring them (instead of raising an error if they are not
** integral values)
*/
#if !defined(LUA_FLOORN2I)
#define LUA_FLOORN2I 0
#endif
#define tonumber(o,n) \ #define tonumber(o,n) \
(ttisfloat(o) ? (*(n) = fltvalue(o), 1) : luaV_tonumber_(o,n)) (ttisfloat(o) ? (*(n) = fltvalue(o), 1) : luaV_tonumber_(o,n))
#define tointeger(o,i) \ #define tointeger(o,i) \
(ttisinteger(o) ? (*(i) = ivalue(o), 1) : luaV_tointeger_(o,i)) (ttisinteger(o) ? (*(i) = ivalue(o), 1) : luaV_tointeger(o,i,LUA_FLOORN2I))
#define intop(op,v1,v2) l_castU2S(l_castS2U(v1) op l_castS2U(v2)) #define intop(op,v1,v2) l_castU2S(l_castS2U(v1) op l_castS2U(v2))
@ -42,7 +52,7 @@ LUAI_FUNC int luaV_equalobj (lua_State *L, const TValue *t1, const TValue *t2);
LUAI_FUNC int luaV_lessthan (lua_State *L, const TValue *l, const TValue *r); LUAI_FUNC int luaV_lessthan (lua_State *L, const TValue *l, const TValue *r);
LUAI_FUNC int luaV_lessequal (lua_State *L, const TValue *l, const TValue *r); LUAI_FUNC int luaV_lessequal (lua_State *L, const TValue *l, const TValue *r);
LUAI_FUNC int luaV_tonumber_ (const TValue *obj, lua_Number *n); LUAI_FUNC int luaV_tonumber_ (const TValue *obj, lua_Number *n);
LUAI_FUNC int luaV_tointeger_ (const TValue *obj, lua_Integer *p); LUAI_FUNC int luaV_tointeger (const TValue *obj, lua_Integer *p, int mode);
LUAI_FUNC void luaV_gettable (lua_State *L, const TValue *t, TValue *key, LUAI_FUNC void luaV_gettable (lua_State *L, const TValue *t, TValue *key,
StkId val); StkId val);
LUAI_FUNC void luaV_settable (lua_State *L, const TValue *t, TValue *key, LUAI_FUNC void luaV_settable (lua_State *L, const TValue *t, TValue *key,