aboutsummaryrefslogtreecommitdiff
path: root/inout.c
diff options
context:
space:
mode:
authorRoberto Ierusalimschy <roberto@inf.puc-rio.br>1997-06-09 14:28:14 -0300
committerRoberto Ierusalimschy <roberto@inf.puc-rio.br>1997-06-09 14:28:14 -0300
commitdd22ea4da550c3a158e0f11b928c349336184f85 (patch)
treeb73e2ae0498c4efa30dee04f710fb1a349f164ed /inout.c
parent5fdcfeb353d726a9bcd6d4db5dd0b62b9b8e4b02 (diff)
downloadlua-dd22ea4da550c3a158e0f11b928c349336184f85.tar.gz
lua-dd22ea4da550c3a158e0f11b928c349336184f85.tar.bz2
lua-dd22ea4da550c3a158e0f11b928c349336184f85.zip
new implementation for udata (again they are just void *);
new implementation for the API: most operations now do not disturb structures lua2C and C2lua.
Diffstat (limited to 'inout.c')
-rw-r--r--inout.c70
1 files changed, 42 insertions, 28 deletions
diff --git a/inout.c b/inout.c
index d0bd707e..50782703 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.58 1997/04/15 17:32:47 roberto Exp roberto $"; 8char *rcs_inout="$Id: inout.c,v 2.59 1997/05/26 14:42:51 roberto Exp roberto $";
9 9
10#include <stdio.h> 10#include <stdio.h>
11#include <string.h> 11#include <string.h>
@@ -103,6 +103,7 @@ void lua_closestring (void)
103} 103}
104 104
105 105
106
106static int passresults (void) 107static int passresults (void)
107{ 108{
108 int arg = 0; 109 int arg = 0;
@@ -111,6 +112,25 @@ static int passresults (void)
111 lua_pushobject(obj); 112 lua_pushobject(obj);
112 return arg-1; 113 return arg-1;
113} 114}
115
116
117static void packresults (void)
118{
119 int arg = 0;
120 lua_Object obj;
121 lua_Object table = lua_createtable();
122 while ((obj = lua_getresult(++arg)) != LUA_NOOBJECT) {
123 lua_pushobject(table);
124 lua_pushnumber(arg);
125 lua_pushobject(obj);
126 lua_rawsettable();
127 }
128 lua_pushobject(table);
129 lua_pushstring("n");
130 lua_pushnumber(arg-1);
131 lua_rawsettable();
132 lua_pushobject(table); /* final result */
133}
114 134
115/* 135/*
116** Internal function: do a string 136** Internal function: do a string
@@ -144,14 +164,8 @@ static char *tostring (lua_Object obj)
144 case LUA_T_CFUNCTION: case LUA_T_NIL: 164 case LUA_T_CFUNCTION: case LUA_T_NIL:
145 return luaI_typenames[-ttype(o)]; 165 return luaI_typenames[-ttype(o)];
146 case LUA_T_USERDATA: { 166 case LUA_T_USERDATA: {
147 char *buff = luaI_buffer(100); 167 char *buff = luaI_buffer(30);
148 int size = o->value.ts->size; 168 sprintf(buff, "userdata: %p", o->value.ts->u.v);
149 int i;
150 strcpy(buff, "userdata: ");
151 if (size > 10) size = 10;
152 for (i=0; i<size; i++)
153 sprintf(buff+strlen(buff), "%.2X",
154 (int)(unsigned char)o->value.ts->str[i]);
155 return buff; 169 return buff;
156 } 170 }
157 default: return "<unknown object>"; 171 default: return "<unknown object>";
@@ -237,37 +251,37 @@ static void luatag (void)
237 lua_pushnumber(lua_tag(lua_getparam(1))); 251 lua_pushnumber(lua_tag(lua_getparam(1)));
238} 252}
239 253
240#define MAXPARAMS 256 254
255static int getnarg (lua_Object table)
256{
257 lua_Object temp;
258 /* temp = table.n */
259 lua_pushobject(table); lua_pushstring("n"); temp = lua_gettable();
260 return (lua_isnumber(temp) ? lua_getnumber(temp) : MAX_WORD);
261}
262
241static void luaI_call (void) 263static void luaI_call (void)
242{ 264{
243 lua_Object f = lua_getparam(1); 265 lua_Object f = lua_getparam(1);
244 lua_Object arg = lua_getparam(2); 266 lua_Object arg = lua_getparam(2);
245 lua_Object temp, params[MAXPARAMS]; 267 int withtable = (luaL_opt_string(3, NULL) != NULL);
246 int narg, i; 268 int narg, i;
247 luaL_arg_check(lua_isfunction(f), 1, "function expected"); 269 luaL_arg_check(lua_isfunction(f), 1, "function expected");
248 luaL_arg_check(lua_istable(arg), 2, "table expected"); 270 luaL_arg_check(lua_istable(arg), 2, "table expected");
249 /* narg = arg.n */ 271 narg = getnarg(arg);
250 lua_pushobject(arg); 272 /* push arg[1...n] */
251 lua_pushstring("n");
252 temp = lua_getsubscript();
253 narg = lua_isnumber(temp) ? lua_getnumber(temp) : MAXPARAMS+1;
254 /* read arg[1...n] */
255 for (i=0; i<narg; i++) { 273 for (i=0; i<narg; i++) {
256 if (i>=MAXPARAMS) 274 lua_Object temp;
257 lua_error("argument list too long in function `call'"); 275 /* temp = arg[i+1] */
258 lua_pushobject(arg); 276 lua_pushobject(arg); lua_pushnumber(i+1); temp = lua_gettable();
259 lua_pushnumber(i+1); 277 if (narg == MAX_WORD && lua_isnil(temp))
260 params[i] = lua_getsubscript();
261 if (narg == MAXPARAMS+1 && lua_isnil(params[i])) {
262 narg = i;
263 break; 278 break;
264 } 279 lua_pushobject(temp);
265 } 280 }
266 /* push parameters and do the call */
267 for (i=0; i<narg; i++)
268 lua_pushobject(params[i]);
269 if (lua_callfunction(f)) 281 if (lua_callfunction(f))
270 lua_error(NULL); 282 lua_error(NULL);
283 else if (withtable)
284 packresults();
271 else 285 else
272 passresults(); 286 passresults();
273} 287}