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