lstate.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  1. /*
  2. ** $Id: lstate.c,v 2.62 2009/10/05 16:44:33 roberto Exp roberto $
  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 "lapi.h"
  11. #include "ldebug.h"
  12. #include "ldo.h"
  13. #include "lfunc.h"
  14. #include "lgc.h"
  15. #include "llex.h"
  16. #include "lmem.h"
  17. #include "lstate.h"
  18. #include "lstring.h"
  19. #include "ltable.h"
  20. #include "ltm.h"
  21. #define state_size(x) (sizeof(x) + LUAI_EXTRASPACE)
  22. #define fromstate(l) (cast(lu_byte *, (l)) - LUAI_EXTRASPACE)
  23. #define tostate(l) (cast(lua_State *, cast(lu_byte *, l) + LUAI_EXTRASPACE))
  24. /*
  25. ** Main thread combines a thread state and the global state
  26. */
  27. typedef struct LG {
  28. lua_State l;
  29. global_State g;
  30. } LG;
  31. /*
  32. ** maximum number of nested calls made by error-handling function
  33. */
  34. #define LUAI_EXTRACALLS 10
  35. CallInfo *luaE_extendCI (lua_State *L) {
  36. CallInfo *ci = luaM_new(L, CallInfo);
  37. lua_assert(L->ci->next == NULL);
  38. L->ci->next = ci;
  39. ci->previous = L->ci;
  40. ci->next = NULL;
  41. return ci;
  42. }
  43. void luaE_freeCI (lua_State *L) {
  44. CallInfo *ci = L->ci;
  45. CallInfo *next = ci->next;
  46. ci->next = NULL;
  47. while ((ci = next) != NULL) {
  48. next = ci->next;
  49. luaM_free(L, ci);
  50. }
  51. }
  52. static void stack_init (lua_State *L1, lua_State *L) {
  53. int i;
  54. /* initialize stack array */
  55. L1->stack = luaM_newvector(L, BASIC_STACK_SIZE, TValue);
  56. L1->stacksize = BASIC_STACK_SIZE;
  57. for (i = 0; i < BASIC_STACK_SIZE; i++)
  58. setnilvalue(L1->stack + i); /* erase new stack */
  59. L1->top = L1->stack;
  60. L1->stack_last = L1->stack + L1->stacksize - EXTRA_STACK;
  61. /* initialize first ci */
  62. L1->ci->func = L1->top;
  63. setnilvalue(L1->top++); /* 'function' entry for this 'ci' */
  64. L1->ci->top = L1->top + LUA_MINSTACK;
  65. L1->ci->callstatus = 0;
  66. }
  67. static void freestack (lua_State *L) {
  68. L->ci = &L->base_ci; /* reset 'ci' list */
  69. luaE_freeCI(L);
  70. luaM_freearray(L, L->stack, L->stacksize);
  71. }
  72. /*
  73. ** Calls the function in variable pointed to by userdata in first argument
  74. ** (Userdata cannot point directly to the function because pointer to
  75. ** function is not compatible with void*.)
  76. */
  77. static int cpcall (lua_State *L) {
  78. lua_CFunction f = *(lua_CFunction *)lua_touserdata(L, 1);
  79. lua_remove(L, 1); /* remove f from stack */
  80. /* restore original environment for 'cpcall' */
  81. lua_copy(L, LUA_GLOBALSINDEX, LUA_ENVIRONINDEX);
  82. return f(L);
  83. }
  84. /*
  85. ** Create registry table and its predefined values
  86. */
  87. static void init_registry (lua_State *L, global_State *g) {
  88. Closure *cp;
  89. TValue mt;
  90. /* create registry */
  91. Table *registry = luaH_new(L);
  92. sethvalue(L, &g->l_registry, registry);
  93. luaH_resize(L, registry, LUA_RIDX_LAST, 0);
  94. /* registry[LUA_RIDX_MAINTHREAD] = L */
  95. setthvalue(L, &mt, L);
  96. setobj2t(L, luaH_setint(L, registry, LUA_RIDX_MAINTHREAD), &mt);
  97. /* registry[LUA_RIDX_CPCALL] = cpcall */
  98. cp = luaF_newCclosure(L, 0, hvalue(&g->l_gt));
  99. cp->c.f = cpcall;
  100. setclvalue(L, &mt, cp);
  101. setobj2t(L, luaH_setint(L, registry, LUA_RIDX_CPCALL), &mt);
  102. }
  103. /*
  104. ** open parts of the state that may cause memory-allocation errors
  105. */
  106. static void f_luaopen (lua_State *L, void *ud) {
  107. global_State *g = G(L);
  108. UNUSED(ud);
  109. stack_init(L, L); /* init stack */
  110. sethvalue(L, &g->l_gt, luaH_new(L)); /* table of globals */
  111. init_registry(L, g);
  112. luaS_resize(L, MINSTRTABSIZE); /* initial size of string table */
  113. luaT_init(L);
  114. luaX_init(L);
  115. luaS_fix(luaS_newliteral(L, MEMERRMSG));
  116. g->estimate = g->totalbytes;
  117. g->GCthreshold = 4*g->totalbytes;
  118. }
  119. /*
  120. ** preinitialize a state with consistent values without allocating
  121. ** any memory (to avoid errors)
  122. */
  123. static void preinit_state (lua_State *L, global_State *g) {
  124. G(L) = g;
  125. L->stack = NULL;
  126. L->stacksize = 0;
  127. L->errorJmp = NULL;
  128. L->hook = NULL;
  129. L->hookmask = 0;
  130. L->basehookcount = 0;
  131. L->allowhook = 1;
  132. resethookcount(L);
  133. L->openupval = NULL;
  134. L->nny = 1;
  135. L->status = LUA_OK;
  136. L->base_ci.next = L->base_ci.previous = NULL;
  137. L->ci = &L->base_ci;
  138. L->errfunc = 0;
  139. }
  140. static void close_state (lua_State *L) {
  141. global_State *g = G(L);
  142. luaF_close(L, L->stack); /* close all upvalues for this thread */
  143. luaC_freeall(L); /* collect all objects */
  144. luaM_freearray(L, G(L)->strt.hash, G(L)->strt.size);
  145. luaZ_freebuffer(L, &g->buff);
  146. freestack(L);
  147. lua_assert(g->totalbytes == sizeof(LG));
  148. (*g->frealloc)(g->ud, fromstate(L), state_size(LG), 0);
  149. }
  150. LUA_API lua_State *lua_newthread (lua_State *L) {
  151. lua_State *L1;
  152. lua_lock(L);
  153. luaC_checkGC(L);
  154. L1 = tostate(luaM_malloc(L, state_size(lua_State)));
  155. luaC_link(L, obj2gco(L1), LUA_TTHREAD);
  156. setthvalue(L, L->top, L1);
  157. api_incr_top(L);
  158. preinit_state(L1, G(L));
  159. stack_init(L1, L); /* init stack */
  160. L1->hookmask = L->hookmask;
  161. L1->basehookcount = L->basehookcount;
  162. L1->hook = L->hook;
  163. resethookcount(L1);
  164. lua_assert(iswhite(obj2gco(L1)));
  165. lua_unlock(L);
  166. luai_userstatethread(L, L1);
  167. return L1;
  168. }
  169. void luaE_freethread (lua_State *L, lua_State *L1) {
  170. luaF_close(L1, L1->stack); /* close all upvalues for this thread */
  171. lua_assert(L1->openupval == NULL);
  172. luai_userstatefree(L1);
  173. freestack(L1);
  174. luaM_freemem(L, fromstate(L1), state_size(lua_State));
  175. }
  176. LUA_API lua_State *lua_newstate (lua_Alloc f, void *ud) {
  177. int i;
  178. lua_State *L;
  179. global_State *g;
  180. void *l = (*f)(ud, NULL, 0, state_size(LG));
  181. if (l == NULL) return NULL;
  182. L = tostate(l);
  183. g = &((LG *)L)->g;
  184. L->next = NULL;
  185. L->tt = LUA_TTHREAD;
  186. g->currentwhite = bit2mask(WHITE0BIT, FIXEDBIT);
  187. L->marked = luaC_white(g);
  188. g->gckind = KGC_NORMAL;
  189. g->nCcalls = 0;
  190. set2bits(L->marked, FIXEDBIT, SFIXEDBIT);
  191. preinit_state(L, g);
  192. g->frealloc = f;
  193. g->ud = ud;
  194. g->mainthread = L;
  195. g->uvhead.u.l.prev = &g->uvhead;
  196. g->uvhead.u.l.next = &g->uvhead;
  197. g->GCthreshold = MAX_LUMEM; /* no GC while building state */
  198. g->strt.size = 0;
  199. g->strt.nuse = 0;
  200. g->strt.hash = NULL;
  201. setnilvalue(&g->l_registry);
  202. setnilvalue(&g->l_gt);
  203. luaZ_initbuffer(L, &g->buff);
  204. g->panic = NULL;
  205. g->version = lua_version(NULL);
  206. g->gcstate = GCSpause;
  207. g->rootgc = obj2gco(L);
  208. g->sweepstrgc = 0;
  209. g->sweepgc = &g->rootgc;
  210. g->gray = NULL;
  211. g->grayagain = NULL;
  212. g->weak = g->ephemeron = g->allweak = NULL;
  213. g->tobefnz = NULL;
  214. g->estimate = g->totalbytes = sizeof(LG);
  215. g->gcpause = LUAI_GCPAUSE;
  216. g->gcstepmul = LUAI_GCMUL;
  217. g->gcdept = 0;
  218. for (i=0; i<NUM_TAGS; i++) g->mt[i] = NULL;
  219. if (luaD_rawrunprotected(L, f_luaopen, NULL) != LUA_OK) {
  220. /* memory allocation error: free partial state */
  221. close_state(L);
  222. L = NULL;
  223. }
  224. else
  225. luai_userstateopen(L);
  226. return L;
  227. }
  228. LUA_API void lua_close (lua_State *L) {
  229. L = G(L)->mainthread; /* only the main thread can be closed */
  230. lua_lock(L);
  231. luaF_close(L, L->stack); /* close all upvalues for this thread */
  232. luaC_separateudata(L, 1); /* separate all udata with GC metamethods */
  233. lua_assert(L->next == NULL);
  234. luaC_callAllGCTM(L); /* call GC metamethods for all udata */
  235. luai_userstateclose(L);
  236. close_state(L);
  237. }