From b3bd3805961c05776c7f398ce98082effebd8ca2 Mon Sep 17 00:00:00 2001 From: Pavel Labath Date: Fri, 8 Jul 2011 00:24:40 +0200 Subject: A lightweight version of lua::state::pushclosure which handles the simple cases --- src/FbTk/Luamm.cc | 12 ++++++++++-- src/FbTk/Luamm.hh | 3 ++- 2 files changed, 12 insertions(+), 3 deletions(-) diff --git a/src/FbTk/Luamm.cc b/src/FbTk/Luamm.cc index 563be9c..1ef751f 100644 --- a/src/FbTk/Luamm.cc +++ b/src/FbTk/Luamm.cc @@ -59,6 +59,7 @@ namespace lua { lua_rawset(l, index); } + template int closure_trampoline(lua_State *l) { lua_checkstack(l, 2); @@ -68,7 +69,7 @@ namespace lua { lua_pop(l, 1); try { - Slot *fn = static_cast( L->touserdata(lua_upvalueindex(1)) ); + Functor *fn = reinterpret_cast( L->touserdata(lua_upvalueindex(1)) ); assert(fn); return (*fn)(L); } @@ -390,7 +391,14 @@ namespace lua { setmetatable(-2); insert(-n-1); - lua_pushcclosure(cobj, &closure_trampoline, n+1); + lua_pushcclosure(cobj, &closure_trampoline, n+1); + } + + void state::pushclosure(int (*fn)(state *), int n) + { + pushlightuserdata(reinterpret_cast(fn)); + insert(-n-1); + lua_pushcclosure(cobj, &closure_trampoline, n+1); } void state::rawgetfield(int index, const char *k) throw(std::bad_alloc) diff --git a/src/FbTk/Luamm.hh b/src/FbTk/Luamm.hh index 05cb569..96a5eea 100644 --- a/src/FbTk/Luamm.hh +++ b/src/FbTk/Luamm.hh @@ -237,9 +237,10 @@ namespace lua { void newtable() { lua_newtable(cobj); } void *newuserdata(size_t size) { return lua_newuserdata(cobj, size); } // Functor can be anything that FbTk::Slot can handle, everything else remains - // identical + // identical. We also provide a specialized, lightweight version for simple functors template void pushclosure(const Functor &fn, int n); + void pushclosure(int (*fn)(state *), int n); template void pushfunction(const Functor &fn) { pushclosure(fn, 0); } void pushstring(const char *s) { lua_pushstring(cobj, s); } -- cgit v0.11.2