summaryrefslogtreecommitdiff
path: root/src/libluaglue/LuaVM.cpp
blob: 566e5166530261e8f5c3c3b3292db380c379ae16 (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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
#include "LuaVM.hpp"
#include "StringUtils.hpp"

#ifdef _WIN32
#define snprintf _snprintf
#endif
#define __STDC_FORMAT_MACROS
#include "util/IntTypes.hpp"

#include <stdexcept>
#include <sstream>
#include <iostream>
#include <iomanip>
#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 std::runtime_error( ss.str( ) );
	}
	//int nresults = lua_gettop( m_lua ) - top;
	
	// TODO: return results
}

void LuaVM::dumpGlobals( )
{
	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 );
	}
}

void LuaVM::dumpStackElement( lua_State *l, int i, int indent )
{
	int type = lua_type( l, i );

	if( indent == 0 ) {
		cout << setfill( '0' ) << setw( 2 ) << i << ": ";
	}
		
	switch( type ) {
		case LUA_TNIL:
			cout << "LUA_TNIL nil";
			break;
			
		case LUA_TBOOLEAN:
			cout << "LUA_TBOOLEAN " << ( lua_toboolean( l, i ) ? "true" : "false" );
			break;

		case LUA_TNUMBER:
			cout << "LUA_TNUMBER " << lua_tonumber( l, i ) << "";
			break;

		case LUA_TSTRING:
			cout << "LUA_TSTRING '" << lua_tostring( l, i ) << "'";
			break;
		
		case LUA_TTABLE:
			cout << "LUA_TTABLE { ";
			
			lua_pushvalue( l, i );
			lua_pushnil( l );
			while( lua_next( l, -2 ) ) {
				lua_pushvalue( l, -2 );
				if( lua_isstring( l, -1 ) ) {
					cout << lua_tostring( l, -1 );
				} else {
					cout << "<unknown key type "
						<< lua_typename( l, lua_type( l, -1 ) ) << ">";
				}
				
				cout << " = ";
				
				dumpStackElement( l, -2, indent + 1 );
				
				cout << ", ";
				
				lua_pop( l, 2 );
			}
			lua_pop( l, 1 );
				
			cout << " }";
			
			break;
			
		case LUA_TFUNCTION: {
			lua_CFunction f = lua_tocfunction( l, i );
			char buf[33];
			snprintf( buf, 32, "function[%016" PRIxPTR "]", (uintptr_t)f );
			// TODO: int lua_getinfo (lua_State *L, const char *what, lua_Debug *ar);
			cout << "LUA_TFUNCTION " << buf;
			break;
		}
		
		case LUA_TLIGHTUSERDATA: {
			void *p = lua_touserdata( l, i );
			char buf[33];
			snprintf( buf, 32, "void *[%016" PRIxPTR "]", (uintptr_t)p );
			cout << "LUA_TLIGHTUSERDATA " << buf;
			break;
		}
		
		case LUA_TUSERDATA: {
			void *p = lua_touserdata( l, i );
			char buf[33];
			snprintf( buf, 32, "void *[%016" PRIxPTR "]", (uintptr_t)p );
			// TODO: is there a tostring entry in the metatable?
			cout << "LUA_TUSERDATA " << buf;
			break;
		}
			
		case LUA_TTHREAD: {
			//lua_State *thread = lua_tothread( l, i );
			// TODO: more information like status of the thread?
			cout << "LUA_TTHREAD ";
			break;
		}
								
		default:
			cout << lua_typename( l, type ) << " <unknown>";
			break;
	}
	
	if( indent == 0 ) {
		cout << endl;
	}
}

void LuaVM::dumpStack( )
{
	cout << "-- stack --" << endl;
	
	int top = lua_gettop( m_lua );
	for( int i = 1; i <= top; i++ ) {
		dumpStackElement( m_lua, i );		
	}

	cout << "-- end of stack --" << endl;
}

void LuaVM::dumpState( )
{
	cout << "globals:" << endl;
	dumpGlobals( );
	cout << "stack:" << endl;
	dumpStack( );
}
    
int LuaVM::findValue( const string &key )
{
	vector<string> parts = split( key, "." );

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

		if( lua_isnil( m_lua, -1 ) ) {
			lua_pop( m_lua, 1 );
			ostringstream ss;
			ss << "key '" << key << "' refers to an empty element";
			throw runtime_error( ss.str( ) );
		}

		return 1;
	}
	
	lua_getglobal( m_lua, parts[0].c_str( ) );
	for( int i = 1; i <= (int)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, (int)parts.size( ) );
		ostringstream ss;
		ss << "key '" << key << "' refers to an empty element";
		throw runtime_error( ss.str( ) );
	}
	
	return parts.size( );
}

string LuaVM::getString( const string &key )
{
	int n = findValue( key );	

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

	//dumpStack( );
	
	return res;
}

bool LuaVM::getBoolean( const string &key )
{
	int n = findValue( key );	

	bool res = lua_toboolean( m_lua, -1 );
	lua_pop( m_lua, n );	

	//dumpStack( );
	
	return res;
}

vector<string> LuaVM::getStringArray( const string &key )
{
	vector<string> res;
	
	int n = findValue( key );
	
	if( !lua_istable( m_lua, -1 ) ) {
		lua_pop( m_lua, n );
		ostringstream ss;
		ss << "key '" << key << "' refers not to a table";
		throw runtime_error( ss.str( ) );
	}
	
	//dumpStack( );
	
	lua_pushvalue( m_lua, -1 );
	lua_pushnil( m_lua );
	while( lua_next( m_lua, -2 ) ) {
		res.push_back( lua_tostring( m_lua, -1 ) );
		lua_pop( m_lua, 1 );
	}
	lua_pop( m_lua, 1 );
		
	lua_pop( m_lua, n );
	
	return res;
}