diff options
| author | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2018-12-28 15:42:34 -0200 |
|---|---|---|
| committer | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2018-12-28 15:42:34 -0200 |
| commit | 437a5b07d415e1a74160ddfd804017171d6cc5cb (patch) | |
| tree | 861f9a56ae175eaed91c163409c33ab85bee7ff9 /ltests.c | |
| parent | ba7da13ec5938f978c37d63aa40a3e340b301f79 (diff) | |
| download | lua-437a5b07d415e1a74160ddfd804017171d6cc5cb.tar.gz lua-437a5b07d415e1a74160ddfd804017171d6cc5cb.tar.bz2 lua-437a5b07d415e1a74160ddfd804017171d6cc5cb.zip | |
Added a warning system to Lua
The warning system is just a way for Lua to emit warnings, messages
to the programmer that do not interfere with the running program.
Diffstat (limited to 'ltests.c')
| -rw-r--r-- | ltests.c | 33 |
1 files changed, 32 insertions, 1 deletions
| @@ -63,10 +63,36 @@ static void pushobject (lua_State *L, const TValue *o) { | |||
| 63 | } | 63 | } |
| 64 | 64 | ||
| 65 | 65 | ||
| 66 | static void badexit (void) { | ||
| 67 | /* avoid assertion failures when exiting */ | ||
| 68 | l_memcontrol.numblocks = l_memcontrol.total = 0; | ||
| 69 | exit(EXIT_FAILURE); | ||
| 70 | } | ||
| 71 | |||
| 72 | |||
| 66 | static int tpanic (lua_State *L) { | 73 | static int tpanic (lua_State *L) { |
| 67 | fprintf(stderr, "PANIC: unprotected error in call to Lua API (%s)\n", | 74 | fprintf(stderr, "PANIC: unprotected error in call to Lua API (%s)\n", |
| 68 | lua_tostring(L, -1)); | 75 | lua_tostring(L, -1)); |
| 69 | return (exit(EXIT_FAILURE), 0); /* do not return to Lua */ | 76 | return (badexit(), 0); /* do not return to Lua */ |
| 77 | } | ||
| 78 | |||
| 79 | |||
| 80 | static int islast (const char *message) { | ||
| 81 | size_t len = strlen(message); | ||
| 82 | return (len > 0 && message[len - 1] == '\n'); | ||
| 83 | } | ||
| 84 | |||
| 85 | |||
| 86 | static void warnf (void **pud, const char *msg) { | ||
| 87 | if (*pud == NULL) /* continuation line? */ | ||
| 88 | printf("%s", msg); /* print it */ | ||
| 89 | else if (msg[0] == '*') /* expected warning? */ | ||
| 90 | printf("Expected Lua warning: %s", msg + 1); /* print without the star */ | ||
| 91 | else { /* a real warning; should not happen during tests */ | ||
| 92 | fprintf(stderr, "Warning in test mode (%s), aborting...\n", msg); | ||
| 93 | badexit(); | ||
| 94 | } | ||
| 95 | *pud = islast(msg) ? pud : NULL; | ||
| 70 | } | 96 | } |
| 71 | 97 | ||
| 72 | 98 | ||
| @@ -1405,6 +1431,10 @@ static int runC (lua_State *L, lua_State *L1, const char *pc) { | |||
| 1405 | const char *msg = getstring; | 1431 | const char *msg = getstring; |
| 1406 | printf("%s\n", msg); | 1432 | printf("%s\n", msg); |
| 1407 | } | 1433 | } |
| 1434 | else if EQ("warning") { | ||
| 1435 | const char *msg = getstring; | ||
| 1436 | lua_warning(L1, msg); | ||
| 1437 | } | ||
| 1408 | else if EQ("pushbool") { | 1438 | else if EQ("pushbool") { |
| 1409 | lua_pushboolean(L1, getnum); | 1439 | lua_pushboolean(L1, getnum); |
| 1410 | } | 1440 | } |
| @@ -1743,6 +1773,7 @@ static void checkfinalmem (void) { | |||
| 1743 | int luaB_opentests (lua_State *L) { | 1773 | int luaB_opentests (lua_State *L) { |
| 1744 | void *ud; | 1774 | void *ud; |
| 1745 | lua_atpanic(L, &tpanic); | 1775 | lua_atpanic(L, &tpanic); |
| 1776 | lua_setwarnf(L, &warnf, L); | ||
| 1746 | atexit(checkfinalmem); | 1777 | atexit(checkfinalmem); |
| 1747 | lua_assert(lua_getallocf(L, &ud) == debug_realloc); | 1778 | lua_assert(lua_getallocf(L, &ud) == debug_realloc); |
| 1748 | lua_assert(ud == cast_voidp(&l_memcontrol)); | 1779 | lua_assert(ud == cast_voidp(&l_memcontrol)); |
