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
26 #include "common/util.h"
28 #define luaA_checkfunction(L, n) \
30 if(!lua_isfunction(L, n)) \
31 luaL_typerror(L, n, "function"); \
34 /** Dump the Lua stack. Useful for debugging.
35 * \param L The Lua VM state.
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
);
47 fprintf(stderr
, "%d: string: `%s'\n", i
, lua_tostring(L
, i
));
50 fprintf(stderr
, "%d: bool: %s\n", i
, lua_toboolean(L
, i
) ? "true" : "false");
53 fprintf(stderr
, "%d: number: %g\n", i
, lua_tonumber(L
, i
));
56 fprintf(stderr
, "%d: nil\n", i
);
59 fprintf(stderr
, "%d: %s\t#%d\t%p\n", i
, lua_typename(L
, t
),
60 (int) lua_objlen(L
, i
),
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.
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.
86 luaA_dofunction(lua_State
*L
, int nargs
, int nret
)
89 lua_insert(L
, - (nargs
+ 1));
90 if(lua_pcall(L
, nargs
, nret
, 0))
92 warn("error running function: %s",
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.
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? */
116 lua_pop(L
, 2); /* remove both metatables */
123 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80