lstate.h 4.9 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. TValue l_registry;
  66. struct lua_State *mainthread;
  67. UpVal uvhead; /* head of double-linked list of all open upvalues */
  68. struct Table *mt[NUM_TAGS]; /* metatables for basic types */
  69. TString *tmname[TM_N]; /* array with tag-method names */
  70. } global_State;
  71. /*
  72. ** `per thread' state
  73. */
  74. struct lua_State {
  75. CommonHeader;
  76. lu_byte status;
  77. StkId top; /* first free slot in the stack */
  78. StkId base; /* base of current function */
  79. global_State *l_G;
  80. CallInfo *ci; /* call info for current function */
  81. const Instruction *savedpc; /* `savedpc' of current function */
  82. StkId stack_last; /* last free slot in the stack */
  83. StkId stack; /* stack base */
  84. CallInfo *end_ci; /* points after end of ci array*/
  85. CallInfo *base_ci; /* array of CallInfo's */
  86. int stacksize;
  87. int size_ci; /* size of array `base_ci' */
  88. unsigned short nCcalls; /* number of nested C calls */
  89. unsigned short baseCcalls; /* nested C calls when resuming coroutine */
  90. lu_byte hookmask;
  91. lu_byte allowhook;
  92. int basehookcount;
  93. int hookcount;
  94. lua_Hook hook;
  95. TValue l_gt; /* table of globals */
  96. TValue env; /* temporary place for environments */
  97. GCObject *openupval; /* list of open upvalues in this stack */
  98. GCObject *gclist;
  99. struct lua_longjmp *errorJmp; /* current error recover point */
  100. ptrdiff_t errfunc; /* current error handling function (stack index) */
  101. };
  102. #define G(L) (L->l_G)
  103. /*
  104. ** Union of all collectable objects
  105. */
  106. union GCObject {
  107. GCheader gch;
  108. union TString ts;
  109. union Udata u;
  110. union Closure cl;
  111. struct Table h;
  112. struct Proto p;
  113. struct UpVal uv;
  114. struct lua_State th; /* thread */
  115. };
  116. /* macros to convert a GCObject into a specific value */
  117. #define rawgco2ts(o) check_exp((o)->gch.tt == LUA_TSTRING, &((o)->ts))
  118. #define gco2ts(o) (&rawgco2ts(o)->tsv)
  119. #define rawgco2u(o) check_exp((o)->gch.tt == LUA_TUSERDATA, &((o)->u))
  120. #define gco2u(o) (&rawgco2u(o)->uv)
  121. #define gco2cl(o) check_exp((o)->gch.tt == LUA_TFUNCTION, &((o)->cl))
  122. #define gco2h(o) check_exp((o)->gch.tt == LUA_TTABLE, &((o)->h))
  123. #define gco2p(o) check_exp((o)->gch.tt == LUA_TPROTO, &((o)->p))
  124. #define gco2uv(o) check_exp((o)->gch.tt == LUA_TUPVAL, &((o)->uv))
  125. #define ngcotouv(o) \
  126. check_exp((o) == NULL || (o)->gch.tt == LUA_TUPVAL, &((o)->uv))
  127. #define gco2th(o) check_exp((o)->gch.tt == LUA_TTHREAD, &((o)->th))
  128. /* macro to convert any Lua object into a GCObject */
  129. #define obj2gco(v) (cast(GCObject *, (v)))
  130. LUAI_FUNC lua_State *luaE_newthread (lua_State *L);
  131. LUAI_FUNC void luaE_freethread (lua_State *L, lua_State *L1);
  132. #endif