From 2a252cd4da0a23bc4a0b0ab08fe319ade432970e Mon Sep 17 00:00:00 2001 From: Julien Danjou Date: Tue, 18 Aug 2009 18:06:41 +0200 Subject: [PATCH] luaobject: add signal_object_emit Signed-off-by: Julien Danjou --- common/luaobject.c | 31 +++++++++++++++++++++++++++++++ common/luaobject.h | 2 ++ 2 files changed, 33 insertions(+) diff --git a/common/luaobject.c b/common/luaobject.c index 72fe303ff..116434be6 100644 --- a/common/luaobject.c +++ b/common/luaobject.c @@ -181,6 +181,37 @@ luaA_object_remove_signal(lua_State *L, int oud, lua_remove(L, ud); } +void +signal_object_emit(lua_State *L, signal_array_t *arr, const char *name, int nargs) +{ + signal_t *sigfound = signal_array_getbyid(arr, + a_strhash((const unsigned char *) name)); + + if(sigfound) + { + int nbfunc = sigfound->sigfuncs.len; + luaL_checkstack(L, lua_gettop(L) + nbfunc + nargs + 1, "too much signal"); + /* Push all functions and then execute, because this list can change + * while executing funcs. */ + foreach(func, sigfound->sigfuncs) + luaA_object_push(L, (void *) *func); + + for(int i = 0; i < nbfunc; i++) + { + /* push all args */ + for(int j = 0; j < nargs; j++) + lua_pushvalue(L, - nargs - nbfunc + i); + /* push first function */ + lua_pushvalue(L, - nargs - nbfunc + i); + /* remove this first function */ + lua_remove(L, - nargs - nbfunc - 1 + i); + luaA_dofunction(L, nargs, 0); + } + } + /* remove args */ + lua_pop(L, nargs); +} + /** Emit a signal to an object. * \param L The Lua VM state. * \param oud The object index on the stack. diff --git a/common/luaobject.h b/common/luaobject.h index 9e3b376da..d0c6801c3 100644 --- a/common/luaobject.h +++ b/common/luaobject.h @@ -134,6 +134,8 @@ luaA_object_push(lua_State *L, void *pointer) return 1; } +void signal_object_emit(lua_State *, signal_array_t *, const char *, int); + void luaA_object_add_signal(lua_State *, int, const char *, int); void luaA_object_remove_signal(lua_State *, int, const char *, int); void luaA_object_emit_signal(lua_State *, int, const char *, int);