diff options
author | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2024-12-02 11:19:03 -0300 |
---|---|---|
committer | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2024-12-02 11:19:03 -0300 |
commit | 04e495403ba66e88abfb5cc4cf1887f094eea57f (patch) | |
tree | 6054afd488012323412027fad6d8f5c74771e1c2 | |
parent | 62afbc6bfce222ebe745863131f952f6fce5eafb (diff) | |
download | lua-04e495403ba66e88abfb5cc4cf1887f094eea57f.tar.gz lua-04e495403ba66e88abfb5cc4cf1887f094eea57f.tar.bz2 lua-04e495403ba66e88abfb5cc4cf1887f094eea57f.zip |
New function 'lua_printvalue' for internal debugging
-rw-r--r-- | ltests.c | 40 | ||||
-rw-r--r-- | ltests.h | 7 |
2 files changed, 43 insertions, 4 deletions
@@ -325,6 +325,37 @@ void lua_printobj (lua_State *L, struct GCObject *o) { | |||
325 | printobj(G(L), o); | 325 | printobj(G(L), o); |
326 | } | 326 | } |
327 | 327 | ||
328 | |||
329 | void lua_printvalue (TValue *v) { | ||
330 | switch (ttype(v)) { | ||
331 | case LUA_TNUMBER: { | ||
332 | char buff[LUA_N2SBUFFSZ]; | ||
333 | unsigned len = luaO_tostringbuff(v, buff); | ||
334 | buff[len] = '\0'; | ||
335 | printf("%s", buff); | ||
336 | break; | ||
337 | } | ||
338 | case LUA_TSTRING: { | ||
339 | printf("'%s'", getstr(tsvalue(v))); | ||
340 | break; | ||
341 | } | ||
342 | case LUA_TBOOLEAN: { | ||
343 | printf("%s", (!l_isfalse(v) ? "true" : "false")); | ||
344 | break; | ||
345 | } | ||
346 | case LUA_TNIL: { | ||
347 | printf("nil"); | ||
348 | break; | ||
349 | } | ||
350 | default: { | ||
351 | void *p = iscollectable(v) ? gcvalue(v) : NULL; | ||
352 | printf("%s: %p", ttypename(ttype(v)), p); | ||
353 | break; | ||
354 | } | ||
355 | } | ||
356 | } | ||
357 | |||
358 | |||
328 | static int testobjref (global_State *g, GCObject *f, GCObject *t) { | 359 | static int testobjref (global_State *g, GCObject *f, GCObject *t) { |
329 | int r1 = testobjref1(g, f, t); | 360 | int r1 = testobjref1(g, f, t); |
330 | if (!r1) { | 361 | if (!r1) { |
@@ -827,8 +858,9 @@ void lua_printstack (lua_State *L) { | |||
827 | int n = lua_gettop(L); | 858 | int n = lua_gettop(L); |
828 | printf("stack: >>\n"); | 859 | printf("stack: >>\n"); |
829 | for (i = 1; i <= n; i++) { | 860 | for (i = 1; i <= n; i++) { |
830 | printf("%3d: %s\n", i, luaL_tolstring(L, i, NULL)); | 861 | printf("%3d: ", i); |
831 | lua_pop(L, 1); | 862 | lua_printvalue(s2v(L->ci->func.p + i)); |
863 | printf("\n"); | ||
832 | } | 864 | } |
833 | printf("<<\n"); | 865 | printf("<<\n"); |
834 | } | 866 | } |
@@ -1681,8 +1713,8 @@ static int runC (lua_State *L, lua_State *L1, const char *pc) { | |||
1681 | else if EQ("printstack") { | 1713 | else if EQ("printstack") { |
1682 | int n = getnum; | 1714 | int n = getnum; |
1683 | if (n != 0) { | 1715 | if (n != 0) { |
1684 | printf("%s\n", luaL_tolstring(L1, n, NULL)); | 1716 | lua_printvalue(s2v(L->ci->func.p + n)); |
1685 | lua_pop(L1, 1); | 1717 | printf("\n"); |
1686 | } | 1718 | } |
1687 | else lua_printstack(L1); | 1719 | else lua_printstack(L1); |
1688 | } | 1720 | } |
@@ -79,6 +79,13 @@ LUAI_FUNC int lua_checkmemory (lua_State *L); | |||
79 | struct GCObject; | 79 | struct GCObject; |
80 | LUAI_FUNC void lua_printobj (lua_State *L, struct GCObject *o); | 80 | LUAI_FUNC void lua_printobj (lua_State *L, struct GCObject *o); |
81 | 81 | ||
82 | |||
83 | /* | ||
84 | ** Function to print a value | ||
85 | */ | ||
86 | struct TValue; | ||
87 | LUAI_FUNC void lua_printvalue (struct TValue *v); | ||
88 | |||
82 | /* | 89 | /* |
83 | ** Function to print the stack | 90 | ** Function to print the stack |
84 | */ | 91 | */ |