Don't use tointegerns when luaV_tointegerns will do

Some places don't need the "fast path" macro tointegerns, either
because speed is not essential (lcode.c) or because the value is not
supposed to be an integer already (luaV_equalobj and luaG_tointerror).
Moreover, luaV_equalobj should always use F2Ieq, even if Lua is
compiled to "round to floor".
This commit is contained in:
Roberto Ierusalimschy 2021-02-24 12:24:42 -03:00
parent 31925e4cc2
commit 5205f073c5
3 changed files with 10 additions and 4 deletions

View File

@ -1298,7 +1298,8 @@ static int validop (int op, TValue *v1, TValue *v2) {
case LUA_OPBAND: case LUA_OPBOR: case LUA_OPBXOR: case LUA_OPBAND: case LUA_OPBOR: case LUA_OPBXOR:
case LUA_OPSHL: case LUA_OPSHR: case LUA_OPBNOT: { /* conversion errors */ case LUA_OPSHL: case LUA_OPSHR: case LUA_OPBNOT: { /* conversion errors */
lua_Integer i; lua_Integer i;
return (tointegerns(v1, &i) && tointegerns(v2, &i)); return (luaV_tointegerns(v1, &i, LUA_FLOORN2I) &&
luaV_tointegerns(v2, &i, LUA_FLOORN2I));
} }
case LUA_OPDIV: case LUA_OPIDIV: case LUA_OPMOD: /* division by 0 */ case LUA_OPDIV: case LUA_OPIDIV: case LUA_OPMOD: /* division by 0 */
return (nvalue(v2) != 0); return (nvalue(v2) != 0);

View File

@ -726,7 +726,7 @@ l_noret luaG_opinterror (lua_State *L, const TValue *p1,
*/ */
l_noret luaG_tointerror (lua_State *L, const TValue *p1, const TValue *p2) { l_noret luaG_tointerror (lua_State *L, const TValue *p1, const TValue *p2) {
lua_Integer temp; lua_Integer temp;
if (!tointegerns(p1, &temp)) if (!luaV_tointegerns(p1, &temp, LUA_FLOORN2I))
p2 = p1; p2 = p1;
luaG_runerror(L, "number%s has no integer representation", varinfo(L, p2)); luaG_runerror(L, "number%s has no integer representation", varinfo(L, p2));
} }

9
lvm.c
View File

@ -568,8 +568,13 @@ int luaV_equalobj (lua_State *L, const TValue *t1, const TValue *t2) {
if (ttype(t1) != ttype(t2) || ttype(t1) != LUA_TNUMBER) if (ttype(t1) != ttype(t2) || ttype(t1) != LUA_TNUMBER)
return 0; /* only numbers can be equal with different variants */ return 0; /* only numbers can be equal with different variants */
else { /* two numbers with different variants */ else { /* two numbers with different variants */
lua_Integer i1, i2; /* compare them as integers */ /* One of them is an integer. If the other does not have an
return (tointegerns(t1, &i1) && tointegerns(t2, &i2) && i1 == i2); integer value, they cannot be equal; otherwise, compare their
integer values. */
lua_Integer i1, i2;
return (luaV_tointegerns(t1, &i1, F2Ieq) &&
luaV_tointegerns(t2, &i2, F2Ieq) &&
i1 == i2);
} }
} }
/* values have same type and same variant */ /* values have same type and same variant */