diff options
author | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2014-10-20 16:19:26 -0200 |
---|---|---|
committer | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2014-10-20 16:19:26 -0200 |
commit | 961760c9b687560308644d90981cb5758dd488fd (patch) | |
tree | 54ae231d57833521313f499c15dbae188d59aa75 | |
parent | 57559547a30164e60ffa05422f9db990517dbcc5 (diff) | |
download | lua-961760c9b687560308644d90981cb5758dd488fd.tar.gz lua-961760c9b687560308644d90981cb5758dd488fd.tar.bz2 lua-961760c9b687560308644d90981cb5758dd488fd.zip |
message handler always adds a traceback to messages (even if they
are not strings), unless they have a __tostring method.
-rw-r--r-- | lua.c | 28 |
1 files changed, 14 insertions, 14 deletions
@@ -1,5 +1,5 @@ | |||
1 | /* | 1 | /* |
2 | ** $Id: lua.c,v 1.214 2014/09/25 14:20:37 roberto Exp roberto $ | 2 | ** $Id: lua.c,v 1.215 2014/10/17 16:28:21 roberto Exp roberto $ |
3 | ** Lua stand-alone interpreter | 3 | ** Lua stand-alone interpreter |
4 | ** See Copyright Notice in lua.h | 4 | ** See Copyright Notice in lua.h |
5 | */ | 5 | */ |
@@ -158,32 +158,32 @@ static void l_message (const char *pname, const char *msg) { | |||
158 | 158 | ||
159 | /* | 159 | /* |
160 | ** Check whether 'status' is not OK and, if so, prints the error | 160 | ** Check whether 'status' is not OK and, if so, prints the error |
161 | ** message on the top of the stack. Because this function can be called | 161 | ** message on the top of the stack. |
162 | ** unprotected, it only accepts actual strings as error messages. (A | ||
163 | ** coercion could raise a memory error.) | ||
164 | */ | 162 | */ |
165 | static int report (lua_State *L, int status) { | 163 | static int report (lua_State *L, int status) { |
166 | if (status != LUA_OK) { | 164 | if (status != LUA_OK) { |
167 | const char *msg = (lua_type(L, -1) == LUA_TSTRING) | 165 | const char *msg = lua_tostring(L, -1); |
168 | ? lua_tostring(L, -1) | ||
169 | : "(error object is not a string)"; | ||
170 | l_message(progname, msg); | 166 | l_message(progname, msg); |
171 | lua_pop(L, 1); | 167 | lua_pop(L, 1); /* remove message */ |
172 | } | 168 | } |
173 | return status; | 169 | return status; |
174 | } | 170 | } |
175 | 171 | ||
176 | 172 | ||
177 | /* | 173 | /* |
178 | ** Message handler to be used to run all chunks | 174 | ** Message handler used to run all chunks |
179 | */ | 175 | */ |
180 | static int msghandler (lua_State *L) { | 176 | static int msghandler (lua_State *L) { |
181 | const char *msg = lua_tostring(L, 1); | 177 | const char *msg = lua_tostring(L, 1); |
182 | if (msg) /* is error object a string? */ | 178 | if (msg == NULL) { /* is error object not a string? */ |
183 | luaL_traceback(L, L, msg, 1); /* use standard traceback */ | 179 | if (luaL_callmeta(L, 1, "__tostring") && /* did have a metamethod */ |
184 | else /* non-string error object */ | 180 | lua_type(L, -1) == LUA_TSTRING) /* and it produce a string? */ |
185 | luaL_callmeta(L, 1, "__tostring"); /* try its 'tostring' metamethod */ | 181 | return 1; /* that is the message */ |
186 | /* if no metamethod, original object still is in the stack */ | 182 | else |
183 | msg = lua_pushfstring(L, "(error object is a %s value)", | ||
184 | luaL_typename(L, 1)); | ||
185 | } | ||
186 | luaL_traceback(L, L, msg, 1); /* append a standard traceback */ | ||
187 | return 1; | 187 | return 1; |
188 | } | 188 | } |
189 | 189 | ||