From 57116d6a1208a89cfbf53a70876ec88ba89243ea Mon Sep 17 00:00:00 2001 From: Christian Heinrich Date: Wed, 20 Jan 2016 18:03:35 +0100 Subject: [PATCH] [Lua] Added new 'dump()' debug function for lua This function will help users to easily dump their tables by calling 'simgrid.dump(table)' in Lua. This is not really an elaborate solution (no recursion handling), but it's OK for the moment --- src/bindings/lua/simgrid_lua.c | 37 ++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) diff --git a/src/bindings/lua/simgrid_lua.c b/src/bindings/lua/simgrid_lua.c index a616e6afdc..81cda2a68f 100644 --- a/src/bindings/lua/simgrid_lua.c +++ b/src/bindings/lua/simgrid_lua.c @@ -66,7 +66,44 @@ static int critical(lua_State* L) { return 0; } +/** + * @brief Dumps a lua table with XBT_DEBUG + * + * This function can be called from within lua via "simgrid.dump(table)". It will + * then dump the table via XBT_DEBUG + */ +static int dump(lua_State* L) { + int argc = lua_gettop(L); + + for (int i = 1; i <= argc; i++) { + if (lua_istable(L, i)) { + lua_pushnil(L); /* table nil */ + + //lua_next pops the topmost element from the stack and + //gets the next pair from the table at the specified index + while (lua_next(L, i)) { /* table key val */ + // we need to copy here, as a cast from "Number" to "String" + // could happen in Lua. + // see remark in the lua manual, function "lua_tolstring" + // http://www.lua.org/manual/5.3/manual.html#lua_tolstring + + lua_pushvalue(L, -2); /* table key val key */ + + const char *key = lua_tostring(L, -1); /* table key val key */ + const char *val = lua_tostring(L, -2); /* table key val key */ + + XBT_DEBUG("%s", sglua_keyvalue_tostring(L, -1, -2)); + } + + lua_settop(L, argc); // Remove everything except the initial arguments + } + } + + return 0; +} + static const luaL_Reg simgrid_functions[] = { + {"dump", dump}, {"debug", debug}, {"info", info}, {"critical", critical}, -- 2.20.1