luaclass: optimize type handling

We use lua_class_t pointer as key in the registry to store metatable we
will compare.
lauxlib uses a string, which sucks, because it forces to do a
pushliteral() each time you want to get a metatable from the registry,
which is slower.

Signed-off-by: Julien Danjou <julien@danjou.info>
This commit is contained in:
Julien Danjou 2009-08-17 15:52:41 +02:00
parent 2f93980ff8
commit dc61d258f0
24 changed files with 171 additions and 168 deletions

View File

@ -91,7 +91,7 @@ luaA_button_array_set(lua_State *L, int oidx, int idx, button_array_t *buttons)
lua_pushnil(L); lua_pushnil(L);
while(lua_next(L, idx)) while(lua_next(L, idx))
if(luaA_toudata(L, -1, "button")) if(luaA_toudata(L, -1, &button_class))
button_array_append(buttons, luaA_object_ref_item(L, oidx, -1)); button_array_append(buttons, luaA_object_ref_item(L, oidx, -1));
else else
lua_pop(L, 1); lua_pop(L, 1);

View File

@ -35,7 +35,7 @@ struct button_t
}; };
lua_class_t button_class; lua_class_t button_class;
LUA_OBJECT_FUNCS(button_class, button_t, button, "button") LUA_OBJECT_FUNCS(button_class, button_t, button)
ARRAY_FUNCS(button_t *, button, DO_NOTHING) ARRAY_FUNCS(button_t *, button, DO_NOTHING)
int luaA_button_array_get(lua_State *, int, button_array_t *); int luaA_button_array_get(lua_State *, int, button_array_t *);

View File

@ -35,7 +35,7 @@
client_t * client_t *
luaA_client_checkudata(lua_State *L, int ud) luaA_client_checkudata(lua_State *L, int ud)
{ {
client_t *c = luaL_checkudata(L, ud, "client"); client_t *c = luaA_checkudata(L, ud, &client_class);
if(c->invalid) if(c->invalid)
luaL_error(L, "client is invalid\n"); luaL_error(L, "client is invalid\n");
return c; return c;
@ -48,7 +48,7 @@ luaA_client_checkudata(lua_State *L, int ud)
static int static int
luaA_client_gc(lua_State *L) luaA_client_gc(lua_State *L)
{ {
client_t *c = luaL_checkudata(L, 1, "client"); client_t *c = luaA_checkudata(L, 1, &client_class);
button_array_wipe(&c->buttons); button_array_wipe(&c->buttons);
key_array_wipe(&c->keys); key_array_wipe(&c->keys);
xcb_get_wm_protocols_reply_wipe(&c->protocols); xcb_get_wm_protocols_reply_wipe(&c->protocols);

View File

@ -167,7 +167,7 @@ ARRAY_FUNCS(client_t *, client, DO_NOTHING)
/** Client class */ /** Client class */
lua_class_t client_class; lua_class_t client_class;
LUA_OBJECT_FUNCS(client_class, client_t, client, "client") LUA_OBJECT_FUNCS(client_class, client_t, client)
#define client_need_reban(c) \ #define client_need_reban(c) \
do { \ do { \

View File

@ -34,34 +34,61 @@ struct lua_class_property
lua_class_propfunc_t newindex; lua_class_propfunc_t newindex;
}; };
typedef struct DO_ARRAY(lua_class_t *, lua_class, DO_NOTHING)
static lua_class_array_t luaA_classes;
/** Convert a object to a udata if possible.
* \param L The Lua VM state.
* \param ud The index.
* \param class The wanted class.
* \return A pointer to the object, NULL otherwise.
*/
void *
luaA_toudata(lua_State *L, int ud, lua_class_t *class)
{ {
int id; void *p = lua_touserdata(L, ud);
lua_class_t *class; if(p) /* value is a userdata? */
} lua_class_id_t; if(lua_getmetatable(L, ud)) /* does it have a metatable? */
{
lua_pushlightuserdata(L, class);
lua_rawget(L, LUA_REGISTRYINDEX);
if(!lua_rawequal(L, -1, -2)) /* does it have the correct mt? */
p = NULL;
lua_pop(L, 2); /* remove both metatables */
}
return p;
}
DO_ARRAY(lua_class_id_t, lua_class_id, DO_NOTHING) /** Check for a udata class.
* \param L The Lua VM state.
* \param ud The object index on the stack.
* \param class The wanted class.
*/
void *
luaA_checkudata(lua_State *L, int ud, lua_class_t *class)
{
void *p = luaA_toudata(L, ud, class);
if(!p)
luaL_typerror(L, ud, class->name);
return p;
}
static lua_class_id_array_t luaA_classes; /** Get an object lua_class.
/* This has to be initialized to the highest natural type of Lua */
#define LUA_HIGHEST_TYPE LUA_TTHREAD
/** Enhanced version of lua_type that recognizes setup Lua classes.
* \param L The Lua VM state. * \param L The Lua VM state.
* \param idx The index of the object on the stack. * \param idx The index of the object on the stack.
*/ */
int lua_class_t *
luaA_type(lua_State *L, int idx) luaA_class_get(lua_State *L, int idx)
{ {
int type = lua_type(L, idx); int type = lua_type(L, idx);
if(type == LUA_TUSERDATA) if(type == LUA_TUSERDATA)
foreach(class, luaA_classes) foreach(class, luaA_classes)
if(luaA_toudata(L, idx, class->class->name)) if(luaA_toudata(L, idx, *class))
return class->id; return *class;
return type; return NULL;
} }
/** Enhanced version of lua_typename that recognizes setup Lua classes. /** Enhanced version of lua_typename that recognizes setup Lua classes.
@ -71,12 +98,14 @@ luaA_type(lua_State *L, int idx)
const char * const char *
luaA_typename(lua_State *L, int idx) luaA_typename(lua_State *L, int idx)
{ {
int type = luaA_type(L, idx); int type = lua_type(L, idx);
if(type > LUA_HIGHEST_TYPE) if(type == LUA_TUSERDATA)
foreach(class, luaA_classes) {
if(class->id == type) lua_class_t *lua_class = luaA_class_get(L, idx);
return class->class->name; if(lua_class)
return lua_class->name;
}
return lua_typename(L, type); return lua_typename(L, type);
} }
@ -129,18 +158,27 @@ luaA_class_setup(lua_State *L, lua_class_t *class,
const struct luaL_reg methods[], const struct luaL_reg methods[],
const struct luaL_reg meta[]) const struct luaL_reg meta[])
{ {
static int class_type_counter = LUA_HIGHEST_TYPE; /* Create the metatable */
lua_newtable(L);
/* Register it with class pointer as key in the registry */
lua_pushlightuserdata(L, class);
/* Duplicate the metatable */
lua_pushvalue(L, -2);
lua_rawset(L, LUA_REGISTRYINDEX);
luaA_openlib(L, name, methods, meta); lua_pushvalue(L, -1); /* dup metatable 2 */
lua_setfield(L, -2, "__index"); /* metatable.__index = metatable 1 */
luaL_register(L, NULL, meta); /* 1 */
luaL_register(L, name, methods); /* 2 */
lua_pushvalue(L, -1); /* dup self as metatable 3 */
lua_setmetatable(L, -2); /* set self as metatable 2 */
lua_pop(L, 2);
class->allocator = allocator; class->allocator = allocator;
class->name = name; class->name = name;
lua_class_id_array_append(&luaA_classes, (lua_class_id_t) lua_class_array_append(&luaA_classes, class);
{
.id = ++class_type_counter,
.class = class,
});
} }
void void
@ -214,29 +252,6 @@ lua_class_property_array_getbyid(lua_class_property_array_t *arr,
return lua_class_property_array_lookup(arr, &lookup_prop); return lua_class_property_array_lookup(arr, &lookup_prop);
} }
/** Get the class of an object.
* \param L The Lua VM state.
* \param idx The index of the object on the stack.
* \return The class if found, NULL otherwise.
*/
static lua_class_t *
luaA_class_get(lua_State *L, int idx)
{
int type = luaA_type(L, 1);
/* Find the class. */
lua_class_t *class = NULL;
foreach(classid, luaA_classes)
if(classid->id == type)
{
class = classid->class;
break;
}
return class;
}
/** Get a property of a object. /** Get a property of a object.
* \param L The Lua VM state. * \param L The Lua VM state.
* \param lua_class The Lua class. * \param lua_class The Lua class.
@ -271,7 +286,7 @@ luaA_class_index(lua_State *L)
/* Property does exist and has an index callback */ /* Property does exist and has an index callback */
if(prop && prop->index) if(prop && prop->index)
return prop->index(L, luaL_checkudata(L, 1, class->name)); return prop->index(L, luaA_checkudata(L, 1, class));
return 0; return 0;
} }
@ -293,7 +308,7 @@ luaA_class_newindex(lua_State *L)
/* Property does exist and has a newindex callback */ /* Property does exist and has a newindex callback */
if(prop && prop->newindex) if(prop && prop->newindex)
return prop->newindex(L, luaL_checkudata(L, 1, class->name)); return prop->newindex(L, luaA_checkudata(L, 1, class));
return 0; return 0;
} }

View File

@ -55,8 +55,8 @@ typedef struct
typedef int (*lua_class_propfunc_t)(lua_State *, lua_object_t *); typedef int (*lua_class_propfunc_t)(lua_State *, lua_object_t *);
int luaA_type(lua_State *, int);
const char * luaA_typename(lua_State *, int); const char * luaA_typename(lua_State *, int);
lua_class_t * luaA_class_get(lua_State *, int);
void luaA_class_add_signal(lua_State *, lua_class_t *, const char *, int); void luaA_class_add_signal(lua_State *, lua_class_t *, const char *, int);
void luaA_class_remove_signal(lua_State *, lua_class_t *, const char *, int); void luaA_class_remove_signal(lua_State *, lua_class_t *, const char *, int);
@ -74,6 +74,9 @@ int luaA_class_index(lua_State *);
int luaA_class_newindex(lua_State *); int luaA_class_newindex(lua_State *);
int luaA_class_new(lua_State *, lua_class_t *); int luaA_class_new(lua_State *, lua_class_t *);
void * luaA_checkudata(lua_State *, int, lua_class_t *);
void * luaA_toudata(lua_State *L, int ud, lua_class_t *);
#define LUA_CLASS_FUNCS(prefix, lua_class) \ #define LUA_CLASS_FUNCS(prefix, lua_class) \
static inline int \ static inline int \
luaA_##prefix##_class_add_signal(lua_State *L) \ luaA_##prefix##_class_add_signal(lua_State *L) \

View File

@ -120,27 +120,6 @@ luaA_dofunction(lua_State *L, int nargs, int nret)
return true; return true;
} }
/** Convert a object to a udata if possible.
* \param L The Lua VM state.
* \param ud The index.
* \param tname The type name.
* \return A pointer to the object, NULL otherwise.
*/
static inline void *
luaA_toudata(lua_State *L, int ud, const char *tname)
{
void *p = lua_touserdata(L, ud);
if(p) /* value is a userdata? */
if(lua_getmetatable(L, ud)) /* does it have a metatable? */
{
lua_getfield(L, LUA_REGISTRYINDEX, tname); /* get correct metatable */
if(!lua_rawequal(L, -1, -2)) /* does it have the correct mt? */
p = NULL;
lua_pop(L, 2); /* remove both metatables */
}
return p;
}
#define luaA_checktable(L, n) \ #define luaA_checktable(L, n) \
do { \ do { \
if(!lua_istable(L, n)) \ if(!lua_istable(L, n)) \

View File

@ -139,6 +139,15 @@ luaA_object_decref(lua_State *L, int tud, void *pointer)
} }
} }
int
luaA_settype(lua_State *L, lua_class_t *lua_class)
{
lua_pushlightuserdata(L, lua_class);
lua_rawget(L, LUA_REGISTRYINDEX);
lua_setmetatable(L, -2);
return 1;
}
/** Add a signal to an object. /** Add a signal to an object.
* \param L The Lua VM state. * \param L The Lua VM state.
* \param oud The object index on the stack. * \param oud The object index on the stack.
@ -219,4 +228,24 @@ luaA_object_emit_signal_simple(lua_State *L)
return 0; return 0;
} }
int
luaA_object_tostring(lua_State *L)
{
lua_class_t *lua_class = luaA_class_get(L, 1);
lua_pushfstring(L, "%s: %p", lua_class->name, luaA_checkudata(L, 1, lua_class));
return 1;
}
/** Garbage collect a Lua object.
* \param L The Lua VM state.
* \return The number of elements pushed on stack.
*/
int
luaA_object_gc(lua_State *L)
{
lua_object_t *item = lua_touserdata(L, 1);
signal_array_wipe(&item->signals);
return 0;
}
// vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80

View File

@ -24,16 +24,9 @@
#include "common/luaclass.h" #include "common/luaclass.h"
static inline int
luaA_settype(lua_State *L, const char *type)
{
luaL_getmetatable(L, type);
lua_setmetatable(L, -2);
return 1;
}
#define LUAA_OBJECT_REGISTRY_KEY "awesome.object.registry" #define LUAA_OBJECT_REGISTRY_KEY "awesome.object.registry"
int luaA_settype(lua_State *, lua_class_t *);
void luaA_object_setup(lua_State *); void luaA_object_setup(lua_State *);
void * luaA_object_incref(lua_State *, int, int); void * luaA_object_incref(lua_State *, int, int);
void luaA_object_decref(lua_State *, int, void *); void luaA_object_decref(lua_State *, int, void *);
@ -149,14 +142,14 @@ int luaA_object_add_signal_simple(lua_State *);
int luaA_object_remove_signal_simple(lua_State *); int luaA_object_remove_signal_simple(lua_State *);
int luaA_object_emit_signal_simple(lua_State *); int luaA_object_emit_signal_simple(lua_State *);
#define LUA_OBJECT_FUNCS(lua_class, type, prefix, lua_type) \ #define LUA_OBJECT_FUNCS(lua_class, type, prefix) \
LUA_CLASS_FUNCS(prefix, lua_class) \ LUA_CLASS_FUNCS(prefix, lua_class) \
static inline type * \ static inline type * \
prefix##_new(lua_State *L) \ prefix##_new(lua_State *L) \
{ \ { \
type *p = lua_newuserdata(L, sizeof(type)); \ type *p = lua_newuserdata(L, sizeof(type)); \
p_clear(p, 1); \ p_clear(p, 1); \
luaA_settype(L, lua_type); \ luaA_settype(L, &(lua_class)); \
lua_newtable(L); \ lua_newtable(L); \
lua_newtable(L); \ lua_newtable(L); \
lua_setmetatable(L, -2); \ lua_setmetatable(L, -2); \
@ -164,13 +157,6 @@ int luaA_object_emit_signal_simple(lua_State *);
lua_pushvalue(L, -1); \ lua_pushvalue(L, -1); \
luaA_class_emit_signal(L, &(lua_class), "new", 1); \ luaA_class_emit_signal(L, &(lua_class), "new", 1); \
return p; \ return p; \
} \
\
static inline int \
luaA_##prefix##_tostring(lua_State *L) \
{ \
lua_pushfstring(L, lua_type ": %p", luaL_checkudata(L, 1, lua_type)); \
return 1; \
} }
#define LUA_OBJECT_EXPORT_PROPERTY(pfx, type, field, pusher) \ #define LUA_OBJECT_EXPORT_PROPERTY(pfx, type, field, pusher) \
@ -181,20 +167,11 @@ int luaA_object_emit_signal_simple(lua_State *);
return 1; \ return 1; \
} }
/** Garbage collect a Lua object. int luaA_object_tostring(lua_State *);
* \param L The Lua VM state. int luaA_object_gc(lua_State *);
* \return The number of elements pushed on stack.
*/
static inline int
luaA_object_gc(lua_State *L)
{
lua_object_t *item = lua_touserdata(L, 1);
signal_array_wipe(&item->signals);
return 0;
}
#define LUA_OBJECT_META(prefix) \ #define LUA_OBJECT_META(prefix) \
{ "__tostring", luaA_##prefix##_tostring }, \ { "__tostring", luaA_object_tostring }, \
{ "add_signal", luaA_object_add_signal_simple }, \ { "add_signal", luaA_object_add_signal_simple }, \
{ "remove_signal", luaA_object_remove_signal_simple }, \ { "remove_signal", luaA_object_remove_signal_simple }, \
{ "emit_signal", luaA_object_emit_signal_simple }, { "emit_signal", luaA_object_emit_signal_simple },

28
image.c
View File

@ -27,7 +27,7 @@
static int static int
luaA_image_gc(lua_State *L) luaA_image_gc(lua_State *L)
{ {
image_t *p = luaL_checkudata(L, 1, "image"); image_t *p = luaA_checkudata(L, 1, &image_class);
imlib_context_set_image(p->image); imlib_context_set_image(p->image);
imlib_free_image(); imlib_free_image();
p_delete(&p->data); p_delete(&p->data);
@ -312,7 +312,7 @@ luaA_image_argb32_new(lua_State *L)
static int static int
luaA_image_orientate(lua_State *L) luaA_image_orientate(lua_State *L)
{ {
image_t *image = luaL_checkudata(L, 1, "image"); image_t *image = luaA_checkudata(L, 1, &image_class);
int orientation = luaL_checknumber(L, 2); int orientation = luaL_checknumber(L, 2);
imlib_context_set_image(image->image); imlib_context_set_image(image->image);
@ -334,7 +334,7 @@ luaA_image_orientate(lua_State *L)
static int static int
luaA_image_rotate(lua_State *L) luaA_image_rotate(lua_State *L)
{ {
image_t *image = luaL_checkudata(L, 1, "image"), *new; image_t *image = luaA_checkudata(L, 1, &image_class), *new;
double angle = luaL_checknumber(L, 2); double angle = luaL_checknumber(L, 2);
new = image_new(L); new = image_new(L);
@ -358,7 +358,7 @@ luaA_image_rotate(lua_State *L)
static int static int
luaA_image_crop(lua_State *L) luaA_image_crop(lua_State *L)
{ {
image_t *image = luaL_checkudata(L, 1, "image"), *new; image_t *image = luaA_checkudata(L, 1, &image_class), *new;
int x = luaL_checkint(L, 2); int x = luaL_checkint(L, 2);
int y = luaL_checkint(L, 3); int y = luaL_checkint(L, 3);
int w = luaL_checkint(L, 4); int w = luaL_checkint(L, 4);
@ -388,7 +388,7 @@ luaA_image_crop(lua_State *L)
static int static int
luaA_image_crop_and_scale(lua_State *L) luaA_image_crop_and_scale(lua_State *L)
{ {
image_t *image = luaL_checkudata(L, 1, "image"), *new; image_t *image = luaA_checkudata(L, 1, &image_class), *new;
int source_x = luaL_checkint(L, 2); int source_x = luaL_checkint(L, 2);
int source_y = luaL_checkint(L, 3); int source_y = luaL_checkint(L, 3);
int w = luaL_checkint(L, 4); int w = luaL_checkint(L, 4);
@ -421,7 +421,7 @@ luaA_image_draw_pixel(lua_State *L)
size_t len; size_t len;
color_t color; color_t color;
color_init_cookie_t cookie; color_init_cookie_t cookie;
image_t *image = luaL_checkudata(L, 1, "image"); image_t *image = luaA_checkudata(L, 1, &image_class);
int x = luaL_checkint(L, 2); int x = luaL_checkint(L, 2);
int y = luaL_checkint(L, 3); int y = luaL_checkint(L, 3);
const char *buf = luaL_checklstring(L, 4, &len); const char *buf = luaL_checklstring(L, 4, &len);
@ -455,7 +455,7 @@ luaA_image_draw_line(lua_State *L)
size_t len; size_t len;
color_t color; color_t color;
color_init_cookie_t cookie; color_init_cookie_t cookie;
image_t *image = luaL_checkudata(L, 1, "image"); image_t *image = luaA_checkudata(L, 1, &image_class);
int x1 = luaL_checkint(L, 2); int x1 = luaL_checkint(L, 2);
int y1 = luaL_checkint(L, 3); int y1 = luaL_checkint(L, 3);
int x2 = luaL_checkint(L, 4); int x2 = luaL_checkint(L, 4);
@ -492,7 +492,7 @@ luaA_image_draw_rectangle(lua_State *L)
size_t len; size_t len;
color_t color; color_t color;
color_init_cookie_t cookie; color_init_cookie_t cookie;
image_t *image = luaL_checkudata(L, 1, "image"); image_t *image = luaA_checkudata(L, 1, &image_class);
int x = luaL_checkint(L, 2); int x = luaL_checkint(L, 2);
int y = luaL_checkint(L, 3); int y = luaL_checkint(L, 3);
int width = luaL_checkint(L, 4); int width = luaL_checkint(L, 4);
@ -584,7 +584,7 @@ luaA_table_to_color_range(lua_State *L, int ud)
static int static int
luaA_image_draw_rectangle_gradient(lua_State *L) luaA_image_draw_rectangle_gradient(lua_State *L)
{ {
image_t *image = luaL_checkudata(L, 1, "image"); image_t *image = luaA_checkudata(L, 1, &image_class);
int x = luaL_checkint(L, 2); int x = luaL_checkint(L, 2);
int y = luaL_checkint(L, 3); int y = luaL_checkint(L, 3);
int width = luaL_checkint(L, 4); int width = luaL_checkint(L, 4);
@ -627,7 +627,7 @@ luaA_image_draw_circle(lua_State *L)
size_t len; size_t len;
color_t color; color_t color;
color_init_cookie_t cookie; color_init_cookie_t cookie;
image_t *image = luaL_checkudata(L, 1, "image"); image_t *image = luaA_checkudata(L, 1, &image_class);
int x = luaL_checkint(L, 2); int x = luaL_checkint(L, 2);
int y = luaL_checkint(L, 3); int y = luaL_checkint(L, 3);
int ah = luaL_checkint(L, 4); int ah = luaL_checkint(L, 4);
@ -664,7 +664,7 @@ luaA_image_draw_circle(lua_State *L)
static int static int
luaA_image_save(lua_State *L) luaA_image_save(lua_State *L)
{ {
image_t *image = luaL_checkudata(L, 1, "image"); image_t *image = luaA_checkudata(L, 1, &image_class);
const char *path = luaL_checkstring(L, 2); const char *path = luaL_checkstring(L, 2);
Imlib_Load_Error err; Imlib_Load_Error err;
@ -697,8 +697,8 @@ luaA_image_save(lua_State *L)
static int static int
luaA_image_insert(lua_State *L) luaA_image_insert(lua_State *L)
{ {
image_t *image_target = luaL_checkudata(L, 1, "image"); image_t *image_target = luaA_checkudata(L, 1, &image_class);
image_t *image_source = luaL_checkudata(L, 2, "image"); image_t *image_source = luaA_checkudata(L, 2, &image_class);
int xoff = luaL_optnumber(L, 3, 0); int xoff = luaL_optnumber(L, 3, 0);
int yoff = luaL_optnumber(L, 4, 0); int yoff = luaL_optnumber(L, 4, 0);
@ -743,7 +743,7 @@ luaA_image_index(lua_State *L)
if(luaA_usemetatable(L, 1, 2)) if(luaA_usemetatable(L, 1, 2))
return 1; return 1;
image_t *image = luaL_checkudata(L, 1, "image"); image_t *image = luaA_checkudata(L, 1, &image_class);
size_t len; size_t len;
const char *attr = luaL_checklstring(L, 2, &len); const char *attr = luaL_checklstring(L, 2, &len);

View File

@ -41,7 +41,7 @@ typedef struct
} image_t; } image_t;
lua_class_t image_class; lua_class_t image_class;
LUA_OBJECT_FUNCS(image_class, image_t, image, "image") LUA_OBJECT_FUNCS(image_class, image_t, image)
int image_new_from_argb32(int, int, uint32_t *); int image_new_from_argb32(int, int, uint32_t *);
uint8_t * image_getdata(image_t *); uint8_t * image_getdata(image_t *);

4
key.c
View File

@ -968,7 +968,7 @@ key_getkeysym(xcb_keycode_t detail, uint16_t state)
static void static void
luaA_keystore(lua_State *L, int ud, const char *str, ssize_t len) luaA_keystore(lua_State *L, int ud, const char *str, ssize_t len)
{ {
keyb_t *key = luaL_checkudata(L, ud, "key"); keyb_t *key = luaA_checkudata(L, ud, &key_class);
if(len) if(len)
{ {
if(*str != '#') if(*str != '#')
@ -1063,7 +1063,7 @@ luaA_key_array_set(lua_State *L, int oidx, int idx, key_array_t *keys)
lua_pushnil(L); lua_pushnil(L);
while(lua_next(L, idx)) while(lua_next(L, idx))
if(luaA_toudata(L, -1, "key")) if(luaA_toudata(L, -1, &key_class))
key_array_append(keys, luaA_object_ref_item(L, oidx, -1)); key_array_append(keys, luaA_object_ref_item(L, oidx, -1));
else else
lua_pop(L, 1); lua_pop(L, 1);

6
key.h
View File

@ -19,8 +19,8 @@
* *
*/ */
#ifndef AWESOME_KEYBINDING_H #ifndef AWESOME_KEY_H
#define AWESOME_KEYBINDING_H #define AWESOME_KEY_H
#include "luaa.h" #include "luaa.h"
@ -36,7 +36,7 @@ typedef struct keyb_t
} keyb_t; } keyb_t;
lua_class_t key_class; lua_class_t key_class;
LUA_OBJECT_FUNCS(key_class, keyb_t, key, "key") LUA_OBJECT_FUNCS(key_class, keyb_t, key)
DO_ARRAY(keyb_t *, key, DO_NOTHING) DO_ARRAY(keyb_t *, key, DO_NOTHING)
void key_class_setup(lua_State *); void key_class_setup(lua_State *);

4
luaa.c
View File

@ -345,7 +345,7 @@ luaA_wtable_newindex(lua_State *L)
/* get current key value in content table */ /* get current key value in content table */
lua_rawget(L, lua_upvalueindex(1)); lua_rawget(L, lua_upvalueindex(1));
/* if value is a widget, notify change */ /* if value is a widget, notify change */
if(lua_istable(L, -1) || luaA_toudata(L, -1, "widget")) if(lua_istable(L, -1) || luaA_toudata(L, -1, &widget_class))
invalid = true; invalid = true;
lua_pop(L, 1); /* remove value */ lua_pop(L, 1); /* remove value */
@ -356,7 +356,7 @@ luaA_wtable_newindex(lua_State *L)
luaA_table2wtable(L); luaA_table2wtable(L);
invalid = true; invalid = true;
} }
else if(!invalid && luaA_toudata(L, 3, "widget")) else if(!invalid && luaA_toudata(L, 3, &widget_class))
invalid = true; invalid = true;
/* upvalue 1 is content table */ /* upvalue 1 is content table */

10
tag.c
View File

@ -26,7 +26,7 @@
#include "widget.h" #include "widget.h"
static lua_class_t tag_class; static lua_class_t tag_class;
LUA_OBJECT_FUNCS(tag_class, tag_t, tag, "tag") LUA_OBJECT_FUNCS(tag_class, tag_t, tag)
void void
tag_unref_simplified(tag_t **tag) tag_unref_simplified(tag_t **tag)
@ -41,7 +41,7 @@ tag_unref_simplified(tag_t **tag)
static int static int
luaA_tag_gc(lua_State *L) luaA_tag_gc(lua_State *L)
{ {
tag_t *tag = luaL_checkudata(L, 1, "tag"); tag_t *tag = luaA_checkudata(L, 1, &tag_class);
client_array_wipe(&tag->clients); client_array_wipe(&tag->clients);
p_delete(&tag->name); p_delete(&tag->name);
return luaA_object_gc(L); return luaA_object_gc(L);
@ -55,7 +55,7 @@ luaA_tag_gc(lua_State *L)
static void static void
tag_view(lua_State *L, int udx, bool view) tag_view(lua_State *L, int udx, bool view)
{ {
tag_t *tag = luaL_checkudata(L, udx, "tag"); tag_t *tag = luaA_checkudata(L, udx, &tag_class);
if(tag->selected != view) if(tag->selected != view)
{ {
tag->selected = view; tag->selected = view;
@ -92,7 +92,7 @@ tag_view(lua_State *L, int udx, bool view)
void void
tag_append_to_screen(lua_State *L, int udx, screen_t *s) tag_append_to_screen(lua_State *L, int udx, screen_t *s)
{ {
tag_t *tag = luaL_checkudata(globalconf.L, udx, "tag"); tag_t *tag = luaA_checkudata(globalconf.L, udx, &tag_class);
/* can't attach a tag twice */ /* can't attach a tag twice */
if(tag->screen) if(tag->screen)
@ -306,7 +306,7 @@ luaA_tag_new(lua_State *L)
static int static int
luaA_tag_clients(lua_State *L) luaA_tag_clients(lua_State *L)
{ {
tag_t *tag = luaL_checkudata(L, 1, "tag"); tag_t *tag = luaA_checkudata(L, 1, &tag_class);
client_array_t *clients = &tag->clients; client_array_t *clients = &tag->clients;
int i; int i;

View File

@ -33,7 +33,7 @@ typedef struct
} atimer_t; } atimer_t;
static lua_class_t timer_class; static lua_class_t timer_class;
LUA_OBJECT_FUNCS(timer_class, atimer_t, timer, "timer") LUA_OBJECT_FUNCS(timer_class, atimer_t, timer)
static void static void
ev_timer_emit_signal(struct ev_loop *loop, struct ev_timer *w, int revents) ev_timer_emit_signal(struct ev_loop *loop, struct ev_timer *w, int revents)
@ -47,7 +47,7 @@ static int
luaA_timer_new(lua_State *L) luaA_timer_new(lua_State *L)
{ {
luaA_class_new(L, &timer_class); luaA_class_new(L, &timer_class);
atimer_t *timer = luaL_checkudata(L, -1, "timer"); atimer_t *timer = luaA_checkudata(L, -1, &timer_class);
timer->timer.data = timer; timer->timer.data = timer;
ev_set_cb(&timer->timer, ev_timer_emit_signal); ev_set_cb(&timer->timer, ev_timer_emit_signal);
return 1; return 1;
@ -72,7 +72,7 @@ luaA_timer_get_timeout(lua_State *L, atimer_t *timer)
static int static int
luaA_timer_start(lua_State *L) luaA_timer_start(lua_State *L)
{ {
atimer_t *timer = luaL_checkudata(L, 1, "timer"); atimer_t *timer = luaA_checkudata(L, 1, &timer_class);
if(timer->started) if(timer->started)
luaA_warn(L, "timer already started"); luaA_warn(L, "timer already started");
else else
@ -87,7 +87,7 @@ luaA_timer_start(lua_State *L)
static int static int
luaA_timer_stop(lua_State *L) luaA_timer_stop(lua_State *L)
{ {
atimer_t *timer = luaL_checkudata(L, 1, "timer"); atimer_t *timer = luaA_checkudata(L, 1, &timer_class);
if(timer->started) if(timer->started)
{ {
ev_timer_stop(globalconf.loop, &timer->timer); ev_timer_stop(globalconf.loop, &timer->timer);

10
wibox.c
View File

@ -37,7 +37,7 @@
static int static int
luaA_wibox_gc(lua_State *L) luaA_wibox_gc(lua_State *L)
{ {
wibox_t *wibox = luaL_checkudata(L, 1, "wibox"); wibox_t *wibox = luaA_checkudata(L, 1, &wibox_class);
p_delete(&wibox->cursor); p_delete(&wibox->cursor);
wibox_wipe(wibox); wibox_wipe(wibox);
button_array_wipe(&wibox->buttons); button_array_wipe(&wibox->buttons);
@ -839,7 +839,7 @@ static int
luaA_wibox_index(lua_State *L) luaA_wibox_index(lua_State *L)
{ {
size_t len; size_t len;
wibox_t *wibox = luaL_checkudata(L, 1, "wibox"); wibox_t *wibox = luaA_checkudata(L, 1, &wibox_class);
const char *attr = luaL_checklstring(L, 2, &len); const char *attr = luaL_checklstring(L, 2, &len);
if(luaA_usemetatable(L, 1, 2)) if(luaA_usemetatable(L, 1, 2))
@ -924,7 +924,7 @@ luaA_wibox_index(lua_State *L)
static int static int
luaA_wibox_geometry(lua_State *L) luaA_wibox_geometry(lua_State *L)
{ {
wibox_t *wibox = luaL_checkudata(L, 1, "wibox"); wibox_t *wibox = luaA_checkudata(L, 1, &wibox_class);
if(lua_gettop(L) == 2) if(lua_gettop(L) == 2)
{ {
@ -961,7 +961,7 @@ static int
luaA_wibox_newindex(lua_State *L) luaA_wibox_newindex(lua_State *L)
{ {
size_t len; size_t len;
wibox_t *wibox = luaL_checkudata(L, 1, "wibox"); wibox_t *wibox = luaA_checkudata(L, 1, &wibox_class);
const char *buf, *attr = luaL_checklstring(L, 2, &len); const char *buf, *attr = luaL_checklstring(L, 2, &len);
awesome_token_t tok; awesome_token_t tok;
@ -1153,7 +1153,7 @@ luaA_wibox_newindex(lua_State *L)
static int static int
luaA_wibox_buttons(lua_State *L) luaA_wibox_buttons(lua_State *L)
{ {
wibox_t *wibox = luaL_checkudata(L, 1, "wibox"); wibox_t *wibox = luaA_checkudata(L, 1, &wibox_class);
if(lua_gettop(L) == 2) if(lua_gettop(L) == 2)
{ {

View File

@ -117,7 +117,7 @@ void wibox_border_color_set(wibox_t *, const xcolor_t *);
void wibox_orientation_set(wibox_t *, orientation_t); void wibox_orientation_set(wibox_t *, orientation_t);
lua_class_t wibox_class; lua_class_t wibox_class;
LUA_OBJECT_FUNCS(wibox_class, wibox_t, wibox, "wibox") LUA_OBJECT_FUNCS(wibox_class, wibox_t, wibox)
#endif #endif
// vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80

View File

@ -32,8 +32,7 @@
#include "common/atoms.h" #include "common/atoms.h"
#include "common/xutil.h" #include "common/xutil.h"
static lua_class_t widget_class; LUA_OBJECT_FUNCS(widget_class, widget_t, widget);
LUA_OBJECT_FUNCS(widget_class, widget_t, widget, "widget");
/** Collect a widget structure. /** Collect a widget structure.
* \param L The Lua VM state. * \param L The Lua VM state.
@ -42,7 +41,7 @@ LUA_OBJECT_FUNCS(widget_class, widget_t, widget, "widget");
static int static int
luaA_widget_gc(lua_State *L) luaA_widget_gc(lua_State *L)
{ {
widget_t *widget = luaL_checkudata(L, 1, "widget"); widget_t *widget = luaA_checkudata(L, 1, &widget_class);
if(widget->destructor) if(widget->destructor)
widget->destructor(widget); widget->destructor(widget);
button_array_wipe(&widget->buttons); button_array_wipe(&widget->buttons);
@ -115,7 +114,7 @@ luaA_table2widgets(lua_State *L, widget_node_array_t *widgets)
} }
else else
{ {
widget_t *widget = luaA_toudata(L, -1, "widget"); widget_t *widget = luaA_toudata(L, -1, &widget_class);
if(widget) if(widget)
{ {
widget_node_t w; widget_node_t w;
@ -405,7 +404,7 @@ luaA_widget_new(lua_State *L)
{ {
luaA_class_new(L, &widget_class); luaA_class_new(L, &widget_class);
widget_t *w = luaL_checkudata(L, -1, "widget"); widget_t *w = luaA_checkudata(L, -1, &widget_class);
/* Set visible by default. */ /* Set visible by default. */
w->isvisible = true; w->isvisible = true;
@ -423,7 +422,7 @@ luaA_widget_new(lua_State *L)
static int static int
luaA_widget_buttons(lua_State *L) luaA_widget_buttons(lua_State *L)
{ {
widget_t *widget = luaL_checkudata(L, 1, "widget"); widget_t *widget = luaA_checkudata(L, 1, &widget_class);
if(lua_gettop(L) == 2) if(lua_gettop(L) == 2)
{ {
@ -455,7 +454,7 @@ luaA_widget_index(lua_State *L)
return 1; return 1;
/* Then call special widget index */ /* Then call special widget index */
widget_t *widget = luaL_checkudata(L, 1, "widget"); widget_t *widget = luaA_checkudata(L, 1, &widget_class);
return widget->index ? widget->index(L, token) : 0; return widget->index ? widget->index(L, token) : 0;
} }
@ -474,14 +473,14 @@ luaA_widget_newindex(lua_State *L)
luaA_class_newindex(L); luaA_class_newindex(L);
/* Then call special widget newindex */ /* Then call special widget newindex */
widget_t *widget = luaL_checkudata(L, 1, "widget"); widget_t *widget = luaA_checkudata(L, 1, &widget_class);
return widget->newindex ? widget->newindex(L, token) : 0; return widget->newindex ? widget->newindex(L, token) : 0;
} }
static int static int
luaA_widget_extents(lua_State *L) luaA_widget_extents(lua_State *L)
{ {
widget_t *widget = luaL_checkudata(L, 1, "widget"); widget_t *widget = luaA_checkudata(L, 1, &widget_class);
area_t g = { area_t g = {
.x = 0, .x = 0,
.y = 0, .y = 0,

View File

@ -69,6 +69,7 @@ bool widget_geometries(wibox_t *);
void widget_invalidate_bywidget(widget_t *); void widget_invalidate_bywidget(widget_t *);
void widget_invalidate_bytype(widget_constructor_t *); void widget_invalidate_bytype(widget_constructor_t *);
lua_class_t widget_class;
void widget_class_setup(lua_State *); void widget_class_setup(lua_State *);
widget_constructor_t widget_textbox; widget_constructor_t widget_textbox;

View File

@ -305,7 +305,7 @@ graph_draw(widget_t *widget, draw_context_t *ctx,
static int static int
luaA_graph_plot_properties_set(lua_State *L) luaA_graph_plot_properties_set(lua_State *L)
{ {
widget_t *widget = luaL_checkudata(L, 1, "widget"); widget_t *widget = luaA_checkudata(L, 1, &widget_class);
graph_data_t *d = widget->data; graph_data_t *d = widget->data;
float max_value; float max_value;
const char *title, *buf; const char *title, *buf;
@ -370,7 +370,7 @@ luaA_graph_plot_properties_set(lua_State *L)
static int static int
luaA_graph_plot_data_add(lua_State *L) luaA_graph_plot_data_add(lua_State *L)
{ {
widget_t *widget = luaL_checkudata(L, 1, "widget"); widget_t *widget = luaA_checkudata(L, 1, &widget_class);
graph_data_t *d = widget->data; graph_data_t *d = widget->data;
plot_t *plot = NULL; plot_t *plot = NULL;
const char *title = luaL_checkstring(L, 2); const char *title = luaL_checkstring(L, 2);
@ -448,7 +448,7 @@ luaA_graph_plot_data_add(lua_State *L)
static int static int
luaA_graph_index(lua_State *L, awesome_token_t token) luaA_graph_index(lua_State *L, awesome_token_t token)
{ {
widget_t *widget = luaL_checkudata(L, 1, "widget"); widget_t *widget = luaA_checkudata(L, 1, &widget_class);
graph_data_t *d = widget->data; graph_data_t *d = widget->data;
switch(token) switch(token)
@ -500,7 +500,7 @@ static int
luaA_graph_newindex(lua_State *L, awesome_token_t token) luaA_graph_newindex(lua_State *L, awesome_token_t token)
{ {
size_t len; size_t len;
widget_t *widget = luaL_checkudata(L, 1, "widget"); widget_t *widget = luaA_checkudata(L, 1, &widget_class);
graph_data_t *d = widget->data; graph_data_t *d = widget->data;
const char *buf; const char *buf;
int width; int width;

View File

@ -102,7 +102,7 @@ imagebox_destructor(widget_t *w)
static int static int
luaA_imagebox_index(lua_State *L, awesome_token_t token) luaA_imagebox_index(lua_State *L, awesome_token_t token)
{ {
widget_t *widget = luaL_checkudata(L, 1, "widget"); widget_t *widget = luaA_checkudata(L, 1, &widget_class);
imagebox_data_t *d = widget->data; imagebox_data_t *d = widget->data;
switch(token) switch(token)
@ -131,7 +131,7 @@ luaA_imagebox_index(lua_State *L, awesome_token_t token)
static int static int
luaA_imagebox_newindex(lua_State *L, awesome_token_t token) luaA_imagebox_newindex(lua_State *L, awesome_token_t token)
{ {
widget_t *widget = luaL_checkudata(L, 1, "widget"); widget_t *widget = luaA_checkudata(L, 1, &widget_class);
imagebox_data_t *d = widget->data; imagebox_data_t *d = widget->data;
switch(token) switch(token)

View File

@ -443,7 +443,7 @@ static int
luaA_progressbar_bar_properties_set(lua_State *L) luaA_progressbar_bar_properties_set(lua_State *L)
{ {
size_t len; size_t len;
widget_t *widget = luaL_checkudata(L, 1, "widget"); widget_t *widget = luaA_checkudata(L, 1, &widget_class);
const char *buf, *title = luaL_checkstring(L, 2); const char *buf, *title = luaL_checkstring(L, 2);
bar_t *bar; bar_t *bar;
progressbar_data_t *d = widget->data; progressbar_data_t *d = widget->data;
@ -508,7 +508,7 @@ luaA_progressbar_bar_properties_set(lua_State *L)
static int static int
luaA_progressbar_bar_data_add(lua_State *L) luaA_progressbar_bar_data_add(lua_State *L)
{ {
widget_t *widget = luaL_checkudata(L, 1, "widget"); widget_t *widget = luaA_checkudata(L, 1, &widget_class);
const char *title = luaL_checkstring(L, 2); const char *title = luaL_checkstring(L, 2);
progressbar_data_t *d = widget->data; progressbar_data_t *d = widget->data;
bar_t *bar; bar_t *bar;
@ -543,7 +543,7 @@ luaA_progressbar_bar_data_add(lua_State *L)
static int static int
luaA_progressbar_index(lua_State *L, awesome_token_t token) luaA_progressbar_index(lua_State *L, awesome_token_t token)
{ {
widget_t *widget = luaL_checkudata(L, 1, "widget"); widget_t *widget = luaA_checkudata(L, 1, &widget_class);
progressbar_data_t *d = widget->data; progressbar_data_t *d = widget->data;
switch(token) switch(token)
@ -593,7 +593,7 @@ luaA_progressbar_index(lua_State *L, awesome_token_t token)
static int static int
luaA_progressbar_newindex(lua_State *L, awesome_token_t token) luaA_progressbar_newindex(lua_State *L, awesome_token_t token)
{ {
widget_t *widget = luaL_checkudata(L, 1, "widget"); widget_t *widget = luaA_checkudata(L, 1, &widget_class);
progressbar_data_t *d = widget->data; progressbar_data_t *d = widget->data;
switch(token) switch(token)

View File

@ -145,7 +145,7 @@ textbox_destructor(widget_t *w)
static int static int
luaA_textbox_margin(lua_State *L) luaA_textbox_margin(lua_State *L)
{ {
widget_t *widget = luaL_checkudata(L, 1, "widget"); widget_t *widget = luaA_checkudata(L, 1, &widget_class);
textbox_data_t *d = widget->data; textbox_data_t *d = widget->data;
if(lua_gettop(L) == 2) if(lua_gettop(L) == 2)
@ -178,7 +178,7 @@ luaA_textbox_margin(lua_State *L)
static int static int
luaA_textbox_index(lua_State *L, awesome_token_t token) luaA_textbox_index(lua_State *L, awesome_token_t token)
{ {
widget_t *widget = luaL_checkudata(L, 1, "widget"); widget_t *widget = luaA_checkudata(L, 1, &widget_class);
textbox_data_t *d = widget->data; textbox_data_t *d = widget->data;
switch(token) switch(token)
@ -257,7 +257,7 @@ static int
luaA_textbox_newindex(lua_State *L, awesome_token_t token) luaA_textbox_newindex(lua_State *L, awesome_token_t token)
{ {
size_t len = 0; size_t len = 0;
widget_t *widget = luaL_checkudata(L, 1, "widget"); widget_t *widget = luaA_checkudata(L, 1, &widget_class);
const char *buf = NULL; const char *buf = NULL;
textbox_data_t *d = widget->data; textbox_data_t *d = widget->data;