00001 /** @file woshluad.cpp 00002 * @author Alessandro Polo 00003 * @version $Id: woshluad.cpp 4321 2011-02-22 07:29:59Z alex $ 00004 * @brief 00005 * The header for this class can be found in woshlua.h, check that file 00006 * for class description. 00007 ****************************************************************************/ 00008 /* Copyright (c) 2007-2011, WOSH - Wide Open Smart Home 00009 * by Alessandro Polo - OpenSmartHome.com 00010 * All rights reserved. 00011 * 00012 * Redistribution and use in source and binary forms, with or without 00013 * modification, are permitted provided that the following conditions are met: 00014 * * Redistributions of source code must retain the above copyright 00015 * notice, this list of conditions and the following disclaimer. 00016 * * Redistributions in binary form must reproduce the above copyright 00017 * notice, this list of conditions and the following disclaimer in the 00018 * documentation and/or other materials provided with the distribution. 00019 * * Neither the name of the OpenSmartHome.com WOSH nor the 00020 * names of its contributors may be used to endorse or promote products 00021 * derived from this software without specific prior written permission. 00022 * 00023 * THIS SOFTWARE IS PROVIDED BY Alessandro Polo ''AS IS'' AND ANY 00024 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 00025 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 00026 * DISCLAIMED. IN NO EVENT SHALL Alessandro Polo BE LIABLE FOR ANY 00027 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 00028 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 00029 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 00030 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 00031 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 00032 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 00033 ****************************************************************************/ 00034 00035 #include <framework/scripting/lua/woshlua.h> 00036 #include <framework/scripting/lua/luaincludes.h> 00037 #include <core/String.h> 00038 00039 00040 using namespace std; 00041 namespace wosh { 00042 namespace scripting { 00043 namespace lua { 00044 00045 ////////////////////////////////////////////////////////////////////////////////////////////////////////////// 00046 ////////////////////////////////////////////////////////////////////////////////////////////////////////////// 00047 00048 int wosh_lua_d_render_debug( lua_Debug* ar, std::string& output ) { 00049 if ( ar == NULL ) return 0; 00050 output += String::sprintf("Event:\t%d", ar->event); 00051 output += String::sprintf("Name:\t%s", ar->name); 00052 output += String::sprintf("NameWhat:\t%s", ar->namewhat); 00053 output += String::sprintf("Source:\t%s [%s]", ar->source, ar->short_src); 00054 output += String::sprintf("CurrentLine:\t%d", ar->currentline); 00055 output += String::sprintf("UpValues#:\t%d", ar->nups); 00056 output += String::sprintf("LineDefined:\t%d", ar->linedefined); 00057 output += String::sprintf("LastLineDefined:\t%d", ar->lastlinedefined); 00058 return 1; 00059 } 00060 00061 ////////////////////////////////////////////////////////////////////////////////////////////////////////////// 00062 ////////////////////////////////////////////////////////////////////////////////////////////////////////////// 00063 00064 int wosh_lua_d_dump_stack( lua_State* ls, std::string& output, int indent ) { 00065 if ( ls == NULL ) return WRET_ERR_ILLEGAL_USE; 00066 int top = lua_gettop(ls); 00067 if ( indent == 0 ) { 00068 output += "Elements in the Stack: " + String::toString(top) + "\n"; 00069 } 00070 output += "\t"; 00071 for (int i = 1; i <= top; ++i) { 00072 int t = lua_type(ls, i); 00073 switch (t) { 00074 00075 case LUA_TNONE: 00076 output += "NONE\n"; 00077 break; 00078 case LUA_TNIL: 00079 output += "NIL\n"; 00080 break; 00081 case LUA_TBOOLEAN: 00082 output += "BOOLEAN(" + std::string((lua_toboolean(ls, i) ? "true" : "false")) + ")\n"; 00083 break; 00084 case LUA_TLIGHTUSERDATA: 00085 output += "LIGHTUSERDATA(" ")\n"; 00086 break; 00087 case LUA_TNUMBER: 00088 output += "NUMBER(" + String::toString(lua_tonumber(ls, i)) + ")\n"; 00089 break; 00090 case LUA_TSTRING: 00091 output += "STRING(" + std::string(lua_tostring(ls, i)) + ")\n"; 00092 break; 00093 case LUA_TTABLE: 00094 output += "TABLE(" ")\n"; 00095 break; 00096 case LUA_TFUNCTION: 00097 output += "FUNCTION(" ")\n"; 00098 break; 00099 case LUA_TUSERDATA: 00100 output += "USERDATA(" ")\n"; 00101 break; 00102 case LUA_TTHREAD: 00103 output += "THREAD(" ")\n"; 00104 break; 00105 default: // other values 00106 output += "UNKNOWN(" + std::string(lua_typename(ls, t)) + ")\n"; 00107 break; 00108 } 00109 } 00110 return top; 00111 } 00112 00113 ////////////////////////////////////////////////////////////////////////////////////////////////////////////// 00114 ////////////////////////////////////////////////////////////////////////////////////////////////////////////// 00115 00116 int wosh_lua_d_dump_global( lua_State* ls, std::string& output ) { 00117 if ( ls == NULL ) return 0; 00118 int iErr = 0; 00119 lua_Debug ar; 00120 //http://pgl.yoyo.org/luai/i/lua_getinfo 00121 if ( iErr != 0 ) { 00122 // Log(LOG_CRITICAL, "load() : FAILED: %s", lua_tostring(ls, -1)); 00123 return WRET_ERR_PARAM; 00124 } 00125 wosh_lua_d_render_debug( &ar, output ); 00126 return 1; 00127 } 00128 00129 ////////////////////////////////////////////////////////////////////////////////////////////////////////////// 00130 ////////////////////////////////////////////////////////////////////////////////////////////////////////////// 00131 00132 int wosh_lua_d_dump_local( lua_State* ls, int n, std::string& name, std::string& output ) { 00133 if ( ls == NULL ) return 0; 00134 lua_Debug ar; 00135 const char* nname = lua_getlocal(ls, &ar, n); 00136 if ( nname != NULL ) name.assign(nname); 00137 int iErr = wosh_lua_d_render_debug( &ar, output ); 00138 return iErr; 00139 } 00140 00141 ////////////////////////////////////////////////////////////////////////////////////////////////////////////// 00142 ////////////////////////////////////////////////////////////////////////////////////////////////////////////// 00143 00144 //lua_Hook lua_gethook (lua_State *L); 00145 //int lua_gethookcount (lua_State *L); 00146 //int lua_gethookmask (lua_State *L); 00147 00148 ////////////////////////////////////////////////////////////////////////////////////////////////////////////// 00149 ////////////////////////////////////////////////////////////////////////////////////////////////////////////// 00150 00151 //const char *lua_getupvalue (lua_State *L, int funcindex, int n); 00152 00153 ////////////////////////////////////////////////////////////////////////////////////////////////////////////// 00154 ////////////////////////////////////////////////////////////////////////////////////////////////////////////// 00155 00156 }; // namespace lua 00157 }; // namespace scripting 00158 }; // namespace wosh