Browse Source

From Lua 5.2: Try __tostring metamethod on non-string error messages.

Mike Pall 13 years ago
parent
commit
ff71394931
1 changed files with 10 additions and 5 deletions
  1. 10 5
      src/luajit.c

+ 10 - 5
src/luajit.c

@@ -93,8 +93,13 @@ static int report(lua_State *L, int status)
 
 static int traceback(lua_State *L)
 {
-  if (!lua_isstring(L, 1))  /* 'message' not a string? */
-    return 1;  /* keep it intact */
+  if (!lua_isstring(L, 1)) { /* Non-string error object? Try metamethod. */
+    if (lua_isnoneornil(L, 1) ||
+	!luaL_callmeta(L, 1, "__tostring") ||
+	!lua_isstring(L, -1))
+      return 1;  /* Return non-string error object. */
+    lua_remove(L, 1);  /* Replace object by result of __tostring metamethod. */
+  }
   lua_getfield(L, LUA_GLOBALSINDEX, "debug");
   if (!lua_istable(L, -1)) {
     lua_pop(L, 1);
@@ -105,9 +110,9 @@ static int traceback(lua_State *L)
     lua_pop(L, 2);
     return 1;
   }
-  lua_pushvalue(L, 1);  /* pass error message */
-  lua_pushinteger(L, 2);  /* skip this function and traceback */
-  lua_call(L, 2, 1);  /* call debug.traceback */
+  lua_pushvalue(L, 1);  /* Push error message. */
+  lua_pushinteger(L, 2);  /* Skip this function and debug.traceback(). */
+  lua_call(L, 2, 1);  /* Call debug.traceback(). */
   return 1;
 }