blob: 520ee9dfd7933fac84f1b20d142e62738a9d3e5f (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
|
#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::set_say_func(boost::function<void (const char*)> 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<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());
}
}
|