misc/liblua/lstate.h
changeset 2812 0a24853de796
child 10017 de822cd3df3a
equal deleted inserted replaced
2811:4cad87e11bf6 2812:0a24853de796
       
     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 
       
    18 struct lua_longjmp;  /* defined in ldo.c */
       
    19 
       
    20 
       
    21 /* table of globals */
       
    22 #define gt(L)	(&L->l_gt)
       
    23 
       
    24 /* registry */
       
    25 #define registry(L)	(&G(L)->l_registry)
       
    26 
       
    27 
       
    28 /* extra stack space to handle TM calls and some other extras */
       
    29 #define EXTRA_STACK   5
       
    30 
       
    31 
       
    32 #define BASIC_CI_SIZE           8
       
    33 
       
    34 #define BASIC_STACK_SIZE        (2*LUA_MINSTACK)
       
    35 
       
    36 
       
    37 
       
    38 typedef struct stringtable {
       
    39   GCObject **hash;
       
    40   lu_int32 nuse;  /* number of elements */
       
    41   int size;
       
    42 } stringtable;
       
    43 
       
    44 
       
    45 /*
       
    46 ** informations about a call
       
    47 */
       
    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;
       
    56 
       
    57 
       
    58 
       
    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))
       
    63 
       
    64 
       
    65 /*
       
    66 ** `global state', shared by all threads of this state
       
    67 */
       
    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;
       
    95 
       
    96 
       
    97 /*
       
    98 ** `per thread' state
       
    99 */
       
   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   unsigned short baseCcalls;  /* nested C calls when resuming coroutine */
       
   116   lu_byte hookmask;
       
   117   lu_byte allowhook;
       
   118   int basehookcount;
       
   119   int hookcount;
       
   120   lua_Hook hook;
       
   121   TValue l_gt;  /* table of globals */
       
   122   TValue env;  /* temporary place for environments */
       
   123   GCObject *openupval;  /* list of open upvalues in this stack */
       
   124   GCObject *gclist;
       
   125   struct lua_longjmp *errorJmp;  /* current error recover point */
       
   126   ptrdiff_t errfunc;  /* current error handling function (stack index) */
       
   127 };
       
   128 
       
   129 
       
   130 #define G(L)	(L->l_G)
       
   131 
       
   132 
       
   133 /*
       
   134 ** Union of all collectable objects
       
   135 */
       
   136 union GCObject {
       
   137   GCheader gch;
       
   138   union TString ts;
       
   139   union Udata u;
       
   140   union Closure cl;
       
   141   struct Table h;
       
   142   struct Proto p;
       
   143   struct UpVal uv;
       
   144   struct lua_State th;  /* thread */
       
   145 };
       
   146 
       
   147 
       
   148 /* macros to convert a GCObject into a specific value */
       
   149 #define rawgco2ts(o)	check_exp((o)->gch.tt == LUA_TSTRING, &((o)->ts))
       
   150 #define gco2ts(o)	(&rawgco2ts(o)->tsv)
       
   151 #define rawgco2u(o)	check_exp((o)->gch.tt == LUA_TUSERDATA, &((o)->u))
       
   152 #define gco2u(o)	(&rawgco2u(o)->uv)
       
   153 #define gco2cl(o)	check_exp((o)->gch.tt == LUA_TFUNCTION, &((o)->cl))
       
   154 #define gco2h(o)	check_exp((o)->gch.tt == LUA_TTABLE, &((o)->h))
       
   155 #define gco2p(o)	check_exp((o)->gch.tt == LUA_TPROTO, &((o)->p))
       
   156 #define gco2uv(o)	check_exp((o)->gch.tt == LUA_TUPVAL, &((o)->uv))
       
   157 #define ngcotouv(o) \
       
   158 	check_exp((o) == NULL || (o)->gch.tt == LUA_TUPVAL, &((o)->uv))
       
   159 #define gco2th(o)	check_exp((o)->gch.tt == LUA_TTHREAD, &((o)->th))
       
   160 
       
   161 /* macro to convert any Lua object into a GCObject */
       
   162 #define obj2gco(v)	(cast(GCObject *, (v)))
       
   163 
       
   164 
       
   165 LUAI_FUNC lua_State *luaE_newthread (lua_State *L);
       
   166 LUAI_FUNC void luaE_freethread (lua_State *L, lua_State *L1);
       
   167 
       
   168 #endif
       
   169