Skip to content

Commit

Permalink
upgrade Lua to v5.4.7
Browse files Browse the repository at this point in the history
`lua.c` and `luac.c` omitted, sjasmplus binds to lua programatically.

As far as I can tell everything works without me touching anything else,
just extracted the tar file over v5.4.6, built it, ran the tests. Done.
  • Loading branch information
ped7g committed Nov 28, 2024
1 parent a2bfa64 commit 6e16cf4
Show file tree
Hide file tree
Showing 30 changed files with 355 additions and 252 deletions.
3 changes: 3 additions & 0 deletions TODO.txt
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@ extern struct tm *gmtime (const time_t *__timer) __THROW;
extern struct tm *localtime (const time_t *__timer) __THROW;
-------------

unittest-cpp seems to be pretty dead now, consider migration, for example to https://github.com/neacsum/utpp
(author claims C++20, header only and somewhat more compatible with GoogleTest)

https://www.codefactor.io/repository/github/z00m128/sjasmplus

coverage keeps fluctuating because of io_nex.cpp `SNexFile::~SNexFile()`, probably depends on compiler's exact version and mood???
Expand Down
5 changes: 3 additions & 2 deletions docs/documentation.xml
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,7 @@
- added Amstrad CPC+ <link linkend="po_savecpr">`SAVECPR`</link> to save CPR cartridge - by Laurent Carlier
- ZX Next CSpect emulator v2.19.9.1: `break` opcode changed to `FD 00`
- fix: windows cmd.exe console state after running sjasmplus.exe
- Lua: minor version upgrade to 5.4.7 (from 5.4.6)
</synopsis>
</listitem>
</varlistentry>
Expand Down Expand Up @@ -4224,7 +4225,7 @@ head1 BLOCK_HEADER { ; Multi-line initialization requires curly braces.
as <ulink url="https://www.lua.org/">Lua</ulink> embedded to the assembler? Answer is simple: it can be used to add extra
features by users. And Lua is small enough, fast and powerful scripting engine.</para>

<para>Sjasmplus currently includes Lua 5.4.6 (since v1.20.3). The upgrade from Lua 5.1 in v1.20.0
<para>Sjasmplus currently includes Lua 5.4.7 (since v1.21.0). The upgrade from Lua 5.1 in v1.20.0
was done along replacing tolua++ with LuaBridge2.6 and while there was lot of effort
to maintain original Lua bindings to sjasmplus, there were few changes to the API.</para>

Expand Down Expand Up @@ -4518,7 +4519,7 @@ head1 BLOCK_HEADER { ; Multi-line initialization requires curly braces.

<para><emphasis>None at this moment.</emphasis></para>

<para>Since v1.20.0 the sjasmplus integrates Lua 5.4.4 (now 5.4.6).</para>
<para>Since v1.20.0 the sjasmplus integrates Lua 5.4.4 (now 5.4.7).</para>

<para>The libraries included in older versions of sjasmplus: lpack.c, bit (bitwise operators)
and hex (convert to/from hexa numbers) are now all part of the standard Lua 5.4.</para>
Expand Down
4 changes: 2 additions & 2 deletions lua5.4/lapi.c
Original file line number Diff line number Diff line change
Expand Up @@ -417,9 +417,9 @@ LUA_API const char *lua_tolstring (lua_State *L, int idx, size_t *len) {
o = index2value(L, idx); /* previous call may reallocate the stack */
}
if (len != NULL)
*len = vslen(o);
*len = tsslen(tsvalue(o));
lua_unlock(L);
return svalue(o);
return getstr(tsvalue(o));
}


Expand Down
28 changes: 21 additions & 7 deletions lua5.4/lauxlib.c
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ static int pushglobalfuncname (lua_State *L, lua_Debug *ar) {
int top = lua_gettop(L);
lua_getinfo(L, "f", ar); /* push function */
lua_getfield(L, LUA_REGISTRYINDEX, LUA_LOADED_TABLE);
luaL_checkstack(L, 6, "not enough stack"); /* slots for 'findfield' */
if (findfield(L, top + 1, 2)) {
const char *name = lua_tostring(L, -1);
if (strncmp(name, LUA_GNAME ".", 3) == 0) { /* name start with '_G.'? */
Expand Down Expand Up @@ -249,11 +250,13 @@ LUALIB_API int luaL_fileresult (lua_State *L, int stat, const char *fname) {
return 1;
}
else {
const char *msg;
luaL_pushfail(L);
msg = (en != 0) ? strerror(en) : "(no extra info)";
if (fname)
lua_pushfstring(L, "%s: %s", fname, strerror(en));
lua_pushfstring(L, "%s: %s", fname, msg);
else
lua_pushstring(L, strerror(en));
lua_pushstring(L, msg);
lua_pushinteger(L, en);
return 3;
}
Expand Down Expand Up @@ -732,9 +735,12 @@ static const char *getF (lua_State *L, void *ud, size_t *size) {


static int errfile (lua_State *L, const char *what, int fnameindex) {
const char *serr = strerror(errno);
int err = errno;
const char *filename = lua_tostring(L, fnameindex) + 1;
lua_pushfstring(L, "cannot %s %s: %s", what, filename, serr);
if (err != 0)
lua_pushfstring(L, "cannot %s %s: %s", what, filename, strerror(err));
else
lua_pushfstring(L, "cannot %s %s", what, filename);
lua_remove(L, fnameindex);
return LUA_ERRFILE;
}
Expand Down Expand Up @@ -787,6 +793,7 @@ LUALIB_API int luaL_loadfilex (lua_State *L, const char *filename,
}
else {
lua_pushfstring(L, "@%s", filename);
errno = 0;
lf.f = fopen(filename, "r");
if (lf.f == NULL) return errfile(L, "open", fnameindex);
}
Expand All @@ -796,13 +803,15 @@ LUALIB_API int luaL_loadfilex (lua_State *L, const char *filename,
if (c == LUA_SIGNATURE[0]) { /* binary file? */
lf.n = 0; /* remove possible newline */
if (filename) { /* "real" file? */
errno = 0;
lf.f = freopen(filename, "rb", lf.f); /* reopen in binary mode */
if (lf.f == NULL) return errfile(L, "reopen", fnameindex);
skipcomment(lf.f, &c); /* re-read initial portion */
}
}
if (c != EOF)
lf.buff[lf.n++] = c; /* 'c' is the first character of the stream */
errno = 0;
status = lua_load(L, getF, &lf, lua_tostring(L, -1), mode);
readstatus = ferror(lf.f);
if (filename) fclose(lf.f); /* close file (even in case of errors) */
Expand Down Expand Up @@ -933,7 +942,7 @@ LUALIB_API const char *luaL_tolstring (lua_State *L, int idx, size_t *len) {
LUALIB_API void luaL_setfuncs (lua_State *L, const luaL_Reg *l, int nup) {
luaL_checkstack(L, nup, "too many upvalues");
for (; l->name != NULL; l++) { /* fill the table with given functions */
if (l->func == NULL) /* place holder? */
if (l->func == NULL) /* placeholder? */
lua_pushboolean(L, 0);
else {
int i;
Expand Down Expand Up @@ -1025,9 +1034,14 @@ static void *l_alloc (void *ud, void *ptr, size_t osize, size_t nsize) {
}


/*
** Standard panic funcion just prints an error message. The test
** with 'lua_type' avoids possible memory errors in 'lua_tostring'.
*/
static int panic (lua_State *L) {
const char *msg = lua_tostring(L, -1);
if (msg == NULL) msg = "error object is not a string";
const char *msg = (lua_type(L, -1) == LUA_TSTRING)
? lua_tostring(L, -1)
: "error object is not a string";
lua_writestringerror("PANIC: unprotected error in call to Lua API (%s)\n",
msg);
return 0; /* return to Lua to abort */
Expand Down
35 changes: 19 additions & 16 deletions lua5.4/lcode.c
Original file line number Diff line number Diff line change
Expand Up @@ -415,7 +415,7 @@ int luaK_codeABx (FuncState *fs, OpCode o, int a, unsigned int bc) {
/*
** Format and emit an 'iAsBx' instruction.
*/
int luaK_codeAsBx (FuncState *fs, OpCode o, int a, int bc) {
static int codeAsBx (FuncState *fs, OpCode o, int a, int bc) {
unsigned int b = bc + OFFSET_sBx;
lua_assert(getOpMode(o) == iAsBx);
lua_assert(a <= MAXARG_A && b <= MAXARG_Bx);
Expand Down Expand Up @@ -671,7 +671,7 @@ static int fitsBx (lua_Integer i) {

void luaK_int (FuncState *fs, int reg, lua_Integer i) {
if (fitsBx(i))
luaK_codeAsBx(fs, OP_LOADI, reg, cast_int(i));
codeAsBx(fs, OP_LOADI, reg, cast_int(i));
else
luaK_codek(fs, reg, luaK_intK(fs, i));
}
Expand All @@ -680,7 +680,7 @@ void luaK_int (FuncState *fs, int reg, lua_Integer i) {
static void luaK_float (FuncState *fs, int reg, lua_Number f) {
lua_Integer fi;
if (luaV_flttointeger(f, &fi, F2Ieq) && fitsBx(fi))
luaK_codeAsBx(fs, OP_LOADF, reg, cast_int(fi));
codeAsBx(fs, OP_LOADF, reg, cast_int(fi));
else
luaK_codek(fs, reg, luaK_numberK(fs, f));
}
Expand Down Expand Up @@ -776,7 +776,8 @@ void luaK_dischargevars (FuncState *fs, expdesc *e) {
break;
}
case VLOCAL: { /* already in a register */
e->u.info = e->u.var.ridx;
int temp = e->u.var.ridx;
e->u.info = temp; /* (can't do a direct assignment; values overlap) */
e->k = VNONRELOC; /* becomes a non-relocatable value */
break;
}
Expand Down Expand Up @@ -1025,7 +1026,7 @@ static int luaK_exp2K (FuncState *fs, expdesc *e) {
** in the range of R/K indices).
** Returns 1 iff expression is K.
*/
int luaK_exp2RK (FuncState *fs, expdesc *e) {
static int exp2RK (FuncState *fs, expdesc *e) {
if (luaK_exp2K(fs, e))
return 1;
else { /* not a constant in the right range: put it in a register */
Expand All @@ -1037,7 +1038,7 @@ int luaK_exp2RK (FuncState *fs, expdesc *e) {

static void codeABRK (FuncState *fs, OpCode o, int a, int b,
expdesc *ec) {
int k = luaK_exp2RK(fs, ec);
int k = exp2RK(fs, ec);
luaK_codeABCk(fs, o, a, b, ec->u.info, k);
}

Expand Down Expand Up @@ -1215,7 +1216,7 @@ static void codenot (FuncState *fs, expdesc *e) {


/*
** Check whether expression 'e' is a small literal string
** Check whether expression 'e' is a short literal string
*/
static int isKstr (FuncState *fs, expdesc *e) {
return (e->k == VK && !hasjumps(e) && e->u.info <= MAXARG_B &&
Expand All @@ -1225,7 +1226,7 @@ static int isKstr (FuncState *fs, expdesc *e) {
/*
** Check whether expression 'e' is a literal integer.
*/
int luaK_isKint (expdesc *e) {
static int isKint (expdesc *e) {
return (e->k == VKINT && !hasjumps(e));
}

Expand All @@ -1235,7 +1236,7 @@ int luaK_isKint (expdesc *e) {
** proper range to fit in register C
*/
static int isCint (expdesc *e) {
return luaK_isKint(e) && (l_castS2U(e->u.ival) <= l_castS2U(MAXARG_C));
return isKint(e) && (l_castS2U(e->u.ival) <= l_castS2U(MAXARG_C));
}


Expand All @@ -1244,7 +1245,7 @@ static int isCint (expdesc *e) {
** proper range to fit in register sC
*/
static int isSCint (expdesc *e) {
return luaK_isKint(e) && fitsC(e->u.ival);
return isKint(e) && fitsC(e->u.ival);
}


Expand Down Expand Up @@ -1283,15 +1284,17 @@ void luaK_indexed (FuncState *fs, expdesc *t, expdesc *k) {
if (t->k == VUPVAL && !isKstr(fs, k)) /* upvalue indexed by non 'Kstr'? */
luaK_exp2anyreg(fs, t); /* put it in a register */
if (t->k == VUPVAL) {
t->u.ind.t = t->u.info; /* upvalue index */
t->u.ind.idx = k->u.info; /* literal string */
int temp = t->u.info; /* upvalue index */
lua_assert(isKstr(fs, k));
t->u.ind.t = temp; /* (can't do a direct assignment; values overlap) */
t->u.ind.idx = k->u.info; /* literal short string */
t->k = VINDEXUP;
}
else {
/* register index of the table */
t->u.ind.t = (t->k == VLOCAL) ? t->u.var.ridx: t->u.info;
if (isKstr(fs, k)) {
t->u.ind.idx = k->u.info; /* literal string */
t->u.ind.idx = k->u.info; /* literal short string */
t->k = VINDEXSTR;
}
else if (isCint(k)) {
Expand Down Expand Up @@ -1459,7 +1462,7 @@ static void codebinK (FuncState *fs, BinOpr opr,
*/
static int finishbinexpneg (FuncState *fs, expdesc *e1, expdesc *e2,
OpCode op, int line, TMS event) {
if (!luaK_isKint(e2))
if (!isKint(e2))
return 0; /* not an integer constant */
else {
lua_Integer i2 = e2->u.ival;
Expand Down Expand Up @@ -1592,7 +1595,7 @@ static void codeeq (FuncState *fs, BinOpr opr, expdesc *e1, expdesc *e2) {
op = OP_EQI;
r2 = im; /* immediate operand */
}
else if (luaK_exp2RK(fs, e2)) { /* 2nd expression is constant? */
else if (exp2RK(fs, e2)) { /* 2nd expression is constant? */
op = OP_EQK;
r2 = e2->u.info; /* constant index */
}
Expand Down Expand Up @@ -1658,7 +1661,7 @@ void luaK_infix (FuncState *fs, BinOpr op, expdesc *v) {
}
case OPR_EQ: case OPR_NE: {
if (!tonumeral(v, NULL))
luaK_exp2RK(fs, v);
exp2RK(fs, v);
/* else keep numeral, which may be an immediate operand */
break;
}
Expand Down
3 changes: 0 additions & 3 deletions lua5.4/lcode.h
Original file line number Diff line number Diff line change
Expand Up @@ -61,10 +61,8 @@ typedef enum UnOpr { OPR_MINUS, OPR_BNOT, OPR_NOT, OPR_LEN, OPR_NOUNOPR } UnOpr;

LUAI_FUNC int luaK_code (FuncState *fs, Instruction i);
LUAI_FUNC int luaK_codeABx (FuncState *fs, OpCode o, int A, unsigned int Bx);
LUAI_FUNC int luaK_codeAsBx (FuncState *fs, OpCode o, int A, int Bx);
LUAI_FUNC int luaK_codeABCk (FuncState *fs, OpCode o, int A,
int B, int C, int k);
LUAI_FUNC int luaK_isKint (expdesc *e);
LUAI_FUNC int luaK_exp2const (FuncState *fs, const expdesc *e, TValue *v);
LUAI_FUNC void luaK_fixline (FuncState *fs, int line);
LUAI_FUNC void luaK_nil (FuncState *fs, int from, int n);
Expand All @@ -76,7 +74,6 @@ LUAI_FUNC int luaK_exp2anyreg (FuncState *fs, expdesc *e);
LUAI_FUNC void luaK_exp2anyregup (FuncState *fs, expdesc *e);
LUAI_FUNC void luaK_exp2nextreg (FuncState *fs, expdesc *e);
LUAI_FUNC void luaK_exp2val (FuncState *fs, expdesc *e);
LUAI_FUNC int luaK_exp2RK (FuncState *fs, expdesc *e);
LUAI_FUNC void luaK_self (FuncState *fs, expdesc *e, expdesc *key);
LUAI_FUNC void luaK_indexed (FuncState *fs, expdesc *t, expdesc *k);
LUAI_FUNC void luaK_goiftrue (FuncState *fs, expdesc *e);
Expand Down
Loading

0 comments on commit 6e16cf4

Please sign in to comment.