Created webview, notebook, textarea and box widgets and window class
[luakit.git] / luah.h
blobd08d993eb47269444ee26d2e2ab62c501a03310d
1 /*
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/>.
22 #ifndef LUAKIT_LUA_H
23 #define LUAKIT_LUA_H
25 #include <lua.h>
26 #include <lauxlib.h>
28 #include <basedir.h>
30 #include "common/luaobject.h"
31 #include "common/lualib.h"
33 #define luaH_deprecate(L, repl) \
34 do { \
35 luaH_warn(L, "%s: This function is deprecated and will be removed, see %s", \
36 __FUNCTION__, repl); \
37 lua_pushlstring(L, __FUNCTION__, sizeof(__FUNCTION__)); \
38 signal_object_emit(L, &luakit.signals, "debug::deprecation", 1); \
39 } while(0)
41 static inline gboolean
42 luaH_checkboolean(lua_State *L, gint n) {
43 if(!lua_isboolean(L, n))
44 luaL_typerror(L, n, "boolean");
45 return lua_toboolean(L, n);
48 static inline gboolean
49 luaH_optboolean(lua_State *L, gint idx, gboolean def) {
50 return luaL_opt(L, luaH_checkboolean, idx, def);
53 static inline lua_Number
54 luaH_getopt_number(lua_State *L, gint idx, const gchar *name, lua_Number def) {
55 lua_getfield(L, idx, name);
56 if (lua_isnil(L, -1) || lua_isnumber(L, -1))
57 def = luaL_optnumber(L, -1, def);
58 lua_pop(L, 1);
59 return def;
62 static inline const gchar *
63 luaH_getopt_lstring(lua_State *L, gint idx, const gchar *name, const gchar *def, size_t *len) {
64 lua_getfield(L, idx, name);
65 const gchar *s = luaL_optlstring(L, -1, def, len);
66 lua_pop(L, 1);
67 return s;
70 static inline gboolean
71 luaH_getopt_boolean(lua_State *L, gint idx, const gchar *name, gboolean def) {
72 lua_getfield(L, idx, name);
73 gboolean b = luaH_optboolean(L, -1, def);
74 lua_pop(L, 1);
75 return b;
78 /* Register an Lua object.
79 * \param L The Lua stack.
80 * \param idx Index of the object in the stack.
81 * \param ref A gint address: it will be filled with the gint
82 * registered. If the address points to an already registered object, it will
83 * be unregistered.
84 * \return Always 0.
86 static inline gint
87 luaH_register(lua_State *L, gint idx, gint *ref) {
88 lua_pushvalue(L, idx);
89 if(*ref != LUA_REFNIL)
90 luaL_unref(L, LUA_REGISTRYINDEX, *ref);
91 *ref = luaL_ref(L, LUA_REGISTRYINDEX);
92 return 0;
95 /* Unregister a Lua object.
96 * \param L The Lua stack.
97 * \param ref A reference to an Lua object.
99 static inline void
100 luaH_unregister(lua_State *L, gint *ref) {
101 luaL_unref(L, LUA_REGISTRYINDEX, *ref);
102 *ref = LUA_REFNIL;
105 /* Register a function.
106 * \param L The Lua stack.
107 * \param idx Index of the function in the stack.
108 * \param fct A gint address: it will be filled with the gint
109 * registered. If the address points to an already registered function, it will
110 * be unregistered.
111 * \return luaH_register value.
113 static inline gint
114 luaH_registerfct(lua_State *L, gint idx, gint *fct) {
115 luaH_checkfunction(L, idx);
116 return luaH_register(L, idx, fct);
119 /* Grab a function from the registry and execute it.
120 * \param L The Lua stack.
121 * \param ref The function reference.
122 * \param nargs The number of arguments for the Lua function.
123 * \param nret The number of returned value from the Lua function.
124 * \return True on no error, false otherwise.
126 static inline gboolean
127 luaH_dofunction_from_registry(lua_State *L, gint ref, gint nargs, gint nret) {
128 lua_rawgeti(L, LUA_REGISTRYINDEX, ref);
129 return luaH_dofunction(L, nargs, nret);
132 /* Print a warning about some Lua code.
133 * This is less mean than luaL_error() which setjmp via lua_error() and kills
134 * everything. This only warn, it's up to you to then do what's should be done.
135 * \param L The Lua VM state.
136 * \param fmt The warning message.
138 static inline void __attribute__ ((format(printf, 2, 3)))
139 luaH_warn(lua_State *L, const gchar *fmt, ...) {
140 va_list ap;
141 luaL_where(L, 1);
142 fprintf(stderr, "%sW: ", lua_tostring(L, -1));
143 lua_pop(L, 1);
144 va_start(ap, fmt);
145 vfprintf(stderr, fmt, ap);
146 va_end(ap);
147 fprintf(stderr, "\n");
150 void luaH_init(xdgHandle *);
151 gboolean luaH_parserc(xdgHandle *, const gchar *, gboolean);
152 gboolean luaH_hasitem(lua_State *, gconstpointer);
153 gint luaH_next(lua_State *, gint);
154 gboolean luaH_isloop(lua_State *, gint);
156 gint luaH_class_index_miss_property(lua_State *, lua_object_t *);
157 gint luaH_class_newindex_miss_property(lua_State *, lua_object_t *);
159 #endif
160 // vim: ft=c:et:sw=4:ts=8:sts=4:enc=utf-8:tw=80