removed coercion string->number in bitwise operations

(can be done with a Lua module)
This commit is contained in:
Roberto Ierusalimschy 2018-02-21 14:48:31 -03:00
parent c80c7a49fd
commit b03dddf9e4

View File

@ -1,5 +1,5 @@
/*
** $Id: lstrlib.c,v 1.260 2017/11/23 19:29:04 roberto Exp roberto $
** $Id: lstrlib.c,v 1.261 2018/02/21 13:48:44 roberto Exp roberto $
** Standard library for string operations and pattern-matching
** See Copyright Notice in lua.h
*/
@ -220,25 +220,6 @@ static int tonum (lua_State *L, int arg) {
}
static int toint (lua_State *L, int arg) {
if (!tonum(L, arg))
return 0; /* not coercible to a number */
else if (lua_isinteger(L, arg))
return 1; /* already an integer */
else { /* a float */
int ok;
lua_Integer n = lua_tointegerx(L, arg, &ok);
if (!ok)
return 0;
else {
lua_pop(L, 1); /* remove the float */
lua_pushinteger(L, n); /* push an integer */
return 1;
}
}
}
static void trymt (lua_State *L, const char *mtname) {
lua_settop(L, 2); /* back to the original arguments */
if (lua_type(L, 2) == LUA_TSTRING || !luaL_getmetafield(L, 2, mtname))
@ -258,15 +239,6 @@ static int arith (lua_State *L, int op, const char *mtname) {
}
static int bitwise (lua_State *L, int op, const char *mtname) {
if (toint(L, 1) && toint(L, 2))
lua_arith(L, op); /* result will be on the top */
else
trymt(L, mtname);
return 1;
}
static int arith_add (lua_State *L) {
return arith(L, LUA_OPADD, "__add");
}
@ -299,30 +271,6 @@ static int arith_unm (lua_State *L) {
return arith(L, LUA_OPUNM, "__unm");
}
static int bitwise_band (lua_State *L) {
return bitwise(L, LUA_OPBAND, "__band");
}
static int bitwise_bor (lua_State *L) {
return bitwise(L, LUA_OPBOR, "__bor");
}
static int bitwise_bxor (lua_State *L) {
return bitwise(L, LUA_OPBXOR, "__bxor");
}
static int bitwise_shl (lua_State *L) {
return bitwise(L, LUA_OPSHL, "__shl");
}
static int bitwise_shr (lua_State *L) {
return bitwise(L, LUA_OPSHR, "__shr");
}
static int bitwise_bnot (lua_State *L) {
return bitwise(L, LUA_OPBNOT, "__bnot");
}
static const luaL_Reg stringmetamethods[] = {
{"__add", arith_add},
@ -333,12 +281,6 @@ static const luaL_Reg stringmetamethods[] = {
{"__div", arith_div},
{"__idiv", arith_idiv},
{"__unm", arith_unm},
{"__band", bitwise_band},
{"__bor", bitwise_bor},
{"__bxor", bitwise_bxor},
{"__shl", bitwise_shl},
{"__shr", bitwise_shr},
{"__bnot", bitwise_bnot},
{"__index", NULL}, /* placeholder */
{NULL, NULL}
};