From b1abaff6353be16009b1bf4ff2f91174632f0819 Mon Sep 17 00:00:00 2001 From: Philipp Janda Date: Tue, 8 Aug 2017 08:45:17 +0200 Subject: Update behavior of luaL_tolstring(). --- c-api/compat-5.3.c | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) (limited to 'c-api') diff --git a/c-api/compat-5.3.c b/c-api/compat-5.3.c index b913196..1e985f9 100644 --- a/c-api/compat-5.3.c +++ b/c-api/compat-5.3.c @@ -238,7 +238,8 @@ COMPAT53_API void *luaL_testudata (lua_State *L, int i, const char *tname) { COMPAT53_API const char *luaL_tolstring (lua_State *L, int idx, size_t *len) { if (!luaL_callmeta(L, idx, "__tostring")) { - int t = lua_type(L, idx); + int t = lua_type(L, idx), tt = 0; + char const* name = NULL; switch (t) { case LUA_TNIL: lua_pushliteral(L, "nil"); @@ -254,10 +255,16 @@ COMPAT53_API const char *luaL_tolstring (lua_State *L, int idx, size_t *len) { lua_pushliteral(L, "false"); break; default: - lua_pushfstring(L, "%s: %p", lua_typename(L, t), - lua_topointer(L, idx)); + tt = luaL_getmetafield(L, idx, "__name"); + name = (tt == LUA_TSTRING) ? lua_tostring(L, -1) : lua_typename(L, t); + lua_pushfstring(L, "%s: %p", name, lua_topointer(L, idx)); + if (tt != LUA_TNIL) + lua_replace(L, -2); break; } + } else { + if (!lua_isstring(L, -1)) + luaL_error(L, "'__tostring' must return a string"); } return lua_tolstring(L, -1, len); } -- cgit v1.2.3-55-g6feb