diff --git a/CMakeLists.txt b/CMakeLists.txt index 62cb14f6..e9f8aa25 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -61,6 +61,7 @@ set(AWE_SRCS ${SOURCE_DIR}/common/backtrace.c ${SOURCE_DIR}/common/buffer.c ${SOURCE_DIR}/common/luaclass.c + ${SOURCE_DIR}/common/lualib.c ${SOURCE_DIR}/common/luaobject.c ${SOURCE_DIR}/common/util.c ${SOURCE_DIR}/common/version.c diff --git a/common/luaclass.c b/common/luaclass.c index fbc7b04f..3efaea65 100644 --- a/common/luaclass.c +++ b/common/luaclass.c @@ -21,6 +21,7 @@ #include "common/luaclass.h" #include "common/luaobject.h" +#include "luaa.h" struct lua_class_property { @@ -76,7 +77,7 @@ luaA_checkudata(lua_State *L, int ud, lua_class_t *class) { void *p = luaA_toudata(L, ud, class); if(!p) - luaL_typerror(L, ud, class->name); + luaA_typerror(L, ud, class->name); else if(class->checker && !class->checker(p)) luaL_error(L, "invalid object"); return p; diff --git a/common/lualib.c b/common/lualib.c new file mode 100644 index 00000000..ab0b3bad --- /dev/null +++ b/common/lualib.c @@ -0,0 +1,62 @@ +/* + * lualib.h - useful functions and type for Lua + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + * + */ + +#include "luaa.h" + +void luaA_checkfunction(lua_State *L, int idx) +{ + if(!lua_isfunction(L, idx)) + luaA_typerror(L, idx, "function"); +} + +void luaA_checktable(lua_State *L, int idx) +{ + if(!lua_istable(L, idx)) + luaA_typerror(L, idx, "table"); +} + +void luaA_dumpstack(lua_State *L) +{ + fprintf(stderr, "-------- Lua stack dump ---------\n"); + for(int i = lua_gettop(L); i; i--) + { + int t = lua_type(L, i); + switch (t) + { + case LUA_TSTRING: + fprintf(stderr, "%d: string: `%s'\n", i, lua_tostring(L, i)); + break; + case LUA_TBOOLEAN: + fprintf(stderr, "%d: bool: %s\n", i, lua_toboolean(L, i) ? "true" : "false"); + break; + case LUA_TNUMBER: + fprintf(stderr, "%d: number: %g\n", i, lua_tonumber(L, i)); + break; + case LUA_TNIL: + fprintf(stderr, "%d: nil\n", i); + break; + default: + fprintf(stderr, "%d: %s\t#%d\t%p\n", i, lua_typename(L, t), + (int) lua_objlen(L, i), + lua_topointer(L, i)); + break; + } + } + fprintf(stderr, "------- Lua stack dump end ------\n"); +} diff --git a/common/lualib.h b/common/lualib.h index 6173f8df..59d6a16d 100644 --- a/common/lualib.h +++ b/common/lualib.h @@ -23,56 +23,20 @@ #define AWESOME_COMMON_LUALIB #include + #include "common/util.h" +#include "common/luaclass.h" /** Lua function to call on dofuction() error */ lua_CFunction lualib_dofunction_on_error; -static inline void luaA_checkfunction(lua_State *L, int idx) -{ - if(!lua_isfunction(L, idx)) - luaL_typerror(L, idx, "function"); -} - -static inline void luaA_checktable(lua_State *L, int idx) -{ - if(!lua_istable(L, idx)) - luaL_typerror(L, idx, "table"); -} +void luaA_checkfunction(lua_State *, int); +void luaA_checktable(lua_State *, int); /** Dump the Lua stack. Useful for debugging. * \param L The Lua VM state. */ -static inline void -luaA_dumpstack(lua_State *L) -{ - fprintf(stderr, "-------- Lua stack dump ---------\n"); - for(int i = lua_gettop(L); i; i--) - { - int t = lua_type(L, i); - switch (t) - { - case LUA_TSTRING: - fprintf(stderr, "%d: string: `%s'\n", i, lua_tostring(L, i)); - break; - case LUA_TBOOLEAN: - fprintf(stderr, "%d: bool: %s\n", i, lua_toboolean(L, i) ? "true" : "false"); - break; - case LUA_TNUMBER: - fprintf(stderr, "%d: number: %g\n", i, lua_tonumber(L, i)); - break; - case LUA_TNIL: - fprintf(stderr, "%d: nil\n", i); - break; - default: - fprintf(stderr, "%d: %s\t#%d\t%p\n", i, lua_typename(L, t), - (int) lua_objlen(L, i), - lua_topointer(L, i)); - break; - } - } - fprintf(stderr, "------- Lua stack dump end ------\n"); -} +void luaA_dumpstack(lua_State *); /** Convert s stack index to positive. * \param L The Lua VM state. diff --git a/luaa.h b/luaa.h index 151b2eb7..cfdafd6e 100644 --- a/luaa.h +++ b/luaa.h @@ -186,6 +186,14 @@ luaA_warn(lua_State *L, const char *fmt, ...) fprintf(stderr, "\n"); } +static inline int +luaA_typerror(lua_State *L, int narg, const char *tname) +{ + const char *msg = lua_pushfstring(L, "%s expected, got %s", + tname, luaL_typename(L, narg)); + return luaL_argerror(L, narg, msg); +} + void luaA_init(xdgHandle *); bool luaA_parserc(xdgHandle *, const char *, bool); bool luaA_hasitem(lua_State *, const void *);