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