Skip to content

Commit

Permalink
handle INT_MIN (#9), and test that integer/float representation prese…
Browse files Browse the repository at this point in the history
…rves the type on Lua 5.3 (close #5)
  • Loading branch information
grafi-tt committed Sep 3, 2017
1 parent b0b2eb3 commit 18692c1
Show file tree
Hide file tree
Showing 16 changed files with 604 additions and 564 deletions.
13 changes: 10 additions & 3 deletions src/lunajson/decoder.lua
Expand Up @@ -4,6 +4,8 @@ local byte, char, find, gsub, match, sub =
string.byte, string.char, string.find, string.gsub, string.match, string.sub
local floor, inf =
math.floor, math.huge
local mininteger, tointeger =
math.mininteger or nil, math.tointeger or nil

local _ENV = nil

Expand Down Expand Up @@ -141,11 +143,16 @@ local function newdecoder()
end
pos = postmp
num = fixedtonumber(num)
c = fixedtonumber(num)
if mns then
num = -num
c = -c
if c == mininteger then
if not find(num, '[^0-9]') then
c = mininteger
end
end
end
return num
return c
end
-- skip minus sign
Expand Down
20 changes: 17 additions & 3 deletions src/lunajson/sax.lua
Expand Up @@ -4,10 +4,13 @@ local byte, char, find, gsub, match, sub =
string.byte, string.char, string.find, string.gsub, string.match, string.sub
local floor, inf =
math.floor, math.huge
local mininteger, tointeger =
math.mininteger or nil, math.tointeger or nil
local type, unpack = type, table.unpack or unpack

local _ENV = nil


local function nop() end

local function newparser(src, saxtbl)
Expand Down Expand Up @@ -159,6 +162,7 @@ local function newparser(src, saxtbl)
local function generic_number(mns)
local buf = {}
local i = 1
local is_int = true
local c = byte(json, pos)
pos = pos+1
Expand All @@ -176,13 +180,15 @@ local function newparser(src, saxtbl)
repeat nxt() until not (c and 0x30 <= c and c < 0x3A)
end
if c == 0x2E then
is_int = false
nxt()
if not (c and 0x30 <= c and c < 0x3A) then
parseerror('invalid number')
end
repeat nxt() until not (c and 0x30 <= c and c < 0x3A)
end
if c == 0x45 or c == 0x65 then
is_int = false
nxt()
if c == 0x2B or c == 0x2D then
nxt()
Expand All @@ -198,6 +204,9 @@ local function newparser(src, saxtbl)
num = fixedtonumber(num)
if mns then
num = -num
if num == mininteger and is_int then
num = mininteger
end
end
return sax_number(num)
end
Expand Down Expand Up @@ -270,11 +279,16 @@ local function newparser(src, saxtbl)
return generic_number(mns)
end
pos = postmp
num = fixedtonumber(num)
c = fixedtonumber(num)
if mns then
num = -num
c = -c
if c == mininteger then
if match(num, '^[0-9]*$') then
c = tointeger(c)
end
end
end
return sax_number(num)
return sax_number(c)
end
-- skip minus sign
Expand Down
10 changes: 9 additions & 1 deletion test/decoder/test.lua
Expand Up @@ -37,6 +37,14 @@ local function isomorphic(u, v)
isomorphic(u[j], v[k])
until false
else
if math.type then
s = math.type(u)
t = math.type(v)
if s ~= t then
error('different values: ' .. tostring(s) .. tostring(u) .. ' and ' .. tostring(t) .. tostring(v))

end
end
if u ~= v then
error('different values: ' .. tostring(u) .. ' and ' .. tostring(v))
end
Expand All @@ -55,7 +63,7 @@ local function test_valid(decode, fn)
local function check()
local v = decode(json, nullv)
local ans = util.load(fn .. '.lua')
isomorphic(ans, v)
isomorphic(v, ans)
end
local ok, err = pcall(check)
if not ok then
Expand Down
2 changes: 2 additions & 0 deletions test/decoder/valid_data.lua
Expand Up @@ -5,6 +5,8 @@ return {
'backslash',
'emptyarray',
'emptyobject',
'mininteger',
'mininteger-f',
'regarded-as-rest-digits',
'regarded-as-rest-eperiod',
'regarded-as-rest-period',
Expand Down
2 changes: 1 addition & 1 deletion test/decoder/valid_data/0e.json
@@ -1 +1 @@
[0e+1]
0e+1
2 changes: 1 addition & 1 deletion test/decoder/valid_data/0e.lua
@@ -1 +1 @@
return {0}
return 0.0
2 changes: 1 addition & 1 deletion test/decoder/valid_data/0p0e.json
@@ -1 +1 @@
[0.0e+1]
0.0e+1
2 changes: 1 addition & 1 deletion test/decoder/valid_data/0p0e.lua
@@ -1 +1 @@
return {0}
return 0.0
1 change: 1 addition & 0 deletions test/decoder/valid_data/mininteger-f.json
@@ -0,0 +1 @@
-9223372036854775808.0
1 change: 1 addition & 0 deletions test/decoder/valid_data/mininteger-f.lua
@@ -0,0 +1 @@
return -9223372036854775808.0
1 change: 1 addition & 0 deletions test/decoder/valid_data/mininteger.json
@@ -0,0 +1 @@
-9223372036854775808
1 change: 1 addition & 0 deletions test/decoder/valid_data/mininteger.lua
@@ -0,0 +1 @@
return math.mininteger or -9223372036854775808
2 changes: 1 addition & 1 deletion test/decoder/valid_data/regarded-as-rest-eperiod.lua
@@ -1 +1 @@
return 10
return 10.0

0 comments on commit 18692c1

Please sign in to comment.