lstate.h 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  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. #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. lu_byte currentwhite;
  49. lu_byte gcstate; /* state of garbage collector */
  50. int sweepstrgc; /* position of sweep in `strt' */
  51. GCObject *rootgc; /* list of all collectable objects */
  52. GCObject **sweepgc; /* position of sweep in `rootgc' */
  53. GCObject *gray; /* list of gray objects */
  54. GCObject *grayagain; /* list of objects to be traversed atomically */
  55. GCObject *weak; /* list of weak tables (to be cleared) */
  56. GCObject *tmudata; /* last element of list of userdata to be GC */
  57. Mbuffer buff; /* temporary buffer for string concatentation */
  58. lu_mem GCthreshold;
  59. lu_mem totalbytes; /* number of bytes currently allocated */
  60. lu_mem estimate; /* an estimate of number of bytes actually in use */
  61. lu_mem gcdept; /* how much GC is `behind schedule' */
  62. int gcpause; /* size of pause between successive GCs */
  63. int gcstepmul; /* GC `granularity' */
  64. lua_CFunction panic; /* to be called in unprotected errors */
  65. lua_CFunctionwrapper wrapcf; /* MINETEST-SPECIFIC CHANGE */
  66. TValue l_registry;
  67. struct lua_State *mainthread;
  68. UpVal uvhead; /* head of double-linked list of all open upvalues */
  69. struct Table *mt[NUM_TAGS]; /* metatables for basic types */
  70. TString *tmname[TM_N]; /* array with tag-method names */
  71. } global_State;
  72. /*
  73. ** `per thread' state
  74. */
  75. struct lua_State {
  76. CommonHeader;
  77. lu_byte status;
  78. StkId top; /* first free slot in the stack */
  79. StkId base; /* base of current function */
  80. global_State *l_G;
  81. CallInfo *ci; /* call info for current function */
  82. const Instruction *savedpc; /* `savedpc' of current function */
  83. StkId stack_last; /* last free slot in the stack */
  84. StkId stack; /* stack base */
  85. CallInfo *end_ci; /* points after end of ci array*/
  86. CallInfo *base_ci; /* array of CallInfo's */
  87. int stacksize;
  88. int size_ci; /* size of array `base_ci' */
  89. unsigned short nCcalls; /* number of nested C calls */
  90. unsigned short baseCcalls; /* nested C calls when resuming coroutine */
  91. lu_byte hookmask;
  92. lu_byte allowhook;
  93. int basehookcount;
  94. int hookcount;
  95. lua_Hook hook;
  96. TValue l_gt; /* table of globals */
  97. TValue env; /* temporary place for environments */
  98. GCObject *openupval; /* list of open upvalues in this stack */
  99. GCObject *gclist;
  100. struct lua_longjmp *errorJmp; /* current error recover point */
  101. ptrdiff_t errfunc; /* current error handling function (stack index) */
  102. };
  103. #define G(L) (L->l_G)
  104. /*
  105. ** Union of all collectable objects
  106. */
  107. union GCObject {
  108. GCheader gch;
  109. union TString ts;
  110. union Udata u;
  111. union Closure cl;
  112. struct Table h;
  113. struct Proto p;
  114. struct UpVal uv;
  115. struct lua_State th; /* thread */
  116. };
  117. /* macros to convert a GCObject into a specific value */
  118. #define rawgco2ts(o) check_exp((o)->gch.tt == LUA_TSTRING, &((o)->ts))
  119. #define gco2ts(o) (&rawgco2ts(o)->tsv)
  120. #define rawgco2u(o) check_exp((o)->gch.tt == LUA_TUSERDATA, &((o)->u))
  121. #define gco2u(o) (&rawgco2u(o)->uv)
  122. #define gco2cl(o) check_exp((o)->gch.tt == LUA_TFUNCTION, &((o)->cl))
  123. #define gco2h(o) check_exp((o)->gch.tt == LUA_TTABLE, &((o)->h))
  124. #define gco2p(o) check_exp((o)->gch.tt == LUA_TPROTO, &((o)->p))
  125. #define gco2uv(o) check_exp((o)->gch.tt == LUA_TUPVAL, &((o)->uv))
  126. #define ngcotouv(o) \
  127. check_exp((o) == NULL || (o)->gch.tt == LUA_TUPVAL, &((o)->uv))
  128. #define gco2th(o) check_exp((o)->gch.tt == LUA_TTHREAD, &((o)->th))
  129. /* macro to convert any Lua object into a GCObject */
  130. #define obj2gco(v) (cast(GCObject *, (v)))
  131. LUAI_FUNC lua_State *luaE_newthread (lua_State *L);
  132. LUAI_FUNC void luaE_freethread (lua_State *L, lua_State *L1);
  133. #endif