aboutsummaryrefslogtreecommitdiff
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
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.
-rw-r--r--lbitlib.c14
-rwxr-xr-xtests/test-bit32.lua1
2 files changed, 13 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 */
diff --git a/tests/test-bit32.lua b/tests/test-bit32.lua
index cc91e52..a408b7d 100755
--- a/tests/test-bit32.lua
+++ b/tests/test-bit32.lua
@@ -4,6 +4,7 @@ local bit32 = require("bit32")
4 4
5 5
6assert(bit32.bnot(0) == 2^32-1) 6assert(bit32.bnot(0) == 2^32-1)
7assert(bit32.bnot(-1) == 0)
7assert(bit32.band(1, 3, 5) == 1) 8assert(bit32.band(1, 3, 5) == 1)
8assert(bit32.bor(1, 3, 5) == 7) 9assert(bit32.bor(1, 3, 5) == 7)
9 10