lstate.h 5.3 KB

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