1
0
mirror of https://github.com/lua/lua.git synced 2025-01-28 06:03:00 +08:00

Using an enumeration for float->integer coercion modes

This commit is contained in:
Roberto Ierusalimschy 2019-12-05 14:14:29 -03:00
parent 2d92102dee
commit d30569c064
4 changed files with 30 additions and 22 deletions

View File

@ -653,7 +653,7 @@ void luaK_int (FuncState *fs, int reg, lua_Integer i) {
static void luaK_float (FuncState *fs, int reg, lua_Number f) { static void luaK_float (FuncState *fs, int reg, lua_Number f) {
lua_Integer fi; lua_Integer fi;
if (luaV_flttointeger(f, &fi, 0) && fitsBx(fi)) if (luaV_flttointeger(f, &fi, F2Ieq) && fitsBx(fi))
luaK_codeAsBx(fs, OP_LOADF, reg, cast_int(fi)); luaK_codeAsBx(fs, OP_LOADF, reg, cast_int(fi));
else else
luaK_codek(fs, reg, luaK_numberK(fs, f)); luaK_codek(fs, reg, luaK_numberK(fs, f));
@ -1220,7 +1220,7 @@ static int isSCnumber (expdesc *e, int *pi, int *isfloat) {
lua_Integer i; lua_Integer i;
if (e->k == VKINT) if (e->k == VKINT)
i = e->u.ival; i = e->u.ival;
else if (e->k == VKFLT && luaV_flttointeger(e->u.nval, &i, 0)) else if (e->k == VKFLT && luaV_flttointeger(e->u.nval, &i, F2Ieq))
*isfloat = 1; *isfloat = 1;
else else
return 0; /* not a number */ return 0; /* not a number */

View File

@ -626,7 +626,7 @@ TValue *luaH_newkey (lua_State *L, Table *t, const TValue *key) {
else if (ttisfloat(key)) { else if (ttisfloat(key)) {
lua_Number f = fltvalue(key); lua_Number f = fltvalue(key);
lua_Integer k; lua_Integer k;
if (luaV_flttointeger(f, &k, 0)) { /* does key fit in an integer? */ if (luaV_flttointeger(f, &k, F2Ieq)) { /* 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 */
} }
@ -745,7 +745,7 @@ const TValue *luaH_get (Table *t, const TValue *key) {
case LUA_TNIL: return &absentkey; case LUA_TNIL: return &absentkey;
case LUA_TNUMFLT: { case LUA_TNUMFLT: {
lua_Integer k; lua_Integer k;
if (luaV_flttointeger(fltvalue(key), &k, 0)) /* index is an integral? */ if (luaV_flttointeger(fltvalue(key), &k, F2Ieq)) /* integral index? */
return luaH_getint(t, k); /* use specialized version */ return luaH_getint(t, k); /* use specialized version */
/* else... */ /* else... */
} /* FALLTHROUGH */ } /* FALLTHROUGH */

25
lvm.c
View File

@ -116,16 +116,13 @@ int luaV_tonumber_ (const TValue *obj, lua_Number *n) {
/* /*
** try to convert a float to an integer, rounding according to 'mode': ** try to convert a float to an integer, rounding according to 'mode'.
** mode == 0: accepts only integral values
** mode == 1: takes the floor of the number
** mode == 2: takes the ceil of the number
*/ */
int luaV_flttointeger (lua_Number n, lua_Integer *p, int mode) { int luaV_flttointeger (lua_Number n, lua_Integer *p, F2Imod mode) {
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 == F2Ieq) return 0; /* fails if mode demands integral value */
else if (mode == 2) /* needs ceil? */ else if (mode == F2Iceil) /* needs ceil? */
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);
@ -137,7 +134,7 @@ int luaV_flttointeger (lua_Number n, lua_Integer *p, int mode) {
** without string coercion. ** without string coercion.
** ("Fast track" handled by macro 'tointegerns'.) ** ("Fast track" handled by macro 'tointegerns'.)
*/ */
int luaV_tointegerns (const TValue *obj, lua_Integer *p, int mode) { int luaV_tointegerns (const TValue *obj, lua_Integer *p, F2Imod mode) {
if (ttisfloat(obj)) if (ttisfloat(obj))
return luaV_flttointeger(fltvalue(obj), p, mode); return luaV_flttointeger(fltvalue(obj), p, mode);
else if (ttisinteger(obj)) { else if (ttisinteger(obj)) {
@ -152,7 +149,7 @@ int luaV_tointegerns (const TValue *obj, lua_Integer *p, int mode) {
/* /*
** try to convert a value to an integer. ** 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, F2Imod mode) {
TValue v; TValue v;
if (l_strton(obj, &v)) /* does 'obj' point to a numerical string? */ if (l_strton(obj, &v)) /* does 'obj' point to a numerical string? */
obj = &v; /* change it to point to its corresponding number */ obj = &v; /* change it to point to its corresponding number */
@ -178,7 +175,7 @@ int luaV_tointeger (const TValue *obj, lua_Integer *p, int mode) {
*/ */
static int forlimit (lua_State *L, lua_Integer init, const TValue *lim, static int forlimit (lua_State *L, lua_Integer init, const TValue *lim,
lua_Integer *p, lua_Integer step) { lua_Integer *p, lua_Integer step) {
if (!luaV_tointeger(lim, p, (step < 0 ? 2 : 1))) { if (!luaV_tointeger(lim, p, (step < 0 ? F2Iceil : F2Ifloor))) {
/* not coercible to in integer */ /* not coercible to in integer */
lua_Number flim; /* try to convert to float */ lua_Number flim; /* try to convert to float */
if (!tonumber(lim, &flim)) /* cannot convert to float? */ if (!tonumber(lim, &flim)) /* cannot convert to float? */
@ -417,7 +414,7 @@ static int LTintfloat (lua_Integer i, lua_Number f) {
return luai_numlt(cast_num(i), f); /* compare them as floats */ return luai_numlt(cast_num(i), f); /* compare them as floats */
else { /* i < f <=> i < ceil(f) */ else { /* i < f <=> i < ceil(f) */
lua_Integer fi; lua_Integer fi;
if (luaV_flttointeger(f, &fi, 2)) /* fi = ceil(f) */ if (luaV_flttointeger(f, &fi, F2Iceil)) /* fi = ceil(f) */
return i < fi; /* compare them as integers */ return i < fi; /* compare them as integers */
else /* 'f' is either greater or less than all integers */ else /* 'f' is either greater or less than all integers */
return f > 0; /* greater? */ return f > 0; /* greater? */
@ -434,7 +431,7 @@ static int LEintfloat (lua_Integer i, lua_Number f) {
return luai_numle(cast_num(i), f); /* compare them as floats */ return luai_numle(cast_num(i), f); /* compare them as floats */
else { /* i <= f <=> i <= floor(f) */ else { /* i <= f <=> i <= floor(f) */
lua_Integer fi; lua_Integer fi;
if (luaV_flttointeger(f, &fi, 1)) /* fi = floor(f) */ if (luaV_flttointeger(f, &fi, F2Ifloor)) /* fi = floor(f) */
return i <= fi; /* compare them as integers */ return i <= fi; /* compare them as integers */
else /* 'f' is either greater or less than all integers */ else /* 'f' is either greater or less than all integers */
return f > 0; /* greater? */ return f > 0; /* greater? */
@ -451,7 +448,7 @@ static int LTfloatint (lua_Number f, lua_Integer i) {
return luai_numlt(f, cast_num(i)); /* compare them as floats */ return luai_numlt(f, cast_num(i)); /* compare them as floats */
else { /* f < i <=> floor(f) < i */ else { /* f < i <=> floor(f) < i */
lua_Integer fi; lua_Integer fi;
if (luaV_flttointeger(f, &fi, 1)) /* fi = floor(f) */ if (luaV_flttointeger(f, &fi, F2Ifloor)) /* fi = floor(f) */
return fi < i; /* compare them as integers */ return fi < i; /* compare them as integers */
else /* 'f' is either greater or less than all integers */ else /* 'f' is either greater or less than all integers */
return f < 0; /* less? */ return f < 0; /* less? */
@ -468,7 +465,7 @@ static int LEfloatint (lua_Number f, lua_Integer i) {
return luai_numle(f, cast_num(i)); /* compare them as floats */ return luai_numle(f, cast_num(i)); /* compare them as floats */
else { /* f <= i <=> ceil(f) <= i */ else { /* f <= i <=> ceil(f) <= i */
lua_Integer fi; lua_Integer fi;
if (luaV_flttointeger(f, &fi, 2)) /* fi = ceil(f) */ if (luaV_flttointeger(f, &fi, F2Iceil)) /* fi = ceil(f) */
return fi <= i; /* compare them as integers */ return fi <= i; /* compare them as integers */
else /* 'f' is either greater or less than all integers */ else /* 'f' is either greater or less than all integers */
return f < 0; /* less? */ return f < 0; /* less? */

19
lvm.h
View File

@ -33,10 +33,20 @@
** integral values) ** integral values)
*/ */
#if !defined(LUA_FLOORN2I) #if !defined(LUA_FLOORN2I)
#define LUA_FLOORN2I 0 #define LUA_FLOORN2I F2Ieq
#endif #endif
/*
** Rounding modes for float->integer coercion
*/
typedef enum {
F2Ieq, /* no rounding; accepts only integral values */
F2Ifloor, /* takes the floor of the number */
F2Iceil, /* takes the ceil of the number */
} F2Imod;
/* convert an object to a float (including string coercion) */ /* convert an object to a float (including string coercion) */
#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))
@ -104,9 +114,10 @@ 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, int mode); LUAI_FUNC int luaV_tointeger (const TValue *obj, lua_Integer *p, F2Imod mode);
LUAI_FUNC int luaV_tointegerns (const TValue *obj, lua_Integer *p, int mode); LUAI_FUNC int luaV_tointegerns (const TValue *obj, lua_Integer *p,
LUAI_FUNC int luaV_flttointeger (lua_Number n, lua_Integer *p, int mode); F2Imod mode);
LUAI_FUNC int luaV_flttointeger (lua_Number n, lua_Integer *p, F2Imod 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,