Skip to content

Commit d30569c

Browse files
committed
Using an enumeration for float->integer coercion modes
1 parent 2d92102 commit d30569c

4 files changed

Lines changed: 30 additions & 22 deletions

File tree

lcode.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -653,7 +653,7 @@ void luaK_int (FuncState *fs, int reg, lua_Integer i) {
653653

654654
static void luaK_float (FuncState *fs, int reg, lua_Number f) {
655655
lua_Integer fi;
656-
if (luaV_flttointeger(f, &fi, 0) && fitsBx(fi))
656+
if (luaV_flttointeger(f, &fi, F2Ieq) && fitsBx(fi))
657657
luaK_codeAsBx(fs, OP_LOADF, reg, cast_int(fi));
658658
else
659659
luaK_codek(fs, reg, luaK_numberK(fs, f));
@@ -1220,7 +1220,7 @@ static int isSCnumber (expdesc *e, int *pi, int *isfloat) {
12201220
lua_Integer i;
12211221
if (e->k == VKINT)
12221222
i = e->u.ival;
1223-
else if (e->k == VKFLT && luaV_flttointeger(e->u.nval, &i, 0))
1223+
else if (e->k == VKFLT && luaV_flttointeger(e->u.nval, &i, F2Ieq))
12241224
*isfloat = 1;
12251225
else
12261226
return 0; /* not a number */

ltable.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -626,7 +626,7 @@ TValue *luaH_newkey (lua_State *L, Table *t, const TValue *key) {
626626
else if (ttisfloat(key)) {
627627
lua_Number f = fltvalue(key);
628628
lua_Integer k;
629-
if (luaV_flttointeger(f, &k, 0)) { /* does key fit in an integer? */
629+
if (luaV_flttointeger(f, &k, F2Ieq)) { /* does key fit in an integer? */
630630
setivalue(&aux, k);
631631
key = &aux; /* insert it as an integer */
632632
}
@@ -745,7 +745,7 @@ const TValue *luaH_get (Table *t, const TValue *key) {
745745
case LUA_TNIL: return &absentkey;
746746
case LUA_TNUMFLT: {
747747
lua_Integer k;
748-
if (luaV_flttointeger(fltvalue(key), &k, 0)) /* index is an integral? */
748+
if (luaV_flttointeger(fltvalue(key), &k, F2Ieq)) /* integral index? */
749749
return luaH_getint(t, k); /* use specialized version */
750750
/* else... */
751751
} /* FALLTHROUGH */

lvm.c

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -116,16 +116,13 @@ int luaV_tonumber_ (const TValue *obj, lua_Number *n) {
116116

117117

118118
/*
119-
** try to convert a float to an integer, rounding according to 'mode':
120-
** mode == 0: accepts only integral values
121-
** mode == 1: takes the floor of the number
122-
** mode == 2: takes the ceil of the number
119+
** try to convert a float to an integer, rounding according to 'mode'.
123120
*/
124-
int luaV_flttointeger (lua_Number n, lua_Integer *p, int mode) {
121+
int luaV_flttointeger (lua_Number n, lua_Integer *p, F2Imod mode) {
125122
lua_Number f = l_floor(n);
126123
if (n != f) { /* not an integral value? */
127-
if (mode == 0) return 0; /* fails if mode demands integral value */
128-
else if (mode == 2) /* needs ceil? */
124+
if (mode == F2Ieq) return 0; /* fails if mode demands integral value */
125+
else if (mode == F2Iceil) /* needs ceil? */
129126
f += 1; /* convert floor to ceil (remember: n != f) */
130127
}
131128
return lua_numbertointeger(f, p);
@@ -137,7 +134,7 @@ int luaV_flttointeger (lua_Number n, lua_Integer *p, int mode) {
137134
** without string coercion.
138135
** ("Fast track" handled by macro 'tointegerns'.)
139136
*/
140-
int luaV_tointegerns (const TValue *obj, lua_Integer *p, int mode) {
137+
int luaV_tointegerns (const TValue *obj, lua_Integer *p, F2Imod mode) {
141138
if (ttisfloat(obj))
142139
return luaV_flttointeger(fltvalue(obj), p, mode);
143140
else if (ttisinteger(obj)) {
@@ -152,7 +149,7 @@ int luaV_tointegerns (const TValue *obj, lua_Integer *p, int mode) {
152149
/*
153150
** try to convert a value to an integer.
154151
*/
155-
int luaV_tointeger (const TValue *obj, lua_Integer *p, int mode) {
152+
int luaV_tointeger (const TValue *obj, lua_Integer *p, F2Imod mode) {
156153
TValue v;
157154
if (l_strton(obj, &v)) /* does 'obj' point to a numerical string? */
158155
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) {
178175
*/
179176
static int forlimit (lua_State *L, lua_Integer init, const TValue *lim,
180177
lua_Integer *p, lua_Integer step) {
181-
if (!luaV_tointeger(lim, p, (step < 0 ? 2 : 1))) {
178+
if (!luaV_tointeger(lim, p, (step < 0 ? F2Iceil : F2Ifloor))) {
182179
/* not coercible to in integer */
183180
lua_Number flim; /* try to convert to float */
184181
if (!tonumber(lim, &flim)) /* cannot convert to float? */
@@ -417,7 +414,7 @@ static int LTintfloat (lua_Integer i, lua_Number f) {
417414
return luai_numlt(cast_num(i), f); /* compare them as floats */
418415
else { /* i < f <=> i < ceil(f) */
419416
lua_Integer fi;
420-
if (luaV_flttointeger(f, &fi, 2)) /* fi = ceil(f) */
417+
if (luaV_flttointeger(f, &fi, F2Iceil)) /* fi = ceil(f) */
421418
return i < fi; /* compare them as integers */
422419
else /* 'f' is either greater or less than all integers */
423420
return f > 0; /* greater? */
@@ -434,7 +431,7 @@ static int LEintfloat (lua_Integer i, lua_Number f) {
434431
return luai_numle(cast_num(i), f); /* compare them as floats */
435432
else { /* i <= f <=> i <= floor(f) */
436433
lua_Integer fi;
437-
if (luaV_flttointeger(f, &fi, 1)) /* fi = floor(f) */
434+
if (luaV_flttointeger(f, &fi, F2Ifloor)) /* fi = floor(f) */
438435
return i <= fi; /* compare them as integers */
439436
else /* 'f' is either greater or less than all integers */
440437
return f > 0; /* greater? */
@@ -451,7 +448,7 @@ static int LTfloatint (lua_Number f, lua_Integer i) {
451448
return luai_numlt(f, cast_num(i)); /* compare them as floats */
452449
else { /* f < i <=> floor(f) < i */
453450
lua_Integer fi;
454-
if (luaV_flttointeger(f, &fi, 1)) /* fi = floor(f) */
451+
if (luaV_flttointeger(f, &fi, F2Ifloor)) /* fi = floor(f) */
455452
return fi < i; /* compare them as integers */
456453
else /* 'f' is either greater or less than all integers */
457454
return f < 0; /* less? */
@@ -468,7 +465,7 @@ static int LEfloatint (lua_Number f, lua_Integer i) {
468465
return luai_numle(f, cast_num(i)); /* compare them as floats */
469466
else { /* f <= i <=> ceil(f) <= i */
470467
lua_Integer fi;
471-
if (luaV_flttointeger(f, &fi, 2)) /* fi = ceil(f) */
468+
if (luaV_flttointeger(f, &fi, F2Iceil)) /* fi = ceil(f) */
472469
return fi <= i; /* compare them as integers */
473470
else /* 'f' is either greater or less than all integers */
474471
return f < 0; /* less? */

lvm.h

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,20 @@
3333
** integral values)
3434
*/
3535
#if !defined(LUA_FLOORN2I)
36-
#define LUA_FLOORN2I 0
36+
#define LUA_FLOORN2I F2Ieq
3737
#endif
3838

3939

40+
/*
41+
** Rounding modes for float->integer coercion
42+
*/
43+
typedef enum {
44+
F2Ieq, /* no rounding; accepts only integral values */
45+
F2Ifloor, /* takes the floor of the number */
46+
F2Iceil, /* takes the ceil of the number */
47+
} F2Imod;
48+
49+
4050
/* convert an object to a float (including string coercion) */
4151
#define tonumber(o,n) \
4252
(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);
104114
LUAI_FUNC int luaV_lessthan (lua_State *L, const TValue *l, const TValue *r);
105115
LUAI_FUNC int luaV_lessequal (lua_State *L, const TValue *l, const TValue *r);
106116
LUAI_FUNC int luaV_tonumber_ (const TValue *obj, lua_Number *n);
107-
LUAI_FUNC int luaV_tointeger (const TValue *obj, lua_Integer *p, int mode);
108-
LUAI_FUNC int luaV_tointegerns (const TValue *obj, lua_Integer *p, int mode);
109-
LUAI_FUNC int luaV_flttointeger (lua_Number n, lua_Integer *p, int mode);
117+
LUAI_FUNC int luaV_tointeger (const TValue *obj, lua_Integer *p, F2Imod mode);
118+
LUAI_FUNC int luaV_tointegerns (const TValue *obj, lua_Integer *p,
119+
F2Imod mode);
120+
LUAI_FUNC int luaV_flttointeger (lua_Number n, lua_Integer *p, F2Imod mode);
110121
LUAI_FUNC void luaV_finishget (lua_State *L, const TValue *t, TValue *key,
111122
StkId val, const TValue *slot);
112123
LUAI_FUNC void luaV_finishset (lua_State *L, const TValue *t, TValue *key,

0 commit comments

Comments
 (0)