When available, use metafield '__name' in error messages

This commit is contained in:
Roberto Ierusalimschy
2016-02-26 16:20:15 -03:00
parent c3e9b14d24
commit 7777b412de
3 changed files with 25 additions and 8 deletions

18
ltm.c
View File

@@ -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);