lstate.h 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. /*
  2. ** $Id: lstate.h,v 1.110 2003/04/28 19:26:16 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. ** macros for thread synchronization inside Lua core machine:
  14. ** all accesses to the global state and to global objects are synchronized.
  15. ** Because threads can read the stack of other threads
  16. ** (when running garbage collection),
  17. ** a thread must also synchronize any write-access to its own stack.
  18. ** Unsynchronized accesses are allowed only when reading its own stack,
  19. ** or when reading immutable fields from global objects
  20. ** (such as string values and udata values).
  21. */
  22. #ifndef lua_lock
  23. #define lua_lock(L) ((void) 0)
  24. #endif
  25. #ifndef lua_unlock
  26. #define lua_unlock(L) ((void) 0)
  27. #endif
  28. #ifndef lua_userstateopen
  29. #define lua_userstateopen(l)
  30. #endif
  31. struct lua_longjmp; /* defined in ldo.c */
  32. /* default meta table (both for tables and udata) */
  33. #define defaultmeta(L) (&G(L)->_defaultmeta)
  34. /* table of globals */
  35. #define gt(L) (&L->_gt)
  36. /* registry */
  37. #define registry(L) (&G(L)->_registry)
  38. /* extra stack space to handle TM calls and some other extras */
  39. #define EXTRA_STACK 5
  40. #define BASIC_CI_SIZE 8
  41. #define BASIC_STACK_SIZE (2*LUA_MINSTACK)
  42. typedef struct stringtable {
  43. GCObject **hash;
  44. lu_int32 nuse; /* number of elements */
  45. int size;
  46. } stringtable;
  47. /*
  48. ** informations about a call
  49. */
  50. typedef struct CallInfo {
  51. StkId base; /* base for called function */
  52. StkId top; /* top for this function */
  53. union {
  54. struct { /* for Lua functions */
  55. const Instruction *savedpc;
  56. int tailcalls; /* number of tail calls lost under this entry */
  57. } l;
  58. struct { /* for C functions */
  59. int dummy; /* just to avoid an empty struct */
  60. } c;
  61. } u;
  62. } CallInfo;
  63. #define ci_func(ci) (clvalue((ci)->base - 1))
  64. #define f_isLua(ci) (!ci_func(ci)->c.isC)
  65. #define isLua(ci) (ttisfunction((ci)->base - 1) && f_isLua(ci))
  66. /*
  67. ** `global state', shared by all threads of this state
  68. */
  69. typedef struct global_State {
  70. stringtable strt; /* hash table for strings */
  71. GCObject *rootgc; /* list of (almost) all collectable objects */
  72. GCObject *rootudata; /* (separated) list of all userdata */
  73. GCObject *tmudata; /* list of userdata to be GC */
  74. Mbuffer buff; /* temporary buffer for string concatentation */
  75. lu_mem GCthreshold;
  76. lu_mem nblocks; /* number of `bytes' currently allocated */
  77. lua_CFunction panic; /* to be called in unprotected errors */
  78. TObject _registry;
  79. TObject _defaultmeta;
  80. struct lua_State *mainthread;
  81. Node dummynode[1]; /* common node array for all empty tables */
  82. TString *tmname[TM_N]; /* array with tag-method names */
  83. } global_State;
  84. /*
  85. ** `per thread' state
  86. */
  87. struct lua_State {
  88. CommonHeader;
  89. StkId top; /* first free slot in the stack */
  90. StkId base; /* base of current function */
  91. global_State *l_G;
  92. CallInfo *ci; /* call info for current function */
  93. StkId stack_last; /* last free slot in the stack */
  94. StkId stack; /* stack base */
  95. int stacksize;
  96. CallInfo *end_ci; /* points after end of ci array*/
  97. CallInfo *base_ci; /* array of CallInfo's */
  98. unsigned short size_ci; /* size of array `base_ci' */
  99. unsigned short nCcalls; /* number of nested C calls */
  100. lu_byte hookmask;
  101. lu_byte allowhook;
  102. lu_byte isSuspended;
  103. int basehookcount;
  104. int hookcount;
  105. lua_Hook hook;
  106. TObject _gt; /* table of globals */
  107. GCObject *openupval; /* list of open upvalues in this stack */
  108. GCObject *gclist;
  109. struct lua_longjmp *errorJmp; /* current error recover point */
  110. ptrdiff_t errfunc; /* current error handling function (stack index) */
  111. };
  112. #define G(L) (L->l_G)
  113. /*
  114. ** Union of all collectable objects
  115. */
  116. union GCObject {
  117. GCheader gch;
  118. union TString ts;
  119. union Udata u;
  120. union Closure cl;
  121. struct Table h;
  122. struct Proto p;
  123. struct UpVal uv;
  124. struct lua_State th; /* thread */
  125. };
  126. /* macros to convert a GCObject into a specific value */
  127. #define gcotots(o) check_exp((o)->gch.tt == LUA_TSTRING, &((o)->ts))
  128. #define gcotou(o) check_exp((o)->gch.tt == LUA_TUSERDATA, &((o)->u))
  129. #define gcotocl(o) check_exp((o)->gch.tt == LUA_TFUNCTION, &((o)->cl))
  130. #define gcotoh(o) check_exp((o)->gch.tt == LUA_TTABLE, &((o)->h))
  131. #define gcotop(o) check_exp((o)->gch.tt == LUA_TPROTO, &((o)->p))
  132. #define gcotouv(o) check_exp((o)->gch.tt == LUA_TUPVAL, &((o)->uv))
  133. #define ngcotouv(o) \
  134. check_exp((o) == NULL || (o)->gch.tt == LUA_TUPVAL, &((o)->uv))
  135. #define gcototh(o) check_exp((o)->gch.tt == LUA_TTHREAD, &((o)->th))
  136. /* macro to convert any value into a GCObject */
  137. #define valtogco(v) (cast(GCObject *, (v)))
  138. lua_State *luaE_newthread (lua_State *L);
  139. void luaE_freethread (lua_State *L, lua_State *L1);
  140. #endif