small reorganization of 'luaV_flttointeger'/'luaV_tointeger'

This commit is contained in:
Roberto Ierusalimschy 2018-02-21 09:54:26 -03:00
parent 6353d619a5
commit 465b474899
4 changed files with 39 additions and 34 deletions

View File

@ -1,5 +1,5 @@
/* /*
** $Id: lcode.c,v 2.154 2018/02/15 15:34:29 roberto Exp roberto $ ** $Id: lcode.c,v 2.155 2018/02/17 19:20:00 roberto Exp roberto $
** Code generator for Lua ** Code generator for Lua
** See Copyright Notice in lua.h ** See Copyright Notice in lua.h
*/ */
@ -605,9 +605,7 @@ void luaK_int (FuncState *fs, int reg, lua_Integer i) {
static int floatI (lua_Number f, lua_Integer *fi) { static int floatI (lua_Number f, lua_Integer *fi) {
TValue v; return (luaV_flttointeger(f, fi, 0) && fitsBx(*fi));
setfltvalue(&v, f);
return (luaV_flttointeger(&v, fi, 0) && fitsBx(*fi));
} }

View File

@ -1,5 +1,5 @@
/* /*
** $Id: ltable.c,v 2.131 2018/01/28 15:13:26 roberto Exp roberto $ ** $Id: ltable.c,v 2.132 2018/02/19 20:06:56 roberto Exp roberto $
** Lua tables (hash) ** Lua tables (hash)
** See Copyright Notice in lua.h ** See Copyright Notice in lua.h
*/ */
@ -559,12 +559,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 f = fltvalue(key);
lua_Integer k; lua_Integer k;
if (luaV_flttointeger(key, &k, 0)) { /* does index fit in an integer? */ if (luaV_flttointeger(f, &k, 0)) { /* does key fit in an integer? */
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))) else if (luai_numisnan(f))
luaG_runerror(L, "table index is NaN"); luaG_runerror(L, "table index is NaN");
} }
mp = mainpositionTV(t, key); mp = mainpositionTV(t, key);
@ -669,7 +670,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 (luaV_flttointeger(key, &k, 0)) /* index is an integral? */ if (luaV_flttointeger(fltvalue(key), &k, 0)) /* index is an integral? */
return luaH_getint(t, k); /* use specialized version */ return luaH_getint(t, k); /* use specialized version */
/* else... */ /* else... */
} /* FALLTHROUGH */ } /* FALLTHROUGH */

36
lvm.c
View File

@ -1,5 +1,5 @@
/* /*
** $Id: lvm.c,v 2.341 2018/02/17 19:20:00 roberto Exp roberto $ ** $Id: lvm.c,v 2.342 2018/02/19 20:06:56 roberto Exp roberto $
** Lua virtual machine ** Lua virtual machine
** See Copyright Notice in lua.h ** See Copyright Notice in lua.h
*/ */
@ -90,11 +90,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
*/ */
int luaV_flttointeger (const TValue *obj, lua_Integer *p, int mode) { int luaV_flttointeger (lua_Number n, lua_Integer *p, int mode) {
if (!ttisfloat(obj))
return 0;
else {
lua_Number n = fltvalue(obj);
lua_Number f = l_floor(n); lua_Number f = l_floor(n);
if (n != f) { /* not an integral value? */ if (n != f) { /* not an integral value? */
if (mode == 0) return 0; /* fails if mode demands integral value */ if (mode == 0) return 0; /* fails if mode demands integral value */
@ -102,24 +98,34 @@ int luaV_flttointeger (const TValue *obj, lua_Integer *p, int mode) {
f += 1; /* convert floor to ceil (remember: n != f) */ f += 1; /* convert floor to ceil (remember: n != f) */
} }
return lua_numbertointeger(f, p); return lua_numbertointeger(f, p);
}
} }
/* /*
** try to convert a value to an integer. ("Fast track" is handled ** try to convert a value to an integer, rounding according to 'mode',
** by macro 'tointeger'.) ** without string coercion.
** ("Fast track" handled by macro 'tointegerns'.)
*/
int luaV_tointegerns (const TValue *obj, lua_Integer *p, int mode) {
if (ttisfloat(obj))
return luaV_flttointeger(fltvalue(obj), p, mode);
else if (ttisinteger(obj)) {
*p = ivalue(obj);
return 1;
}
else
return 0;
}
/*
** try to convert a value to an integer.
*/ */
int luaV_tointeger (const TValue *obj, lua_Integer *p, int mode) { int luaV_tointeger (const TValue *obj, lua_Integer *p, int mode) {
TValue v; TValue v;
if (cvt2num(obj) && luaO_str2num(svalue(obj), &v) == vslen(obj) + 1) if (cvt2num(obj) && luaO_str2num(svalue(obj), &v) == vslen(obj) + 1)
obj = &v; /* change string to its corresponding number */ obj = &v; /* change string to its corresponding number */
if (ttisinteger(obj)) { return luaV_tointegerns(obj, p, mode);
*p = ivalue(obj);
return 1;
}
else
return luaV_flttointeger(obj, p, mode);
} }

8
lvm.h
View File

@ -1,5 +1,5 @@
/* /*
** $Id: lvm.h,v 2.48 2017/11/29 13:02:17 roberto Exp roberto $ ** $Id: lvm.h,v 2.49 2018/02/19 20:06:56 roberto Exp roberto $
** Lua virtual machine ** Lua virtual machine
** See Copyright Notice in lua.h ** See Copyright Notice in lua.h
*/ */
@ -55,8 +55,7 @@
/* convert an object to an integer (without string coercion) */ /* convert an object to an integer (without string coercion) */
#define tointegerns(o,i) \ #define tointegerns(o,i) \
(ttisinteger(o) ? (*(i) = ivalue(o), 1) \ (ttisinteger(o) ? (*(i) = ivalue(o), 1) : luaV_tointegerns(o,i,LUA_FLOORN2I))
: luaV_flttointeger(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))
@ -106,7 +105,8 @@ 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, int mode); LUAI_FUNC int luaV_tointeger (const TValue *obj, lua_Integer *p, int mode);
LUAI_FUNC int luaV_flttointeger (const TValue *obj, lua_Integer *p, int mode); LUAI_FUNC int luaV_tointegerns (const TValue *obj, lua_Integer *p, int mode);
LUAI_FUNC int luaV_flttointeger (lua_Number n, lua_Integer *p, int mode);
LUAI_FUNC void luaV_finishget (lua_State *L, const TValue *t, TValue *key, LUAI_FUNC void luaV_finishget (lua_State *L, const TValue *t, TValue *key,
StkId val, const TValue *slot); StkId val, const TValue *slot);
LUAI_FUNC void luaV_finishset (lua_State *L, const TValue *t, TValue *key, LUAI_FUNC void luaV_finishset (lua_State *L, const TValue *t, TValue *key,