Skip to content

Commit 1c64d7a

Browse files
committed
Remove srand() and rnd() functions.
It’s a bit sad but since these functions have a state that is not directly accessible to the VM, I am faced with the choice of either exposing that state and patching Lua even more, or implement these functions in the VM rather than in Lua.
1 parent 0f649c5 commit 1c64d7a

File tree

3 files changed

+1
-27
lines changed

3 files changed

+1
-27
lines changed

lpico8lib.c

+1-25
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
//
22
// ZEPTO-8 — Fantasy console emulator
33
//
4-
// Copyright © 2016—2017 Sam Hocevar <[email protected]>
4+
// Copyright © 2016—2020 Sam Hocevar <[email protected]>
55
//
66
// This program is free software. It comes without any warranty, to
77
// the extent permitted by applicable law. You can redistribute it
@@ -24,7 +24,6 @@
2424
#include "lauxlib.h"
2525
#include "llimits.h"
2626
#include "lobject.h"
27-
#include "lstate.h"
2827

2928
#define TAU 6.2831853071795864769252867665590057683936
3029

@@ -198,27 +197,6 @@ static int pico8_ord(lua_State *l) {
198197
return 1;
199198
}
200199

201-
static void update_prng(global_State *g) {
202-
g->prngseed2 = g->prngseed1 + ((g->prngseed2 >> 16) | (g->prngseed2 << 16));
203-
g->prngseed1 += g->prngseed2;
204-
}
205-
206-
static int pico8_srand(lua_State *L) {
207-
uint32_t seed = lua_tonumber(L, 1).bits();
208-
G(L)->prngseed1 = seed ? seed : 0xdeadbeef;
209-
G(L)->prngseed2 = G(L)->prngseed1 ^ 0xbead29ba;
210-
for (int i = 0; i < 32; ++i)
211-
update_prng(G(L));
212-
return 0;
213-
}
214-
215-
static int pico8_rnd(lua_State *L) {
216-
uint32_t range = lua_isnone(L, 1) ? 0x10000 : lua_tonumber(L, 1).bits();
217-
update_prng(G(L));
218-
lua_pushnumber(L, lua_Number::frombits(range ? G(L)->prngseed2 % range : 0));
219-
return 1;
220-
}
221-
222200
static const luaL_Reg pico8lib[] = {
223201
{"max", pico8_max},
224202
{"min", pico8_min},
@@ -244,8 +222,6 @@ static const luaL_Reg pico8lib[] = {
244222
{"tonum", pico8_tonum},
245223
{"chr", pico8_chr},
246224
{"ord", pico8_ord},
247-
{"srand", pico8_srand},
248-
{"rnd", pico8_rnd},
249225
{NULL, NULL}
250226
};
251227

lstate.c

-1
Original file line numberDiff line numberDiff line change
@@ -281,7 +281,6 @@ LUA_API lua_State *lua_newstate (lua_Alloc f, void *ud) {
281281
g->ud = ud;
282282
g->mainthread = L;
283283
g->seed = makeseed(L);
284-
g->prngseed1 = g->prngseed2 = g->seed;
285284
g->uvhead.u.l.prev = &g->uvhead;
286285
g->uvhead.u.l.next = &g->uvhead;
287286
g->gcrunning = 0; /* no GC while building state */

lstate.h

-1
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,6 @@ typedef struct global_State {
119119
stringtable strt; /* hash table for strings */
120120
TValue l_registry;
121121
unsigned int seed; /* randomized seed for hashes */
122-
uint32_t prngseed1, prngseed2; /* randomized seed for PRNG */
123122
lu_byte currentwhite;
124123
lu_byte gcstate; /* state of garbage collector */
125124
lu_byte gckind; /* kind of GC running */

0 commit comments

Comments
 (0)