diff options
author | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 1999-06-22 17:37:23 -0300 |
---|---|---|
committer | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 1999-06-22 17:37:23 -0300 |
commit | 521b38532a29ab0590be722712b7c367607e5ba9 (patch) | |
tree | d675eb5dc86c6ccd9db3409be2a6c4bc441f97bf /ldo.c | |
parent | 36b6fe8d175bb2aec8fc55ffb090eab90cb12fd8 (diff) | |
download | lua-521b38532a29ab0590be722712b7c367607e5ba9.tar.gz lua-521b38532a29ab0590be722712b7c367607e5ba9.tar.bz2 lua-521b38532a29ab0590be722712b7c367607e5ba9.zip |
better interfaces for luaD_calln (x luaD_call)
Diffstat (limited to 'ldo.c')
-rw-r--r-- | ldo.c | 58 |
1 files changed, 23 insertions, 35 deletions
@@ -1,5 +1,5 @@ | |||
1 | /* | 1 | /* |
2 | ** $Id: ldo.c,v 1.43 1999/05/24 17:53:03 roberto Exp roberto $ | 2 | ** $Id: ldo.c,v 1.44 1999/06/17 17:04:03 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 | */ |
@@ -145,8 +145,7 @@ static StkId callC (lua_CFunction f, StkId base) { | |||
145 | } | 145 | } |
146 | 146 | ||
147 | 147 | ||
148 | static StkId callCclosure (struct Closure *cl, lua_CFunction f, StkId base) | 148 | static StkId callCclosure (struct Closure *cl, lua_CFunction f, StkId base) { |
149 | { | ||
150 | TObject *pbase; | 149 | TObject *pbase; |
151 | int nup = cl->nelems; /* number of upvalues */ | 150 | int nup = cl->nelems; /* number of upvalues */ |
152 | luaD_checkstack(nup); | 151 | luaD_checkstack(nup); |
@@ -168,15 +167,17 @@ void luaD_callTM (TObject *f, int nParams, int nResults) { | |||
168 | 167 | ||
169 | 168 | ||
170 | /* | 169 | /* |
171 | ** Call a function (C or Lua). The parameters must be on the L->stack.stack, | 170 | ** Call a function (C or Lua). The parameters must be on the stack, |
172 | ** between [L->stack.stack+base,L->stack.top). The function to be called is at L->stack.stack+base-1. | 171 | ** between [top-nArgs,top). The function to be called is right below the |
173 | ** When returns, the results are on the L->stack.stack, between [L->stack.stack+base-1,L->stack.top). | 172 | ** arguments. |
173 | ** When returns, the results are on the stack, between [top-nArgs-1,top). | ||
174 | ** The number of results is nResults, unless nResults=MULT_RET. | 174 | ** The number of results is nResults, unless nResults=MULT_RET. |
175 | */ | 175 | */ |
176 | void luaD_call (StkId base, int nResults) | 176 | void luaD_calln (int nArgs, int nResults) { |
177 | { | 177 | struct Stack *S = &L->stack; /* to optimize */ |
178 | StkId base = (S->top-S->stack)-nArgs; | ||
179 | TObject *func = S->stack+base-1; | ||
178 | StkId firstResult; | 180 | StkId firstResult; |
179 | TObject *func = L->stack.stack+base-1; | ||
180 | int i; | 181 | int i; |
181 | switch (ttype(func)) { | 182 | switch (ttype(func)) { |
182 | case LUA_T_CPROTO: | 183 | case LUA_T_CPROTO: |
@@ -201,24 +202,20 @@ void luaD_call (StkId base, int nResults) | |||
201 | TObject *im = luaT_getimbyObj(func, IM_FUNCTION); | 202 | TObject *im = luaT_getimbyObj(func, IM_FUNCTION); |
202 | if (ttype(im) == LUA_T_NIL) | 203 | if (ttype(im) == LUA_T_NIL) |
203 | lua_error("call expression not a function"); | 204 | lua_error("call expression not a function"); |
204 | luaD_callTM(im, (L->stack.top-L->stack.stack)-(base-1), nResults); | 205 | luaD_callTM(im, (S->top-S->stack)-(base-1), nResults); |
205 | return; | 206 | return; |
206 | } | 207 | } |
207 | } | 208 | } |
208 | /* adjust the number of results */ | 209 | /* adjust the number of results */ |
209 | if (nResults != MULT_RET) | 210 | if (nResults == MULT_RET) |
211 | nResults = (S->top-S->stack)-firstResult; | ||
212 | else | ||
210 | luaD_adjusttop(firstResult+nResults); | 213 | luaD_adjusttop(firstResult+nResults); |
211 | /* move results to base-1 (to erase parameters and function) */ | 214 | /* move results to base-1 (to erase parameters and function) */ |
212 | base--; | 215 | base--; |
213 | nResults = L->stack.top - (L->stack.stack+firstResult); /* actual number of results */ | ||
214 | for (i=0; i<nResults; i++) | 216 | for (i=0; i<nResults; i++) |
215 | *(L->stack.stack+base+i) = *(L->stack.stack+firstResult+i); | 217 | *(S->stack+base+i) = *(S->stack+firstResult+i); |
216 | L->stack.top -= firstResult-base; | 218 | S->top -= firstResult-base; |
217 | } | ||
218 | |||
219 | |||
220 | void luaD_calln (int nArgs, int nResults) { | ||
221 | luaD_call((L->stack.top-L->stack.stack)-nArgs, nResults); | ||
222 | } | 219 | } |
223 | 220 | ||
224 | 221 | ||
@@ -258,32 +255,23 @@ void lua_error (char *s) { | |||
258 | } | 255 | } |
259 | } | 256 | } |
260 | 257 | ||
261 | /* | ||
262 | ** Call the function at L->Cstack.base, and incorporate results on | ||
263 | ** the Lua2C structure. | ||
264 | */ | ||
265 | static void do_callinc (int nResults) | ||
266 | { | ||
267 | StkId base = L->Cstack.base; | ||
268 | luaD_call(base+1, nResults); | ||
269 | L->Cstack.lua2C = base; /* position of the new results */ | ||
270 | L->Cstack.num = (L->stack.top-L->stack.stack) - base; /* number of results */ | ||
271 | L->Cstack.base = base + L->Cstack.num; /* incorporate results on stack */ | ||
272 | } | ||
273 | |||
274 | 258 | ||
275 | /* | 259 | /* |
276 | ** Execute a protected call. Assumes that function is at L->Cstack.base and | 260 | ** Execute a protected call. Assumes that function is at L->Cstack.base and |
277 | ** parameters are on top of it. Leave nResults on the stack. | 261 | ** parameters are on top of it. Leave nResults on the stack. |
278 | */ | 262 | */ |
279 | int luaD_protectedrun (int nResults) { | 263 | int luaD_protectedrun (void) { |
280 | volatile struct C_Lua_Stack oldCLS = L->Cstack; | 264 | volatile struct C_Lua_Stack oldCLS = L->Cstack; |
281 | struct lua_longjmp myErrorJmp; | 265 | struct lua_longjmp myErrorJmp; |
282 | volatile int status; | 266 | volatile int status; |
283 | struct lua_longjmp *volatile oldErr = L->errorJmp; | 267 | struct lua_longjmp *volatile oldErr = L->errorJmp; |
284 | L->errorJmp = &myErrorJmp; | 268 | L->errorJmp = &myErrorJmp; |
285 | if (setjmp(myErrorJmp.b) == 0) { | 269 | if (setjmp(myErrorJmp.b) == 0) { |
286 | do_callinc(nResults); | 270 | StkId base = L->Cstack.base; |
271 | luaD_calln((L->stack.top-L->stack.stack)-base-1, MULT_RET); | ||
272 | L->Cstack.lua2C = base; /* position of the new results */ | ||
273 | L->Cstack.num = (L->stack.top-L->stack.stack) - base; | ||
274 | L->Cstack.base = base + L->Cstack.num; /* incorporate results on stack */ | ||
287 | status = 0; | 275 | status = 0; |
288 | } | 276 | } |
289 | else { /* an error occurred: restore L->Cstack and L->stack.top */ | 277 | else { /* an error occurred: restore L->Cstack and L->stack.top */ |
@@ -338,7 +326,7 @@ static int do_main (ZIO *z, int bin) { | |||
338 | else { | 326 | else { |
339 | unsigned long newelems2 = 2*(L->nblocks-old_blocks); | 327 | unsigned long newelems2 = 2*(L->nblocks-old_blocks); |
340 | L->GCthreshold += newelems2; | 328 | L->GCthreshold += newelems2; |
341 | status = luaD_protectedrun(MULT_RET); | 329 | status = luaD_protectedrun(); |
342 | L->GCthreshold -= newelems2; | 330 | L->GCthreshold -= newelems2; |
343 | } | 331 | } |
344 | } while (bin && status == 0); | 332 | } while (bin && status == 0); |