From 305850e0328b4689eaf891e515ecea212e94de77 Mon Sep 17 00:00:00 2001 From: Lev Leontiev Date: Sun, 8 Mar 2026 16:51:52 +0000 Subject: [PATCH] fix: make mock_ngx.lua Lua 5.1/LuaJIT-compatible (refs #2) Replace Lua 5.3+ bitwise XOR operator ~ with bit.bxor and add safe fallback. --- spec/helpers/mock_ngx.lua | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/spec/helpers/mock_ngx.lua b/spec/helpers/mock_ngx.lua index 58f9fdc..12ac5d8 100644 --- a/spec/helpers/mock_ngx.lua +++ b/spec/helpers/mock_ngx.lua @@ -5,6 +5,10 @@ local string_sub = string.sub local string_find = string.find local table_concat = table.concat +local bit +local ok_bit, b = pcall(require, "bit") +if ok_bit then bit = b end + local _M = {} local string_byte = string.byte @@ -106,7 +110,12 @@ local function _simple_digest(input) local h2 = 16777619 for i = 1, #input do local b = string_byte(input, i) - h1 = (h1 ~ b) % 4294967296 + if bit then + h1 = bit.bxor(h1, b) % 4294967296 + else + -- Fallback if bit is not available (not cryptographically same, but avoids crash) + h1 = (h1 + b) % 4294967296 + end h1 = (h1 * 16777619) % 4294967296 h2 = (h2 + (b * i)) % 4294967296 end