Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions LuaGObject/marshal.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ static lua_Number
check_integer(lua_State *L, int narg, lua_Number val_min, lua_Number val_max)
{
lua_Number val = luaL_checknumber(L, narg);
/* If the value is negative and the target type is unsigned, return it so it underflows. */
if (val_min == 0 && val < 0)
return val;
if (val < val_min || val > val_max) {
lua_pushfstring(L, "%f is out of <%f, %f>", val, val_min, val_max);
luaL_argerror(L, narg, lua_tostring(L, -1));
Expand All @@ -32,6 +35,9 @@ static lua_Integer
check_integer(lua_State *L, int narg, lua_Integer val_min, lua_Integer val_max)
{
lua_Integer val = luaL_checkint(L, narg);
/* If the value is negative and the target type is unsigned, return it so it underflows. */
if (val_min == 0 && val < 0)
return val;
if (val < val_min || val > val_max) {
lua_pushfstring(L, "%I is out of <%I, %I>", val, val_min, val_max);
luaL_argerror(L, narg, lua_tostring(L, -1));
Expand Down
25 changes: 15 additions & 10 deletions tests/gireg.lua
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,10 @@ function gireg.type_uint8()
if not nativeIntegers then
checkv(R.test_uint8(1.1), 1, 'number')
end
checkv(R.test_uint8(-1), 0xff, 'number')
checkv(R.test_uint8(-2), 0xfe, 'number')
checkv(R.test_uint8(0xff), 0xff, 'number')
check(not pcall(R.test_uint8, 0x100))
check(not pcall(R.test_uint8, -1))
check(not pcall(R.test_uint8))
check(not pcall(R.test_uint8, nil))
check(not pcall(R.test_uint8, 'string'))
Expand Down Expand Up @@ -95,9 +96,10 @@ function gireg.type_uint16()
if not nativeIntegers then
checkv(R.test_uint16(1.1), 1, 'number')
end
checkv(R.test_uint16(-1), 0xffff, 'number')
checkv(R.test_uint16(-3), 0xfffd, 'number')
checkv(R.test_uint16(0xffff), 0xffff, 'number')
check(not pcall(R.test_uint16, 0x10000))
check(not pcall(R.test_uint16, -1))
check(not pcall(R.test_uint16))
check(not pcall(R.test_uint16, nil))
check(not pcall(R.test_uint16, 'string'))
Expand Down Expand Up @@ -134,9 +136,10 @@ function gireg.type_uint32()
if not nativeIntegers then
checkv(R.test_uint32(1.1), 1, 'number')
end
checkv(R.test_uint32(-1), 0xffffffff, 'number')
checkv(R.test_uint32(-4), 0xfffffffc, 'number')
checkv(R.test_uint32(0xffffffff), 0xffffffff, 'number')
check(not pcall(R.test_uint32, 0x100000000))
check(not pcall(R.test_uint32, -1))
check(not pcall(R.test_uint32))
check(not pcall(R.test_uint32, nil))
check(not pcall(R.test_uint32, 'string'))
Expand Down Expand Up @@ -181,16 +184,22 @@ function gireg.type_uint64()
if not nativeIntegers then
checkv(R.test_uint64(1.1), 1, 'number')
end
check(not pcall(R.test_uint64, -1))
if nativeIntegers then
checkv(R.test_uint64(-1), 0xffffffffffffffff, 'number')
checkv(R.test_uint64(-5), 0xfffffffffffffffb, 'number')
end
check(not pcall(R.test_uint64))
check(not pcall(R.test_uint64, nil))
check(not pcall(R.test_uint64, 'string'))
check(not pcall(R.test_uint64, true))
check(not pcall(R.test_uint64, {}))
check(not pcall(R.test_uint64, function() end))

-- With integer underflows, this actually works.
if nativeIntegers then
checkv(R.test_uint64(0xffffffffffffffff), 0xffffffffffffffff, 'number')
end
-- See comment above about lossy conversions.
--checkv(R.test_uint64(0xffffffffffffffff), 0xffffffffffffffff, 'number')
--check(not pcall(R.test_uint64, 0x10000000000000000))
end

Expand All @@ -212,7 +221,6 @@ function gireg.type_ushort()
if not nativeIntegers then
checkv(R.test_ushort(1.1), 1, 'number')
end
check(not pcall(R.test_ushort, -1))
end

function gireg.type_int()
Expand All @@ -233,7 +241,6 @@ function gireg.type_uint()
if not nativeIntegers then
checkv(R.test_uint(1.1), 1, 'number')
end
check(not pcall(R.test_uint, -1))
end

function gireg.type_ssize()
Expand All @@ -254,11 +261,9 @@ function gireg.type_size()
if not nativeIntegers then
checkv(R.test_size(1.1), 1, 'number')
end
check(not pcall(R.test_size, -1))
end

-- Helper, checks that given value has requested type and value, with some
-- tolerance because of low precision of gfloat type.
-- Helper, checks that given value has requested type and value, with some tolerance because of low precision of gfloat type.
local function checkvf(val, exp, tolerance)
check(type(val) == 'number',
string.format("got type `%s', expected `number'", type(val)), 2)
Expand Down