Skip to content

Commit

Permalink
feat: throw error in Lua 5.1/5.2 when passing a decimal number to an …
Browse files Browse the repository at this point in the history
…integer argument

Thanks to @jakob-kruse for helping me brainstorm this!
  • Loading branch information
jonasgeiler committed Apr 23, 2024
1 parent d4b4f5b commit 356942a
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 56 deletions.
76 changes: 20 additions & 56 deletions spec/fenster_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,14 @@ describe('fenster', function()
assert.has_errors(function() fenster.open({}) end)
assert.has_errors(function() fenster.open(function() end) end)
assert.has_errors(function() fenster.open(io.stdout) end)
assert.has_errors(function() fenster.open(100.5) end)

assert.has_errors(function() fenster.open(256, 'ERROR') end)
assert.has_errors(function() fenster.open(256, true) end)
assert.has_errors(function() fenster.open(256, {}) end)
assert.has_errors(function() fenster.open(256, function() end) end)
assert.has_errors(function() fenster.open(256, io.stdout) end)

if _VERSION ~= 'Lua 5.1' and _VERSION ~= 'Lua 5.2' then
-- Lua 5.1 and 5.2 don't throw errors when using a number as an integer
assert.has_errors(function() fenster.open(100.5) end)
assert.has_errors(function() fenster.open(256, 100.5) end)
end
assert.has_errors(function() fenster.open(256, 100.5) end)
end)

it('should throw when width/height are out of range', function()
Expand Down Expand Up @@ -86,11 +82,7 @@ describe('fenster', function()
assert.has_errors(function() fenster.open(256, 144, 'Test', {}) end)
assert.has_errors(function() fenster.open(256, 144, 'Test', function() end) end)
assert.has_errors(function() fenster.open(256, 144, 'Test', io.stdout) end)

if _VERSION ~= 'Lua 5.1' and _VERSION ~= 'Lua 5.2' then
-- Lua 5.1 and 5.2 don't throw errors when using a number as an integer
assert.has_errors(function() fenster.open(256, 144, 'Test', 2.5) end)
end
assert.has_errors(function() fenster.open(256, 144, 'Test', 2.5) end)
end)

it('should throw when scale is not a power of 2', function()
Expand Down Expand Up @@ -145,11 +137,7 @@ describe('fenster', function()
assert.has_errors(function() fenster.sleep({}) end)
assert.has_errors(function() fenster.sleep(function() end) end)
assert.has_errors(function() fenster.sleep(io.stdout) end)

if _VERSION ~= 'Lua 5.1' and _VERSION ~= 'Lua 5.2' then
-- Lua 5.1 and 5.2 don't throw errors when using a number as an integer
assert.has_errors(function() fenster.sleep(2.5) end)
end
assert.has_errors(function() fenster.sleep(2.5) end)
end)

it('should sleep for the given amount of time', function()
Expand Down Expand Up @@ -178,11 +166,7 @@ describe('fenster', function()
assert.has_errors(function() fenster.rgb({}) end)
assert.has_errors(function() fenster.rgb(function() end) end)
assert.has_errors(function() fenster.rgb(io.stdout) end)

if _VERSION ~= 'Lua 5.1' and _VERSION ~= 'Lua 5.2' then
-- Lua 5.1 and 5.2 don't throw errors when using a number as an integer
assert.has_errors(function() fenster.rgb(2.5) end)
end
assert.has_errors(function() fenster.rgb(2.5) end)
end)

it('should throw when color is out of range', function()
Expand All @@ -208,25 +192,21 @@ describe('fenster', function()
assert.has_errors(function() fenster.rgb({}, 0, 0) end)
assert.has_errors(function() fenster.rgb(function() end, 0, 0) end)
assert.has_errors(function() fenster.rgb(io.stdout) end)
assert.has_errors(function() fenster.rgb(255.5, 0, 0) end)

assert.has_errors(function() fenster.rgb(0, 'ERROR', 0) end)
assert.has_errors(function() fenster.rgb(0, true, 0) end)
assert.has_errors(function() fenster.rgb(0, {}, 0) end)
assert.has_errors(function() fenster.rgb(0, function() end, 0) end)
assert.has_errors(function() fenster.rgb(0, io.stdout, 0) end)
assert.has_errors(function() fenster.rgb(0, 255.5, 0) end)

assert.has_errors(function() fenster.rgb(0, 0, 'ERROR') end)
assert.has_errors(function() fenster.rgb(0, 0, true) end)
assert.has_errors(function() fenster.rgb(0, 0, {}) end)
assert.has_errors(function() fenster.rgb(0, 0, function() end) end)
assert.has_errors(function() fenster.rgb(0, 0, io.stdout) end)

if _VERSION ~= 'Lua 5.1' and _VERSION ~= 'Lua 5.2' then
-- Lua 5.1 and 5.2 don't throw errors when using a number as an integer
assert.has_errors(function() fenster.rgb(255.5, 0, 0) end)
assert.has_errors(function() fenster.rgb(0, 255.5, 0) end)
assert.has_errors(function() fenster.rgb(0, 0, 255.5) end)
end
assert.has_errors(function() fenster.rgb(0, 0, 255.5) end)
end)

it('should throw when r/g/b are out of range', function()
Expand Down Expand Up @@ -338,30 +318,26 @@ describe('fenster', function()
assert.has_errors(function() fenster.set(window, {}, 0, 0) end)
assert.has_errors(function() fenster.set(window, function() end, 0, 0) end)
assert.has_errors(function() fenster.set(window, io.stdout, 0, 0) end)
assert.has_errors(function() fenster.set(window, 2.5, 0, 0) end)
assert.has_errors(function() fenster.set(window, 0, 'ERROR', 0) end)
assert.has_errors(function() fenster.set(window, 0, true, 0) end)
assert.has_errors(function() fenster.set(window, 0, {}, 0) end)
assert.has_errors(function() fenster.set(window, 0, function() end, 0) end)
assert.has_errors(function() fenster.set(window, 0, io.stdout, 0) end)
assert.has_errors(function() fenster.set(window, 0, 2.5, 0) end)

assert.has_errors(function() window:set('ERROR', 0, 0) end)
assert.has_errors(function() window:set(true, 0, 0) end)
assert.has_errors(function() window:set({}, 0, 0) end)
assert.has_errors(function() window:set(function() end, 0, 0) end)
assert.has_errors(function() window:set(io.stdout, 0, 0) end)
assert.has_errors(function() window:set(2.5, 0, 0) end)
assert.has_errors(function() window:set(0, 'ERROR', 0) end)
assert.has_errors(function() window:set(0, true, 0) end)
assert.has_errors(function() window:set(0, {}, 0) end)
assert.has_errors(function() window:set(0, function() end, 0) end)
assert.has_errors(function() window:set(0, io.stdout, 0) end)

if _VERSION ~= 'Lua 5.1' and _VERSION ~= 'Lua 5.2' then
-- Lua 5.1 and 5.2 don't throw errors when using a number as an integer
assert.has_errors(function() fenster.set(window, 2.5, 0, 0) end)
assert.has_errors(function() fenster.set(window, 0, 2.5, 0) end)
assert.has_errors(function() window:set(2.5, 0, 0) end)
assert.has_errors(function() window:set(0, 2.5, 0) end)
end
assert.has_errors(function() window:set(0, 2.5, 0) end)
end)

it('should throw when color is not an integer #needsdisplay', function()
Expand All @@ -373,18 +349,14 @@ describe('fenster', function()
assert.has_errors(function() fenster.set(window, 0, 0, {}) end)
assert.has_errors(function() fenster.set(window, 0, 0, function() end) end)
assert.has_errors(function() fenster.set(window, 0, 0, io.stdout) end)
assert.has_errors(function() fenster.set(window, 0, 0, 2.5) end)

assert.has_errors(function() window:set(0, 0, 'ERROR') end)
assert.has_errors(function() window:set(0, 0, true) end)
assert.has_errors(function() window:set(0, 0, {}) end)
assert.has_errors(function() window:set(0, 0, function() end) end)
assert.has_errors(function() window:set(0, 0, io.stdout) end)

if _VERSION ~= 'Lua 5.1' and _VERSION ~= 'Lua 5.2' then
-- Lua 5.1 and 5.2 don't throw errors when using a number as an integer
assert.has_errors(function() fenster.set(window, 0, 0, 2.5) end)
assert.has_errors(function() window:set(0, 0, 2.5) end)
end
assert.has_errors(function() window:set(0, 0, 2.5) end)
end)

it('should throw when x/y are out of range #needsdisplay', function()
Expand Down Expand Up @@ -449,30 +421,26 @@ describe('fenster', function()
assert.has_errors(function() fenster.get(window, {}, 0) end)
assert.has_errors(function() fenster.get(window, function() end, 0) end)
assert.has_errors(function() fenster.get(window, io.stdout, 0) end)
assert.has_errors(function() fenster.get(window, 2.5, 0) end)
assert.has_errors(function() fenster.get(window, 0, 'ERROR') end)
assert.has_errors(function() fenster.get(window, 0, true) end)
assert.has_errors(function() fenster.get(window, 0, {}) end)
assert.has_errors(function() fenster.get(window, 0, function() end) end)
assert.has_errors(function() fenster.get(window, 0, io.stdout) end)
assert.has_errors(function() fenster.get(window, 0, 2.5) end)

assert.has_errors(function() window:get('ERROR', 0) end)
assert.has_errors(function() window:get(true, 0) end)
assert.has_errors(function() window:get({}, 0) end)
assert.has_errors(function() window:get(function() end, 0) end)
assert.has_errors(function() window:get(io.stdout, 0) end)
assert.has_errors(function() window:get(2.5, 0) end)
assert.has_errors(function() window:get(0, 'ERROR') end)
assert.has_errors(function() window:get(0, true) end)
assert.has_errors(function() window:get(0, {}) end)
assert.has_errors(function() window:get(0, function() end) end)
assert.has_errors(function() window:get(0, io.stdout) end)

if _VERSION ~= 'Lua 5.1' and _VERSION ~= 'Lua 5.2' then
-- Lua 5.1 and 5.2 don't throw errors when using a number as an integer
assert.has_errors(function() fenster.get(window, 2.5, 0) end)
assert.has_errors(function() fenster.get(window, 0, 2.5) end)
assert.has_errors(function() window:get(2.5, 0) end)
assert.has_errors(function() window:get(0, 2.5) end)
end
assert.has_errors(function() window:get(0, 2.5) end)
end)

it('should throw when x/y are out of range #needsdisplay', function()
Expand Down Expand Up @@ -543,18 +511,14 @@ describe('fenster', function()
assert.has_errors(function() fenster.clear(window, {}) end)
assert.has_errors(function() fenster.clear(window, function() end) end)
assert.has_errors(function() fenster.clear(window, io.stdout) end)
assert.has_errors(function() fenster.clear(window, 2.5) end)

assert.has_errors(function() window:clear('ERROR') end)
assert.has_errors(function() window:clear(true) end)
assert.has_errors(function() window:clear({}) end)
assert.has_errors(function() window:clear(function() end) end)
assert.has_errors(function() window:clear(io.stdout) end)

if _VERSION ~= 'Lua 5.1' and _VERSION ~= 'Lua 5.2' then
-- Lua 5.1 and 5.2 don't throw errors when using a number as an integer
assert.has_errors(function() fenster.clear(window, 2.5) end)
assert.has_errors(function() window:clear(2.5) end)
end
assert.has_errors(function() window:clear(2.5) end)
end)

it('should throw when color is out of range #needsdisplay', function()
Expand Down
14 changes: 14 additions & 0 deletions src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,20 @@
#include "../lib/compat-5.3/compat-5.3.h"
#include "../lib/fenster/fenster.h"

// Macros that ensure the same integer argument behavior in Lua 5.1/5.2
// and 5.3/5.4. In Lua 5.1/5.2 luaL_checkinteger/luaL_optinteger normally floor
// decimal numbers, while in Lua 5.3/5.4 they throw an error. These macros make
// sure to always throw an error if the number has a decimal part.
#if defined(LUA_VERSION_NUM) && LUA_VERSION_NUM <= 502
#define luaL_checkinteger(L, arg) \
(luaL_argcheck(L, \
floorl(luaL_checknumber(L, arg)) == luaL_checknumber(L, arg), \
arg, "number has no integer representation"), \
luaL_checkinteger(L, arg))
#define luaL_optinteger(L, arg, def) \
(lua_isnoneornil(L, arg) ? def : luaL_checkinteger(L, arg))
#endif

/** Default window title */
static const char *DEFAULT_TITLE = "fenster";

Expand Down

0 comments on commit 356942a

Please sign in to comment.