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.
This commit is contained in:
Daniel Hahler 2015-07-16 15:33:20 +02:00
parent 9cbab8fa08
commit 178f479d15
2 changed files with 33 additions and 8 deletions

25
luaa.h
View File

@ -141,6 +141,31 @@ luaA_getopt_number(lua_State *L, int idx, const char *name, lua_Number def)
return 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. /** Push a area type to a table on stack.
* \param L The Lua VM state. * \param L The Lua VM state.
* \param geometry The area geometry to push. * \param geometry The area geometry to push.

View File

@ -380,10 +380,10 @@ luaA_drawin_geometry(lua_State *L)
area_t wingeom; area_t wingeom;
luaA_checktable(L, 2); luaA_checktable(L, 2);
wingeom.x = luaA_getopt_number(L, 2, "x", drawin->geometry.x); wingeom.x = luaA_getopt_integer(L, 2, "x", drawin->geometry.x);
wingeom.y = luaA_getopt_number(L, 2, "y", drawin->geometry.y); wingeom.y = luaA_getopt_integer(L, 2, "y", drawin->geometry.y);
wingeom.width = luaA_getopt_number(L, 2, "width", drawin->geometry.width); wingeom.width = luaA_getopt_integer(L, 2, "width", drawin->geometry.width);
wingeom.height = luaA_getopt_number(L, 2, "height", drawin->geometry.height); wingeom.height = luaA_getopt_integer(L, 2, "height", drawin->geometry.height);
if(wingeom.width > 0 && wingeom.height > 0) if(wingeom.width > 0 && wingeom.height > 0)
drawin_moveresize(L, 1, wingeom); drawin_moveresize(L, 1, wingeom);
@ -400,7 +400,7 @@ LUA_OBJECT_EXPORT_PROPERTY(drawin, drawin_t, visible, lua_pushboolean)
static int static int
luaA_drawin_set_x(lua_State *L, drawin_t *drawin) 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, .y = drawin->geometry.y,
.width = drawin->geometry.width, .width = drawin->geometry.width,
.height = drawin->geometry.height }); .height = drawin->geometry.height });
@ -418,7 +418,7 @@ static int
luaA_drawin_set_y(lua_State *L, drawin_t *drawin) luaA_drawin_set_y(lua_State *L, drawin_t *drawin)
{ {
drawin_moveresize(L, -3, (area_t) { .x = drawin->geometry.x, drawin_moveresize(L, -3, (area_t) { .x = drawin->geometry.x,
.y = luaL_checknumber(L, -1), .y = luaA_checkinteger(L, -1),
.width = drawin->geometry.width, .width = drawin->geometry.width,
.height = drawin->geometry.height }); .height = drawin->geometry.height });
return 0; return 0;
@ -434,7 +434,7 @@ luaA_drawin_get_y(lua_State *L, drawin_t *drawin)
static int static int
luaA_drawin_set_width(lua_State *L, drawin_t *drawin) 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) if(width <= 0)
luaL_error(L, "invalid width"); luaL_error(L, "invalid width");
drawin_moveresize(L, -3, (area_t) { .x = drawin->geometry.x, 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 static int
luaA_drawin_set_height(lua_State *L, drawin_t *drawin) 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) if(height <= 0)
luaL_error(L, "invalid height"); luaL_error(L, "invalid height");
drawin_moveresize(L, -3, (area_t) { .x = drawin->geometry.x, drawin_moveresize(L, -3, (area_t) { .x = drawin->geometry.x,