diff --git a/http/bit.lua b/http/bit.lua index 0f6f0b69..3a95d1ed 100644 --- a/http/bit.lua +++ b/http/bit.lua @@ -7,16 +7,18 @@ 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 - -- 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 { - 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))() -end +-- Lua 5.1 uses loadstring for load with a string, Lua 5.2+ just +-- uses load. +local load = loadstring or load + +-- Lua 5.3+ has built-in bit operators, wrap them in a function. +local info = debug.getinfo(1, "Sl") +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)) +if has_bitwise then return bitwise end -- The "bit" library that comes with luajit -- also available for lua 5.1 as "luabitop": http://bitop.luajit.org/