diff options
author | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 1996-05-28 18:07:32 -0300 |
---|---|---|
committer | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 1996-05-28 18:07:32 -0300 |
commit | 9863223fbf512d903a1677c861e4beb4f8feda4d (patch) | |
tree | 964f1b36a5a7c9aff238d454fa4bffe88dacbe2d /inout.c | |
parent | 9a1948e67d940d988260e738f2a251087835a318 (diff) | |
download | lua-9863223fbf512d903a1677c861e4beb4f8feda4d.tar.gz lua-9863223fbf512d903a1677c861e4beb4f8feda4d.tar.bz2 lua-9863223fbf512d903a1677c861e4beb4f8feda4d.zip |
first version of vararg facility (plus new function "call").
Diffstat (limited to 'inout.c')
-rw-r--r-- | inout.c | 63 |
1 files changed, 58 insertions, 5 deletions
@@ -5,7 +5,7 @@ | |||
5 | ** Also provides some predefined lua functions. | 5 | ** Also provides some predefined lua functions. |
6 | */ | 6 | */ |
7 | 7 | ||
8 | char *rcs_inout="$Id: inout.c,v 2.35 1996/03/19 16:50:24 roberto Exp roberto $"; | 8 | char *rcs_inout="$Id: inout.c,v 2.36 1996/03/19 22:28:37 roberto Exp roberto $"; |
9 | 9 | ||
10 | #include <stdio.h> | 10 | #include <stdio.h> |
11 | 11 | ||
@@ -93,6 +93,17 @@ void lua_closestring (void) | |||
93 | { | 93 | { |
94 | } | 94 | } |
95 | 95 | ||
96 | |||
97 | static void check_arg (int cond, char *func) | ||
98 | { | ||
99 | if (!cond) | ||
100 | { | ||
101 | char buff[100]; | ||
102 | sprintf(buff, "incorrect argument to function `%s'", func); | ||
103 | lua_error(buff); | ||
104 | } | ||
105 | } | ||
106 | |||
96 | 107 | ||
97 | /* | 108 | /* |
98 | ** Internal function: do a string | 109 | ** Internal function: do a string |
@@ -230,8 +241,7 @@ void luaI_setglobal (void) | |||
230 | { | 241 | { |
231 | lua_Object name = lua_getparam(1); | 242 | lua_Object name = lua_getparam(1); |
232 | lua_Object value = lua_getparam(2); | 243 | lua_Object value = lua_getparam(2); |
233 | if (!lua_isstring(name)) | 244 | check_arg(lua_isstring(name), "setglobal"); |
234 | lua_error("incorrect argument to function `setglobal'"); | ||
235 | lua_pushobject(value); | 245 | lua_pushobject(value); |
236 | lua_storeglobal(lua_getstring(name)); | 246 | lua_storeglobal(lua_getstring(name)); |
237 | lua_pushobject(value); /* return given value */ | 247 | lua_pushobject(value); /* return given value */ |
@@ -240,7 +250,50 @@ void luaI_setglobal (void) | |||
240 | void luaI_getglobal (void) | 250 | void luaI_getglobal (void) |
241 | { | 251 | { |
242 | lua_Object name = lua_getparam(1); | 252 | lua_Object name = lua_getparam(1); |
243 | if (!lua_isstring(name)) | 253 | check_arg(lua_isstring(name), "getglobal"); |
244 | lua_error("incorrect argument to function `getglobal'"); | ||
245 | lua_pushobject(lua_getglobal(lua_getstring(name))); | 254 | lua_pushobject(lua_getglobal(lua_getstring(name))); |
246 | } | 255 | } |
256 | |||
257 | #define MAXPARAMS 256 | ||
258 | void luaI_call (void) | ||
259 | { | ||
260 | lua_Object f = lua_getparam(1); | ||
261 | lua_Object arg = lua_getparam(2); | ||
262 | lua_Object temp, params[MAXPARAMS]; | ||
263 | int narg, i; | ||
264 | check_arg(lua_istable(arg), "call"); | ||
265 | check_arg(lua_isfunction(f), "call"); | ||
266 | /* narg = arg.n */ | ||
267 | lua_pushobject(arg); | ||
268 | lua_pushstring("n"); | ||
269 | temp = lua_getsubscript(); | ||
270 | narg = lua_isnumber(temp) ? lua_getnumber(temp) : MAXPARAMS+1; | ||
271 | /* read arg[1...n] */ | ||
272 | for (i=0; i<narg; i++) | ||
273 | { | ||
274 | if (i>=MAXPARAMS) | ||
275 | lua_error("argument list too long in function `call'"); | ||
276 | lua_pushobject(arg); | ||
277 | lua_pushnumber(i+1); | ||
278 | params[i] = lua_getsubscript(); | ||
279 | if (narg == MAXPARAMS+1 && lua_isnil(params[i])) | ||
280 | { | ||
281 | narg = i; | ||
282 | break; | ||
283 | } | ||
284 | } | ||
285 | /* push parameters and do the call */ | ||
286 | for (i=0; i<narg; i++) | ||
287 | lua_pushobject(params[i]); | ||
288 | if (lua_callfunction(f)) | ||
289 | { /* error */ | ||
290 | lua_error(NULL); | ||
291 | } | ||
292 | else | ||
293 | { /* push results */ | ||
294 | Object r; | ||
295 | arg = lua_getresult(1); | ||
296 | luaI_packarg((arg == LUA_NOOBJECT)?NULL:luaI_Address(arg), &r); | ||
297 | luaI_pushobject(&r); | ||
298 | } | ||
299 | } | ||