diff options
author | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2018-02-21 14:48:31 -0300 |
---|---|---|
committer | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2018-02-21 14:48:31 -0300 |
commit | b03dddf9e406f29e5d8bfa06ccb6db1dfad6e405 (patch) | |
tree | f21cffd2139e8d1ec53a6b84045832ecf4652f85 /lstrlib.c | |
parent | c80c7a49fdbd5c6540bd49ef47925edd9eed8c99 (diff) | |
download | lua-b03dddf9e406f29e5d8bfa06ccb6db1dfad6e405.tar.gz lua-b03dddf9e406f29e5d8bfa06ccb6db1dfad6e405.tar.bz2 lua-b03dddf9e406f29e5d8bfa06ccb6db1dfad6e405.zip |
removed coercion string->number in bitwise operations
(can be done with a Lua module)
Diffstat (limited to 'lstrlib.c')
-rw-r--r-- | lstrlib.c | 60 |
1 files changed, 1 insertions, 59 deletions
@@ -1,5 +1,5 @@ | |||
1 | /* | 1 | /* |
2 | ** $Id: lstrlib.c,v 1.260 2017/11/23 19:29:04 roberto Exp roberto $ | 2 | ** $Id: lstrlib.c,v 1.261 2018/02/21 13:48:44 roberto Exp roberto $ |
3 | ** Standard library for string operations and pattern-matching | 3 | ** Standard library for string operations and pattern-matching |
4 | ** See Copyright Notice in lua.h | 4 | ** See Copyright Notice in lua.h |
5 | */ | 5 | */ |
@@ -220,25 +220,6 @@ static int tonum (lua_State *L, int arg) { | |||
220 | } | 220 | } |
221 | 221 | ||
222 | 222 | ||
223 | static int toint (lua_State *L, int arg) { | ||
224 | if (!tonum(L, arg)) | ||
225 | return 0; /* not coercible to a number */ | ||
226 | else if (lua_isinteger(L, arg)) | ||
227 | return 1; /* already an integer */ | ||
228 | else { /* a float */ | ||
229 | int ok; | ||
230 | lua_Integer n = lua_tointegerx(L, arg, &ok); | ||
231 | if (!ok) | ||
232 | return 0; | ||
233 | else { | ||
234 | lua_pop(L, 1); /* remove the float */ | ||
235 | lua_pushinteger(L, n); /* push an integer */ | ||
236 | return 1; | ||
237 | } | ||
238 | } | ||
239 | } | ||
240 | |||
241 | |||
242 | static void trymt (lua_State *L, const char *mtname) { | 223 | static void trymt (lua_State *L, const char *mtname) { |
243 | lua_settop(L, 2); /* back to the original arguments */ | 224 | lua_settop(L, 2); /* back to the original arguments */ |
244 | if (lua_type(L, 2) == LUA_TSTRING || !luaL_getmetafield(L, 2, mtname)) | 225 | 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) { | |||
258 | } | 239 | } |
259 | 240 | ||
260 | 241 | ||
261 | static int bitwise (lua_State *L, int op, const char *mtname) { | ||
262 | if (toint(L, 1) && toint(L, 2)) | ||
263 | lua_arith(L, op); /* result will be on the top */ | ||
264 | else | ||
265 | trymt(L, mtname); | ||
266 | return 1; | ||
267 | } | ||
268 | |||
269 | |||
270 | static int arith_add (lua_State *L) { | 242 | static int arith_add (lua_State *L) { |
271 | return arith(L, LUA_OPADD, "__add"); | 243 | return arith(L, LUA_OPADD, "__add"); |
272 | } | 244 | } |
@@ -299,30 +271,6 @@ static int arith_unm (lua_State *L) { | |||
299 | return arith(L, LUA_OPUNM, "__unm"); | 271 | return arith(L, LUA_OPUNM, "__unm"); |
300 | } | 272 | } |
301 | 273 | ||
302 | static int bitwise_band (lua_State *L) { | ||
303 | return bitwise(L, LUA_OPBAND, "__band"); | ||
304 | } | ||
305 | |||
306 | static int bitwise_bor (lua_State *L) { | ||
307 | return bitwise(L, LUA_OPBOR, "__bor"); | ||
308 | } | ||
309 | |||
310 | static int bitwise_bxor (lua_State *L) { | ||
311 | return bitwise(L, LUA_OPBXOR, "__bxor"); | ||
312 | } | ||
313 | |||
314 | static int bitwise_shl (lua_State *L) { | ||
315 | return bitwise(L, LUA_OPSHL, "__shl"); | ||
316 | } | ||
317 | |||
318 | static int bitwise_shr (lua_State *L) { | ||
319 | return bitwise(L, LUA_OPSHR, "__shr"); | ||
320 | } | ||
321 | |||
322 | static int bitwise_bnot (lua_State *L) { | ||
323 | return bitwise(L, LUA_OPBNOT, "__bnot"); | ||
324 | } | ||
325 | |||
326 | 274 | ||
327 | static const luaL_Reg stringmetamethods[] = { | 275 | static const luaL_Reg stringmetamethods[] = { |
328 | {"__add", arith_add}, | 276 | {"__add", arith_add}, |
@@ -333,12 +281,6 @@ static const luaL_Reg stringmetamethods[] = { | |||
333 | {"__div", arith_div}, | 281 | {"__div", arith_div}, |
334 | {"__idiv", arith_idiv}, | 282 | {"__idiv", arith_idiv}, |
335 | {"__unm", arith_unm}, | 283 | {"__unm", arith_unm}, |
336 | {"__band", bitwise_band}, | ||
337 | {"__bor", bitwise_bor}, | ||
338 | {"__bxor", bitwise_bxor}, | ||
339 | {"__shl", bitwise_shl}, | ||
340 | {"__shr", bitwise_shr}, | ||
341 | {"__bnot", bitwise_bnot}, | ||
342 | {"__index", NULL}, /* placeholder */ | 284 | {"__index", NULL}, /* placeholder */ |
343 | {NULL, NULL} | 285 | {NULL, NULL} |
344 | }; | 286 | }; |