From 7777b412deb46d051967b699697d1d6e6286a7b6 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Fri, 26 Feb 2016 16:20:15 -0300 Subject: When available, use metafield '__name' in error messages --- ltm.c | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) (limited to 'ltm.c') diff --git a/ltm.c b/ltm.c index 3f400d4b..cc28a414 100644 --- a/ltm.c +++ b/ltm.c @@ -1,5 +1,5 @@ /* -** $Id: ltm.c,v 2.35 2015/11/02 18:48:07 roberto Exp roberto $ +** $Id: ltm.c,v 2.36 2015/11/03 15:47:30 roberto Exp roberto $ ** Tag methods ** See Copyright Notice in lua.h */ @@ -83,6 +83,22 @@ const TValue *luaT_gettmbyobj (lua_State *L, const TValue *o, TMS event) { } +/* +** Return the name of the type of an object. For tables and userdata +** with metatable, use their '__name' metafield, if present. +*/ +const char *luaT_objtypename (lua_State *L, const TValue *o) { + Table *mt; + if ((ttistable(o) && (mt = hvalue(o)->metatable) != NULL) || + (ttisfulluserdata(o) && (mt = uvalue(o)->metatable) != NULL)) { + const TValue *name = luaH_getshortstr(mt, luaS_new(L, "__name")); + if (ttisstring(name)) /* is '__name' a string? */ + return getstr(tsvalue(name)); /* use it as type name */ + } + return ttypename(ttnov(o)); /* else use standard type name */ +} + + void luaT_callTM (lua_State *L, const TValue *f, const TValue *p1, const TValue *p2, TValue *p3, int hasres) { ptrdiff_t result = savestack(L, p3); -- cgit v1.2.3-55-g6feb