35 #include "LuaComponent.hpp" 47 #define LuaComponent LuaTLSFComponent 49 #define LuaComponent LuaComponent 56 LuaComponent::LuaComponent(std::string name)
57 : TaskContext(name, PreOperational)
59 os::MutexLock lock(m);
62 if(tlsf_rtt_init_mp(tlsf_inf, TLSF_INITIAL_POOLSIZE)) {
63 Logger::log(Logger::Error) <<
"LuaComponent '" << name <<
": failed to create tlsf pool (" 64 << std::hex << TLSF_INITIAL_POOLSIZE <<
"bytes)" << endlog();
68 L = lua_newstate(tlsf_alloc, tlsf_inf);
70 set_context_tlsf_info(tlsf_inf);
76 Logger::log(Logger::Error) <<
"LuaComponent '" << name
77 <<
"': failed to allocate memory for Lua state" << endlog();
81 lua_gc(L, LUA_GCSTOP, 0);
83 lua_gc(L, LUA_GCRESTART, 0);
86 lua_pushcfunction(L, luaopen_rtt);
89 set_context_tc(
this, L);
91 this->addProperty(
"lua_string", lua_string).doc(
"string of lua code to be executed during configureHook");
92 this->addProperty(
"lua_file", lua_file).doc(
"file with lua program to be executed during configuration");
94 this->addOperation(
"exec_file", &LuaComponent::exec_file,
this, OwnThread)
95 .doc(
"load (and run) the given lua script")
96 .arg(
"filename",
"filename of the lua script");
98 this->addOperation(
"exec_str", &LuaComponent::exec_str,
this, OwnThread)
99 .doc(
"evaluate the given string in the lua environment")
100 .arg(
"lua-string",
"string of lua code to evaluate");
103 this->addOperation(
"tlsf_incmem", &LuaComponent::tlsf_incmem,
this, OwnThread)
104 .doc(
"increase the TLSF memory pool")
105 .arg(
"size",
"size in bytes to add to pool");
109 LuaComponent::~LuaComponent()
111 os::MutexLock lock(m);
114 tlsf_rtt_free_mp(tlsf_inf);
120 bool LuaComponent::tlsf_incmem(
unsigned int size)
122 return tlsf_rtt_incmem(tlsf_inf, size);
127 bool LuaComponent::exec_file(
const std::string &file)
129 os::MutexLock lock(m);
130 if (luaL_dofile(L, file.c_str())) {
131 Logger::log(Logger::Error) <<
"LuaComponent '" << this->getName() <<
"': " << lua_tostring(L, -1) << endlog();
137 bool LuaComponent::exec_str(
const std::string &str)
139 os::MutexLock lock(m);
140 if (luaL_dostring(L, str.c_str())) {
141 Logger::log(Logger::Error) <<
"LuaComponent '" << this->getName() <<
"': " << lua_tostring(L, -1) << endlog();
147 bool LuaComponent::configureHook()
149 if(!lua_string.empty())
150 exec_str(lua_string);
152 if(!lua_file.empty())
154 return call_func(L,
"configureHook",
this, 0, 1);
157 bool LuaComponent::activateHook()
159 os::MutexLock lock(m);
160 return call_func(L,
"activateHook",
this, 0, 1);
163 bool LuaComponent::startHook()
165 os::MutexLock lock(m);
166 return call_func(L,
"startHook",
this, 0, 1);
169 void LuaComponent::updateHook()
171 os::MutexLock lock(m);
172 call_func(L,
"updateHook",
this, 0, 0);
175 void LuaComponent::stopHook()
177 os::MutexLock lock(m);
178 call_func(L,
"stopHook",
this, 0, 0);
181 void LuaComponent::cleanupHook()
183 os::MutexLock lock(m);
184 call_func(L,
"cleanupHook",
this, 0, 0);
187 void LuaComponent::errorHook()
189 os::MutexLock lock(m);
190 call_func(L,
"errorHook",
this, 0, 0);
193 LuaStateHandle LuaComponent::getLuaState()
195 return LuaStateHandle(L, m);
This file contains the macros and definitions to create dynamically loadable components.
The Orocos Component Library.