From adcbc6f259e01a08a816d4020b1c6f7a25129154 Mon Sep 17 00:00:00 2001 From: Andreas Baumann Date: Sat, 11 Oct 2014 20:50:18 +0200 Subject: added dumping of lua stack --- README.3rdPARTY | 35 ++++++++++++ include/luaglue/LuaVM.hpp | 11 ++++ src/libluaglue/LuaVM.cpp | 138 ++++++++++++++++++++++++++++++++++++++++++---- 3 files changed, 173 insertions(+), 11 deletions(-) diff --git a/README.3rdPARTY b/README.3rdPARTY index df3e52d..d6376db 100755 --- a/README.3rdPARTY +++ b/README.3rdPARTY @@ -347,3 +347,38 @@ tolua is freely available; you can redistribute it and/or modify it. The software provided hereunder is on an "as is" basis, and the author has no obligation to provide maintenance, support, updates, enhancements, or modifications. + +msinttypes +---------- + +https://code.google.com/p/msinttypes/ + +ISO C9x compliant stdint.h for Microsoft Visual Studio +Based on ISO/IEC 9899:TC2 Committee draft (May 6, 2005) WG14/N1124 + + Copyright (c) 2006-2013 Alexander Chemeris + +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions are met: + + 1. Redistributions of source code must retain the above copyright notice, + this list of conditions and the following disclaimer. + + 2. Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in the + documentation and/or other materials provided with the distribution. + + 3. Neither the name of the product nor the names of its contributors may + be used to endorse or promote products derived from this software + without specific prior written permission. + +THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED +WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF +MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO +EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, +PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; +OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, +WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR +OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF +ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. diff --git a/include/luaglue/LuaVM.hpp b/include/luaglue/LuaVM.hpp index 0877468..488c810 100755 --- a/include/luaglue/LuaVM.hpp +++ b/include/luaglue/LuaVM.hpp @@ -14,17 +14,28 @@ class LuaVM LUAGLUE_DLL_VISIBLE ~LuaVM( ); LUAGLUE_DLL_VISIBLE void loadSource( const char *sourceFilename ); + LUAGLUE_DLL_VISIBLE void executeMain( ); LUAGLUE_DLL_VISIBLE void executeFunction( const std::string &f ); + LUAGLUE_DLL_VISIBLE void dumpState( ); + LUAGLUE_DLL_VISIBLE void dumpGlobals( ); + LUAGLUE_DLL_VISIBLE void dumpStack( ); + LUAGLUE_DLL_VISIBLE void fullGarbageCollect( ); + LUAGLUE_DLL_VISIBLE std::string getString( const std::string &key ); + LUAGLUE_DLL_VISIBLE bool getBoolean( const std::string &key ); LUAGLUE_DLL_VISIBLE lua_State *handle( ); private: void initialize( ); + + int findValue( const std::string &key ); + void dumpStackElement( lua_State *l, int i, int indent = 0 ); + private: lua_State *m_lua; std::string m_sourceFilename; 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 #include #include +#include #include 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 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 << ""; + } + + 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 ) << " "; + 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 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; } -- cgit v1.2.3-54-g00ecf