2 * luaobject.h - useful functions for handling Lua objects
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_LUAOBJECT
23 #define AWESOME_COMMON_LUAOBJECT
26 #include "common/array.h"
28 /** Type for Lua references */
32 luaA_settype(lua_State
*L
, const char *type
)
34 luaL_getmetatable(L
, type
);
35 lua_setmetatable(L
, -2);
39 DO_ARRAY(luaA_ref
, luaA_ref
, DO_NOTHING
)
41 #define LUA_OBJECT_FUNCS(type, prefix, lua_type) \
42 static inline type * \
43 prefix##_new(lua_State *L) \
45 type *p = lua_newuserdata(L, sizeof(type)); \
47 luaA_settype(L, lua_type); \
52 prefix##_push(lua_State *L, type *item) \
56 assert(item->refs.len); \
57 lua_rawgeti(L, LUA_REGISTRYINDEX, item->refs.tab[0]); \
64 static inline type * \
65 prefix##_ref(lua_State *L) \
67 if(lua_isnil(L, -1)) \
69 type *item = luaL_checkudata(L, -1, lua_type); \
70 luaA_ref_array_append(&item->refs, luaL_ref(L, LUA_REGISTRYINDEX)); \
75 prefix##_unref(lua_State *L, type *item) \
79 assert(item->refs.len); \
80 luaL_unref(L, LUA_REGISTRYINDEX, item->refs.tab[0]); \
81 luaA_ref_array_take(&item->refs, 0); \
87 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80