From 45702de15813eef317b085380ff6a3cdc2350e23 Mon Sep 17 00:00:00 2001 From: Julien Danjou Date: Thu, 25 Jun 2009 11:21:51 +0200 Subject: [PATCH] luaclass: add support for new() Signed-off-by: Julien Danjou --- common/luaclass.c | 40 ++++++++++++++++++++++++++++++++++++++++ common/luaclass.h | 1 + common/lualib.h | 6 ++++++ luaa.h | 15 --------------- 4 files changed, 47 insertions(+), 15 deletions(-) diff --git a/common/luaclass.c b/common/luaclass.c index a6006a3a..6c769348 100644 --- a/common/luaclass.c +++ b/common/luaclass.c @@ -302,4 +302,44 @@ luaA_class_newindex(lua_State *L) return 0; } +/** Generic constructor function for objects. + * \param L The Lua VM state. + * \return The number of elements pushed on stack. + */ +int +luaA_class_new(lua_State *L, lua_class_t *lua_class) +{ + /* Check we have a table that should contains some properties */ + luaA_checktable(L, 2); + + /* Create a new object */ + void *object = lua_class->allocator(L); + + /* Push the first key before iterating */ + lua_pushnil(L); + /* Iterate over the property keys */ + while(lua_next(L, 2)) + { + /* Check that the key is a string. + * We cannot call tostring blindly or Lua will convert a key that is a + * 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)); + + if(prop && prop->new) + prop->new(L, object); + } + /* Remove value */ + lua_pop(L, 1); + } + + return 1; +} + // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80 diff --git a/common/luaclass.h b/common/luaclass.h index 5d01ca17..3f1377cf 100644 --- a/common/luaclass.h +++ b/common/luaclass.h @@ -72,6 +72,7 @@ void luaA_class_add_property(lua_class_t *, awesome_token_t, const char *, int luaA_usemetatable(lua_State *, int, int); int luaA_class_index(lua_State *); int luaA_class_newindex(lua_State *); +int luaA_class_new(lua_State *, lua_class_t *); #define LUA_CLASS_FUNCS(prefix, lua_class) \ static inline int \ diff --git a/common/lualib.h b/common/lualib.h index 8b6cdfd3..c611b90c 100644 --- a/common/lualib.h +++ b/common/lualib.h @@ -141,6 +141,12 @@ luaA_toudata(lua_State *L, int ud, const char *tname) return p; } +#define luaA_checktable(L, n) \ + do { \ + if(!lua_istable(L, n)) \ + luaL_typerror(L, n, "table"); \ + } while(0) + #endif // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80 diff --git a/luaa.h b/luaa.h index 0912c85a..8adf7e4d 100644 --- a/luaa.h +++ b/luaa.h @@ -35,21 +35,6 @@ luaA_warn(L, "%s: This function is deprecated and will be removed, see %s", \ __FUNCTION__, repl) -#define DO_LUA_TOSTRING(type, prefix, lua_type) \ - static int \ - luaA_##prefix##_tostring(lua_State *L) \ - { \ - type *p = luaL_checkudata(L, 1, lua_type); \ - lua_pushfstring(L, lua_type ": %p", p); \ - return 1; \ - } - -#define luaA_checktable(L, n) \ - do { \ - if(!lua_istable(L, n)) \ - luaL_typerror(L, n, "table"); \ - } while(0) - #define luaA_checkscreen(screen) \ do { \ if(screen < 0 || screen >= globalconf.screens.len) \