lstate.h 5.1 KB

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