xref: /minix/external/mit/lua/dist/src/lstate.h (revision 0a6a1f1d)
1 /*	$NetBSD: lstate.h,v 1.4 2015/10/08 13:21:00 mbalmer Exp $	*/
2 
3 /*
4 ** Id: lstate.h,v 2.122 2015/06/01 16:34:37 roberto Exp
5 ** Global State
6 ** See Copyright Notice in lua.h
7 */
8 
9 #ifndef lstate_h
10 #define lstate_h
11 
12 #include "lua.h"
13 
14 #include "lobject.h"
15 #include "ltm.h"
16 #include "lzio.h"
17 
18 
19 /*
20 
21 ** Some notes about garbage-collected objects: All objects in Lua must
22 ** be kept somehow accessible until being freed, so all objects always
23 ** belong to one (and only one) of these lists, using field 'next' of
24 ** the 'CommonHeader' for the link:
25 **
26 ** 'allgc': all objects not marked for finalization;
27 ** 'finobj': all objects marked for finalization;
28 ** 'tobefnz': all objects ready to be finalized;
29 ** 'fixedgc': all objects that are not to be collected (currently
30 ** only small strings, such as reserved words).
31 
32 */
33 
34 
35 struct lua_longjmp;  /* defined in ldo.c */
36 
37 
38 
39 /* extra stack space to handle TM calls and some other extras */
40 #define EXTRA_STACK   5
41 
42 
43 #define BASIC_STACK_SIZE        (2*LUA_MINSTACK)
44 
45 
46 /* kinds of Garbage Collection */
47 #define KGC_NORMAL	0
48 #define KGC_EMERGENCY	1	/* gc was forced by an allocation failure */
49 
50 
51 typedef struct stringtable {
52   TString **hash;
53   int nuse;  /* number of elements */
54   int size;
55 } stringtable;
56 
57 
58 /*
59 ** Information about a call.
60 ** When a thread yields, 'func' is adjusted to pretend that the
61 ** top function has only the yielded values in its stack; in that
62 ** case, the actual 'func' value is saved in field 'extra'.
63 ** When a function calls another with a continuation, 'extra' keeps
64 ** the function index so that, in case of errors, the continuation
65 ** function can be called with the correct top.
66 */
67 typedef struct CallInfo {
68   StkId func;  /* function index in the stack */
69   StkId	top;  /* top for this function */
70   struct CallInfo *previous, *next;  /* dynamic call link */
71   union {
72     struct {  /* only for Lua functions */
73       StkId base;  /* base for this function */
74       const Instruction *savedpc;
75     } l;
76     struct {  /* only for C functions */
77       lua_KFunction k;  /* continuation in case of yields */
78       ptrdiff_t old_errfunc;
79       lua_KContext ctx;  /* context info. in case of yields */
80     } c;
81   } u;
82   ptrdiff_t extra;
83   short nresults;  /* expected number of results from this function */
84   lu_byte callstatus;
85 } CallInfo;
86 
87 
88 /*
89 ** Bits in CallInfo status
90 */
91 #define CIST_OAH	(1<<0)	/* original value of 'allowhook' */
92 #define CIST_LUA	(1<<1)	/* call is running a Lua function */
93 #define CIST_HOOKED	(1<<2)	/* call is running a debug hook */
94 #define CIST_REENTRY	(1<<3)	/* call is running on same invocation of
95                                    luaV_execute of previous call */
96 #define CIST_YPCALL	(1<<4)	/* call is a yieldable protected call */
97 #define CIST_TAIL	(1<<5)	/* call was tail called */
98 #define CIST_HOOKYIELD	(1<<6)	/* last hook called yielded */
99 #define CIST_LEQ	(1<<7)  /* using __lt for __le */
100 
101 #define isLua(ci)	((ci)->callstatus & CIST_LUA)
102 
103 /* assume that CIST_OAH has offset 0 and that 'v' is strictly 0/1 */
104 #define setoah(st,v)	((st) = ((st) & ~CIST_OAH) | (v))
105 #define getoah(st)	((st) & CIST_OAH)
106 
107 
108 /*
109 ** 'global state', shared by all threads of this state
110 */
111 typedef struct global_State {
112   lua_Alloc frealloc;  /* function to reallocate memory */
113   void *ud;         /* auxiliary data to 'frealloc' */
114   lu_mem totalbytes;  /* number of bytes currently allocated - GCdebt */
115   l_mem GCdebt;  /* bytes allocated not yet compensated by the collector */
116   lu_mem GCmemtrav;  /* memory traversed by the GC */
117   lu_mem GCestimate;  /* an estimate of the non-garbage memory in use */
118   stringtable strt;  /* hash table for strings */
119   TValue l_registry;
120   unsigned int seed;  /* randomized seed for hashes */
121   lu_byte currentwhite;
122   lu_byte gcstate;  /* state of garbage collector */
123   lu_byte gckind;  /* kind of GC running */
124   lu_byte gcrunning;  /* true if GC is running */
125   GCObject *allgc;  /* list of all collectable objects */
126   GCObject **sweepgc;  /* current position of sweep in list */
127   GCObject *finobj;  /* list of collectable objects with finalizers */
128   GCObject *gray;  /* list of gray objects */
129   GCObject *grayagain;  /* list of objects to be traversed atomically */
130   GCObject *weak;  /* list of tables with weak values */
131   GCObject *ephemeron;  /* list of ephemeron tables (weak keys) */
132   GCObject *allweak;  /* list of all-weak tables */
133   GCObject *tobefnz;  /* list of userdata to be GC */
134   GCObject *fixedgc;  /* list of objects not to be collected */
135   struct lua_State *twups;  /* list of threads with open upvalues */
136   Mbuffer buff;  /* temporary buffer for string concatenation */
137   unsigned int gcfinnum;  /* number of finalizers to call in each GC step */
138   int gcpause;  /* size of pause between successive GCs */
139   int gcstepmul;  /* GC 'granularity' */
140   lua_CFunction panic;  /* to be called in unprotected errors */
141   struct lua_State *mainthread;
142   const lua_Number *version;  /* pointer to version number */
143   TString *memerrmsg;  /* memory-error message */
144   TString *tmname[TM_N];  /* array with tag-method names */
145   struct Table *mt[LUA_NUMTAGS];  /* metatables for basic types */
146   TString *strcache[STRCACHE_SIZE][1];  /* cache for strings in API */
147 } global_State;
148 
149 
150 /*
151 ** 'per thread' state
152 */
153 struct lua_State {
154   CommonHeader;
155   lu_byte status;
156   StkId top;  /* first free slot in the stack */
157   global_State *l_G;
158   CallInfo *ci;  /* call info for current function */
159   const Instruction *oldpc;  /* last pc traced */
160   StkId stack_last;  /* last free slot in the stack */
161   StkId stack;  /* stack base */
162   UpVal *openupval;  /* list of open upvalues in this stack */
163   GCObject *gclist;
164   struct lua_State *twups;  /* list of threads with open upvalues */
165   struct lua_longjmp *errorJmp;  /* current error recover point */
166   CallInfo base_ci;  /* CallInfo for first level (C calling Lua) */
167   lua_Hook hook;
168   ptrdiff_t errfunc;  /* current error handling function (stack index) */
169   int stacksize;
170   int basehookcount;
171   int hookcount;
172   unsigned short nny;  /* number of non-yieldable calls in stack */
173   unsigned short nCcalls;  /* number of nested C calls */
174   lu_byte hookmask;
175   lu_byte allowhook;
176 };
177 
178 
179 #define G(L)	(L->l_G)
180 
181 
182 /*
183 ** Union of all collectable objects (only for conversions)
184 */
185 union GCUnion {
186   GCObject gc;  /* common header */
187   struct TString ts;
188   struct Udata u;
189   union Closure cl;
190   struct Table h;
191   struct Proto p;
192   struct lua_State th;  /* thread */
193 };
194 
195 
196 #define cast_u(o)	cast(union GCUnion *, (o))
197 
198 /* macros to convert a GCObject into a specific value */
199 #define gco2ts(o)  \
200 	check_exp(novariant((o)->tt) == LUA_TSTRING, &((cast_u(o))->ts))
201 #define gco2u(o)  check_exp((o)->tt == LUA_TUSERDATA, &((cast_u(o))->u))
202 #define gco2lcl(o)  check_exp((o)->tt == LUA_TLCL, &((cast_u(o))->cl.l))
203 #define gco2ccl(o)  check_exp((o)->tt == LUA_TCCL, &((cast_u(o))->cl.c))
204 #define gco2cl(o)  \
205 	check_exp(novariant((o)->tt) == LUA_TFUNCTION, &((cast_u(o))->cl))
206 #define gco2t(o)  check_exp((o)->tt == LUA_TTABLE, &((cast_u(o))->h))
207 #define gco2p(o)  check_exp((o)->tt == LUA_TPROTO, &((cast_u(o))->p))
208 #define gco2th(o)  check_exp((o)->tt == LUA_TTHREAD, &((cast_u(o))->th))
209 
210 
211 /* macro to convert a Lua object into a GCObject */
212 #define obj2gco(v) \
213 	check_exp(novariant((v)->tt) < LUA_TDEADKEY, (&(cast_u(v)->gc)))
214 
215 
216 /* actual number of total bytes allocated */
217 #define gettotalbytes(g)	((g)->totalbytes + (g)->GCdebt)
218 
219 LUAI_FUNC void luaE_setdebt (global_State *g, l_mem debt);
220 LUAI_FUNC void luaE_freethread (lua_State *L, lua_State *L1);
221 LUAI_FUNC CallInfo *luaE_extendCI (lua_State *L);
222 LUAI_FUNC void luaE_freeCI (lua_State *L);
223 LUAI_FUNC void luaE_shrinkCI (lua_State *L);
224 
225 
226 #endif
227 
228