diff options
author | Andreas Baumann <mail@andreasbaumann.cc> | 2014-10-11 20:50:18 +0200 |
---|---|---|
committer | Andreas Baumann <mail@andreasbaumann.cc> | 2014-10-11 20:50:18 +0200 |
commit | adcbc6f259e01a08a816d4020b1c6f7a25129154 (patch) | |
tree | ebfeed39c68ac4469bae62ff1bc10d630e648c17 /src | |
parent | 6a5ef462395c8b2e804551464bca132cddd59525 (diff) | |
download | crawler-adcbc6f259e01a08a816d4020b1c6f7a25129154.tar.gz crawler-adcbc6f259e01a08a816d4020b1c6f7a25129154.tar.bz2 |
added dumping of lua stack
Diffstat (limited to 'src')
-rw-r--r-- | src/libluaglue/LuaVM.cpp | 138 |
1 files changed, 127 insertions, 11 deletions
diff --git a/src/libluaglue/LuaVM.cpp b/src/libluaglue/LuaVM.cpp index d3e2122..fb40c85 100644 --- a/src/libluaglue/LuaVM.cpp +++ b/src/libluaglue/LuaVM.cpp @@ -1,9 +1,12 @@ #include "LuaVM.hpp" #include "StringUtils.hpp" +#include "util/IntTypes.hpp" + #include <stdexcept> #include <sstream> #include <iostream> +#include <iomanip> #include <string> using namespace std; @@ -77,7 +80,7 @@ void LuaVM::executeFunction( const string &f ) // TODO: return results } -void LuaVM::dumpState( ) +void LuaVM::dumpGlobals( ) { lua_rawgeti( m_lua, LUA_REGISTRYINDEX, LUA_RIDX_GLOBALS ); lua_pushnil( m_lua ); @@ -89,24 +92,116 @@ void LuaVM::dumpState( ) } } -string LuaVM::getString( const string &key ) +void LuaVM::dumpStackElement( lua_State *l, int i, int indent ) { - vector<string> parts = split( key, "." ); + 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 ); + cout << "LUA_TFUNCTION " << buf; + break; + } + +//#define LUA_TLIGHTUSERDATA 2 +//#define LUA_TUSERDATA 7 +//#define LUA_TTHREAD 8 + + default: + cout << lua_typename( l, type ) << " <unknown>"; + break; + } + + if( indent == 0 ) { + cout << endl; + } +} - string res; +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 ); - return ""; + 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, 1 ); - - return res; + return 1; } lua_getglobal( m_lua, parts[0].c_str( ) ); @@ -127,8 +222,29 @@ string LuaVM::getString( const string &key ) throw runtime_error( ss.str( ) ); } - res = lua_tostring( m_lua, -1 ); - lua_pop( m_lua, parts.size( ) ); + 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; } |