/*1** $Id: lstate.h,v 2.24.1.2 2008/01/03 15:20:39 roberto Exp $2** Global State3** See Copyright Notice in lua.h4*/56#ifndef lstate_h7#define lstate_h89#include "lua.h"1011#include "lobject.h"12#include "ltm.h"13#include "lzio.h"14151617struct lua_longjmp; /* defined in ldo.c */181920/* table of globals */21#define gt(L) (&L->l_gt)2223/* registry */24#define registry(L) (&G(L)->l_registry)252627/* extra stack space to handle TM calls and some other extras */28#define EXTRA_STACK 5293031#define BASIC_CI_SIZE 83233#define BASIC_STACK_SIZE (2*LUA_MINSTACK)34353637typedef struct stringtable {38GCObject **hash;39lu_int32 nuse; /* number of elements */40int size;41} stringtable;424344/*45** informations about a call46*/47typedef struct CallInfo {48StkId base; /* base for this function */49StkId func; /* function index in the stack */50StkId top; /* top for this function */51const Instruction *savedpc;52int nresults; /* expected number of results from this function */53int tailcalls; /* number of tail calls lost under this entry */54} CallInfo;55565758#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))626364/*65** `global state', shared by all threads of this state66*/67typedef struct global_State {68stringtable strt; /* hash table for strings */69lua_Alloc frealloc; /* function to reallocate memory */70void *ud; /* auxiliary data to `frealloc' */71lu_byte currentwhite;72lu_byte gcstate; /* state of garbage collector */73int sweepstrgc; /* position of sweep in `strt' */74GCObject *rootgc; /* list of all collectable objects */75GCObject **sweepgc; /* position of sweep in `rootgc' */76GCObject *gray; /* list of gray objects */77GCObject *grayagain; /* list of objects to be traversed atomically */78GCObject *weak; /* list of weak tables (to be cleared) */79GCObject *tmudata; /* last element of list of userdata to be GC */80Mbuffer buff; /* temporary buffer for string concatentation */81lu_mem GCthreshold;82lu_mem totalbytes; /* number of bytes currently allocated */83lu_mem estimate; /* an estimate of number of bytes actually in use */84lu_mem gcdept; /* how much GC is `behind schedule' */85int gcpause; /* size of pause between successive GCs */86int gcstepmul; /* GC `granularity' */87lua_CFunction panic; /* to be called in unprotected errors */88TValue l_registry;89struct lua_State *mainthread;90UpVal uvhead; /* head of double-linked list of all open upvalues */91struct Table *mt[NUM_TAGS]; /* metatables for basic types */92TString *tmname[TM_N]; /* array with tag-method names */93} global_State;949596/*97** `per thread' state98*/99struct lua_State {100CommonHeader;101lu_byte status;102StkId top; /* first free slot in the stack */103StkId base; /* base of current function */104global_State *l_G;105CallInfo *ci; /* call info for current function */106const Instruction *savedpc; /* `savedpc' of current function */107StkId stack_last; /* last free slot in the stack */108StkId stack; /* stack base */109CallInfo *end_ci; /* points after end of ci array*/110CallInfo *base_ci; /* array of CallInfo's */111int stacksize;112int size_ci; /* size of array `base_ci' */113unsigned short nCcalls; /* number of nested C calls */114unsigned short baseCcalls; /* nested C calls when resuming coroutine */115lu_byte hookmask;116lu_byte allowhook;117int basehookcount;118int hookcount;119lua_Hook hook;120TValue l_gt; /* table of globals */121TValue env; /* temporary place for environments */122GCObject *openupval; /* list of open upvalues in this stack */123GCObject *gclist;124struct lua_longjmp *errorJmp; /* current error recover point */125ptrdiff_t errfunc; /* current error handling function (stack index) */126};127128129#define G(L) (L->l_G)130131132/*133** Union of all collectable objects134*/135union GCObject {136GCheader gch;137union TString ts;138union Udata u;139union Closure cl;140struct Table h;141struct Proto p;142struct UpVal uv;143struct lua_State th; /* thread */144};145146147/* 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) \157check_exp((o) == NULL || (o)->gch.tt == LUA_TUPVAL, &((o)->uv))158#define gco2th(o) check_exp((o)->gch.tt == LUA_TTHREAD, &((o)->th))159160/* macro to convert any Lua object into a GCObject */161#define obj2gco(v) (cast(GCObject *, (v)))162163164LUAI_FUNC lua_State *luaE_newthread (lua_State *L);165LUAI_FUNC void luaE_freethread (lua_State *L, lua_State *L1);166167#endif168169170171