Skip to content

Commit

Permalink
Fix. Unpack integer if bit library returns signed value.
Browse files Browse the repository at this point in the history
  • Loading branch information
moteus committed Feb 27, 2017
1 parent 1c84693 commit 8f33fff
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 10 deletions.
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ before_install:
- psql -c "create database $LLUV_PG_DBNAME;" -U postgres

install:
- if [ "$LUA" != "lua 5.3" ]; then luarocks show struct > /dev/null 2>&1 || luarocks install struct; fi
- luarocks show lluv > /dev/null 2>&1 || luarocks install lluv UV_DIR=$TRAVIS_BUILD_DIR/libuv
- luarocks show null > /dev/null 2>&1 || luarocks install null --server=http://luarocks.org/dev
- luarocks make rockspecs/lluv-pg-scm-0.rockspec
Expand Down
2 changes: 1 addition & 1 deletion rockspecs/lluv-pg-scm-0.rockspec
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ dependencies = {
"lua >= 5.1, < 5.4",
"lluv > 0.1.1",
"eventemitter",
"struct >= 1.2",
-- "struct >= 1.2",
"lmd5",
"null",
"luuid",
Expand Down
14 changes: 10 additions & 4 deletions spec/bin_spec.busted
Original file line number Diff line number Diff line change
@@ -1,10 +1,16 @@
io.stdout:setvbuf'no';io.stderr:setvbuf'no';
package.path = "..\\src\\?.lua;" .. package.path

local struct = require "struct"
local bin = require "lluv.pg.utils.bin"
local prequire = function(m)
local ok, m = pcall(require, m)
if ok then return m end
end

describe("test struct", function()
-- seems Lua struct has problem with Lua 5.3
local struct = prequire "struct"
local bin = require "lluv.pg.utils.bin"

if struct then describe("test struct", function()
local function check_unpack(fmt, int)
local s = struct.pack(fmt, int)
local a = struct.unpack(fmt, s)
Expand Down Expand Up @@ -208,4 +214,4 @@ describe("test struct", function()
assert.equal(#s + 1,pos)
end)

end)
end) end
21 changes: 16 additions & 5 deletions src/lluv/pg/utils/bin.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ local struct_unpack, struct_pack

if string.pack then -- Lua 5.3
struct_unpack, struct_pack, struct_size = string.unpack, string.pack
elseif not jit then -- Lua 5.1, 5.3
elseif not jit then -- Lua 5.1, 5.2
local struct = require "struct"
struct_unpack, struct_pack, struct_size = struct.unpack, struct.pack
else -- LuaJIT
Expand All @@ -11,6 +11,19 @@ local unpack = unpack or table.unpack

local bit = require "bit"

local is_bit_has_sign = bit.bor(0xFFFFFFFF, 0xFFFFFFFF) ~= 0xFFFFFFFF

local lshift
if is_bit_has_sign then
lshift = function(n, p)
return n * 2^p
end
else
lshift = function(n, p)
return bit.lshift(n, p)
end
end

local function sign1(n)
if n >= 0x80 then
n = -1 - bit.band(0xFF, bit.bnot(n) )
Expand Down Expand Up @@ -89,7 +102,7 @@ local function read_be_uint4(str, pos)
local a, b
a, pos = read_be_uint2(str, pos)
b, pos = read_be_uint2(str, pos)
return bit.lshift(a, 16) + b, pos
return lshift(a, 16) + b, pos
end

local function read_be_int4(str, pos)
Expand All @@ -103,7 +116,7 @@ local function read_le_uint4(str, pos)
local a, b
a, pos = read_le_uint2(str, pos)
b, pos = read_le_uint2(str, pos)
return a + bit.lshift(b, 16), pos
return a + lshift(b, 16), pos
end

local function read_le_int4(str, pos)
Expand Down Expand Up @@ -201,8 +214,6 @@ local function pack_zstr(s)
return s .. '\0'
end

local function printf(...) print(string.format(...)) end

local sunpack do

local unpack_int = {
Expand Down

0 comments on commit 8f33fff

Please sign in to comment.