new 4475edb243ed4627f4c5f2c470ca40b3def034d4
[tagua/yd.git] / lua / src / lstate.h
blobd296a4cab99e1cb1ec5f09bc0cd1bfc74e1cd5cc
1 /*
2 ** $Id: lstate.h,v 2.24 2006/02/06 18:27:59 roberto Exp $
3 ** Global State
4 ** See Copyright Notice in lua.h
5 */
7 #ifndef lstate_h
8 #define lstate_h
10 #include "lua.h"
12 #include "lobject.h"
13 #include "ltm.h"
14 #include "lzio.h"
18 struct lua_longjmp; /* defined in ldo.c */
21 /* table of globals */
22 #define gt(L) (&L->l_gt)
24 /* registry */
25 #define registry(L) (&G(L)->l_registry)
28 /* extra stack space to handle TM calls and some other extras */
29 #define EXTRA_STACK 5
32 #define BASIC_CI_SIZE 8
34 #define BASIC_STACK_SIZE (2*LUA_MINSTACK)
38 typedef struct stringtable {
39 GCObject **hash;
40 lu_int32 nuse; /* number of elements */
41 int size;
42 } stringtable;
46 ** informations about a call
48 typedef struct CallInfo {
49 StkId base; /* base for this function */
50 StkId func; /* function index in the stack */
51 StkId top; /* top for this function */
52 const Instruction *savedpc;
53 int nresults; /* expected number of results from this function */
54 int tailcalls; /* number of tail calls lost under this entry */
55 } CallInfo;
59 #define curr_func(L) (clvalue(L->ci->func))
60 #define ci_func(ci) (clvalue((ci)->func))
61 #define f_isLua(ci) (!ci_func(ci)->c.isC)
62 #define isLua(ci) (ttisfunction((ci)->func) && f_isLua(ci))
66 ** `global state', shared by all threads of this state
68 typedef struct global_State {
69 stringtable strt; /* hash table for strings */
70 lua_Alloc frealloc; /* function to reallocate memory */
71 void *ud; /* auxiliary data to `frealloc' */
72 lu_byte currentwhite;
73 lu_byte gcstate; /* state of garbage collector */
74 int sweepstrgc; /* position of sweep in `strt' */
75 GCObject *rootgc; /* list of all collectable objects */
76 GCObject **sweepgc; /* position of sweep in `rootgc' */
77 GCObject *gray; /* list of gray objects */
78 GCObject *grayagain; /* list of objects to be traversed atomically */
79 GCObject *weak; /* list of weak tables (to be cleared) */
80 GCObject *tmudata; /* last element of list of userdata to be GC */
81 Mbuffer buff; /* temporary buffer for string concatentation */
82 lu_mem GCthreshold;
83 lu_mem totalbytes; /* number of bytes currently allocated */
84 lu_mem estimate; /* an estimate of number of bytes actually in use */
85 lu_mem gcdept; /* how much GC is `behind schedule' */
86 int gcpause; /* size of pause between successive GCs */
87 int gcstepmul; /* GC `granularity' */
88 lua_CFunction panic; /* to be called in unprotected errors */
89 TValue l_registry;
90 struct lua_State *mainthread;
91 UpVal uvhead; /* head of double-linked list of all open upvalues */
92 struct Table *mt[NUM_TAGS]; /* metatables for basic types */
93 TString *tmname[TM_N]; /* array with tag-method names */
94 } global_State;
98 ** `per thread' state
100 struct lua_State {
101 CommonHeader;
102 lu_byte status;
103 StkId top; /* first free slot in the stack */
104 StkId base; /* base of current function */
105 global_State *l_G;
106 CallInfo *ci; /* call info for current function */
107 const Instruction *savedpc; /* `savedpc' of current function */
108 StkId stack_last; /* last free slot in the stack */
109 StkId stack; /* stack base */
110 CallInfo *end_ci; /* points after end of ci array*/
111 CallInfo *base_ci; /* array of CallInfo's */
112 int stacksize;
113 int size_ci; /* size of array `base_ci' */
114 unsigned short nCcalls; /* number of nested C calls */
115 lu_byte hookmask;
116 lu_byte allowhook;
117 int basehookcount;
118 int hookcount;
119 lua_Hook hook;
120 TValue l_gt; /* table of globals */
121 TValue env; /* temporary place for environments */
122 GCObject *openupval; /* list of open upvalues in this stack */
123 GCObject *gclist;
124 struct lua_longjmp *errorJmp; /* current error recover point */
125 ptrdiff_t errfunc; /* current error handling function (stack index) */
129 #define G(L) (L->l_G)
133 ** Union of all collectable objects
135 union GCObject {
136 GCheader gch;
137 union TString ts;
138 union Udata u;
139 union Closure cl;
140 struct Table h;
141 struct Proto p;
142 struct UpVal uv;
143 struct lua_State th; /* thread */
147 /* macros to convert a GCObject into a specific value */
148 #define rawgco2ts(o) check_exp((o)->gch.tt == LUA_TSTRING, &((o)->ts))
149 #define gco2ts(o) (&rawgco2ts(o)->tsv)
150 #define rawgco2u(o) check_exp((o)->gch.tt == LUA_TUSERDATA, &((o)->u))
151 #define gco2u(o) (&rawgco2u(o)->uv)
152 #define gco2cl(o) check_exp((o)->gch.tt == LUA_TFUNCTION, &((o)->cl))
153 #define gco2h(o) check_exp((o)->gch.tt == LUA_TTABLE, &((o)->h))
154 #define gco2p(o) check_exp((o)->gch.tt == LUA_TPROTO, &((o)->p))
155 #define gco2uv(o) check_exp((o)->gch.tt == LUA_TUPVAL, &((o)->uv))
156 #define ngcotouv(o) \
157 check_exp((o) == NULL || (o)->gch.tt == LUA_TUPVAL, &((o)->uv))
158 #define gco2th(o) check_exp((o)->gch.tt == LUA_TTHREAD, &((o)->th))
160 /* macro to convert any Lua object into a GCObject */
161 #define obj2gco(v) (cast(GCObject *, (v)))
164 LUAI_FUNC lua_State *luaE_newthread (lua_State *L);
165 LUAI_FUNC void luaE_freethread (lua_State *L, lua_State *L1);
167 #endif