awful.remote: enhance description
[awesome.git] / common / lualib.h
blob291d4b1240adf32e343e14718c9545d0862ae099
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 /** Lua function to call on dofuction() error */
29 lua_CFunction lualib_dofunction_on_error;
31 #define luaA_checkfunction(L, n) \
32 do { \
33 if(!lua_isfunction(L, n)) \
34 luaL_typerror(L, n, "function"); \
35 } while(0)
37 /** Dump the Lua stack. Useful for debugging.
38 * \param L The Lua VM state.
40 static inline void
41 luaA_dumpstack(lua_State *L)
43 fprintf(stderr, "-------- Lua stack dump ---------\n");
44 for(int i = lua_gettop(L); i; i--)
46 int t = lua_type(L, i);
47 switch (t)
49 case LUA_TSTRING:
50 fprintf(stderr, "%d: string: `%s'\n", i, lua_tostring(L, i));
51 break;
52 case LUA_TBOOLEAN:
53 fprintf(stderr, "%d: bool: %s\n", i, lua_toboolean(L, i) ? "true" : "false");
54 break;
55 case LUA_TNUMBER:
56 fprintf(stderr, "%d: number: %g\n", i, lua_tonumber(L, i));
57 break;
58 case LUA_TNIL:
59 fprintf(stderr, "%d: nil\n", i);
60 break;
61 default:
62 fprintf(stderr, "%d: %s\t#%d\t%p\n", i, lua_typename(L, t),
63 (int) lua_objlen(L, i),
64 lua_topointer(L, i));
65 break;
68 fprintf(stderr, "------- Lua stack dump end ------\n");
71 /** Convert s stack index to positive.
72 * \param L The Lua VM state.
73 * \param ud The index.
74 * \return A positive index.
76 static inline int
77 luaA_absindex(lua_State *L, int ud)
79 return (ud > 0 || ud <= LUA_REGISTRYINDEX) ? ud : lua_gettop(L) + ud + 1;
82 static inline int
83 luaA_dofunction_error(lua_State *L)
85 if(lualib_dofunction_on_error)
86 return lualib_dofunction_on_error(L);
87 return 0;
90 /** Execute an Lua function on top of the stack.
91 * \param L The Lua stack.
92 * \param nargs The number of arguments for the Lua function.
93 * \param nret The number of returned value from the Lua function.
94 * \return True on no error, false otherwise.
96 static inline bool
97 luaA_dofunction(lua_State *L, int nargs, int nret)
99 /* Move function before arguments */
100 lua_insert(L, - nargs - 1);
101 /* Push error handling function */
102 lua_pushcfunction(L, luaA_dofunction_error);
103 /* Move error handling function before args and function */
104 lua_insert(L, - nargs - 2);
105 int error_func_pos = lua_gettop(L) - nargs - 1;
106 if(lua_pcall(L, nargs, nret, - nargs - 2))
108 warn("%s", lua_tostring(L, -1));
109 /* Remove error function and error string */
110 lua_pop(L, 2);
111 return false;
113 /* Remove error function */
114 lua_remove(L, error_func_pos);
115 return true;
118 #define luaA_checktable(L, n) \
119 do { \
120 if(!lua_istable(L, n)) \
121 luaL_typerror(L, n, "table"); \
122 } while(0)
124 #endif
126 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80