xref: /freebsd/contrib/lua/src/lstate.c (revision a9490b81b032b43cdb3c8c76b4d01bbad9ff82c1)
18e3e3a7aSWarner Losh /*
20495ed39SKyle Evans ** $Id: lstate.c $
38e3e3a7aSWarner Losh ** Global State
48e3e3a7aSWarner Losh ** See Copyright Notice in lua.h
58e3e3a7aSWarner Losh */
68e3e3a7aSWarner Losh 
78e3e3a7aSWarner Losh #define lstate_c
88e3e3a7aSWarner Losh #define LUA_CORE
98e3e3a7aSWarner Losh 
108e3e3a7aSWarner Losh #include "lprefix.h"
118e3e3a7aSWarner Losh 
128e3e3a7aSWarner Losh 
138e3e3a7aSWarner Losh #include <stddef.h>
148e3e3a7aSWarner Losh #include <string.h>
158e3e3a7aSWarner Losh 
168e3e3a7aSWarner Losh #include "lua.h"
178e3e3a7aSWarner Losh 
188e3e3a7aSWarner Losh #include "lapi.h"
198e3e3a7aSWarner Losh #include "ldebug.h"
208e3e3a7aSWarner Losh #include "ldo.h"
218e3e3a7aSWarner Losh #include "lfunc.h"
228e3e3a7aSWarner Losh #include "lgc.h"
238e3e3a7aSWarner Losh #include "llex.h"
248e3e3a7aSWarner Losh #include "lmem.h"
258e3e3a7aSWarner Losh #include "lstate.h"
268e3e3a7aSWarner Losh #include "lstring.h"
278e3e3a7aSWarner Losh #include "ltable.h"
288e3e3a7aSWarner Losh #include "ltm.h"
298e3e3a7aSWarner Losh 
308e3e3a7aSWarner Losh 
318e3e3a7aSWarner Losh 
328e3e3a7aSWarner Losh /*
338e3e3a7aSWarner Losh ** thread state + extra space
348e3e3a7aSWarner Losh */
358e3e3a7aSWarner Losh typedef struct LX {
368e3e3a7aSWarner Losh   lu_byte extra_[LUA_EXTRASPACE];
378e3e3a7aSWarner Losh   lua_State l;
388e3e3a7aSWarner Losh } LX;
398e3e3a7aSWarner Losh 
408e3e3a7aSWarner Losh 
418e3e3a7aSWarner Losh /*
428e3e3a7aSWarner Losh ** Main thread combines a thread state and the global state
438e3e3a7aSWarner Losh */
448e3e3a7aSWarner Losh typedef struct LG {
458e3e3a7aSWarner Losh   LX l;
468e3e3a7aSWarner Losh   global_State g;
478e3e3a7aSWarner Losh } LG;
488e3e3a7aSWarner Losh 
498e3e3a7aSWarner Losh 
508e3e3a7aSWarner Losh 
518e3e3a7aSWarner Losh #define fromstate(L)	(cast(LX *, cast(lu_byte *, (L)) - offsetof(LX, l)))
528e3e3a7aSWarner Losh 
538e3e3a7aSWarner Losh 
548e3e3a7aSWarner Losh /*
550495ed39SKyle Evans ** A macro to create a "random" seed when a state is created;
560495ed39SKyle Evans ** the seed is used to randomize string hashes.
570495ed39SKyle Evans */
580495ed39SKyle Evans #if !defined(luai_makeseed)
590495ed39SKyle Evans 
600495ed39SKyle Evans #include <time.h>
610495ed39SKyle Evans 
620495ed39SKyle Evans /*
630495ed39SKyle Evans ** Compute an initial seed with some level of randomness.
640495ed39SKyle Evans ** Rely on Address Space Layout Randomization (if present) and
650495ed39SKyle Evans ** current time.
668e3e3a7aSWarner Losh */
678e3e3a7aSWarner Losh #define addbuff(b,p,e) \
680495ed39SKyle Evans   { size_t t = cast_sizet(e); \
698e3e3a7aSWarner Losh     memcpy(b + p, &t, sizeof(t)); p += sizeof(t); }
708e3e3a7aSWarner Losh 
luai_makeseed(lua_State * L)710495ed39SKyle Evans static unsigned int luai_makeseed (lua_State *L) {
720495ed39SKyle Evans   char buff[3 * sizeof(size_t)];
730495ed39SKyle Evans   unsigned int h = cast_uint(time(NULL));
748e3e3a7aSWarner Losh   int p = 0;
758e3e3a7aSWarner Losh   addbuff(buff, p, L);  /* heap variable */
768e3e3a7aSWarner Losh   addbuff(buff, p, &h);  /* local variable */
778e3e3a7aSWarner Losh   addbuff(buff, p, &lua_newstate);  /* public function */
788e3e3a7aSWarner Losh   lua_assert(p == sizeof(buff));
798e3e3a7aSWarner Losh   return luaS_hash(buff, p, h);
808e3e3a7aSWarner Losh }
818e3e3a7aSWarner Losh 
820495ed39SKyle Evans #endif
830495ed39SKyle Evans 
848e3e3a7aSWarner Losh 
858e3e3a7aSWarner Losh /*
868e3e3a7aSWarner Losh ** set GCdebt to a new value keeping the value (totalbytes + GCdebt)
878e3e3a7aSWarner Losh ** invariant (and avoiding underflows in 'totalbytes')
888e3e3a7aSWarner Losh */
luaE_setdebt(global_State * g,l_mem debt)898e3e3a7aSWarner Losh void luaE_setdebt (global_State *g, l_mem debt) {
908e3e3a7aSWarner Losh   l_mem tb = gettotalbytes(g);
918e3e3a7aSWarner Losh   lua_assert(tb > 0);
928e3e3a7aSWarner Losh   if (debt < tb - MAX_LMEM)
938e3e3a7aSWarner Losh     debt = tb - MAX_LMEM;  /* will make 'totalbytes == MAX_LMEM' */
948e3e3a7aSWarner Losh   g->totalbytes = tb - debt;
958e3e3a7aSWarner Losh   g->GCdebt = debt;
968e3e3a7aSWarner Losh }
978e3e3a7aSWarner Losh 
988e3e3a7aSWarner Losh 
lua_setcstacklimit(lua_State * L,unsigned int limit)990495ed39SKyle Evans LUA_API int lua_setcstacklimit (lua_State *L, unsigned int limit) {
1000495ed39SKyle Evans   UNUSED(L); UNUSED(limit);
1010495ed39SKyle Evans   return LUAI_MAXCCALLS;  /* warning?? */
1020495ed39SKyle Evans }
1030495ed39SKyle Evans 
1040495ed39SKyle Evans 
luaE_extendCI(lua_State * L)1058e3e3a7aSWarner Losh CallInfo *luaE_extendCI (lua_State *L) {
1060495ed39SKyle Evans   CallInfo *ci;
1070495ed39SKyle Evans   lua_assert(L->ci->next == NULL);
1080495ed39SKyle Evans   ci = luaM_new(L, CallInfo);
1098e3e3a7aSWarner Losh   lua_assert(L->ci->next == NULL);
1108e3e3a7aSWarner Losh   L->ci->next = ci;
1118e3e3a7aSWarner Losh   ci->previous = L->ci;
1128e3e3a7aSWarner Losh   ci->next = NULL;
1130495ed39SKyle Evans   ci->u.l.trap = 0;
1148e3e3a7aSWarner Losh   L->nci++;
1158e3e3a7aSWarner Losh   return ci;
1168e3e3a7aSWarner Losh }
1178e3e3a7aSWarner Losh 
1188e3e3a7aSWarner Losh 
1198e3e3a7aSWarner Losh /*
1208e3e3a7aSWarner Losh ** free all CallInfo structures not in use by a thread
1218e3e3a7aSWarner Losh */
luaE_freeCI(lua_State * L)1228e3e3a7aSWarner Losh void luaE_freeCI (lua_State *L) {
1238e3e3a7aSWarner Losh   CallInfo *ci = L->ci;
1248e3e3a7aSWarner Losh   CallInfo *next = ci->next;
1258e3e3a7aSWarner Losh   ci->next = NULL;
1268e3e3a7aSWarner Losh   while ((ci = next) != NULL) {
1278e3e3a7aSWarner Losh     next = ci->next;
1288e3e3a7aSWarner Losh     luaM_free(L, ci);
1298e3e3a7aSWarner Losh     L->nci--;
1308e3e3a7aSWarner Losh   }
1318e3e3a7aSWarner Losh }
1328e3e3a7aSWarner Losh 
1338e3e3a7aSWarner Losh 
1348e3e3a7aSWarner Losh /*
1350495ed39SKyle Evans ** free half of the CallInfo structures not in use by a thread,
1360495ed39SKyle Evans ** keeping the first one.
1378e3e3a7aSWarner Losh */
luaE_shrinkCI(lua_State * L)1388e3e3a7aSWarner Losh void luaE_shrinkCI (lua_State *L) {
1390495ed39SKyle Evans   CallInfo *ci = L->ci->next;  /* first free CallInfo */
1400495ed39SKyle Evans   CallInfo *next;
1410495ed39SKyle Evans   if (ci == NULL)
1420495ed39SKyle Evans     return;  /* no extra elements */
1430495ed39SKyle Evans   while ((next = ci->next) != NULL) {  /* two extra elements? */
1440495ed39SKyle Evans     CallInfo *next2 = next->next;  /* next's next */
1450495ed39SKyle Evans     ci->next = next2;  /* remove next from the list */
1468e3e3a7aSWarner Losh     L->nci--;
1470495ed39SKyle Evans     luaM_free(L, next);  /* free next */
1480495ed39SKyle Evans     if (next2 == NULL)
1490495ed39SKyle Evans       break;  /* no more elements */
1500495ed39SKyle Evans     else {
1518e3e3a7aSWarner Losh       next2->previous = ci;
1520495ed39SKyle Evans       ci = next2;  /* continue */
1538e3e3a7aSWarner Losh     }
1548e3e3a7aSWarner Losh   }
1550495ed39SKyle Evans }
1560495ed39SKyle Evans 
1570495ed39SKyle Evans 
1580495ed39SKyle Evans /*
1590495ed39SKyle Evans ** Called when 'getCcalls(L)' larger or equal to LUAI_MAXCCALLS.
1600495ed39SKyle Evans ** If equal, raises an overflow error. If value is larger than
1610495ed39SKyle Evans ** LUAI_MAXCCALLS (which means it is handling an overflow) but
1620495ed39SKyle Evans ** not much larger, does not report an error (to allow overflow
1630495ed39SKyle Evans ** handling to work).
1640495ed39SKyle Evans */
luaE_checkcstack(lua_State * L)1650495ed39SKyle Evans void luaE_checkcstack (lua_State *L) {
1660495ed39SKyle Evans   if (getCcalls(L) == LUAI_MAXCCALLS)
1670495ed39SKyle Evans     luaG_runerror(L, "C stack overflow");
1680495ed39SKyle Evans   else if (getCcalls(L) >= (LUAI_MAXCCALLS / 10 * 11))
1698c784bb8SWarner Losh     luaD_throw(L, LUA_ERRERR);  /* error while handling stack error */
1700495ed39SKyle Evans }
1710495ed39SKyle Evans 
1720495ed39SKyle Evans 
luaE_incCstack(lua_State * L)1730495ed39SKyle Evans LUAI_FUNC void luaE_incCstack (lua_State *L) {
1740495ed39SKyle Evans   L->nCcalls++;
1758c784bb8SWarner Losh   if (l_unlikely(getCcalls(L) >= LUAI_MAXCCALLS))
1760495ed39SKyle Evans     luaE_checkcstack(L);
1770495ed39SKyle Evans }
1788e3e3a7aSWarner Losh 
1798e3e3a7aSWarner Losh 
stack_init(lua_State * L1,lua_State * L)1808e3e3a7aSWarner Losh static void stack_init (lua_State *L1, lua_State *L) {
1818e3e3a7aSWarner Losh   int i; CallInfo *ci;
1828e3e3a7aSWarner Losh   /* initialize stack array */
183*a9490b81SWarner Losh   L1->stack.p = luaM_newvector(L, BASIC_STACK_SIZE + EXTRA_STACK, StackValue);
184*a9490b81SWarner Losh   L1->tbclist.p = L1->stack.p;
1850495ed39SKyle Evans   for (i = 0; i < BASIC_STACK_SIZE + EXTRA_STACK; i++)
186*a9490b81SWarner Losh     setnilvalue(s2v(L1->stack.p + i));  /* erase new stack */
187*a9490b81SWarner Losh   L1->top.p = L1->stack.p;
188*a9490b81SWarner Losh   L1->stack_last.p = L1->stack.p + BASIC_STACK_SIZE;
1898e3e3a7aSWarner Losh   /* initialize first ci */
1908e3e3a7aSWarner Losh   ci = &L1->base_ci;
1918e3e3a7aSWarner Losh   ci->next = ci->previous = NULL;
1920495ed39SKyle Evans   ci->callstatus = CIST_C;
193*a9490b81SWarner Losh   ci->func.p = L1->top.p;
1940495ed39SKyle Evans   ci->u.c.k = NULL;
1950495ed39SKyle Evans   ci->nresults = 0;
196*a9490b81SWarner Losh   setnilvalue(s2v(L1->top.p));  /* 'function' entry for this 'ci' */
197*a9490b81SWarner Losh   L1->top.p++;
198*a9490b81SWarner Losh   ci->top.p = L1->top.p + LUA_MINSTACK;
1998e3e3a7aSWarner Losh   L1->ci = ci;
2008e3e3a7aSWarner Losh }
2018e3e3a7aSWarner Losh 
2028e3e3a7aSWarner Losh 
freestack(lua_State * L)2038e3e3a7aSWarner Losh static void freestack (lua_State *L) {
204*a9490b81SWarner Losh   if (L->stack.p == NULL)
2058e3e3a7aSWarner Losh     return;  /* stack not completely built yet */
2068e3e3a7aSWarner Losh   L->ci = &L->base_ci;  /* free the entire 'ci' list */
2078e3e3a7aSWarner Losh   luaE_freeCI(L);
2088e3e3a7aSWarner Losh   lua_assert(L->nci == 0);
209*a9490b81SWarner Losh   luaM_freearray(L, L->stack.p, stacksize(L) + EXTRA_STACK);  /* free stack */
2108e3e3a7aSWarner Losh }
2118e3e3a7aSWarner Losh 
2128e3e3a7aSWarner Losh 
2138e3e3a7aSWarner Losh /*
2148e3e3a7aSWarner Losh ** Create registry table and its predefined values
2158e3e3a7aSWarner Losh */
init_registry(lua_State * L,global_State * g)2168e3e3a7aSWarner Losh static void init_registry (lua_State *L, global_State *g) {
2178e3e3a7aSWarner Losh   /* create registry */
2188e3e3a7aSWarner Losh   Table *registry = luaH_new(L);
2198e3e3a7aSWarner Losh   sethvalue(L, &g->l_registry, registry);
2208e3e3a7aSWarner Losh   luaH_resize(L, registry, LUA_RIDX_LAST, 0);
2218e3e3a7aSWarner Losh   /* registry[LUA_RIDX_MAINTHREAD] = L */
2228c784bb8SWarner Losh   setthvalue(L, &registry->array[LUA_RIDX_MAINTHREAD - 1], L);
2238c784bb8SWarner Losh   /* registry[LUA_RIDX_GLOBALS] = new table (table of globals) */
2248c784bb8SWarner Losh   sethvalue(L, &registry->array[LUA_RIDX_GLOBALS - 1], luaH_new(L));
2258e3e3a7aSWarner Losh }
2268e3e3a7aSWarner Losh 
2278e3e3a7aSWarner Losh 
2288e3e3a7aSWarner Losh /*
2298e3e3a7aSWarner Losh ** open parts of the state that may cause memory-allocation errors.
2308e3e3a7aSWarner Losh */
f_luaopen(lua_State * L,void * ud)2318e3e3a7aSWarner Losh static void f_luaopen (lua_State *L, void *ud) {
2328e3e3a7aSWarner Losh   global_State *g = G(L);
2338e3e3a7aSWarner Losh   UNUSED(ud);
2348e3e3a7aSWarner Losh   stack_init(L, L);  /* init stack */
2358e3e3a7aSWarner Losh   init_registry(L, g);
2368e3e3a7aSWarner Losh   luaS_init(L);
2378e3e3a7aSWarner Losh   luaT_init(L);
2388e3e3a7aSWarner Losh   luaX_init(L);
2398c784bb8SWarner Losh   g->gcstp = 0;  /* allow gc */
2408c784bb8SWarner Losh   setnilvalue(&g->nilvalue);  /* now state is complete */
2418e3e3a7aSWarner Losh   luai_userstateopen(L);
2428e3e3a7aSWarner Losh }
2438e3e3a7aSWarner Losh 
2448e3e3a7aSWarner Losh 
2458e3e3a7aSWarner Losh /*
2468e3e3a7aSWarner Losh ** preinitialize a thread with consistent values without allocating
2478e3e3a7aSWarner Losh ** any memory (to avoid errors)
2488e3e3a7aSWarner Losh */
preinit_thread(lua_State * L,global_State * g)2498e3e3a7aSWarner Losh static void preinit_thread (lua_State *L, global_State *g) {
2508e3e3a7aSWarner Losh   G(L) = g;
251*a9490b81SWarner Losh   L->stack.p = NULL;
2528e3e3a7aSWarner Losh   L->ci = NULL;
2538e3e3a7aSWarner Losh   L->nci = 0;
2548e3e3a7aSWarner Losh   L->twups = L;  /* thread has no upvalues */
2558c784bb8SWarner Losh   L->nCcalls = 0;
2568e3e3a7aSWarner Losh   L->errorJmp = NULL;
2578e3e3a7aSWarner Losh   L->hook = NULL;
2588e3e3a7aSWarner Losh   L->hookmask = 0;
2598e3e3a7aSWarner Losh   L->basehookcount = 0;
2608e3e3a7aSWarner Losh   L->allowhook = 1;
2618e3e3a7aSWarner Losh   resethookcount(L);
2628e3e3a7aSWarner Losh   L->openupval = NULL;
2638e3e3a7aSWarner Losh   L->status = LUA_OK;
2648e3e3a7aSWarner Losh   L->errfunc = 0;
2650495ed39SKyle Evans   L->oldpc = 0;
2668e3e3a7aSWarner Losh }
2678e3e3a7aSWarner Losh 
2688e3e3a7aSWarner Losh 
close_state(lua_State * L)2698e3e3a7aSWarner Losh static void close_state (lua_State *L) {
2708e3e3a7aSWarner Losh   global_State *g = G(L);
2718c784bb8SWarner Losh   if (!completestate(g))  /* closing a partially built state? */
2728c784bb8SWarner Losh     luaC_freeallobjects(L);  /* just collect its objects */
2738c784bb8SWarner Losh   else {  /* closing a fully built state */
2748c784bb8SWarner Losh     L->ci = &L->base_ci;  /* unwind CallInfo list */
2758c784bb8SWarner Losh     luaD_closeprotected(L, 1, LUA_OK);  /* close all upvalues */
2768e3e3a7aSWarner Losh     luaC_freeallobjects(L);  /* collect all objects */
2778e3e3a7aSWarner Losh     luai_userstateclose(L);
2788c784bb8SWarner Losh   }
2798e3e3a7aSWarner Losh   luaM_freearray(L, G(L)->strt.hash, G(L)->strt.size);
2808e3e3a7aSWarner Losh   freestack(L);
2818e3e3a7aSWarner Losh   lua_assert(gettotalbytes(g) == sizeof(LG));
2828e3e3a7aSWarner Losh   (*g->frealloc)(g->ud, fromstate(L), sizeof(LG), 0);  /* free main block */
2838e3e3a7aSWarner Losh }
2848e3e3a7aSWarner Losh 
2858e3e3a7aSWarner Losh 
lua_newthread(lua_State * L)2868e3e3a7aSWarner Losh LUA_API lua_State *lua_newthread (lua_State *L) {
287*a9490b81SWarner Losh   global_State *g = G(L);
288*a9490b81SWarner Losh   GCObject *o;
2898e3e3a7aSWarner Losh   lua_State *L1;
2908e3e3a7aSWarner Losh   lua_lock(L);
2918e3e3a7aSWarner Losh   luaC_checkGC(L);
2928e3e3a7aSWarner Losh   /* create new thread */
293*a9490b81SWarner Losh   o = luaC_newobjdt(L, LUA_TTHREAD, sizeof(LX), offsetof(LX, l));
294*a9490b81SWarner Losh   L1 = gco2th(o);
2958e3e3a7aSWarner Losh   /* anchor it on L stack */
296*a9490b81SWarner Losh   setthvalue2s(L, L->top.p, L1);
2978e3e3a7aSWarner Losh   api_incr_top(L);
2988e3e3a7aSWarner Losh   preinit_thread(L1, g);
2998e3e3a7aSWarner Losh   L1->hookmask = L->hookmask;
3008e3e3a7aSWarner Losh   L1->basehookcount = L->basehookcount;
3018e3e3a7aSWarner Losh   L1->hook = L->hook;
3028e3e3a7aSWarner Losh   resethookcount(L1);
3038e3e3a7aSWarner Losh   /* initialize L1 extra space */
3048e3e3a7aSWarner Losh   memcpy(lua_getextraspace(L1), lua_getextraspace(g->mainthread),
3058e3e3a7aSWarner Losh          LUA_EXTRASPACE);
3068e3e3a7aSWarner Losh   luai_userstatethread(L, L1);
3078e3e3a7aSWarner Losh   stack_init(L1, L);  /* init stack */
3088e3e3a7aSWarner Losh   lua_unlock(L);
3098e3e3a7aSWarner Losh   return L1;
3108e3e3a7aSWarner Losh }
3118e3e3a7aSWarner Losh 
3128e3e3a7aSWarner Losh 
luaE_freethread(lua_State * L,lua_State * L1)3138e3e3a7aSWarner Losh void luaE_freethread (lua_State *L, lua_State *L1) {
3148e3e3a7aSWarner Losh   LX *l = fromstate(L1);
315*a9490b81SWarner Losh   luaF_closeupval(L1, L1->stack.p);  /* close all upvalues */
3168e3e3a7aSWarner Losh   lua_assert(L1->openupval == NULL);
3178e3e3a7aSWarner Losh   luai_userstatefree(L, L1);
3188e3e3a7aSWarner Losh   freestack(L1);
3198e3e3a7aSWarner Losh   luaM_free(L, l);
3208e3e3a7aSWarner Losh }
3218e3e3a7aSWarner Losh 
3228e3e3a7aSWarner Losh 
luaE_resetthread(lua_State * L,int status)3238c784bb8SWarner Losh int luaE_resetthread (lua_State *L, int status) {
3248c784bb8SWarner Losh   CallInfo *ci = L->ci = &L->base_ci;  /* unwind CallInfo list */
325*a9490b81SWarner Losh   setnilvalue(s2v(L->stack.p));  /* 'function' entry for basic 'ci' */
326*a9490b81SWarner Losh   ci->func.p = L->stack.p;
3270495ed39SKyle Evans   ci->callstatus = CIST_C;
3288c784bb8SWarner Losh   if (status == LUA_YIELD)
3290495ed39SKyle Evans     status = LUA_OK;
3308c784bb8SWarner Losh   L->status = LUA_OK;  /* so it can run __close metamethods */
3318c784bb8SWarner Losh   status = luaD_closeprotected(L, 1, status);
3328c784bb8SWarner Losh   if (status != LUA_OK)  /* errors? */
333*a9490b81SWarner Losh     luaD_seterrorobj(L, status, L->stack.p + 1);
3348c784bb8SWarner Losh   else
335*a9490b81SWarner Losh     L->top.p = L->stack.p + 1;
336*a9490b81SWarner Losh   ci->top.p = L->top.p + LUA_MINSTACK;
337*a9490b81SWarner Losh   luaD_reallocstack(L, cast_int(ci->top.p - L->stack.p), 0);
3388c784bb8SWarner Losh   return status;
3398c784bb8SWarner Losh }
3408c784bb8SWarner Losh 
3418c784bb8SWarner Losh 
lua_closethread(lua_State * L,lua_State * from)342*a9490b81SWarner Losh LUA_API int lua_closethread (lua_State *L, lua_State *from) {
3438c784bb8SWarner Losh   int status;
3448c784bb8SWarner Losh   lua_lock(L);
345*a9490b81SWarner Losh   L->nCcalls = (from) ? getCcalls(from) : 0;
3468c784bb8SWarner Losh   status = luaE_resetthread(L, L->status);
3470495ed39SKyle Evans   lua_unlock(L);
3480495ed39SKyle Evans   return status;
3490495ed39SKyle Evans }
3500495ed39SKyle Evans 
3510495ed39SKyle Evans 
352*a9490b81SWarner Losh /*
353*a9490b81SWarner Losh ** Deprecated! Use 'lua_closethread' instead.
354*a9490b81SWarner Losh */
lua_resetthread(lua_State * L)355*a9490b81SWarner Losh LUA_API int lua_resetthread (lua_State *L) {
356*a9490b81SWarner Losh   return lua_closethread(L, NULL);
357*a9490b81SWarner Losh }
358*a9490b81SWarner Losh 
359*a9490b81SWarner Losh 
lua_newstate(lua_Alloc f,void * ud)3608e3e3a7aSWarner Losh LUA_API lua_State *lua_newstate (lua_Alloc f, void *ud) {
3618e3e3a7aSWarner Losh   int i;
3628e3e3a7aSWarner Losh   lua_State *L;
3638e3e3a7aSWarner Losh   global_State *g;
3648e3e3a7aSWarner Losh   LG *l = cast(LG *, (*f)(ud, NULL, LUA_TTHREAD, sizeof(LG)));
3658e3e3a7aSWarner Losh   if (l == NULL) return NULL;
3668e3e3a7aSWarner Losh   L = &l->l.l;
3678e3e3a7aSWarner Losh   g = &l->g;
3680495ed39SKyle Evans   L->tt = LUA_VTHREAD;
3698e3e3a7aSWarner Losh   g->currentwhite = bitmask(WHITE0BIT);
3708e3e3a7aSWarner Losh   L->marked = luaC_white(g);
3718e3e3a7aSWarner Losh   preinit_thread(L, g);
3720495ed39SKyle Evans   g->allgc = obj2gco(L);  /* by now, only object is the main thread */
3730495ed39SKyle Evans   L->next = NULL;
3740495ed39SKyle Evans   incnny(L);  /* main thread is always non yieldable */
3758e3e3a7aSWarner Losh   g->frealloc = f;
3768e3e3a7aSWarner Losh   g->ud = ud;
3770495ed39SKyle Evans   g->warnf = NULL;
3780495ed39SKyle Evans   g->ud_warn = NULL;
3798e3e3a7aSWarner Losh   g->mainthread = L;
3800495ed39SKyle Evans   g->seed = luai_makeseed(L);
3818c784bb8SWarner Losh   g->gcstp = GCSTPGC;  /* no GC while building state */
3828e3e3a7aSWarner Losh   g->strt.size = g->strt.nuse = 0;
3838e3e3a7aSWarner Losh   g->strt.hash = NULL;
3848e3e3a7aSWarner Losh   setnilvalue(&g->l_registry);
3858e3e3a7aSWarner Losh   g->panic = NULL;
3868e3e3a7aSWarner Losh   g->gcstate = GCSpause;
3870495ed39SKyle Evans   g->gckind = KGC_INC;
3888c784bb8SWarner Losh   g->gcstopem = 0;
3890495ed39SKyle Evans   g->gcemergency = 0;
3900495ed39SKyle Evans   g->finobj = g->tobefnz = g->fixedgc = NULL;
3910495ed39SKyle Evans   g->firstold1 = g->survival = g->old1 = g->reallyold = NULL;
3920495ed39SKyle Evans   g->finobjsur = g->finobjold1 = g->finobjrold = NULL;
3938e3e3a7aSWarner Losh   g->sweepgc = NULL;
3948e3e3a7aSWarner Losh   g->gray = g->grayagain = NULL;
3958e3e3a7aSWarner Losh   g->weak = g->ephemeron = g->allweak = NULL;
3968e3e3a7aSWarner Losh   g->twups = NULL;
3978e3e3a7aSWarner Losh   g->totalbytes = sizeof(LG);
3988e3e3a7aSWarner Losh   g->GCdebt = 0;
3990495ed39SKyle Evans   g->lastatomic = 0;
4000495ed39SKyle Evans   setivalue(&g->nilvalue, 0);  /* to signal that state is not yet built */
4010495ed39SKyle Evans   setgcparam(g->gcpause, LUAI_GCPAUSE);
4020495ed39SKyle Evans   setgcparam(g->gcstepmul, LUAI_GCMUL);
4030495ed39SKyle Evans   g->gcstepsize = LUAI_GCSTEPSIZE;
4040495ed39SKyle Evans   setgcparam(g->genmajormul, LUAI_GENMAJORMUL);
4050495ed39SKyle Evans   g->genminormul = LUAI_GENMINORMUL;
4068e3e3a7aSWarner Losh   for (i=0; i < LUA_NUMTAGS; i++) g->mt[i] = NULL;
4078e3e3a7aSWarner Losh   if (luaD_rawrunprotected(L, f_luaopen, NULL) != LUA_OK) {
4088e3e3a7aSWarner Losh     /* memory allocation error: free partial state */
4098e3e3a7aSWarner Losh     close_state(L);
4108e3e3a7aSWarner Losh     L = NULL;
4118e3e3a7aSWarner Losh   }
4128e3e3a7aSWarner Losh   return L;
4138e3e3a7aSWarner Losh }
4148e3e3a7aSWarner Losh 
4158e3e3a7aSWarner Losh 
lua_close(lua_State * L)4168e3e3a7aSWarner Losh LUA_API void lua_close (lua_State *L) {
4178e3e3a7aSWarner Losh   lua_lock(L);
4180495ed39SKyle Evans   L = G(L)->mainthread;  /* only the main thread can be closed */
4198e3e3a7aSWarner Losh   close_state(L);
4208e3e3a7aSWarner Losh }
4218e3e3a7aSWarner Losh 
4228e3e3a7aSWarner Losh 
luaE_warning(lua_State * L,const char * msg,int tocont)4230495ed39SKyle Evans void luaE_warning (lua_State *L, const char *msg, int tocont) {
4240495ed39SKyle Evans   lua_WarnFunction wf = G(L)->warnf;
4250495ed39SKyle Evans   if (wf != NULL)
4260495ed39SKyle Evans     wf(G(L)->ud_warn, msg, tocont);
4270495ed39SKyle Evans }
4280495ed39SKyle Evans 
4290495ed39SKyle Evans 
4300495ed39SKyle Evans /*
4310495ed39SKyle Evans ** Generate a warning from an error message
4320495ed39SKyle Evans */
luaE_warnerror(lua_State * L,const char * where)4330495ed39SKyle Evans void luaE_warnerror (lua_State *L, const char *where) {
434*a9490b81SWarner Losh   TValue *errobj = s2v(L->top.p - 1);  /* error object */
4350495ed39SKyle Evans   const char *msg = (ttisstring(errobj))
4360495ed39SKyle Evans                   ? svalue(errobj)
4370495ed39SKyle Evans                   : "error object is not a string";
4380495ed39SKyle Evans   /* produce warning "error in %s (%s)" (where, msg) */
4390495ed39SKyle Evans   luaE_warning(L, "error in ", 1);
4400495ed39SKyle Evans   luaE_warning(L, where, 1);
4410495ed39SKyle Evans   luaE_warning(L, " (", 1);
4420495ed39SKyle Evans   luaE_warning(L, msg, 1);
4430495ed39SKyle Evans   luaE_warning(L, ")", 0);
4440495ed39SKyle Evans }
4450495ed39SKyle Evans 
446