aboutsummaryrefslogtreecommitdiff
path: root/src/lua/lstate.c
diff options
context:
space:
mode:
authorLi Jin <dragon-fly@qq.com>2020-06-22 16:50:40 +0800
committerLi Jin <dragon-fly@qq.com>2020-06-22 16:50:40 +0800
commitcd2b60b101a398cb9356d746364e70eaed1860f1 (patch)
treea1fe71b76faabc4883f16905a94164ce5c23e692 /src/lua/lstate.c
parent88c1052e700f38cf3d8ad82d469da4c487760b7e (diff)
downloadyuescript-cd2b60b101a398cb9356d746364e70eaed1860f1.tar.gz
yuescript-cd2b60b101a398cb9356d746364e70eaed1860f1.tar.bz2
yuescript-cd2b60b101a398cb9356d746364e70eaed1860f1.zip
add support for local variable declared with attribute 'close' and 'const' for Lua 5.4.
Diffstat (limited to '')
-rw-r--r--src/lua/lstate.c (renamed from src/lua-5.3/lstate.c)242
1 files changed, 181 insertions, 61 deletions
diff --git a/src/lua-5.3/lstate.c b/src/lua/lstate.c
index c1a7664..4434211 100644
--- a/src/lua-5.3/lstate.c
+++ b/src/lua/lstate.c
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: lstate.c,v 2.133.1.1 2017/04/19 17:39:34 roberto Exp $ 2** $Id: lstate.c $
3** Global State 3** Global State
4** See Copyright Notice in lua.h 4** See Copyright Notice in lua.h
5*/ 5*/
@@ -28,25 +28,6 @@
28#include "ltm.h" 28#include "ltm.h"
29 29
30 30
31#if !defined(LUAI_GCPAUSE)
32#define LUAI_GCPAUSE 200 /* 200% */
33#endif
34
35#if !defined(LUAI_GCMUL)
36#define LUAI_GCMUL 200 /* GC runs 'twice the speed' of memory allocation */
37#endif
38
39
40/*
41** a macro to help the creation of a unique random seed when a state is
42** created; the seed is used to randomize hashes.
43*/
44#if !defined(luai_makeseed)
45#include <time.h>
46#define luai_makeseed() cast(unsigned int, time(NULL))
47#endif
48
49
50 31
51/* 32/*
52** thread state + extra space 33** thread state + extra space
@@ -71,25 +52,35 @@ typedef struct LG {
71 52
72 53
73/* 54/*
74** Compute an initial seed as random as possible. Rely on Address Space 55** A macro to create a "random" seed when a state is created;
75** Layout Randomization (if present) to increase randomness.. 56** the seed is used to randomize string hashes.
57*/
58#if !defined(luai_makeseed)
59
60#include <time.h>
61
62/*
63** Compute an initial seed with some level of randomness.
64** Rely on Address Space Layout Randomization (if present) and
65** current time.
76*/ 66*/
77#define addbuff(b,p,e) \ 67#define addbuff(b,p,e) \
78 { size_t t = cast(size_t, e); \ 68 { size_t t = cast_sizet(e); \
79 memcpy(b + p, &t, sizeof(t)); p += sizeof(t); } 69 memcpy(b + p, &t, sizeof(t)); p += sizeof(t); }
80 70
81static unsigned int makeseed (lua_State *L) { 71static unsigned int luai_makeseed (lua_State *L) {
82 char buff[4 * sizeof(size_t)]; 72 char buff[3 * sizeof(size_t)];
83 unsigned int h = luai_makeseed(); 73 unsigned int h = cast_uint(time(NULL));
84 int p = 0; 74 int p = 0;
85 addbuff(buff, p, L); /* heap variable */ 75 addbuff(buff, p, L); /* heap variable */
86 addbuff(buff, p, &h); /* local variable */ 76 addbuff(buff, p, &h); /* local variable */
87 addbuff(buff, p, luaO_nilobject); /* global variable */
88 addbuff(buff, p, &lua_newstate); /* public function */ 77 addbuff(buff, p, &lua_newstate); /* public function */
89 lua_assert(p == sizeof(buff)); 78 lua_assert(p == sizeof(buff));
90 return luaS_hash(buff, p, h); 79 return luaS_hash(buff, p, h, 1);
91} 80}
92 81
82#endif
83
93 84
94/* 85/*
95** set GCdebt to a new value keeping the value (totalbytes + GCdebt) 86** set GCdebt to a new value keeping the value (totalbytes + GCdebt)
@@ -105,12 +96,73 @@ void luaE_setdebt (global_State *g, l_mem debt) {
105} 96}
106 97
107 98
99LUA_API int lua_setcstacklimit (lua_State *L, unsigned int limit) {
100 global_State *g = G(L);
101 int ccalls;
102 luaE_freeCI(L); /* release unused CIs */
103 ccalls = getCcalls(L);
104 if (limit >= 40000)
105 return 0; /* out of bounds */
106 limit += CSTACKERR;
107 if (L != g-> mainthread)
108 return 0; /* only main thread can change the C stack */
109 else if (ccalls <= CSTACKERR)
110 return 0; /* handling overflow */
111 else {
112 int diff = limit - g->Cstacklimit;
113 if (ccalls + diff <= CSTACKERR)
114 return 0; /* new limit would cause an overflow */
115 g->Cstacklimit = limit; /* set new limit */
116 L->nCcalls += diff; /* correct 'nCcalls' */
117 return limit - diff - CSTACKERR; /* success; return previous limit */
118 }
119}
120
121
122/*
123** Decrement count of "C calls" and check for overflows. In case of
124** a stack overflow, check appropriate error ("regular" overflow or
125** overflow while handling stack overflow). If 'nCcalls' is smaller
126** than CSTACKERR but larger than CSTACKMARK, it means it has just
127** entered the "overflow zone", so the function raises an overflow
128** error. If 'nCcalls' is smaller than CSTACKMARK (which means it is
129** already handling an overflow) but larger than CSTACKERRMARK, does
130** not report an error (to allow message handling to work). Otherwise,
131** report a stack overflow while handling a stack overflow (probably
132** caused by a repeating error in the message handling function).
133*/
134
135void luaE_enterCcall (lua_State *L) {
136 int ncalls = getCcalls(L);
137 L->nCcalls--;
138 if (ncalls <= CSTACKERR) { /* possible overflow? */
139 luaE_freeCI(L); /* release unused CIs */
140 ncalls = getCcalls(L); /* update call count */
141 if (ncalls <= CSTACKERR) { /* still overflow? */
142 if (ncalls <= CSTACKERRMARK) /* below error-handling zone? */
143 luaD_throw(L, LUA_ERRERR); /* error while handling stack error */
144 else if (ncalls >= CSTACKMARK) {
145 /* not in error-handling zone; raise the error now */
146 L->nCcalls = (CSTACKMARK - 1); /* enter error-handling zone */
147 luaG_runerror(L, "C stack overflow");
148 }
149 /* else stack is in the error-handling zone;
150 allow message handler to work */
151 }
152 }
153}
154
155
108CallInfo *luaE_extendCI (lua_State *L) { 156CallInfo *luaE_extendCI (lua_State *L) {
109 CallInfo *ci = luaM_new(L, CallInfo); 157 CallInfo *ci;
158 lua_assert(L->ci->next == NULL);
159 luaE_enterCcall(L);
160 ci = luaM_new(L, CallInfo);
110 lua_assert(L->ci->next == NULL); 161 lua_assert(L->ci->next == NULL);
111 L->ci->next = ci; 162 L->ci->next = ci;
112 ci->previous = L->ci; 163 ci->previous = L->ci;
113 ci->next = NULL; 164 ci->next = NULL;
165 ci->u.l.trap = 0;
114 L->nci++; 166 L->nci++;
115 return ci; 167 return ci;
116} 168}
@@ -123,46 +175,60 @@ void luaE_freeCI (lua_State *L) {
123 CallInfo *ci = L->ci; 175 CallInfo *ci = L->ci;
124 CallInfo *next = ci->next; 176 CallInfo *next = ci->next;
125 ci->next = NULL; 177 ci->next = NULL;
178 L->nCcalls += L->nci; /* add removed elements back to 'nCcalls' */
126 while ((ci = next) != NULL) { 179 while ((ci = next) != NULL) {
127 next = ci->next; 180 next = ci->next;
128 luaM_free(L, ci); 181 luaM_free(L, ci);
129 L->nci--; 182 L->nci--;
130 } 183 }
184 L->nCcalls -= L->nci; /* adjust result */
131} 185}
132 186
133 187
134/* 188/*
135** free half of the CallInfo structures not in use by a thread 189** free half of the CallInfo structures not in use by a thread,
190** keeping the first one.
136*/ 191*/
137void luaE_shrinkCI (lua_State *L) { 192void luaE_shrinkCI (lua_State *L) {
138 CallInfo *ci = L->ci; 193 CallInfo *ci = L->ci->next; /* first free CallInfo */
139 CallInfo *next2; /* next's next */ 194 CallInfo *next;
140 /* while there are two nexts */ 195 if (ci == NULL)
141 while (ci->next != NULL && (next2 = ci->next->next) != NULL) { 196 return; /* no extra elements */
142 luaM_free(L, ci->next); /* free next */ 197 L->nCcalls += L->nci; /* add removed elements back to 'nCcalls' */
198 while ((next = ci->next) != NULL) { /* two extra elements? */
199 CallInfo *next2 = next->next; /* next's next */
200 ci->next = next2; /* remove next from the list */
143 L->nci--; 201 L->nci--;
144 ci->next = next2; /* remove 'next' from the list */ 202 luaM_free(L, next); /* free next */
145 next2->previous = ci; 203 if (next2 == NULL)
146 ci = next2; /* keep next's next */ 204 break; /* no more elements */
205 else {
206 next2->previous = ci;
207 ci = next2; /* continue */
208 }
147 } 209 }
210 L->nCcalls -= L->nci; /* adjust result */
148} 211}
149 212
150 213
151static void stack_init (lua_State *L1, lua_State *L) { 214static void stack_init (lua_State *L1, lua_State *L) {
152 int i; CallInfo *ci; 215 int i; CallInfo *ci;
153 /* initialize stack array */ 216 /* initialize stack array */
154 L1->stack = luaM_newvector(L, BASIC_STACK_SIZE, TValue); 217 L1->stack = luaM_newvector(L, BASIC_STACK_SIZE, StackValue);
155 L1->stacksize = BASIC_STACK_SIZE; 218 L1->stacksize = BASIC_STACK_SIZE;
156 for (i = 0; i < BASIC_STACK_SIZE; i++) 219 for (i = 0; i < BASIC_STACK_SIZE; i++)
157 setnilvalue(L1->stack + i); /* erase new stack */ 220 setnilvalue(s2v(L1->stack + i)); /* erase new stack */
158 L1->top = L1->stack; 221 L1->top = L1->stack;
159 L1->stack_last = L1->stack + L1->stacksize - EXTRA_STACK; 222 L1->stack_last = L1->stack + L1->stacksize - EXTRA_STACK;
160 /* initialize first ci */ 223 /* initialize first ci */
161 ci = &L1->base_ci; 224 ci = &L1->base_ci;
162 ci->next = ci->previous = NULL; 225 ci->next = ci->previous = NULL;
163 ci->callstatus = 0; 226 ci->callstatus = CIST_C;
164 ci->func = L1->top; 227 ci->func = L1->top;
165 setnilvalue(L1->top++); /* 'function' entry for this 'ci' */ 228 ci->u.c.k = NULL;
229 ci->nresults = 0;
230 setnilvalue(s2v(L1->top)); /* 'function' entry for this 'ci' */
231 L1->top++;
166 ci->top = L1->top + LUA_MINSTACK; 232 ci->top = L1->top + LUA_MINSTACK;
167 L1->ci = ci; 233 L1->ci = ci;
168} 234}
@@ -198,7 +264,8 @@ static void init_registry (lua_State *L, global_State *g) {
198 264
199/* 265/*
200** open parts of the state that may cause memory-allocation errors. 266** open parts of the state that may cause memory-allocation errors.
201** ('g->version' != NULL flags that the state was completely build) 267** ('g->nilvalue' being a nil value flags that the state was completely
268** build.)
202*/ 269*/
203static void f_luaopen (lua_State *L, void *ud) { 270static void f_luaopen (lua_State *L, void *ud) {
204 global_State *g = G(L); 271 global_State *g = G(L);
@@ -209,7 +276,7 @@ static void f_luaopen (lua_State *L, void *ud) {
209 luaT_init(L); 276 luaT_init(L);
210 luaX_init(L); 277 luaX_init(L);
211 g->gcrunning = 1; /* allow gc */ 278 g->gcrunning = 1; /* allow gc */
212 g->version = lua_version(NULL); 279 setnilvalue(&g->nilvalue);
213 luai_userstateopen(L); 280 luai_userstateopen(L);
214} 281}
215 282
@@ -226,14 +293,12 @@ static void preinit_thread (lua_State *L, global_State *g) {
226 L->stacksize = 0; 293 L->stacksize = 0;
227 L->twups = L; /* thread has no upvalues */ 294 L->twups = L; /* thread has no upvalues */
228 L->errorJmp = NULL; 295 L->errorJmp = NULL;
229 L->nCcalls = 0;
230 L->hook = NULL; 296 L->hook = NULL;
231 L->hookmask = 0; 297 L->hookmask = 0;
232 L->basehookcount = 0; 298 L->basehookcount = 0;
233 L->allowhook = 1; 299 L->allowhook = 1;
234 resethookcount(L); 300 resethookcount(L);
235 L->openupval = NULL; 301 L->openupval = NULL;
236 L->nny = 1;
237 L->status = LUA_OK; 302 L->status = LUA_OK;
238 L->errfunc = 0; 303 L->errfunc = 0;
239} 304}
@@ -241,9 +306,9 @@ static void preinit_thread (lua_State *L, global_State *g) {
241 306
242static void close_state (lua_State *L) { 307static void close_state (lua_State *L) {
243 global_State *g = G(L); 308 global_State *g = G(L);
244 luaF_close(L, L->stack); /* close all upvalues for this thread */ 309 luaF_close(L, L->stack, CLOSEPROTECT); /* close all upvalues */
245 luaC_freeallobjects(L); /* collect all objects */ 310 luaC_freeallobjects(L); /* collect all objects */
246 if (g->version) /* closing a fully built state? */ 311 if (ttisnil(&g->nilvalue)) /* closing a fully built state? */
247 luai_userstateclose(L); 312 luai_userstateclose(L);
248 luaM_freearray(L, G(L)->strt.hash, G(L)->strt.size); 313 luaM_freearray(L, G(L)->strt.hash, G(L)->strt.size);
249 freestack(L); 314 freestack(L);
@@ -260,14 +325,15 @@ LUA_API lua_State *lua_newthread (lua_State *L) {
260 /* create new thread */ 325 /* create new thread */
261 L1 = &cast(LX *, luaM_newobject(L, LUA_TTHREAD, sizeof(LX)))->l; 326 L1 = &cast(LX *, luaM_newobject(L, LUA_TTHREAD, sizeof(LX)))->l;
262 L1->marked = luaC_white(g); 327 L1->marked = luaC_white(g);
263 L1->tt = LUA_TTHREAD; 328 L1->tt = LUA_VTHREAD;
264 /* link it on list 'allgc' */ 329 /* link it on list 'allgc' */
265 L1->next = g->allgc; 330 L1->next = g->allgc;
266 g->allgc = obj2gco(L1); 331 g->allgc = obj2gco(L1);
267 /* anchor it on L stack */ 332 /* anchor it on L stack */
268 setthvalue(L, L->top, L1); 333 setthvalue2s(L, L->top, L1);
269 api_incr_top(L); 334 api_incr_top(L);
270 preinit_thread(L1, g); 335 preinit_thread(L1, g);
336 L1->nCcalls = getCcalls(L);
271 L1->hookmask = L->hookmask; 337 L1->hookmask = L->hookmask;
272 L1->basehookcount = L->basehookcount; 338 L1->basehookcount = L->basehookcount;
273 L1->hook = L->hook; 339 L1->hook = L->hook;
@@ -284,7 +350,7 @@ LUA_API lua_State *lua_newthread (lua_State *L) {
284 350
285void luaE_freethread (lua_State *L, lua_State *L1) { 351void luaE_freethread (lua_State *L, lua_State *L1) {
286 LX *l = fromstate(L1); 352 LX *l = fromstate(L1);
287 luaF_close(L1, L1->stack); /* close all upvalues for this thread */ 353 luaF_close(L1, L1->stack, NOCLOSINGMETH); /* close all upvalues */
288 lua_assert(L1->openupval == NULL); 354 lua_assert(L1->openupval == NULL);
289 luai_userstatefree(L, L1); 355 luai_userstatefree(L, L1);
290 freestack(L1); 356 freestack(L1);
@@ -292,6 +358,28 @@ void luaE_freethread (lua_State *L, lua_State *L1) {
292} 358}
293 359
294 360
361int lua_resetthread (lua_State *L) {
362 CallInfo *ci;
363 int status;
364 lua_lock(L);
365 L->ci = ci = &L->base_ci; /* unwind CallInfo list */
366 setnilvalue(s2v(L->stack)); /* 'function' entry for basic 'ci' */
367 ci->func = L->stack;
368 ci->callstatus = CIST_C;
369 status = luaF_close(L, L->stack, CLOSEPROTECT);
370 if (status != CLOSEPROTECT) /* real errors? */
371 luaD_seterrorobj(L, status, L->stack + 1);
372 else {
373 status = LUA_OK;
374 L->top = L->stack + 1;
375 }
376 ci->top = L->top + LUA_MINSTACK;
377 L->status = status;
378 lua_unlock(L);
379 return status;
380}
381
382
295LUA_API lua_State *lua_newstate (lua_Alloc f, void *ud) { 383LUA_API lua_State *lua_newstate (lua_Alloc f, void *ud) {
296 int i; 384 int i;
297 lua_State *L; 385 lua_State *L;
@@ -300,34 +388,43 @@ LUA_API lua_State *lua_newstate (lua_Alloc f, void *ud) {
300 if (l == NULL) return NULL; 388 if (l == NULL) return NULL;
301 L = &l->l.l; 389 L = &l->l.l;
302 g = &l->g; 390 g = &l->g;
303 L->next = NULL; 391 L->tt = LUA_VTHREAD;
304 L->tt = LUA_TTHREAD;
305 g->currentwhite = bitmask(WHITE0BIT); 392 g->currentwhite = bitmask(WHITE0BIT);
306 L->marked = luaC_white(g); 393 L->marked = luaC_white(g);
307 preinit_thread(L, g); 394 preinit_thread(L, g);
395 g->allgc = obj2gco(L); /* by now, only object is the main thread */
396 L->next = NULL;
397 g->Cstacklimit = L->nCcalls = LUAI_MAXCSTACK + CSTACKERR;
308 g->frealloc = f; 398 g->frealloc = f;
309 g->ud = ud; 399 g->ud = ud;
400 g->warnf = NULL;
401 g->ud_warn = NULL;
310 g->mainthread = L; 402 g->mainthread = L;
311 g->seed = makeseed(L); 403 g->seed = luai_makeseed(L);
312 g->gcrunning = 0; /* no GC while building state */ 404 g->gcrunning = 0; /* no GC while building state */
313 g->GCestimate = 0;
314 g->strt.size = g->strt.nuse = 0; 405 g->strt.size = g->strt.nuse = 0;
315 g->strt.hash = NULL; 406 g->strt.hash = NULL;
316 setnilvalue(&g->l_registry); 407 setnilvalue(&g->l_registry);
317 g->panic = NULL; 408 g->panic = NULL;
318 g->version = NULL;
319 g->gcstate = GCSpause; 409 g->gcstate = GCSpause;
320 g->gckind = KGC_NORMAL; 410 g->gckind = KGC_INC;
321 g->allgc = g->finobj = g->tobefnz = g->fixedgc = NULL; 411 g->gcemergency = 0;
412 g->finobj = g->tobefnz = g->fixedgc = NULL;
413 g->survival = g->old = g->reallyold = NULL;
414 g->finobjsur = g->finobjold = g->finobjrold = NULL;
322 g->sweepgc = NULL; 415 g->sweepgc = NULL;
323 g->gray = g->grayagain = NULL; 416 g->gray = g->grayagain = NULL;
324 g->weak = g->ephemeron = g->allweak = NULL; 417 g->weak = g->ephemeron = g->allweak = NULL;
325 g->twups = NULL; 418 g->twups = NULL;
326 g->totalbytes = sizeof(LG); 419 g->totalbytes = sizeof(LG);
327 g->GCdebt = 0; 420 g->GCdebt = 0;
328 g->gcfinnum = 0; 421 g->lastatomic = 0;
329 g->gcpause = LUAI_GCPAUSE; 422 setivalue(&g->nilvalue, 0); /* to signal that state is not yet built */
330 g->gcstepmul = LUAI_GCMUL; 423 setgcparam(g->gcpause, LUAI_GCPAUSE);
424 setgcparam(g->gcstepmul, LUAI_GCMUL);
425 g->gcstepsize = LUAI_GCSTEPSIZE;
426 setgcparam(g->genmajormul, LUAI_GENMAJORMUL);
427 g->genminormul = LUAI_GENMINORMUL;
331 for (i=0; i < LUA_NUMTAGS; i++) g->mt[i] = NULL; 428 for (i=0; i < LUA_NUMTAGS; i++) g->mt[i] = NULL;
332 if (luaD_rawrunprotected(L, f_luaopen, NULL) != LUA_OK) { 429 if (luaD_rawrunprotected(L, f_luaopen, NULL) != LUA_OK) {
333 /* memory allocation error: free partial state */ 430 /* memory allocation error: free partial state */
@@ -345,3 +442,26 @@ LUA_API void lua_close (lua_State *L) {
345} 442}
346 443
347 444
445void luaE_warning (lua_State *L, const char *msg, int tocont) {
446 lua_WarnFunction wf = G(L)->warnf;
447 if (wf != NULL)
448 wf(G(L)->ud_warn, msg, tocont);
449}
450
451
452/*
453** Generate a warning from an error message
454*/
455void luaE_warnerror (lua_State *L, const char *where) {
456 TValue *errobj = s2v(L->top - 1); /* error object */
457 const char *msg = (ttisstring(errobj))
458 ? svalue(errobj)
459 : "error object is not a string";
460 /* produce warning "error in %s (%s)" (where, msg) */
461 luaE_warning(L, "error in ", 1);
462 luaE_warning(L, where, 1);
463 luaE_warning(L, " (", 1);
464 luaE_warning(L, msg, 1);
465 luaE_warning(L, ")", 0);
466}
467