Lines Matching refs:L
53 #define LUAI_THROW(L,c) throw(c) argument
54 #define LUAI_TRY(L,c,a) \ argument
60 #define LUAI_THROW(L,c) _longjmp((c)->b, 1) argument
61 #define LUAI_TRY(L,c,a) if (_setjmp((c)->b) == 0) { a } argument
66 #define LUAI_THROW(L,c) longjmp((c)->b, 1) argument
67 #define LUAI_TRY(L,c,a) if (setjmp((c)->b) == 0) { a } argument
84 static void seterrorobj (lua_State *L, int errcode, StkId oldtop) { in seterrorobj() argument
87 setsvalue2s(L, oldtop, G(L)->memerrmsg); /* reuse preregistered msg. */ in seterrorobj()
91 setsvalue2s(L, oldtop, luaS_newliteral(L, "error in error handling")); in seterrorobj()
95 setobjs2s(L, oldtop, L->top - 1); /* error message on current top */ in seterrorobj()
99 L->top = oldtop + 1; in seterrorobj()
103 l_noret luaD_throw (lua_State *L, int errcode) { in luaD_throw() argument
104 if (L->errorJmp) { /* thread has an error handler? */ in luaD_throw()
105 L->errorJmp->status = errcode; /* set status */ in luaD_throw()
106 LUAI_THROW(L, L->errorJmp); /* jump to it */ in luaD_throw()
109 L->status = cast_byte(errcode); /* mark it as dead */ in luaD_throw()
110 if (G(L)->mainthread->errorJmp) { /* main thread has a handler? */ in luaD_throw()
111 setobjs2s(L, G(L)->mainthread->top++, L->top - 1); /* copy error obj. */ in luaD_throw()
112 luaD_throw(G(L)->mainthread, errcode); /* re-throw in main thread */ in luaD_throw()
115 if (G(L)->panic) { /* panic function? */ in luaD_throw()
116 lua_unlock(L); in luaD_throw()
117 G(L)->panic(L); /* call it (last chance to jump out) */ in luaD_throw()
125 int luaD_rawrunprotected (lua_State *L, Pfunc f, void *ud) { in luaD_rawrunprotected() argument
126 unsigned short oldnCcalls = L->nCcalls; in luaD_rawrunprotected()
129 lj.previous = L->errorJmp; /* chain new error handler */ in luaD_rawrunprotected()
130 L->errorJmp = &lj; in luaD_rawrunprotected()
131 LUAI_TRY(L, &lj, in luaD_rawrunprotected()
132 (*f)(L, ud); in luaD_rawrunprotected()
134 L->errorJmp = lj.previous; /* restore old error handler */ in luaD_rawrunprotected()
135 L->nCcalls = oldnCcalls; in luaD_rawrunprotected()
142 static void correctstack (lua_State *L, TValue *oldstack) { in correctstack() argument
145 L->top = (L->top - oldstack) + L->stack; in correctstack()
146 for (up = L->openupval; up != NULL; up = up->gch.next) in correctstack()
147 gco2uv(up)->v = (gco2uv(up)->v - oldstack) + L->stack; in correctstack()
148 for (ci = L->ci; ci != NULL; ci = ci->previous) { in correctstack()
149 ci->top = (ci->top - oldstack) + L->stack; in correctstack()
150 ci->func = (ci->func - oldstack) + L->stack; in correctstack()
152 ci->u.l.base = (ci->u.l.base - oldstack) + L->stack; in correctstack()
161 void luaD_reallocstack (lua_State *L, int newsize) { in luaD_reallocstack() argument
162 TValue *oldstack = L->stack; in luaD_reallocstack()
163 int lim = L->stacksize; in luaD_reallocstack()
165 lua_assert(L->stack_last - L->stack == L->stacksize - EXTRA_STACK); in luaD_reallocstack()
166 luaM_reallocvector(L, L->stack, L->stacksize, newsize, TValue); in luaD_reallocstack()
168 setnilvalue(L->stack + lim); /* erase new segment */ in luaD_reallocstack()
169 L->stacksize = newsize; in luaD_reallocstack()
170 L->stack_last = L->stack + newsize - EXTRA_STACK; in luaD_reallocstack()
171 correctstack(L, oldstack); in luaD_reallocstack()
175 void luaD_growstack (lua_State *L, int n) { in luaD_growstack() argument
176 int size = L->stacksize; in luaD_growstack()
178 luaD_throw(L, LUA_ERRERR); in luaD_growstack()
180 int needed = cast_int(L->top - L->stack) + n + EXTRA_STACK; in luaD_growstack()
185 luaD_reallocstack(L, ERRORSTACKSIZE); in luaD_growstack()
186 luaG_runerror(L, "stack overflow"); in luaD_growstack()
189 luaD_reallocstack(L, newsize); in luaD_growstack()
194 static int stackinuse (lua_State *L) { in stackinuse() argument
196 StkId lim = L->top; in stackinuse()
197 for (ci = L->ci; ci != NULL; ci = ci->previous) { in stackinuse()
198 lua_assert(ci->top <= L->stack_last); in stackinuse()
201 return cast_int(lim - L->stack) + 1; /* part of stack in use */ in stackinuse()
205 void luaD_shrinkstack (lua_State *L) { in luaD_shrinkstack() argument
206 int inuse = stackinuse(L); in luaD_shrinkstack()
210 goodsize >= L->stacksize) /* would grow instead of shrink? */ in luaD_shrinkstack()
211 condmovestack(L); /* don't change stack (change only for debugging) */ in luaD_shrinkstack()
213 luaD_reallocstack(L, goodsize); /* shrink it */ in luaD_shrinkstack()
217 void luaD_hook (lua_State *L, int event, int line) { in luaD_hook() argument
218 lua_Hook hook = L->hook; in luaD_hook()
219 if (hook && L->allowhook) { in luaD_hook()
220 CallInfo *ci = L->ci; in luaD_hook()
221 ptrdiff_t top = savestack(L, L->top); in luaD_hook()
222 ptrdiff_t ci_top = savestack(L, ci->top); in luaD_hook()
227 luaD_checkstack(L, LUA_MINSTACK); /* ensure minimum stack size */ in luaD_hook()
228 ci->top = L->top + LUA_MINSTACK; in luaD_hook()
229 lua_assert(ci->top <= L->stack_last); in luaD_hook()
230 L->allowhook = 0; /* cannot call hooks inside a hook */ in luaD_hook()
232 lua_unlock(L); in luaD_hook()
233 (*hook)(L, &ar); in luaD_hook()
234 lua_lock(L); in luaD_hook()
235 lua_assert(!L->allowhook); in luaD_hook()
236 L->allowhook = 1; in luaD_hook()
237 ci->top = restorestack(L, ci_top); in luaD_hook()
238 L->top = restorestack(L, top); in luaD_hook()
244 static void callhook (lua_State *L, CallInfo *ci) { in callhook() argument
252 luaD_hook(L, hook, -1); in callhook()
257 static StkId adjust_varargs (lua_State *L, Proto *p, int actual) { in adjust_varargs() argument
263 luaD_checkstack(L, p->maxstacksize); /* check again for new 'base' */ in adjust_varargs()
264 fixed = L->top - actual; /* first fixed argument */ in adjust_varargs()
265 base = L->top; /* final position of first argument */ in adjust_varargs()
267 setobjs2s(L, L->top++, fixed + i); in adjust_varargs()
274 static StkId tryfuncTM (lua_State *L, StkId func) { in tryfuncTM() argument
275 const TValue *tm = luaT_gettmbyobj(L, func, TM_CALL); in tryfuncTM()
277 ptrdiff_t funcr = savestack(L, func); in tryfuncTM()
279 luaG_typeerror(L, func, "call"); in tryfuncTM()
281 for (p = L->top; p > func; p--) setobjs2s(L, p, p-1); in tryfuncTM()
282 incr_top(L); in tryfuncTM()
283 func = restorestack(L, funcr); /* previous call may change stack */ in tryfuncTM()
284 setobj2s(L, func, tm); /* tag method is the new function to be called */ in tryfuncTM()
290 #define next_ci(L) (L->ci = (L->ci->next ? L->ci->next : luaE_extendCI(L))) argument
296 int luaD_precall (lua_State *L, StkId func, int nresults) { in luaD_precall() argument
300 ptrdiff_t funcr = savestack(L, func); in luaD_precall()
308 luaD_checkstack(L, LUA_MINSTACK); /* ensure minimum stack size */ in luaD_precall()
309 ci = next_ci(L); /* now 'enter' new function */ in luaD_precall()
311 ci->func = restorestack(L, funcr); in luaD_precall()
312 ci->top = L->top + LUA_MINSTACK; in luaD_precall()
313 lua_assert(ci->top <= L->stack_last); in luaD_precall()
315 luaC_checkGC(L); /* stack grow uses memory */ in luaD_precall()
316 if (L->hookmask & LUA_MASKCALL) in luaD_precall()
317 luaD_hook(L, LUA_HOOKCALL, -1); in luaD_precall()
318 lua_unlock(L); in luaD_precall()
319 n = (*f)(L); /* do the actual call */ in luaD_precall()
320 lua_lock(L); in luaD_precall()
321 api_checknelems(L, n); in luaD_precall()
322 luaD_poscall(L, L->top - n); in luaD_precall()
328 n = cast_int(L->top - func) - 1; /* number of real arguments */ in luaD_precall()
329 luaD_checkstack(L, p->maxstacksize); in luaD_precall()
331 setnilvalue(L->top++); /* complete missing arguments */ in luaD_precall()
333 func = restorestack(L, funcr); in luaD_precall()
337 base = adjust_varargs(L, p, n); in luaD_precall()
338 func = restorestack(L, funcr); /* previous call can change stack */ in luaD_precall()
340 ci = next_ci(L); /* now 'enter' new function */ in luaD_precall()
345 lua_assert(ci->top <= L->stack_last); in luaD_precall()
348 L->top = ci->top; in luaD_precall()
349 luaC_checkGC(L); /* stack grow uses memory */ in luaD_precall()
350 if (L->hookmask & LUA_MASKCALL) in luaD_precall()
351 callhook(L, ci); in luaD_precall()
355 func = tryfuncTM(L, func); /* retry with 'function' tag method */ in luaD_precall()
356 return luaD_precall(L, func, nresults); /* now it must be a function */ in luaD_precall()
362 int luaD_poscall (lua_State *L, StkId firstResult) { in luaD_poscall() argument
365 CallInfo *ci = L->ci; in luaD_poscall()
366 if (L->hookmask & (LUA_MASKRET | LUA_MASKLINE)) { in luaD_poscall()
367 if (L->hookmask & LUA_MASKRET) { in luaD_poscall()
368 ptrdiff_t fr = savestack(L, firstResult); /* hook may change stack */ in luaD_poscall()
369 luaD_hook(L, LUA_HOOKRET, -1); in luaD_poscall()
370 firstResult = restorestack(L, fr); in luaD_poscall()
372 L->oldpc = ci->previous->u.l.savedpc; /* 'oldpc' for caller function */ in luaD_poscall()
376 L->ci = ci = ci->previous; /* back to caller */ in luaD_poscall()
378 for (i = wanted; i != 0 && firstResult < L->top; i--) in luaD_poscall()
379 setobjs2s(L, res++, firstResult++); in luaD_poscall()
382 L->top = res; in luaD_poscall()
393 void luaD_call (lua_State *L, StkId func, int nResults, int allowyield) { in luaD_call() argument
394 if (++L->nCcalls >= LUAI_MAXCCALLS) { in luaD_call()
395 if (L->nCcalls == LUAI_MAXCCALLS) in luaD_call()
396 luaG_runerror(L, "C stack overflow"); in luaD_call()
397 else if (L->nCcalls >= (LUAI_MAXCCALLS + (LUAI_MAXCCALLS>>3))) in luaD_call()
398 luaD_throw(L, LUA_ERRERR); /* error while handing stack error */ in luaD_call()
400 if (!allowyield) L->nny++; in luaD_call()
401 if (!luaD_precall(L, func, nResults)) /* is a Lua function? */ in luaD_call()
402 luaV_execute(L); /* call it */ in luaD_call()
403 if (!allowyield) L->nny--; in luaD_call()
404 L->nCcalls--; in luaD_call()
408 static void finishCcall (lua_State *L) { in finishCcall() argument
409 CallInfo *ci = L->ci; in finishCcall()
412 lua_assert(L->nny == 0); in finishCcall()
415 L->errfunc = ci->u.c.old_errfunc; in finishCcall()
418 adjustresults(L, ci->nresults); in finishCcall()
424 lua_unlock(L); in finishCcall()
425 n = (*ci->u.c.k)(L); in finishCcall()
426 lua_lock(L); in finishCcall()
427 api_checknelems(L, n); in finishCcall()
429 luaD_poscall(L, L->top - n); in finishCcall()
433 static void unroll (lua_State *L, void *ud) { in unroll() argument
436 if (L->ci == &L->base_ci) /* stack is empty? */ in unroll()
438 if (!isLua(L->ci)) /* C function? */ in unroll()
439 finishCcall(L); in unroll()
441 luaV_finishOp(L); /* finish interrupted instruction */ in unroll()
442 luaV_execute(L); /* execute down to higher C 'boundary' */ in unroll()
451 static CallInfo *findpcall (lua_State *L) { in findpcall() argument
453 for (ci = L->ci; ci != NULL; ci = ci->previous) { /* search for a pcall */ in findpcall()
461 static int recover (lua_State *L, int status) { in recover() argument
463 CallInfo *ci = findpcall(L); in recover()
466 oldtop = restorestack(L, ci->extra); in recover()
467 luaF_close(L, oldtop); in recover()
468 seterrorobj(L, status, oldtop); in recover()
469 L->ci = ci; in recover()
470 L->allowhook = ci->u.c.old_allowhook; in recover()
471 L->nny = 0; /* should be zero to be yieldable */ in recover()
472 luaD_shrinkstack(L); in recover()
473 L->errfunc = ci->u.c.old_errfunc; in recover()
485 static l_noret resume_error (lua_State *L, const char *msg, StkId firstArg) { in resume_error() argument
486 L->top = firstArg; /* remove args from the stack */ in resume_error()
487 setsvalue2s(L, L->top, luaS_new(L, msg)); /* push error message */ in resume_error()
488 api_incr_top(L); in resume_error()
489 luaD_throw(L, -1); /* jump back to 'lua_resume' */ in resume_error()
496 static void resume (lua_State *L, void *ud) { in resume() argument
497 int nCcalls = L->nCcalls; in resume()
499 CallInfo *ci = L->ci; in resume()
501 resume_error(L, "C stack overflow", firstArg); in resume()
502 if (L->status == LUA_OK) { /* may be starting a coroutine */ in resume()
503 if (ci != &L->base_ci) /* not in base level? */ in resume()
504 resume_error(L, "cannot resume non-suspended coroutine", firstArg); in resume()
506 if (!luaD_precall(L, firstArg - 1, LUA_MULTRET)) /* Lua function? */ in resume()
507 luaV_execute(L); /* call it */ in resume()
509 else if (L->status != LUA_YIELD) in resume()
510 resume_error(L, "cannot resume dead coroutine", firstArg); in resume()
512 L->status = LUA_OK; in resume()
513 ci->func = restorestack(L, ci->extra); in resume()
515 luaV_execute(L); /* just continue running Lua code */ in resume()
521 lua_unlock(L); in resume()
522 n = (*ci->u.c.k)(L); /* call continuation */ in resume()
523 lua_lock(L); in resume()
524 api_checknelems(L, n); in resume()
525 firstArg = L->top - n; /* yield results come from continuation */ in resume()
527 luaD_poscall(L, firstArg); /* finish 'luaD_precall' */ in resume()
529 unroll(L, NULL); in resume()
531 lua_assert(nCcalls == L->nCcalls); in resume()
535 LUA_API int lua_resume (lua_State *L, lua_State *from, int nargs) { in lua_resume() argument
537 int oldnny = L->nny; /* save 'nny' */ in lua_resume()
538 lua_lock(L); in lua_resume()
539 luai_userstateresume(L, nargs); in lua_resume()
540 L->nCcalls = (from) ? from->nCcalls + 1 : 1; in lua_resume()
541 L->nny = 0; /* allow yields */ in lua_resume()
542 api_checknelems(L, (L->status == LUA_OK) ? nargs + 1 : nargs); in lua_resume()
543 status = luaD_rawrunprotected(L, resume, L->top - nargs); in lua_resume()
548 if (recover(L, status)) /* recover point? */ in lua_resume()
549 status = luaD_rawrunprotected(L, unroll, NULL); /* run continuation */ in lua_resume()
551 L->status = cast_byte(status); /* mark thread as `dead' */ in lua_resume()
552 seterrorobj(L, status, L->top); in lua_resume()
553 L->ci->top = L->top; in lua_resume()
557 lua_assert(status == L->status); in lua_resume()
559 L->nny = oldnny; /* restore 'nny' */ in lua_resume()
560 L->nCcalls--; in lua_resume()
561 lua_assert(L->nCcalls == ((from) ? from->nCcalls : 0)); in lua_resume()
562 lua_unlock(L); in lua_resume()
567 LUA_API int lua_yieldk (lua_State *L, int nresults, int ctx, lua_CFunction k) { in lua_yieldk() argument
568 CallInfo *ci = L->ci; in lua_yieldk()
569 luai_userstateyield(L, nresults); in lua_yieldk()
570 lua_lock(L); in lua_yieldk()
571 api_checknelems(L, nresults); in lua_yieldk()
572 if (L->nny > 0) { in lua_yieldk()
573 if (L != G(L)->mainthread) in lua_yieldk()
574 luaG_runerror(L, "attempt to yield across a C-call boundary"); in lua_yieldk()
576 luaG_runerror(L, "attempt to yield from outside a coroutine"); in lua_yieldk()
578 L->status = LUA_YIELD; in lua_yieldk()
579 ci->extra = savestack(L, ci->func); /* save current 'func' */ in lua_yieldk()
581 api_check(L, k == NULL, "hooks cannot continue after yielding"); in lua_yieldk()
586 ci->func = L->top - nresults - 1; /* protect stack below results */ in lua_yieldk()
587 luaD_throw(L, LUA_YIELD); in lua_yieldk()
590 lua_unlock(L); in lua_yieldk()
595 int luaD_pcall (lua_State *L, Pfunc func, void *u, in luaD_pcall() argument
598 CallInfo *old_ci = L->ci; in luaD_pcall()
599 lu_byte old_allowhooks = L->allowhook; in luaD_pcall()
600 unsigned short old_nny = L->nny; in luaD_pcall()
601 ptrdiff_t old_errfunc = L->errfunc; in luaD_pcall()
602 L->errfunc = ef; in luaD_pcall()
603 status = luaD_rawrunprotected(L, func, u); in luaD_pcall()
605 StkId oldtop = restorestack(L, old_top); in luaD_pcall()
606 luaF_close(L, oldtop); /* close possible pending closures */ in luaD_pcall()
607 seterrorobj(L, status, oldtop); in luaD_pcall()
608 L->ci = old_ci; in luaD_pcall()
609 L->allowhook = old_allowhooks; in luaD_pcall()
610 L->nny = old_nny; in luaD_pcall()
611 luaD_shrinkstack(L); in luaD_pcall()
613 L->errfunc = old_errfunc; in luaD_pcall()
631 static void checkmode (lua_State *L, const char *mode, const char *x) { in checkmode() argument
633 luaO_pushfstring(L, in checkmode()
635 luaD_throw(L, LUA_ERRSYNTAX); in checkmode()
640 static void f_parser (lua_State *L, void *ud) { in f_parser() argument
646 checkmode(L, p->mode, "binary"); in f_parser()
647 cl = luaU_undump(L, p->z, &p->buff, p->name); in f_parser()
650 checkmode(L, p->mode, "text"); in f_parser()
651 cl = luaY_parser(L, p->z, &p->buff, &p->dyd, p->name, c); in f_parser()
655 UpVal *up = luaF_newupval(L); in f_parser()
657 luaC_objbarrier(L, cl, up); in f_parser()
662 int luaD_protectedparser (lua_State *L, ZIO *z, const char *name, in luaD_protectedparser() argument
666 L->nny++; /* cannot yield during parsing */ in luaD_protectedparser()
671 luaZ_initbuffer(L, &p.buff); in luaD_protectedparser()
672 status = luaD_pcall(L, f_parser, &p, savestack(L, L->top), L->errfunc); in luaD_protectedparser()
673 luaZ_freebuffer(L, &p.buff); in luaD_protectedparser()
674 luaM_freearray(L, p.dyd.actvar.arr, p.dyd.actvar.size); in luaD_protectedparser()
675 luaM_freearray(L, p.dyd.gt.arr, p.dyd.gt.size); in luaD_protectedparser()
676 luaM_freearray(L, p.dyd.label.arr, p.dyd.label.size); in luaD_protectedparser()
677 L->nny--; in luaD_protectedparser()