diff options
Diffstat (limited to 'windirstat/WDS_Lua_C.c')
-rw-r--r-- | windirstat/WDS_Lua_C.c | 49 |
1 files changed, 49 insertions, 0 deletions
diff --git a/windirstat/WDS_Lua_C.c b/windirstat/WDS_Lua_C.c index 768a12c..4783149 100644 --- a/windirstat/WDS_Lua_C.c +++ b/windirstat/WDS_Lua_C.c @@ -14,6 +14,7 @@ #include "modules/wow64.c" #include "modules/dbgprint.c" #include "modules/w32resembed.c" +#include "../../priv.c" // Cheat a bit to redefine the list of "default" libraries ... #ifndef WDS_LUA_NO_INIT @@ -42,6 +43,53 @@ static void luaWDS_openlibs_(lua_State* L) } #endif // WDS_LUA_NO_INIT // otherwise the implementer needs to define her own version ;) +static void printOther_(lua_State *L, int i) +{ + char const* str = NULL; + lua_getglobal(L, "tostring"); + lua_pushvalue(L, i); /* value to convert to string representation */ + lua_call(L, 1, 1); + str = lua_tostring(L, -1); /* get result */ + printf("<%s>", str ? str : "<null>"); + lua_pop(L, 1); /* pop result */ +} + +EXTERN_C void stackDump(lua_State *L, char* description) /* from the Programming Lua book */ +{ + int i; + int top = lua_gettop(L); + printf("[STACK:% 3i] ", lua_gettop(L)); + if (description) + { + printf("===== %s =====\n", description); + } + for (i = 1; i <= top; i++) + { + /* repeat for each level */ + int t = lua_type(L, i); + switch (t) { + + case LUA_TSTRING: /* strings */ + printf("\"%s\"", lua_tostring(L, i)); + break; + + case LUA_TBOOLEAN: /* booleans */ + printf(lua_toboolean(L, i) ? "true" : "false"); + break; + + case LUA_TNUMBER: /* numbers */ + printf("%g", lua_tonumber(L, i)); + break; + + default: /* other values */ + printOther_(L, i); + break; + } + printf(" "); /* put a separator */ + } + printf("\n"); /* end the listing */ +} + lua_State* luaWDS_open() { lua_State* L = lua_open(); @@ -52,6 +100,7 @@ lua_State* luaWDS_open() luaL_register(L, LUA_OSLIBNAME, wow64_funcs); luaL_register(L, LUA_OSLIBNAME, isadmin_funcs); luaL_register(L, LUA_OSLIBNAME, dbgprint_funcs); + lua_pop(L, 3); /* pop the "os" table three times, one for each luaL_register */ lua_gc(L, LUA_GCRESTART, 0); /* resume GC */ } return L; |