| 5408 | |
| 5409 | |
| 5410 | int luaD_precall (lua_State *L, StkId func, int nresults) { |
| 5411 | LClosure *cl; |
| 5412 | ptrdiff_t funcr; |
| 5413 | if (!ttisfunction(func)) /* `func' is not a function? */ |
| 5414 | func = tryfuncTM(L, func); /* check the `function' tag method */ |
| 5415 | funcr = savestack(L, func); |
| 5416 | cl = &clvalue(func)->l; |
| 5417 | L->ci->savedpc = L->savedpc; |
| 5418 | if (!cl->isC) { /* Lua function? prepare its call */ |
| 5419 | CallInfo *ci; |
| 5420 | StkId st, base; |
| 5421 | Proto *p = cl->p; |
| 5422 | luaD_checkstack(L, p->maxstacksize); |
| 5423 | func = restorestack(L, funcr); |
| 5424 | if (!p->is_vararg) { /* no varargs? */ |
| 5425 | base = func + 1; |
| 5426 | if (L->top > base + p->numparams) |
| 5427 | L->top = base + p->numparams; |
| 5428 | } |
| 5429 | else { /* vararg function */ |
| 5430 | int nargs = cast_int(L->top - func) - 1; |
| 5431 | base = adjust_varargs(L, p, nargs); |
| 5432 | func = restorestack(L, funcr); /* previous call may change the stack */ |
| 5433 | } |
| 5434 | ci = inc_ci(L); /* now `enter' new function */ |
| 5435 | ci->func = func; |
| 5436 | L->base = ci->base = base; |
| 5437 | ci->top = L->base + p->maxstacksize; |
| 5438 | lua_assert(ci->top <= L->stack_last); |
| 5439 | L->savedpc = p->code; /* starting point */ |
| 5440 | ci->tailcalls = 0; |
| 5441 | ci->nresults = nresults; |
| 5442 | for (st = L->top; st < ci->top; st++) |
| 5443 | setnilvalue(st); |
| 5444 | L->top = ci->top; |
| 5445 | if (L->hookmask & LUA_MASKCALL) { |
| 5446 | L->savedpc++; /* hooks assume 'pc' is already incremented */ |
| 5447 | luaD_callhook(L, LUA_HOOKCALL, -1); |
| 5448 | L->savedpc--; /* correct 'pc' */ |
| 5449 | } |
| 5450 | return PCRLUA; |
| 5451 | } |
| 5452 | else { /* if is a C function, call it */ |
| 5453 | CallInfo *ci; |
| 5454 | int n; |
| 5455 | luaD_checkstack(L, LUA_MINSTACK); /* ensure minimum stack size */ |
| 5456 | ci = inc_ci(L); /* now `enter' new function */ |
| 5457 | ci->func = restorestack(L, funcr); |
| 5458 | L->base = ci->base = ci->func + 1; |
| 5459 | ci->top = L->top + LUA_MINSTACK; |
| 5460 | lua_assert(ci->top <= L->stack_last); |
| 5461 | ci->nresults = nresults; |
| 5462 | if (L->hookmask & LUA_MASKCALL) |
| 5463 | luaD_callhook(L, LUA_HOOKCALL, -1); |
| 5464 | lua_unlock(L); |
| 5465 | n = (*curr_func(L)->c.f)(L); /* do the actual call */ |
| 5466 | lua_lock(L); |
| 5467 | if (n < 0) /* yielding? */ |
no test coverage detected