lstate.h 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. /*
  2. ** $Id: lstate.h,v 1.93 2002/08/07 19:22:39 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. /*
  12. ** macros for thread syncronization inside Lua core machine:
  13. ** all accesses to the global state and to global objects are syncronized.
  14. ** Because threads can read the stack of other threads
  15. ** (when running garbage collection),
  16. ** a thread must also syncronize any write-access to its own stack.
  17. ** Unsyncronized accesses are allowed only when reading its own stack,
  18. ** or when reading immutable fields from global objects
  19. ** (such as string values and udata values).
  20. */
  21. #ifndef lua_lock
  22. #define lua_lock(L) ((void) 0)
  23. #endif
  24. #ifndef lua_unlock
  25. #define lua_unlock(L) ((void) 0)
  26. #endif
  27. /*
  28. ** macro to allow the inclusion of user information in Lua state
  29. */
  30. #ifndef LUA_USERSTATE
  31. #define LUA_USERSTATE
  32. #endif
  33. #ifndef lua_userstateopen
  34. #define lua_userstateopen(l)
  35. #endif
  36. struct lua_longjmp; /* defined in ldo.c */
  37. /*
  38. ** array of `global' objects
  39. */
  40. #define NUMGLOBS 3
  41. /* default meta table (both for tables and udata) */
  42. #define defaultmeta(L) (L->globs)
  43. /* table of globals */
  44. #define gt(L) (L->globs + 1)
  45. /* registry */
  46. #define registry(L) (L->globs + 2)
  47. /* extra stack space to handle TM calls and some other extras */
  48. #define EXTRA_STACK 5
  49. #define BASIC_CI_SIZE 8
  50. #define BASIC_STACK_SIZE (2*LUA_MINSTACK)
  51. typedef struct stringtable {
  52. TString **hash;
  53. ls_nstr nuse; /* number of elements */
  54. int size;
  55. } stringtable;
  56. /*
  57. ** informations about a call
  58. */
  59. typedef struct CallInfo {
  60. StkId base; /* base for called function */
  61. StkId top; /* top for this function */
  62. int state; /* bit fields; see below */
  63. union {
  64. struct { /* for Lua functions */
  65. const Instruction *savedpc;
  66. const Instruction **pc; /* points to `pc' variable in `luaV_execute' */
  67. StkId *pb; /* points to `base' variable in `luaV_execute' */
  68. } l;
  69. struct { /* for C functions */
  70. int yield_results;
  71. } c;
  72. } u;
  73. } CallInfo;
  74. /*
  75. ** bit fields for `CallInfo.state'
  76. */
  77. #define CI_C 1 /* 1 if function is a C function */
  78. /* 1 if (Lua) function has an active `luaV_execute' running it */
  79. #define CI_HASFRAME 2
  80. /* 1 if Lua function is calling another Lua function (and therefore its
  81. `pc' is being used by the other, and therefore CI_SAVEDPC is 1 too) */
  82. #define CI_CALLING 4
  83. #define CI_SAVEDPC 8 /* 1 if `savedpc' is updated */
  84. #define ci_func(ci) (clvalue((ci)->base - 1))
  85. /*
  86. ** `global state', shared by all threads of this state
  87. */
  88. typedef struct global_State {
  89. stringtable strt; /* hash table for strings */
  90. Proto *rootproto; /* list of all prototypes */
  91. Closure *rootcl; /* list of all closures */
  92. Table *roottable; /* list of all tables */
  93. UpVal *rootupval; /* list of closed up values */
  94. Udata *rootudata; /* list of all userdata */
  95. Udata *tmudata; /* list of userdata to be GC */
  96. void *Mbuffer; /* global buffer */
  97. size_t Mbuffsize; /* size of Mbuffer */
  98. lu_mem GCthreshold;
  99. lu_mem nblocks; /* number of `bytes' currently allocated */
  100. lua_CFunction panic; /* to be called in unprotected errors */
  101. Node dummynode[1]; /* common node array for all empty tables */
  102. TString *tmname[TM_N]; /* array with tag-method names */
  103. } global_State;
  104. /*
  105. ** `per thread' state
  106. */
  107. struct lua_State {
  108. LUA_USERSTATE
  109. StkId top; /* first free slot in the stack */
  110. global_State *l_G;
  111. CallInfo *ci; /* call info for current function */
  112. StkId stack_last; /* last free slot in the stack */
  113. StkId stack; /* stack base */
  114. int stacksize;
  115. CallInfo *end_ci; /* points after end of ci array*/
  116. CallInfo *base_ci; /* array of CallInfo's */
  117. int size_ci; /* size of array `base_ci' */
  118. unsigned long hookmask;
  119. ls_count hookcount;
  120. lua_Hook hook;
  121. UpVal *openupval; /* list of open upvalues in this stack */
  122. struct lua_longjmp *errorJmp; /* current error recover point */
  123. ptrdiff_t errfunc; /* current error handling function (stack index) */
  124. lua_State *next; /* circular double linked list of states */
  125. lua_State *previous;
  126. TObject globs[NUMGLOBS]; /* registry, table of globals, etc. */
  127. };
  128. #define G(L) (L->l_G)
  129. void luaE_closethread (lua_State *OL, lua_State *L);
  130. #endif