"call" now handles errors (instead of "dostring")

This commit is contained in:
Roberto Ierusalimschy 1997-11-07 16:19:13 -02:00
parent 92791b9dd6
commit c957b270d2
1 changed files with 29 additions and 20 deletions

View File

@ -1,5 +1,5 @@
/* /*
** $Id: lbuiltin.c,v 1.5 1997/10/24 17:17:24 roberto Exp roberto $ ** $Id: lbuiltin.c,v 1.6 1997/11/04 15:27:53 roberto Exp roberto $
** Built-in functions ** Built-in functions
** See Copyright Notice in lua.h ** See Copyright Notice in lua.h
*/ */
@ -126,18 +126,11 @@ static void foreach (void)
static void internaldostring (void) static void internaldostring (void)
{ {
lua_Object err = lua_getparam(2); if (lua_getparam(2) != LUA_NOOBJECT)
if (err != LUA_NOOBJECT) { /* set new error method */ lua_error("invalid 2nd argument (probably obsolete code)");
lua_pushobject(err);
err = lua_seterrormethod();
}
if (lua_dostring(luaL_check_string(1)) == 0) if (lua_dostring(luaL_check_string(1)) == 0)
if (luaA_passresults() == 0) if (luaA_passresults() == 0)
lua_pushuserdata(NULL); /* at least one result to signal no errors */ lua_pushuserdata(NULL); /* at least one result to signal no errors */
if (err != LUA_NOOBJECT) { /* restore old error method */
lua_pushobject(err);
lua_seterrormethod();
}
} }
@ -259,32 +252,48 @@ static int getnarg (lua_Object table)
{ {
lua_Object temp; lua_Object temp;
/* temp = table.n */ /* temp = table.n */
lua_pushobject(table); lua_pushstring("n"); temp = lua_gettable(); lua_pushobject(table); lua_pushstring("n"); temp = lua_rawgettable();
return (lua_isnumber(temp) ? lua_getnumber(temp) : MAX_WORD); return (lua_isnumber(temp) ? lua_getnumber(temp) : MAX_WORD);
} }
static void luaI_call (void) static void luaI_call (void)
{ {
lua_Object f = functionarg(1); lua_Object f = luaL_nonnullarg(1);
lua_Object arg = tablearg(2); lua_Object arg = tablearg(2);
char *options = luaL_opt_string(3, ""); char *options = luaL_opt_string(3, "");
lua_Object err = lua_getparam(4);
int narg = getnarg(arg); int narg = getnarg(arg);
int i; int i, status;
if (err != LUA_NOOBJECT) { /* set new error method */
lua_pushobject(err);
err = lua_seterrormethod();
}
/* push arg[1...n] */ /* push arg[1...n] */
for (i=0; i<narg; i++) { for (i=0; i<narg; i++) {
lua_Object temp; lua_Object temp;
/* temp = arg[i+1] */ /* temp = arg[i+1] */
lua_pushobject(arg); lua_pushnumber(i+1); temp = lua_gettable(); lua_pushobject(arg); lua_pushnumber(i+1); temp = lua_rawgettable();
if (narg == MAX_WORD && lua_isnil(temp)) if (narg == MAX_WORD && lua_isnil(temp))
break; break;
lua_pushobject(temp); lua_pushobject(temp);
} }
if (lua_callfunction(f)) status = lua_callfunction(f);
lua_error(NULL); if (err != LUA_NOOBJECT) { /* restore old error method */
else if (strchr(options, 'p')) lua_pushobject(err);
luaA_packresults(); lua_seterrormethod();
else }
luaA_passresults(); if (status != 0) { /* error in call? */
if (strchr(options, 'x'))
return; /* return nil to signal the error */
else
lua_error(NULL);
}
else { /* no errors */
if (strchr(options, 'p'))
luaA_packresults();
else
luaA_passresults();
}
} }