Nut/OS  4.10.3
API Reference
lstate.h
Go to the documentation of this file.
00001 /*
00002 ** $Id: lstate.h 2831 2009-12-08 14:19:44Z haraldkipp $
00003 ** Global State
00004 ** See Copyright Notice in lua.h
00005 */
00006 
00007 #ifndef lstate_h
00008 #define lstate_h
00009 
00010 #include <lua/lua.h>
00011 
00012 #include <lua/lobject.h>
00013 #include <lua/ltm.h>
00014 #include <lua/lzio.h>
00015 
00016 
00017 
00018 struct lua_longjmp;  /* defined in ldo.c */
00019 
00020 
00021 /* table of globals */
00022 #define gt(L)   (&L->l_gt)
00023 
00024 /* registry */
00025 #define registry(L)     (&G(L)->l_registry)
00026 
00027 
00028 /* extra stack space to handle TM calls and some other extras */
00029 #define EXTRA_STACK   5
00030 
00031 
00032 #define BASIC_CI_SIZE           8
00033 
00034 #define BASIC_STACK_SIZE        (2*LUA_MINSTACK)
00035 
00036 
00037 
00038 typedef struct stringtable {
00039   GCObject **hash;
00040   lu_int32 nuse;  /* number of elements */
00041   int size;
00042 } stringtable;
00043 
00044 
00045 /*
00046 ** informations about a call
00047 */
00048 typedef struct CallInfo {
00049   StkId base;  /* base for this function */
00050   StkId func;  /* function index in the stack */
00051   StkId top;  /* top for this function */
00052   const Instruction *savedpc;
00053   int nresults;  /* expected number of results from this function */
00054   int tailcalls;  /* number of tail calls lost under this entry */
00055 } CallInfo;
00056 
00057 
00058 
00059 #define curr_func(L)    (ttisfunction(L->ci->func) ? clvalue(L->ci->func) : NULL)
00060 #define ci_func(ci)     (ttisfunction((ci)->func) ? clvalue((ci)->func) : NULL)
00061 #define f_isLua(ci)     (!ttislightfunction((ci)->func) && !ci_func(ci)->c.isC)
00062 #define isLua(ci)       (ttisfunction((ci)->func) && f_isLua(ci))
00063 
00064 
00065 /*
00066 ** `global state', shared by all threads of this state
00067 */
00068 typedef struct global_State {
00069   stringtable strt;  /* hash table for strings */
00070   lua_Alloc frealloc;  /* function to reallocate memory */
00071   void *ud;         /* auxiliary data to `frealloc' */
00072   lu_byte currentwhite;
00073   lu_byte gcstate;  /* state of garbage collector */
00074   int sweepstrgc;  /* position of sweep in `strt' */
00075   GCObject *rootgc;  /* list of all collectable objects */
00076   GCObject **sweepgc;  /* position of sweep in `rootgc' */
00077   GCObject *gray;  /* list of gray objects */
00078   GCObject *grayagain;  /* list of objects to be traversed atomically */
00079   GCObject *weak;  /* list of weak tables (to be cleared) */
00080   GCObject *tmudata;  /* last element of list of userdata to be GC */
00081   Mbuffer buff;  /* temporary buffer for string concatentation */
00082   lu_mem GCthreshold;
00083   lu_mem totalbytes;  /* number of bytes currently allocated */
00084   lu_mem estimate;  /* an estimate of number of bytes actually in use */
00085   lu_mem gcdept;  /* how much GC is `behind schedule' */
00086   int gcpause;  /* size of pause between successive GCs */
00087   int gcstepmul;  /* GC `granularity' */
00088   lua_CFunction panic;  /* to be called in unprotected errors */
00089   TValue l_registry;
00090   struct lua_State *mainthread;
00091   UpVal uvhead;  /* head of double-linked list of all open upvalues */
00092   struct Table *mt[NUM_TAGS];  /* metatables for basic types */
00093   TString *tmname[TM_N];  /* array with tag-method names */
00094 } global_State;
00095 
00096 
00097 /*
00098 ** `per thread' state
00099 */
00100 struct lua_State {
00101   CommonHeader;
00102   lu_byte status;
00103   StkId top;  /* first free slot in the stack */
00104   StkId base;  /* base of current function */
00105   global_State *l_G;
00106   CallInfo *ci;  /* call info for current function */
00107   const Instruction *savedpc;  /* `savedpc' of current function */
00108   StkId stack_last;  /* last free slot in the stack */
00109   StkId stack;  /* stack base */
00110   CallInfo *end_ci;  /* points after end of ci array*/
00111   CallInfo *base_ci;  /* array of CallInfo's */
00112   int stacksize;
00113   int size_ci;  /* size of array `base_ci' */
00114   unsigned short nCcalls;  /* number of nested C calls */
00115   unsigned short baseCcalls;  /* nested C calls when resuming coroutine */
00116   lu_byte hookmask;
00117   lu_byte allowhook;
00118   int basehookcount;
00119   int hookcount;
00120   lua_Hook hook;
00121   TValue l_gt;  /* table of globals */
00122   TValue env;  /* temporary place for environments */
00123   GCObject *openupval;  /* list of open upvalues in this stack */
00124   GCObject *gclist;
00125   struct lua_longjmp *errorJmp;  /* current error recover point */
00126   ptrdiff_t errfunc;  /* current error handling function (stack index) */
00127 };
00128 
00129 
00130 #define G(L)    (L->l_G)
00131 
00132 
00133 /*
00134 ** Union of all collectable objects
00135 */
00136 union GCObject {
00137   GCheader gch;
00138   union TString ts;
00139   union Udata u;
00140   union Closure cl;
00141   struct Table h;
00142   struct Proto p;
00143   struct UpVal uv;
00144   struct lua_State th;  /* thread */
00145 };
00146 
00147 
00148 /* macros to convert a GCObject into a specific value */
00149 #define rawgco2ts(o)    check_exp((o)->gch.tt == LUA_TSTRING, &((o)->ts))
00150 #define gco2ts(o)       (&rawgco2ts(o)->tsv)
00151 #define rawgco2u(o)     check_exp((o)->gch.tt == LUA_TUSERDATA, &((o)->u))
00152 #define gco2u(o)        (&rawgco2u(o)->uv)
00153 #define gco2cl(o)       check_exp((o)->gch.tt == LUA_TFUNCTION, &((o)->cl))
00154 #define gco2h(o)        check_exp((o)->gch.tt == LUA_TTABLE, &((o)->h))
00155 #define gco2p(o)        check_exp((o)->gch.tt == LUA_TPROTO, &((o)->p))
00156 #define gco2uv(o)       check_exp((o)->gch.tt == LUA_TUPVAL, &((o)->uv))
00157 #define ngcotouv(o) \
00158         check_exp((o) == NULL || (o)->gch.tt == LUA_TUPVAL, &((o)->uv))
00159 #define gco2th(o)       check_exp((o)->gch.tt == LUA_TTHREAD, &((o)->th))
00160 
00161 /* macro to convert any Lua object into a GCObject */
00162 #define obj2gco(v)      (cast(GCObject *, (v)))
00163 
00164 
00165 LUAI_FUNC lua_State *luaE_newthread (lua_State *L);
00166 LUAI_FUNC void luaE_freethread (lua_State *L, lua_State *L1);
00167 
00168 #endif
00169