#include "scripting.h" #include "tolua++.h" #include "scripting/tolua_test.h" #include #include #include Lua::Lua() { L = luaL_newstate(); luaL_openlibs(L); tolua_test_open(L); /* store a pointer to 'this' in the lua state */ lua_pushlightuserdata(L, this); lua_setglobal(L, "lua_instance"); } Lua::~Lua() { lua_close(L); } void Lua::set_log_func(boost::function log_func) { this->log_func = log_func; log_func("Lua::set_log_func() called"); } void Lua::set_say_func(boost::function say_func) { this->say_func = say_func; } void Lua::print(const char *s) { if(log_func) log_func(s); else std::cout << s << std::endl; } void Lua::say(const char *s) { say_func(s); } void Lua::dostring(std::string s) { int before = lua_gettop(L); int err = luaL_dostring(L, s.c_str()); if(err == 1) { std::string s= lua_tostring(L, -1); lua_pop(L, 1); throw(LuaError(s)); } else { int after = lua_gettop(L); std::list vals; for(int i = before; i < after; i++) { const char *s = lua_tostring(L, -1); std::string str; if(s == NULL) { str = lua_typename(L, lua_type(L, -1)); } else { str = s; } vals.push_front(str); lua_pop(L, 1); } this->print(boost::algorithm::join(vals, ",").c_str()); } }