From 178f479d15640ed904f43267c24420223058902d Mon Sep 17 00:00:00 2001 From: Daniel Hahler Date: Thu, 16 Jul 2015 15:33:20 +0200 Subject: [PATCH] drawin.c: check for integer for x, y, width, height This adds luaA_getopt_integer, luaA_optinteger and luaA_checkinteger. Lua 5.2 does not have support for this, but it would be available in Lua 5.3. Closes https://github.com/awesomeWM/awesome/pull/320. --- luaa.h | 25 +++++++++++++++++++++++++ objects/drawin.c | 16 ++++++++-------- 2 files changed, 33 insertions(+), 8 deletions(-) diff --git a/luaa.h b/luaa.h index ead2514a..d098761f 100644 --- a/luaa.h +++ b/luaa.h @@ -141,6 +141,31 @@ luaA_getopt_number(lua_State *L, int idx, const char *name, lua_Number def) return def; } +static inline int +luaA_checkinteger(lua_State *L, int n) +{ + double d = lua_tonumber(L, n); + if (d != (int)d) + luaA_typerror(L, n, "integer"); + return d; +} + +static inline lua_Integer +luaA_optinteger (lua_State *L, int narg, lua_Integer def) +{ + return luaL_opt(L, luaA_checkinteger, narg, def); +} + +static inline int +luaA_getopt_integer(lua_State *L, int idx, const char *name, lua_Integer def) +{ + lua_getfield(L, idx, name); + if (lua_isnil(L, -1) || lua_isnumber(L, -1)) + def = luaA_optinteger(L, -1, def); + lua_pop(L, 1); + return def; +} + /** Push a area type to a table on stack. * \param L The Lua VM state. * \param geometry The area geometry to push. diff --git a/objects/drawin.c b/objects/drawin.c index 62c20029..b24240ed 100644 --- a/objects/drawin.c +++ b/objects/drawin.c @@ -380,10 +380,10 @@ luaA_drawin_geometry(lua_State *L) area_t wingeom; luaA_checktable(L, 2); - wingeom.x = luaA_getopt_number(L, 2, "x", drawin->geometry.x); - wingeom.y = luaA_getopt_number(L, 2, "y", drawin->geometry.y); - wingeom.width = luaA_getopt_number(L, 2, "width", drawin->geometry.width); - wingeom.height = luaA_getopt_number(L, 2, "height", drawin->geometry.height); + wingeom.x = luaA_getopt_integer(L, 2, "x", drawin->geometry.x); + wingeom.y = luaA_getopt_integer(L, 2, "y", drawin->geometry.y); + wingeom.width = luaA_getopt_integer(L, 2, "width", drawin->geometry.width); + wingeom.height = luaA_getopt_integer(L, 2, "height", drawin->geometry.height); if(wingeom.width > 0 && wingeom.height > 0) drawin_moveresize(L, 1, wingeom); @@ -400,7 +400,7 @@ LUA_OBJECT_EXPORT_PROPERTY(drawin, drawin_t, visible, lua_pushboolean) static int luaA_drawin_set_x(lua_State *L, drawin_t *drawin) { - drawin_moveresize(L, -3, (area_t) { .x = luaL_checknumber(L, -1), + drawin_moveresize(L, -3, (area_t) { .x = luaA_checkinteger(L, -1), .y = drawin->geometry.y, .width = drawin->geometry.width, .height = drawin->geometry.height }); @@ -418,7 +418,7 @@ static int luaA_drawin_set_y(lua_State *L, drawin_t *drawin) { drawin_moveresize(L, -3, (area_t) { .x = drawin->geometry.x, - .y = luaL_checknumber(L, -1), + .y = luaA_checkinteger(L, -1), .width = drawin->geometry.width, .height = drawin->geometry.height }); return 0; @@ -434,7 +434,7 @@ luaA_drawin_get_y(lua_State *L, drawin_t *drawin) static int luaA_drawin_set_width(lua_State *L, drawin_t *drawin) { - int width = luaL_checknumber(L, -1); + int width = luaA_checkinteger(L, -1); if(width <= 0) luaL_error(L, "invalid width"); drawin_moveresize(L, -3, (area_t) { .x = drawin->geometry.x, @@ -454,7 +454,7 @@ luaA_drawin_get_width(lua_State *L, drawin_t *drawin) static int luaA_drawin_set_height(lua_State *L, drawin_t *drawin) { - int height = luaL_checknumber(L, -1); + int height = luaA_checkinteger(L, -1); if(height <= 0) luaL_error(L, "invalid height"); drawin_moveresize(L, -3, (area_t) { .x = drawin->geometry.x,