2
0

lstate.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420
  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_errerr(L); /* 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 resetCI (lua_State *L) {
  119. CallInfo *ci = L->ci = &L->base_ci;
  120. ci->func.p = L->stack.p;
  121. setnilvalue(s2v(ci->func.p)); /* 'function' entry for basic 'ci' */
  122. ci->top.p = ci->func.p + 1 + LUA_MINSTACK; /* +1 for 'function' entry */
  123. ci->u.c.k = NULL;
  124. ci->callstatus = CIST_C;
  125. L->status = LUA_OK;
  126. L->errfunc = 0; /* stack unwind can "throw away" the error function */
  127. }
  128. static void stack_init (lua_State *L1, lua_State *L) {
  129. int i;
  130. /* initialize stack array */
  131. L1->stack.p = luaM_newvector(L, BASIC_STACK_SIZE + EXTRA_STACK, StackValue);
  132. L1->tbclist.p = L1->stack.p;
  133. for (i = 0; i < BASIC_STACK_SIZE + EXTRA_STACK; i++)
  134. setnilvalue(s2v(L1->stack.p + i)); /* erase new stack */
  135. L1->stack_last.p = L1->stack.p + BASIC_STACK_SIZE;
  136. /* initialize first ci */
  137. resetCI(L1);
  138. L1->top.p = L1->stack.p + 1; /* +1 for 'function' entry */
  139. }
  140. static void freestack (lua_State *L) {
  141. if (L->stack.p == NULL)
  142. return; /* stack not completely built yet */
  143. L->ci = &L->base_ci; /* free the entire 'ci' list */
  144. freeCI(L);
  145. lua_assert(L->nci == 0);
  146. /* free stack */
  147. luaM_freearray(L, L->stack.p, cast_sizet(stacksize(L) + EXTRA_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. L->base_ci.previous = L->base_ci.next = NULL;
  205. }
  206. lu_mem luaE_threadsize (lua_State *L) {
  207. lu_mem sz = cast(lu_mem, sizeof(LX))
  208. + cast_uint(L->nci) * sizeof(CallInfo);
  209. if (L->stack.p != NULL)
  210. sz += cast_uint(stacksize(L) + EXTRA_STACK) * sizeof(StackValue);
  211. return sz;
  212. }
  213. static void close_state (lua_State *L) {
  214. global_State *g = G(L);
  215. if (!completestate(g)) /* closing a partially built state? */
  216. luaC_freeallobjects(L); /* just collect its objects */
  217. else { /* closing a fully built state */
  218. resetCI(L);
  219. luaD_closeprotected(L, 1, LUA_OK); /* close all upvalues */
  220. L->top.p = L->stack.p + 1; /* empty the stack to run finalizers */
  221. luaC_freeallobjects(L); /* collect all objects */
  222. luai_userstateclose(L);
  223. }
  224. luaM_freearray(L, G(L)->strt.hash, cast_sizet(G(L)->strt.size));
  225. freestack(L);
  226. lua_assert(gettotalbytes(g) == sizeof(global_State));
  227. (*g->frealloc)(g->ud, g, sizeof(global_State), 0); /* free main block */
  228. }
  229. LUA_API lua_State *lua_newthread (lua_State *L) {
  230. global_State *g = G(L);
  231. GCObject *o;
  232. lua_State *L1;
  233. lua_lock(L);
  234. luaC_checkGC(L);
  235. /* create new thread */
  236. o = luaC_newobjdt(L, LUA_TTHREAD, sizeof(LX), offsetof(LX, l));
  237. L1 = gco2th(o);
  238. /* anchor it on L stack */
  239. setthvalue2s(L, L->top.p, L1);
  240. api_incr_top(L);
  241. preinit_thread(L1, g);
  242. L1->hookmask = L->hookmask;
  243. L1->basehookcount = L->basehookcount;
  244. L1->hook = L->hook;
  245. resethookcount(L1);
  246. /* initialize L1 extra space */
  247. memcpy(lua_getextraspace(L1), lua_getextraspace(mainthread(g)),
  248. LUA_EXTRASPACE);
  249. luai_userstatethread(L, L1);
  250. stack_init(L1, L); /* init stack */
  251. lua_unlock(L);
  252. return L1;
  253. }
  254. void luaE_freethread (lua_State *L, lua_State *L1) {
  255. LX *l = fromstate(L1);
  256. luaF_closeupval(L1, L1->stack.p); /* close all upvalues */
  257. lua_assert(L1->openupval == NULL);
  258. luai_userstatefree(L, L1);
  259. freestack(L1);
  260. luaM_free(L, l);
  261. }
  262. TStatus luaE_resetthread (lua_State *L, TStatus status) {
  263. resetCI(L);
  264. if (status == LUA_YIELD)
  265. status = LUA_OK;
  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. luaD_reallocstack(L, cast_int(L->ci->top.p - L->stack.p), 0);
  272. return status;
  273. }
  274. LUA_API int lua_closethread (lua_State *L, lua_State *from) {
  275. TStatus status;
  276. lua_lock(L);
  277. L->nCcalls = (from) ? getCcalls(from) : 0;
  278. status = luaE_resetthread(L, L->status);
  279. if (L == from) /* closing itself? */
  280. luaD_throwbaselevel(L, status);
  281. lua_unlock(L);
  282. return APIstatus(status);
  283. }
  284. LUA_API lua_State *lua_newstate (lua_Alloc f, void *ud, unsigned seed) {
  285. int i;
  286. lua_State *L;
  287. global_State *g = cast(global_State*,
  288. (*f)(ud, NULL, LUA_TTHREAD, sizeof(global_State)));
  289. if (g == NULL) return NULL;
  290. L = &g->mainth.l;
  291. L->tt = LUA_VTHREAD;
  292. g->currentwhite = bitmask(WHITE0BIT);
  293. L->marked = luaC_white(g);
  294. preinit_thread(L, g);
  295. g->allgc = obj2gco(L); /* by now, only object is the main thread */
  296. L->next = NULL;
  297. incnny(L); /* main thread is always non yieldable */
  298. g->frealloc = f;
  299. g->ud = ud;
  300. g->warnf = NULL;
  301. g->ud_warn = NULL;
  302. g->seed = seed;
  303. g->gcstp = GCSTPGC; /* no GC while building state */
  304. g->strt.size = g->strt.nuse = 0;
  305. g->strt.hash = NULL;
  306. setnilvalue(&g->l_registry);
  307. g->panic = NULL;
  308. g->gcstate = GCSpause;
  309. g->gckind = KGC_INC;
  310. g->gcstopem = 0;
  311. g->gcemergency = 0;
  312. g->finobj = g->tobefnz = g->fixedgc = NULL;
  313. g->firstold1 = g->survival = g->old1 = g->reallyold = NULL;
  314. g->finobjsur = g->finobjold1 = g->finobjrold = NULL;
  315. g->sweepgc = NULL;
  316. g->gray = g->grayagain = NULL;
  317. g->weak = g->ephemeron = g->allweak = NULL;
  318. g->twups = NULL;
  319. g->GCtotalbytes = sizeof(global_State);
  320. g->GCmarked = 0;
  321. g->GCdebt = 0;
  322. setivalue(&g->nilvalue, 0); /* to signal that state is not yet built */
  323. setgcparam(g, PAUSE, LUAI_GCPAUSE);
  324. setgcparam(g, STEPMUL, LUAI_GCMUL);
  325. setgcparam(g, STEPSIZE, LUAI_GCSTEPSIZE);
  326. setgcparam(g, MINORMUL, LUAI_GENMINORMUL);
  327. setgcparam(g, MINORMAJOR, LUAI_MINORMAJOR);
  328. setgcparam(g, MAJORMINOR, LUAI_MAJORMINOR);
  329. for (i=0; i < LUA_NUMTYPES; i++) g->mt[i] = NULL;
  330. if (luaD_rawrunprotected(L, f_luaopen, NULL) != LUA_OK) {
  331. /* memory allocation error: free partial state */
  332. close_state(L);
  333. L = NULL;
  334. }
  335. return L;
  336. }
  337. LUA_API void lua_close (lua_State *L) {
  338. lua_lock(L);
  339. L = mainthread(G(L)); /* only the main thread can be closed */
  340. close_state(L);
  341. }
  342. void luaE_warning (lua_State *L, const char *msg, int tocont) {
  343. lua_WarnFunction wf = G(L)->warnf;
  344. if (wf != NULL)
  345. wf(G(L)->ud_warn, msg, tocont);
  346. }
  347. /*
  348. ** Generate a warning from an error message
  349. */
  350. void luaE_warnerror (lua_State *L, const char *where) {
  351. TValue *errobj = s2v(L->top.p - 1); /* error object */
  352. const char *msg = (ttisstring(errobj))
  353. ? getstr(tsvalue(errobj))
  354. : "error object is not a string";
  355. /* produce warning "error in %s (%s)" (where, msg) */
  356. luaE_warning(L, "error in ", 1);
  357. luaE_warning(L, where, 1);
  358. luaE_warning(L, " (", 1);
  359. luaE_warning(L, msg, 1);
  360. luaE_warning(L, ")", 0);
  361. }