Home | History | Annotate | Line # | Download | only in src
      1 /*	$NetBSD: ldo.h,v 1.12 2023/06/08 21:12:08 nikita Exp $	*/
      2 
      3 /*
      4 ** Id: ldo.h
      5 ** Stack and Call structure of Lua
      6 ** See Copyright Notice in lua.h
      7 */
      8 
      9 #ifndef ldo_h
     10 #define ldo_h
     11 
     12 
     13 #include "llimits.h"
     14 #include "lobject.h"
     15 #include "lstate.h"
     16 #include "lzio.h"
     17 
     18 
     19 /*
     20 ** Macro to check stack size and grow stack if needed.  Parameters
     21 ** 'pre'/'pos' allow the macro to preserve a pointer into the
     22 ** stack across reallocations, doing the work only when needed.
     23 ** It also allows the running of one GC step when the stack is
     24 ** reallocated.
     25 ** 'condmovestack' is used in heavy tests to force a stack reallocation
     26 ** at every check.
     27 */
     28 #define luaD_checkstackaux(L,n,pre,pos)  \
     29 	if (l_unlikely(L->stack_last.p - L->top.p <= (n))) \
     30 	  { pre; luaD_growstack(L, n, 1); pos; } \
     31         else { condmovestack(L,pre,pos); }
     32 
     33 /* In general, 'pre'/'pos' are empty (nothing to save) */
     34 #define luaD_checkstack(L,n)	luaD_checkstackaux(L,n,(void)0,(void)0)
     35 
     36 
     37 
     38 #define savestack(L,pt)		(cast_charp(pt) - cast_charp(L->stack.p))
     39 #define restorestack(L,n)	cast(StkId, cast_charp(L->stack.p) + (n))
     40 
     41 
     42 /* macro to check stack size, preserving 'p' */
     43 #define checkstackp(L,n,p)  \
     44   luaD_checkstackaux(L, n, \
     45     ptrdiff_t t__ = savestack(L, p),  /* save 'p' */ \
     46     p = restorestack(L, t__))  /* 'pos' part: restore 'p' */
     47 
     48 
     49 /* macro to check stack size and GC, preserving 'p' */
     50 #define checkstackGCp(L,n,p)  \
     51   luaD_checkstackaux(L, n, \
     52     ptrdiff_t t__ = savestack(L, p);  /* save 'p' */ \
     53     luaC_checkGC(L),  /* stack grow uses memory */ \
     54     p = restorestack(L, t__))  /* 'pos' part: restore 'p' */
     55 
     56 
     57 /* macro to check stack size and GC */
     58 #define checkstackGC(L,fsize)  \
     59 	luaD_checkstackaux(L, (fsize), luaC_checkGC(L), (void)0)
     60 
     61 
     62 /* type of protected functions, to be ran by 'runprotected' */
     63 typedef void (*Pfunc) (lua_State *L, void *ud);
     64 
     65 LUAI_FUNC void luaD_seterrorobj (lua_State *L, int errcode, StkId oldtop);
     66 LUAI_FUNC int luaD_protectedparser (lua_State *L, ZIO *z, const char *name,
     67                                                   const char *mode);
     68 LUAI_FUNC void luaD_hook (lua_State *L, int event, int line,
     69                                         int fTransfer, int nTransfer);
     70 LUAI_FUNC void luaD_hookcall (lua_State *L, CallInfo *ci);
     71 LUAI_FUNC int luaD_pretailcall (lua_State *L, CallInfo *ci, StkId func,
     72                                               int narg1, int delta);
     73 LUAI_FUNC CallInfo *luaD_precall (lua_State *L, StkId func, int nResults);
     74 LUAI_FUNC void luaD_call (lua_State *L, StkId func, int nResults);
     75 LUAI_FUNC void luaD_callnoyield (lua_State *L, StkId func, int nResults);
     76 LUAI_FUNC StkId luaD_tryfuncTM (lua_State *L, StkId func);
     77 LUAI_FUNC int luaD_closeprotected (lua_State *L, ptrdiff_t level, int status);
     78 LUAI_FUNC int luaD_pcall (lua_State *L, Pfunc func, void *u,
     79                                         ptrdiff_t oldtop, ptrdiff_t ef);
     80 LUAI_FUNC void luaD_poscall (lua_State *L, CallInfo *ci, int nres);
     81 LUAI_FUNC int luaD_reallocstack (lua_State *L, int newsize, int raiseerror);
     82 LUAI_FUNC int luaD_growstack (lua_State *L, int n, int raiseerror);
     83 LUAI_FUNC void luaD_shrinkstack (lua_State *L);
     84 LUAI_FUNC void luaD_inctop (lua_State *L);
     85 
     86 LUAI_FUNC l_noret luaD_throw (lua_State *L, int errcode);
     87 LUAI_FUNC int luaD_rawrunprotected (lua_State *L, Pfunc f, void *ud);
     88 
     89 #endif
     90 
     91