first version of vararg facility (plus new function "call").

This commit is contained in:
Roberto Ierusalimschy
1996-05-28 18:07:32 -03:00
parent 9a1948e67d
commit 9863223fbf
7 changed files with 217 additions and 115 deletions

63
inout.c
View File

@@ -5,7 +5,7 @@
** Also provides some predefined lua functions.
*/
char *rcs_inout="$Id: inout.c,v 2.35 1996/03/19 16:50:24 roberto Exp roberto $";
char *rcs_inout="$Id: inout.c,v 2.36 1996/03/19 22:28:37 roberto Exp roberto $";
#include <stdio.h>
@@ -93,6 +93,17 @@ void lua_closestring (void)
{
}
static void check_arg (int cond, char *func)
{
if (!cond)
{
char buff[100];
sprintf(buff, "incorrect argument to function `%s'", func);
lua_error(buff);
}
}
/*
** Internal function: do a string
@@ -230,8 +241,7 @@ void luaI_setglobal (void)
{
lua_Object name = lua_getparam(1);
lua_Object value = lua_getparam(2);
if (!lua_isstring(name))
lua_error("incorrect argument to function `setglobal'");
check_arg(lua_isstring(name), "setglobal");
lua_pushobject(value);
lua_storeglobal(lua_getstring(name));
lua_pushobject(value); /* return given value */
@@ -240,7 +250,50 @@ void luaI_setglobal (void)
void luaI_getglobal (void)
{
lua_Object name = lua_getparam(1);
if (!lua_isstring(name))
lua_error("incorrect argument to function `getglobal'");
check_arg(lua_isstring(name), "getglobal");
lua_pushobject(lua_getglobal(lua_getstring(name)));
}
#define MAXPARAMS 256
void luaI_call (void)
{
lua_Object f = lua_getparam(1);
lua_Object arg = lua_getparam(2);
lua_Object temp, params[MAXPARAMS];
int narg, i;
check_arg(lua_istable(arg), "call");
check_arg(lua_isfunction(f), "call");
/* narg = arg.n */
lua_pushobject(arg);
lua_pushstring("n");
temp = lua_getsubscript();
narg = lua_isnumber(temp) ? lua_getnumber(temp) : MAXPARAMS+1;
/* read arg[1...n] */
for (i=0; i<narg; i++)
{
if (i>=MAXPARAMS)
lua_error("argument list too long in function `call'");
lua_pushobject(arg);
lua_pushnumber(i+1);
params[i] = lua_getsubscript();
if (narg == MAXPARAMS+1 && lua_isnil(params[i]))
{
narg = i;
break;
}
}
/* push parameters and do the call */
for (i=0; i<narg; i++)
lua_pushobject(params[i]);
if (lua_callfunction(f))
{ /* error */
lua_error(NULL);
}
else
{ /* push results */
Object r;
arg = lua_getresult(1);
luaI_packarg((arg == LUA_NOOBJECT)?NULL:luaI_Address(arg), &r);
luaI_pushobject(&r);
}
}