From 7bddb06461d8f80030b3b86175ec737dbb16ac3b Mon Sep 17 00:00:00 2001 From: Benoit Germain Date: Fri, 4 Jul 2025 14:54:36 +0200 Subject: Added Lua 5.5 support * some unit tests fail/segfault/freeze, but that could be because Lua 5.5 is still in beta yet --- src/compat.cpp | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) (limited to 'src/compat.cpp') diff --git a/src/compat.cpp b/src/compat.cpp index 08f22ca..c833480 100644 --- a/src/compat.cpp +++ b/src/compat.cpp @@ -149,3 +149,41 @@ int lua_setiuservalue(lua_State* const L_, StackIndex const idx_, UserValueIndex } #endif // LUA_VERSION_NUM + +// ################################################################################################# + +#if LUA_VERSION_NUM < 505 + +#include + +/* Size for the buffer, in bytes */ +#define BUFSEEDB (sizeof(void*) + sizeof(time_t)) + +/* Size for the buffer in int's, rounded up */ +#define BUFSEED ((BUFSEEDB + sizeof(int) - 1) / sizeof(int)) + +/* +** Copy the contents of variable 'v' into the buffer pointed by 'b'. +** (The '&b[0]' disguises 'b' to fix an absurd warning from clang.) +*/ +#define addbuff(b, v) (memcpy(&b[0], &(v), sizeof(v)), b += sizeof(v)) + +// Copied from Lua 5.5 lauxlib.c +unsigned int luaL_makeseed(lua_State*) +{ + unsigned int buff[BUFSEED]; + unsigned int res; + unsigned int i; + time_t t = time(nullptr); + char* b = (char*) buff; + addbuff(b, b); /* local variable's address */ + addbuff(b, t); /* time */ + /* fill (rare but possible) remain of the buffer with zeros */ + memset(b, 0, sizeof(buff) - BUFSEEDB); + res = buff[0]; + for (i = 1; i < BUFSEED; i++) + res ^= (res >> 3) + (res << 7) + buff[i]; + return res; +} + +#endif // LUA_VERSION_NUM < 505 -- cgit v1.2.3-55-g6feb