/src/testdir/build/lua-master/source/lstate.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | ** $Id: lstate.c $ |
3 | | ** Global State |
4 | | ** See Copyright Notice in lua.h |
5 | | */ |
6 | | |
7 | | #define lstate_c |
8 | | #define LUA_CORE |
9 | | |
10 | | #include "lprefix.h" |
11 | | |
12 | | |
13 | | #include <stddef.h> |
14 | | #include <string.h> |
15 | | |
16 | | #include "lua.h" |
17 | | |
18 | | #include "lapi.h" |
19 | | #include "ldebug.h" |
20 | | #include "ldo.h" |
21 | | #include "lfunc.h" |
22 | | #include "lgc.h" |
23 | | #include "llex.h" |
24 | | #include "lmem.h" |
25 | | #include "lstate.h" |
26 | | #include "lstring.h" |
27 | | #include "ltable.h" |
28 | | #include "ltm.h" |
29 | | |
30 | | |
31 | | |
32 | | /* |
33 | | ** thread state + extra space |
34 | | */ |
35 | | typedef struct LX { |
36 | | lu_byte extra_[LUA_EXTRASPACE]; |
37 | | lua_State l; |
38 | | } LX; |
39 | | |
40 | | |
41 | | /* |
42 | | ** Main thread combines a thread state and the global state |
43 | | */ |
44 | | typedef struct LG { |
45 | | LX l; |
46 | | global_State g; |
47 | | } LG; |
48 | | |
49 | | |
50 | | |
51 | 561 | #define fromstate(L) (cast(LX *, cast(lu_byte *, (L)) - offsetof(LX, l))) |
52 | | |
53 | | |
54 | | /* |
55 | | ** set GCdebt to a new value keeping the real number of allocated |
56 | | ** objects (totalobjs - GCdebt) invariant and avoiding overflows in |
57 | | ** 'totalobjs'. |
58 | | */ |
59 | 13.7k | void luaE_setdebt (global_State *g, l_obj debt) { |
60 | 13.7k | l_obj tb = gettotalobjs(g); |
61 | 13.7k | lua_assert(tb > 0); |
62 | 13.7k | if (debt > MAX_LOBJ - tb) |
63 | 0 | debt = MAX_LOBJ - tb; /* will make 'totalobjs == MAX_LMEM' */ |
64 | 13.7k | g->totalobjs = tb + debt; |
65 | 13.7k | g->GCdebt = debt; |
66 | 13.7k | } |
67 | | |
68 | | |
69 | 1.40k | CallInfo *luaE_extendCI (lua_State *L) { |
70 | 1.40k | CallInfo *ci; |
71 | 1.40k | lua_assert(L->ci->next == NULL); |
72 | 1.40k | ci = luaM_new(L, CallInfo); |
73 | 1.40k | lua_assert(L->ci->next == NULL); |
74 | 1.40k | L->ci->next = ci; |
75 | 1.40k | ci->previous = L->ci; |
76 | 1.40k | ci->next = NULL; |
77 | 1.40k | ci->u.l.trap = 0; |
78 | 1.40k | L->nci++; |
79 | 1.40k | return ci; |
80 | 1.40k | } |
81 | | |
82 | | |
83 | | /* |
84 | | ** free all CallInfo structures not in use by a thread |
85 | | */ |
86 | 561 | static void freeCI (lua_State *L) { |
87 | 561 | CallInfo *ci = L->ci; |
88 | 561 | CallInfo *next = ci->next; |
89 | 561 | ci->next = NULL; |
90 | 1.29k | while ((ci = next) != NULL) { |
91 | 735 | next = ci->next; |
92 | 735 | luaM_free(L, ci); |
93 | 735 | L->nci--; |
94 | 735 | } |
95 | 561 | } |
96 | | |
97 | | |
98 | | /* |
99 | | ** free half of the CallInfo structures not in use by a thread, |
100 | | ** keeping the first one. |
101 | | */ |
102 | 5.29k | void luaE_shrinkCI (lua_State *L) { |
103 | 5.29k | CallInfo *ci = L->ci->next; /* first free CallInfo */ |
104 | 5.29k | CallInfo *next; |
105 | 5.29k | if (ci == NULL) |
106 | 2.53k | return; /* no extra elements */ |
107 | 3.32k | while ((next = ci->next) != NULL) { /* two extra elements? */ |
108 | 673 | CallInfo *next2 = next->next; /* next's next */ |
109 | 673 | ci->next = next2; /* remove next from the list */ |
110 | 673 | L->nci--; |
111 | 673 | luaM_free(L, next); /* free next */ |
112 | 673 | if (next2 == NULL) |
113 | 107 | break; /* no more elements */ |
114 | 566 | else { |
115 | 566 | next2->previous = ci; |
116 | 566 | ci = next2; /* continue */ |
117 | 566 | } |
118 | 673 | } |
119 | 2.76k | } |
120 | | |
121 | | |
122 | | /* |
123 | | ** Called when 'getCcalls(L)' larger or equal to LUAI_MAXCCALLS. |
124 | | ** If equal, raises an overflow error. If value is larger than |
125 | | ** LUAI_MAXCCALLS (which means it is handling an overflow) but |
126 | | ** not much larger, does not report an error (to allow overflow |
127 | | ** handling to work). |
128 | | */ |
129 | 6 | void luaE_checkcstack (lua_State *L) { |
130 | 6 | if (getCcalls(L) == LUAI_MAXCCALLS) |
131 | 6 | luaG_runerror(L, "C stack overflow"); |
132 | 0 | else if (getCcalls(L) >= (LUAI_MAXCCALLS / 10 * 11)) |
133 | 0 | luaD_throw(L, LUA_ERRERR); /* error while handling stack error */ |
134 | 6 | } |
135 | | |
136 | | |
137 | 27.9M | LUAI_FUNC void luaE_incCstack (lua_State *L) { |
138 | 27.9M | L->nCcalls++; |
139 | 27.9M | if (l_unlikely(getCcalls(L) >= LUAI_MAXCCALLS)) |
140 | 6 | luaE_checkcstack(L); |
141 | 27.9M | } |
142 | | |
143 | | |
144 | 561 | static void stack_init (lua_State *L1, lua_State *L) { |
145 | 561 | int i; CallInfo *ci; |
146 | | /* initialize stack array */ |
147 | 561 | L1->stack.p = luaM_newvector(L, BASIC_STACK_SIZE + EXTRA_STACK, StackValue); |
148 | 561 | L1->tbclist.p = L1->stack.p; |
149 | 25.8k | for (i = 0; i < BASIC_STACK_SIZE + EXTRA_STACK; i++) |
150 | 25.2k | setnilvalue(s2v(L1->stack.p + i)); /* erase new stack */ |
151 | 561 | L1->top.p = L1->stack.p; |
152 | 561 | L1->stack_last.p = L1->stack.p + BASIC_STACK_SIZE; |
153 | | /* initialize first ci */ |
154 | 561 | ci = &L1->base_ci; |
155 | 561 | ci->next = ci->previous = NULL; |
156 | 561 | ci->callstatus = CIST_C; |
157 | 561 | ci->func.p = L1->top.p; |
158 | 561 | ci->u.c.k = NULL; |
159 | 561 | ci->nresults = 0; |
160 | 561 | setnilvalue(s2v(L1->top.p)); /* 'function' entry for this 'ci' */ |
161 | 561 | L1->top.p++; |
162 | 561 | ci->top.p = L1->top.p + LUA_MINSTACK; |
163 | 561 | L1->ci = ci; |
164 | 561 | } |
165 | | |
166 | | |
167 | 561 | static void freestack (lua_State *L) { |
168 | 561 | if (L->stack.p == NULL) |
169 | 0 | return; /* stack not completely built yet */ |
170 | 561 | L->ci = &L->base_ci; /* free the entire 'ci' list */ |
171 | 561 | freeCI(L); |
172 | 561 | lua_assert(L->nci == 0); |
173 | 561 | luaM_freearray(L, L->stack.p, stacksize(L) + EXTRA_STACK); /* free stack */ |
174 | 561 | } |
175 | | |
176 | | |
177 | | /* |
178 | | ** Create registry table and its predefined values |
179 | | */ |
180 | 561 | static void init_registry (lua_State *L, global_State *g) { |
181 | | /* create registry */ |
182 | 561 | TValue aux; |
183 | 561 | Table *registry = luaH_new(L); |
184 | 561 | sethvalue(L, &g->l_registry, registry); |
185 | 561 | luaH_resize(L, registry, LUA_RIDX_LAST, 0); |
186 | | /* registry[1] = false */ |
187 | 561 | setbfvalue(&aux); |
188 | 561 | luaH_setint(L, registry, 1, &aux); |
189 | | /* registry[LUA_RIDX_MAINTHREAD] = L */ |
190 | 561 | setthvalue(L, &aux, L); |
191 | 561 | luaH_setint(L, registry, LUA_RIDX_MAINTHREAD, &aux); |
192 | | /* registry[LUA_RIDX_GLOBALS] = new table (table of globals) */ |
193 | 561 | sethvalue(L, &aux, luaH_new(L)); |
194 | 561 | luaH_setint(L, registry, LUA_RIDX_GLOBALS, &aux); |
195 | 561 | } |
196 | | |
197 | | |
198 | | /* |
199 | | ** open parts of the state that may cause memory-allocation errors. |
200 | | */ |
201 | 561 | static void f_luaopen (lua_State *L, void *ud) { |
202 | 561 | global_State *g = G(L); |
203 | 561 | UNUSED(ud); |
204 | 561 | stack_init(L, L); /* init stack */ |
205 | 561 | init_registry(L, g); |
206 | 561 | luaS_init(L); |
207 | 561 | luaT_init(L); |
208 | 561 | luaX_init(L); |
209 | 561 | g->gcstp = 0; /* allow gc */ |
210 | 561 | setnilvalue(&g->nilvalue); /* now state is complete */ |
211 | 561 | luai_userstateopen(L); |
212 | 561 | } |
213 | | |
214 | | |
215 | | /* |
216 | | ** preinitialize a thread with consistent values without allocating |
217 | | ** any memory (to avoid errors) |
218 | | */ |
219 | 561 | static void preinit_thread (lua_State *L, global_State *g) { |
220 | 561 | G(L) = g; |
221 | 561 | L->stack.p = NULL; |
222 | 561 | L->ci = NULL; |
223 | 561 | L->nci = 0; |
224 | 561 | L->twups = L; /* thread has no upvalues */ |
225 | 561 | L->nCcalls = 0; |
226 | 561 | L->errorJmp = NULL; |
227 | 561 | L->hook = NULL; |
228 | 561 | L->hookmask = 0; |
229 | 561 | L->basehookcount = 0; |
230 | 561 | L->allowhook = 1; |
231 | 561 | resethookcount(L); |
232 | 561 | L->openupval = NULL; |
233 | 561 | L->status = LUA_OK; |
234 | 561 | L->errfunc = 0; |
235 | 561 | L->oldpc = 0; |
236 | 561 | } |
237 | | |
238 | | |
239 | 561 | static void close_state (lua_State *L) { |
240 | 561 | global_State *g = G(L); |
241 | 561 | if (!completestate(g)) /* closing a partially built state? */ |
242 | 0 | luaC_freeallobjects(L); /* just collect its objects */ |
243 | 561 | else { /* closing a fully built state */ |
244 | 561 | L->ci = &L->base_ci; /* unwind CallInfo list */ |
245 | 561 | luaD_closeprotected(L, 1, LUA_OK); /* close all upvalues */ |
246 | 561 | luaC_freeallobjects(L); /* collect all objects */ |
247 | 561 | luai_userstateclose(L); |
248 | 561 | } |
249 | 561 | luaM_freearray(L, G(L)->strt.hash, G(L)->strt.size); |
250 | 561 | freestack(L); |
251 | 561 | lua_assert(g->totalbytes == sizeof(LG)); |
252 | 561 | lua_assert(gettotalobjs(g) == 1); |
253 | 561 | (*g->frealloc)(g->ud, fromstate(L), sizeof(LG), 0); /* free main block */ |
254 | 561 | } |
255 | | |
256 | | |
257 | 0 | LUA_API lua_State *lua_newthread (lua_State *L) { |
258 | 0 | global_State *g = G(L); |
259 | 0 | GCObject *o; |
260 | 0 | lua_State *L1; |
261 | 0 | lua_lock(L); |
262 | 0 | luaC_checkGC(L); |
263 | | /* create new thread */ |
264 | 0 | o = luaC_newobjdt(L, LUA_TTHREAD, sizeof(LX), offsetof(LX, l)); |
265 | 0 | L1 = gco2th(o); |
266 | | /* anchor it on L stack */ |
267 | 0 | setthvalue2s(L, L->top.p, L1); |
268 | 0 | api_incr_top(L); |
269 | 0 | preinit_thread(L1, g); |
270 | 0 | L1->hookmask = L->hookmask; |
271 | 0 | L1->basehookcount = L->basehookcount; |
272 | 0 | L1->hook = L->hook; |
273 | 0 | resethookcount(L1); |
274 | | /* initialize L1 extra space */ |
275 | 0 | memcpy(lua_getextraspace(L1), lua_getextraspace(g->mainthread), |
276 | 0 | LUA_EXTRASPACE); |
277 | 0 | luai_userstatethread(L, L1); |
278 | 0 | stack_init(L1, L); /* init stack */ |
279 | 0 | lua_unlock(L); |
280 | 0 | return L1; |
281 | 0 | } |
282 | | |
283 | | |
284 | 0 | void luaE_freethread (lua_State *L, lua_State *L1) { |
285 | 0 | LX *l = fromstate(L1); |
286 | 0 | luaF_closeupval(L1, L1->stack.p); /* close all upvalues */ |
287 | 0 | lua_assert(L1->openupval == NULL); |
288 | 0 | luai_userstatefree(L, L1); |
289 | 0 | freestack(L1); |
290 | 0 | luaM_free(L, l); |
291 | 0 | } |
292 | | |
293 | | |
294 | 0 | int luaE_resetthread (lua_State *L, int status) { |
295 | 0 | CallInfo *ci = L->ci = &L->base_ci; /* unwind CallInfo list */ |
296 | 0 | setnilvalue(s2v(L->stack.p)); /* 'function' entry for basic 'ci' */ |
297 | 0 | ci->func.p = L->stack.p; |
298 | 0 | ci->callstatus = CIST_C; |
299 | 0 | if (status == LUA_YIELD) |
300 | 0 | status = LUA_OK; |
301 | 0 | L->status = LUA_OK; /* so it can run __close metamethods */ |
302 | 0 | status = luaD_closeprotected(L, 1, status); |
303 | 0 | if (status != LUA_OK) /* errors? */ |
304 | 0 | luaD_seterrorobj(L, status, L->stack.p + 1); |
305 | 0 | else |
306 | 0 | L->top.p = L->stack.p + 1; |
307 | 0 | ci->top.p = L->top.p + LUA_MINSTACK; |
308 | 0 | luaD_reallocstack(L, cast_int(ci->top.p - L->stack.p), 0); |
309 | 0 | return status; |
310 | 0 | } |
311 | | |
312 | | |
313 | 0 | LUA_API int lua_closethread (lua_State *L, lua_State *from) { |
314 | 0 | int status; |
315 | 0 | lua_lock(L); |
316 | 0 | L->nCcalls = (from) ? getCcalls(from) : 0; |
317 | 0 | status = luaE_resetthread(L, L->status); |
318 | 0 | lua_unlock(L); |
319 | 0 | return status; |
320 | 0 | } |
321 | | |
322 | | |
323 | 561 | LUA_API lua_State *lua_newstate (lua_Alloc f, void *ud, unsigned seed) { |
324 | 561 | int i; |
325 | 561 | lua_State *L; |
326 | 561 | global_State *g; |
327 | 561 | LG *l = cast(LG *, (*f)(ud, NULL, LUA_TTHREAD, sizeof(LG))); |
328 | 561 | if (l == NULL) return NULL; |
329 | 561 | L = &l->l.l; |
330 | 561 | g = &l->g; |
331 | 561 | L->tt = LUA_VTHREAD; |
332 | 561 | g->currentwhite = bitmask(WHITE0BIT); |
333 | 561 | L->marked = luaC_white(g); |
334 | 561 | preinit_thread(L, g); |
335 | 561 | g->allgc = obj2gco(L); /* by now, only object is the main thread */ |
336 | 0 | L->next = NULL; |
337 | 561 | incnny(L); /* main thread is always non yieldable */ |
338 | 561 | g->frealloc = f; |
339 | 561 | g->ud = ud; |
340 | 561 | g->warnf = NULL; |
341 | 561 | g->ud_warn = NULL; |
342 | 561 | g->mainthread = L; |
343 | 561 | g->seed = seed; |
344 | 561 | g->gcstp = GCSTPGC; /* no GC while building state */ |
345 | 561 | g->strt.size = g->strt.nuse = 0; |
346 | 561 | g->strt.hash = NULL; |
347 | 561 | setnilvalue(&g->l_registry); |
348 | 561 | g->panic = NULL; |
349 | 561 | g->gcstate = GCSpause; |
350 | 561 | g->gckind = KGC_INC; |
351 | 561 | g->gcstopem = 0; |
352 | 561 | g->gcemergency = 0; |
353 | 561 | g->finobj = g->tobefnz = g->fixedgc = NULL; |
354 | 561 | g->firstold1 = g->survival = g->old1 = g->reallyold = NULL; |
355 | 561 | g->finobjsur = g->finobjold1 = g->finobjrold = NULL; |
356 | 561 | g->sweepgc = NULL; |
357 | 561 | g->gray = g->grayagain = NULL; |
358 | 561 | g->weak = g->ephemeron = g->allweak = NULL; |
359 | 561 | g->twups = NULL; |
360 | 561 | g->totalbytes = sizeof(LG); |
361 | 561 | g->totalobjs = 1; |
362 | 561 | g->marked = 0; |
363 | 561 | g->GCdebt = 0; |
364 | 561 | setivalue(&g->nilvalue, 0); /* to signal that state is not yet built */ |
365 | 561 | setgcparam(g, PAUSE, LUAI_GCPAUSE); |
366 | 561 | setgcparam(g, STEPMUL, LUAI_GCMUL); |
367 | 561 | setgcparam(g, STEPSIZE, LUAI_GCSTEPSIZE); |
368 | 561 | setgcparam(g, MINORMUL, LUAI_GENMINORMUL); |
369 | 561 | setgcparam(g, MINORMAJOR, LUAI_MINORMAJOR); |
370 | 561 | setgcparam(g, MAJORMINOR, LUAI_MAJORMINOR); |
371 | 5.61k | for (i=0; i < LUA_NUMTYPES; i++) g->mt[i] = NULL; |
372 | 561 | if (luaD_rawrunprotected(L, f_luaopen, NULL) != LUA_OK) { |
373 | | /* memory allocation error: free partial state */ |
374 | 0 | close_state(L); |
375 | 0 | L = NULL; |
376 | 0 | } |
377 | 561 | return L; |
378 | 561 | } |
379 | | |
380 | | |
381 | 561 | LUA_API void lua_close (lua_State *L) { |
382 | 561 | lua_lock(L); |
383 | 561 | L = G(L)->mainthread; /* only the main thread can be closed */ |
384 | 561 | close_state(L); |
385 | 561 | } |
386 | | |
387 | | |
388 | 0 | void luaE_warning (lua_State *L, const char *msg, int tocont) { |
389 | 0 | lua_WarnFunction wf = G(L)->warnf; |
390 | 0 | if (wf != NULL) |
391 | 0 | wf(G(L)->ud_warn, msg, tocont); |
392 | 0 | } |
393 | | |
394 | | |
395 | | /* |
396 | | ** Generate a warning from an error message |
397 | | */ |
398 | 0 | void luaE_warnerror (lua_State *L, const char *where) { |
399 | 0 | TValue *errobj = s2v(L->top.p - 1); /* error object */ |
400 | 0 | const char *msg = (ttisstring(errobj)) |
401 | 0 | ? getstr(tsvalue(errobj)) |
402 | 0 | : "error object is not a string"; |
403 | | /* produce warning "error in %s (%s)" (where, msg) */ |
404 | 0 | luaE_warning(L, "error in ", 1); |
405 | 0 | luaE_warning(L, where, 1); |
406 | 0 | luaE_warning(L, " (", 1); |
407 | 0 | luaE_warning(L, msg, 1); |
408 | 0 | luaE_warning(L, ")", 0); |
409 | 0 | } |
410 | | |