The Design and Implementation of the FreeBSD Operating System, Second Edition
Now available: The Design and Implementation of the FreeBSD Operating System (Second Edition)


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]

FreeBSD/Linux Kernel Cross Reference
sys/contrib/openzfs/module/lua/ldo.c

Version: -  FREEBSD  -  FREEBSD-13-STABLE  -  FREEBSD-13-0  -  FREEBSD-12-STABLE  -  FREEBSD-12-0  -  FREEBSD-11-STABLE  -  FREEBSD-11-0  -  FREEBSD-10-STABLE  -  FREEBSD-10-0  -  FREEBSD-9-STABLE  -  FREEBSD-9-0  -  FREEBSD-8-STABLE  -  FREEBSD-8-0  -  FREEBSD-7-STABLE  -  FREEBSD-7-0  -  FREEBSD-6-STABLE  -  FREEBSD-6-0  -  FREEBSD-5-STABLE  -  FREEBSD-5-0  -  FREEBSD-4-STABLE  -  FREEBSD-3-STABLE  -  FREEBSD22  -  l41  -  OPENBSD  -  linux-2.6  -  MK84  -  PLAN9  -  xnu-8792 
SearchContext: -  none  -  3  -  10 

    1 /*
    2 ** $Id: ldo.c,v 2.108.1.3 2013/11/08 18:22:50 roberto Exp $
    3 ** Stack and Call structure of Lua
    4 ** See Copyright Notice in lua.h
    5 */
    6 
    7 
    8 #define ldo_c
    9 #define LUA_CORE
   10 
   11 #include <sys/lua/lua.h>
   12 #include <sys/asm_linkage.h>
   13 
   14 #include "lapi.h"
   15 #include "ldebug.h"
   16 #include "ldo.h"
   17 #include "lfunc.h"
   18 #include "lgc.h"
   19 #include "lmem.h"
   20 #include "lobject.h"
   21 #include "lopcodes.h"
   22 #include "lparser.h"
   23 #include "lstate.h"
   24 #include "lstring.h"
   25 #include "ltable.h"
   26 #include "ltm.h"
   27 #include "lvm.h"
   28 #include "lzio.h"
   29 
   30 
   31 /* Return the number of bytes available on the stack. */
   32 #if defined (_KERNEL) && defined(__linux__)
   33 #include <asm/current.h>
   34 static intptr_t stack_remaining(void) {
   35   intptr_t local;
   36   local = (intptr_t)&local - (intptr_t)current->stack;
   37   return local;
   38 }
   39 #elif defined (_KERNEL) && defined(__FreeBSD__)
   40 #include <sys/pcpu.h>
   41 static intptr_t stack_remaining(void) {
   42   intptr_t local;
   43   local = (intptr_t)&local - (intptr_t)curthread->td_kstack;
   44   return local;
   45 }
   46 #else
   47 static intptr_t stack_remaining(void) {
   48   return INTPTR_MAX;
   49 }
   50 #endif
   51 
   52 /*
   53 ** {======================================================
   54 ** Error-recovery functions
   55 ** =======================================================
   56 */
   57 
   58 /*
   59 ** LUAI_THROW/LUAI_TRY define how Lua does exception handling. By
   60 ** default, Lua handles errors with exceptions when compiling as
   61 ** C++ code, with _longjmp/_setjmp when asked to use them, and with
   62 ** longjmp/setjmp otherwise.
   63 */
   64 #if !defined(LUAI_THROW)
   65 
   66 #ifdef _KERNEL
   67 
   68 #ifdef __linux__
   69 #if defined(__i386__)
   70 #define JMP_BUF_CNT     6
   71 #elif defined(__x86_64__)
   72 #define JMP_BUF_CNT     8
   73 #elif defined(__sparc__) && defined(__arch64__)
   74 #define JMP_BUF_CNT     6
   75 #elif defined(__powerpc__)
   76 #define JMP_BUF_CNT     26
   77 #elif defined(__aarch64__)
   78 #define JMP_BUF_CNT     64
   79 #elif defined(__arm__)
   80 #define JMP_BUF_CNT     65
   81 #elif defined(__mips__)
   82 #define JMP_BUF_CNT     12
   83 #elif defined(__s390x__)
   84 #define JMP_BUF_CNT     18
   85 #elif defined(__riscv)
   86 #define JMP_BUF_CNT     64
   87 #else
   88 #define JMP_BUF_CNT     1
   89 #endif
   90 
   91 typedef struct _label_t { long long unsigned val[JMP_BUF_CNT]; } label_t;
   92 
   93 int ASMABI setjmp(label_t *) __attribute__ ((__nothrow__));
   94 extern __attribute__((noreturn)) void ASMABI longjmp(label_t *);
   95 
   96 #define LUAI_THROW(L,c)         longjmp(&(c)->b)
   97 #define LUAI_TRY(L,c,a)         if (setjmp(&(c)->b) == 0) { a }
   98 #define luai_jmpbuf             label_t
   99 
  100 /* unsupported arches will build but not be able to run lua programs */
  101 #if JMP_BUF_CNT == 1
  102 int setjmp (label_t *buf) {
  103         return 1;
  104 }
  105 
  106 void longjmp (label_t * buf) {
  107         for (;;);
  108 }
  109 #endif
  110 #else
  111 #define LUAI_THROW(L,c)         longjmp((c)->b, 1)
  112 #define LUAI_TRY(L,c,a)         if (setjmp((c)->b) == 0) { a }
  113 #define luai_jmpbuf             jmp_buf
  114 #endif
  115 
  116 #else /* _KERNEL */
  117 
  118 #if defined(__cplusplus) && !defined(LUA_USE_LONGJMP)
  119 /* C++ exceptions */
  120 #define LUAI_THROW(L,c)         throw(c)
  121 #define LUAI_TRY(L,c,a) \
  122         try { a } catch(...) { if ((c)->status == 0) (c)->status = -1; }
  123 #define luai_jmpbuf             int  /* dummy variable */
  124 
  125 #elif defined(LUA_USE_ULONGJMP)
  126 /* in Unix, try _longjmp/_setjmp (more efficient) */
  127 #define LUAI_THROW(L,c)         _longjmp((c)->b, 1)
  128 #define LUAI_TRY(L,c,a)         if (_setjmp((c)->b) == 0) { a }
  129 #define luai_jmpbuf             jmp_buf
  130 
  131 #else
  132 /* default handling with long jumps */
  133 #define LUAI_THROW(L,c)         longjmp((c)->b, 1)
  134 #define LUAI_TRY(L,c,a)         if (setjmp((c)->b) == 0) { a }
  135 #define luai_jmpbuf             jmp_buf
  136 
  137 #endif
  138 
  139 #endif /* _KERNEL */
  140 
  141 #endif /* LUAI_THROW */
  142 
  143 
  144 /* chain list of long jump buffers */
  145 struct lua_longjmp {
  146   struct lua_longjmp *previous;
  147   luai_jmpbuf b;
  148   volatile int status;  /* error code */
  149 };
  150 
  151 
  152 static void seterrorobj (lua_State *L, int errcode, StkId oldtop) {
  153   switch (errcode) {
  154     case LUA_ERRMEM: {  /* memory error? */
  155       setsvalue2s(L, oldtop, G(L)->memerrmsg); /* reuse preregistered msg. */
  156       break;
  157     }
  158     case LUA_ERRERR: {
  159       setsvalue2s(L, oldtop, luaS_newliteral(L, "error in error handling"));
  160       break;
  161     }
  162     default: {
  163       setobjs2s(L, oldtop, L->top - 1);  /* error message on current top */
  164       break;
  165     }
  166   }
  167   L->top = oldtop + 1;
  168 }
  169 
  170 /*
  171  * Silence infinite recursion warning which was added to -Wall in gcc 12.1
  172  */
  173 #if defined(__GNUC__) && !defined(__clang__) && \
  174         defined(HAVE_KERNEL_INFINITE_RECURSION)
  175 #pragma GCC diagnostic push
  176 #pragma GCC diagnostic ignored "-Winfinite-recursion"
  177 #endif
  178 
  179 l_noret luaD_throw (lua_State *L, int errcode) {
  180   if (L->errorJmp) {  /* thread has an error handler? */
  181     L->errorJmp->status = errcode;  /* set status */
  182     LUAI_THROW(L, L->errorJmp);  /* jump to it */
  183   }
  184   else {  /* thread has no error handler */
  185     L->status = cast_byte(errcode);  /* mark it as dead */
  186     if (G(L)->mainthread->errorJmp) {  /* main thread has a handler? */
  187       setobjs2s(L, G(L)->mainthread->top++, L->top - 1);  /* copy error obj. */
  188       luaD_throw(G(L)->mainthread, errcode);  /* re-throw in main thread */
  189     }
  190     else {  /* no handler at all; abort */
  191       if (G(L)->panic) {  /* panic function? */
  192         lua_unlock(L);
  193         G(L)->panic(L);  /* call it (last chance to jump out) */
  194       }
  195       panic("no error handler");
  196     }
  197   }
  198 }
  199 
  200 #if defined(__GNUC__) && !defined(__clang__) && \
  201         defined(HAVE_INFINITE_RECURSION)
  202 #pragma GCC diagnostic pop
  203 #endif
  204 
  205 
  206 int luaD_rawrunprotected (lua_State *L, Pfunc f, void *ud) {
  207   unsigned short oldnCcalls = L->nCcalls;
  208   struct lua_longjmp lj;
  209   lj.status = LUA_OK;
  210   lj.previous = L->errorJmp;  /* chain new error handler */
  211   L->errorJmp = &lj;
  212   LUAI_TRY(L, &lj,
  213     (*f)(L, ud);
  214   );
  215   L->errorJmp = lj.previous;  /* restore old error handler */
  216   L->nCcalls = oldnCcalls;
  217   return lj.status;
  218 }
  219 
  220 /* }====================================================== */
  221 
  222 
  223 static void correctstack (lua_State *L, TValue *oldstack) {
  224   CallInfo *ci;
  225   GCObject *up;
  226   L->top = (L->top - oldstack) + L->stack;
  227   for (up = L->openupval; up != NULL; up = up->gch.next)
  228     gco2uv(up)->v = (gco2uv(up)->v - oldstack) + L->stack;
  229   for (ci = L->ci; ci != NULL; ci = ci->previous) {
  230     ci->top = (ci->top - oldstack) + L->stack;
  231     ci->func = (ci->func - oldstack) + L->stack;
  232     if (isLua(ci))
  233       ci->u.l.base = (ci->u.l.base - oldstack) + L->stack;
  234   }
  235 }
  236 
  237 
  238 /* some space for error handling */
  239 #define ERRORSTACKSIZE  (LUAI_MAXSTACK + 200)
  240 
  241 
  242 void luaD_reallocstack (lua_State *L, int newsize) {
  243   TValue *oldstack = L->stack;
  244   int lim = L->stacksize;
  245   lua_assert(newsize <= LUAI_MAXSTACK || newsize == ERRORSTACKSIZE);
  246   lua_assert(L->stack_last - L->stack == L->stacksize - EXTRA_STACK);
  247   luaM_reallocvector(L, L->stack, L->stacksize, newsize, TValue);
  248   for (; lim < newsize; lim++)
  249     setnilvalue(L->stack + lim); /* erase new segment */
  250   L->stacksize = newsize;
  251   L->stack_last = L->stack + newsize - EXTRA_STACK;
  252   correctstack(L, oldstack);
  253 }
  254 
  255 
  256 void luaD_growstack (lua_State *L, int n) {
  257   int size = L->stacksize;
  258   if (size > LUAI_MAXSTACK)  /* error after extra size? */
  259     luaD_throw(L, LUA_ERRERR);
  260   else {
  261     int needed = cast_int(L->top - L->stack) + n + EXTRA_STACK;
  262     int newsize = 2 * size;
  263     if (newsize > LUAI_MAXSTACK) newsize = LUAI_MAXSTACK;
  264     if (newsize < needed) newsize = needed;
  265     if (newsize > LUAI_MAXSTACK) {  /* stack overflow? */
  266       luaD_reallocstack(L, ERRORSTACKSIZE);
  267       luaG_runerror(L, "stack overflow");
  268     }
  269     else
  270       luaD_reallocstack(L, newsize);
  271   }
  272 }
  273 
  274 
  275 static int stackinuse (lua_State *L) {
  276   CallInfo *ci;
  277   StkId lim = L->top;
  278   for (ci = L->ci; ci != NULL; ci = ci->previous) {
  279     lua_assert(ci->top <= L->stack_last);
  280     if (lim < ci->top) lim = ci->top;
  281   }
  282   return cast_int(lim - L->stack) + 1;  /* part of stack in use */
  283 }
  284 
  285 
  286 void luaD_shrinkstack (lua_State *L) {
  287   int inuse = stackinuse(L);
  288   int goodsize = inuse + (inuse / 8) + 2*EXTRA_STACK;
  289   if (goodsize > LUAI_MAXSTACK) goodsize = LUAI_MAXSTACK;
  290   if (inuse > LUAI_MAXSTACK ||  /* handling stack overflow? */
  291       goodsize >= L->stacksize)  /* would grow instead of shrink? */
  292     condmovestack(L);  /* don't change stack (change only for debugging) */
  293   else
  294     luaD_reallocstack(L, goodsize);  /* shrink it */
  295 }
  296 
  297 
  298 void luaD_hook (lua_State *L, int event, int line) {
  299   lua_Hook hook = L->hook;
  300   if (hook && L->allowhook) {
  301     CallInfo *ci = L->ci;
  302     ptrdiff_t top = savestack(L, L->top);
  303     ptrdiff_t ci_top = savestack(L, ci->top);
  304     lua_Debug ar;
  305     ar.event = event;
  306     ar.currentline = line;
  307     ar.i_ci = ci;
  308     luaD_checkstack(L, LUA_MINSTACK);  /* ensure minimum stack size */
  309     ci->top = L->top + LUA_MINSTACK;
  310     lua_assert(ci->top <= L->stack_last);
  311     L->allowhook = 0;  /* cannot call hooks inside a hook */
  312     ci->callstatus |= CIST_HOOKED;
  313     lua_unlock(L);
  314     (*hook)(L, &ar);
  315     lua_lock(L);
  316     lua_assert(!L->allowhook);
  317     L->allowhook = 1;
  318     ci->top = restorestack(L, ci_top);
  319     L->top = restorestack(L, top);
  320     ci->callstatus &= ~CIST_HOOKED;
  321   }
  322 }
  323 
  324 
  325 static void callhook (lua_State *L, CallInfo *ci) {
  326   int hook = LUA_HOOKCALL;
  327   ci->u.l.savedpc++;  /* hooks assume 'pc' is already incremented */
  328   if (isLua(ci->previous) &&
  329       GET_OPCODE(*(ci->previous->u.l.savedpc - 1)) == OP_TAILCALL) {
  330     ci->callstatus |= CIST_TAIL;
  331     hook = LUA_HOOKTAILCALL;
  332   }
  333   luaD_hook(L, hook, -1);
  334   ci->u.l.savedpc--;  /* correct 'pc' */
  335 }
  336 
  337 
  338 static StkId adjust_varargs (lua_State *L, Proto *p, int actual) {
  339   int i;
  340   int nfixargs = p->numparams;
  341   StkId base, fixed;
  342   lua_assert(actual >= nfixargs);
  343   /* move fixed parameters to final position */
  344   luaD_checkstack(L, p->maxstacksize);  /* check again for new 'base' */
  345   fixed = L->top - actual;  /* first fixed argument */
  346   base = L->top;  /* final position of first argument */
  347   for (i=0; i<nfixargs; i++) {
  348     setobjs2s(L, L->top++, fixed + i);
  349     setnilvalue(fixed + i);
  350   }
  351   return base;
  352 }
  353 
  354 
  355 static StkId tryfuncTM (lua_State *L, StkId func) {
  356   const TValue *tm = luaT_gettmbyobj(L, func, TM_CALL);
  357   StkId p;
  358   ptrdiff_t funcr = savestack(L, func);
  359   if (!ttisfunction(tm))
  360     luaG_typeerror(L, func, "call");
  361   /* Open a hole inside the stack at `func' */
  362   for (p = L->top; p > func; p--) setobjs2s(L, p, p-1);
  363   incr_top(L);
  364   func = restorestack(L, funcr);  /* previous call may change stack */
  365   setobj2s(L, func, tm);  /* tag method is the new function to be called */
  366   return func;
  367 }
  368 
  369 
  370 
  371 #define next_ci(L) (L->ci = (L->ci->next ? L->ci->next : luaE_extendCI(L)))
  372 
  373 
  374 /*
  375 ** returns true if function has been executed (C function)
  376 */
  377 int luaD_precall (lua_State *L, StkId func, int nresults) {
  378   lua_CFunction f;
  379   CallInfo *ci;
  380   int n;  /* number of arguments (Lua) or returns (C) */
  381   ptrdiff_t funcr = savestack(L, func);
  382   switch (ttype(func)) {
  383     case LUA_TLCF:  /* light C function */
  384       f = fvalue(func);
  385       goto Cfunc;
  386     case LUA_TCCL: {  /* C closure */
  387       f = clCvalue(func)->f;
  388      Cfunc:
  389       luaD_checkstack(L, LUA_MINSTACK);  /* ensure minimum stack size */
  390       ci = next_ci(L);  /* now 'enter' new function */
  391       ci->nresults = nresults;
  392       ci->func = restorestack(L, funcr);
  393       ci->top = L->top + LUA_MINSTACK;
  394       lua_assert(ci->top <= L->stack_last);
  395       ci->callstatus = 0;
  396       luaC_checkGC(L);  /* stack grow uses memory */
  397       if (L->hookmask & LUA_MASKCALL)
  398         luaD_hook(L, LUA_HOOKCALL, -1);
  399       lua_unlock(L);
  400       n = (*f)(L);  /* do the actual call */
  401       lua_lock(L);
  402       api_checknelems(L, n);
  403       luaD_poscall(L, L->top - n);
  404       return 1;
  405     }
  406     case LUA_TLCL: {  /* Lua function: prepare its call */
  407       StkId base;
  408       Proto *p = clLvalue(func)->p;
  409       n = cast_int(L->top - func) - 1;  /* number of real arguments */
  410       luaD_checkstack(L, p->maxstacksize + p->numparams);
  411       for (; n < p->numparams; n++)
  412         setnilvalue(L->top++);  /* complete missing arguments */
  413       if (!p->is_vararg) {
  414         func = restorestack(L, funcr);
  415         base = func + 1;
  416       }
  417       else {
  418         base = adjust_varargs(L, p, n);
  419         func = restorestack(L, funcr);  /* previous call can change stack */
  420       }
  421       ci = next_ci(L);  /* now 'enter' new function */
  422       ci->nresults = nresults;
  423       ci->func = func;
  424       ci->u.l.base = base;
  425       ci->top = base + p->maxstacksize;
  426       lua_assert(ci->top <= L->stack_last);
  427       ci->u.l.savedpc = p->code;  /* starting point */
  428       ci->callstatus = CIST_LUA;
  429       L->top = ci->top;
  430       luaC_checkGC(L);  /* stack grow uses memory */
  431       if (L->hookmask & LUA_MASKCALL)
  432         callhook(L, ci);
  433       return 0;
  434     }
  435     default: {  /* not a function */
  436       func = tryfuncTM(L, func);  /* retry with 'function' tag method */
  437       return luaD_precall(L, func, nresults);  /* now it must be a function */
  438     }
  439   }
  440 }
  441 
  442 
  443 int luaD_poscall (lua_State *L, StkId firstResult) {
  444   StkId res;
  445   int wanted, i;
  446   CallInfo *ci = L->ci;
  447   if (L->hookmask & (LUA_MASKRET | LUA_MASKLINE)) {
  448     if (L->hookmask & LUA_MASKRET) {
  449       ptrdiff_t fr = savestack(L, firstResult);  /* hook may change stack */
  450       luaD_hook(L, LUA_HOOKRET, -1);
  451       firstResult = restorestack(L, fr);
  452     }
  453     L->oldpc = ci->previous->u.l.savedpc;  /* 'oldpc' for caller function */
  454   }
  455   res = ci->func;  /* res == final position of 1st result */
  456   wanted = ci->nresults;
  457   L->ci = ci->previous;  /* back to caller */
  458   /* move results to correct place */
  459   for (i = wanted; i != 0 && firstResult < L->top; i--)
  460     setobjs2s(L, res++, firstResult++);
  461   while (i-- > 0)
  462     setnilvalue(res++);
  463   L->top = res;
  464   return (wanted - LUA_MULTRET);  /* 0 iff wanted == LUA_MULTRET */
  465 }
  466 
  467 
  468 /*
  469 ** Call a function (C or Lua). The function to be called is at *func.
  470 ** The arguments are on the stack, right after the function.
  471 ** When returns, all the results are on the stack, starting at the original
  472 ** function position.
  473 */
  474 void luaD_call (lua_State *L, StkId func, int nResults, int allowyield) {
  475   if (++L->nCcalls >= LUAI_MAXCCALLS) {
  476     if (L->nCcalls == LUAI_MAXCCALLS)
  477       luaG_runerror(L, "C stack overflow");
  478     else if (L->nCcalls >= (LUAI_MAXCCALLS + (LUAI_MAXCCALLS>>3)))
  479       luaD_throw(L, LUA_ERRERR);  /* error while handling stack error */
  480   }
  481   intptr_t remaining = stack_remaining();
  482   if (L->runerror == 0 && remaining < LUAI_MINCSTACK)
  483     luaG_runerror(L, "C stack overflow");
  484   if (L->runerror != 0 && remaining < LUAI_MINCSTACK / 2)
  485     luaD_throw(L, LUA_ERRERR);  /* error while handling stack error */
  486   if (!allowyield) L->nny++;
  487   if (!luaD_precall(L, func, nResults))  /* is a Lua function? */
  488     luaV_execute(L);  /* call it */
  489   if (!allowyield) L->nny--;
  490   L->nCcalls--;
  491 }
  492 
  493 
  494 static void finishCcall (lua_State *L) {
  495   CallInfo *ci = L->ci;
  496   int n;
  497   lua_assert(ci->u.c.k != NULL);  /* must have a continuation */
  498   lua_assert(L->nny == 0);
  499   if (ci->callstatus & CIST_YPCALL) {  /* was inside a pcall? */
  500     ci->callstatus &= ~CIST_YPCALL;  /* finish 'lua_pcall' */
  501     L->errfunc = ci->u.c.old_errfunc;
  502   }
  503   /* finish 'lua_callk'/'lua_pcall' */
  504   adjustresults(L, ci->nresults);
  505   /* call continuation function */
  506   if (!(ci->callstatus & CIST_STAT))  /* no call status? */
  507     ci->u.c.status = LUA_YIELD;  /* 'default' status */
  508   lua_assert(ci->u.c.status != LUA_OK);
  509   ci->callstatus = (ci->callstatus & ~(CIST_YPCALL | CIST_STAT)) | CIST_YIELDED;
  510   lua_unlock(L);
  511   n = (*ci->u.c.k)(L);
  512   lua_lock(L);
  513   api_checknelems(L, n);
  514   /* finish 'luaD_precall' */
  515   luaD_poscall(L, L->top - n);
  516 }
  517 
  518 
  519 static void unroll (lua_State *L, void *ud) {
  520   UNUSED(ud);
  521   for (;;) {
  522     if (L->ci == &L->base_ci)  /* stack is empty? */
  523       return;  /* coroutine finished normally */
  524     if (!isLua(L->ci))  /* C function? */
  525       finishCcall(L);
  526     else {  /* Lua function */
  527       luaV_finishOp(L);  /* finish interrupted instruction */
  528       luaV_execute(L);  /* execute down to higher C 'boundary' */
  529     }
  530   }
  531 }
  532 
  533 
  534 /*
  535 ** check whether thread has a suspended protected call
  536 */
  537 static CallInfo *findpcall (lua_State *L) {
  538   CallInfo *ci;
  539   for (ci = L->ci; ci != NULL; ci = ci->previous) {  /* search for a pcall */
  540     if (ci->callstatus & CIST_YPCALL)
  541       return ci;
  542   }
  543   return NULL;  /* no pending pcall */
  544 }
  545 
  546 
  547 static int recover (lua_State *L, int status) {
  548   StkId oldtop;
  549   CallInfo *ci = findpcall(L);
  550   if (ci == NULL) return 0;  /* no recovery point */
  551   /* "finish" luaD_pcall */
  552   oldtop = restorestack(L, ci->extra);
  553   luaF_close(L, oldtop);
  554   seterrorobj(L, status, oldtop);
  555   L->ci = ci;
  556   L->allowhook = ci->u.c.old_allowhook;
  557   L->nny = 0;  /* should be zero to be yieldable */
  558   luaD_shrinkstack(L);
  559   L->errfunc = ci->u.c.old_errfunc;
  560   ci->callstatus |= CIST_STAT;  /* call has error status */
  561   ci->u.c.status = status;  /* (here it is) */
  562   return 1;  /* continue running the coroutine */
  563 }
  564 
  565 
  566 /*
  567 ** signal an error in the call to 'resume', not in the execution of the
  568 ** coroutine itself. (Such errors should not be handled by any coroutine
  569 ** error handler and should not kill the coroutine.)
  570 */
  571 static l_noret resume_error (lua_State *L, const char *msg, StkId firstArg) {
  572   L->top = firstArg;  /* remove args from the stack */
  573   setsvalue2s(L, L->top, luaS_new(L, msg));  /* push error message */
  574   api_incr_top(L);
  575   luaD_throw(L, -1);  /* jump back to 'lua_resume' */
  576 }
  577 
  578 
  579 /*
  580 ** do the work for 'lua_resume' in protected mode
  581 */
  582 static void resume_cb (lua_State *L, void *ud) {
  583   int nCcalls = L->nCcalls;
  584   StkId firstArg = cast(StkId, ud);
  585   CallInfo *ci = L->ci;
  586   if (nCcalls >= LUAI_MAXCCALLS)
  587     resume_error(L, "C stack overflow", firstArg);
  588   if (L->status == LUA_OK) {  /* may be starting a coroutine */
  589     if (ci != &L->base_ci)  /* not in base level? */
  590       resume_error(L, "cannot resume non-suspended coroutine", firstArg);
  591     /* coroutine is in base level; start running it */
  592     if (!luaD_precall(L, firstArg - 1, LUA_MULTRET))  /* Lua function? */
  593       luaV_execute(L);  /* call it */
  594   }
  595   else if (L->status != LUA_YIELD)
  596     resume_error(L, "cannot resume dead coroutine", firstArg);
  597   else {  /* resuming from previous yield */
  598     L->status = LUA_OK;
  599     ci->func = restorestack(L, ci->extra);
  600     if (isLua(ci))  /* yielded inside a hook? */
  601       luaV_execute(L);  /* just continue running Lua code */
  602     else {  /* 'common' yield */
  603       if (ci->u.c.k != NULL) {  /* does it have a continuation? */
  604         int n;
  605         ci->u.c.status = LUA_YIELD;  /* 'default' status */
  606         ci->callstatus |= CIST_YIELDED;
  607         lua_unlock(L);
  608         n = (*ci->u.c.k)(L);  /* call continuation */
  609         lua_lock(L);
  610         api_checknelems(L, n);
  611         firstArg = L->top - n;  /* yield results come from continuation */
  612       }
  613       luaD_poscall(L, firstArg);  /* finish 'luaD_precall' */
  614     }
  615     unroll(L, NULL);
  616   }
  617   lua_assert(nCcalls == L->nCcalls);
  618 }
  619 
  620 
  621 LUA_API int lua_resume (lua_State *L, lua_State *from, int nargs) {
  622   int status;
  623   int oldnny = L->nny;  /* save 'nny' */
  624   lua_lock(L);
  625   luai_userstateresume(L, nargs);
  626   L->nCcalls = (from) ? from->nCcalls + 1 : 1;
  627   L->nny = 0;  /* allow yields */
  628   api_checknelems(L, (L->status == LUA_OK) ? nargs + 1 : nargs);
  629   status = luaD_rawrunprotected(L, resume_cb, L->top - nargs);
  630   if (status == -1)  /* error calling 'lua_resume'? */
  631     status = LUA_ERRRUN;
  632   else {  /* yield or regular error */
  633     while (status != LUA_OK && status != LUA_YIELD) {  /* error? */
  634       if (recover(L, status))  /* recover point? */
  635         status = luaD_rawrunprotected(L, unroll, NULL);  /* run continuation */
  636       else {  /* unrecoverable error */
  637         L->status = cast_byte(status);  /* mark thread as `dead' */
  638         seterrorobj(L, status, L->top);
  639         L->ci->top = L->top;
  640         break;
  641       }
  642     }
  643     lua_assert(status == L->status);
  644   }
  645   L->nny = oldnny;  /* restore 'nny' */
  646   L->nCcalls--;
  647   lua_assert(L->nCcalls == ((from) ? from->nCcalls : 0));
  648   lua_unlock(L);
  649   return status;
  650 }
  651 
  652 
  653 LUA_API int lua_yieldk (lua_State *L, int nresults, int ctx, lua_CFunction k) {
  654   CallInfo *ci = L->ci;
  655   luai_userstateyield(L, nresults);
  656   lua_lock(L);
  657   api_checknelems(L, nresults);
  658   if (L->nny > 0) {
  659     if (L != G(L)->mainthread)
  660       luaG_runerror(L, "attempt to yield across a C-call boundary");
  661     else
  662       luaG_runerror(L, "attempt to yield from outside a coroutine");
  663   }
  664   L->status = LUA_YIELD;
  665   ci->extra = savestack(L, ci->func);  /* save current 'func' */
  666   if (isLua(ci)) {  /* inside a hook? */
  667     api_check(L, k == NULL, "hooks cannot continue after yielding");
  668   }
  669   else {
  670     if ((ci->u.c.k = k) != NULL)  /* is there a continuation? */
  671       ci->u.c.ctx = ctx;  /* save context */
  672     ci->func = L->top - nresults - 1;  /* protect stack below results */
  673     luaD_throw(L, LUA_YIELD);
  674   }
  675   lua_assert(ci->callstatus & CIST_HOOKED);  /* must be inside a hook */
  676   lua_unlock(L);
  677   return 0;  /* return to 'luaD_hook' */
  678 }
  679 
  680 
  681 int luaD_pcall (lua_State *L, Pfunc func, void *u,
  682                 ptrdiff_t old_top, ptrdiff_t ef) {
  683   int status;
  684   CallInfo *old_ci = L->ci;
  685   lu_byte old_allowhooks = L->allowhook;
  686   unsigned short old_nny = L->nny;
  687   ptrdiff_t old_errfunc = L->errfunc;
  688   L->errfunc = ef;
  689   status = luaD_rawrunprotected(L, func, u);
  690   if (status != LUA_OK) {  /* an error occurred? */
  691     StkId oldtop = restorestack(L, old_top);
  692     luaF_close(L, oldtop);  /* close possible pending closures */
  693     seterrorobj(L, status, oldtop);
  694     L->ci = old_ci;
  695     L->allowhook = old_allowhooks;
  696     L->nny = old_nny;
  697     luaD_shrinkstack(L);
  698   }
  699   L->errfunc = old_errfunc;
  700   return status;
  701 }
  702 
  703 
  704 
  705 /*
  706 ** Execute a protected parser.
  707 */
  708 struct SParser {  /* data to `f_parser' */
  709   ZIO *z;
  710   Mbuffer buff;  /* dynamic structure used by the scanner */
  711   Dyndata dyd;  /* dynamic structures used by the parser */
  712   const char *mode;
  713   const char *name;
  714 };
  715 
  716 
  717 static void checkmode (lua_State *L, const char *mode, const char *x) {
  718   if (mode && strchr(mode, x[0]) == NULL) {
  719     luaO_pushfstring(L,
  720        "attempt to load a %s chunk (mode is " LUA_QS ")", x, mode);
  721     luaD_throw(L, LUA_ERRSYNTAX);
  722   }
  723 }
  724 
  725 
  726 static void f_parser (lua_State *L, void *ud) {
  727   int i;
  728   Closure *cl;
  729   struct SParser *p = cast(struct SParser *, ud);
  730   int c = zgetc(p->z);  /* read first character */
  731   lua_assert(c != LUA_SIGNATURE[0]);    /* binary not supported */
  732   checkmode(L, p->mode, "text");
  733   cl = luaY_parser(L, p->z, &p->buff, &p->dyd, p->name, c);
  734   lua_assert(cl->l.nupvalues == cl->l.p->sizeupvalues);
  735   for (i = 0; i < cl->l.nupvalues; i++) {  /* initialize upvalues */
  736     UpVal *up = luaF_newupval(L);
  737     cl->l.upvals[i] = up;
  738     luaC_objbarrier(L, cl, up);
  739   }
  740 }
  741 
  742 
  743 int luaD_protectedparser (lua_State *L, ZIO *z, const char *name,
  744                                         const char *mode) {
  745   struct SParser p;
  746   int status;
  747   L->nny++;  /* cannot yield during parsing */
  748   p.z = z; p.name = name; p.mode = mode;
  749   p.dyd.actvar.arr = NULL; p.dyd.actvar.size = 0;
  750   p.dyd.gt.arr = NULL; p.dyd.gt.size = 0;
  751   p.dyd.label.arr = NULL; p.dyd.label.size = 0;
  752   luaZ_initbuffer(L, &p.buff);
  753   status = luaD_pcall(L, f_parser, &p, savestack(L, L->top), L->errfunc);
  754   luaZ_freebuffer(L, &p.buff);
  755   luaM_freearray(L, p.dyd.actvar.arr, p.dyd.actvar.size);
  756   luaM_freearray(L, p.dyd.gt.arr, p.dyd.gt.size);
  757   luaM_freearray(L, p.dyd.label.arr, p.dyd.label.size);
  758   L->nny--;
  759   return status;
  760 }

Cache object: bf7f035c6ef827994efb381f70c81132


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]


This page is part of the FreeBSD/Linux Linux Kernel Cross-Reference, and was automatically generated using a modified version of the LXR engine.