lualib: import stack dumping function
[awesome.git] / common / lualib.h
blobc9288d5a89105c82482ae2834573b2fc1fc9ecc6
1 /*
2 * lualib.h - useful functions and type for Lua
4 * Copyright © 2009 Julien Danjou <julien@danjou.info>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License along
17 * with this program; if not, write to the Free Software Foundation, Inc.,
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22 #ifndef AWESOME_COMMON_LUALIB
23 #define AWESOME_COMMON_LUALIB
25 #include <lauxlib.h>
26 #include "common/util.h"
28 #define luaA_checkfunction(L, n) \
29 do { \
30 if(!lua_isfunction(L, n)) \
31 luaL_typerror(L, n, "function"); \
32 } while(0)
34 /** Dump the Lua stack. Useful for debugging.
35 * \param L The Lua VM state.
37 static inline void
38 luaA_dumpstack(lua_State *L)
40 fprintf(stderr, "-------- Lua stack dump ---------\n");
41 for(int i = lua_gettop(L); i; i--)
43 int t = lua_type(L, i);
44 switch (t)
46 case LUA_TSTRING:
47 fprintf(stderr, "%d: string: `%s'\n", i, lua_tostring(L, i));
48 break;
49 case LUA_TBOOLEAN:
50 fprintf(stderr, "%d: bool: %s\n", i, lua_toboolean(L, i) ? "true" : "false");
51 break;
52 case LUA_TNUMBER:
53 fprintf(stderr, "%d: number: %g\n", i, lua_tonumber(L, i));
54 break;
55 case LUA_TNIL:
56 fprintf(stderr, "%d: nil\n", i);
57 break;
58 default:
59 fprintf(stderr, "%d: %s\t#%d\t%p\n", i, lua_typename(L, t),
60 (int) lua_objlen(L, i),
61 lua_topointer(L, i));
62 break;
65 fprintf(stderr, "------- Lua stack dump end ------\n");
68 /** Convert s stack index to positive.
69 * \param L The Lua VM state.
70 * \param ud The index.
71 * \return A positive index.
73 static inline int
74 luaA_absindex(lua_State *L, int ud)
76 return (ud > 0 || ud <= LUA_REGISTRYINDEX) ? ud : lua_gettop(L) + ud + 1;
79 /** Execute an Lua function on top of the stack.
80 * \param L The Lua stack.
81 * \param nargs The number of arguments for the Lua function.
82 * \param nret The number of returned value from the Lua function.
83 * \return True on no error, false otherwise.
85 static inline bool
86 luaA_dofunction(lua_State *L, int nargs, int nret)
88 if(nargs)
89 lua_insert(L, - (nargs + 1));
90 if(lua_pcall(L, nargs, nret, 0))
92 warn("error running function: %s",
93 lua_tostring(L, -1));
94 lua_pop(L, 1);
95 return false;
97 return true;
100 /** Convert a object to a udata if possible.
101 * \param L The Lua VM state.
102 * \param ud The index.
103 * \param tname The type name.
104 * \return A pointer to the object, NULL otherwise.
106 static inline void *
107 luaA_toudata(lua_State *L, int ud, const char *tname)
109 void *p = lua_touserdata(L, ud);
110 if(p) /* value is a userdata? */
111 if(lua_getmetatable(L, ud)) /* does it have a metatable? */
113 lua_getfield(L, LUA_REGISTRYINDEX, tname); /* get correct metatable */
114 if(!lua_rawequal(L, -1, -2)) /* does it have the correct mt? */
115 p = NULL;
116 lua_pop(L, 2); /* remove both metatables */
118 return p;
121 #endif
123 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80