lstate.h 5.2 KB

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