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
27 #include "common/util.h"
29 /** Lua function to call on dofuction() error */
30 lua_CFunction lualib_dofunction_on_error
;
32 void luaA_checkfunction(lua_State
*, int);
33 void luaA_checktable(lua_State
*, int);
35 /** Dump the Lua stack. Useful for debugging.
36 * \param L The Lua VM state.
38 void luaA_dumpstack(lua_State
*);
40 /** Convert s stack index to positive.
41 * \param L The Lua VM state.
42 * \param ud The index.
43 * \return A positive index.
46 luaA_absindex(lua_State
*L
, int ud
)
48 return (ud
> 0 || ud
<= LUA_REGISTRYINDEX
) ? ud
: lua_gettop(L
) + ud
+ 1;
52 luaA_dofunction_error(lua_State
*L
)
54 if(lualib_dofunction_on_error
)
55 return lualib_dofunction_on_error(L
);
59 /** Execute an Lua function on top of the stack.
60 * \param L The Lua stack.
61 * \param nargs The number of arguments for the Lua function.
62 * \param nret The number of returned value from the Lua function.
63 * \return True on no error, false otherwise.
66 luaA_dofunction(lua_State
*L
, int nargs
, int nret
)
68 /* Move function before arguments */
69 lua_insert(L
, - nargs
- 1);
70 /* Push error handling function */
71 lua_pushcfunction(L
, luaA_dofunction_error
);
72 /* Move error handling function before args and function */
73 lua_insert(L
, - nargs
- 2);
74 int error_func_pos
= lua_gettop(L
) - nargs
- 1;
75 if(lua_pcall(L
, nargs
, nret
, - nargs
- 2))
77 warn("%s", lua_tostring(L
, -1));
78 /* Remove error function and error string */
82 /* Remove error function */
83 lua_remove(L
, error_func_pos
);
89 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:textwidth=80