luaclass: add inheritance support

Signed-off-by: Julien Danjou <julien@danjou.info>
This commit is contained in:
Julien Danjou 2009-09-30 14:21:25 +02:00
parent 9393b2d111
commit fccc451f89
12 changed files with 132 additions and 47 deletions

View File

@ -48,16 +48,22 @@ void *
luaA_toudata(lua_State *L, int ud, lua_class_t *class)
{
void *p = lua_touserdata(L, ud);
if(p) /* value is a userdata? */
if(lua_getmetatable(L, ud)) /* does it have a metatable? */
if(p && lua_getmetatable(L, ud)) /* does it have a metatable? */
{
lua_pushlightuserdata(L, class);
/* Get the lua_class_t that matches this metatable */
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 */
}
lua_class_t *metatable_class = lua_touserdata(L, -1);
/* remove lightuserdata (lua_class pointer) */
lua_pop(L, 1);
/* Now, check that the class given in argument is the same as the
* metatable's object, or one of its parent (inheritance) */
for(; metatable_class; metatable_class = metatable_class->parent)
if(metatable_class == class)
return p;
}
return NULL;
}
/** Check for a udata class.
@ -85,10 +91,14 @@ luaA_class_get(lua_State *L, int idx)
{
int type = lua_type(L, idx);
if(type == LUA_TUSERDATA)
foreach(class, luaA_classes)
if(luaA_toudata(L, idx, *class))
return *class;
if(type == LUA_TUSERDATA && lua_getmetatable(L, idx))
{
/* Use the metatable has key to get the class from registry */
lua_rawget(L, LUA_REGISTRYINDEX);
lua_class_t *class = lua_touserdata(L, -1);
lua_pop(L, 1);
return class;
}
return NULL;
}
@ -153,9 +163,23 @@ luaA_class_add_property(lua_class_t *lua_class,
});
}
/** Setup a new Lua class.
* \param L The Lua VM state.
* \param name The class name.
* \param parent The parent class (inheritance).
* \param allocator The allocator function used when creating a new object.
* \param checker The check function to call when using luaA_checkudata().
* \param index_miss_property Function to call when an object of this class
* receive a __index request on an unknown property.
* \param newindex_miss_property Function to call when an object of this class
* receive a __newindex request on an unknown property.
* \param methods The methods to set on the class table.
* \param meta The meta-methods to set on the class objects.
*/
void
luaA_class_setup(lua_State *L, lua_class_t *class,
const char *name,
lua_class_t *parent,
lua_class_allocator_t allocator,
lua_class_checker_t checker,
lua_class_propfunc_t index_miss_property,
@ -165,11 +189,17 @@ luaA_class_setup(lua_State *L, lua_class_t *class,
{
/* Create the metatable */
lua_newtable(L);
/* Register it with class pointer as key in the registry */
/* Register it with class pointer as key in the registry
* class-pointer -> metatable */
lua_pushlightuserdata(L, class);
/* Duplicate the metatable */
lua_pushvalue(L, -2);
lua_rawset(L, LUA_REGISTRYINDEX);
/* Now register class pointer with metatable as key in the registry
* metatable -> class-pointer */
lua_pushvalue(L, -1);
lua_pushlightuserdata(L, class);
lua_rawset(L, LUA_REGISTRYINDEX);
lua_pushvalue(L, -1); /* dup metatable 2 */
lua_setfield(L, -2, "__index"); /* metatable.__index = metatable 1 */
@ -185,6 +215,7 @@ luaA_class_setup(lua_State *L, lua_class_t *class,
class->index_miss_property = index_miss_property;
class->newindex_miss_property = newindex_miss_property;
class->checker = checker;
class->parent = parent;
lua_class_array_append(&luaA_classes, class);
}
@ -224,20 +255,28 @@ luaA_class_emit_signal(lua_State *L, lua_class_t *lua_class,
int
luaA_usemetatable(lua_State *L, int idxobj, int idxfield)
{
/* Get metatable of the object. */
lua_getmetatable(L, idxobj);
/* Get the field */
lua_class_t *class = luaA_class_get(L, idxobj);
for(; class; class = class->parent)
{
/* Push the class */
lua_pushlightuserdata(L, class);
/* Get its metatable from registry */
lua_rawget(L, LUA_REGISTRYINDEX);
/* Push the field */
lua_pushvalue(L, idxfield);
/* Get the field in the metatable */
lua_rawget(L, -2);
/* Do we have a field like that? */
if(!lua_isnil(L, -1))
{
/* Yes, so return it! */
/* Yes, so remove the metatable and return it! */
lua_remove(L, -2);
return 1;
}
/* No, so remove everything. */
/* No, so remove the metatable and its value */
lua_pop(L, 2);
}
return 0;
}
@ -264,7 +303,17 @@ luaA_class_property_get(lua_State *L, lua_class_t *lua_class, int fieldidx)
const char *attr = luaL_checklstring(L, fieldidx, &len);
awesome_token_t token = a_tokenize(attr, len);
return lua_class_property_array_getbyid(&lua_class->properties, token);
/* Look for the property in the class; if not found, go in the parent class. */
for(; lua_class; lua_class = lua_class->parent)
{
lua_class_property_t *prop =
lua_class_property_array_getbyid(&lua_class->properties, token);
if(prop)
return prop;
}
return NULL;
}
/** Generic index meta function for objects.
@ -350,12 +399,7 @@ luaA_class_new(lua_State *L, lua_class_t *lua_class)
* number TO A STRING, confusing lua_next() */
if(lua_isstring(L, -2))
{
/* Lookup the property using token */
size_t len;
const char *attr = lua_tolstring(L, -2, &len);
lua_class_property_t *prop =
lua_class_property_array_getbyid(&lua_class->properties,
a_tokenize(attr, len));
lua_class_property_t *prop = luaA_class_property_get(L, lua_class, -2);
if(prop && prop->new)
prop->new(L, object);

View File

@ -46,12 +46,15 @@ typedef int (*lua_class_propfunc_t)(lua_State *, lua_object_t *);
typedef bool (*lua_class_checker_t)(lua_object_t *);
typedef struct
typedef struct lua_class_t lua_class_t;
struct lua_class_t
{
/** Class name */
const char *name;
/** Class signals */
signal_array_t signals;
/** Parent class */
lua_class_t *parent;
/** Allocator for creating new objects of that class */
lua_class_allocator_t allocator;
/** Class properties */
@ -62,7 +65,7 @@ typedef struct
lua_class_propfunc_t newindex_miss_property;
/** Function to call to check if an object is valid */
lua_class_checker_t checker;
} lua_class_t;
};
const char * luaA_typename(lua_State *, int);
lua_class_t * luaA_class_get(lua_State *, int);
@ -72,7 +75,8 @@ void luaA_class_remove_signal(lua_State *, lua_class_t *, const char *, int);
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_checker_t,
void luaA_class_setup(lua_State *, lua_class_t *, const char *, lua_class_t *,
lua_class_allocator_t, lua_class_checker_t,
lua_class_propfunc_t, lua_class_propfunc_t,
const struct luaL_reg[], const struct luaL_reg[]);

View File

@ -279,7 +279,24 @@ 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));
lua_object_t *object = luaA_checkudata(L, 1, lua_class);
int i = 0;
for(; lua_class; lua_class = lua_class->parent, i++)
{
if(i)
{
lua_pushliteral(L, "/");
lua_insert(L, - (i * 2));
}
lua_pushstring(L, NONULL(lua_class->name));
lua_insert(L, - (i * 2) - 1);
}
lua_pushfstring(L, ": %p", object);
lua_concat(L, i * 2);
return 1;
}

View File

@ -114,7 +114,8 @@ button_class_setup(lua_State *L)
{ NULL, NULL }
};
luaA_class_setup(L, &button_class, "button", (lua_class_allocator_t) button_new, NULL,
luaA_class_setup(L, &button_class, "button", NULL,
(lua_class_allocator_t) button_new, NULL,
luaA_class_index_miss_property, luaA_class_newindex_miss_property,
button_methods, button_meta);
luaA_class_add_property(&button_class, A_TK_BUTTON,

View File

@ -2089,7 +2089,8 @@ client_class_setup(lua_State *L)
{ NULL, NULL }
};
luaA_class_setup(L, &client_class, "client", (lua_class_allocator_t) client_new,
luaA_class_setup(L, &client_class, "client", NULL,
(lua_class_allocator_t) client_new,
(lua_class_checker_t) client_checker,
luaA_class_index_miss_property, luaA_class_newindex_miss_property,
client_methods, client_meta);

View File

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

View File

@ -1126,7 +1126,8 @@ key_class_setup(lua_State *L)
{ NULL, NULL },
};
luaA_class_setup(L, &key_class, "key", (lua_class_allocator_t) key_new, NULL,
luaA_class_setup(L, &key_class, "key", NULL,
(lua_class_allocator_t) key_new, NULL,
luaA_class_index_miss_property, luaA_class_newindex_miss_property,
key_methods, key_meta);
luaA_class_add_property(&key_class, A_TK_KEY,

View File

@ -417,7 +417,8 @@ tag_class_setup(lua_State *L)
{ NULL, NULL },
};
luaA_class_setup(L, &tag_class, "tag", (lua_class_allocator_t) tag_new, NULL,
luaA_class_setup(L, &tag_class, "tag", NULL,
(lua_class_allocator_t) tag_new, NULL,
luaA_class_index_miss_property, luaA_class_newindex_miss_property,
tag_methods, tag_meta);
luaA_class_add_property(&tag_class, A_TK_NAME,

View File

@ -122,7 +122,8 @@ timer_class_setup(lua_State *L)
{ NULL, NULL },
};
luaA_class_setup(L, &timer_class, "timer", (lua_class_allocator_t) timer_new, NULL,
luaA_class_setup(L, &timer_class, "timer", NULL,
(lua_class_allocator_t) timer_new, NULL,
luaA_class_index_miss_property, luaA_class_newindex_miss_property,
timer_methods, timer_meta);
luaA_class_add_property(&timer_class, A_TK_TIMEOUT,

View File

@ -1389,7 +1389,8 @@ wibox_class_setup(lua_State *L)
{ NULL, NULL },
};
luaA_class_setup(L, &wibox_class, "wibox", (lua_class_allocator_t) wibox_new, NULL,
luaA_class_setup(L, &wibox_class, "wibox", NULL,
(lua_class_allocator_t) wibox_new, NULL,
luaA_class_index_miss_property, luaA_class_newindex_miss_property,
wibox_methods, wibox_meta);
luaA_class_add_property(&wibox_class, A_TK_WIDGETS,

View File

@ -578,7 +578,8 @@ widget_class_setup(lua_State *L)
{ NULL, NULL }
};
luaA_class_setup(L, &widget_class, "widget", (lua_class_allocator_t) widget_new, NULL,
luaA_class_setup(L, &widget_class, "widget", NULL,
(lua_class_allocator_t) widget_new, NULL,
NULL, NULL,
widget_methods, widget_meta);
luaA_class_add_property(&widget_class, A_TK_VISIBLE,

View File

@ -507,8 +507,20 @@ luaA_screen_index(lua_State *L)
const char *buf;
screen_t *s;
if(luaA_usemetatable(L, 1, 2))
/* Get metatable of the screen. */
lua_getmetatable(L, 1);
/* Get the field */
lua_pushvalue(L, 2);
lua_rawget(L, -2);
/* Do we have a field like that? */
if(!lua_isnil(L, -1))
{
/* Yes, so return it! */
lua_remove(L, -2);
return 1;
}
/* No, so remove everything. */
lua_pop(L, 2);
buf = luaL_checklstring(L, 2, &len);
s = lua_touserdata(L, 1);