Lines Matching refs:L
98 #define LUAI_THROW(L,c) longjmp(&(c)->b) argument
99 #define LUAI_TRY(L,c,a) if (setjmp(&(c)->b) == 0) { a } argument
113 #define LUAI_THROW(L,c) longjmp((c)->b, 1) argument
114 #define LUAI_TRY(L,c,a) if (setjmp((c)->b) == 0) { a } argument
122 #define LUAI_THROW(L,c) throw(c) argument
123 #define LUAI_TRY(L,c,a) \ argument
129 #define LUAI_THROW(L,c) _longjmp((c)->b, 1) argument
130 #define LUAI_TRY(L,c,a) if (_setjmp((c)->b) == 0) { a } argument
135 #define LUAI_THROW(L,c) longjmp((c)->b, 1) argument
136 #define LUAI_TRY(L,c,a) if (setjmp((c)->b) == 0) { a } argument
154 static void seterrorobj (lua_State *L, int errcode, StkId oldtop) { in seterrorobj() argument
157 setsvalue2s(L, oldtop, G(L)->memerrmsg); /* reuse preregistered msg. */ in seterrorobj()
161 setsvalue2s(L, oldtop, luaS_newliteral(L, "error in error handling")); in seterrorobj()
165 setobjs2s(L, oldtop, L->top - 1); /* error message on current top */ in seterrorobj()
169 L->top = oldtop + 1; in seterrorobj()
181 l_noret luaD_throw (lua_State *L, int errcode) { in luaD_throw() argument
182 if (L->errorJmp) { /* thread has an error handler? */ in luaD_throw()
183 L->errorJmp->status = errcode; /* set status */ in luaD_throw()
184 LUAI_THROW(L, L->errorJmp); /* jump to it */ in luaD_throw()
187 L->status = cast_byte(errcode); /* mark it as dead */ in luaD_throw()
188 if (G(L)->mainthread->errorJmp) { /* main thread has a handler? */ in luaD_throw()
189 setobjs2s(L, G(L)->mainthread->top++, L->top - 1); /* copy error obj. */ in luaD_throw()
190 luaD_throw(G(L)->mainthread, errcode); /* re-throw in main thread */ in luaD_throw()
193 if (G(L)->panic) { /* panic function? */ in luaD_throw()
194 lua_unlock(L); in luaD_throw()
195 G(L)->panic(L); /* call it (last chance to jump out) */ in luaD_throw()
208 int luaD_rawrunprotected (lua_State *L, Pfunc f, void *ud) { in luaD_rawrunprotected() argument
209 unsigned short oldnCcalls = L->nCcalls; in luaD_rawrunprotected()
212 lj.previous = L->errorJmp; /* chain new error handler */ in luaD_rawrunprotected()
213 L->errorJmp = &lj; in luaD_rawrunprotected()
214 LUAI_TRY(L, &lj, in luaD_rawrunprotected()
215 (*f)(L, ud); in luaD_rawrunprotected()
217 L->errorJmp = lj.previous; /* restore old error handler */ in luaD_rawrunprotected()
218 L->nCcalls = oldnCcalls; in luaD_rawrunprotected()
225 static void correctstack (lua_State *L, TValue *oldstack) { in correctstack() argument
228 L->top = (L->top - oldstack) + L->stack; in correctstack()
229 for (up = L->openupval; up != NULL; up = up->gch.next) in correctstack()
230 gco2uv(up)->v = (gco2uv(up)->v - oldstack) + L->stack; in correctstack()
231 for (ci = L->ci; ci != NULL; ci = ci->previous) { in correctstack()
232 ci->top = (ci->top - oldstack) + L->stack; in correctstack()
233 ci->func = (ci->func - oldstack) + L->stack; in correctstack()
235 ci->u.l.base = (ci->u.l.base - oldstack) + L->stack; in correctstack()
244 void luaD_reallocstack (lua_State *L, int newsize) { in luaD_reallocstack() argument
245 TValue *oldstack = L->stack; in luaD_reallocstack()
246 int lim = L->stacksize; in luaD_reallocstack()
248 lua_assert(L->stack_last - L->stack == L->stacksize - EXTRA_STACK); in luaD_reallocstack()
249 luaM_reallocvector(L, L->stack, L->stacksize, newsize, TValue); in luaD_reallocstack()
251 setnilvalue(L->stack + lim); /* erase new segment */ in luaD_reallocstack()
252 L->stacksize = newsize; in luaD_reallocstack()
253 L->stack_last = L->stack + newsize - EXTRA_STACK; in luaD_reallocstack()
254 correctstack(L, oldstack); in luaD_reallocstack()
258 void luaD_growstack (lua_State *L, int n) { in luaD_growstack() argument
259 int size = L->stacksize; in luaD_growstack()
261 luaD_throw(L, LUA_ERRERR); in luaD_growstack()
263 int needed = cast_int(L->top - L->stack) + n + EXTRA_STACK; in luaD_growstack()
268 luaD_reallocstack(L, ERRORSTACKSIZE); in luaD_growstack()
269 luaG_runerror(L, "stack overflow"); in luaD_growstack()
272 luaD_reallocstack(L, newsize); in luaD_growstack()
277 static int stackinuse (lua_State *L) { in stackinuse() argument
279 StkId lim = L->top; in stackinuse()
280 for (ci = L->ci; ci != NULL; ci = ci->previous) { in stackinuse()
281 lua_assert(ci->top <= L->stack_last); in stackinuse()
284 return cast_int(lim - L->stack) + 1; /* part of stack in use */ in stackinuse()
288 void luaD_shrinkstack (lua_State *L) { in luaD_shrinkstack() argument
289 int inuse = stackinuse(L); in luaD_shrinkstack()
293 goodsize >= L->stacksize) /* would grow instead of shrink? */ in luaD_shrinkstack()
294 condmovestack(L); /* don't change stack (change only for debugging) */ in luaD_shrinkstack()
296 luaD_reallocstack(L, goodsize); /* shrink it */ in luaD_shrinkstack()
300 void luaD_hook (lua_State *L, int event, int line) { in luaD_hook() argument
301 lua_Hook hook = L->hook; in luaD_hook()
302 if (hook && L->allowhook) { in luaD_hook()
303 CallInfo *ci = L->ci; in luaD_hook()
304 ptrdiff_t top = savestack(L, L->top); in luaD_hook()
305 ptrdiff_t ci_top = savestack(L, ci->top); in luaD_hook()
310 luaD_checkstack(L, LUA_MINSTACK); /* ensure minimum stack size */ in luaD_hook()
311 ci->top = L->top + LUA_MINSTACK; in luaD_hook()
312 lua_assert(ci->top <= L->stack_last); in luaD_hook()
313 L->allowhook = 0; /* cannot call hooks inside a hook */ in luaD_hook()
315 lua_unlock(L); in luaD_hook()
316 (*hook)(L, &ar); in luaD_hook()
317 lua_lock(L); in luaD_hook()
318 lua_assert(!L->allowhook); in luaD_hook()
319 L->allowhook = 1; in luaD_hook()
320 ci->top = restorestack(L, ci_top); in luaD_hook()
321 L->top = restorestack(L, top); in luaD_hook()
327 static void callhook (lua_State *L, CallInfo *ci) { in callhook() argument
335 luaD_hook(L, hook, -1); in callhook()
340 static StkId adjust_varargs (lua_State *L, Proto *p, int actual) { in adjust_varargs() argument
346 luaD_checkstack(L, p->maxstacksize); /* check again for new 'base' */ in adjust_varargs()
347 fixed = L->top - actual; /* first fixed argument */ in adjust_varargs()
348 base = L->top; /* final position of first argument */ in adjust_varargs()
350 setobjs2s(L, L->top++, fixed + i); in adjust_varargs()
357 static StkId tryfuncTM (lua_State *L, StkId func) { in tryfuncTM() argument
358 const TValue *tm = luaT_gettmbyobj(L, func, TM_CALL); in tryfuncTM()
360 ptrdiff_t funcr = savestack(L, func); in tryfuncTM()
362 luaG_typeerror(L, func, "call"); in tryfuncTM()
364 for (p = L->top; p > func; p--) setobjs2s(L, p, p-1); in tryfuncTM()
365 incr_top(L); in tryfuncTM()
366 func = restorestack(L, funcr); /* previous call may change stack */ in tryfuncTM()
367 setobj2s(L, func, tm); /* tag method is the new function to be called */ in tryfuncTM()
373 #define next_ci(L) (L->ci = (L->ci->next ? L->ci->next : luaE_extendCI(L))) argument
379 int luaD_precall (lua_State *L, StkId func, int nresults) { in luaD_precall() argument
383 ptrdiff_t funcr = savestack(L, func); in luaD_precall()
391 luaD_checkstack(L, LUA_MINSTACK); /* ensure minimum stack size */ in luaD_precall()
392 ci = next_ci(L); /* now 'enter' new function */ in luaD_precall()
394 ci->func = restorestack(L, funcr); in luaD_precall()
395 ci->top = L->top + LUA_MINSTACK; in luaD_precall()
396 lua_assert(ci->top <= L->stack_last); in luaD_precall()
398 luaC_checkGC(L); /* stack grow uses memory */ in luaD_precall()
399 if (L->hookmask & LUA_MASKCALL) in luaD_precall()
400 luaD_hook(L, LUA_HOOKCALL, -1); in luaD_precall()
401 lua_unlock(L); in luaD_precall()
402 n = (*f)(L); /* do the actual call */ in luaD_precall()
403 lua_lock(L); in luaD_precall()
404 api_checknelems(L, n); in luaD_precall()
405 luaD_poscall(L, L->top - n); in luaD_precall()
411 n = cast_int(L->top - func) - 1; /* number of real arguments */ in luaD_precall()
412 luaD_checkstack(L, p->maxstacksize + p->numparams); in luaD_precall()
414 setnilvalue(L->top++); /* complete missing arguments */ in luaD_precall()
416 func = restorestack(L, funcr); in luaD_precall()
420 base = adjust_varargs(L, p, n); in luaD_precall()
421 func = restorestack(L, funcr); /* previous call can change stack */ in luaD_precall()
423 ci = next_ci(L); /* now 'enter' new function */ in luaD_precall()
428 lua_assert(ci->top <= L->stack_last); in luaD_precall()
431 L->top = ci->top; in luaD_precall()
432 luaC_checkGC(L); /* stack grow uses memory */ in luaD_precall()
433 if (L->hookmask & LUA_MASKCALL) in luaD_precall()
434 callhook(L, ci); in luaD_precall()
438 func = tryfuncTM(L, func); /* retry with 'function' tag method */ in luaD_precall()
439 return luaD_precall(L, func, nresults); /* now it must be a function */ in luaD_precall()
445 int luaD_poscall (lua_State *L, StkId firstResult) { in luaD_poscall() argument
448 CallInfo *ci = L->ci; in luaD_poscall()
449 if (L->hookmask & (LUA_MASKRET | LUA_MASKLINE)) { in luaD_poscall()
450 if (L->hookmask & LUA_MASKRET) { in luaD_poscall()
451 ptrdiff_t fr = savestack(L, firstResult); /* hook may change stack */ in luaD_poscall()
452 luaD_hook(L, LUA_HOOKRET, -1); in luaD_poscall()
453 firstResult = restorestack(L, fr); in luaD_poscall()
455 L->oldpc = ci->previous->u.l.savedpc; /* 'oldpc' for caller function */ in luaD_poscall()
459 L->ci = ci->previous; /* back to caller */ in luaD_poscall()
461 for (i = wanted; i != 0 && firstResult < L->top; i--) in luaD_poscall()
462 setobjs2s(L, res++, firstResult++); in luaD_poscall()
465 L->top = res; in luaD_poscall()
476 void luaD_call (lua_State *L, StkId func, int nResults, int allowyield) { in luaD_call() argument
477 if (++L->nCcalls >= LUAI_MAXCCALLS) { in luaD_call()
478 if (L->nCcalls == LUAI_MAXCCALLS) in luaD_call()
479 luaG_runerror(L, "C stack overflow"); in luaD_call()
480 else if (L->nCcalls >= (LUAI_MAXCCALLS + (LUAI_MAXCCALLS>>3))) in luaD_call()
481 luaD_throw(L, LUA_ERRERR); /* error while handling stack error */ in luaD_call()
484 if (L->runerror == 0 && remaining < LUAI_MINCSTACK) in luaD_call()
485 luaG_runerror(L, "C stack overflow"); in luaD_call()
486 if (L->runerror != 0 && remaining < LUAI_MINCSTACK / 2) in luaD_call()
487 luaD_throw(L, LUA_ERRERR); /* error while handling stack error */ in luaD_call()
488 if (!allowyield) L->nny++; in luaD_call()
489 if (!luaD_precall(L, func, nResults)) /* is a Lua function? */ in luaD_call()
490 luaV_execute(L); /* call it */ in luaD_call()
491 if (!allowyield) L->nny--; in luaD_call()
492 L->nCcalls--; in luaD_call()
496 static void finishCcall (lua_State *L) { in finishCcall() argument
497 CallInfo *ci = L->ci; in finishCcall()
500 lua_assert(L->nny == 0); in finishCcall()
503 L->errfunc = ci->u.c.old_errfunc; in finishCcall()
506 adjustresults(L, ci->nresults); in finishCcall()
512 lua_unlock(L); in finishCcall()
513 n = (*ci->u.c.k)(L); in finishCcall()
514 lua_lock(L); in finishCcall()
515 api_checknelems(L, n); in finishCcall()
517 luaD_poscall(L, L->top - n); in finishCcall()
521 static void unroll (lua_State *L, void *ud) { in unroll() argument
524 if (L->ci == &L->base_ci) /* stack is empty? */ in unroll()
526 if (!isLua(L->ci)) /* C function? */ in unroll()
527 finishCcall(L); in unroll()
529 luaV_finishOp(L); /* finish interrupted instruction */ in unroll()
530 luaV_execute(L); /* execute down to higher C 'boundary' */ in unroll()
539 static CallInfo *findpcall (lua_State *L) { in findpcall() argument
541 for (ci = L->ci; ci != NULL; ci = ci->previous) { /* search for a pcall */ in findpcall()
549 static int recover (lua_State *L, int status) { in recover() argument
551 CallInfo *ci = findpcall(L); in recover()
554 oldtop = restorestack(L, ci->extra); in recover()
555 luaF_close(L, oldtop); in recover()
556 seterrorobj(L, status, oldtop); in recover()
557 L->ci = ci; in recover()
558 L->allowhook = ci->u.c.old_allowhook; in recover()
559 L->nny = 0; /* should be zero to be yieldable */ in recover()
560 luaD_shrinkstack(L); in recover()
561 L->errfunc = ci->u.c.old_errfunc; in recover()
573 static l_noret resume_error (lua_State *L, const char *msg, StkId firstArg) { in resume_error() argument
574 L->top = firstArg; /* remove args from the stack */ in resume_error()
575 setsvalue2s(L, L->top, luaS_new(L, msg)); /* push error message */ in resume_error()
576 api_incr_top(L); in resume_error()
577 luaD_throw(L, -1); /* jump back to 'lua_resume' */ in resume_error()
584 static void resume_cb (lua_State *L, void *ud) { in resume_cb() argument
585 int nCcalls = L->nCcalls; in resume_cb()
587 CallInfo *ci = L->ci; in resume_cb()
589 resume_error(L, "C stack overflow", firstArg); in resume_cb()
590 if (L->status == LUA_OK) { /* may be starting a coroutine */ in resume_cb()
591 if (ci != &L->base_ci) /* not in base level? */ in resume_cb()
592 resume_error(L, "cannot resume non-suspended coroutine", firstArg); in resume_cb()
594 if (!luaD_precall(L, firstArg - 1, LUA_MULTRET)) /* Lua function? */ in resume_cb()
595 luaV_execute(L); /* call it */ in resume_cb()
597 else if (L->status != LUA_YIELD) in resume_cb()
598 resume_error(L, "cannot resume dead coroutine", firstArg); in resume_cb()
600 L->status = LUA_OK; in resume_cb()
601 ci->func = restorestack(L, ci->extra); in resume_cb()
603 luaV_execute(L); /* just continue running Lua code */ in resume_cb()
609 lua_unlock(L); in resume_cb()
610 n = (*ci->u.c.k)(L); /* call continuation */ in resume_cb()
611 lua_lock(L); in resume_cb()
612 api_checknelems(L, n); in resume_cb()
613 firstArg = L->top - n; /* yield results come from continuation */ in resume_cb()
615 luaD_poscall(L, firstArg); /* finish 'luaD_precall' */ in resume_cb()
617 unroll(L, NULL); in resume_cb()
619 lua_assert(nCcalls == L->nCcalls); in resume_cb()
623 LUA_API int lua_resume (lua_State *L, lua_State *from, int nargs) { in lua_resume() argument
625 int oldnny = L->nny; /* save 'nny' */ in lua_resume()
626 lua_lock(L); in lua_resume()
627 luai_userstateresume(L, nargs); in lua_resume()
628 L->nCcalls = (from) ? from->nCcalls + 1 : 1; in lua_resume()
629 L->nny = 0; /* allow yields */ in lua_resume()
630 api_checknelems(L, (L->status == LUA_OK) ? nargs + 1 : nargs); in lua_resume()
631 status = luaD_rawrunprotected(L, resume_cb, L->top - nargs); in lua_resume()
636 if (recover(L, status)) /* recover point? */ in lua_resume()
637 status = luaD_rawrunprotected(L, unroll, NULL); /* run continuation */ in lua_resume()
639 L->status = cast_byte(status); /* mark thread as `dead' */ in lua_resume()
640 seterrorobj(L, status, L->top); in lua_resume()
641 L->ci->top = L->top; in lua_resume()
645 lua_assert(status == L->status); in lua_resume()
647 L->nny = oldnny; /* restore 'nny' */ in lua_resume()
648 L->nCcalls--; in lua_resume()
649 lua_assert(L->nCcalls == ((from) ? from->nCcalls : 0)); in lua_resume()
650 lua_unlock(L); in lua_resume()
655 LUA_API int lua_yieldk (lua_State *L, int nresults, int ctx, lua_CFunction k) { in lua_yieldk() argument
656 CallInfo *ci = L->ci; in lua_yieldk()
657 luai_userstateyield(L, nresults); in lua_yieldk()
658 lua_lock(L); in lua_yieldk()
659 api_checknelems(L, nresults); in lua_yieldk()
660 if (L->nny > 0) { in lua_yieldk()
661 if (L != G(L)->mainthread) in lua_yieldk()
662 luaG_runerror(L, "attempt to yield across a C-call boundary"); in lua_yieldk()
664 luaG_runerror(L, "attempt to yield from outside a coroutine"); in lua_yieldk()
666 L->status = LUA_YIELD; in lua_yieldk()
667 ci->extra = savestack(L, ci->func); /* save current 'func' */ in lua_yieldk()
669 api_check(L, k == NULL, "hooks cannot continue after yielding"); in lua_yieldk()
674 ci->func = L->top - nresults - 1; /* protect stack below results */ in lua_yieldk()
675 luaD_throw(L, LUA_YIELD); in lua_yieldk()
678 lua_unlock(L); in lua_yieldk()
683 int luaD_pcall (lua_State *L, Pfunc func, void *u, in luaD_pcall() argument
686 CallInfo *old_ci = L->ci; in luaD_pcall()
687 lu_byte old_allowhooks = L->allowhook; in luaD_pcall()
688 unsigned short old_nny = L->nny; in luaD_pcall()
689 ptrdiff_t old_errfunc = L->errfunc; in luaD_pcall()
690 L->errfunc = ef; in luaD_pcall()
691 status = luaD_rawrunprotected(L, func, u); in luaD_pcall()
693 StkId oldtop = restorestack(L, old_top); in luaD_pcall()
694 luaF_close(L, oldtop); /* close possible pending closures */ in luaD_pcall()
695 seterrorobj(L, status, oldtop); in luaD_pcall()
696 L->ci = old_ci; in luaD_pcall()
697 L->allowhook = old_allowhooks; in luaD_pcall()
698 L->nny = old_nny; in luaD_pcall()
699 luaD_shrinkstack(L); in luaD_pcall()
701 L->errfunc = old_errfunc; in luaD_pcall()
719 static void checkmode (lua_State *L, const char *mode, const char *x) { in checkmode() argument
721 luaO_pushfstring(L, in checkmode()
723 luaD_throw(L, LUA_ERRSYNTAX); in checkmode()
728 static void f_parser (lua_State *L, void *ud) { in f_parser() argument
734 checkmode(L, p->mode, "text"); in f_parser()
735 cl = luaY_parser(L, p->z, &p->buff, &p->dyd, p->name, c); in f_parser()
738 UpVal *up = luaF_newupval(L); in f_parser()
740 luaC_objbarrier(L, cl, up); in f_parser()
745 int luaD_protectedparser (lua_State *L, ZIO *z, const char *name, in luaD_protectedparser() argument
749 L->nny++; /* cannot yield during parsing */ in luaD_protectedparser()
754 luaZ_initbuffer(L, &p.buff); in luaD_protectedparser()
755 status = luaD_pcall(L, f_parser, &p, savestack(L, L->top), L->errfunc); in luaD_protectedparser()
756 luaZ_freebuffer(L, &p.buff); in luaD_protectedparser()
757 luaM_freearray(L, p.dyd.actvar.arr, p.dyd.actvar.size); in luaD_protectedparser()
758 luaM_freearray(L, p.dyd.gt.arr, p.dyd.gt.size); in luaD_protectedparser()
759 luaM_freearray(L, p.dyd.label.arr, p.dyd.label.size); in luaD_protectedparser()
760 L->nny--; in luaD_protectedparser()