From 8eed5e7bcf44d598814992536547d707767062df Mon Sep 17 00:00:00 2001 From: Uli Schlachter Date: Sat, 6 Dec 2014 18:16:05 +0100 Subject: [PATCH] client: Include c.name in the result of tostring(c) Signed-off-by: Uli Schlachter --- common/luaclass.c | 1 + common/luaclass.h | 7 +++++++ common/luaobject.c | 24 ++++++++++++++++++------ objects/client.c | 14 ++++++++++++++ 4 files changed, 40 insertions(+), 6 deletions(-) diff --git a/common/luaclass.c b/common/luaclass.c index 68be33065..ac7c8178e 100644 --- a/common/luaclass.c +++ b/common/luaclass.c @@ -244,6 +244,7 @@ luaA_class_setup(lua_State *L, lua_class_t *class, class->newindex_miss_property = newindex_miss_property; class->checker = checker; class->parent = parent; + class->tostring = NULL; class->instances = 0; signal_add(&class->signals, "new"); diff --git a/common/luaclass.h b/common/luaclass.h index ad9197347..a057108aa 100644 --- a/common/luaclass.h +++ b/common/luaclass.h @@ -72,6 +72,8 @@ struct lua_class_t lua_class_checker_t checker; /** Number of instances of this class in lua */ unsigned int instances; + /** Class tostring method */ + lua_class_propfunc_t tostring; }; const char * luaA_typename(lua_State *, int); @@ -100,6 +102,11 @@ 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 *); +static inline void luaA_class_set_tostring(lua_class_t *class, lua_class_propfunc_t callback) +{ + class->tostring = callback; +} + static inline void * luaA_checkudataornil(lua_State *L, int udx, lua_class_t *class) { diff --git a/common/luaobject.c b/common/luaobject.c index 118be3a3a..a6d9a8eb5 100644 --- a/common/luaobject.c +++ b/common/luaobject.c @@ -317,22 +317,34 @@ luaA_object_tostring(lua_State *L) { lua_class_t *lua_class = luaA_class_get(L, 1); lua_object_t *object = luaA_checkudata(L, 1, lua_class); + int offset = 0; - int i = 0; - for(; lua_class; lua_class = lua_class->parent, i++) + for(; lua_class; lua_class = lua_class->parent) { - if(i) + if(offset) { lua_pushliteral(L, "/"); - lua_insert(L, - (i * 2)); + lua_insert(L, -++offset); } lua_pushstring(L, NONULL(lua_class->name)); - lua_insert(L, - (i * 2) - 1); + lua_insert(L, -++offset); + + if (lua_class->tostring) { + int k, n; + + lua_pushliteral(L, "("); + n = 2 + lua_class->tostring(L, object); + lua_pushliteral(L, ")"); + + for (k = 0; k < n; k++) + lua_insert(L, -offset); + offset += n; + } } lua_pushfstring(L, ": %p", object); - lua_concat(L, i * 2); + lua_concat(L, offset + 1); return 1; } diff --git a/objects/client.c b/objects/client.c index 138d8a46e..81b853d0f 100644 --- a/objects/client.c +++ b/objects/client.c @@ -2156,6 +2156,19 @@ luaA_client_keys(lua_State *L) return luaA_key_array_get(L, 1, keys); } +static int +client_tostring(lua_State *L, client_t *c) +{ + char *name = c->name ? c->name : c->alt_name; + ssize_t len = a_strlen(name); + ssize_t limit = 20; + + lua_pushlstring(L, name, MIN(len, limit)); + if (len > limit) + lua_pushstring(L, "..."); + return len > limit ? 2 : 1; +} + /* Client module. * \param L The Lua VM state. * \return The number of pushed elements. @@ -2233,6 +2246,7 @@ client_class_setup(lua_State *L) (lua_class_checker_t) client_checker, luaA_class_index_miss_property, luaA_class_newindex_miss_property, client_methods, client_meta); + luaA_class_set_tostring(&client_class, (lua_class_propfunc_t) client_tostring); luaA_class_add_property(&client_class, "name", NULL, (lua_class_propfunc_t) luaA_client_get_name,