summaryrefslogtreecommitdiff
path: root/inout.c
diff options
context:
space:
mode:
authorRoberto Ierusalimschy <roberto@inf.puc-rio.br>1997-04-02 19:53:35 -0300
committerRoberto Ierusalimschy <roberto@inf.puc-rio.br>1997-04-02 19:53:35 -0300
commit7c99149a76ce0c7469c9bafec5dfff529f88512c (patch)
tree7e3db5dcafcf9ae7fdc867ad8c2bd1ec9c290f71 /inout.c
parent27d95f18803aded238eaa8adaf7f2a3ee6966fd3 (diff)
downloadlua-7c99149a76ce0c7469c9bafec5dfff529f88512c.tar.gz
lua-7c99149a76ce0c7469c9bafec5dfff529f88512c.tar.bz2
lua-7c99149a76ce0c7469c9bafec5dfff529f88512c.zip
"tostring" gives an overview of a userdata.
Diffstat (limited to '')
-rw-r--r--inout.c49
1 files changed, 37 insertions, 12 deletions
diff --git a/inout.c b/inout.c
index fe45c421..af4ff550 100644
--- a/inout.c
+++ b/inout.c
@@ -5,7 +5,7 @@
5** Also provides some predefined lua functions. 5** Also provides some predefined lua functions.
6*/ 6*/
7 7
8char *rcs_inout="$Id: inout.c,v 2.51 1997/04/01 17:31:42 roberto Exp roberto $"; 8char *rcs_inout="$Id: inout.c,v 2.52 1997/04/01 19:02:43 roberto Exp roberto $";
9 9
10#include <stdio.h> 10#include <stdio.h>
11#include <string.h> 11#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 $";
20#include "hash.h" 20#include "hash.h"
21#include "luamem.h" 21#include "luamem.h"
22#include "fallback.h" 22#include "fallback.h"
23#include "luamem.h"
23 24
24 25
25/* Exported variables */ 26/* Exported variables */
@@ -27,6 +28,12 @@ Word lua_linenumber;
27char *lua_parsedfile; 28char *lua_parsedfile;
28 29
29 30
31static char *typenames[] = { /* ORDER LUA_T */
32 "userdata", "line", "cmark", "mark", "function",
33 "function", "table", "string", "number", "nil",
34 NULL
35};
36
30static FILE *fp; 37static FILE *fp;
31static char *st; 38static char *st;
32 39
@@ -135,16 +142,26 @@ static void lua_internaldofile (void)
135 142
136static char *tostring (lua_Object obj) 143static char *tostring (lua_Object obj)
137{ 144{
138 if (lua_isstring(obj)) /* get strings and numbers */ 145 TObject *o = luaI_Address(obj);
139 return lua_getstring(obj); 146 switch (ttype(o)) {
140 else if (lua_istable(obj)) 147 case LUA_T_NUMBER: case LUA_T_STRING:
141 return "<table>"; 148 return lua_getstring(obj);
142 else if (lua_isfunction(obj)) 149 case LUA_T_ARRAY: case LUA_T_FUNCTION:
143 return "<function>"; 150 case LUA_T_CFUNCTION: case LUA_T_NIL:
144 else if (lua_isnil(obj)) 151 return typenames[-ttype(o)];
145 return "nil"; 152 case LUA_T_USERDATA: {
146 else /* if (lua_isuserdata(obj)) */ 153 char *buff = luaI_buffer(100);
147 return "<userdata>"; 154 int size = o->value.ts->size;
155 int i;
156 strcpy(buff, "userdata: ");
157 if (size > 10) size = 10;
158 for (i=0; i<size; i++)
159 sprintf(buff+strlen(buff), "%.2X",
160 (int)(unsigned char)o->value.ts->str[i]);
161 return buff;
162 }
163 default: return "<unknown object>";
164 }
148} 165}
149 166
150static void luaI_tostring (void) 167static void luaI_tostring (void)
@@ -160,6 +177,14 @@ static void luaI_print (void)
160 printf("%s\n", tostring(obj)); 177 printf("%s\n", tostring(obj));
161} 178}
162 179
180static void luaI_type (void)
181{
182 lua_Object o = lua_getparam(1);
183 luaL_arg_check(o != LUA_NOOBJECT, "type", 1, "no argument");
184 lua_pushstring(typenames[-ttype(luaI_Address(o))]);
185 lua_pushnumber(lua_tag(o));
186}
187
163/* 188/*
164** Internal function: convert an object to a number 189** Internal function: convert an object to a number
165*/ 190*/
@@ -263,7 +288,7 @@ static void luaIl_settag (void)
263 288
264static void luaIl_newtag (void) 289static void luaIl_newtag (void)
265{ 290{
266 lua_pushnumber(lua_newtag(luaL_check_string(1, "newtag"))); 291 lua_pushnumber(lua_newtag());
267} 292}
268 293
269static void basicindex (void) 294static void basicindex (void)