diff options
author | Asbjørn Sloth Tønnesen <ast@2e8.dk> | 2019-07-28 21:28:21 +0000 |
---|---|---|
committer | Asbjørn Sloth Tønnesen <ast@2e8.dk> | 2019-07-28 21:28:21 +0000 |
commit | 9c31bbc8cfd2ea8cfe2740c47c045e1773725315 (patch) | |
tree | d6616b441ac1fdcb4785b1dc727bf1e3f105baf9 | |
parent | de97a7a8473f68eff527c1ec6fb2482ba3d31523 (diff) | |
download | lua-inet-9c31bbc8cfd2ea8cfe2740c47c045e1773725315.tar.gz lua-inet-9c31bbc8cfd2ea8cfe2740c47c045e1773725315.tar.xz lua-inet-9c31bbc8cfd2ea8cfe2740c47c045e1773725315.zip |
add support for lua 5.4 alpha
Signed-off-by: Asbjørn Sloth Tønnesen <ast@2e8.dk>
-rw-r--r-- | README.rst | 2 | ||||
-rw-r--r-- | lua/inet/bitops/init.lua | 6 | ||||
-rw-r--r-- | lua/inet/bitops/native.lua | 105 | ||||
-rw-r--r-- | lua/inet/core.lua | 19 |
4 files changed, 123 insertions, 9 deletions
@@ -18,7 +18,7 @@ Dependencies ============ -- Lua_ version 5.2 or 5.3 +- Lua_ version 5.2, 5.3 or 5.4 alpha - LPeg_ - Parsing Expression Grammars For Lua API diff --git a/lua/inet/bitops/init.lua b/lua/inet/bitops/init.lua new file mode 100644 index 0000000..6af992b --- /dev/null +++ b/lua/inet/bitops/init.lua @@ -0,0 +1,6 @@ +local versions = { + ['Lua 5.2'] = 'bit32', +} + +local library = versions[_VERSION] or 'inet.bitops.native' +return assert(require(library)) diff --git a/lua/inet/bitops/native.lua b/lua/inet/bitops/native.lua new file mode 100644 index 0000000..48470f9 --- /dev/null +++ b/lua/inet/bitops/native.lua @@ -0,0 +1,105 @@ +-- Until Lua 5.2 support is dropped, we can't use the native operators directly +-- https://pastebin.com/fepexfdU - assumed public domain license + +--[[ Backwards compat for Lua 5.3; only loaded in 5.3 because package.loaded is + prepopulated with the existing global bit32 in 5.2. ]] + +local bit32 = {} + +------------------------------------------------------------------------------- + +local function fold(init, op, ...) + local result = init + local args = table.pack(...) + for i = 1, args.n do + result = op(result, args[i]) + end + return result +end + +local function trim(n) + return n & 0xFFFFFFFF +end + +local function mask(w) + return ~(0xFFFFFFFF << w) +end + +function bit32.arshift(x, disp) + return x // (2 ^ disp) +end + +function bit32.band(...) + return fold(0xFFFFFFFF, function(a, b) return a & b end, ...) +end + +function bit32.bnot(x) + return ~x +end + +function bit32.bor(...) + return fold(0, function(a, b) return a | b end, ...) +end + +function bit32.btest(...) + return bit32.band(...) ~= 0 +end + +function bit32.bxor(...) + return fold(0, function(a, b) return a ~ b end, ...) +end + +local function fieldargs(f, w) + w = w or 1 + assert(f >= 0, "field cannot be negative") + assert(w > 0, "width must be positive") + assert(f + w <= 32, "trying to access non-existent bits") + return f, w +end + +function bit32.extract(n, field, width) + local f, w = fieldargs(field, width) + return (n >> f) & mask(w) +end + +function bit32.replace(n, v, field, width) + local f, w = fieldargs(field, width) + local m = mask(w) + return (n & ~(m << f)) | ((v & m) << f) +end + +function bit32.lrotate(x, disp) + if disp == 0 then + return x + elseif disp < 0 then + return bit32.rrotate(x, -disp) + else + disp = disp & 31 + x = trim(x) + return trim((x << disp) | (x >> (32 - disp))) + end +end + +function bit32.lshift(x, disp) + return trim(x << disp) +end + +function bit32.rrotate(x, disp) + if disp == 0 then + return x + elseif disp < 0 then + return bit32.lrotate(x, -disp) + else + disp = disp & 31 + x = trim(x) + return trim((x >> disp) | (x << (32 - disp))) + end +end + +function bit32.rshift(x, disp) + return trim(x >> disp) +end + +------------------------------------------------------------------------------- + +return bit32 diff --git a/lua/inet/core.lua b/lua/inet/core.lua index 15dd0f9..7037885 100644 --- a/lua/inet/core.lua +++ b/lua/inet/core.lua @@ -1,6 +1,5 @@ -local bit32 = require 'bit32' - local common = require 'inet.common' +local bitops = require 'inet.bitops' local format = string.format local floor = math.floor @@ -9,12 +8,16 @@ local max = math.max local pow = math.pow local insert = table.insert -local lshift = bit32.lshift -local rshift = bit32.rshift -local band = bit32.band -local extract = bit32.extract -local replace = bit32.replace -local bxor = bit32.bxor +local lshift = bitops.lshift +local rshift = bitops.rshift +local band = bitops.band +local extract = bitops.extract +local replace = bitops.replace +local bxor = bitops.bxor + +if not pow then + function pow(x, y) return x ^ y end +end local get_mt = common.get_mt |