summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRoberto Ierusalimschy <roberto@inf.puc-rio.br>2014-04-15 11:29:30 -0300
committerRoberto Ierusalimschy <roberto@inf.puc-rio.br>2014-04-15 11:29:30 -0300
commit8f961da3dbf8c45389d1431e6dff8e44e41f1a57 (patch)
treeee67fa31c29eab0569c35c62b17f43092f60d940
parent5c46b7b8cf3a71cce5572b1fca84542e24723efd (diff)
downloadlua-8f961da3dbf8c45389d1431e6dff8e44e41f1a57.tar.gz
lua-8f961da3dbf8c45389d1431e6dff8e44e41f1a57.tar.bz2
lua-8f961da3dbf8c45389d1431e6dff8e44e41f1a57.zip
macros cast_integer/cast_unsigned replaced by cast_u2s/cast_s2u, that
should be used only between lua_Integer and lua_Unsigned
-rw-r--r--lapi.c8
-rw-r--r--llimits.h15
-rw-r--r--lobject.c8
-rw-r--r--ltable.c4
-rw-r--r--lundump.h4
-rw-r--r--lvm.c14
-rw-r--r--lvm.h5
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 @@
1/* 1/*
2** $Id: lapi.c,v 2.202 2014/04/01 18:51:23 roberto Exp roberto $ 2** $Id: lapi.c,v 2.203 2014/04/12 14:45:10 roberto Exp roberto $
3** Lua API 3** Lua API
4** See Copyright Notice in lua.h 4** See Copyright Notice in lua.h
5*/ 5*/
@@ -376,7 +376,7 @@ LUA_API lua_Unsigned lua_tounsignedx (lua_State *L, int idx, int *pisnum) {
376 int isnum = 0; 376 int isnum = 0;
377 switch (ttype(o)) { 377 switch (ttype(o)) {
378 case LUA_TNUMINT: { 378 case LUA_TNUMINT: {
379 res = cast_unsigned(ivalue(o)); 379 res = cast_s2u(ivalue(o));
380 isnum = 1; 380 isnum = 1;
381 break; 381 break;
382 } 382 }
@@ -392,7 +392,7 @@ LUA_API lua_Unsigned lua_tounsignedx (lua_State *L, int idx, int *pisnum) {
392 n = l_mathop(fmod)(n, two2n); /* n = n % 2^(numbits in an integer) */ 392 n = l_mathop(fmod)(n, two2n); /* n = n % 2^(numbits in an integer) */
393 if (luai_numisnan(n)) /* not a number? */ 393 if (luai_numisnan(n)) /* not a number? */
394 break; /* not an integer, too */ 394 break; /* not an integer, too */
395 res = cast_unsigned(n); /* 'n' now must fit in an unsigned */ 395 res = cast(lua_Unsigned, n); /* 'n' now must fit in an unsigned */
396 if (neg) res = 0u - res; /* back to negative, if needed */ 396 if (neg) res = 0u - res; /* back to negative, if needed */
397 isnum = 1; 397 isnum = 1;
398 break; 398 break;
@@ -514,7 +514,7 @@ LUA_API void lua_pushinteger (lua_State *L, lua_Integer n) {
514 514
515LUA_API void lua_pushunsigned (lua_State *L, lua_Unsigned u) { 515LUA_API void lua_pushunsigned (lua_State *L, lua_Unsigned u) {
516 lua_lock(L); 516 lua_lock(L);
517 setivalue(L->top, cast_integer(u)); 517 setivalue(L->top, cast_u2s(u));
518 api_incr_top(L); 518 api_incr_top(L);
519 lua_unlock(L); 519 lua_unlock(L);
520} 520}
diff --git a/llimits.h b/llimits.h
index 1286ddba..0feba1d9 100644
--- a/llimits.h
+++ b/llimits.h
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: llimits.h,v 1.113 2014/04/11 19:56:04 roberto Exp roberto $ 2** $Id: llimits.h,v 1.114 2014/04/12 14:45:10 roberto Exp roberto $
3** Limits, basic types, and some other `installation-dependent' definitions 3** Limits, basic types, and some other `installation-dependent' definitions
4** See Copyright Notice in lua.h 4** See Copyright Notice in lua.h
5*/ 5*/
@@ -105,8 +105,17 @@ typedef LUAI_UACINT l_uacInt;
105#define cast_num(i) cast(lua_Number, (i)) 105#define cast_num(i) cast(lua_Number, (i))
106#define cast_int(i) cast(int, (i)) 106#define cast_int(i) cast(int, (i))
107#define cast_uchar(i) cast(unsigned char, (i)) 107#define cast_uchar(i) cast(unsigned char, (i))
108#define cast_integer(i) cast(lua_Integer, (i)) 108
109#define cast_unsigned(i) cast(lua_Unsigned, (i)) 109
110/*
111** cast a lua_Unsigned to a signed lua_Integer; this cast is
112** not strict ANSI C, but two-complement architectures should
113** work fine.
114*/
115#define cast_u2s(i) ((lua_Integer)(i))
116
117/* cast a signed lua_Integer to lua_Unsigned */
118#define cast_s2u(i) ((lua_Unsigned)(i))
110 119
111 120
112/* 121/*
diff --git a/lobject.c b/lobject.c
index 4e8b1d8e..c736c8a8 100644
--- a/lobject.c
+++ b/lobject.c
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: lobject.c,v 2.77 2014/04/09 17:05:11 roberto Exp roberto $ 2** $Id: lobject.c,v 2.78 2014/04/11 19:52:26 roberto Exp roberto $
3** Some generic functions over Lua objects 3** Some generic functions over Lua objects
4** See Copyright Notice in lua.h 4** See Copyright Notice in lua.h
5*/ 5*/
@@ -85,7 +85,7 @@ static lua_Integer intarith (lua_State *L, int op, lua_Integer v1,
85 case LUA_OPSHL: return luaV_shiftl(v1, v2); 85 case LUA_OPSHL: return luaV_shiftl(v1, v2);
86 case LUA_OPSHR: return luaV_shiftl(v1, -v2); 86 case LUA_OPSHR: return luaV_shiftl(v1, -v2);
87 case LUA_OPUNM: return intop(-, 0, v1); 87 case LUA_OPUNM: return intop(-, 0, v1);
88 case LUA_OPBNOT: return intop(^, cast_integer(-1), v1); 88 case LUA_OPBNOT: return intop(^, ~cast_s2u(0), v1);
89 default: lua_assert(0); return 0; 89 default: lua_assert(0); return 0;
90 } 90 }
91} 91}
@@ -291,7 +291,7 @@ int luaO_str2int (const char *s, size_t len, lua_Integer *result) {
291 while (lisspace(cast_uchar(*s))) s++; /* skip trailing spaces */ 291 while (lisspace(cast_uchar(*s))) s++; /* skip trailing spaces */
292 if (empty || s != ends) return 0; /* something wrong in the numeral */ 292 if (empty || s != ends) return 0; /* something wrong in the numeral */
293 else { 293 else {
294 *result = cast_integer((neg) ? 0u - a : a); 294 *result = cast_u2s((neg) ? 0u - a : a);
295 return 1; 295 return 1;
296 } 296 }
297} 297}
@@ -346,7 +346,7 @@ const char *luaO_pushvfstring (lua_State *L, const char *fmt, va_list argp) {
346 break; 346 break;
347 } 347 }
348 case 'I': { 348 case 'I': {
349 setivalue(L->top++, cast_integer(va_arg(argp, l_uacInt))); 349 setivalue(L->top++, cast(lua_Integer, va_arg(argp, l_uacInt)));
350 break; 350 break;
351 } 351 }
352 case 'f': { 352 case 'f': {
diff --git a/ltable.c b/ltable.c
index 3d4f3c38..520ba98c 100644
--- a/ltable.c
+++ b/ltable.c
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: ltable.c,v 2.85 2014/04/01 14:39:55 roberto Exp roberto $ 2** $Id: ltable.c,v 2.86 2014/04/13 21:11:19 roberto Exp roberto $
3** Lua tables (hash) 3** Lua tables (hash)
4** See Copyright Notice in lua.h 4** See Copyright Notice in lua.h
5*/ 5*/
@@ -472,7 +472,7 @@ TValue *luaH_newkey (lua_State *L, Table *t, const TValue *key) {
472*/ 472*/
473const TValue *luaH_getint (Table *t, lua_Integer key) { 473const TValue *luaH_getint (Table *t, lua_Integer key) {
474 /* (1 <= key && key <= t->sizearray) */ 474 /* (1 <= key && key <= t->sizearray) */
475 if (cast_unsigned(key - 1) < cast_unsigned(t->sizearray)) 475 if (cast_s2u(key - 1) < cast(unsigned int, t->sizearray))
476 return &t->array[key - 1]; 476 return &t->array[key - 1];
477 else { 477 else {
478 Node *n = hashint(t, key); 478 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 @@
1/* 1/*
2** $Id: lundump.h,v 1.41 2014/03/10 19:52:47 roberto Exp roberto $ 2** $Id: lundump.h,v 1.42 2014/03/11 14:22:54 roberto Exp roberto $
3** load precompiled Lua chunks 3** load precompiled Lua chunks
4** See Copyright Notice in lua.h 4** See Copyright Notice in lua.h
5*/ 5*/
@@ -15,7 +15,7 @@
15/* data to catch conversion errors */ 15/* data to catch conversion errors */
16#define LUAC_DATA "\x19\x93\r\n\x1a\n" 16#define LUAC_DATA "\x19\x93\r\n\x1a\n"
17 17
18#define LUAC_INT cast_integer(0xABCD) 18#define LUAC_INT 0x5678
19#define LUAC_NUM cast_num(370.5) 19#define LUAC_NUM cast_num(370.5)
20 20
21#define MYINT(s) (s[0]-'0') 21#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 @@
1/* 1/*
2** $Id: lvm.c,v 2.195 2014/04/09 17:05:11 roberto Exp roberto $ 2** $Id: lvm.c,v 2.196 2014/04/11 19:02:16 roberto Exp roberto $
3** Lua virtual machine 3** Lua virtual machine
4** See Copyright Notice in lua.h 4** See Copyright Notice in lua.h
5*/ 5*/
@@ -88,7 +88,7 @@ int luaV_tostring (lua_State *L, StkId obj) {
88*/ 88*/
89int luaV_numtointeger (lua_Number n, lua_Integer *p) { 89int luaV_numtointeger (lua_Number n, lua_Integer *p) {
90 if (cast_num(LUA_MININTEGER) <= n && n < (LUA_MAXINTEGER + cast_num(1))) { 90 if (cast_num(LUA_MININTEGER) <= n && n < (LUA_MAXINTEGER + cast_num(1))) {
91 *p = cast_integer(n); 91 *p = cast(lua_Integer, n);
92 lua_assert(cast_num(*p) == n); 92 lua_assert(cast_num(*p) == n);
93 return 1; 93 return 1;
94 } 94 }
@@ -343,7 +343,7 @@ void luaV_objlen (lua_State *L, StkId ra, const TValue *rb) {
343 343
344 344
345lua_Integer luaV_div (lua_State *L, lua_Integer x, lua_Integer y) { 345lua_Integer luaV_div (lua_State *L, lua_Integer x, lua_Integer y) {
346 if (cast_unsigned(y) + 1u <= 1u) { /* special cases: -1 or 0 */ 346 if (cast_s2u(y) + 1u <= 1u) { /* special cases: -1 or 0 */
347 if (y == 0) 347 if (y == 0)
348 luaG_runerror(L, "attempt to divide by zero"); 348 luaG_runerror(L, "attempt to divide by zero");
349 return intop(-, 0, x); /* y==-1; avoid overflow with 0x80000...//-1 */ 349 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) {
359 359
360 360
361lua_Integer luaV_mod (lua_State *L, lua_Integer x, lua_Integer y) { 361lua_Integer luaV_mod (lua_State *L, lua_Integer x, lua_Integer y) {
362 if (cast_unsigned(y) + 1u <= 1u) { /* special cases: -1 or 0 */ 362 if (cast_s2u(y) + 1u <= 1u) { /* special cases: -1 or 0 */
363 if (y == 0) 363 if (y == 0)
364 luaG_runerror(L, "attempt to perform 'n%%0'"); 364 luaG_runerror(L, "attempt to perform 'n%%0'");
365 return 0; /* y==-1; avoid overflow with 0x80000...%-1 */ 365 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) {
398lua_Integer luaV_shiftl (lua_Integer x, lua_Integer y) { 398lua_Integer luaV_shiftl (lua_Integer x, lua_Integer y) {
399 if (y < 0) { /* shift right? */ 399 if (y < 0) { /* shift right? */
400 if (y <= -NBITS) return 0; 400 if (y <= -NBITS) return 0;
401 else return cast_integer(cast_unsigned(x) >> (-y)); 401 else return intop(>>, x, -y);
402 } 402 }
403 else { /* shift left */ 403 else { /* shift left */
404 if (y >= NBITS) return 0; 404 if (y >= NBITS) return 0;
405 else return x << y; 405 else return intop(<<, x, y);
406 } 406 }
407} 407}
408 408
@@ -792,7 +792,7 @@ void luaV_execute (lua_State *L) {
792 TValue *rb = RB(i); 792 TValue *rb = RB(i);
793 lua_Integer ib; 793 lua_Integer ib;
794 if (tointeger(rb, &ib)) { 794 if (tointeger(rb, &ib)) {
795 setivalue(ra, intop(^, cast_integer(-1), ib)); 795 setivalue(ra, intop(^, ~cast_s2u(0), ib));
796 } 796 }
797 else { 797 else {
798 Protect(luaT_trybinTM(L, rb, rb, ra, TM_BNOT)); 798 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 @@
1/* 1/*
2** $Id: lvm.h,v 2.25 2013/12/30 20:47:58 roberto Exp roberto $ 2** $Id: lvm.h,v 2.26 2014/03/31 18:37:52 roberto Exp roberto $
3** Lua virtual machine 3** Lua virtual machine
4** See Copyright Notice in lua.h 4** See Copyright Notice in lua.h
5*/ 5*/
@@ -19,8 +19,7 @@
19#define tointeger(o,i) \ 19#define tointeger(o,i) \
20 (ttisinteger(o) ? (*(i) = ivalue(o), 1) : luaV_tointeger_(o,i)) 20 (ttisinteger(o) ? (*(i) = ivalue(o), 1) : luaV_tointeger_(o,i))
21 21
22#define intop(op,v1,v2) \ 22#define intop(op,v1,v2) cast_u2s(cast_s2u(v1) op cast_s2u(v2))
23 cast_integer(cast_unsigned(v1) op cast_unsigned(v2))
24 23
25#define luaV_rawequalobj(t1,t2) luaV_equalobj(NULL,t1,t2) 24#define luaV_rawequalobj(t1,t2) luaV_equalobj(NULL,t1,t2)
26 25