rpm  4.5
lstate.h
Go to the documentation of this file.
1 /*
2 ** $Id: lstate.h,v 1.1 2004/03/16 21:58:30 niemeyer 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 /*
18 ** macros for thread synchronization inside Lua core machine:
19 ** all accesses to the global state and to global objects are synchronized.
20 ** Because threads can read the stack of other threads
21 ** (when running garbage collection),
22 ** a thread must also synchronize any write-access to its own stack.
23 ** Unsynchronized accesses are allowed only when reading its own stack,
24 ** or when reading immutable fields from global objects
25 ** (such as string values and udata values).
26 */
27 #ifndef lua_lock
28 #define lua_lock(L) ((void) 0)
29 #endif
30 
31 #ifndef lua_unlock
32 #define lua_unlock(L) ((void) 0)
33 #endif
34 
35 
36 #ifndef lua_userstateopen
37 #define lua_userstateopen(l)
38 #endif
39 
40 
41 
42 struct lua_longjmp; /* defined in ldo.c */
43 
44 
45 /* default meta table (both for tables and udata) */
46 #define defaultmeta(L) (&G(L)->_defaultmeta)
47 
48 /* table of globals */
49 #define gt(L) (&L->_gt)
50 
51 /* registry */
52 #define registry(L) (&G(L)->_registry)
53 
54 
55 /* extra stack space to handle TM calls and some other extras */
56 #define EXTRA_STACK 5
57 
58 
59 #define BASIC_CI_SIZE 8
60 
61 #define BASIC_STACK_SIZE (2*LUA_MINSTACK)
62 
63 
64 
65 typedef struct stringtable {
66 /*@null@*/
68  ls_nstr nuse; /* number of elements */
69  int size;
70 } stringtable;
71 
72 
73 /*
74 ** informations about a call
75 */
76 typedef struct CallInfo {
77 /*@dependent@*/ /*@relnull@*/
78  StkId base; /* base for called function */
79 /*@dependent@*/ /*@relnull@*/
80  StkId top; /* top for this function */
81  int state; /* bit fields; see below */
82  union {
83  struct { /* for Lua functions */
84 /*@observer@*/
86 /*@observer@*/
87  const Instruction **pc; /* points to `pc' variable in `luaV_execute' */
88  int tailcalls; /* number of tail calls lost under this entry */
89  } l;
90  struct { /* for C functions */
91  int dummy; /* just to avoid an empty struct */
92  } c;
93  } u;
94 } CallInfo;
95 
96 
97 /*
98 ** bit fields for `CallInfo.state'
99 */
100 #define CI_C (1<<0) /* 1 if function is a C function */
101 /* 1 if (Lua) function has an active `luaV_execute' running it */
102 #define CI_HASFRAME (1<<1)
103 /* 1 if Lua function is calling another Lua function (and therefore its
104  `pc' is being used by the other, and therefore CI_SAVEDPC is 1 too) */
105 #define CI_CALLING (1<<2)
106 #define CI_SAVEDPC (1<<3) /* 1 if `savedpc' is updated */
107 #define CI_YIELD (1<<4) /* 1 if thread is suspended */
108 
109 
110 #define ci_func(ci) (clvalue((ci)->base - 1))
111 
112 
113 /*
114 ** `global state', shared by all threads of this state
115 */
116 typedef struct global_State {
117  stringtable strt; /* hash table for strings */
118 /*@owned@*/
119  GCObject *rootgc; /* list of (almost) all collectable objects */
120 /*@dependent@*/ /*@null@*/
121  GCObject *rootudata; /* (separated) list of all userdata */
122 /*@dependent@*/ /*@null@*/
123  GCObject *tmudata; /* list of userdata to be GC */
124  Mbuffer buff; /* temporary buffer for string concatentation */
126  lu_mem nblocks; /* number of `bytes' currently allocated */
127  lua_CFunction panic; /* to be called in unprotected errors */
131  Node dummynode[1]; /* common node array for all empty tables */
132  TString *tmname[TM_N]; /* array with tag-method names */
133 } global_State;
134 
135 
136 /*
137 ** `per thread' state
138 */
139 struct lua_State {
141 /*@dependent@*/ /*@relnull@*/
142  StkId top; /* first free slot in the stack */
143 /*@dependent@*/ /*@relnull@*/
144  StkId base; /* base of current function */
145 /*@relnull@*/
147 /*@dependent@*/ /*@relnull@*/
148  CallInfo *ci; /* call info for current function */
149 /*@dependent@*/
150  StkId stack_last; /* last free slot in the stack */
151 /*@owned@*/ /*@relnull@*/
152  StkId stack; /* stack base */
154 /*@dependent@*/ /*@relnull@*/
155  CallInfo *end_ci; /* points after end of ci array*/
156 /*@owned@*/ /*@relnull@*/
157  CallInfo *base_ci; /* array of CallInfo's */
158  unsigned short size_ci; /* size of array `base_ci' */
159  unsigned short nCcalls; /* number of nested C calls */
165 /*@relnull@*/
166  lua_Hook hook;
167  TObject _gt; /* table of globals */
168 /*@dependent@*/ /*@relnull@*/
169  GCObject *openupval; /* list of open upvalues in this stack */
170 /*@dependent@*/ /*@relnull@*/
172 /*@relnull@*/
173  struct lua_longjmp *errorJmp; /* current error recover point */
174  ptrdiff_t errfunc; /* current error handling function (stack index) */
175 };
176 
177 
178 #define G(L) (L->l_G)
179 
180 
181 /*
182 ** Union of all collectable objects
183 */
184 union GCObject {
186  union TString ts;
187  union Udata u;
188  union Closure cl;
189  struct Table h;
190  struct Proto p;
191  struct UpVal uv;
192  struct lua_State th; /* thread */
193 };
194 
195 
196 /* macros to convert a GCObject into a specific value */
197 #define gcotots(o) check_exp((o)->gch.tt == LUA_TSTRING, &((o)->ts))
198 #define gcotou(o) check_exp((o)->gch.tt == LUA_TUSERDATA, &((o)->u))
199 #define gcotocl(o) check_exp((o)->gch.tt == LUA_TFUNCTION, &((o)->cl))
200 #define gcotoh(o) check_exp((o)->gch.tt == LUA_TTABLE, &((o)->h))
201 #define gcotop(o) check_exp((o)->gch.tt == LUA_TPROTO, &((o)->p))
202 #define gcotouv(o) check_exp((o)->gch.tt == LUA_TUPVAL, &((o)->uv))
203 #define ngcotouv(o) \
204  check_exp((o) == NULL || (o)->gch.tt == LUA_TUPVAL, &((o)->uv))
205 #define gcototh(o) check_exp((o)->gch.tt == LUA_TTHREAD, &((o)->th))
206 
207 /* macro to convert any value into a GCObject */
208 #define valtogco(v) (cast(GCObject *, (v)))
209 
210 
211 /*@null@*/
213  /*@modifies L @*/;
214 void luaE_freethread (lua_State *L, lua_State *L1)
215  /*@modifies L, L1 @*/;
216 
217 #endif
218