1 | /* |
---|
2 | ** $Id: lstate.c,v 1.123 2003/04/03 13:35:34 roberto Exp $ |
---|
3 | ** Global State |
---|
4 | ** See Copyright Notice in lua.h |
---|
5 | */ |
---|
6 | |
---|
7 | |
---|
8 | #include <stdlib.h> |
---|
9 | |
---|
10 | #define lstate_c |
---|
11 | |
---|
12 | #include "lua.h" |
---|
13 | |
---|
14 | #include "ldebug.h" |
---|
15 | #include "ldo.h" |
---|
16 | #include "lfunc.h" |
---|
17 | #include "lgc.h" |
---|
18 | #include "llex.h" |
---|
19 | #include "lmem.h" |
---|
20 | #include "lstate.h" |
---|
21 | #include "lstring.h" |
---|
22 | #include "ltable.h" |
---|
23 | #include "ltm.h" |
---|
24 | |
---|
25 | |
---|
26 | /* |
---|
27 | ** macro to allow the inclusion of user information in Lua state |
---|
28 | */ |
---|
29 | #ifndef LUA_USERSTATE |
---|
30 | #define EXTRASPACE 0 |
---|
31 | #else |
---|
32 | union UEXTRASPACE {L_Umaxalign a; LUA_USERSTATE b;}; |
---|
33 | #define EXTRASPACE (sizeof(union UEXTRASPACE)) |
---|
34 | #endif |
---|
35 | |
---|
36 | |
---|
37 | |
---|
38 | /* |
---|
39 | ** you can change this function through the official API: |
---|
40 | ** call `lua_setpanicf' |
---|
41 | */ |
---|
42 | static int default_panic (lua_State *L) { |
---|
43 | UNUSED(L); |
---|
44 | return 0; |
---|
45 | } |
---|
46 | |
---|
47 | |
---|
48 | static lua_State *mallocstate (lua_State *L) { |
---|
49 | lu_byte *block = (lu_byte *)luaM_malloc(L, sizeof(lua_State) + EXTRASPACE); |
---|
50 | if (block == NULL) return NULL; |
---|
51 | else { |
---|
52 | block += EXTRASPACE; |
---|
53 | return cast(lua_State *, block); |
---|
54 | } |
---|
55 | } |
---|
56 | |
---|
57 | |
---|
58 | static void freestate (lua_State *L, lua_State *L1) { |
---|
59 | luaM_free(L, cast(lu_byte *, L1) - EXTRASPACE, |
---|
60 | sizeof(lua_State) + EXTRASPACE); |
---|
61 | } |
---|
62 | |
---|
63 | |
---|
64 | static void stack_init (lua_State *L1, lua_State *L) { |
---|
65 | L1->stack = luaM_newvector(L, BASIC_STACK_SIZE + EXTRA_STACK, TObject); |
---|
66 | L1->stacksize = BASIC_STACK_SIZE + EXTRA_STACK; |
---|
67 | L1->top = L1->stack; |
---|
68 | L1->stack_last = L1->stack+(L1->stacksize - EXTRA_STACK)-1; |
---|
69 | L1->base_ci = luaM_newvector(L, BASIC_CI_SIZE, CallInfo); |
---|
70 | L1->ci = L1->base_ci; |
---|
71 | L1->ci->state = CI_C; /* not a Lua function */ |
---|
72 | setnilvalue(L1->top++); /* `function' entry for this `ci' */ |
---|
73 | L1->base = L1->ci->base = L1->top; |
---|
74 | L1->ci->top = L1->top + LUA_MINSTACK; |
---|
75 | L1->size_ci = BASIC_CI_SIZE; |
---|
76 | L1->end_ci = L1->base_ci + L1->size_ci; |
---|
77 | } |
---|
78 | |
---|
79 | |
---|
80 | static void freestack (lua_State *L, lua_State *L1) { |
---|
81 | luaM_freearray(L, L1->base_ci, L1->size_ci, CallInfo); |
---|
82 | luaM_freearray(L, L1->stack, L1->stacksize, TObject); |
---|
83 | } |
---|
84 | |
---|
85 | |
---|
86 | /* |
---|
87 | ** open parts that may cause memory-allocation errors |
---|
88 | */ |
---|
89 | static void f_luaopen (lua_State *L, void *ud) { |
---|
90 | /* create a new global state */ |
---|
91 | global_State *g = luaM_new(NULL, global_State); |
---|
92 | UNUSED(ud); |
---|
93 | if (g == NULL) luaD_throw(L, LUA_ERRMEM); |
---|
94 | L->l_G = g; |
---|
95 | g->mainthread = L; |
---|
96 | g->GCthreshold = 0; /* mark it as unfinished state */ |
---|
97 | g->strt.size = 0; |
---|
98 | g->strt.nuse = 0; |
---|
99 | g->strt.hash = NULL; |
---|
100 | setnilvalue(defaultmeta(L)); |
---|
101 | setnilvalue(registry(L)); |
---|
102 | luaZ_initbuffer(L, &g->buff); |
---|
103 | g->panic = default_panic; |
---|
104 | g->rootgc = NULL; |
---|
105 | g->rootudata = NULL; |
---|
106 | g->tmudata = NULL; |
---|
107 | setnilvalue(gkey(g->dummynode)); |
---|
108 | setnilvalue(gval(g->dummynode)); |
---|
109 | g->dummynode->next = NULL; |
---|
110 | g->nblocks = sizeof(lua_State) + sizeof(global_State); |
---|
111 | stack_init(L, L); /* init stack */ |
---|
112 | /* create default meta table with a dummy table, and then close the loop */ |
---|
113 | defaultmeta(L)->tt = LUA_TTABLE; |
---|
114 | sethvalue(defaultmeta(L), luaH_new(L, 0, 0)); |
---|
115 | hvalue(defaultmeta(L))->metatable = hvalue(defaultmeta(L)); |
---|
116 | sethvalue(gt(L), luaH_new(L, 0, 4)); /* table of globals */ |
---|
117 | sethvalue(registry(L), luaH_new(L, 4, 4)); /* registry */ |
---|
118 | luaS_resize(L, MINSTRTABSIZE); /* initial size of string table */ |
---|
119 | luaT_init(L); |
---|
120 | luaX_init(L); |
---|
121 | luaS_fix(luaS_newliteral(L, MEMERRMSG)); |
---|
122 | g->GCthreshold = 4*G(L)->nblocks; |
---|
123 | } |
---|
124 | |
---|
125 | |
---|
126 | static void preinit_state (lua_State *L) { |
---|
127 | L->stack = NULL; |
---|
128 | L->stacksize = 0; |
---|
129 | L->errorJmp = NULL; |
---|
130 | L->hook = NULL; |
---|
131 | L->hookmask = L->hookinit = 0; |
---|
132 | L->basehookcount = 0; |
---|
133 | L->allowhook = 1; |
---|
134 | resethookcount(L); |
---|
135 | L->openupval = NULL; |
---|
136 | L->size_ci = 0; |
---|
137 | L->nCcalls = 0; |
---|
138 | L->base_ci = L->ci = NULL; |
---|
139 | L->errfunc = 0; |
---|
140 | setnilvalue(gt(L)); |
---|
141 | } |
---|
142 | |
---|
143 | |
---|
144 | static void close_state (lua_State *L) { |
---|
145 | luaF_close(L, L->stack); /* close all upvalues for this thread */ |
---|
146 | if (G(L)) { /* close global state */ |
---|
147 | luaC_sweep(L, 1); /* collect all elements */ |
---|
148 | lua_assert(G(L)->rootgc == NULL); |
---|
149 | lua_assert(G(L)->rootudata == NULL); |
---|
150 | luaS_freeall(L); |
---|
151 | luaZ_freebuffer(L, &G(L)->buff); |
---|
152 | } |
---|
153 | freestack(L, L); |
---|
154 | if (G(L)) { |
---|
155 | lua_assert(G(L)->nblocks == sizeof(lua_State) + sizeof(global_State)); |
---|
156 | luaM_freelem(NULL, G(L)); |
---|
157 | } |
---|
158 | freestate(NULL, L); |
---|
159 | } |
---|
160 | |
---|
161 | |
---|
162 | lua_State *luaE_newthread (lua_State *L) { |
---|
163 | lua_State *L1 = mallocstate(L); |
---|
164 | luaC_link(L, valtogco(L1), LUA_TTHREAD); |
---|
165 | preinit_state(L1); |
---|
166 | L1->l_G = L->l_G; |
---|
167 | stack_init(L1, L); /* init stack */ |
---|
168 | setobj2n(gt(L1), gt(L)); /* share table of globals */ |
---|
169 | return L1; |
---|
170 | } |
---|
171 | |
---|
172 | |
---|
173 | void luaE_freethread (lua_State *L, lua_State *L1) { |
---|
174 | luaF_close(L1, L1->stack); /* close all upvalues for this thread */ |
---|
175 | lua_assert(L1->openupval == NULL); |
---|
176 | freestack(L, L1); |
---|
177 | freestate(L, L1); |
---|
178 | } |
---|
179 | |
---|
180 | |
---|
181 | LUA_API lua_State *lua_open (void) { |
---|
182 | lua_State *L = mallocstate(NULL); |
---|
183 | if (L) { /* allocation OK? */ |
---|
184 | L->tt = LUA_TTHREAD; |
---|
185 | L->marked = 0; |
---|
186 | L->next = L->gclist = NULL; |
---|
187 | preinit_state(L); |
---|
188 | L->l_G = NULL; |
---|
189 | if (luaD_rawrunprotected(L, f_luaopen, NULL) != 0) { |
---|
190 | /* memory allocation error: free partial state */ |
---|
191 | close_state(L); |
---|
192 | L = NULL; |
---|
193 | } |
---|
194 | } |
---|
195 | lua_userstateopen(L); |
---|
196 | return L; |
---|
197 | } |
---|
198 | |
---|
199 | |
---|
200 | static void callallgcTM (lua_State *L, void *ud) { |
---|
201 | UNUSED(ud); |
---|
202 | luaC_callGCTM(L); /* call GC metamethods for all udata */ |
---|
203 | } |
---|
204 | |
---|
205 | |
---|
206 | LUA_API void lua_close (lua_State *L) { |
---|
207 | lua_lock(L); |
---|
208 | L = G(L)->mainthread; /* only the main thread can be closed */ |
---|
209 | luaF_close(L, L->stack); /* close all upvalues for this thread */ |
---|
210 | luaC_separateudata(L); /* separate udata that have GC metamethods */ |
---|
211 | L->errfunc = 0; /* no error function during GC metamethods */ |
---|
212 | do { /* repeat until no more errors */ |
---|
213 | L->ci = L->base_ci; |
---|
214 | L->base = L->top = L->ci->base; |
---|
215 | L->nCcalls = 0; |
---|
216 | } while (luaD_rawrunprotected(L, callallgcTM, NULL) != 0); |
---|
217 | lua_assert(G(L)->tmudata == NULL); |
---|
218 | close_state(L); |
---|
219 | } |
---|
220 | |
---|