diff options
author | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2014-11-11 15:15:06 -0200 |
---|---|---|
committer | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2014-11-11 15:15:06 -0200 |
commit | 6624ccff264ae9f7b0edb922cacd5c54826797da (patch) | |
tree | 9b0f5e77a5f01e43cab3cfbbc0bf392a524d05d8 | |
parent | 9a38c08011a2d8a4d291c9267efbdd44e7beb7c4 (diff) | |
download | lua-6624ccff264ae9f7b0edb922cacd5c54826797da.tar.gz lua-6624ccff264ae9f7b0edb922cacd5c54826797da.tar.bz2 lua-6624ccff264ae9f7b0edb922cacd5c54826797da.zip |
independent code for 'printstack' + test for panic function can
ran code there
-rw-r--r-- | ltests.c | 38 |
1 files changed, 21 insertions, 17 deletions
@@ -1,5 +1,5 @@ | |||
1 | /* | 1 | /* |
2 | ** $Id: ltests.c,v 2.194 2014/11/10 14:47:29 roberto Exp roberto $ | 2 | ** $Id: ltests.c,v 2.195 2014/11/10 17:41:36 roberto Exp roberto $ |
3 | ** Internal Module for Debugging of the Lua Implementation | 3 | ** Internal Module for Debugging of the Lua Implementation |
4 | ** See Copyright Notice in lua.h | 4 | ** See Copyright Notice in lua.h |
5 | */ | 5 | */ |
@@ -555,6 +555,16 @@ static int listlocals (lua_State *L) { | |||
555 | 555 | ||
556 | 556 | ||
557 | 557 | ||
558 | static void printstack (lua_State *L) { | ||
559 | int i; | ||
560 | int n = lua_gettop(L); | ||
561 | for (i = 1; i <= n; i++) { | ||
562 | printf("%3d: %s\n", i, luaL_tolstring(L, i, NULL)); | ||
563 | lua_pop(L, 1); | ||
564 | } | ||
565 | printf("\n"); | ||
566 | } | ||
567 | |||
558 | 568 | ||
559 | static int get_limits (lua_State *L) { | 569 | static int get_limits (lua_State *L) { |
560 | lua_createtable(L, 0, 5); | 570 | lua_createtable(L, 0, 5); |
@@ -906,19 +916,18 @@ static int int2fb_aux (lua_State *L) { | |||
906 | } | 916 | } |
907 | 917 | ||
908 | 918 | ||
909 | struct Aux { jmp_buf jb; const char *msg; }; | 919 | struct Aux { jmp_buf jb; const char *paniccode; lua_State *L; }; |
910 | 920 | ||
911 | /* | 921 | /* |
912 | ** does a long-jump back to "main program". | 922 | ** does a long-jump back to "main program". |
913 | */ | 923 | */ |
914 | static int panicback (lua_State *L) { | 924 | static int panicback (lua_State *L) { |
915 | struct Aux *b; | 925 | struct Aux *b; |
916 | const char *msg = lua_tostring(L, -1); | 926 | lua_checkstack(L, 1); /* open space for 'Aux' struct */ |
917 | lua_pop(L, 1); | ||
918 | lua_getfield(L, LUA_REGISTRYINDEX, "_jmpbuf"); /* get 'Aux' struct */ | 927 | lua_getfield(L, LUA_REGISTRYINDEX, "_jmpbuf"); /* get 'Aux' struct */ |
919 | b = (struct Aux *)lua_touserdata(L, -1); | 928 | b = (struct Aux *)lua_touserdata(L, -1); |
920 | lua_pop(L, 1); /* remove 'Aux' struct */ | 929 | lua_pop(L, 1); /* remove 'Aux' struct */ |
921 | b->msg = msg; | 930 | runC(b->L, L, b->paniccode); /* run optional panic code */ |
922 | longjmp(b->jb, 1); | 931 | longjmp(b->jb, 1); |
923 | return 1; /* to avoid warnings */ | 932 | return 1; /* to avoid warnings */ |
924 | } | 933 | } |
@@ -926,9 +935,12 @@ static int panicback (lua_State *L) { | |||
926 | static int checkpanic (lua_State *L) { | 935 | static int checkpanic (lua_State *L) { |
927 | struct Aux b; | 936 | struct Aux b; |
928 | void *ud; | 937 | void *ud; |
929 | const char *code = luaL_checkstring(L, 1); /* create new state */ | 938 | lua_State *L1; |
939 | const char *code = luaL_checkstring(L, 1); | ||
930 | lua_Alloc f = lua_getallocf(L, &ud); | 940 | lua_Alloc f = lua_getallocf(L, &ud); |
931 | lua_State *L1 = lua_newstate(f, ud); | 941 | b.paniccode = luaL_optstring(L, 2, ""); |
942 | b.L = L; | ||
943 | L1 = lua_newstate(f, ud); /* create new state */ | ||
932 | if (L1 == NULL) { /* error? */ | 944 | if (L1 == NULL) { /* error? */ |
933 | lua_pushnil(L); | 945 | lua_pushnil(L); |
934 | return 1; | 946 | return 1; |
@@ -942,7 +954,7 @@ static int checkpanic (lua_State *L) { | |||
942 | } | 954 | } |
943 | else { /* error handling */ | 955 | else { /* error handling */ |
944 | /* move error message to original state */ | 956 | /* move error message to original state */ |
945 | lua_pushstring(L, b.msg); | 957 | lua_pushstring(L, lua_tostring(L1, -1)); |
946 | } | 958 | } |
947 | lua_close(L1); | 959 | lua_close(L1); |
948 | return 1; | 960 | return 1; |
@@ -1206,15 +1218,7 @@ static int runC (lua_State *L, lua_State *L1, const char *pc) { | |||
1206 | printf("%s\n", luaL_tolstring(L1, n, NULL)); | 1218 | printf("%s\n", luaL_tolstring(L1, n, NULL)); |
1207 | lua_pop(L1, 1); | 1219 | lua_pop(L1, 1); |
1208 | } | 1220 | } |
1209 | else { | 1221 | else printstack(L1); |
1210 | int i; | ||
1211 | n = lua_gettop(L1); | ||
1212 | for (i = 1; i <= n; i++) { | ||
1213 | printf("%s ", luaL_tolstring(L1, i, NULL)); | ||
1214 | lua_pop(L1, 1); | ||
1215 | } | ||
1216 | printf("\n"); | ||
1217 | } | ||
1218 | } | 1222 | } |
1219 | else if EQ("pushbool") { | 1223 | else if EQ("pushbool") { |
1220 | lua_pushboolean(L1, getnum); | 1224 | lua_pushboolean(L1, getnum); |