2 * luah.h - Lua helper functions
4 * Copyright (C) 2010 Mason Larobina <mason.larobina@gmail.com>
5 * Copyright (C) 2008-2009 Julien Danjou <julien@danjou.info>
7 * This program is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program. If not, see <http://www.gnu.org/licenses/>.
26 #include "globalconf.h"
27 #include "common/luaobject.h"
28 #include "common/lualib.h"
30 #define luaH_deprecate(L, repl) \
32 luaH_warn(L, "%s: This function is deprecated and will be removed, see %s", \
33 __FUNCTION__, repl); \
34 lua_pushlstring(L, __FUNCTION__, sizeof(__FUNCTION__)); \
35 signal_object_emit(L, &globalconf.signals, "debug::deprecation", 1); \
38 static inline gboolean
39 luaH_checkboolean(lua_State
*L
, gint n
) {
40 if(!lua_isboolean(L
, n
))
41 luaL_typerror(L
, n
, "boolean");
42 return lua_toboolean(L
, n
);
45 static inline gboolean
46 luaH_optboolean(lua_State
*L
, gint idx
, gboolean def
) {
47 return luaL_opt(L
, luaH_checkboolean
, idx
, def
);
50 static inline lua_Number
51 luaH_getopt_number(lua_State
*L
, gint idx
, const gchar
*name
, lua_Number def
) {
52 lua_getfield(L
, idx
, name
);
53 if (lua_isnil(L
, -1) || lua_isnumber(L
, -1))
54 def
= luaL_optnumber(L
, -1, def
);
59 static inline const gchar
*
60 luaH_getopt_lstring(lua_State
*L
, gint idx
, const gchar
*name
, const gchar
*def
, size_t *len
) {
61 lua_getfield(L
, idx
, name
);
62 const gchar
*s
= luaL_optlstring(L
, -1, def
, len
);
67 static inline gboolean
68 luaH_getopt_boolean(lua_State
*L
, gint idx
, const gchar
*name
, gboolean def
) {
69 lua_getfield(L
, idx
, name
);
70 gboolean b
= luaH_optboolean(L
, -1, def
);
75 /* Register an Lua object.
76 * \param L The Lua stack.
77 * \param idx Index of the object in the stack.
78 * \param ref A gint address: it will be filled with the gint
79 * registered. If the address points to an already registered object, it will
84 luaH_register(lua_State
*L
, gint idx
, gint
*ref
) {
85 lua_pushvalue(L
, idx
);
86 if(*ref
!= LUA_REFNIL
)
87 luaL_unref(L
, LUA_REGISTRYINDEX
, *ref
);
88 *ref
= luaL_ref(L
, LUA_REGISTRYINDEX
);
92 /* Unregister a Lua object.
93 * \param L The Lua stack.
94 * \param ref A reference to an Lua object.
97 luaH_unregister(lua_State
*L
, gint
*ref
) {
98 luaL_unref(L
, LUA_REGISTRYINDEX
, *ref
);
102 /* Register a function.
103 * \param L The Lua stack.
104 * \param idx Index of the function in the stack.
105 * \param fct A gint address: it will be filled with the gint
106 * registered. If the address points to an already registered function, it will
108 * \return luaH_register value.
111 luaH_registerfct(lua_State
*L
, gint idx
, gint
*fct
) {
112 luaH_checkfunction(L
, idx
);
113 return luaH_register(L
, idx
, fct
);
116 /* Grab a function from the registry and execute it.
117 * \param L The Lua stack.
118 * \param ref The function reference.
119 * \param nargs The number of arguments for the Lua function.
120 * \param nret The number of returned value from the Lua function.
121 * \return True on no error, false otherwise.
123 static inline gboolean
124 luaH_dofunction_from_registry(lua_State
*L
, gint ref
, gint nargs
, gint nret
) {
125 lua_rawgeti(L
, LUA_REGISTRYINDEX
, ref
);
126 return luaH_dofunction(L
, nargs
, nret
);
129 /* Print a warning about some Lua code.
130 * This is less mean than luaL_error() which setjmp via lua_error() and kills
131 * everything. This only warn, it's up to you to then do what's should be done.
132 * \param L The Lua VM state.
133 * \param fmt The warning message.
135 static inline void __attribute__ ((format(printf
, 2, 3)))
136 luaH_warn(lua_State
*L
, const gchar
*fmt
, ...) {
139 fprintf(stderr
, "%sW: ", lua_tostring(L
, -1));
142 vfprintf(stderr
, fmt
, ap
);
144 fprintf(stderr
, "\n");
148 gboolean
luaH_parserc(const gchar
*, gboolean
);
149 gboolean
luaH_hasitem(lua_State
*, gconstpointer
);
150 gint
luaH_next(lua_State
*, gint
);
151 gboolean
luaH_isloop(lua_State
*, gint
);
153 gint
luaH_class_index_miss_property(lua_State
*, lua_object_t
*);
154 gint
luaH_class_newindex_miss_property(lua_State
*, lua_object_t
*);
155 void luaH_modifier_table_push(lua_State
*, guint
);
156 void luaH_keystr_push(lua_State
*, guint
);
159 // vim: ft=c:et:sw=4:ts=8:sts=4:tw=80