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