diff options
author | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2019-03-14 15:30:54 -0300 |
---|---|---|
committer | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2019-03-14 15:30:54 -0300 |
commit | b56d4e570a60a8e84df8288c3122eb5bb5c20af6 (patch) | |
tree | d5597a7865712fc407adbb41fe0749e728617ca7 /lauxlib.c | |
parent | 9eca305e75010e30342486a4139846faf1b3eccb (diff) | |
download | lua-b56d4e570a60a8e84df8288c3122eb5bb5c20af6.tar.gz lua-b56d4e570a60a8e84df8288c3122eb5bb5c20af6.tar.bz2 lua-b56d4e570a60a8e84df8288c3122eb5bb5c20af6.zip |
Changes in the warning system
- The warning functions get an extra parameter that tells whether
message is to be continued (instead of using end-of-lines as a signal).
- The user data for the warning function is a regular value, instead
of a writable slot inside the Lua state.
Diffstat (limited to 'lauxlib.c')
-rw-r--r-- | lauxlib.c | 34 |
1 files changed, 15 insertions, 19 deletions
@@ -987,33 +987,29 @@ static int panic (lua_State *L) { | |||
987 | 987 | ||
988 | 988 | ||
989 | /* | 989 | /* |
990 | ** checks whether 'message' ends with end-of-line | 990 | ** Emit a warning. '*previoustocont' signals whether previous message |
991 | ** (and therefore is the last part of a warning) | 991 | ** was to be continued by the current one. |
992 | */ | 992 | */ |
993 | static int islast (const char *message) { | 993 | static void warnf (void *ud, const char *message, int tocont) { |
994 | size_t len = strlen(message); | 994 | int *previoustocont = (int *)ud; |
995 | return (len > 0 && message[len - 1] == '\n'); | 995 | if (!*previoustocont) /* previous message was the last? */ |
996 | } | 996 | lua_writestringerror("%s", "Lua warning: "); /* start a new warning */ |
997 | 997 | lua_writestringerror("%s", message); /* write message */ | |
998 | 998 | if (!tocont) /* is this the last part? */ | |
999 | /* | 999 | lua_writestringerror("%s", "\n"); /* finish message with end-of-line */ |
1000 | ** Emit a warning. If '*pud' is NULL, previous message was to be | 1000 | *previoustocont = tocont; |
1001 | ** continued by the current one. | ||
1002 | */ | ||
1003 | static void warnf (void **pud, const char *message) { | ||
1004 | if (*pud == NULL) /* previous message was not the last? */ | ||
1005 | lua_writestringerror("%s", message); | ||
1006 | else /* start a new warning */ | ||
1007 | lua_writestringerror("Lua warning: %s", message); | ||
1008 | *pud = (islast(message)) ? pud : NULL; | ||
1009 | } | 1001 | } |
1010 | 1002 | ||
1011 | 1003 | ||
1012 | LUALIB_API lua_State *luaL_newstate (void) { | 1004 | LUALIB_API lua_State *luaL_newstate (void) { |
1013 | lua_State *L = lua_newstate(l_alloc, NULL); | 1005 | lua_State *L = lua_newstate(l_alloc, NULL); |
1014 | if (L) { | 1006 | if (L) { |
1007 | int *previoustocont; /* space for warning state */ | ||
1015 | lua_atpanic(L, &panic); | 1008 | lua_atpanic(L, &panic); |
1016 | lua_setwarnf(L, warnf, L); | 1009 | previoustocont = (int *)lua_newuserdatauv(L, sizeof(int), 0); |
1010 | luaL_ref(L, LUA_REGISTRYINDEX); /* make sure it won't be collected */ | ||
1011 | *previoustocont = 0; /* next message starts a new warning */ | ||
1012 | lua_setwarnf(L, warnf, previoustocont); | ||
1017 | } | 1013 | } |
1018 | return L; | 1014 | return L; |
1019 | } | 1015 | } |