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;
80 luaA_dofunction_error(lua_State
*L
)
82 if(!luaL_dostring(L
, "return debug.traceback(\"error while running function\", 3)"))
84 /* Move traceback before error */
87 lua_pushliteral(L
, "\nerror: ");
88 /* Move it before error */
95 /** Execute an Lua function on top of the stack.
96 * \param L The Lua stack.
97 * \param nargs The number of arguments for the Lua function.
98 * \param nret The number of returned value from the Lua function.
99 * \return True on no error, false otherwise.
102 luaA_dofunction(lua_State
*L
, int nargs
, int nret
)
104 /* Move function before arguments */
105 lua_insert(L
, - nargs
- 1);
106 /* Push error handling function */
107 lua_pushcfunction(L
, luaA_dofunction_error
);
108 /* Move error handling function before args and function */
109 lua_insert(L
, - nargs
- 2);
110 int error_func_pos
= lua_gettop(L
) - nargs
- 1;
111 if(lua_pcall(L
, nargs
, nret
, - nargs
- 2))
113 warn("%s", lua_tostring(L
, -1));
114 /* Remove error function and error string */
118 /* Remove error function */
119 lua_remove(L
, error_func_pos
);
123 #define luaA_checktable(L, n) \
125 if(!lua_istable(L, n)) \
126 luaL_typerror(L, n, "table"); \
131 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80