From 932e7fb0e1b35758bd96361479ca3ddab3fc43da Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Tue, 4 Jun 2013 16:34:51 -0300 Subject: [PATCH] 'lua_tounsigned' takes number modulo 2^numbits as a result when number is a float (That may change...) --- lapi.c | 30 ++++++++++++++++++++++++++++-- 1 file changed, 28 insertions(+), 2 deletions(-) diff --git a/lapi.c b/lapi.c index 7b4e7024..73573c3d 100644 --- a/lapi.c +++ b/lapi.c @@ -1,10 +1,11 @@ /* -** $Id: lapi.c,v 2.179 2013/05/02 12:37:24 roberto Exp roberto $ +** $Id: lapi.c,v 2.180 2013/05/14 16:00:11 roberto Exp roberto $ ** Lua API ** See Copyright Notice in lua.h */ +#include #include #include @@ -376,7 +377,32 @@ LUA_API lua_Integer lua_tointegerx (lua_State *L, int idx, int *pisnum) { LUA_API lua_Unsigned lua_tounsignedx (lua_State *L, int idx, int *pisnum) { - return lua_tointegerx(L, idx, pisnum); /* at least for now... <<<< */ + lua_Unsigned res = 0; + const TValue *o = index2addr(L, idx); + int isnum = 0; + switch (ttype(o)) { + case LUA_TNUMINT: { + res = cast_unsigned(ivalue(o)); + isnum = 1; + break; + } + case LUA_TNUMFLT: { + const lua_Number twop = (~(lua_Unsigned)0) + cast_num(1); + lua_Number n = fltvalue(o); + n = l_mathop(fmod)(n, twop); + n = l_mathop(floor)(n); + if (luai_numisnan(L,n)) /* not a number? */ + break; /* not an integer, too */ + if (n < 0) + n += twop; /* correct 'mod' */ + res = cast_unsigned(n); + isnum = 1; + break; + } + default: break; + } + if (pisnum) *pisnum = isnum; + return res; }