1 /*
2 ** $Id$
3 ** Global State
4 ** See Copyright Notice in lua.h
5 */
6 
7 
8 #define lstate_c
9 #define LUA_CORE
10 
11 #include "lua.h"
12 
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 
24 
25 #define state_size(x)	(sizeof(x) + LUAI_EXTRASPACE)
26 #define fromstate(l)	(cast(lu_byte *, (l)) - LUAI_EXTRASPACE)
27 #define tostate(l)   (cast(lua_State *, cast(lu_byte *, l) + LUAI_EXTRASPACE))
28 
29 
30 /*
31 ** Main thread combines a thread state and the global state
32 */
33 typedef struct LG {
34   lua_State l;
35   global_State g;
36 } LG;
37 
38 
39 
stack_init(lua_State * L1,lua_State * L)40 static void stack_init (lua_State *L1, lua_State *L) {
41   /* initialize CallInfo array */
42   L1->base_ci = luaM_newvector(L, BASIC_CI_SIZE, CallInfo);
43   L1->ci = L1->base_ci;
44   L1->size_ci = BASIC_CI_SIZE;
45   L1->end_ci = L1->base_ci + L1->size_ci - 1;
46   /* initialize stack array */
47   L1->stack = luaM_newvector(L, BASIC_STACK_SIZE + EXTRA_STACK, TValue);
48   L1->stacksize = BASIC_STACK_SIZE + EXTRA_STACK;
49   L1->top = L1->stack;
50   L1->stack_last = L1->stack+(L1->stacksize - EXTRA_STACK)-1;
51   /* initialize first ci */
52   L1->ci->func = L1->top;
53   setnilvalue(L1->top++);  /* `function' entry for this `ci' */
54   L1->base = L1->ci->base = L1->top;
55   L1->ci->top = L1->top + LUA_MINSTACK;
56 }
57 
58 
freestack(lua_State * L,lua_State * L1)59 static void freestack (lua_State *L, lua_State *L1) {
60   luaM_freearray(L, L1->base_ci, L1->size_ci, CallInfo);
61   luaM_freearray(L, L1->stack, L1->stacksize, TValue);
62 }
63 
64 
65 /*
66 ** open parts that may cause memory-allocation errors
67 */
f_luaopen(lua_State * L,void * ud)68 static void f_luaopen (lua_State *L, void *ud) {
69   global_State *g = G(L);
70   UNUSED(ud);
71   stack_init(L, L);  /* init stack */
72   sethvalue(L, gt(L), luaH_new(L, 0, 2));  /* table of globals */
73   sethvalue(L, registry(L), luaH_new(L, 0, 2));  /* registry */
74   luaS_resize(L, MINSTRTABSIZE);  /* initial size of string table */
75   luaT_init(L);
76   luaX_init(L);
77   luaS_fix(luaS_newliteral(L, MEMERRMSG));
78   g->GCthreshold = 4*g->totalbytes;
79 }
80 
81 
preinit_state(lua_State * L,global_State * g)82 static void preinit_state (lua_State *L, global_State *g) {
83   G(L) = g;
84   L->stack = NULL;
85   L->stacksize = 0;
86   L->errorJmp = NULL;
87   L->hook = NULL;
88   L->hookmask = 0;
89   L->basehookcount = 0;
90   L->allowhook = 1;
91   resethookcount(L);
92   L->openupval = NULL;
93   L->size_ci = 0;
94   L->nCcalls = L->baseCcalls = 0;
95   L->status = 0;
96   L->base_ci = L->ci = NULL;
97   L->savedpc = NULL;
98   L->errfunc = 0;
99   setnilvalue(gt(L));
100 }
101 
102 
close_state(lua_State * L)103 static void close_state (lua_State *L) {
104   global_State *g = G(L);
105   luaF_close(L, L->stack);  /* close all upvalues for this thread */
106   luaC_freeall(L);  /* collect all objects */
107   lua_assert(g->rootgc == obj2gco(L));
108   lua_assert(g->strt.nuse == 0);
109   luaM_freearray(L, G(L)->strt.hash, G(L)->strt.size, TString *);
110   luaZ_freebuffer(L, &g->buff);
111   freestack(L, L);
112   lua_assert(g->totalbytes == sizeof(LG));
113   (*g->frealloc)(g->ud, fromstate(L), state_size(LG), 0);
114 }
115 
116 
luaE_newthread(lua_State * L)117 lua_State *luaE_newthread (lua_State *L) {
118   lua_State *L1 = tostate(luaM_malloc(L, state_size(lua_State)));
119   luaC_link(L, obj2gco(L1), LUA_TTHREAD);
120   preinit_state(L1, G(L));
121   stack_init(L1, L);  /* init stack */
122   setobj2n(L, gt(L1), gt(L));  /* share table of globals */
123   L1->hookmask = L->hookmask;
124   L1->basehookcount = L->basehookcount;
125   L1->hook = L->hook;
126   resethookcount(L1);
127   lua_assert(iswhite(obj2gco(L1)));
128   return L1;
129 }
130 
131 
luaE_freethread(lua_State * L,lua_State * L1)132 void luaE_freethread (lua_State *L, lua_State *L1) {
133   luaF_close(L1, L1->stack);  /* close all upvalues for this thread */
134   lua_assert(L1->openupval == NULL);
135   luai_userstatefree(L1);
136   freestack(L, L1);
137   luaM_freemem(L, fromstate(L1), state_size(lua_State));
138 }
139 
140 
lua_newstate(lua_Alloc f,void * ud)141 LUA_API lua_State *lua_newstate (lua_Alloc f, void *ud) {
142   int i;
143   lua_State *L;
144   global_State *g;
145   void *l = (*f)(ud, NULL, 0, state_size(LG));
146   if (l == NULL) return NULL;
147   L = tostate(l);
148   g = &((LG *)L)->g;
149   L->next = NULL;
150   L->tt = LUA_TTHREAD;
151   g->currentwhite = bit2mask(WHITE0BIT, FIXEDBIT);
152   L->marked = luaC_white(g);
153   set2bits(L->marked, FIXEDBIT, SFIXEDBIT);
154   preinit_state(L, g);
155   g->frealloc = f;
156   g->ud = ud;
157   g->mainthread = L;
158   g->uvhead.u.l.prev = &g->uvhead;
159   g->uvhead.u.l.next = &g->uvhead;
160   g->GCthreshold = 0;  /* mark it as unfinished state */
161   g->strt.size = 0;
162   g->strt.nuse = 0;
163   g->strt.hash = NULL;
164   setnilvalue(registry(L));
165   luaZ_initbuffer(L, &g->buff);
166   g->panic = NULL;
167   g->gcstate = GCSpause;
168   g->rootgc = obj2gco(L);
169   g->sweepstrgc = 0;
170   g->sweepgc = &g->rootgc;
171   g->gray = NULL;
172   g->grayagain = NULL;
173   g->weak = NULL;
174   g->tmudata = NULL;
175   g->totalbytes = sizeof(LG);
176   g->gcpause = LUAI_GCPAUSE;
177   g->gcstepmul = LUAI_GCMUL;
178   g->gcdept = 0;
179   for (i=0; i<NUM_TAGS; i++) g->mt[i] = NULL;
180   if (luaD_rawrunprotected(L, f_luaopen, NULL) != 0) {
181     /* memory allocation error: free partial state */
182     close_state(L);
183     L = NULL;
184   }
185   else
186     luai_userstateopen(L);
187   return L;
188 }
189 
190 
callallgcTM(lua_State * L,void * ud)191 static void callallgcTM (lua_State *L, void *ud) {
192   UNUSED(ud);
193   luaC_callGCTM(L);  /* call GC metamethods for all udata */
194 }
195 
196 
lua_close(lua_State * L)197 LUA_API void lua_close (lua_State *L) {
198   L = G(L)->mainthread;  /* only the main thread can be closed */
199   lua_lock(L);
200   luaF_close(L, L->stack);  /* close all upvalues for this thread */
201   luaC_separateudata(L, 1);  /* separate udata that have GC metamethods */
202   L->errfunc = 0;  /* no error function during GC metamethods */
203   do {  /* repeat until no more errors */
204     L->ci = L->base_ci;
205     L->base = L->top = L->ci->base;
206     L->nCcalls = L->baseCcalls = 0;
207   } while (luaD_rawrunprotected(L, callallgcTM, NULL) != 0);
208   lua_assert(G(L)->tmudata == NULL);
209   luai_userstateclose(L);
210   close_state(L);
211 }
212