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.
This commit is contained in:
Roberto Ierusalimschy
2018-12-28 15:42:34 -02:00
parent ba7da13ec5
commit 437a5b07d4
10 changed files with 173 additions and 15 deletions

View File

@@ -986,9 +986,35 @@ static int panic (lua_State *L) {
}
/*
** checks whether 'message' ends with end-of-line
** (and therefore is the last part of a warning)
*/
static int islast (const char *message) {
size_t len = strlen(message);
return (len > 0 && message[len - 1] == '\n');
}
/*
** Emit a warning. If '*pud' is NULL, previous message was to be
** continued by the current one.
*/
static void warnf (void **pud, const char *message) {
if (*pud == NULL) /* previous message was not the last? */
lua_writestringerror("%s", message);
else /* start a new warning */
lua_writestringerror("Lua warning: %s", message);
*pud = (islast(message)) ? pud : NULL;
}
LUALIB_API lua_State *luaL_newstate (void) {
lua_State *L = lua_newstate(l_alloc, NULL);
if (L) lua_atpanic(L, &panic);
if (L) {
lua_atpanic(L, &panic);
lua_setwarnf(L, warnf, L);
}
return L;
}