diff options
-rw-r--r-- | lbaselib.c | 27 | ||||
-rw-r--r-- | liolib.c | 12 | ||||
-rw-r--r-- | lobject.c | 22 |
3 files changed, 30 insertions, 31 deletions
@@ -1,5 +1,5 @@ | |||
1 | /* | 1 | /* |
2 | ** $Id: lbaselib.c,v 1.151 2004/07/01 14:26:28 roberto Exp roberto $ | 2 | ** $Id: lbaselib.c,v 1.152 2004/07/02 18:09:11 roberto Exp roberto $ |
3 | ** Basic library | 3 | ** Basic library |
4 | ** See Copyright Notice in lua.h | 4 | ** See Copyright Notice in lua.h |
5 | */ | 5 | */ |
@@ -383,45 +383,36 @@ static int luaB_xpcall (lua_State *L) { | |||
383 | 383 | ||
384 | 384 | ||
385 | static int luaB_tostring (lua_State *L) { | 385 | static int luaB_tostring (lua_State *L) { |
386 | char buff[4*sizeof(void *) + 2]; /* enough space for a `%p' */ | ||
387 | const char *tn = ""; | ||
388 | const void *p = NULL; | ||
389 | luaL_checkany(L, 1); | 386 | luaL_checkany(L, 1); |
390 | if (luaL_callmeta(L, 1, "__tostring")) /* is there a metafield? */ | 387 | if (luaL_callmeta(L, 1, "__tostring")) /* is there a metafield? */ |
391 | return 1; /* use its value */ | 388 | return 1; /* use its value */ |
392 | switch (lua_type(L, 1)) { | 389 | switch (lua_type(L, 1)) { |
393 | case LUA_TNUMBER: | 390 | case LUA_TNUMBER: |
394 | lua_pushstring(L, lua_tostring(L, 1)); | 391 | lua_pushstring(L, lua_tostring(L, 1)); |
395 | return 1; | 392 | break; |
396 | case LUA_TSTRING: | 393 | case LUA_TSTRING: |
397 | lua_pushvalue(L, 1); | 394 | lua_pushvalue(L, 1); |
398 | return 1; | 395 | break; |
399 | case LUA_TBOOLEAN: | 396 | case LUA_TBOOLEAN: |
400 | lua_pushstring(L, (lua_toboolean(L, 1) ? "true" : "false")); | 397 | lua_pushstring(L, (lua_toboolean(L, 1) ? "true" : "false")); |
401 | return 1; | 398 | break; |
402 | case LUA_TNIL: | 399 | case LUA_TNIL: |
403 | lua_pushliteral(L, "nil"); | 400 | lua_pushliteral(L, "nil"); |
404 | return 1; | 401 | break; |
405 | case LUA_TTABLE: | 402 | case LUA_TTABLE: |
406 | p = lua_topointer(L, 1); | 403 | lua_pushfstring(L, "table: %p", lua_topointer(L, 1)); |
407 | tn = "table"; | ||
408 | break; | 404 | break; |
409 | case LUA_TFUNCTION: | 405 | case LUA_TFUNCTION: |
410 | p = lua_topointer(L, 1); | 406 | lua_pushfstring(L, "function: %p", lua_topointer(L, 1)); |
411 | tn = "function"; | ||
412 | break; | 407 | break; |
413 | case LUA_TUSERDATA: | 408 | case LUA_TUSERDATA: |
414 | case LUA_TLIGHTUSERDATA: | 409 | case LUA_TLIGHTUSERDATA: |
415 | p = lua_touserdata(L, 1); | 410 | lua_pushfstring(L, "userdata: %p", lua_topointer(L, 1)); |
416 | tn = "userdata"; | ||
417 | break; | 411 | break; |
418 | case LUA_TTHREAD: | 412 | case LUA_TTHREAD: |
419 | p = lua_tothread(L, 1); | 413 | lua_pushfstring(L, "thread: %p", lua_topointer(L, 1)); |
420 | tn = "thread"; | ||
421 | break; | 414 | break; |
422 | } | 415 | } |
423 | sprintf(buff, "%p", p); | ||
424 | lua_pushfstring(L, "%s: %s", tn, buff); | ||
425 | return 1; | 416 | return 1; |
426 | } | 417 | } |
427 | 418 | ||
@@ -1,5 +1,5 @@ | |||
1 | /* | 1 | /* |
2 | ** $Id: liolib.c,v 2.53 2004/05/28 18:35:05 roberto Exp roberto $ | 2 | ** $Id: liolib.c,v 2.54 2004/07/09 15:47:48 roberto Exp roberto $ |
3 | ** Standard I/O (and system) library | 3 | ** Standard I/O (and system) library |
4 | ** See Copyright Notice in lua.h | 4 | ** See Copyright Notice in lua.h |
5 | */ | 5 | */ |
@@ -114,13 +114,11 @@ static int io_gc (lua_State *L) { | |||
114 | 114 | ||
115 | 115 | ||
116 | static int io_tostring (lua_State *L) { | 116 | static int io_tostring (lua_State *L) { |
117 | char buff[4*sizeof(void *) + 8]; /* enough space for a `%p' */ | 117 | FILE *f = *topfile(L); |
118 | FILE **f = topfile(L); | 118 | if (f == NULL) |
119 | if (*f == NULL) | 119 | lua_pushstring(L, "file (closed)"); |
120 | strcpy(buff, "closed"); | ||
121 | else | 120 | else |
122 | sprintf(buff, "%p", lua_touserdata(L, 1)); | 121 | lua_pushfstring(L, "file (%p)", f); |
123 | lua_pushfstring(L, "file (%s)", buff); | ||
124 | return 1; | 122 | return 1; |
125 | } | 123 | } |
126 | 124 | ||
@@ -1,5 +1,5 @@ | |||
1 | /* | 1 | /* |
2 | ** $Id: lobject.c,v 2.2 2004/04/30 20:13:38 roberto Exp roberto $ | 2 | ** $Id: lobject.c,v 2.3 2004/05/03 12:30:41 roberto Exp roberto $ |
3 | ** Some generic functions over Lua objects | 3 | ** Some generic functions over Lua objects |
4 | ** See Copyright Notice in lua.h | 4 | ** See Copyright Notice in lua.h |
5 | */ | 5 | */ |
@@ -94,7 +94,7 @@ static void pushstr (lua_State *L, const char *str) { | |||
94 | } | 94 | } |
95 | 95 | ||
96 | 96 | ||
97 | /* this function handles only `%d', `%c', %f, and `%s' formats */ | 97 | /* this function handles only `%d', `%c', %f, %p, and `%s' formats */ |
98 | const char *luaO_pushvfstring (lua_State *L, const char *fmt, va_list argp) { | 98 | const char *luaO_pushvfstring (lua_State *L, const char *fmt, va_list argp) { |
99 | int n = 1; | 99 | int n = 1; |
100 | pushstr(L, ""); | 100 | pushstr(L, ""); |
@@ -104,9 +104,10 @@ const char *luaO_pushvfstring (lua_State *L, const char *fmt, va_list argp) { | |||
104 | setsvalue2s(L, L->top, luaS_newlstr(L, fmt, e-fmt)); | 104 | setsvalue2s(L, L->top, luaS_newlstr(L, fmt, e-fmt)); |
105 | incr_top(L); | 105 | incr_top(L); |
106 | switch (*(e+1)) { | 106 | switch (*(e+1)) { |
107 | case 's': | 107 | case 's': { |
108 | pushstr(L, va_arg(argp, char *)); | 108 | pushstr(L, va_arg(argp, char *)); |
109 | break; | 109 | break; |
110 | } | ||
110 | case 'c': { | 111 | case 'c': { |
111 | char buff[2]; | 112 | char buff[2]; |
112 | buff[0] = cast(char, va_arg(argp, int)); | 113 | 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) { | |||
114 | pushstr(L, buff); | 115 | pushstr(L, buff); |
115 | break; | 116 | break; |
116 | } | 117 | } |
117 | case 'd': | 118 | case 'd': { |
118 | setnvalue(L->top, cast(lua_Number, va_arg(argp, int))); | 119 | setnvalue(L->top, cast(lua_Number, va_arg(argp, int))); |
119 | incr_top(L); | 120 | incr_top(L); |
120 | break; | 121 | break; |
121 | case 'f': | 122 | } |
123 | case 'f': { | ||
122 | setnvalue(L->top, cast(lua_Number, va_arg(argp, l_uacNumber))); | 124 | setnvalue(L->top, cast(lua_Number, va_arg(argp, l_uacNumber))); |
123 | incr_top(L); | 125 | incr_top(L); |
124 | break; | 126 | break; |
125 | case '%': | 127 | } |
128 | case 'p': { | ||
129 | char buff[4*sizeof(void *) + 8]; /* should be enough space for a `%p' */ | ||
130 | sprintf(buff, "%p", va_arg(argp, void *)); | ||
131 | pushstr(L, buff); | ||
132 | break; | ||
133 | } | ||
134 | case '%': { | ||
126 | pushstr(L, "%"); | 135 | pushstr(L, "%"); |
127 | break; | 136 | break; |
137 | } | ||
128 | default: { | 138 | default: { |
129 | char buff[3]; | 139 | char buff[3]; |
130 | buff[0] = '%'; | 140 | buff[0] = '%'; |