Skip to content

http/bit: refactor to try bitwise operators in 5.3+ #180

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 27, 2021
Merged
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
12 changes: 8 additions & 4 deletions http/bit.lua
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,19 @@ The bit operations are only done
This means we can ignore the differences between bit libraries.
]]

-- Lua 5.3 has built-in bit operators, wrap them in a function.
if _VERSION == "Lua 5.3" then
-- Lua 5.1 didn't have `load` or bitwise operators, just let it fall through.
if _VERSION ~= "Lua 5.1" then
-- Lua 5.3+ has built-in bit operators, wrap them in a function.
-- Use debug.getinfo to get correct file+line numbers for loaded snippet
local info = debug.getinfo(1, "Sl")
return assert(load(("\n"):rep(info.currentline+1)..[[return {
local has_bitwise, bitwise = pcall(load(("\n"):rep(info.currentline+1)..[[return {
band = function(a, b) return a & b end;
bor = function(a, b) return a | b end;
bxor = function(a, b) return a ~ b end;
}]], info.source))()
}]], info.source))
if has_bitwise then
return bitwise
end
end

-- The "bit" library that comes with luajit
Expand Down