OrocosComponentLibrary  2.8.3
LuaService.cpp
1 /*
2  * Lua scripting plugin
3  */
4 
5 #include "LuaService.hpp"
6 #include <rtt/plugin/ServicePlugin.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 
27  LuaService::LuaService(RTT::TaskContext* tc)
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;
39  if(tlsf_rtt_init_mp(tlsf_inf, TLSF_INITIAL_POOLSIZE)) {
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);
49  register_tlsf_api(L);
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 */
66  lua_pushcfunction(L, luaopen_rtt);
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
137  ORO_SERVICE_NAMED_PLUGIN(LuaService , "LuaTLSF")
138 #else
139  ORO_SERVICE_NAMED_PLUGIN(LuaService , "Lua")
140 #endif
The Orocos Component Library.
Definition: Component.hpp:43
Definition: Category.hpp:10