lstate.h 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. /*
  2. ** $Id: lstate.h,v 2.111 2014/07/18 12:17:54 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. /*
  13. ** Some notes about garbage-collected objects: All objects in Lua must
  14. ** be kept somehow accessible until being freed, so all objects always
  15. ** belong to one (and only one) of these lists, using field 'next' of
  16. ** the 'CommonHeader' for the link:
  17. **
  18. ** allgc: all objects not marked for finalization;
  19. ** finobj: all objects marked for finalization;
  20. ** tobefnz: all objects ready to be finalized;
  21. ** fixedgc: all objects that are not to be collected (currently
  22. ** only small strings, such as reserved words).
  23. */
  24. struct lua_longjmp; /* defined in ldo.c */
  25. /* extra stack space to handle TM calls and some other extras */
  26. #define EXTRA_STACK 5
  27. #define BASIC_STACK_SIZE (2*LUA_MINSTACK)
  28. /* kinds of Garbage Collection */
  29. #define KGC_NORMAL 0
  30. #define KGC_EMERGENCY 1 /* gc was forced by an allocation failure */
  31. typedef struct stringtable {
  32. TString **hash;
  33. int nuse; /* number of elements */
  34. int size;
  35. } stringtable;
  36. /*
  37. ** information about a call
  38. */
  39. typedef struct CallInfo {
  40. StkId func; /* function index in the stack */
  41. StkId top; /* top for this function */
  42. struct CallInfo *previous, *next; /* dynamic call link */
  43. ptrdiff_t extra;
  44. short nresults; /* expected number of results from this function */
  45. lu_byte callstatus;
  46. union {
  47. struct { /* only for Lua functions */
  48. StkId base; /* base for this function */
  49. const Instruction *savedpc;
  50. } l;
  51. struct { /* only for C functions */
  52. lua_KFunction k; /* continuation in case of yields */
  53. ptrdiff_t old_errfunc;
  54. lua_Ctx ctx; /* context info. in case of yields */
  55. } c;
  56. } u;
  57. } CallInfo;
  58. /*
  59. ** Bits in CallInfo status
  60. */
  61. #define CIST_OAH (1<<0) /* original value of 'allowhook' */
  62. #define CIST_LUA (1<<1) /* call is running a Lua function */
  63. #define CIST_HOOKED (1<<2) /* call is running a debug hook */
  64. #define CIST_REENTRY (1<<3) /* call is running on same invocation of
  65. luaV_execute of previous call */
  66. #define CIST_YPCALL (1<<4) /* call is a yieldable protected call */
  67. #define CIST_TAIL (1<<5) /* call was tail called */
  68. #define CIST_HOOKYIELD (1<<6) /* last hook called yielded */
  69. #define isLua(ci) ((ci)->callstatus & CIST_LUA)
  70. /* assume that CIST_OAH has offset 0 and that 'v' is strictly 0/1 */
  71. #define setoah(st,v) ((st) = ((st) & ~CIST_OAH) | (v))
  72. #define getoah(st) ((st) & CIST_OAH)
  73. /*
  74. ** `global state', shared by all threads of this state
  75. */
  76. typedef struct global_State {
  77. lua_Alloc frealloc; /* function to reallocate memory */
  78. void *ud; /* auxiliary data to `frealloc' */
  79. lu_mem totalbytes; /* number of bytes currently allocated - GCdebt */
  80. l_mem GCdebt; /* bytes allocated not yet compensated by the collector */
  81. lu_mem GCmemtrav; /* memory traversed by the GC */
  82. lu_mem GCestimate; /* an estimate of the non-garbage memory in use */
  83. stringtable strt; /* hash table for strings */
  84. TValue l_registry;
  85. unsigned int seed; /* randomized seed for hashes */
  86. lu_byte currentwhite;
  87. lu_byte gcstate; /* state of garbage collector */
  88. lu_byte gckind; /* kind of GC running */
  89. lu_byte gcrunning; /* true if GC is running */
  90. GCObject *allgc; /* list of all collectable objects */
  91. GCObject **sweepgc; /* current position of sweep in list */
  92. GCObject *finobj; /* list of collectable objects with finalizers */
  93. GCObject *gray; /* list of gray objects */
  94. GCObject *grayagain; /* list of objects to be traversed atomically */
  95. GCObject *weak; /* list of tables with weak values */
  96. GCObject *ephemeron; /* list of ephemeron tables (weak keys) */
  97. GCObject *allweak; /* list of all-weak tables */
  98. GCObject *tobefnz; /* list of userdata to be GC */
  99. GCObject *fixedgc; /* list of objects not to be collected */
  100. struct lua_State *twups; /* list of threads with open upvalues */
  101. Mbuffer buff; /* temporary buffer for string concatenation */
  102. unsigned int gcfinnum; /* number of finalizers to call in each GC step */
  103. int gcpause; /* size of pause between successive GCs */
  104. int gcstepmul; /* GC `granularity' */
  105. lua_CFunction panic; /* to be called in unprotected errors */
  106. struct lua_State *mainthread;
  107. const lua_Number *version; /* pointer to version number */
  108. TString *memerrmsg; /* memory-error message */
  109. TString *tmname[TM_N]; /* array with tag-method names */
  110. struct Table *mt[LUA_NUMTAGS]; /* metatables for basic types */
  111. } global_State;
  112. /*
  113. ** `per thread' state
  114. */
  115. struct lua_State {
  116. CommonHeader;
  117. lu_byte status;
  118. StkId top; /* first free slot in the stack */
  119. global_State *l_G;
  120. CallInfo *ci; /* call info for current function */
  121. const Instruction *oldpc; /* last pc traced */
  122. StkId stack_last; /* last free slot in the stack */
  123. StkId stack; /* stack base */
  124. int stacksize;
  125. unsigned short nny; /* number of non-yieldable calls in stack */
  126. unsigned short nCcalls; /* number of nested C calls */
  127. lu_byte hookmask;
  128. lu_byte allowhook;
  129. int basehookcount;
  130. int hookcount;
  131. lua_Hook hook;
  132. UpVal *openupval; /* list of open upvalues in this stack */
  133. GCObject *gclist;
  134. struct lua_State *twups; /* list of threads with open upvalues */
  135. struct lua_longjmp *errorJmp; /* current error recover point */
  136. ptrdiff_t errfunc; /* current error handling function (stack index) */
  137. CallInfo base_ci; /* CallInfo for first level (C calling Lua) */
  138. };
  139. #define G(L) (L->l_G)
  140. /*
  141. ** Union of all collectable objects (only for conversions)
  142. */
  143. union GCUnion {
  144. GCObject gc; /* common header */
  145. struct TString ts;
  146. union Udata u;
  147. union Closure cl;
  148. struct Table h;
  149. struct Proto p;
  150. struct lua_State th; /* thread */
  151. };
  152. #define cast_u(o) cast(union GCUnion *, (o))
  153. /* macros to convert a GCObject into a specific value */
  154. #define gco2ts(o) \
  155. check_exp(novariant((o)->tt) == LUA_TSTRING, &((cast_u(o))->ts))
  156. #define rawgco2u(o) check_exp((o)->tt == LUA_TUSERDATA, &((cast_u(o))->u))
  157. #define gco2u(o) (&rawgco2u(o)->uv)
  158. #define gco2lcl(o) check_exp((o)->tt == LUA_TLCL, &((cast_u(o))->cl.l))
  159. #define gco2ccl(o) check_exp((o)->tt == LUA_TCCL, &((cast_u(o))->cl.c))
  160. #define gco2cl(o) \
  161. check_exp(novariant((o)->tt) == LUA_TFUNCTION, &((cast_u(o))->cl))
  162. #define gco2t(o) check_exp((o)->tt == LUA_TTABLE, &((cast_u(o))->h))
  163. #define gco2p(o) check_exp((o)->tt == LUA_TPROTO, &((cast_u(o))->p))
  164. #define gco2th(o) check_exp((o)->tt == LUA_TTHREAD, &((cast_u(o))->th))
  165. /* macro to convert a Lua object into a GCObject */
  166. #define obj2gco(v) \
  167. check_exp(novariant((v)->tt) < LUA_TDEADKEY, (&(cast_u(v)->gc)))
  168. /* actual number of total bytes allocated */
  169. #define gettotalbytes(g) ((g)->totalbytes + (g)->GCdebt)
  170. LUAI_FUNC void luaE_setdebt (global_State *g, l_mem debt);
  171. LUAI_FUNC void luaE_freethread (lua_State *L, lua_State *L1);
  172. LUAI_FUNC CallInfo *luaE_extendCI (lua_State *L);
  173. LUAI_FUNC void luaE_freeCI (lua_State *L);
  174. LUAI_FUNC void luaE_shrinkCI (lua_State *L);
  175. #endif