lstate.c 10 KB

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