Skip to content

Commit

Permalink
bit: add a bit module compatible with Lua 5.1~5.3
Browse files Browse the repository at this point in the history
Signed-off-by: Jianhui Zhao <[email protected]>
  • Loading branch information
zhaojh329 committed Feb 9, 2023
1 parent 77530af commit cd380d9
Show file tree
Hide file tree
Showing 5 changed files with 80 additions and 77 deletions.
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,6 @@ install(
)

install(
FILES time.lua sys.lua file.lua buffer.lua dns.lua socket.lua http.lua websocket.lua mqtt.lua sync.lua
FILES time.lua sys.lua file.lua buffer.lua dns.lua socket.lua http.lua websocket.lua mqtt.lua sync.lua bit.lua
DESTINATION ${LUA_INSTALL_PREFIX}/eco
)
61 changes: 61 additions & 0 deletions bit.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
--[[
* MIT License
*
* Copyright (c) 2022 Jianhui Zhao <[email protected]>
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
--]]

local bit

if tonumber(_VERSION:match('%d%.%d')) < 5.3 then
bit = require 'bit'
else
bit = load([[
local M = {}
function M.band(a, b)
return a & b
end
function M.bor(a, b)
return a | b
end
function M.bxor(a, b)
return a ~ b
end
function M.rshift(x, n)
return x >> n
end
function M.lshift(x, n)
return x << n
end
function M.bnot(x)
return ~x
end
return M
]])()
end

return bit
33 changes: 4 additions & 29 deletions dns.lua
Original file line number Diff line number Diff line change
Expand Up @@ -25,36 +25,11 @@
--]]

local socket = require 'eco.socket'
local bit = require 'eco.bit'

local rshift
local lshift
local band

if tonumber(_VERSION:match('%d%.%d')) < 5.3 then
local bit = require 'bit'
rshift = bit.rshift
lshift = bit.lshift
band = bit.band
else
local bit = load([[
local M = {}
function M.rshift(x, n)
return x >> n
end
function M.lshift(x, n)
return x << n
end
function M.band(a, b)
return a & b
end
return M
]])()
rshift = bit.rshift
lshift = bit.lshift
band = bit.band
end
local rshift = bit.rshift
local lshift = bit.lshift
local band = bit.band

local TYPE_A = 1
local TYPE_NS = 2
Expand Down
12 changes: 8 additions & 4 deletions examples/network/ping.lua
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,11 @@

local socket = require 'eco.socket'
local time = require 'eco.time'
local bit = require 'bit'
local bit = require 'eco.bit'

local rshift = bit.rshift
local lshift = bit.lshift
local band = bit.band

local ICMP_HEADER_LEN = 8
local ICMP_ECHO = 8
Expand All @@ -25,7 +29,7 @@ local function build_icmp_req()
'\0', -- code
'\0\0', -- checksum
'\0\0', -- id: the kernel will assign it with local port
string.char(bit.rshift(local_seq, 8), bit.band(local_seq, 0xff)), -- sequence
string.char(rshift(local_seq, 8), band(local_seq, 0xff)), -- sequence
local_data
}

Expand All @@ -42,11 +46,11 @@ local function parse_icmp_resp(data)
local icmp_type = data:byte(1)
local id_hi = data:byte(5)
local id_lo = data:byte(6)
local id = bit.lshift(id_hi, 8) + id_lo
local id = lshift(id_hi, 8) + id_lo

local seq_hi = data:byte(7)
local seq_lo = data:byte(8)
local seq = bit.lshift(seq_hi, 8) + seq_lo
local seq = lshift(seq_hi, 8) + seq_lo

return icmp_type, id, seq, #data - ICMP_HEADER_LEN
end
Expand Down
49 changes: 6 additions & 43 deletions websocket.lua
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
local base64 = require 'eco.encoding.base64'
local sha1 = require 'eco.crypto.sha1'
local http = require 'eco.http'
local bit = require 'eco.bit'

local tostring = tostring
local concat = table.concat
Expand All @@ -37,49 +38,11 @@ local str_lower = string.lower
local str_sub = string.sub
local type = type

local rshift
local lshift
local band
local bor
local bxor

if tonumber(_VERSION:match('%d%.%d')) < 5.3 then
local bit = require 'bit'
rshift = bit.rshift
lshift = bit.lshift
band = bit.band
bor = bit.bor
bxor = bit.bxor
else
local bit = load([[
local M = {}
function M.rshift(x, n)
return x >> n
end
function M.lshift(x, n)
return x << n
end
function M.band(a, b)
return a & b
end
function M.bor(a, b)
return a | b
end
function M.bxor(a, b)
return a ~ b
end
return M
]])()
rshift = bit.rshift
lshift = bit.lshift
band = bit.band
bor = bit.bor
bxor = bit.bxor
end
local rshift = bit.rshift
local lshift = bit.lshift
local band = bit.band
local bor = bit.bor
local bxor = bit.bxor

local M = {}

Expand Down

0 comments on commit cd380d9

Please sign in to comment.