aboutsummaryrefslogtreecommitdiff
path: root/lauxlib.c
diff options
context:
space:
mode:
authorRoberto Ierusalimschy <roberto@inf.puc-rio.br>2016-12-04 18:09:45 -0200
committerRoberto Ierusalimschy <roberto@inf.puc-rio.br>2016-12-04 18:09:45 -0200
commitbeec5af2010ad0df9d95b0aaa4842ded1ac60d8a (patch)
tree58dba0d5479d898d01e1f616642d24d2914a84e2 /lauxlib.c
parent7b1fba69b7a887e37e57744309299d134e76e06e (diff)
downloadlua-beec5af2010ad0df9d95b0aaa4842ded1ac60d8a.tar.gz
lua-beec5af2010ad0df9d95b0aaa4842ded1ac60d8a.tar.bz2
lua-beec5af2010ad0df9d95b0aaa4842ded1ac60d8a.zip
'luaL_tolstring' uses metatable's "__name" when available
Diffstat (limited to 'lauxlib.c')
-rw-r--r--lauxlib.c19
1 files changed, 14 insertions, 5 deletions
diff --git a/lauxlib.c b/lauxlib.c
index d6957564..e951d83d 100644
--- a/lauxlib.c
+++ b/lauxlib.c
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: lauxlib.c,v 1.285 2015/12/14 11:59:27 roberto Exp roberto $ 2** $Id: lauxlib.c,v 1.286 2016/01/08 15:33:09 roberto Exp roberto $
3** Auxiliary functions for building Lua libraries 3** Auxiliary functions for building Lua libraries
4** See Copyright Notice in lua.h 4** See Copyright Notice in lua.h
5*/ 5*/
@@ -809,7 +809,11 @@ LUALIB_API lua_Integer luaL_len (lua_State *L, int idx) {
809 809
810 810
811LUALIB_API const char *luaL_tolstring (lua_State *L, int idx, size_t *len) { 811LUALIB_API const char *luaL_tolstring (lua_State *L, int idx, size_t *len) {
812 if (!luaL_callmeta(L, idx, "__tostring")) { /* no metafield? */ 812 if (luaL_callmeta(L, idx, "__tostring")) { /* metafield? */
813 if (!lua_isstring(L, -1))
814 luaL_error(L, "'__tostring' must return a string");
815 }
816 else {
813 switch (lua_type(L, idx)) { 817 switch (lua_type(L, idx)) {
814 case LUA_TNUMBER: { 818 case LUA_TNUMBER: {
815 if (lua_isinteger(L, idx)) 819 if (lua_isinteger(L, idx))
@@ -827,10 +831,15 @@ LUALIB_API const char *luaL_tolstring (lua_State *L, int idx, size_t *len) {
827 case LUA_TNIL: 831 case LUA_TNIL:
828 lua_pushliteral(L, "nil"); 832 lua_pushliteral(L, "nil");
829 break; 833 break;
830 default: 834 default: {
831 lua_pushfstring(L, "%s: %p", luaL_typename(L, idx), 835 int tt = luaL_getmetafield(L, idx, "__name"); /* try name */
832 lua_topointer(L, idx)); 836 const char *kind = (tt == LUA_TSTRING) ? lua_tostring(L, -1) :
837 luaL_typename(L, idx);
838 lua_pushfstring(L, "%s: %p", kind, lua_topointer(L, idx));
839 if (tt != LUA_TNIL)
840 lua_remove(L, -2); /* remove '__name' */
833 break; 841 break;
842 }
834 } 843 }
835 } 844 }
836 return lua_tolstring(L, -1, len); 845 return lua_tolstring(L, -1, len);