lstate.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. /*
  2. ** $Id: lstate.c,v 2.36.1.2 2008/01/03 15:20:39 roberto Exp $
  3. ** Global State
  4. ** See Copyright Notice in lua.h
  5. */
  6. #define lstate_c
  7. #define LUA_CORE
  8. #define LUAC_CROSS_FILE
  9. #include "lua.h"
  10. #include "ldebug.h"
  11. #include "ldo.h"
  12. #include "lfunc.h"
  13. #include "lgc.h"
  14. #include "llex.h"
  15. #include "lmem.h"
  16. #include "lstate.h"
  17. #include "lstring.h"
  18. #include "ltable.h"
  19. #include "ltm.h"
  20. #define state_size(x) (sizeof(x) + LUAI_EXTRASPACE)
  21. #define fromstate(l) (cast(lu_byte *, (l)) - LUAI_EXTRASPACE)
  22. #define tostate(l) (cast(lua_State *, cast(lu_byte *, l) + LUAI_EXTRASPACE))
  23. /*
  24. ** Main thread combines a thread state and the global state
  25. */
  26. typedef struct LG {
  27. lua_State l;
  28. global_State g;
  29. } LG;
  30. static void stack_init (lua_State *L1, lua_State *L) {
  31. /* initialize CallInfo array */
  32. L1->base_ci = luaM_newvector(L, BASIC_CI_SIZE, CallInfo);
  33. L1->ci = L1->base_ci;
  34. L1->size_ci = BASIC_CI_SIZE;
  35. L1->end_ci = L1->base_ci + L1->size_ci - 1;
  36. /* initialize stack array */
  37. L1->stack = luaM_newvector(L, BASIC_STACK_SIZE + EXTRA_STACK, TValue);
  38. L1->stacksize = BASIC_STACK_SIZE + EXTRA_STACK;
  39. L1->top = L1->stack;
  40. L1->stack_last = L1->stack+(L1->stacksize - EXTRA_STACK)-1;
  41. /* initialize first ci */
  42. L1->ci->func = L1->top;
  43. setnilvalue(L1->top++); /* `function' entry for this `ci' */
  44. L1->base = L1->ci->base = L1->top;
  45. L1->ci->top = L1->top + LUA_MINSTACK;
  46. }
  47. static void freestack (lua_State *L, lua_State *L1) {
  48. luaM_freearray(L, L1->base_ci, L1->size_ci, CallInfo);
  49. luaM_freearray(L, L1->stack, L1->stacksize, TValue);
  50. }
  51. /*
  52. ** open parts that may cause memory-allocation errors
  53. */
  54. static void f_luaopen (lua_State *L, void *ud) {
  55. global_State *g = G(L);
  56. UNUSED(ud);
  57. stack_init(L, L); /* init stack */
  58. sethvalue(L, gt(L), luaH_new(L, 0, 2)); /* table of globals */
  59. sethvalue(L, registry(L), luaH_new(L, 0, 2)); /* registry */
  60. luaS_resize(L, MINSTRTABSIZE); /* initial size of string table */
  61. luaT_init(L);
  62. luaX_init(L);
  63. luaS_fix(luaS_newliteral(L, MEMERRMSG));
  64. g->GCthreshold = 4*g->totalbytes;
  65. }
  66. static void preinit_state (lua_State *L, global_State *g) {
  67. G(L) = g;
  68. L->stack = NULL;
  69. L->stacksize = 0;
  70. L->errorJmp = NULL;
  71. L->hook = NULL;
  72. L->hookmask = 0;
  73. L->basehookcount = 0;
  74. L->allowhook = 1;
  75. resethookcount(L);
  76. L->openupval = NULL;
  77. L->size_ci = 0;
  78. L->nCcalls = L->baseCcalls = 0;
  79. L->status = 0;
  80. L->base_ci = L->ci = NULL;
  81. L->savedpc = NULL;
  82. L->errfunc = 0;
  83. setnilvalue(gt(L));
  84. }
  85. static void close_state (lua_State *L) {
  86. global_State *g = G(L);
  87. luaF_close(L, L->stack); /* close all upvalues for this thread */
  88. luaC_freeall(L); /* collect all objects */
  89. lua_assert(g->rootgc == obj2gco(L));
  90. lua_assert(g->strt.nuse == 0);
  91. luaM_freearray(L, G(L)->strt.hash, G(L)->strt.size, TString *);
  92. luaZ_freebuffer(L, &g->buff);
  93. freestack(L, L);
  94. lua_assert(g->totalbytes == sizeof(LG));
  95. (*g->frealloc)(g->ud, fromstate(L), state_size(LG), 0);
  96. }
  97. lua_State *luaE_newthread (lua_State *L) {
  98. lua_State *L1 = tostate(luaM_malloc(L, state_size(lua_State)));
  99. luaC_link(L, obj2gco(L1), LUA_TTHREAD);
  100. setthvalue(L, L->top, L1); /* put thread on stack */
  101. incr_top(L);
  102. preinit_state(L1, G(L));
  103. stack_init(L1, L); /* init stack */
  104. setobj2n(L, gt(L1), gt(L)); /* share table of globals */
  105. L1->hookmask = L->hookmask;
  106. L1->basehookcount = L->basehookcount;
  107. L1->hook = L->hook;
  108. resethookcount(L1);
  109. lua_assert(!isdead(G(L), obj2gco(L1)));
  110. L->top--; /* remove thread from stack */
  111. return L1;
  112. }
  113. void luaE_freethread (lua_State *L, lua_State *L1) {
  114. luaF_close(L1, L1->stack); /* close all upvalues for this thread */
  115. lua_assert(L1->openupval == NULL);
  116. luai_userstatefree(L1);
  117. freestack(L, L1);
  118. luaM_freemem(L, fromstate(L1), state_size(lua_State));
  119. }
  120. LUA_API lua_State *lua_newstate (lua_Alloc f, void *ud) {
  121. int i;
  122. lua_State *L;
  123. global_State *g;
  124. void *l = (*f)(ud, NULL, 0, state_size(LG));
  125. if (l == NULL) return NULL;
  126. L = tostate(l);
  127. g = &((LG *)L)->g;
  128. L->next = NULL;
  129. L->tt = LUA_TTHREAD;
  130. g->currentwhite = bit2mask(WHITE0BIT, FIXEDBIT);
  131. L->marked = luaC_white(g);
  132. set2bits(L->marked, FIXEDBIT, SFIXEDBIT);
  133. preinit_state(L, g);
  134. g->frealloc = f;
  135. g->ud = ud;
  136. g->mainthread = L;
  137. g->uvhead.u.l.prev = &g->uvhead;
  138. g->uvhead.u.l.next = &g->uvhead;
  139. g->GCthreshold = 0; /* mark it as unfinished state */
  140. g->estimate = 0;
  141. g->strt.size = 0;
  142. g->strt.nuse = 0;
  143. g->strt.hash = NULL;
  144. setnilvalue(registry(L));
  145. luaZ_initbuffer(L, &g->buff);
  146. g->panic = NULL;
  147. g->gcstate = GCSpause;
  148. g->gcflags = GCFlagsNone;
  149. g->rootgc = obj2gco(L);
  150. g->sweepstrgc = 0;
  151. g->sweepgc = &g->rootgc;
  152. g->gray = NULL;
  153. g->grayagain = NULL;
  154. g->weak = NULL;
  155. g->tmudata = NULL;
  156. g->totalbytes = sizeof(LG);
  157. g->memlimit = 0;
  158. g->gcpause = LUAI_GCPAUSE;
  159. g->gcstepmul = LUAI_GCMUL;
  160. g->gcdept = 0;
  161. #ifdef EGC_INITIAL_MODE
  162. g->egcmode = EGC_INITIAL_MODE;
  163. #else
  164. g->egcmode = 0;
  165. #endif
  166. #ifdef EGC_INITIAL_MEMLIMIT
  167. g->memlimit = EGC_INITIAL_MEMLIMIT;
  168. #else
  169. g->memlimit = 0;
  170. #endif
  171. for (i=0; i<NUM_TAGS; i++) g->mt[i] = NULL;
  172. if (luaD_rawrunprotected(L, f_luaopen, NULL) != 0) {
  173. /* memory allocation error: free partial state */
  174. close_state(L);
  175. L = NULL;
  176. }
  177. else
  178. luai_userstateopen(L);
  179. return L;
  180. }
  181. static void callallgcTM (lua_State *L, void *ud) {
  182. UNUSED(ud);
  183. luaC_callGCTM(L); /* call GC metamethods for all udata */
  184. }
  185. // BogdanM: modified for eLua interrupt support
  186. extern lua_State *luaL_newstate (void);
  187. static lua_State *lua_crtstate;
  188. lua_State *lua_open(void) {
  189. lua_crtstate = luaL_newstate();
  190. return lua_crtstate;
  191. }
  192. lua_State *lua_getstate(void) {
  193. return lua_crtstate;
  194. }
  195. LUA_API void lua_close (lua_State *L) {
  196. #ifndef LUA_CROSS_COMPILER
  197. lua_sethook( L, NULL, 0, 0 );
  198. lua_crtstate = NULL;
  199. lua_pushnil( L );
  200. // lua_rawseti( L, LUA_REGISTRYINDEX, LUA_INT_HANDLER_KEY );
  201. #endif
  202. L = G(L)->mainthread; /* only the main thread can be closed */
  203. lua_lock(L);
  204. luaF_close(L, L->stack); /* close all upvalues for this thread */
  205. luaC_separateudata(L, 1); /* separate udata that have GC metamethods */
  206. L->errfunc = 0; /* no error function during GC metamethods */
  207. do { /* repeat until no more errors */
  208. L->ci = L->base_ci;
  209. L->base = L->top = L->ci->base;
  210. L->nCcalls = L->baseCcalls = 0;
  211. } while (luaD_rawrunprotected(L, callallgcTM, NULL) != 0);
  212. lua_assert(G(L)->tmudata == NULL);
  213. luai_userstateclose(L);
  214. close_state(L);
  215. }