From 0c053e09d4616b290a3c23fbafc5b0a4ea030535 Mon Sep 17 00:00:00 2001 From: Alex Szpakowski Date: Sat, 3 Aug 2013 18:52:00 -0300 Subject: [PATCH] Improved error messages when using the wrong love type in a function --- src/common/runtime.cpp | 25 +++++++++++++++++++++++-- 1 file changed, 23 insertions(+), 2 deletions(-) diff --git a/src/common/runtime.cpp b/src/common/runtime.cpp index c0053f2fe..972f2dab2 100644 --- a/src/common/runtime.cpp +++ b/src/common/runtime.cpp @@ -514,8 +514,29 @@ int luax_getregistry(lua_State *L, Registry r) extern "C" int luax_typerror(lua_State *L, int narg, const char *tname) { - const char *msg = lua_pushfstring(L, "%s expected, got %s", - tname, luaL_typename(L, narg)); + int argtype = lua_type(L, narg); + const char *argtname = 0; + + // We want to use the love type name for userdata, if possible. + if (argtype == LUA_TUSERDATA && luaL_getmetafield(L, narg, "__tostring") != 0) + { + lua_pushvalue(L, narg); + if (lua_pcall(L, 1, 1, 0) == 0 && lua_type(L, -1) == LUA_TSTRING) + { + argtname = lua_tostring(L, -1); + + // Non-love userdata might have a tostring metamethod which doesn't + // describe its type, so we only use __tostring for love types. + love::Type t; + if (!love::getType(argtname, t)) + argtname = 0; + } + } + + if (argtname == 0) + argtname = lua_typename(L, argtype); + + const char *msg = lua_pushfstring(L, "%s expected, got %s", tname, argtname); return luaL_argerror(L, narg, msg); }