diff mbox series

[v2,4/5] Lua: Dump tables in Lua stack recursive

Message ID 1516199646-5607-4-git-send-email-stefan@herbrechtsmeier.net
State Accepted
Headers show
Series [v2,1/5] dict: Rename dictionary struct and its key to distinguish it from simple lists | expand

Commit Message

Stefan Herbrechtsmeier Jan. 17, 2018, 2:34 p.m. UTC
From: Stefan Herbrechtsmeier <stefan.herbrechtsmeier@weidmueller.com>

Signed-off-by: Stefan Herbrechtsmeier <stefan.herbrechtsmeier@weidmueller.com>

---

Changes in v3:
- Replace -1 by ENOMEM_ASPRINTF define
- Move declarations into the required blocks

Changes in v2: None

 corelib/lua_interface.c | 89 +++++++++++++++++++++++++++++--------------------
 1 file changed, 53 insertions(+), 36 deletions(-)

Comments

Stefano Babic Jan. 19, 2018, 9:35 a.m. UTC | #1
On 17/01/2018 15:34, stefan@herbrechtsmeier.net wrote:
> From: Stefan Herbrechtsmeier <stefan.herbrechtsmeier@weidmueller.com>
> 
> Signed-off-by: Stefan Herbrechtsmeier <stefan.herbrechtsmeier@weidmueller.com>
> 
> ---
> 

Applied to -master, thanks !

Best regards,
Stefano Babic
diff mbox series

Patch

diff --git a/corelib/lua_interface.c b/corelib/lua_interface.c
index b617f61..6b92cf0 100644
--- a/corelib/lua_interface.c
+++ b/corelib/lua_interface.c
@@ -61,6 +61,52 @@  static bool is_type(lua_State *L, uintptr_t type)
 }
 #endif
 
+static void lua_dump_table(lua_State *L, char *str)
+{
+	/* Stack: table, ... */
+	lua_pushnil(L);
+	/* Stack: nil, table, ... */
+	while (lua_next(L, -2)) {
+		/* Stack: value, key, table, ... */
+		lua_pushvalue(L, -2);
+		/* Stack: key, value, key, table, ... */
+		switch(lua_type(L, -2)) {
+			case LUA_TSTRING:
+			case LUA_TNUMBER:
+				TRACE("%s %s = %s", str,
+					lua_tostring(L, -1),
+					lua_tostring(L, -2));
+				break;
+			case LUA_TFUNCTION:
+				TRACE("%s %s()", str,
+					lua_tostring(L, -1));
+				break;
+			case LUA_TTABLE: {
+				char *s;
+
+				if (asprintf(&s, "%s %s:", str, lua_tostring(L, -1)) != ENOMEM_ASPRINTF) {
+					lua_pushvalue(L, -2);
+					lua_dump_table(L, s);
+					lua_pop(L, 1);
+					free(s);
+				}
+				break;
+			}
+			case LUA_TBOOLEAN:
+				TRACE("%s %s = %s", str,
+					lua_tostring(L, -1),
+					(lua_toboolean(L, -2) ? "true" : "false"));
+				break;
+			default:
+				TRACE("%s %s = <unparsed type>", str,
+					lua_tostring(L, -1));
+		}
+		lua_pop(L, 2);
+		/* Stack: key, table, ... */
+	}
+	/* Stack: table, ... */
+}
+
 void LUAstackDump(lua_State *L)
 {
 	int top = lua_gettop(L);
@@ -84,43 +130,14 @@  void LUAstackDump(lua_State *L)
 				break;
 			}
 			case LUA_TTABLE: {
-				lua_pushvalue(L, -1);
-				lua_pushnil(L);
-				/* Stack: nil, table */
-				while (lua_next(L, -2)) {
-					/* Stack: value, key, table */
-					lua_pushvalue(L, -2);
-					/* Stack: key, value, key, table */
-					switch(lua_type(L, -2)) {
-						case LUA_TSTRING:
-						case LUA_TNUMBER:
-							TRACE("(%d) [table ] %s = %s", i,
-								lua_tostring(L, -1),
-								lua_tostring(L, -2));
-							break;
-						case LUA_TFUNCTION:
-							TRACE("(%d) [table ] %s()", i,
-								lua_tostring(L, -1));
-							break;
-						case LUA_TTABLE:
-							TRACE("(%d) [table ] %s <table>", i,
-								lua_tostring(L, -1));
-							break;
-						case LUA_TBOOLEAN:
-							TRACE("(%d) [table ] %s = %s", i,
-								lua_tostring(L, -1),
-								(lua_toboolean(L, -2) ? "true" : "false"));
-							break;
-						default:
-							TRACE("(%d) [table ] %s = <unparsed type>", i,
-								lua_tostring(L, -1));
-					}
-					lua_pop(L, 2);
-					/* Stack: key, table */
+				char *s;
+
+				if (asprintf(&s, "(%d) [table ]", i) != ENOMEM_ASPRINTF) {
+					lua_pushvalue(L, -1);
+					lua_dump_table(L, s);
+					lua_pop(L, 1);
+					free(s);
 				}
-				/* Stack: table */
-				lua_pop(L, 1);
-				/* Stack: <empty> */
 				break;
 			}
 			default: {