annotate lib/lua/lua-5.2.2/lstate.c @ 2575:a76d408c5132 tip

DrawTranslucent -> DrawTextureGrayShade Removed old texture drawing stuff
author a.parshin
date Wed, 09 Mar 2016 01:39:52 +0200
parents 41cc4dd3c122
children
rev   line source
1866
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1 /*
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
2 ** $Id: lstate.c,v 2.99 2012/10/02 17:40:53 roberto Exp $
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
3 ** Global State
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
4 ** See Copyright Notice in lua.h
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
5 */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
6
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
7
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
8 #include <stddef.h>
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
9 #include <string.h>
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
10
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
11 #define lstate_c
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
12 #define LUA_CORE
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
13
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
14 #include "lua.h"
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
15
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
16 #include "lapi.h"
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
17 #include "ldebug.h"
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
18 #include "ldo.h"
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
19 #include "lfunc.h"
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
20 #include "lgc.h"
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
21 #include "llex.h"
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
22 #include "lmem.h"
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
23 #include "lstate.h"
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
24 #include "lstring.h"
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
25 #include "ltable.h"
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
26 #include "ltm.h"
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
27
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
28
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
29 #if !defined(LUAI_GCPAUSE)
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
30 #define LUAI_GCPAUSE 200 /* 200% */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
31 #endif
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
32
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
33 #if !defined(LUAI_GCMAJOR)
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
34 #define LUAI_GCMAJOR 200 /* 200% */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
35 #endif
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
36
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
37 #if !defined(LUAI_GCMUL)
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
38 #define LUAI_GCMUL 200 /* GC runs 'twice the speed' of memory allocation */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
39 #endif
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
40
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
41
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
42 #define MEMERRMSG "not enough memory"
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
43
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
44
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
45 /*
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
46 ** a macro to help the creation of a unique random seed when a state is
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
47 ** created; the seed is used to randomize hashes.
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
48 */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
49 #if !defined(luai_makeseed)
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
50 #include <time.h>
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
51 #define luai_makeseed() cast(unsigned int, time(NULL))
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
52 #endif
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
53
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
54
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
55
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
56 /*
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
57 ** thread state + extra space
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
58 */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
59 typedef struct LX {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
60 #if defined(LUAI_EXTRASPACE)
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
61 char buff[LUAI_EXTRASPACE];
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
62 #endif
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
63 lua_State l;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
64 } LX;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
65
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
66
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
67 /*
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
68 ** Main thread combines a thread state and the global state
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
69 */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
70 typedef struct LG {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
71 LX l;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
72 global_State g;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
73 } LG;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
74
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
75
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
76
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
77 #define fromstate(L) (cast(LX *, cast(lu_byte *, (L)) - offsetof(LX, l)))
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
78
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
79
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
80 /*
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
81 ** Compute an initial seed as random as possible. In ANSI, rely on
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
82 ** Address Space Layout Randomization (if present) to increase
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
83 ** randomness..
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
84 */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
85 #define addbuff(b,p,e) \
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
86 { size_t t = cast(size_t, e); \
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
87 memcpy(buff + p, &t, sizeof(t)); p += sizeof(t); }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
88
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
89 static unsigned int makeseed (lua_State *L) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
90 char buff[4 * sizeof(size_t)];
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
91 unsigned int h = luai_makeseed();
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
92 int p = 0;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
93 addbuff(buff, p, L); /* heap variable */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
94 addbuff(buff, p, &h); /* local variable */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
95 addbuff(buff, p, luaO_nilobject); /* global variable */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
96 addbuff(buff, p, &lua_newstate); /* public function */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
97 lua_assert(p == sizeof(buff));
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
98 return luaS_hash(buff, p, h);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
99 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
100
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
101
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
102 /*
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
103 ** set GCdebt to a new value keeping the value (totalbytes + GCdebt)
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
104 ** invariant
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
105 */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
106 void luaE_setdebt (global_State *g, l_mem debt) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
107 g->totalbytes -= (debt - g->GCdebt);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
108 g->GCdebt = debt;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
109 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
110
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
111
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
112 CallInfo *luaE_extendCI (lua_State *L) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
113 CallInfo *ci = luaM_new(L, CallInfo);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
114 lua_assert(L->ci->next == NULL);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
115 L->ci->next = ci;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
116 ci->previous = L->ci;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
117 ci->next = NULL;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
118 return ci;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
119 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
120
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
121
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
122 void luaE_freeCI (lua_State *L) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
123 CallInfo *ci = L->ci;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
124 CallInfo *next = ci->next;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
125 ci->next = NULL;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
126 while ((ci = next) != NULL) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
127 next = ci->next;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
128 luaM_free(L, ci);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
129 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
130 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
131
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
132
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
133 static void stack_init (lua_State *L1, lua_State *L) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
134 int i; CallInfo *ci;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
135 /* initialize stack array */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
136 L1->stack = luaM_newvector(L, BASIC_STACK_SIZE, TValue);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
137 L1->stacksize = BASIC_STACK_SIZE;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
138 for (i = 0; i < BASIC_STACK_SIZE; i++)
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
139 setnilvalue(L1->stack + i); /* erase new stack */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
140 L1->top = L1->stack;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
141 L1->stack_last = L1->stack + L1->stacksize - EXTRA_STACK;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
142 /* initialize first ci */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
143 ci = &L1->base_ci;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
144 ci->next = ci->previous = NULL;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
145 ci->callstatus = 0;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
146 ci->func = L1->top;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
147 setnilvalue(L1->top++); /* 'function' entry for this 'ci' */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
148 ci->top = L1->top + LUA_MINSTACK;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
149 L1->ci = ci;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
150 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
151
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
152
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
153 static void freestack (lua_State *L) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
154 if (L->stack == NULL)
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
155 return; /* stack not completely built yet */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
156 L->ci = &L->base_ci; /* free the entire 'ci' list */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
157 luaE_freeCI(L);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
158 luaM_freearray(L, L->stack, L->stacksize); /* free stack array */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
159 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
160
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
161
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
162 /*
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
163 ** Create registry table and its predefined values
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
164 */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
165 static void init_registry (lua_State *L, global_State *g) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
166 TValue mt;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
167 /* create registry */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
168 Table *registry = luaH_new(L);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
169 sethvalue(L, &g->l_registry, registry);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
170 luaH_resize(L, registry, LUA_RIDX_LAST, 0);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
171 /* registry[LUA_RIDX_MAINTHREAD] = L */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
172 setthvalue(L, &mt, L);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
173 luaH_setint(L, registry, LUA_RIDX_MAINTHREAD, &mt);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
174 /* registry[LUA_RIDX_GLOBALS] = table of globals */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
175 sethvalue(L, &mt, luaH_new(L));
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
176 luaH_setint(L, registry, LUA_RIDX_GLOBALS, &mt);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
177 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
178
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
179
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
180 /*
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
181 ** open parts of the state that may cause memory-allocation errors
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
182 */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
183 static void f_luaopen (lua_State *L, void *ud) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
184 global_State *g = G(L);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
185 UNUSED(ud);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
186 stack_init(L, L); /* init stack */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
187 init_registry(L, g);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
188 luaS_resize(L, MINSTRTABSIZE); /* initial size of string table */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
189 luaT_init(L);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
190 luaX_init(L);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
191 /* pre-create memory-error message */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
192 g->memerrmsg = luaS_newliteral(L, MEMERRMSG);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
193 luaS_fix(g->memerrmsg); /* it should never be collected */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
194 g->gcrunning = 1; /* allow gc */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
195 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
196
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
197
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
198 /*
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
199 ** preinitialize a state with consistent values without allocating
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
200 ** any memory (to avoid errors)
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
201 */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
202 static void preinit_state (lua_State *L, global_State *g) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
203 G(L) = g;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
204 L->stack = NULL;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
205 L->ci = NULL;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
206 L->stacksize = 0;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
207 L->errorJmp = NULL;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
208 L->nCcalls = 0;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
209 L->hook = NULL;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
210 L->hookmask = 0;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
211 L->basehookcount = 0;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
212 L->allowhook = 1;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
213 resethookcount(L);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
214 L->openupval = NULL;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
215 L->nny = 1;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
216 L->status = LUA_OK;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
217 L->errfunc = 0;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
218 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
219
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
220
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
221 static void close_state (lua_State *L) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
222 global_State *g = G(L);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
223 luaF_close(L, L->stack); /* close all upvalues for this thread */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
224 luaC_freeallobjects(L); /* collect all objects */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
225 luaM_freearray(L, G(L)->strt.hash, G(L)->strt.size);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
226 luaZ_freebuffer(L, &g->buff);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
227 freestack(L);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
228 lua_assert(gettotalbytes(g) == sizeof(LG));
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
229 (*g->frealloc)(g->ud, fromstate(L), sizeof(LG), 0); /* free main block */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
230 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
231
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
232
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
233 LUA_API lua_State *lua_newthread (lua_State *L) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
234 lua_State *L1;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
235 lua_lock(L);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
236 luaC_checkGC(L);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
237 L1 = &luaC_newobj(L, LUA_TTHREAD, sizeof(LX), NULL, offsetof(LX, l))->th;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
238 setthvalue(L, L->top, L1);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
239 api_incr_top(L);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
240 preinit_state(L1, G(L));
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
241 L1->hookmask = L->hookmask;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
242 L1->basehookcount = L->basehookcount;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
243 L1->hook = L->hook;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
244 resethookcount(L1);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
245 luai_userstatethread(L, L1);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
246 stack_init(L1, L); /* init stack */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
247 lua_unlock(L);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
248 return L1;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
249 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
250
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
251
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
252 void luaE_freethread (lua_State *L, lua_State *L1) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
253 LX *l = fromstate(L1);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
254 luaF_close(L1, L1->stack); /* close all upvalues for this thread */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
255 lua_assert(L1->openupval == NULL);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
256 luai_userstatefree(L, L1);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
257 freestack(L1);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
258 luaM_free(L, l);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
259 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
260
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
261
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
262 LUA_API lua_State *lua_newstate (lua_Alloc f, void *ud) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
263 int i;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
264 lua_State *L;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
265 global_State *g;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
266 LG *l = cast(LG *, (*f)(ud, NULL, LUA_TTHREAD, sizeof(LG)));
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
267 if (l == NULL) return NULL;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
268 L = &l->l.l;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
269 g = &l->g;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
270 L->next = NULL;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
271 L->tt = LUA_TTHREAD;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
272 g->currentwhite = bit2mask(WHITE0BIT, FIXEDBIT);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
273 L->marked = luaC_white(g);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
274 g->gckind = KGC_NORMAL;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
275 preinit_state(L, g);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
276 g->frealloc = f;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
277 g->ud = ud;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
278 g->mainthread = L;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
279 g->seed = makeseed(L);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
280 g->uvhead.u.l.prev = &g->uvhead;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
281 g->uvhead.u.l.next = &g->uvhead;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
282 g->gcrunning = 0; /* no GC while building state */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
283 g->GCestimate = 0;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
284 g->strt.size = 0;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
285 g->strt.nuse = 0;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
286 g->strt.hash = NULL;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
287 setnilvalue(&g->l_registry);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
288 luaZ_initbuffer(L, &g->buff);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
289 g->panic = NULL;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
290 g->version = lua_version(NULL);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
291 g->gcstate = GCSpause;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
292 g->allgc = NULL;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
293 g->finobj = NULL;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
294 g->tobefnz = NULL;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
295 g->sweepgc = g->sweepfin = NULL;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
296 g->gray = g->grayagain = NULL;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
297 g->weak = g->ephemeron = g->allweak = NULL;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
298 g->totalbytes = sizeof(LG);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
299 g->GCdebt = 0;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
300 g->gcpause = LUAI_GCPAUSE;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
301 g->gcmajorinc = LUAI_GCMAJOR;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
302 g->gcstepmul = LUAI_GCMUL;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
303 for (i=0; i < LUA_NUMTAGS; i++) g->mt[i] = NULL;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
304 if (luaD_rawrunprotected(L, f_luaopen, NULL) != LUA_OK) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
305 /* memory allocation error: free partial state */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
306 close_state(L);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
307 L = NULL;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
308 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
309 else
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
310 luai_userstateopen(L);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
311 return L;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
312 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
313
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
314
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
315 LUA_API void lua_close (lua_State *L) {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
316 L = G(L)->mainthread; /* only the main thread can be closed */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
317 lua_lock(L);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
318 luai_userstateclose(L);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
319 close_state(L);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
320 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
321
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
322