aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPavel Labath <pavelo@centrum.sk>2011-07-07 22:24:40 (GMT)
committerPaul Tagliamonte <paultag@fluxbox.org>2012-04-07 02:11:30 (GMT)
commit42b54c31ed07df2e3d68bcff8882104c5f7bd50b (patch)
treeaa6beff02a275fe065c9413b5ab727783f5c1f57
parent13aab4e8e2ba6cccbadfd2e2a9f5cf94e1806c0b (diff)
downloadfluxbox_paul-42b54c31ed07df2e3d68bcff8882104c5f7bd50b.zip
fluxbox_paul-42b54c31ed07df2e3d68bcff8882104c5f7bd50b.tar.bz2
A lightweight version of lua::state::pushclosure which handles the simple cases
-rw-r--r--src/FbTk/Luamm.cc12
-rw-r--r--src/FbTk/Luamm.hh3
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 {
59 lua_rawset(l, index); 59 lua_rawset(l, index);
60 } 60 }
61 61
62 template<typename Functor>
62 int closure_trampoline(lua_State *l) 63 int closure_trampoline(lua_State *l)
63 { 64 {
64 lua_checkstack(l, 2); 65 lua_checkstack(l, 2);
@@ -68,7 +69,7 @@ namespace lua {
68 lua_pop(l, 1); 69 lua_pop(l, 1);
69 70
70 try { 71 try {
71 Slot *fn = static_cast<Slot *>( L->touserdata(lua_upvalueindex(1)) ); 72 Functor *fn = reinterpret_cast<Functor *>( L->touserdata(lua_upvalueindex(1)) );
72 assert(fn); 73 assert(fn);
73 return (*fn)(L); 74 return (*fn)(L);
74 } 75 }
@@ -390,7 +391,14 @@ namespace lua {
390 setmetatable(-2); 391 setmetatable(-2);
391 392
392 insert(-n-1); 393 insert(-n-1);
393 lua_pushcclosure(cobj, &closure_trampoline, n+1); 394 lua_pushcclosure(cobj, &closure_trampoline<Slot>, n+1);
395 }
396
397 void state::pushclosure(int (*fn)(state *), int n)
398 {
399 pushlightuserdata(reinterpret_cast<void *>(fn));
400 insert(-n-1);
401 lua_pushcclosure(cobj, &closure_trampoline<int (state *)>, n+1);
394 } 402 }
395 403
396 void state::rawgetfield(int index, const char *k) throw(std::bad_alloc) 404 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 {
237 void newtable() { lua_newtable(cobj); } 237 void newtable() { lua_newtable(cobj); }
238 void *newuserdata(size_t size) { return lua_newuserdata(cobj, size); } 238 void *newuserdata(size_t size) { return lua_newuserdata(cobj, size); }
239 // Functor can be anything that FbTk::Slot can handle, everything else remains 239 // Functor can be anything that FbTk::Slot can handle, everything else remains
240 // identical 240 // identical. We also provide a specialized, lightweight version for simple functors
241 template<typename Functor> 241 template<typename Functor>
242 void pushclosure(const Functor &fn, int n); 242 void pushclosure(const Functor &fn, int n);
243 void pushclosure(int (*fn)(state *), int n);
243 template<typename Functor> 244 template<typename Functor>
244 void pushfunction(const Functor &fn) { pushclosure(fn, 0); } 245 void pushfunction(const Functor &fn) { pushclosure(fn, 0); }
245 void pushstring(const char *s) { lua_pushstring(cobj, s); } 246 void pushstring(const char *s) { lua_pushstring(cobj, s); }