1
0
mirror of https://github.com/lua/lua.git synced 2025-01-14 05:43:00 +08:00

lua_pushfstring' now supports %p' option too

This commit is contained in:
Roberto Ierusalimschy 2004-07-09 13:01:38 -03:00
parent ce455481ab
commit 76d8b8db06
3 changed files with 30 additions and 31 deletions

View File

@ -1,5 +1,5 @@
/* /*
** $Id: lbaselib.c,v 1.151 2004/07/01 14:26:28 roberto Exp roberto $ ** $Id: lbaselib.c,v 1.152 2004/07/02 18:09:11 roberto Exp roberto $
** Basic library ** Basic library
** See Copyright Notice in lua.h ** See Copyright Notice in lua.h
*/ */
@ -383,45 +383,36 @@ static int luaB_xpcall (lua_State *L) {
static int luaB_tostring (lua_State *L) { static int luaB_tostring (lua_State *L) {
char buff[4*sizeof(void *) + 2]; /* enough space for a `%p' */
const char *tn = "";
const void *p = NULL;
luaL_checkany(L, 1); luaL_checkany(L, 1);
if (luaL_callmeta(L, 1, "__tostring")) /* is there a metafield? */ if (luaL_callmeta(L, 1, "__tostring")) /* is there a metafield? */
return 1; /* use its value */ return 1; /* use its value */
switch (lua_type(L, 1)) { switch (lua_type(L, 1)) {
case LUA_TNUMBER: case LUA_TNUMBER:
lua_pushstring(L, lua_tostring(L, 1)); lua_pushstring(L, lua_tostring(L, 1));
return 1; break;
case LUA_TSTRING: case LUA_TSTRING:
lua_pushvalue(L, 1); lua_pushvalue(L, 1);
return 1; break;
case LUA_TBOOLEAN: case LUA_TBOOLEAN:
lua_pushstring(L, (lua_toboolean(L, 1) ? "true" : "false")); lua_pushstring(L, (lua_toboolean(L, 1) ? "true" : "false"));
return 1; break;
case LUA_TNIL: case LUA_TNIL:
lua_pushliteral(L, "nil"); lua_pushliteral(L, "nil");
return 1; break;
case LUA_TTABLE: case LUA_TTABLE:
p = lua_topointer(L, 1); lua_pushfstring(L, "table: %p", lua_topointer(L, 1));
tn = "table";
break; break;
case LUA_TFUNCTION: case LUA_TFUNCTION:
p = lua_topointer(L, 1); lua_pushfstring(L, "function: %p", lua_topointer(L, 1));
tn = "function";
break; break;
case LUA_TUSERDATA: case LUA_TUSERDATA:
case LUA_TLIGHTUSERDATA: case LUA_TLIGHTUSERDATA:
p = lua_touserdata(L, 1); lua_pushfstring(L, "userdata: %p", lua_topointer(L, 1));
tn = "userdata";
break; break;
case LUA_TTHREAD: case LUA_TTHREAD:
p = lua_tothread(L, 1); lua_pushfstring(L, "thread: %p", lua_topointer(L, 1));
tn = "thread";
break; break;
} }
sprintf(buff, "%p", p);
lua_pushfstring(L, "%s: %s", tn, buff);
return 1; return 1;
} }

View File

@ -1,5 +1,5 @@
/* /*
** $Id: liolib.c,v 2.53 2004/05/28 18:35:05 roberto Exp roberto $ ** $Id: liolib.c,v 2.54 2004/07/09 15:47:48 roberto Exp roberto $
** Standard I/O (and system) library ** Standard I/O (and system) library
** See Copyright Notice in lua.h ** See Copyright Notice in lua.h
*/ */
@ -114,13 +114,11 @@ static int io_gc (lua_State *L) {
static int io_tostring (lua_State *L) { static int io_tostring (lua_State *L) {
char buff[4*sizeof(void *) + 8]; /* enough space for a `%p' */ FILE *f = *topfile(L);
FILE **f = topfile(L); if (f == NULL)
if (*f == NULL) lua_pushstring(L, "file (closed)");
strcpy(buff, "closed");
else else
sprintf(buff, "%p", lua_touserdata(L, 1)); lua_pushfstring(L, "file (%p)", f);
lua_pushfstring(L, "file (%s)", buff);
return 1; return 1;
} }

View File

@ -1,5 +1,5 @@
/* /*
** $Id: lobject.c,v 2.2 2004/04/30 20:13:38 roberto Exp roberto $ ** $Id: lobject.c,v 2.3 2004/05/03 12:30:41 roberto Exp roberto $
** Some generic functions over Lua objects ** Some generic functions over Lua objects
** See Copyright Notice in lua.h ** See Copyright Notice in lua.h
*/ */
@ -94,7 +94,7 @@ static void pushstr (lua_State *L, const char *str) {
} }
/* this function handles only `%d', `%c', %f, and `%s' formats */ /* this function handles only `%d', `%c', %f, %p, and `%s' formats */
const char *luaO_pushvfstring (lua_State *L, const char *fmt, va_list argp) { const char *luaO_pushvfstring (lua_State *L, const char *fmt, va_list argp) {
int n = 1; int n = 1;
pushstr(L, ""); pushstr(L, "");
@ -104,9 +104,10 @@ const char *luaO_pushvfstring (lua_State *L, const char *fmt, va_list argp) {
setsvalue2s(L, L->top, luaS_newlstr(L, fmt, e-fmt)); setsvalue2s(L, L->top, luaS_newlstr(L, fmt, e-fmt));
incr_top(L); incr_top(L);
switch (*(e+1)) { switch (*(e+1)) {
case 's': case 's': {
pushstr(L, va_arg(argp, char *)); pushstr(L, va_arg(argp, char *));
break; break;
}
case 'c': { case 'c': {
char buff[2]; char buff[2];
buff[0] = cast(char, va_arg(argp, int)); buff[0] = cast(char, va_arg(argp, int));
@ -114,17 +115,26 @@ const char *luaO_pushvfstring (lua_State *L, const char *fmt, va_list argp) {
pushstr(L, buff); pushstr(L, buff);
break; break;
} }
case 'd': case 'd': {
setnvalue(L->top, cast(lua_Number, va_arg(argp, int))); setnvalue(L->top, cast(lua_Number, va_arg(argp, int)));
incr_top(L); incr_top(L);
break; break;
case 'f': }
case 'f': {
setnvalue(L->top, cast(lua_Number, va_arg(argp, l_uacNumber))); setnvalue(L->top, cast(lua_Number, va_arg(argp, l_uacNumber)));
incr_top(L); incr_top(L);
break; break;
case '%': }
case 'p': {
char buff[4*sizeof(void *) + 8]; /* should be enough space for a `%p' */
sprintf(buff, "%p", va_arg(argp, void *));
pushstr(L, buff);
break;
}
case '%': {
pushstr(L, "%"); pushstr(L, "%");
break; break;
}
default: { default: {
char buff[3]; char buff[3];
buff[0] = '%'; buff[0] = '%';