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