lstate.c 11 KB

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