aboutsummaryrefslogtreecommitdiff
path: root/src/lua/lstate.c
diff options
context:
space:
mode:
authorLi Jin <dragon-fly@qq.com>2021-03-03 21:31:01 +0800
committerLi Jin <dragon-fly@qq.com>2021-03-03 21:33:37 +0800
commit1df786307c1983b8ce693e3916081a8bcd4e95ae (patch)
tree6c7aeb2198d825877fd3d179c394b7a5c1f06a17 /src/lua/lstate.c
parent66168b112b707172b9035edf8c1daed469781e06 (diff)
downloadyuescript-1df786307c1983b8ce693e3916081a8bcd4e95ae.tar.gz
yuescript-1df786307c1983b8ce693e3916081a8bcd4e95ae.tar.bz2
yuescript-1df786307c1983b8ce693e3916081a8bcd4e95ae.zip
add new metatable syntax for issue #41, fix reusing local variable issue, update built-in Lua.
Diffstat (limited to 'src/lua/lstate.c')
-rw-r--r--src/lua/lstate.c24
1 files changed, 13 insertions, 11 deletions
diff --git a/src/lua/lstate.c b/src/lua/lstate.c
index 92ccbf9..c5e3b43 100644
--- a/src/lua/lstate.c
+++ b/src/lua/lstate.c
@@ -172,7 +172,7 @@ void luaE_checkcstack (lua_State *L) {
172 172
173LUAI_FUNC void luaE_incCstack (lua_State *L) { 173LUAI_FUNC void luaE_incCstack (lua_State *L) {
174 L->nCcalls++; 174 L->nCcalls++;
175 if (unlikely(getCcalls(L) >= LUAI_MAXCCALLS)) 175 if (l_unlikely(getCcalls(L) >= LUAI_MAXCCALLS))
176 luaE_checkcstack(L); 176 luaE_checkcstack(L);
177} 177}
178 178
@@ -181,6 +181,7 @@ static void stack_init (lua_State *L1, lua_State *L) {
181 int i; CallInfo *ci; 181 int i; CallInfo *ci;
182 /* initialize stack array */ 182 /* initialize stack array */
183 L1->stack = luaM_newvector(L, BASIC_STACK_SIZE + EXTRA_STACK, StackValue); 183 L1->stack = luaM_newvector(L, BASIC_STACK_SIZE + EXTRA_STACK, StackValue);
184 L1->tbclist = L1->stack;
184 for (i = 0; i < BASIC_STACK_SIZE + EXTRA_STACK; i++) 185 for (i = 0; i < BASIC_STACK_SIZE + EXTRA_STACK; i++)
185 setnilvalue(s2v(L1->stack + i)); /* erase new stack */ 186 setnilvalue(s2v(L1->stack + i)); /* erase new stack */
186 L1->top = L1->stack; 187 L1->top = L1->stack;
@@ -226,8 +227,6 @@ static void init_registry (lua_State *L, global_State *g) {
226 227
227/* 228/*
228** open parts of the state that may cause memory-allocation errors. 229** open parts of the state that may cause memory-allocation errors.
229** ('g->nilvalue' being a nil value flags that the state was completely
230** build.)
231*/ 230*/
232static void f_luaopen (lua_State *L, void *ud) { 231static void f_luaopen (lua_State *L, void *ud) {
233 global_State *g = G(L); 232 global_State *g = G(L);
@@ -238,7 +237,7 @@ static void f_luaopen (lua_State *L, void *ud) {
238 luaT_init(L); 237 luaT_init(L);
239 luaX_init(L); 238 luaX_init(L);
240 g->gcrunning = 1; /* allow gc */ 239 g->gcrunning = 1; /* allow gc */
241 setnilvalue(&g->nilvalue); 240 setnilvalue(&g->nilvalue); /* now state is complete */
242 luai_userstateopen(L); 241 luai_userstateopen(L);
243} 242}
244 243
@@ -253,6 +252,7 @@ static void preinit_thread (lua_State *L, global_State *g) {
253 L->ci = NULL; 252 L->ci = NULL;
254 L->nci = 0; 253 L->nci = 0;
255 L->twups = L; /* thread has no upvalues */ 254 L->twups = L; /* thread has no upvalues */
255 L->nCcalls = 0;
256 L->errorJmp = NULL; 256 L->errorJmp = NULL;
257 L->hook = NULL; 257 L->hook = NULL;
258 L->hookmask = 0; 258 L->hookmask = 0;
@@ -268,10 +268,13 @@ static void preinit_thread (lua_State *L, global_State *g) {
268 268
269static void close_state (lua_State *L) { 269static void close_state (lua_State *L) {
270 global_State *g = G(L); 270 global_State *g = G(L);
271 luaD_closeprotected(L, 0, LUA_OK); /* close all upvalues */ 271 if (!completestate(g)) /* closing a partially built state? */
272 luaC_freeallobjects(L); /* collect all objects */ 272 luaC_freeallobjects(L); /* jucst collect its objects */
273 if (ttisnil(&g->nilvalue)) /* closing a fully built state? */ 273 else { /* closing a fully built state */
274 luaD_closeprotected(L, 1, LUA_OK); /* close all upvalues */
275 luaC_freeallobjects(L); /* collect all objects */
274 luai_userstateclose(L); 276 luai_userstateclose(L);
277 }
275 luaM_freearray(L, G(L)->strt.hash, G(L)->strt.size); 278 luaM_freearray(L, G(L)->strt.hash, G(L)->strt.size);
276 freestack(L); 279 freestack(L);
277 lua_assert(gettotalbytes(g) == sizeof(LG)); 280 lua_assert(gettotalbytes(g) == sizeof(LG));
@@ -296,7 +299,6 @@ LUA_API lua_State *lua_newthread (lua_State *L) {
296 setthvalue2s(L, L->top, L1); 299 setthvalue2s(L, L->top, L1);
297 api_incr_top(L); 300 api_incr_top(L);
298 preinit_thread(L1, g); 301 preinit_thread(L1, g);
299 L1->nCcalls = 0;
300 L1->hookmask = L->hookmask; 302 L1->hookmask = L->hookmask;
301 L1->basehookcount = L->basehookcount; 303 L1->basehookcount = L->basehookcount;
302 L1->hook = L->hook; 304 L1->hook = L->hook;
@@ -313,7 +315,7 @@ LUA_API lua_State *lua_newthread (lua_State *L) {
313 315
314void luaE_freethread (lua_State *L, lua_State *L1) { 316void luaE_freethread (lua_State *L, lua_State *L1) {
315 LX *l = fromstate(L1); 317 LX *l = fromstate(L1);
316 luaF_close(L1, L1->stack, NOCLOSINGMETH, 0); /* close all upvalues */ 318 luaF_closeupval(L1, L1->stack); /* close all upvalues */
317 lua_assert(L1->openupval == NULL); 319 lua_assert(L1->openupval == NULL);
318 luai_userstatefree(L, L1); 320 luai_userstatefree(L, L1);
319 freestack(L1); 321 freestack(L1);
@@ -328,7 +330,7 @@ int luaE_resetthread (lua_State *L, int status) {
328 ci->callstatus = CIST_C; 330 ci->callstatus = CIST_C;
329 if (status == LUA_YIELD) 331 if (status == LUA_YIELD)
330 status = LUA_OK; 332 status = LUA_OK;
331 status = luaD_closeprotected(L, 0, status); 333 status = luaD_closeprotected(L, 1, status);
332 if (status != LUA_OK) /* errors? */ 334 if (status != LUA_OK) /* errors? */
333 luaD_seterrorobj(L, status, L->stack + 1); 335 luaD_seterrorobj(L, status, L->stack + 1);
334 else 336 else
@@ -363,7 +365,6 @@ LUA_API lua_State *lua_newstate (lua_Alloc f, void *ud) {
363 preinit_thread(L, g); 365 preinit_thread(L, g);
364 g->allgc = obj2gco(L); /* by now, only object is the main thread */ 366 g->allgc = obj2gco(L); /* by now, only object is the main thread */
365 L->next = NULL; 367 L->next = NULL;
366 L->nCcalls = 0;
367 incnny(L); /* main thread is always non yieldable */ 368 incnny(L); /* main thread is always non yieldable */
368 g->frealloc = f; 369 g->frealloc = f;
369 g->ud = ud; 370 g->ud = ud;
@@ -378,6 +379,7 @@ LUA_API lua_State *lua_newstate (lua_Alloc f, void *ud) {
378 g->panic = NULL; 379 g->panic = NULL;
379 g->gcstate = GCSpause; 380 g->gcstate = GCSpause;
380 g->gckind = KGC_INC; 381 g->gckind = KGC_INC;
382 g->gcstopem = 0;
381 g->gcemergency = 0; 383 g->gcemergency = 0;
382 g->finobj = g->tobefnz = g->fixedgc = NULL; 384 g->finobj = g->tobefnz = g->fixedgc = NULL;
383 g->firstold1 = g->survival = g->old1 = g->reallyold = NULL; 385 g->firstold1 = g->survival = g->old1 = g->reallyold = NULL;