diff --git a/lauxlib.c b/lauxlib.c index 53c175a7..f2ac74db 100644 --- a/lauxlib.c +++ b/lauxlib.c @@ -1,5 +1,5 @@ /* -** $Id: lauxlib.c,v 1.163 2006/09/25 15:35:00 roberto Exp roberto $ +** $Id: lauxlib.c,v 1.164 2006/10/16 14:38:38 roberto Exp roberto $ ** Auxiliary functions for building Lua libraries ** See Copyright Notice in lua.h */ @@ -230,6 +230,31 @@ LUALIB_API int luaL_callmeta (lua_State *L, int obj, const char *event) { } +LUALIB_API const char *luaL_tostring (lua_State *L, int idx) { + if (!luaL_callmeta(L, idx, "__tostring")) { /* no metafield? */ + switch (lua_type(L, idx)) { + case LUA_TNUMBER: + lua_pushstring(L, lua_tostring(L, idx)); + break; + case LUA_TSTRING: + lua_pushvalue(L, idx); + break; + case LUA_TBOOLEAN: + lua_pushstring(L, (lua_toboolean(L, idx) ? "true" : "false")); + break; + case LUA_TNIL: + lua_pushliteral(L, "nil"); + break; + default: + lua_pushfstring(L, "%s: %p", luaL_typename(L, idx), + lua_topointer(L, idx)); + break; + } + } + return lua_tostring(L, -1); +} + + LUALIB_API void luaL_register (lua_State *L, const char *libname, const luaL_Reg *l) { luaI_openlib(L, libname, l, 0); diff --git a/lauxlib.h b/lauxlib.h index f7b02db5..b6fedef5 100644 --- a/lauxlib.h +++ b/lauxlib.h @@ -1,5 +1,5 @@ /* -** $Id: lauxlib.h,v 1.87 2005/12/29 15:32:11 roberto Exp roberto $ +** $Id: lauxlib.h,v 1.88 2006/04/12 20:31:15 roberto Exp roberto $ ** Auxiliary functions for building Lua libraries ** See Copyright Notice in lua.h */ @@ -45,6 +45,7 @@ LUALIB_API void (luaL_register) (lua_State *L, const char *libname, const luaL_Reg *l); LUALIB_API int (luaL_getmetafield) (lua_State *L, int obj, const char *e); LUALIB_API int (luaL_callmeta) (lua_State *L, int obj, const char *e); +LUALIB_API const char *luaL_tostring (lua_State *L, int idx); LUALIB_API int (luaL_typerror) (lua_State *L, int narg, const char *tname); LUALIB_API int (luaL_argerror) (lua_State *L, int numarg, const char *extramsg); LUALIB_API const char *(luaL_checklstring) (lua_State *L, int numArg, diff --git a/lbaselib.c b/lbaselib.c index 01decb46..556325af 100644 --- a/lbaselib.c +++ b/lbaselib.c @@ -1,5 +1,5 @@ /* -** $Id: lbaselib.c,v 1.194 2006/10/20 19:30:53 roberto Exp roberto $ +** $Id: lbaselib.c,v 1.195 2006/10/24 19:46:12 roberto Exp roberto $ ** Basic library ** See Copyright Notice in lua.h */ @@ -396,25 +396,7 @@ static int luaB_xpcall (lua_State *L) { static int luaB_tostring (lua_State *L) { luaL_checkany(L, 1); - if (luaL_callmeta(L, 1, "__tostring")) /* is there a metafield? */ - return 1; /* use its value */ - switch (lua_type(L, 1)) { - case LUA_TNUMBER: - lua_pushstring(L, lua_tostring(L, 1)); - break; - case LUA_TSTRING: - lua_pushvalue(L, 1); - break; - case LUA_TBOOLEAN: - lua_pushstring(L, (lua_toboolean(L, 1) ? "true" : "false")); - break; - case LUA_TNIL: - lua_pushliteral(L, "nil"); - break; - default: - lua_pushfstring(L, "%s: %p", luaL_typename(L, 1), lua_topointer(L, 1)); - break; - } + luaL_tostring(L, 1); return 1; }