36 lu_byte extra_[LUA_EXTRASPACE];
51#define fromstate(L) (cast(LX *, cast(lu_byte *, (L)) - offsetof(LX, l)))
58#if !defined(luai_makeseed)
67#define addbuff(b,p,e) \
68 { size_t t = cast_sizet(e); \
69 memcpy(b + p, &t, sizeof(t)); p += sizeof(t); }
71static unsigned int luai_makeseed (
lua_State *L) {
72 char buff[3 *
sizeof(size_t)];
73 unsigned int h = cast_uint(time(NULL));
77 addbuff(buff, p, &lua_newstate);
78 lua_assert(p ==
sizeof(buff));
79 return luaS_hash(buff, p, h);
90 l_mem tb = gettotalbytes(g);
92 if (debt < tb - MAX_LMEM)
94 g->totalbytes = tb - debt;
99LUA_API
int lua_setcstacklimit (
lua_State *L,
unsigned int limit) {
100 UNUSED(L); UNUSED(limit);
101 return LUAI_MAXCCALLS;
107 lua_assert(L->ci->next == NULL);
109 lua_assert(L->ci->next == NULL);
111 ci->previous = L->ci;
126 while ((ci = next) != NULL) {
143 while ((next = ci->next) != NULL) {
151 next2->previous = ci;
166 if (getCcalls(L) == LUAI_MAXCCALLS)
167 luaG_runerror(L,
"C stack overflow");
168 else if (getCcalls(L) >= (LUAI_MAXCCALLS / 10 * 11))
169 luaD_throw(L, LUA_ERRERR);
173LUAI_FUNC
void luaE_incCstack (
lua_State *L) {
175 if (l_unlikely(getCcalls(L) >= LUAI_MAXCCALLS))
183 L1->stack.p = luaM_newvector(L, BASIC_STACK_SIZE + EXTRA_STACK,
StackValue);
184 L1->tbclist.p = L1->stack.p;
185 for (i = 0; i < BASIC_STACK_SIZE + EXTRA_STACK; i++)
186 setnilvalue(s2v(L1->stack.p + i));
187 L1->top.p = L1->stack.p;
188 L1->stack_last.p = L1->stack.p + BASIC_STACK_SIZE;
191 ci->next = ci->previous = NULL;
192 ci->callstatus = CIST_C;
193 ci->func.p = L1->top.p;
196 setnilvalue(s2v(L1->top.p));
198 ci->top.p = L1->top.p + LUA_MINSTACK;
204 if (L->stack.p == NULL)
208 lua_assert(L->nci == 0);
209 luaM_freearray(L, L->stack.p, stacksize(L) + EXTRA_STACK);
218 Table *registry = luaH_new(L);
219 sethvalue(L, &g->l_registry, registry);
220 luaH_resize(L, registry, LUA_RIDX_LAST, 0);
222 setthvalue(L, ®istry->array[LUA_RIDX_MAINTHREAD - 1], L);
224 sethvalue(L, ®istry->array[LUA_RIDX_GLOBALS - 1], luaH_new(L));
231static void f_luaopen (
lua_State *L,
void *ud) {
240 setnilvalue(&g->nilvalue);
241 luai_userstateopen(L);
259 L->basehookcount = 0;
271 if (!completestate(g))
272 luaC_freeallobjects(L);
275 luaD_closeprotected(L, 1, LUA_OK);
276 luaC_freeallobjects(L);
277 luai_userstateclose(L);
279 luaM_freearray(L, G(L)->strt.hash, G(L)->strt.size);
281 lua_assert(gettotalbytes(g) ==
sizeof(
LG));
282 (*g->frealloc)(g->ud, fromstate(L),
sizeof(
LG), 0);
293 o = luaC_newobjdt(L, LUA_TTHREAD,
sizeof(
LX), offsetof(
LX, l));
296 setthvalue2s(L, L->top.p, L1);
298 preinit_thread(L1, g);
299 L1->hookmask = L->hookmask;
300 L1->basehookcount = L->basehookcount;
304 memcpy(lua_getextraspace(L1), lua_getextraspace(g->mainthread),
306 luai_userstatethread(L, L1);
314 LX *l = fromstate(L1);
315 luaF_closeupval(L1, L1->stack.p);
316 lua_assert(L1->openupval == NULL);
317 luai_userstatefree(L, L1);
323int luaE_resetthread (
lua_State *L,
int status) {
325 setnilvalue(s2v(L->stack.p));
326 ci->func.p = L->stack.p;
327 ci->callstatus = CIST_C;
328 if (status == LUA_YIELD)
331 status = luaD_closeprotected(L, 1, status);
332 if (status != LUA_OK)
333 luaD_seterrorobj(L, status, L->stack.p + 1);
335 L->top.p = L->stack.p + 1;
336 ci->top.p = L->top.p + LUA_MINSTACK;
337 luaD_reallocstack(L, cast_int(ci->top.p - L->stack.p), 0);
345 L->nCcalls = (from) ? getCcalls(from) : 0;
346 status = luaE_resetthread(L, L->status);
355LUA_API
int lua_resetthread (
lua_State *L) {
356 return lua_closethread(L, NULL);
360LUA_API
lua_State *lua_newstate (lua_Alloc f,
void *ud) {
364 LG *l = cast(
LG *, (*f)(ud, NULL, LUA_TTHREAD,
sizeof(
LG)));
365 if (l == NULL)
return NULL;
369 g->currentwhite = bitmask(WHITE0BIT);
370 L->marked = luaC_white(g);
371 preinit_thread(L, g);
372 g->allgc = obj2gco(L);
380 g->seed = luai_makeseed(L);
382 g->strt.size = g->strt.nuse = 0;
384 setnilvalue(&g->l_registry);
386 g->gcstate = GCSpause;
390 g->finobj = g->tobefnz = g->fixedgc = NULL;
391 g->firstold1 = g->survival = g->old1 = g->reallyold = NULL;
392 g->finobjsur = g->finobjold1 = g->finobjrold = NULL;
394 g->gray = g->grayagain = NULL;
395 g->weak = g->ephemeron = g->allweak = NULL;
397 g->totalbytes =
sizeof(
LG);
400 setivalue(&g->nilvalue, 0);
401 setgcparam(g->gcpause, LUAI_GCPAUSE);
402 setgcparam(g->gcstepmul, LUAI_GCMUL);
403 g->gcstepsize = LUAI_GCSTEPSIZE;
404 setgcparam(g->genmajormul, LUAI_GENMAJORMUL);
405 g->genminormul = LUAI_GENMINORMUL;
406 for (i=0; i < LUA_NUMTAGS; i++) g->mt[i] = NULL;
407 if (luaD_rawrunprotected(L, f_luaopen, NULL) != LUA_OK) {
418 L = G(L)->mainthread;
423void luaE_warning (
lua_State *L,
const char *msg,
int tocont) {
424 lua_WarnFunction wf = G(L)->warnf;
426 wf(G(L)->ud_warn, msg, tocont);
433void luaE_warnerror (
lua_State *L,
const char *where) {
434 TValue *errobj = s2v(L->top.p - 1);
435 const char *msg = (ttisstring(errobj))
437 :
"error object is not a string";
439 luaE_warning(L,
"error in ", 1);
440 luaE_warning(L, where, 1);
441 luaE_warning(L,
" (", 1);
442 luaE_warning(L, msg, 1);
443 luaE_warning(L,
")", 0);