#ifndef lstate_h
#define lstate_h
#include "lua.h"
typedef struct CallInfo CallInfo;
#include "lobject.h"
#include "ltm.h"
#include "lzio.h"
#define yieldable(L) (((L)->nCcalls & 0xffff0000) == 0)
#define getCcalls(L) ((L)->nCcalls & 0xffff)
#define incnny(L) ((L)->nCcalls += 0x10000)
#define decnny(L) ((L)->nCcalls -= 0x10000)
#define nyci (0x10000 | 1)
struct lua_longjmp;
#if !defined(l_signalT)
#include <signal.h>
#define l_signalT sig_atomic_t
#endif
#define EXTRA_STACK 5
#if !defined(STRCACHE_N)
#define STRCACHE_N 53
#define STRCACHE_M 2
#endif
#define BASIC_STACK_SIZE (2*LUA_MINSTACK)
#define stacksize(th) cast_int((th)->stack_last.p - (th)->stack.p)
#define KGC_INC 0
#define KGC_GENMINOR 1
#define KGC_GENMAJOR 2
typedef struct stringtable {
TString **hash;
int nuse;
int size;
} stringtable;
struct CallInfo {
StkIdRel func;
StkIdRel top;
struct CallInfo *previous, *next;
union {
struct {
const Instruction *savedpc;
volatile l_signalT trap;
int nextraargs;
} l;
struct {
lua_KFunction k;
ptrdiff_t old_errfunc;
lua_KContext ctx;
} c;
} u;
union {
int funcidx;
int nyield;
int nres;
} u2;
l_uint32 callstatus;
};
#define MAXRESULTS 250
#define CIST_NRESULTS 0xffu
#define CIST_CCMT 8
#define MAX_CCMT (0xfu << CIST_CCMT)
#define CIST_RECST 12
#define CIST_C (1u << (CIST_RECST + 3))
#define CIST_FRESH (cast(l_uint32, CIST_C) << 1)
#define CIST_CLSRET (CIST_FRESH << 1)
#define CIST_TBC (CIST_CLSRET << 1)
#define CIST_OAH (CIST_TBC << 1)
#define CIST_HOOKED (CIST_OAH << 1)
#define CIST_YPCALL (CIST_HOOKED << 1)
#define CIST_TAIL (CIST_YPCALL << 1)
#define CIST_HOOKYIELD (CIST_TAIL << 1)
#define CIST_FIN (CIST_HOOKYIELD << 1)
#define get_nresults(cs) (cast_int((cs) & CIST_NRESULTS) - 1)
#define getcistrecst(ci) (((ci)->callstatus >> CIST_RECST) & 7)
#define setcistrecst(ci,st) \
check_exp(((st) & 7) == (st), \
((ci)->callstatus = ((ci)->callstatus & ~(7u << CIST_RECST)) \
| (cast(l_uint32, st) << CIST_RECST)))
#define isLua(ci) (!((ci)->callstatus & CIST_C))
#define isLuacode(ci) (!((ci)->callstatus & (CIST_C | CIST_HOOKED)))
#define setoah(ci,v) \
((ci)->callstatus = ((v) ? (ci)->callstatus | CIST_OAH \
: (ci)->callstatus & ~CIST_OAH))
#define getoah(ci) (((ci)->callstatus & CIST_OAH) ? 1 : 0)
struct lua_State {
CommonHeader;
lu_byte allowhook;
TStatus status;
StkIdRel top;
struct global_State *l_G;
CallInfo *ci;
StkIdRel stack_last;
StkIdRel stack;
UpVal *openupval;
StkIdRel tbclist;
GCObject *gclist;
struct lua_State *twups;
struct lua_longjmp *errorJmp;
CallInfo base_ci;
volatile lua_Hook hook;
ptrdiff_t errfunc;
l_uint32 nCcalls;
int oldpc;
int nci;
int basehookcount;
int hookcount;
volatile l_signalT hookmask;
struct {
int ftransfer;
int ntransfer;
} transferinfo;
};
typedef struct LX {
lu_byte extra_[LUA_EXTRASPACE];
lua_State l;
} LX;
typedef struct global_State {
lua_Alloc frealloc;
void *ud;
l_mem GCtotalbytes;
l_mem GCdebt;
l_mem GCmarked;
l_mem GCmajorminor;
stringtable strt;
TValue l_registry;
TValue nilvalue;
unsigned int seed;
lu_byte gcparams[LUA_GCPN];
lu_byte currentwhite;
lu_byte gcstate;
lu_byte gckind;
lu_byte gcstopem;
lu_byte gcstp;
lu_byte gcemergency;
GCObject *allgc;
GCObject **sweepgc;
GCObject *finobj;
GCObject *gray;
GCObject *grayagain;
GCObject *weak;
GCObject *ephemeron;
GCObject *allweak;
GCObject *tobefnz;
GCObject *fixedgc;
GCObject *survival;
GCObject *old1;
GCObject *reallyold;
GCObject *firstold1;
GCObject *finobjsur;
GCObject *finobjold1;
GCObject *finobjrold;
struct lua_State *twups;
lua_CFunction panic;
TString *memerrmsg;
TString *tmname[TM_N];
struct Table *mt[LUA_NUMTYPES];
TString *strcache[STRCACHE_N][STRCACHE_M];
lua_WarnFunction warnf;
void *ud_warn;
LX mainth;
} global_State;
#define G(L) (L->l_G)
#define mainthread(G) (&(G)->mainth.l)
#define completestate(g) ttisnil(&g->nilvalue)
union GCUnion {
GCObject gc;
struct TString ts;
struct Udata u;
union Closure cl;
struct Table h;
struct Proto p;
struct lua_State th;
struct UpVal upv;
};
#define cast_u(o) cast(union GCUnion *, (o))
#define gco2ts(o) \
check_exp(novariant((o)->tt) == LUA_TSTRING, &((cast_u(o))->ts))
#define gco2u(o) check_exp((o)->tt == LUA_VUSERDATA, &((cast_u(o))->u))
#define gco2lcl(o) check_exp((o)->tt == LUA_VLCL, &((cast_u(o))->cl.l))
#define gco2ccl(o) check_exp((o)->tt == LUA_VCCL, &((cast_u(o))->cl.c))
#define gco2cl(o) \
check_exp(novariant((o)->tt) == LUA_TFUNCTION, &((cast_u(o))->cl))
#define gco2t(o) check_exp((o)->tt == LUA_VTABLE, &((cast_u(o))->h))
#define gco2p(o) check_exp((o)->tt == LUA_VPROTO, &((cast_u(o))->p))
#define gco2th(o) check_exp((o)->tt == LUA_VTHREAD, &((cast_u(o))->th))
#define gco2upv(o) check_exp((o)->tt == LUA_VUPVAL, &((cast_u(o))->upv))
#define obj2gco(v) \
check_exp(novariant((v)->tt) >= LUA_TSTRING, &(cast_u(v)->gc))
#define gettotalbytes(g) ((g)->GCtotalbytes - (g)->GCdebt)
LUAI_FUNC void luaE_setdebt (global_State *g, l_mem debt);
LUAI_FUNC void luaE_freethread (lua_State *L, lua_State *L1);
LUAI_FUNC lu_mem luaE_threadsize (lua_State *L);
LUAI_FUNC CallInfo *luaE_extendCI (lua_State *L);
LUAI_FUNC void luaE_shrinkCI (lua_State *L);
LUAI_FUNC void luaE_checkcstack (lua_State *L);
LUAI_FUNC void luaE_incCstack (lua_State *L);
LUAI_FUNC void luaE_warning (lua_State *L, const char *msg, int tocont);
LUAI_FUNC void luaE_warnerror (lua_State *L, const char *where);
LUAI_FUNC TStatus luaE_resetthread (lua_State *L, TStatus status);
#endif