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