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-github-437a5b07d415e1a74160ddfd804017171d6cc5cb.tar.gz |
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) { } +static void badexit (void) { + /* avoid assertion failures when exiting */ + l_memcontrol.numblocks = l_memcontrol.total = 0; + exit(EXIT_FAILURE); +} + + static int tpanic (lua_State *L) { fprintf(stderr, "PANIC: unprotected error in call to Lua API (%s)\n", lua_tostring(L, -1)); - return (exit(EXIT_FAILURE), 0); /* do not return to Lua */ + return (badexit(), 0); /* do not return to Lua */ +} + + +static int islast (const char *message) { + size_t len = strlen(message); + return (len > 0 && message[len - 1] == '\n'); +} + + +static void warnf (void **pud, const char *msg) { + if (*pud == NULL) /* continuation line? */ + printf("%s", msg); /* print it */ + else if (msg[0] == '*') /* expected warning? */ + printf("Expected Lua warning: %s", msg + 1); /* print without the star */ + else { /* a real warning; should not happen during tests */ + fprintf(stderr, "Warning in test mode (%s), aborting...\n", msg); + badexit(); + } + *pud = islast(msg) ? pud : NULL; } @@ -1405,6 +1431,10 @@ static int runC (lua_State *L, lua_State *L1, const char *pc) { const char *msg = getstring; printf("%s\n", msg); } + else if EQ("warning") { + const char *msg = getstring; + lua_warning(L1, msg); + } else if EQ("pushbool") { lua_pushboolean(L1, getnum); } @@ -1743,6 +1773,7 @@ static void checkfinalmem (void) { int luaB_opentests (lua_State *L) { void *ud; lua_atpanic(L, &tpanic); + lua_setwarnf(L, &warnf, L); atexit(checkfinalmem); lua_assert(lua_getallocf(L, &ud) == debug_realloc); lua_assert(ud == cast_voidp(&l_memcontrol)); |