From 702a1260e24966b0ae07668e33359914a5405c4e Mon Sep 17 00:00:00 2001 From: John Regan Date: Mon, 23 Nov 2020 13:59:23 -0500 Subject: [PATCH 1/2] try using native bitwise operations without checking lua version --- http/bit.lua | 27 ++++++++++++++++++--------- 1 file changed, 18 insertions(+), 9 deletions(-) diff --git a/http/bit.lua b/http/bit.lua index 0f6f0b69..55c0dfae 100644 --- a/http/bit.lua +++ b/http/bit.lua @@ -7,17 +7,26 @@ 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))() +-- Lua 5.1's load function doesn't support reading strings, only +-- functions. +local function string_loader(str) + local sent = false + return function() + if sent then return nil end + sent = false + return str + end end +-- 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(string_loader(("\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/ local has_bit, bit = pcall(require, "bit") From 6c3c69ce7c8bbcd68ba14c37954fd1c93b350ef0 Mon Sep 17 00:00:00 2001 From: John Regan Date: Tue, 24 Nov 2020 08:54:52 -0500 Subject: [PATCH 2/2] bit: check for loadstring and use it if it exists --- http/bit.lua | 17 +++++------------ 1 file changed, 5 insertions(+), 12 deletions(-) diff --git a/http/bit.lua b/http/bit.lua index 55c0dfae..3a95d1ed 100644 --- a/http/bit.lua +++ b/http/bit.lua @@ -7,24 +7,17 @@ The bit operations are only done This means we can ignore the differences between bit libraries. ]] --- Lua 5.1's load function doesn't support reading strings, only --- functions. -local function string_loader(str) - local sent = false - return function() - if sent then return nil end - sent = false - return str - end -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(string_loader(("\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 -- The "bit" library that comes with luajit