From 3bb33734a92e86024488adf88dc2a368c8c952b2 Mon Sep 17 00:00:00 2001 From: Jon Bergli Heier Date: Fri, 20 May 2011 14:11:40 +0200 Subject: Basic lua implementation. --- scripting.cpp | 61 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 61 insertions(+) create mode 100644 scripting.cpp (limited to 'scripting.cpp') 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 + +#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::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 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()); + } +} -- cgit v1.2.3