lstate.h 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. /*
  2. ** $Id: lstate.h,v 1.107 2002/11/22 18:01:46 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. ls_nstr 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. int state; /* bit fields; see below */
  54. union {
  55. struct { /* for Lua functions */
  56. const Instruction *savedpc;
  57. const Instruction **pc; /* points to `pc' variable in `luaV_execute' */
  58. } l;
  59. struct { /* for C functions */
  60. int dummy; /* just to avoid an empty struct */
  61. } c;
  62. } u;
  63. } CallInfo;
  64. /*
  65. ** bit fields for `CallInfo.state'
  66. */
  67. #define CI_C (1<<0) /* 1 if function is a C function */
  68. /* 1 if (Lua) function has an active `luaV_execute' running it */
  69. #define CI_HASFRAME (1<<1)
  70. /* 1 if Lua function is calling another Lua function (and therefore its
  71. `pc' is being used by the other, and therefore CI_SAVEDPC is 1 too) */
  72. #define CI_CALLING (1<<2)
  73. #define CI_SAVEDPC (1<<3) /* 1 if `savedpc' is updated */
  74. #define CI_YIELD (1<<4) /* 1 if thread is suspended */
  75. #define ci_func(ci) (clvalue((ci)->base - 1))
  76. /*
  77. ** `global state', shared by all threads of this state
  78. */
  79. typedef struct global_State {
  80. stringtable strt; /* hash table for strings */
  81. GCObject *rootgc; /* list of (almost) all collectable objects */
  82. GCObject *rootudata; /* (separated) list of all userdata */
  83. GCObject *tmudata; /* list of userdata to be GC */
  84. Mbuffer buff; /* temporary buffer for string concatentation */
  85. lu_mem GCthreshold;
  86. lu_mem nblocks; /* number of `bytes' currently allocated */
  87. lua_CFunction panic; /* to be called in unprotected errors */
  88. TObject _registry;
  89. TObject _defaultmeta;
  90. struct lua_State *mainthread;
  91. Node dummynode[1]; /* common node array for all empty tables */
  92. TString *tmname[TM_N]; /* array with tag-method names */
  93. } global_State;
  94. /*
  95. ** `per thread' state
  96. */
  97. struct lua_State {
  98. CommonHeader;
  99. StkId top; /* first free slot in the stack */
  100. StkId base; /* base of current function */
  101. global_State *l_G;
  102. CallInfo *ci; /* call info for current function */
  103. StkId stack_last; /* last free slot in the stack */
  104. StkId stack; /* stack base */
  105. int stacksize;
  106. CallInfo *end_ci; /* points after end of ci array*/
  107. CallInfo *base_ci; /* array of CallInfo's */
  108. unsigned short size_ci; /* size of array `base_ci' */
  109. unsigned short nCcalls; /* number of nested C calls */
  110. lu_byte hookmask;
  111. lu_byte allowhook;
  112. lu_byte hookinit;
  113. int basehookcount;
  114. int hookcount;
  115. lua_Hook hook;
  116. TObject _gt; /* table of globals */
  117. GCObject *openupval; /* list of open upvalues in this stack */
  118. GCObject *gclist;
  119. struct lua_longjmp *errorJmp; /* current error recover point */
  120. ptrdiff_t errfunc; /* current error handling function (stack index) */
  121. };
  122. #define G(L) (L->l_G)
  123. /*
  124. ** Union of all collectable objects
  125. */
  126. union GCObject {
  127. GCheader gch;
  128. union TString ts;
  129. union Udata u;
  130. union Closure cl;
  131. struct Table h;
  132. struct Proto p;
  133. struct UpVal uv;
  134. struct lua_State th; /* thread */
  135. };
  136. /* macros to convert a GCObject into a specific value */
  137. #define gcotots(o) check_exp((o)->gch.tt == LUA_TSTRING, &((o)->ts))
  138. #define gcotou(o) check_exp((o)->gch.tt == LUA_TUSERDATA, &((o)->u))
  139. #define gcotocl(o) check_exp((o)->gch.tt == LUA_TFUNCTION, &((o)->cl))
  140. #define gcotoh(o) check_exp((o)->gch.tt == LUA_TTABLE, &((o)->h))
  141. #define gcotop(o) check_exp((o)->gch.tt == LUA_TPROTO, &((o)->p))
  142. #define gcotouv(o) check_exp((o)->gch.tt == LUA_TUPVAL, &((o)->uv))
  143. #define ngcotouv(o) \
  144. check_exp((o) == NULL || (o)->gch.tt == LUA_TUPVAL, &((o)->uv))
  145. #define gcototh(o) check_exp((o)->gch.tt == LUA_TTHREAD, &((o)->th))
  146. /* macro to convert any value into a GCObject */
  147. #define valtogco(v) (cast(GCObject *, (v)))
  148. lua_State *luaE_newthread (lua_State *L);
  149. void luaE_freethread (lua_State *L, lua_State *L1);
  150. #endif