1997-11-19 20:31:19 +03:00
|
|
|
/*
|
2017-12-19 19:40:17 +03:00
|
|
|
** $Id: lstate.h,v 2.152 2017/11/23 16:35:54 roberto Exp roberto $
|
1997-11-19 20:31:19 +03:00
|
|
|
** Global State
|
|
|
|
** See Copyright Notice in lua.h
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef lstate_h
|
|
|
|
#define lstate_h
|
|
|
|
|
1998-06-03 00:37:04 +04:00
|
|
|
#include "lua.h"
|
2001-12-05 23:15:18 +03:00
|
|
|
|
|
|
|
#include "lobject.h"
|
|
|
|
#include "ltm.h"
|
2002-10-08 22:46:08 +04:00
|
|
|
#include "lzio.h"
|
1997-11-19 20:31:19 +03:00
|
|
|
|
|
|
|
|
2008-06-26 23:42:45 +04:00
|
|
|
/*
|
|
|
|
|
2013-09-11 18:09:55 +04:00
|
|
|
** Some notes about garbage-collected objects: All objects in Lua must
|
|
|
|
** be kept somehow accessible until being freed, so all objects always
|
|
|
|
** belong to one (and only one) of these lists, using field 'next' of
|
|
|
|
** the 'CommonHeader' for the link:
|
2008-06-26 23:42:45 +04:00
|
|
|
**
|
2014-10-30 21:53:28 +03:00
|
|
|
** 'allgc': all objects not marked for finalization;
|
|
|
|
** 'finobj': all objects marked for finalization;
|
2016-12-22 16:08:50 +03:00
|
|
|
** 'tobefnz': all objects ready to be finalized;
|
2014-10-30 21:53:28 +03:00
|
|
|
** 'fixedgc': all objects that are not to be collected (currently
|
2013-08-22 00:09:51 +04:00
|
|
|
** only small strings, such as reserved words).
|
2017-02-15 21:52:13 +03:00
|
|
|
**
|
|
|
|
** Moreover, there is another set of lists that control gray objects.
|
|
|
|
** These lists are linked by fields 'gclist'. (All objects that
|
|
|
|
** can become gray have such a field. The field is not the same
|
|
|
|
** in all objects, but it always has this name.) Any gray object
|
|
|
|
** must belong to one of these lists, and all objects in these lists
|
|
|
|
** must be gray:
|
|
|
|
**
|
|
|
|
** 'gray': regular gray objects, still waiting to be visited.
|
|
|
|
** 'grayagain': objects that must be revisited at the atomic phase.
|
|
|
|
** That includes
|
|
|
|
** - black objects got in a write barrier;
|
|
|
|
** - all kinds of weak tables during propagation phase;
|
|
|
|
** - all threads.
|
|
|
|
** 'weak': tables with weak values to be cleared;
|
|
|
|
** 'ephemeron': ephemeron tables with white->white entries;
|
|
|
|
** 'allweak': tables with weak keys and/or weak values to be cleared.
|
2017-05-04 16:32:01 +03:00
|
|
|
** There is also a list 'protogray' for prototypes that need to have
|
|
|
|
** their caches cleared.
|
2008-06-26 23:42:45 +04:00
|
|
|
|
|
|
|
*/
|
|
|
|
|
1999-05-10 17:54:01 +04:00
|
|
|
|
2000-09-25 20:22:42 +04:00
|
|
|
struct lua_longjmp; /* defined in ldo.c */
|
2001-12-05 23:15:18 +03:00
|
|
|
|
|
|
|
|
2015-12-14 14:54:49 +03:00
|
|
|
/*
|
2016-12-22 16:08:50 +03:00
|
|
|
** Atomic type (relative to signals) to better ensure that 'lua_sethook'
|
2015-12-14 14:54:49 +03:00
|
|
|
** is thread safe
|
|
|
|
*/
|
|
|
|
#if !defined(l_signalT)
|
|
|
|
#include <signal.h>
|
|
|
|
#define l_signalT sig_atomic_t
|
|
|
|
#endif
|
|
|
|
|
2002-01-26 01:14:54 +03:00
|
|
|
|
2002-08-05 21:36:24 +04:00
|
|
|
/* extra stack space to handle TM calls and some other extras */
|
2002-05-08 21:34:23 +04:00
|
|
|
#define EXTRA_STACK 5
|
2002-01-26 01:14:54 +03:00
|
|
|
|
|
|
|
|
|
|
|
#define BASIC_STACK_SIZE (2*LUA_MINSTACK)
|
|
|
|
|
1997-11-19 20:31:19 +03:00
|
|
|
|
2008-02-11 18:45:30 +03:00
|
|
|
/* kinds of Garbage Collection */
|
2017-04-24 19:59:26 +03:00
|
|
|
#define KGC_INC 0 /* incremental gc */
|
2017-02-24 00:07:34 +03:00
|
|
|
#define KGC_GEN 1 /* generational gc */
|
2008-02-11 18:45:30 +03:00
|
|
|
|
1997-11-19 20:31:19 +03:00
|
|
|
|
1999-05-10 17:54:01 +04:00
|
|
|
typedef struct stringtable {
|
2013-08-21 23:21:16 +04:00
|
|
|
TString **hash;
|
2013-08-22 19:21:48 +04:00
|
|
|
int nuse; /* number of elements */
|
|
|
|
int size;
|
1997-11-19 20:31:19 +03:00
|
|
|
} stringtable;
|
|
|
|
|
|
|
|
|
2017-11-07 16:25:26 +03:00
|
|
|
/*
|
|
|
|
** Information about a call.
|
|
|
|
*/
|
|
|
|
typedef struct CallInfo {
|
|
|
|
StkId func; /* function index in the stack */
|
|
|
|
StkId top; /* top for this function */
|
|
|
|
struct CallInfo *previous, *next; /* dynamic call link */
|
|
|
|
union {
|
|
|
|
struct { /* only for Lua functions */
|
|
|
|
const Instruction *savedpc;
|
2017-11-13 18:36:52 +03:00
|
|
|
l_signalT trap;
|
2017-11-07 16:25:26 +03:00
|
|
|
} l;
|
|
|
|
struct { /* only for C functions */
|
|
|
|
lua_KFunction k; /* continuation in case of yields */
|
|
|
|
ptrdiff_t old_errfunc;
|
|
|
|
lua_KContext ctx; /* context info. in case of yields */
|
|
|
|
} c;
|
|
|
|
} u;
|
|
|
|
union {
|
|
|
|
int funcidx; /* called-function index */
|
|
|
|
int nyield; /* number of values yielded */
|
|
|
|
} u2;
|
|
|
|
short nresults; /* expected number of results from this function */
|
2017-11-23 19:41:16 +03:00
|
|
|
lu_byte callstatus;
|
2017-11-07 16:25:26 +03:00
|
|
|
} CallInfo;
|
|
|
|
|
2002-07-16 18:26:56 +04:00
|
|
|
|
2008-08-26 17:27:42 +04:00
|
|
|
/*
|
|
|
|
** Bits in CallInfo status
|
|
|
|
*/
|
2014-06-12 23:07:30 +04:00
|
|
|
#define CIST_OAH (1<<0) /* original value of 'allowhook' */
|
2017-12-19 19:40:17 +03:00
|
|
|
#define CIST_C (1<<1) /* call is running a C function */
|
2017-11-07 16:25:26 +03:00
|
|
|
#define CIST_HOOKED (1<<2) /* call is running a debug hook */
|
2017-11-23 19:41:16 +03:00
|
|
|
#define CIST_YPCALL (1<<3) /* call is a yieldable protected call */
|
|
|
|
#define CIST_TAIL (1<<4) /* call was tail called */
|
|
|
|
#define CIST_HOOKYIELD (1<<5) /* last hook called yielded */
|
|
|
|
#define CIST_LEQ (1<<6) /* using __lt for __le */
|
|
|
|
#define CIST_FIN (1<<7) /* call is running a finalizer */
|
2002-07-16 18:26:56 +04:00
|
|
|
|
2017-12-19 19:40:17 +03:00
|
|
|
/* active function is a Lua function */
|
|
|
|
#define isLua(ci) (!((ci)->callstatus & CIST_C))
|
|
|
|
|
|
|
|
/* call is running Lua code (not a hook) */
|
|
|
|
#define isLuacode(ci) (!((ci)->callstatus & (CIST_C | CIST_HOOKED)))
|
2001-12-18 23:52:30 +03:00
|
|
|
|
2014-06-12 23:07:30 +04:00
|
|
|
/* assume that CIST_OAH has offset 0 and that 'v' is strictly 0/1 */
|
|
|
|
#define setoah(st,v) ((st) = ((st) & ~CIST_OAH) | (v))
|
|
|
|
#define getoah(st) ((st) & CIST_OAH)
|
|
|
|
|
2001-12-18 23:52:30 +03:00
|
|
|
|
2001-01-19 16:20:30 +03:00
|
|
|
/*
|
2014-10-25 15:50:46 +04:00
|
|
|
** 'global state', shared by all threads of this state
|
2001-01-19 16:20:30 +03:00
|
|
|
*/
|
|
|
|
typedef struct global_State {
|
2005-02-23 20:30:22 +03:00
|
|
|
lua_Alloc frealloc; /* function to reallocate memory */
|
2014-10-25 15:50:46 +04:00
|
|
|
void *ud; /* auxiliary data to 'frealloc' */
|
2015-07-04 19:33:17 +03:00
|
|
|
l_mem totalbytes; /* number of bytes currently allocated - GCdebt */
|
2010-12-20 22:40:07 +03:00
|
|
|
l_mem GCdebt; /* bytes allocated not yet compensated by the collector */
|
2012-05-22 21:50:39 +04:00
|
|
|
lu_mem GCestimate; /* an estimate of the non-garbage memory in use */
|
2010-03-25 22:37:23 +03:00
|
|
|
stringtable strt; /* hash table for strings */
|
|
|
|
TValue l_registry;
|
2012-02-02 01:57:15 +04:00
|
|
|
unsigned int seed; /* randomized seed for hashes */
|
2004-08-25 00:12:06 +04:00
|
|
|
lu_byte currentwhite;
|
|
|
|
lu_byte gcstate; /* state of garbage collector */
|
2008-02-11 18:45:30 +03:00
|
|
|
lu_byte gckind; /* kind of GC running */
|
2017-04-19 20:02:50 +03:00
|
|
|
lu_byte genminormul; /* control for minor generational collections */
|
|
|
|
lu_byte genmajormul; /* control for major generational collections */
|
2010-12-20 21:17:46 +03:00
|
|
|
lu_byte gcrunning; /* true if GC is running */
|
2017-04-24 19:59:26 +03:00
|
|
|
lu_byte gcemergency; /* true if this is an emergency collection */
|
2017-05-26 22:14:29 +03:00
|
|
|
lu_byte gcpause; /* size of pause between successive GCs */
|
|
|
|
lu_byte gcstepmul; /* GC "speed" */
|
|
|
|
lu_byte gcstepsize; /* (log2 of) GC granularity */
|
2010-03-24 16:07:01 +03:00
|
|
|
GCObject *allgc; /* list of all collectable objects */
|
2013-08-30 23:14:26 +04:00
|
|
|
GCObject **sweepgc; /* current position of sweep in list */
|
2013-09-03 19:37:10 +04:00
|
|
|
GCObject *finobj; /* list of collectable objects with finalizers */
|
2003-11-18 17:55:11 +03:00
|
|
|
GCObject *gray; /* list of gray objects */
|
2003-12-04 21:52:23 +03:00
|
|
|
GCObject *grayagain; /* list of objects to be traversed atomically */
|
2008-02-19 21:55:09 +03:00
|
|
|
GCObject *weak; /* list of tables with weak values */
|
|
|
|
GCObject *ephemeron; /* list of ephemeron tables (weak keys) */
|
2007-10-31 18:41:19 +03:00
|
|
|
GCObject *allweak; /* list of all-weak tables */
|
2017-05-04 16:32:01 +03:00
|
|
|
GCObject *protogray; /* list of prototypes with "new" caches */
|
2008-06-26 23:42:45 +04:00
|
|
|
GCObject *tobefnz; /* list of userdata to be GC */
|
2013-08-22 00:09:51 +04:00
|
|
|
GCObject *fixedgc; /* list of objects not to be collected */
|
2017-02-24 00:07:34 +03:00
|
|
|
/* fields for generational collector */
|
|
|
|
GCObject *survival; /* start of objects that survived one GC cycle */
|
2017-04-05 19:50:51 +03:00
|
|
|
GCObject *old; /* start of old objects */
|
|
|
|
GCObject *reallyold; /* old objects with more than one cycle */
|
|
|
|
GCObject *finobjsur; /* list of survival objects with finalizers */
|
|
|
|
GCObject *finobjold; /* list of old objects with finalizers */
|
|
|
|
GCObject *finobjrold; /* list of really old objects with finalizers */
|
2014-02-18 17:39:37 +04:00
|
|
|
struct lua_State *twups; /* list of threads with open upvalues */
|
2002-04-16 21:08:28 +04:00
|
|
|
lua_CFunction panic; /* to be called in unprotected errors */
|
2002-10-26 00:05:28 +04:00
|
|
|
struct lua_State *mainthread;
|
2009-06-18 22:59:18 +04:00
|
|
|
const lua_Number *version; /* pointer to version number */
|
2017-07-27 16:50:16 +03:00
|
|
|
TString *nfield; /* string "n" (key in vararg tables) */
|
2010-03-22 21:28:03 +03:00
|
|
|
TString *tmname[TM_N]; /* array with tag-method names */
|
2010-04-12 20:07:29 +04:00
|
|
|
struct Table *mt[LUA_NUMTAGS]; /* metatables for basic types */
|
2015-09-22 17:18:24 +03:00
|
|
|
TString *strcache[STRCACHE_N][STRCACHE_M]; /* cache for strings in API */
|
2001-01-19 16:20:30 +03:00
|
|
|
} global_State;
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
2014-10-25 15:50:46 +04:00
|
|
|
** 'per thread' state
|
2001-01-19 16:20:30 +03:00
|
|
|
*/
|
|
|
|
struct lua_State {
|
2002-10-26 00:05:28 +04:00
|
|
|
CommonHeader;
|
2015-11-02 19:01:41 +03:00
|
|
|
unsigned short nci; /* number of items in 'ci' list */
|
2006-02-06 21:27:59 +03:00
|
|
|
lu_byte status;
|
2001-01-19 16:20:30 +03:00
|
|
|
StkId top; /* first free slot in the stack */
|
2005-07-09 17:22:34 +04:00
|
|
|
global_State *l_G;
|
2017-11-07 16:25:26 +03:00
|
|
|
CallInfo *ci; /* call info for current function */
|
2006-09-19 17:57:50 +04:00
|
|
|
const Instruction *oldpc; /* last pc traced */
|
2001-01-19 16:20:30 +03:00
|
|
|
StkId stack_last; /* last free slot in the stack */
|
2001-03-07 21:09:25 +03:00
|
|
|
StkId stack; /* stack base */
|
2013-08-27 22:53:35 +04:00
|
|
|
UpVal *openupval; /* list of open upvalues in this stack */
|
2002-11-13 14:32:26 +03:00
|
|
|
GCObject *gclist;
|
2014-02-18 17:39:37 +04:00
|
|
|
struct lua_State *twups; /* list of threads with open upvalues */
|
2002-08-05 21:36:24 +04:00
|
|
|
struct lua_longjmp *errorJmp; /* current error recover point */
|
2017-11-07 16:25:26 +03:00
|
|
|
CallInfo base_ci; /* CallInfo for first level (C calling Lua) */
|
2015-12-16 19:39:38 +03:00
|
|
|
volatile lua_Hook hook;
|
2014-07-23 21:15:43 +04:00
|
|
|
ptrdiff_t errfunc; /* current error handling function (stack index) */
|
|
|
|
int stacksize;
|
|
|
|
int basehookcount;
|
|
|
|
int hookcount;
|
|
|
|
unsigned short nny; /* number of non-yieldable calls in stack */
|
|
|
|
unsigned short nCcalls; /* number of nested C calls */
|
2015-12-14 14:54:49 +03:00
|
|
|
l_signalT hookmask;
|
2014-07-23 21:15:43 +04:00
|
|
|
lu_byte allowhook;
|
1998-06-03 00:37:04 +04:00
|
|
|
};
|
1997-11-19 20:31:19 +03:00
|
|
|
|
|
|
|
|
2005-07-09 17:22:34 +04:00
|
|
|
#define G(L) (L->l_G)
|
2001-01-19 16:20:30 +03:00
|
|
|
|
|
|
|
|
2002-10-26 00:05:28 +04:00
|
|
|
/*
|
2014-07-18 16:17:54 +04:00
|
|
|
** Union of all collectable objects (only for conversions)
|
2002-10-26 00:05:28 +04:00
|
|
|
*/
|
2014-07-17 21:09:50 +04:00
|
|
|
union GCUnion {
|
|
|
|
GCObject gc; /* common header */
|
2014-07-18 17:36:14 +04:00
|
|
|
struct TString ts;
|
2014-07-18 18:46:47 +04:00
|
|
|
struct Udata u;
|
2002-10-26 00:05:28 +04:00
|
|
|
union Closure cl;
|
|
|
|
struct Table h;
|
|
|
|
struct Proto p;
|
|
|
|
struct lua_State th; /* thread */
|
2017-04-11 21:41:09 +03:00
|
|
|
struct UpVal upv;
|
2002-10-26 00:05:28 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2014-07-17 21:09:50 +04:00
|
|
|
#define cast_u(o) cast(union GCUnion *, (o))
|
|
|
|
|
2002-11-13 14:32:26 +03:00
|
|
|
/* macros to convert a GCObject into a specific value */
|
2014-07-18 17:36:14 +04:00
|
|
|
#define gco2ts(o) \
|
2014-07-17 21:27:49 +04:00
|
|
|
check_exp(novariant((o)->tt) == LUA_TSTRING, &((cast_u(o))->ts))
|
2014-07-18 18:46:47 +04:00
|
|
|
#define gco2u(o) check_exp((o)->tt == LUA_TUSERDATA, &((cast_u(o))->u))
|
2014-07-17 21:27:49 +04:00
|
|
|
#define gco2lcl(o) check_exp((o)->tt == LUA_TLCL, &((cast_u(o))->cl.l))
|
|
|
|
#define gco2ccl(o) check_exp((o)->tt == LUA_TCCL, &((cast_u(o))->cl.c))
|
2012-01-21 02:05:50 +04:00
|
|
|
#define gco2cl(o) \
|
2014-07-17 21:27:49 +04:00
|
|
|
check_exp(novariant((o)->tt) == LUA_TFUNCTION, &((cast_u(o))->cl))
|
|
|
|
#define gco2t(o) check_exp((o)->tt == LUA_TTABLE, &((cast_u(o))->h))
|
|
|
|
#define gco2p(o) check_exp((o)->tt == LUA_TPROTO, &((cast_u(o))->p))
|
|
|
|
#define gco2th(o) check_exp((o)->tt == LUA_TTHREAD, &((cast_u(o))->th))
|
2017-04-11 21:41:09 +03:00
|
|
|
#define gco2upv(o) check_exp((o)->tt == LUA_TUPVAL, &((cast_u(o))->upv))
|
2014-07-17 21:09:50 +04:00
|
|
|
|
2002-11-13 14:32:26 +03:00
|
|
|
|
2014-07-18 16:17:54 +04:00
|
|
|
/* macro to convert a Lua object into a GCObject */
|
2017-06-12 17:21:44 +03:00
|
|
|
#define obj2gco(v) (&(cast_u(v)->gc))
|
2014-07-18 16:17:54 +04:00
|
|
|
|
2002-11-13 14:32:26 +03:00
|
|
|
|
2010-12-20 22:40:07 +03:00
|
|
|
/* actual number of total bytes allocated */
|
2015-07-04 19:33:17 +03:00
|
|
|
#define gettotalbytes(g) cast(lu_mem, (g)->totalbytes + (g)->GCdebt)
|
2010-12-20 22:40:07 +03:00
|
|
|
|
|
|
|
LUAI_FUNC void luaE_setdebt (global_State *g, l_mem debt);
|
2005-04-25 23:24:10 +04:00
|
|
|
LUAI_FUNC void luaE_freethread (lua_State *L, lua_State *L1);
|
2017-11-07 16:25:26 +03:00
|
|
|
LUAI_FUNC CallInfo *luaE_extendCI (lua_State *L);
|
|
|
|
LUAI_FUNC void luaE_freeCI (lua_State *L);
|
|
|
|
LUAI_FUNC void luaE_shrinkCI (lua_State *L);
|
2017-11-23 19:41:16 +03:00
|
|
|
LUAI_FUNC void luaE_incCcalls (lua_State *L);
|
2009-04-17 18:28:06 +04:00
|
|
|
|
2002-01-11 23:26:52 +03:00
|
|
|
|
1997-11-19 20:31:19 +03:00
|
|
|
#endif
|
1999-02-04 20:47:59 +03:00
|
|
|
|