lstate.c 11 KB

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