Lua 5.1.4: lstate.c
L0001
L0006
L0007
L0008 #include <stddef.h>
L0009
L0010 #define lstate_c
L0011 #define LUA_CORE
L0012
L0013 #include "lua.h"
L0014
L0015 #include "ldebug.h"
L0016 #include "ldo.h"
L0017 #include "lfunc.h"
L0018 #include "lgc.h"
L0019 #include "llex.h"
L0020 #include "lmem.h"
L0021 #include "lstate.h"
L0022 #include "lstring.h"
L0023 #include "ltable.h"
L0024 #include "ltm.h"
L0025
L0026
L0027 #define state_size(x) (sizeof(x) + LUAI_EXTRASPACE)
L0028 #define fromstate(l) (cast(lu_byte *, (l)) - LUAI_EXTRASPACE)
L0029 #define tostate(l) (cast(lua_State *, cast(lu_byte *, l) + LUAI_EXTRASPACE))
L0030
L0031
L0032
L0035 typedef struct LG {
L0036 lua_State l;
L0037 global_State g;
L0038 } LG;
L0039
L0040
L0041
L0042 static void stack_init (lua_State *L1, lua_State *L) {
L0043
L0044 L1->base_ci = luaM_newvector(L, BASIC_CI_SIZE, CallInfo);
L0045 L1->ci = L1->base_ci;
L0046 L1->size_ci = BASIC_CI_SIZE;
L0047 L1->end_ci = L1->base_ci + L1->size_ci - 1;
L0048
L0049 L1->stack = luaM_newvector(L, BASIC_STACK_SIZE + EXTRA_STACK, TValue);
L0050 L1->stacksize = BASIC_STACK_SIZE + EXTRA_STACK;
L0051 L1->top = L1->stack;
L0052 L1->stack_last = L1->stack+(L1->stacksize - EXTRA_STACK)-1;
L0053
L0054 L1->ci->func = L1->top;
L0055 setnilvalue(L1->top++);
L0056 L1->base = L1->ci->base = L1->top;
L0057 L1->ci->top = L1->top + LUA_MINSTACK;
L0058 }
L0059
L0060
L0061 static void freestack (lua_State *L, lua_State *L1) {
L0062 luaM_freearray(L, L1->base_ci, L1->size_ci, CallInfo);
L0063 luaM_freearray(L, L1->stack, L1->stacksize, TValue);
L0064 }
L0065
L0066
L0067
L0070 static void f_luaopen (lua_State *L, void *ud) {
L0071 global_State *g = G(L);
L0072 UNUSED(ud);
L0073 stack_init(L, L);
L0074 sethvalue(L, gt(L), luaH_new(L, 0, 2));
L0075 sethvalue(L, registry(L), luaH_new(L, 0, 2));
L0076 luaS_resize(L, MINSTRTABSIZE);
L0077 luaT_init(L);
L0078 luaX_init(L);
L0079 luaS_fix(luaS_newliteral(L, MEMERRMSG));
L0080 g->GCthreshold = 4*g->totalbytes;
L0081 }
L0082
L0083
L0084 static void preinit_state (lua_State *L, global_State *g) {
L0085 G(L) = g;
L0086 L->stack = NULL;
L0087 L->stacksize = 0;
L0088 L->errorJmp = NULL;
L0089 L->hook = NULL;
L0090 L->hookmask = 0;
L0091 L->basehookcount = 0;
L0092 L->allowhook = 1;
L0093 resethookcount(L);
L0094 L->openupval = NULL;
L0095 L->size_ci = 0;
L0096 L->nCcalls = L->baseCcalls = 0;
L0097 L->status = 0;
L0098 L->base_ci = L->ci = NULL;
L0099 L->savedpc = NULL;
L0100 L->errfunc = 0;
L0101 setnilvalue(gt(L));
L0102 }
L0103
L0104
L0105 static void close_state (lua_State *L) {
L0106 global_State *g = G(L);
L0107 luaF_close(L, L->stack);
L0108 luaC_freeall(L);
L0109 lua_assert(g->rootgc == obj2gco(L));
L0110 lua_assert(g->strt.nuse == 0);
L0111 luaM_freearray(L, G(L)->strt.hash, G(L)->strt.size, TString *);
L0112 luaZ_freebuffer(L, &g->buff);
L0113 freestack(L, L);
L0114 lua_assert(g->totalbytes == sizeof(LG));
L0115 (*g->frealloc)(g->ud, fromstate(L), state_size(LG), 0);
L0116 }
L0117
L0118
L0119 lua_State *luaE_newthread (lua_State *L) {
L0120 lua_State *L1 = tostate(luaM_malloc(L, state_size(lua_State)));
L0121 luaC_link(L, obj2gco(L1), LUA_TTHREAD);
L0122 preinit_state(L1, G(L));
L0123 stack_init(L1, L);
L0124 setobj2n(L, gt(L1), gt(L));
L0125 L1->hookmask = L->hookmask;
L0126 L1->basehookcount = L->basehookcount;
L0127 L1->hook = L->hook;
L0128 resethookcount(L1);
L0129 lua_assert(iswhite(obj2gco(L1)));
L0130 return L1;
L0131 }
L0132
L0133
L0134 void luaE_freethread (lua_State *L, lua_State *L1) {
L0135 luaF_close(L1, L1->stack);
L0136 lua_assert(L1->openupval == NULL);
L0137 luai_userstatefree(L1);
L0138 freestack(L, L1);
L0139 luaM_freemem(L, fromstate(L1), state_size(lua_State));
L0140 }
L0141
L0142
L0143 LUA_API lua_State *lua_newstate (lua_Alloc f, void *ud) {
L0144 int i;
L0145 lua_State *L;
L0146 global_State *g;
L0147 void *l = (*f)(ud, NULL, 0, state_size(LG));
L0148 if (l == NULL) return NULL;
L0149 L = tostate(l);
L0150 g = &((LG *)L)->g;
L0151 L->next = NULL;
L0152 L->tt = LUA_TTHREAD;
L0153 g->currentwhite = bit2mask(WHITE0BIT, FIXEDBIT);
L0154 L->marked = luaC_white(g);
L0155 set2bits(L->marked, FIXEDBIT, SFIXEDBIT);
L0156 preinit_state(L, g);
L0157 g->frealloc = f;
L0158 g->ud = ud;
L0159 g->mainthread = L;
L0160 g->uvhead.u.l.prev = &g->uvhead;
L0161 g->uvhead.u.l.next = &g->uvhead;
L0162 g->GCthreshold = 0;
L0163 g->strt.size = 0;
L0164 g->strt.nuse = 0;
L0165 g->strt.hash = NULL;
L0166 setnilvalue(registry(L));
L0167 luaZ_initbuffer(L, &g->buff);
L0168 g->panic = NULL;
L0169 g->gcstate = GCSpause;
L0170 g->rootgc = obj2gco(L);
L0171 g->sweepstrgc = 0;
L0172 g->sweepgc = &g->rootgc;
L0173 g->gray = NULL;
L0174 g->grayagain = NULL;
L0175 g->weak = NULL;
L0176 g->tmudata = NULL;
L0177 g->totalbytes = sizeof(LG);
L0178 g->gcpause = LUAI_GCPAUSE;
L0179 g->gcstepmul = LUAI_GCMUL;
L0180 g->gcdept = 0;
L0181 for (i=0; i<NUM_TAGS; i++) g->mt[i] = NULL;
L0182 if (luaD_rawrunprotected(L, f_luaopen, NULL) != 0) {
L0183
L0184 close_state(L);
L0185 L = NULL;
L0186 }
L0187 else
L0188 luai_userstateopen(L);
L0189 return L;
L0190 }
L0191
L0192
L0193 static void callallgcTM (lua_State *L, void *ud) {
L0194 UNUSED(ud);
L0195 luaC_callGCTM(L);
L0196 }
L0197
L0198
L0199 LUA_API void lua_close (lua_State *L) {
L0200 L = G(L)->mainthread;
L0201 lua_lock(L);
L0202 luaF_close(L, L->stack);
L0203 luaC_separateudata(L, 1);
L0204 L->errfunc = 0;
L0205 do {
L0206 L->ci = L->base_ci;
L0207 L->base = L->top = L->ci->base;
L0208 L->nCcalls = L->baseCcalls = 0;
L0209 } while (luaD_rawrunprotected(L, callallgcTM, NULL) != 0);
L0210 lua_assert(G(L)->tmudata == NULL);
L0211 luai_userstateclose(L);
L0212 close_state(L);
L0213 }
L0214
Generated by pretty.lua