Github User Fetcher 1.0.0
C Application with Server and GUI
Loading...
Searching...
No Matches
ldo.c File Reference
#include "lprefix.h"
#include <setjmp.h>
#include <stdlib.h>
#include <string.h>
#include "lua.h"
#include "lapi.h"
#include "ldebug.h"
#include "ldo.h"
#include "lfunc.h"
#include "lgc.h"
#include "lmem.h"
#include "lobject.h"
#include "lopcodes.h"
#include "lparser.h"
#include "lstate.h"
#include "lstring.h"
#include "ltable.h"
#include "ltm.h"
#include "lundump.h"
#include "lvm.h"
#include "lzio.h"

Go to the source code of this file.

Data Structures

struct  lua_longjmp
 
struct  SParser
 

Macros

#define ldo_c
 
#define LUA_CORE
 
#define errorstatus(s)   ((s) > LUA_YIELD)
 
#define LUAI_THROW(L, c)   longjmp((c)->b, 1)
 
#define LUAI_TRY(L, c, a)   if (setjmp((c)->b) == 0) { a }
 
#define luai_jmpbuf   jmp_buf
 
#define ERRORSTACKSIZE   (LUAI_MAXSTACK + 200)
 
#define next_ci(L)   (L->ci = (L->ci->next ? L->ci->next : luaE_extendCI(L)))
 
#define checkstackp(L, n, p)
 

Functions

static void seterrorobj (lua_State *L, int errcode, StkId oldtop)
 
l_noret luaD_throw (lua_State *L, int errcode)
 
int luaD_rawrunprotected (lua_State *L, Pfunc f, void *ud)
 
static void correctstack (lua_State *L, TValue *oldstack)
 
void luaD_reallocstack (lua_State *L, int newsize)
 
void luaD_growstack (lua_State *L, int n)
 
static int stackinuse (lua_State *L)
 
void luaD_shrinkstack (lua_State *L)
 
void luaD_inctop (lua_State *L)
 
void luaD_hook (lua_State *L, int event, int line)
 
static void callhook (lua_State *L, CallInfo *ci)
 
static StkId adjust_varargs (lua_State *L, Proto *p, int actual)
 
static void tryfuncTM (lua_State *L, StkId func)
 
static int moveresults (lua_State *L, const TValue *firstResult, StkId res, int nres, int wanted)
 
int luaD_poscall (lua_State *L, CallInfo *ci, StkId firstResult, int nres)
 
int luaD_precall (lua_State *L, StkId func, int nresults)
 
static void stackerror (lua_State *L)
 
void luaD_call (lua_State *L, StkId func, int nResults)
 
void luaD_callnoyield (lua_State *L, StkId func, int nResults)
 
static void finishCcall (lua_State *L, int status)
 
static void unroll (lua_State *L, void *ud)
 
static CallInfofindpcall (lua_State *L)
 
static int recover (lua_State *L, int status)
 
static int resume_error (lua_State *L, const char *msg, int narg)
 
static void resume (lua_State *L, void *ud)
 
LUA_API int lua_resume (lua_State *L, lua_State *from, int nargs)
 
LUA_API int lua_isyieldable (lua_State *L)
 
LUA_API int lua_yieldk (lua_State *L, int nresults, lua_KContext ctx, lua_KFunction k)
 
int luaD_pcall (lua_State *L, Pfunc func, void *u, ptrdiff_t old_top, ptrdiff_t ef)
 
static void checkmode (lua_State *L, const char *mode, const char *x)
 
static void f_parser (lua_State *L, void *ud)
 
int luaD_protectedparser (lua_State *L, ZIO *z, const char *name, const char *mode)
 

Macro Definition Documentation

◆ checkstackp

#define checkstackp ( L,
n,
p )
Value:
ptrdiff_t t__ = savestack(L, p); /* save 'p' */ \
luaC_checkGC(L), /* stack grow uses memory */ \
p = restorestack(L, t__)) /* 'pos' part: restore 'p' */
#define savestack(L, p)
#define restorestack(L, n)
#define luaD_checkstackaux(L, n, pre, pos)

Definition at line 399 of file lua-5.3.6/src/ldo.c.

399#define checkstackp(L,n,p) \
400 luaD_checkstackaux(L, n, \
401 ptrdiff_t t__ = savestack(L, p); /* save 'p' */ \
402 luaC_checkGC(L), /* stack grow uses memory */ \
403 p = restorestack(L, t__)) /* 'pos' part: restore 'p' */

Referenced by luaD_precall().

◆ ERRORSTACKSIZE

#define ERRORSTACKSIZE   (LUAI_MAXSTACK + 200)

Definition at line 174 of file lua-5.3.6/src/ldo.c.

Referenced by luaD_growstack(), and luaD_reallocstack().

◆ errorstatus

#define errorstatus ( s)    ((s) > LUA_YIELD)

Definition at line 38 of file lua-5.3.6/src/ldo.c.

Referenced by lua_resume().

◆ ldo_c

#define ldo_c

Definition at line 7 of file lua-5.3.6/src/ldo.c.

◆ LUA_CORE

#define LUA_CORE

Definition at line 8 of file lua-5.3.6/src/ldo.c.

◆ luai_jmpbuf

#define luai_jmpbuf   jmp_buf

Definition at line 75 of file lua-5.3.6/src/ldo.c.

◆ LUAI_THROW

#define LUAI_THROW ( L,
c )   longjmp((c)->b, 1)

Definition at line 73 of file lua-5.3.6/src/ldo.c.

Referenced by luaD_throw().

◆ LUAI_TRY

#define LUAI_TRY ( L,
c,
a )   if (setjmp((c)->b) == 0) { a }

Definition at line 74 of file lua-5.3.6/src/ldo.c.

Referenced by luaD_rawrunprotected().

◆ next_ci

#define next_ci ( L)    (L->ci = (L->ci->next ? L->ci->next : luaE_extendCI(L)))

Definition at line 395 of file lua-5.3.6/src/ldo.c.

Referenced by luaD_precall().

Function Documentation

◆ adjust_varargs()

static StkId adjust_varargs ( lua_State * L,
Proto * p,
int actual )
static

Definition at line 293 of file lua-5.3.6/src/ldo.c.

293 {
294 int i;
295 int nfixargs = p->numparams;
296 StkId base, fixed;
297 /* move fixed parameters to final position */
298 fixed = L->top - actual; /* first fixed argument */
299 base = L->top; /* final position of first argument */
300 for (i = 0; i < nfixargs && i < actual; i++) {
301 setobjs2s(L, L->top++, fixed + i);
302 setnilvalue(fixed + i); /* erase original copy (for GC) */
303 }
304 for (; i < nfixargs; i++)
305 setnilvalue(L->top++); /* complete missing arguments */
306 return base;
307}
#define setobjs2s
#define setnilvalue(obj)

References Proto::numparams, setnilvalue, setobjs2s, and lua_State::top.

Referenced by luaD_precall().

◆ callhook()

static void callhook ( lua_State * L,
CallInfo * ci )
static

Definition at line 280 of file lua-5.3.6/src/ldo.c.

280 {
281 int hook = LUA_HOOKCALL;
282 ci->u.l.savedpc++; /* hooks assume 'pc' is already incremented */
283 if (isLua(ci->previous) &&
284 GET_OPCODE(*(ci->previous->u.l.savedpc - 1)) == OP_TAILCALL) {
285 ci->callstatus |= CIST_TAIL;
286 hook = LUA_HOOKTAILCALL;
287 }
288 luaD_hook(L, hook, -1);
289 ci->u.l.savedpc--; /* correct 'pc' */
290}
#define GET_OPCODE(i)
#define isLua(ci)
#define LUA_HOOKCALL
#define CIST_TAIL
#define LUA_HOOKTAILCALL
void luaD_hook(lua_State *L, int event, int line)
struct CallInfo * previous
struct CallInfo::@64::@65 l
const Instruction * savedpc
union CallInfo::@64 u

References CallInfo::callstatus, CIST_TAIL, GET_OPCODE, isLua, CallInfo::l, LUA_HOOKCALL, LUA_HOOKTAILCALL, luaD_hook(), OP_TAILCALL, CallInfo::previous, CallInfo::savedpc, and CallInfo::u.

Referenced by luaD_precall().

◆ checkmode()

static void checkmode ( lua_State * L,
const char * mode,
const char * x )
static

Definition at line 757 of file lua-5.3.6/src/ldo.c.

757 {
758 if (mode && strchr(mode, x[0]) == NULL) {
760 "attempt to load a %s chunk (mode is '%s')", x, mode);
762 }
763}
#define NULL
Definition gmacros.h:924
const char * luaO_pushfstring(lua_State *L, const char *fmt,...)
#define LUA_ERRSYNTAX
l_noret luaD_throw(lua_State *L, int errcode)

References LUA_ERRSYNTAX, luaD_throw(), luaO_pushfstring(), and NULL.

Referenced by f_parser().

◆ correctstack()

static void correctstack ( lua_State * L,
TValue * oldstack )
static

Definition at line 158 of file lua-5.3.6/src/ldo.c.

158 {
159 CallInfo *ci;
160 UpVal *up;
161 L->top = (L->top - oldstack) + L->stack;
162 for (up = L->openupval; up != NULL; up = up->u.open.next)
163 up->v = (up->v - oldstack) + L->stack;
164 for (ci = L->ci; ci != NULL; ci = ci->previous) {
165 ci->top = (ci->top - oldstack) + L->stack;
166 ci->func = (ci->func - oldstack) + L->stack;
167 if (isLua(ci))
168 ci->u.l.base = (ci->u.l.base - oldstack) + L->stack;
169 }
170}
GCObject * openupval

References CallInfo::base, lua_State::ci, CallInfo::func, isLua, CallInfo::l, UpVal::next, NULL, UpVal::open, lua_State::openupval, CallInfo::previous, lua_State::stack, CallInfo::top, lua_State::top, UpVal::u, CallInfo::u, and UpVal::v.

Referenced by luaD_reallocstack().

◆ f_parser()

static void f_parser ( lua_State * L,
void * ud )
static

Definition at line 766 of file lua-5.3.6/src/ldo.c.

766 {
767 LClosure *cl;
768 struct SParser *p = cast(struct SParser *, ud);
769 int c = zgetc(p->z); /* read first character */
770 if (c == LUA_SIGNATURE[0]) {
771 checkmode(L, p->mode, "binary");
772 cl = luaU_undump(L, p->z, p->name);
773 }
774 else {
775 checkmode(L, p->mode, "text");
776 cl = luaY_parser(L, p->z, &p->buff, &p->dyd, p->name, c);
777 }
778 lua_assert(cl->nupvalues == cl->p->sizeupvalues);
779 luaF_initupvals(L, cl);
780}
#define lua_assert(c)
#define cast(t, exp)
#define LUA_SIGNATURE
Proto * luaU_undump(lua_State *L, ZIO *Z, Mbuffer *buff, const char *name)
#define zgetc(z)
static void checkmode(lua_State *L, const char *mode, const char *x)
void luaF_initupvals(lua_State *L, LClosure *cl)
LUAI_FUNC Proto * luaY_parser(lua_State *L, ZIO *z, Mbuffer *buff, const char *name)
Definition noparser.c:25
struct Proto * p
const char * mode
const char * name

References SParser::buff, cast, checkmode(), SParser::dyd, lua_assert, LUA_SIGNATURE, luaF_initupvals(), luaU_undump(), luaY_parser(), SParser::mode, SParser::name, LClosure::p, Proto::sizeupvalues, SParser::z, and zgetc.

Referenced by luaD_protectedparser().

◆ findpcall()

static CallInfo * findpcall ( lua_State * L)
static

Definition at line 566 of file lua-5.3.6/src/ldo.c.

566 {
567 CallInfo *ci;
568 for (ci = L->ci; ci != NULL; ci = ci->previous) { /* search for a pcall */
569 if (ci->callstatus & CIST_YPCALL)
570 return ci;
571 }
572 return NULL; /* no pending pcall */
573}
#define CIST_YPCALL

References CallInfo::callstatus, lua_State::ci, CIST_YPCALL, NULL, and CallInfo::previous.

Referenced by recover().

◆ finishCcall()

static void finishCcall ( lua_State * L,
int status )
static

Definition at line 518 of file lua-5.3.6/src/ldo.c.

518 {
519 CallInfo *ci = L->ci;
520 int n;
521 /* must have a continuation and must be able to call it */
522 lua_assert(ci->u.c.k != NULL && L->nny == 0);
523 /* error status can only happen in a protected call */
524 lua_assert((ci->callstatus & CIST_YPCALL) || status == LUA_YIELD);
525 if (ci->callstatus & CIST_YPCALL) { /* was inside a pcall? */
526 ci->callstatus &= ~CIST_YPCALL; /* continuation is also inside it */
527 L->errfunc = ci->u.c.old_errfunc; /* with the same error function */
528 }
529 /* finish 'lua_callk'/'lua_pcall'; CIST_YPCALL and 'errfunc' already
530 handled */
531 adjustresults(L, ci->nresults);
532 lua_unlock(L);
533 n = (*ci->u.c.k)(L, status, ci->u.c.ctx); /* call continuation function */
534 lua_lock(L);
535 api_checknelems(L, n);
536 luaD_poscall(L, ci, L->top - n, n); /* finish 'luaD_precall' */
537}
#define api_checknelems(L, n)
#define adjustresults(L, nres)
#define lua_unlock(L)
#define lua_lock(L)
#define LUA_YIELD
int luaD_poscall(lua_State *L, CallInfo *ci, StkId firstResult, int nres)
lua_CFunction k
ptrdiff_t old_errfunc
struct CallInfo::@64::@66 c
unsigned short nny

References adjustresults, api_checknelems, CallInfo::c, CallInfo::callstatus, lua_State::ci, CIST_YPCALL, CallInfo::ctx, lua_State::errfunc, CallInfo::k, lua_assert, lua_lock, lua_unlock, LUA_YIELD, luaD_poscall(), lua_State::nny, CallInfo::nresults, NULL, CallInfo::old_errfunc, lua_longjmp::status, lua_State::top, and CallInfo::u.

Referenced by unroll().

◆ lua_isyieldable()

LUA_API int lua_isyieldable ( lua_State * L)

Definition at line 687 of file lua-5.3.6/src/ldo.c.

687 {
688 return (L->nny == 0);
689}

References lua_State::nny.

Referenced by luaB_yieldable(), and luaB_yieldable().

◆ lua_resume()

LUA_API int lua_resume ( lua_State * L,
lua_State * from,
int nargs )

Definition at line 648 of file lua-5.3.6/src/ldo.c.

648 {
649 int status;
650 unsigned short oldnny = L->nny; /* save "number of non-yieldable" calls */
651 lua_lock(L);
652 if (L->status == LUA_OK) { /* may be starting a coroutine */
653 if (L->ci != &L->base_ci) /* not in base level? */
654 return resume_error(L, "cannot resume non-suspended coroutine", nargs);
655 }
656 else if (L->status != LUA_YIELD)
657 return resume_error(L, "cannot resume dead coroutine", nargs);
658 L->nCcalls = (from) ? from->nCcalls + 1 : 1;
659 if (L->nCcalls >= LUAI_MAXCCALLS)
660 return resume_error(L, "C stack overflow", nargs);
661 luai_userstateresume(L, nargs);
662 L->nny = 0; /* allow yields */
663 api_checknelems(L, (L->status == LUA_OK) ? nargs + 1 : nargs);
664 status = luaD_rawrunprotected(L, resume, &nargs);
665 if (status == -1) /* error calling 'lua_resume'? */
666 status = LUA_ERRRUN;
667 else { /* continue running after recoverable errors */
668 while (errorstatus(status) && recover(L, status)) {
669 /* unroll continuation */
670 status = luaD_rawrunprotected(L, unroll, &status);
671 }
672 if (errorstatus(status)) { /* unrecoverable error? */
673 L->status = cast_byte(status); /* mark thread as 'dead' */
674 seterrorobj(L, status, L->top); /* push error message */
675 L->ci->top = L->top;
676 }
677 else lua_assert(status == L->status); /* normal end or yield */
678 }
679 L->nny = oldnny; /* restore 'nny' */
680 L->nCcalls--;
681 lua_assert(L->nCcalls == ((from) ? from->nCcalls : 0));
682 lua_unlock(L);
683 return status;
684}
#define cast_byte(i)
#define LUA_ERRRUN
#define LUAI_MAXCCALLS
#define luai_userstateresume(L, n)
#define LUA_OK
static void unroll(lua_State *L, void *ud)
static int resume_error(lua_State *L, const char *msg, int narg)
static int recover(lua_State *L, int status)
#define errorstatus(s)
int luaD_rawrunprotected(lua_State *L, Pfunc f, void *ud)
static void resume(lua_State *L, void *ud)
static void seterrorobj(lua_State *L, int errcode, StkId oldtop)
unsigned short nCcalls

References api_checknelems, lua_State::base_ci, cast_byte, lua_State::ci, errorstatus, lua_assert, LUA_ERRRUN, lua_lock, LUA_OK, lua_unlock, LUA_YIELD, luaD_rawrunprotected(), LUAI_MAXCCALLS, luai_userstateresume, lua_State::nCcalls, lua_State::nny, recover(), resume(), resume_error(), seterrorobj(), lua_longjmp::status, lua_State::status, CallInfo::top, lua_State::top, and unroll().

◆ lua_yieldk()

LUA_API int lua_yieldk ( lua_State * L,
int nresults,
lua_KContext ctx,
lua_KFunction k )

Definition at line 692 of file lua-5.3.6/src/ldo.c.

693 {
694 CallInfo *ci = L->ci;
695 luai_userstateyield(L, nresults);
696 lua_lock(L);
697 api_checknelems(L, nresults);
698 if (L->nny > 0) {
699 if (L != G(L)->mainthread)
700 luaG_runerror(L, "attempt to yield across a C-call boundary");
701 else
702 luaG_runerror(L, "attempt to yield from outside a coroutine");
703 }
704 L->status = LUA_YIELD;
705 ci->extra = savestack(L, ci->func); /* save current 'func' */
706 if (isLua(ci)) { /* inside a hook? */
707 api_check(L, k == NULL, "hooks cannot continue after yielding");
708 }
709 else {
710 if ((ci->u.c.k = k) != NULL) /* is there a continuation? */
711 ci->u.c.ctx = ctx; /* save context */
712 ci->func = L->top - nresults - 1; /* protect stack below results */
714 }
715 lua_assert(ci->callstatus & CIST_HOOKED); /* must be inside a hook */
716 lua_unlock(L);
717 return 0; /* return to 'luaD_hook' */
718}
void luaG_runerror(lua_State *L, const char *fmt,...)
#define api_check
#define G(L)
#define luai_userstateyield(L, n)
#define CIST_HOOKED

References api_check, api_checknelems, CallInfo::c, CallInfo::callstatus, lua_State::ci, CIST_HOOKED, CallInfo::ctx, CallInfo::extra, CallInfo::func, G, isLua, CallInfo::k, lua_assert, lua_lock, lua_unlock, LUA_YIELD, luaD_throw(), luaG_runerror(), luai_userstateyield, lua_State::nny, NULL, savestack, lua_State::status, lua_State::top, and CallInfo::u.

◆ luaD_call()

void luaD_call ( lua_State * L,
StkId func,
int nResults )

Definition at line 495 of file lua-5.3.6/src/ldo.c.

495 {
496 if (++L->nCcalls >= LUAI_MAXCCALLS)
497 stackerror(L);
498 if (!luaD_precall(L, func, nResults)) /* is a Lua function? */
499 luaV_execute(L); /* call it */
500 L->nCcalls--;
501}
void luaV_execute(lua_State *L, int nexeccalls)
static void stackerror(lua_State *L)
int luaD_precall(lua_State *L, StkId func, int nresults)

References luaD_precall(), LUAI_MAXCCALLS, luaV_execute(), lua_State::nCcalls, and stackerror().

Referenced by luaD_callnoyield().

◆ luaD_callnoyield()

void luaD_callnoyield ( lua_State * L,
StkId func,
int nResults )

Definition at line 507 of file lua-5.3.6/src/ldo.c.

507 {
508 L->nny++;
509 luaD_call(L, func, nResults);
510 L->nny--;
511}
void luaD_call(lua_State *L, StkId func, int nResults)

References luaD_call(), and lua_State::nny.

Referenced by callclosemethod(), dothecall(), dothecall(), f_call(), f_call(), lua_callk(), luaG_errormsg(), luaT_callTM(), luaT_callTM(), and luaT_callTMres().

◆ luaD_growstack()

void luaD_growstack ( lua_State * L,
int n )

Definition at line 191 of file lua-5.3.6/src/ldo.c.

191 {
192 int size = L->stacksize;
193 if (size > LUAI_MAXSTACK) /* error after extra size? */
195 else {
196 int needed = cast_int(L->top - L->stack) + n + EXTRA_STACK;
197 int newsize = 2 * size;
198 if (newsize > LUAI_MAXSTACK) newsize = LUAI_MAXSTACK;
199 if (newsize < needed) newsize = needed;
200 if (newsize > LUAI_MAXSTACK) { /* stack overflow? */
202 luaG_runerror(L, "stack overflow");
203 }
204 else
205 luaD_reallocstack(L, newsize);
206 }
207}
#define cast_int(i)
#define EXTRA_STACK
#define LUA_ERRERR
#define LUAI_MAXSTACK
#define ERRORSTACKSIZE
void luaD_reallocstack(lua_State *L, int newsize)

References cast_int, ERRORSTACKSIZE, EXTRA_STACK, LUA_ERRERR, luaD_reallocstack(), luaD_throw(), luaG_runerror(), LUAI_MAXSTACK, lua_State::stack, lua_State::stacksize, and lua_State::top.

◆ luaD_hook()

void luaD_hook ( lua_State * L,
int event,
int line )

Definition at line 253 of file lua-5.3.6/src/ldo.c.

253 {
254 lua_Hook hook = L->hook;
255 if (hook && L->allowhook) { /* make sure there is a hook */
256 CallInfo *ci = L->ci;
257 ptrdiff_t top = savestack(L, L->top);
258 ptrdiff_t ci_top = savestack(L, ci->top);
259 lua_Debug ar;
260 ar.event = event;
261 ar.currentline = line;
262 ar.i_ci = ci;
263 luaD_checkstack(L, LUA_MINSTACK); /* ensure minimum stack size */
264 ci->top = L->top + LUA_MINSTACK;
265 lua_assert(ci->top <= L->stack_last);
266 L->allowhook = 0; /* cannot call hooks inside a hook */
267 ci->callstatus |= CIST_HOOKED;
268 lua_unlock(L);
269 (*hook)(L, &ar);
270 lua_lock(L);
271 lua_assert(!L->allowhook);
272 L->allowhook = 1;
273 ci->top = restorestack(L, ci_top);
274 L->top = restorestack(L, top);
275 ci->callstatus &= ~CIST_HOOKED;
276 }
277}
#define luaD_checkstack(L, n)
void(* lua_Hook)(lua_State *L, lua_Debug *ar)
#define LUA_MINSTACK

References lua_State::allowhook, CallInfo::callstatus, lua_State::ci, CIST_HOOKED, lua_Debug::currentline, lua_Debug::event, lua_State::hook, lua_Debug::i_ci, lua_assert, lua_lock, LUA_MINSTACK, lua_unlock, luaD_checkstack, restorestack, savestack, lua_State::stack_last, CallInfo::top, and lua_State::top.

Referenced by callhook(), luaD_poscall(), and luaD_precall().

◆ luaD_inctop()

void luaD_inctop ( lua_State * L)

Definition at line 240 of file lua-5.3.6/src/ldo.c.

240 {
241 luaD_checkstack(L, 1);
242 L->top++;
243}

References luaD_checkstack, and lua_State::top.

Referenced by LoadString(), loadStringN(), luaO_pushvfstring(), luaU_undump(), luaY_parser(), and pushstr().

◆ luaD_pcall()

int luaD_pcall ( lua_State * L,
Pfunc func,
void * u,
ptrdiff_t old_top,
ptrdiff_t ef )

Definition at line 721 of file lua-5.3.6/src/ldo.c.

722 {
723 int status;
724 CallInfo *old_ci = L->ci;
725 lu_byte old_allowhooks = L->allowhook;
726 unsigned short old_nny = L->nny;
727 ptrdiff_t old_errfunc = L->errfunc;
728 L->errfunc = ef;
729 status = luaD_rawrunprotected(L, func, u);
730 if (status != LUA_OK) { /* an error occurred? */
731 StkId oldtop = restorestack(L, old_top);
732 luaF_close(L, oldtop); /* close possible pending closures */
733 seterrorobj(L, status, oldtop);
734 L->ci = old_ci;
735 L->allowhook = old_allowhooks;
736 L->nny = old_nny;
738 }
739 L->errfunc = old_errfunc;
740 return status;
741}
void luaF_close(lua_State *L, StkId level)
unsigned char lu_byte
void luaD_shrinkstack(lua_State *L)

References lua_State::allowhook, lua_State::ci, lua_State::errfunc, LUA_OK, luaD_rawrunprotected(), luaD_shrinkstack(), luaF_close(), lua_State::nny, restorestack, seterrorobj(), and lua_longjmp::status.

Referenced by luaD_protectedparser().

◆ luaD_poscall()

int luaD_poscall ( lua_State * L,
CallInfo * ci,
StkId firstResult,
int nres )

Definition at line 376 of file lua-5.3.6/src/ldo.c.

376 {
377 StkId res;
378 int wanted = ci->nresults;
379 if (L->hookmask & (LUA_MASKRET | LUA_MASKLINE)) {
380 if (L->hookmask & LUA_MASKRET) {
381 ptrdiff_t fr = savestack(L, firstResult); /* hook may change stack */
382 luaD_hook(L, LUA_HOOKRET, -1);
383 firstResult = restorestack(L, fr);
384 }
385 L->oldpc = ci->previous->u.l.savedpc; /* 'oldpc' for caller function */
386 }
387 res = ci->func; /* res == final position of 1st result */
388 L->ci = ci->previous; /* back to caller */
389 /* move results to proper place */
390 return moveresults(L, firstResult, res, nres, wanted);
391}
#define LUA_MASKLINE
#define LUA_HOOKRET
#define LUA_MASKRET
static int moveresults(lua_State *L, const TValue *firstResult, StkId res, int nres, int wanted)
const Instruction * oldpc

References lua_State::ci, CallInfo::func, lua_State::hookmask, CallInfo::l, LUA_HOOKRET, LUA_MASKLINE, LUA_MASKRET, luaD_hook(), moveresults(), CallInfo::nresults, lua_State::oldpc, CallInfo::previous, restorestack, CallInfo::savedpc, savestack, and CallInfo::u.

Referenced by finishCcall(), luaD_precall(), and resume().

◆ luaD_precall()

int luaD_precall ( lua_State * L,
StkId func,
int nresults )

Definition at line 413 of file lua-5.3.6/src/ldo.c.

413 {
415 CallInfo *ci;
416 switch (ttype(func)) {
417 case LUA_TCCL: /* C closure */
418 f = clCvalue(func)->f;
419 goto Cfunc;
420 case LUA_TLCF: /* light C function */
421 f = fvalue(func);
422 Cfunc: {
423 int n; /* number of returns */
424 checkstackp(L, LUA_MINSTACK, func); /* ensure minimum stack size */
425 ci = next_ci(L); /* now 'enter' new function */
426 ci->nresults = nresults;
427 ci->func = func;
428 ci->top = L->top + LUA_MINSTACK;
429 lua_assert(ci->top <= L->stack_last);
430 ci->callstatus = 0;
431 if (L->hookmask & LUA_MASKCALL)
432 luaD_hook(L, LUA_HOOKCALL, -1);
433 lua_unlock(L);
434 n = (*f)(L); /* do the actual call */
435 lua_lock(L);
436 api_checknelems(L, n);
437 luaD_poscall(L, ci, L->top - n, n);
438 return 1;
439 }
440 case LUA_TLCL: { /* Lua function: prepare its call */
441 StkId base;
442 Proto *p = clLvalue(func)->p;
443 int n = cast_int(L->top - func) - 1; /* number of real arguments */
444 int fsize = p->maxstacksize; /* frame size */
445 checkstackp(L, fsize, func);
446 if (p->is_vararg)
447 base = adjust_varargs(L, p, n);
448 else { /* non vararg function */
449 for (; n < p->numparams; n++)
450 setnilvalue(L->top++); /* complete missing arguments */
451 base = func + 1;
452 }
453 ci = next_ci(L); /* now 'enter' new function */
454 ci->nresults = nresults;
455 ci->func = func;
456 ci->u.l.base = base;
457 L->top = ci->top = base + fsize;
458 lua_assert(ci->top <= L->stack_last);
459 ci->u.l.savedpc = p->code; /* starting point */
460 ci->callstatus = CIST_LUA;
461 if (L->hookmask & LUA_MASKCALL)
462 callhook(L, ci);
463 return 0;
464 }
465 default: { /* not a function */
466 checkstackp(L, 1, func); /* ensure space for metamethod */
467 tryfuncTM(L, func); /* try to get '__call' metamethod */
468 return luaD_precall(L, func, nresults); /* now it must be a function */
469 }
470 }
471}
#define ttype(o)
#define LUA_MASKCALL
int(* lua_CFunction)(lua_State *L)
#define LUA_TLCL
#define fvalue(o)
#define clCvalue(o)
#define LUA_TCCL
#define LUA_TLCF
#define clLvalue(o)
#define CIST_LUA
static StkId adjust_varargs(lua_State *L, Proto *p, int actual)
static void callhook(lua_State *L, CallInfo *ci)
#define checkstackp(L, n, p)
static void tryfuncTM(lua_State *L, StkId func)
#define next_ci(L)
Instruction * code
struct Proto ** p
lu_byte maxstacksize

References adjust_varargs(), api_checknelems, CallInfo::base, callhook(), CallInfo::callstatus, cast_int, checkstackp, CIST_LUA, clCvalue, clLvalue, Proto::code, CallInfo::func, fvalue, lua_State::hookmask, Proto::is_vararg, CallInfo::l, lua_assert, LUA_HOOKCALL, lua_lock, LUA_MASKCALL, LUA_MINSTACK, LUA_TCCL, LUA_TLCF, LUA_TLCL, lua_unlock, luaD_hook(), luaD_poscall(), luaD_precall(), Proto::maxstacksize, next_ci, CallInfo::nresults, Proto::numparams, Proto::p, CallInfo::savedpc, setnilvalue, lua_State::stack_last, CallInfo::top, lua_State::top, tryfuncTM(), ttype, and CallInfo::u.

Referenced by luaD_call(), luaD_precall(), and resume().

◆ luaD_protectedparser()

int luaD_protectedparser ( lua_State * L,
ZIO * z,
const char * name,
const char * mode )

Definition at line 783 of file lua-5.3.6/src/ldo.c.

784 {
785 struct SParser p;
786 int status;
787 L->nny++; /* cannot yield during parsing */
788 p.z = z; p.name = name; p.mode = mode;
789 p.dyd.actvar.arr = NULL; p.dyd.actvar.size = 0;
790 p.dyd.gt.arr = NULL; p.dyd.gt.size = 0;
791 p.dyd.label.arr = NULL; p.dyd.label.size = 0;
792 luaZ_initbuffer(L, &p.buff);
793 status = luaD_pcall(L, f_parser, &p, savestack(L, L->top), L->errfunc);
794 luaZ_freebuffer(L, &p.buff);
795 luaM_freearray(L, p.dyd.actvar.arr, p.dyd.actvar.size);
796 luaM_freearray(L, p.dyd.gt.arr, p.dyd.gt.size);
797 luaM_freearray(L, p.dyd.label.arr, p.dyd.label.size);
798 L->nny--;
799 return status;
800}
const char * name
Definition lsqlite3.c:2154
#define luaM_freearray(L, b, n, t)
#define luaZ_initbuffer(L, buff)
#define luaZ_freebuffer(L, buff)
static void f_parser(lua_State *L, void *ud)
int luaD_pcall(lua_State *L, Pfunc func, void *u, ptrdiff_t old_top, ptrdiff_t ef)

References Dyndata::actvar, Labellist::arr, Dyndata::arr, SParser::buff, SParser::dyd, lua_State::errfunc, f_parser(), Dyndata::gt, Dyndata::label, luaD_pcall(), luaM_freearray, luaZ_freebuffer, luaZ_initbuffer, SParser::mode, name, SParser::name, lua_State::nny, NULL, savestack, Labellist::size, Dyndata::size, lua_State::top, and SParser::z.

◆ luaD_rawrunprotected()

int luaD_rawrunprotected ( lua_State * L,
Pfunc f,
void * ud )

Definition at line 136 of file lua-5.3.6/src/ldo.c.

136 {
137 unsigned short oldnCcalls = L->nCcalls;
138 struct lua_longjmp lj;
139 lj.status = LUA_OK;
140 lj.previous = L->errorJmp; /* chain new error handler */
141 L->errorJmp = &lj;
142 LUAI_TRY(L, &lj,
143 (*f)(L, ud);
144 );
145 L->errorJmp = lj.previous; /* restore old error handler */
146 L->nCcalls = oldnCcalls;
147 return lj.status;
148}
#define LUAI_TRY(L, c, a)
struct lua_longjmp * errorJmp
struct lua_longjmp * previous
volatile int status

References lua_State::errorJmp, LUA_OK, LUAI_TRY, lua_State::nCcalls, lua_longjmp::previous, and lua_longjmp::status.

Referenced by lua_resume(), and luaD_pcall().

◆ luaD_reallocstack()

void luaD_reallocstack ( lua_State * L,
int newsize )

Definition at line 177 of file lua-5.3.6/src/ldo.c.

177 {
178 TValue *oldstack = L->stack;
179 int lim = L->stacksize;
180 lua_assert(newsize <= LUAI_MAXSTACK || newsize == ERRORSTACKSIZE);
182 luaM_reallocvector(L, L->stack, L->stacksize, newsize, TValue);
183 for (; lim < newsize; lim++)
184 setnilvalue(L->stack + lim); /* erase new segment */
185 L->stacksize = newsize;
186 L->stack_last = L->stack + newsize - EXTRA_STACK;
187 correctstack(L, oldstack);
188}
#define luaM_reallocvector(L, v, oldn, n, t)
static void correctstack(lua_State *L, TValue *oldstack)

References correctstack(), ERRORSTACKSIZE, EXTRA_STACK, lua_assert, LUAI_MAXSTACK, luaM_reallocvector, setnilvalue, lua_State::stack, lua_State::stack_last, and lua_State::stacksize.

Referenced by luaD_growstack(), and luaD_shrinkstack().

◆ luaD_shrinkstack()

void luaD_shrinkstack ( lua_State * L)

Definition at line 221 of file lua-5.3.6/src/ldo.c.

221 {
222 int inuse = stackinuse(L);
223 int goodsize = inuse + (inuse / 8) + 2*EXTRA_STACK;
224 if (goodsize > LUAI_MAXSTACK)
225 goodsize = LUAI_MAXSTACK; /* respect stack limit */
226 if (L->stacksize > LUAI_MAXSTACK) /* had been handling stack overflow? */
227 luaE_freeCI(L); /* free all CIs (list grew because of an error) */
228 else
229 luaE_shrinkCI(L); /* shrink list */
230 /* if thread is currently not handling a stack overflow and its
231 good size is smaller than current size, shrink its stack */
232 if (inuse <= (LUAI_MAXSTACK - EXTRA_STACK) &&
233 goodsize < L->stacksize)
234 luaD_reallocstack(L, goodsize);
235 else /* don't change stack */
236 condmovestack(L,{},{}); /* (change only for debugging) */
237}
#define condmovestack(L)
void luaE_freeCI(lua_State *L)
static int stackinuse(lua_State *L)
void luaE_shrinkCI(lua_State *L)
#define stacksize(th)

References condmovestack, EXTRA_STACK, luaD_reallocstack(), luaE_freeCI(), luaE_shrinkCI(), LUAI_MAXSTACK, stackinuse(), lua_State::stacksize, and stacksize.

Referenced by luaD_pcall(), and recover().

◆ luaD_throw()

l_noret luaD_throw ( lua_State * L,
int errcode )

Definition at line 110 of file lua-5.3.6/src/ldo.c.

110 {
111 if (L->errorJmp) { /* thread has an error handler? */
112 L->errorJmp->status = errcode; /* set status */
113 LUAI_THROW(L, L->errorJmp); /* jump to it */
114 }
115 else { /* thread has no error handler */
116 global_State *g = G(L);
117 L->status = cast_byte(errcode); /* mark it as dead */
118 if (g->mainthread->errorJmp) { /* main thread has a handler? */
119 setobjs2s(L, g->mainthread->top++, L->top - 1); /* copy error obj. */
120 luaD_throw(g->mainthread, errcode); /* re-throw in main thread */
121 }
122 else { /* no handler at all; abort */
123 if (g->panic) { /* panic function? */
124 seterrorobj(L, errcode, L->top); /* assume EXTRA_STACK */
125 if (L->ci->top < L->top)
126 L->ci->top = L->top; /* pushing msg. can break this invariant */
127 lua_unlock(L);
128 g->panic(L); /* call panic function (last chance to jump out) */
129 }
130 abort();
131 }
132 }
133}
#define LUAI_THROW(L, c)
struct lua_State * mainthread
lua_CFunction panic

References cast_byte, lua_State::ci, lua_State::errorJmp, G, lua_unlock, luaD_throw(), LUAI_THROW, global_State::mainthread, global_State::panic, seterrorobj(), setobjs2s, lua_longjmp::status, lua_State::status, CallInfo::top, and lua_State::top.

Referenced by checkmode(), lua_yieldk(), luaD_growstack(), luaD_throw(), and stackerror().

◆ moveresults()

static int moveresults ( lua_State * L,
const TValue * firstResult,
StkId res,
int nres,
int wanted )
static

Definition at line 334 of file lua-5.3.6/src/ldo.c.

335 {
336 switch (wanted) { /* handle typical cases separately */
337 case 0: break; /* nothing to move */
338 case 1: { /* one result needed */
339 if (nres == 0) /* no results? */
340 firstResult = luaO_nilobject; /* adjust with nil */
341 setobjs2s(L, res, firstResult); /* move it to proper place */
342 break;
343 }
344 case LUA_MULTRET: {
345 int i;
346 for (i = 0; i < nres; i++) /* move all results to correct place */
347 setobjs2s(L, res + i, firstResult + i);
348 L->top = res + nres;
349 return 0; /* wanted == LUA_MULTRET */
350 }
351 default: {
352 int i;
353 if (wanted <= nres) { /* enough results? */
354 for (i = 0; i < wanted; i++) /* move wanted results to correct place */
355 setobjs2s(L, res + i, firstResult + i);
356 }
357 else { /* not enough results; use all of them plus nils */
358 for (i = 0; i < nres; i++) /* move all results to correct place */
359 setobjs2s(L, res + i, firstResult + i);
360 for (; i < wanted; i++) /* complete wanted number of results */
361 setnilvalue(res + i);
362 }
363 break;
364 }
365 }
366 L->top = res + wanted; /* top points after the last result */
367 return 1;
368}
#define luaO_nilobject
#define LUA_MULTRET

References LUA_MULTRET, luaO_nilobject, setnilvalue, setobjs2s, and lua_State::top.

Referenced by luaD_poscall().

◆ recover()

static int recover ( lua_State * L,
int status )
static

Definition at line 581 of file lua-5.3.6/src/ldo.c.

581 {
582 StkId oldtop;
583 CallInfo *ci = findpcall(L);
584 if (ci == NULL) return 0; /* no recovery point */
585 /* "finish" luaD_pcall */
586 oldtop = restorestack(L, ci->extra);
587 luaF_close(L, oldtop);
588 seterrorobj(L, status, oldtop);
589 L->ci = ci;
590 L->allowhook = getoah(ci->callstatus); /* restore original 'allowhook' */
591 L->nny = 0; /* should be zero to be yieldable */
593 L->errfunc = ci->u.c.old_errfunc;
594 return 1; /* continue running the coroutine */
595}
static CallInfo * findpcall(lua_State *L)
#define getoah(st)

References lua_State::allowhook, CallInfo::c, CallInfo::callstatus, lua_State::ci, lua_State::errfunc, CallInfo::extra, findpcall(), getoah, luaD_shrinkstack(), luaF_close(), lua_State::nny, NULL, CallInfo::old_errfunc, restorestack, seterrorobj(), lua_longjmp::status, and CallInfo::u.

Referenced by lua_resume().

◆ resume()

static void resume ( lua_State * L,
void * ud )
static

Definition at line 619 of file lua-5.3.6/src/ldo.c.

619 {
620 int n = *(cast(int*, ud)); /* number of arguments */
621 StkId firstArg = L->top - n; /* first argument */
622 CallInfo *ci = L->ci;
623 if (L->status == LUA_OK) { /* starting a coroutine? */
624 if (!luaD_precall(L, firstArg - 1, LUA_MULTRET)) /* Lua function? */
625 luaV_execute(L); /* call it */
626 }
627 else { /* resuming from previous yield */
629 L->status = LUA_OK; /* mark that it is running (again) */
630 ci->func = restorestack(L, ci->extra);
631 if (isLua(ci)) /* yielded inside a hook? */
632 luaV_execute(L); /* just continue running Lua code */
633 else { /* 'common' yield */
634 if (ci->u.c.k != NULL) { /* does it have a continuation function? */
635 lua_unlock(L);
636 n = (*ci->u.c.k)(L, LUA_YIELD, ci->u.c.ctx); /* call continuation */
637 lua_lock(L);
638 api_checknelems(L, n);
639 firstArg = L->top - n; /* yield results come from continuation */
640 }
641 luaD_poscall(L, ci, firstArg, n); /* finish 'luaD_precall' */
642 }
643 unroll(L, NULL); /* run continuation */
644 }
645}

References api_checknelems, CallInfo::c, cast, lua_State::ci, CallInfo::ctx, CallInfo::extra, CallInfo::func, isLua, CallInfo::k, lua_assert, lua_lock, LUA_MULTRET, LUA_OK, lua_unlock, LUA_YIELD, luaD_poscall(), luaD_precall(), luaV_execute(), NULL, restorestack, lua_State::status, lua_State::top, CallInfo::u, and unroll().

Referenced by lua_resume().

◆ resume_error()

static int resume_error ( lua_State * L,
const char * msg,
int narg )
static

Definition at line 603 of file lua-5.3.6/src/ldo.c.

603 {
604 L->top -= narg; /* remove args from the stack */
605 setsvalue2s(L, L->top, luaS_new(L, msg)); /* push error message */
606 api_incr_top(L);
607 lua_unlock(L);
608 return LUA_ERRRUN;
609}
#define api_incr_top(L)
#define setsvalue2s
#define luaS_new(L, s)

References api_incr_top, LUA_ERRRUN, lua_unlock, luaS_new, setsvalue2s, and lua_State::top.

Referenced by lua_resume().

◆ seterrorobj()

static void seterrorobj ( lua_State * L,
int errcode,
StkId oldtop )
static

Definition at line 91 of file lua-5.3.6/src/ldo.c.

91 {
92 switch (errcode) {
93 case LUA_ERRMEM: { /* memory error? */
94 setsvalue2s(L, oldtop, G(L)->memerrmsg); /* reuse preregistered msg. */
95 break;
96 }
97 case LUA_ERRERR: {
98 setsvalue2s(L, oldtop, luaS_newliteral(L, "error in error handling"));
99 break;
100 }
101 default: {
102 setobjs2s(L, oldtop, L->top - 1); /* error message on current top */
103 break;
104 }
105 }
106 L->top = oldtop + 1;
107}
#define luaS_newliteral(L, s)
#define LUA_ERRMEM

References G, LUA_ERRERR, LUA_ERRMEM, luaS_newliteral, setobjs2s, setsvalue2s, and lua_State::top.

Referenced by lua_resume(), luaD_pcall(), luaD_throw(), and recover().

◆ stackerror()

static void stackerror ( lua_State * L)
static

Definition at line 481 of file lua-5.3.6/src/ldo.c.

481 {
482 if (L->nCcalls == LUAI_MAXCCALLS)
483 luaG_runerror(L, "C stack overflow");
484 else if (L->nCcalls >= (LUAI_MAXCCALLS + (LUAI_MAXCCALLS>>3)))
485 luaD_throw(L, LUA_ERRERR); /* error while handing stack error */
486}

References LUA_ERRERR, luaD_throw(), luaG_runerror(), LUAI_MAXCCALLS, and lua_State::nCcalls.

Referenced by luaD_call().

◆ stackinuse()

static int stackinuse ( lua_State * L)
static

Definition at line 210 of file lua-5.3.6/src/ldo.c.

210 {
211 CallInfo *ci;
212 StkId lim = L->top;
213 for (ci = L->ci; ci != NULL; ci = ci->previous) {
214 if (lim < ci->top) lim = ci->top;
215 }
216 lua_assert(lim <= L->stack_last);
217 return cast_int(lim - L->stack) + 1; /* part of stack in use */
218}

References cast_int, lua_State::ci, lua_assert, NULL, CallInfo::previous, lua_State::stack, CallInfo::top, and lua_State::top.

Referenced by luaD_shrinkstack().

◆ tryfuncTM()

static void tryfuncTM ( lua_State * L,
StkId func )
static

Definition at line 315 of file lua-5.3.6/src/ldo.c.

315 {
316 const TValue *tm = luaT_gettmbyobj(L, func, TM_CALL);
317 StkId p;
318 if (!ttisfunction(tm))
319 luaG_typeerror(L, func, "call");
320 /* Open a hole inside the stack at 'func' */
321 for (p = L->top; p > func; p--)
322 setobjs2s(L, p, p-1);
323 L->top++; /* slot ensured by caller */
324 setobj2s(L, func, tm); /* tag method is the new function to be called */
325}
void luaG_typeerror(lua_State *L, const TValue *o, const char *op)
#define setobj2s
#define ttisfunction(o)
const TValue * luaT_gettmbyobj(lua_State *L, const TValue *o, TMS event)
@ TM_CALL

References luaG_typeerror(), luaT_gettmbyobj(), setobj2s, setobjs2s, TM_CALL, lua_State::top, and ttisfunction.

Referenced by luaD_precall().

◆ unroll()

static void unroll ( lua_State * L,
void * ud )
static

Definition at line 548 of file lua-5.3.6/src/ldo.c.

548 {
549 if (ud != NULL) /* error status? */
550 finishCcall(L, *(int *)ud); /* finish 'lua_pcallk' callee */
551 while (L->ci != &L->base_ci) { /* something in the stack */
552 if (!isLua(L->ci)) /* C function? */
553 finishCcall(L, LUA_YIELD); /* complete its execution */
554 else { /* Lua function */
555 luaV_finishOp(L); /* finish interrupted instruction */
556 luaV_execute(L); /* execute down to higher C 'boundary' */
557 }
558 }
559}
void luaV_finishOp(lua_State *L)
static void finishCcall(lua_State *L, int status)

References lua_State::base_ci, lua_State::ci, finishCcall(), isLua, LUA_YIELD, luaV_execute(), luaV_finishOp(), and NULL.

Referenced by lua_resume(), and resume().