Skip to content

Commit 4360cbf

Browse files
committed
Support @"1" syntax in addition to @1
1 parent 4bfc33b commit 4360cbf

File tree

8 files changed

+21
-11
lines changed

8 files changed

+21
-11
lines changed

lapi.c

+3-2
Original file line numberDiff line numberDiff line change
@@ -301,7 +301,8 @@ LUA_API void lua_arith (lua_State *L, int op) {
301301
StkId o1; /* 1st operand */
302302
StkId o2; /* 2nd operand */
303303
lua_lock(L);
304-
if (op != LUA_OPUNM && op != LUA_OPBNOT) /* all other operations expect two operands */
304+
if (op != LUA_OPUNM && op != LUA_OPBNOT && op != LUA_OPPEEK &&
305+
op != LUA_OPPEEK2 && op != LUA_OPPEEK4) /* all other operations expect two operands */
305306
api_checknelems(L, 2);
306307
else { /* for unary minus, add fake 2nd operand */
307308
api_checknelems(L, 1);
@@ -311,7 +312,7 @@ LUA_API void lua_arith (lua_State *L, int op) {
311312
o1 = L->top - 2;
312313
o2 = L->top - 1;
313314
if (ttisnumber(o1) && ttisnumber(o2)) {
314-
setnvalue(o1, luaO_arith(op, nvalue(o1), nvalue(o2)));
315+
setnvalue(o1, luaO_arith(L, op, nvalue(o1), nvalue(o2)));
315316
}
316317
else
317318
luaV_arith(L, o1, o1, o2, cast(TMS, op - LUA_OPADD + TM_ADD));

lcode.c

+3-1
Original file line numberDiff line numberDiff line change
@@ -715,7 +715,9 @@ static int constfolding (OpCode op, expdesc *e1, expdesc *e2) {
715715
if (!isnumeral(e1) || !isnumeral(e2)) return 0;
716716
if ((op == OP_DIV || op == OP_MOD) && e2->u.nval == 0)
717717
return 0; /* do not attempt to divide by 0 */
718-
r = luaO_arith(op - OP_ADD + LUA_OPADD, e1->u.nval, e2->u.nval);
718+
if (op == OP_PEEK || op == OP_PEEK2 || op == OP_PEEK4)
719+
return 0; /* const folding cannot work with peek */
720+
r = luaO_arith(NULL, op - OP_ADD + LUA_OPADD, e1->u.nval, e2->u.nval);
719721
e1->u.nval = r;
720722
return 1;
721723
}

lobject.c

+4-1
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ int luaO_ceillog2 (unsigned int x) {
7070
}
7171

7272

73-
lua_Number luaO_arith (int op, lua_Number v1, lua_Number v2) {
73+
lua_Number luaO_arith (lua_State *L, int op, lua_Number v1, lua_Number v2) {
7474
switch (op) {
7575
case LUA_OPADD: return luai_numadd(NULL, v1, v2);
7676
case LUA_OPSUB: return luai_numsub(NULL, v1, v2);
@@ -89,6 +89,9 @@ lua_Number luaO_arith (int op, lua_Number v1, lua_Number v2) {
8989
case LUA_OPROTR: return luai_numrotr(NULL, v1, v2);
9090
case LUA_OPUNM: return luai_numunm(NULL, v1);
9191
case LUA_OPBNOT: return luai_numbnot(NULL, v1);
92+
case LUA_OPPEEK: return luai_numpeek(L, v1);
93+
case LUA_OPPEEK2: return luai_numpeek2(L, v1);
94+
case LUA_OPPEEK4: return luai_numpeek4(L, v1);
9295
default: lua_assert(0); return 0;
9396
}
9497
}

lobject.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -594,7 +594,7 @@ LUAI_DDEC const TValue luaO_nilobject_;
594594
LUAI_FUNC int luaO_int2fb (unsigned int x);
595595
LUAI_FUNC int luaO_fb2int (int x);
596596
LUAI_FUNC int luaO_ceillog2 (unsigned int x);
597-
LUAI_FUNC lua_Number luaO_arith (int op, lua_Number v1, lua_Number v2);
597+
LUAI_FUNC lua_Number luaO_arith (lua_State *L, int op, lua_Number v1, lua_Number v2);
598598
LUAI_FUNC int luaO_str2d (const char *s, size_t len, lua_Number *result);
599599
LUAI_FUNC int luaO_hexavalue (int c);
600600
LUAI_FUNC const char *luaO_pushvfstring (lua_State *L, const char *fmt,

lua.h

+3
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,9 @@ LUA_API const void *(lua_topointer) (lua_State *L, int idx);
198198
#define LUA_OPROTR 14
199199
#define LUA_OPUNM 15
200200
#define LUA_OPBNOT 16
201+
#define LUA_OPPEEK 17
202+
#define LUA_OPPEEK2 18
203+
#define LUA_OPPEEK4 19
201204

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

luaconf.h

+3-3
Original file line numberDiff line numberDiff line change
@@ -585,9 +585,9 @@
585585
#define luai_numrotl(L,a,b) (l_mathop(rotl)((a),int((b))))
586586
#define luai_numrotr(L,a,b) (l_mathop(rotr)((a),int((b))))
587587
#define luai_numbnot(L,a) (~(a))
588-
#define luai_numpeek(L,a) (lua_peek(L,a,1))
589-
#define luai_numpeek2(L,a) (lua_peek(L,a,2))
590-
#define luai_numpeek4(L,a) (lua_peek(L,a,4))
588+
#define luai_numpeek(L,a) (luaV_peek(L,a,1))
589+
#define luai_numpeek2(L,a) (luaV_peek(L,a,2))
590+
#define luai_numpeek4(L,a) (luaV_peek(L,a,4))
591591

592592
#define lua_number2str(s,n) [&]() { \
593593
int i = sprintf(s, "%1.4f", (double)n); \

lvm.c

+3-3
Original file line numberDiff line numberDiff line change
@@ -220,7 +220,7 @@ static int call_orderTM (lua_State *L, const TValue *p1, const TValue *p2,
220220

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

223-
static z8::fix32 lua_peek(struct lua_State *L, z8::fix32 a, int count)
223+
lua_Number luaV_peek(struct lua_State *L, lua_Number a, int count)
224224
{
225225
unsigned char const *p = G(L)->pico8memory;
226226
int address = int(a) & 0x7fff;
@@ -236,7 +236,7 @@ static z8::fix32 lua_peek(struct lua_State *L, z8::fix32 a, int count)
236236
ret |= PEEK(p, address) << 16;
237237
break;
238238
}
239-
return z8::fix32::frombits(ret);
239+
return lua_Number::frombits(ret);
240240
}
241241

242242

@@ -397,7 +397,7 @@ void luaV_arith (lua_State *L, StkId ra, const TValue *rb,
397397
const TValue *b, *c;
398398
if ((b = luaV_tonumber(rb, &tempb)) != NULL &&
399399
(c = luaV_tonumber(rc, &tempc)) != NULL) {
400-
lua_Number res = luaO_arith(op - TM_ADD + LUA_OPADD, nvalue(b), nvalue(c));
400+
lua_Number res = luaO_arith(L, op - TM_ADD + LUA_OPADD, nvalue(b), nvalue(c));
401401
setnvalue(ra, res);
402402
}
403403
else if (!call_binTM(L, rb, rc, ra, op))

lvm.h

+1
Original file line numberDiff line numberDiff line change
@@ -41,4 +41,5 @@ LUAI_FUNC void luaV_arith (lua_State *L, StkId ra, const TValue *rb,
4141
const TValue *rc, TMS op);
4242
LUAI_FUNC void luaV_objlen (lua_State *L, StkId ra, const TValue *rb);
4343

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

0 commit comments

Comments
 (0)