aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPavel Labath <pavelo@centrum.sk>2011-07-07 22:24:40 (GMT)
committerPavel Labath <pavelo@centrum.sk>2011-11-01 09:57:21 (GMT)
commitb3bd3805961c05776c7f398ce98082effebd8ca2 (patch)
treebe6ae2777f181067d94523e3d2a1870235d06665
parent1477ad91d172bde0e88b3b90ecef882f6a7bf31c (diff)
downloadfluxbox_pavel-b3bd3805961c05776c7f398ce98082effebd8ca2.zip
fluxbox_pavel-b3bd3805961c05776c7f398ce98082effebd8ca2.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); }