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