forked from awesomeWM/awesome
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlualib.h
More file actions
126 lines (115 loc) · 3.74 KB
/
Copy pathlualib.h
File metadata and controls
126 lines (115 loc) · 3.74 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
/*
* lualib.h - useful functions and type for Lua
*
* Copyright © 2009 Julien Danjou <julien@danjou.info>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
#ifndef AWESOME_COMMON_LUALIB
#define AWESOME_COMMON_LUALIB
#include <lauxlib.h>
#include "common/util.h"
/** Lua function to call on dofuction() error */
lua_CFunction lualib_dofunction_on_error;
#define luaA_checkfunction(L, n) \
do { \
if(!lua_isfunction(L, n)) \
luaL_typerror(L, n, "function"); \
} while(0)
/** Dump the Lua stack. Useful for debugging.
* \param L The Lua VM state.
*/
static inline void
luaA_dumpstack(lua_State *L)
{
fprintf(stderr, "-------- Lua stack dump ---------\n");
for(int i = lua_gettop(L); i; i--)
{
int t = lua_type(L, i);
switch (t)
{
case LUA_TSTRING:
fprintf(stderr, "%d: string: `%s'\n", i, lua_tostring(L, i));
break;
case LUA_TBOOLEAN:
fprintf(stderr, "%d: bool: %s\n", i, lua_toboolean(L, i) ? "true" : "false");
break;
case LUA_TNUMBER:
fprintf(stderr, "%d: number: %g\n", i, lua_tonumber(L, i));
break;
case LUA_TNIL:
fprintf(stderr, "%d: nil\n", i);
break;
default:
fprintf(stderr, "%d: %s\t#%d\t%p\n", i, lua_typename(L, t),
(int) lua_objlen(L, i),
lua_topointer(L, i));
break;
}
}
fprintf(stderr, "------- Lua stack dump end ------\n");
}
/** Convert s stack index to positive.
* \param L The Lua VM state.
* \param ud The index.
* \return A positive index.
*/
static inline int
luaA_absindex(lua_State *L, int ud)
{
return (ud > 0 || ud <= LUA_REGISTRYINDEX) ? ud : lua_gettop(L) + ud + 1;
}
static inline int
luaA_dofunction_error(lua_State *L)
{
if(lualib_dofunction_on_error)
return lualib_dofunction_on_error(L);
return 0;
}
/** Execute an Lua function.
* \param L The Lua stack.
* \param nargs The number of arguments for the Lua function.
* \param nret The number of returned value from the Lua function.
* \return The number of argument returned, or -1 on error.
*/
static inline int __attribute__ ((warn_unused_result))
luaA_dofunction(lua_State *L, int nargs, int nret)
{
/* Push error handling function */
lua_pushcfunction(L, luaA_dofunction_error);
/* Move error handling function before args and function */
lua_insert(L, - nargs - 2);
int error_func_pos = lua_gettop(L) - nargs - 1;
if(lua_pcall(L, nargs, nret, - nargs - 2))
{
warn("%s", lua_tostring(L, -1));
/* Remove error function and error string */
lua_pop(L, 2);
return -1;
}
/* Remove error function */
lua_remove(L, error_func_pos);
if(nret == LUA_MULTRET)
/* error_func_pos is (size of the initial stack + 1) */
nret = lua_gettop(L) - (error_func_pos - 1);
return nret;
}
#define luaA_checktable(L, n) \
do { \
if(!lua_istable(L, n)) \
luaL_typerror(L, n, "table"); \
} while(0)
#endif
// vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80