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"
28 #include "common/luaclass.h"
30 /** Lua function to call on dofuction() error */
31 lua_CFunction lualib_dofunction_on_error
;
33 void luaA_checkfunction(lua_State
*, int);
34 void luaA_checktable(lua_State
*, int);
36 /** Dump the Lua stack. Useful for debugging.
37 * \param L The Lua VM state.
39 void luaA_dumpstack(lua_State
*);
41 /** Convert s stack index to positive.
42 * \param L The Lua VM state.
43 * \param ud The index.
44 * \return A positive index.
47 luaA_absindex(lua_State
*L
, int ud
)
49 return (ud
> 0 || ud
<= LUA_REGISTRYINDEX
) ? ud
: lua_gettop(L
) + ud
+ 1;
53 luaA_dofunction_error(lua_State
*L
)
55 if(lualib_dofunction_on_error
)
56 return lualib_dofunction_on_error(L
);
60 /** Execute an Lua function on top of the stack.
61 * \param L The Lua stack.
62 * \param nargs The number of arguments for the Lua function.
63 * \param nret The number of returned value from the Lua function.
64 * \return True on no error, false otherwise.
67 luaA_dofunction(lua_State
*L
, int nargs
, int nret
)
69 /* Move function before arguments */
70 lua_insert(L
, - nargs
- 1);
71 /* Push error handling function */
72 lua_pushcfunction(L
, luaA_dofunction_error
);
73 /* Move error handling function before args and function */
74 lua_insert(L
, - nargs
- 2);
75 int error_func_pos
= lua_gettop(L
) - nargs
- 1;
76 if(lua_pcall(L
, nargs
, nret
, - nargs
- 2))
78 warn("%s", lua_tostring(L
, -1));
79 /* Remove error function and error string */
83 /* Remove error function */
84 lua_remove(L
, error_func_pos
);
90 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:textwidth=80