diff options
Diffstat (limited to 'src/compat.cpp')
| -rw-r--r-- | src/compat.cpp | 38 |
1 files changed, 38 insertions, 0 deletions
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 | |||
| 149 | } | 149 | } |
| 150 | 150 | ||
| 151 | #endif // LUA_VERSION_NUM | 151 | #endif // LUA_VERSION_NUM |
| 152 | |||
| 153 | // ################################################################################################# | ||
| 154 | |||
| 155 | #if LUA_VERSION_NUM < 505 | ||
| 156 | |||
| 157 | #include <time.h> | ||
| 158 | |||
| 159 | /* Size for the buffer, in bytes */ | ||
| 160 | #define BUFSEEDB (sizeof(void*) + sizeof(time_t)) | ||
| 161 | |||
| 162 | /* Size for the buffer in int's, rounded up */ | ||
| 163 | #define BUFSEED ((BUFSEEDB + sizeof(int) - 1) / sizeof(int)) | ||
| 164 | |||
| 165 | /* | ||
| 166 | ** Copy the contents of variable 'v' into the buffer pointed by 'b'. | ||
| 167 | ** (The '&b[0]' disguises 'b' to fix an absurd warning from clang.) | ||
| 168 | */ | ||
| 169 | #define addbuff(b, v) (memcpy(&b[0], &(v), sizeof(v)), b += sizeof(v)) | ||
| 170 | |||
| 171 | // Copied from Lua 5.5 lauxlib.c | ||
| 172 | unsigned int luaL_makeseed(lua_State*) | ||
| 173 | { | ||
| 174 | unsigned int buff[BUFSEED]; | ||
| 175 | unsigned int res; | ||
| 176 | unsigned int i; | ||
| 177 | time_t t = time(nullptr); | ||
| 178 | char* b = (char*) buff; | ||
| 179 | addbuff(b, b); /* local variable's address */ | ||
| 180 | addbuff(b, t); /* time */ | ||
| 181 | /* fill (rare but possible) remain of the buffer with zeros */ | ||
| 182 | memset(b, 0, sizeof(buff) - BUFSEEDB); | ||
| 183 | res = buff[0]; | ||
| 184 | for (i = 1; i < BUFSEED; i++) | ||
| 185 | res ^= (res >> 3) + (res << 7) + buff[i]; | ||
| 186 | return res; | ||
| 187 | } | ||
| 188 | |||
| 189 | #endif // LUA_VERSION_NUM < 505 | ||
