"tostring" gives an overview of a userdata.

This commit is contained in:
Roberto Ierusalimschy
1997-04-02 19:53:35 -03:00
parent 27d95f1880
commit 7c99149a76

49
inout.c
View File

@@ -5,7 +5,7 @@
** Also provides some predefined lua functions. ** Also provides some predefined lua functions.
*/ */
char *rcs_inout="$Id: inout.c,v 2.51 1997/04/01 17:31:42 roberto Exp roberto $"; char *rcs_inout="$Id: inout.c,v 2.52 1997/04/01 19:02:43 roberto Exp roberto $";
#include <stdio.h> #include <stdio.h>
#include <string.h> #include <string.h>
@@ -20,6 +20,7 @@ char *rcs_inout="$Id: inout.c,v 2.51 1997/04/01 17:31:42 roberto Exp roberto $";
#include "hash.h" #include "hash.h"
#include "luamem.h" #include "luamem.h"
#include "fallback.h" #include "fallback.h"
#include "luamem.h"
/* Exported variables */ /* Exported variables */
@@ -27,6 +28,12 @@ Word lua_linenumber;
char *lua_parsedfile; char *lua_parsedfile;
static char *typenames[] = { /* ORDER LUA_T */
"userdata", "line", "cmark", "mark", "function",
"function", "table", "string", "number", "nil",
NULL
};
static FILE *fp; static FILE *fp;
static char *st; static char *st;
@@ -135,16 +142,26 @@ static void lua_internaldofile (void)
static char *tostring (lua_Object obj) static char *tostring (lua_Object obj)
{ {
if (lua_isstring(obj)) /* get strings and numbers */ TObject *o = luaI_Address(obj);
return lua_getstring(obj); switch (ttype(o)) {
else if (lua_istable(obj)) case LUA_T_NUMBER: case LUA_T_STRING:
return "<table>"; return lua_getstring(obj);
else if (lua_isfunction(obj)) case LUA_T_ARRAY: case LUA_T_FUNCTION:
return "<function>"; case LUA_T_CFUNCTION: case LUA_T_NIL:
else if (lua_isnil(obj)) return typenames[-ttype(o)];
return "nil"; case LUA_T_USERDATA: {
else /* if (lua_isuserdata(obj)) */ char *buff = luaI_buffer(100);
return "<userdata>"; int size = o->value.ts->size;
int i;
strcpy(buff, "userdata: ");
if (size > 10) size = 10;
for (i=0; i<size; i++)
sprintf(buff+strlen(buff), "%.2X",
(int)(unsigned char)o->value.ts->str[i]);
return buff;
}
default: return "<unknown object>";
}
} }
static void luaI_tostring (void) static void luaI_tostring (void)
@@ -160,6 +177,14 @@ static void luaI_print (void)
printf("%s\n", tostring(obj)); printf("%s\n", tostring(obj));
} }
static void luaI_type (void)
{
lua_Object o = lua_getparam(1);
luaL_arg_check(o != LUA_NOOBJECT, "type", 1, "no argument");
lua_pushstring(typenames[-ttype(luaI_Address(o))]);
lua_pushnumber(lua_tag(o));
}
/* /*
** Internal function: convert an object to a number ** Internal function: convert an object to a number
*/ */
@@ -263,7 +288,7 @@ static void luaIl_settag (void)
static void luaIl_newtag (void) static void luaIl_newtag (void)
{ {
lua_pushnumber(lua_newtag(luaL_check_string(1, "newtag"))); lua_pushnumber(lua_newtag());
} }
static void basicindex (void) static void basicindex (void)