Use lua native mods for bit rotation#2
Conversation
There was a problem hiding this comment.
Pull request overview
This PR replaces the dynamic bit library loading system with native Lua bitwise operators to simplify the codebase and remove dependencies on external bit libraries.
Changes:
- Removed conditional loading of bit libraries (bit, bit32, bit.numberlua) and compatibility layer
- Implemented all bitwise operations (band, bor, bxor, bnot, lshift, rshift, lrotate, rrotate) using native Lua operators (&, |, ~, <<, >>)
- Simplified bit rotation functions with direct implementations using native operators
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| 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, | ||
| } |
There was a problem hiding this comment.
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.
6b497e2 to
2af0128
Compare
020e5a8 to
3ae92ad
Compare
|
Closed in favor of focused migration branches |
No description provided.