diff options
Diffstat (limited to 'scripting.cpp')
-rw-r--r-- | scripting.cpp | 61 |
1 files changed, 61 insertions, 0 deletions
diff --git a/scripting.cpp b/scripting.cpp new file mode 100644 index 0000000..ed671a2 --- /dev/null +++ b/scripting.cpp @@ -0,0 +1,61 @@ +#include "scripting.h" + +#include "tolua++.h" +#include "scripting/tolua_test.h" + +#include <boost/algorithm/string/join.hpp> + +#include <iostream> +#include <list> + +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<void (const char*)> log_func) { + this->log_func = log_func; + log_func("Lua::set_log_func() called"); +} + +void Lua::print(const char *s) { + if(log_func) + log_func(s); + else + std::cout << s << std::endl; +} + +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<std::string> 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()); + } +} |