lua/ldo.c

Go to the documentation of this file.
00001 /*
00002 ** $Id: ldo.c,v 2.102 2011/11/29 15:55:08 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_jailbreak_exception.hpp"
00016 #include <exception>
00017 #if !defined(__cplusplus)
00018 #error "Exception support requires a C++ compiler."
00019 #endif
00020 
00021 #include "lua.h"
00022 
00023 #include "lapi.h"
00024 #include "ldebug.h"
00025 #include "ldo.h"
00026 #include "lfunc.h"
00027 #include "lgc.h"
00028 #include "lmem.h"
00029 #include "lobject.h"
00030 #include "lopcodes.h"
00031 #include "lparser.h"
00032 #include "lstate.h"
00033 #include "lstring.h"
00034 #include "ltable.h"
00035 #include "ltm.h"
00036 #include "lundump.h"
00037 #include "lvm.h"
00038 #include "lzio.h"
00039 
00040 
00041 
00042 
00043 /*
00044 ** {======================================================
00045 ** Error-recovery functions
00046 ** =======================================================
00047 */
00048 
00049 /*
00050 ** LUAI_THROW/LUAI_TRY define how Lua does exception handling. By
00051 ** default, Lua handles errors with exceptions when compiling as
00052 ** C++ code, with _longjmp/_setjmp when asked to use them, and with
00053 ** longjmp/setjmp otherwise.
00054 */
00055 #if defined(__cplusplus)
00056 /* C++ exceptions */
00057 #define LUAI_THROW(L,c) throw(c)
00058 #define LUAI_TRY(L,c,a) \
00059         try { \
00060                 try { \
00061                         a \
00062                 } catch(const tlua_jailbreak_exception &e) { \
00063                         e.store(); \
00064                         throw; \
00065                 } catch(const std::exception &e) { \
00066                         lua_pushstring(L, e.what()); \
00067                         luaG_errormsg(L); \
00068                         throw; \
00069                 } \
00070         } catch(...) { \
00071                 if((c)->status == 0) \
00072                         (c)->status = -1;\
00073         }
00074 #define luai_jmpbuf     int  /* dummy variable */
00075 
00076 
00077 #elif defined(LUA_USE_ULONGJMP)
00078 /* in Unix, try _longjmp/_setjmp (more efficient) */
00079 #define LUAI_THROW(L,c)     _longjmp((c)->b, 1)
00080 #define LUAI_TRY(L,c,a)     if (_setjmp((c)->b) == 0) { a }
00081 #define luai_jmpbuf     jmp_buf
00082 
00083 #else
00084 /* default handling with long jumps */
00085 #define LUAI_THROW(L,c)     longjmp((c)->b, 1)
00086 #define LUAI_TRY(L,c,a)     if (setjmp((c)->b) == 0) { a }
00087 #define luai_jmpbuf     jmp_buf
00088 
00089 #endif
00090 
00091 
00092 
00093 /* chain list of long jump buffers */
00094 struct lua_longjmp {
00095   struct lua_longjmp *previous;
00096   luai_jmpbuf b;
00097   volatile int status;  /* error code */
00098 };
00099 
00100 
00101 static void seterrorobj (lua_State *L, int errcode, StkId oldtop) {
00102   switch (errcode) {
00103     case LUA_ERRMEM: {  /* memory error? */
00104       setsvalue2s(L, oldtop, G(L)->memerrmsg); /* reuse preregistered msg. */
00105       break;
00106     }
00107     case LUA_ERRERR: {
00108       setsvalue2s(L, oldtop, luaS_newliteral(L, "error in error handling"));
00109       break;
00110     }
00111     default: {
00112       setobjs2s(L, oldtop, L->top - 1);  /* error message on current top */
00113       break;
00114     }
00115   }
00116   L->top = oldtop + 1;
00117 }
00118 
00119 
00120 l_noret luaD_throw (lua_State *L, int errcode) {
00121   if (L->errorJmp) {  /* thread has an error handler? */
00122     L->errorJmp->status = errcode;  /* set status */
00123     LUAI_THROW(L, L->errorJmp);  /* jump to it */
00124   }
00125   else {  /* thread has no error handler */
00126     L->status = cast_byte(errcode);  /* mark it as dead */
00127     if (G(L)->mainthread->errorJmp) {  /* main thread has a handler? */
00128       setobjs2s(L, G(L)->mainthread->top++, L->top - 1);  /* copy error obj. */
00129       luaD_throw(G(L)->mainthread, errcode);  /* re-throw in main thread */
00130     }
00131     else {  /* no handler at all; abort */
00132       if (G(L)->panic) {  /* panic function? */
00133         lua_unlock(L);
00134         G(L)->panic(L);  /* call it (last chance to jump out) */
00135       }
00136       abort();
00137     }
00138   }
00139 }
00140 
00141 
00142 int luaD_rawrunprotected (lua_State *L, Pfunc f, void *ud) {
00143   unsigned short oldnCcalls = L->nCcalls;
00144   struct lua_longjmp lj;
00145   lj.status = LUA_OK;
00146   lj.previous = L->errorJmp;  /* chain new error handler */
00147   L->errorJmp = &lj;
00148   LUAI_TRY(L, &lj,
00149     (*f)(L, ud);
00150   );
00151   L->errorJmp = lj.previous;  /* restore old error handler */
00152   L->nCcalls = oldnCcalls;
00153   return lj.status;
00154 }
00155 
00156 /* }====================================================== */
00157 
00158 
00159 static void correctstack (lua_State *L, TValue *oldstack) {
00160   CallInfo *ci;
00161   GCObject *up;
00162   L->top = (L->top - oldstack) + L->stack;
00163   for (up = L->openupval; up != NULL; up = up->gch.next)
00164     gco2uv(up)->v = (gco2uv(up)->v - oldstack) + L->stack;
00165   for (ci = L->ci; ci != NULL; ci = ci->previous) {
00166     ci->top = (ci->top - oldstack) + L->stack;
00167     ci->func = (ci->func - oldstack) + L->stack;
00168     if (isLua(ci))
00169       ci->u.l.base = (ci->u.l.base - oldstack) + L->stack;
00170   }
00171 }
00172 
00173 
00174 /* some space for error handling */
00175 #define ERRORSTACKSIZE  (LUAI_MAXSTACK + 200)
00176 
00177 
00178 void luaD_reallocstack (lua_State *L, int newsize) {
00179   TValue *oldstack = L->stack;
00180   int lim = L->stacksize;
00181   lua_assert(newsize <= LUAI_MAXSTACK || newsize == ERRORSTACKSIZE);
00182   lua_assert(L->stack_last - L->stack == L->stacksize - EXTRA_STACK);
00183   luaM_reallocvector(L, L->stack, L->stacksize, newsize, TValue);
00184   for (; lim < newsize; lim++)
00185     setnilvalue(L->stack + lim); /* erase new segment */
00186   L->stacksize = newsize;
00187   L->stack_last = L->stack + newsize - EXTRA_STACK;
00188   correctstack(L, oldstack);
00189 }
00190 
00191 
00192 void luaD_growstack (lua_State *L, int n) {
00193   int size = L->stacksize;
00194   if (size > LUAI_MAXSTACK)  /* error after extra size? */
00195     luaD_throw(L, LUA_ERRERR);
00196   else {
00197     int needed = cast_int(L->top - L->stack) + n + EXTRA_STACK;
00198     int newsize = 2 * size;
00199     if (newsize > LUAI_MAXSTACK) newsize = LUAI_MAXSTACK;
00200     if (newsize < needed) newsize = needed;
00201     if (newsize > LUAI_MAXSTACK) {  /* stack overflow? */
00202       luaD_reallocstack(L, ERRORSTACKSIZE);
00203       luaG_runerror(L, "stack overflow");
00204     }
00205     else
00206       luaD_reallocstack(L, newsize);
00207   }
00208 }
00209 
00210 
00211 static int stackinuse (lua_State *L) {
00212   CallInfo *ci;
00213   StkId lim = L->top;
00214   for (ci = L->ci; ci != NULL; ci = ci->previous) {
00215     lua_assert(ci->top <= L->stack_last);
00216     if (lim < ci->top) lim = ci->top;
00217   }
00218   return cast_int(lim - L->stack) + 1;  /* part of stack in use */
00219 }
00220 
00221 
00222 void luaD_shrinkstack (lua_State *L) {
00223   int inuse = stackinuse(L);
00224   int goodsize = inuse + (inuse / 8) + 2*EXTRA_STACK;
00225   if (goodsize > LUAI_MAXSTACK) goodsize = LUAI_MAXSTACK;
00226   if (inuse > LUAI_MAXSTACK ||  /* handling stack overflow? */
00227       goodsize >= L->stacksize)  /* would grow instead of shrink? */
00228     condmovestack(L);  /* don't change stack (change only for debugging) */
00229   else
00230     luaD_reallocstack(L, goodsize);  /* shrink it */
00231 }
00232 
00233 
00234 void luaD_hook (lua_State *L, int event, int line) {
00235   lua_Hook hook = L->hook;
00236   if (hook && L->allowhook) {
00237     CallInfo *ci = L->ci;
00238     ptrdiff_t top = savestack(L, L->top);
00239     ptrdiff_t ci_top = savestack(L, ci->top);
00240     lua_Debug ar;
00241     ar.event = event;
00242     ar.currentline = line;
00243     ar.i_ci = ci;
00244     luaD_checkstack(L, LUA_MINSTACK);  /* ensure minimum stack size */
00245     ci->top = L->top + LUA_MINSTACK;
00246     lua_assert(ci->top <= L->stack_last);
00247     L->allowhook = 0;  /* cannot call hooks inside a hook */
00248     ci->callstatus |= CIST_HOOKED;
00249     lua_unlock(L);
00250     (*hook)(L, &ar);
00251     lua_lock(L);
00252     lua_assert(!L->allowhook);
00253     L->allowhook = 1;
00254     ci->top = restorestack(L, ci_top);
00255     L->top = restorestack(L, top);
00256     ci->callstatus &= ~CIST_HOOKED;
00257   }
00258 }
00259 
00260 
00261 static void callhook (lua_State *L, CallInfo *ci) {
00262   int hook = LUA_HOOKCALL;
00263   ci->u.l.savedpc++;  /* hooks assume 'pc' is already incremented */
00264   if (isLua(ci->previous) &&
00265       GET_OPCODE(*(ci->previous->u.l.savedpc - 1)) == OP_TAILCALL) {
00266     ci->callstatus |= CIST_TAIL;
00267     hook = LUA_HOOKTAILCALL;
00268   }
00269   luaD_hook(L, hook, -1);
00270   ci->u.l.savedpc--;  /* correct 'pc' */
00271 }
00272 
00273 
00274 static StkId adjust_varargs (lua_State *L, Proto *p, int actual) {
00275   int i;
00276   int nfixargs = p->numparams;
00277   StkId base, fixed;
00278   lua_assert(actual >= nfixargs);
00279   /* move fixed parameters to final position */
00280   fixed = L->top - actual;  /* first fixed argument */
00281   base = L->top;  /* final position of first argument */
00282   for (i=0; i<nfixargs; i++) {
00283     setobjs2s(L, L->top++, fixed + i);
00284     setnilvalue(fixed + i);
00285   }
00286   return base;
00287 }
00288 
00289 
00290 static StkId tryfuncTM (lua_State *L, StkId func) {
00291   const TValue *tm = luaT_gettmbyobj(L, func, TM_CALL);
00292   StkId p;
00293   ptrdiff_t funcr = savestack(L, func);
00294   if (!ttisfunction(tm))
00295     luaG_typeerror(L, func, "call");
00296   /* Open a hole inside the stack at `func' */
00297   for (p = L->top; p > func; p--) setobjs2s(L, p, p-1);
00298   incr_top(L);
00299   func = restorestack(L, funcr);  /* previous call may change stack */
00300   setobj2s(L, func, tm);  /* tag method is the new function to be called */
00301   return func;
00302 }
00303 
00304 
00305 
00306 #define next_ci(L) (L->ci = (L->ci->next ? L->ci->next : luaE_extendCI(L)))
00307 
00308 
00309 /*
00310 ** returns true if function has been executed (C function)
00311 */
00312 int luaD_precall (lua_State *L, StkId func, int nresults) {
00313   lua_CFunction f;
00314   CallInfo *ci;
00315   int n;  /* number of arguments (Lua) or returns (C) */
00316   ptrdiff_t funcr = savestack(L, func);
00317   switch (ttype(func)) {
00318     case LUA_TLCF:  /* light C function */
00319       f = fvalue(func);
00320       goto Cfunc;
00321     case LUA_TCCL: {  /* C closure */
00322       f = clCvalue(func)->f;
00323      Cfunc:
00324       luaD_checkstack(L, LUA_MINSTACK);  /* ensure minimum stack size */
00325       ci = next_ci(L);  /* now 'enter' new function */
00326       ci->nresults = nresults;
00327       ci->func = restorestack(L, funcr);
00328       ci->top = L->top + LUA_MINSTACK;
00329       lua_assert(ci->top <= L->stack_last);
00330       ci->callstatus = 0;
00331       if (L->hookmask & LUA_MASKCALL)
00332         luaD_hook(L, LUA_HOOKCALL, -1);
00333       lua_unlock(L);
00334       n = (*f)(L);  /* do the actual call */
00335       lua_lock(L);
00336       api_checknelems(L, n);
00337       luaD_poscall(L, L->top - n);
00338       return 1;
00339     }
00340     case LUA_TLCL: {  /* Lua function: prepare its call */
00341       StkId base;
00342       Proto *p = clLvalue(func)->p;
00343       luaD_checkstack(L, p->maxstacksize);
00344       func = restorestack(L, funcr);
00345       n = cast_int(L->top - func) - 1;  /* number of real arguments */
00346       for (; n < p->numparams; n++)
00347         setnilvalue(L->top++);  /* complete missing arguments */
00348       base = (!p->is_vararg) ? func + 1 : adjust_varargs(L, p, n);
00349       ci = next_ci(L);  /* now 'enter' new function */
00350       ci->nresults = nresults;
00351       ci->func = func;
00352       ci->u.l.base = base;
00353       ci->top = base + p->maxstacksize;
00354       lua_assert(ci->top <= L->stack_last);
00355       ci->u.l.savedpc = p->code;  /* starting point */
00356       ci->callstatus = CIST_LUA;
00357       L->top = ci->top;
00358       if (L->hookmask & LUA_MASKCALL)
00359         callhook(L, ci);
00360       return 0;
00361     }
00362     default: {  /* not a function */
00363       func = tryfuncTM(L, func);  /* retry with 'function' tag method */
00364       return luaD_precall(L, func, nresults);  /* now it must be a function */
00365     }
00366   }
00367 }
00368 
00369 
00370 int luaD_poscall (lua_State *L, StkId firstResult) {
00371   StkId res;
00372   int wanted, i;
00373   CallInfo *ci = L->ci;
00374   if (L->hookmask & (LUA_MASKRET | LUA_MASKLINE)) {
00375     if (L->hookmask & LUA_MASKRET) {
00376       ptrdiff_t fr = savestack(L, firstResult);  /* hook may change stack */
00377       luaD_hook(L, LUA_HOOKRET, -1);
00378       firstResult = restorestack(L, fr);
00379     }
00380     L->oldpc = ci->previous->u.l.savedpc;  /* 'oldpc' for caller function */
00381   }
00382   res = ci->func;  /* res == final position of 1st result */
00383   wanted = ci->nresults;
00384   L->ci = ci = ci->previous;  /* back to caller */
00385   /* move results to correct place */
00386   for (i = wanted; i != 0 && firstResult < L->top; i--)
00387     setobjs2s(L, res++, firstResult++);
00388   while (i-- > 0)
00389     setnilvalue(res++);
00390   L->top = res;
00391   return (wanted - LUA_MULTRET);  /* 0 iff wanted == LUA_MULTRET */
00392 }
00393 
00394 
00395 /*
00396 ** Call a function (C or Lua). The function to be called is at *func.
00397 ** The arguments are on the stack, right after the function.
00398 ** When returns, all the results are on the stack, starting at the original
00399 ** function position.
00400 */
00401 void luaD_call (lua_State *L, StkId func, int nResults, int allowyield) {
00402   if (++L->nCcalls >= LUAI_MAXCCALLS) {
00403     if (L->nCcalls == LUAI_MAXCCALLS)
00404       luaG_runerror(L, "C stack overflow");
00405     else if (L->nCcalls >= (LUAI_MAXCCALLS + (LUAI_MAXCCALLS>>3)))
00406       luaD_throw(L, LUA_ERRERR);  /* error while handing stack error */
00407   }
00408   if (!allowyield) L->nny++;
00409   if (!luaD_precall(L, func, nResults))  /* is a Lua function? */
00410     luaV_execute(L);  /* call it */
00411   if (!allowyield) L->nny--;
00412   L->nCcalls--;
00413   luaC_checkGC(L);
00414 }
00415 
00416 
00417 static void finishCcall (lua_State *L) {
00418   CallInfo *ci = L->ci;
00419   int n;
00420   lua_assert(ci->u.c.k != NULL);  /* must have a continuation */
00421   lua_assert(L->nny == 0);
00422   /* finish 'luaD_call' */
00423   L->nCcalls--;
00424   /* finish 'lua_callk' */
00425   adjustresults(L, ci->nresults);
00426   /* call continuation function */
00427   if (!(ci->callstatus & CIST_STAT))  /* no call status? */
00428     ci->u.c.status = LUA_YIELD;  /* 'default' status */
00429   lua_assert(ci->u.c.status != LUA_OK);
00430   ci->callstatus = (ci->callstatus & ~(CIST_YPCALL | CIST_STAT)) | CIST_YIELDED;
00431   lua_unlock(L);
00432   n = (*ci->u.c.k)(L);
00433   lua_lock(L);
00434   api_checknelems(L, n);
00435   /* finish 'luaD_precall' */
00436   luaD_poscall(L, L->top - n);
00437 }
00438 
00439 
00440 static void unroll (lua_State *L, void *ud) {
00441   UNUSED(ud);
00442   for (;;) {
00443     if (L->ci == &L->base_ci)  /* stack is empty? */
00444       return;  /* coroutine finished normally */
00445     if (!isLua(L->ci))  /* C function? */
00446       finishCcall(L);
00447     else {  /* Lua function */
00448       luaV_finishOp(L);  /* finish interrupted instruction */
00449       luaV_execute(L);  /* execute down to higher C 'boundary' */
00450     }
00451   }
00452 }
00453 
00454 
00455 /*
00456 ** check whether thread has a suspended protected call
00457 */
00458 static CallInfo *findpcall (lua_State *L) {
00459   CallInfo *ci;
00460   for (ci = L->ci; ci != NULL; ci = ci->previous) {  /* search for a pcall */
00461     if (ci->callstatus & CIST_YPCALL)
00462       return ci;
00463   }
00464   return NULL;  /* no pending pcall */
00465 }
00466 
00467 
00468 static int recover (lua_State *L, int status) {
00469   StkId oldtop;
00470   CallInfo *ci = findpcall(L);
00471   if (ci == NULL) return 0;  /* no recovery point */
00472   /* "finish" luaD_pcall */
00473   oldtop = restorestack(L, ci->u.c.extra);
00474   luaF_close(L, oldtop);
00475   seterrorobj(L, status, oldtop);
00476   L->ci = ci;
00477   L->allowhook = ci->u.c.old_allowhook;
00478   L->nny = 0;  /* should be zero to be yieldable */
00479   luaD_shrinkstack(L);
00480   L->errfunc = ci->u.c.old_errfunc;
00481   ci->callstatus |= CIST_STAT;  /* call has error status */
00482   ci->u.c.status = status;  /* (here it is) */
00483   return 1;  /* continue running the coroutine */
00484 }
00485 
00486 
00487 /*
00488 ** signal an error in the call to 'resume', not in the execution of the
00489 ** coroutine itself. (Such errors should not be handled by any coroutine
00490 ** error handler and should not kill the coroutine.)
00491 */
00492 static l_noret resume_error (lua_State *L, const char *msg, StkId firstArg) {
00493   L->top = firstArg;  /* remove args from the stack */
00494   setsvalue2s(L, L->top, luaS_new(L, msg));  /* push error message */
00495   incr_top(L);
00496   luaD_throw(L, -1);  /* jump back to 'lua_resume' */
00497 }
00498 
00499 
00500 /*
00501 ** do the work for 'lua_resume' in protected mode
00502 */
00503 static void resume (lua_State *L, void *ud) {
00504   StkId firstArg = cast(StkId, ud);
00505   CallInfo *ci = L->ci;
00506   if (L->nCcalls >= LUAI_MAXCCALLS)
00507     resume_error(L, "C stack overflow", firstArg);
00508   if (L->status == LUA_OK) {  /* may be starting a coroutine */
00509     if (ci != &L->base_ci)  /* not in base level? */
00510       resume_error(L, "cannot resume non-suspended coroutine", firstArg);
00511     /* coroutine is in base level; start running it */
00512     if (!luaD_precall(L, firstArg - 1, LUA_MULTRET))  /* Lua function? */
00513       luaV_execute(L);  /* call it */
00514   }
00515   else if (L->status != LUA_YIELD)
00516     resume_error(L, "cannot resume dead coroutine", firstArg);
00517   else {  /* resuming from previous yield */
00518     L->status = LUA_OK;
00519     if (isLua(ci))  /* yielded inside a hook? */
00520       luaV_execute(L);  /* just continue running Lua code */
00521     else {  /* 'common' yield */
00522       ci->func = restorestack(L, ci->u.c.extra);
00523       if (ci->u.c.k != NULL) {  /* does it have a continuation? */
00524         int n;
00525         ci->u.c.status = LUA_YIELD;  /* 'default' status */
00526         ci->callstatus |= CIST_YIELDED;
00527         lua_unlock(L);
00528         n = (*ci->u.c.k)(L);  /* call continuation */
00529         lua_lock(L);
00530         api_checknelems(L, n);
00531         firstArg = L->top - n;  /* yield results come from continuation */
00532       }
00533       L->nCcalls--;  /* finish 'luaD_call' */
00534       luaD_poscall(L, firstArg);  /* finish 'luaD_precall' */
00535     }
00536     unroll(L, NULL);
00537   }
00538 }
00539 
00540 
00541 LUA_API int lua_resume (lua_State *L, lua_State *from, int nargs) {
00542   int status;
00543   lua_lock(L);
00544   luai_userstateresume(L, nargs);
00545   L->nCcalls = (from) ? from->nCcalls + 1 : 1;
00546   L->nny = 0;  /* allow yields */
00547   api_checknelems(L, (L->status == LUA_OK) ? nargs + 1 : nargs);
00548   status = luaD_rawrunprotected(L, resume, L->top - nargs);
00549   if (status == -1)  /* error calling 'lua_resume'? */
00550     status = LUA_ERRRUN;
00551   else {  /* yield or regular error */
00552     while (status != LUA_OK && status != LUA_YIELD) {  /* error? */
00553       if (recover(L, status))  /* recover point? */
00554         status = luaD_rawrunprotected(L, unroll, NULL);  /* run continuation */
00555       else {  /* unrecoverable error */
00556         L->status = cast_byte(status);  /* mark thread as `dead' */
00557         seterrorobj(L, status, L->top);
00558         L->ci->top = L->top;
00559         break;
00560       }
00561     }
00562     lua_assert(status == L->status);
00563   }
00564   L->nny = 1;  /* do not allow yields */
00565   L->nCcalls--;
00566   lua_assert(L->nCcalls == ((from) ? from->nCcalls : 0));
00567   lua_unlock(L);
00568   return status;
00569 }
00570 
00571 
00572 LUA_API int lua_yieldk (lua_State *L, int nresults, int ctx, lua_CFunction k) {
00573   CallInfo *ci = L->ci;
00574   luai_userstateyield(L, nresults);
00575   lua_lock(L);
00576   api_checknelems(L, nresults);
00577   if (L->nny > 0) {
00578     if (L != G(L)->mainthread)
00579       luaG_runerror(L, "attempt to yield across metamethod/C-call boundary");
00580     else
00581       luaG_runerror(L, "attempt to yield from outside a coroutine");
00582   }
00583   L->status = LUA_YIELD;
00584   if (isLua(ci)) {  /* inside a hook? */
00585     api_check(L, k == NULL, "hooks cannot continue after yielding");
00586   }
00587   else {
00588     if ((ci->u.c.k = k) != NULL)  /* is there a continuation? */
00589       ci->u.c.ctx = ctx;  /* save context */
00590     ci->u.c.extra = savestack(L, ci->func);  /* save current 'func' */
00591     ci->func = L->top - nresults - 1;  /* protect stack below results */
00592     luaD_throw(L, LUA_YIELD);
00593   }
00594   lua_assert(ci->callstatus & CIST_HOOKED);  /* must be inside a hook */
00595   lua_unlock(L);
00596   return 0;  /* return to 'luaD_hook' */
00597 }
00598 
00599 
00600 int luaD_pcall (lua_State *L, Pfunc func, void *u,
00601                 ptrdiff_t old_top, ptrdiff_t ef) {
00602   int status;
00603   CallInfo *old_ci = L->ci;
00604   lu_byte old_allowhooks = L->allowhook;
00605   unsigned short old_nny = L->nny;
00606   ptrdiff_t old_errfunc = L->errfunc;
00607   L->errfunc = ef;
00608   status = luaD_rawrunprotected(L, func, u);
00609   if (status != LUA_OK) {  /* an error occurred? */
00610     StkId oldtop = restorestack(L, old_top);
00611     luaF_close(L, oldtop);  /* close possible pending closures */
00612     seterrorobj(L, status, oldtop);
00613     L->ci = old_ci;
00614     L->allowhook = old_allowhooks;
00615     L->nny = old_nny;
00616     luaD_shrinkstack(L);
00617   }
00618   L->errfunc = old_errfunc;
00619   return status;
00620 }
00621 
00622 
00623 
00624 /*
00625 ** Execute a protected parser.
00626 */
00627 struct SParser {  /* data to `f_parser' */
00628   ZIO *z;
00629   Mbuffer buff;  /* dynamic structure used by the scanner */
00630   Dyndata dyd;  /* dynamic structures used by the parser */
00631   const char *mode;
00632   const char *name;
00633 };
00634 
00635 
00636 static void checkmode (lua_State *L, const char *mode, const char *x) {
00637   if (mode && strchr(mode, x[0]) == NULL) {
00638     luaO_pushfstring(L,
00639        "attempt to load a %s chunk (mode is " LUA_QS ")", x, mode);
00640     luaD_throw(L, LUA_ERRSYNTAX);
00641   }
00642 }
00643 
00644 
00645 static void f_parser (lua_State *L, void *ud) {
00646   int i;
00647   Proto *tf;
00648   Closure *cl;
00649   struct SParser *p = cast(struct SParser *, ud);
00650   int c = zgetc(p->z);  /* read first character */
00651   if (c == LUA_SIGNATURE[0]) {
00652     checkmode(L, p->mode, "binary");
00653     tf = luaU_undump(L, p->z, &p->buff, p->name);
00654   }
00655   else {
00656     checkmode(L, p->mode, "text");
00657     tf = luaY_parser(L, p->z, &p->buff, &p->dyd, p->name, c);
00658   }
00659   setptvalue2s(L, L->top, tf);
00660   incr_top(L);
00661   cl = luaF_newLclosure(L, tf);
00662   setclLvalue(L, L->top - 1, cl);
00663   for (i = 0; i < tf->sizeupvalues; i++)  /* initialize upvalues */
00664     cl->l.upvals[i] = luaF_newupval(L);
00665 }
00666 
00667 
00668 int luaD_protectedparser (lua_State *L, ZIO *z, const char *name,
00669                                         const char *mode) {
00670   struct SParser p;
00671   int status;
00672   L->nny++;  /* cannot yield during parsing */
00673   p.z = z; p.name = name; p.mode = mode;
00674   p.dyd.actvar.arr = NULL; p.dyd.actvar.size = 0;
00675   p.dyd.gt.arr = NULL; p.dyd.gt.size = 0;
00676   p.dyd.label.arr = NULL; p.dyd.label.size = 0;
00677   luaZ_initbuffer(L, &p.buff);
00678   status = luaD_pcall(L, f_parser, &p, savestack(L, L->top), L->errfunc);
00679   luaZ_freebuffer(L, &p.buff);
00680   luaM_freearray(L, p.dyd.actvar.arr, p.dyd.actvar.size);
00681   luaM_freearray(L, p.dyd.gt.arr, p.dyd.gt.size);
00682   luaM_freearray(L, p.dyd.label.arr, p.dyd.label.size);
00683   L->nny--;
00684   return status;
00685 }
00686 
00687 
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines

Generated by doxygen 1.7.1 on Fri May 25 2012 01:03:03 for The Battle for Wesnoth
Gna! | Forum | Wiki | CIA | devdocs