From 037a70dfea9de66e8d27c8d3ce2ed9f159a1b094 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Tue, 15 Apr 2014 13:32:49 -0300 Subject: [PATCH] cast_u2s/cast_s2u renamed l_castS2U/l_castU2S to be configurable from outside (mostly for testing) --- lapi.c | 6 +++--- llimits.h | 14 +++++++++----- lobject.c | 6 +++--- ltable.c | 4 ++-- lvm.c | 8 ++++---- lvm.h | 4 ++-- 6 files changed, 23 insertions(+), 19 deletions(-) diff --git a/lapi.c b/lapi.c index dc620081..7f85ebd5 100644 --- a/lapi.c +++ b/lapi.c @@ -1,5 +1,5 @@ /* -** $Id: lapi.c,v 2.203 2014/04/12 14:45:10 roberto Exp roberto $ +** $Id: lapi.c,v 2.204 2014/04/15 14:29:30 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_s2u(ivalue(o)); + res = l_castS2U(ivalue(o)); 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_u2s(u)); + setivalue(L->top, l_castU2S(u)); api_incr_top(L); lua_unlock(L); } diff --git a/llimits.h b/llimits.h index 0feba1d9..41f207e6 100644 --- a/llimits.h +++ b/llimits.h @@ -1,5 +1,5 @@ /* -** $Id: llimits.h,v 1.114 2014/04/12 14:45:10 roberto Exp roberto $ +** $Id: llimits.h,v 1.115 2014/04/15 14:28:20 roberto Exp roberto $ ** Limits, basic types, and some other `installation-dependent' definitions ** See Copyright Notice in lua.h */ @@ -107,15 +107,19 @@ typedef LUAI_UACINT l_uacInt; #define cast_uchar(i) cast(unsigned char, (i)) +/* cast a signed lua_Integer to lua_Unsigned */ +#if !defined(l_castS2U) +#define l_castS2U(i) ((lua_Unsigned)(i)) +#endif + /* ** 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)) +#if !defined(l_castU2S) +#define l_castU2S(i) ((lua_Integer)(i)) +#endif /* diff --git a/lobject.c b/lobject.c index c736c8a8..2c437527 100644 --- a/lobject.c +++ b/lobject.c @@ -1,5 +1,5 @@ /* -** $Id: lobject.c,v 2.78 2014/04/11 19:52:26 roberto Exp roberto $ +** $Id: lobject.c,v 2.79 2014/04/15 14:28:20 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_s2u(0), v1); + case LUA_OPBNOT: return intop(^, ~l_castS2U(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_u2s((neg) ? 0u - a : a); + *result = l_castU2S((neg) ? 0u - a : a); return 1; } } diff --git a/ltable.c b/ltable.c index 520ba98c..945123af 100644 --- a/ltable.c +++ b/ltable.c @@ -1,5 +1,5 @@ /* -** $Id: ltable.c,v 2.86 2014/04/13 21:11:19 roberto Exp roberto $ +** $Id: ltable.c,v 2.87 2014/04/15 14:28:20 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_s2u(key - 1) < cast(unsigned int, t->sizearray)) + if (l_castS2U(key - 1) < cast(unsigned int, t->sizearray)) return &t->array[key - 1]; else { Node *n = hashint(t, key); diff --git a/lvm.c b/lvm.c index 4efc2949..653e6a16 100644 --- a/lvm.c +++ b/lvm.c @@ -1,5 +1,5 @@ /* -** $Id: lvm.c,v 2.196 2014/04/11 19:02:16 roberto Exp roberto $ +** $Id: lvm.c,v 2.197 2014/04/15 14:28:20 roberto Exp roberto $ ** Lua virtual machine ** See Copyright Notice in lua.h */ @@ -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_s2u(y) + 1u <= 1u) { /* special cases: -1 or 0 */ + if (l_castS2U(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_s2u(y) + 1u <= 1u) { /* special cases: -1 or 0 */ + if (l_castS2U(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 */ @@ -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_s2u(0), ib)); + setivalue(ra, intop(^, ~l_castS2U(0), ib)); } else { Protect(luaT_trybinTM(L, rb, rb, ra, TM_BNOT)); diff --git a/lvm.h b/lvm.h index d85c3a4e..669cf391 100644 --- a/lvm.h +++ b/lvm.h @@ -1,5 +1,5 @@ /* -** $Id: lvm.h,v 2.26 2014/03/31 18:37:52 roberto Exp roberto $ +** $Id: lvm.h,v 2.27 2014/04/15 14:28:20 roberto Exp roberto $ ** Lua virtual machine ** See Copyright Notice in lua.h */ @@ -19,7 +19,7 @@ #define tointeger(o,i) \ (ttisinteger(o) ? (*(i) = ivalue(o), 1) : luaV_tointeger_(o,i)) -#define intop(op,v1,v2) cast_u2s(cast_s2u(v1) op cast_s2u(v2)) +#define intop(op,v1,v2) l_castU2S(l_castS2U(v1) op l_castS2U(v2)) #define luaV_rawequalobj(t1,t2) luaV_equalobj(NULL,t1,t2)