aboutsummaryrefslogtreecommitdiff
path: root/ldo.c
diff options
context:
space:
mode:
authorRoberto Ierusalimschy <roberto@inf.puc-rio.br>2001-06-08 16:01:38 -0300
committerRoberto Ierusalimschy <roberto@inf.puc-rio.br>2001-06-08 16:01:38 -0300
commitcbc59592ff684b646b21766a66630df1f7974b25 (patch)
tree1e28529cda3b8f6baa63285f6a2571767c3ed05e /ldo.c
parent4905fdd1350bde68cd818b9198f28f5a47c208b0 (diff)
downloadlua-cbc59592ff684b646b21766a66630df1f7974b25.tar.gz
lua-cbc59592ff684b646b21766a66630df1f7974b25.tar.bz2
lua-cbc59592ff684b646b21766a66630df1f7974b25.zip
new definition for `luaD_call' and `luaD_adjusttop'
Diffstat (limited to 'ldo.c')
-rw-r--r--ldo.c55
1 files changed, 20 insertions, 35 deletions
diff --git a/ldo.c b/ldo.c
index cbaca68c..c2d0e92d 100644
--- a/ldo.c
+++ b/ldo.c
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: ldo.c,v 1.134 2001/04/11 18:39:37 roberto Exp roberto $ 2** $Id: ldo.c,v 1.135 2001/06/05 19:27:32 roberto Exp roberto $
3** Stack and Call structure of Lua 3** Stack and Call structure of Lua
4** See Copyright Notice in lua.h 4** See Copyright Notice in lua.h
5*/ 5*/
@@ -62,19 +62,12 @@ void luaD_stackerror (lua_State *L) {
62 62
63 63
64/* 64/*
65** Adjust stack. Set top to base+extra, pushing NILs if needed. 65** adjust top to new value; assume that new top is valid
66** (we cannot add base+extra unless we are sure it fits in the stack;
67** otherwise the result of such operation on pointers is undefined)
68*/ 66*/
69void luaD_adjusttop (lua_State *L, StkId base, int extra) { 67void luaD_adjusttop (lua_State *L, StkId newtop) {
70 int diff = extra-(L->top-base); 68 while (L->top < newtop)
71 if (diff <= 0) 69 setnilvalue(L->top++);
72 L->top = base+extra; 70 L->top = newtop; /* `newtop' could be lower than `top' */
73 else {
74 luaD_checkstack(L, diff);
75 while (diff--)
76 setnilvalue(L->top++);
77 }
78} 71}
79 72
80 73
@@ -140,11 +133,10 @@ static StkId callCclosure (lua_State *L, const struct Closure *cl) {
140/* 133/*
141** Call a function (C or Lua). The function to be called is at *func. 134** Call a function (C or Lua). The function to be called is at *func.
142** The arguments are on the stack, right after the function. 135** The arguments are on the stack, right after the function.
143** When returns, the results are on the stack, starting at the original 136** When returns, all the results are on the stack, starting at the original
144** function position. 137** function position.
145** The number of results is nResults, unless nResults=LUA_MULTRET.
146*/ 138*/
147void luaD_call (lua_State *L, StkId func, int nResults) { 139void luaD_call (lua_State *L, StkId func) {
148 lua_Hook callhook; 140 lua_Hook callhook;
149 StkId firstResult; 141 StkId firstResult;
150 CallInfo ci; 142 CallInfo ci;
@@ -168,20 +160,9 @@ void luaD_call (lua_State *L, StkId func, int nResults) {
168 luaD_callHook(L, callhook, l_s("return")); 160 luaD_callHook(L, callhook, l_s("return"));
169 L->ci = ci.prev; /* unchain callinfo */ 161 L->ci = ci.prev; /* unchain callinfo */
170 /* move results to `func' (to erase parameters and function) */ 162 /* move results to `func' (to erase parameters and function) */
171 if (nResults == LUA_MULTRET) { 163 while (firstResult < L->top)
172 while (firstResult < L->top) /* copy all results */ 164 setobj(func++, firstResult++);
173 setobj(func++, firstResult++); 165 L->top = func;
174 L->top = func;
175 }
176 else { /* copy at most `nResults' */
177 for (; nResults > 0 && firstResult < L->top; nResults--)
178 setobj(func++, firstResult++);
179 L->top = func;
180 for (; nResults > 0; nResults--) { /* if there are not enough results */
181 setnilvalue(L->top); /* adjust the stack */
182 incr_top; /* must check stack space */
183 }
184 }
185 luaC_checkGC(L); 166 luaC_checkGC(L);
186} 167}
187 168
@@ -196,7 +177,9 @@ struct CallS { /* data to `f_call' */
196 177
197static void f_call (lua_State *L, void *ud) { 178static void f_call (lua_State *L, void *ud) {
198 struct CallS *c = (struct CallS *)ud; 179 struct CallS *c = (struct CallS *)ud;
199 luaD_call(L, c->func, c->nresults); 180 luaD_call(L, c->func);
181 if (c->nresults != LUA_MULTRET)
182 luaD_adjusttop(L, c->func + c->nresults);
200} 183}
201 184
202 185
@@ -307,12 +290,14 @@ struct lua_longjmp {
307 290
308 291
309static void message (lua_State *L, const l_char *s) { 292static void message (lua_State *L, const l_char *s) {
310 luaV_getglobal(L, luaS_newliteral(L, l_s(LUA_ERRORMESSAGE)), L->top); 293 StkId top = L->top;
311 if (ttype(L->top) == LUA_TFUNCTION) { 294 luaV_getglobal(L, luaS_newliteral(L, l_s(LUA_ERRORMESSAGE)), top);
295 if (ttype(top) == LUA_TFUNCTION) {
312 incr_top; 296 incr_top;
313 setsvalue(L->top, luaS_new(L, s)); 297 setsvalue(top+1, luaS_new(L, s));
314 incr_top; 298 incr_top;
315 luaD_call(L, L->top-2, 0); 299 luaD_call(L, top);
300 L->top = top;
316 } 301 }
317} 302}
318 303