From ff7139493196f8b43bc8912a9ea33d4d5e169796 Mon Sep 17 00:00:00 2001 From: Mike Pall Date: Mon, 13 Feb 2012 20:08:29 +0100 Subject: [PATCH] From Lua 5.2: Try __tostring metamethod on non-string error messages. --- src/luajit.c | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/src/luajit.c b/src/luajit.c index 0860248b..ecf4ef26 100644 --- a/src/luajit.c +++ b/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; }