lstate.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410
  1. /*
  2. ** $Id: lstate.c $
  3. ** Global State
  4. ** See Copyright Notice in lua.h
  5. */
  6. #define lstate_c
  7. #define LUA_CORE
  8. #include "lprefix.h"
  9. #include <stddef.h>
  10. #include <string.h>
  11. #include "lua.h"
  12. #include "lapi.h"
  13. #include "ldebug.h"
  14. #include "ldo.h"
  15. #include "lfunc.h"
  16. #include "lgc.h"
  17. #include "llex.h"
  18. #include "lmem.h"
  19. #include "lstate.h"
  20. #include "lstring.h"
  21. #include "ltable.h"
  22. #include "ltm.h"
  23. /*
  24. ** thread state + extra space
  25. */
  26. typedef struct LX {
  27. lu_byte extra_[LUA_EXTRASPACE];
  28. lua_State l;
  29. } LX;
  30. /*
  31. ** Main thread combines a thread state and the global state
  32. */
  33. typedef struct LG {
  34. LX l;
  35. global_State g;
  36. } LG;
  37. #define fromstate(L) (cast(LX *, cast(lu_byte *, (L)) - offsetof(LX, l)))
  38. /*
  39. ** set GCdebt to a new value keeping the real number of allocated
  40. ** objects (totalobjs - GCdebt) invariant and avoiding overflows in
  41. ** 'totalobjs'.
  42. */
  43. void luaE_setdebt (global_State *g, l_obj debt) {
  44. l_obj tb = gettotalobjs(g);
  45. lua_assert(tb > 0);
  46. if (debt > MAX_LOBJ - tb)
  47. debt = MAX_LOBJ - tb; /* will make 'totalobjs == MAX_LMEM' */
  48. g->totalobjs = tb + debt;
  49. g->GCdebt = debt;
  50. }
  51. CallInfo *luaE_extendCI (lua_State *L) {
  52. CallInfo *ci;
  53. lua_assert(L->ci->next == NULL);
  54. ci = luaM_new(L, CallInfo);
  55. lua_assert(L->ci->next == NULL);
  56. L->ci->next = ci;
  57. ci->previous = L->ci;
  58. ci->next = NULL;
  59. ci->u.l.trap = 0;
  60. L->nci++;
  61. return ci;
  62. }
  63. /*
  64. ** free all CallInfo structures not in use by a thread
  65. */
  66. static void freeCI (lua_State *L) {
  67. CallInfo *ci = L->ci;
  68. CallInfo *next = ci->next;
  69. ci->next = NULL;
  70. while ((ci = next) != NULL) {
  71. next = ci->next;
  72. luaM_free(L, ci);
  73. L->nci--;
  74. }
  75. }
  76. /*
  77. ** free half of the CallInfo structures not in use by a thread,
  78. ** keeping the first one.
  79. */
  80. void luaE_shrinkCI (lua_State *L) {
  81. CallInfo *ci = L->ci->next; /* first free CallInfo */
  82. CallInfo *next;
  83. if (ci == NULL)
  84. return; /* no extra elements */
  85. while ((next = ci->next) != NULL) { /* two extra elements? */
  86. CallInfo *next2 = next->next; /* next's next */
  87. ci->next = next2; /* remove next from the list */
  88. L->nci--;
  89. luaM_free(L, next); /* free next */
  90. if (next2 == NULL)
  91. break; /* no more elements */
  92. else {
  93. next2->previous = ci;
  94. ci = next2; /* continue */
  95. }
  96. }
  97. }
  98. /*
  99. ** Called when 'getCcalls(L)' larger or equal to LUAI_MAXCCALLS.
  100. ** If equal, raises an overflow error. If value is larger than
  101. ** LUAI_MAXCCALLS (which means it is handling an overflow) but
  102. ** not much larger, does not report an error (to allow overflow
  103. ** handling to work).
  104. */
  105. void luaE_checkcstack (lua_State *L) {
  106. if (getCcalls(L) == LUAI_MAXCCALLS)
  107. luaG_runerror(L, "C stack overflow");
  108. else if (getCcalls(L) >= (LUAI_MAXCCALLS / 10 * 11))
  109. luaD_throw(L, LUA_ERRERR); /* error while handling stack error */
  110. }
  111. LUAI_FUNC void luaE_incCstack (lua_State *L) {
  112. L->nCcalls++;
  113. if (l_unlikely(getCcalls(L) >= LUAI_MAXCCALLS))
  114. luaE_checkcstack(L);
  115. }
  116. static void stack_init (lua_State *L1, lua_State *L) {
  117. int i; CallInfo *ci;
  118. /* initialize stack array */
  119. L1->stack.p = luaM_newvector(L, BASIC_STACK_SIZE + EXTRA_STACK, StackValue);
  120. L1->tbclist.p = L1->stack.p;
  121. for (i = 0; i < BASIC_STACK_SIZE + EXTRA_STACK; i++)
  122. setnilvalue(s2v(L1->stack.p + i)); /* erase new stack */
  123. L1->top.p = L1->stack.p;
  124. L1->stack_last.p = L1->stack.p + BASIC_STACK_SIZE;
  125. /* initialize first ci */
  126. ci = &L1->base_ci;
  127. ci->next = ci->previous = NULL;
  128. ci->callstatus = CIST_C;
  129. ci->func.p = L1->top.p;
  130. ci->u.c.k = NULL;
  131. ci->nresults = 0;
  132. setnilvalue(s2v(L1->top.p)); /* 'function' entry for this 'ci' */
  133. L1->top.p++;
  134. ci->top.p = L1->top.p + LUA_MINSTACK;
  135. L1->ci = ci;
  136. }
  137. static void freestack (lua_State *L) {
  138. if (L->stack.p == NULL)
  139. return; /* stack not completely built yet */
  140. L->ci = &L->base_ci; /* free the entire 'ci' list */
  141. freeCI(L);
  142. lua_assert(L->nci == 0);
  143. luaM_freearray(L, L->stack.p, stacksize(L) + EXTRA_STACK); /* free stack */
  144. }
  145. /*
  146. ** Create registry table and its predefined values
  147. */
  148. static void init_registry (lua_State *L, global_State *g) {
  149. /* create registry */
  150. TValue aux;
  151. Table *registry = luaH_new(L);
  152. sethvalue(L, &g->l_registry, registry);
  153. luaH_resize(L, registry, LUA_RIDX_LAST, 0);
  154. /* registry[1] = false */
  155. setbfvalue(&aux);
  156. luaH_setint(L, registry, 1, &aux);
  157. /* registry[LUA_RIDX_MAINTHREAD] = L */
  158. setthvalue(L, &aux, L);
  159. luaH_setint(L, registry, LUA_RIDX_MAINTHREAD, &aux);
  160. /* registry[LUA_RIDX_GLOBALS] = new table (table of globals) */
  161. sethvalue(L, &aux, luaH_new(L));
  162. luaH_setint(L, registry, LUA_RIDX_GLOBALS, &aux);
  163. }
  164. /*
  165. ** open parts of the state that may cause memory-allocation errors.
  166. */
  167. static void f_luaopen (lua_State *L, void *ud) {
  168. global_State *g = G(L);
  169. UNUSED(ud);
  170. stack_init(L, L); /* init stack */
  171. init_registry(L, g);
  172. luaS_init(L);
  173. luaT_init(L);
  174. luaX_init(L);
  175. g->gcstp = 0; /* allow gc */
  176. setnilvalue(&g->nilvalue); /* now state is complete */
  177. luai_userstateopen(L);
  178. }
  179. /*
  180. ** preinitialize a thread with consistent values without allocating
  181. ** any memory (to avoid errors)
  182. */
  183. static void preinit_thread (lua_State *L, global_State *g) {
  184. G(L) = g;
  185. L->stack.p = NULL;
  186. L->ci = NULL;
  187. L->nci = 0;
  188. L->twups = L; /* thread has no upvalues */
  189. L->nCcalls = 0;
  190. L->errorJmp = NULL;
  191. L->hook = NULL;
  192. L->hookmask = 0;
  193. L->basehookcount = 0;
  194. L->allowhook = 1;
  195. resethookcount(L);
  196. L->openupval = NULL;
  197. L->status = LUA_OK;
  198. L->errfunc = 0;
  199. L->oldpc = 0;
  200. }
  201. static void close_state (lua_State *L) {
  202. global_State *g = G(L);
  203. if (!completestate(g)) /* closing a partially built state? */
  204. luaC_freeallobjects(L); /* just collect its objects */
  205. else { /* closing a fully built state */
  206. L->ci = &L->base_ci; /* unwind CallInfo list */
  207. luaD_closeprotected(L, 1, LUA_OK); /* close all upvalues */
  208. luaC_freeallobjects(L); /* collect all objects */
  209. luai_userstateclose(L);
  210. }
  211. luaM_freearray(L, G(L)->strt.hash, G(L)->strt.size);
  212. freestack(L);
  213. lua_assert(g->totalbytes == sizeof(LG));
  214. lua_assert(gettotalobjs(g) == 1);
  215. (*g->frealloc)(g->ud, fromstate(L), sizeof(LG), 0); /* free main block */
  216. }
  217. LUA_API lua_State *lua_newthread (lua_State *L) {
  218. global_State *g = G(L);
  219. GCObject *o;
  220. lua_State *L1;
  221. lua_lock(L);
  222. luaC_checkGC(L);
  223. /* create new thread */
  224. o = luaC_newobjdt(L, LUA_TTHREAD, sizeof(LX), offsetof(LX, l));
  225. L1 = gco2th(o);
  226. /* anchor it on L stack */
  227. setthvalue2s(L, L->top.p, L1);
  228. api_incr_top(L);
  229. preinit_thread(L1, g);
  230. L1->hookmask = L->hookmask;
  231. L1->basehookcount = L->basehookcount;
  232. L1->hook = L->hook;
  233. resethookcount(L1);
  234. /* initialize L1 extra space */
  235. memcpy(lua_getextraspace(L1), lua_getextraspace(g->mainthread),
  236. LUA_EXTRASPACE);
  237. luai_userstatethread(L, L1);
  238. stack_init(L1, L); /* init stack */
  239. lua_unlock(L);
  240. return L1;
  241. }
  242. void luaE_freethread (lua_State *L, lua_State *L1) {
  243. LX *l = fromstate(L1);
  244. luaF_closeupval(L1, L1->stack.p); /* close all upvalues */
  245. lua_assert(L1->openupval == NULL);
  246. luai_userstatefree(L, L1);
  247. freestack(L1);
  248. luaM_free(L, l);
  249. }
  250. int luaE_resetthread (lua_State *L, int status) {
  251. CallInfo *ci = L->ci = &L->base_ci; /* unwind CallInfo list */
  252. setnilvalue(s2v(L->stack.p)); /* 'function' entry for basic 'ci' */
  253. ci->func.p = L->stack.p;
  254. ci->callstatus = CIST_C;
  255. if (status == LUA_YIELD)
  256. status = LUA_OK;
  257. L->status = LUA_OK; /* so it can run __close metamethods */
  258. status = luaD_closeprotected(L, 1, status);
  259. if (status != LUA_OK) /* errors? */
  260. luaD_seterrorobj(L, status, L->stack.p + 1);
  261. else
  262. L->top.p = L->stack.p + 1;
  263. ci->top.p = L->top.p + LUA_MINSTACK;
  264. luaD_reallocstack(L, cast_int(ci->top.p - L->stack.p), 0);
  265. return status;
  266. }
  267. LUA_API int lua_closethread (lua_State *L, lua_State *from) {
  268. int status;
  269. lua_lock(L);
  270. L->nCcalls = (from) ? getCcalls(from) : 0;
  271. status = luaE_resetthread(L, L->status);
  272. lua_unlock(L);
  273. return status;
  274. }
  275. LUA_API lua_State *lua_newstate (lua_Alloc f, void *ud, unsigned seed) {
  276. int i;
  277. lua_State *L;
  278. global_State *g;
  279. LG *l = cast(LG *, (*f)(ud, NULL, LUA_TTHREAD, sizeof(LG)));
  280. if (l == NULL) return NULL;
  281. L = &l->l.l;
  282. g = &l->g;
  283. L->tt = LUA_VTHREAD;
  284. g->currentwhite = bitmask(WHITE0BIT);
  285. L->marked = luaC_white(g);
  286. preinit_thread(L, g);
  287. g->allgc = obj2gco(L); /* by now, only object is the main thread */
  288. L->next = NULL;
  289. incnny(L); /* main thread is always non yieldable */
  290. g->frealloc = f;
  291. g->ud = ud;
  292. g->warnf = NULL;
  293. g->ud_warn = NULL;
  294. g->mainthread = L;
  295. g->seed = seed;
  296. g->gcstp = GCSTPGC; /* no GC while building state */
  297. g->strt.size = g->strt.nuse = 0;
  298. g->strt.hash = NULL;
  299. setnilvalue(&g->l_registry);
  300. g->panic = NULL;
  301. g->gcstate = GCSpause;
  302. g->gckind = KGC_INC;
  303. g->gcstopem = 0;
  304. g->gcemergency = 0;
  305. g->finobj = g->tobefnz = g->fixedgc = NULL;
  306. g->firstold1 = g->survival = g->old1 = g->reallyold = NULL;
  307. g->finobjsur = g->finobjold1 = g->finobjrold = NULL;
  308. g->sweepgc = NULL;
  309. g->gray = g->grayagain = NULL;
  310. g->weak = g->ephemeron = g->allweak = NULL;
  311. g->twups = NULL;
  312. g->totalbytes = sizeof(LG);
  313. g->totalobjs = 1;
  314. g->marked = 0;
  315. g->GCdebt = 0;
  316. setivalue(&g->nilvalue, 0); /* to signal that state is not yet built */
  317. setgcparam(g, PAUSE, LUAI_GCPAUSE);
  318. setgcparam(g, STEPMUL, LUAI_GCMUL);
  319. setgcparam(g, STEPSIZE, LUAI_GCSTEPSIZE);
  320. setgcparam(g, MINORMUL, LUAI_GENMINORMUL);
  321. setgcparam(g, MINORMAJOR, LUAI_MINORMAJOR);
  322. setgcparam(g, MAJORMINOR, LUAI_MAJORMINOR);
  323. for (i=0; i < LUA_NUMTYPES; i++) g->mt[i] = NULL;
  324. if (luaD_rawrunprotected(L, f_luaopen, NULL) != LUA_OK) {
  325. /* memory allocation error: free partial state */
  326. close_state(L);
  327. L = NULL;
  328. }
  329. return L;
  330. }
  331. LUA_API void lua_close (lua_State *L) {
  332. lua_lock(L);
  333. L = G(L)->mainthread; /* only the main thread can be closed */
  334. close_state(L);
  335. }
  336. void luaE_warning (lua_State *L, const char *msg, int tocont) {
  337. lua_WarnFunction wf = G(L)->warnf;
  338. if (wf != NULL)
  339. wf(G(L)->ud_warn, msg, tocont);
  340. }
  341. /*
  342. ** Generate a warning from an error message
  343. */
  344. void luaE_warnerror (lua_State *L, const char *where) {
  345. TValue *errobj = s2v(L->top.p - 1); /* error object */
  346. const char *msg = (ttisstring(errobj))
  347. ? getstr(tsvalue(errobj))
  348. : "error object is not a string";
  349. /* produce warning "error in %s (%s)" (where, msg) */
  350. luaE_warning(L, "error in ", 1);
  351. luaE_warning(L, where, 1);
  352. luaE_warning(L, " (", 1);
  353. luaE_warning(L, msg, 1);
  354. luaE_warning(L, ")", 0);
  355. }