Skip to content

Commit a024b63

Browse files
committed
update lua
1 parent f55b698 commit a024b63

9 files changed

Lines changed: 52 additions & 34 deletions

File tree

3rd/lua55/lapi.c

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -366,7 +366,7 @@ LUA_API int lua_compare (lua_State *L, int index1, int index2, int op) {
366366
}
367367

368368

369-
LUA_API unsigned (lua_numbertocstring) (lua_State *L, int idx, char *buff) {
369+
LUA_API unsigned lua_numbertocstring (lua_State *L, int idx, char *buff) {
370370
const TValue *o = index2value(L, idx);
371371
if (ttisnumber(o)) {
372372
unsigned len = luaO_tostringbuff(o, buff);
@@ -1201,11 +1201,16 @@ LUA_API int lua_gc (lua_State *L, int what, ...) {
12011201
case LUA_GCSTEP: {
12021202
lu_byte oldstp = g->gcstp;
12031203
l_mem n = cast(l_mem, va_arg(argp, size_t));
1204+
l_mem newdebt;
12041205
int work = 0; /* true if GC did some work */
12051206
g->gcstp = 0; /* allow GC to run (other bits must be zero here) */
12061207
if (n <= 0)
1207-
n = g->GCdebt; /* force to run one basic step */
1208-
luaE_setdebt(g, g->GCdebt - n);
1208+
newdebt = 0; /* force to run one basic step */
1209+
else if (g->GCdebt >= n - MAX_LMEM) /* no overflow? */
1210+
newdebt = g->GCdebt - n;
1211+
else /* overflow */
1212+
newdebt = -MAX_LMEM; /* set debt to miminum value */
1213+
luaE_setdebt(g, newdebt);
12091214
luaC_condGC(L, (void)0, work = 1);
12101215
if (work && g->gcstate == GCSpause) /* end of cycle? */
12111216
res = 1; /* signal it */

3rd/lua55/lauxlib.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -81,8 +81,8 @@ LUALIB_API int (luaL_checkoption) (lua_State *L, int arg, const char *def,
8181
LUALIB_API int (luaL_fileresult) (lua_State *L, int stat, const char *fname);
8282
LUALIB_API int (luaL_execresult) (lua_State *L, int stat);
8383

84-
LUALIB_API void *luaL_alloc (void *ud, void *ptr, size_t osize,
85-
size_t nsize);
84+
LUALIB_API void *(luaL_alloc) (void *ud, void *ptr, size_t osize,
85+
size_t nsize);
8686

8787

8888
/* predefined references */
@@ -103,7 +103,7 @@ LUALIB_API int (luaL_loadstring) (lua_State *L, const char *s);
103103

104104
LUALIB_API lua_State *(luaL_newstate) (void);
105105

106-
LUALIB_API unsigned luaL_makeseed (lua_State *L);
106+
LUALIB_API unsigned (luaL_makeseed) (lua_State *L);
107107

108108
LUALIB_API lua_Integer (luaL_len) (lua_State *L, int idx);
109109

3rd/lua55/ldo.c

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -224,13 +224,21 @@ l_noret luaD_errerr (lua_State *L) {
224224

225225

226226
/*
227-
** Check whether stack has enough space to run a simple function (such
228-
** as a finalizer): At least BASIC_STACK_SIZE in the Lua stack and
229-
** 2 slots in the C stack.
227+
** Check whether stacks have enough space to run a simple function (such
228+
** as a finalizer): At least BASIC_STACK_SIZE in the Lua stack, two
229+
** available CallInfos, and two "slots" in the C stack.
230230
*/
231231
int luaD_checkminstack (lua_State *L) {
232-
return ((stacksize(L) < MAXSTACK - BASIC_STACK_SIZE) &&
233-
(getCcalls(L) < LUAI_MAXCCALLS - 2));
232+
if (getCcalls(L) >= LUAI_MAXCCALLS - 2)
233+
return 0; /* not enough C-stack slots */
234+
if (L->ci->next == NULL && luaE_extendCI(L, 0) == NULL)
235+
return 0; /* unable to allocate first ci */
236+
if (L->ci->next->next == NULL && luaE_extendCI(L, 0) == NULL)
237+
return 0; /* unable to allocate second ci */
238+
if (L->stack_last.p - L->top.p >= BASIC_STACK_SIZE)
239+
return 1; /* enough (BASIC_STACK_SIZE) free slots in the Lua stack */
240+
else /* try to grow stack to a size with enough free slots */
241+
return luaD_growstack(L, BASIC_STACK_SIZE, 0);
234242
}
235243

236244

@@ -619,7 +627,7 @@ void luaD_poscall (lua_State *L, CallInfo *ci, int nres) {
619627

620628

621629

622-
#define next_ci(L) (L->ci->next ? L->ci->next : luaE_extendCI(L))
630+
#define next_ci(L) (L->ci->next ? L->ci->next : luaE_extendCI(L, 1))
623631

624632

625633
/*

3rd/lua55/lgc.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1293,7 +1293,7 @@ static void finishgencycle (lua_State *L, global_State *g) {
12931293
correctgraylists(g);
12941294
checkSizes(L, g);
12951295
g->gcstate = GCSpropagate; /* skip restart */
1296-
if (!g->gcemergency && luaD_checkminstack(L))
1296+
if (g->tobefnz != NULL && !g->gcemergency && luaD_checkminstack(L))
12971297
callallpendingfinalizers(L);
12981298
}
12991299

@@ -1672,7 +1672,7 @@ static l_mem singlestep (lua_State *L, int fast) {
16721672
GCTM(L); /* call one finalizer */
16731673
stepresult = CWUFIN;
16741674
}
1675-
else { /* no more finalizers or emergency mode or no enough stack
1675+
else { /* no more finalizers or emergency mode or not enough stack
16761676
to run finalizers */
16771677
g->gcstate = GCSpause; /* finish collection */
16781678
stepresult = step2pause;

3rd/lua55/lstate.c

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -68,14 +68,19 @@ void luaE_setdebt (global_State *g, l_mem debt) {
6868
}
6969

7070

71-
CallInfo *luaE_extendCI (lua_State *L) {
71+
CallInfo *luaE_extendCI (lua_State *L, int err) {
7272
CallInfo *ci;
73-
lua_assert(L->ci->next == NULL);
74-
ci = luaM_new(L, CallInfo);
75-
lua_assert(L->ci->next == NULL);
76-
L->ci->next = ci;
73+
ci = luaM_reallocvector(L, NULL, 0, 1, CallInfo);
74+
if (l_unlikely(ci == NULL)) { /* allocation failed? */
75+
if (err)
76+
luaM_error(L); /* raise the error */
77+
return NULL; /* else only report it */
78+
}
79+
ci->next = L->ci->next;
7780
ci->previous = L->ci;
78-
ci->next = NULL;
81+
L->ci->next = ci;
82+
if (ci->next)
83+
ci->next->previous = ci;
7984
ci->u.l.trap = 0;
8085
L->nci++;
8186
return ci;

3rd/lua55/lstate.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -438,7 +438,7 @@ union GCUnion {
438438
LUAI_FUNC void luaE_setdebt (global_State *g, l_mem debt);
439439
LUAI_FUNC void luaE_freethread (lua_State *L, lua_State *L1);
440440
LUAI_FUNC lu_mem luaE_threadsize (lua_State *L);
441-
LUAI_FUNC CallInfo *luaE_extendCI (lua_State *L);
441+
LUAI_FUNC CallInfo *luaE_extendCI (lua_State *L, int err);
442442
LUAI_FUNC void luaE_shrinkCI (lua_State *L);
443443
LUAI_FUNC void luaE_checkcstack (lua_State *L);
444444
LUAI_FUNC void luaE_incCstack (lua_State *L);

3rd/lua55/lstrlib.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -141,8 +141,8 @@ static int str_rep (lua_State *L) {
141141
const char *s = luaL_checklstring(L, 1, &len);
142142
lua_Integer n = luaL_checkinteger(L, 2);
143143
const char *sep = luaL_optlstring(L, 3, "", &lsep);
144-
if (n <= 0)
145-
lua_pushliteral(L, "");
144+
if (n <= 0 || (len | lsep) == 0)
145+
lua_pushliteral(L, ""); /* no repetitions or both strings empty */
146146
else if (l_unlikely(len > MAX_SIZE - lsep ||
147147
cast_st2S(len + lsep) > cast_st2S(MAX_SIZE) / n))
148148
return luaL_error(L, "resulting string too large");
@@ -968,7 +968,7 @@ static int str_gsub (lua_State *L) {
968968
reprepstate(&ms); /* (re)prepare state for new match */
969969
if ((e = match(&ms, src, p)) != NULL && e != lastmatch) { /* match? */
970970
n++;
971-
changed = add_value(&ms, &b, src, e, tr) | changed;
971+
changed = add_value(&ms, &b, src, e, tr) || changed;
972972
src = lastmatch = e;
973973
}
974974
else if (src < ms.src_end) /* otherwise, skip one character */
@@ -1726,7 +1726,7 @@ static int str_packsize (lua_State *L) {
17261726
luaL_argcheck(L, opt != Kstring && opt != Kzstr, 1,
17271727
"variable-length format");
17281728
size += ntoalign; /* total space used by option */
1729-
luaL_argcheck(L, totalsize <= LUA_MAXINTEGER - size,
1729+
luaL_argcheck(L, totalsize <= MAX_SIZE - size,
17301730
1, "format result too large");
17311731
totalsize += size;
17321732
}

3rd/lua55/ltable.c

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -651,10 +651,9 @@ static void reinserthash (lua_State *L, Table *ot, Table *t) {
651651

652652

653653
/*
654-
** Exchange the hash part of 't1' and 't2'. (In 'flags', only the
655-
** dummy bit must be exchanged: The 'isrealasize' is not related
656-
** to the hash part, and the metamethod bits do not change during
657-
** a resize, so the "real" table can keep their values.)
654+
** Exchange the hash part of 't1' and 't2'. (In 'flags', only the dummy
655+
** bit must be exchanged: The metamethod bits do not change during a
656+
** resize, so the "real" table can keep their values.)
658657
*/
659658
static void exchangehashpart (Table *t1, Table *t2) {
660659
lu_byte lsizenode = t1->lsizenode;
@@ -1156,14 +1155,15 @@ void luaH_finishset (lua_State *L, Table *t, const TValue *key,
11561155
lua_assert(hres != HOK);
11571156
if (hres == HNOTFOUND) {
11581157
TValue aux;
1158+
const TValue *actk = key; /* actual key to insert */
11591159
if (l_unlikely(ttisnil(key)))
11601160
luaG_runerror(L, "table index is nil");
11611161
else if (ttisfloat(key)) {
11621162
lua_Number f = fltvalue(key);
11631163
lua_Integer k;
1164-
if (luaV_flttointeger(f, &k, F2Ieq)) {
1165-
setivalue(&aux, k); /* key is equal to an integer */
1166-
key = &aux; /* insert it as an integer */
1164+
if (luaV_flttointeger(f, &k, F2Ieq)) { /* is key equal to an integer? */
1165+
setivalue(&aux, k);
1166+
actk = &aux; /* use the integer as the key */
11671167
}
11681168
else if (l_unlikely(luai_numisnan(f)))
11691169
luaG_runerror(L, "table index is NaN");
@@ -1176,7 +1176,7 @@ void luaH_finishset (lua_State *L, Table *t, const TValue *key,
11761176
L->top.p--;
11771177
return;
11781178
}
1179-
luaH_newkey(L, t, key, value);
1179+
luaH_newkey(L, t, actk, value);
11801180
}
11811181
else if (hres > 0) { /* regular Node? */
11821182
setobj2t(L, gval(gnode(t, hres - HFIRSTNODE)), value);

3rd/lua55/ltm.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ typedef enum {
4949
** Mask with 1 in all fast-access methods. A 1 in any of these bits
5050
** in the flag of a (meta)table means the metatable does not have the
5151
** corresponding metamethod field. (Bit 6 of the flag indicates that
52-
** the table is using the dummy node; bit 7 is used for 'isrealasize'.)
52+
** the table is using the dummy node.)
5353
*/
5454
#define maskflags cast_byte(~(~0u << (TM_EQ + 1)))
5555

0 commit comments

Comments
 (0)