Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 18 additions & 43 deletions lockbox/util/bit.lua
Original file line number Diff line number Diff line change
@@ -1,44 +1,19 @@
local ok, e
ok = nil
if not ok then
ok, e = pcall(require, "bit") -- the LuaJIT one ?
end
if not ok then
ok, e = pcall(require, "bit32") -- Lua 5.2
end
if not ok then
ok, e = pcall(require, "bit.numberlua") -- for Lua 5.1, https://github.com/tst2005/lua-bit-numberlua/
end
if not ok then
error("no bitwise support found", 2)
end
assert(type(e) == "table", "invalid bit module")
return {
band = function(l, r) return l & r end,
bor = function(l, r) return l | r end,
bxor = function(l, r) return l ~ r end,
bnot = function(u) return ~u end,
lshift = function(l, r) return l << r end,
rshift = function(l, r) return l >> r end,

-- Workaround to support Lua 5.2 bit32 API with the LuaJIT bit one
if e.rol and not e.lrotate then
e.lrotate = e.rol
end
if e.ror and not e.rrotate then
e.rrotate = e.ror
end

-- Workaround to support incomplete bit operations set
if not e.ror and not e.rrotate then
local ror = function(b, n)
return e.bor(e.rshift(b, n), e.lshift(b, 32 - n))
end

e.ror = ror
e.rrotate = ror
end

if not e.rol and not e.lrotate then
local rol = function(b, n)
return e.bor(e.lshift(b, n), e.rshift(b, 32 - n))
end

e.rol = rol
e.lrotate = rol
end

return e
lrotate = function(l, r)
l = l & 0xffffffff
r = r & 0x1f
return ((l << r) & 0xffffffff) | (l >> (32 - r))
end,
rrotate = function(l, r)
l = l & 0xffffffff
r = r & 0x1f
return (l >> r) | (0xffffffff & (l << (32 - r)))
end,
}
Comment on lines +1 to +19
Copy link

Copilot AI Jan 18, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This implementation uses native bitwise operators (&, |, ~, <<, >>) that were only introduced in Lua 5.3. However, the project's rockspec specifies "lua >= 5.2" and .travis.yml tests against Lua 5.2 and LuaJIT 2.0, neither of which support these operators. This change breaks backward compatibility and will cause runtime errors on Lua 5.2 and LuaJIT.

The previous implementation correctly used conditional requires to support multiple Lua versions. Either the Lua version requirement needs to be updated to >= 5.3 in the rockspec and CI configuration, or this implementation needs to be revised to maintain backward compatibility.

Copilot uses AI. Check for mistakes.