diff options
author | Andreas Baumann <mail@andreasbaumann.cc> | 2014-10-01 20:30:01 +0200 |
---|---|---|
committer | Andreas Baumann <mail@andreasbaumann.cc> | 2014-10-01 20:30:01 +0200 |
commit | ee52b3eab8cc7feb49fa6db964b94b35e2bc8bac (patch) | |
tree | 19836bc002121846a897e1d54014d2bed8278422 /src/libluaglue | |
parent | a8b82e12caea9f2ef6ea57239be47e31a7bba5c9 (diff) | |
download | crawler-ee52b3eab8cc7feb49fa6db964b94b35e2bc8bac.tar.gz crawler-ee52b3eab8cc7feb49fa6db964b94b35e2bc8bac.tar.bz2 |
reading some Lua globals
Diffstat (limited to 'src/libluaglue')
-rw-r--r-- | src/libluaglue/LuaVM.cpp | 34 |
1 files changed, 31 insertions, 3 deletions
diff --git a/src/libluaglue/LuaVM.cpp b/src/libluaglue/LuaVM.cpp index 9363cac..7c1ce51 100644 --- a/src/libluaglue/LuaVM.cpp +++ b/src/libluaglue/LuaVM.cpp @@ -2,6 +2,7 @@ #include <stdexcept> #include <sstream> +#include <iostream> using namespace std; @@ -22,15 +23,42 @@ void LuaVM::initialize( ) luaL_openlibs( m_lua ); } -void LuaVM::loadSource( const char *filename ) +void LuaVM::loadSource( const char *sourceFilename ) { int res; - res = luaL_loadfile( m_lua, filename ); + 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; + + res = lua_pcall( m_lua, 0, LUA_MULTRET, 0 ); if( res != 0 ) { ostringstream ss; - ss << "Can't read Lua source file from file '" << filename << "': " << lua_tostring( m_lua, -1 ); + 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::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 ); + } +} |