commit c6b64ffe65549b179bfa565e8329430857e335ee
parent 4590a89b32b62c75fca7ced96c282c7793b8885c
Author: Roberto Ierusalimschy <roberto@inf.puc-rio.br>
Date: Mon, 25 Oct 2010 18:30:47 -0200
new type lua_Unsigned and corresponding projection/injection functions
Diffstat:
6 files changed, 66 insertions(+), 22 deletions(-)
diff --git a/lapi.c b/lapi.c
@@ -1,5 +1,5 @@
/*
-** $Id: lapi.c,v 2.137 2010/09/07 19:35:04 roberto Exp roberto $
+** $Id: lapi.c,v 2.138 2010/10/25 19:01:37 roberto Exp roberto $
** Lua API
** See Copyright Notice in lua.h
*/
@@ -347,6 +347,23 @@ LUA_API lua_Integer lua_tointegerx (lua_State *L, int idx, int *isnum) {
}
+LUA_API lua_Unsigned lua_tounsignedx (lua_State *L, int idx, int *isnum) {
+ TValue n;
+ const TValue *o = index2addr(L, idx);
+ if (tonumber(o, &n)) {
+ lua_Unsigned res;
+ lua_Number num = nvalue(o);
+ lua_number2uint(res, num);
+ if (isnum) *isnum = 1;
+ return res;
+ }
+ else {
+ if (isnum) *isnum = 0;
+ return 0;
+ }
+}
+
+
LUA_API int lua_toboolean (lua_State *L, int idx) {
const TValue *o = index2addr(L, idx);
return !l_isfalse(o);
@@ -452,6 +469,16 @@ LUA_API void lua_pushinteger (lua_State *L, lua_Integer n) {
}
+LUA_API void lua_pushunsigned (lua_State *L, lua_Unsigned u) {
+ lua_Number n;
+ lua_lock(L);
+ n = lua_uint2number(u);
+ setnvalue(L->top, n);
+ api_incr_top(L);
+ lua_unlock(L);
+}
+
+
LUA_API const char *lua_pushlstring (lua_State *L, const char *s, size_t len) {
TString *ts;
lua_lock(L);
diff --git a/lauxlib.c b/lauxlib.c
@@ -1,5 +1,5 @@
/*
-** $Id: lauxlib.c,v 1.221 2010/10/01 18:53:00 roberto Exp roberto $
+** $Id: lauxlib.c,v 1.222 2010/10/25 19:01:37 roberto Exp roberto $
** Auxiliary functions for building Lua libraries
** See Copyright Notice in lua.h
*/
@@ -330,11 +330,26 @@ LUALIB_API lua_Integer luaL_checkinteger (lua_State *L, int narg) {
}
+LUALIB_API lua_Unsigned luaL_checkunsigned (lua_State *L, int narg) {
+ int isnum;
+ lua_Unsigned d = lua_tounsignedx(L, narg, &isnum);
+ if (!isnum)
+ tag_error(L, narg, LUA_TNUMBER);
+ return d;
+}
+
+
LUALIB_API lua_Integer luaL_optinteger (lua_State *L, int narg,
lua_Integer def) {
return luaL_opt(L, luaL_checkinteger, narg, def);
}
+
+LUALIB_API lua_Unsigned luaL_optunsigned (lua_State *L, int narg,
+ lua_Unsigned def) {
+ return luaL_opt(L, luaL_checkunsigned, narg, def);
+}
+
/* }====================================================== */
diff --git a/lauxlib.h b/lauxlib.h
@@ -1,5 +1,5 @@
/*
-** $Id: lauxlib.h,v 1.107 2010/06/30 17:40:27 roberto Exp roberto $
+** $Id: lauxlib.h,v 1.108 2010/07/02 11:38:13 roberto Exp roberto $
** Auxiliary functions for building Lua libraries
** See Copyright Notice in lua.h
*/
@@ -44,6 +44,9 @@ LUALIB_API lua_Number (luaL_optnumber) (lua_State *L, int nArg, lua_Number def);
LUALIB_API lua_Integer (luaL_checkinteger) (lua_State *L, int numArg);
LUALIB_API lua_Integer (luaL_optinteger) (lua_State *L, int nArg,
lua_Integer def);
+LUALIB_API lua_Unsigned (luaL_checkunsigned) (lua_State *L, int numArg);
+LUALIB_API lua_Unsigned (luaL_optunsigned) (lua_State *L, int numArg,
+ lua_Unsigned def);
LUALIB_API void (luaL_checkstack) (lua_State *L, int sz, const char *msg);
LUALIB_API void (luaL_checktype) (lua_State *L, int narg, int t);
diff --git a/lbitlib.c b/lbitlib.c
@@ -1,5 +1,5 @@
/*
-** $Id: lbitlib.c,v 1.6 2010/07/02 12:01:53 roberto Exp roberto $
+** $Id: lbitlib.c,v 1.7 2010/10/25 14:32:36 roberto Exp roberto $
** Standard library for bitwise operations
** See Copyright Notice in lua.h
*/
@@ -21,14 +21,7 @@ typedef LUA_INT32 b_int;
typedef unsigned LUA_INT32 b_uint;
-static b_uint getuintarg (lua_State *L, int arg) {
- b_uint r;
- int isnum;
- lua_Number x = lua_tonumberx(L, arg, &isnum);
- if (!isnum) luaL_typeerror(L, arg, "number");
- lua_number2uint(r, x);
- return r;
-}
+#define getuintarg(L,arg) luaL_checkunsigned(L,arg)
static b_uint andaux (lua_State *L) {
@@ -42,7 +35,7 @@ static b_uint andaux (lua_State *L) {
static int b_and (lua_State *L) {
b_uint r = andaux(L);
- lua_pushnumber(L, lua_uint2number(r));
+ lua_pushunsigned(L, r);
return 1;
}
@@ -59,7 +52,7 @@ static int b_or (lua_State *L) {
b_uint r = 0;
for (i = 1; i <= n; i++)
r |= getuintarg(L, i);
- lua_pushnumber(L, lua_uint2number(r));
+ lua_pushunsigned(L, r);
return 1;
}
@@ -69,14 +62,14 @@ static int b_xor (lua_State *L) {
b_uint r = 0;
for (i = 1; i <= n; i++)
r ^= getuintarg(L, i);
- lua_pushnumber(L, lua_uint2number(r));
+ lua_pushunsigned(L, r);
return 1;
}
static int b_not (lua_State *L) {
b_uint r = ~getuintarg(L, 1);
- lua_pushnumber(L, lua_uint2number(r));
+ lua_pushunsigned(L, r);
return 1;
}
@@ -91,7 +84,7 @@ static int b_shift (lua_State *L, b_uint r, int i) {
if (i >= NBITS) r = 0;
else r <<= i;
}
- lua_pushnumber(L, lua_uint2number(r));
+ lua_pushunsigned(L, r);
return 1;
}
@@ -115,7 +108,7 @@ static int b_arshift (lua_State *L) {
if (i >= NBITS) r = 0xffffffff;
else
r = (r >> i) | ~(~(b_uint)0 >> i); /* add signal bit */
- lua_pushnumber(L, lua_uint2number(r));
+ lua_pushunsigned(L, r);
return 1;
}
}
@@ -125,7 +118,7 @@ static int b_rot (lua_State *L, int i) {
b_uint r = getuintarg(L, 1);
i &= (NBITS - 1); /* i = i % NBITS */
r = (r << i) | (r >> (NBITS - i));
- lua_pushnumber(L, lua_uint2number(r));
+ lua_pushunsigned(L, r);
return 1;
}
diff --git a/lmathlib.c b/lmathlib.c
@@ -1,5 +1,5 @@
/*
-** $Id: lmathlib.c,v 1.74 2009/11/24 12:05:44 roberto Exp roberto $
+** $Id: lmathlib.c,v 1.75 2010/07/02 11:38:13 roberto Exp roberto $
** Standard mathematical library
** See Copyright Notice in lua.h
*/
@@ -220,7 +220,7 @@ static int math_random (lua_State *L) {
static int math_randomseed (lua_State *L) {
- srand(luaL_checkint(L, 1));
+ srand(luaL_checkunsigned(L, 1));
(void)rand(); /* discard first value to avoid undesirable correlations */
return 0;
}
diff --git a/lua.h b/lua.h
@@ -1,5 +1,5 @@
/*
-** $Id: lua.h,v 1.273 2010/07/25 15:18:19 roberto Exp roberto $
+** $Id: lua.h,v 1.274 2010/09/03 14:14:01 roberto Exp roberto $
** Lua - A Scripting Language
** Lua.org, PUC-Rio, Brazil (http://www.lua.org)
** See Copyright Notice at the end of this file
@@ -106,6 +106,9 @@ typedef LUA_NUMBER lua_Number;
/* type for integer functions */
typedef LUA_INTEGER lua_Integer;
+/* unsigned integer type */
+typedef unsigned LUA_INT32 lua_Unsigned;
+
/*
@@ -159,6 +162,7 @@ LUA_API const char *(lua_typename) (lua_State *L, int tp);
LUA_API lua_Number (lua_tonumberx) (lua_State *L, int idx, int *isnum);
LUA_API lua_Integer (lua_tointegerx) (lua_State *L, int idx, int *isnum);
+LUA_API lua_Unsigned (lua_tounsignedx) (lua_State *L, int idx, int *isnum);
LUA_API int (lua_toboolean) (lua_State *L, int idx);
LUA_API const char *(lua_tolstring) (lua_State *L, int idx, size_t *len);
LUA_API size_t (lua_rawlen) (lua_State *L, int idx);
@@ -196,6 +200,7 @@ LUA_API int (lua_compare) (lua_State *L, int idx1, int idx2, int op);
LUA_API void (lua_pushnil) (lua_State *L);
LUA_API void (lua_pushnumber) (lua_State *L, lua_Number n);
LUA_API void (lua_pushinteger) (lua_State *L, lua_Integer n);
+LUA_API void (lua_pushunsigned) (lua_State *L, lua_Unsigned n);
LUA_API const char *(lua_pushlstring) (lua_State *L, const char *s, size_t l);
LUA_API const char *(lua_pushstring) (lua_State *L, const char *s);
LUA_API const char *(lua_pushvfstring) (lua_State *L, const char *fmt,
@@ -303,6 +308,7 @@ LUA_API void (lua_setallocf) (lua_State *L, lua_Alloc f, void *ud);
#define lua_tonumber(L,i) lua_tonumberx(L,i,NULL)
#define lua_tointeger(L,i) lua_tointegerx(L,i,NULL)
+#define lua_tounsigned(L,i) lua_tounsignedx(L,i,NULL)
#define lua_pop(L,n) lua_settop(L, -(n)-1)