summaryrefslogtreecommitdiff
path: root/scripting.cpp
diff options
context:
space:
mode:
authorJon Bergli Heier <snakebite@jvnv.net>2011-05-20 14:11:40 +0200
committerJon Bergli Heier <snakebite@jvnv.net>2011-05-20 14:11:40 +0200
commit3bb33734a92e86024488adf88dc2a368c8c952b2 (patch)
treeaee8ab7832b7fef021668d94cbaf7d21d6c9c839 /scripting.cpp
parent7b22d822f9871222fbbe401c9c79d6a624d21331 (diff)
Basic lua implementation.
Diffstat (limited to 'scripting.cpp')
-rw-r--r--scripting.cpp61
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());
+ }
+}