Skip to content

Commit

Permalink
Merge branch 'pico8' into zepto8
Browse files Browse the repository at this point in the history
"split" function now works properly even with \0 in lua strings
  • Loading branch information
Nusan committed Nov 14, 2024
2 parents a64c3f3 + 8575dfb commit 4a7089e
Show file tree
Hide file tree
Showing 9 changed files with 23 additions and 13 deletions.
5 changes: 3 additions & 2 deletions lapi.c
Original file line number Diff line number Diff line change
Expand Up @@ -301,7 +301,8 @@ LUA_API void lua_arith (lua_State *L, int op) {
StkId o1; /* 1st operand */
StkId o2; /* 2nd operand */
lua_lock(L);
if (op != LUA_OPUNM && op != LUA_OPBNOT) /* all other operations expect two operands */
if (op != LUA_OPUNM && op != LUA_OPBNOT && op != LUA_OPPEEK &&
op != LUA_OPPEEK2 && op != LUA_OPPEEK4) /* all other operations expect two operands */
api_checknelems(L, 2);
else { /* for unary minus, add fake 2nd operand */
api_checknelems(L, 1);
Expand All @@ -311,7 +312,7 @@ LUA_API void lua_arith (lua_State *L, int op) {
o1 = L->top - 2;
o2 = L->top - 1;
if (ttisnumber(o1) && ttisnumber(o2)) {
setnvalue(o1, luaO_arith(op, nvalue(o1), nvalue(o2)));
setnvalue(o1, luaO_arith(L, op, nvalue(o1), nvalue(o2)));
}
else
luaV_arith(L, o1, o1, o2, cast(TMS, op - LUA_OPADD + TM_ADD));
Expand Down
4 changes: 3 additions & 1 deletion lcode.c
Original file line number Diff line number Diff line change
Expand Up @@ -715,7 +715,9 @@ static int constfolding (OpCode op, expdesc *e1, expdesc *e2) {
if (!isnumeral(e1) || !isnumeral(e2)) return 0;
if ((op == OP_DIV || op == OP_MOD) && e2->u.nval == 0)
return 0; /* do not attempt to divide by 0 */
r = luaO_arith(op - OP_ADD + LUA_OPADD, e1->u.nval, e2->u.nval);
if (op == OP_PEEK || op == OP_PEEK2 || op == OP_PEEK4)
return 0; /* const folding cannot work with peek */
r = luaO_arith(NULL, op - OP_ADD + LUA_OPADD, e1->u.nval, e2->u.nval);
e1->u.nval = r;
return 1;
}
Expand Down
5 changes: 4 additions & 1 deletion lobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ int luaO_ceillog2 (unsigned int x) {
}


lua_Number luaO_arith (int op, lua_Number v1, lua_Number v2) {
lua_Number luaO_arith (lua_State *L, int op, lua_Number v1, lua_Number v2) {
switch (op) {
case LUA_OPADD: return luai_numadd(NULL, v1, v2);
case LUA_OPSUB: return luai_numsub(NULL, v1, v2);
Expand All @@ -89,6 +89,9 @@ lua_Number luaO_arith (int op, lua_Number v1, lua_Number v2) {
case LUA_OPROTR: return luai_numrotr(NULL, v1, v2);
case LUA_OPUNM: return luai_numunm(NULL, v1);
case LUA_OPBNOT: return luai_numbnot(NULL, v1);
case LUA_OPPEEK: return luai_numpeek(L, v1);
case LUA_OPPEEK2: return luai_numpeek2(L, v1);
case LUA_OPPEEK4: return luai_numpeek4(L, v1);
default: lua_assert(0); return 0;
}
}
Expand Down
2 changes: 1 addition & 1 deletion lobject.h
Original file line number Diff line number Diff line change
Expand Up @@ -594,7 +594,7 @@ LUAI_DDEC const TValue luaO_nilobject_;
LUAI_FUNC int luaO_int2fb (unsigned int x);
LUAI_FUNC int luaO_fb2int (int x);
LUAI_FUNC int luaO_ceillog2 (unsigned int x);
LUAI_FUNC lua_Number luaO_arith (int op, lua_Number v1, lua_Number v2);
LUAI_FUNC lua_Number luaO_arith (lua_State *L, int op, lua_Number v1, lua_Number v2);
LUAI_FUNC int luaO_str2d (const char *s, size_t len, lua_Number *result);
LUAI_FUNC int luaO_hexavalue (int c);
LUAI_FUNC const char *luaO_pushvfstring (lua_State *L, const char *fmt,
Expand Down
4 changes: 2 additions & 2 deletions lpico8lib.c
Original file line number Diff line number Diff line change
Expand Up @@ -326,15 +326,15 @@ static int pico8_split(lua_State *l) {
for (char const *parser = haystack; parser < end; ) {
lua_Number num;
char const *next = size ? parser + size
: needle ? strchr(parser, needle) : parser + 1;
: needle ? (char const*)memchr(parser, needle, end - parser) : parser + 1;
if (!next || next > end)
next = haystack + hlen;
char saved = *next; // temporarily put a null terminator here
*(char *)next = '\0';
if (convert && luaO_str2d(parser, next - parser, &num))
lua_pushnumber(l, num);
else
lua_pushstring(l, parser);
lua_pushlstring(l, parser, next - parser);
*(char *)next = saved;
lua_rawseti(l, -2, int(++count));
parser = next + (!size && needle);
Expand Down
3 changes: 3 additions & 0 deletions lua.h
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,9 @@ LUA_API const void *(lua_topointer) (lua_State *L, int idx);
#define LUA_OPROTR 14
#define LUA_OPUNM 15
#define LUA_OPBNOT 16
#define LUA_OPPEEK 17
#define LUA_OPPEEK2 18
#define LUA_OPPEEK4 19

LUA_API void (lua_arith) (lua_State *L, int op);

Expand Down
6 changes: 3 additions & 3 deletions luaconf.h
Original file line number Diff line number Diff line change
Expand Up @@ -585,9 +585,9 @@
#define luai_numrotl(L,a,b) (l_mathop(rotl)((a),int((b))))
#define luai_numrotr(L,a,b) (l_mathop(rotr)((a),int((b))))
#define luai_numbnot(L,a) (~(a))
#define luai_numpeek(L,a) (lua_peek(L,a,1))
#define luai_numpeek2(L,a) (lua_peek(L,a,2))
#define luai_numpeek4(L,a) (lua_peek(L,a,4))
#define luai_numpeek(L,a) (luaV_peek(L,a,1))
#define luai_numpeek2(L,a) (luaV_peek(L,a,2))
#define luai_numpeek4(L,a) (luaV_peek(L,a,4))

#define lua_number2str(s,n) [&]() { \
int i = sprintf(s, "%1.4f", (double)n); \
Expand Down
6 changes: 3 additions & 3 deletions lvm.c
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,7 @@ static int call_orderTM (lua_State *L, const TValue *p1, const TValue *p2,

#define PEEK(ram, address) (ram && (address < 0x8000) ? ram[address] : 0)

static z8::fix32 lua_peek(struct lua_State *L, z8::fix32 a, int count)
lua_Number luaV_peek(struct lua_State *L, lua_Number a, int count)
{
unsigned char const *p = G(L)->pico8memory;
int address = int(a) & 0x7fff;
Expand All @@ -236,7 +236,7 @@ static z8::fix32 lua_peek(struct lua_State *L, z8::fix32 a, int count)
ret |= PEEK(p, address) << 16;
break;
}
return z8::fix32::frombits(ret);
return lua_Number::frombits(ret);
}


Expand Down Expand Up @@ -397,7 +397,7 @@ void luaV_arith (lua_State *L, StkId ra, const TValue *rb,
const TValue *b, *c;
if ((b = luaV_tonumber(rb, &tempb)) != NULL &&
(c = luaV_tonumber(rc, &tempc)) != NULL) {
lua_Number res = luaO_arith(op - TM_ADD + LUA_OPADD, nvalue(b), nvalue(c));
lua_Number res = luaO_arith(L, op - TM_ADD + LUA_OPADD, nvalue(b), nvalue(c));
setnvalue(ra, res);
}
else if (!call_binTM(L, rb, rc, ra, op))
Expand Down
1 change: 1 addition & 0 deletions lvm.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,5 @@ LUAI_FUNC void luaV_arith (lua_State *L, StkId ra, const TValue *rb,
const TValue *rc, TMS op);
LUAI_FUNC void luaV_objlen (lua_State *L, StkId ra, const TValue *rb);

LUAI_FUNC lua_Number luaV_peek(struct lua_State *L, lua_Number a, int count);
#endif

0 comments on commit 4a7089e

Please sign in to comment.