From 8f961da3dbf8c45389d1431e6dff8e44e41f1a57 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Tue, 15 Apr 2014 11:29:30 -0300 Subject: macros cast_integer/cast_unsigned replaced by cast_u2s/cast_s2u, that should be used only between lua_Integer and lua_Unsigned --- lapi.c | 8 ++++---- llimits.h | 15 ++++++++++++--- lobject.c | 8 ++++---- ltable.c | 4 ++-- lundump.h | 4 ++-- lvm.c | 14 +++++++------- lvm.h | 5 ++--- 7 files changed, 33 insertions(+), 25 deletions(-) diff --git a/lapi.c b/lapi.c index c58ff807..dc620081 100644 --- a/lapi.c +++ b/lapi.c @@ -1,5 +1,5 @@ /* -** $Id: lapi.c,v 2.202 2014/04/01 18:51:23 roberto Exp roberto $ +** $Id: lapi.c,v 2.203 2014/04/12 14:45:10 roberto Exp roberto $ ** Lua API ** See Copyright Notice in lua.h */ @@ -376,7 +376,7 @@ LUA_API lua_Unsigned lua_tounsignedx (lua_State *L, int idx, int *pisnum) { int isnum = 0; switch (ttype(o)) { case LUA_TNUMINT: { - res = cast_unsigned(ivalue(o)); + res = cast_s2u(ivalue(o)); isnum = 1; break; } @@ -392,7 +392,7 @@ LUA_API lua_Unsigned lua_tounsignedx (lua_State *L, int idx, int *pisnum) { n = l_mathop(fmod)(n, two2n); /* n = n % 2^(numbits in an integer) */ if (luai_numisnan(n)) /* not a number? */ break; /* not an integer, too */ - res = cast_unsigned(n); /* 'n' now must fit in an unsigned */ + res = cast(lua_Unsigned, n); /* 'n' now must fit in an unsigned */ if (neg) res = 0u - res; /* back to negative, if needed */ isnum = 1; break; @@ -514,7 +514,7 @@ LUA_API void lua_pushinteger (lua_State *L, lua_Integer n) { LUA_API void lua_pushunsigned (lua_State *L, lua_Unsigned u) { lua_lock(L); - setivalue(L->top, cast_integer(u)); + setivalue(L->top, cast_u2s(u)); api_incr_top(L); lua_unlock(L); } diff --git a/llimits.h b/llimits.h index 1286ddba..0feba1d9 100644 --- a/llimits.h +++ b/llimits.h @@ -1,5 +1,5 @@ /* -** $Id: llimits.h,v 1.113 2014/04/11 19:56:04 roberto Exp roberto $ +** $Id: llimits.h,v 1.114 2014/04/12 14:45:10 roberto Exp roberto $ ** Limits, basic types, and some other `installation-dependent' definitions ** See Copyright Notice in lua.h */ @@ -105,8 +105,17 @@ typedef LUAI_UACINT l_uacInt; #define cast_num(i) cast(lua_Number, (i)) #define cast_int(i) cast(int, (i)) #define cast_uchar(i) cast(unsigned char, (i)) -#define cast_integer(i) cast(lua_Integer, (i)) -#define cast_unsigned(i) cast(lua_Unsigned, (i)) + + +/* +** cast a lua_Unsigned to a signed lua_Integer; this cast is +** not strict ANSI C, but two-complement architectures should +** work fine. +*/ +#define cast_u2s(i) ((lua_Integer)(i)) + +/* cast a signed lua_Integer to lua_Unsigned */ +#define cast_s2u(i) ((lua_Unsigned)(i)) /* diff --git a/lobject.c b/lobject.c index 4e8b1d8e..c736c8a8 100644 --- a/lobject.c +++ b/lobject.c @@ -1,5 +1,5 @@ /* -** $Id: lobject.c,v 2.77 2014/04/09 17:05:11 roberto Exp roberto $ +** $Id: lobject.c,v 2.78 2014/04/11 19:52:26 roberto Exp roberto $ ** Some generic functions over Lua objects ** See Copyright Notice in lua.h */ @@ -85,7 +85,7 @@ static lua_Integer intarith (lua_State *L, int op, lua_Integer v1, case LUA_OPSHL: return luaV_shiftl(v1, v2); case LUA_OPSHR: return luaV_shiftl(v1, -v2); case LUA_OPUNM: return intop(-, 0, v1); - case LUA_OPBNOT: return intop(^, cast_integer(-1), v1); + case LUA_OPBNOT: return intop(^, ~cast_s2u(0), v1); default: lua_assert(0); return 0; } } @@ -291,7 +291,7 @@ int luaO_str2int (const char *s, size_t len, lua_Integer *result) { while (lisspace(cast_uchar(*s))) s++; /* skip trailing spaces */ if (empty || s != ends) return 0; /* something wrong in the numeral */ else { - *result = cast_integer((neg) ? 0u - a : a); + *result = cast_u2s((neg) ? 0u - a : a); return 1; } } @@ -346,7 +346,7 @@ const char *luaO_pushvfstring (lua_State *L, const char *fmt, va_list argp) { break; } case 'I': { - setivalue(L->top++, cast_integer(va_arg(argp, l_uacInt))); + setivalue(L->top++, cast(lua_Integer, va_arg(argp, l_uacInt))); break; } case 'f': { diff --git a/ltable.c b/ltable.c index 3d4f3c38..520ba98c 100644 --- a/ltable.c +++ b/ltable.c @@ -1,5 +1,5 @@ /* -** $Id: ltable.c,v 2.85 2014/04/01 14:39:55 roberto Exp roberto $ +** $Id: ltable.c,v 2.86 2014/04/13 21:11:19 roberto Exp roberto $ ** Lua tables (hash) ** See Copyright Notice in lua.h */ @@ -472,7 +472,7 @@ TValue *luaH_newkey (lua_State *L, Table *t, const TValue *key) { */ const TValue *luaH_getint (Table *t, lua_Integer key) { /* (1 <= key && key <= t->sizearray) */ - if (cast_unsigned(key - 1) < cast_unsigned(t->sizearray)) + if (cast_s2u(key - 1) < cast(unsigned int, t->sizearray)) return &t->array[key - 1]; else { Node *n = hashint(t, key); diff --git a/lundump.h b/lundump.h index 6b851da0..67f6b57c 100644 --- a/lundump.h +++ b/lundump.h @@ -1,5 +1,5 @@ /* -** $Id: lundump.h,v 1.41 2014/03/10 19:52:47 roberto Exp roberto $ +** $Id: lundump.h,v 1.42 2014/03/11 14:22:54 roberto Exp roberto $ ** load precompiled Lua chunks ** See Copyright Notice in lua.h */ @@ -15,7 +15,7 @@ /* data to catch conversion errors */ #define LUAC_DATA "\x19\x93\r\n\x1a\n" -#define LUAC_INT cast_integer(0xABCD) +#define LUAC_INT 0x5678 #define LUAC_NUM cast_num(370.5) #define MYINT(s) (s[0]-'0') diff --git a/lvm.c b/lvm.c index f30806bd..4efc2949 100644 --- a/lvm.c +++ b/lvm.c @@ -1,5 +1,5 @@ /* -** $Id: lvm.c,v 2.195 2014/04/09 17:05:11 roberto Exp roberto $ +** $Id: lvm.c,v 2.196 2014/04/11 19:02:16 roberto Exp roberto $ ** Lua virtual machine ** See Copyright Notice in lua.h */ @@ -88,7 +88,7 @@ int luaV_tostring (lua_State *L, StkId obj) { */ int luaV_numtointeger (lua_Number n, lua_Integer *p) { if (cast_num(LUA_MININTEGER) <= n && n < (LUA_MAXINTEGER + cast_num(1))) { - *p = cast_integer(n); + *p = cast(lua_Integer, n); lua_assert(cast_num(*p) == n); return 1; } @@ -343,7 +343,7 @@ void luaV_objlen (lua_State *L, StkId ra, const TValue *rb) { lua_Integer luaV_div (lua_State *L, lua_Integer x, lua_Integer y) { - if (cast_unsigned(y) + 1u <= 1u) { /* special cases: -1 or 0 */ + if (cast_s2u(y) + 1u <= 1u) { /* special cases: -1 or 0 */ if (y == 0) luaG_runerror(L, "attempt to divide by zero"); return intop(-, 0, x); /* y==-1; avoid overflow with 0x80000...//-1 */ @@ -359,7 +359,7 @@ lua_Integer luaV_div (lua_State *L, lua_Integer x, lua_Integer y) { lua_Integer luaV_mod (lua_State *L, lua_Integer x, lua_Integer y) { - if (cast_unsigned(y) + 1u <= 1u) { /* special cases: -1 or 0 */ + if (cast_s2u(y) + 1u <= 1u) { /* special cases: -1 or 0 */ if (y == 0) luaG_runerror(L, "attempt to perform 'n%%0'"); return 0; /* y==-1; avoid overflow with 0x80000...%-1 */ @@ -398,11 +398,11 @@ lua_Integer luaV_pow (lua_State *L, lua_Integer x, lua_Integer y) { lua_Integer luaV_shiftl (lua_Integer x, lua_Integer y) { if (y < 0) { /* shift right? */ if (y <= -NBITS) return 0; - else return cast_integer(cast_unsigned(x) >> (-y)); + else return intop(>>, x, -y); } else { /* shift left */ if (y >= NBITS) return 0; - else return x << y; + else return intop(<<, x, y); } } @@ -792,7 +792,7 @@ void luaV_execute (lua_State *L) { TValue *rb = RB(i); lua_Integer ib; if (tointeger(rb, &ib)) { - setivalue(ra, intop(^, cast_integer(-1), ib)); + setivalue(ra, intop(^, ~cast_s2u(0), ib)); } else { Protect(luaT_trybinTM(L, rb, rb, ra, TM_BNOT)); diff --git a/lvm.h b/lvm.h index ffda5fb2..d85c3a4e 100644 --- a/lvm.h +++ b/lvm.h @@ -1,5 +1,5 @@ /* -** $Id: lvm.h,v 2.25 2013/12/30 20:47:58 roberto Exp roberto $ +** $Id: lvm.h,v 2.26 2014/03/31 18:37:52 roberto Exp roberto $ ** Lua virtual machine ** See Copyright Notice in lua.h */ @@ -19,8 +19,7 @@ #define tointeger(o,i) \ (ttisinteger(o) ? (*(i) = ivalue(o), 1) : luaV_tointeger_(o,i)) -#define intop(op,v1,v2) \ - cast_integer(cast_unsigned(v1) op cast_unsigned(v2)) +#define intop(op,v1,v2) cast_u2s(cast_s2u(v1) op cast_s2u(v2)) #define luaV_rawequalobj(t1,t2) luaV_equalobj(NULL,t1,t2) -- cgit v1.2.3-55-g6feb