interplane.Get Omega
Read in your language

lua

[Ported from github.com/lua/lua] Cast in 'rl_readline_name'

Brought by drakestapleton

branch master Branches Tags History

lua / lstate.c

443 lines · 12042 bytes Raw Blame History
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#define fromstate(L) (cast(LX *, cast(lu_byte *, (L)) - offsetof(LX, l)))
33
34
35/*
36** these macros allow user-specific actions when a thread is
37** created/deleted
38*/
39#if !defined(luai_userstateopen)
40#define luai_userstateopen(L) ((void)L)
41#endif
42
43#if !defined(luai_userstateclose)
44#define luai_userstateclose(L) ((void)L)
45#endif
46
47#if !defined(luai_userstatethread)
48#define luai_userstatethread(L,L1) ((void)L)
49#endif
50
51#if !defined(luai_userstatefree)
52#define luai_userstatefree(L,L1) ((void)L)
53#endif
54
55
56/*
57** set GCdebt to a new value keeping the real number of allocated
58** objects (GCtotalobjs - GCdebt) invariant and avoiding overflows in
59** 'GCtotalobjs'.
60*/
61void luaE_setdebt (global_State *g, l_mem debt) {
62 l_mem tb = gettotalbytes(g);
63 lua_assert(tb > 0);
64 if (debt > MAX_LMEM - tb)
65 debt = MAX_LMEM - tb; /* will make GCtotalbytes == MAX_LMEM */
66 g->GCtotalbytes = tb + debt;
67 g->GCdebt = debt;
68}
69
70
71CallInfo *luaE_extendCI (lua_State *L, int err) {
72 CallInfo *ci;
73 ci = luaM_reallocvector(L, NULL, 0, 1, CallInfo);
74 if (l_unlikely(ci == NULL)) { /* allocation failed? */
75 if (err)
76 luaM_error(L); /* raise the error */
77 return NULL; /* else only report it */
78 }
79 ci->next = L->ci->next;
80 ci->previous = L->ci;
81 L->ci->next = ci;
82 if (ci->next)
83 ci->next->previous = ci;
84 ci->u.l.trap = 0;
85 L->nci++;
86 return ci;
87}
88
89
90/*
91** free all CallInfo structures not in use by a thread
92*/
93static void freeCI (lua_State *L) {
94 CallInfo *ci = L->ci;
95 CallInfo *next = ci->next;
96 ci->next = NULL;
97 while ((ci = next) != NULL) {
98 next = ci->next;
99 luaM_free(L, ci);
100 L->nci--;
101 }
102}
103
104
105/*
106** free half of the CallInfo structures not in use by a thread,
107** keeping the first one.
108*/
109void luaE_shrinkCI (lua_State *L) {
110 CallInfo *ci = L->ci->next; /* first free CallInfo */
111 CallInfo *next;
112 if (ci == NULL)
113 return; /* no extra elements */
114 while ((next = ci->next) != NULL) { /* two extra elements? */
115 CallInfo *next2 = next->next; /* next's next */
116 ci->next = next2; /* remove next from the list */
117 L->nci--;
118 luaM_free(L, next); /* free next */
119 if (next2 == NULL)
120 break; /* no more elements */
121 else {
122 next2->previous = ci;
123 ci = next2; /* continue */
124 }
125 }
126}
127
128
129/*
130** Called when 'getCcalls(L)' larger or equal to LUAI_MAXCCALLS.
131** If equal, raises an overflow error. If value is larger than
132** LUAI_MAXCCALLS (which means it is handling an overflow) but
133** not much larger, does not report an error (to allow overflow
134** handling to work).
135*/
136void luaE_checkcstack (lua_State *L) {
137 if (getCcalls(L) == LUAI_MAXCCALLS)
138 luaG_runerror(L, "C stack overflow");
139 else if (getCcalls(L) >= (LUAI_MAXCCALLS / 10 * 11))
140 luaD_errerr(L); /* error while handling stack error */
141}
142
143
144LUAI_FUNC void luaE_incCstack (lua_State *L) {
145 L->nCcalls++;
146 if (l_unlikely(getCcalls(L) >= LUAI_MAXCCALLS))
147 luaE_checkcstack(L);
148}
149
150
151static void resetCI (lua_State *L) {
152 CallInfo *ci = L->ci = &L->base_ci;
153 ci->func.p = L->stack.p;
154 setnilvalue2s(ci->func.p); /* 'function' entry for basic 'ci' */
155 ci->top.p = ci->func.p + 1 + LUA_MINSTACK; /* +1 for 'function' entry */
156 ci->u.c.k = NULL;
157 ci->callstatus = CIST_C;
158 L->status = LUA_OK;
159 L->errfunc = 0; /* stack unwind can "throw away" the error function */
160}
161
162
163static void stack_init (lua_State *L1, lua_State *L) {
164 int i;
165 /* initialize stack array */
166 L1->stack.p = luaM_newvector(L, BASIC_STACK_SIZE + EXTRA_STACK, StackValue);
167 L1->tbclist.p = L1->stack.p;
168 for (i = 0; i < BASIC_STACK_SIZE + EXTRA_STACK; i++)
169 setnilvalue2s(L1->stack.p + i); /* erase new stack */
170 L1->stack_last.p = L1->stack.p + BASIC_STACK_SIZE;
171 /* initialize first ci */
172 resetCI(L1);
173 L1->top.p = L1->stack.p + 1; /* +1 for 'function' entry */
174}
175
176
177static void freestack (lua_State *L) {
178 if (L->stack.p == NULL)
179 return; /* stack not completely built yet */
180 L->ci = &L->base_ci; /* free the entire 'ci' list */
181 freeCI(L);
182 lua_assert(L->nci == 0);
183 /* free stack */
184 luaM_freearray(L, L->stack.p, cast_sizet(stacksize(L) + EXTRA_STACK));
185}
186
187
188/*
189** Create registry table and its predefined values
190*/
191static void init_registry (lua_State *L, global_State *g) {
192 /* create registry */
193 TValue aux;
194 Table *registry = luaH_new(L);
195 sethvalue(L, &g->l_registry, registry);
196 luaH_resize(L, registry, LUA_RIDX_LAST, 0);
197 /* registry[1] = false */
198 setbfvalue(&aux);
199 luaH_setint(L, registry, 1, &aux);
200 /* registry[LUA_RIDX_MAINTHREAD] = L */
201 setthvalue(L, &aux, L);
202 luaH_setint(L, registry, LUA_RIDX_MAINTHREAD, &aux);
203 /* registry[LUA_RIDX_GLOBALS] = new table (table of globals) */
204 sethvalue(L, &aux, luaH_new(L));
205 luaH_setint(L, registry, LUA_RIDX_GLOBALS, &aux);
206}
207
208
209/*
210** open parts of the state that may cause memory-allocation errors.
211*/
212static void f_luaopen (lua_State *L, void *ud) {
213 global_State *g = G(L);
214 UNUSED(ud);
215 stack_init(L, L); /* init stack */
216 init_registry(L, g);
217 luaS_init(L);
218 luaT_init(L);
219 luaX_init(L);
220 g->gcstp = 0; /* allow gc */
221 setnilvalue(&g->nilvalue); /* now state is complete */
222 luai_userstateopen(L);
223}
224
225
226/*
227** preinitialize a thread with consistent values without allocating
228** any memory (to avoid errors)
229*/
230static void preinit_thread (lua_State *L, global_State *g) {
231 G(L) = g;
232 L->stack.p = NULL;
233 L->ci = NULL;
234 L->nci = 0;
235 L->twups = L; /* thread has no upvalues */
236 L->nCcalls = 0;
237 L->errorJmp = NULL;
238 L->hook = NULL;
239 L->hookmask = 0;
240 L->basehookcount = 0;
241 L->allowhook = 1;
242 resethookcount(L);
243 L->openupval = NULL;
244 L->status = LUA_OK;
245 L->errfunc = 0;
246 L->oldpc = 0;
247 L->base_ci.previous = L->base_ci.next = NULL;
248}
249
250
251lu_mem luaE_threadsize (lua_State *L) {
252 lu_mem sz = cast(lu_mem, sizeof(LX))
253 + cast_uint(L->nci) * sizeof(CallInfo);
254 if (L->stack.p != NULL)
255 sz += cast_uint(stacksize(L) + EXTRA_STACK) * sizeof(StackValue);
256 return sz;
257}
258
259
260static void close_state (lua_State *L) {
261 global_State *g = G(L);
262 if (!completestate(g)) /* closing a partially built state? */
263 luaC_freeallobjects(L); /* just collect its objects */
264 else { /* closing a fully built state */
265 resetCI(L);
266 luaD_closeprotected(L, 1, LUA_OK); /* close all upvalues */
267 L->top.p = L->stack.p + 1; /* empty the stack to run finalizers */
268 luaC_freeallobjects(L); /* collect all objects */
269 luai_userstateclose(L);
270 }
271 luaM_freearray(L, G(L)->strt.hash, cast_sizet(G(L)->strt.size));
272 freestack(L);
273 lua_assert(gettotalbytes(g) == sizeof(global_State));
274 (*g->frealloc)(g->ud, g, sizeof(global_State), 0); /* free main block */
275}
276
277
278LUA_API lua_State *lua_newthread (lua_State *L) {
279 global_State *g = G(L);
280 GCObject *o;
281 lua_State *L1;
282 lua_lock(L);
283 luaC_checkGC(L);
284 /* create new thread */
285 o = luaC_newobjdt(L, LUA_TTHREAD, sizeof(LX), offsetof(LX, l));
286 L1 = gco2th(o);
287 /* anchor it on L stack */
288 setthvalue2s(L, L->top.p, L1);
289 api_incr_top(L);
290 preinit_thread(L1, g);
291 L1->hookmask = L->hookmask;
292 L1->basehookcount = L->basehookcount;
293 L1->hook = L->hook;
294 resethookcount(L1);
295 /* initialize L1 extra space */
296 memcpy(lua_getextraspace(L1), lua_getextraspace(mainthread(g)),
297 LUA_EXTRASPACE);
298 luai_userstatethread(L, L1);
299 stack_init(L1, L); /* init stack */
300 lua_unlock(L);
301 return L1;
302}
303
304
305void luaE_freethread (lua_State *L, lua_State *L1) {
306 LX *l = fromstate(L1);
307 luaF_closeupval(L1, L1->stack.p); /* close all upvalues */
308 lua_assert(L1->openupval == NULL);
309 luai_userstatefree(L, L1);
310 freestack(L1);
311 luaM_free(L, l);
312}
313
314
315TStatus luaE_resetthread (lua_State *L, TStatus status) {
316 resetCI(L);
317 if (status == LUA_YIELD)
318 status = LUA_OK;
319 status = luaD_closeprotected(L, 1, status);
320 if (status != LUA_OK) /* errors? */
321 luaD_seterrorobj(L, status, L->stack.p + 1);
322 else /* keep in the stack only the fake function-entry for basic ci */
323 L->top.p = L->stack.p + 1;
324 luaD_reallocstack(L, cast_int(L->ci->top.p - L->stack.p), 0);
325 return status;
326}
327
328
329/*
330** Auxiliary function to call luaE_resetthread in protected mode.
331** That function closes all tbc variables in protected mode, so the
332** only possible "error" is when a tbc variable closes the thread being
333** closed and therefore calls 'luaD_throwbaselevel'; in that case,
334** control goes directly back to the first call to lua_closethread (not
335** to the second), to properly return it.
336*/
337static void resetthread_aux (lua_State *L, void *ud) {
338 TStatus status = *cast(TStatus*, ud);
339 status = luaE_resetthread(L, status);
340 if (status != LUA_OK)
341 luaD_throw(L, status);
342}
343
344
345LUA_API int lua_closethread (lua_State *L, lua_State *from) {
346 TStatus status;
347 lua_lock(L);
348 L->nCcalls = (from) ? getCcalls(from) : 0;
349 status = luaD_rawrunprotected(L, resetthread_aux, &L->status);
350 /* stack has error message iff there was an error */
351 lua_assert(L->top.p == L->stack.p + (status == LUA_OK ? 1 : 2));
352 if (L == from) /* closing itself? */
353 luaD_throwbaselevel(L, status);
354 lua_unlock(L);
355 return APIstatus(status);
356}
357
358
359LUA_API lua_State *lua_newstate (lua_Alloc f, void *ud, unsigned seed) {
360 int i;
361 lua_State *L;
362 global_State *g = cast(global_State*,
363 (*f)(ud, NULL, LUA_TTHREAD, sizeof(global_State)));
364 if (g == NULL) return NULL;
365 L = &g->mainth.l;
366 L->tt = LUA_VTHREAD;
367 g->currentwhite = bitmask(WHITE0BIT);
368 L->marked = luaC_white(g);
369 preinit_thread(L, g);
370 g->allgc = obj2gco(L); /* by now, only object is the main thread */
371 L->next = NULL;
372 incnny(L); /* main thread is always non yieldable */
373 g->frealloc = f;
374 g->ud = ud;
375 g->warnf = NULL;
376 g->ud_warn = NULL;
377 g->seed = seed;
378 g->gcstp = GCSTPGC; /* no GC while building state */
379 g->strt.size = g->strt.nuse = 0;
380 g->strt.hash = NULL;
381 setnilvalue(&g->l_registry);
382 g->panic = NULL;
383 g->gcstate = GCSpause;
384 g->gckind = KGC_INC;
385 g->gcstopem = 0;
386 g->gcemergency = 0;
387 g->finobj = g->tobefnz = g->fixedgc = NULL;
388 g->firstold1 = g->survival = g->old1 = g->reallyold = NULL;
389 g->finobjsur = g->finobjold1 = g->finobjrold = NULL;
390 g->sweepgc = NULL;
391 g->gray = g->grayagain = NULL;
392 g->weak = g->ephemeron = g->allweak = NULL;
393 g->twups = NULL;
394 g->GCtotalbytes = sizeof(global_State);
395 g->GCmarked = 0;
396 g->GCdebt = 0;
397 setivalue(&g->nilvalue, 0); /* to signal that state is not yet built */
398 setgcparam(g, PAUSE, LUAI_GCPAUSE);
399 setgcparam(g, STEPMUL, LUAI_GCMUL);
400 setgcparam(g, STEPSIZE, LUAI_GCSTEPSIZE);
401 setgcparam(g, MINORMUL, LUAI_GENMINORMUL);
402 setgcparam(g, MINORMAJOR, LUAI_MINORMAJOR);
403 setgcparam(g, MAJORMINOR, LUAI_MAJORMINOR);
404 for (i=0; i < LUA_NUMTYPES; i++) g->mt[i] = NULL;
405 if (luaD_rawrunprotected(L, f_luaopen, NULL) != LUA_OK) {
406 /* memory allocation error: free partial state */
407 close_state(L);
408 L = NULL;
409 }
410 return L;
411}
412
413
414LUA_API void lua_close (lua_State *L) {
415 lua_lock(L);
416 L = mainthread(G(L)); /* only the main thread can be closed */
417 close_state(L);
418}
419
420
421void luaE_warning (lua_State *L, const char *msg, int tocont) {
422 lua_WarnFunction wf = G(L)->warnf;
423 if (wf != NULL)
424 wf(G(L)->ud_warn, msg, tocont);
425}
426
427
428/*
429** Generate a warning from an error message
430*/
431void luaE_warnerror (lua_State *L, const char *where) {
432 TValue *errobj = s2v(L->top.p - 1); /* error object */
433 const char *msg = (ttisstring(errobj))
434 ? getstr(tsvalue(errobj))
435 : "error object is not a string";
436 /* produce warning "error in %s (%s)" (where, msg) */
437 luaE_warning(L, "error in ", 1);
438 luaE_warning(L, where, 1);
439 luaE_warning(L, " (", 1);
440 luaE_warning(L, msg, 1);
441 luaE_warning(L, ")", 0);
442}
443