?login_element?

Subversion Repositories NedoOS

Rev

Blame | Last modification | View Log | Download | RSS feed

  1. // https://github.com/vinniefalco/LuaBridge
  2. // Copyright 2019, Dmitry Tarakanov
  3. // Copyright 2012, Vinnie Falco <vinnie.falco@gmail.com>
  4. // Copyright 2007, Nathan Reed
  5. // SPDX-License-Identifier: MIT
  6.  
  7. #pragma once
  8.  
  9. #include "LuaBridge/detail/ClassInfo.h"
  10.  
  11. #include <iostream>
  12. #include <string>
  13.  
  14. namespace luabridge {
  15.  
  16. namespace debug {
  17.  
  18. inline void putIndent(std::ostream& stream, unsigned level)
  19. {
  20.     for (unsigned i = 0; i < level; ++i)
  21.     {
  22.         stream << "  ";
  23.     }
  24. }
  25.  
  26. inline void dumpTable(lua_State* L, int index, std::ostream& stream, unsigned level);
  27.  
  28. inline void dumpValue(lua_State* L, int index, std::ostream& stream, unsigned level = 0)
  29. {
  30.     const int type = lua_type(L, index);
  31.     switch (type)
  32.     {
  33.     case LUA_TNIL:
  34.         stream << "nil";
  35.         break;
  36.  
  37.     case LUA_TBOOLEAN:
  38.         stream << (lua_toboolean(L, index) ? "true" : "false");
  39.         break;
  40.  
  41.     case LUA_TNUMBER:
  42.         stream << lua_tonumber(L, index);
  43.         break;
  44.  
  45.     case LUA_TSTRING:
  46.         stream << '"' << lua_tostring(L, index) << '"';
  47.         break;
  48.  
  49.     case LUA_TFUNCTION:
  50.         if (lua_iscfunction(L, index))
  51.         {
  52.             stream << "cfunction@" << lua_topointer(L, index);
  53.         }
  54.         else
  55.         {
  56.             stream << "function@" << lua_topointer(L, index);
  57.         }
  58.         break;
  59.  
  60.     case LUA_TTHREAD:
  61.         stream << "thread@" << lua_tothread(L, index);
  62.         break;
  63.  
  64.     case LUA_TLIGHTUSERDATA:
  65.         stream << "lightuserdata@" << lua_touserdata(L, index);
  66.         break;
  67.  
  68.     case LUA_TTABLE:
  69.         dumpTable(L, index, stream, level);
  70.         break;
  71.  
  72.     case LUA_TUSERDATA:
  73.         stream << "userdata@" << lua_touserdata(L, index);
  74.         break;
  75.  
  76.     default:
  77.         stream << lua_typename(L, type);
  78.         ;
  79.         break;
  80.     }
  81. }
  82.  
  83. inline void dumpTable(lua_State* L, int index, std::ostream& stream, unsigned level)
  84. {
  85.     stream << "table@" << lua_topointer(L, index);
  86.  
  87.     if (level > 0)
  88.     {
  89.         return;
  90.     }
  91.  
  92.     index = lua_absindex(L, index);
  93.     stream << " {";
  94.     lua_pushnil(L); // Initial key
  95.     while (lua_next(L, index))
  96.     {
  97.         stream << "\n";
  98.         putIndent(stream, level + 1);
  99.         dumpValue(L, -2, stream, level + 1); // Key
  100.         stream << ": ";
  101.         dumpValue(L, -1, stream, level + 1); // Value
  102.         lua_pop(L, 1); // Value
  103.     }
  104.     putIndent(stream, level);
  105.     stream << "\n}";
  106. }
  107.  
  108. inline void dumpState(lua_State* L, std::ostream& stream = std::cerr)
  109. {
  110.     int top = lua_gettop(L);
  111.     for (int i = 1; i <= top; ++i)
  112.     {
  113.         stream << "stack #" << i << ": ";
  114.         dumpValue(L, i, stream, 0);
  115.         stream << "\n";
  116.     }
  117. }
  118.  
  119. } // namespace debug
  120.  
  121. } // namespace luabridge
  122.