diff options
author | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2017-11-04 10:57:02 -0200 |
---|---|---|
committer | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2017-11-04 10:57:02 -0200 |
commit | 93fd67b7936f0f1fc20645d18eb85a193887c315 (patch) | |
tree | 11cd249fad6b0253f0de48b94c08ab08671b8544 /ldo.c | |
parent | 6bb3e40a8d2cdb64a6ac1962d8748dd638a11721 (diff) | |
download | lua-93fd67b7936f0f1fc20645d18eb85a193887c315.tar.gz lua-93fd67b7936f0f1fc20645d18eb85a193887c315.tar.bz2 lua-93fd67b7936f0f1fc20645d18eb85a193887c315.zip |
no more 'CallInfo' structure
Diffstat (limited to 'ldo.c')
-rw-r--r-- | ldo.c | 64 |
1 files changed, 22 insertions, 42 deletions
@@ -1,5 +1,5 @@ | |||
1 | /* | 1 | /* |
2 | ** $Id: ldo.c,v 2.167 2017/11/03 17:22:54 roberto Exp roberto $ | 2 | ** $Id: ldo.c,v 2.168 2017/11/03 20:41:05 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 | */ |
@@ -157,15 +157,11 @@ int luaD_rawrunprotected (lua_State *L, Pfunc f, void *ud) { | |||
157 | ** =================================================================== | 157 | ** =================================================================== |
158 | */ | 158 | */ |
159 | static void correctstack (lua_State *L, StkId oldstack) { | 159 | static void correctstack (lua_State *L, StkId oldstack) { |
160 | CallInfo *ci; | ||
161 | UpVal *up; | 160 | UpVal *up; |
162 | L->top = (L->top - oldstack) + L->stack; | 161 | L->top = (L->top - oldstack) + L->stack; |
163 | L->func = (L->func - oldstack) + L->stack; | 162 | L->func = (L->func - oldstack) + L->stack; |
164 | for (up = L->openupval; up != NULL; up = up->u.open.next) | 163 | for (up = L->openupval; up != NULL; up = up->u.open.next) |
165 | up->v = s2v((uplevel(up) - oldstack) + L->stack); | 164 | up->v = s2v((uplevel(up) - oldstack) + L->stack); |
166 | for (ci = L->ci; ci != NULL; ci = ci->previous) { | ||
167 | ci->func = (ci->func - oldstack) + L->stack; | ||
168 | } | ||
169 | } | 165 | } |
170 | 166 | ||
171 | 167 | ||
@@ -223,10 +219,6 @@ void luaD_shrinkstack (lua_State *L) { | |||
223 | int goodsize = inuse + (inuse / 8) + 2*EXTRA_STACK; | 219 | int goodsize = inuse + (inuse / 8) + 2*EXTRA_STACK; |
224 | if (goodsize > LUAI_MAXSTACK) | 220 | if (goodsize > LUAI_MAXSTACK) |
225 | goodsize = LUAI_MAXSTACK; /* respect stack limit */ | 221 | goodsize = LUAI_MAXSTACK; /* respect stack limit */ |
226 | if (L->stacksize > LUAI_MAXSTACK) /* had been handling stack overflow? */ | ||
227 | luaE_freeCI(L); /* free all CIs (list grew because of an error) */ | ||
228 | else | ||
229 | luaE_shrinkCI(L); /* shrink list */ | ||
230 | /* if thread is currently not handling a stack overflow and its | 222 | /* if thread is currently not handling a stack overflow and its |
231 | good size is smaller than current size, shrink its stack */ | 223 | good size is smaller than current size, shrink its stack */ |
232 | if (inuse <= (LUAI_MAXSTACK - EXTRA_STACK) && | 224 | if (inuse <= (LUAI_MAXSTACK - EXTRA_STACK) && |
@@ -363,7 +355,7 @@ static int moveresults (lua_State *L, StkId firstResult, StkId res, | |||
363 | ** moves current number of results to proper place; returns 0 iff call | 355 | ** moves current number of results to proper place; returns 0 iff call |
364 | ** wanted multiple (variable number of) results. | 356 | ** wanted multiple (variable number of) results. |
365 | */ | 357 | */ |
366 | int luaD_poscall (lua_State *L, CallInfo *ci, StkId firstResult, int nres) { | 358 | int luaD_poscall (lua_State *L, StkId firstResult, int nres) { |
367 | StkId res = L->func; /* res == final position of 1st result */ | 359 | StkId res = L->func; /* res == final position of 1st result */ |
368 | int wanted = res->stkci.nresults; | 360 | int wanted = res->stkci.nresults; |
369 | if (L->hookmask & (LUA_MASKRET | LUA_MASKLINE)) { | 361 | if (L->hookmask & (LUA_MASKRET | LUA_MASKLINE)) { |
@@ -376,18 +368,12 @@ int luaD_poscall (lua_State *L, CallInfo *ci, StkId firstResult, int nres) { | |||
376 | /* 'oldpc' for caller function */ | 368 | /* 'oldpc' for caller function */ |
377 | L->oldpc = (res - res->stkci.previous)->stkci.u.l.savedpc; | 369 | L->oldpc = (res - res->stkci.previous)->stkci.u.l.savedpc; |
378 | } | 370 | } |
379 | L->ci = ci->previous; /* back to caller */ | ||
380 | L->func = res - res->stkci.previous; | 371 | L->func = res - res->stkci.previous; |
381 | lua_assert(L->func == L->ci->func); | ||
382 | /* move results to proper place */ | 372 | /* move results to proper place */ |
383 | return moveresults(L, firstResult, res, nres, wanted); | 373 | return moveresults(L, firstResult, res, nres, wanted); |
384 | } | 374 | } |
385 | 375 | ||
386 | 376 | ||
387 | |||
388 | #define next_ci(L) (L->ci = (L->ci->next ? L->ci->next : luaE_extendCI(L))) | ||
389 | |||
390 | |||
391 | /* | 377 | /* |
392 | ** Prepares a function call: checks the stack, creates a new CallInfo | 378 | ** Prepares a function call: checks the stack, creates a new CallInfo |
393 | ** entry, fills in the relevant information, calls hook if needed. | 379 | ** entry, fills in the relevant information, calls hook if needed. |
@@ -398,7 +384,6 @@ int luaD_poscall (lua_State *L, CallInfo *ci, StkId firstResult, int nres) { | |||
398 | int luaD_precall (lua_State *L, StkId func, int nresults) { | 384 | int luaD_precall (lua_State *L, StkId func, int nresults) { |
399 | lua_CFunction f; | 385 | lua_CFunction f; |
400 | TValue *funcv = s2v(func); | 386 | TValue *funcv = s2v(func); |
401 | CallInfo *ci; | ||
402 | switch (ttype(funcv)) { | 387 | switch (ttype(funcv)) { |
403 | case LUA_TCCL: /* C closure */ | 388 | case LUA_TCCL: /* C closure */ |
404 | f = clCvalue(funcv)->f; | 389 | f = clCvalue(funcv)->f; |
@@ -408,10 +393,9 @@ int luaD_precall (lua_State *L, StkId func, int nresults) { | |||
408 | Cfunc: { | 393 | Cfunc: { |
409 | int n; /* number of returns */ | 394 | int n; /* number of returns */ |
410 | checkstackp(L, LUA_MINSTACK, func); /* ensure minimum stack size */ | 395 | checkstackp(L, LUA_MINSTACK, func); /* ensure minimum stack size */ |
411 | ci = next_ci(L); /* now 'enter' new function */ | ||
412 | func->stkci.nresults = nresults; | 396 | func->stkci.nresults = nresults; |
413 | func->stkci.previous = func - L->func; | 397 | func->stkci.previous = func - L->func; |
414 | L->func = ci->func = func; | 398 | L->func = func; |
415 | setfunctop(func, L->top + LUA_MINSTACK); | 399 | setfunctop(func, L->top + LUA_MINSTACK); |
416 | lua_assert(functop(func) <= L->stack_last); | 400 | lua_assert(functop(func) <= L->stack_last); |
417 | callstatus(func) = 0; | 401 | callstatus(func) = 0; |
@@ -421,7 +405,7 @@ int luaD_precall (lua_State *L, StkId func, int nresults) { | |||
421 | n = (*f)(L); /* do the actual call */ | 405 | n = (*f)(L); /* do the actual call */ |
422 | lua_lock(L); | 406 | lua_lock(L); |
423 | api_checknelems(L, n); | 407 | api_checknelems(L, n); |
424 | luaD_poscall(L, ci, L->top - n, n); | 408 | luaD_poscall(L, L->top - n, n); |
425 | return 1; | 409 | return 1; |
426 | } | 410 | } |
427 | case LUA_TLCL: { /* Lua function: prepare its call */ | 411 | case LUA_TLCL: { /* Lua function: prepare its call */ |
@@ -433,11 +417,10 @@ int luaD_precall (lua_State *L, StkId func, int nresults) { | |||
433 | setnilvalue(s2v(L->top++)); /* complete missing arguments */ | 417 | setnilvalue(s2v(L->top++)); /* complete missing arguments */ |
434 | if (p->is_vararg) | 418 | if (p->is_vararg) |
435 | luaT_adjustvarargs(L, p, n); | 419 | luaT_adjustvarargs(L, p, n); |
436 | ci = next_ci(L); /* now 'enter' new function */ | ||
437 | func->stkci.nresults = nresults; | 420 | func->stkci.nresults = nresults; |
438 | func->stkci.previous = func - L->func; | 421 | func->stkci.previous = func - L->func; |
439 | func->stkci.framesize = fsize + 1; /* size includes function itself */ | 422 | func->stkci.framesize = fsize + 1; /* size includes function itself */ |
440 | L->func = ci->func = func; | 423 | L->func = func; |
441 | L->top = func + 1 + fsize; | 424 | L->top = func + 1 + fsize; |
442 | lua_assert(functop(func) <= L->stack_last); | 425 | lua_assert(functop(func) <= L->stack_last); |
443 | func->stkci.u.l.savedpc = p->code; /* starting point */ | 426 | func->stkci.u.l.savedpc = p->code; /* starting point */ |
@@ -500,7 +483,6 @@ void luaD_callnoyield (lua_State *L, StkId func, int nResults) { | |||
500 | ** continuation function. | 483 | ** continuation function. |
501 | */ | 484 | */ |
502 | static void finishCcall (lua_State *L, int status) { | 485 | static void finishCcall (lua_State *L, int status) { |
503 | CallInfo *ci = L->ci; | ||
504 | StkId func = L->func; | 486 | StkId func = L->func; |
505 | int n; | 487 | int n; |
506 | /* must have a continuation and must be able to call it */ | 488 | /* must have a continuation and must be able to call it */ |
@@ -519,7 +501,7 @@ static void finishCcall (lua_State *L, int status) { | |||
519 | n = (*func->stkci.u.c.k)(L, status, func->stkci.u.c.ctx); | 501 | n = (*func->stkci.u.c.k)(L, status, func->stkci.u.c.ctx); |
520 | lua_lock(L); | 502 | lua_lock(L); |
521 | api_checknelems(L, n); | 503 | api_checknelems(L, n); |
522 | luaD_poscall(L, ci, L->top - n, n); /* finish 'luaD_precall' */ | 504 | luaD_poscall(L, L->top - n, n); /* finish 'luaD_precall' */ |
523 | } | 505 | } |
524 | 506 | ||
525 | 507 | ||
@@ -534,7 +516,7 @@ static void finishCcall (lua_State *L, int status) { | |||
534 | static void unroll (lua_State *L, void *ud) { | 516 | static void unroll (lua_State *L, void *ud) { |
535 | if (ud != NULL) /* error status? */ | 517 | if (ud != NULL) /* error status? */ |
536 | finishCcall(L, *(int *)ud); /* finish 'lua_pcallk' callee */ | 518 | finishCcall(L, *(int *)ud); /* finish 'lua_pcallk' callee */ |
537 | while (L->ci != &L->base_ci) { /* something in the stack */ | 519 | while (L->func != L->stack) { /* something in the stack */ |
538 | if (!isLua(L->func)) /* C function? */ | 520 | if (!isLua(L->func)) /* C function? */ |
539 | finishCcall(L, LUA_YIELD); /* complete its execution */ | 521 | finishCcall(L, LUA_YIELD); /* complete its execution */ |
540 | else { /* Lua function */ | 522 | else { /* Lua function */ |
@@ -549,11 +531,13 @@ static void unroll (lua_State *L, void *ud) { | |||
549 | ** Try to find a suspended protected call (a "recover point") for the | 531 | ** Try to find a suspended protected call (a "recover point") for the |
550 | ** given thread. | 532 | ** given thread. |
551 | */ | 533 | */ |
552 | static CallInfo *findpcall (lua_State *L) { | 534 | static StkId findpcall (lua_State *L) { |
553 | CallInfo *ci; | 535 | StkId func; |
554 | for (ci = L->ci; ci != NULL; ci = ci->previous) { /* search for a pcall */ | 536 | for (func = L->func; |
555 | if (callstatus(ci->func) & CIST_YPCALL) | 537 | func->stkci.previous != 0; |
556 | return ci; | 538 | func -= func->stkci.previous) { |
539 | if (callstatus(func) & CIST_YPCALL) | ||
540 | return func; | ||
557 | } | 541 | } |
558 | return NULL; /* no pending pcall */ | 542 | return NULL; /* no pending pcall */ |
559 | } | 543 | } |
@@ -566,18 +550,17 @@ static CallInfo *findpcall (lua_State *L) { | |||
566 | */ | 550 | */ |
567 | static int recover (lua_State *L, int status) { | 551 | static int recover (lua_State *L, int status) { |
568 | StkId oldtop; | 552 | StkId oldtop; |
569 | CallInfo *ci = findpcall(L); | 553 | StkId recf = findpcall(L); |
570 | if (ci == NULL) return 0; /* no recovery point */ | 554 | if (recf == NULL) return 0; /* no recovery point */ |
571 | /* "finish" luaD_pcall */ | 555 | /* "finish" luaD_pcall */ |
572 | oldtop = ci->func + ci->func->stkci.u2.funcidx; | 556 | oldtop = recf + recf->stkci.u2.funcidx; |
573 | luaF_close(L, oldtop); | 557 | luaF_close(L, oldtop); |
574 | seterrorobj(L, status, oldtop); | 558 | seterrorobj(L, status, oldtop); |
575 | L->ci = ci; | 559 | L->func = recf; |
576 | L->func = ci->func; | 560 | L->allowhook = getoah(callstatus(recf)); /* restore original 'allowhook' */ |
577 | L->allowhook = getoah(callstatus(L->func)); /* restore original 'allowhook' */ | ||
578 | L->nny = 0; /* should be zero to be yieldable */ | 561 | L->nny = 0; /* should be zero to be yieldable */ |
579 | luaD_shrinkstack(L); | 562 | luaD_shrinkstack(L); |
580 | L->errfunc = ci->func->stkci.u.c.old_errfunc; | 563 | L->errfunc = recf->stkci.u.c.old_errfunc; |
581 | return 1; /* continue running the coroutine */ | 564 | return 1; /* continue running the coroutine */ |
582 | } | 565 | } |
583 | 566 | ||
@@ -606,7 +589,6 @@ static int resume_error (lua_State *L, const char *msg, int narg) { | |||
606 | static void resume (lua_State *L, void *ud) { | 589 | static void resume (lua_State *L, void *ud) { |
607 | int n = *(cast(int*, ud)); /* number of arguments */ | 590 | int n = *(cast(int*, ud)); /* number of arguments */ |
608 | StkId firstArg = L->top - n; /* first argument */ | 591 | StkId firstArg = L->top - n; /* first argument */ |
609 | CallInfo *ci = L->ci; | ||
610 | StkId func = L->func; | 592 | StkId func = L->func; |
611 | if (L->status == LUA_OK) { /* starting a coroutine? */ | 593 | if (L->status == LUA_OK) { /* starting a coroutine? */ |
612 | if (!luaD_precall(L, firstArg - 1, LUA_MULTRET)) /* Lua function? */ | 594 | if (!luaD_precall(L, firstArg - 1, LUA_MULTRET)) /* Lua function? */ |
@@ -626,7 +608,7 @@ static void resume (lua_State *L, void *ud) { | |||
626 | api_checknelems(L, n); | 608 | api_checknelems(L, n); |
627 | firstArg = L->top - n; /* yield results come from continuation */ | 609 | firstArg = L->top - n; /* yield results come from continuation */ |
628 | } | 610 | } |
629 | luaD_poscall(L, ci, firstArg, n); /* finish 'luaD_precall' */ | 611 | luaD_poscall(L, firstArg, n); /* finish 'luaD_precall' */ |
630 | } | 612 | } |
631 | unroll(L, NULL); /* run continuation */ | 613 | unroll(L, NULL); /* run continuation */ |
632 | } | 614 | } |
@@ -639,7 +621,7 @@ LUA_API int lua_resume (lua_State *L, lua_State *from, int nargs, | |||
639 | unsigned short oldnny = L->nny; /* save "number of non-yieldable" calls */ | 621 | unsigned short oldnny = L->nny; /* save "number of non-yieldable" calls */ |
640 | lua_lock(L); | 622 | lua_lock(L); |
641 | if (L->status == LUA_OK) { /* may be starting a coroutine */ | 623 | if (L->status == LUA_OK) { /* may be starting a coroutine */ |
642 | if (L->ci != &L->base_ci) /* not in base level? */ | 624 | if (L->func != L->stack) /* not in base level? */ |
643 | return resume_error(L, "cannot resume non-suspended coroutine", nargs); | 625 | return resume_error(L, "cannot resume non-suspended coroutine", nargs); |
644 | } | 626 | } |
645 | else if (L->status != LUA_YIELD) | 627 | else if (L->status != LUA_YIELD) |
@@ -712,7 +694,6 @@ LUA_API int lua_yieldk (lua_State *L, int nresults, lua_KContext ctx, | |||
712 | int luaD_pcall (lua_State *L, Pfunc func, void *u, | 694 | int luaD_pcall (lua_State *L, Pfunc func, void *u, |
713 | ptrdiff_t old_top, ptrdiff_t ef) { | 695 | ptrdiff_t old_top, ptrdiff_t ef) { |
714 | int status; | 696 | int status; |
715 | CallInfo *old_ci = L->ci; | ||
716 | ptrdiff_t oldfunc = savestack(L, L->func); | 697 | ptrdiff_t oldfunc = savestack(L, L->func); |
717 | lu_byte old_allowhooks = L->allowhook; | 698 | lu_byte old_allowhooks = L->allowhook; |
718 | unsigned short old_nny = L->nny; | 699 | unsigned short old_nny = L->nny; |
@@ -723,7 +704,6 @@ int luaD_pcall (lua_State *L, Pfunc func, void *u, | |||
723 | StkId oldtop = restorestack(L, old_top); | 704 | StkId oldtop = restorestack(L, old_top); |
724 | luaF_close(L, oldtop); /* close possible pending closures */ | 705 | luaF_close(L, oldtop); /* close possible pending closures */ |
725 | seterrorobj(L, status, oldtop); | 706 | seterrorobj(L, status, oldtop); |
726 | L->ci = old_ci; | ||
727 | L->func = restorestack(L, oldfunc); | 707 | L->func = restorestack(L, oldfunc); |
728 | L->allowhook = old_allowhooks; | 708 | L->allowhook = old_allowhooks; |
729 | L->nny = old_nny; | 709 | L->nny = old_nny; |