Skip to content

Commit 8abf59d

Browse files
committed
Update Lua 5.4 to 5.4.3 and bump version to 543.0.0
1 parent 45324ee commit 8abf59d

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

61 files changed

+1050
-702
lines changed

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "lua-src"
3-
version = "542.0.0"
3+
version = "543.0.0"
44
authors = ["Aleksandr Orlenko <[email protected]>"]
55
edition = "2018"
66
repository = "https://github.com/khvzak/lua-src-rs"

lua-5.4.2/lapi.c renamed to lua-5.4.3/lapi.c

Lines changed: 43 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ const char lua_ident[] =
3939

4040

4141
/*
42-
** Test for a valid index.
42+
** Test for a valid index (one that is not the 'nilvalue').
4343
** '!ttisnil(o)' implies 'o != &G(L)->nilvalue', so it is not needed.
4444
** However, it covers the most common cases in a faster way.
4545
*/
@@ -74,7 +74,8 @@ static TValue *index2value (lua_State *L, int idx) {
7474
return &G(L)->nilvalue; /* it has no upvalues */
7575
else {
7676
CClosure *func = clCvalue(s2v(ci->func));
77-
return (idx <= func->nupvalues) ? &func->upvalue[idx-1] : &G(L)->nilvalue;
77+
return (idx <= func->nupvalues) ? &func->upvalue[idx-1]
78+
: &G(L)->nilvalue;
7879
}
7980
}
8081
}
@@ -172,7 +173,7 @@ LUA_API int lua_gettop (lua_State *L) {
172173

173174
LUA_API void lua_settop (lua_State *L, int idx) {
174175
CallInfo *ci;
175-
StkId func;
176+
StkId func, newtop;
176177
ptrdiff_t diff; /* difference for new top */
177178
lua_lock(L);
178179
ci = L->ci;
@@ -187,9 +188,26 @@ LUA_API void lua_settop (lua_State *L, int idx) {
187188
api_check(L, -(idx+1) <= (L->top - (func + 1)), "invalid new top");
188189
diff = idx + 1; /* will "subtract" index (as it is negative) */
189190
}
190-
if (diff < 0 && hastocloseCfunc(ci->nresults))
191-
luaF_close(L, L->top + diff, LUA_OK);
192-
L->top += diff; /* correct top only after closing any upvalue */
191+
api_check(L, L->tbclist < L->top, "previous pop of an unclosed slot");
192+
newtop = L->top + diff;
193+
if (diff < 0 && L->tbclist >= newtop) {
194+
lua_assert(hastocloseCfunc(ci->nresults));
195+
luaF_close(L, newtop, CLOSEKTOP, 0);
196+
}
197+
L->top = newtop; /* correct top only after closing any upvalue */
198+
lua_unlock(L);
199+
}
200+
201+
202+
LUA_API void lua_closeslot (lua_State *L, int idx) {
203+
StkId level;
204+
lua_lock(L);
205+
level = index2stack(L, idx);
206+
api_check(L, hastocloseCfunc(L->ci->nresults) && L->tbclist == level,
207+
"no variable to close at given level");
208+
luaF_close(L, level, CLOSEKTOP, 0);
209+
level = index2stack(L, idx); /* stack may be moved */
210+
setnilvalue(s2v(level));
193211
lua_unlock(L);
194212
}
195213

@@ -629,11 +647,21 @@ static int auxgetstr (lua_State *L, const TValue *t, const char *k) {
629647
}
630648

631649

650+
/*
651+
** Get the global table in the registry. Since all predefined
652+
** indices in the registry were inserted right when the registry
653+
** was created and never removed, they must always be in the array
654+
** part of the registry.
655+
*/
656+
#define getGtable(L) \
657+
(&hvalue(&G(L)->l_registry)->array[LUA_RIDX_GLOBALS - 1])
658+
659+
632660
LUA_API int lua_getglobal (lua_State *L, const char *name) {
633-
Table *reg;
661+
const TValue *G;
634662
lua_lock(L);
635-
reg = hvalue(&G(L)->l_registry);
636-
return auxgetstr(L, luaH_getint(reg, LUA_RIDX_GLOBALS), name);
663+
G = getGtable(L);
664+
return auxgetstr(L, G, name);
637665
}
638666

639667

@@ -811,10 +839,10 @@ static void auxsetstr (lua_State *L, const TValue *t, const char *k) {
811839

812840

813841
LUA_API void lua_setglobal (lua_State *L, const char *name) {
814-
Table *reg;
842+
const TValue *G;
815843
lua_lock(L); /* unlock done in 'auxsetstr' */
816-
reg = hvalue(&G(L)->l_registry);
817-
auxsetstr(L, luaH_getint(reg, LUA_RIDX_GLOBALS), name);
844+
G = getGtable(L);
845+
auxsetstr(L, G, name);
818846
}
819847

820848

@@ -861,12 +889,10 @@ LUA_API void lua_seti (lua_State *L, int idx, lua_Integer n) {
861889

862890
static void aux_rawset (lua_State *L, int idx, TValue *key, int n) {
863891
Table *t;
864-
TValue *slot;
865892
lua_lock(L);
866893
api_checknelems(L, n);
867894
t = gettable(L, idx);
868-
slot = luaH_set(L, t, key);
869-
setobj2t(L, slot, s2v(L->top - 1));
895+
luaH_set(L, t, key, s2v(L->top - 1));
870896
invalidateTMcache(t);
871897
luaC_barrierback(L, obj2gco(t), s2v(L->top - 1));
872898
L->top -= n;
@@ -1063,8 +1089,7 @@ LUA_API int lua_load (lua_State *L, lua_Reader reader, void *data,
10631089
LClosure *f = clLvalue(s2v(L->top - 1)); /* get newly created function */
10641090
if (f->nupvalues >= 1) { /* does it have an upvalue? */
10651091
/* get global table from registry */
1066-
Table *reg = hvalue(&G(L)->l_registry);
1067-
const TValue *gt = luaH_getint(reg, LUA_RIDX_GLOBALS);
1092+
const TValue *gt = getGtable(L);
10681093
/* set global table as 1st upvalue of 'f' (may be LUA_ENV) */
10691094
setobj(L, f->upvals[0]->v, gt);
10701095
luaC_barrier(L, f->upvals[0], gt);
@@ -1240,8 +1265,7 @@ LUA_API void lua_toclose (lua_State *L, int idx) {
12401265
lua_lock(L);
12411266
o = index2stack(L, idx);
12421267
nresults = L->ci->nresults;
1243-
api_check(L, L->openupval == NULL || uplevel(L->openupval) <= o,
1244-
"marked index below or equal new one");
1268+
api_check(L, L->tbclist < o, "given index below or equal a marked one");
12451269
luaF_newtbcupval(L, o); /* create new to-be-closed upvalue */
12461270
if (!hastocloseCfunc(nresults)) /* function not marked yet? */
12471271
L->ci->nresults = codeNresults(nresults); /* mark it */

lua-5.4.2/lapi.h renamed to lua-5.4.3/lapi.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,8 @@
4242

4343
#define hastocloseCfunc(n) ((n) < LUA_MULTRET)
4444

45+
/* Map [-1, inf) (range of 'nresults') into (-inf, -2] */
4546
#define codeNresults(n) (-(n) - 3)
47+
#define decodeNresults(n) (-(n) - 3)
4648

4749
#endif

lua-5.4.2/lauxlib.c renamed to lua-5.4.3/lauxlib.c

Lines changed: 48 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,7 @@ LUALIB_API int luaL_argerror (lua_State *L, int arg, const char *extramsg) {
190190
}
191191

192192

193-
int luaL_typeerror (lua_State *L, int arg, const char *tname) {
193+
LUALIB_API int luaL_typeerror (lua_State *L, int arg, const char *tname) {
194194
const char *msg;
195195
const char *typearg; /* name for the type of the actual argument */
196196
if (luaL_getmetafield(L, arg, "__name") == LUA_TSTRING)
@@ -378,7 +378,7 @@ LUALIB_API int luaL_checkoption (lua_State *L, int arg, const char *def,
378378
** but without 'msg'.)
379379
*/
380380
LUALIB_API void luaL_checkstack (lua_State *L, int space, const char *msg) {
381-
if (!lua_checkstack(L, space)) {
381+
if (l_unlikely(!lua_checkstack(L, space))) {
382382
if (msg)
383383
luaL_error(L, "stack overflow (%s)", msg);
384384
else
@@ -388,20 +388,20 @@ LUALIB_API void luaL_checkstack (lua_State *L, int space, const char *msg) {
388388

389389

390390
LUALIB_API void luaL_checktype (lua_State *L, int arg, int t) {
391-
if (lua_type(L, arg) != t)
391+
if (l_unlikely(lua_type(L, arg) != t))
392392
tag_error(L, arg, t);
393393
}
394394

395395

396396
LUALIB_API void luaL_checkany (lua_State *L, int arg) {
397-
if (lua_type(L, arg) == LUA_TNONE)
397+
if (l_unlikely(lua_type(L, arg) == LUA_TNONE))
398398
luaL_argerror(L, arg, "value expected");
399399
}
400400

401401

402402
LUALIB_API const char *luaL_checklstring (lua_State *L, int arg, size_t *len) {
403403
const char *s = lua_tolstring(L, arg, len);
404-
if (!s) tag_error(L, arg, LUA_TSTRING);
404+
if (l_unlikely(!s)) tag_error(L, arg, LUA_TSTRING);
405405
return s;
406406
}
407407

@@ -420,7 +420,7 @@ LUALIB_API const char *luaL_optlstring (lua_State *L, int arg,
420420
LUALIB_API lua_Number luaL_checknumber (lua_State *L, int arg) {
421421
int isnum;
422422
lua_Number d = lua_tonumberx(L, arg, &isnum);
423-
if (!isnum)
423+
if (l_unlikely(!isnum))
424424
tag_error(L, arg, LUA_TNUMBER);
425425
return d;
426426
}
@@ -442,7 +442,7 @@ static void interror (lua_State *L, int arg) {
442442
LUALIB_API lua_Integer luaL_checkinteger (lua_State *L, int arg) {
443443
int isnum;
444444
lua_Integer d = lua_tointegerx(L, arg, &isnum);
445-
if (!isnum) {
445+
if (l_unlikely(!isnum)) {
446446
interror(L, arg);
447447
}
448448
return d;
@@ -475,7 +475,7 @@ static void *resizebox (lua_State *L, int idx, size_t newsize) {
475475
lua_Alloc allocf = lua_getallocf(L, &ud);
476476
UBox *box = (UBox *)lua_touserdata(L, idx);
477477
void *temp = allocf(ud, box->box, box->bsize, newsize);
478-
if (temp == NULL && newsize > 0) { /* allocation error? */
478+
if (l_unlikely(temp == NULL && newsize > 0)) { /* allocation error? */
479479
lua_pushliteral(L, "not enough memory");
480480
lua_error(L); /* raise a memory error */
481481
}
@@ -515,13 +515,22 @@ static void newbox (lua_State *L) {
515515
#define buffonstack(B) ((B)->b != (B)->init.b)
516516

517517

518+
/*
519+
** Whenever buffer is accessed, slot 'idx' must either be a box (which
520+
** cannot be NULL) or it is a placeholder for the buffer.
521+
*/
522+
#define checkbufferlevel(B,idx) \
523+
lua_assert(buffonstack(B) ? lua_touserdata(B->L, idx) != NULL \
524+
: lua_touserdata(B->L, idx) == (void*)B)
525+
526+
518527
/*
519528
** Compute new size for buffer 'B', enough to accommodate extra 'sz'
520529
** bytes.
521530
*/
522531
static size_t newbuffsize (luaL_Buffer *B, size_t sz) {
523532
size_t newsize = B->size * 2; /* double buffer size */
524-
if (MAX_SIZET - sz < B->n) /* overflow in (B->n + sz)? */
533+
if (l_unlikely(MAX_SIZET - sz < B->n)) /* overflow in (B->n + sz)? */
525534
return luaL_error(B->L, "buffer too large");
526535
if (newsize < B->n + sz) /* double is not big enough? */
527536
newsize = B->n + sz;
@@ -531,10 +540,11 @@ static size_t newbuffsize (luaL_Buffer *B, size_t sz) {
531540

532541
/*
533542
** Returns a pointer to a free area with at least 'sz' bytes in buffer
534-
** 'B'. 'boxidx' is the relative position in the stack where the
535-
** buffer's box is or should be.
543+
** 'B'. 'boxidx' is the relative position in the stack where is the
544+
** buffer's box or its placeholder.
536545
*/
537546
static char *prepbuffsize (luaL_Buffer *B, size_t sz, int boxidx) {
547+
checkbufferlevel(B, boxidx);
538548
if (B->size - B->n >= sz) /* enough space? */
539549
return B->b + B->n;
540550
else {
@@ -545,10 +555,9 @@ static char *prepbuffsize (luaL_Buffer *B, size_t sz, int boxidx) {
545555
if (buffonstack(B)) /* buffer already has a box? */
546556
newbuff = (char *)resizebox(L, boxidx, newsize); /* resize it */
547557
else { /* no box yet */
548-
lua_pushnil(L); /* reserve slot for final result */
558+
lua_remove(L, boxidx); /* remove placeholder */
549559
newbox(L); /* create a new box */
550-
/* move box (and slot) to its intended position */
551-
lua_rotate(L, boxidx - 1, 2);
560+
lua_insert(L, boxidx); /* move box to its intended position */
552561
lua_toclose(L, boxidx);
553562
newbuff = (char *)resizebox(L, boxidx, newsize);
554563
memcpy(newbuff, B->b, B->n * sizeof(char)); /* copy original content */
@@ -583,11 +592,11 @@ LUALIB_API void luaL_addstring (luaL_Buffer *B, const char *s) {
583592

584593
LUALIB_API void luaL_pushresult (luaL_Buffer *B) {
585594
lua_State *L = B->L;
595+
checkbufferlevel(B, -1);
586596
lua_pushlstring(L, B->b, B->n);
587-
if (buffonstack(B)) {
588-
lua_copy(L, -1, -3); /* move string to reserved slot */
589-
lua_pop(L, 2); /* pop string and box (closing the box) */
590-
}
597+
if (buffonstack(B))
598+
lua_closeslot(L, -2); /* close the box */
599+
lua_remove(L, -2); /* remove box or placeholder from the stack */
591600
}
592601

593602

@@ -622,6 +631,7 @@ LUALIB_API void luaL_buffinit (lua_State *L, luaL_Buffer *B) {
622631
B->b = B->init.b;
623632
B->n = 0;
624633
B->size = LUAL_BUFFERSIZE;
634+
lua_pushlightuserdata(L, (void*)B); /* push placeholder */
625635
}
626636

627637

@@ -639,20 +649,31 @@ LUALIB_API char *luaL_buffinitsize (lua_State *L, luaL_Buffer *B, size_t sz) {
639649
** =======================================================
640650
*/
641651

642-
/* index of free-list header */
643-
#define freelist 0
644-
652+
/* index of free-list header (after the predefined values) */
653+
#define freelist (LUA_RIDX_LAST + 1)
645654

655+
/*
656+
** The previously freed references form a linked list:
657+
** t[freelist] is the index of a first free index, or zero if list is
658+
** empty; t[t[freelist]] is the index of the second element; etc.
659+
*/
646660
LUALIB_API int luaL_ref (lua_State *L, int t) {
647661
int ref;
648662
if (lua_isnil(L, -1)) {
649663
lua_pop(L, 1); /* remove from stack */
650664
return LUA_REFNIL; /* 'nil' has a unique fixed reference */
651665
}
652666
t = lua_absindex(L, t);
653-
lua_rawgeti(L, t, freelist); /* get first free element */
654-
ref = (int)lua_tointeger(L, -1); /* ref = t[freelist] */
655-
lua_pop(L, 1); /* remove it from stack */
667+
if (lua_rawgeti(L, t, freelist) == LUA_TNIL) { /* first access? */
668+
ref = 0; /* list is empty */
669+
lua_pushinteger(L, 0); /* initialize as an empty list */
670+
lua_rawseti(L, t, freelist); /* ref = t[freelist] = 0 */
671+
}
672+
else { /* already initialized */
673+
lua_assert(lua_isinteger(L, -1));
674+
ref = (int)lua_tointeger(L, -1); /* ref = t[freelist] */
675+
}
676+
lua_pop(L, 1); /* remove element from stack */
656677
if (ref != 0) { /* any free element? */
657678
lua_rawgeti(L, t, ref); /* remove it from list */
658679
lua_rawseti(L, t, freelist); /* (t[freelist] = t[ref]) */
@@ -668,6 +689,7 @@ LUALIB_API void luaL_unref (lua_State *L, int t, int ref) {
668689
if (ref >= 0) {
669690
t = lua_absindex(L, t);
670691
lua_rawgeti(L, t, freelist);
692+
lua_assert(lua_isinteger(L, -1));
671693
lua_rawseti(L, t, ref); /* t[ref] = t[freelist] */
672694
lua_pushinteger(L, ref);
673695
lua_rawseti(L, t, freelist); /* t[freelist] = ref */
@@ -851,7 +873,7 @@ LUALIB_API lua_Integer luaL_len (lua_State *L, int idx) {
851873
int isnum;
852874
lua_len(L, idx);
853875
l = lua_tointegerx(L, -1, &isnum);
854-
if (!isnum)
876+
if (l_unlikely(!isnum))
855877
luaL_error(L, "object length is not an integer");
856878
lua_pop(L, 1); /* remove object */
857879
return l;
@@ -1064,7 +1086,7 @@ static void warnfon (void *ud, const char *message, int tocont) {
10641086

10651087
LUALIB_API lua_State *luaL_newstate (void) {
10661088
lua_State *L = lua_newstate(l_alloc, NULL);
1067-
if (L) {
1089+
if (l_likely(L)) {
10681090
lua_atpanic(L, &panic);
10691091
lua_setwarnf(L, warnfoff, L); /* default is warnings off */
10701092
}

lua-5.4.2/lauxlib.h renamed to lua-5.4.3/lauxlib.h

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
#include <stddef.h>
1313
#include <stdio.h>
1414

15+
#include "luaconf.h"
1516
#include "lua.h"
1617

1718

@@ -130,10 +131,10 @@ LUALIB_API void (luaL_requiref) (lua_State *L, const char *modname,
130131
(luaL_checkversion(L), luaL_newlibtable(L,l), luaL_setfuncs(L,l,0))
131132

132133
#define luaL_argcheck(L, cond,arg,extramsg) \
133-
((void)((cond) || luaL_argerror(L, (arg), (extramsg))))
134+
((void)(luai_likely(cond) || luaL_argerror(L, (arg), (extramsg))))
134135

135136
#define luaL_argexpected(L,cond,arg,tname) \
136-
((void)((cond) || luaL_typeerror(L, (arg), (tname))))
137+
((void)(luai_likely(cond) || luaL_typeerror(L, (arg), (tname))))
137138

138139
#define luaL_checkstring(L,n) (luaL_checklstring(L, (n), NULL))
139140
#define luaL_optstring(L,n,d) (luaL_optlstring(L, (n), (d), NULL))
@@ -157,6 +158,22 @@ LUALIB_API void (luaL_requiref) (lua_State *L, const char *modname,
157158
#define luaL_pushfail(L) lua_pushnil(L)
158159

159160

161+
/*
162+
** Internal assertions for in-house debugging
163+
*/
164+
#if !defined(lua_assert)
165+
166+
#if defined LUAI_ASSERT
167+
#include <assert.h>
168+
#define lua_assert(c) assert(c)
169+
#else
170+
#define lua_assert(c) ((void)0)
171+
#endif
172+
173+
#endif
174+
175+
176+
160177
/*
161178
** {======================================================
162179
** Generic Buffer manipulation

0 commit comments

Comments
 (0)