diff options
author | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2019-08-15 13:44:36 -0300 |
---|---|---|
committer | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2019-08-15 13:44:36 -0300 |
commit | a1d8eb27431c02c4529be1efd92143ad65434f3a (patch) | |
tree | 58db9340ba2b8ea1cb91004b96f15a955f167c58 /lauxlib.c | |
parent | f64a1b175a5fa65434a073e6d071b32bb7b0ab69 (diff) | |
download | lua-a1d8eb27431c02c4529be1efd92143ad65434f3a.tar.gz lua-a1d8eb27431c02c4529be1efd92143ad65434f3a.tar.bz2 lua-a1d8eb27431c02c4529be1efd92143ad65434f3a.zip |
Added control messages to warnings
Added the concept of control messages to the warning system, plus the
implementation of the controls "@on"/"@off" to turn warnings on/off.
Moreover, the warning system in the test library adds some other
controls to ease the test of warnings.
Diffstat (limited to 'lauxlib.c')
-rw-r--r-- | lauxlib.c | 34 |
1 files changed, 24 insertions, 10 deletions
@@ -1002,29 +1002,43 @@ static int panic (lua_State *L) { | |||
1002 | 1002 | ||
1003 | 1003 | ||
1004 | /* | 1004 | /* |
1005 | ** Emit a warning. '*previoustocont' signals whether previous message | 1005 | ** Emit a warning. '*warnstate' means: |
1006 | ** was to be continued by the current one. | 1006 | ** 0 - warning system is off; |
1007 | ** 1 - ready to start a new message; | ||
1008 | ** 2 - previous message is to be continued. | ||
1007 | */ | 1009 | */ |
1008 | static void warnf (void *ud, const char *message, int tocont) { | 1010 | static void warnf (void *ud, const char *message, int tocont) { |
1009 | int *previoustocont = (int *)ud; | 1011 | int *warnstate = (int *)ud; |
1010 | if (!*previoustocont) /* previous message was the last? */ | 1012 | if (*warnstate != 2 && !tocont && *message == '@') { /* control message? */ |
1013 | if (strcmp(message + 1, "off") == 0) | ||
1014 | *warnstate = 0; | ||
1015 | else if (strcmp(message + 1, "on") == 0) | ||
1016 | *warnstate = 1; | ||
1017 | return; | ||
1018 | } | ||
1019 | else if (*warnstate == 0) /* warnings off? */ | ||
1020 | return; | ||
1021 | if (*warnstate == 1) /* previous message was the last? */ | ||
1011 | lua_writestringerror("%s", "Lua warning: "); /* start a new warning */ | 1022 | lua_writestringerror("%s", "Lua warning: "); /* start a new warning */ |
1012 | lua_writestringerror("%s", message); /* write message */ | 1023 | lua_writestringerror("%s", message); /* write message */ |
1013 | if (!tocont) /* is this the last part? */ | 1024 | if (tocont) /* not the last part? */ |
1025 | *warnstate = 2; /* to be continued */ | ||
1026 | else { /* last part */ | ||
1014 | lua_writestringerror("%s", "\n"); /* finish message with end-of-line */ | 1027 | lua_writestringerror("%s", "\n"); /* finish message with end-of-line */ |
1015 | *previoustocont = tocont; | 1028 | *warnstate = 1; /* ready to start a new message */ |
1029 | } | ||
1016 | } | 1030 | } |
1017 | 1031 | ||
1018 | 1032 | ||
1019 | LUALIB_API lua_State *luaL_newstate (void) { | 1033 | LUALIB_API lua_State *luaL_newstate (void) { |
1020 | lua_State *L = lua_newstate(l_alloc, NULL); | 1034 | lua_State *L = lua_newstate(l_alloc, NULL); |
1021 | if (L) { | 1035 | if (L) { |
1022 | int *previoustocont; /* space for warning state */ | 1036 | int *warnstate; /* space for warning state */ |
1023 | lua_atpanic(L, &panic); | 1037 | lua_atpanic(L, &panic); |
1024 | previoustocont = (int *)lua_newuserdatauv(L, sizeof(int), 0); | 1038 | warnstate = (int *)lua_newuserdatauv(L, sizeof(int), 0); |
1025 | luaL_ref(L, LUA_REGISTRYINDEX); /* make sure it won't be collected */ | 1039 | luaL_ref(L, LUA_REGISTRYINDEX); /* make sure it won't be collected */ |
1026 | *previoustocont = 0; /* next message starts a new warning */ | 1040 | *warnstate = 1; /* next message starts a new warning */ |
1027 | lua_setwarnf(L, warnf, previoustocont); | 1041 | lua_setwarnf(L, warnf, warnstate); |
1028 | } | 1042 | } |
1029 | return L; | 1043 | return L; |
1030 | } | 1044 | } |