lstate.c 6.6 KB

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