aboutsummaryrefslogtreecommitdiff
path: root/lbitlib.c
diff options
context:
space:
mode:
authorPhilipp Janda <siffiejoe@gmx.net>2020-10-10 16:43:46 +0200
committerPhilipp Janda <siffiejoe@gmx.net>2020-10-10 16:43:46 +0200
commite245d3a18957e43ef902a59a72c8902e2e4435b9 (patch)
treeabfa4c390f13023f5959e29d6ec1b48b368fb1cf /lbitlib.c
parent8186510a981fcc72685716f15f100838fa7d0b4b (diff)
downloadlua-compat-5.3-0.10.tar.gz
lua-compat-5.3-0.10.tar.bz2
lua-compat-5.3-0.10.zip
Fix bit32 conversion issues for Lua 5.1 on 32 bitv0.10
The default unsigned conversion procedure from upstream using `lua_Integer` as an intermediate value fails if `lua_Integer` has only 32 bits (as is the case on 32 bit Lua 5.1). This fix uses a `lua_Number` (hopefully double) as intermediate value in those cases.
Diffstat (limited to 'lbitlib.c')
-rw-r--r--lbitlib.c14
1 files changed, 12 insertions, 2 deletions
diff --git a/lbitlib.c b/lbitlib.c
index 4786c0d..db2652a 100644
--- a/lbitlib.c
+++ b/lbitlib.c
@@ -19,8 +19,18 @@
19#if defined(LUA_COMPAT_BITLIB) /* { */ 19#if defined(LUA_COMPAT_BITLIB) /* { */
20 20
21 21
22#define pushunsigned(L,n) lua_pushinteger(L, (lua_Integer)(n)) 22#define pushunsigned(L,n) (sizeof(lua_Integer) > 4 ? lua_pushinteger(L, (lua_Integer)(n)) : lua_pushnumber(L, (lua_Number)(n)))
23#define checkunsigned(L,i) ((lua_Unsigned)luaL_checkinteger(L,i)) 23static lua_Unsigned checkunsigned(lua_State *L, int i) {
24 if (sizeof(lua_Integer) > 4)
25 return (lua_Unsigned)luaL_checkinteger(L, i);
26 else {
27 lua_Number d = luaL_checknumber(L, i);
28 if (d < 0)
29 d = (d + 1) + (~(lua_Unsigned)0);
30 luaL_argcheck(L, d >= 0 && d <= (~(lua_Unsigned)0), i, "value out of range");
31 return (lua_Unsigned)d;
32 }
33}
24 34
25 35
26/* number of bits to consider in a number */ 36/* number of bits to consider in a number */