lstate.c 11 KB

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