Browse Source

small reorganization of 'luaV_flttointeger'/'luaV_tointeger'

pull/15/head
Roberto Ierusalimschy 7 years ago
parent
commit
465b474899
  1. 6
      lcode.c
  2. 9
      ltable.c
  3. 34
      lvm.c
  4. 8
      lvm.h

6
lcode.c

@ -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
** 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) {
TValue v;
setfltvalue(&v, f);
return (luaV_flttointeger(&v, fi, 0) && fitsBx(*fi));
return (luaV_flttointeger(f, fi, 0) && fitsBx(*fi));
}

9
ltable.c

@ -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)
** See Copyright Notice in lua.h
*/
@ -559,12 +559,13 @@ TValue *luaH_newkey (lua_State *L, Table *t, const TValue *key) {
TValue aux;
if (ttisnil(key)) luaG_runerror(L, "table index is nil");
else if (ttisfloat(key)) {
lua_Number f = fltvalue(key);
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);
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");
}
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_TNUMFLT: {
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 */
/* else... */
} /* FALLTHROUGH */

34
lvm.c

@ -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
** 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 == 2: takes the ceil of the number
*/
int luaV_flttointeger (const TValue *obj, lua_Integer *p, int mode) {
if (!ttisfloat(obj))
return 0;
else {
lua_Number n = fltvalue(obj);
int luaV_flttointeger (lua_Number n, lua_Integer *p, int mode) {
lua_Number f = l_floor(n);
if (n != f) { /* not an integral value? */
if (mode == 0) return 0; /* fails if mode demands integral value */
@ -103,23 +99,33 @@ int luaV_flttointeger (const TValue *obj, lua_Integer *p, int mode) {
}
return lua_numbertointeger(f, p);
}
/*
** try to convert a value to an integer, rounding according to 'mode',
** 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. ("Fast track" is handled
** by macro 'tointeger'.)
** try to convert a value to an integer.
*/
int luaV_tointeger (const TValue *obj, lua_Integer *p, int mode) {
TValue v;
if (cvt2num(obj) && luaO_str2num(svalue(obj), &v) == vslen(obj) + 1)
obj = &v; /* change string to its corresponding number */
if (ttisinteger(obj)) {
*p = ivalue(obj);
return 1;
}
else
return luaV_flttointeger(obj, p, mode);
return luaV_tointegerns(obj, p, mode);
}

8
lvm.h

@ -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
** See Copyright Notice in lua.h
*/
@ -55,8 +55,7 @@
/* convert an object to an integer (without string coercion) */
#define tointegerns(o,i) \
(ttisinteger(o) ? (*(i) = ivalue(o), 1) \
: luaV_flttointeger(o,i,LUA_FLOORN2I))
(ttisinteger(o) ? (*(i) = ivalue(o), 1) : luaV_tointegerns(o,i,LUA_FLOORN2I))
#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_tonumber_ (const TValue *obj, lua_Number *n);
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,
StkId val, const TValue *slot);
LUAI_FUNC void luaV_finishset (lua_State *L, const TValue *t, TValue *key,

Loading…
Cancel
Save