lua

A copy of the Lua development repository
Log | Files | Refs | README

commit 04e495403ba66e88abfb5cc4cf1887f094eea57f
parent 62afbc6bfce222ebe745863131f952f6fce5eafb
Author: Roberto Ierusalimschy <roberto@inf.puc-rio.br>
Date:   Mon,  2 Dec 2024 11:18:36 -0300

New function 'lua_printvalue' for internal debugging

Diffstat:
Mltests.c | 40++++++++++++++++++++++++++++++++++++----
Mltests.h | 7+++++++
2 files changed, 43 insertions(+), 4 deletions(-)

diff --git a/ltests.c b/ltests.c @@ -325,6 +325,37 @@ void lua_printobj (lua_State *L, struct GCObject *o) { printobj(G(L), o); } + +void lua_printvalue (TValue *v) { + switch (ttype(v)) { + case LUA_TNUMBER: { + char buff[LUA_N2SBUFFSZ]; + unsigned len = luaO_tostringbuff(v, buff); + buff[len] = '\0'; + printf("%s", buff); + break; + } + case LUA_TSTRING: { + printf("'%s'", getstr(tsvalue(v))); + break; + } + case LUA_TBOOLEAN: { + printf("%s", (!l_isfalse(v) ? "true" : "false")); + break; + } + case LUA_TNIL: { + printf("nil"); + break; + } + default: { + void *p = iscollectable(v) ? gcvalue(v) : NULL; + printf("%s: %p", ttypename(ttype(v)), p); + break; + } + } +} + + static int testobjref (global_State *g, GCObject *f, GCObject *t) { int r1 = testobjref1(g, f, t); if (!r1) { @@ -827,8 +858,9 @@ void lua_printstack (lua_State *L) { int n = lua_gettop(L); printf("stack: >>\n"); for (i = 1; i <= n; i++) { - printf("%3d: %s\n", i, luaL_tolstring(L, i, NULL)); - lua_pop(L, 1); + printf("%3d: ", i); + lua_printvalue(s2v(L->ci->func.p + i)); + printf("\n"); } printf("<<\n"); } @@ -1681,8 +1713,8 @@ static int runC (lua_State *L, lua_State *L1, const char *pc) { else if EQ("printstack") { int n = getnum; if (n != 0) { - printf("%s\n", luaL_tolstring(L1, n, NULL)); - lua_pop(L1, 1); + lua_printvalue(s2v(L->ci->func.p + n)); + printf("\n"); } else lua_printstack(L1); } diff --git a/ltests.h b/ltests.h @@ -79,6 +79,13 @@ LUAI_FUNC int lua_checkmemory (lua_State *L); struct GCObject; LUAI_FUNC void lua_printobj (lua_State *L, struct GCObject *o); + +/* +** Function to print a value +*/ +struct TValue; +LUAI_FUNC void lua_printvalue (struct TValue *v); + /* ** Function to print the stack */