- Main Page
- Related Pages
- Modules
- Data Structures
- Files

# lua/ldo.c

Go to the documentation of this file.

    00001 /*
    00002 ** $Id: ldo.c,v 2.38 2006/06/05 19:36:14 roberto Exp $
    00003 ** Stack and Call structure of Lua
    00004 ** See Copyright Notice in lua.h
    00005 */
    00006
    00007
    00008 #include <setjmp.h>
    00009 #include <stdlib.h>
    00010 #include <string.h>
    00011
    00012 #define ldo_c
    00013 #define LUA_CORE
    00014
    00015 #include "lua.h"
    00016
    00017 #include "ldebug.h"
    00018 #include "ldo.h"
    00019 #include "lfunc.h"
    00020 #include "lgc.h"
    00021 #include "lmem.h"
    00022 #include "lobject.h"
    00023 #include "lopcodes.h"
    00024 #include "lparser.h"
    00025 #include "lstate.h"
    00026 #include "lstring.h"
    00027 #include "ltable.h"
    00028 #include "ltm.h"
    00029 #include "lundump.h"
    00030 #include "lvm.h"
    00031 #include "lzio.h"
    00032
    00033
    00034
    00035
    00036 /*
    00037 ** {======================================================
    00038 ** Error-recovery functions
    00039 ** =======================================================
    00040 */
    00041
    00042
    00043 /* chain list of long jump buffers */
    00044 struct lua_longjmp {
    00045   struct lua_longjmp *previous;
    00046   luai_jmpbuf b;
    00047   volatile int status;  /* error code */
    00048 };
    00049
    00050
    00051 void luaD_seterrorobj (lua_State *L, int errcode, StkId oldtop) {
    00052   switch (errcode) {
    00053     case LUA_ERRMEM: {
    00054       setsvalue2s(L, oldtop, luaS_newliteral(L, MEMERRMSG));
    00055       break;
    00056     }
    00057     case LUA_ERRERR: {
    00058       setsvalue2s(L, oldtop, luaS_newliteral(L, "error in error handling"));
    00059       break;
    00060     }
    00061     case LUA_ERRSYNTAX:
    00062     case LUA_ERRRUN: {
    00063       setobjs2s(L, oldtop, L->top - 1);  /* error message on current top */
    00064       break;
    00065     }
    00066   }
    00067   L->top = oldtop + 1;
    00068 }
    00069
    00070
    00071 static void restore_stack_limit (lua_State *L) {
    00072   lua_assert(L->stack_last - L->stack == L->stacksize - EXTRA_STACK - 1);
    00073   if (L->size_ci > LUAI_MAXCALLS) {  /* there was an overflow? */
    00074     int inuse = cast_int(L->ci - L->base_ci);
    00075     if (inuse + 1 < LUAI_MAXCALLS)  /* can `undo' overflow? */
    00076       luaD_reallocCI(L, LUAI_MAXCALLS);
    00077   }
    00078 }
    00079
    00080
    00081 static void resetstack (lua_State *L, int status) {
    00082   L->ci = L->base_ci;
    00083   L->base = L->ci->base;
    00084   luaF_close(L, L->base);  /* close eventual pending closures */
    00085   luaD_seterrorobj(L, status, L->base);
    00086   L->nCcalls = 0;
    00087   L->allowhook = 1;
    00088   restore_stack_limit(L);
    00089   L->errfunc = 0;
    00090   L->errorJmp = NULL;
    00091 }
    00092
    00093
    00094 void luaD_throw (lua_State *L, int errcode) {
    00095   if (L->errorJmp) {
    00096     L->errorJmp->status = errcode;
    00097     LUAI_THROW(L, L->errorJmp);
    00098   }
    00099   else {
    00100     L->status = cast_byte(errcode);
    00101     if (G(L)->panic) {
    00102       resetstack(L, errcode);
    00103       lua_unlock(L);
    00104       G(L)->panic(L);
    00105     }
    00106     exit(EXIT_FAILURE);
    00107   }
    00108 }
    00109
    00110
    00111 int luaD_rawrunprotected (lua_State *L, Pfunc f, void *ud) {
    00112   struct lua_longjmp lj;
    00113   lj.status = 0;
    00114   lj.previous = L->errorJmp;  /* chain new error handler */
    00115   L->errorJmp = &lj;
    00116   LUAI_TRY(L, &lj,
    00117     (*f)(L, ud);
    00118   );
    00119   L->errorJmp = lj.previous;  /* restore old error handler */
    00120   return lj.status;
    00121 }
    00122
    00123 /* }====================================================== */
    00124
    00125
    00126 static void correctstack (lua_State *L, TValue *oldstack) {
    00127   CallInfo *ci;
    00128   GCObject *up;
    00129   L->top = (L->top - oldstack) + L->stack;
    00130   for (up = L->openupval; up != NULL; up = up->gch.next)
    00131     gco2uv(up)->v = (gco2uv(up)->v - oldstack) + L->stack;
    00132   for (ci = L->base_ci; ci <= L->ci; ci++) {
    00133     ci->top = (ci->top - oldstack) + L->stack;
    00134     ci->base = (ci->base - oldstack) + L->stack;
    00135     ci->func = (ci->func - oldstack) + L->stack;
    00136   }
    00137   L->base = (L->base - oldstack) + L->stack;
    00138 }
    00139
    00140
    00141 void luaD_reallocstack (lua_State *L, int newsize) {
    00142   TValue *oldstack = L->stack;
    00143   int realsize = newsize + 1 + EXTRA_STACK;
    00144   lua_assert(L->stack_last - L->stack == L->stacksize - EXTRA_STACK - 1);
    00145   luaM_reallocvector(L, L->stack, L->stacksize, realsize, TValue);
    00146   L->stacksize = realsize;
    00147   L->stack_last = L->stack+newsize;
    00148   correctstack(L, oldstack);
    00149 }
    00150
    00151
    00152 void luaD_reallocCI (lua_State *L, int newsize) {
    00153   CallInfo *oldci = L->base_ci;
    00154   luaM_reallocvector(L, L->base_ci, L->size_ci, newsize, CallInfo);
    00155   L->size_ci = newsize;
    00156   L->ci = (L->ci - oldci) + L->base_ci;
    00157   L->end_ci = L->base_ci + L->size_ci - 1;
    00158 }
    00159
    00160
    00161 void luaD_growstack (lua_State *L, int n) {
    00162   if (n <= L->stacksize)  /* double size is enough? */
    00163     luaD_reallocstack(L, 2*L->stacksize);
    00164   else
    00165     luaD_reallocstack(L, L->stacksize + n);
    00166 }
    00167
    00168
    00169 static CallInfo *growCI (lua_State *L) {
    00170   if (L->size_ci > LUAI_MAXCALLS)  /* overflow while handling overflow? */
    00171     luaD_throw(L, LUA_ERRERR);
    00172   else {
    00173     luaD_reallocCI(L, 2*L->size_ci);
    00174     if (L->size_ci > LUAI_MAXCALLS)
    00175       luaG_runerror(L, "stack overflow");
    00176   }
    00177   return ++L->ci;
    00178 }
    00179
    00180
    00181 void luaD_callhook (lua_State *L, int event, int line) {
    00182   lua_Hook hook = L->hook;
    00183   if (hook && L->allowhook) {
    00184     ptrdiff_t top = savestack(L, L->top);
    00185     ptrdiff_t ci_top = savestack(L, L->ci->top);
    00186     lua_Debug ar;
    00187     ar.event = event;
    00188     ar.currentline = line;
    00189     if (event == LUA_HOOKTAILRET)
    00190       ar.i_ci = 0;  /* tail call; no debug information about it */
    00191     else
    00192       ar.i_ci = cast_int(L->ci - L->base_ci);
    00193     luaD_checkstack(L, LUA_MINSTACK);  /* ensure minimum stack size */
    00194     L->ci->top = L->top + LUA_MINSTACK;
    00195     lua_assert(L->ci->top <= L->stack_last);
    00196     L->allowhook = 0;  /* cannot call hooks inside a hook */
    00197     lua_unlock(L);
    00198     (*hook)(L, &ar);
    00199     lua_lock(L);
    00200     lua_assert(!L->allowhook);
    00201     L->allowhook = 1;
    00202     L->ci->top = restorestack(L, ci_top);
    00203     L->top = restorestack(L, top);
    00204   }
    00205 }
    00206
    00207
    00208 static StkId adjust_varargs (lua_State *L, Proto *p, int actual) {
    00209   int i;
    00210   int nfixargs = p->numparams;
    00211   Table *htab = NULL;
    00212   StkId base, fixed;
    00213   for (; actual < nfixargs; ++actual)
    00214     setnilvalue(L->top++);
    00215 #if defined(LUA_COMPAT_VARARG)
    00216   if (p->is_vararg & VARARG_NEEDSARG) { /* compat. with old-style vararg? */
    00217     int nvar = actual - nfixargs;  /* number of extra arguments */
    00218     lua_assert(p->is_vararg & VARARG_HASARG);
    00219     luaC_checkGC(L);
    00220     htab = luaH_new(L, nvar, 1);  /* create `arg' table */
    00221     for (i=0; i<nvar; i++)  /* put extra arguments into `arg' table */
    00222       setobj2n(L, luaH_setnum(L, htab, i+1), L->top - nvar + i);
    00223     /* store counter in field `n' */
    00224     setnvalue(luaH_setstr(L, htab, luaS_newliteral(L, "n")), cast_num(nvar));
    00225   }
    00226 #endif
    00227   /* move fixed parameters to final position */
    00228   fixed = L->top - actual;  /* first fixed argument */
    00229   base = L->top;  /* final position of first argument */
    00230   for (i=0; i<nfixargs; i++) {
    00231     setobjs2s(L, L->top++, fixed+i);
    00232     setnilvalue(fixed+i);
    00233   }
    00234   /* add `arg' parameter */
    00235   if (htab) {
    00236     sethvalue(L, L->top++, htab);
    00237     lua_assert(iswhite(obj2gco(htab)));
    00238   }
    00239   return base;
    00240 }
    00241
    00242
    00243 static StkId tryfuncTM (lua_State *L, StkId func) {
    00244   const TValue *tm = luaT_gettmbyobj(L, func, TM_CALL);
    00245   StkId p;
    00246   ptrdiff_t funcr = savestack(L, func);
    00247   if (!ttisfunction(tm))
    00248     luaG_typeerror(L, func, "call");
    00249   /* Open a hole inside the stack at `func' */
    00250   for (p = L->top; p > func; p--) setobjs2s(L, p, p-1);
    00251   incr_top(L);
    00252   func = restorestack(L, funcr);  /* previous call may change stack */
    00253   setobj2s(L, func, tm);  /* tag method is the new function to be called */
    00254   return func;
    00255 }
    00256
    00257
    00258
    00259 #define inc_ci(L) \
    00260   ((L->ci == L->end_ci) ? growCI(L) : \
    00261    (condhardstacktests(luaD_reallocCI(L, L->size_ci)), ++L->ci))
    00262
    00263
    00264 int luaD_precall (lua_State *L, StkId func, int nresults) {
    00265   LClosure *cl;
    00266   ptrdiff_t funcr;
    00267   if (!ttisfunction(func)) /* `func' is not a function? */
    00268     func = tryfuncTM(L, func);  /* check the `function' tag method */
    00269   funcr = savestack(L, func);
    00270   cl = &clvalue(func)->l;
    00271   L->ci->savedpc = L->savedpc;
    00272   if (!cl->isC) {  /* Lua function? prepare its call */
    00273     CallInfo *ci;
    00274     StkId st, base;
    00275     Proto *p = cl->p;
    00276     luaD_checkstack(L, p->maxstacksize);
    00277     func = restorestack(L, funcr);
    00278     if (!p->is_vararg) {  /* no varargs? */
    00279       base = func + 1;
    00280       if (L->top > base + p->numparams)
    00281         L->top = base + p->numparams;
    00282     }
    00283     else {  /* vararg function */
    00284       int nargs = cast_int(L->top - func) - 1;
    00285       base = adjust_varargs(L, p, nargs);
    00286       func = restorestack(L, funcr);  /* previous call may change the stack */
    00287     }
    00288     ci = inc_ci(L);  /* now `enter' new function */
    00289     ci->func = func;
    00290     L->base = ci->base = base;
    00291     ci->top = L->base + p->maxstacksize;
    00292     lua_assert(ci->top <= L->stack_last);
    00293     L->savedpc = p->code;  /* starting point */
    00294     ci->tailcalls = 0;
    00295     ci->nresults = nresults;
    00296     for (st = L->top; st < ci->top; st++)
    00297       setnilvalue(st);
    00298     L->top = ci->top;
    00299     if (L->hookmask & LUA_MASKCALL) {
    00300       L->savedpc++;  /* hooks assume 'pc' is already incremented */
    00301       luaD_callhook(L, LUA_HOOKCALL, -1);
    00302       L->savedpc--;  /* correct 'pc' */
    00303     }
    00304     return PCRLUA;
    00305   }
    00306   else {  /* if is a C function, call it */
    00307     CallInfo *ci;
    00308     int n;
    00309     luaD_checkstack(L, LUA_MINSTACK);  /* ensure minimum stack size */
    00310     ci = inc_ci(L);  /* now `enter' new function */
    00311     ci->func = restorestack(L, funcr);
    00312     L->base = ci->base = ci->func + 1;
    00313     ci->top = L->top + LUA_MINSTACK;
    00314     lua_assert(ci->top <= L->stack_last);
    00315     ci->nresults = nresults;
    00316     if (L->hookmask & LUA_MASKCALL)
    00317       luaD_callhook(L, LUA_HOOKCALL, -1);
    00318     lua_unlock(L);
    00319     n = (*curr_func(L)->c.f)(L);  /* do the actual call */
    00320     lua_lock(L);
    00321     if (n < 0)  /* yielding? */
    00322       return PCRYIELD;
    00323     else {
    00324       luaD_poscall(L, L->top - n);
    00325       return PCRC;
    00326     }
    00327   }
    00328 }
    00329
    00330
    00331 static StkId callrethooks (lua_State *L, StkId firstResult) {
    00332   ptrdiff_t fr = savestack(L, firstResult);  /* next call may change stack */
    00333   luaD_callhook(L, LUA_HOOKRET, -1);
    00334   if (f_isLua(L->ci)) {  /* Lua function? */
    00335     while (L->ci->tailcalls--)  /* call hook for eventual tail calls */
    00336       luaD_callhook(L, LUA_HOOKTAILRET, -1);
    00337   }
    00338   return restorestack(L, fr);
    00339 }
    00340
    00341
    00342 int luaD_poscall (lua_State *L, StkId firstResult) {
    00343   StkId res;
    00344   int wanted, i;
    00345   CallInfo *ci;
    00346   if (L->hookmask & LUA_MASKRET)
    00347     firstResult = callrethooks(L, firstResult);
    00348   ci = L->ci--;
    00349   res = ci->func;  /* res == final position of 1st result */
    00350   wanted = ci->nresults;
    00351   L->base = (ci - 1)->base;  /* restore base */
    00352   L->savedpc = (ci - 1)->savedpc;  /* restore savedpc */
    00353   /* move results to correct place */
    00354   for (i = wanted; i != 0 && firstResult < L->top; i--)
    00355     setobjs2s(L, res++, firstResult++);
    00356   while (i-- > 0)
    00357     setnilvalue(res++);
    00358   L->top = res;
    00359   return (wanted - LUA_MULTRET);  /* 0 iff wanted == LUA_MULTRET */
    00360 }
    00361
    00362
    00363 /*
    00364 ** Call a function (C or Lua). The function to be called is at *func.
    00365 ** The arguments are on the stack, right after the function.
    00366 ** When returns, all the results are on the stack, starting at the original
    00367 ** function position.
    00368 */
    00369 void luaD_call (lua_State *L, StkId func, int nResults) {
    00370   if (++L->nCcalls >= LUAI_MAXCCALLS) {
    00371     if (L->nCcalls == LUAI_MAXCCALLS)
    00372       luaG_runerror(L, "C stack overflow");
    00373     else if (L->nCcalls >= (LUAI_MAXCCALLS + (LUAI_MAXCCALLS>>3)))
    00374       luaD_throw(L, LUA_ERRERR);  /* error while handing stack error */
    00375   }
    00376   if (luaD_precall(L, func, nResults) == PCRLUA)  /* is a Lua function? */
    00377     luaV_execute(L, 1);  /* call it */
    00378   L->nCcalls--;
    00379   luaC_checkGC(L);
    00380 }
    00381
    00382
    00383 static void resume (lua_State *L, void *ud) {
    00384   StkId firstArg = cast(StkId, ud);
    00385   CallInfo *ci = L->ci;
    00386   if (L->status == 0) {  /* start coroutine? */
    00387     lua_assert(ci == L->base_ci && firstArg > L->base);
    00388     if (luaD_precall(L, firstArg - 1, LUA_MULTRET) != PCRLUA)
    00389       return;
    00390   }
    00391   else {  /* resuming from previous yield */
    00392     lua_assert(L->status == LUA_YIELD);
    00393     L->status = 0;
    00394     if (!f_isLua(ci)) {  /* `common' yield? */
    00395       /* finish interrupted execution of `OP_CALL' */
    00396       lua_assert(GET_OPCODE(*((ci-1)->savedpc - 1)) == OP_CALL ||
    00397                  GET_OPCODE(*((ci-1)->savedpc - 1)) == OP_TAILCALL);
    00398       if (luaD_poscall(L, firstArg))  /* complete it... */
    00399         L->top = L->ci->top;  /* and correct top if not multiple results */
    00400     }
    00401     else  /* yielded inside a hook: just continue its execution */
    00402       L->base = L->ci->base;
    00403   }
    00404   luaV_execute(L, cast_int(L->ci - L->base_ci));
    00405 }
    00406
    00407
    00408 static int resume_error (lua_State *L, const char *msg) {
    00409   L->top = L->ci->base;
    00410   setsvalue2s(L, L->top, luaS_new(L, msg));
    00411   incr_top(L);
    00412   lua_unlock(L);
    00413   return LUA_ERRRUN;
    00414 }
    00415
    00416
    00417 LUA_API int lua_resume (lua_State *L, int nargs) {
    00418   int status;
    00419   lua_lock(L);
    00420   if (L->status != LUA_YIELD) {
    00421     if (L->status != 0)
    00422       return resume_error(L, "cannot resume dead coroutine");
    00423     else if (L->ci != L->base_ci)
    00424       return resume_error(L, "cannot resume non-suspended coroutine");
    00425   }
    00426   luai_userstateresume(L, nargs);
    00427   lua_assert(L->errfunc == 0 && L->nCcalls == 0);
    00428   status = luaD_rawrunprotected(L, resume, L->top - nargs);
    00429   if (status != 0) {  /* error? */
    00430     L->status = cast_byte(status);  /* mark thread as `dead' */
    00431     luaD_seterrorobj(L, status, L->top);
    00432     L->ci->top = L->top;
    00433   }
    00434   else
    00435     status = L->status;
    00436   lua_unlock(L);
    00437   return status;
    00438 }
    00439
    00440
    00441 LUA_API int lua_yield (lua_State *L, int nresults) {
    00442   luai_userstateyield(L, nresults);
    00443   lua_lock(L);
    00444   if (L->nCcalls > 0)
    00445     luaG_runerror(L, "attempt to yield across metamethod/C-call boundary");
    00446   L->base = L->top - nresults;  /* protect stack slots below */
    00447   L->status = LUA_YIELD;
    00448   lua_unlock(L);
    00449   return -1;
    00450 }
    00451
    00452
    00453 int luaD_pcall (lua_State *L, Pfunc func, void *u,
    00454                 ptrdiff_t old_top, ptrdiff_t ef) {
    00455   int status;
    00456   unsigned short oldnCcalls = L->nCcalls;
    00457   ptrdiff_t old_ci = saveci(L, L->ci);
    00458   lu_byte old_allowhooks = L->allowhook;
    00459   ptrdiff_t old_errfunc = L->errfunc;
    00460   L->errfunc = ef;
    00461   status = luaD_rawrunprotected(L, func, u);
    00462   if (status != 0) {  /* an error occurred? */
    00463     StkId oldtop = restorestack(L, old_top);
    00464     luaF_close(L, oldtop);  /* close eventual pending closures */
    00465     luaD_seterrorobj(L, status, oldtop);
    00466     L->nCcalls = oldnCcalls;
    00467     L->ci = restoreci(L, old_ci);
    00468     L->base = L->ci->base;
    00469     L->savedpc = L->ci->savedpc;
    00470     L->allowhook = old_allowhooks;
    00471     restore_stack_limit(L);
    00472   }
    00473   L->errfunc = old_errfunc;
    00474   return status;
    00475 }
    00476
    00477
    00478
    00479 /*
    00480 ** Execute a protected parser.
    00481 */
    00482 struct SParser {  /* data to `f_parser' */
    00483   ZIO *z;
    00484   Mbuffer buff;  /* buffer to be used by the scanner */
    00485   const char *name;
    00486 };
    00487
    00488 static void f_parser (lua_State *L, void *ud) {
    00489   int i;
    00490   Proto *tf;
    00491   Closure *cl;
    00492   struct SParser *p = cast(struct SParser *, ud);
    00493   int c = luaZ_lookahead(p->z);
    00494   luaC_checkGC(L);
    00495   tf = ((c == LUA_SIGNATURE[0]) ? luaU_undump : luaY_parser)(L, p->z,
    00496                                                              &p->buff, p->name);
    00497   cl = luaF_newLclosure(L, tf->nups, hvalue(gt(L)));
    00498   cl->l.p = tf;
    00499   for (i = 0; i < tf->nups; i++)  /* initialize eventual upvalues */
    00500     cl->l.upvals[i] = luaF_newupval(L);
    00501   setclvalue(L, L->top, cl);
    00502   incr_top(L);
    00503 }
    00504
    00505
    00506 int luaD_protectedparser (lua_State *L, ZIO *z, const char *name) {
    00507   struct SParser p;
    00508   int status;
    00509   p.z = z; p.name = name;
    00510   luaZ_initbuffer(L, &p.buff);
    00511   status = luaD_pcall(L, f_parser, &p, savestack(L, L->top), L->errfunc);
    00512   luaZ_freebuffer(L, &p.buff);
    00513   return status;
    00514 }
    00515
    00516