Skip to content

Commit ef03d41

Browse files
committed
Update Lua 5.4 to 5.4.4 and bump version to 544.0.0
1 parent fa127e9 commit ef03d41

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

+505
-256
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 = "543.1.0"
3+
version = "544.0.0"
44
authors = ["Aleksandr Orlenko <[email protected]>"]
55
edition = "2018"
66
repository = "https://github.com/khvzak/lua-src-rs"

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

Lines changed: 28 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,10 @@ const char lua_ident[] =
5353
#define isupvalue(i) ((i) < LUA_REGISTRYINDEX)
5454

5555

56+
/*
57+
** Convert an acceptable index to a pointer to its respective value.
58+
** Non-valid indices return the special nil value 'G(L)->nilvalue'.
59+
*/
5660
static TValue *index2value (lua_State *L, int idx) {
5761
CallInfo *ci = L->ci;
5862
if (idx > 0) {
@@ -70,22 +74,28 @@ static TValue *index2value (lua_State *L, int idx) {
7074
else { /* upvalues */
7175
idx = LUA_REGISTRYINDEX - idx;
7276
api_check(L, idx <= MAXUPVAL + 1, "upvalue index too large");
73-
if (ttislcf(s2v(ci->func))) /* light C function? */
74-
return &G(L)->nilvalue; /* it has no upvalues */
75-
else {
77+
if (ttisCclosure(s2v(ci->func))) { /* C closure? */
7678
CClosure *func = clCvalue(s2v(ci->func));
7779
return (idx <= func->nupvalues) ? &func->upvalue[idx-1]
7880
: &G(L)->nilvalue;
7981
}
82+
else { /* light C function or Lua function (through a hook)?) */
83+
api_check(L, ttislcf(s2v(ci->func)), "caller not a C function");
84+
return &G(L)->nilvalue; /* no upvalues */
85+
}
8086
}
8187
}
8288

8389

84-
static StkId index2stack (lua_State *L, int idx) {
90+
91+
/*
92+
** Convert a valid actual index (not a pseudo-index) to its address.
93+
*/
94+
l_sinline StkId index2stack (lua_State *L, int idx) {
8595
CallInfo *ci = L->ci;
8696
if (idx > 0) {
8797
StkId o = ci->func + idx;
88-
api_check(L, o < L->top, "unacceptable index");
98+
api_check(L, o < L->top, "invalid index");
8999
return o;
90100
}
91101
else { /* non-positive index */
@@ -218,7 +228,7 @@ LUA_API void lua_closeslot (lua_State *L, int idx) {
218228
** Note that we move(copy) only the value inside the stack.
219229
** (We do not move additional fields that may exist.)
220230
*/
221-
static void reverse (lua_State *L, StkId from, StkId to) {
231+
l_sinline void reverse (lua_State *L, StkId from, StkId to) {
222232
for (; from < to; from++, to--) {
223233
TValue temp;
224234
setobj(L, &temp, s2v(from));
@@ -438,7 +448,7 @@ LUA_API lua_CFunction lua_tocfunction (lua_State *L, int idx) {
438448
}
439449

440450

441-
static void *touserdata (const TValue *o) {
451+
l_sinline void *touserdata (const TValue *o) {
442452
switch (ttype(o)) {
443453
case LUA_TUSERDATA: return getudatamem(uvalue(o));
444454
case LUA_TLIGHTUSERDATA: return pvalue(o);
@@ -630,7 +640,7 @@ LUA_API int lua_pushthread (lua_State *L) {
630640
*/
631641

632642

633-
static int auxgetstr (lua_State *L, const TValue *t, const char *k) {
643+
l_sinline int auxgetstr (lua_State *L, const TValue *t, const char *k) {
634644
const TValue *slot;
635645
TString *str = luaS_new(L, k);
636646
if (luaV_fastget(L, t, str, slot, luaH_getstr)) {
@@ -705,7 +715,7 @@ LUA_API int lua_geti (lua_State *L, int idx, lua_Integer n) {
705715
}
706716

707717

708-
static int finishrawget (lua_State *L, const TValue *val) {
718+
l_sinline int finishrawget (lua_State *L, const TValue *val) {
709719
if (isempty(val)) /* avoid copying empty items to the stack */
710720
setnilvalue(s2v(L->top));
711721
else
@@ -1126,18 +1136,19 @@ LUA_API int lua_status (lua_State *L) {
11261136
LUA_API int lua_gc (lua_State *L, int what, ...) {
11271137
va_list argp;
11281138
int res = 0;
1129-
global_State *g;
1139+
global_State *g = G(L);
1140+
if (g->gcstp & GCSTPGC) /* internal stop? */
1141+
return -1; /* all options are invalid when stopped */
11301142
lua_lock(L);
1131-
g = G(L);
11321143
va_start(argp, what);
11331144
switch (what) {
11341145
case LUA_GCSTOP: {
1135-
g->gcrunning = 0;
1146+
g->gcstp = GCSTPUSR; /* stopped by the user */
11361147
break;
11371148
}
11381149
case LUA_GCRESTART: {
11391150
luaE_setdebt(g, 0);
1140-
g->gcrunning = 1;
1151+
g->gcstp = 0; /* (GCSTPGC must be already zero here) */
11411152
break;
11421153
}
11431154
case LUA_GCCOLLECT: {
@@ -1156,8 +1167,8 @@ LUA_API int lua_gc (lua_State *L, int what, ...) {
11561167
case LUA_GCSTEP: {
11571168
int data = va_arg(argp, int);
11581169
l_mem debt = 1; /* =1 to signal that it did an actual step */
1159-
lu_byte oldrunning = g->gcrunning;
1160-
g->gcrunning = 1; /* allow GC to run */
1170+
lu_byte oldstp = g->gcstp;
1171+
g->gcstp = 0; /* allow GC to run (GCSTPGC must be zero here) */
11611172
if (data == 0) {
11621173
luaE_setdebt(g, 0); /* do a basic step */
11631174
luaC_step(L);
@@ -1167,7 +1178,7 @@ LUA_API int lua_gc (lua_State *L, int what, ...) {
11671178
luaE_setdebt(g, debt);
11681179
luaC_checkGC(L);
11691180
}
1170-
g->gcrunning = oldrunning; /* restore previous state */
1181+
g->gcstp = oldstp; /* restore previous state */
11711182
if (debt > 0 && g->gcstate == GCSpause) /* end of cycle? */
11721183
res = 1; /* signal it */
11731184
break;
@@ -1185,7 +1196,7 @@ LUA_API int lua_gc (lua_State *L, int what, ...) {
11851196
break;
11861197
}
11871198
case LUA_GCISRUNNING: {
1188-
res = g->gcrunning;
1199+
res = gcrunning(g);
11891200
break;
11901201
}
11911202
case LUA_GCGEN: {
File renamed without changes.

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -881,6 +881,7 @@ LUALIB_API lua_Integer luaL_len (lua_State *L, int idx) {
881881

882882

883883
LUALIB_API const char *luaL_tolstring (lua_State *L, int idx, size_t *len) {
884+
idx = lua_absindex(L,idx);
884885
if (luaL_callmeta(L, idx, "__tostring")) { /* metafield? */
885886
if (!lua_isstring(L, -1))
886887
luaL_error(L, "'__tostring' must return a string");

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

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ LUALIB_API lua_State *(luaL_newstate) (void);
102102

103103
LUALIB_API lua_Integer (luaL_len) (lua_State *L, int idx);
104104

105-
LUALIB_API void luaL_addgsub (luaL_Buffer *b, const char *s,
105+
LUALIB_API void (luaL_addgsub) (luaL_Buffer *b, const char *s,
106106
const char *p, const char *r);
107107
LUALIB_API const char *(luaL_gsub) (lua_State *L, const char *s,
108108
const char *p, const char *r);
@@ -154,6 +154,14 @@ LUALIB_API void (luaL_requiref) (lua_State *L, const char *modname,
154154
#define luaL_loadbuffer(L,s,sz,n) luaL_loadbufferx(L,s,sz,n,NULL)
155155

156156

157+
/*
158+
** Perform arithmetic operations on lua_Integer values with wrap-around
159+
** semantics, as the Lua core does.
160+
*/
161+
#define luaL_intop(op,v1,v2) \
162+
((lua_Integer)((lua_Unsigned)(v1) op (lua_Unsigned)(v2)))
163+
164+
157165
/* push the value used to represent failure/error */
158166
#define luaL_pushfail(L) lua_pushnil(L)
159167

lua-5.4.3/lbaselib.c renamed to lua-5.4.4/lbaselib.c

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -182,12 +182,20 @@ static int luaB_rawset (lua_State *L) {
182182

183183

184184
static int pushmode (lua_State *L, int oldmode) {
185-
lua_pushstring(L, (oldmode == LUA_GCINC) ? "incremental"
186-
: "generational");
185+
if (oldmode == -1)
186+
luaL_pushfail(L); /* invalid call to 'lua_gc' */
187+
else
188+
lua_pushstring(L, (oldmode == LUA_GCINC) ? "incremental"
189+
: "generational");
187190
return 1;
188191
}
189192

190193

194+
/*
195+
** check whether call to 'lua_gc' was valid (not inside a finalizer)
196+
*/
197+
#define checkvalres(res) { if (res == -1) break; }
198+
191199
static int luaB_collectgarbage (lua_State *L) {
192200
static const char *const opts[] = {"stop", "restart", "collect",
193201
"count", "step", "setpause", "setstepmul",
@@ -200,24 +208,28 @@ static int luaB_collectgarbage (lua_State *L) {
200208
case LUA_GCCOUNT: {
201209
int k = lua_gc(L, o);
202210
int b = lua_gc(L, LUA_GCCOUNTB);
211+
checkvalres(k);
203212
lua_pushnumber(L, (lua_Number)k + ((lua_Number)b/1024));
204213
return 1;
205214
}
206215
case LUA_GCSTEP: {
207216
int step = (int)luaL_optinteger(L, 2, 0);
208217
int res = lua_gc(L, o, step);
218+
checkvalres(res);
209219
lua_pushboolean(L, res);
210220
return 1;
211221
}
212222
case LUA_GCSETPAUSE:
213223
case LUA_GCSETSTEPMUL: {
214224
int p = (int)luaL_optinteger(L, 2, 0);
215225
int previous = lua_gc(L, o, p);
226+
checkvalres(previous);
216227
lua_pushinteger(L, previous);
217228
return 1;
218229
}
219230
case LUA_GCISRUNNING: {
220231
int res = lua_gc(L, o);
232+
checkvalres(res);
221233
lua_pushboolean(L, res);
222234
return 1;
223235
}
@@ -234,10 +246,13 @@ static int luaB_collectgarbage (lua_State *L) {
234246
}
235247
default: {
236248
int res = lua_gc(L, o);
249+
checkvalres(res);
237250
lua_pushinteger(L, res);
238251
return 1;
239252
}
240253
}
254+
luaL_pushfail(L); /* invalid call (inside a finalizer) */
255+
return 1;
241256
}
242257

243258

@@ -261,6 +276,11 @@ static int luaB_next (lua_State *L) {
261276
}
262277

263278

279+
static int pairscont (lua_State *L, int status, lua_KContext k) {
280+
(void)L; (void)status; (void)k; /* unused */
281+
return 3;
282+
}
283+
264284
static int luaB_pairs (lua_State *L) {
265285
luaL_checkany(L, 1);
266286
if (luaL_getmetafield(L, 1, "__pairs") == LUA_TNIL) { /* no metamethod? */
@@ -270,7 +290,7 @@ static int luaB_pairs (lua_State *L) {
270290
}
271291
else {
272292
lua_pushvalue(L, 1); /* argument 'self' to metamethod */
273-
lua_call(L, 1, 3); /* get 3 values from metamethod */
293+
lua_callk(L, 1, 3, 0, pairscont); /* get 3 values from metamethod */
274294
}
275295
return 3;
276296
}
@@ -280,7 +300,8 @@ static int luaB_pairs (lua_State *L) {
280300
** Traversal function for 'ipairs'
281301
*/
282302
static int ipairsaux (lua_State *L) {
283-
lua_Integer i = luaL_checkinteger(L, 2) + 1;
303+
lua_Integer i = luaL_checkinteger(L, 2);
304+
i = luaL_intop(+, i, 1);
284305
lua_pushinteger(L, i);
285306
return (lua_geti(L, 1, i) == LUA_TNIL) ? 1 : 2;
286307
}

lua-5.4.3/lcode.c renamed to lua-5.4.4/lcode.c

Lines changed: 26 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#include "lprefix.h"
1111

1212

13+
#include <float.h>
1314
#include <limits.h>
1415
#include <math.h>
1516
#include <stdlib.h>
@@ -580,24 +581,41 @@ static int stringK (FuncState *fs, TString *s) {
580581

581582
/*
582583
** Add an integer to list of constants and return its index.
583-
** Integers use userdata as keys to avoid collision with floats with
584-
** same value; conversion to 'void*' is used only for hashing, so there
585-
** are no "precision" problems.
586584
*/
587585
static int luaK_intK (FuncState *fs, lua_Integer n) {
588-
TValue k, o;
589-
setpvalue(&k, cast_voidp(cast_sizet(n)));
586+
TValue o;
590587
setivalue(&o, n);
591-
return addk(fs, &k, &o);
588+
return addk(fs, &o, &o); /* use integer itself as key */
592589
}
593590

594591
/*
595-
** Add a float to list of constants and return its index.
592+
** Add a float to list of constants and return its index. Floats
593+
** with integral values need a different key, to avoid collision
594+
** with actual integers. To that, we add to the number its smaller
595+
** power-of-two fraction that is still significant in its scale.
596+
** For doubles, that would be 1/2^52.
597+
** (This method is not bulletproof: there may be another float
598+
** with that value, and for floats larger than 2^53 the result is
599+
** still an integer. At worst, this only wastes an entry with
600+
** a duplicate.)
596601
*/
597602
static int luaK_numberK (FuncState *fs, lua_Number r) {
598603
TValue o;
604+
lua_Integer ik;
599605
setfltvalue(&o, r);
600-
return addk(fs, &o, &o); /* use number itself as key */
606+
if (!luaV_flttointeger(r, &ik, F2Ieq)) /* not an integral value? */
607+
return addk(fs, &o, &o); /* use number itself as key */
608+
else { /* must build an alternative key */
609+
const int nbm = l_floatatt(MANT_DIG);
610+
const lua_Number q = l_mathop(ldexp)(l_mathop(1.0), -nbm + 1);
611+
const lua_Number k = (ik == 0) ? q : r + r*q; /* new key */
612+
TValue kv;
613+
setfltvalue(&kv, k);
614+
/* result is not an integral value, unless value is too large */
615+
lua_assert(!luaV_flttointeger(k, &ik, F2Ieq) ||
616+
l_mathop(fabs)(r) >= l_mathop(1e6));
617+
return addk(fs, &kv, &o);
618+
}
601619
}
602620

603621

File renamed without changes.

lua-5.4.3/lcorolib.c renamed to lua-5.4.4/lcorolib.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ static int luaB_auxwrap (lua_State *L) {
7878
if (stat != LUA_OK && stat != LUA_YIELD) { /* error in the coroutine? */
7979
stat = lua_resetthread(co); /* close its tbc variables */
8080
lua_assert(stat != LUA_OK);
81-
lua_xmove(co, L, 1); /* copy error message */
81+
lua_xmove(co, L, 1); /* move error message to the caller */
8282
}
8383
if (stat != LUA_ERRMEM && /* not a memory error and ... */
8484
lua_type(L, -1) == LUA_TSTRING) { /* ... error object is a string? */
@@ -179,7 +179,7 @@ static int luaB_close (lua_State *L) {
179179
}
180180
else {
181181
lua_pushboolean(L, 0);
182-
lua_xmove(co, L, 1); /* copy error message */
182+
lua_xmove(co, L, 1); /* move error message */
183183
return 2;
184184
}
185185
}
File renamed without changes.

0 commit comments

Comments
 (0)