LuaService.cpp
Go to the documentation of this file.
1 /*
2  * Lua scripting plugin
3  */
4 
5 #include "LuaService.hpp"
7 #include <iostream>
8 
9 #include "rtt.hpp"
10 
11 #ifdef LUA_RTT_TLSF
12 extern "C" {
13 #include "tlsf_rtt.h"
14 }
15 #endif
16 
17 #ifdef LUA_RTT_TLSF
18 #define LuaService LuaTLSFService
19 #else
20 #define LuaService LuaService
21 #endif
22 
23 using namespace RTT;
24 
25 namespace OCL {
26 
28 #if LUA_RTT_TLSF
29  : RTT::Service("LuaTLSF", tc)
30 #else
31  : RTT::Service("Lua", tc)
32 #endif
33  {
34  /* initialize lua */
35  os::MutexLock lock(m);
36 
37 #if LUA_RTT_TLSF
38  tlsf_inf = new lua_tlsf_info;
40  Logger::log(Logger::Error) << "LuaService (TLSF)'"
41  << this->getOwner()->getName() << ": failed to create tlsf pool ("
42  << std::hex << TLSF_INITIAL_POOLSIZE << "bytes)" << endlog();
43  throw;
44  }
45 
46  L = lua_newstate(tlsf_alloc, tlsf_inf);
47  tlsf_inf->L = L;
48  set_context_tlsf_info(tlsf_inf);
50 #else
51  L = luaL_newstate();
52 #endif
53 
54  if (L == NULL) {
55  Logger::log(Logger::Error) << "LuaService ctr '" << this->getOwner()->getName() << "': "
56  << "cannot create state: not enough memory" << endlog();
57  throw;
58  }
59 
60 
61  lua_gc(L, LUA_GCSTOP, 0);
62  luaL_openlibs(L);
63  lua_gc(L, LUA_GCRESTART, 0);
64 
65  /* setup rtt bindings */
67  lua_call(L, 0, 0);
68 
69  set_context_tc(tc, L);
70 
71  this->addOperation("exec_file", &LuaService::exec_file, this)
72  .doc("load (and run) the given lua script")
73  .arg("filename", "filename of the lua script");
74 
75  this->addOperation("exec_str", &LuaService::exec_str, this)
76  .doc("evaluate the given string in the lua environment")
77  .arg("lua-string", "string of lua code to evaluate");
78 
79 #ifdef LUA_RTT_TLSF
80  this->addOperation("tlsf_incmem", &LuaService::tlsf_incmem, this, OwnThread)
81  .doc("increase the TLSF memory pool")
82  .arg("size", "size in bytes to add to pool");
83 #endif
84  }
85 
86  // Destructor
87  LuaService::~LuaService()
88  {
89  os::MutexLock lock(m);
90  lua_close(L);
91 #ifdef LUA_RTT_TLSF
92  tlsf_rtt_free_mp(tlsf_inf);
93  delete tlsf_inf;
94 #endif
95  }
96 
97 
98 #ifdef LUA_RTT_TLSF
99  bool LuaService::tlsf_incmem(unsigned int size)
100  {
101  return tlsf_rtt_incmem(tlsf_inf, size);
102  }
103 #endif
104 
105  bool LuaService::exec_file(const std::string &file)
106  {
107  os::MutexLock lock(m);
108  if (luaL_dofile(L, file.c_str())) {
109  Logger::log(Logger::Error) << "LuaService '" << this->getOwner()->getName()
110  << "': " << lua_tostring(L, -1) << endlog();
111  return false;
112  }
113  return true;
114  }
115 
116  bool LuaService::exec_str(const std::string &str)
117  {
118  os::MutexLock lock(m);
119  if (luaL_dostring(L, str.c_str())) {
120  Logger::log(Logger::Error) << "LuaService '" << this->getOwner()->getName()
121  << "': " << lua_tostring(L, -1) << endlog();
122  return false;
123  }
124  return true;
125  }
126 
127  LuaStateHandle LuaService::getLuaState()
128  {
129  return LuaStateHandle(L, m);
130  }
131 
132 } // namespace OCL
133 
134 using namespace OCL;
135 
136 #ifdef LUA_RTT_TLSF
138 #else
140 #endif
#define TLSF_INITIAL_POOLSIZE
Definition: tlsf_rtt.h:6
#define lua_tostring(L, i)
Definition: lua-repl.h:281
int tlsf_rtt_init_mp(struct lua_tlsf_info *tlsf_inf, size_t sz)
Definition: tlsf_rtt.c:24
#define ORO_SERVICE_NAMED_PLUGIN(SERVICE, NAME)
int set_context_tc(TaskContext *tc, lua_State *L)
Definition: rtt.cpp:3139
LUA_API lua_State *() lua_newstate(lua_Alloc f, void *ud)
void set_context_tlsf_info(struct lua_tlsf_info *tlsf_inf)
Definition: tlsf_rtt.c:105
#define LUA_GCRESTART
Definition: lua-repl.h:224
int luaopen_rtt(lua_State *L)
Definition: rtt.cpp:3051
LUA_API int() lua_gc(lua_State *L, int what, int data)
#define LUA_GCSTOP
Definition: lua-repl.h:223
LUA_API void() lua_close(lua_State *L)
void tlsf_rtt_free_mp(struct lua_tlsf_info *tlsf_inf)
Definition: tlsf_rtt.c:50
int tlsf_rtt_incmem(struct lua_tlsf_info *tlsf_inf, size_t sz)
Definition: tlsf_rtt.c:88
static Logger & log()
#define lua_pushcfunction(L, f)
Definition: lua-repl.h:262
void register_tlsf_api(lua_State *L)
Definition: tlsf_rtt.c:185
LUA_API void() lua_call(lua_State *L, int nargs, int nresults)
#define LuaService
Definition: LuaService.cpp:20
void * tlsf_alloc(void *ud, void *ptr, size_t osize, size_t nsize)
Definition: tlsf_rtt.c:68
static Logger::LogFunction endlog()


ocl
Author(s): OCL Development Team
autogenerated on Wed Jun 26 2019 19:26:27