Skip to content

Commit d9baa97

Browse files
committed
Fix number casting in z8lua, revert rnd() to previous implementation (which works now as intended)
1 parent a98f858 commit d9baa97

File tree

4 files changed

+12
-16
lines changed

4 files changed

+12
-16
lines changed

src/api.c

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -549,7 +549,7 @@ static int pico8_pget(lua_State* L)
549549
int x = (int)fix32_to_int32(luaL_checknumber(L, 1));
550550
int y = (int)fix32_to_int32(luaL_checknumber(L, 2));
551551

552-
lua_pushinteger(L, fix32_from_int32(pget(x, y)));
552+
lua_pushinteger(L, pget(x, y));
553553
return 1;
554554
}
555555

@@ -612,7 +612,7 @@ static int pico8_print(lua_State* L)
612612

613613
pico8_ram[0x5f26] = cursor_x;
614614
pico8_ram[0x5f27] = cursor_y;
615-
lua_pushnumber(L, fix32_from_uint8(cursor_x));
615+
lua_pushnumber(L, cursor_x);
616616

617617
return 1;
618618
}
@@ -793,16 +793,10 @@ static int pico8_rnd(lua_State* L)
793793
{
794794
if (lua_istable(L, 1))
795795
{
796-
lua_Integer len = lua_rawlen(L, 1);
797-
if (len == 0)
798-
{
799-
return 0;
800-
}
801-
802-
fix32_t rnd_value = pico8_random(len << 8);
803-
lua_Integer index = (rnd_value >> 8) + 1;
804796

805-
lua_rawgeti(L, 1, index);
797+
const fix32_t value = pico8_random(lua_rawlen(L, 1) << 8);
798+
lua_pushnumber(L, ((value >> 8) + 1) * fix32_value(1, 0));
799+
lua_gettable(L, -2);
806800
}
807801
else
808802
{

src/z8lua/llimits.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ typedef LUAI_UACNUMBER l_uacNumber;
9393
#define cast(t, exp) ((t)(exp))
9494

9595
#define cast_byte(i) cast(lu_byte, (i))
96-
#define cast_num(i) cast(lua_Number, (i))
96+
#define cast_num(i) fix32_from_double(i)
9797
#define cast_int(i) cast(int, (i))
9898
#define cast_uchar(i) cast(unsigned char, (i))
9999

src/z8lua/lvm.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -380,11 +380,11 @@ void luaV_objlen (lua_State *L, StkId ra, const TValue *rb) {
380380
Table *h = hvalue(rb);
381381
tm = fasttm(L, h->metatable, TM_LEN);
382382
if (tm) break; /* metamethod? break switch to call it */
383-
setnvalue(ra, cast_num(fix32_from_uint32(luaH_getn(h)))); /* else primitive len */
383+
setnvalue(ra, cast_num(luaH_getn(h))); /* else primitive len */
384384
return;
385385
}
386386
case LUA_TSTRING: {
387-
setnvalue(ra, cast_num(fix32_from_uint32(tsvalue(rb)->len)));
387+
setnvalue(ra, cast_num(tsvalue(rb)->len));
388388
return;
389389
}
390390
default: { /* try metamethod */

tests/tests.lua

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,9 @@ end
104104
function test_tables()
105105
local tbl = {0, 1, 1, 2, 3, 5, 8, 13}
106106
log("tbl = {0, 1, 1, 2, 3, 5, 8, 13}")
107-
--assert_equal(tbl[1], 0 "tbl[1]")
107+
assert_equal(tbl[5], 3, "tbl[5]")
108+
assert_equal(tbl[6], 5, "tbl[6]")
109+
assert_equal(tbl[7], 8, "tbl[7]")
108110
end
109111

110112
-- PICO-8 API.
@@ -299,8 +301,8 @@ function run_tests()
299301
test_relational_operations()
300302
test_logical_operations()
301303
test_control_flow()
302-
test_tables()
303304
test_pico8_api()
305+
test_tables()
304306
end
305307

306308
run_tests()

0 commit comments

Comments
 (0)