From ac30aad09b8f793f42ccec1d3c463683f8989384 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Sat, 18 Oct 1997 14:33:36 -0200 Subject: new functions "foreach" and "foreachvar" --- lbuiltin.c | 101 +++++++++++++++++++++++++++++++++++++++++++++++-------------- 1 file changed, 79 insertions(+), 22 deletions(-) (limited to 'lbuiltin.c') diff --git a/lbuiltin.c b/lbuiltin.c index 58c3e57b..e6225456 100644 --- a/lbuiltin.c +++ b/lbuiltin.c @@ -1,5 +1,5 @@ /* -** $Id: lbuiltin.c,v 1.1 1997/09/16 19:25:59 roberto Exp roberto $ +** $Id: lbuiltin.c,v 1.2 1997/09/26 15:02:26 roberto Exp roberto $ ** Built-in functions ** See Copyright Notice in lua.h */ @@ -10,7 +10,9 @@ #include "lapi.h" #include "lauxlib.h" #include "lbuiltin.h" +#include "ldo.h" #include "lmem.h" +#include "lobject.h" #include "lstring.h" #include "ltable.h" #include "ltm.h" @@ -18,35 +20,73 @@ +static lua_Object tablearg (int arg) +{ + lua_Object o = lua_getparam(arg); + luaL_arg_check(lua_istable(o), arg, "table expected"); + return o; +} + +static lua_Object functionarg (int arg) +{ + lua_Object o = lua_getparam(arg); + luaL_arg_check(lua_isfunction(o), arg, "function expected"); + return o; +} + + +static void pushstring (TaggedString *s) +{ + TObject o; + o.ttype = LUA_T_STRING; + o.value.ts = s; + luaA_pushobject(&o); +} + + static void nextvar (void) { - lua_Object v = luaL_nonnullarg(1); + TObject *o = luaA_Address(luaL_nonnullarg(1)); TaggedString *g; - if (lua_isnil(v)) + if (ttype(o) == LUA_T_NIL) g = (TaggedString *)luaS_root.next; else { - TObject *o = luaA_Address(v); luaL_arg_check(ttype(o) == LUA_T_STRING, 1, "variable name expected"); g = tsvalue(o); + /* check whether name is in global var list */ luaL_arg_check((GCnode *)g != g->head.next, 1, "variable name expected"); g = (TaggedString *)g->head.next; } - while (g && g->u.globalval.ttype == LUA_T_NIL) + while (g && g->u.globalval.ttype == LUA_T_NIL) /* skip globals with nil */ g = (TaggedString *)g->head.next; if (g) { - lua_pushstring(g->str); + pushstring(g); luaA_pushobject(&g->u.globalval); } } +static void foreachvar (void) +{ + TObject *f = luaA_Address(functionarg(1)); + GCnode *g; + for (g = luaS_root.next; g; g = g->next) { + TaggedString *s = (TaggedString *)g; + if (s->u.globalval.ttype != LUA_T_NIL) { + luaA_pushobject(f); + pushstring(s); + luaA_pushobject(&s->u.globalval); + luaD_call((luaD_stack.top-luaD_stack.stack)-2, 0); + } + } +} + + static void next (void) { - lua_Object o = lua_getparam(1); + lua_Object o = tablearg(1); lua_Object r = luaL_nonnullarg(2); - Node *n; - luaL_arg_check(lua_istable(o), 1, "table expected"); - n = luaH_next(luaA_Address(o), luaA_Address(r)); + Node *n = luaH_next(luaA_Address(o), luaA_Address(r)); if (n) { luaA_pushobject(&n->ref); luaA_pushobject(&n->val); @@ -54,6 +94,23 @@ static void next (void) } +static void foreach (void) +{ + TObject *t = luaA_Address(tablearg(1)); + TObject *f = luaA_Address(functionarg(2)); + Node *nd = avalue(t)->node; + int i; + for (i=0; inhash; (i++, nd++)) { + if (ttype(ref(nd)) != LUA_T_NIL && ttype(val(nd)) != LUA_T_NIL) { + luaA_pushobject(f); + luaA_pushobject(ref(nd)); + luaA_pushobject(val(nd)); + luaD_call((luaD_stack.top-luaD_stack.stack)-2, 0); + } + } +} + + static void internaldostring (void) { lua_Object err = lua_getparam(2); @@ -120,7 +177,8 @@ static void luaI_print (void) int i = 1; lua_Object obj; while ((obj = lua_getparam(i++)) != LUA_NOOBJECT) - printf("%s\n", to_string(obj)); + printf("%s\t", to_string(obj)); + printf("\n"); } @@ -198,13 +256,11 @@ static int getnarg (lua_Object table) static void luaI_call (void) { - lua_Object f = lua_getparam(1); - lua_Object arg = lua_getparam(2); - int withtable = (strcmp(luaL_opt_string(3, ""), "pack") == 0); - int narg, i; - luaL_arg_check(lua_isfunction(f), 1, "function expected"); - luaL_arg_check(lua_istable(arg), 2, "table expected"); - narg = getnarg(arg); + lua_Object f = functionarg(1); + lua_Object arg = tablearg(2); + int pack = (strcmp(luaL_opt_string(3, ""), "pack") == 0); + int narg = getnarg(arg); + int i; /* push arg[1...n] */ for (i=0; i