luaclass: add handling of {new,}index of missing properties (FS#584)

Signed-off-by: Julien Danjou <julien@danjou.info>
This commit is contained in:
Julien Danjou 2009-08-20 17:37:46 +02:00
parent a9880a6c89
commit 4d0a025f51
12 changed files with 57 additions and 6 deletions

View File

@ -154,6 +154,7 @@ button_class_setup(lua_State *L)
};
luaA_class_setup(L, &button_class, "button", (lua_class_allocator_t) button_new,
luaA_class_index_miss_property, luaA_class_newindex_miss_property,
button_methods, button_meta);
luaA_class_add_property(&button_class, A_TK_BUTTON,
(lua_class_propfunc_t) luaA_button_set_button,

View File

@ -2137,6 +2137,7 @@ client_class_setup(lua_State *L)
};
luaA_class_setup(L, &client_class, "client", (lua_class_allocator_t) client_new,
luaA_class_index_miss_property, luaA_class_newindex_miss_property,
client_methods, client_meta);
luaA_class_add_property(&client_class, A_TK_NAME,
NULL,

View File

@ -155,6 +155,8 @@ void
luaA_class_setup(lua_State *L, lua_class_t *class,
const char *name,
lua_class_allocator_t allocator,
lua_class_propfunc_t index_miss_property,
lua_class_propfunc_t newindex_miss_property,
const struct luaL_reg methods[],
const struct luaL_reg meta[])
{
@ -177,6 +179,8 @@ luaA_class_setup(lua_State *L, lua_class_t *class,
class->allocator = allocator;
class->name = name;
class->index_miss_property = index_miss_property;
class->newindex_miss_property = newindex_miss_property;
lua_class_array_append(&luaA_classes, class);
}
@ -275,8 +279,16 @@ luaA_class_index(lua_State *L)
lua_class_property_t *prop = luaA_class_property_get(L, class, 2);
/* Property does exist and has an index callback */
if(prop && prop->index)
return prop->index(L, luaA_checkudata(L, 1, class));
if(prop)
{
if(prop->index)
return prop->index(L, luaA_checkudata(L, 1, class));
}
else
{
if(class->index_miss_property)
return class->index_miss_property(L, luaA_checkudata(L, 1, class));
}
return 0;
}
@ -297,8 +309,16 @@ luaA_class_newindex(lua_State *L)
lua_class_property_t *prop = luaA_class_property_get(L, class, 2);
/* Property does exist and has a newindex callback */
if(prop && prop->newindex)
return prop->newindex(L, luaA_checkudata(L, 1, class));
if(prop)
{
if(prop->newindex)
return prop->newindex(L, luaA_checkudata(L, 1, class));
}
else
{
if(class->newindex_miss_property)
return class->newindex_miss_property(L, luaA_checkudata(L, 1, class));
}
return 0;
}

View File

@ -42,19 +42,24 @@ typedef struct
typedef lua_object_t *(*lua_class_allocator_t)(lua_State *);
typedef int (*lua_class_propfunc_t)(lua_State *, lua_object_t *);
typedef struct
{
/** Class name */
const char *name;
/** Class signals */
signal_array_t signals;
/** Allocator for creating new objects of that class */
lua_class_allocator_t allocator;
/** Class properties */
lua_class_property_array_t properties;
/** Function to call when a indexing an unknown property */
lua_class_propfunc_t index_miss_property;
/** Function to call when a indexing an unknown property */
lua_class_propfunc_t newindex_miss_property;
} lua_class_t;
typedef int (*lua_class_propfunc_t)(lua_State *, lua_object_t *);
const char * luaA_typename(lua_State *, int);
lua_class_t * luaA_class_get(lua_State *, int);
@ -64,6 +69,7 @@ void luaA_class_emit_signal(lua_State *, lua_class_t *, const char *, int);
void luaA_openlib(lua_State *, const char *, const struct luaL_reg[], const struct luaL_reg[]);
void luaA_class_setup(lua_State *, lua_class_t *, const char *, lua_class_allocator_t,
lua_class_propfunc_t, lua_class_propfunc_t,
const struct luaL_reg[], const struct luaL_reg[]);
void luaA_class_add_property(lua_class_t *, awesome_token_t,

View File

@ -800,6 +800,7 @@ image_class_setup(lua_State *L)
};
luaA_class_setup(L, &image_class, "image", (lua_class_allocator_t) image_new,
luaA_class_index_miss_property, luaA_class_newindex_miss_property,
image_methods, image_meta);
luaA_class_add_property(&image_class, A_TK_WIDTH,
NULL,

1
key.c
View File

@ -1199,6 +1199,7 @@ key_class_setup(lua_State *L)
};
luaA_class_setup(L, &key_class, "key", (lua_class_allocator_t) key_new,
luaA_class_index_miss_property, luaA_class_newindex_miss_property,
key_methods, key_meta);
luaA_class_add_property(&key_class, A_TK_KEY,
(lua_class_propfunc_t) luaA_key_set_key,

14
luaa.c
View File

@ -869,4 +869,18 @@ luaA_on_timer(EV_P_ ev_timer *w, int revents)
luaA_dofunction_from_registry(globalconf.L, globalconf.hooks.timer, 0, 0);
}
int
luaA_class_index_miss_property(lua_State *L, lua_object_t *obj)
{
signal_object_emit(L, &global_signals, "debug::index::miss", 2);
return 0;
}
int
luaA_class_newindex_miss_property(lua_State *L, lua_object_t *obj)
{
signal_object_emit(L, &global_signals, "debug::newindex::miss", 3);
return 0;
}
// vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80

3
luaa.h
View File

@ -243,5 +243,8 @@ bool luaA_isloop(lua_State *, int);
/** Global signals */
signal_array_t global_signals;
int luaA_class_index_miss_property(lua_State *, lua_object_t *);
int luaA_class_newindex_miss_property(lua_State *, lua_object_t *);
#endif
// vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80

1
tag.c
View File

@ -470,6 +470,7 @@ tag_class_setup(lua_State *L)
};
luaA_class_setup(L, &tag_class, "tag", (lua_class_allocator_t) tag_new,
luaA_class_index_miss_property, luaA_class_newindex_miss_property,
tag_methods, tag_meta);
luaA_class_add_property(&tag_class, A_TK_NAME,
(lua_class_propfunc_t) luaA_tag_set_name,

View File

@ -123,6 +123,7 @@ timer_class_setup(lua_State *L)
};
luaA_class_setup(L, &timer_class, "timer", (lua_class_allocator_t) timer_new,
luaA_class_index_miss_property, luaA_class_newindex_miss_property,
timer_methods, timer_meta);
luaA_class_add_property(&timer_class, A_TK_TIMEOUT,
(lua_class_propfunc_t) luaA_timer_set_timeout,

View File

@ -1505,6 +1505,7 @@ wibox_class_setup(lua_State *L)
};
luaA_class_setup(L, &wibox_class, "wibox", (lua_class_allocator_t) wibox_new,
luaA_class_index_miss_property, luaA_class_newindex_miss_property,
wibox_methods, wibox_meta);
luaA_class_add_property(&wibox_class, A_TK_WIDGETS,
(lua_class_propfunc_t) luaA_wibox_set_widgets,

View File

@ -577,6 +577,7 @@ widget_class_setup(lua_State *L)
};
luaA_class_setup(L, &widget_class, "widget", (lua_class_allocator_t) widget_new,
NULL, NULL,
widget_methods, widget_meta);
luaA_class_add_property(&widget_class, A_TK_VISIBLE,
(lua_class_propfunc_t) luaA_widget_set_visible,