diff options
author | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2010-10-25 18:31:11 -0200 |
---|---|---|
committer | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2010-10-25 18:31:11 -0200 |
commit | c6b64ffe65549b179bfa565e8329430857e335ee (patch) | |
tree | 9fa644de3c5dc2e4edf42aac7249b42264f2f6e5 | |
parent | 4590a89b32b62c75fca7ced96c282c7793b8885c (diff) | |
download | lua-c6b64ffe65549b179bfa565e8329430857e335ee.tar.gz lua-c6b64ffe65549b179bfa565e8329430857e335ee.tar.bz2 lua-c6b64ffe65549b179bfa565e8329430857e335ee.zip |
new type lua_Unsigned and corresponding projection/injection functions
-rw-r--r-- | lapi.c | 29 | ||||
-rw-r--r-- | lauxlib.c | 17 | ||||
-rw-r--r-- | lauxlib.h | 5 | ||||
-rw-r--r-- | lbitlib.c | 25 | ||||
-rw-r--r-- | lmathlib.c | 4 | ||||
-rw-r--r-- | lua.h | 8 |
6 files changed, 66 insertions, 22 deletions
@@ -1,5 +1,5 @@ | |||
1 | /* | 1 | /* |
2 | ** $Id: lapi.c,v 2.137 2010/09/07 19:35:04 roberto Exp roberto $ | 2 | ** $Id: lapi.c,v 2.138 2010/10/25 19:01:37 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 | */ |
@@ -347,6 +347,23 @@ LUA_API lua_Integer lua_tointegerx (lua_State *L, int idx, int *isnum) { | |||
347 | } | 347 | } |
348 | 348 | ||
349 | 349 | ||
350 | LUA_API lua_Unsigned lua_tounsignedx (lua_State *L, int idx, int *isnum) { | ||
351 | TValue n; | ||
352 | const TValue *o = index2addr(L, idx); | ||
353 | if (tonumber(o, &n)) { | ||
354 | lua_Unsigned res; | ||
355 | lua_Number num = nvalue(o); | ||
356 | lua_number2uint(res, num); | ||
357 | if (isnum) *isnum = 1; | ||
358 | return res; | ||
359 | } | ||
360 | else { | ||
361 | if (isnum) *isnum = 0; | ||
362 | return 0; | ||
363 | } | ||
364 | } | ||
365 | |||
366 | |||
350 | LUA_API int lua_toboolean (lua_State *L, int idx) { | 367 | LUA_API int lua_toboolean (lua_State *L, int idx) { |
351 | const TValue *o = index2addr(L, idx); | 368 | const TValue *o = index2addr(L, idx); |
352 | return !l_isfalse(o); | 369 | return !l_isfalse(o); |
@@ -452,6 +469,16 @@ LUA_API void lua_pushinteger (lua_State *L, lua_Integer n) { | |||
452 | } | 469 | } |
453 | 470 | ||
454 | 471 | ||
472 | LUA_API void lua_pushunsigned (lua_State *L, lua_Unsigned u) { | ||
473 | lua_Number n; | ||
474 | lua_lock(L); | ||
475 | n = lua_uint2number(u); | ||
476 | setnvalue(L->top, n); | ||
477 | api_incr_top(L); | ||
478 | lua_unlock(L); | ||
479 | } | ||
480 | |||
481 | |||
455 | LUA_API const char *lua_pushlstring (lua_State *L, const char *s, size_t len) { | 482 | LUA_API const char *lua_pushlstring (lua_State *L, const char *s, size_t len) { |
456 | TString *ts; | 483 | TString *ts; |
457 | lua_lock(L); | 484 | lua_lock(L); |
@@ -1,5 +1,5 @@ | |||
1 | /* | 1 | /* |
2 | ** $Id: lauxlib.c,v 1.221 2010/10/01 18:53:00 roberto Exp roberto $ | 2 | ** $Id: lauxlib.c,v 1.222 2010/10/25 19:01:37 roberto Exp roberto $ |
3 | ** Auxiliary functions for building Lua libraries | 3 | ** Auxiliary functions for building Lua libraries |
4 | ** See Copyright Notice in lua.h | 4 | ** See Copyright Notice in lua.h |
5 | */ | 5 | */ |
@@ -330,11 +330,26 @@ LUALIB_API lua_Integer luaL_checkinteger (lua_State *L, int narg) { | |||
330 | } | 330 | } |
331 | 331 | ||
332 | 332 | ||
333 | LUALIB_API lua_Unsigned luaL_checkunsigned (lua_State *L, int narg) { | ||
334 | int isnum; | ||
335 | lua_Unsigned d = lua_tounsignedx(L, narg, &isnum); | ||
336 | if (!isnum) | ||
337 | tag_error(L, narg, LUA_TNUMBER); | ||
338 | return d; | ||
339 | } | ||
340 | |||
341 | |||
333 | LUALIB_API lua_Integer luaL_optinteger (lua_State *L, int narg, | 342 | LUALIB_API lua_Integer luaL_optinteger (lua_State *L, int narg, |
334 | lua_Integer def) { | 343 | lua_Integer def) { |
335 | return luaL_opt(L, luaL_checkinteger, narg, def); | 344 | return luaL_opt(L, luaL_checkinteger, narg, def); |
336 | } | 345 | } |
337 | 346 | ||
347 | |||
348 | LUALIB_API lua_Unsigned luaL_optunsigned (lua_State *L, int narg, | ||
349 | lua_Unsigned def) { | ||
350 | return luaL_opt(L, luaL_checkunsigned, narg, def); | ||
351 | } | ||
352 | |||
338 | /* }====================================================== */ | 353 | /* }====================================================== */ |
339 | 354 | ||
340 | 355 | ||
@@ -1,5 +1,5 @@ | |||
1 | /* | 1 | /* |
2 | ** $Id: lauxlib.h,v 1.107 2010/06/30 17:40:27 roberto Exp roberto $ | 2 | ** $Id: lauxlib.h,v 1.108 2010/07/02 11:38:13 roberto Exp roberto $ |
3 | ** Auxiliary functions for building Lua libraries | 3 | ** Auxiliary functions for building Lua libraries |
4 | ** See Copyright Notice in lua.h | 4 | ** See Copyright Notice in lua.h |
5 | */ | 5 | */ |
@@ -44,6 +44,9 @@ LUALIB_API lua_Number (luaL_optnumber) (lua_State *L, int nArg, lua_Number def); | |||
44 | LUALIB_API lua_Integer (luaL_checkinteger) (lua_State *L, int numArg); | 44 | LUALIB_API lua_Integer (luaL_checkinteger) (lua_State *L, int numArg); |
45 | LUALIB_API lua_Integer (luaL_optinteger) (lua_State *L, int nArg, | 45 | LUALIB_API lua_Integer (luaL_optinteger) (lua_State *L, int nArg, |
46 | lua_Integer def); | 46 | lua_Integer def); |
47 | LUALIB_API lua_Unsigned (luaL_checkunsigned) (lua_State *L, int numArg); | ||
48 | LUALIB_API lua_Unsigned (luaL_optunsigned) (lua_State *L, int numArg, | ||
49 | lua_Unsigned def); | ||
47 | 50 | ||
48 | LUALIB_API void (luaL_checkstack) (lua_State *L, int sz, const char *msg); | 51 | LUALIB_API void (luaL_checkstack) (lua_State *L, int sz, const char *msg); |
49 | LUALIB_API void (luaL_checktype) (lua_State *L, int narg, int t); | 52 | LUALIB_API void (luaL_checktype) (lua_State *L, int narg, int t); |
@@ -1,5 +1,5 @@ | |||
1 | /* | 1 | /* |
2 | ** $Id: lbitlib.c,v 1.6 2010/07/02 12:01:53 roberto Exp roberto $ | 2 | ** $Id: lbitlib.c,v 1.7 2010/10/25 14:32:36 roberto Exp roberto $ |
3 | ** Standard library for bitwise operations | 3 | ** Standard library for bitwise operations |
4 | ** See Copyright Notice in lua.h | 4 | ** See Copyright Notice in lua.h |
5 | */ | 5 | */ |
@@ -21,14 +21,7 @@ typedef LUA_INT32 b_int; | |||
21 | typedef unsigned LUA_INT32 b_uint; | 21 | typedef unsigned LUA_INT32 b_uint; |
22 | 22 | ||
23 | 23 | ||
24 | static b_uint getuintarg (lua_State *L, int arg) { | 24 | #define getuintarg(L,arg) luaL_checkunsigned(L,arg) |
25 | b_uint r; | ||
26 | int isnum; | ||
27 | lua_Number x = lua_tonumberx(L, arg, &isnum); | ||
28 | if (!isnum) luaL_typeerror(L, arg, "number"); | ||
29 | lua_number2uint(r, x); | ||
30 | return r; | ||
31 | } | ||
32 | 25 | ||
33 | 26 | ||
34 | static b_uint andaux (lua_State *L) { | 27 | static b_uint andaux (lua_State *L) { |
@@ -42,7 +35,7 @@ static b_uint andaux (lua_State *L) { | |||
42 | 35 | ||
43 | static int b_and (lua_State *L) { | 36 | static int b_and (lua_State *L) { |
44 | b_uint r = andaux(L); | 37 | b_uint r = andaux(L); |
45 | lua_pushnumber(L, lua_uint2number(r)); | 38 | lua_pushunsigned(L, r); |
46 | return 1; | 39 | return 1; |
47 | } | 40 | } |
48 | 41 | ||
@@ -59,7 +52,7 @@ static int b_or (lua_State *L) { | |||
59 | b_uint r = 0; | 52 | b_uint r = 0; |
60 | for (i = 1; i <= n; i++) | 53 | for (i = 1; i <= n; i++) |
61 | r |= getuintarg(L, i); | 54 | r |= getuintarg(L, i); |
62 | lua_pushnumber(L, lua_uint2number(r)); | 55 | lua_pushunsigned(L, r); |
63 | return 1; | 56 | return 1; |
64 | } | 57 | } |
65 | 58 | ||
@@ -69,14 +62,14 @@ static int b_xor (lua_State *L) { | |||
69 | b_uint r = 0; | 62 | b_uint r = 0; |
70 | for (i = 1; i <= n; i++) | 63 | for (i = 1; i <= n; i++) |
71 | r ^= getuintarg(L, i); | 64 | r ^= getuintarg(L, i); |
72 | lua_pushnumber(L, lua_uint2number(r)); | 65 | lua_pushunsigned(L, r); |
73 | return 1; | 66 | return 1; |
74 | } | 67 | } |
75 | 68 | ||
76 | 69 | ||
77 | static int b_not (lua_State *L) { | 70 | static int b_not (lua_State *L) { |
78 | b_uint r = ~getuintarg(L, 1); | 71 | b_uint r = ~getuintarg(L, 1); |
79 | lua_pushnumber(L, lua_uint2number(r)); | 72 | lua_pushunsigned(L, r); |
80 | return 1; | 73 | return 1; |
81 | } | 74 | } |
82 | 75 | ||
@@ -91,7 +84,7 @@ static int b_shift (lua_State *L, b_uint r, int i) { | |||
91 | if (i >= NBITS) r = 0; | 84 | if (i >= NBITS) r = 0; |
92 | else r <<= i; | 85 | else r <<= i; |
93 | } | 86 | } |
94 | lua_pushnumber(L, lua_uint2number(r)); | 87 | lua_pushunsigned(L, r); |
95 | return 1; | 88 | return 1; |
96 | } | 89 | } |
97 | 90 | ||
@@ -115,7 +108,7 @@ static int b_arshift (lua_State *L) { | |||
115 | if (i >= NBITS) r = 0xffffffff; | 108 | if (i >= NBITS) r = 0xffffffff; |
116 | else | 109 | else |
117 | r = (r >> i) | ~(~(b_uint)0 >> i); /* add signal bit */ | 110 | r = (r >> i) | ~(~(b_uint)0 >> i); /* add signal bit */ |
118 | lua_pushnumber(L, lua_uint2number(r)); | 111 | lua_pushunsigned(L, r); |
119 | return 1; | 112 | return 1; |
120 | } | 113 | } |
121 | } | 114 | } |
@@ -125,7 +118,7 @@ static int b_rot (lua_State *L, int i) { | |||
125 | b_uint r = getuintarg(L, 1); | 118 | b_uint r = getuintarg(L, 1); |
126 | i &= (NBITS - 1); /* i = i % NBITS */ | 119 | i &= (NBITS - 1); /* i = i % NBITS */ |
127 | r = (r << i) | (r >> (NBITS - i)); | 120 | r = (r << i) | (r >> (NBITS - i)); |
128 | lua_pushnumber(L, lua_uint2number(r)); | 121 | lua_pushunsigned(L, r); |
129 | return 1; | 122 | return 1; |
130 | } | 123 | } |
131 | 124 | ||
@@ -1,5 +1,5 @@ | |||
1 | /* | 1 | /* |
2 | ** $Id: lmathlib.c,v 1.74 2009/11/24 12:05:44 roberto Exp roberto $ | 2 | ** $Id: lmathlib.c,v 1.75 2010/07/02 11:38:13 roberto Exp roberto $ |
3 | ** Standard mathematical library | 3 | ** Standard mathematical library |
4 | ** See Copyright Notice in lua.h | 4 | ** See Copyright Notice in lua.h |
5 | */ | 5 | */ |
@@ -220,7 +220,7 @@ static int math_random (lua_State *L) { | |||
220 | 220 | ||
221 | 221 | ||
222 | static int math_randomseed (lua_State *L) { | 222 | static int math_randomseed (lua_State *L) { |
223 | srand(luaL_checkint(L, 1)); | 223 | srand(luaL_checkunsigned(L, 1)); |
224 | (void)rand(); /* discard first value to avoid undesirable correlations */ | 224 | (void)rand(); /* discard first value to avoid undesirable correlations */ |
225 | return 0; | 225 | return 0; |
226 | } | 226 | } |
@@ -1,5 +1,5 @@ | |||
1 | /* | 1 | /* |
2 | ** $Id: lua.h,v 1.273 2010/07/25 15:18:19 roberto Exp roberto $ | 2 | ** $Id: lua.h,v 1.274 2010/09/03 14:14:01 roberto Exp roberto $ |
3 | ** Lua - A Scripting Language | 3 | ** Lua - A Scripting Language |
4 | ** Lua.org, PUC-Rio, Brazil (http://www.lua.org) | 4 | ** Lua.org, PUC-Rio, Brazil (http://www.lua.org) |
5 | ** See Copyright Notice at the end of this file | 5 | ** See Copyright Notice at the end of this file |
@@ -106,6 +106,9 @@ typedef LUA_NUMBER lua_Number; | |||
106 | /* type for integer functions */ | 106 | /* type for integer functions */ |
107 | typedef LUA_INTEGER lua_Integer; | 107 | typedef LUA_INTEGER lua_Integer; |
108 | 108 | ||
109 | /* unsigned integer type */ | ||
110 | typedef unsigned LUA_INT32 lua_Unsigned; | ||
111 | |||
109 | 112 | ||
110 | 113 | ||
111 | /* | 114 | /* |
@@ -159,6 +162,7 @@ LUA_API const char *(lua_typename) (lua_State *L, int tp); | |||
159 | 162 | ||
160 | LUA_API lua_Number (lua_tonumberx) (lua_State *L, int idx, int *isnum); | 163 | LUA_API lua_Number (lua_tonumberx) (lua_State *L, int idx, int *isnum); |
161 | LUA_API lua_Integer (lua_tointegerx) (lua_State *L, int idx, int *isnum); | 164 | LUA_API lua_Integer (lua_tointegerx) (lua_State *L, int idx, int *isnum); |
165 | LUA_API lua_Unsigned (lua_tounsignedx) (lua_State *L, int idx, int *isnum); | ||
162 | LUA_API int (lua_toboolean) (lua_State *L, int idx); | 166 | LUA_API int (lua_toboolean) (lua_State *L, int idx); |
163 | LUA_API const char *(lua_tolstring) (lua_State *L, int idx, size_t *len); | 167 | LUA_API const char *(lua_tolstring) (lua_State *L, int idx, size_t *len); |
164 | LUA_API size_t (lua_rawlen) (lua_State *L, int idx); | 168 | 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); | |||
196 | LUA_API void (lua_pushnil) (lua_State *L); | 200 | LUA_API void (lua_pushnil) (lua_State *L); |
197 | LUA_API void (lua_pushnumber) (lua_State *L, lua_Number n); | 201 | LUA_API void (lua_pushnumber) (lua_State *L, lua_Number n); |
198 | LUA_API void (lua_pushinteger) (lua_State *L, lua_Integer n); | 202 | LUA_API void (lua_pushinteger) (lua_State *L, lua_Integer n); |
203 | LUA_API void (lua_pushunsigned) (lua_State *L, lua_Unsigned n); | ||
199 | LUA_API const char *(lua_pushlstring) (lua_State *L, const char *s, size_t l); | 204 | LUA_API const char *(lua_pushlstring) (lua_State *L, const char *s, size_t l); |
200 | LUA_API const char *(lua_pushstring) (lua_State *L, const char *s); | 205 | LUA_API const char *(lua_pushstring) (lua_State *L, const char *s); |
201 | LUA_API const char *(lua_pushvfstring) (lua_State *L, const char *fmt, | 206 | 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); | |||
303 | 308 | ||
304 | #define lua_tonumber(L,i) lua_tonumberx(L,i,NULL) | 309 | #define lua_tonumber(L,i) lua_tonumberx(L,i,NULL) |
305 | #define lua_tointeger(L,i) lua_tointegerx(L,i,NULL) | 310 | #define lua_tointeger(L,i) lua_tointegerx(L,i,NULL) |
311 | #define lua_tounsigned(L,i) lua_tounsignedx(L,i,NULL) | ||
306 | 312 | ||
307 | #define lua_pop(L,n) lua_settop(L, -(n)-1) | 313 | #define lua_pop(L,n) lua_settop(L, -(n)-1) |
308 | 314 | ||