aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--lbuiltin.c36
1 files changed, 24 insertions, 12 deletions
diff --git a/lbuiltin.c b/lbuiltin.c
index e6225456..902fe124 100644
--- a/lbuiltin.c
+++ b/lbuiltin.c
@@ -1,11 +1,12 @@
1/* 1/*
2** $Id: lbuiltin.c,v 1.2 1997/09/26 15:02:26 roberto Exp roberto $ 2** $Id: lbuiltin.c,v 1.3 1997/10/18 16:33:36 roberto Exp roberto $
3** Built-in functions 3** Built-in functions
4** See Copyright Notice in lua.h 4** See Copyright Notice in lua.h
5*/ 5*/
6 6
7 7
8#include <stdio.h> 8#include <stdio.h>
9#include <string.h>
9 10
10#include "lapi.h" 11#include "lapi.h"
11#include "lauxlib.h" 12#include "lauxlib.h"
@@ -68,15 +69,23 @@ static void nextvar (void)
68 69
69static void foreachvar (void) 70static void foreachvar (void)
70{ 71{
71 TObject *f = luaA_Address(functionarg(1)); 72 TObject f = *luaA_Address(functionarg(1));
72 GCnode *g; 73 GCnode *g;
74 StkId name = luaD_Cstack.base++; /* place to keep var name (to avoid GC) */
75 ttype(luaD_stack.stack+name) = LUA_T_NIL;
76 luaD_stack.top++;
73 for (g = luaS_root.next; g; g = g->next) { 77 for (g = luaS_root.next; g; g = g->next) {
74 TaggedString *s = (TaggedString *)g; 78 TaggedString *s = (TaggedString *)g;
75 if (s->u.globalval.ttype != LUA_T_NIL) { 79 if (s->u.globalval.ttype != LUA_T_NIL) {
76 luaA_pushobject(f); 80 ttype(luaD_stack.stack+name) = LUA_T_STRING;
81 tsvalue(luaD_stack.stack+name) = s; /* keep s on stack to avoid GC */
82 luaA_pushobject(&f);
77 pushstring(s); 83 pushstring(s);
78 luaA_pushobject(&s->u.globalval); 84 luaA_pushobject(&s->u.globalval);
79 luaD_call((luaD_stack.top-luaD_stack.stack)-2, 0); 85 luaD_call((luaD_stack.top-luaD_stack.stack)-2, 1);
86 if (ttype(luaD_stack.top-1) != LUA_T_NIL)
87 return;
88 luaD_stack.top--;
80 } 89 }
81 } 90 }
82} 91}
@@ -96,16 +105,19 @@ static void next (void)
96 105
97static void foreach (void) 106static void foreach (void)
98{ 107{
99 TObject *t = luaA_Address(tablearg(1)); 108 TObject t = *luaA_Address(tablearg(1));
100 TObject *f = luaA_Address(functionarg(2)); 109 TObject f = *luaA_Address(functionarg(2));
101 Node *nd = avalue(t)->node;
102 int i; 110 int i;
103 for (i=0; i<avalue(t)->nhash; (i++, nd++)) { 111 for (i=0; i<avalue(&t)->nhash; i++) {
112 Node *nd = &(avalue(&t)->node[i]);
104 if (ttype(ref(nd)) != LUA_T_NIL && ttype(val(nd)) != LUA_T_NIL) { 113 if (ttype(ref(nd)) != LUA_T_NIL && ttype(val(nd)) != LUA_T_NIL) {
105 luaA_pushobject(f); 114 luaA_pushobject(&f);
106 luaA_pushobject(ref(nd)); 115 luaA_pushobject(ref(nd));
107 luaA_pushobject(val(nd)); 116 luaA_pushobject(val(nd));
108 luaD_call((luaD_stack.top-luaD_stack.stack)-2, 0); 117 luaD_call((luaD_stack.top-luaD_stack.stack)-2, 1);
118 if (ttype(luaD_stack.top-1) != LUA_T_NIL)
119 return;
120 luaD_stack.top--;
109 } 121 }
110 } 122 }
111} 123}
@@ -258,7 +270,7 @@ static void luaI_call (void)
258{ 270{
259 lua_Object f = functionarg(1); 271 lua_Object f = functionarg(1);
260 lua_Object arg = tablearg(2); 272 lua_Object arg = tablearg(2);
261 int pack = (strcmp(luaL_opt_string(3, ""), "pack") == 0); 273 char *options = luaL_opt_string(3, "");
262 int narg = getnarg(arg); 274 int narg = getnarg(arg);
263 int i; 275 int i;
264 /* push arg[1...n] */ 276 /* push arg[1...n] */
@@ -272,7 +284,7 @@ static void luaI_call (void)
272 } 284 }
273 if (lua_callfunction(f)) 285 if (lua_callfunction(f))
274 lua_error(NULL); 286 lua_error(NULL);
275 else if (pack) 287 else if (strchr(options, 'p'))
276 luaA_packresults(); 288 luaA_packresults();
277 else 289 else
278 luaA_passresults(); 290 luaA_passresults();