lstate.c 6.6 KB

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