summaryrefslogtreecommitdiff
path: root/src/libluaglue/LuaVM.cpp
blob: d3e2122cf0aad3ad0bdc3c844b56787d3d4ca1c2 (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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
#include "LuaVM.hpp"
#include "StringUtils.hpp"

#include <stdexcept>
#include <sstream>
#include <iostream>
#include <string>

using namespace std;

LuaVM::LuaVM( ) : m_lua( 0 )
{
	initialize( );
}

LuaVM::~LuaVM( )
{
	lua_close( m_lua );
}

lua_State *LuaVM::handle( )
{
	return m_lua;
}

void LuaVM::initialize( )
{
	m_lua = luaL_newstate( );
	
	luaL_openlibs( m_lua );
}

void LuaVM::fullGarbageCollect( )
{
	lua_gc( m_lua, LUA_GCCOLLECT, 0 );
}

void LuaVM::loadSource( const char *sourceFilename )
{
	int res;
	
	m_sourceFilename.assign( sourceFilename );
	
	res = luaL_loadfile( m_lua, m_sourceFilename.c_str( ) );
	if( res != 0 ) {
		ostringstream ss;
		ss << "Can't read Lua source file from file '" << m_sourceFilename << "': " << lua_tostring( m_lua, -1 );
		lua_pop( m_lua, 1 );
		throw std::runtime_error( ss.str( ) );
	}
}

void LuaVM::executeMain( )
{
	int res = lua_pcall( m_lua, 0, 0, 0 );
	if( res != 0 ) {
		ostringstream ss;
		ss << "Can't execute main body of Lua source file '" << m_sourceFilename << "': " << lua_tostring( m_lua, -1 );
		lua_pop( m_lua, 1 );
		throw std::runtime_error( ss.str( ) );
	}
}

void LuaVM::executeFunction( const string &f )
{
	//int top = lua_gettop( m_lua );
	lua_getglobal( m_lua, f.c_str( ) );	
	int res = lua_pcall( m_lua, 0, LUA_MULTRET, 0 );
	if( res != 0 ) {
		ostringstream ss;
		ss << "Unable to call Lua function '" << f << "': " << lua_tostring( m_lua, -1 );
		lua_pop( m_lua, 1 );
		throw new std::runtime_error( ss.str( ) );
	}
	//int nresults = lua_gettop( m_lua ) - top;
	
	// TODO: return results
}

void LuaVM::dumpState( )
{
	lua_rawgeti( m_lua, LUA_REGISTRYINDEX, LUA_RIDX_GLOBALS );
	lua_pushnil( m_lua );
	while( lua_next( m_lua, -2 ) ) {
		if( lua_type( m_lua, -2 ) == LUA_TSTRING ) {
			cout << lua_tostring( m_lua, -2 ) << endl;
		}
		lua_pop( m_lua, 1 );
	}
}

string LuaVM::getString( const string &key )
{
	vector<string> parts = split( key, "." );

	string res;
	
	if( parts.size( ) == 1 ) {
		lua_getglobal( m_lua, parts[0].c_str( ) );

		if( lua_isnil( m_lua, -1 ) ) {
			lua_pop( m_lua, 1 );
			return "";
		}

		res = lua_tostring( m_lua, -1 );
		lua_pop( m_lua, 1 );

		return res;
	}
	
	lua_getglobal( m_lua, parts[0].c_str( ) );
	for( size_t i = 1; i <= parts.size( ) - 1; i++ ) {
		if( !lua_istable( m_lua, -1 ) ) {
			lua_pop( m_lua, i );
			ostringstream ss;
			ss << "table expected in '" << key << "' when derefencing field '" << parts[i] << "'";
			throw runtime_error( ss.str( ) );
		}
		lua_getfield( m_lua, -1, parts[i].c_str( ) );
	}
	
	if( lua_isnil( m_lua, -1 ) ) {
		lua_pop( m_lua, parts.size( ) );
		ostringstream ss;
		ss << "key '" << key << "' refers to an empty element";
		throw runtime_error( ss.str( ) );
	}
	
	res = lua_tostring( m_lua, -1 );
	lua_pop( m_lua, parts.size( ) );	
	
	return res;
}