** Prepare a function for a tail call, building its call info on top ** of the current call info. 'narg1' is the number of arguments plus 1 ** (so that it includes the function itself). Return the number of ** results, if it was a C function, or -1 for a Lua function. */
| 667 | ** results, if it was a C function, or -1 for a Lua function. |
| 668 | */ |
| 669 | int luaD_pretailcall (lua_State *L, CallInfo *ci, StkId func, |
| 670 | int narg1, int delta) { |
| 671 | unsigned status = LUA_MULTRET + 1; |
| 672 | retry: |
| 673 | switch (ttypetag(s2v(func))) { |
| 674 | case LUA_VCCL: /* C closure */ |
| 675 | return precallC(L, func, status, clCvalue(s2v(func))->f); |
| 676 | case LUA_VLCF: /* light C function */ |
| 677 | return precallC(L, func, status, fvalue(s2v(func))); |
| 678 | case LUA_VLCL: { /* Lua function */ |
| 679 | Proto *p = clLvalue(s2v(func))->p; |
| 680 | int fsize = p->maxstacksize; /* frame size */ |
| 681 | int nfixparams = p->numparams; |
| 682 | int i; |
| 683 | checkstackp(L, fsize - delta, func); |
| 684 | ci->func.p -= delta; /* restore 'func' (if vararg) */ |
| 685 | for (i = 0; i < narg1; i++) /* move down function and arguments */ |
| 686 | setobjs2s(L, ci->func.p + i, func + i); |
| 687 | func = ci->func.p; /* moved-down function */ |
| 688 | for (; narg1 <= nfixparams; narg1++) |
| 689 | setnilvalue(s2v(func + narg1)); /* complete missing arguments */ |
| 690 | ci->top.p = func + 1 + fsize; /* top for new function */ |
| 691 | lua_assert(ci->top.p <= L->stack_last.p); |
| 692 | ci->u.l.savedpc = p->code; /* starting point */ |
| 693 | ci->callstatus |= CIST_TAIL; |
| 694 | L->top.p = func + narg1; /* set top */ |
| 695 | return -1; |
| 696 | } |
| 697 | default: { /* not a function */ |
| 698 | checkstackp(L, 1, func); /* space for metamethod */ |
| 699 | status = tryfuncTM(L, func, status); /* try '__call' metamethod */ |
| 700 | narg1++; |
| 701 | goto retry; /* try again */ |
| 702 | } |
| 703 | } |
| 704 | } |
| 705 | |
| 706 | |
| 707 | /* |
no test coverage detected