Lines Matching refs:L

50 #define LUAI_THROW(L,c)		longjmp(&(c)->b)  argument
51 #define LUAI_TRY(L,c,a) if (setjmp(&(c)->b) == 0) { a } argument
56 #define LUAI_THROW(L,c) throw(c) argument
57 #define LUAI_TRY(L,c,a) \ argument
63 #define LUAI_THROW(L,c) _longjmp((c)->b, 1) argument
64 #define LUAI_TRY(L,c,a) if (_setjmp((c)->b) == 0) { a } argument
69 #define LUAI_THROW(L,c) longjmp((c)->b, 1) argument
70 #define LUAI_TRY(L,c,a) if (setjmp((c)->b) == 0) { a } argument
88 static void seterrorobj (lua_State *L, int errcode, StkId oldtop) { in seterrorobj() argument
91 setsvalue2s(L, oldtop, G(L)->memerrmsg); /* reuse preregistered msg. */ in seterrorobj()
95 setsvalue2s(L, oldtop, luaS_newliteral(L, "error in error handling")); in seterrorobj()
99 setobjs2s(L, oldtop, L->top - 1); /* error message on current top */ in seterrorobj()
103 L->top = oldtop + 1; in seterrorobj()
107 l_noret luaD_throw (lua_State *L, int errcode) { in luaD_throw() argument
108 if (L->errorJmp) { /* thread has an error handler? */ in luaD_throw()
109 L->errorJmp->status = errcode; /* set status */ in luaD_throw()
110 LUAI_THROW(L, L->errorJmp); /* jump to it */ in luaD_throw()
113 L->status = cast_byte(errcode); /* mark it as dead */ in luaD_throw()
114 if (G(L)->mainthread->errorJmp) { /* main thread has a handler? */ in luaD_throw()
115 setobjs2s(L, G(L)->mainthread->top++, L->top - 1); /* copy error obj. */ in luaD_throw()
116 luaD_throw(G(L)->mainthread, errcode); /* re-throw in main thread */ in luaD_throw()
119 if (G(L)->panic) { /* panic function? */ in luaD_throw()
120 lua_unlock(L); in luaD_throw()
121 G(L)->panic(L); /* call it (last chance to jump out) */ in luaD_throw()
129 int luaD_rawrunprotected (lua_State *L, Pfunc f, void *ud) { in luaD_rawrunprotected() argument
130 unsigned short oldnCcalls = L->nCcalls; in luaD_rawrunprotected()
133 lj.previous = L->errorJmp; /* chain new error handler */ in luaD_rawrunprotected()
134 L->errorJmp = &lj; in luaD_rawrunprotected()
135 LUAI_TRY(L, &lj, in luaD_rawrunprotected()
136 (*f)(L, ud); in luaD_rawrunprotected()
138 L->errorJmp = lj.previous; /* restore old error handler */ in luaD_rawrunprotected()
139 L->nCcalls = oldnCcalls; in luaD_rawrunprotected()
146 static void correctstack (lua_State *L, TValue *oldstack) { in correctstack() argument
149 L->top = (L->top - oldstack) + L->stack; in correctstack()
150 for (up = L->openupval; up != NULL; up = up->gch.next) in correctstack()
151 gco2uv(up)->v = (gco2uv(up)->v - oldstack) + L->stack; in correctstack()
152 for (ci = L->ci; ci != NULL; ci = ci->previous) { in correctstack()
153 ci->top = (ci->top - oldstack) + L->stack; in correctstack()
154 ci->func = (ci->func - oldstack) + L->stack; in correctstack()
156 ci->u.l.base = (ci->u.l.base - oldstack) + L->stack; in correctstack()
165 void luaD_reallocstack (lua_State *L, int newsize) { in luaD_reallocstack() argument
166 TValue *oldstack = L->stack; in luaD_reallocstack()
167 int lim = L->stacksize; in luaD_reallocstack()
169 lua_assert(L->stack_last - L->stack == L->stacksize - EXTRA_STACK); in luaD_reallocstack()
170 luaM_reallocvector(L, L->stack, L->stacksize, newsize, TValue); in luaD_reallocstack()
172 setnilvalue(L->stack + lim); /* erase new segment */ in luaD_reallocstack()
173 L->stacksize = newsize; in luaD_reallocstack()
174 L->stack_last = L->stack + newsize - EXTRA_STACK; in luaD_reallocstack()
175 correctstack(L, oldstack); in luaD_reallocstack()
179 void luaD_growstack (lua_State *L, int n) { in luaD_growstack() argument
180 int size = L->stacksize; in luaD_growstack()
182 luaD_throw(L, LUA_ERRERR); in luaD_growstack()
184 int needed = cast_int(L->top - L->stack) + n + EXTRA_STACK; in luaD_growstack()
189 luaD_reallocstack(L, ERRORSTACKSIZE); in luaD_growstack()
190 luaG_runerror(L, "stack overflow"); in luaD_growstack()
193 luaD_reallocstack(L, newsize); in luaD_growstack()
198 static int stackinuse (lua_State *L) { in stackinuse() argument
200 StkId lim = L->top; in stackinuse()
201 for (ci = L->ci; ci != NULL; ci = ci->previous) { in stackinuse()
202 lua_assert(ci->top <= L->stack_last); in stackinuse()
205 return cast_int(lim - L->stack) + 1; /* part of stack in use */ in stackinuse()
209 void luaD_shrinkstack (lua_State *L) { in luaD_shrinkstack() argument
210 int inuse = stackinuse(L); in luaD_shrinkstack()
214 goodsize >= L->stacksize) /* would grow instead of shrink? */ in luaD_shrinkstack()
215 condmovestack(L); /* don't change stack (change only for debugging) */ in luaD_shrinkstack()
217 luaD_reallocstack(L, goodsize); /* shrink it */ in luaD_shrinkstack()
221 void luaD_hook (lua_State *L, int event, int line) { in luaD_hook() argument
222 lua_Hook hook = L->hook; in luaD_hook()
223 if (hook && L->allowhook) { in luaD_hook()
224 CallInfo *ci = L->ci; in luaD_hook()
225 ptrdiff_t top = savestack(L, L->top); in luaD_hook()
226 ptrdiff_t ci_top = savestack(L, ci->top); in luaD_hook()
231 luaD_checkstack(L, LUA_MINSTACK); /* ensure minimum stack size */ in luaD_hook()
232 ci->top = L->top + LUA_MINSTACK; in luaD_hook()
233 lua_assert(ci->top <= L->stack_last); in luaD_hook()
234 L->allowhook = 0; /* cannot call hooks inside a hook */ in luaD_hook()
236 lua_unlock(L); in luaD_hook()
237 (*hook)(L, &ar); in luaD_hook()
238 lua_lock(L); in luaD_hook()
239 lua_assert(!L->allowhook); in luaD_hook()
240 L->allowhook = 1; in luaD_hook()
241 ci->top = restorestack(L, ci_top); in luaD_hook()
242 L->top = restorestack(L, top); in luaD_hook()
248 static void callhook (lua_State *L, CallInfo *ci) { in callhook() argument
256 luaD_hook(L, hook, -1); in callhook()
261 static StkId adjust_varargs (lua_State *L, Proto *p, int actual) { in adjust_varargs() argument
267 luaD_checkstack(L, p->maxstacksize); /* check again for new 'base' */ in adjust_varargs()
268 fixed = L->top - actual; /* first fixed argument */ in adjust_varargs()
269 base = L->top; /* final position of first argument */ in adjust_varargs()
271 setobjs2s(L, L->top++, fixed + i); in adjust_varargs()
278 static StkId tryfuncTM (lua_State *L, StkId func) { in tryfuncTM() argument
279 const TValue *tm = luaT_gettmbyobj(L, func, TM_CALL); in tryfuncTM()
281 ptrdiff_t funcr = savestack(L, func); in tryfuncTM()
283 luaG_typeerror(L, func, "call"); in tryfuncTM()
285 for (p = L->top; p > func; p--) setobjs2s(L, p, p-1); in tryfuncTM()
286 incr_top(L); in tryfuncTM()
287 func = restorestack(L, funcr); /* previous call may change stack */ in tryfuncTM()
288 setobj2s(L, func, tm); /* tag method is the new function to be called */ in tryfuncTM()
294 #define next_ci(L) (L->ci = (L->ci->next ? L->ci->next : luaE_extendCI(L))) argument
300 int luaD_precall (lua_State *L, StkId func, int nresults) { in luaD_precall() argument
304 ptrdiff_t funcr = savestack(L, func); in luaD_precall()
312 luaD_checkstack(L, LUA_MINSTACK); /* ensure minimum stack size */ in luaD_precall()
313 ci = next_ci(L); /* now 'enter' new function */ in luaD_precall()
315 ci->func = restorestack(L, funcr); in luaD_precall()
316 ci->top = L->top + LUA_MINSTACK; in luaD_precall()
317 lua_assert(ci->top <= L->stack_last); in luaD_precall()
319 luaC_checkGC(L); /* stack grow uses memory */ in luaD_precall()
320 if (L->hookmask & LUA_MASKCALL) in luaD_precall()
321 luaD_hook(L, LUA_HOOKCALL, -1); in luaD_precall()
322 lua_unlock(L); in luaD_precall()
323 n = (*f)(L); /* do the actual call */ in luaD_precall()
324 lua_lock(L); in luaD_precall()
325 api_checknelems(L, n); in luaD_precall()
326 luaD_poscall(L, L->top - n); in luaD_precall()
332 n = cast_int(L->top - func) - 1; /* number of real arguments */ in luaD_precall()
333 luaD_checkstack(L, p->maxstacksize); in luaD_precall()
335 setnilvalue(L->top++); /* complete missing arguments */ in luaD_precall()
337 func = restorestack(L, funcr); in luaD_precall()
341 base = adjust_varargs(L, p, n); in luaD_precall()
342 func = restorestack(L, funcr); /* previous call can change stack */ in luaD_precall()
344 ci = next_ci(L); /* now 'enter' new function */ in luaD_precall()
349 lua_assert(ci->top <= L->stack_last); in luaD_precall()
352 L->top = ci->top; in luaD_precall()
353 luaC_checkGC(L); /* stack grow uses memory */ in luaD_precall()
354 if (L->hookmask & LUA_MASKCALL) in luaD_precall()
355 callhook(L, ci); in luaD_precall()
359 func = tryfuncTM(L, func); /* retry with 'function' tag method */ in luaD_precall()
360 return luaD_precall(L, func, nresults); /* now it must be a function */ in luaD_precall()
366 int luaD_poscall (lua_State *L, StkId firstResult) { in luaD_poscall() argument
369 CallInfo *ci = L->ci; in luaD_poscall()
370 if (L->hookmask & (LUA_MASKRET | LUA_MASKLINE)) { in luaD_poscall()
371 if (L->hookmask & LUA_MASKRET) { in luaD_poscall()
372 ptrdiff_t fr = savestack(L, firstResult); /* hook may change stack */ in luaD_poscall()
373 luaD_hook(L, LUA_HOOKRET, -1); in luaD_poscall()
374 firstResult = restorestack(L, fr); in luaD_poscall()
376 L->oldpc = ci->previous->u.l.savedpc; /* 'oldpc' for caller function */ in luaD_poscall()
380 L->ci = ci = ci->previous; /* back to caller */ in luaD_poscall()
382 for (i = wanted; i != 0 && firstResult < L->top; i--) in luaD_poscall()
383 setobjs2s(L, res++, firstResult++); in luaD_poscall()
386 L->top = res; in luaD_poscall()
397 void luaD_call (lua_State *L, StkId func, int nResults, int allowyield) { in luaD_call() argument
398 if (++L->nCcalls >= LUAI_MAXCCALLS) { in luaD_call()
399 if (L->nCcalls == LUAI_MAXCCALLS) in luaD_call()
400 luaG_runerror(L, "C stack overflow"); in luaD_call()
401 else if (L->nCcalls >= (LUAI_MAXCCALLS + (LUAI_MAXCCALLS>>3))) in luaD_call()
402 luaD_throw(L, LUA_ERRERR); /* error while handing stack error */ in luaD_call()
404 if (!allowyield) L->nny++; in luaD_call()
405 if (!luaD_precall(L, func, nResults)) /* is a Lua function? */ in luaD_call()
406 luaV_execute(L); /* call it */ in luaD_call()
407 if (!allowyield) L->nny--; in luaD_call()
408 L->nCcalls--; in luaD_call()
412 static void finishCcall (lua_State *L) { in finishCcall() argument
413 CallInfo *ci = L->ci; in finishCcall()
416 lua_assert(L->nny == 0); in finishCcall()
419 L->errfunc = ci->u.c.old_errfunc; in finishCcall()
422 adjustresults(L, ci->nresults); in finishCcall()
428 lua_unlock(L); in finishCcall()
429 n = (*ci->u.c.k)(L); in finishCcall()
430 lua_lock(L); in finishCcall()
431 api_checknelems(L, n); in finishCcall()
433 luaD_poscall(L, L->top - n); in finishCcall()
437 static void unroll (lua_State *L, void *ud) { in unroll() argument
440 if (L->ci == &L->base_ci) /* stack is empty? */ in unroll()
442 if (!isLua(L->ci)) /* C function? */ in unroll()
443 finishCcall(L); in unroll()
445 luaV_finishOp(L); /* finish interrupted instruction */ in unroll()
446 luaV_execute(L); /* execute down to higher C 'boundary' */ in unroll()
455 static CallInfo *findpcall (lua_State *L) { in findpcall() argument
457 for (ci = L->ci; ci != NULL; ci = ci->previous) { /* search for a pcall */ in findpcall()
465 static int recover (lua_State *L, int status) { in recover() argument
467 CallInfo *ci = findpcall(L); in recover()
470 oldtop = restorestack(L, ci->extra); in recover()
471 luaF_close(L, oldtop); in recover()
472 seterrorobj(L, status, oldtop); in recover()
473 L->ci = ci; in recover()
474 L->allowhook = ci->u.c.old_allowhook; in recover()
475 L->nny = 0; /* should be zero to be yieldable */ in recover()
476 luaD_shrinkstack(L); in recover()
477 L->errfunc = ci->u.c.old_errfunc; in recover()
489 static l_noret resume_error (lua_State *L, const char *msg, StkId firstArg) { in resume_error() argument
490 L->top = firstArg; /* remove args from the stack */ in resume_error()
491 setsvalue2s(L, L->top, luaS_new(L, msg)); /* push error message */ in resume_error()
492 api_incr_top(L); in resume_error()
493 luaD_throw(L, -1); /* jump back to 'lua_resume' */ in resume_error()
500 static void resume_cb (lua_State *L, void *ud) { in resume_cb() argument
501 int nCcalls = L->nCcalls; in resume_cb()
503 CallInfo *ci = L->ci; in resume_cb()
505 resume_error(L, "C stack overflow", firstArg); in resume_cb()
506 if (L->status == LUA_OK) { /* may be starting a coroutine */ in resume_cb()
507 if (ci != &L->base_ci) /* not in base level? */ in resume_cb()
508 resume_error(L, "cannot resume non-suspended coroutine", firstArg); in resume_cb()
510 if (!luaD_precall(L, firstArg - 1, LUA_MULTRET)) /* Lua function? */ in resume_cb()
511 luaV_execute(L); /* call it */ in resume_cb()
513 else if (L->status != LUA_YIELD) in resume_cb()
514 resume_error(L, "cannot resume dead coroutine", firstArg); in resume_cb()
516 L->status = LUA_OK; in resume_cb()
517 ci->func = restorestack(L, ci->extra); in resume_cb()
519 luaV_execute(L); /* just continue running Lua code */ in resume_cb()
525 lua_unlock(L); in resume_cb()
526 n = (*ci->u.c.k)(L); /* call continuation */ in resume_cb()
527 lua_lock(L); in resume_cb()
528 api_checknelems(L, n); in resume_cb()
529 firstArg = L->top - n; /* yield results come from continuation */ in resume_cb()
531 luaD_poscall(L, firstArg); /* finish 'luaD_precall' */ in resume_cb()
533 unroll(L, NULL); in resume_cb()
535 lua_assert(nCcalls == L->nCcalls); in resume_cb()
539 LUA_API int lua_resume (lua_State *L, lua_State *from, int nargs) { in lua_resume() argument
541 int oldnny = L->nny; /* save 'nny' */ in lua_resume()
542 lua_lock(L); in lua_resume()
543 luai_userstateresume(L, nargs); in lua_resume()
544 L->nCcalls = (from) ? from->nCcalls + 1 : 1; in lua_resume()
545 L->nny = 0; /* allow yields */ in lua_resume()
546 api_checknelems(L, (L->status == LUA_OK) ? nargs + 1 : nargs); in lua_resume()
547 status = luaD_rawrunprotected(L, resume_cb, L->top - nargs); in lua_resume()
552 if (recover(L, status)) /* recover point? */ in lua_resume()
553 status = luaD_rawrunprotected(L, unroll, NULL); /* run continuation */ in lua_resume()
555 L->status = cast_byte(status); /* mark thread as `dead' */ in lua_resume()
556 seterrorobj(L, status, L->top); in lua_resume()
557 L->ci->top = L->top; in lua_resume()
561 lua_assert(status == L->status); in lua_resume()
563 L->nny = oldnny; /* restore 'nny' */ in lua_resume()
564 L->nCcalls--; in lua_resume()
565 lua_assert(L->nCcalls == ((from) ? from->nCcalls : 0)); in lua_resume()
566 lua_unlock(L); in lua_resume()
571 LUA_API int lua_yieldk (lua_State *L, int nresults, int ctx, lua_CFunction k) { in lua_yieldk() argument
572 CallInfo *ci = L->ci; in lua_yieldk()
573 luai_userstateyield(L, nresults); in lua_yieldk()
574 lua_lock(L); in lua_yieldk()
575 api_checknelems(L, nresults); in lua_yieldk()
576 if (L->nny > 0) { in lua_yieldk()
577 if (L != G(L)->mainthread) in lua_yieldk()
578 luaG_runerror(L, "attempt to yield across a C-call boundary"); in lua_yieldk()
580 luaG_runerror(L, "attempt to yield from outside a coroutine"); in lua_yieldk()
582 L->status = LUA_YIELD; in lua_yieldk()
583 ci->extra = savestack(L, ci->func); /* save current 'func' */ in lua_yieldk()
585 api_check(L, k == NULL, "hooks cannot continue after yielding"); in lua_yieldk()
590 ci->func = L->top - nresults - 1; /* protect stack below results */ in lua_yieldk()
591 luaD_throw(L, LUA_YIELD); in lua_yieldk()
594 lua_unlock(L); in lua_yieldk()
599 int luaD_pcall (lua_State *L, Pfunc func, void *u, in luaD_pcall() argument
602 CallInfo *old_ci = L->ci; in luaD_pcall()
603 lu_byte old_allowhooks = L->allowhook; in luaD_pcall()
604 unsigned short old_nny = L->nny; in luaD_pcall()
605 ptrdiff_t old_errfunc = L->errfunc; in luaD_pcall()
606 L->errfunc = ef; in luaD_pcall()
607 status = luaD_rawrunprotected(L, func, u); in luaD_pcall()
609 StkId oldtop = restorestack(L, old_top); in luaD_pcall()
610 luaF_close(L, oldtop); /* close possible pending closures */ in luaD_pcall()
611 seterrorobj(L, status, oldtop); in luaD_pcall()
612 L->ci = old_ci; in luaD_pcall()
613 L->allowhook = old_allowhooks; in luaD_pcall()
614 L->nny = old_nny; in luaD_pcall()
615 luaD_shrinkstack(L); in luaD_pcall()
617 L->errfunc = old_errfunc; in luaD_pcall()
635 static void checkmode (lua_State *L, const char *mode, const char *x) { in checkmode() argument
637 luaO_pushfstring(L, in checkmode()
639 luaD_throw(L, LUA_ERRSYNTAX); in checkmode()
644 static void f_parser (lua_State *L, void *ud) { in f_parser() argument
650 checkmode(L, p->mode, "binary"); in f_parser()
651 cl = luaU_undump(L, p->z, &p->buff, p->name); in f_parser()
654 checkmode(L, p->mode, "text"); in f_parser()
655 cl = luaY_parser(L, p->z, &p->buff, &p->dyd, p->name, c); in f_parser()
659 UpVal *up = luaF_newupval(L); in f_parser()
661 luaC_objbarrier(L, cl, up); in f_parser()
666 int luaD_protectedparser (lua_State *L, ZIO *z, const char *name, in luaD_protectedparser() argument
670 L->nny++; /* cannot yield during parsing */ in luaD_protectedparser()
675 luaZ_initbuffer(L, &p.buff); in luaD_protectedparser()
676 status = luaD_pcall(L, f_parser, &p, savestack(L, L->top), L->errfunc); in luaD_protectedparser()
677 luaZ_freebuffer(L, &p.buff); in luaD_protectedparser()
678 luaM_freearray(L, p.dyd.actvar.arr, p.dyd.actvar.size); in luaD_protectedparser()
679 luaM_freearray(L, p.dyd.gt.arr, p.dyd.gt.size); in luaD_protectedparser()
680 luaM_freearray(L, p.dyd.label.arr, p.dyd.label.size); in luaD_protectedparser()
681 L->nny--; in luaD_protectedparser()